11#ifndef _TEUCHOS_SERIALSYMDENSEMATRIX_HPP_
12#define _TEUCHOS_SERIALSYMDENSEMATRIX_HPP_
22#include "Teuchos_Assert.hpp"
90template<
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); }
373 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,
400 return new ScalarType[size];
403 OrdinalType numRowCols_ = 0;
404 OrdinalType stride_ = 0;
405 bool valuesCopied_ =
false;
406 ScalarType* values_ =
nullptr;
415template<
typename OrdinalType,
typename ScalarType>
417 : numRowCols_(numRowCols_in), stride_(numRowCols_in), valuesCopied_(false), upper_(false), UPLO_(
'L')
419 values_ = allocateValues(stride_, numRowCols_);
420 valuesCopied_ =
true;
427template<
typename OrdinalType,
typename ScalarType>
429 DataAccess CV,
bool upper_in, ScalarType* values_in, OrdinalType stride_in, OrdinalType numRowCols_in
431 : numRowCols_(numRowCols_in), stride_(stride_in), valuesCopied_(false),
432 values_(values_in), upper_(upper_in)
441 stride_ = numRowCols_;
442 values_ = allocateValues(stride_, numRowCols_);
443 copyMat(upper_in, values_in, stride_in, numRowCols_, upper_, values_, stride_, 0);
444 valuesCopied_ =
true;
448template<
typename OrdinalType,
typename ScalarType>
450 : numRowCols_(Source.numRowCols_), stride_(0), valuesCopied_(true),
451 values_(0), upper_(Source.upper_), UPLO_(Source.UPLO_)
453 if (!Source.valuesCopied_)
455 stride_ = Source.stride_;
456 values_ = Source.values_;
457 valuesCopied_ = false;
461 stride_ = numRowCols_;
462 if(stride_ > 0 && numRowCols_ > 0) {
463 values_ = allocateValues(stride_, numRowCols_);
464 copyMat(Source.upper_, Source.values_, Source.stride_, numRowCols_, upper_, values_, stride_, 0);
469 valuesCopied_ = false;
474template<
typename OrdinalType,
typename ScalarType>
477 ScalarType> &Source, OrdinalType numRowCols_in, OrdinalType startRowCol )
479 numRowCols_(numRowCols_in), stride_(Source.stride_), valuesCopied_(false), upper_(Source.upper_), UPLO_(Source.UPLO_)
483 stride_ = numRowCols_in;
484 values_ = allocateValues(stride_, numRowCols_in);
485 copyMat(Source.upper_, Source.values_, Source.stride_, numRowCols_in, upper_, values_, stride_, startRowCol);
486 valuesCopied_ =
true;
490 values_ = Source.values_ + (stride_ * startRowCol) + startRowCol;
494template<
typename OrdinalType,
typename ScalarType>
504template<
typename OrdinalType,
typename ScalarType>
508 numRowCols_ = numRowCols_in;
509 stride_ = numRowCols_;
510 values_ = allocateValues(stride_, numRowCols_);
512 valuesCopied_ =
true;
516template<
typename OrdinalType,
typename ScalarType>
520 numRowCols_ = numRowCols_in;
521 stride_ = numRowCols_;
522 values_ = allocateValues(stride_, numRowCols_);
523 valuesCopied_ =
true;
527template<
typename OrdinalType,
typename ScalarType>
531 ScalarType* values_tmp = allocateValues(numRowCols_in, numRowCols_in);
533 for(OrdinalType k = 0; k < numRowCols_in * numRowCols_in; k++)
535 values_tmp[k] = zero;
537 OrdinalType numRowCols_tmp = TEUCHOS_MIN(numRowCols_, numRowCols_in);
540 copyMat(upper_, values_, stride_, numRowCols_tmp, upper_, values_tmp, numRowCols_in, 0);
543 numRowCols_ = numRowCols_in;
544 stride_ = numRowCols_;
545 values_ = values_tmp;
546 valuesCopied_ =
true;
554template<
typename OrdinalType,
typename ScalarType>
558 if (upper_ !=
false) {
559 copyUPLOMat(
true, values_, stride_, numRowCols_ );
565template<
typename OrdinalType,
typename ScalarType>
569 if (upper_ ==
false) {
570 copyUPLOMat(
false, values_, stride_, numRowCols_ );
576template<
typename OrdinalType,
typename ScalarType>
580 if (fullMatrix ==
true) {
581 for(OrdinalType j = 0; j < numRowCols_; j++)
583 for(OrdinalType i = 0; i < numRowCols_; i++)
585 values_[i + j*stride_] = value_in;
592 for(OrdinalType j = 0; j < numRowCols_; j++) {
593 for(OrdinalType i = 0; i <= j; i++) {
594 values_[i + j*stride_] = value_in;
599 for(OrdinalType j = 0; j < numRowCols_; j++) {
600 for(OrdinalType i = j; i < numRowCols_; i++) {
601 values_[i + j*stride_] = value_in;
609template<
typename OrdinalType,
typename ScalarType>
void
613 std::swap(values_ , B.values_);
614 std::swap(numRowCols_, B.numRowCols_);
615 std::swap(stride_, B.stride_);
616 std::swap(valuesCopied_, B.valuesCopied_);
617 std::swap(upper_, B.upper_);
618 std::swap(UPLO_, B.UPLO_);
621template<
typename OrdinalType,
typename ScalarType>
629 for(OrdinalType j = 0; j < numRowCols_; j++) {
630 for(OrdinalType i = 0; i < j; i++) {
638 for(OrdinalType j = 0; j < numRowCols_; j++) {
639 for(OrdinalType i = j+1; i < numRowCols_; i++) {
648 for(OrdinalType i = 0; i < numRowCols_; i++) {
649 values_[i + i*stride_] = diagSum[i] + bias;
654template<
typename OrdinalType,
typename ScalarType>
660 if((!valuesCopied_) && (!Source.valuesCopied_) && (values_ == Source.values_)) {
661 upper_ = Source.upper_;
666 if (!Source.valuesCopied_) {
671 numRowCols_ = Source.numRowCols_;
672 stride_ = Source.stride_;
673 values_ = Source.values_;
674 upper_ = Source.upper_;
675 UPLO_ = Source.UPLO_;
680 numRowCols_ = Source.numRowCols_;
681 stride_ = Source.numRowCols_;
682 upper_ = Source.upper_;
683 UPLO_ = Source.UPLO_;
684 if(stride_ > 0 && numRowCols_ > 0) {
685 values_ = allocateValues(stride_, numRowCols_);
686 valuesCopied_ =
true;
694 if((Source.numRowCols_ <= stride_) && (Source.numRowCols_ == numRowCols_)) {
695 numRowCols_ = Source.numRowCols_;
696 upper_ = Source.upper_;
697 UPLO_ = Source.UPLO_;
701 numRowCols_ = Source.numRowCols_;
702 stride_ = Source.numRowCols_;
703 upper_ = Source.upper_;
704 UPLO_ = Source.UPLO_;
705 if(stride_ > 0 && numRowCols_ > 0) {
706 values_ = allocateValues(stride_, numRowCols_);
707 valuesCopied_ =
true;
711 copyMat(Source.upper_, Source.values_, Source.stride_, Source.numRowCols_, upper_, values_, stride_, 0);
716template<
typename OrdinalType,
typename ScalarType>
723template<
typename OrdinalType,
typename ScalarType>
727 if ((numRowCols_ != Source.numRowCols_))
729 TEUCHOS_CHK_REF(*
this);
735template<
typename OrdinalType,
typename ScalarType>
739 if ((numRowCols_ != Source.numRowCols_))
741 TEUCHOS_CHK_REF(*
this);
747template<
typename OrdinalType,
typename ScalarType>
751 if((!valuesCopied_) && (!Source.valuesCopied_) && (values_ == Source.values_)) {
752 upper_ = Source.upper_;
757 if ((numRowCols_ != Source.numRowCols_))
759 TEUCHOS_CHK_REF(*
this);
761 copyMat(Source.upper_, Source.values_, Source.stride_, numRowCols_, upper_, values_, stride_, 0 );
769template<
typename OrdinalType,
typename ScalarType>
772#ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
773 checkIndex( rowIndex, colIndex );
775 if ( rowIndex <= colIndex ) {
778 return(values_[colIndex * stride_ + rowIndex]);
780 return(values_[rowIndex * stride_ + colIndex]);
785 return(values_[rowIndex * stride_ + colIndex]);
787 return(values_[colIndex * stride_ + rowIndex]);
791template<
typename OrdinalType,
typename ScalarType>
794#ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
795 checkIndex( rowIndex, colIndex );
797 if ( rowIndex <= colIndex ) {
800 return(values_[colIndex * stride_ + rowIndex]);
802 return(values_[rowIndex * stride_ + colIndex]);
807 return(values_[rowIndex * stride_ + colIndex]);
809 return(values_[colIndex * stride_ + rowIndex]);
817template<
typename OrdinalType,
typename ScalarType>
823template<
typename OrdinalType,
typename ScalarType>
834 for (j=0; j<numRowCols_; j++) {
836 ptr = values_ + j*stride_;
837 for (i=0; i<j; i++) {
840 ptr = values_ + j + j*stride_;
841 for (i=j; i<numRowCols_; i++) {
845 anorm = TEUCHOS_MAX( anorm, sum );
849 for (j=0; j<numRowCols_; j++) {
851 ptr = values_ + j + j*stride_;
852 for (i=j; i<numRowCols_; i++) {
856 for (i=0; i<j; i++) {
860 anorm = TEUCHOS_MAX( anorm, sum );
866template<
typename OrdinalType,
typename ScalarType>
876 for (j = 0; j < numRowCols_; j++) {
877 for (i = 0; i < j; i++) {
884 for (j = 0; j < numRowCols_; j++) {
886 for (i = j+1; i < numRowCols_; i++) {
899template<
typename OrdinalType,
typename ScalarType>
903 if((numRowCols_ != Operand.numRowCols_)) {
908 for(i = 0; i < numRowCols_; i++) {
909 for(j = 0; j < numRowCols_; j++) {
910 if((*
this)(i, j) != Operand(i, j)) {
919template<
typename OrdinalType,
typename ScalarType>
922 return !((*this) == Operand);
929template<
typename OrdinalType,
typename ScalarType>
930void SerialSymDenseMatrix<OrdinalType, ScalarType>::scale(
const ScalarType alpha )
936 for (j=0; j<numRowCols_; j++) {
937 ptr = values_ + j*stride_;
938 for (i=0; i<= j; i++) { *
ptr = alpha * (*ptr);
ptr++; }
942 for (j=0; j<numRowCols_; j++) {
943 ptr = values_ + j*stride_ + j;
944 for (i=j; i<numRowCols_; i++) { *
ptr = alpha * (*ptr);
ptr++; }
978template<
typename OrdinalType,
typename ScalarType>
983 os <<
"Values_copied : yes" << std::endl;
985 os <<
"Values_copied : no" << std::endl;
986 os <<
"Rows / Columns : " << numRowCols_ << std::endl;
987 os <<
"LDA : " << stride_ << std::endl;
989 os <<
"Storage: Upper" << std::endl;
991 os <<
"Storage: Lower" << std::endl;
992 if(numRowCols_ == 0) {
993 os <<
"(matrix is empty, no values to display)" << std::endl;
995 for(OrdinalType i = 0; i < numRowCols_; i++) {
996 for(OrdinalType j = 0; j < numRowCols_; j++){
997 os << (*this)(i,j) <<
" ";
1009template<
typename OrdinalType,
typename ScalarType>
1010inline void SerialSymDenseMatrix<OrdinalType, ScalarType>::checkIndex( OrdinalType rowIndex, OrdinalType colIndex )
const {
1012 "SerialSymDenseMatrix<T>::checkIndex: "
1013 "Row index " << rowIndex <<
" out of range [0, "<< numRowCols_ <<
")");
1015 "SerialSymDenseMatrix<T>::checkIndex: "
1016 "Col index " << colIndex <<
" out of range [0, "<< numRowCols_ <<
")");
1019template<
typename OrdinalType,
typename ScalarType>
1020void SerialSymDenseMatrix<OrdinalType, ScalarType>::deleteArrays(
void)
1026 valuesCopied_ =
false;
1030template<
typename OrdinalType,
typename ScalarType>
1031void SerialSymDenseMatrix<OrdinalType, ScalarType>::copyMat(
1032 bool inputUpper, ScalarType* inputMatrix,
1033 OrdinalType inputStride, OrdinalType numRowCols_in,
1034 bool outputUpper, ScalarType* outputMatrix,
1035 OrdinalType outputStride, OrdinalType startRowCol,
1040 ScalarType* ptr1 = 0;
1041 ScalarType* ptr2 = 0;
1043 for (j = 0; j < numRowCols_in; j++) {
1044 if (inputUpper ==
true) {
1046 ptr2 = inputMatrix + (j + startRowCol) * inputStride + startRowCol;
1047 if (outputUpper ==
true) {
1049 ptr1 = outputMatrix + j*outputStride;
1050 if (alpha != Teuchos::ScalarTraits<ScalarType>::zero() ) {
1051 for(i = 0; i <= j; i++) {
1052 *ptr1++ += alpha*(*ptr2++);
1055 for(i = 0; i <= j; i++) {
1063 ptr1 = outputMatrix + j;
1064 if (alpha != Teuchos::ScalarTraits<ScalarType>::zero() ) {
1065 for(i = 0; i <= j; i++) {
1066 *ptr1 += alpha*(*ptr2++);
1067 ptr1 += outputStride;
1070 for(i = 0; i <= j; i++) {
1072 ptr1 += outputStride;
1079 ptr2 = inputMatrix + (startRowCol+j) * inputStride + startRowCol + j;
1080 if (outputUpper ==
true) {
1083 ptr1 = outputMatrix + j*outputStride + j;
1084 if (alpha != Teuchos::ScalarTraits<ScalarType>::zero() ) {
1085 for(i = j; i < numRowCols_in; i++) {
1086 *ptr1 += alpha*(*ptr2++);
1087 ptr1 += outputStride;
1090 for(i = j; i < numRowCols_in; i++) {
1092 ptr1 += outputStride;
1098 ptr1 = outputMatrix + j*outputStride + j;
1099 if (alpha != Teuchos::ScalarTraits<ScalarType>::zero() ) {
1100 for(i = j; i < numRowCols_in; i++) {
1101 *ptr1++ += alpha*(*ptr2++);
1104 for(i = j; i < numRowCols_in; i++) {
1113template<
typename OrdinalType,
typename ScalarType>
1114void SerialSymDenseMatrix<OrdinalType, ScalarType>::copyUPLOMat(
1115 bool inputUpper, ScalarType* inputMatrix,
1116 OrdinalType inputStride, OrdinalType inputRows
1120 ScalarType * ptr1 = 0;
1121 ScalarType * ptr2 = 0;
1124 for (j=1; j<inputRows; j++) {
1125 ptr1 = inputMatrix + j;
1126 ptr2 = inputMatrix + j*inputStride;
1127 for (i=0; i<j; i++) {
1134 for (i=1; i<inputRows; i++) {
1135 ptr1 = inputMatrix + i;
1136 ptr2 = inputMatrix + i*inputStride;
1137 for (j=0; j<i; j++) {
1146template<
typename OrdinalType,
typename ScalarType>
1147struct SerialSymDenseMatrixPrinter {
1150 SerialSymDenseMatrixPrinter(
1156template<
typename OrdinalType,
typename ScalarType>
1158operator<<(std::ostream &out,
1161 printer.obj.print(out);
1166template<
typename OrdinalType,
typename ScalarType>
1167SerialSymDenseMatrixPrinter<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.
BLAS(void)
Default constructor.
CompObject()
Default constructor.
Ptr< T > ptr(T *p)
Create a pointer to an object from a raw pointer.
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:
static magnitudeType magnitude(T a)
Returns the magnitudeType of the scalar type a.
static T one()
Returns representation of one for this scalar type.
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.
static T squareroot(T x)
Returns a number of magnitudeType that is the square root of this scalar type x.
Ostream manipulator for SerialSymDenseMatrix.