43 #ifndef _TEUCHOS_SERIALSYMDENSEMATRIX_HPP_ 44 #define _TEUCHOS_SERIALSYMDENSEMATRIX_HPP_ 54 #include "Teuchos_Assert.hpp" 120 template<
typename OrdinalType,
typename ScalarType>
202 int shape(OrdinalType numRowsCols);
227 int reshape(OrdinalType numRowsCols);
292 ScalarType&
operator () (OrdinalType rowIndex, OrdinalType colIndex);
302 const ScalarType&
operator () (OrdinalType rowIndex, OrdinalType colIndex)
const;
307 ScalarType*
values()
const {
return(values_); }
315 bool upper()
const {
return(upper_);};
318 char UPLO()
const {
return(UPLO_);};
368 OrdinalType
numRows()
const {
return(numRowCols_); }
371 OrdinalType
numCols()
const {
return(numRowCols_); }
374 OrdinalType
stride()
const {
return(stride_); }
377 bool empty()
const {
return(numRowCols_ == 0); }
396 virtual void print(std::ostream& os)
const;
404 void scale(
const ScalarType alpha );
407 void copyMat(
bool inputUpper, ScalarType* inputMatrix, OrdinalType inputStride,
408 OrdinalType numRowCols,
bool outputUpper, ScalarType* outputMatrix,
409 OrdinalType outputStride, OrdinalType startRowCol,
413 void copyUPLOMat(
bool inputUpper, ScalarType* inputMatrix,
414 OrdinalType inputStride, OrdinalType inputRows);
417 void checkIndex( OrdinalType rowIndex, OrdinalType colIndex = 0 )
const;
418 OrdinalType numRowCols_;
432 template<
typename OrdinalType,
typename ScalarType>
434 :
CompObject(),
Object(
"Teuchos::SerialSymDenseMatrix"), numRowCols_(0), stride_(0), valuesCopied_(false), values_(0), upper_(false), UPLO_(
'L')
437 template<
typename OrdinalType,
typename ScalarType>
439 :
CompObject(),
Object(
"Teuchos::SerialSymDenseMatrix"), numRowCols_(numRowCols_in), stride_(numRowCols_in), valuesCopied_(false), upper_(false), UPLO_(
'L')
441 values_ =
new ScalarType[stride_*numRowCols_];
442 valuesCopied_ =
true;
447 template<
typename OrdinalType,
typename ScalarType>
449 DataAccess CV,
bool upper_in, ScalarType* values_in, OrdinalType stride_in, OrdinalType numRowCols_in
451 :
CompObject(),
Object(
"Teuchos::SerialSymDenseMatrix"), numRowCols_(numRowCols_in), stride_(stride_in), valuesCopied_(false),
452 values_(values_in), upper_(upper_in)
461 stride_ = numRowCols_;
462 values_ =
new ScalarType[stride_*numRowCols_];
463 copyMat(upper_in, values_in, stride_in, numRowCols_, upper_, values_, stride_, 0);
464 valuesCopied_ =
true;
468 template<
typename OrdinalType,
typename ScalarType>
471 values_ =
new ScalarType[stride_*numRowCols_];
472 copyMat(Source.upper_, Source.values_, Source.stride_, numRowCols_, upper_, values_, stride_, 0);
473 valuesCopied_ =
true;
476 template<
typename OrdinalType,
typename ScalarType>
479 ScalarType> &Source, OrdinalType numRowCols_in, OrdinalType startRowCol )
480 :
CompObject(),
Object(
"Teuchos::SerialSymDenseMatrix"), numRowCols_(numRowCols_in), stride_(Source.stride_), valuesCopied_(false), upper_(Source.upper_), UPLO_(Source.UPLO_)
484 stride_ = numRowCols_in;
485 values_ =
new ScalarType[stride_ * numRowCols_in];
486 copyMat(Source.upper_, Source.values_, Source.stride_, numRowCols_in, upper_, values_, stride_, startRowCol);
487 valuesCopied_ =
true;
491 values_ = Source.values_ + (stride_ * startRowCol) + startRowCol;
495 template<
typename OrdinalType,
typename ScalarType>
505 template<
typename OrdinalType,
typename ScalarType>
509 numRowCols_ = numRowCols_in;
510 stride_ = numRowCols_;
511 values_ =
new ScalarType[stride_*numRowCols_];
513 valuesCopied_ =
true;
517 template<
typename OrdinalType,
typename ScalarType>
521 numRowCols_ = numRowCols_in;
522 stride_ = numRowCols_;
523 values_ =
new ScalarType[stride_*numRowCols_];
524 valuesCopied_ =
true;
528 template<
typename OrdinalType,
typename ScalarType>
532 ScalarType* values_tmp =
new ScalarType[numRowCols_in * numRowCols_in];
534 for(OrdinalType k = 0; k < numRowCols_in * numRowCols_in; k++)
536 values_tmp[k] = zero;
538 OrdinalType numRowCols_tmp = TEUCHOS_MIN(numRowCols_, numRowCols_in);
541 copyMat(upper_, values_, stride_, numRowCols_tmp, upper_, values_tmp, numRowCols_in, 0);
544 numRowCols_ = numRowCols_in;
545 stride_ = numRowCols_;
546 values_ = values_tmp;
547 valuesCopied_ =
true;
555 template<
typename OrdinalType,
typename ScalarType>
559 if (upper_ !=
false) {
560 copyUPLOMat(
true, values_, stride_, numRowCols_ );
566 template<
typename OrdinalType,
typename ScalarType>
570 if (upper_ ==
false) {
571 copyUPLOMat(
false, values_, stride_, numRowCols_ );
577 template<
typename OrdinalType,
typename ScalarType>
581 if (fullMatrix ==
true) {
582 for(OrdinalType j = 0; j < numRowCols_; j++)
584 for(OrdinalType i = 0; i < numRowCols_; i++)
586 values_[i + j*stride_] = value_in;
593 for(OrdinalType j = 0; j < numRowCols_; j++) {
594 for(OrdinalType i = 0; i <= j; i++) {
595 values_[i + j*stride_] = value_in;
600 for(OrdinalType j = 0; j < numRowCols_; j++) {
601 for(OrdinalType i = j; i < numRowCols_; i++) {
602 values_[i + j*stride_] = value_in;
610 template<
typename OrdinalType,
typename ScalarType>
618 for(OrdinalType j = 0; j < numRowCols_; j++) {
619 for(OrdinalType i = 0; i < j; i++) {
627 for(OrdinalType j = 0; j < numRowCols_; j++) {
628 for(OrdinalType i = j+1; i < numRowCols_; i++) {
637 for(OrdinalType i = 0; i < numRowCols_; i++) {
638 values_[i + i*stride_] = diagSum[i] + bias;
643 template<
typename OrdinalType,
typename ScalarType>
649 if((!valuesCopied_) && (!Source.valuesCopied_) && (values_ == Source.values_)) {
650 upper_ = Source.upper_;
655 if (!Source.valuesCopied_) {
660 numRowCols_ = Source.numRowCols_;
661 stride_ = Source.stride_;
662 values_ = Source.values_;
663 upper_ = Source.upper_;
664 UPLO_ = Source.UPLO_;
669 numRowCols_ = Source.numRowCols_;
670 stride_ = Source.numRowCols_;
671 upper_ = Source.upper_;
672 UPLO_ = Source.UPLO_;
673 const OrdinalType newsize = stride_ * numRowCols_;
675 values_ =
new ScalarType[newsize];
676 valuesCopied_ =
true;
684 if((Source.numRowCols_ <= stride_) && (Source.numRowCols_ == numRowCols_)) {
685 numRowCols_ = Source.numRowCols_;
686 upper_ = Source.upper_;
687 UPLO_ = Source.UPLO_;
691 numRowCols_ = Source.numRowCols_;
692 stride_ = Source.numRowCols_;
693 upper_ = Source.upper_;
694 UPLO_ = Source.UPLO_;
695 const OrdinalType newsize = stride_ * numRowCols_;
697 values_ =
new ScalarType[newsize];
698 valuesCopied_ =
true;
702 copyMat(Source.upper_, Source.values_, Source.stride_, Source.numRowCols_, upper_, values_, stride_, 0);
707 template<
typename OrdinalType,
typename ScalarType>
714 template<
typename OrdinalType,
typename ScalarType>
718 if ((numRowCols_ != Source.numRowCols_))
720 TEUCHOS_CHK_REF(*
this);
726 template<
typename OrdinalType,
typename ScalarType>
730 if ((numRowCols_ != Source.numRowCols_))
732 TEUCHOS_CHK_REF(*
this);
738 template<
typename OrdinalType,
typename ScalarType>
742 if((!valuesCopied_) && (!Source.valuesCopied_) && (values_ == Source.values_)) {
743 upper_ = Source.upper_;
748 if ((numRowCols_ != Source.numRowCols_))
750 TEUCHOS_CHK_REF(*
this);
752 copyMat(Source.upper_, Source.values_, Source.stride_, numRowCols_, upper_, values_, stride_, 0 );
760 template<
typename OrdinalType,
typename ScalarType>
763 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK 764 checkIndex( rowIndex, colIndex );
766 if ( rowIndex <= colIndex ) {
769 return(values_[colIndex * stride_ + rowIndex]);
771 return(values_[rowIndex * stride_ + colIndex]);
776 return(values_[rowIndex * stride_ + colIndex]);
778 return(values_[colIndex * stride_ + rowIndex]);
782 template<
typename OrdinalType,
typename ScalarType>
785 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK 786 checkIndex( rowIndex, colIndex );
788 if ( rowIndex <= colIndex ) {
791 return(values_[colIndex * stride_ + rowIndex]);
793 return(values_[rowIndex * stride_ + colIndex]);
798 return(values_[rowIndex * stride_ + colIndex]);
800 return(values_[colIndex * stride_ + rowIndex]);
808 template<
typename OrdinalType,
typename ScalarType>
814 template<
typename OrdinalType,
typename ScalarType>
825 for (j=0; j<numRowCols_; j++) {
827 ptr = values_ + j*stride_;
828 for (i=0; i<j; i++) {
831 ptr = values_ + j + j*stride_;
832 for (i=j; i<numRowCols_; i++) {
836 anorm = TEUCHOS_MAX( anorm, sum );
840 for (j=0; j<numRowCols_; j++) {
842 ptr = values_ + j + j*stride_;
843 for (i=j; i<numRowCols_; i++) {
847 for (i=0; i<j; i++) {
851 anorm = TEUCHOS_MAX( anorm, sum );
857 template<
typename OrdinalType,
typename ScalarType>
867 for (j = 0; j < numRowCols_; j++) {
868 for (i = 0; i < j; i++) {
875 for (j = 0; j < numRowCols_; j++) {
877 for (i = j+1; i < numRowCols_; i++) {
890 template<
typename OrdinalType,
typename ScalarType>
894 if((numRowCols_ != Operand.numRowCols_)) {
899 for(i = 0; i < numRowCols_; i++) {
900 for(j = 0; j < numRowCols_; j++) {
901 if((*
this)(i, j) != Operand(i, j)) {
910 template<
typename OrdinalType,
typename ScalarType>
913 return !((*this) == Operand);
920 template<
typename OrdinalType,
typename ScalarType>
927 for (j=0; j<numRowCols_; j++) {
928 ptr = values_ + j*stride_;
929 for (i=0; i<= j; i++) { *ptr = alpha * (*ptr); ptr++; }
933 for (j=0; j<numRowCols_; j++) {
934 ptr = values_ + j*stride_ + j;
935 for (i=j; i<numRowCols_; i++) { *ptr = alpha * (*ptr); ptr++; }
969 template<
typename OrdinalType,
typename ScalarType>
974 os <<
"Values_copied : yes" << std::endl;
976 os <<
"Values_copied : no" << std::endl;
977 os <<
"Rows / Columns : " << numRowCols_ << std::endl;
978 os <<
"LDA : " << stride_ << std::endl;
980 os <<
"Storage: Upper" << std::endl;
982 os <<
"Storage: Lower" << std::endl;
983 if(numRowCols_ == 0) {
984 os <<
"(matrix is empty, no values to display)" << std::endl;
986 for(OrdinalType i = 0; i < numRowCols_; i++) {
987 for(OrdinalType j = 0; j < numRowCols_; j++){
988 os << (*this)(i,j) <<
" ";
999 template<
typename OrdinalType,
typename ScalarType>
1002 "SerialSymDenseMatrix<T>::checkIndex: " 1003 "Row index " << rowIndex <<
" out of range [0, "<< numRowCols_ <<
")");
1005 "SerialSymDenseMatrix<T>::checkIndex: " 1006 "Col index " << colIndex <<
" out of range [0, "<< numRowCols_ <<
")");
1009 template<
typename OrdinalType,
typename ScalarType>
1016 valuesCopied_ =
false;
1020 template<
typename OrdinalType,
typename ScalarType>
1022 bool inputUpper, ScalarType* inputMatrix,
1023 OrdinalType inputStride, OrdinalType numRowCols_in,
1024 bool outputUpper, ScalarType* outputMatrix,
1025 OrdinalType outputStride, OrdinalType startRowCol,
1030 ScalarType* ptr1 = 0;
1031 ScalarType* ptr2 = 0;
1033 for (j = 0; j < numRowCols_in; j++) {
1034 if (inputUpper ==
true) {
1036 ptr2 = inputMatrix + (j + startRowCol) * inputStride + startRowCol;
1037 if (outputUpper ==
true) {
1039 ptr1 = outputMatrix + j*outputStride;
1041 for(i = 0; i <= j; i++) {
1042 *ptr1++ += alpha*(*ptr2++);
1045 for(i = 0; i <= j; i++) {
1053 ptr1 = outputMatrix + j;
1055 for(i = 0; i <= j; i++) {
1056 *ptr1 += alpha*(*ptr2++);
1057 ptr1 += outputStride;
1060 for(i = 0; i <= j; i++) {
1062 ptr1 += outputStride;
1069 ptr2 = inputMatrix + (startRowCol+j) * inputStride + startRowCol + j;
1070 if (outputUpper ==
true) {
1073 ptr1 = outputMatrix + j*outputStride + j;
1075 for(i = j; i < numRowCols_in; i++) {
1076 *ptr1 += alpha*(*ptr2++);
1077 ptr1 += outputStride;
1080 for(i = j; i < numRowCols_in; i++) {
1082 ptr1 += outputStride;
1088 ptr1 = outputMatrix + j*outputStride + j;
1090 for(i = j; i < numRowCols_in; i++) {
1091 *ptr1++ += alpha*(*ptr2++);
1094 for(i = j; i < numRowCols_in; i++) {
1103 template<
typename OrdinalType,
typename ScalarType>
1105 bool inputUpper, ScalarType* inputMatrix,
1106 OrdinalType inputStride, OrdinalType inputRows
1110 ScalarType * ptr1 = 0;
1111 ScalarType * ptr2 = 0;
1114 for (j=1; j<inputRows; j++) {
1115 ptr1 = inputMatrix + j;
1116 ptr2 = inputMatrix + j*inputStride;
1117 for (i=0; i<j; i++) {
1124 for (i=1; i<inputRows; i++) {
1125 ptr1 = inputMatrix + i;
1126 ptr2 = inputMatrix + i*inputStride;
1127 for (j=0; j<i; j++) {
bool upper() const
Returns true if upper triangular part of this matrix has and will be used.
T magnitudeType
Mandatory typedef for result of magnitude.
SerialSymDenseMatrix()
Default constructor; defines a zero size object.
virtual void print(std::ostream &os) const
Print method. Defines the behavior of the std::ostream << operator inherited from the Object class...
Templated interface class to BLAS routines.
ScalarType * values() const
Returns the pointer to the ScalarType data array contained in the object.
ScalarType & operator()(OrdinalType rowIndex, OrdinalType colIndex)
Element access method (non-const).
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
Macro for throwing an exception with breakpointing to ease debugging.
int shape(OrdinalType numRowsCols)
Set dimensions of a Teuchos::SerialSymDenseMatrix object; init values to zero.
Teuchos header file which uses auto-configuration information to include necessary C++ headers...
This class creates and provides basic support for symmetric, positive-definite dense matrices of temp...
OrdinalType numRows() const
Returns the row dimension of this matrix.
ScalarTraits< ScalarType >::magnitudeType normInf() const
Returns the Infinity-norm of the matrix.
Object for storing data and providing functionality that is common to all computational classes...
int reshape(OrdinalType numRowsCols)
Reshape a Teuchos::SerialSymDenseMatrix object.
This structure defines some basic traits for a scalar field type.
void setLower()
Specify that the lower triangle of the this matrix should be used.
void setUpper()
Specify that the upper triangle of the this matrix should be used.
Functionality and data that is common to all computational classes.
SerialSymDenseMatrix< OrdinalType, ScalarType > & operator*=(const ScalarType alpha)
Inplace scalar-matrix product A = alpha*A.
bool operator==(const SerialSymDenseMatrix< OrdinalType, ScalarType > &Operand) const
Equality of two matrices.
int putScalar(const ScalarType value=Teuchos::ScalarTraits< ScalarType >::zero(), bool fullMatrix=false)
Set all values in the matrix to a constant value.
SerialSymDenseMatrix< OrdinalType, ScalarType > & operator+=(const SerialSymDenseMatrix< OrdinalType, ScalarType > &Source)
Add another matrix to this matrix.
ScalarTraits< ScalarType >::magnitudeType normOne() const
Returns the 1-norm of the matrix.
int random(const ScalarType bias=0.1 *Teuchos::ScalarTraits< ScalarType >::one())
Set all values in the active area (upper/lower triangle) of this matrix to be random numbers...
char UPLO() const
Returns character value of UPLO used by LAPACK routines.
virtual ~SerialSymDenseMatrix()
Teuchos::SerialSymDenseMatrix destructor.
static magnitudeType magnitude(T a)
Returns the magnitudeType of the scalar type a.
SerialSymDenseMatrix< OrdinalType, ScalarType > & operator-=(const SerialSymDenseMatrix< OrdinalType, ScalarType > &Source)
Subtract another matrix from this matrix.
The Teuchos namespace contains all of the classes, structs and enums used by Teuchos, as well as a number of utility routines.
OrdinalType ordinalType
Typedef for ordinal type.
SerialSymDenseMatrix< OrdinalType, ScalarType > & operator=(const SerialSymDenseMatrix< OrdinalType, ScalarType > &Source)
Copies values from one matrix to another.
int shapeUninitialized(OrdinalType numRowsCols)
Set dimensions of a Teuchos::SerialSymDenseMatrix object; don't initialize values.
Defines basic traits for the scalar field type.
static T zero()
Returns representation of zero for this scalar type.
static T random()
Returns a random number (between -one() and +one()) of this scalar type.
OrdinalType stride() const
Returns the stride between the columns of this matrix in memory.
OrdinalType numCols() const
Returns the column dimension of this matrix.
ScalarTraits< ScalarType >::magnitudeType normFrobenius() const
Returns the Frobenius-norm of the matrix.
SerialSymDenseMatrix< OrdinalType, ScalarType > & assign(const SerialSymDenseMatrix< OrdinalType, ScalarType > &Source)
Copies values from one matrix to another.
bool operator!=(const SerialSymDenseMatrix< OrdinalType, ScalarType > &Operand) const
Inequality of two matrices.
Teuchos::DataAccess Mode enumerable type.
bool empty() const
Returns whether this matrix is empty.
ScalarType scalarType
Typedef for scalar type.