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>
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>
479 numRowCols_(
numRowCols_in), stride_(Source.stride_), valuesCopied_(
false), upper_(Source.upper_), UPLO_(Source.UPLO_)
486 valuesCopied_ =
true;
494template<
typename OrdinalType,
typename ScalarType>
504template<
typename OrdinalType,
typename ScalarType>
509 stride_ = numRowCols_;
510 values_ = allocateValues(stride_, numRowCols_);
512 valuesCopied_ =
true;
516template<
typename OrdinalType,
typename ScalarType>
521 stride_ = numRowCols_;
522 values_ = allocateValues(stride_, numRowCols_);
523 valuesCopied_ =
true;
527template<
typename OrdinalType,
typename ScalarType>
544 stride_ = numRowCols_;
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>
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>
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++) {
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++) {
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++) {
919template<
typename OrdinalType,
typename ScalarType>
929template<
typename OrdinalType,
typename ScalarType>
936 for (
j=0;
j<numRowCols_;
j++) {
937 ptr = values_ +
j*stride_;
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;
997 os << (*this)(
i,
j) <<
" ";
1009template<
typename OrdinalType,
typename ScalarType>
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;
1051 for(i = 0; i <= j; i++) {
1052 *ptr1++ += alpha*(*ptr2++);
1055 for(i = 0; i <= j; i++) {
1063 ptr1 = outputMatrix + j;
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;
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;
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>
1156template<
typename OrdinalType,
typename ScalarType>
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.
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.