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;
373 OrdinalType numRows_ = 0;
374 OrdinalType numCols_ = 0;
375 OrdinalType stride_ = 0;
376 bool valuesCopied_ =
false;
377 ScalarType* values_ =
nullptr;
384template<
typename OrdinalType,
typename ScalarType>
399template<
typename OrdinalType,
typename ScalarType>
407 valuesCopied_(
false),
413 values_ = allocateValues(stride_, numCols_);
415 valuesCopied_ =
true;
419template<
typename OrdinalType,
typename ScalarType>
427 numRows_ = Source.numRows_;
428 numCols_ = Source.numCols_;
430 if (!Source.valuesCopied_)
432 stride_ = Source.stride_;
433 values_ = Source.values_;
434 valuesCopied_ =
false;
439 if(stride_ > 0 && numCols_ > 0) {
440 values_ = allocateValues(stride_, numCols_);
441 copyMat(Source.values_, Source.stride_, numRows_, numCols_, values_, stride_, 0, 0);
444 numRows_ = 0; numCols_ = 0; stride_ = 0;
445 valuesCopied_ =
false;
451 numRows_ = Source.numCols_;
452 numCols_ = Source.numRows_;
454 values_ = allocateValues(stride_, numCols_);
463 numRows_ = Source.numCols_;
464 numCols_ = Source.numRows_;
466 values_ = allocateValues(stride_, numCols_);
469 values_[
j*stride_ +
i] = Source.values_[
i*Source.stride_ +
j];
476template<
typename OrdinalType,
typename ScalarType>
480 : numRows_(Source.numRows_), numCols_(Source.numCols_), stride_(Source.stride_),
481 valuesCopied_(
false), values_(Source.values_)
486 values_ = allocateValues(stride_, numCols_);
487 copyMat(Source.values_, Source.stride_, numRows_, numCols_, values_, stride_, 0, 0);
488 valuesCopied_ =
true;
493template<
typename OrdinalType,
typename ScalarType>
500 valuesCopied_(
false), values_(Source.values_)
505 values_ = allocateValues(stride_,
numCols_in);
507 valuesCopied_ =
true;
515template<
typename OrdinalType,
typename ScalarType>
525template<
typename OrdinalType,
typename ScalarType>
534 values_ = allocateValues(stride_, numCols_);
536 valuesCopied_ =
true;
540template<
typename OrdinalType,
typename ScalarType>
549 values_ = allocateValues(stride_, numCols_);
550 valuesCopied_ =
true;
554template<
typename OrdinalType,
typename ScalarType>
578 valuesCopied_ =
true;
586template<
typename OrdinalType,
typename ScalarType>
600template<
typename OrdinalType,
typename ScalarType>
void
618 std::swap(values_ ,
B.values_);
619 std::swap(numRows_,
B.numRows_);
620 std::swap(numCols_,
B.numCols_);
621 std::swap(stride_,
B.stride_);
622 std::swap(valuesCopied_,
B.valuesCopied_);
625template<
typename OrdinalType,
typename ScalarType>
639template<
typename OrdinalType,
typename ScalarType>
647 if((!valuesCopied_) && (!Source.valuesCopied_) && (values_ == Source.values_))
651 if (!Source.valuesCopied_) {
656 numRows_ = Source.numRows_;
657 numCols_ = Source.numCols_;
658 stride_ = Source.stride_;
659 values_ = Source.values_;
664 numRows_ = Source.numRows_;
665 numCols_ = Source.numCols_;
666 stride_ = Source.numRows_;
667 if(stride_ > 0 && numCols_ > 0) {
668 values_ = allocateValues(stride_, numCols_);
669 valuesCopied_ =
true;
677 if((Source.numRows_ <= stride_) && (Source.numCols_ == numCols_)) {
678 numRows_ = Source.numRows_;
679 numCols_ = Source.numCols_;
683 numRows_ = Source.numRows_;
684 numCols_ = Source.numCols_;
685 stride_ = Source.numRows_;
686 if(stride_ > 0 && numCols_ > 0) {
687 values_ = allocateValues(stride_, numCols_);
688 valuesCopied_ =
true;
692 copyMat(Source.values_, Source.stride_, numRows_, numCols_, values_, stride_, 0, 0);
697template<
typename OrdinalType,
typename ScalarType>
701 if ((numRows_ != Source.numRows_) || (numCols_ != Source.numCols_))
703 TEUCHOS_CHK_REF(*
this);
709template<
typename OrdinalType,
typename ScalarType>
713 if ((numRows_ != Source.numRows_) || (numCols_ != Source.numCols_))
715 TEUCHOS_CHK_REF(*
this);
721template<
typename OrdinalType,
typename ScalarType>
725 if((!valuesCopied_) && (!Source.valuesCopied_) && (values_ == Source.values_))
729 if ((numRows_ != Source.numRows_) || (numCols_ != Source.numCols_))
731 TEUCHOS_CHK_REF(*
this);
733 copyMat(Source.values_, Source.stride_, numRows_, numCols_, values_, stride_, 0, 0);
741template<
typename OrdinalType,
typename ScalarType>
744#ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
745 checkIndex( rowIndex, colIndex );
747 return(values_[colIndex * stride_ + rowIndex]);
750template<
typename OrdinalType,
typename ScalarType>
753#ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
754 checkIndex( rowIndex, colIndex );
756 return(values_[colIndex * stride_ + rowIndex]);
759template<
typename OrdinalType,
typename ScalarType>
762#ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
763 checkIndex( 0, colIndex );
765 return(values_ + colIndex * stride_);
768template<
typename OrdinalType,
typename ScalarType>
771#ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
772 checkIndex( 0, colIndex );
774 return(values_ + colIndex * stride_);
781template<
typename OrdinalType,
typename ScalarType>
788 for(
j = 0;
j < numCols_;
j++)
791 ptr = values_ +
j * stride_;
792 for(
i = 0;
i < numRows_;
i++)
803 if (flopCounter_!=0) updateFlops(numRows_ * numCols_);
807template<
typename OrdinalType,
typename ScalarType>
813 for (
i = 0;
i < numRows_;
i++) {
815 for (
j=0;
j< numCols_;
j++) {
821 if (flopCounter_!=0) updateFlops(numRows_ * numCols_);
825template<
typename OrdinalType,
typename ScalarType>
830 for (
j = 0;
j < numCols_;
j++) {
831 for (
i = 0;
i < numRows_;
i++) {
837 if (flopCounter_!=0) updateFlops(numRows_ * numCols_);
845template<
typename OrdinalType,
typename ScalarType>
849 if((numRows_ !=
Operand.numRows_) || (numCols_ !=
Operand.numCols_))
856 for(
i = 0;
i < numRows_;
i++)
858 for(
j = 0;
j < numCols_;
j++)
870template<
typename OrdinalType,
typename ScalarType>
880template<
typename OrdinalType,
typename ScalarType>
883 this->scale(
alpha );
887template<
typename OrdinalType,
typename ScalarType>
893 for (
j=0;
j<numCols_;
j++) {
894 ptr = values_ +
j*stride_;
898 if (flopCounter_!=0) updateFlops( numRows_*numCols_ );
902template<
typename OrdinalType,
typename ScalarType>
909 if ((numRows_ !=
A.numRows_) || (numCols_ !=
A.numCols_))
913 for (
j=0;
j<numCols_;
j++) {
914 ptr = values_ +
j*stride_;
915 for (
i=0;
i<numRows_;
i++) { *
ptr =
A(
i,
j) * (*ptr);
ptr++; }
918 if (flopCounter_!=0) updateFlops( numRows_*numCols_ );
922template<
typename OrdinalType,
typename ScalarType>
935 this->GEMM(
transa,
transb, numRows_, numCols_,
A_ncols,
alpha,
A.values(),
A.stride(),
B.values(),
B.stride(),
beta, values_, stride_);
938 if (flopCounter_!=0) {
939 double nflops = 2 * numRows_;
947template<
typename OrdinalType,
typename ScalarType>
954 if (ESideChar[
sideA]==
'L') {
966 this->SYMM(
sideA,
uplo, numRows_, numCols_,
alpha,
A.values(),
A.stride(),
B.values(),
B.stride(),
beta, values_, stride_);
969 if (flopCounter_!=0) {
970 double nflops = 2 * numRows_;
978template<
typename OrdinalType,
typename ScalarType>
983 os <<
"Values_copied : yes" << std::endl;
985 os <<
"Values_copied : no" << std::endl;
986 os <<
"Rows : " << numRows_ << std::endl;
987 os <<
"Columns : " << numCols_ << std::endl;
988 os <<
"LDA : " << stride_ << std::endl;
989 if(numRows_ == 0 || numCols_ == 0) {
990 os <<
"(matrix is empty, no values to display)" << std::endl;
994 os << (*this)(
i,
j) <<
" ";
1006template<
typename OrdinalType,
typename ScalarType>
1009 "SerialDenseMatrix<T>::checkIndex: "
1010 "Row index " << rowIndex <<
" out of range [0, "<< numRows_ <<
")");
1012 "SerialDenseMatrix<T>::checkIndex: "
1013 "Col index " << colIndex <<
" out of range [0, "<< numCols_ <<
")");
1016template<
typename OrdinalType,
typename ScalarType>
1017void SerialDenseMatrix<OrdinalType, ScalarType>::deleteArrays(
void)
1023 valuesCopied_ =
false;
1027template<
typename OrdinalType,
typename ScalarType>
1028void SerialDenseMatrix<OrdinalType, ScalarType>::copyMat(
1029 ScalarType* inputMatrix, OrdinalType strideInput, OrdinalType numRows_in,
1030 OrdinalType numCols_in, ScalarType* outputMatrix, OrdinalType strideOutput,
1031 OrdinalType startRow, OrdinalType startCol, ScalarType alpha
1035 ScalarType* ptr1 = 0;
1036 ScalarType* ptr2 = 0;
1037 for(j = 0; j < numCols_in; j++) {
1038 ptr1 = outputMatrix + (j * strideOutput);
1039 ptr2 = inputMatrix + (j + startCol) * strideInput + startRow;
1041 for(i = 0; i < numRows_in; i++)
1043 *ptr1++ += alpha*(*ptr2++);
1046 for(i = 0; i < numRows_in; i++)
1055template<
typename OrdinalType,
typename ScalarType>
1065template<
typename OrdinalType,
typename ScalarType>
1075template<
typename OrdinalType,
typename ScalarType>
1076SerialDenseMatrixPrinter<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.