42 #ifndef _TEUCHOS_SERIALDENSEMATRIX_HPP_
43 #define _TEUCHOS_SERIALDENSEMATRIX_HPP_
53 #include "Teuchos_Assert.hpp"
68 template<
typename OrdinalType,
typename ScalarType>
230 ScalarType&
operator () (OrdinalType rowIndex, OrdinalType colIndex);
240 const ScalarType&
operator () (OrdinalType rowIndex, OrdinalType colIndex)
const;
260 const ScalarType*
operator [] (OrdinalType colIndex)
const;
264 ScalarType*
values()
const {
return(values_); }
294 int scale (
const ScalarType alpha );
359 OrdinalType
numRows()
const {
return(numRows_); }
362 OrdinalType
numCols()
const {
return(numCols_); }
365 OrdinalType
stride()
const {
return(stride_); }
368 bool empty()
const {
return(numRows_ == 0 || numCols_ == 0); }
386 #ifndef TEUCHOS_HIDE_DEPRECATED_CODE
388 virtual void print(std::ostream& os)
const;
390 virtual std::ostream&
print(std::ostream& os)
const;
395 void copyMat(ScalarType* inputMatrix, OrdinalType strideInput,
396 OrdinalType
numRows, OrdinalType
numCols, ScalarType* outputMatrix,
397 OrdinalType strideOutput, OrdinalType startRow, OrdinalType startCol,
400 void checkIndex( OrdinalType rowIndex, OrdinalType colIndex = 0 )
const;
401 OrdinalType numRows_;
402 OrdinalType numCols_;
413 template<
typename OrdinalType,
typename ScalarType>
415 :
CompObject(),
BLAS<OrdinalType,ScalarType>(), numRows_(0), numCols_(0), stride_(0), valuesCopied_(false), values_(0)
418 template<
typename OrdinalType,
typename ScalarType>
420 OrdinalType numRows_in, OrdinalType numCols_in,
bool zeroOut
422 :
CompObject(),
BLAS<OrdinalType,ScalarType>(), numRows_(numRows_in), numCols_(numCols_in), stride_(numRows_in)
424 values_ =
new ScalarType[stride_*numCols_];
425 valuesCopied_ =
true;
430 template<
typename OrdinalType,
typename ScalarType>
432 DataAccess CV, ScalarType* values_in, OrdinalType stride_in, OrdinalType numRows_in,
433 OrdinalType numCols_in
435 :
CompObject(),
BLAS<OrdinalType,ScalarType>(), numRows_(numRows_in), numCols_(numCols_in), stride_(stride_in),
436 valuesCopied_(false), values_(values_in)
441 values_ =
new ScalarType[stride_*numCols_];
442 copyMat(values_in, stride_in, numRows_, numCols_, values_, stride_, 0, 0);
443 valuesCopied_ =
true;
447 template<
typename OrdinalType,
typename ScalarType>
449 :
CompObject(),
BLAS<OrdinalType,ScalarType>(), numRows_(0), numCols_(0), stride_(0), valuesCopied_(true), values_(0)
453 numRows_ = Source.numRows_;
454 numCols_ = Source.numCols_;
456 if (!Source.valuesCopied_)
458 stride_ = Source.stride_;
459 values_ = Source.values_;
460 valuesCopied_ =
false;
465 const OrdinalType newsize = stride_ * numCols_;
467 values_ =
new ScalarType[newsize];
468 copyMat(Source.values_, Source.stride_, numRows_, numCols_, values_, stride_, 0, 0);
471 numRows_ = 0; numCols_ = 0; stride_ = 0;
472 valuesCopied_ =
false;
478 numRows_ = Source.numCols_;
479 numCols_ = Source.numRows_;
481 values_ =
new ScalarType[stride_*numCols_];
482 for (OrdinalType j=0; j<numCols_; j++) {
483 for (OrdinalType i=0; i<numRows_; i++) {
490 numRows_ = Source.numCols_;
491 numCols_ = Source.numRows_;
493 values_ =
new ScalarType[stride_*numCols_];
494 for (OrdinalType j=0; j<numCols_; j++) {
495 for (OrdinalType i=0; i<numRows_; i++) {
496 values_[j*stride_ + i] = Source.values_[i*Source.stride_ + j];
503 template<
typename OrdinalType,
typename ScalarType>
507 :
CompObject(), numRows_(Source.numRows_), numCols_(Source.numCols_), stride_(Source.stride_),
508 valuesCopied_(false), values_(Source.values_)
513 values_ =
new ScalarType[stride_ * numCols_];
514 copyMat(Source.values_, Source.stride_, numRows_, numCols_, values_, stride_, 0, 0);
515 valuesCopied_ =
true;
520 template<
typename OrdinalType,
typename ScalarType>
523 OrdinalType numRows_in, OrdinalType numCols_in, OrdinalType startRow,
526 :
CompObject(), numRows_(numRows_in), numCols_(numCols_in), stride_(Source.stride_),
527 valuesCopied_(false), values_(Source.values_)
531 stride_ = numRows_in;
532 values_ =
new ScalarType[stride_ * numCols_in];
533 copyMat(Source.values_, Source.stride_, numRows_in, numCols_in, values_, stride_, startRow, startCol);
534 valuesCopied_ =
true;
538 values_ = values_ + (stride_ * startCol) + startRow;
542 template<
typename OrdinalType,
typename ScalarType>
552 template<
typename OrdinalType,
typename ScalarType>
554 OrdinalType numRows_in, OrdinalType numCols_in
558 numRows_ = numRows_in;
559 numCols_ = numCols_in;
561 values_ =
new ScalarType[stride_*numCols_];
563 valuesCopied_ =
true;
567 template<
typename OrdinalType,
typename ScalarType>
569 OrdinalType numRows_in, OrdinalType numCols_in
573 numRows_ = numRows_in;
574 numCols_ = numCols_in;
576 values_ =
new ScalarType[stride_*numCols_];
577 valuesCopied_ =
true;
581 template<
typename OrdinalType,
typename ScalarType>
583 OrdinalType numRows_in, OrdinalType numCols_in
587 ScalarType* values_tmp =
new ScalarType[numRows_in * numCols_in];
589 for(OrdinalType k = 0; k < numRows_in * numCols_in; k++)
591 values_tmp[k] = zero;
593 OrdinalType numRows_tmp = TEUCHOS_MIN(numRows_, numRows_in);
594 OrdinalType numCols_tmp = TEUCHOS_MIN(numCols_, numCols_in);
597 copyMat(values_, stride_, numRows_tmp, numCols_tmp, values_tmp,
601 numRows_ = numRows_in;
602 numCols_ = numCols_in;
604 values_ = values_tmp;
605 valuesCopied_ =
true;
613 template<
typename OrdinalType,
typename ScalarType>
617 for(OrdinalType j = 0; j < numCols_; j++)
619 for(OrdinalType i = 0; i < numRows_; i++)
621 values_[i + j*stride_] = value_in;
627 template<
typename OrdinalType,
typename ScalarType>
void
645 std::swap(values_ , B.values_);
646 std::swap(numRows_, B.numRows_);
647 std::swap(numCols_, B.numCols_);
648 std::swap(stride_, B.stride_);
649 std::swap(valuesCopied_, B.valuesCopied_);
652 template<
typename OrdinalType,
typename ScalarType>
656 for(OrdinalType j = 0; j < numCols_; j++)
658 for(OrdinalType i = 0; i < numRows_; i++)
666 template<
typename OrdinalType,
typename ScalarType>
674 if((!valuesCopied_) && (!Source.valuesCopied_) && (values_ == Source.values_))
678 if (!Source.valuesCopied_) {
683 numRows_ = Source.numRows_;
684 numCols_ = Source.numCols_;
685 stride_ = Source.stride_;
686 values_ = Source.values_;
691 numRows_ = Source.numRows_;
692 numCols_ = Source.numCols_;
693 stride_ = Source.numRows_;
694 const OrdinalType newsize = stride_ * numCols_;
696 values_ =
new ScalarType[newsize];
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 const OrdinalType newsize = stride_ * numCols_;
716 values_ =
new ScalarType[newsize];
717 valuesCopied_ =
true;
721 copyMat(Source.values_, Source.stride_, numRows_, numCols_, values_, stride_, 0, 0);
726 template<
typename OrdinalType,
typename ScalarType>
730 if ((numRows_ != Source.numRows_) || (numCols_ != Source.numCols_))
732 TEUCHOS_CHK_REF(*
this);
738 template<
typename OrdinalType,
typename ScalarType>
742 if ((numRows_ != Source.numRows_) || (numCols_ != Source.numCols_))
744 TEUCHOS_CHK_REF(*
this);
750 template<
typename OrdinalType,
typename ScalarType>
754 if((!valuesCopied_) && (!Source.valuesCopied_) && (values_ == Source.values_))
758 if ((numRows_ != Source.numRows_) || (numCols_ != Source.numCols_))
760 TEUCHOS_CHK_REF(*
this);
762 copyMat(Source.values_, Source.stride_, numRows_, numCols_, values_, stride_, 0, 0);
770 template<
typename OrdinalType,
typename ScalarType>
773 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
774 checkIndex( rowIndex, colIndex );
776 return(values_[colIndex * stride_ + rowIndex]);
779 template<
typename OrdinalType,
typename ScalarType>
782 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
783 checkIndex( rowIndex, colIndex );
785 return(values_[colIndex * stride_ + rowIndex]);
788 template<
typename OrdinalType,
typename ScalarType>
791 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
792 checkIndex( 0, colIndex );
794 return(values_ + colIndex * stride_);
797 template<
typename OrdinalType,
typename ScalarType>
800 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
801 checkIndex( 0, colIndex );
803 return(values_ + colIndex * stride_);
810 template<
typename OrdinalType,
typename ScalarType>
817 for(j = 0; j < numCols_; j++)
820 ptr = values_ + j * stride_;
821 for(i = 0; i < numRows_; i++)
832 if (flopCounter_!=0) updateFlops(numRows_ * numCols_);
836 template<
typename OrdinalType,
typename ScalarType>
842 for (i = 0; i < numRows_; i++) {
844 for (j=0; j< numCols_; j++) {
847 anorm = TEUCHOS_MAX( anorm, sum );
850 if (flopCounter_!=0) updateFlops(numRows_ * numCols_);
854 template<
typename OrdinalType,
typename ScalarType>
859 for (j = 0; j < numCols_; j++) {
860 for (i = 0; i < numRows_; i++) {
866 if (flopCounter_!=0) updateFlops(numRows_ * numCols_);
874 template<
typename OrdinalType,
typename ScalarType>
878 if((numRows_ != Operand.numRows_) || (numCols_ != Operand.numCols_))
885 for(i = 0; i < numRows_; i++)
887 for(j = 0; j < numCols_; j++)
889 if((*
this)(i, j) != Operand(i, j))
899 template<
typename OrdinalType,
typename ScalarType>
902 return !((*this) == Operand);
909 template<
typename OrdinalType,
typename ScalarType>
912 this->scale( alpha );
916 template<
typename OrdinalType,
typename ScalarType>
922 for (j=0; j<numCols_; j++) {
923 ptr = values_ + j*stride_;
924 for (i=0; i<numRows_; i++) { *ptr = alpha * (*ptr); ptr++; }
927 if (flopCounter_!=0) updateFlops( numRows_*numCols_ );
931 template<
typename OrdinalType,
typename ScalarType>
938 if ((numRows_ != A.numRows_) || (numCols_ != A.numCols_))
942 for (j=0; j<numCols_; j++) {
943 ptr = values_ + j*stride_;
944 for (i=0; i<numRows_; i++) { *ptr = A(i,j) * (*ptr); ptr++; }
947 if (flopCounter_!=0) updateFlops( numRows_*numCols_ );
951 template<
typename OrdinalType,
typename ScalarType>
955 OrdinalType A_nrows = (ETranspChar[transa]!=
'N') ? A.
numCols() : A.
numRows();
956 OrdinalType A_ncols = (ETranspChar[transa]!=
'N') ? A.
numRows() : A.
numCols();
957 OrdinalType B_nrows = (ETranspChar[transb]!=
'N') ? B.
numCols() : B.
numRows();
958 OrdinalType B_ncols = (ETranspChar[transb]!=
'N') ? B.
numRows() : B.
numCols();
959 if ((numRows_ != A_nrows) || (A_ncols != B_nrows) || (numCols_ != B_ncols))
964 this->GEMM(transa, transb, numRows_, numCols_, A_ncols, alpha, A.
values(), A.
stride(), B.
values(), B.
stride(), beta, values_, stride_);
967 if (flopCounter_!=0) {
968 double nflops = 2 * numRows_;
976 template<
typename OrdinalType,
typename ScalarType>
983 if (ESideChar[sideA]==
'L') {
984 if ((numRows_ != A_nrows) || (A_ncols != B_nrows) || (numCols_ != B_ncols)) {
988 if ((numRows_ != B_nrows) || (B_ncols != A_nrows) || (numCols_ != A_ncols)) {
995 this->SYMM(sideA, uplo, numRows_, numCols_, alpha, A.
values(), A.
stride(), B.
values(), B.
stride(), beta, values_, stride_);
998 if (flopCounter_!=0) {
999 double nflops = 2 * numRows_;
1002 updateFlops(nflops);
1007 template<
typename OrdinalType,
typename ScalarType>
1008 #ifndef TEUCHOS_HIDE_DEPRECATED_CODE
1016 os <<
"Values_copied : yes" << std::endl;
1018 os <<
"Values_copied : no" << std::endl;
1019 os <<
"Rows : " << numRows_ << std::endl;
1020 os <<
"Columns : " << numCols_ << std::endl;
1021 os <<
"LDA : " << stride_ << std::endl;
1022 if(numRows_ == 0 || numCols_ == 0) {
1023 os <<
"(matrix is empty, no values to display)" << std::endl;
1025 for(OrdinalType i = 0; i < numRows_; i++) {
1026 for(OrdinalType j = 0; j < numCols_; j++){
1027 os << (*this)(i,j) <<
" ";
1032 #ifdef TEUCHOS_HIDE_DEPRECATED_CODE
1041 template<
typename OrdinalType,
typename ScalarType>
1044 "SerialDenseMatrix<T>::checkIndex: "
1045 "Row index " << rowIndex <<
" out of range [0, "<< numRows_ <<
")");
1047 "SerialDenseMatrix<T>::checkIndex: "
1048 "Col index " << colIndex <<
" out of range [0, "<< numCols_ <<
")");
1051 template<
typename OrdinalType,
typename ScalarType>
1052 void SerialDenseMatrix<OrdinalType, ScalarType>::deleteArrays(
void)
1058 valuesCopied_ =
false;
1062 template<
typename OrdinalType,
typename ScalarType>
1063 void SerialDenseMatrix<OrdinalType, ScalarType>::copyMat(
1064 ScalarType* inputMatrix, OrdinalType strideInput, OrdinalType numRows_in,
1065 OrdinalType numCols_in, ScalarType* outputMatrix, OrdinalType strideOutput,
1066 OrdinalType startRow, OrdinalType startCol, ScalarType alpha
1070 ScalarType* ptr1 = 0;
1071 ScalarType* ptr2 = 0;
1072 for(j = 0; j < numCols_in; j++) {
1073 ptr1 = outputMatrix + (j * strideOutput);
1074 ptr2 = inputMatrix + (j + startCol) * strideInput + startRow;
1076 for(i = 0; i < numRows_in; i++)
1078 *ptr1++ += alpha*(*ptr2++);
1081 for(i = 0; i < numRows_in; i++)
1089 #ifndef TEUCHOS_HIDE_DEPRECATED_CODE
1090 template<
typename OrdinalType,
typename ScalarType>
1092 std::ostream& operator<< (std::ostream& os, const Teuchos::SerialDenseMatrix<OrdinalType, ScalarType>& obj)
1100 template<
typename OrdinalType,
typename ScalarType>
1110 template<
typename OrdinalType,
typename ScalarType>
1112 operator<<(std::ostream &out,
1115 printer.obj.print(out);
1120 template<
typename OrdinalType,
typename ScalarType>
1121 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.
virtual void print(std::ostream &os) const
Print method. Defines the behavior of the std::ostream << operator.
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.
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.
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.
SerialDenseMatrix()
Default Constructor.
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...