10#ifndef _TEUCHOS_SERIALDENSEMATRIX_HPP_
11#define _TEUCHOS_SERIALDENSEMATRIX_HPP_
21#include "Teuchos_Assert.hpp"
36template<
typename OrdinalType,
typename ScalarType>
332 bool empty()
const {
return(numRows_ == 0 || numCols_ == 0); }
351 virtual std::ostream& print(std::ostream& os)
const;
370 OrdinalType numRows_ = 0;
371 OrdinalType numCols_ = 0;
372 OrdinalType stride_ = 0;
373 bool valuesCopied_ =
false;
374 ScalarType* values_ =
nullptr;
381template<
typename OrdinalType,
typename ScalarType>
396template<
typename OrdinalType,
typename ScalarType>
404 valuesCopied_(
false),
410 values_ = allocateValues(stride_, numCols_);
412 valuesCopied_ =
true;
416template<
typename OrdinalType,
typename ScalarType>
418 : valuesCopied_(
true)
422 numRows_ = Source.numRows_;
423 numCols_ = Source.numCols_;
425 if (!Source.valuesCopied_)
427 stride_ = Source.stride_;
428 values_ = Source.values_;
429 valuesCopied_ =
false;
434 if(stride_ > 0 && numCols_ > 0) {
435 values_ = allocateValues(stride_, numCols_);
436 copyMat(Source.values_, Source.stride_, numRows_, numCols_, values_, stride_, 0, 0);
439 numRows_ = 0; numCols_ = 0; stride_ = 0;
440 valuesCopied_ =
false;
446 numRows_ = Source.numCols_;
447 numCols_ = Source.numRows_;
449 values_ = allocateValues(stride_, numCols_);
458 numRows_ = Source.numCols_;
459 numCols_ = Source.numRows_;
461 values_ = allocateValues(stride_, numCols_);
464 values_[
j*stride_ +
i] = Source.values_[
i*Source.stride_ +
j];
471template<
typename OrdinalType,
typename ScalarType>
475 : numRows_(Source.numRows_), numCols_(Source.numCols_), stride_(Source.stride_),
476 valuesCopied_(
false), values_(Source.values_)
481 values_ = allocateValues(stride_, numCols_);
482 copyMat(Source.values_, Source.stride_, numRows_, numCols_, values_, stride_, 0, 0);
483 valuesCopied_ =
true;
488template<
typename OrdinalType,
typename ScalarType>
495 valuesCopied_(
false), values_(Source.values_)
500 values_ = allocateValues(stride_,
numCols_in);
502 valuesCopied_ =
true;
510template<
typename OrdinalType,
typename ScalarType>
520template<
typename OrdinalType,
typename ScalarType>
529 values_ = allocateValues(stride_, numCols_);
531 valuesCopied_ =
true;
535template<
typename OrdinalType,
typename ScalarType>
544 values_ = allocateValues(stride_, numCols_);
545 valuesCopied_ =
true;
549template<
typename OrdinalType,
typename ScalarType>
573 valuesCopied_ =
true;
581template<
typename OrdinalType,
typename ScalarType>
595template<
typename OrdinalType,
typename ScalarType>
void
613 std::swap(values_ ,
B.values_);
614 std::swap(numRows_,
B.numRows_);
615 std::swap(numCols_,
B.numCols_);
616 std::swap(stride_,
B.stride_);
617 std::swap(valuesCopied_,
B.valuesCopied_);
620template<
typename OrdinalType,
typename ScalarType>
634template<
typename OrdinalType,
typename ScalarType>
642 if((!valuesCopied_) && (!Source.valuesCopied_) && (values_ == Source.values_))
646 if (!Source.valuesCopied_) {
651 numRows_ = Source.numRows_;
652 numCols_ = Source.numCols_;
653 stride_ = Source.stride_;
654 values_ = Source.values_;
659 numRows_ = Source.numRows_;
660 numCols_ = Source.numCols_;
661 stride_ = Source.numRows_;
662 if(stride_ > 0 && numCols_ > 0) {
663 values_ = allocateValues(stride_, numCols_);
664 valuesCopied_ =
true;
672 if((Source.numRows_ <= stride_) && (Source.numCols_ == numCols_)) {
673 numRows_ = Source.numRows_;
674 numCols_ = Source.numCols_;
678 numRows_ = Source.numRows_;
679 numCols_ = Source.numCols_;
680 stride_ = Source.numRows_;
681 if(stride_ > 0 && numCols_ > 0) {
682 values_ = allocateValues(stride_, numCols_);
683 valuesCopied_ =
true;
687 copyMat(Source.values_, Source.stride_, numRows_, numCols_, values_, stride_, 0, 0);
692template<
typename OrdinalType,
typename ScalarType>
696 if ((numRows_ != Source.numRows_) || (numCols_ != Source.numCols_))
698 TEUCHOS_CHK_REF(*
this);
704template<
typename OrdinalType,
typename ScalarType>
708 if ((numRows_ != Source.numRows_) || (numCols_ != Source.numCols_))
710 TEUCHOS_CHK_REF(*
this);
716template<
typename OrdinalType,
typename ScalarType>
720 if((!valuesCopied_) && (!Source.valuesCopied_) && (values_ == Source.values_))
724 if ((numRows_ != Source.numRows_) || (numCols_ != Source.numCols_))
726 TEUCHOS_CHK_REF(*
this);
728 copyMat(Source.values_, Source.stride_, numRows_, numCols_, values_, stride_, 0, 0);
736template<
typename OrdinalType,
typename ScalarType>
739#ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
740 checkIndex( rowIndex, colIndex );
742 return(values_[colIndex * stride_ + rowIndex]);
745template<
typename OrdinalType,
typename ScalarType>
748#ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
749 checkIndex( rowIndex, colIndex );
751 return(values_[colIndex * stride_ + rowIndex]);
754template<
typename OrdinalType,
typename ScalarType>
757#ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
758 checkIndex( 0, colIndex );
760 return(values_ + colIndex * stride_);
763template<
typename OrdinalType,
typename ScalarType>
766#ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
767 checkIndex( 0, colIndex );
769 return(values_ + colIndex * stride_);
776template<
typename OrdinalType,
typename ScalarType>
783 for(
j = 0;
j < numCols_;
j++)
786 ptr = values_ +
j * stride_;
787 for(
i = 0;
i < numRows_;
i++)
798 if (flopCounter_!=0) updateFlops(numRows_ * numCols_);
802template<
typename OrdinalType,
typename ScalarType>
808 for (
i = 0;
i < numRows_;
i++) {
810 for (
j=0;
j< numCols_;
j++) {
816 if (flopCounter_!=0) updateFlops(numRows_ * numCols_);
820template<
typename OrdinalType,
typename ScalarType>
825 for (
j = 0;
j < numCols_;
j++) {
826 for (
i = 0;
i < numRows_;
i++) {
832 if (flopCounter_!=0) updateFlops(numRows_ * numCols_);
840template<
typename OrdinalType,
typename ScalarType>
844 if((numRows_ !=
Operand.numRows_) || (numCols_ !=
Operand.numCols_))
851 for(
i = 0;
i < numRows_;
i++)
853 for(
j = 0;
j < numCols_;
j++)
865template<
typename OrdinalType,
typename ScalarType>
875template<
typename OrdinalType,
typename ScalarType>
878 this->scale(
alpha );
882template<
typename OrdinalType,
typename ScalarType>
888 for (
j=0;
j<numCols_;
j++) {
889 ptr = values_ +
j*stride_;
893 if (flopCounter_!=0) updateFlops( numRows_*numCols_ );
897template<
typename OrdinalType,
typename ScalarType>
904 if ((numRows_ !=
A.numRows_) || (numCols_ !=
A.numCols_))
908 for (
j=0;
j<numCols_;
j++) {
909 ptr = values_ +
j*stride_;
910 for (
i=0;
i<numRows_;
i++) { *
ptr =
A(
i,
j) * (*ptr);
ptr++; }
913 if (flopCounter_!=0) updateFlops( numRows_*numCols_ );
917template<
typename OrdinalType,
typename ScalarType>
930 this->GEMM(
transa,
transb, numRows_, numCols_,
A_ncols,
alpha,
A.values(),
A.stride(),
B.values(),
B.stride(),
beta, values_, stride_);
933 if (flopCounter_!=0) {
934 double nflops = 2 * numRows_;
942template<
typename OrdinalType,
typename ScalarType>
949 if (ESideChar[
sideA]==
'L') {
961 this->SYMM(
sideA,
uplo, numRows_, numCols_,
alpha,
A.values(),
A.stride(),
B.values(),
B.stride(),
beta, values_, stride_);
964 if (flopCounter_!=0) {
965 double nflops = 2 * numRows_;
973template<
typename OrdinalType,
typename ScalarType>
978 os <<
"Values_copied : yes" << std::endl;
980 os <<
"Values_copied : no" << std::endl;
981 os <<
"Rows : " << numRows_ << std::endl;
982 os <<
"Columns : " << numCols_ << std::endl;
983 os <<
"LDA : " << stride_ << std::endl;
984 if(numRows_ == 0 || numCols_ == 0) {
985 os <<
"(matrix is empty, no values to display)" << std::endl;
989 os << (*this)(
i,
j) <<
" ";
1001template<
typename OrdinalType,
typename ScalarType>
1004 "SerialDenseMatrix<T>::checkIndex: "
1005 "Row index " << rowIndex <<
" out of range [0, "<< numRows_ <<
")");
1007 "SerialDenseMatrix<T>::checkIndex: "
1008 "Col index " << colIndex <<
" out of range [0, "<< numCols_ <<
")");
1011template<
typename OrdinalType,
typename ScalarType>
1012void SerialDenseMatrix<OrdinalType, ScalarType>::deleteArrays(
void)
1018 valuesCopied_ =
false;
1022template<
typename OrdinalType,
typename ScalarType>
1023void SerialDenseMatrix<OrdinalType, ScalarType>::copyMat(
1024 ScalarType* inputMatrix, OrdinalType strideInput, OrdinalType numRows_in,
1025 OrdinalType numCols_in, ScalarType* outputMatrix, OrdinalType strideOutput,
1026 OrdinalType startRow, OrdinalType startCol, ScalarType alpha
1030 ScalarType* ptr1 = 0;
1031 ScalarType* ptr2 = 0;
1032 for(j = 0; j < numCols_in; j++) {
1033 ptr1 = outputMatrix + (j * strideOutput);
1034 ptr2 = inputMatrix + (j + startCol) * strideInput + startRow;
1036 for(i = 0; i < numRows_in; i++)
1038 *ptr1++ += alpha*(*ptr2++);
1041 for(i = 0; i < numRows_in; i++)
1050template<
typename OrdinalType,
typename ScalarType>
1060template<
typename OrdinalType,
typename ScalarType>
1070template<
typename OrdinalType,
typename ScalarType>
1071SerialDenseMatrixPrinter<OrdinalType,ScalarType>
Templated interface class to BLAS routines.
Object for storing data and providing functionality that is common to all computational classes.
Teuchos header file which uses auto-configuration information to include necessary C++ headers.
Teuchos::DataAccess Mode enumerable type.
Defines basic traits for the scalar field type.
Templated serial, dense, symmetric matrix class.
Functionality and data that is common to all computational classes.
Smart reference counting pointer class for automatic garbage collection.
Ptr< T > ptr() const
Get a safer wrapper raw C++ pointer to the underlying object.
This class creates and provides basic support for dense rectangular matrix of templated type.
int putScalar(const ScalarType value=Teuchos::ScalarTraits< ScalarType >::zero())
Set all values in the matrix to a constant value.
ScalarType & operator()(OrdinalType rowIndex, OrdinalType colIndex)
Element access method (non-const).
void swap(SerialDenseMatrix< OrdinalType, ScalarType > &B)
Swap values between this matrix and incoming matrix.
ScalarTraits< ScalarType >::magnitudeType normOne() const
Returns the 1-norm of the matrix.
SerialDenseMatrix< OrdinalType, ScalarType > & assign(const SerialDenseMatrix< OrdinalType, ScalarType > &Source)
Copies values from one matrix to another.
int reshape(OrdinalType numRows, OrdinalType numCols)
Reshaping method for changing the size of a SerialDenseMatrix, keeping the entries.
bool operator!=(const SerialDenseMatrix< OrdinalType, ScalarType > &Operand) const
Inequality of two matrices.
int scale(const ScalarType alpha)
Scale this matrix by alpha; *this = alpha**this.
SerialDenseMatrix< OrdinalType, ScalarType > & operator=(const SerialDenseMatrix< OrdinalType, ScalarType > &Source)
Copies values from one matrix to another.
bool empty() const
Returns whether this matrix is empty.
virtual ~SerialDenseMatrix()
Destructor.
SerialDenseMatrix< OrdinalType, ScalarType > & operator-=(const SerialDenseMatrix< OrdinalType, ScalarType > &Source)
Subtract another matrix from this matrix.
ScalarType * operator[](OrdinalType colIndex)
Column access method (non-const).
SerialDenseMatrix< OrdinalType, ScalarType > & operator+=(const SerialDenseMatrix< OrdinalType, ScalarType > &Source)
Add another matrix to this matrix.
OrdinalType stride() const
Returns the stride between the columns of this matrix in memory.
int random()
Set all values in the matrix to be random numbers.
ScalarType scalarType
Typedef for scalar type.
ScalarTraits< ScalarType >::magnitudeType normInf() const
Returns the Infinity-norm of the matrix.
OrdinalType numRows() const
Returns the row dimension of this matrix.
ScalarType * values() const
Data array access method.
int shapeUninitialized(OrdinalType numRows, OrdinalType numCols)
Same as shape() except leaves uninitialized.
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.
bool operator==(const SerialDenseMatrix< OrdinalType, ScalarType > &Operand) const
Equality of two matrices.
virtual std::ostream & print(std::ostream &os) const
Print method. Defines the behavior of the std::ostream << operator.
ScalarTraits< ScalarType >::magnitudeType normFrobenius() const
Returns the Frobenius-norm of the matrix.
OrdinalType numCols() const
Returns the column dimension of this matrix.
SerialDenseMatrix()=default
Default Constructor.
SerialDenseMatrix< OrdinalType, ScalarType > & operator*=(const ScalarType alpha)
Scale this matrix by alpha; *this = alpha**this.
int shape(OrdinalType numRows, OrdinalType numCols)
Shape method for changing the size of a SerialDenseMatrix, initializing entries to zero.
OrdinalType ordinalType
Typedef for ordinal type.
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
Macro for throwing an exception with breakpointing to ease debugging.
The Teuchos namespace contains all of the classes, structs and enums used by Teuchos,...
SerialBandDenseMatrixPrinter< OrdinalType, ScalarType > printMat(const SerialBandDenseMatrix< OrdinalType, ScalarType > &obj)
Return SerialBandDenseMatrix ostream manipulator Use as:
This structure defines some basic traits for a scalar field type.
static magnitudeType magnitude(T a)
Returns the magnitudeType of the scalar type a.
T magnitudeType
Mandatory typedef for result of magnitude.
static T zero()
Returns representation of zero for this scalar type.
static T conjugate(T a)
Returns the conjugate of the scalar type a.
static T random()
Returns a random number (between -one() and +one()) of this scalar type.
Ostream manipulator for SerialDenseMatrix.