61 #ifndef _TEUCHOS_SERIALDENSEMATRIX_HPP_
62 #define _TEUCHOS_SERIALDENSEMATRIX_HPP_
72 #include "Teuchos_Assert.hpp"
85 template<
typename OrdinalType,
typename ScalarType>
236 ScalarType&
operator () (OrdinalType rowIndex, OrdinalType colIndex);
246 const ScalarType&
operator () (OrdinalType rowIndex, OrdinalType colIndex)
const;
266 const ScalarType*
operator [] (OrdinalType colIndex)
const;
270 ScalarType*
values()
const {
return(values_); }
300 int scale (
const ScalarType alpha );
365 OrdinalType
numRows()
const {
return(numRows_); }
368 OrdinalType
numCols()
const {
return(numCols_); }
371 OrdinalType
stride()
const {
return(stride_); }
374 bool empty()
const {
return(numRows_ == 0 || numCols_ == 0); }
392 virtual void print(std::ostream& os)
const;
397 void copyMat(ScalarType* inputMatrix, OrdinalType strideInput,
398 OrdinalType
numRows, OrdinalType
numCols, ScalarType* outputMatrix,
399 OrdinalType strideOutput, OrdinalType startRow, OrdinalType startCol,
402 void checkIndex( OrdinalType rowIndex, OrdinalType colIndex = 0 )
const;
403 OrdinalType numRows_;
404 OrdinalType numCols_;
415 template<
typename OrdinalType,
typename ScalarType>
417 :
CompObject(),
Object(
"Teuchos::SerialDenseMatrix"), numRows_(0), numCols_(0), stride_(0), valuesCopied_(false), values_(0)
420 template<
typename OrdinalType,
typename ScalarType>
422 OrdinalType numRows_in, OrdinalType numCols_in,
bool zeroOut
424 :
CompObject(),
Object(
"Teuchos::SerialDenseMatrix"), numRows_(numRows_in), numCols_(numCols_in), stride_(numRows_in)
426 values_ =
new ScalarType[stride_*numCols_];
427 valuesCopied_ =
true;
432 template<
typename OrdinalType,
typename ScalarType>
434 DataAccess CV, ScalarType* values_in, OrdinalType stride_in, OrdinalType numRows_in,
435 OrdinalType numCols_in
437 :
CompObject(),
Object(
"Teuchos::SerialDenseMatrix"), numRows_(numRows_in), numCols_(numCols_in), stride_(stride_in),
438 valuesCopied_(false), values_(values_in)
443 values_ =
new ScalarType[stride_*numCols_];
444 copyMat(values_in, stride_in, numRows_, numCols_, values_, stride_, 0, 0);
445 valuesCopied_ =
true;
449 template<
typename OrdinalType,
typename ScalarType>
450 SerialDenseMatrix<OrdinalType, ScalarType>::SerialDenseMatrix(
const SerialDenseMatrix<OrdinalType, ScalarType> &Source,
ETransp trans) :
CompObject(),
Object(
"Teuchos::SerialDenseMatrix"),
BLAS<OrdinalType,ScalarType>(), numRows_(0), numCols_(0), stride_(0), valuesCopied_(true), values_(0)
454 numRows_ = Source.numRows_;
455 numCols_ = Source.numCols_;
457 values_ =
new ScalarType[stride_*numCols_];
458 copyMat(Source.values_, Source.stride_, numRows_, numCols_, values_, stride_, 0, 0);
462 numRows_ = Source.numCols_;
463 numCols_ = Source.numRows_;
465 values_ =
new ScalarType[stride_*numCols_];
466 for (OrdinalType j=0; j<numCols_; j++) {
467 for (OrdinalType i=0; i<numRows_; i++) {
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++) {
480 values_[j*stride_ + i] = Source.values_[i*Source.stride_ + j];
486 template<
typename OrdinalType,
typename ScalarType>
489 OrdinalType numRows_in, OrdinalType numCols_in, OrdinalType startRow,
492 :
CompObject(),
Object(
"Teuchos::SerialDenseMatrix"), numRows_(numRows_in), numCols_(numCols_in), stride_(Source.stride_),
493 valuesCopied_(false), values_(Source.values_)
497 stride_ = numRows_in;
498 values_ =
new ScalarType[stride_ * numCols_in];
499 copyMat(Source.values_, Source.stride_, numRows_in, numCols_in, values_, stride_, startRow, startCol);
500 valuesCopied_ =
true;
504 values_ = values_ + (stride_ * startCol) + startRow;
508 template<
typename OrdinalType,
typename ScalarType>
518 template<
typename OrdinalType,
typename ScalarType>
520 OrdinalType numRows_in, OrdinalType numCols_in
524 numRows_ = numRows_in;
525 numCols_ = numCols_in;
527 values_ =
new ScalarType[stride_*numCols_];
529 valuesCopied_ =
true;
533 template<
typename OrdinalType,
typename ScalarType>
535 OrdinalType numRows_in, OrdinalType numCols_in
539 numRows_ = numRows_in;
540 numCols_ = numCols_in;
542 values_ =
new ScalarType[stride_*numCols_];
543 valuesCopied_ =
true;
547 template<
typename OrdinalType,
typename ScalarType>
549 OrdinalType numRows_in, OrdinalType numCols_in
553 ScalarType* values_tmp =
new ScalarType[numRows_in * numCols_in];
555 for(OrdinalType k = 0; k < numRows_in * numCols_in; k++)
557 values_tmp[k] = zero;
559 OrdinalType numRows_tmp = TEUCHOS_MIN(numRows_, numRows_in);
560 OrdinalType numCols_tmp = TEUCHOS_MIN(numCols_, numCols_in);
563 copyMat(values_, stride_, numRows_tmp, numCols_tmp, values_tmp,
567 numRows_ = numRows_in;
568 numCols_ = numCols_in;
570 values_ = values_tmp;
571 valuesCopied_ =
true;
579 template<
typename OrdinalType,
typename ScalarType>
583 for(OrdinalType j = 0; j < numCols_; j++)
585 for(OrdinalType i = 0; i < numRows_; i++)
587 values_[i + j*stride_] = value_in;
593 template<
typename OrdinalType,
typename ScalarType>
597 for(OrdinalType j = 0; j < numCols_; j++)
599 for(OrdinalType i = 0; i < numRows_; i++)
607 template<
typename OrdinalType,
typename ScalarType>
615 if((!valuesCopied_) && (!Source.valuesCopied_) && (values_ == Source.values_))
619 if (!Source.valuesCopied_) {
624 numRows_ = Source.numRows_;
625 numCols_ = Source.numCols_;
626 stride_ = Source.stride_;
627 values_ = Source.values_;
632 numRows_ = Source.numRows_;
633 numCols_ = Source.numCols_;
634 stride_ = Source.numRows_;
635 const OrdinalType newsize = stride_ * numCols_;
637 values_ =
new ScalarType[newsize];
638 valuesCopied_ =
true;
646 if((Source.numRows_ <= stride_) && (Source.numCols_ == numCols_)) {
647 numRows_ = Source.numRows_;
648 numCols_ = Source.numCols_;
652 numRows_ = Source.numRows_;
653 numCols_ = Source.numCols_;
654 stride_ = Source.numRows_;
655 const OrdinalType newsize = stride_ * numCols_;
657 values_ =
new ScalarType[newsize];
658 valuesCopied_ =
true;
662 copyMat(Source.values_, Source.stride_, numRows_, numCols_, values_, stride_, 0, 0);
667 template<
typename OrdinalType,
typename ScalarType>
671 if ((numRows_ != Source.numRows_) || (numCols_ != Source.numCols_))
673 TEUCHOS_CHK_REF(*
this);
679 template<
typename OrdinalType,
typename ScalarType>
683 if ((numRows_ != Source.numRows_) || (numCols_ != Source.numCols_))
685 TEUCHOS_CHK_REF(*
this);
691 template<
typename OrdinalType,
typename ScalarType>
695 if((!valuesCopied_) && (!Source.valuesCopied_) && (values_ == Source.values_))
699 if ((numRows_ != Source.numRows_) || (numCols_ != Source.numCols_))
701 TEUCHOS_CHK_REF(*
this);
703 copyMat(Source.values_, Source.stride_, numRows_, numCols_, values_, stride_, 0, 0);
711 template<
typename OrdinalType,
typename ScalarType>
714 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
715 checkIndex( rowIndex, colIndex );
717 return(values_[colIndex * stride_ + rowIndex]);
720 template<
typename OrdinalType,
typename ScalarType>
723 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
724 checkIndex( rowIndex, colIndex );
726 return(values_[colIndex * stride_ + rowIndex]);
729 template<
typename OrdinalType,
typename ScalarType>
732 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
733 checkIndex( 0, colIndex );
735 return(values_ + colIndex * stride_);
738 template<
typename OrdinalType,
typename ScalarType>
741 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
742 checkIndex( 0, colIndex );
744 return(values_ + colIndex * stride_);
751 template<
typename OrdinalType,
typename ScalarType>
758 for(j = 0; j < numCols_; j++)
761 ptr = values_ + j * stride_;
762 for(i = 0; i < numRows_; i++)
772 updateFlops(numRows_ * numCols_);
776 template<
typename OrdinalType,
typename ScalarType>
782 for (i = 0; i < numRows_; i++) {
784 for (j=0; j< numCols_; j++) {
787 anorm = TEUCHOS_MAX( anorm, sum );
789 updateFlops(numRows_ * numCols_);
793 template<
typename OrdinalType,
typename ScalarType>
798 for (j = 0; j < numCols_; j++) {
799 for (i = 0; i < numRows_; i++) {
804 updateFlops(numRows_ * numCols_);
812 template<
typename OrdinalType,
typename ScalarType>
816 if((numRows_ != Operand.numRows_) || (numCols_ != Operand.numCols_))
823 for(i = 0; i < numRows_; i++)
825 for(j = 0; j < numCols_; j++)
827 if((*
this)(i, j) != Operand(i, j))
837 template<
typename OrdinalType,
typename ScalarType>
840 return !((*this) == Operand);
847 template<
typename OrdinalType,
typename ScalarType>
850 this->scale( alpha );
854 template<
typename OrdinalType,
typename ScalarType>
860 for (j=0; j<numCols_; j++) {
861 ptr = values_ + j*stride_;
862 for (i=0; i<numRows_; i++) { *ptr = alpha * (*ptr); ptr++; }
864 updateFlops( numRows_*numCols_ );
868 template<
typename OrdinalType,
typename ScalarType>
875 if ((numRows_ != A.numRows_) || (numCols_ != A.numCols_))
879 for (j=0; j<numCols_; j++) {
880 ptr = values_ + j*stride_;
881 for (i=0; i<numRows_; i++) { *ptr = A(i,j) * (*ptr); ptr++; }
883 updateFlops( numRows_*numCols_ );
887 template<
typename OrdinalType,
typename ScalarType>
891 OrdinalType A_nrows = (ETranspChar[transa]!=
'N') ? A.
numCols() : A.
numRows();
892 OrdinalType A_ncols = (ETranspChar[transa]!=
'N') ? A.
numRows() : A.
numCols();
893 OrdinalType B_nrows = (ETranspChar[transb]!=
'N') ? B.
numCols() : B.
numRows();
894 OrdinalType B_ncols = (ETranspChar[transb]!=
'N') ? B.
numRows() : B.
numCols();
895 if ((numRows_ != A_nrows) || (A_ncols != B_nrows) || (numCols_ != B_ncols))
900 this->GEMM(transa, transb, numRows_, numCols_, A_ncols, alpha, A.
values(), A.
stride(), B.
values(), B.
stride(), beta, values_, stride_);
901 double nflops = 2 * numRows_;
908 template<
typename OrdinalType,
typename ScalarType>
915 if (ESideChar[sideA]==
'L') {
916 if ((numRows_ != A_nrows) || (A_ncols != B_nrows) || (numCols_ != B_ncols)) {
920 if ((numRows_ != B_nrows) || (B_ncols != A_nrows) || (numCols_ != A_ncols)) {
927 this->SYMM(sideA, uplo, numRows_, numCols_, alpha, A.
values(), A.
stride(), B.
values(), B.
stride(), beta, values_, stride_);
928 double nflops = 2 * numRows_;
935 template<
typename OrdinalType,
typename ScalarType>
940 os <<
"Values_copied : yes" << std::endl;
942 os <<
"Values_copied : no" << std::endl;
943 os <<
"Rows : " << numRows_ << std::endl;
944 os <<
"Columns : " << numCols_ << std::endl;
945 os <<
"LDA : " << stride_ << std::endl;
946 if(numRows_ == 0 || numCols_ == 0) {
947 os <<
"(matrix is empty, no values to display)" << std::endl;
949 for(OrdinalType i = 0; i < numRows_; i++) {
950 for(OrdinalType j = 0; j < numCols_; j++){
951 os << (*this)(i,j) <<
" ";
962 template<
typename OrdinalType,
typename ScalarType>
965 "SerialDenseMatrix<T>::checkIndex: "
966 "Row index " << rowIndex <<
" out of range [0, "<< numRows_ <<
")");
968 "SerialDenseMatrix<T>::checkIndex: "
969 "Col index " << colIndex <<
" out of range [0, "<< numCols_ <<
")");
972 template<
typename OrdinalType,
typename ScalarType>
973 void SerialDenseMatrix<OrdinalType, ScalarType>::deleteArrays(
void)
979 valuesCopied_ =
false;
983 template<
typename OrdinalType,
typename ScalarType>
984 void SerialDenseMatrix<OrdinalType, ScalarType>::copyMat(
985 ScalarType* inputMatrix, OrdinalType strideInput, OrdinalType numRows_in,
986 OrdinalType numCols_in, ScalarType* outputMatrix, OrdinalType strideOutput,
987 OrdinalType startRow, OrdinalType startCol, ScalarType alpha
991 ScalarType* ptr1 = 0;
992 ScalarType* ptr2 = 0;
993 for(j = 0; j < numCols_in; j++) {
994 ptr1 = outputMatrix + (j * strideOutput);
995 ptr2 = inputMatrix + (j + startCol) * strideInput + startRow;
997 for(i = 0; i < numRows_in; i++)
999 *ptr1++ += alpha*(*ptr2++);
1002 for(i = 0; i < numRows_in; i++)
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 inherited from the Object class...
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.
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...