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 virtual void print(std::ostream& os)
const;
391 void copyMat(ScalarType* inputMatrix, OrdinalType strideInput,
392 OrdinalType
numRows, OrdinalType
numCols, ScalarType* outputMatrix,
393 OrdinalType strideOutput, OrdinalType startRow, OrdinalType startCol,
396 void checkIndex( OrdinalType rowIndex, OrdinalType colIndex = 0 )
const;
397 OrdinalType numRows_;
398 OrdinalType numCols_;
409 template<
typename OrdinalType,
typename ScalarType>
411 :
CompObject(),
BLAS<OrdinalType,ScalarType>(), numRows_(0), numCols_(0), stride_(0), valuesCopied_(false), values_(0)
414 template<
typename OrdinalType,
typename ScalarType>
416 OrdinalType numRows_in, OrdinalType numCols_in,
bool zeroOut
418 :
CompObject(),
BLAS<OrdinalType,ScalarType>(), numRows_(numRows_in), numCols_(numCols_in), stride_(numRows_in)
420 values_ =
new ScalarType[stride_*numCols_];
421 valuesCopied_ =
true;
426 template<
typename OrdinalType,
typename ScalarType>
428 DataAccess CV, ScalarType* values_in, OrdinalType stride_in, OrdinalType numRows_in,
429 OrdinalType numCols_in
431 :
CompObject(),
BLAS<OrdinalType,ScalarType>(), numRows_(numRows_in), numCols_(numCols_in), stride_(stride_in),
432 valuesCopied_(false), values_(values_in)
437 values_ =
new ScalarType[stride_*numCols_];
438 copyMat(values_in, stride_in, numRows_, numCols_, values_, stride_, 0, 0);
439 valuesCopied_ =
true;
443 template<
typename OrdinalType,
typename ScalarType>
445 :
CompObject(),
BLAS<OrdinalType,ScalarType>(), numRows_(0), numCols_(0), stride_(0), valuesCopied_(true), values_(0)
449 numRows_ = Source.numRows_;
450 numCols_ = Source.numCols_;
452 if (!Source.valuesCopied_)
454 stride_ = Source.stride_;
455 values_ = Source.values_;
456 valuesCopied_ =
false;
461 const OrdinalType newsize = stride_ * numCols_;
463 values_ =
new ScalarType[newsize];
464 copyMat(Source.values_, Source.stride_, numRows_, numCols_, values_, stride_, 0, 0);
467 numRows_ = 0; numCols_ = 0; stride_ = 0;
468 valuesCopied_ =
false;
474 numRows_ = Source.numCols_;
475 numCols_ = Source.numRows_;
477 values_ =
new ScalarType[stride_*numCols_];
478 for (OrdinalType j=0; j<numCols_; j++) {
479 for (OrdinalType i=0; i<numRows_; i++) {
486 numRows_ = Source.numCols_;
487 numCols_ = Source.numRows_;
489 values_ =
new ScalarType[stride_*numCols_];
490 for (OrdinalType j=0; j<numCols_; j++) {
491 for (OrdinalType i=0; i<numRows_; i++) {
492 values_[j*stride_ + i] = Source.values_[i*Source.stride_ + j];
499 template<
typename OrdinalType,
typename ScalarType>
503 :
CompObject(), numRows_(Source.numRows_), numCols_(Source.numCols_), stride_(Source.stride_),
504 valuesCopied_(false), values_(Source.values_)
509 values_ =
new ScalarType[stride_ * numCols_];
510 copyMat(Source.values_, Source.stride_, numRows_, numCols_, values_, stride_, 0, 0);
511 valuesCopied_ =
true;
516 template<
typename OrdinalType,
typename ScalarType>
519 OrdinalType numRows_in, OrdinalType numCols_in, OrdinalType startRow,
522 :
CompObject(), numRows_(numRows_in), numCols_(numCols_in), stride_(Source.stride_),
523 valuesCopied_(false), values_(Source.values_)
527 stride_ = numRows_in;
528 values_ =
new ScalarType[stride_ * numCols_in];
529 copyMat(Source.values_, Source.stride_, numRows_in, numCols_in, values_, stride_, startRow, startCol);
530 valuesCopied_ =
true;
534 values_ = values_ + (stride_ * startCol) + startRow;
538 template<
typename OrdinalType,
typename ScalarType>
548 template<
typename OrdinalType,
typename ScalarType>
550 OrdinalType numRows_in, OrdinalType numCols_in
554 numRows_ = numRows_in;
555 numCols_ = numCols_in;
557 values_ =
new ScalarType[stride_*numCols_];
559 valuesCopied_ =
true;
563 template<
typename OrdinalType,
typename ScalarType>
565 OrdinalType numRows_in, OrdinalType numCols_in
569 numRows_ = numRows_in;
570 numCols_ = numCols_in;
572 values_ =
new ScalarType[stride_*numCols_];
573 valuesCopied_ =
true;
577 template<
typename OrdinalType,
typename ScalarType>
579 OrdinalType numRows_in, OrdinalType numCols_in
583 ScalarType* values_tmp =
new ScalarType[numRows_in * numCols_in];
585 for(OrdinalType k = 0; k < numRows_in * numCols_in; k++)
587 values_tmp[k] = zero;
589 OrdinalType numRows_tmp = TEUCHOS_MIN(numRows_, numRows_in);
590 OrdinalType numCols_tmp = TEUCHOS_MIN(numCols_, numCols_in);
593 copyMat(values_, stride_, numRows_tmp, numCols_tmp, values_tmp,
597 numRows_ = numRows_in;
598 numCols_ = numCols_in;
600 values_ = values_tmp;
601 valuesCopied_ =
true;
609 template<
typename OrdinalType,
typename ScalarType>
613 for(OrdinalType j = 0; j < numCols_; j++)
615 for(OrdinalType i = 0; i < numRows_; i++)
617 values_[i + j*stride_] = value_in;
623 template<
typename OrdinalType,
typename ScalarType>
void
641 std::swap(values_ , B.values_);
642 std::swap(numRows_, B.numRows_);
643 std::swap(numCols_, B.numCols_);
644 std::swap(stride_, B.stride_);
645 std::swap(valuesCopied_, B.valuesCopied_);
648 template<
typename OrdinalType,
typename ScalarType>
652 for(OrdinalType j = 0; j < numCols_; j++)
654 for(OrdinalType i = 0; i < numRows_; i++)
662 template<
typename OrdinalType,
typename ScalarType>
670 if((!valuesCopied_) && (!Source.valuesCopied_) && (values_ == Source.values_))
674 if (!Source.valuesCopied_) {
679 numRows_ = Source.numRows_;
680 numCols_ = Source.numCols_;
681 stride_ = Source.stride_;
682 values_ = Source.values_;
687 numRows_ = Source.numRows_;
688 numCols_ = Source.numCols_;
689 stride_ = Source.numRows_;
690 const OrdinalType newsize = stride_ * numCols_;
692 values_ =
new ScalarType[newsize];
693 valuesCopied_ =
true;
701 if((Source.numRows_ <= stride_) && (Source.numCols_ == numCols_)) {
702 numRows_ = Source.numRows_;
703 numCols_ = Source.numCols_;
707 numRows_ = Source.numRows_;
708 numCols_ = Source.numCols_;
709 stride_ = Source.numRows_;
710 const OrdinalType newsize = stride_ * numCols_;
712 values_ =
new ScalarType[newsize];
713 valuesCopied_ =
true;
717 copyMat(Source.values_, Source.stride_, numRows_, numCols_, values_, stride_, 0, 0);
722 template<
typename OrdinalType,
typename ScalarType>
726 if ((numRows_ != Source.numRows_) || (numCols_ != Source.numCols_))
728 TEUCHOS_CHK_REF(*
this);
734 template<
typename OrdinalType,
typename ScalarType>
738 if ((numRows_ != Source.numRows_) || (numCols_ != Source.numCols_))
740 TEUCHOS_CHK_REF(*
this);
746 template<
typename OrdinalType,
typename ScalarType>
750 if((!valuesCopied_) && (!Source.valuesCopied_) && (values_ == Source.values_))
754 if ((numRows_ != Source.numRows_) || (numCols_ != Source.numCols_))
756 TEUCHOS_CHK_REF(*
this);
758 copyMat(Source.values_, Source.stride_, numRows_, numCols_, values_, stride_, 0, 0);
766 template<
typename OrdinalType,
typename ScalarType>
769 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
770 checkIndex( rowIndex, colIndex );
772 return(values_[colIndex * stride_ + rowIndex]);
775 template<
typename OrdinalType,
typename ScalarType>
778 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
779 checkIndex( rowIndex, colIndex );
781 return(values_[colIndex * stride_ + rowIndex]);
784 template<
typename OrdinalType,
typename ScalarType>
787 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
788 checkIndex( 0, colIndex );
790 return(values_ + colIndex * stride_);
793 template<
typename OrdinalType,
typename ScalarType>
796 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
797 checkIndex( 0, colIndex );
799 return(values_ + colIndex * stride_);
806 template<
typename OrdinalType,
typename ScalarType>
813 for(j = 0; j < numCols_; j++)
816 ptr = values_ + j * stride_;
817 for(i = 0; i < numRows_; i++)
828 if (flopCounter_!=0) updateFlops(numRows_ * numCols_);
832 template<
typename OrdinalType,
typename ScalarType>
838 for (i = 0; i < numRows_; i++) {
840 for (j=0; j< numCols_; j++) {
843 anorm = TEUCHOS_MAX( anorm, sum );
846 if (flopCounter_!=0) updateFlops(numRows_ * numCols_);
850 template<
typename OrdinalType,
typename ScalarType>
855 for (j = 0; j < numCols_; j++) {
856 for (i = 0; i < numRows_; i++) {
862 if (flopCounter_!=0) updateFlops(numRows_ * numCols_);
870 template<
typename OrdinalType,
typename ScalarType>
874 if((numRows_ != Operand.numRows_) || (numCols_ != Operand.numCols_))
881 for(i = 0; i < numRows_; i++)
883 for(j = 0; j < numCols_; j++)
885 if((*
this)(i, j) != Operand(i, j))
895 template<
typename OrdinalType,
typename ScalarType>
898 return !((*this) == Operand);
905 template<
typename OrdinalType,
typename ScalarType>
908 this->scale( alpha );
912 template<
typename OrdinalType,
typename ScalarType>
918 for (j=0; j<numCols_; j++) {
919 ptr = values_ + j*stride_;
920 for (i=0; i<numRows_; i++) { *ptr = alpha * (*ptr); ptr++; }
923 if (flopCounter_!=0) updateFlops( numRows_*numCols_ );
927 template<
typename OrdinalType,
typename ScalarType>
934 if ((numRows_ != A.numRows_) || (numCols_ != A.numCols_))
938 for (j=0; j<numCols_; j++) {
939 ptr = values_ + j*stride_;
940 for (i=0; i<numRows_; i++) { *ptr = A(i,j) * (*ptr); ptr++; }
943 if (flopCounter_!=0) updateFlops( numRows_*numCols_ );
947 template<
typename OrdinalType,
typename ScalarType>
951 OrdinalType A_nrows = (ETranspChar[transa]!=
'N') ? A.
numCols() : A.
numRows();
952 OrdinalType A_ncols = (ETranspChar[transa]!=
'N') ? A.
numRows() : A.
numCols();
953 OrdinalType B_nrows = (ETranspChar[transb]!=
'N') ? B.
numCols() : B.
numRows();
954 OrdinalType B_ncols = (ETranspChar[transb]!=
'N') ? B.
numRows() : B.
numCols();
955 if ((numRows_ != A_nrows) || (A_ncols != B_nrows) || (numCols_ != B_ncols))
960 this->GEMM(transa, transb, numRows_, numCols_, A_ncols, alpha, A.
values(), A.
stride(), B.
values(), B.
stride(), beta, values_, stride_);
963 if (flopCounter_!=0) {
964 double nflops = 2 * numRows_;
972 template<
typename OrdinalType,
typename ScalarType>
979 if (ESideChar[sideA]==
'L') {
980 if ((numRows_ != A_nrows) || (A_ncols != B_nrows) || (numCols_ != B_ncols)) {
984 if ((numRows_ != B_nrows) || (B_ncols != A_nrows) || (numCols_ != A_ncols)) {
991 this->SYMM(sideA, uplo, numRows_, numCols_, alpha, A.
values(), A.
stride(), B.
values(), B.
stride(), beta, values_, stride_);
994 if (flopCounter_!=0) {
995 double nflops = 2 * numRows_;
1003 template<
typename OrdinalType,
typename ScalarType>
1008 os <<
"Values_copied : yes" << std::endl;
1010 os <<
"Values_copied : no" << std::endl;
1011 os <<
"Rows : " << numRows_ << std::endl;
1012 os <<
"Columns : " << numCols_ << std::endl;
1013 os <<
"LDA : " << stride_ << std::endl;
1014 if(numRows_ == 0 || numCols_ == 0) {
1015 os <<
"(matrix is empty, no values to display)" << std::endl;
1017 for(OrdinalType i = 0; i < numRows_; i++) {
1018 for(OrdinalType j = 0; j < numCols_; j++){
1019 os << (*this)(i,j) <<
" ";
1030 template<
typename OrdinalType,
typename ScalarType>
1033 "SerialDenseMatrix<T>::checkIndex: "
1034 "Row index " << rowIndex <<
" out of range [0, "<< numRows_ <<
")");
1036 "SerialDenseMatrix<T>::checkIndex: "
1037 "Col index " << colIndex <<
" out of range [0, "<< numCols_ <<
")");
1040 template<
typename OrdinalType,
typename ScalarType>
1041 void SerialDenseMatrix<OrdinalType, ScalarType>::deleteArrays(
void)
1047 valuesCopied_ =
false;
1051 template<
typename OrdinalType,
typename ScalarType>
1052 void SerialDenseMatrix<OrdinalType, ScalarType>::copyMat(
1053 ScalarType* inputMatrix, OrdinalType strideInput, OrdinalType numRows_in,
1054 OrdinalType numCols_in, ScalarType* outputMatrix, OrdinalType strideOutput,
1055 OrdinalType startRow, OrdinalType startCol, ScalarType alpha
1059 ScalarType* ptr1 = 0;
1060 ScalarType* ptr2 = 0;
1061 for(j = 0; j < numCols_in; j++) {
1062 ptr1 = outputMatrix + (j * strideOutput);
1063 ptr2 = inputMatrix + (j + startCol) * strideInput + startRow;
1065 for(i = 0; i < numRows_in; i++)
1067 *ptr1++ += alpha*(*ptr2++);
1070 for(i = 0; i < numRows_in; i++)
1078 #ifndef TEUCHOS_HIDE_DEPRECATED_CODE
1079 template<
typename OrdinalType,
typename ScalarType>
1081 std::ostream& operator<< (std::ostream& os, const Teuchos::SerialDenseMatrix<OrdinalType, ScalarType>& obj)
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.
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.
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...