11#ifndef _TEUCHOS_SERIALSYMDENSEMATRIX_HPP_
12#define _TEUCHOS_SERIALSYMDENSEMATRIX_HPP_
22#include "Teuchos_Assert.hpp"
90template<
typename OrdinalType,
typename ScalarType>
291 bool upper()
const {
return(upper_);};
294 char UPLO()
const {
return(UPLO_);};
353 bool empty()
const {
return(numRowCols_ == 0); }
373 virtual std::ostream& print(std::ostream& os)
const;
403 OrdinalType numRowCols_ = 0;
404 OrdinalType stride_ = 0;
405 bool valuesCopied_ =
false;
406 ScalarType* values_ =
nullptr;
415template<
typename OrdinalType,
typename ScalarType>
419 values_ = allocateValues(stride_, numRowCols_);
420 valuesCopied_ =
true;
427template<
typename OrdinalType,
typename ScalarType>
441 stride_ = numRowCols_;
442 values_ = allocateValues(stride_, numRowCols_);
444 valuesCopied_ =
true;
448template<
typename OrdinalType,
typename ScalarType>
452 numRowCols_(Source.numRowCols_), stride_(0), valuesCopied_(
true),
453 values_(0), upper_(Source.upper_), UPLO_(Source.UPLO_)
455 if (!Source.valuesCopied_)
457 stride_ = Source.stride_;
458 values_ = Source.values_;
459 valuesCopied_ =
false;
463 stride_ = numRowCols_;
464 if(stride_ > 0 && numRowCols_ > 0) {
465 values_ = allocateValues(stride_, numRowCols_);
466 copyMat(Source.upper_, Source.values_, Source.stride_, numRowCols_, upper_, values_, stride_, 0);
471 valuesCopied_ =
false;
476template<
typename OrdinalType,
typename ScalarType>
481 numRowCols_(
numRowCols_in), stride_(Source.stride_), valuesCopied_(
false), upper_(Source.upper_), UPLO_(Source.UPLO_)
488 valuesCopied_ =
true;
496template<
typename OrdinalType,
typename ScalarType>
506template<
typename OrdinalType,
typename ScalarType>
511 stride_ = numRowCols_;
512 values_ = allocateValues(stride_, numRowCols_);
514 valuesCopied_ =
true;
518template<
typename OrdinalType,
typename ScalarType>
523 stride_ = numRowCols_;
524 values_ = allocateValues(stride_, numRowCols_);
525 valuesCopied_ =
true;
529template<
typename OrdinalType,
typename ScalarType>
546 stride_ = numRowCols_;
548 valuesCopied_ =
true;
556template<
typename OrdinalType,
typename ScalarType>
560 if (upper_ !=
false) {
561 copyUPLOMat(
true, values_, stride_, numRowCols_ );
567template<
typename OrdinalType,
typename ScalarType>
571 if (upper_ ==
false) {
572 copyUPLOMat(
false, values_, stride_, numRowCols_ );
578template<
typename OrdinalType,
typename ScalarType>
611template<
typename OrdinalType,
typename ScalarType>
void
615 std::swap(values_ ,
B.values_);
616 std::swap(numRowCols_,
B.numRowCols_);
617 std::swap(stride_,
B.stride_);
618 std::swap(valuesCopied_,
B.valuesCopied_);
619 std::swap(upper_,
B.upper_);
620 std::swap(UPLO_,
B.UPLO_);
623template<
typename OrdinalType,
typename ScalarType>
656template<
typename OrdinalType,
typename ScalarType>
662 if((!valuesCopied_) && (!Source.valuesCopied_) && (values_ == Source.values_)) {
663 upper_ = Source.upper_;
668 if (!Source.valuesCopied_) {
673 numRowCols_ = Source.numRowCols_;
674 stride_ = Source.stride_;
675 values_ = Source.values_;
676 upper_ = Source.upper_;
677 UPLO_ = Source.UPLO_;
682 numRowCols_ = Source.numRowCols_;
683 stride_ = Source.numRowCols_;
684 upper_ = Source.upper_;
685 UPLO_ = Source.UPLO_;
686 if(stride_ > 0 && numRowCols_ > 0) {
687 values_ = allocateValues(stride_, numRowCols_);
688 valuesCopied_ =
true;
696 if((Source.numRowCols_ <= stride_) && (Source.numRowCols_ == numRowCols_)) {
697 numRowCols_ = Source.numRowCols_;
698 upper_ = Source.upper_;
699 UPLO_ = Source.UPLO_;
703 numRowCols_ = Source.numRowCols_;
704 stride_ = Source.numRowCols_;
705 upper_ = Source.upper_;
706 UPLO_ = Source.UPLO_;
707 if(stride_ > 0 && numRowCols_ > 0) {
708 values_ = allocateValues(stride_, numRowCols_);
709 valuesCopied_ =
true;
713 copyMat(Source.upper_, Source.values_, Source.stride_, Source.numRowCols_, upper_, values_, stride_, 0);
718template<
typename OrdinalType,
typename ScalarType>
725template<
typename OrdinalType,
typename ScalarType>
729 if ((numRowCols_ != Source.numRowCols_))
731 TEUCHOS_CHK_REF(*
this);
737template<
typename OrdinalType,
typename ScalarType>
741 if ((numRowCols_ != Source.numRowCols_))
743 TEUCHOS_CHK_REF(*
this);
749template<
typename OrdinalType,
typename ScalarType>
753 if((!valuesCopied_) && (!Source.valuesCopied_) && (values_ == Source.values_)) {
754 upper_ = Source.upper_;
759 if ((numRowCols_ != Source.numRowCols_))
761 TEUCHOS_CHK_REF(*
this);
763 copyMat(Source.upper_, Source.values_, Source.stride_, numRowCols_, upper_, values_, stride_, 0 );
771template<
typename OrdinalType,
typename ScalarType>
774#ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
775 checkIndex( rowIndex, colIndex );
777 if ( rowIndex <= colIndex ) {
780 return(values_[colIndex * stride_ + rowIndex]);
782 return(values_[rowIndex * stride_ + colIndex]);
787 return(values_[rowIndex * stride_ + colIndex]);
789 return(values_[colIndex * stride_ + rowIndex]);
793template<
typename OrdinalType,
typename ScalarType>
796#ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
797 checkIndex( rowIndex, colIndex );
799 if ( rowIndex <= colIndex ) {
802 return(values_[colIndex * stride_ + rowIndex]);
804 return(values_[rowIndex * stride_ + colIndex]);
809 return(values_[rowIndex * stride_ + colIndex]);
811 return(values_[colIndex * stride_ + rowIndex]);
819template<
typename OrdinalType,
typename ScalarType>
825template<
typename OrdinalType,
typename ScalarType>
836 for (
j=0;
j<numRowCols_;
j++) {
838 ptr = values_ +
j*stride_;
839 for (
i=0;
i<
j;
i++) {
842 ptr = values_ +
j +
j*stride_;
843 for (
i=
j;
i<numRowCols_;
i++) {
851 for (
j=0;
j<numRowCols_;
j++) {
853 ptr = values_ +
j +
j*stride_;
854 for (
i=
j;
i<numRowCols_;
i++) {
858 for (
i=0;
i<
j;
i++) {
868template<
typename OrdinalType,
typename ScalarType>
878 for (
j = 0;
j < numRowCols_;
j++) {
879 for (
i = 0;
i <
j;
i++) {
886 for (
j = 0;
j < numRowCols_;
j++) {
888 for (
i =
j+1;
i < numRowCols_;
i++) {
901template<
typename OrdinalType,
typename ScalarType>
905 if((numRowCols_ !=
Operand.numRowCols_)) {
910 for(
i = 0;
i < numRowCols_;
i++) {
911 for(
j = 0;
j < numRowCols_;
j++) {
921template<
typename OrdinalType,
typename ScalarType>
931template<
typename OrdinalType,
typename ScalarType>
938 for (
j=0;
j<numRowCols_;
j++) {
939 ptr = values_ +
j*stride_;
944 for (j=0; j<numRowCols_; j++) {
945 ptr = values_ + j*stride_ + j;
946 for (i=j; i<numRowCols_; i++) { *
ptr = alpha * (*ptr);
ptr++; }
980template<
typename OrdinalType,
typename ScalarType>
985 os <<
"Values_copied : yes" << std::endl;
987 os <<
"Values_copied : no" << std::endl;
988 os <<
"Rows / Columns : " << numRowCols_ << std::endl;
989 os <<
"LDA : " << stride_ << std::endl;
991 os <<
"Storage: Upper" << std::endl;
993 os <<
"Storage: Lower" << std::endl;
994 if(numRowCols_ == 0) {
995 os <<
"(matrix is empty, no values to display)" << std::endl;
999 os << (*this)(
i,
j) <<
" ";
1011template<
typename OrdinalType,
typename ScalarType>
1014 "SerialSymDenseMatrix<T>::checkIndex: "
1015 "Row index " << rowIndex <<
" out of range [0, "<< numRowCols_ <<
")");
1017 "SerialSymDenseMatrix<T>::checkIndex: "
1018 "Col index " << colIndex <<
" out of range [0, "<< numRowCols_ <<
")");
1021template<
typename OrdinalType,
typename ScalarType>
1022void SerialSymDenseMatrix<OrdinalType, ScalarType>::deleteArrays(
void)
1028 valuesCopied_ =
false;
1032template<
typename OrdinalType,
typename ScalarType>
1033void SerialSymDenseMatrix<OrdinalType, ScalarType>::copyMat(
1034 bool inputUpper, ScalarType* inputMatrix,
1035 OrdinalType inputStride, OrdinalType numRowCols_in,
1036 bool outputUpper, ScalarType* outputMatrix,
1037 OrdinalType outputStride, OrdinalType startRowCol,
1042 ScalarType* ptr1 = 0;
1043 ScalarType* ptr2 = 0;
1045 for (j = 0; j < numRowCols_in; j++) {
1046 if (inputUpper ==
true) {
1048 ptr2 = inputMatrix + (j + startRowCol) * inputStride + startRowCol;
1049 if (outputUpper ==
true) {
1051 ptr1 = outputMatrix + j*outputStride;
1053 for(i = 0; i <= j; i++) {
1054 *ptr1++ += alpha*(*ptr2++);
1057 for(i = 0; i <= j; i++) {
1065 ptr1 = outputMatrix + j;
1067 for(i = 0; i <= j; i++) {
1068 *ptr1 += alpha*(*ptr2++);
1069 ptr1 += outputStride;
1072 for(i = 0; i <= j; i++) {
1074 ptr1 += outputStride;
1081 ptr2 = inputMatrix + (startRowCol+j) * inputStride + startRowCol + j;
1082 if (outputUpper ==
true) {
1085 ptr1 = outputMatrix + j*outputStride + j;
1087 for(i = j; i < numRowCols_in; i++) {
1088 *ptr1 += alpha*(*ptr2++);
1089 ptr1 += outputStride;
1092 for(i = j; i < numRowCols_in; i++) {
1094 ptr1 += outputStride;
1100 ptr1 = outputMatrix + j*outputStride + j;
1102 for(i = j; i < numRowCols_in; i++) {
1103 *ptr1++ += alpha*(*ptr2++);
1106 for(i = j; i < numRowCols_in; i++) {
1115template<
typename OrdinalType,
typename ScalarType>
1116void SerialSymDenseMatrix<OrdinalType, ScalarType>::copyUPLOMat(
1117 bool inputUpper, ScalarType* inputMatrix,
1118 OrdinalType inputStride, OrdinalType inputRows
1122 ScalarType * ptr1 = 0;
1123 ScalarType * ptr2 = 0;
1126 for (j=1; j<inputRows; j++) {
1127 ptr1 = inputMatrix + j;
1128 ptr2 = inputMatrix + j*inputStride;
1129 for (i=0; i<j; i++) {
1136 for (i=1; i<inputRows; i++) {
1137 ptr1 = inputMatrix + i;
1138 ptr2 = inputMatrix + i*inputStride;
1139 for (j=0; j<i; j++) {
1148template<
typename OrdinalType,
typename ScalarType>
1158template<
typename OrdinalType,
typename ScalarType>
1168template<
typename OrdinalType,
typename ScalarType>
1169SerialSymDenseMatrixPrinter<OrdinalType,ScalarType>
Templated interface class to BLAS routines.
Object for storing data and providing functionality that is common to all computational classes.
Teuchos header file which uses auto-configuration information to include necessary C++ headers.
Teuchos::DataAccess Mode enumerable type.
Defines basic traits for the scalar field type.
Functionality and data that is common to all computational classes.
Smart reference counting pointer class for automatic garbage collection.
Ptr< T > ptr() const
Get a safer wrapper raw C++ pointer to the underlying object.
This class creates and provides basic support for symmetric, positive-definite dense matrices of temp...
SerialSymDenseMatrix()=default
Default constructor; defines a zero size object.
bool operator==(const SerialSymDenseMatrix< OrdinalType, ScalarType > &Operand) const
Equality of two matrices.
int shapeUninitialized(OrdinalType numRowsCols)
Set dimensions of a Teuchos::SerialSymDenseMatrix object; don't initialize values.
int shape(OrdinalType numRowsCols)
Set dimensions of a Teuchos::SerialSymDenseMatrix object; init values to zero.
ScalarTraits< ScalarType >::magnitudeType normFrobenius() const
Returns the Frobenius-norm of the matrix.
ScalarType & operator()(OrdinalType rowIndex, OrdinalType colIndex)
Element access method (non-const).
OrdinalType stride() const
Returns the stride between the columns of this matrix in memory.
SerialSymDenseMatrix< OrdinalType, ScalarType > & operator-=(const SerialSymDenseMatrix< OrdinalType, ScalarType > &Source)
Subtract another matrix from this matrix.
virtual std::ostream & print(std::ostream &os) const
Print method. Defines the behavior of the std::ostream << operator.
SerialSymDenseMatrix< OrdinalType, ScalarType > & operator+=(const SerialSymDenseMatrix< OrdinalType, ScalarType > &Source)
Add another matrix to this matrix.
bool operator!=(const SerialSymDenseMatrix< OrdinalType, ScalarType > &Operand) const
Inequality of two matrices.
void swap(SerialSymDenseMatrix< OrdinalType, ScalarType > &B)
Swap values between this matrix and incoming matrix.
OrdinalType numCols() const
Returns the column dimension of this matrix.
int putScalar(const ScalarType value=Teuchos::ScalarTraits< ScalarType >::zero(), bool fullMatrix=false)
Set all values in the matrix to a constant value.
virtual ~SerialSymDenseMatrix()
Teuchos::SerialSymDenseMatrix destructor.
char UPLO() const
Returns character value of UPLO used by LAPACK routines.
SerialSymDenseMatrix< OrdinalType, ScalarType > & operator*=(const ScalarType alpha)
Inplace scalar-matrix product A = alpha*A.
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.
ScalarType scalarType
Typedef for scalar type.
bool upper() const
Returns true if upper triangular part of this matrix has and will be used.
ScalarTraits< ScalarType >::magnitudeType normInf() const
Returns the Infinity-norm of the matrix.
ScalarType * values() const
Returns the pointer to the ScalarType data array contained in the object.
SerialSymDenseMatrix< OrdinalType, ScalarType > & operator=(const SerialSymDenseMatrix< OrdinalType, ScalarType > &Source)
Copies values from one matrix to another.
bool empty() const
Returns whether this matrix is empty.
SerialSymDenseMatrix< OrdinalType, ScalarType > & assign(const SerialSymDenseMatrix< OrdinalType, ScalarType > &Source)
Copies values from one matrix to another.
void setLower()
Specify that the lower triangle of the this matrix should be used.
int reshape(OrdinalType numRowsCols)
Reshape a Teuchos::SerialSymDenseMatrix object.
OrdinalType numRows() const
Returns the row dimension of this matrix.
void setUpper()
Specify that the upper triangle of the this matrix should be used.
ScalarTraits< ScalarType >::magnitudeType normOne() const
Returns the 1-norm of the matrix.
OrdinalType ordinalType
Typedef for ordinal type.
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
Macro for throwing an exception with breakpointing to ease debugging.
The Teuchos namespace contains all of the classes, structs and enums used by Teuchos,...
SerialBandDenseMatrixPrinter< OrdinalType, ScalarType > printMat(const SerialBandDenseMatrix< OrdinalType, ScalarType > &obj)
Return SerialBandDenseMatrix ostream manipulator Use as:
This structure defines some basic traits for a scalar field type.
static magnitudeType magnitude(T a)
Returns the magnitudeType of the scalar type a.
T magnitudeType
Mandatory typedef for result of magnitude.
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.
Ostream manipulator for SerialSymDenseMatrix.