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 virtual void print(std::ostream& os)
const;
429 void copyMat(ScalarType* inputMatrix, OrdinalType strideInput,
430 OrdinalType
numRows, OrdinalType
numCols, ScalarType* outputMatrix,
433 void checkIndex( OrdinalType rowIndex, OrdinalType colIndex = 0 )
const;
434 OrdinalType numRows_;
435 OrdinalType numCols_;
448 template<
typename OrdinalType,
typename ScalarType>
451 Object (
"Teuchos::SerialBandDenseMatrix"),
457 valuesCopied_ (false),
461 template<
typename OrdinalType,
typename ScalarType>
464 OrdinalType numCols_in,
469 Object (
"Teuchos::SerialBandDenseMatrix"),
470 numRows_ (numRows_in),
471 numCols_ (numCols_in),
472 stride_ (kl_in+ku_in+1),
475 valuesCopied_ (true),
478 values_ =
new ScalarType[stride_ * numCols_];
484 template<
typename OrdinalType,
typename ScalarType>
487 ScalarType* values_in,
488 OrdinalType stride_in,
489 OrdinalType numRows_in,
490 OrdinalType numCols_in,
494 Object (
"Teuchos::SerialBandDenseMatrix"),
495 numRows_ (numRows_in),
496 numCols_ (numCols_in),
500 valuesCopied_ (false),
505 values_ =
new ScalarType[stride_*numCols_];
506 copyMat (values_in, stride_in, numRows_, numCols_, values_, stride_, 0);
507 valuesCopied_ =
true;
511 template<
typename OrdinalType,
typename ScalarType>
515 Object (
"Teuchos::SerialBandDenseMatrix"),
521 valuesCopied_ (true),
525 numRows_ = Source.numRows_;
526 numCols_ = Source.numCols_;
530 values_ =
new ScalarType[stride_*numCols_];
531 copyMat (Source.values_, Source.stride_, numRows_, numCols_,
532 values_, stride_, 0);
535 numRows_ = Source.numCols_;
536 numCols_ = Source.numRows_;
540 values_ =
new ScalarType[stride_*numCols_];
541 for (OrdinalType j = 0; j < numCols_; ++j) {
542 for (OrdinalType i = TEUCHOS_MAX(0,j-ku_);
543 i <= TEUCHOS_MIN(numRows_-1,j+kl_); ++i) {
544 values_[j*stride_ + (ku_+i-j)] =
550 numRows_ = Source.numCols_;
551 numCols_ = Source.numRows_;
555 values_ =
new ScalarType[stride_*numCols_];
556 for (OrdinalType j=0; j<numCols_; j++) {
557 for (OrdinalType i = TEUCHOS_MAX(0,j-ku_);
558 i <= TEUCHOS_MIN(numRows_-1,j+kl_); ++i) {
559 values_[j*stride_ + (ku_+i-j)] = Source.values_[i*Source.stride_ + (Source.ku_+j-i)];
565 template<
typename OrdinalType,
typename ScalarType>
569 OrdinalType numRows_in,
570 OrdinalType numCols_in,
571 OrdinalType startCol)
573 Object (
"Teuchos::SerialBandDenseMatrix"),
574 numRows_ (numRows_in),
575 numCols_ (numCols_in),
576 stride_ (Source.stride_),
579 valuesCopied_ (false),
580 values_ (Source.values_)
583 values_ =
new ScalarType[stride_ * numCols_in];
584 copyMat (Source.values_, Source.stride_, numRows_in, numCols_in,
585 values_, stride_, startCol);
586 valuesCopied_ =
true;
588 values_ = values_ + (stride_ * startCol);
592 template<
typename OrdinalType,
typename ScalarType>
602 template<
typename OrdinalType,
typename ScalarType>
604 OrdinalType numRows_in, OrdinalType numCols_in, OrdinalType kl_in, OrdinalType ku_in
608 numRows_ = numRows_in;
609 numCols_ = numCols_in;
613 values_ =
new ScalarType[stride_*numCols_];
615 valuesCopied_ =
true;
620 template<
typename OrdinalType,
typename ScalarType>
622 OrdinalType numRows_in, OrdinalType numCols_in, OrdinalType kl_in, OrdinalType ku_in
626 numRows_ = numRows_in;
627 numCols_ = numCols_in;
631 values_ =
new ScalarType[stride_*numCols_];
632 valuesCopied_ =
true;
637 template<
typename OrdinalType,
typename ScalarType>
639 OrdinalType numRows_in, OrdinalType numCols_in, OrdinalType kl_in, OrdinalType ku_in
644 ScalarType* values_tmp =
new ScalarType[(kl_in+ku_in+1) * numCols_in];
646 for(OrdinalType k = 0; k < (kl_in+ku_in+1) * numCols_in; k++) {
647 values_tmp[k] = zero;
649 OrdinalType numRows_tmp = TEUCHOS_MIN(numRows_, numRows_in);
650 OrdinalType numCols_tmp = TEUCHOS_MIN(numCols_, numCols_in);
652 copyMat(values_, stride_, numRows_tmp, numCols_tmp, values_tmp,
656 numRows_ = numRows_in;
657 numCols_ = numCols_in;
661 values_ = values_tmp;
662 valuesCopied_ =
true;
671 template<
typename OrdinalType,
typename ScalarType>
676 for(OrdinalType j = 0; j < numCols_; j++) {
677 for (OrdinalType i=TEUCHOS_MAX(0,j-ku_); i<=TEUCHOS_MIN(numRows_-1,j+kl_); i++) {
678 values_[(ku_+i-j) + j*stride_] = value_in;
685 template<
typename OrdinalType,
typename ScalarType>
690 for(OrdinalType j = 0; j < numCols_; j++) {
691 for (OrdinalType i=TEUCHOS_MAX(0,j-ku_); i<=TEUCHOS_MIN(numRows_-1,j+kl_); i++) {
699 template<
typename OrdinalType,
typename ScalarType>
708 if((!valuesCopied_) && (!Source.valuesCopied_) && (values_ == Source.values_))
712 if (!Source.valuesCopied_) {
717 numRows_ = Source.numRows_;
718 numCols_ = Source.numCols_;
721 stride_ = Source.stride_;
722 values_ = Source.values_;
726 numRows_ = Source.numRows_;
727 numCols_ = Source.numCols_;
731 const OrdinalType newsize = stride_ * numCols_;
733 values_ =
new ScalarType[newsize];
734 valuesCopied_ =
true;
740 if((Source.numRows_ <= stride_) && (Source.numCols_ == numCols_)) {
741 numRows_ = Source.numRows_;
742 numCols_ = Source.numCols_;
748 numRows_ = Source.numRows_;
749 numCols_ = Source.numCols_;
753 const OrdinalType newsize = stride_ * numCols_;
755 values_ =
new ScalarType[newsize];
756 valuesCopied_ =
true;
760 copyMat(Source.values_, Source.stride_, numRows_, numCols_, values_, stride_, 0);
766 template<
typename OrdinalType,
typename ScalarType>
771 if ((numRows_ != Source.numRows_) || (numCols_ != Source.numCols_) || (kl_ != Source.kl_) || (ku_ != Source.ku_)) {
772 TEUCHOS_CHK_REF(*
this);
779 template<
typename OrdinalType,
typename ScalarType>
784 if ((numRows_ != Source.numRows_) || (numCols_ != Source.numCols_) || (kl_ != Source.kl_) || (ku_ != Source.ku_)) {
785 TEUCHOS_CHK_REF(*
this);
792 template<
typename OrdinalType,
typename ScalarType>
797 if((!valuesCopied_) && (!Source.valuesCopied_) && (values_ == Source.values_))
801 if ((numRows_ != Source.numRows_) || (numCols_ != Source.numCols_) || (kl_ != Source.kl_) || (ku_ != Source.ku_)) {
802 TEUCHOS_CHK_REF(*
this);
804 copyMat(Source.values_, Source.stride_, numRows_, numCols_, values_, stride_, 0);
813 template<
typename OrdinalType,
typename ScalarType>
816 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK 817 checkIndex( rowIndex, colIndex );
819 return(values_[colIndex * stride_ + ku_+rowIndex-colIndex]);
822 template<
typename OrdinalType,
typename ScalarType>
825 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK 826 checkIndex( rowIndex, colIndex );
828 return(values_[colIndex * stride_ + ku_+rowIndex-colIndex]);
831 template<
typename OrdinalType,
typename ScalarType>
834 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK 835 checkIndex( 0, colIndex );
837 return(values_ + colIndex * stride_);
840 template<
typename OrdinalType,
typename ScalarType>
843 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK 844 checkIndex( 0, colIndex );
846 return(values_ + colIndex * stride_);
853 template<
typename OrdinalType,
typename ScalarType>
861 for(j = 0; j < numCols_; j++) {
863 ptr = values_ + j * stride_ + TEUCHOS_MAX(0, ku_-j);
864 for (i=TEUCHOS_MAX(0,j-ku_); i<=TEUCHOS_MIN(numRows_-1,j+kl_); i++) {
877 template<
typename OrdinalType,
typename ScalarType>
883 for (i = 0; i < numRows_; i++) {
885 for (j=TEUCHOS_MAX(0,i-kl_); j<=TEUCHOS_MIN(numCols_-1,i+ku_); j++) {
888 anorm = TEUCHOS_MAX( anorm, sum );
895 template<
typename OrdinalType,
typename ScalarType>
901 for (j = 0; j < numCols_; j++) {
902 for (i=TEUCHOS_MAX(0,j-ku_); i<=TEUCHOS_MIN(numRows_-1,j+kl_); i++) {
916 template<
typename OrdinalType,
typename ScalarType>
921 if((numRows_ != Operand.numRows_) || (numCols_ != Operand.numCols_) || (kl_ != Operand.kl_) || (ku_ != Operand.ku_)) {
925 for(j = 0; j < numCols_; j++) {
926 for (i=TEUCHOS_MAX(0,j-ku_); i<=TEUCHOS_MIN(numRows_-1,j+kl_); i++) {
927 if((*
this)(i, j) != Operand(i, j)) {
937 template<
typename OrdinalType,
typename ScalarType>
940 return !((*this) == Operand);
947 template<
typename OrdinalType,
typename ScalarType>
950 this->
scale( alpha );
954 template<
typename OrdinalType,
typename ScalarType>
961 for (j=0; j<numCols_; j++) {
962 ptr = values_ + j*stride_ + TEUCHOS_MAX(0, ku_-j);
963 for (i=TEUCHOS_MAX(0,j-ku_); i<=TEUCHOS_MIN(numRows_-1,j+kl_); i++) {
964 *ptr = alpha * (*ptr); ptr++;
972 template<
typename OrdinalType,
typename ScalarType>
980 if ((numRows_ != A.numRows_) || (numCols_ != A.numCols_) || (kl_ != A.kl_) || (ku_ != A.ku_)) {
983 for (j=0; j<numCols_; j++) {
984 ptr = values_ + j*stride_ + TEUCHOS_MAX(0, ku_-j);
985 for (i=TEUCHOS_MAX(0,j-ku_); i<=TEUCHOS_MIN(numRows_-1,j+kl_); i++) {
986 *ptr = A(i,j) * (*ptr); ptr++;
994 template<
typename OrdinalType,
typename ScalarType>
999 os <<
"Values_copied : yes" << std::endl;
1001 os <<
"Values_copied : no" << std::endl;
1002 os <<
"Rows : " << numRows_ << std::endl;
1003 os <<
"Columns : " << numCols_ << std::endl;
1004 os <<
"Lower Bandwidth : " << kl_ << std::endl;
1005 os <<
"Upper Bandwidth : " << ku_ << std::endl;
1006 os <<
"LDA : " << stride_ << std::endl;
1007 if(numRows_ == 0 || numCols_ == 0) {
1008 os <<
"(matrix is empty, no values to display)" << std::endl;
1011 for(OrdinalType i = 0; i < numRows_; i++) {
1012 for (OrdinalType j=TEUCHOS_MAX(0,i-kl_); j<=TEUCHOS_MIN(numCols_-1,i+ku_); j++) {
1013 os << (*this)(i,j) <<
" ";
1024 template<
typename OrdinalType,
typename ScalarType>
1028 rowIndex < TEUCHOS_MAX(0,colIndex-ku_) || rowIndex > TEUCHOS_MIN(numRows_-1,colIndex+kl_),
1030 "SerialBandDenseMatrix<T>::checkIndex: " 1031 "Row index " << rowIndex <<
" out of range [0, "<< numRows_ <<
")");
1033 "SerialBandDenseMatrix<T>::checkIndex: " 1034 "Col index " << colIndex <<
" out of range [0, "<< numCols_ <<
")");
1038 template<
typename OrdinalType,
typename ScalarType>
1041 if (valuesCopied_) {
1044 valuesCopied_ =
false;
1048 template<
typename OrdinalType,
typename ScalarType>
1050 ScalarType* inputMatrix, OrdinalType strideInput, OrdinalType numRows_in,
1051 OrdinalType numCols_in, ScalarType* outputMatrix, OrdinalType strideOutput, OrdinalType startCol, ScalarType alpha
1055 ScalarType* ptr1 = 0;
1056 ScalarType* ptr2 = 0;
1058 for(j = 0; j < numCols_in; j++) {
1059 ptr1 = outputMatrix + (j * strideOutput) + TEUCHOS_MAX(0, ku_-j);
1060 ptr2 = inputMatrix + (j + startCol) * strideInput + TEUCHOS_MAX(0, ku_-j);
1062 for (i=TEUCHOS_MAX(0,j-ku_); i<=TEUCHOS_MIN(numRows_in-1,j+kl_); i++) {
1063 *ptr1++ += alpha*(*ptr2++);
1066 for (i=TEUCHOS_MAX(0,j-ku_); i<=TEUCHOS_MIN(numRows_in-1,j+kl_); i++) {
bool empty() const
Returns whether this matrix is empty.
OrdinalType numRows() const
Returns the row dimension of this matrix.
SerialBandDenseMatrix< OrdinalType, ScalarType > & operator+=(const SerialBandDenseMatrix< OrdinalType, ScalarType > &Source)
Add another matrix to this matrix.
ScalarType & operator()(OrdinalType rowIndex, OrdinalType colIndex)
Element access method (non-const).
int scale(const ScalarType alpha)
Scale this matrix by alpha; *this = alpha**this.
int shape(OrdinalType numRows, OrdinalType numCols, OrdinalType kl, OrdinalType ku)
Shape method for changing the size of a SerialBandDenseMatrix, initializing entries to zero...
ScalarTraits< ScalarType >::magnitudeType normInf() const
Returns the Infinity-norm of the matrix.
Templated interface class to BLAS routines.
virtual ~SerialBandDenseMatrix()
Destructor.
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
Macro for throwing an exception with breakpointing to ease debugging.
int reshape(OrdinalType numRows, OrdinalType numCols, OrdinalType kl, OrdinalType ku)
Reshaping method for changing the size of a SerialBandDenseMatrix, keeping the entries.
Teuchos header file which uses auto-configuration information to include necessary C++ headers...
ScalarTraits< ScalarType >::magnitudeType normOne() const
Returns the 1-norm of the matrix.
SerialBandDenseMatrix< OrdinalType, ScalarType > & assign(const SerialBandDenseMatrix< OrdinalType, ScalarType > &Source)
Copies values from one matrix to another.
Object for storing data and providing functionality that is common to all computational classes...
This structure defines some basic traits for a scalar field type.
OrdinalType stride() const
Returns the stride between the columns of this matrix in memory.
virtual void print(std::ostream &os) const
Print method. Defines the behavior of the std::ostream << operator inherited from the Object class...
ScalarType * values() const
Data array access method.
Functionality and data that is common to all computational classes.
bool operator!=(const SerialBandDenseMatrix< OrdinalType, ScalarType > &Operand) const
Inequality of two matrices.
ScalarTraits< ScalarType >::magnitudeType normFrobenius() const
Returns the Frobenius-norm of the matrix.
This class creates and provides basic support for banded dense matrices of templated type...
SerialBandDenseMatrix< OrdinalType, ScalarType > & operator*=(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.
int putScalar(const ScalarType value=Teuchos::ScalarTraits< ScalarType >::zero())
Set all values in the matrix to a constant value.
int random()
Set all values in the matrix to be random numbers.
SerialBandDenseMatrix()
Default Constructor.
bool operator==(const SerialBandDenseMatrix< OrdinalType, ScalarType > &Operand) const
Equality of two matrices.
static magnitudeType magnitude(T a)
Returns the magnitudeType of the scalar type a.
OrdinalType ordinalType
Typedef for ordinal type.
SerialBandDenseMatrix< OrdinalType, ScalarType > & operator=(const SerialBandDenseMatrix< OrdinalType, ScalarType > &Source)
Copies values from one matrix to another.
OrdinalType upperBandwidth() const
Returns the upper bandwidth of this matrix.
The Teuchos namespace contains all of the classes, structs and enums used by Teuchos, as well as a number of utility routines.
void updateFlops(int addflops) const
Increment Flop count for this object.
Defines basic traits for the scalar field type.
static T zero()
Returns representation of zero for this scalar type.
OrdinalType lowerBandwidth() const
Returns the lower bandwidth of this matrix.
ScalarType * operator[](OrdinalType colIndex)
Column access method (non-const).
ScalarType scalarType
Typedef for scalar type.
OrdinalType numCols() const
Returns the column dimension of this matrix.
Reference-counted pointer class and non-member templated function implementations.
int shapeUninitialized(OrdinalType numRows, OrdinalType numCols, OrdinalType kl, OrdinalType ku)
Same as shape() except leaves uninitialized.
Teuchos::DataAccess Mode enumerable type.