/usr/include/trilinos/DenseLinAlgPack_DMatrixClass.hpp is in libtrilinos-dev 10.4.0.dfsg-1ubuntu2.
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 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 | // @HEADER
// ***********************************************************************
//
// Moocho: Multi-functional Object-Oriented arCHitecture for Optimization
// Copyright (2003) Sandia Corporation
//
// Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
// license for use of this work by or on behalf of the U.S. Government.
//
// This library is free software; you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as
// published by the Free Software Foundation; either version 2.1 of the
// License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
// USA
// Questions? Contact Roscoe A. Bartlett (rabartl@sandia.gov)
//
// ***********************************************************************
// @HEADER
//
// General matrix and matrix region (slice) classes
#ifndef GEN_MATRIX_CLASS_H
#define GEN_MATRIX_CLASS_H
#include "DenseLinAlgPack_DVectorClass.hpp"
#include "DenseLinAlgPack_DMatrixAssign.hpp"
/* * @name {\bf Dense 2-D Rectangular Matrix Absractions}.
*
* The class DMatrix is a storage class for 2-D matrices while the class DMatrixSlice
* is used to represent rectangular regions of a DMatrix object.
*/
// @{
// begin General Rectangular 2-D Matrices scope
namespace DenseLinAlgPack {
class DMatrix;
/* * @name {\bf General Matrix Classes}. */
// @{
// ////////////////////////////////////////////////////////////////////////////////////////////////////////
// GenMatrixClass
//
/** \brief . */
/* * 2-D General Rectangular Matrix Subregion Class (Slice) (column major).
*
* This class is used to represent a rectangular matrix. It uses a BLAS-like
* slice of a raw C++ array. Objects of this class can represent
* an entire matrix or any rectangular subregion of a matrix.
*/
class DMatrixSlice {
public:
/* * @name {\bf Nested Member Types (STL)}.
*
* These nested types give the types used in the interface to the class.
*
* \begin{description}
* <li>[#value_type#] - type being stored in the underlying valarray<>
* <li>[#size_type#] - type for the number rows and coluns
* <li>[#difference_type#] - type for the stride between elements (in a row or column)
* <li>[#reference#] - value_type&
* <li>[#const_reference#] - const value_type&
* \end{description}
*/
// @{
// @}
typedef DenseLinAlgPack::value_type value_type;
typedef DenseLinAlgPack::size_type size_type;
typedef ptrdiff_t difference_type;
typedef value_type& reference;
typedef const value_type& const_reference;
/* * @name {\bf Constructors}.
*
* These constructors are used by the other entities in the library
* to create DMatrixSlices. In general user need not use these
* constructors directly. Instead, the general user should use the
* subscript operators to create subregions of DMatrix and DMatrixSlice
* objects.
*
* The default copy constructor is used and is therefore not shown here.
*/
// @{
///
/* * Construct an empty view.
*
* The client can then call bind(...) to bind the view.
*/
DMatrixSlice();
///
/* * Construct a veiw of a matrix from a raw C++ array.
*
* The DMatrixSlice constructed represents a 2-D matrix whos elements are stored
* in the array starting at #ptr#. This is how the BLAS represent general rectangular
* matrices.
* The class can be used to provide a non-constant view the elements (#DMatrix#)
* or a constant view (#const DMatrixSlice#). Here is an example of how to
* create a constant view:
*
\verbatim
const DMatrixSlice::size_type m = 4, n = 4;
const DMatrixSlice::value_type ptr[m*n] = { ... };
const GenMatrixslice mat(cosnt_cast<DMatrixSlice::value_type*>(ptr),m*n,m,m,n);
\endverbatim
*
* The #const_cast<...># such as in the example above is perfectly safe to use
* when constructing #const# veiw of #const# data. On the other hand casting
* away #const# and then non-#const# use is not safe in general since the original
* #const# data may reside in ROM for example. By using a non-#const# pointer in the
* constructor you avoid accidentally making a non-#const# view of #const# data.
*
* Preconditions: <ul>
* <li> #rows <= max_rows# (throw out_of_range)
* <li> #start + rows + max_rows * (cols - 1) <= v.size()# (throw out_of_range)
* </ul>
*
* @param ptr pointer to the storage of the elements of the matrix (column oriented).
* @param size total size of the storage pointed to by #ptr# (for size checking)
* @param max_rows number of rows in the full matrix this sub-matrix was taken from.
* This is equivalent to the leading dimension argument (LDA) in
* the BLAS.
* @param rows number of rows this matrix represents
* @param cols number of columns this matrix represents
*/
DMatrixSlice( value_type* ptr, size_type size
, size_type max_rows, size_type rows, size_type cols );
///
/* * Construct a submatrix region of another DMatrixSlice.
*
* This constructor simplifies the creation of subregions using the subscript
* operators.
*
* I and J must be bounded ranges (full_range() == false).
*
* Preconditions: <ul>
* <li> #I.full_range() == false# (throw out_of_range)
* <li> #J.full_range() == false# (throw out_of_range)
* <li> #I.ubound() <= gms.rows()# (throw out_of_range)
* <li> #J.ubound() <= gms.cols()# (throw out_of_range)
* </ul>
*/
DMatrixSlice( DMatrixSlice& gms, const Range1D& I
, const Range1D& J );
// @}
/** \brief . */
/* * Set to the view of the input DMatrixSlice.
*
*
*/
void bind( DMatrixSlice gms );
/* * @name {\bf Dimensionality, Misc}. */
// @{
/// Return the number of rows
size_type rows() const;
/// Return the number of columns
size_type cols() const;
///
/* * Returns the degree of memory overlap of #this# and the DMatrixSlice object #gms#.
*
* @return
* \begin{description}
* <li>[NO_OVERLAP] There is no memory overlap between #this# and #gms#.
* <li>[SOME_OVERLAP] There is some memory locations that #this# and #gms# share.
* <li>[SAME_MEM] The DMatrixSlice objects #this# and #gms# share the exact same memory locations.
* \end{description}
*/
EOverLap overlap(const DMatrixSlice& gms) const;
// @}
/* * @name {\bf Individual Element Access Subscripting (lvalue)}. */
// @{
/// Return element at row i, col j (i,j) (1-based) (throws std::out_of_range if i, j are out of bounds)
reference operator()(size_type i, size_type j);
/// Return element at row i, col j (i,j) (1-based) (throws std::out_of_range if i, j are out of bounds)
const_reference operator()(size_type i, size_type j) const;
// @}
/* * @name {\bf Subregion Access (1-based)}.
*
* These member functions allow access to subregions of the DMatrixSlice object.
* The functions, row(i), col(j), and diag(k) return DVectorSlice objects while
* the subscripting operators opeator()(I,J) return DMatrixSlice objects for
* rectangular subregions.
*/
// @{
/// Return DVectorSlice object representing the ith row (1-based; 1,2,..,#this->rows()#, or throw std::out_of_range)
DVectorSlice row(size_type i);
/// Same as above
const DVectorSlice row(size_type i) const;
/// Return DVectorSlice object representing the jth column (1-based; 1,2,..,#this->cols()#, or throw std::out_of_range)
DVectorSlice col(size_type j);
/// Same as above
const DVectorSlice col(size_type j) const;
///
/* * Return DVectorSlice object representing a diagonal.
*
* Passing k == 0 returns the center diagonal. Values of k < 0 are the lower diagonals
* (k = -1, -2, ..., -#this->rows()# + 1). Values of k > 0 are the upper diagonals
* (k = 1, 2, ..., #this->cols()# - 1).
*
* Preconditions: <ul>
* <li> #[k < 0] k <= this->rows() + 1# (throw out_of_range)
* <li> #[k > 0] k <= this->cols() + 1# (throw out_of_range)
* </ul>
*/
DVectorSlice diag(difference_type k = 0);
/// Same as above.
const DVectorSlice diag(difference_type k = 0) const;
///
/* * Extract a rectangular subregion containing rows I, and columns J.
*
* This operator function returns a DMatrixSlice that represents a
* rectangular region of this DMatrixSlice. This submatrix region
* represents the rows I.lbound() to I.ubound() and columns J.lbound()
* to J.lbound(). If I or J is unbounded (full_range() == true, constructed
* with Range1D()), the all of the rows or columns respectively will be
* selected. For example. To select all the rows and the first 5 columns of
* a matrix #A# you would use #A(Range1D(),Range1D(1,5))#.
*
* Preconditions: <ul>
* <li> #[I.full_range() == false] I.ubound() <= this->rows()# (throw out_of_range)
* <li> #[J.full_range() == false] J.ubound() <= this->cols()# (throw out_of_range)
* </ul>
*/
DMatrixSlice operator()(const Range1D& I, const Range1D& J);
/// Same as above.
const DMatrixSlice operator()(const Range1D& I, const Range1D& J) const;
///
/* * Extract a rectangular subregion containing rows i1 to i2, and columns j1 to j2.
*
* This operator function returns a DMatrixSlice that represents a
* rectangular region of this DMatrixSlice. This submatrix region
* represents the rows i1 to 12 and colunms j1 to j2.
*
* Preconditions: <ul>
* <li> #i1 <= i2# (throw out_of_range)
* <li> #i2 <= this->rows()# (throw out_of_range)
* <li> #j1 <= j2# (throw out_of_range)
* <li> #j2 <= this->cols()# (throw out_of_range)
* </ul>
*/
DMatrixSlice operator()(size_type i1, size_type i2, size_type j1
, size_type j2);
/// Same as above.
const DMatrixSlice operator()(size_type i1, size_type i2, size_type j1
, size_type j2) const;
/// Allow the address to be taken of an rvalue of this object.
DMatrixSlice* operator&() {
return this;
}
/** \brief . */
const DMatrixSlice* operator&() const {
return this;
}
/// Return reference of this. Included for iniformity with DMatrix
DMatrixSlice& operator()();
/// Same as above
const DMatrixSlice& operator()() const;
// @}
/* * @name {\bf Assignment operators}. */
// @{
/** \brief . */
/* * Sets all elements = alpha
*
* If the underlying valarray is unsized (#this->v().size() == 0#) the matrix is sized to 1 x 1
* and the single element is set to alpha.
*
* Postcondtions: <ul>
* <li> #this->operator()(i,j) == alpha#, i = 1,2,...,#this->rows()#, j = 1,2,...,#this->cols()#
* </ul>
*/
DMatrixSlice& operator=(value_type alpha);
/** \brief . */
/* * Copies all of the elements of the DMatrixSlice, #rhs#, into the elements of #this#.
*
* If the underlying valarray is unsized (#this->v().size() == 0#) the matrix is sized to
* the size of the rhs matrix.
*
* Precondtions: <ul>
* <li> #this->rows() == gms_rhs.rows()# (throw length_error)
* <li> #this->cols() == gms_rhs.cols()# (throw length_error)
* </ul>
*
* Postcondtions: <ul>
* <li> #this->operator()(i,j) == gms_rhs(i,j)#, i = 1,2,...,#this->rows()#, j = 1,2,...,#this->cols()#
* </ul>
*/
DMatrixSlice& operator=(const DMatrixSlice& gms_rhs);
// @}
/* * @name {\bf Raw data access}.
*/
// @{
/// Return the number of rows in the full matrix. Equivalent to BLAS LDA argument.
size_type max_rows() const;
/** \brief . */
/* * Return pointer to the first element in the underlying array the jth
* col (j is 1-based here [1,cols]). If unsized col_ptr(1) returns zero if unsized.
*/
value_type* col_ptr(size_type j);
/// Same as above.
const value_type* col_ptr(size_type j) const;
// @}
private:
value_type *ptr_; // contains the data for the matrix region
size_type max_rows_, // the number of rows in the full matrix
rows_, // the number of rows in this matrix region
cols_; // the number of cols in this matrix region
// Assert the row subscript is in bounds (1-based), (throw std::out_of_range)
void validate_row_subscript(size_type i) const;
// Assert the column subscript is in bounds (1-based), (throw std::out_of_range)
void validate_col_subscript(size_type j) const;
// Assert that a constructed DMatrixSlice has a valid range, (throw std::out_of_range)
void validate_setup(size_type size) const;
// Get a diagonal
DVectorSlice p_diag(difference_type k) const;
}; // end class DMatrixSlice
/** \brief . */
/* * 2-D General Rectangular Matrix (column major) Storage Class.
*
* This class provides the storage for 2-D rectangular matrices.
*/
class DMatrix {
public:
/* * @name {\bf Nested Member Types (STL)}.
*
* These nested types give the types used in the interface to the class.
*
* \begin{description}
* <li>[#value_type#] type being stored in the underlying valarray<>
* <li>[#size_type#] type for the number rows and coluns
* <li>[#difference_type#] type for the stride between elements (in a row or column)
* <li>[#reference#] value_type&
* <li>[#const_reference#] const value_type&
* \end{description}
*/
// @{
// @}
typedef DenseLinAlgPack::value_type value_type;
typedef DenseLinAlgPack::size_type size_type;
typedef ptrdiff_t difference_type;
typedef value_type& reference;
typedef const value_type& const_reference;
typedef std::valarray<value_type> valarray;
/* * @name {\bf Constructors}.
*
* The general user uses these constructors to create a matrix.
*
* The default constructor is used and is therefore not shown here.
*/
// @{
/// Construct a matrix with rows = cols = 0
DMatrix();
/// Construct an uninitialied rectangular matrix (rows x cols)
explicit DMatrix(size_type rows, size_type cols);
/** \brief . */
/* * Construct rectangular matrix (rows x cols) with elements initialized to val.
*
* Postconditions: <ul>
* <li> #this->operator()(i,j) == val#, i = 1,2,...,#rows#, j = 1,2,...,#cols#
* </ul>
*/
explicit DMatrix(value_type val, size_type rows, size_type cols);
///
/* * Construct rectangular matrix (rows x cols) initialized to elements of p (by column).
*
* Postconditions: <ul>
* <li> #this->operator()(i,j) == p[i-1 + rows * (j - 1)]#, i = 1,2,...,#rows#, j = 1,2,...,#cols#
* </ul>
*/
explicit DMatrix(const value_type* p, size_type rows, size_type cols);
/** \brief . */
/* * Construct a matrix from the elements in another DMatrixSlice, #gms#.
*
* Postconditions: <ul>
* <li> #this->operator()(i,j) == gms(i,j)#, i = 1,2,...,#rows#, j = 1,2,...,#cols#
* </ul>
*/
DMatrix(const DMatrixSlice& gms);
// @}
/* * @name {\bf Memory Management, Dimensionality, Misc}. */
// @{
/// Resize matrix to a (rows x cols) matrix and initializes any added elements by val
void resize(size_type rows, size_type cols, value_type val = value_type());
/// frees memory and leaves a (0 x 0) matrix
void free();
/// Return the number of rows
size_type rows() const;
/// Return the number of columns
size_type cols() const;
///
/* * Returns the degree of memory overlap of #this# and the DMatrixSlice object #gms#.
*
* @return
* \begin{description}
* <li>[NO_OVERLAP] There is no memory overlap between #this# and #gms#.
* <li>[SOME_OVERLAP] There is some memory locations that #this# and #gms# share.
* <li>[SAME_MEM] The DMatrixSlice objects #this# and #gms# share the exact same memory locations.
* \end{description}
*/
EOverLap overlap(const DMatrixSlice& gms) const;
// @}
/* * @name {\bf Individual Element Access Subscripting (lvalue)}. */
// @{
/// Return element at row i, col j (i,j) (1-based)
reference operator()(size_type i, size_type j);
/// Return element at row i, col j (i,j) (1-based)
const_reference operator()(size_type i, size_type j) const;
// @}
/* * @name {\bf Subregion Access (1-based)}.
*
* These member functions allow access to subregions of the DMatrix object.
* The functions, row(i), col(j), and diag(k) return DVectorSlice objects while
* the subscripting operators opeator()(I,J) return DMatrixSlice objects for
* rectangular subregions.
*/
// @{
/// Return DVectorSlice object representing the ith row (1-based; 1,2,..,#this->rows()#)
DVectorSlice row(size_type i);
/** \brief . */
const DVectorSlice row(size_type i) const;
/// Return DVectorSlice object representing the jth column (1-based; 1,2,..,#this->cols()#)
DVectorSlice col(size_type j);
/** \brief . */
const DVectorSlice col(size_type j) const;
///
/* * Return DVectorSlice object representing a diagonal.
*
* Passing k == 0 returns the center diagonal. Values of k < 0 are the lower diagonals
* (k = -1, -2, ..., #this->rows()# - 1). Values of k > 0 are the upper diagonals
* (k = 1, 2, ..., #this->cols()# - 1).
*
* Preconditions: <ul>
* <li> #[k < 0] k <= this->rows() + 1# (throw out_of_range)
* <li> #[k > 0] k <= this->cols() + 1# (throw out_of_range)
* </ul>
*/
DVectorSlice diag(difference_type k = 0);
/** \brief . */
const DVectorSlice diag(difference_type k = 0) const;
///
/* * Extract a rectangular subregion containing rows I, and columns J.
*
* This operator function returns a DMatrixSlice that represents a
* rectangular region of this DMatrixSlice. This submatrix region
* represents the rows I.lbound() to I.ubound() and columns J.lbound()
* to J.lbound(). If I or J is unbounded (full_range() == true, constructed
* with Range1D()), the all of the rows or columns respectively will be
* selected. For example. To select all the rows and the first 5 columns of
* a matrix #A# you would use #A(Range1D(),Range1D(1,5))#.
*
* Preconditions: <ul>
* <li> #[I.full_range() == false] I.ubound() <= this->rows()# (throw out_of_range)
* <li> #[J.full_range() == false] J.ubound() <= this->cols()# (throw out_of_range)
* </ul>
*/
DMatrixSlice operator()(const Range1D& I, const Range1D& J);
/** \brief . */
const DMatrixSlice operator()(const Range1D& I, const Range1D& J) const;
///
/* * Extract a rectangular subregion containing rows i1 to i2, and columns j1 to j2.
*
* This operator function returns a DMatrixSlice that represents a
* rectangular region of this DMatrixSlice. This submatrix region
* represents the rows i1 to 12 and colunms j1 to j2.
*
* Preconditions: <ul>
* <li> #i1 <= i2# (throw out_of_range)
* <li> #i2 <= this->rows()# (throw out_of_range)
* <li> #j1 <= j2# (throw out_of_range)
* <li> #j2 <= this->cols()# (throw out_of_range)
* </ul>
*/
DMatrixSlice operator()(size_type i1, size_type i2, size_type j1
, size_type j2);
/** \brief . */
const DMatrixSlice operator()(size_type i1, size_type i2, size_type j1
, size_type j2) const;
/// Return a DMatrixSlice that represents this entire matrix.
DMatrixSlice operator()();
/** \brief . */
const DMatrixSlice operator()() const;
// @}
/* * @name {\bf Implicit conversion operators}.
*
* These functions allow for the implicit converstion from a DMatrix to a DMatrixSlice.
* This implicit converstion is important for the proper usage of much of the
* libraries functionality.
*/
// @{
/** \brief . */
operator DMatrixSlice();
/** \brief . */
operator const DMatrixSlice() const;
// @}
/* * @name {\bf Assignment Operators}. */
// @{
/** \brief . */
/* * Sets all elements = alpha
*
* If the underlying valarray is unsized (#this->v().size() == 0#) the matrix is sized to 1 x 1
* and the single element is set to alpha.
*
* Postcondtions: <ul>
* <li> #this->operator()(i,j) == alpha#, i = 1,2,...,#this->rows()#, j = 1,2,...,#this->cols()#
*/
DMatrix& operator=(value_type rhs);
/** \brief . */
/* * Copies all of the elements of the DMatrixSlice, #rhs#, into the elements of #this#.
*
* If #this# is not the same size as gms_rhs the #this# is resized.
*
* Postcondtions: <ul>
* <li> #this->operator()(i,j) == gms_rhs(i,j)#, i = 1,2,...,#this->rows()#, j = 1,2,...,#this->cols()#
*/
DMatrix& operator=(const DMatrixSlice& gms_rhs);
/// Same as above. Needed to override the default assignment operator.
DMatrix& operator=(const DMatrix& rhs);
// @}
/* * @name {\bf Raw data access}.
*/
// @{
/// Return the number of rows in the full matrix. Equivalent to BLAS LDA argument.
size_type max_rows() const;
/** \brief . */
/* * Return pointer to the first element in the underlying array the jth
* col (j is 1-based here [1,cols]). If unsized col_ptr(1) returns zero if unsized.
*/
value_type* col_ptr(size_type j);
/// Same as above.
const value_type* col_ptr(size_type j) const;
// @}
private:
std::valarray<value_type> v_;
size_type rows_;
// Assert the row subscript is in bounds (1-based), (throw std::out_of_range)
void validate_row_subscript(size_type i) const;
// Assert the column subscript is in bounds (1-based), (throw std::out_of_range)
void validate_col_subscript(size_type j) const;
// Get a diagonal, (throw std::out_of_range)
DVectorSlice p_diag(difference_type k) const;
}; // end class DMatrix
// end General Matix Classes scope
// @}
// ///////////////////////////////////////////////////////////////////////////////
// Non-member function declarations //
// ///////////////////////////////////////////////////////////////////////////////
/* * @name {\bf DMatrix / DMatrixSlice Associated Non-Member Functions}. */
// @{
// begin non-member functions scope
inline
///
/* * Explicit conversion function from DMatrix to DMatrixSlice.
*
* This is needed to allow a defered evaluation class (TCOL) to be evaluated using its
* implicit conversion operator temp_type() (which returns DMatrix for DMatrixSlice
* resulting expressions).
*/
//DMatrixSlice EvaluateToDMatrixSlice(const DMatrix& gm)
//{ return DMatrixSlice(gm); }
/// Assert two matrices are the same size and throws length_error if they are not (LINALGPACK_CHECK_RHS_SIZES).
void assert_gms_sizes(const DMatrixSlice& gms1, BLAS_Cpp::Transp trans1, const DMatrixSlice& gms2
, BLAS_Cpp::Transp trans2);
inline
/// Assert a matrix is square and throws length_error if it is not (LINALGPACK_CHECK_SLICE_SETUP).
void assert_gms_square(const DMatrixSlice& gms) {
#ifdef LINALGPACK_CHECK_SLICE_SETUP
if(gms.rows() != gms.cols())
throw std::length_error("Matrix must be square");
#endif
}
inline
/** \brief . */
/* * Utility to check if a lhs matrix slice is the same size as a rhs matrix slice.
*
* A DMatrixSlice can not be resized since the rows_ property of the
* DMatrix it came from will not be updated. Allowing a DMatrixSlice
* to resize from unsized would require that the DMatrixSlice carry
* a reference to the DMatrix it was created from. If this is needed
* then it will be added.
*/
void assert_gms_lhs(const DMatrixSlice& gms_lhs, size_type rows, size_type cols
, BLAS_Cpp::Transp trans_rhs = BLAS_Cpp::no_trans)
{
if(trans_rhs == BLAS_Cpp::trans) std::swap(rows,cols);
if(gms_lhs.rows() == rows && gms_lhs.cols() == cols) return; // same size
// not the same size so is an error
throw std::length_error("assert_gms_lhs(...): lhs DMatrixSlice dim does not match rhs dim");
}
/* * @name Return rows or columns from a possiblly transposed DMatrix or DMatrixSlice. */
// @{
inline
/** \brief . */
DVectorSlice row(DMatrixSlice& gms, BLAS_Cpp::Transp trans, size_type i) {
return (trans == BLAS_Cpp::no_trans) ? gms.row(i) : gms.col(i);
}
inline
/** \brief . */
DVectorSlice col(DMatrixSlice& gms, BLAS_Cpp::Transp trans, size_type j) {
return (trans == BLAS_Cpp::no_trans) ? gms.col(j) : gms.row(j);
}
inline
/** \brief . */
const DVectorSlice row(const DMatrixSlice& gms, BLAS_Cpp::Transp trans, size_type i) {
return (trans == BLAS_Cpp::no_trans) ? gms.row(i) : gms.col(i);
}
inline
/** \brief . */
const DVectorSlice col(const DMatrixSlice& gms, BLAS_Cpp::Transp trans, size_type j) {
return (trans == BLAS_Cpp::no_trans) ? gms.col(j) : gms.row(j);
}
inline
/** \brief . */
DVectorSlice row(DMatrix& gm, BLAS_Cpp::Transp trans, size_type i) {
return (trans == BLAS_Cpp::no_trans) ? gm.row(i) : gm.col(i);
}
inline
/** \brief . */
DVectorSlice col(DMatrix& gm, BLAS_Cpp::Transp trans, size_type j) {
return (trans == BLAS_Cpp::no_trans) ? gm.col(j) : gm.row(j);
}
inline
/** \brief . */
const DVectorSlice row(const DMatrix& gm, BLAS_Cpp::Transp trans, size_type i) {
return (trans == BLAS_Cpp::no_trans) ? gm.row(i) : gm.col(i);
}
inline
/** \brief . */
const DVectorSlice col(const DMatrix& gm, BLAS_Cpp::Transp trans, size_type j) {
return (trans == BLAS_Cpp::no_trans) ? gm.col(j) : gm.row(j);
}
// @}
inline
/// Utility to resize a DMatrix to the size of a rhs matrix.
void resize_gm_lhs(DMatrix* gm_rhs, size_type rows, size_type cols
, BLAS_Cpp::Transp trans_rhs)
{
if(trans_rhs == BLAS_Cpp::trans) std::swap(rows,cols);
gm_rhs->resize(rows,cols);
}
// end non-member functions scope
// @}
// end General Rectangular 2-D Matrices scope
// @}
// ////////////////////////////////////////////////////////////////////////////////
// Inline definitions of computationally independent member function definitions //
// ////////////////////////////////////////////////////////////////////////////////
// /////////////////////////////////////////////////////////////////////////////
// DMatrixSlice inline member function definitions
// Private utilities
#ifndef LINALGPACK_CHECK_RANGE
inline
void DMatrixSlice::validate_row_subscript(size_type i) const
{}
#endif
#ifndef LINALGPACK_CHECK_RANGE
inline
void DMatrixSlice::validate_col_subscript(size_type j) const
{}
#endif
#ifndef LINALGPACK_CHECK_SLICE_SETUP
inline
void DMatrixSlice::validate_setup(size_type size) const
{}
#endif
// Constructors
inline
DMatrixSlice::DMatrixSlice()
: ptr_(0), max_rows_(0), rows_(0), cols_(0)
{}
inline
DMatrixSlice::DMatrixSlice( value_type* ptr, size_type size
, size_type max_rows, size_type rows, size_type cols )
: ptr_(ptr), max_rows_(max_rows), rows_(rows), cols_(cols)
{
validate_setup(size);
}
inline
DMatrixSlice::DMatrixSlice( DMatrixSlice& gms, const Range1D& I
, const Range1D& J)
: ptr_( gms.col_ptr(1) + (I.lbound() - 1) + (J.lbound() - 1) * gms.max_rows() )
, max_rows_(gms.max_rows())
, rows_(I.size())
, cols_(J.size())
{
gms.validate_row_subscript(I.ubound());
gms.validate_col_subscript(J.ubound());
}
inline
void DMatrixSlice::bind(DMatrixSlice gms) {
ptr_ = gms.ptr_;
max_rows_ = gms.max_rows_;
rows_ = gms.rows_;
cols_ = gms.cols_;
}
// Size / Dimensionality
inline
DMatrixSlice::size_type DMatrixSlice::rows() const {
return rows_;
}
inline
DMatrixSlice::size_type DMatrixSlice::cols() const {
return cols_;
}
// Misc
// Element access
inline
DMatrixSlice::reference DMatrixSlice::operator()(size_type i, size_type j)
{
validate_row_subscript(i);
validate_col_subscript(j);
return ptr_[(i-1) + (j-1) * max_rows_];
}
inline
DMatrixSlice::const_reference DMatrixSlice::operator()(size_type i, size_type j) const
{
validate_row_subscript(i);
validate_col_subscript(j);
return ptr_[(i-1) + (j-1) * max_rows_];
}
// Subregion access (validated by constructor for DMatrixSlice)
inline
DVectorSlice DMatrixSlice::row(size_type i) {
validate_row_subscript(i);
return DVectorSlice( ptr_ + (i-1), cols(), max_rows() );
}
inline
const DVectorSlice DMatrixSlice::row(size_type i) const {
validate_row_subscript(i);
return DVectorSlice( const_cast<value_type*>(ptr_) + (i-1), cols(), max_rows() );
}
inline
DVectorSlice DMatrixSlice::col(size_type j) {
validate_col_subscript(j);
return DVectorSlice( ptr_ + (j-1)*max_rows(), rows(), 1 );
}
inline
const DVectorSlice DMatrixSlice::col(size_type j) const {
validate_col_subscript(j);
return DVectorSlice( const_cast<value_type*>(ptr_) + (j-1)*max_rows(), rows(), 1 );
}
inline
DVectorSlice DMatrixSlice::diag(difference_type k) {
return p_diag(k);
}
inline
const DVectorSlice DMatrixSlice::diag(difference_type k) const {
return p_diag(k);
}
inline
DMatrixSlice DMatrixSlice::operator()(const Range1D& I, const Range1D& J) {
return DMatrixSlice(*this, RangePack::full_range(I, 1, rows()), RangePack::full_range(J,1,cols()));
}
inline
const DMatrixSlice DMatrixSlice::operator()(const Range1D& I, const Range1D& J) const {
return DMatrixSlice( const_cast<DMatrixSlice&>(*this)
, RangePack::full_range(I, 1, rows()), RangePack::full_range(J,1,cols()) );
}
inline
DMatrixSlice DMatrixSlice::operator()(size_type i1, size_type i2, size_type j1
, size_type j2)
{
return DMatrixSlice(*this, Range1D(i1,i2), Range1D(j1,j2));
}
inline
const DMatrixSlice DMatrixSlice::operator()(size_type i1, size_type i2, size_type j1
, size_type j2) const
{
return DMatrixSlice( const_cast<DMatrixSlice&>(*this), Range1D(i1,i2)
, Range1D(j1,j2) );
}
inline
DMatrixSlice& DMatrixSlice::operator()() {
return *this;
}
inline
const DMatrixSlice& DMatrixSlice::operator()() const {
return *this;
}
// Assignment operators
inline
DMatrixSlice& DMatrixSlice::operator=(value_type alpha) {
assign(this, alpha);
return *this;
}
inline
DMatrixSlice& DMatrixSlice::operator=(const DMatrixSlice& rhs) {
assign(this, rhs, BLAS_Cpp::no_trans);
return *this;
}
// Raw data access
inline
DMatrixSlice::size_type DMatrixSlice::max_rows() const
{ return max_rows_; }
inline
DMatrixSlice::value_type* DMatrixSlice::col_ptr(size_type j) {
if( ptr_ )
validate_col_subscript(j);
return ptr_ + (j-1) * max_rows(); // will be 0 if not bound to a view.
}
inline
const DMatrixSlice::value_type* DMatrixSlice::col_ptr(size_type j) const {
if( ptr_ )
validate_col_subscript(j);
return ptr_ + (j-1) * max_rows(); // will be 0 if not bound to a view.
}
// ////////////////////////////////////////////////////////////////////////////////////////
// DMatrix inline member function definitions
// Private utilities
#ifndef LINALGPACK_CHECK_RANGE
inline
void DMatrix::validate_row_subscript(size_type i) const
{}
#endif
#ifndef LINALGPACK_CHECK_RANGE
inline
void DMatrix::validate_col_subscript(size_type j) const
{}
#endif
// constructors
inline
DMatrix::DMatrix() : v_(), rows_(0)
{}
inline
DMatrix::DMatrix(size_type rows, size_type cols)
: v_(rows*cols), rows_(rows)
{}
inline
DMatrix::DMatrix(value_type val, size_type rows, size_type cols)
: v_(val,rows*cols), rows_(rows)
{}
inline
DMatrix::DMatrix(const value_type* p, size_type rows, size_type cols)
: v_(rows*cols), rows_(rows)
{
// 6/7/00: valarray<> in libstdc++-2.90.7 has a bug in v_(p,size) so we do not
// use it. This is a hack until I can find the time to remove valarray all
// together.
std::copy( p, p + rows*cols, &v_[0] );
}
inline
DMatrix::DMatrix(const DMatrixSlice& gms)
: v_(gms.rows() * gms.cols()), rows_(gms.rows())
{
assign(this, gms, BLAS_Cpp::no_trans);
}
// Memory management
inline
void DMatrix::resize(size_type rows, size_type cols, value_type val)
{
v_.resize(rows*cols,val);
v_ = val;
rows_ = rows;
}
inline
void DMatrix::free() {
v_.resize(0);
rows_ = 0;
}
// Size / Dimensionality
inline
DMatrix::size_type DMatrix::rows() const {
return rows_;
}
inline
DMatrix::size_type DMatrix::cols() const {
return rows_ > 0 ? v_.size() / rows_ : 0;
}
// Element access
inline
DMatrix::reference DMatrix::operator()(size_type i, size_type j)
{
validate_row_subscript(i); validate_col_subscript(j);
return v_[(i-1) + (j-1) * rows_];
}
inline
DMatrix::const_reference DMatrix::operator()(size_type i, size_type j) const
{
validate_row_subscript(i); validate_col_subscript(j);
return (const_cast<std::valarray<value_type>&>(v_))[(i-1) + (j-1) * rows_];
}
// subregion access (range checked by constructors)
inline
DVectorSlice DMatrix::row(size_type i)
{
validate_row_subscript(i);
return DVectorSlice( col_ptr(1) + (i-1), cols(), rows() );
}
inline
const DVectorSlice DMatrix::row(size_type i) const
{
validate_row_subscript(i);
return DVectorSlice( const_cast<value_type*>(col_ptr(1)) + (i-1), cols(), rows() );
}
inline
DVectorSlice DMatrix::col(size_type j)
{
validate_col_subscript(j);
return DVectorSlice( col_ptr(1) + (j-1) * rows(), rows(), 1 );
}
inline
const DVectorSlice DMatrix::col(size_type j) const
{
validate_col_subscript(j);
return DVectorSlice( const_cast<value_type*>(col_ptr(1)) + (j-1) * rows(), rows(), 1 ) ;
}
inline
DVectorSlice DMatrix::diag(difference_type k)
{
return p_diag(k);
}
inline
const DVectorSlice DMatrix::diag(difference_type k) const
{
return p_diag(k);
}
inline
DMatrixSlice DMatrix::operator()(const Range1D& I, const Range1D& J)
{
Range1D Ix = RangePack::full_range(I,1,rows()), Jx = RangePack::full_range(J,1,cols());
return DMatrixSlice( col_ptr(1) + (Ix.lbound() - 1) + (Jx.lbound() - 1) * rows()
, max_rows() * cols(), max_rows(), Ix.size(), Jx.size() );
}
inline
const DMatrixSlice DMatrix::operator()(const Range1D& I, const Range1D& J) const
{
Range1D Ix = RangePack::full_range(I,1,rows()), Jx = RangePack::full_range(J,1,cols());
return DMatrixSlice( const_cast<value_type*>(col_ptr(1)) + (Ix.lbound() - 1) + (Jx.lbound() - 1) * rows()
, max_rows() * cols(), max_rows(), Ix.size(), Jx.size() );
}
inline
DMatrixSlice DMatrix::operator()(size_type i1, size_type i2, size_type j1
, size_type j2)
{
return DMatrixSlice( col_ptr(1) + (i1 - 1) + (j1 - 1) * rows()
, max_rows() * cols(), max_rows(), i2 - i1 + 1, j2 - j1 + 1 );
}
inline
const DMatrixSlice DMatrix::operator()(size_type i1, size_type i2, size_type j1
, size_type j2) const
{
return DMatrixSlice( const_cast<value_type*>(col_ptr(1)) + (i1 - 1) + (j1 - 1) * rows()
, max_rows() * cols(), max_rows(), i2 - i1 + 1, j2 - j1 + 1 );
}
inline
DMatrixSlice DMatrix::operator()()
{
return DMatrixSlice( col_ptr(1), max_rows() * cols(), max_rows(), rows(), cols() );
}
inline
const DMatrixSlice DMatrix::operator()() const
{
return DMatrixSlice( const_cast<value_type*>(col_ptr(1)), max_rows() * cols(), max_rows()
, rows(), cols() );
}
// Implicit conversion operators
inline
DMatrix::operator DMatrixSlice() {
return (*this)();
}
inline
DMatrix::operator const DMatrixSlice() const
{
return (*this)();
}
// Assignment operators
inline
DMatrix& DMatrix::operator=(value_type alpha)
{
assign(this, alpha);
return *this;
}
inline
DMatrix& DMatrix::operator=(const DMatrix& rhs)
{
assign(this, rhs, BLAS_Cpp::no_trans);
return *this;
}
inline
DMatrix& DMatrix::operator=(const DMatrixSlice& rhs)
{
assign(this, rhs, BLAS_Cpp::no_trans);
return *this;
}
// Raw data access
inline
DMatrix::size_type DMatrix::max_rows() const
{ return rows_; }
inline
DMatrix::value_type* DMatrix::col_ptr(size_type j)
{
if( v_.size() ) {
validate_col_subscript(j);
return &v_[ (j-1) * max_rows() ];
}
else {
return 0;
}
}
inline
const DMatrix::value_type* DMatrix::col_ptr(size_type j) const
{
if( v_.size() ) {
validate_col_subscript(j);
return &const_cast<valarray&>(v_)[ (j-1) * max_rows() ];
}
else {
return 0;
}
}
} // end namespace DenseLinAlgPack
#endif // GEN_MATRIX_CLASS_H
|