11 #ifndef _TEUCHOS_SERIALSYMDENSEMATRIX_HPP_
12 #define _TEUCHOS_SERIALSYMDENSEMATRIX_HPP_
22 #include "Teuchos_Assert.hpp"
90 template<
typename OrdinalType,
typename ScalarType>
172 int shape(OrdinalType numRowsCols);
197 int reshape(OrdinalType numRowsCols);
268 ScalarType&
operator () (OrdinalType rowIndex, OrdinalType colIndex);
278 const ScalarType&
operator () (OrdinalType rowIndex, OrdinalType colIndex)
const;
283 ScalarType*
values()
const {
return(values_); }
291 bool upper()
const {
return(upper_);};
294 char UPLO()
const {
return(UPLO_);};
344 OrdinalType
numRows()
const {
return(numRowCols_); }
347 OrdinalType
numCols()
const {
return(numRowCols_); }
350 OrdinalType
stride()
const {
return(stride_); }
353 bool empty()
const {
return(numRowCols_ == 0); }
372 virtual std::ostream&
print(std::ostream& os)
const;
380 void scale(
const ScalarType alpha );
383 void copyMat(
bool inputUpper, ScalarType* inputMatrix, OrdinalType inputStride,
384 OrdinalType numRowCols,
bool outputUpper, ScalarType* outputMatrix,
385 OrdinalType outputStride, OrdinalType startRowCol,
389 void copyUPLOMat(
bool inputUpper, ScalarType* inputMatrix,
390 OrdinalType inputStride, OrdinalType inputRows);
393 void checkIndex( OrdinalType rowIndex, OrdinalType colIndex = 0 )
const;
396 allocateValues(
const OrdinalType
numRows,
399 const size_t size = size_t(numRows) * size_t(numCols);
400 #pragma GCC diagnostic push
401 #pragma GCC diagnostic ignored "-Wvla"
402 return new ScalarType[size];
403 #pragma GCC diagnostic pop
406 OrdinalType numRowCols_ = 0;
407 OrdinalType stride_ = 0;
408 bool valuesCopied_ =
false;
409 ScalarType* values_ =
nullptr;
418 template<
typename OrdinalType,
typename ScalarType>
420 : numRowCols_(numRowCols_in), stride_(numRowCols_in), valuesCopied_(false), upper_(false), UPLO_(
'L')
422 values_ = allocateValues(stride_, numRowCols_);
423 valuesCopied_ =
true;
430 template<
typename OrdinalType,
typename ScalarType>
432 DataAccess CV,
bool upper_in, ScalarType* values_in, OrdinalType stride_in, OrdinalType numRowCols_in
434 : numRowCols_(numRowCols_in), stride_(stride_in), valuesCopied_(false),
435 values_(values_in), upper_(upper_in)
444 stride_ = numRowCols_;
445 values_ = allocateValues(stride_, numRowCols_);
446 copyMat(upper_in, values_in, stride_in, numRowCols_, upper_, values_, stride_, 0);
447 valuesCopied_ =
true;
451 template<
typename OrdinalType,
typename ScalarType>
453 : numRowCols_(Source.numRowCols_), stride_(0), valuesCopied_(true),
454 values_(0), upper_(Source.upper_), UPLO_(Source.UPLO_)
456 if (!Source.valuesCopied_)
458 stride_ = Source.stride_;
459 values_ = Source.values_;
460 valuesCopied_ =
false;
464 stride_ = numRowCols_;
465 if(stride_ > 0 && numRowCols_ > 0) {
466 values_ = allocateValues(stride_, numRowCols_);
467 copyMat(Source.upper_, Source.values_, Source.stride_, numRowCols_, upper_, values_, stride_, 0);
472 valuesCopied_ =
false;
477 template<
typename OrdinalType,
typename ScalarType>
480 ScalarType> &Source, OrdinalType numRowCols_in, OrdinalType startRowCol )
482 numRowCols_(numRowCols_in), stride_(Source.stride_), valuesCopied_(false), upper_(Source.upper_), UPLO_(Source.UPLO_)
486 stride_ = numRowCols_in;
487 values_ = allocateValues(stride_, numRowCols_in);
488 copyMat(Source.upper_, Source.values_, Source.stride_, numRowCols_in, upper_, values_, stride_, startRowCol);
489 valuesCopied_ =
true;
493 values_ = Source.values_ + (stride_ * startRowCol) + startRowCol;
497 template<
typename OrdinalType,
typename ScalarType>
507 template<
typename OrdinalType,
typename ScalarType>
511 numRowCols_ = numRowCols_in;
512 stride_ = numRowCols_;
513 values_ = allocateValues(stride_, numRowCols_);
515 valuesCopied_ =
true;
519 template<
typename OrdinalType,
typename ScalarType>
523 numRowCols_ = numRowCols_in;
524 stride_ = numRowCols_;
525 values_ = allocateValues(stride_, numRowCols_);
526 valuesCopied_ =
true;
530 template<
typename OrdinalType,
typename ScalarType>
534 ScalarType* values_tmp = allocateValues(numRowCols_in, numRowCols_in);
536 for(OrdinalType k = 0; k < numRowCols_in * numRowCols_in; k++)
538 values_tmp[k] = zero;
540 OrdinalType numRowCols_tmp = TEUCHOS_MIN(numRowCols_, numRowCols_in);
543 copyMat(upper_, values_, stride_, numRowCols_tmp, upper_, values_tmp, numRowCols_in, 0);
546 numRowCols_ = numRowCols_in;
547 stride_ = numRowCols_;
548 values_ = values_tmp;
549 valuesCopied_ =
true;
557 template<
typename OrdinalType,
typename ScalarType>
561 if (upper_ !=
false) {
562 copyUPLOMat(
true, values_, stride_, numRowCols_ );
568 template<
typename OrdinalType,
typename ScalarType>
572 if (upper_ ==
false) {
573 copyUPLOMat(
false, values_, stride_, numRowCols_ );
579 template<
typename OrdinalType,
typename ScalarType>
583 if (fullMatrix ==
true) {
584 for(OrdinalType j = 0; j < numRowCols_; j++)
586 for(OrdinalType i = 0; i < numRowCols_; i++)
588 values_[i + j*stride_] = value_in;
595 for(OrdinalType j = 0; j < numRowCols_; j++) {
596 for(OrdinalType i = 0; i <= j; i++) {
597 values_[i + j*stride_] = value_in;
602 for(OrdinalType j = 0; j < numRowCols_; j++) {
603 for(OrdinalType i = j; i < numRowCols_; i++) {
604 values_[i + j*stride_] = value_in;
612 template<
typename OrdinalType,
typename ScalarType>
void
616 std::swap(values_ , B.values_);
617 std::swap(numRowCols_, B.numRowCols_);
618 std::swap(stride_, B.stride_);
619 std::swap(valuesCopied_, B.valuesCopied_);
620 std::swap(upper_, B.upper_);
621 std::swap(UPLO_, B.UPLO_);
624 template<
typename OrdinalType,
typename ScalarType>
632 for(OrdinalType j = 0; j < numRowCols_; j++) {
633 for(OrdinalType i = 0; i < j; i++) {
641 for(OrdinalType j = 0; j < numRowCols_; j++) {
642 for(OrdinalType i = j+1; i < numRowCols_; i++) {
651 for(OrdinalType i = 0; i < numRowCols_; i++) {
652 values_[i + i*stride_] = diagSum[i] + bias;
657 template<
typename OrdinalType,
typename ScalarType>
663 if((!valuesCopied_) && (!Source.valuesCopied_) && (values_ == Source.values_)) {
664 upper_ = Source.upper_;
669 if (!Source.valuesCopied_) {
674 numRowCols_ = Source.numRowCols_;
675 stride_ = Source.stride_;
676 values_ = Source.values_;
677 upper_ = Source.upper_;
678 UPLO_ = Source.UPLO_;
683 numRowCols_ = Source.numRowCols_;
684 stride_ = Source.numRowCols_;
685 upper_ = Source.upper_;
686 UPLO_ = Source.UPLO_;
687 if(stride_ > 0 && numRowCols_ > 0) {
688 values_ = allocateValues(stride_, numRowCols_);
689 valuesCopied_ =
true;
697 if((Source.numRowCols_ <= stride_) && (Source.numRowCols_ == numRowCols_)) {
698 numRowCols_ = Source.numRowCols_;
699 upper_ = Source.upper_;
700 UPLO_ = Source.UPLO_;
704 numRowCols_ = Source.numRowCols_;
705 stride_ = Source.numRowCols_;
706 upper_ = Source.upper_;
707 UPLO_ = Source.UPLO_;
708 if(stride_ > 0 && numRowCols_ > 0) {
709 values_ = allocateValues(stride_, numRowCols_);
710 valuesCopied_ =
true;
714 copyMat(Source.upper_, Source.values_, Source.stride_, Source.numRowCols_, upper_, values_, stride_, 0);
719 template<
typename OrdinalType,
typename ScalarType>
726 template<
typename OrdinalType,
typename ScalarType>
730 if ((numRowCols_ != Source.numRowCols_))
732 TEUCHOS_CHK_REF(*
this);
738 template<
typename OrdinalType,
typename ScalarType>
742 if ((numRowCols_ != Source.numRowCols_))
744 TEUCHOS_CHK_REF(*
this);
750 template<
typename OrdinalType,
typename ScalarType>
754 if((!valuesCopied_) && (!Source.valuesCopied_) && (values_ == Source.values_)) {
755 upper_ = Source.upper_;
760 if ((numRowCols_ != Source.numRowCols_))
762 TEUCHOS_CHK_REF(*
this);
764 copyMat(Source.upper_, Source.values_, Source.stride_, numRowCols_, upper_, values_, stride_, 0 );
772 template<
typename OrdinalType,
typename ScalarType>
775 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
776 checkIndex( rowIndex, colIndex );
778 if ( rowIndex <= colIndex ) {
781 return(values_[colIndex * stride_ + rowIndex]);
783 return(values_[rowIndex * stride_ + colIndex]);
788 return(values_[rowIndex * stride_ + colIndex]);
790 return(values_[colIndex * stride_ + rowIndex]);
794 template<
typename OrdinalType,
typename ScalarType>
797 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
798 checkIndex( rowIndex, colIndex );
800 if ( rowIndex <= colIndex ) {
803 return(values_[colIndex * stride_ + rowIndex]);
805 return(values_[rowIndex * stride_ + colIndex]);
810 return(values_[rowIndex * stride_ + colIndex]);
812 return(values_[colIndex * stride_ + rowIndex]);
820 template<
typename OrdinalType,
typename ScalarType>
826 template<
typename OrdinalType,
typename ScalarType>
837 for (j=0; j<numRowCols_; j++) {
839 ptr = values_ + j*stride_;
840 for (i=0; i<j; i++) {
843 ptr = values_ + j + j*stride_;
844 for (i=j; i<numRowCols_; i++) {
848 anorm = TEUCHOS_MAX( anorm, sum );
852 for (j=0; j<numRowCols_; j++) {
854 ptr = values_ + j + j*stride_;
855 for (i=j; i<numRowCols_; i++) {
859 for (i=0; i<j; i++) {
863 anorm = TEUCHOS_MAX( anorm, sum );
869 template<
typename OrdinalType,
typename ScalarType>
879 for (j = 0; j < numRowCols_; j++) {
880 for (i = 0; i < j; i++) {
887 for (j = 0; j < numRowCols_; j++) {
889 for (i = j+1; i < numRowCols_; i++) {
902 template<
typename OrdinalType,
typename ScalarType>
906 if((numRowCols_ != Operand.numRowCols_)) {
911 for(i = 0; i < numRowCols_; i++) {
912 for(j = 0; j < numRowCols_; j++) {
913 if((*
this)(i, j) != Operand(i, j)) {
922 template<
typename OrdinalType,
typename ScalarType>
925 return !((*this) == Operand);
932 template<
typename OrdinalType,
typename ScalarType>
939 for (j=0; j<numRowCols_; j++) {
940 ptr = values_ + j*stride_;
941 for (i=0; i<= j; i++) { *ptr = alpha * (*ptr); ptr++; }
945 for (j=0; j<numRowCols_; j++) {
946 ptr = values_ + j*stride_ + j;
947 for (i=j; i<numRowCols_; i++) { *ptr = alpha * (*ptr); ptr++; }
981 template<
typename OrdinalType,
typename ScalarType>
986 os <<
"Values_copied : yes" << std::endl;
988 os <<
"Values_copied : no" << std::endl;
989 os <<
"Rows / Columns : " << numRowCols_ << std::endl;
990 os <<
"LDA : " << stride_ << std::endl;
992 os <<
"Storage: Upper" << std::endl;
994 os <<
"Storage: Lower" << std::endl;
995 if(numRowCols_ == 0) {
996 os <<
"(matrix is empty, no values to display)" << std::endl;
998 for(OrdinalType i = 0; i < numRowCols_; i++) {
999 for(OrdinalType j = 0; j < numRowCols_; j++){
1000 os << (*this)(i,j) <<
" ";
1012 template<
typename OrdinalType,
typename ScalarType>
1015 "SerialSymDenseMatrix<T>::checkIndex: "
1016 "Row index " << rowIndex <<
" out of range [0, "<< numRowCols_ <<
")");
1018 "SerialSymDenseMatrix<T>::checkIndex: "
1019 "Col index " << colIndex <<
" out of range [0, "<< numRowCols_ <<
")");
1022 template<
typename OrdinalType,
typename ScalarType>
1023 void SerialSymDenseMatrix<OrdinalType, ScalarType>::deleteArrays(
void)
1029 valuesCopied_ =
false;
1033 template<
typename OrdinalType,
typename ScalarType>
1034 void SerialSymDenseMatrix<OrdinalType, ScalarType>::copyMat(
1035 bool inputUpper, ScalarType* inputMatrix,
1036 OrdinalType inputStride, OrdinalType numRowCols_in,
1037 bool outputUpper, ScalarType* outputMatrix,
1038 OrdinalType outputStride, OrdinalType startRowCol,
1043 ScalarType* ptr1 = 0;
1044 ScalarType* ptr2 = 0;
1046 for (j = 0; j < numRowCols_in; j++) {
1047 if (inputUpper ==
true) {
1049 ptr2 = inputMatrix + (j + startRowCol) * inputStride + startRowCol;
1050 if (outputUpper ==
true) {
1052 ptr1 = outputMatrix + j*outputStride;
1054 for(i = 0; i <= j; i++) {
1055 *ptr1++ += alpha*(*ptr2++);
1058 for(i = 0; i <= j; i++) {
1066 ptr1 = outputMatrix + j;
1068 for(i = 0; i <= j; i++) {
1069 *ptr1 += alpha*(*ptr2++);
1070 ptr1 += outputStride;
1073 for(i = 0; i <= j; i++) {
1075 ptr1 += outputStride;
1082 ptr2 = inputMatrix + (startRowCol+j) * inputStride + startRowCol + j;
1083 if (outputUpper ==
true) {
1086 ptr1 = outputMatrix + j*outputStride + j;
1088 for(i = j; i < numRowCols_in; i++) {
1089 *ptr1 += alpha*(*ptr2++);
1090 ptr1 += outputStride;
1093 for(i = j; i < numRowCols_in; i++) {
1095 ptr1 += outputStride;
1101 ptr1 = outputMatrix + j*outputStride + j;
1103 for(i = j; i < numRowCols_in; i++) {
1104 *ptr1++ += alpha*(*ptr2++);
1107 for(i = j; i < numRowCols_in; i++) {
1116 template<
typename OrdinalType,
typename ScalarType>
1117 void SerialSymDenseMatrix<OrdinalType, ScalarType>::copyUPLOMat(
1118 bool inputUpper, ScalarType* inputMatrix,
1119 OrdinalType inputStride, OrdinalType inputRows
1123 ScalarType * ptr1 = 0;
1124 ScalarType * ptr2 = 0;
1127 for (j=1; j<inputRows; j++) {
1128 ptr1 = inputMatrix + j;
1129 ptr2 = inputMatrix + j*inputStride;
1130 for (i=0; i<j; i++) {
1137 for (i=1; i<inputRows; i++) {
1138 ptr1 = inputMatrix + i;
1139 ptr2 = inputMatrix + i*inputStride;
1140 for (j=0; j<i; j++) {
1149 template<
typename OrdinalType,
typename ScalarType>
1159 template<
typename OrdinalType,
typename ScalarType>
1161 operator<<(std::ostream &out,
1164 printer.obj.print(out);
1169 template<
typename OrdinalType,
typename ScalarType>
1170 SerialSymDenseMatrixPrinter<OrdinalType,ScalarType>
bool upper() const
Returns true if upper triangular part of this matrix has and will be used.
Ostream manipulator for SerialSymDenseMatrix.
SerialSymDenseMatrix()=default
Default constructor; defines a zero size object.
T magnitudeType
Mandatory typedef for result of magnitude.
Templated interface class to BLAS routines.
virtual std::ostream & print(std::ostream &os) const
Print method. Defines the behavior of the std::ostream << operator.
SerialBandDenseMatrixPrinter< OrdinalType, ScalarType > printMat(const SerialBandDenseMatrix< OrdinalType, ScalarType > &obj)
Return SerialBandDenseMatrix ostream manipulator Use as:
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.