40#ifndef _TEUCHOS_SERIALDENSEMATRIX_HPP_
41#define _TEUCHOS_SERIALDENSEMATRIX_HPP_
51#include "Teuchos_Assert.hpp"
66template<
typename OrdinalType,
typename ScalarType>
362 bool empty()
const {
return(numRows_ == 0 || numCols_ == 0); }
381 virtual std::ostream&
print(std::ostream& os)
const;
397#pragma GCC diagnostic push
398#pragma GCC diagnostic ignored "-Wvla"
400#pragma GCC diagnostic pop
403 OrdinalType numRows_ = 0;
404 OrdinalType numCols_ = 0;
405 OrdinalType stride_ = 0;
406 bool valuesCopied_ =
false;
407 ScalarType* values_ =
nullptr;
414template<
typename OrdinalType,
typename ScalarType>
429template<
typename OrdinalType,
typename ScalarType>
437 valuesCopied_(
false),
443 values_ = allocateValues(stride_, numCols_);
445 valuesCopied_ =
true;
449template<
typename OrdinalType,
typename ScalarType>
451 : valuesCopied_(
true)
455 numRows_ = Source.numRows_;
456 numCols_ = Source.numCols_;
458 if (!Source.valuesCopied_)
460 stride_ = Source.stride_;
461 values_ = Source.values_;
462 valuesCopied_ =
false;
467 if(stride_ > 0 && numCols_ > 0) {
468 values_ = allocateValues(stride_, numCols_);
469 copyMat(Source.values_, Source.stride_, numRows_, numCols_, values_, stride_, 0, 0);
472 numRows_ = 0; numCols_ = 0; stride_ = 0;
473 valuesCopied_ =
false;
479 numRows_ = Source.numCols_;
480 numCols_ = Source.numRows_;
482 values_ = allocateValues(stride_, numCols_);
491 numRows_ = Source.numCols_;
492 numCols_ = Source.numRows_;
494 values_ = allocateValues(stride_, numCols_);
497 values_[
j*stride_ +
i] = Source.values_[
i*Source.stride_ +
j];
504template<
typename OrdinalType,
typename ScalarType>
508 : numRows_(Source.numRows_), numCols_(Source.numCols_), stride_(Source.stride_),
509 valuesCopied_(
false), values_(Source.values_)
514 values_ = allocateValues(stride_, numCols_);
515 copyMat(Source.values_, Source.stride_, numRows_, numCols_, values_, stride_, 0, 0);
516 valuesCopied_ =
true;
521template<
typename OrdinalType,
typename ScalarType>
528 valuesCopied_(
false), values_(Source.values_)
533 values_ = allocateValues(stride_,
numCols_in);
535 valuesCopied_ =
true;
543template<
typename OrdinalType,
typename ScalarType>
553template<
typename OrdinalType,
typename ScalarType>
562 values_ = allocateValues(stride_, numCols_);
564 valuesCopied_ =
true;
568template<
typename OrdinalType,
typename ScalarType>
577 values_ = allocateValues(stride_, numCols_);
578 valuesCopied_ =
true;
582template<
typename OrdinalType,
typename ScalarType>
606 valuesCopied_ =
true;
614template<
typename OrdinalType,
typename ScalarType>
628template<
typename OrdinalType,
typename ScalarType>
void
646 std::swap(values_ ,
B.values_);
647 std::swap(numRows_,
B.numRows_);
648 std::swap(numCols_,
B.numCols_);
649 std::swap(stride_,
B.stride_);
650 std::swap(valuesCopied_,
B.valuesCopied_);
653template<
typename OrdinalType,
typename ScalarType>
667template<
typename OrdinalType,
typename ScalarType>
675 if((!valuesCopied_) && (!Source.valuesCopied_) && (values_ == Source.values_))
679 if (!Source.valuesCopied_) {
684 numRows_ = Source.numRows_;
685 numCols_ = Source.numCols_;
686 stride_ = Source.stride_;
687 values_ = Source.values_;
692 numRows_ = Source.numRows_;
693 numCols_ = Source.numCols_;
694 stride_ = Source.numRows_;
695 if(stride_ > 0 && numCols_ > 0) {
696 values_ = allocateValues(stride_, numCols_);
697 valuesCopied_ =
true;
705 if((Source.numRows_ <= stride_) && (Source.numCols_ == numCols_)) {
706 numRows_ = Source.numRows_;
707 numCols_ = Source.numCols_;
711 numRows_ = Source.numRows_;
712 numCols_ = Source.numCols_;
713 stride_ = Source.numRows_;
714 if(stride_ > 0 && numCols_ > 0) {
715 values_ = allocateValues(stride_, numCols_);
716 valuesCopied_ =
true;
720 copyMat(Source.values_, Source.stride_, numRows_, numCols_, values_, stride_, 0, 0);
725template<
typename OrdinalType,
typename ScalarType>
729 if ((numRows_ != Source.numRows_) || (numCols_ != Source.numCols_))
731 TEUCHOS_CHK_REF(*
this);
737template<
typename OrdinalType,
typename ScalarType>
741 if ((numRows_ != Source.numRows_) || (numCols_ != Source.numCols_))
743 TEUCHOS_CHK_REF(*
this);
749template<
typename OrdinalType,
typename ScalarType>
753 if((!valuesCopied_) && (!Source.valuesCopied_) && (values_ == Source.values_))
757 if ((numRows_ != Source.numRows_) || (numCols_ != Source.numCols_))
759 TEUCHOS_CHK_REF(*
this);
761 copyMat(Source.values_, Source.stride_, numRows_, numCols_, values_, stride_, 0, 0);
769template<
typename OrdinalType,
typename ScalarType>
772#ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
773 checkIndex( rowIndex, colIndex );
775 return(values_[colIndex * stride_ + rowIndex]);
778template<
typename OrdinalType,
typename ScalarType>
781#ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
782 checkIndex( rowIndex, colIndex );
784 return(values_[colIndex * stride_ + rowIndex]);
787template<
typename OrdinalType,
typename ScalarType>
790#ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
791 checkIndex( 0, colIndex );
793 return(values_ + colIndex * stride_);
796template<
typename OrdinalType,
typename ScalarType>
799#ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
800 checkIndex( 0, colIndex );
802 return(values_ + colIndex * stride_);
809template<
typename OrdinalType,
typename ScalarType>
816 for(
j = 0;
j < numCols_;
j++)
819 ptr = values_ +
j * stride_;
820 for(
i = 0;
i < numRows_;
i++)
831 if (flopCounter_!=0) updateFlops(numRows_ * numCols_);
835template<
typename OrdinalType,
typename ScalarType>
841 for (
i = 0;
i < numRows_;
i++) {
843 for (
j=0;
j< numCols_;
j++) {
849 if (flopCounter_!=0) updateFlops(numRows_ * numCols_);
853template<
typename OrdinalType,
typename ScalarType>
858 for (
j = 0;
j < numCols_;
j++) {
859 for (
i = 0;
i < numRows_;
i++) {
865 if (flopCounter_!=0) updateFlops(numRows_ * numCols_);
873template<
typename OrdinalType,
typename ScalarType>
877 if((numRows_ !=
Operand.numRows_) || (numCols_ !=
Operand.numCols_))
884 for(
i = 0;
i < numRows_;
i++)
886 for(
j = 0;
j < numCols_;
j++)
898template<
typename OrdinalType,
typename ScalarType>
908template<
typename OrdinalType,
typename ScalarType>
911 this->scale(
alpha );
915template<
typename OrdinalType,
typename ScalarType>
921 for (
j=0;
j<numCols_;
j++) {
922 ptr = values_ +
j*stride_;
926 if (flopCounter_!=0) updateFlops( numRows_*numCols_ );
930template<
typename OrdinalType,
typename ScalarType>
937 if ((numRows_ !=
A.numRows_) || (numCols_ !=
A.numCols_))
941 for (
j=0;
j<numCols_;
j++) {
942 ptr = values_ +
j*stride_;
943 for (
i=0;
i<numRows_;
i++) { *
ptr =
A(
i,
j) * (*ptr);
ptr++; }
946 if (flopCounter_!=0) updateFlops( numRows_*numCols_ );
950template<
typename OrdinalType,
typename ScalarType>
963 this->GEMM(
transa,
transb, numRows_, numCols_,
A_ncols,
alpha,
A.values(),
A.stride(),
B.values(),
B.stride(),
beta, values_, stride_);
966 if (flopCounter_!=0) {
967 double nflops = 2 * numRows_;
975template<
typename OrdinalType,
typename ScalarType>
982 if (ESideChar[
sideA]==
'L') {
994 this->SYMM(
sideA,
uplo, numRows_, numCols_,
alpha,
A.values(),
A.stride(),
B.values(),
B.stride(),
beta, values_, stride_);
997 if (flopCounter_!=0) {
998 double nflops = 2 * numRows_;
1006template<
typename OrdinalType,
typename ScalarType>
1011 os <<
"Values_copied : yes" << std::endl;
1013 os <<
"Values_copied : no" << std::endl;
1014 os <<
"Rows : " << numRows_ << std::endl;
1015 os <<
"Columns : " << numCols_ << std::endl;
1016 os <<
"LDA : " << stride_ << std::endl;
1017 if(numRows_ == 0 || numCols_ == 0) {
1018 os <<
"(matrix is empty, no values to display)" << std::endl;
1022 os << (*this)(
i,
j) <<
" ";
1034template<
typename OrdinalType,
typename ScalarType>
1037 "SerialDenseMatrix<T>::checkIndex: "
1038 "Row index " << rowIndex <<
" out of range [0, "<< numRows_ <<
")");
1040 "SerialDenseMatrix<T>::checkIndex: "
1041 "Col index " << colIndex <<
" out of range [0, "<< numCols_ <<
")");
1044template<
typename OrdinalType,
typename ScalarType>
1045void SerialDenseMatrix<OrdinalType, ScalarType>::deleteArrays(
void)
1051 valuesCopied_ =
false;
1055template<
typename OrdinalType,
typename ScalarType>
1056void SerialDenseMatrix<OrdinalType, ScalarType>::copyMat(
1057 ScalarType* inputMatrix, OrdinalType strideInput, OrdinalType numRows_in,
1058 OrdinalType numCols_in, ScalarType* outputMatrix, OrdinalType strideOutput,
1059 OrdinalType startRow, OrdinalType startCol, ScalarType alpha
1083template<
typename OrdinalType,
typename ScalarType>
1093template<
typename OrdinalType,
typename ScalarType>
1103template<
typename OrdinalType,
typename ScalarType>
1104SerialDenseMatrixPrinter<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.
RCP(ENull null_arg=null)
Initialize RCP<T> to NULL.
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.