42 #ifndef _TEUCHOS_SERIALBANDDENSEMATRIX_HPP_
43 #define _TEUCHOS_SERIALBANDDENSEMATRIX_HPP_
54 #include "Teuchos_Assert.hpp"
132 template<
typename OrdinalType,
typename ScalarType>
293 ScalarType&
operator () (OrdinalType rowIndex, OrdinalType colIndex);
303 const ScalarType&
operator () (OrdinalType rowIndex, OrdinalType colIndex)
const;
323 const ScalarType*
operator [] (OrdinalType colIndex)
const;
327 ScalarType*
values()
const {
return(values_); }
357 int scale (
const ScalarType alpha );
391 OrdinalType
numRows()
const {
return(numRows_); }
394 OrdinalType
numCols()
const {
return(numCols_); }
403 OrdinalType
stride()
const {
return(stride_); }
406 bool empty()
const {
return(numRows_ == 0 || numCols_ == 0); }
424 #ifndef TEUCHOS_HIDE_DEPRECATED_CODE
426 virtual void print(std::ostream& os)
const;
428 virtual std::ostream&
print(std::ostream& os)
const;
433 void copyMat(ScalarType* inputMatrix, OrdinalType strideInput,
434 OrdinalType
numRows, OrdinalType
numCols, ScalarType* outputMatrix,
437 void checkIndex( OrdinalType rowIndex, OrdinalType colIndex = 0 )
const;
438 OrdinalType numRows_;
439 OrdinalType numCols_;
452 template<
typename OrdinalType,
typename ScalarType>
455 BLAS<OrdinalType,ScalarType>(),
461 valuesCopied_ (false),
465 template<
typename OrdinalType,
typename ScalarType>
468 OrdinalType numCols_in,
473 BLAS<OrdinalType,ScalarType>(),
474 numRows_ (numRows_in),
475 numCols_ (numCols_in),
476 stride_ (kl_in+ku_in+1),
479 valuesCopied_ (true),
482 values_ =
new ScalarType[stride_ * numCols_];
488 template<
typename OrdinalType,
typename ScalarType>
491 ScalarType* values_in,
492 OrdinalType stride_in,
493 OrdinalType numRows_in,
494 OrdinalType numCols_in,
498 BLAS<OrdinalType,ScalarType>(),
499 numRows_ (numRows_in),
500 numCols_ (numCols_in),
504 valuesCopied_ (false),
509 values_ =
new ScalarType[stride_*numCols_];
510 copyMat (values_in, stride_in, numRows_, numCols_, values_, stride_, 0);
511 valuesCopied_ =
true;
515 template<
typename OrdinalType,
typename ScalarType>
519 BLAS<OrdinalType,ScalarType>(),
525 valuesCopied_ (true),
529 numRows_ = Source.numRows_;
530 numCols_ = Source.numCols_;
534 values_ =
new ScalarType[stride_*numCols_];
535 copyMat (Source.values_, Source.stride_, numRows_, numCols_,
536 values_, stride_, 0);
539 numRows_ = Source.numCols_;
540 numCols_ = Source.numRows_;
544 values_ =
new ScalarType[stride_*numCols_];
545 for (OrdinalType j = 0; j < numCols_; ++j) {
546 for (OrdinalType i = TEUCHOS_MAX(0,j-ku_);
547 i <= TEUCHOS_MIN(numRows_-1,j+kl_); ++i) {
548 values_[j*stride_ + (ku_+i-j)] =
554 numRows_ = Source.numCols_;
555 numCols_ = Source.numRows_;
559 values_ =
new ScalarType[stride_*numCols_];
560 for (OrdinalType j=0; j<numCols_; j++) {
561 for (OrdinalType i = TEUCHOS_MAX(0,j-ku_);
562 i <= TEUCHOS_MIN(numRows_-1,j+kl_); ++i) {
563 values_[j*stride_ + (ku_+i-j)] = Source.values_[i*Source.stride_ + (Source.ku_+j-i)];
569 template<
typename OrdinalType,
typename ScalarType>
573 OrdinalType numRows_in,
574 OrdinalType numCols_in,
575 OrdinalType startCol)
577 BLAS<OrdinalType,ScalarType>(),
578 numRows_ (numRows_in),
579 numCols_ (numCols_in),
580 stride_ (Source.stride_),
583 valuesCopied_ (false),
584 values_ (Source.values_)
587 values_ =
new ScalarType[stride_ * numCols_in];
588 copyMat (Source.values_, Source.stride_, numRows_in, numCols_in,
589 values_, stride_, startCol);
590 valuesCopied_ =
true;
592 values_ = values_ + (stride_ * startCol);
596 template<
typename OrdinalType,
typename ScalarType>
606 template<
typename OrdinalType,
typename ScalarType>
608 OrdinalType numRows_in, OrdinalType numCols_in, OrdinalType kl_in, OrdinalType ku_in
612 numRows_ = numRows_in;
613 numCols_ = numCols_in;
617 values_ =
new ScalarType[stride_*numCols_];
619 valuesCopied_ =
true;
624 template<
typename OrdinalType,
typename ScalarType>
626 OrdinalType numRows_in, OrdinalType numCols_in, OrdinalType kl_in, OrdinalType ku_in
630 numRows_ = numRows_in;
631 numCols_ = numCols_in;
635 values_ =
new ScalarType[stride_*numCols_];
636 valuesCopied_ =
true;
641 template<
typename OrdinalType,
typename ScalarType>
643 OrdinalType numRows_in, OrdinalType numCols_in, OrdinalType kl_in, OrdinalType ku_in
648 ScalarType* values_tmp =
new ScalarType[(kl_in+ku_in+1) * numCols_in];
650 for(OrdinalType k = 0; k < (kl_in+ku_in+1) * numCols_in; k++) {
651 values_tmp[k] = zero;
653 OrdinalType numRows_tmp = TEUCHOS_MIN(numRows_, numRows_in);
654 OrdinalType numCols_tmp = TEUCHOS_MIN(numCols_, numCols_in);
656 copyMat(values_, stride_, numRows_tmp, numCols_tmp, values_tmp,
660 numRows_ = numRows_in;
661 numCols_ = numCols_in;
665 values_ = values_tmp;
666 valuesCopied_ =
true;
675 template<
typename OrdinalType,
typename ScalarType>
680 for(OrdinalType j = 0; j < numCols_; j++) {
681 for (OrdinalType i=TEUCHOS_MAX(0,j-ku_); i<=TEUCHOS_MIN(numRows_-1,j+kl_); i++) {
682 values_[(ku_+i-j) + j*stride_] = value_in;
689 template<
typename OrdinalType,
typename ScalarType>
694 for(OrdinalType j = 0; j < numCols_; j++) {
695 for (OrdinalType i=TEUCHOS_MAX(0,j-ku_); i<=TEUCHOS_MIN(numRows_-1,j+kl_); i++) {
703 template<
typename OrdinalType,
typename ScalarType>
712 if((!valuesCopied_) && (!Source.valuesCopied_) && (values_ == Source.values_))
716 if (!Source.valuesCopied_) {
721 numRows_ = Source.numRows_;
722 numCols_ = Source.numCols_;
725 stride_ = Source.stride_;
726 values_ = Source.values_;
730 numRows_ = Source.numRows_;
731 numCols_ = Source.numCols_;
735 const OrdinalType newsize = stride_ * numCols_;
737 values_ =
new ScalarType[newsize];
738 valuesCopied_ =
true;
744 if((Source.numRows_ <= stride_) && (Source.numCols_ == numCols_)) {
745 numRows_ = Source.numRows_;
746 numCols_ = Source.numCols_;
752 numRows_ = Source.numRows_;
753 numCols_ = Source.numCols_;
757 const OrdinalType newsize = stride_ * numCols_;
759 values_ =
new ScalarType[newsize];
760 valuesCopied_ =
true;
764 copyMat(Source.values_, Source.stride_, numRows_, numCols_, values_, stride_, 0);
770 template<
typename OrdinalType,
typename ScalarType>
775 if ((numRows_ != Source.numRows_) || (numCols_ != Source.numCols_) || (kl_ != Source.kl_) || (ku_ != Source.ku_)) {
776 TEUCHOS_CHK_REF(*
this);
783 template<
typename OrdinalType,
typename ScalarType>
788 if ((numRows_ != Source.numRows_) || (numCols_ != Source.numCols_) || (kl_ != Source.kl_) || (ku_ != Source.ku_)) {
789 TEUCHOS_CHK_REF(*
this);
796 template<
typename OrdinalType,
typename ScalarType>
801 if((!valuesCopied_) && (!Source.valuesCopied_) && (values_ == Source.values_))
805 if ((numRows_ != Source.numRows_) || (numCols_ != Source.numCols_) || (kl_ != Source.kl_) || (ku_ != Source.ku_)) {
806 TEUCHOS_CHK_REF(*
this);
808 copyMat(Source.values_, Source.stride_, numRows_, numCols_, values_, stride_, 0);
817 template<
typename OrdinalType,
typename ScalarType>
820 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
821 checkIndex( rowIndex, colIndex );
823 return(values_[colIndex * stride_ + ku_+rowIndex-colIndex]);
826 template<
typename OrdinalType,
typename ScalarType>
829 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
830 checkIndex( rowIndex, colIndex );
832 return(values_[colIndex * stride_ + ku_+rowIndex-colIndex]);
835 template<
typename OrdinalType,
typename ScalarType>
838 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
839 checkIndex( 0, colIndex );
841 return(values_ + colIndex * stride_);
844 template<
typename OrdinalType,
typename ScalarType>
847 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
848 checkIndex( 0, colIndex );
850 return(values_ + colIndex * stride_);
857 template<
typename OrdinalType,
typename ScalarType>
865 for(j = 0; j < numCols_; j++) {
867 ptr = values_ + j * stride_ + TEUCHOS_MAX(0, ku_-j);
868 for (i=TEUCHOS_MAX(0,j-ku_); i<=TEUCHOS_MIN(numRows_-1,j+kl_); i++) {
876 updateFlops((kl_+ku_+1) * numCols_);
881 template<
typename OrdinalType,
typename ScalarType>
887 for (i = 0; i < numRows_; i++) {
889 for (j=TEUCHOS_MAX(0,i-kl_); j<=TEUCHOS_MIN(numCols_-1,i+ku_); j++) {
892 anorm = TEUCHOS_MAX( anorm, sum );
894 updateFlops((kl_+ku_+1) * numCols_);
899 template<
typename OrdinalType,
typename ScalarType>
905 for (j = 0; j < numCols_; j++) {
906 for (i=TEUCHOS_MAX(0,j-ku_); i<=TEUCHOS_MIN(numRows_-1,j+kl_); i++) {
911 updateFlops((kl_+ku_+1) * numCols_);
920 template<
typename OrdinalType,
typename ScalarType>
925 if((numRows_ != Operand.numRows_) || (numCols_ != Operand.numCols_) || (kl_ != Operand.kl_) || (ku_ != Operand.ku_)) {
929 for(j = 0; j < numCols_; j++) {
930 for (i=TEUCHOS_MAX(0,j-ku_); i<=TEUCHOS_MIN(numRows_-1,j+kl_); i++) {
931 if((*
this)(i, j) != Operand(i, j)) {
941 template<
typename OrdinalType,
typename ScalarType>
944 return !((*this) == Operand);
951 template<
typename OrdinalType,
typename ScalarType>
954 this->scale( alpha );
958 template<
typename OrdinalType,
typename ScalarType>
965 for (j=0; j<numCols_; j++) {
966 ptr = values_ + j*stride_ + TEUCHOS_MAX(0, ku_-j);
967 for (i=TEUCHOS_MAX(0,j-ku_); i<=TEUCHOS_MIN(numRows_-1,j+kl_); i++) {
968 *ptr = alpha * (*ptr); ptr++;
971 updateFlops( (kl_+ku_+1)*numCols_ );
976 template<
typename OrdinalType,
typename ScalarType>
984 if ((numRows_ != A.numRows_) || (numCols_ != A.numCols_) || (kl_ != A.kl_) || (ku_ != A.ku_)) {
987 for (j=0; j<numCols_; j++) {
988 ptr = values_ + j*stride_ + TEUCHOS_MAX(0, ku_-j);
989 for (i=TEUCHOS_MAX(0,j-ku_); i<=TEUCHOS_MIN(numRows_-1,j+kl_); i++) {
990 *ptr = A(i,j) * (*ptr); ptr++;
993 updateFlops( (kl_+ku_+1)*numCols_ );
998 template<
typename OrdinalType,
typename ScalarType>
999 #ifndef TEUCHOS_HIDE_DEPRECATED_CODE
1007 os <<
"Values_copied : yes" << std::endl;
1009 os <<
"Values_copied : no" << std::endl;
1010 os <<
"Rows : " << numRows_ << std::endl;
1011 os <<
"Columns : " << numCols_ << std::endl;
1012 os <<
"Lower Bandwidth : " << kl_ << std::endl;
1013 os <<
"Upper Bandwidth : " << ku_ << std::endl;
1014 os <<
"LDA : " << stride_ << std::endl;
1015 if(numRows_ == 0 || numCols_ == 0) {
1016 os <<
"(matrix is empty, no values to display)" << std::endl;
1019 for(OrdinalType i = 0; i < numRows_; i++) {
1020 for (OrdinalType j=TEUCHOS_MAX(0,i-kl_); j<=TEUCHOS_MIN(numCols_-1,i+ku_); j++) {
1021 os << (*this)(i,j) <<
" ";
1026 #ifdef TEUCHOS_HIDE_DEPRECATED_CODE
1035 template<
typename OrdinalType,
typename ScalarType>
1039 rowIndex < TEUCHOS_MAX(0,colIndex-ku_) || rowIndex > TEUCHOS_MIN(numRows_-1,colIndex+kl_),
1041 "SerialBandDenseMatrix<T>::checkIndex: "
1042 "Row index " << rowIndex <<
" out of range [0, "<< numRows_ <<
")");
1044 "SerialBandDenseMatrix<T>::checkIndex: "
1045 "Col index " << colIndex <<
" out of range [0, "<< numCols_ <<
")");
1049 template<
typename OrdinalType,
typename ScalarType>
1050 void SerialBandDenseMatrix<OrdinalType, ScalarType>::deleteArrays(
void)
1052 if (valuesCopied_) {
1055 valuesCopied_ =
false;
1059 template<
typename OrdinalType,
typename ScalarType>
1060 void SerialBandDenseMatrix<OrdinalType, ScalarType>::copyMat(
1061 ScalarType* inputMatrix, OrdinalType strideInput, OrdinalType numRows_in,
1062 OrdinalType numCols_in, ScalarType* outputMatrix, OrdinalType strideOutput, OrdinalType startCol, ScalarType alpha
1066 ScalarType* ptr1 = 0;
1067 ScalarType* ptr2 = 0;
1069 for(j = 0; j < numCols_in; j++) {
1070 ptr1 = outputMatrix + (j * strideOutput) + TEUCHOS_MAX(0, ku_-j);
1071 ptr2 = inputMatrix + (j + startCol) * strideInput + TEUCHOS_MAX(0, ku_-j);
1073 for (i=TEUCHOS_MAX(0,j-ku_); i<=TEUCHOS_MIN(numRows_in-1,j+kl_); i++) {
1074 *ptr1++ += alpha*(*ptr2++);
1077 for (i=TEUCHOS_MAX(0,j-ku_); i<=TEUCHOS_MIN(numRows_in-1,j+kl_); i++) {
1084 #ifndef TEUCHOS_HIDE_DEPRECATED_CODE
1086 template<
typename OrdinalType,
typename ScalarType>
1095 template<
typename OrdinalType,
typename ScalarType>
1105 template<
typename OrdinalType,
typename ScalarType>
1107 operator<<(std::ostream &out,
1110 printer.obj.print(out);
1115 template<
typename OrdinalType,
typename ScalarType>
1116 SerialBandDenseMatrixPrinter<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.
Reference-counted pointer class and non-member templated function implementations.
Defines basic traits for the scalar field type.
Functionality and data that is common to all computational classes.
This class creates and provides basic support for banded dense matrices of templated type.
SerialBandDenseMatrix< OrdinalType, ScalarType > & operator+=(const SerialBandDenseMatrix< OrdinalType, ScalarType > &Source)
Add another matrix to this matrix.
virtual ~SerialBandDenseMatrix()
Destructor.
int shapeUninitialized(OrdinalType numRows, OrdinalType numCols, OrdinalType kl, OrdinalType ku)
Same as shape() except leaves uninitialized.
SerialBandDenseMatrix()
Default Constructor.
int random()
Set all values in the matrix to be random numbers.
OrdinalType numCols() const
Returns the column dimension of this matrix.
ScalarTraits< ScalarType >::magnitudeType normFrobenius() const
Returns the Frobenius-norm of the matrix.
ScalarType scalarType
Typedef for scalar type.
SerialBandDenseMatrix< OrdinalType, ScalarType > & operator=(const SerialBandDenseMatrix< OrdinalType, ScalarType > &Source)
Copies values from one matrix to another.
bool empty() const
Returns whether this matrix is empty.
OrdinalType ordinalType
Typedef for ordinal type.
OrdinalType numRows() const
Returns the row dimension of this matrix.
int scale(const ScalarType alpha)
Scale this matrix by alpha; *this = alpha**this.
SerialBandDenseMatrix< OrdinalType, ScalarType > & operator-=(const SerialBandDenseMatrix< OrdinalType, ScalarType > &Source)
Subtract another matrix from this matrix.
ScalarType * operator[](OrdinalType colIndex)
Column access method (non-const).
int putScalar(const ScalarType value=Teuchos::ScalarTraits< ScalarType >::zero())
Set all values in the matrix to a constant value.
SerialBandDenseMatrix< OrdinalType, ScalarType > & operator*=(const ScalarType alpha)
Scale this matrix by alpha; *this = alpha**this.
OrdinalType lowerBandwidth() const
Returns the lower bandwidth of this matrix.
bool operator==(const SerialBandDenseMatrix< OrdinalType, ScalarType > &Operand) const
Equality of two matrices.
int reshape(OrdinalType numRows, OrdinalType numCols, OrdinalType kl, OrdinalType ku)
Reshaping method for changing the size of a SerialBandDenseMatrix, keeping the entries.
OrdinalType stride() const
Returns the stride between the columns of this matrix in memory.
SerialBandDenseMatrix< OrdinalType, ScalarType > & assign(const SerialBandDenseMatrix< OrdinalType, ScalarType > &Source)
Copies values from one matrix to another.
ScalarType * values() const
Data array access method.
bool operator!=(const SerialBandDenseMatrix< OrdinalType, ScalarType > &Operand) const
Inequality of two matrices.
ScalarType & operator()(OrdinalType rowIndex, OrdinalType colIndex)
Element access method (non-const).
int shape(OrdinalType numRows, OrdinalType numCols, OrdinalType kl, OrdinalType ku)
Shape method for changing the size of a SerialBandDenseMatrix, initializing entries to zero.
virtual void print(std::ostream &os) const
Print method. Defines the behavior of the std::ostream << operator.
OrdinalType upperBandwidth() const
Returns the upper bandwidth of this matrix.
ScalarTraits< ScalarType >::magnitudeType normInf() const
Returns the Infinity-norm of the matrix.
ScalarTraits< ScalarType >::magnitudeType normOne() const
Returns the 1-norm of the matrix.
#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.
static T zero()
Returns representation of zero for this scalar type.
Ostream manipulator for SerialBandDenseMatrix.