10 #ifndef _TEUCHOS_SERIALDENSEMATRIX_HPP_
11 #define _TEUCHOS_SERIALDENSEMATRIX_HPP_
36 template<
typename OrdinalType,
typename ScalarType>
198 ScalarType&
operator () (OrdinalType rowIndex, OrdinalType colIndex);
208 const ScalarType&
operator () (OrdinalType rowIndex, OrdinalType colIndex)
const;
224 const ScalarType*
operator [] (OrdinalType colIndex)
const;
258 int scale (
const ScalarType alpha );
350 virtual std::ostream&
print(std::ostream& os)
const;
355 void copyMat(ScalarType* inputMatrix, OrdinalType strideInput,
356 OrdinalType
numRows, OrdinalType
numCols, ScalarType* outputMatrix,
357 OrdinalType strideOutput, OrdinalType startRow, OrdinalType startCol,
360 void checkIndex( OrdinalType rowIndex, OrdinalType colIndex = 0 )
const;
364 const OrdinalType numCols)
366 const size_t size = size_t(numRows) * size_t(numCols);
367 #pragma GCC diagnostic push
368 #pragma GCC diagnostic ignored "-Wvla"
369 return new ScalarType[size];
370 #pragma GCC diagnostic pop
384 template<
typename OrdinalType,
typename ScalarType>
386 OrdinalType numRows_in, OrdinalType numCols_in,
bool zeroOut
388 : numRows_(numRows_in),
389 numCols_(numCols_in),
392 values_(allocateValues(numRows_in, numCols_in))
399 template<
typename OrdinalType,
typename ScalarType>
401 DataAccess CV, ScalarType* values_in, OrdinalType stride_in, OrdinalType numRows_in,
402 OrdinalType numCols_in
404 : numRows_(numRows_in),
405 numCols_(numCols_in),
407 valuesCopied_(false),
419 template<
typename OrdinalType,
typename ScalarType>
421 : valuesCopied_(true)
453 for (OrdinalType j=0; j<
numCols_; j++) {
454 for (OrdinalType i=0; i<
numRows_; i++) {
465 for (OrdinalType j=0; j<
numCols_; j++) {
466 for (OrdinalType i=0; i<
numRows_; i++) {
474 template<
typename OrdinalType,
typename ScalarType>
478 : numRows_(Source.numRows_), numCols_(Source.numCols_), stride_(Source.stride_),
479 valuesCopied_(false), values_(Source.values_)
491 template<
typename OrdinalType,
typename ScalarType>
494 OrdinalType numRows_in, OrdinalType numCols_in, OrdinalType startRow,
497 :
CompObject(), numRows_(numRows_in), numCols_(numCols_in), stride_(Source.stride_),
498 valuesCopied_(false), values_(Source.values_)
513 template<
typename OrdinalType,
typename ScalarType>
523 template<
typename OrdinalType,
typename ScalarType>
525 OrdinalType numRows_in, OrdinalType numCols_in
529 numRows_ = numRows_in;
530 numCols_ = numCols_in;
532 values_ = allocateValues(stride_, numCols_);
534 valuesCopied_ =
true;
538 template<
typename OrdinalType,
typename ScalarType>
540 OrdinalType numRows_in, OrdinalType numCols_in
544 numRows_ = numRows_in;
545 numCols_ = numCols_in;
547 values_ = allocateValues(stride_, numCols_);
548 valuesCopied_ =
true;
552 template<
typename OrdinalType,
typename ScalarType>
554 OrdinalType numRows_in, OrdinalType numCols_in
558 ScalarType* values_tmp = allocateValues(numRows_in, numCols_in);
560 for(OrdinalType k = 0; k < numRows_in * numCols_in; k++)
562 values_tmp[k] = zero;
564 OrdinalType numRows_tmp =
TEUCHOS_MIN(numRows_, numRows_in);
565 OrdinalType numCols_tmp =
TEUCHOS_MIN(numCols_, numCols_in);
568 copyMat(values_, stride_, numRows_tmp, numCols_tmp, values_tmp,
572 numRows_ = numRows_in;
573 numCols_ = numCols_in;
575 values_ = values_tmp;
576 valuesCopied_ =
true;
584 template<
typename OrdinalType,
typename ScalarType>
588 for(OrdinalType j = 0; j < numCols_; j++)
590 for(OrdinalType i = 0; i < numRows_; i++)
592 values_[i + j*stride_] = value_in;
598 template<
typename OrdinalType,
typename ScalarType>
void
616 std::swap(values_ , B.
values_);
623 template<
typename OrdinalType,
typename ScalarType>
627 for(OrdinalType j = 0; j < numCols_; j++)
629 for(OrdinalType i = 0; i < numRows_; i++)
637 template<
typename OrdinalType,
typename ScalarType>
665 if(stride_ > 0 && numCols_ > 0) {
666 values_ = allocateValues(stride_, numCols_);
667 valuesCopied_ =
true;
684 if(stride_ > 0 && numCols_ > 0) {
685 values_ = allocateValues(stride_, numCols_);
686 valuesCopied_ =
true;
690 copyMat(Source.
values_, Source.
stride_, numRows_, numCols_, values_, stride_, 0, 0);
695 template<
typename OrdinalType,
typename ScalarType>
707 template<
typename OrdinalType,
typename ScalarType>
719 template<
typename OrdinalType,
typename ScalarType>
731 copyMat(Source.
values_, Source.
stride_, numRows_, numCols_, values_, stride_, 0, 0);
739 template<
typename OrdinalType,
typename ScalarType>
742 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
743 checkIndex( rowIndex, colIndex );
745 return(values_[colIndex * stride_ + rowIndex]);
748 template<
typename OrdinalType,
typename ScalarType>
751 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
752 checkIndex( rowIndex, colIndex );
754 return(values_[colIndex * stride_ + rowIndex]);
757 template<
typename OrdinalType,
typename ScalarType>
760 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
761 checkIndex( 0, colIndex );
763 return(values_ + colIndex * stride_);
766 template<
typename OrdinalType,
typename ScalarType>
769 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
770 checkIndex( 0, colIndex );
772 return(values_ + colIndex * stride_);
779 template<
typename OrdinalType,
typename ScalarType>
786 for(j = 0; j < numCols_; j++)
789 ptr = values_ + j * stride_;
790 for(i = 0; i < numRows_; i++)
801 if (flopCounter_!=0) updateFlops(numRows_ * numCols_);
805 template<
typename OrdinalType,
typename ScalarType>
811 for (i = 0; i < numRows_; i++) {
813 for (j=0; j< numCols_; j++) {
819 if (flopCounter_!=0) updateFlops(numRows_ * numCols_);
823 template<
typename OrdinalType,
typename ScalarType>
828 for (j = 0; j < numCols_; j++) {
829 for (i = 0; i < numRows_; i++) {
835 if (flopCounter_!=0) updateFlops(numRows_ * numCols_);
843 template<
typename OrdinalType,
typename ScalarType>
854 for(i = 0; i < numRows_; i++)
856 for(j = 0; j < numCols_; j++)
858 if((*
this)(i, j) != Operand(i, j))
868 template<
typename OrdinalType,
typename ScalarType>
871 return !((*this) == Operand);
878 template<
typename OrdinalType,
typename ScalarType>
881 this->scale( alpha );
885 template<
typename OrdinalType,
typename ScalarType>
891 for (j=0; j<numCols_; j++) {
892 ptr = values_ + j*stride_;
893 for (i=0; i<numRows_; i++) { *ptr = alpha * (*ptr); ptr++; }
896 if (flopCounter_!=0) updateFlops( numRows_*numCols_ );
900 template<
typename OrdinalType,
typename ScalarType>
911 for (j=0; j<numCols_; j++) {
912 ptr = values_ + j*stride_;
913 for (i=0; i<numRows_; i++) { *ptr = A(i,j) * (*ptr); ptr++; }
916 if (flopCounter_!=0) updateFlops( numRows_*numCols_ );
920 template<
typename OrdinalType,
typename ScalarType>
928 if ((numRows_ != A_nrows) || (A_ncols != B_nrows) || (numCols_ != B_ncols))
933 this->GEMM(transa, transb, numRows_, numCols_, A_ncols, alpha, A.
values(), A.
stride(), B.
values(), B.
stride(), beta, values_, stride_);
936 if (flopCounter_!=0) {
937 double nflops = 2 * numRows_;
945 template<
typename OrdinalType,
typename ScalarType>
953 if ((numRows_ != A_nrows) || (A_ncols != B_nrows) || (numCols_ != B_ncols)) {
957 if ((numRows_ != B_nrows) || (B_ncols != A_nrows) || (numCols_ != A_ncols)) {
964 this->SYMM(sideA, uplo, numRows_, numCols_, 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>
981 os <<
"Values_copied : yes" << std::endl;
983 os <<
"Values_copied : no" << std::endl;
984 os <<
"Rows : " << numRows_ << std::endl;
985 os <<
"Columns : " << numCols_ << std::endl;
986 os <<
"LDA : " << stride_ << std::endl;
987 if(numRows_ == 0 || numCols_ == 0) {
988 os <<
"(matrix is empty, no values to display)" << std::endl;
990 for(OrdinalType i = 0; i < numRows_; i++) {
991 for(OrdinalType j = 0; j < numCols_; j++){
992 os << (*this)(i,j) <<
" ";
1004 template<
typename OrdinalType,
typename ScalarType>
1007 "SerialDenseMatrix<T>::checkIndex: "
1008 "Row index " << rowIndex <<
" out of range [0, "<< numRows_ <<
")");
1010 "SerialDenseMatrix<T>::checkIndex: "
1011 "Col index " << colIndex <<
" out of range [0, "<< numCols_ <<
")");
1014 template<
typename OrdinalType,
typename ScalarType>
1021 valuesCopied_ =
false;
1025 template<
typename OrdinalType,
typename ScalarType>
1027 ScalarType* inputMatrix, OrdinalType strideInput, OrdinalType numRows_in,
1028 OrdinalType numCols_in, ScalarType* outputMatrix, OrdinalType strideOutput,
1029 OrdinalType startRow, OrdinalType startCol, ScalarType alpha
1033 ScalarType* ptr1 = 0;
1034 ScalarType* ptr2 = 0;
1035 for(j = 0; j < numCols_in; j++) {
1036 ptr1 = outputMatrix + (j * strideOutput);
1037 ptr2 = inputMatrix + (j + startCol) * strideInput + startRow;
1039 for(i = 0; i < numRows_in; i++)
1041 *ptr1++ += alpha*(*ptr2++);
1044 for(i = 0; i < numRows_in; i++)
1053 template<
typename OrdinalType,
typename ScalarType>
1063 template<
typename OrdinalType,
typename ScalarType>
1068 printer.
obj.print(out);
1073 template<
typename OrdinalType,
typename ScalarType>
1074 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.
void checkIndex(OrdinalType rowIndex, OrdinalType colIndex=0) const
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...
SerialDenseMatrixPrinter(const SerialDenseMatrix< OrdinalType, ScalarType > &obj_in)
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.
#define TEUCHOS_CHK_ERR(a)
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.
const SerialDenseMatrix< OrdinalType, ScalarType > & obj
bool empty() const
Returns whether this matrix is empty.
std::ostream & operator<<(std::ostream &os, BigUInt< n > a)
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.
#define TEUCHOS_MAX(x, y)
bool operator!=(const SerialDenseMatrix< OrdinalType, ScalarType > &Operand) const
Inequality of two matrices.
TEUCHOSNUMERICS_LIB_DLL_EXPORT const char ESideChar[]
ScalarType & operator()(OrdinalType rowIndex, OrdinalType colIndex)
Element access method (non-const).
void copyMat(ScalarType *inputMatrix, OrdinalType strideInput, OrdinalType numRows, OrdinalType numCols, ScalarType *outputMatrix, OrdinalType strideOutput, OrdinalType startRow, OrdinalType startCol, ScalarType alpha=ScalarTraits< ScalarType >::zero())
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.
TEUCHOSNUMERICS_LIB_DLL_EXPORT const char ETranspChar[]
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.
#define TEUCHOS_CHK_REF(a)
#define TEUCHOS_MIN(x, y)
SerialDenseMatrix< OrdinalType, ScalarType > & assign(const SerialDenseMatrix< OrdinalType, ScalarType > &Source)
Copies values from one matrix to another.
static ScalarType * allocateValues(const OrdinalType numRows, const OrdinalType numCols)
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...