/usr/include/dlib/matrix/matrix_abstract.h is in libdlib-dev 18.18-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 | // Copyright (C) 2006 Davis E. King (davis@dlib.net)
// License: Boost Software License See LICENSE.txt for the full license.
#undef DLIB_MATRIx_ABSTRACT_
#ifdef DLIB_MATRIx_ABSTRACT_
#include "matrix_exp_abstract.h"
#include "../serialize.h"
#include "../algs.h"
#include "matrix_data_layout_abstract.h"
namespace dlib
{
// ----------------------------------------------------------------------------------------
/*
Note that these operator prototypes are not correct C++ (the real versions, which
you can see in the implementation are really complex and so probably would
distract/confuse people if shown here). Think of this as just a list of the
operators available to you and what they do.
*/
const matrix_exp operator* (
const matrix_exp& m1,
const matrix_exp& m2
);
/*!
requires
- m1.nc() == m2.nr()
- m1.size() > 0 && m2.size() > 0
(you can't multiply any sort of empty matrices together)
- m1 and m2 both contain elements of the same type
ensures
- returns the result of doing the matrix multiplication m1*m2. The resulting
matrix will have m1.nr() rows and m2.nc() columns.
!*/
const matrix_exp operator+ (
const matrix_exp& m1,
const matrix_exp& m2
);
/*!
requires
- m1.nr() == m2.nr()
- m1.nc() == m2.nc()
- m1 and m2 both contain elements of the same type
ensures
- returns a matrix R such that for all valid r and c:
R(r,c) == m1(r,c) + m2(r,c)
(i.e. returns the result of doing a pairwise addition of the matrices m1 and m2.)
The resulting matrix will have the same dimensions as the originals.
!*/
const matrix_exp operator- (
const matrix_exp& m1,
const matrix_exp& m2
);
/*!
requires
- m1.nr() == m2.nr()
- m1.nc() == m2.nc()
- m1 and m2 both contain elements of the same type
ensures
- returns a matrix R such that for all valid r and c:
R(r,c) == m1(r,c) - m2(r,c)
(i.e. returns the result of doing a pairwise subtraction of the matrices m1 and m2.)
The resulting matrix will have the same dimensions as the originals.
!*/
template <typename T>
const matrix_exp operator* (
const matrix_exp& m,
const T& value
);
/*!
ensures
- returns the result of multiplying all the elements of matrix m by the given
scalar value. The resulting matrix will have the same dimensions as m.
!*/
template <typename T>
const matrix_exp operator* (
const T& value,
const matrix_exp& m
);
/*!
ensures
- returns the result of multiplying all the elements of matrix m by the given
scalar value. The resulting matrix will have the same dimensions as m.
!*/
const matrix_exp operator- (
const matrix_exp& m
);
/*!
ensures
- returns -1*m
!*/
template <typename T>
const matrix_exp operator/ (
const matrix_exp& m,
const T& value
);
/*!
ensures
- returns the result of dividing all the elements of matrix m by the given
scalar value. The resulting matrix will have the same dimensions as m.
!*/
template <typename T>
const matrix_exp operator/ (
const T& value,
const matrix_exp& m
);
/*!
ensures
- returns the result of dividing the given scalar value by all the elements
of matrix m. The resulting matrix will have the same dimensions as m.
!*/
template <typename T>
const matrix_exp operator+ (
const matrix_exp& m,
const T& value
);
/*!
ensures
- returns the result of adding value to all the elements of matrix m.
The resulting matrix will have the same dimensions as m.
!*/
template <typename T>
const matrix_exp operator+ (
const T& value,
const matrix_exp& m
);
/*!
ensures
- returns the result of adding value to all the elements of matrix m.
The resulting matrix will have the same dimensions as m.
!*/
template <typename T>
const matrix_exp operator- (
const matrix_exp& m,
const T& value
);
/*!
ensures
- returns the result of subtracting value from all the elements of matrix m.
The resulting matrix will have the same dimensions as m.
!*/
template <typename T>
const matrix_exp operator- (
const T& value,
const matrix_exp& m
);
/*!
ensures
- Returns a matrix M such that:
- M has the same dimensions as m
- M contains the same type of element as m
- for all valid r and c:
- M(r,c) == value - m(r,c)
!*/
bool operator== (
const matrix_exp& m1,
const matrix_exp& m2
);
/*!
ensures
- if (m1.nr() == m2.nr() && m1.nc() == m2.nc() &&
for all valid r and c: m1(r,c) == m2(r,c) ) then
- returns true
- else
- returns false
!*/
bool operator!= (
const matrix_exp& m1,
const matrix_exp& m2
);
/*!
ensures
- returns !(m1 == m2)
!*/
// ----------------------------------------------------------------------------------------
template <
typename T,
long num_rows = 0,
long num_cols = 0,
typename mem_manager = default_memory_manager,
typename layout = row_major_layout
>
class matrix : public matrix_exp<matrix<T,num_rows,num_cols,mem_manager,layout> >
{
/*!
REQUIREMENTS ON num_rows and num_cols
both must be bigger than or equal to 0
REQUIREMENTS ON mem_manager
must be an implementation of memory_manager/memory_manager_kernel_abstract.h or
must be an implementation of memory_manager_global/memory_manager_global_kernel_abstract.h or
must be an implementation of memory_manager_stateless/memory_manager_stateless_kernel_abstract.h
mem_manager::type can be set to anything.
REQUIREMENTS ON layout
Must be either row_major_layout or column_major_layout
INITIAL VALUE
- if (num_rows > 0) then
- nr() == num_rows
- else
- nr() == 0
- if (num_cols > 0) then
- nc() == num_cols
- else
- nc() == 0
WHAT THIS OBJECT REPRESENTS
This object represents a matrix of nr() rows and nc() columns. This object
is also a matrix_exp. Thus it can be used in all of the above
global operators.
The number of rows and columns of this object are determined by the template
arguments num_rows and num_cols. If num_rows or num_cols are 0 then
the matrix starts out empty (i.e. nr() == 0 and nc() == 0) and you may change
its size via the set_size() member function.
Setting num_rows or num_cols to something other than 0 causes that dimension
to have a fixed size. Setting a fixed size at compile time is useful because
any errors related to operating on matrices with incompatible dimensions will
be detected at compile time. It also allows the compiler to perform loop
unrolling which can result in substantially faster code.
Also note that the elements of this matrix are laid out in memory by the layout
object supplied as a template argument to this class. The row_major_layout
sets elements down contiguously in memory and in row major order. Additionally,
all memory allocations are performed using the memory manager object supplied as
a template argument to this class.
!*/
public:
typedef T type;
typedef mem_manager mem_manager_type;
typedef layout layout_type;
const static long NR = num_rows;
const static long NC = num_cols;
const static long cost = 1;
typedef T* iterator;
typedef const T* const_iterator;
matrix (
);
/*!
ensures
- #*this is properly initialized
- #aliases(*this) == true
- #ref().aliases(*this) == true
!*/
explicit matrix (
long length
);
/*!
requires
- NR == 1 || NC == 1 (i.e. this must be a column or row vector)
- length >= 0
- if (NR == 1 && NC > 0) then
- length == NC
- if (NC == 1 && NR > 0) then
- length == NR
ensures
- #*this is properly initialized
- #aliases(*this) == true
- #ref().aliases(*this) == true
- if (NR == 1) then
- #nr() == 1
- #nc() == length
- else
- #nr() == length
- #nc() == 1
!*/
matrix (
long rows,
long cols
);
/*!
requires
- rows == NR || NR == 0
- cols == NC || NC == 0
- rows >= 0 && cols >= 0
ensures
- #*this is properly initialized
- #aliases(*this) == true
- #ref().aliases(*this) == true
- #nr() == rows
- #nc() == cols
!*/
template <typename EXP>
matrix (
const matrix_exp<EXP>& m
);
/*!
requires
- matrix_exp<EXP>::type == T
(i.e. m contains the same type as *this does)
- if (NR != 0) then NR == m.nr()
- if (NC != 0) then NC == m.nc()
ensures
- #*this == m
- #aliases(*this) == true
- #ref().aliases(*this) == true
!*/
template <typename U, size_t len>
explicit matrix (
U (&array)[len]
);
/*!
requires
- NR != 0 && NC != 0 (i.e. you can only use this constructor on statically sized matrices)
- len == nr()*nc() (i.e. the array you give here must be the right size)
ensures
- for all valid r and c:
#(*this)(r,c) == array[r*nc() + c]
(i.e. initializes this matrix with the contents of the given array)
- #aliases(*this) == true
- #ref().aliases(*this) == true
!*/
T& operator() (
long r,
long c
);
/*!
requires
- 0 <= r < nr()
- 0 <= c < nc()
ensures
- returns a reference to the value at the given row and column in
this matrix.
!*/
const T& operator() (
long r,
long c
) const;
/*!
requires
- 0 <= r < nr()
- 0 <= c < nc()
ensures
- returns a const reference to the value at the given row and column in
this matrix.
!*/
T& operator() (
long i
);
/*!
requires
- nc() == 1 || nr() == 1 (i.e. this must be a column or row vector)
- 0 <= i < size()
ensures
- if (nc() == 1) then
- returns a reference to (*this)(i,0)
- else
- returns a reference to (*this)(0,i)
!*/
const T& operator() (
long i
) const;
/*!
requires
- nc() == 1 || nr() == 1 (i.e. this must be a column or row vector)
- 0 <= i < size()
ensures
- if (nc() == 1) then
- returns a reference to (*this)(i,0)
- else
- returns a reference to (*this)(0,i)
!*/
operator const type (
) const;
/*!
requires
- nr() == 1
- nc() == 1
ensures
- returns (*this)(0,0)
!*/
long nr(
) const;
/*!
ensures
- returns the number of rows in this matrix
!*/
long nc(
) const;
/*!
ensures
- returns the number of columns in this matrix
!*/
long size (
) const;
/*!
ensures
- returns nr()*nc()
!*/
void set_size (
long rows,
long cols
);
/*!
requires
- rows == NR || NR == 0
- cols == NC || NC == 0
- rows >= 0 && cols >= 0
ensures
- #nr() == rows
- #nc() == cols
!*/
void set_size (
long length
);
/*!
requires
- NR == 1 || NC == 1 (i.e. this must be a column or row vector)
- length >= 0
- if (NR == 1 && NC > 0) then
- length == NC
- if (NC == 1 && NR > 0) then
- length == NR
ensures
- if (NR == 1) then
- #nr() == 1
- #nc() == length
- else
- #nr() == length
- #nc() == 1
!*/
template <typename U, size_t len>
matrix& operator= (
U (&array)[len]
);
/*!
requires
- len == nr()*nc() (i.e. the array you give here must be the right size)
ensures
- for all valid r and c:
#(*this)(r,c) == array[r*nc() + c]
(i.e. loads this matrix with the contents of the given array)
- returns *this
!*/
template <typename EXP>
matrix& operator= (
const matrix_exp<EXP>& m
);
/*!
requires
- matrix_exp<EXP>::type == T
(i.e. m contains the same type as *this does)
- if (NR != 0) then NR == m.nr()
- if (NC != 0) then NC == m.nc()
ensures
- copies the given matrix expression m to *this
- returns *this
!*/
template <typename EXP>
matrix& operator += (
const matrix_exp<EXP>& m
);
/*!
requires
- matrix_exp<EXP>::type == T
ensures
- if (nr() == m.nr() && nc() == m.nc()) then
- #(*this) == *this + m
- else
- #(*this) == m
(i.e. if the dimensions don't match then this function performs a
normal assignment)
- returns *this
!*/
template <typename EXP>
matrix& operator -= (
const matrix_exp<EXP>& m
);
/*!
requires
- matrix_exp<EXP>::type == T
ensures
- if (nr() == m.nr() && nc() == m.nc()) then
- #(*this) == *this - m
- else
- #(*this) == -m
- returns *this
!*/
template <typename EXP>
matrix& operator *= (
const matrix_exp<EXP>& m
);
/*!
requires
- matrix_exp<EXP>::type == T
(i.e. m must contain the same type of element as *this)
- nc() == m.nr()
- size() > 0 && m.size() > 0
(you can't multiply any sort of empty matrices together)
ensures
- #(*this) == *this * m
- returns *this
!*/
matrix& operator *= (
const T& a
);
/*!
ensures
- #(*this) == *this * a
- returns *this
!*/
matrix& operator /= (
const T& a
);
/*!
ensures
- #(*this) == *this / a
- returns *this
!*/
matrix& operator += (
const T& a
);
/*!
ensures
- #(*this) == *this + a
- returns *this
!*/
matrix& operator -= (
const T& a
);
/*!
ensures
- #(*this) == *this - a
- returns *this
!*/
const literal_assign_helper operator = (
const T& val
);
/*!
This function is somewhat different than all the others defined in this file.
The purpose of this function is to enable you to easily initialize a matrix object.
For example:
matrix<double> m(2,3);
m = 1,2,3,
4,5,6;
The above code creates a matrix m with 2 rows and 3 columns and sets it so that
it contains the matrix | 1 2 3 |
| 4 5 6 |
You can also use this function to assign to all elements of a matrix. So
saying m = 3; would assign all elements of m equal to 3.
Note that to use this method of assignment it is required that you supply
exactly m.size() or 1 values so that the matrix is fully initialized. Supplying
fewer or more than that is an error that will cause a dlib::fatal_error to be
thrown.
Note also that using an expression of the form m = scalar; when m.size() == 0
is legal but has no effect on m.
!*/
void swap (
matrix& item
);
/*!
ensures
- swaps *this and item
!*/
iterator begin(
);
/*!
ensures
- returns a random access iterator pointing to the first element in this
matrix.
- The iterator will iterate over the elements of the matrix in row major
order if layout is row_major_layout or in column major order if layout is
column_major_layout.
!*/
iterator end(
);
/*!
ensures
- returns a random access iterator pointing to one past the end of the last
element in this matrix.
!*/
const_iterator begin(
) const;
/*!
ensures
- returns a random access iterator pointing to the first element in this
matrix.
- The iterator will iterate over the elements of the matrix in row major
order if layout is row_major_layout or in column major order if layout is
column_major_layout.
!*/
const_iterator end(
) const;
/*!
ensures
- returns a random access iterator pointing to one past the end of the last
element in this matrix.
!*/
};
// ----------------------------------------------------------------------------------------
template <
typename T,
long NR,
long NC,
typename mm,
typename l
>
void swap(
matrix<T,NR,NC,mm,l>& a,
matrix<T,NR,NC,mm,l>& b
) { a.swap(b); }
/*!
Provides a global swap function
!*/
template <
typename T,
long NR,
long NC,
typename mm,
typename l
>
void serialize (
const matrix<T,NR,NC,mm,l>& item,
std::ostream& out
);
/*!
Provides serialization support. Note that the serialization formats used by the
dlib::matrix and dlib::array2d objects are compatible. That means you can load the
serialized data from one into another and it will work properly.
!*/
template <
typename T,
long NR,
long NC,
typename mm,
typename l
>
void deserialize (
matrix<T,NR,NC,mm,l>& item,
std::istream& in
);
/*!
Provides deserialization support
!*/
template <
typename EXP
>
std::ostream& operator<< (
std::ostream& out,
const matrix_exp<EXP>& m
);
/*!
ensures
- writes m to the given out stream in a form suitable for human consumption.
- returns out
!*/
template <
typename T,
long NR,
long NC,
typename MM,
typename L
>
std::istream& operator>> (
std::istream& in,
matrix<T,NR,NC,MM,L>& m
);
/*!
ensures
- Tries to read a matrix from the given input stream and store it into #m.
- The format expected is the text format output by the above operator<<().
That is, the format should be a grid of text such as:
2 3 4
5 2 6
- The separation between numbers can be any number of whitespace characters or
commas.
- The matrix data is assumed to end upon the first blank line or end-of-file,
whichever comes first. This means you can create an input stream with
multiple matrices in it by separating them with empty lines.
- returns in.
- If there was a formatting error or something which prevents the input data
from being parsed into a matrix then #in.fail() == true.
!*/
/*!A csv
This object is used to define an io manipulator for matrix expressions. In
particular, you can write statements like:
cout << csv << yourmatrix;
and have it print the matrix with commas separating each element.
!*/
some_undefined_iomnaip_type csv;
// ----------------------------------------------------------------------------------------
template <typename EXP>
class const_temp_matrix : public matrix_exp<const_temp_matrix<EXP> >, noncopyable
{
/*!
REQUIREMENTS ON EXP
- must be an object that inherits publicly from matrix_exp.
WHAT THIS OBJECT REPRESENTS
This object represents a copy of a matrix expression. The twist
is that it only actually makes a copy of its input matrix expression
if that matrix expression is costly to evaluate. If it has
low cost then this object just stores a reference.
This class is useful in cases where you write a function that
takes a matrix_exp object as input and you want to do some
intensive computation that looks at each element of that matrix_exp
many times. If the input matrix_exp has a high cost then you want
to store it into a temporary matrix. But if it has low cost then
it is faster if you just use a reference to it. The const_temp_matrix
makes doing this easy.
!*/
public:
const_temp_matrix (
const matrix_exp<EXP>& item
);
/*!
ensures
- #*this == item
- if (EXP::cost <= 1) then
- this const_temp_matrix stores a reference to the item matrix
- else
- this const_temp_matrix creates a temporary matrix and copies
item into it
!*/
const_temp_matrix (
const EXP& item
);
/*!
ensures
- #*this == item
- if (EXP::cost <= 1) then
- this const_temp_matrix stores a reference to the item matrix
- else
- this const_temp_matrix creates a temporary matrix and copies
item into it
!*/
};
// ----------------------------------------------------------------------------------------
}
#endif // DLIB_MATRIx_ABSTRACT_
|