40 #ifndef _TEUCHOS_SERIALDENSEMATRIX_HPP_
41 #define _TEUCHOS_SERIALDENSEMATRIX_HPP_
51 #include "Teuchos_Assert.hpp"
66 template<
typename OrdinalType,
typename ScalarType>
228 ScalarType&
operator () (OrdinalType rowIndex, OrdinalType colIndex);
238 const ScalarType&
operator () (OrdinalType rowIndex, OrdinalType colIndex)
const;
254 const ScalarType*
operator [] (OrdinalType colIndex)
const;
258 ScalarType*
values()
const {
return values_; }
288 int scale (
const ScalarType alpha );
353 OrdinalType
numRows()
const {
return(numRows_); }
356 OrdinalType
numCols()
const {
return(numCols_); }
359 OrdinalType
stride()
const {
return(stride_); }
362 bool empty()
const {
return(numRows_ == 0 || numCols_ == 0); }
380 virtual std::ostream&
print(std::ostream& os)
const;
385 void copyMat(ScalarType* inputMatrix, OrdinalType strideInput,
386 OrdinalType
numRows, OrdinalType
numCols, ScalarType* outputMatrix,
387 OrdinalType strideOutput, OrdinalType startRow, OrdinalType startCol,
390 void checkIndex( OrdinalType rowIndex, OrdinalType colIndex = 0 )
const;
393 allocateValues(
const OrdinalType numRows,
394 const OrdinalType numCols)
396 const size_t size = size_t(numRows) * size_t(numCols);
397 #pragma GCC diagnostic push
398 #pragma GCC diagnostic ignored "-Wvla"
399 return new ScalarType[size];
400 #pragma GCC diagnostic pop
403 OrdinalType numRows_ = 0;
404 OrdinalType numCols_ = 0;
405 OrdinalType stride_ = 0;
406 bool valuesCopied_ =
false;
407 ScalarType* values_ =
nullptr;
414 template<
typename OrdinalType,
typename ScalarType>
416 OrdinalType numRows_in, OrdinalType numCols_in,
bool zeroOut
418 : numRows_(numRows_in),
419 numCols_(numCols_in),
422 values_(allocateValues(numRows_in, numCols_in))
429 template<
typename OrdinalType,
typename ScalarType>
431 DataAccess CV, ScalarType* values_in, OrdinalType stride_in, OrdinalType numRows_in,
432 OrdinalType numCols_in
434 : numRows_(numRows_in),
435 numCols_(numCols_in),
437 valuesCopied_(false),
443 values_ = allocateValues(stride_, numCols_);
444 copyMat(values_in, stride_in, numRows_, numCols_, values_, stride_, 0, 0);
445 valuesCopied_ =
true;
449 template<
typename OrdinalType,
typename ScalarType>
451 : valuesCopied_(true)
455 numRows_ = Source.numRows_;
456 numCols_ = Source.numCols_;
458 if (!Source.valuesCopied_)
460 stride_ = Source.stride_;
461 values_ = Source.values_;
462 valuesCopied_ =
false;
467 if(stride_ > 0 && numCols_ > 0) {
468 values_ = allocateValues(stride_, numCols_);
469 copyMat(Source.values_, Source.stride_, numRows_, numCols_, values_, stride_, 0, 0);
472 numRows_ = 0; numCols_ = 0; stride_ = 0;
473 valuesCopied_ =
false;
479 numRows_ = Source.numCols_;
480 numCols_ = Source.numRows_;
482 values_ = allocateValues(stride_, numCols_);
483 for (OrdinalType j=0; j<numCols_; j++) {
484 for (OrdinalType i=0; i<numRows_; i++) {
491 numRows_ = Source.numCols_;
492 numCols_ = Source.numRows_;
494 values_ = allocateValues(stride_, numCols_);
495 for (OrdinalType j=0; j<numCols_; j++) {
496 for (OrdinalType i=0; i<numRows_; i++) {
497 values_[j*stride_ + i] = Source.values_[i*Source.stride_ + j];
504 template<
typename OrdinalType,
typename ScalarType>
508 : numRows_(Source.numRows_), numCols_(Source.numCols_), stride_(Source.stride_),
509 valuesCopied_(false), values_(Source.values_)
514 values_ = allocateValues(stride_, numCols_);
515 copyMat(Source.values_, Source.stride_, numRows_, numCols_, values_, stride_, 0, 0);
516 valuesCopied_ =
true;
521 template<
typename OrdinalType,
typename ScalarType>
524 OrdinalType numRows_in, OrdinalType numCols_in, OrdinalType startRow,
527 :
CompObject(), numRows_(numRows_in), numCols_(numCols_in), stride_(Source.stride_),
528 valuesCopied_(false), values_(Source.values_)
532 stride_ = numRows_in;
533 values_ = allocateValues(stride_, numCols_in);
534 copyMat(Source.values_, Source.stride_, numRows_in, numCols_in, values_, stride_, startRow, startCol);
535 valuesCopied_ =
true;
539 values_ = values_ + (stride_ * startCol) + startRow;
543 template<
typename OrdinalType,
typename ScalarType>
553 template<
typename OrdinalType,
typename ScalarType>
555 OrdinalType numRows_in, OrdinalType numCols_in
559 numRows_ = numRows_in;
560 numCols_ = numCols_in;
562 values_ = allocateValues(stride_, numCols_);
564 valuesCopied_ =
true;
568 template<
typename OrdinalType,
typename ScalarType>
570 OrdinalType numRows_in, OrdinalType numCols_in
574 numRows_ = numRows_in;
575 numCols_ = numCols_in;
577 values_ = allocateValues(stride_, numCols_);
578 valuesCopied_ =
true;
582 template<
typename OrdinalType,
typename ScalarType>
584 OrdinalType numRows_in, OrdinalType numCols_in
588 ScalarType* values_tmp = allocateValues(numRows_in, numCols_in);
590 for(OrdinalType k = 0; k < numRows_in * numCols_in; k++)
592 values_tmp[k] = zero;
594 OrdinalType numRows_tmp = TEUCHOS_MIN(numRows_, numRows_in);
595 OrdinalType numCols_tmp = TEUCHOS_MIN(numCols_, numCols_in);
598 copyMat(values_, stride_, numRows_tmp, numCols_tmp, values_tmp,
602 numRows_ = numRows_in;
603 numCols_ = numCols_in;
605 values_ = values_tmp;
606 valuesCopied_ =
true;
614 template<
typename OrdinalType,
typename ScalarType>
618 for(OrdinalType j = 0; j < numCols_; j++)
620 for(OrdinalType i = 0; i < numRows_; i++)
622 values_[i + j*stride_] = value_in;
628 template<
typename OrdinalType,
typename ScalarType>
void
646 std::swap(values_ , B.values_);
647 std::swap(numRows_, B.numRows_);
648 std::swap(numCols_, B.numCols_);
649 std::swap(stride_, B.stride_);
650 std::swap(valuesCopied_, B.valuesCopied_);
653 template<
typename OrdinalType,
typename ScalarType>
657 for(OrdinalType j = 0; j < numCols_; j++)
659 for(OrdinalType i = 0; i < numRows_; i++)
667 template<
typename OrdinalType,
typename ScalarType>
675 if((!valuesCopied_) && (!Source.valuesCopied_) && (values_ == Source.values_))
679 if (!Source.valuesCopied_) {
684 numRows_ = Source.numRows_;
685 numCols_ = Source.numCols_;
686 stride_ = Source.stride_;
687 values_ = Source.values_;
692 numRows_ = Source.numRows_;
693 numCols_ = Source.numCols_;
694 stride_ = Source.numRows_;
695 if(stride_ > 0 && numCols_ > 0) {
696 values_ = allocateValues(stride_, numCols_);
697 valuesCopied_ =
true;
705 if((Source.numRows_ <= stride_) && (Source.numCols_ == numCols_)) {
706 numRows_ = Source.numRows_;
707 numCols_ = Source.numCols_;
711 numRows_ = Source.numRows_;
712 numCols_ = Source.numCols_;
713 stride_ = Source.numRows_;
714 if(stride_ > 0 && numCols_ > 0) {
715 values_ = allocateValues(stride_, numCols_);
716 valuesCopied_ =
true;
720 copyMat(Source.values_, Source.stride_, numRows_, numCols_, values_, stride_, 0, 0);
725 template<
typename OrdinalType,
typename ScalarType>
729 if ((numRows_ != Source.numRows_) || (numCols_ != Source.numCols_))
731 TEUCHOS_CHK_REF(*
this);
737 template<
typename OrdinalType,
typename ScalarType>
741 if ((numRows_ != Source.numRows_) || (numCols_ != Source.numCols_))
743 TEUCHOS_CHK_REF(*
this);
749 template<
typename OrdinalType,
typename ScalarType>
753 if((!valuesCopied_) && (!Source.valuesCopied_) && (values_ == Source.values_))
757 if ((numRows_ != Source.numRows_) || (numCols_ != Source.numCols_))
759 TEUCHOS_CHK_REF(*
this);
761 copyMat(Source.values_, Source.stride_, numRows_, numCols_, values_, stride_, 0, 0);
769 template<
typename OrdinalType,
typename ScalarType>
772 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
773 checkIndex( rowIndex, colIndex );
775 return(values_[colIndex * stride_ + rowIndex]);
778 template<
typename OrdinalType,
typename ScalarType>
781 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
782 checkIndex( rowIndex, colIndex );
784 return(values_[colIndex * stride_ + rowIndex]);
787 template<
typename OrdinalType,
typename ScalarType>
790 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
791 checkIndex( 0, colIndex );
793 return(values_ + colIndex * stride_);
796 template<
typename OrdinalType,
typename ScalarType>
799 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
800 checkIndex( 0, colIndex );
802 return(values_ + colIndex * stride_);
809 template<
typename OrdinalType,
typename ScalarType>
816 for(j = 0; j < numCols_; j++)
819 ptr = values_ + j * stride_;
820 for(i = 0; i < numRows_; i++)
831 if (flopCounter_!=0) updateFlops(numRows_ * numCols_);
835 template<
typename OrdinalType,
typename ScalarType>
841 for (i = 0; i < numRows_; i++) {
843 for (j=0; j< numCols_; j++) {
846 anorm = TEUCHOS_MAX( anorm, sum );
849 if (flopCounter_!=0) updateFlops(numRows_ * numCols_);
853 template<
typename OrdinalType,
typename ScalarType>
858 for (j = 0; j < numCols_; j++) {
859 for (i = 0; i < numRows_; i++) {
865 if (flopCounter_!=0) updateFlops(numRows_ * numCols_);
873 template<
typename OrdinalType,
typename ScalarType>
877 if((numRows_ != Operand.numRows_) || (numCols_ != Operand.numCols_))
884 for(i = 0; i < numRows_; i++)
886 for(j = 0; j < numCols_; j++)
888 if((*
this)(i, j) != Operand(i, j))
898 template<
typename OrdinalType,
typename ScalarType>
901 return !((*this) == Operand);
908 template<
typename OrdinalType,
typename ScalarType>
911 this->scale( alpha );
915 template<
typename OrdinalType,
typename ScalarType>
921 for (j=0; j<numCols_; j++) {
922 ptr = values_ + j*stride_;
923 for (i=0; i<numRows_; i++) { *ptr = alpha * (*ptr); ptr++; }
926 if (flopCounter_!=0) updateFlops( numRows_*numCols_ );
930 template<
typename OrdinalType,
typename ScalarType>
937 if ((numRows_ != A.numRows_) || (numCols_ != A.numCols_))
941 for (j=0; j<numCols_; j++) {
942 ptr = values_ + j*stride_;
943 for (i=0; i<numRows_; i++) { *ptr = A(i,j) * (*ptr); ptr++; }
946 if (flopCounter_!=0) updateFlops( numRows_*numCols_ );
950 template<
typename OrdinalType,
typename ScalarType>
954 OrdinalType A_nrows = (ETranspChar[transa]!=
'N') ? A.
numCols() : A.
numRows();
955 OrdinalType A_ncols = (ETranspChar[transa]!=
'N') ? A.
numRows() : A.
numCols();
956 OrdinalType B_nrows = (ETranspChar[transb]!=
'N') ? B.
numCols() : B.
numRows();
957 OrdinalType B_ncols = (ETranspChar[transb]!=
'N') ? B.
numRows() : B.
numCols();
958 if ((numRows_ != A_nrows) || (A_ncols != B_nrows) || (numCols_ != B_ncols))
963 this->GEMM(transa, transb, numRows_, numCols_, A_ncols, alpha, A.
values(), A.
stride(), B.
values(), B.
stride(), beta, values_, stride_);
966 if (flopCounter_!=0) {
967 double nflops = 2 * numRows_;
975 template<
typename OrdinalType,
typename ScalarType>
982 if (ESideChar[sideA]==
'L') {
983 if ((numRows_ != A_nrows) || (A_ncols != B_nrows) || (numCols_ != B_ncols)) {
987 if ((numRows_ != B_nrows) || (B_ncols != A_nrows) || (numCols_ != A_ncols)) {
994 this->SYMM(sideA, uplo, numRows_, numCols_, alpha, A.
values(), A.
stride(), B.
values(), B.
stride(), beta, values_, stride_);
997 if (flopCounter_!=0) {
998 double nflops = 2 * numRows_;
1001 updateFlops(nflops);
1006 template<
typename OrdinalType,
typename ScalarType>
1011 os <<
"Values_copied : yes" << std::endl;
1013 os <<
"Values_copied : no" << std::endl;
1014 os <<
"Rows : " << numRows_ << std::endl;
1015 os <<
"Columns : " << numCols_ << std::endl;
1016 os <<
"LDA : " << stride_ << std::endl;
1017 if(numRows_ == 0 || numCols_ == 0) {
1018 os <<
"(matrix is empty, no values to display)" << std::endl;
1020 for(OrdinalType i = 0; i < numRows_; i++) {
1021 for(OrdinalType j = 0; j < numCols_; j++){
1022 os << (*this)(i,j) <<
" ";
1034 template<
typename OrdinalType,
typename ScalarType>
1037 "SerialDenseMatrix<T>::checkIndex: "
1038 "Row index " << rowIndex <<
" out of range [0, "<< numRows_ <<
")");
1040 "SerialDenseMatrix<T>::checkIndex: "
1041 "Col index " << colIndex <<
" out of range [0, "<< numCols_ <<
")");
1044 template<
typename OrdinalType,
typename ScalarType>
1045 void SerialDenseMatrix<OrdinalType, ScalarType>::deleteArrays(
void)
1051 valuesCopied_ =
false;
1055 template<
typename OrdinalType,
typename ScalarType>
1056 void SerialDenseMatrix<OrdinalType, ScalarType>::copyMat(
1057 ScalarType* inputMatrix, OrdinalType strideInput, OrdinalType numRows_in,
1058 OrdinalType numCols_in, ScalarType* outputMatrix, OrdinalType strideOutput,
1059 OrdinalType startRow, OrdinalType startCol, ScalarType alpha
1063 ScalarType* ptr1 = 0;
1064 ScalarType* ptr2 = 0;
1065 for(j = 0; j < numCols_in; j++) {
1066 ptr1 = outputMatrix + (j * strideOutput);
1067 ptr2 = inputMatrix + (j + startCol) * strideInput + startRow;
1069 for(i = 0; i < numRows_in; i++)
1071 *ptr1++ += alpha*(*ptr2++);
1074 for(i = 0; i < numRows_in; i++)
1083 template<
typename OrdinalType,
typename ScalarType>
1093 template<
typename OrdinalType,
typename ScalarType>
1095 operator<<(std::ostream &out,
1098 printer.obj.print(out);
1103 template<
typename OrdinalType,
typename ScalarType>
1104 SerialDenseMatrixPrinter<OrdinalType,ScalarType>
ScalarTraits< ScalarType >::magnitudeType normOne() const
Returns the 1-norm of the matrix.
ScalarType * values() const
Data array access method.
bool upper() const
Returns true if upper triangular part of this matrix has and will be used.
bool operator==(const SerialDenseMatrix< OrdinalType, ScalarType > &Operand) const
Equality of two matrices.
int shapeUninitialized(OrdinalType numRows, OrdinalType numCols)
Same as shape() except leaves uninitialized.
Templated interface class to BLAS routines.
SerialDenseMatrix< OrdinalType, ScalarType > & operator-=(const SerialDenseMatrix< OrdinalType, ScalarType > &Source)
Subtract another matrix from this matrix.
SerialBandDenseMatrixPrinter< OrdinalType, ScalarType > printMat(const SerialBandDenseMatrix< OrdinalType, ScalarType > &obj)
Return SerialBandDenseMatrix ostream manipulator Use as:
SerialDenseMatrix< OrdinalType, ScalarType > & operator*=(const ScalarType alpha)
Scale this matrix by alpha; *this = alpha**this.
void swap(SerialDenseMatrix< OrdinalType, ScalarType > &B)
Swap values between this matrix and incoming matrix.
ScalarType * values() const
Returns the pointer to the ScalarType data array contained in the object.
int multiply(ETransp transa, ETransp transb, ScalarType alpha, const SerialDenseMatrix< OrdinalType, ScalarType > &A, const SerialDenseMatrix< OrdinalType, ScalarType > &B, ScalarType beta)
Multiply A * B and add them to this; this = beta * this + alpha*A*B.
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
Macro for throwing an exception with breakpointing to ease debugging.
virtual std::ostream & print(std::ostream &os) const
Print method. Defines the behavior of the std::ostream << operator.
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...
int scale(const ScalarType alpha)
Scale this matrix by alpha; *this = alpha**this.
OrdinalType numRows() const
Returns the row dimension of this matrix.
Object for storing data and providing functionality that is common to all computational classes...
virtual ~SerialDenseMatrix()
Destructor.
SerialDenseMatrix()=default
Default Constructor.
ScalarTraits< ScalarType >::magnitudeType normFrobenius() const
Returns the Frobenius-norm of the matrix.
This structure defines some basic traits for a scalar field type.
SerialDenseMatrix< OrdinalType, ScalarType > & operator+=(const SerialDenseMatrix< OrdinalType, ScalarType > &Source)
Add another matrix to this matrix.
SerialDenseMatrix< OrdinalType, ScalarType > & operator=(const SerialDenseMatrix< OrdinalType, ScalarType > &Source)
Copies values from one matrix to another.
Functionality and data that is common to all computational classes.
static T conjugate(T a)
Returns the conjugate of the scalar type a.
bool empty() const
Returns whether this matrix is empty.
ScalarType scalarType
Typedef for scalar type.
int putScalar(const ScalarType value=Teuchos::ScalarTraits< ScalarType >::zero())
Set all values in the matrix to a constant value.
bool operator!=(const SerialDenseMatrix< OrdinalType, ScalarType > &Operand) const
Inequality of two matrices.
ScalarType & operator()(OrdinalType rowIndex, OrdinalType colIndex)
Element access method (non-const).
Templated serial, dense, symmetric matrix class.
OrdinalType ordinalType
Typedef for ordinal type.
ScalarType * operator[](OrdinalType colIndex)
Column access method (non-const).
static magnitudeType magnitude(T a)
Returns the magnitudeType of the scalar type a.
OrdinalType numCols() const
Returns the column dimension of this matrix.
ScalarTraits< ScalarType >::magnitudeType normInf() const
Returns the Infinity-norm of the matrix.
int random()
Set all values in the matrix to be random numbers.
Ostream manipulator for SerialDenseMatrix.
int reshape(OrdinalType numRows, OrdinalType numCols)
Reshaping method for changing the size of a SerialDenseMatrix, keeping the entries.
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.
int shape(OrdinalType numRows, OrdinalType numCols)
Shape method for changing the size of a SerialDenseMatrix, initializing entries to zero...
OrdinalType numCols() const
Returns the column dimension of this matrix.
SerialDenseMatrix< OrdinalType, ScalarType > & assign(const SerialDenseMatrix< OrdinalType, ScalarType > &Source)
Copies values from one matrix to another.
Teuchos::DataAccess Mode enumerable type.
OrdinalType stride() const
Returns the stride between the columns of this matrix in memory.
OrdinalType numRows() const
Returns the row dimension of this matrix.
This class creates and provides basic support for dense rectangular matrix of templated type...