43 #ifndef _TEUCHOS_SERIALSYMDENSEMATRIX_HPP_
44 #define _TEUCHOS_SERIALSYMDENSEMATRIX_HPP_
54 #include "Teuchos_Assert.hpp"
122 template<
typename OrdinalType,
typename ScalarType>
204 int shape(OrdinalType numRowsCols);
229 int reshape(OrdinalType numRowsCols);
300 ScalarType&
operator () (OrdinalType rowIndex, OrdinalType colIndex);
310 const ScalarType&
operator () (OrdinalType rowIndex, OrdinalType colIndex)
const;
315 ScalarType*
values()
const {
return(values_); }
323 bool upper()
const {
return(upper_);};
326 char UPLO()
const {
return(UPLO_);};
376 OrdinalType
numRows()
const {
return(numRowCols_); }
379 OrdinalType
numCols()
const {
return(numRowCols_); }
382 OrdinalType
stride()
const {
return(stride_); }
385 bool empty()
const {
return(numRowCols_ == 0); }
404 virtual void print(std::ostream& os)
const;
412 void scale(
const ScalarType alpha );
415 void copyMat(
bool inputUpper, ScalarType* inputMatrix, OrdinalType inputStride,
416 OrdinalType numRowCols,
bool outputUpper, ScalarType* outputMatrix,
417 OrdinalType outputStride, OrdinalType startRowCol,
421 void copyUPLOMat(
bool inputUpper, ScalarType* inputMatrix,
422 OrdinalType inputStride, OrdinalType inputRows);
425 void checkIndex( OrdinalType rowIndex, OrdinalType colIndex = 0 )
const;
426 OrdinalType numRowCols_;
440 template<
typename OrdinalType,
typename ScalarType>
443 numRowCols_(0), stride_(0), valuesCopied_(false), values_(0), upper_(false), UPLO_(
'L')
446 template<
typename OrdinalType,
typename ScalarType>
449 numRowCols_(numRowCols_in), stride_(numRowCols_in), valuesCopied_(false), upper_(false), UPLO_(
'L')
451 values_ =
new ScalarType[stride_*numRowCols_];
452 valuesCopied_ =
true;
457 template<
typename OrdinalType,
typename ScalarType>
459 DataAccess CV,
bool upper_in, ScalarType* values_in, OrdinalType stride_in, OrdinalType numRowCols_in
462 numRowCols_(numRowCols_in), stride_(stride_in), valuesCopied_(false),
463 values_(values_in), upper_(upper_in)
472 stride_ = numRowCols_;
473 values_ =
new ScalarType[stride_*numRowCols_];
474 copyMat(upper_in, values_in, stride_in, numRowCols_, upper_, values_, stride_, 0);
475 valuesCopied_ =
true;
479 template<
typename OrdinalType,
typename ScalarType>
482 numRowCols_(Source.numRowCols_), stride_(0), valuesCopied_(true),
483 values_(0), upper_(Source.upper_), UPLO_(Source.UPLO_)
485 if (!Source.valuesCopied_)
487 stride_ = Source.stride_;
488 values_ = Source.values_;
489 valuesCopied_ =
false;
493 stride_ = numRowCols_;
494 const OrdinalType newsize = stride_ * numRowCols_;
496 values_ =
new ScalarType[newsize];
497 copyMat(Source.upper_, Source.values_, Source.stride_, numRowCols_, upper_, values_, stride_, 0);
500 numRowCols_ = 0; stride_ = 0;
501 valuesCopied_ =
false;
506 template<
typename OrdinalType,
typename ScalarType>
509 ScalarType> &Source, OrdinalType numRowCols_in, OrdinalType startRowCol )
511 numRowCols_(numRowCols_in), stride_(Source.stride_), valuesCopied_(false), upper_(Source.upper_), UPLO_(Source.UPLO_)
515 stride_ = numRowCols_in;
516 values_ =
new ScalarType[stride_ * numRowCols_in];
517 copyMat(Source.upper_, Source.values_, Source.stride_, numRowCols_in, upper_, values_, stride_, startRowCol);
518 valuesCopied_ =
true;
522 values_ = Source.values_ + (stride_ * startRowCol) + startRowCol;
526 template<
typename OrdinalType,
typename ScalarType>
536 template<
typename OrdinalType,
typename ScalarType>
540 numRowCols_ = numRowCols_in;
541 stride_ = numRowCols_;
542 values_ =
new ScalarType[stride_*numRowCols_];
544 valuesCopied_ =
true;
548 template<
typename OrdinalType,
typename ScalarType>
552 numRowCols_ = numRowCols_in;
553 stride_ = numRowCols_;
554 values_ =
new ScalarType[stride_*numRowCols_];
555 valuesCopied_ =
true;
559 template<
typename OrdinalType,
typename ScalarType>
563 ScalarType* values_tmp =
new ScalarType[numRowCols_in * numRowCols_in];
565 for(OrdinalType k = 0; k < numRowCols_in * numRowCols_in; k++)
567 values_tmp[k] = zero;
569 OrdinalType numRowCols_tmp = TEUCHOS_MIN(numRowCols_, numRowCols_in);
572 copyMat(upper_, values_, stride_, numRowCols_tmp, upper_, values_tmp, numRowCols_in, 0);
575 numRowCols_ = numRowCols_in;
576 stride_ = numRowCols_;
577 values_ = values_tmp;
578 valuesCopied_ =
true;
586 template<
typename OrdinalType,
typename ScalarType>
590 if (upper_ !=
false) {
591 copyUPLOMat(
true, values_, stride_, numRowCols_ );
597 template<
typename OrdinalType,
typename ScalarType>
601 if (upper_ ==
false) {
602 copyUPLOMat(
false, values_, stride_, numRowCols_ );
608 template<
typename OrdinalType,
typename ScalarType>
612 if (fullMatrix ==
true) {
613 for(OrdinalType j = 0; j < numRowCols_; j++)
615 for(OrdinalType i = 0; i < numRowCols_; i++)
617 values_[i + j*stride_] = value_in;
624 for(OrdinalType j = 0; j < numRowCols_; j++) {
625 for(OrdinalType i = 0; i <= j; i++) {
626 values_[i + j*stride_] = value_in;
631 for(OrdinalType j = 0; j < numRowCols_; j++) {
632 for(OrdinalType i = j; i < numRowCols_; i++) {
633 values_[i + j*stride_] = value_in;
641 template<
typename OrdinalType,
typename ScalarType>
void
645 std::swap(values_ , B.values_);
646 std::swap(numRowCols_, B.numRowCols_);
647 std::swap(stride_, B.stride_);
648 std::swap(valuesCopied_, B.valuesCopied_);
649 std::swap(upper_, B.upper_);
650 std::swap(UPLO_, B.UPLO_);
653 template<
typename OrdinalType,
typename ScalarType>
661 for(OrdinalType j = 0; j < numRowCols_; j++) {
662 for(OrdinalType i = 0; i < j; i++) {
670 for(OrdinalType j = 0; j < numRowCols_; j++) {
671 for(OrdinalType i = j+1; i < numRowCols_; i++) {
680 for(OrdinalType i = 0; i < numRowCols_; i++) {
681 values_[i + i*stride_] = diagSum[i] + bias;
686 template<
typename OrdinalType,
typename ScalarType>
692 if((!valuesCopied_) && (!Source.valuesCopied_) && (values_ == Source.values_)) {
693 upper_ = Source.upper_;
698 if (!Source.valuesCopied_) {
703 numRowCols_ = Source.numRowCols_;
704 stride_ = Source.stride_;
705 values_ = Source.values_;
706 upper_ = Source.upper_;
707 UPLO_ = Source.UPLO_;
712 numRowCols_ = Source.numRowCols_;
713 stride_ = Source.numRowCols_;
714 upper_ = Source.upper_;
715 UPLO_ = Source.UPLO_;
716 const OrdinalType newsize = stride_ * numRowCols_;
718 values_ =
new ScalarType[newsize];
719 valuesCopied_ =
true;
727 if((Source.numRowCols_ <= stride_) && (Source.numRowCols_ == numRowCols_)) {
728 numRowCols_ = Source.numRowCols_;
729 upper_ = Source.upper_;
730 UPLO_ = Source.UPLO_;
734 numRowCols_ = Source.numRowCols_;
735 stride_ = Source.numRowCols_;
736 upper_ = Source.upper_;
737 UPLO_ = Source.UPLO_;
738 const OrdinalType newsize = stride_ * numRowCols_;
740 values_ =
new ScalarType[newsize];
741 valuesCopied_ =
true;
745 copyMat(Source.upper_, Source.values_, Source.stride_, Source.numRowCols_, upper_, values_, stride_, 0);
750 template<
typename OrdinalType,
typename ScalarType>
757 template<
typename OrdinalType,
typename ScalarType>
761 if ((numRowCols_ != Source.numRowCols_))
763 TEUCHOS_CHK_REF(*
this);
769 template<
typename OrdinalType,
typename ScalarType>
773 if ((numRowCols_ != Source.numRowCols_))
775 TEUCHOS_CHK_REF(*
this);
781 template<
typename OrdinalType,
typename ScalarType>
785 if((!valuesCopied_) && (!Source.valuesCopied_) && (values_ == Source.values_)) {
786 upper_ = Source.upper_;
791 if ((numRowCols_ != Source.numRowCols_))
793 TEUCHOS_CHK_REF(*
this);
795 copyMat(Source.upper_, Source.values_, Source.stride_, numRowCols_, upper_, values_, stride_, 0 );
803 template<
typename OrdinalType,
typename ScalarType>
806 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
807 checkIndex( rowIndex, colIndex );
809 if ( rowIndex <= colIndex ) {
812 return(values_[colIndex * stride_ + rowIndex]);
814 return(values_[rowIndex * stride_ + colIndex]);
819 return(values_[rowIndex * stride_ + colIndex]);
821 return(values_[colIndex * stride_ + rowIndex]);
825 template<
typename OrdinalType,
typename ScalarType>
828 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
829 checkIndex( rowIndex, colIndex );
831 if ( rowIndex <= colIndex ) {
834 return(values_[colIndex * stride_ + rowIndex]);
836 return(values_[rowIndex * stride_ + colIndex]);
841 return(values_[rowIndex * stride_ + colIndex]);
843 return(values_[colIndex * stride_ + rowIndex]);
851 template<
typename OrdinalType,
typename ScalarType>
857 template<
typename OrdinalType,
typename ScalarType>
868 for (j=0; j<numRowCols_; j++) {
870 ptr = values_ + j*stride_;
871 for (i=0; i<j; i++) {
874 ptr = values_ + j + j*stride_;
875 for (i=j; i<numRowCols_; i++) {
879 anorm = TEUCHOS_MAX( anorm, sum );
883 for (j=0; j<numRowCols_; j++) {
885 ptr = values_ + j + j*stride_;
886 for (i=j; i<numRowCols_; i++) {
890 for (i=0; i<j; i++) {
894 anorm = TEUCHOS_MAX( anorm, sum );
900 template<
typename OrdinalType,
typename ScalarType>
910 for (j = 0; j < numRowCols_; j++) {
911 for (i = 0; i < j; i++) {
918 for (j = 0; j < numRowCols_; j++) {
920 for (i = j+1; i < numRowCols_; i++) {
933 template<
typename OrdinalType,
typename ScalarType>
937 if((numRowCols_ != Operand.numRowCols_)) {
942 for(i = 0; i < numRowCols_; i++) {
943 for(j = 0; j < numRowCols_; j++) {
944 if((*
this)(i, j) != Operand(i, j)) {
953 template<
typename OrdinalType,
typename ScalarType>
956 return !((*this) == Operand);
963 template<
typename OrdinalType,
typename ScalarType>
970 for (j=0; j<numRowCols_; j++) {
971 ptr = values_ + j*stride_;
972 for (i=0; i<= j; i++) { *ptr = alpha * (*ptr); ptr++; }
976 for (j=0; j<numRowCols_; j++) {
977 ptr = values_ + j*stride_ + j;
978 for (i=j; i<numRowCols_; i++) { *ptr = alpha * (*ptr); ptr++; }
1012 template<
typename OrdinalType,
typename ScalarType>
1017 os <<
"Values_copied : yes" << std::endl;
1019 os <<
"Values_copied : no" << std::endl;
1020 os <<
"Rows / Columns : " << numRowCols_ << std::endl;
1021 os <<
"LDA : " << stride_ << std::endl;
1023 os <<
"Storage: Upper" << std::endl;
1025 os <<
"Storage: Lower" << std::endl;
1026 if(numRowCols_ == 0) {
1027 os <<
"(matrix is empty, no values to display)" << std::endl;
1029 for(OrdinalType i = 0; i < numRowCols_; i++) {
1030 for(OrdinalType j = 0; j < numRowCols_; j++){
1031 os << (*this)(i,j) <<
" ";
1042 template<
typename OrdinalType,
typename ScalarType>
1045 "SerialSymDenseMatrix<T>::checkIndex: "
1046 "Row index " << rowIndex <<
" out of range [0, "<< numRowCols_ <<
")");
1048 "SerialSymDenseMatrix<T>::checkIndex: "
1049 "Col index " << colIndex <<
" out of range [0, "<< numRowCols_ <<
")");
1052 template<
typename OrdinalType,
typename ScalarType>
1053 void SerialSymDenseMatrix<OrdinalType, ScalarType>::deleteArrays(
void)
1059 valuesCopied_ =
false;
1063 template<
typename OrdinalType,
typename ScalarType>
1064 void SerialSymDenseMatrix<OrdinalType, ScalarType>::copyMat(
1065 bool inputUpper, ScalarType* inputMatrix,
1066 OrdinalType inputStride, OrdinalType numRowCols_in,
1067 bool outputUpper, ScalarType* outputMatrix,
1068 OrdinalType outputStride, OrdinalType startRowCol,
1073 ScalarType* ptr1 = 0;
1074 ScalarType* ptr2 = 0;
1076 for (j = 0; j < numRowCols_in; j++) {
1077 if (inputUpper ==
true) {
1079 ptr2 = inputMatrix + (j + startRowCol) * inputStride + startRowCol;
1080 if (outputUpper ==
true) {
1082 ptr1 = outputMatrix + j*outputStride;
1084 for(i = 0; i <= j; i++) {
1085 *ptr1++ += alpha*(*ptr2++);
1088 for(i = 0; i <= j; i++) {
1096 ptr1 = outputMatrix + j;
1098 for(i = 0; i <= j; i++) {
1099 *ptr1 += alpha*(*ptr2++);
1100 ptr1 += outputStride;
1103 for(i = 0; i <= j; i++) {
1105 ptr1 += outputStride;
1112 ptr2 = inputMatrix + (startRowCol+j) * inputStride + startRowCol + j;
1113 if (outputUpper ==
true) {
1116 ptr1 = outputMatrix + j*outputStride + j;
1118 for(i = j; i < numRowCols_in; i++) {
1119 *ptr1 += alpha*(*ptr2++);
1120 ptr1 += outputStride;
1123 for(i = j; i < numRowCols_in; i++) {
1125 ptr1 += outputStride;
1131 ptr1 = outputMatrix + j*outputStride + j;
1133 for(i = j; i < numRowCols_in; i++) {
1134 *ptr1++ += alpha*(*ptr2++);
1137 for(i = j; i < numRowCols_in; i++) {
1146 template<
typename OrdinalType,
typename ScalarType>
1147 void SerialSymDenseMatrix<OrdinalType, ScalarType>::copyUPLOMat(
1148 bool inputUpper, ScalarType* inputMatrix,
1149 OrdinalType inputStride, OrdinalType inputRows
1153 ScalarType * ptr1 = 0;
1154 ScalarType * ptr2 = 0;
1157 for (j=1; j<inputRows; j++) {
1158 ptr1 = inputMatrix + j;
1159 ptr2 = inputMatrix + j*inputStride;
1160 for (i=0; i<j; i++) {
1167 for (i=1; i<inputRows; i++) {
1168 ptr1 = inputMatrix + i;
1169 ptr2 = inputMatrix + i*inputStride;
1170 for (j=0; j<i; j++) {
1178 #ifndef TEUCHOS_HIDE_DEPRECATED_CODE
1179 template<
typename OrdinalType,
typename ScalarType>
1181 std::ostream& operator<< (std::ostream& os, const Teuchos::SerialSymDenseMatrix<OrdinalType, ScalarType>& obj)
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.
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).
void swap(SerialSymDenseMatrix< OrdinalType, ScalarType > &B)
Swap values between this matrix and incoming matrix.
#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.
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.