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>
421 : valuesCopied_(
true)
425 numRows_ = Source.numRows_;
426 numCols_ = Source.numCols_;
428 if (!Source.valuesCopied_)
430 stride_ = Source.stride_;
431 values_ = Source.values_;
432 valuesCopied_ =
false;
437 if(stride_ > 0 && numCols_ > 0) {
438 values_ = allocateValues(stride_, numCols_);
439 copyMat(Source.values_, Source.stride_, numRows_, numCols_, values_, stride_, 0, 0);
442 numRows_ = 0; numCols_ = 0; stride_ = 0;
443 valuesCopied_ =
false;
449 numRows_ = Source.numCols_;
450 numCols_ = Source.numRows_;
452 values_ = allocateValues(stride_, numCols_);
461 numRows_ = Source.numCols_;
462 numCols_ = Source.numRows_;
464 values_ = allocateValues(stride_, numCols_);
467 values_[
j*stride_ +
i] = Source.values_[
i*Source.stride_ +
j];
474template<
typename OrdinalType,
typename ScalarType>
478 : numRows_(Source.numRows_), numCols_(Source.numCols_), stride_(Source.stride_),
479 valuesCopied_(
false), values_(Source.values_)
484 values_ = allocateValues(stride_, numCols_);
485 copyMat(Source.values_, Source.stride_, numRows_, numCols_, values_, stride_, 0, 0);
486 valuesCopied_ =
true;
491template<
typename OrdinalType,
typename ScalarType>
498 valuesCopied_(
false), values_(Source.values_)
503 values_ = allocateValues(stride_,
numCols_in);
505 valuesCopied_ =
true;
513template<
typename OrdinalType,
typename ScalarType>
523template<
typename OrdinalType,
typename ScalarType>
532 values_ = allocateValues(stride_, numCols_);
534 valuesCopied_ =
true;
538template<
typename OrdinalType,
typename ScalarType>
547 values_ = allocateValues(stride_, numCols_);
548 valuesCopied_ =
true;
552template<
typename OrdinalType,
typename ScalarType>
576 valuesCopied_ =
true;
584template<
typename OrdinalType,
typename ScalarType>
598template<
typename OrdinalType,
typename ScalarType>
void
616 std::swap(values_ ,
B.values_);
617 std::swap(numRows_,
B.numRows_);
618 std::swap(numCols_,
B.numCols_);
619 std::swap(stride_,
B.stride_);
620 std::swap(valuesCopied_,
B.valuesCopied_);
623template<
typename OrdinalType,
typename ScalarType>
637template<
typename OrdinalType,
typename ScalarType>
645 if((!valuesCopied_) && (!Source.valuesCopied_) && (values_ == Source.values_))
649 if (!Source.valuesCopied_) {
654 numRows_ = Source.numRows_;
655 numCols_ = Source.numCols_;
656 stride_ = Source.stride_;
657 values_ = Source.values_;
662 numRows_ = Source.numRows_;
663 numCols_ = Source.numCols_;
664 stride_ = Source.numRows_;
665 if(stride_ > 0 && numCols_ > 0) {
666 values_ = allocateValues(stride_, numCols_);
667 valuesCopied_ =
true;
675 if((Source.numRows_ <= stride_) && (Source.numCols_ == numCols_)) {
676 numRows_ = Source.numRows_;
677 numCols_ = Source.numCols_;
681 numRows_ = Source.numRows_;
682 numCols_ = Source.numCols_;
683 stride_ = Source.numRows_;
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);
695template<
typename OrdinalType,
typename ScalarType>
699 if ((numRows_ != Source.numRows_) || (numCols_ != Source.numCols_))
701 TEUCHOS_CHK_REF(*
this);
707template<
typename OrdinalType,
typename ScalarType>
711 if ((numRows_ != Source.numRows_) || (numCols_ != Source.numCols_))
713 TEUCHOS_CHK_REF(*
this);
719template<
typename OrdinalType,
typename ScalarType>
723 if((!valuesCopied_) && (!Source.valuesCopied_) && (values_ == Source.values_))
727 if ((numRows_ != Source.numRows_) || (numCols_ != Source.numCols_))
729 TEUCHOS_CHK_REF(*
this);
731 copyMat(Source.values_, Source.stride_, numRows_, numCols_, values_, stride_, 0, 0);
739template<
typename OrdinalType,
typename ScalarType>
742#ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
743 checkIndex( rowIndex, colIndex );
745 return(values_[colIndex * stride_ + rowIndex]);
748template<
typename OrdinalType,
typename ScalarType>
751#ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
752 checkIndex( rowIndex, colIndex );
754 return(values_[colIndex * stride_ + rowIndex]);
757template<
typename OrdinalType,
typename ScalarType>
760#ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
761 checkIndex( 0, colIndex );
763 return(values_ + colIndex * stride_);
766template<
typename OrdinalType,
typename ScalarType>
769#ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
770 checkIndex( 0, colIndex );
772 return(values_ + colIndex * stride_);
779template<
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_);
805template<
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_);
823template<
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_);
843template<
typename OrdinalType,
typename ScalarType>
847 if((numRows_ !=
Operand.numRows_) || (numCols_ !=
Operand.numCols_))
854 for(
i = 0;
i < numRows_;
i++)
856 for(
j = 0;
j < numCols_;
j++)
868template<
typename OrdinalType,
typename ScalarType>
878template<
typename OrdinalType,
typename ScalarType>
881 this->scale(
alpha );
885template<
typename OrdinalType,
typename ScalarType>
891 for (
j=0;
j<numCols_;
j++) {
892 ptr = values_ +
j*stride_;
896 if (flopCounter_!=0) updateFlops( numRows_*numCols_ );
900template<
typename OrdinalType,
typename ScalarType>
907 if ((numRows_ !=
A.numRows_) || (numCols_ !=
A.numCols_))
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_ );
920template<
typename OrdinalType,
typename ScalarType>
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_;
945template<
typename OrdinalType,
typename ScalarType>
952 if (ESideChar[
sideA]==
'L') {
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_;
976template<
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;
992 os << (*this)(
i,
j) <<
" ";
1004template<
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_ <<
")");
1014template<
typename OrdinalType,
typename ScalarType>
1015void SerialDenseMatrix<OrdinalType, ScalarType>::deleteArrays(
void)
1021 valuesCopied_ =
false;
1025template<
typename OrdinalType,
typename ScalarType>
1026void SerialDenseMatrix<OrdinalType, ScalarType>::copyMat(
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++)
1053template<
typename OrdinalType,
typename ScalarType>
1063template<
typename OrdinalType,
typename ScalarType>
1073template<
typename OrdinalType,
typename ScalarType>
1074SerialDenseMatrixPrinter<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.