/usr/include/trilinos/Teuchos_SerialSymDenseMatrix.hpp is in libtrilinos-teuchos-dev 12.4.2-2.
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 | // @HEADER
// ***********************************************************************
//
// Teuchos: Common Tools Package
// Copyright (2004) 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.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// 1. Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
//
// 3. Neither the name of the Corporation nor the names of the
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
// Questions? Contact Michael A. Heroux (maherou@sandia.gov)
//
// ***********************************************************************
// @HEADER
#ifndef _TEUCHOS_SERIALSYMDENSEMATRIX_HPP_
#define _TEUCHOS_SERIALSYMDENSEMATRIX_HPP_
/*! \file Teuchos_SerialSymDenseMatrix.hpp
\brief Templated serial, dense, symmetric matrix class.
*/
#include "Teuchos_CompObject.hpp"
#include "Teuchos_BLAS.hpp"
#include "Teuchos_ScalarTraits.hpp"
#include "Teuchos_DataAccess.hpp"
#include "Teuchos_ConfigDefs.hpp"
#include "Teuchos_Assert.hpp"
/*! \class Teuchos::SerialSymDenseMatrix
\brief This class creates and provides basic support for symmetric, positive-definite dense matrices of templated type.
The Teuchos_SerialSymDenseMatrix class enables the construction and use of
symmetric, positive-definite, dense matrices of templated type.
The Teuchos::SerialSymDenseMatrix class is intended to provide full-featured support for solving
linear and eigen system problems for symmetric positive-definite (SPD) matrices. It is written on
top of BLAS and LAPACK and thus has excellent performance and numerical capabilities. Using this
class, one can either perform simple factorizations and solves or apply all the tricks available in
LAPACK to get the best possible solution for very ill-conditioned problems.
<b>Teuchos::SerialSymDenseMatrix vs. Teuchos::LAPACK</b>
The Teuchos::LAPACK class provides access to most of the same functionality as Teuchos::SerialSymDenseMatrix.
The primary difference is that Teuchos::LAPACK is a "thin" layer on top of LAPACK and
Teuchos::SerialSymDenseMatrix attempts to provide easy access to the more sophisticated aspects of
solving dense linear and eigensystems.
<ul>
<li> When you should use Teuchos::LAPACK: If you are simply looking for a convenient wrapper around
the Fortran LAPACK routines and you have a well-conditioned problem, you should probably use Teuchos::LAPACK directly.
<li> When you should use Teuchos::SerialSymDenseMatrix: If you want to (or potentially want to) solve ill-conditioned
problems or want to work with a more object-oriented interface, you should probably use Teuchos::SerialSymDenseMatrix.
</ul>
<b>Constructing Teuchos::SerialSymDenseMatrix Objects</b>
There are three Teuchos::SerialSymDenseMatrix constructors. The first constructs a zero-sized object
which should be made to appropriate length using the Shape() or Reshape() functions and then filled with
the [] or () operators. The second is a constructor that accepts user data as a 2D array, the third is a
copy constructor. The second constructor has two data access modes (specified by the Teuchos::DataAccess argument):
<ol>
<li> Copy mode - Allocates memory and makes a copy of the user-provided data. In this case, the
user data is not needed after construction.
<li> View mode - Creates a "view" of the user data. In this case, the
user data is required to remain intact for the life of the object.
</ol>
\warning View mode is \e extremely dangerous from a data hiding perspective. Therefore, we strongly encourage
users to develop code using Copy mode first and only use the View mode in a secondary optimization phase.
<b>Extracting Data from Teuchos::SerialSymDenseMatrix Objects</b>
Once a Teuchos::SerialSymDenseMatrix is constructed, it is possible to view the data via access functions.
\warning Use of these access functions cam be \e extremely dangerous from a data hiding perspective.
<b>Vector and Utility Functions</b>
Once a Teuchos::SerialSymDenseMatrix is constructed, several mathematical functions can be applied to
the object. Specifically:
<ul>
<li> Multiplication.
<li> Norms.
</ul>
*/
/** \example DenseMatrix/cxx_main_sym.cpp
This is an example of how to use the Teuchos::SerialSymDenseMatrix class.
*/
namespace Teuchos {
template<typename OrdinalType, typename ScalarType>
class SerialSymDenseMatrix : public CompObject, public Object, public BLAS<OrdinalType,ScalarType>
{
public:
//! Typedef for ordinal type
typedef OrdinalType ordinalType;
//! Typedef for scalar type
typedef ScalarType scalarType;
//! @name Constructor/Destructor Methods
//@{
//! Default constructor; defines a zero size object.
/*!
Teuchos::SerialSymDenseMatrix objects defined by the default constructor
should be sized with the Shape() or Reshape() functions.
Values should be defined by using the [] or ()operators.
Note: By default the active part of the matrix is assumed to be the lower triangular part.
To set the upper part as active, call SetUpper(). See Detailed Description section for further discussion.
*/
SerialSymDenseMatrix();
//! Basic constructor; defines a matrix of \c numRowsCols size and (optionally) initializes it.
/*!
\param numRowsCols - Number of rows and columns in the matrix.
\param zeroOut - Initializes values to 0 if true (default)
Creates a shaped matrix with \c numRowsCols rows and cols. All values are initialized to 0 when \c zeroOut is true.
Values of this matrix should be set using the [] or the () operators.
\note By default the active part of the matrix is assumed to be the lower triangular part.
To set the upper part as active, call SetUpper(). See Detailed Description section for further discussion.
*/
SerialSymDenseMatrix(OrdinalType numRowsCols, bool zeroOut = true);
//! Set object values from two-dimensional array.
/*!
\param CV - Enumerated type set to Teuchos::Copy or Teuchos::View.
\param values - Pointer to an array of ScalarType. The first column starts at \c values,
the second at \c values+stride, etc.
\param stride - The stride between the columns of the matrix in memory.
\param numRowsCols - Number of rows and columns in the matrix.
\note By default the active part of the matrix is assumed to be the lower triangular part.
To set the upper part as active, call SetUpper(). See Detailed Description section for further discussion.
*/
SerialSymDenseMatrix(DataAccess CV, bool upper, ScalarType* values, OrdinalType stride, OrdinalType numRowsCols);
//! Teuchos::SerialSymDenseMatrix copy constructor.
SerialSymDenseMatrix(const SerialSymDenseMatrix<OrdinalType, ScalarType> &Source);
//! Submatrix Copy Constructor
/*!
\param CV - Enumerated type set to Teuchos::Copy or Teuchos::View.
\param Source - Reference to another dense matrix from which values are to be copied.
\param numRowCols - The number of rows and columns in this matrix.
\param startRowCol - The row and column of \c Source from which the submatrix copy should start.
Creates a shaped matrix with \c numRowCols rows and columns, which is a submatrix of \c Source.
If \c startRowCol are not given, then the submatrix is the leading submatrix of \c Source.
*/
SerialSymDenseMatrix(DataAccess CV, const SerialSymDenseMatrix<OrdinalType, ScalarType> &Source, OrdinalType numRowCols, OrdinalType startRowCol=0);
//! Teuchos::SerialSymDenseMatrix destructor.
virtual ~SerialSymDenseMatrix ();
//@}
//! @name Shaping Methods
//@{
//! Set dimensions of a Teuchos::SerialSymDenseMatrix object; init values to zero.
/*!
\param numRowsCols - Number of rows and columns in object.
Allows user to define the dimensions of a Teuchos::SerialSymDenseMatrix at any point. This function can
be called at any point after construction. Any values that were previously in this object are
destroyed and the resized matrix starts off with all zero values.
\return Integer error code, set to 0 if successful.
*/
int shape(OrdinalType numRowsCols);
//! Set dimensions of a Teuchos::SerialSymDenseMatrix object; don't initialize values.
/*!
\param numRowsCols - Number of rows and columns in object.
Allows user to define the dimensions of a Teuchos::SerialSymDenseMatrix at any point. This function can
be called at any point after construction. Any values that were previously in this object are
destroyed. The resized matrix has uninitialized values.
\return Integer error code, set to 0 if successful.
*/
int shapeUninitialized(OrdinalType numRowsCols);
//! Reshape a Teuchos::SerialSymDenseMatrix object.
/*!
\param numRowsCols - Number of rows and columns in object.
Allows user to define the dimensions of a Teuchos::SerialSymDenseMatrix at any point. This function can
be called at any point after construction. Any values that were previously in this object are
copied into the new shape. If the new shape is smaller than the original, the upper left portion
of the original matrix (the principal submatrix) is copied to the new matrix.
\return Integer error code, set to 0 if successful.
*/
int reshape(OrdinalType numRowsCols);
//! Specify that the lower triangle of the \e this matrix should be used.
/*! \warning This may necessitate the matrix values be copied from the upper to lower portion of the matrix.
*/
void setLower();
//! Specify that the upper triangle of the \e this matrix should be used.
/*! \warning This may necessitate the matrix values be copied from the lower to upper portion of the matrix.
*/
void setUpper();
//@}
//! @name Set methods.
//@{
//! Copies values from one matrix to another.
/*!
The operator= copies the values from one existing SerialSymDenseMatrix to another.
If \c Source is a view (i.e. CV = Teuchos::View), then this method will
return a view. Otherwise, it will return a copy of \c Source. \e this object
will be resized if it is not large enough to copy \c Source into.
*/
SerialSymDenseMatrix<OrdinalType, ScalarType>& operator= (const SerialSymDenseMatrix<OrdinalType, ScalarType>& Source);
//! Copies values from one matrix to another.
/*!
Copies the values from one existing SerialSymDenseMatrix to another if the dimension of both matrices are the same.
If not, \e this matrix will be returned unchanged.
*/
SerialSymDenseMatrix<OrdinalType, ScalarType>& assign (const SerialSymDenseMatrix<OrdinalType, ScalarType>& Source);
//! Set all values in the matrix to a constant value.
/*!
\param value - Value to use;
*/
SerialSymDenseMatrix<OrdinalType, ScalarType>& operator= (const ScalarType value) { putScalar(value); return(*this); }
//! Set all values in the matrix to a constant value.
/*!
\param value - Value to use; zero if none specified.
\param fullMatrix - set full matrix entries to \c value, not just active portion of symmetric matrix.
\return Integer error code, set to 0 if successful.
*/
int putScalar( const ScalarType value = Teuchos::ScalarTraits<ScalarType>::zero(), bool fullMatrix = false );
//! Set all values in the active area (upper/lower triangle) of this matrix to be random numbers.
/*! \note The diagonal will be the sum of the off diagonal elements, plus a bias, so the matrix is SPD.
*/
int random( const ScalarType bias = 0.1*Teuchos::ScalarTraits<ScalarType>::one() );
//@}
//! @name Accessor methods.
//@{
//! Element access method (non-const).
/*! Returns the element in the ith row and jth column if A(i,j) is specified.
\return Element from the specified \c rowIndex row and \c colIndex column.
\note If the requested element lies in the inactive part of the matrix, then A(j,i) will be returned.
\warning The validity of \c rowIndex and \c colIndex will only be checked if Teuchos is
configured with --enable-teuchos-abc.
*/
ScalarType& operator () (OrdinalType rowIndex, OrdinalType colIndex);
//! Element access method (const).
/*! Returns the element in the ith row and jth column if A(i,j) is specified.
\return Element from the specified \c rowIndex row and \c colIndex column.
\note If the requested element lies in the inactive part of the matrix, then A(j,i) will be returned.
\warning The validity of \c rowIndex and \c colIndex will only be checked if Teuchos is
configured with --enable-teuchos-abc.
*/
const ScalarType& operator () (OrdinalType rowIndex, OrdinalType colIndex) const;
//! Returns the pointer to the ScalarType data array contained in the object.
/*! \note The matrix values are only guaranteed to be stored in the active area of the matrix (upper/lower).
*/
ScalarType* values() const { return(values_); }
//@}
//! @name Query methods
//@{
//! Returns true if upper triangular part of \e this matrix has and will be used.
bool upper() const {return(upper_);};
//! Returns character value of UPLO used by LAPACK routines.
char UPLO() const {return(UPLO_);};
//@}
//! @name Mathematical Methods
//@{
//! Inplace scalar-matrix product A = \c alpha*A.
/*! Scale a matrix, entry-by-entry using the value \e alpha. This method is sensitive to
the UPLO() parameter.
\param alpha - Scalar to multiply with A.
*/
SerialSymDenseMatrix<OrdinalType, ScalarType>& operator*= (const ScalarType alpha);
//! Add another matrix to \e this matrix.
/*! Add \c Source to \e this if the dimension of both matrices are the same. If not, \e this matrix
will be returned unchanged.
*/
SerialSymDenseMatrix<OrdinalType, ScalarType>& operator+= (const SerialSymDenseMatrix<OrdinalType, ScalarType>& Source);
//! Subtract another matrix from \e this matrix.
/*! Subtract \c Source from \e this if the dimension of both matrices are the same. If not, \e this matrix
will be returned unchanged.
*/
SerialSymDenseMatrix<OrdinalType, ScalarType>& operator-= (const SerialSymDenseMatrix<OrdinalType, ScalarType>& Source);
//@}
//! @name Comparison methods.
//@{
//! Equality of two matrices.
/*! \return True if \e this matrix and \c Operand are of the same shape (rows / columns) and have
the same entries in the active (upper / lower triangular) area of the matrix, else False will be returned.
*/
bool operator== (const SerialSymDenseMatrix<OrdinalType, ScalarType> &Operand) const;
//! Inequality of two matrices.
/*! \return True if \e this matrix and \c Operand of not of the same shape (rows / columns) or don't
have the same entries in the active (upper / lower triangular), else False will be returned.
*/
bool operator!= (const SerialSymDenseMatrix<OrdinalType, ScalarType> &Operand) const;
//@}
//! @name Attribute methods.
//@{
//! Returns the row dimension of this matrix.
OrdinalType numRows() const { return(numRowCols_); }
//! Returns the column dimension of this matrix.
OrdinalType numCols() const { return(numRowCols_); }
//! Returns the stride between the columns of this matrix in memory.
OrdinalType stride() const { return(stride_); }
//! Returns whether this matrix is empty.
bool empty() const { return(numRowCols_ == 0); }
//@}
//! @name Norm methods.
//@{
//! Returns the 1-norm of the matrix.
typename ScalarTraits<ScalarType>::magnitudeType normOne() const;
//! Returns the Infinity-norm of the matrix.
typename ScalarTraits<ScalarType>::magnitudeType normInf() const;
//! Returns the Frobenius-norm of the matrix.
typename ScalarTraits<ScalarType>::magnitudeType normFrobenius() const;
//@}
//! @name I/O methods.
//@{
//! Print method. Defines the behavior of the std::ostream << operator inherited from the Object class.
virtual void print(std::ostream& os) const;
//@}
protected:
// In-place scaling of matrix.
void scale( const ScalarType alpha );
// Copy the values from one matrix to the other.
void copyMat(bool inputUpper, ScalarType* inputMatrix, OrdinalType inputStride,
OrdinalType numRowCols, bool outputUpper, ScalarType* outputMatrix,
OrdinalType outputStride, OrdinalType startRowCol,
ScalarType alpha = ScalarTraits<ScalarType>::zero() );
// Copy the values from the active triangle of the matrix to the other to make the matrix full symmetric.
void copyUPLOMat(bool inputUpper, ScalarType* inputMatrix,
OrdinalType inputStride, OrdinalType inputRows);
void deleteArrays();
void checkIndex( OrdinalType rowIndex, OrdinalType colIndex = 0 ) const;
OrdinalType numRowCols_;
OrdinalType stride_;
bool valuesCopied_;
ScalarType* values_;
bool upper_;
char UPLO_;
};
//----------------------------------------------------------------------------------------------------
// Constructors and Destructor
//----------------------------------------------------------------------------------------------------
template<typename OrdinalType, typename ScalarType>
SerialSymDenseMatrix<OrdinalType, ScalarType>::SerialSymDenseMatrix()
: CompObject(), numRowCols_(0), stride_(0), valuesCopied_(false), values_(0), upper_(false), UPLO_('L')
{}
template<typename OrdinalType, typename ScalarType>
SerialSymDenseMatrix<OrdinalType, ScalarType>::SerialSymDenseMatrix(OrdinalType numRowCols_in, bool zeroOut)
: CompObject(), numRowCols_(numRowCols_in), stride_(numRowCols_in), valuesCopied_(false), upper_(false), UPLO_('L')
{
values_ = new ScalarType[stride_*numRowCols_];
valuesCopied_ = true;
if (zeroOut == true)
putScalar( Teuchos::ScalarTraits<ScalarType>::zero(), true );
}
template<typename OrdinalType, typename ScalarType>
SerialSymDenseMatrix<OrdinalType, ScalarType>::SerialSymDenseMatrix(
DataAccess CV, bool upper_in, ScalarType* values_in, OrdinalType stride_in, OrdinalType numRowCols_in
)
: CompObject(), numRowCols_(numRowCols_in), stride_(stride_in), valuesCopied_(false),
values_(values_in), upper_(upper_in)
{
if (upper_)
UPLO_ = 'U';
else
UPLO_ = 'L';
if(CV == Copy)
{
stride_ = numRowCols_;
values_ = new ScalarType[stride_*numRowCols_];
copyMat(upper_in, values_in, stride_in, numRowCols_, upper_, values_, stride_, 0);
valuesCopied_ = true;
}
}
template<typename OrdinalType, typename ScalarType>
SerialSymDenseMatrix<OrdinalType, ScalarType>::SerialSymDenseMatrix(const SerialSymDenseMatrix<OrdinalType, ScalarType> &Source) : CompObject(), numRowCols_(Source.numRowCols_), stride_(0), valuesCopied_(true), values_(0), upper_(Source.upper_), UPLO_(Source.UPLO_)
{
if (!Source.valuesCopied_)
{
stride_ = Source.stride_;
values_ = Source.values_;
valuesCopied_ = false;
}
else
{
stride_ = numRowCols_;
const OrdinalType newsize = stride_ * numRowCols_;
if(newsize > 0) {
values_ = new ScalarType[newsize];
copyMat(Source.upper_, Source.values_, Source.stride_, numRowCols_, upper_, values_, stride_, 0);
}
else {
numRowCols_ = 0; stride_ = 0;
valuesCopied_ = false;
}
}
}
template<typename OrdinalType, typename ScalarType>
SerialSymDenseMatrix<OrdinalType, ScalarType>::SerialSymDenseMatrix(
DataAccess CV, const SerialSymDenseMatrix<OrdinalType,
ScalarType> &Source, OrdinalType numRowCols_in, OrdinalType startRowCol )
: CompObject(), numRowCols_(numRowCols_in), stride_(Source.stride_), valuesCopied_(false), upper_(Source.upper_), UPLO_(Source.UPLO_)
{
if(CV == Copy)
{
stride_ = numRowCols_in;
values_ = new ScalarType[stride_ * numRowCols_in];
copyMat(Source.upper_, Source.values_, Source.stride_, numRowCols_in, upper_, values_, stride_, startRowCol);
valuesCopied_ = true;
}
else // CV == View
{
values_ = Source.values_ + (stride_ * startRowCol) + startRowCol;
}
}
template<typename OrdinalType, typename ScalarType>
SerialSymDenseMatrix<OrdinalType, ScalarType>::~SerialSymDenseMatrix()
{
deleteArrays();
}
//----------------------------------------------------------------------------------------------------
// Shape methods
//----------------------------------------------------------------------------------------------------
template<typename OrdinalType, typename ScalarType>
int SerialSymDenseMatrix<OrdinalType, ScalarType>::shape( OrdinalType numRowCols_in )
{
deleteArrays(); // Get rid of anything that might be already allocated
numRowCols_ = numRowCols_in;
stride_ = numRowCols_;
values_ = new ScalarType[stride_*numRowCols_];
putScalar( Teuchos::ScalarTraits<ScalarType>::zero(), true );
valuesCopied_ = true;
return(0);
}
template<typename OrdinalType, typename ScalarType>
int SerialSymDenseMatrix<OrdinalType, ScalarType>::shapeUninitialized( OrdinalType numRowCols_in )
{
deleteArrays(); // Get rid of anything that might be already allocated
numRowCols_ = numRowCols_in;
stride_ = numRowCols_;
values_ = new ScalarType[stride_*numRowCols_];
valuesCopied_ = true;
return(0);
}
template<typename OrdinalType, typename ScalarType>
int SerialSymDenseMatrix<OrdinalType, ScalarType>::reshape( OrdinalType numRowCols_in )
{
// Allocate space for new matrix
ScalarType* values_tmp = new ScalarType[numRowCols_in * numRowCols_in];
ScalarType zero = ScalarTraits<ScalarType>::zero();
for(OrdinalType k = 0; k < numRowCols_in * numRowCols_in; k++)
{
values_tmp[k] = zero;
}
OrdinalType numRowCols_tmp = TEUCHOS_MIN(numRowCols_, numRowCols_in);
if(values_ != 0)
{
copyMat(upper_, values_, stride_, numRowCols_tmp, upper_, values_tmp, numRowCols_in, 0); // Copy principal submatrix of A to new A
}
deleteArrays(); // Get rid of anything that might be already allocated
numRowCols_ = numRowCols_in;
stride_ = numRowCols_;
values_ = values_tmp; // Set pointer to new A
valuesCopied_ = true;
return(0);
}
//----------------------------------------------------------------------------------------------------
// Set methods
//----------------------------------------------------------------------------------------------------
template<typename OrdinalType, typename ScalarType>
void SerialSymDenseMatrix<OrdinalType, ScalarType>::setLower()
{
// Do nothing if the matrix is already a lower triangular matrix
if (upper_ != false) {
copyUPLOMat( true, values_, stride_, numRowCols_ );
upper_ = false;
UPLO_ = 'L';
}
}
template<typename OrdinalType, typename ScalarType>
void SerialSymDenseMatrix<OrdinalType, ScalarType>::setUpper()
{
// Do nothing if the matrix is already an upper triangular matrix
if (upper_ == false) {
copyUPLOMat( false, values_, stride_, numRowCols_ );
upper_ = true;
UPLO_ = 'U';
}
}
template<typename OrdinalType, typename ScalarType>
int SerialSymDenseMatrix<OrdinalType, ScalarType>::putScalar( const ScalarType value_in, bool fullMatrix )
{
// Set each value of the dense matrix to "value".
if (fullMatrix == true) {
for(OrdinalType j = 0; j < numRowCols_; j++)
{
for(OrdinalType i = 0; i < numRowCols_; i++)
{
values_[i + j*stride_] = value_in;
}
}
}
// Set the active upper or lower triangular part of the matrix to "value"
else {
if (upper_) {
for(OrdinalType j = 0; j < numRowCols_; j++) {
for(OrdinalType i = 0; i <= j; i++) {
values_[i + j*stride_] = value_in;
}
}
}
else {
for(OrdinalType j = 0; j < numRowCols_; j++) {
for(OrdinalType i = j; i < numRowCols_; i++) {
values_[i + j*stride_] = value_in;
}
}
}
}
return 0;
}
template<typename OrdinalType, typename ScalarType>
int SerialSymDenseMatrix<OrdinalType, ScalarType>::random( const ScalarType bias )
{
typedef typename Teuchos::ScalarTraits<ScalarType>::magnitudeType MT;
// Set each value of the dense matrix to a random value.
std::vector<MT> diagSum( numRowCols_, Teuchos::ScalarTraits<MT>::zero() );
if (upper_) {
for(OrdinalType j = 0; j < numRowCols_; j++) {
for(OrdinalType i = 0; i < j; i++) {
values_[i + j*stride_] = ScalarTraits<ScalarType>::random();
diagSum[i] += Teuchos::ScalarTraits<ScalarType>::magnitude( values_[i + j*stride_] );
diagSum[j] += Teuchos::ScalarTraits<ScalarType>::magnitude( values_[i + j*stride_] );
}
}
}
else {
for(OrdinalType j = 0; j < numRowCols_; j++) {
for(OrdinalType i = j+1; i < numRowCols_; i++) {
values_[i + j*stride_] = ScalarTraits<ScalarType>::random();
diagSum[i] += Teuchos::ScalarTraits<ScalarType>::magnitude( values_[i + j*stride_] );
diagSum[j] += Teuchos::ScalarTraits<ScalarType>::magnitude( values_[i + j*stride_] );
}
}
}
// Set the diagonal.
for(OrdinalType i = 0; i < numRowCols_; i++) {
values_[i + i*stride_] = diagSum[i] + bias;
}
return 0;
}
template<typename OrdinalType, typename ScalarType>
SerialSymDenseMatrix<OrdinalType,ScalarType>&
SerialSymDenseMatrix<OrdinalType, ScalarType>::operator=( const SerialSymDenseMatrix<OrdinalType,ScalarType>& Source )
{
if(this == &Source)
return(*this); // Special case of source same as target
if((!valuesCopied_) && (!Source.valuesCopied_) && (values_ == Source.values_)) {
upper_ = Source.upper_; // Might have to change the active part of the matrix.
return(*this); // Special case of both are views to same data.
}
// If the source is a view then we will return a view, else we will return a copy.
if (!Source.valuesCopied_) {
if(valuesCopied_) {
// Clean up stored data if this was previously a copy.
deleteArrays();
}
numRowCols_ = Source.numRowCols_;
stride_ = Source.stride_;
values_ = Source.values_;
upper_ = Source.upper_;
UPLO_ = Source.UPLO_;
}
else {
// If we were a view, we will now be a copy.
if(!valuesCopied_) {
numRowCols_ = Source.numRowCols_;
stride_ = Source.numRowCols_;
upper_ = Source.upper_;
UPLO_ = Source.UPLO_;
const OrdinalType newsize = stride_ * numRowCols_;
if(newsize > 0) {
values_ = new ScalarType[newsize];
valuesCopied_ = true;
}
else {
values_ = 0;
}
}
// If we were a copy, we will stay a copy.
else {
if((Source.numRowCols_ <= stride_) && (Source.numRowCols_ == numRowCols_)) { // we don't need to reallocate
numRowCols_ = Source.numRowCols_;
upper_ = Source.upper_;
UPLO_ = Source.UPLO_;
}
else { // we need to allocate more space (or less space)
deleteArrays();
numRowCols_ = Source.numRowCols_;
stride_ = Source.numRowCols_;
upper_ = Source.upper_;
UPLO_ = Source.UPLO_;
const OrdinalType newsize = stride_ * numRowCols_;
if(newsize > 0) {
values_ = new ScalarType[newsize];
valuesCopied_ = true;
}
}
}
copyMat(Source.upper_, Source.values_, Source.stride_, Source.numRowCols_, upper_, values_, stride_, 0);
}
return(*this);
}
template<typename OrdinalType, typename ScalarType>
SerialSymDenseMatrix<OrdinalType, ScalarType>& SerialSymDenseMatrix<OrdinalType, ScalarType>::operator*= (const ScalarType alpha)
{
this->scale(alpha);
return(*this);
}
template<typename OrdinalType, typename ScalarType>
SerialSymDenseMatrix<OrdinalType, ScalarType>& SerialSymDenseMatrix<OrdinalType, ScalarType>::operator+= (const SerialSymDenseMatrix<OrdinalType,ScalarType>& Source )
{
// Check for compatible dimensions
if ((numRowCols_ != Source.numRowCols_))
{
TEUCHOS_CHK_REF(*this); // Return *this without altering it.
}
copyMat(Source.upper_, Source.values_, Source.stride_, numRowCols_, upper_, values_, stride_, 0, ScalarTraits<ScalarType>::one());
return(*this);
}
template<typename OrdinalType, typename ScalarType>
SerialSymDenseMatrix<OrdinalType, ScalarType>& SerialSymDenseMatrix<OrdinalType, ScalarType>::operator-= (const SerialSymDenseMatrix<OrdinalType,ScalarType>& Source )
{
// Check for compatible dimensions
if ((numRowCols_ != Source.numRowCols_))
{
TEUCHOS_CHK_REF(*this); // Return *this without altering it.
}
copyMat(Source.upper_, Source.values_, Source.stride_, numRowCols_, upper_, values_, stride_, 0, -ScalarTraits<ScalarType>::one());
return(*this);
}
template<typename OrdinalType, typename ScalarType>
SerialSymDenseMatrix<OrdinalType,ScalarType>& SerialSymDenseMatrix<OrdinalType, ScalarType>::assign (const SerialSymDenseMatrix<OrdinalType,ScalarType>& Source) {
if(this == &Source)
return(*this); // Special case of source same as target
if((!valuesCopied_) && (!Source.valuesCopied_) && (values_ == Source.values_)) {
upper_ = Source.upper_; // We may have to change the active part of the matrix.
return(*this); // Special case of both are views to same data.
}
// Check for compatible dimensions
if ((numRowCols_ != Source.numRowCols_))
{
TEUCHOS_CHK_REF(*this); // Return *this without altering it.
}
copyMat(Source.upper_, Source.values_, Source.stride_, numRowCols_, upper_, values_, stride_, 0 );
return(*this);
}
//----------------------------------------------------------------------------------------------------
// Accessor methods
//----------------------------------------------------------------------------------------------------
template<typename OrdinalType, typename ScalarType>
inline ScalarType& SerialSymDenseMatrix<OrdinalType, ScalarType>::operator () (OrdinalType rowIndex, OrdinalType colIndex)
{
#ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
checkIndex( rowIndex, colIndex );
#endif
if ( rowIndex <= colIndex ) {
// Accessing upper triangular part of matrix
if (upper_)
return(values_[colIndex * stride_ + rowIndex]);
else
return(values_[rowIndex * stride_ + colIndex]);
}
else {
// Accessing lower triangular part of matrix
if (upper_)
return(values_[rowIndex * stride_ + colIndex]);
else
return(values_[colIndex * stride_ + rowIndex]);
}
}
template<typename OrdinalType, typename ScalarType>
inline const ScalarType& SerialSymDenseMatrix<OrdinalType, ScalarType>::operator () (OrdinalType rowIndex, OrdinalType colIndex) const
{
#ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
checkIndex( rowIndex, colIndex );
#endif
if ( rowIndex <= colIndex ) {
// Accessing upper triangular part of matrix
if (upper_)
return(values_[colIndex * stride_ + rowIndex]);
else
return(values_[rowIndex * stride_ + colIndex]);
}
else {
// Accessing lower triangular part of matrix
if (upper_)
return(values_[rowIndex * stride_ + colIndex]);
else
return(values_[colIndex * stride_ + rowIndex]);
}
}
//----------------------------------------------------------------------------------------------------
// Norm methods
//----------------------------------------------------------------------------------------------------
template<typename OrdinalType, typename ScalarType>
typename ScalarTraits<ScalarType>::magnitudeType SerialSymDenseMatrix<OrdinalType, ScalarType>::normOne() const
{
return(normInf());
}
template<typename OrdinalType, typename ScalarType>
typename ScalarTraits<ScalarType>::magnitudeType SerialSymDenseMatrix<OrdinalType, ScalarType>::normInf() const
{
typedef typename ScalarTraits<ScalarType>::magnitudeType MT;
OrdinalType i, j;
MT sum, anorm = ScalarTraits<MT>::zero();
ScalarType* ptr;
if (upper_) {
for (j=0; j<numRowCols_; j++) {
sum = ScalarTraits<MT>::zero();
ptr = values_ + j*stride_;
for (i=0; i<j; i++) {
sum += ScalarTraits<ScalarType>::magnitude( *ptr++ );
}
ptr = values_ + j + j*stride_;
for (i=j; i<numRowCols_; i++) {
sum += ScalarTraits<ScalarType>::magnitude( *ptr );
ptr += stride_;
}
anorm = TEUCHOS_MAX( anorm, sum );
}
}
else {
for (j=0; j<numRowCols_; j++) {
sum = ScalarTraits<MT>::zero();
ptr = values_ + j + j*stride_;
for (i=j; i<numRowCols_; i++) {
sum += ScalarTraits<ScalarType>::magnitude( *ptr++ );
}
ptr = values_ + j;
for (i=0; i<j; i++) {
sum += ScalarTraits<ScalarType>::magnitude( *ptr );
ptr += stride_;
}
anorm = TEUCHOS_MAX( anorm, sum );
}
}
return(anorm);
}
template<typename OrdinalType, typename ScalarType>
typename ScalarTraits<ScalarType>::magnitudeType SerialSymDenseMatrix<OrdinalType, ScalarType>::normFrobenius() const
{
typedef typename ScalarTraits<ScalarType>::magnitudeType MT;
OrdinalType i, j;
MT sum = ScalarTraits<MT>::zero(), anorm = ScalarTraits<MT>::zero();
if (upper_) {
for (j = 0; j < numRowCols_; j++) {
for (i = 0; i < j; i++) {
sum += ScalarTraits<ScalarType>::magnitude(2.0*values_[i+j*stride_]*values_[i+j*stride_]);
}
sum += ScalarTraits<ScalarType>::magnitude(values_[j + j*stride_]*values_[j + j*stride_]);
}
}
else {
for (j = 0; j < numRowCols_; j++) {
sum += ScalarTraits<ScalarType>::magnitude(values_[j + j*stride_]*values_[j + j*stride_]);
for (i = j+1; i < numRowCols_; i++) {
sum += ScalarTraits<ScalarType>::magnitude(2.0*values_[i+j*stride_]*values_[i+j*stride_]);
}
}
}
anorm = ScalarTraits<ScalarType>::magnitude(ScalarTraits<ScalarType>::squareroot(sum));
return(anorm);
}
//----------------------------------------------------------------------------------------------------
// Comparison methods
//----------------------------------------------------------------------------------------------------
template<typename OrdinalType, typename ScalarType>
bool SerialSymDenseMatrix<OrdinalType, ScalarType>::operator== (const SerialSymDenseMatrix<OrdinalType, ScalarType> &Operand) const
{
bool result = 1;
if((numRowCols_ != Operand.numRowCols_)) {
result = 0;
}
else {
OrdinalType i, j;
for(i = 0; i < numRowCols_; i++) {
for(j = 0; j < numRowCols_; j++) {
if((*this)(i, j) != Operand(i, j)) {
return 0;
}
}
}
}
return result;
}
template<typename OrdinalType, typename ScalarType>
bool SerialSymDenseMatrix<OrdinalType, ScalarType>::operator!= (const SerialSymDenseMatrix<OrdinalType, ScalarType> &Operand) const
{
return !((*this) == Operand);
}
//----------------------------------------------------------------------------------------------------
// Multiplication method
//----------------------------------------------------------------------------------------------------
template<typename OrdinalType, typename ScalarType>
void SerialSymDenseMatrix<OrdinalType, ScalarType>::scale( const ScalarType alpha )
{
OrdinalType i, j;
ScalarType* ptr;
if (upper_) {
for (j=0; j<numRowCols_; j++) {
ptr = values_ + j*stride_;
for (i=0; i<= j; i++) { *ptr = alpha * (*ptr); ptr++; }
}
}
else {
for (j=0; j<numRowCols_; j++) {
ptr = values_ + j*stride_ + j;
for (i=j; i<numRowCols_; i++) { *ptr = alpha * (*ptr); ptr++; }
}
}
}
/*
template<typename OrdinalType, typename ScalarType>
int SerialSymDenseMatrix<OrdinalType, ScalarType>::scale( const SerialSymDenseMatrix<OrdinalType,ScalarType>& A )
{
OrdinalType i, j;
ScalarType* ptr;
// Check for compatible dimensions
if ((numRowCols_ != A.numRowCols_)) {
TEUCHOS_CHK_ERR(-1); // Return error
}
if (upper_) {
for (j=0; j<numRowCols_; j++) {
ptr = values_ + j*stride_;
for (i=0; i<= j; i++) { *ptr = A(i,j) * (*ptr); ptr++; }
}
}
else {
for (j=0; j<numRowCols_; j++) {
ptr = values_ + j*stride_;
for (i=j; i<numRowCols_; i++) { *ptr = A(i,j) * (*ptr); ptr++; }
}
}
return(0);
}
*/
template<typename OrdinalType, typename ScalarType>
void SerialSymDenseMatrix<OrdinalType, ScalarType>::print(std::ostream& os) const
{
os << std::endl;
if(valuesCopied_)
os << "Values_copied : yes" << std::endl;
else
os << "Values_copied : no" << std::endl;
os << "Rows / Columns : " << numRowCols_ << std::endl;
os << "LDA : " << stride_ << std::endl;
if (upper_)
os << "Storage: Upper" << std::endl;
else
os << "Storage: Lower" << std::endl;
if(numRowCols_ == 0) {
os << "(matrix is empty, no values to display)" << std::endl;
} else {
for(OrdinalType i = 0; i < numRowCols_; i++) {
for(OrdinalType j = 0; j < numRowCols_; j++){
os << (*this)(i,j) << " ";
}
os << std::endl;
}
}
}
//----------------------------------------------------------------------------------------------------
// Protected methods
//----------------------------------------------------------------------------------------------------
template<typename OrdinalType, typename ScalarType>
inline void SerialSymDenseMatrix<OrdinalType, ScalarType>::checkIndex( OrdinalType rowIndex, OrdinalType colIndex ) const {
TEUCHOS_TEST_FOR_EXCEPTION(rowIndex < 0 || rowIndex >= numRowCols_, std::out_of_range,
"SerialSymDenseMatrix<T>::checkIndex: "
"Row index " << rowIndex << " out of range [0, "<< numRowCols_ << ")");
TEUCHOS_TEST_FOR_EXCEPTION(colIndex < 0 || colIndex >= numRowCols_, std::out_of_range,
"SerialSymDenseMatrix<T>::checkIndex: "
"Col index " << colIndex << " out of range [0, "<< numRowCols_ << ")");
}
template<typename OrdinalType, typename ScalarType>
void SerialSymDenseMatrix<OrdinalType, ScalarType>::deleteArrays(void)
{
if (valuesCopied_)
{
delete [] values_;
values_ = 0;
valuesCopied_ = false;
}
}
template<typename OrdinalType, typename ScalarType>
void SerialSymDenseMatrix<OrdinalType, ScalarType>::copyMat(
bool inputUpper, ScalarType* inputMatrix,
OrdinalType inputStride, OrdinalType numRowCols_in,
bool outputUpper, ScalarType* outputMatrix,
OrdinalType outputStride, OrdinalType startRowCol,
ScalarType alpha
)
{
OrdinalType i, j;
ScalarType* ptr1 = 0;
ScalarType* ptr2 = 0;
for (j = 0; j < numRowCols_in; j++) {
if (inputUpper == true) {
// The input matrix is upper triangular, start at the beginning of each column.
ptr2 = inputMatrix + (j + startRowCol) * inputStride + startRowCol;
if (outputUpper == true) {
// The output matrix matches the same pattern as the input matrix.
ptr1 = outputMatrix + j*outputStride;
if (alpha != Teuchos::ScalarTraits<ScalarType>::zero() ) {
for(i = 0; i <= j; i++) {
*ptr1++ += alpha*(*ptr2++);
}
} else {
for(i = 0; i <= j; i++) {
*ptr1++ = *ptr2++;
}
}
}
else {
// The output matrix has the opposite pattern as the input matrix.
// Copy down across rows of the output matrix, but down columns of the input matrix.
ptr1 = outputMatrix + j;
if (alpha != Teuchos::ScalarTraits<ScalarType>::zero() ) {
for(i = 0; i <= j; i++) {
*ptr1 += alpha*(*ptr2++);
ptr1 += outputStride;
}
} else {
for(i = 0; i <= j; i++) {
*ptr1 = *ptr2++;
ptr1 += outputStride;
}
}
}
}
else {
// The input matrix is lower triangular, start at the diagonal of each row.
ptr2 = inputMatrix + (startRowCol+j) * inputStride + startRowCol + j;
if (outputUpper == true) {
// The output matrix has the opposite pattern as the input matrix.
// Copy across rows of the output matrix, but down columns of the input matrix.
ptr1 = outputMatrix + j*outputStride + j;
if (alpha != Teuchos::ScalarTraits<ScalarType>::zero() ) {
for(i = j; i < numRowCols_in; i++) {
*ptr1 += alpha*(*ptr2++);
ptr1 += outputStride;
}
} else {
for(i = j; i < numRowCols_in; i++) {
*ptr1 = *ptr2++;
ptr1 += outputStride;
}
}
}
else {
// The output matrix matches the same pattern as the input matrix.
ptr1 = outputMatrix + j*outputStride + j;
if (alpha != Teuchos::ScalarTraits<ScalarType>::zero() ) {
for(i = j; i < numRowCols_in; i++) {
*ptr1++ += alpha*(*ptr2++);
}
} else {
for(i = j; i < numRowCols_in; i++) {
*ptr1++ = *ptr2++;
}
}
}
}
}
}
template<typename OrdinalType, typename ScalarType>
void SerialSymDenseMatrix<OrdinalType, ScalarType>::copyUPLOMat(
bool inputUpper, ScalarType* inputMatrix,
OrdinalType inputStride, OrdinalType inputRows
)
{
OrdinalType i, j;
ScalarType * ptr1 = 0;
ScalarType * ptr2 = 0;
if (inputUpper) {
for (j=1; j<inputRows; j++) {
ptr1 = inputMatrix + j;
ptr2 = inputMatrix + j*inputStride;
for (i=0; i<j; i++) {
*ptr1 = *ptr2++;
ptr1+=inputStride;
}
}
}
else {
for (i=1; i<inputRows; i++) {
ptr1 = inputMatrix + i;
ptr2 = inputMatrix + i*inputStride;
for (j=0; j<i; j++) {
*ptr2++ = *ptr1;
ptr1+=inputStride;
}
}
}
}
} // namespace Teuchos
#endif /* _TEUCHOS_SERIALSYMDENSEMATRIX_HPP_ */
|