43 #ifndef _TEUCHOS_SERIALSYMDENSEMATRIX_HPP_
44 #define _TEUCHOS_SERIALSYMDENSEMATRIX_HPP_
54 #include "Teuchos_Assert.hpp"
121 template<
typename OrdinalType,
typename ScalarType>
203 int shape(OrdinalType numRowsCols);
228 int reshape(OrdinalType numRowsCols);
293 ScalarType&
operator () (OrdinalType rowIndex, OrdinalType colIndex);
303 const ScalarType&
operator () (OrdinalType rowIndex, OrdinalType colIndex)
const;
308 ScalarType*
values()
const {
return(values_); }
316 bool upper()
const {
return(upper_);};
319 char UPLO()
const {
return(UPLO_);};
369 OrdinalType
numRows()
const {
return(numRowCols_); }
372 OrdinalType
numCols()
const {
return(numRowCols_); }
375 OrdinalType
stride()
const {
return(stride_); }
378 bool empty()
const {
return(numRowCols_ == 0); }
397 virtual void print(std::ostream& os)
const;
405 void scale(
const ScalarType alpha );
408 void copyMat(
bool inputUpper, ScalarType* inputMatrix, OrdinalType inputStride,
409 OrdinalType numRowCols,
bool outputUpper, ScalarType* outputMatrix,
410 OrdinalType outputStride, OrdinalType startRowCol,
414 void copyUPLOMat(
bool inputUpper, ScalarType* inputMatrix,
415 OrdinalType inputStride, OrdinalType inputRows);
418 void checkIndex( OrdinalType rowIndex, OrdinalType colIndex = 0 )
const;
419 OrdinalType numRowCols_;
433 template<
typename OrdinalType,
typename ScalarType>
436 numRowCols_(0), stride_(0), valuesCopied_(false), values_(0), upper_(false), UPLO_(
'L')
439 template<
typename OrdinalType,
typename ScalarType>
442 numRowCols_(numRowCols_in), stride_(numRowCols_in), valuesCopied_(false), upper_(false), UPLO_(
'L')
444 values_ =
new ScalarType[stride_*numRowCols_];
445 valuesCopied_ =
true;
450 template<
typename OrdinalType,
typename ScalarType>
452 DataAccess CV,
bool upper_in, ScalarType* values_in, OrdinalType stride_in, OrdinalType numRowCols_in
455 numRowCols_(numRowCols_in), stride_(stride_in), valuesCopied_(false),
456 values_(values_in), upper_(upper_in)
465 stride_ = numRowCols_;
466 values_ =
new ScalarType[stride_*numRowCols_];
467 copyMat(upper_in, values_in, stride_in, numRowCols_, upper_, values_, stride_, 0);
468 valuesCopied_ =
true;
472 template<
typename OrdinalType,
typename ScalarType>
475 numRowCols_(Source.numRowCols_), stride_(0), valuesCopied_(true),
476 values_(0), upper_(Source.upper_), UPLO_(Source.UPLO_)
478 if (!Source.valuesCopied_)
480 stride_ = Source.stride_;
481 values_ = Source.values_;
482 valuesCopied_ =
false;
486 stride_ = numRowCols_;
487 const OrdinalType newsize = stride_ * numRowCols_;
489 values_ =
new ScalarType[newsize];
490 copyMat(Source.upper_, Source.values_, Source.stride_, numRowCols_, upper_, values_, stride_, 0);
493 numRowCols_ = 0; stride_ = 0;
494 valuesCopied_ =
false;
499 template<
typename OrdinalType,
typename ScalarType>
502 ScalarType> &Source, OrdinalType numRowCols_in, OrdinalType startRowCol )
504 numRowCols_(numRowCols_in), stride_(Source.stride_), valuesCopied_(false), upper_(Source.upper_), UPLO_(Source.UPLO_)
508 stride_ = numRowCols_in;
509 values_ =
new ScalarType[stride_ * numRowCols_in];
510 copyMat(Source.upper_, Source.values_, Source.stride_, numRowCols_in, upper_, values_, stride_, startRowCol);
511 valuesCopied_ =
true;
515 values_ = Source.values_ + (stride_ * startRowCol) + startRowCol;
519 template<
typename OrdinalType,
typename ScalarType>
529 template<
typename OrdinalType,
typename ScalarType>
533 numRowCols_ = numRowCols_in;
534 stride_ = numRowCols_;
535 values_ =
new ScalarType[stride_*numRowCols_];
537 valuesCopied_ =
true;
541 template<
typename OrdinalType,
typename ScalarType>
545 numRowCols_ = numRowCols_in;
546 stride_ = numRowCols_;
547 values_ =
new ScalarType[stride_*numRowCols_];
548 valuesCopied_ =
true;
552 template<
typename OrdinalType,
typename ScalarType>
556 ScalarType* values_tmp =
new ScalarType[numRowCols_in * numRowCols_in];
558 for(OrdinalType k = 0; k < numRowCols_in * numRowCols_in; k++)
560 values_tmp[k] = zero;
562 OrdinalType numRowCols_tmp = TEUCHOS_MIN(numRowCols_, numRowCols_in);
565 copyMat(upper_, values_, stride_, numRowCols_tmp, upper_, values_tmp, numRowCols_in, 0);
568 numRowCols_ = numRowCols_in;
569 stride_ = numRowCols_;
570 values_ = values_tmp;
571 valuesCopied_ =
true;
579 template<
typename OrdinalType,
typename ScalarType>
583 if (upper_ !=
false) {
584 copyUPLOMat(
true, values_, stride_, numRowCols_ );
590 template<
typename OrdinalType,
typename ScalarType>
594 if (upper_ ==
false) {
595 copyUPLOMat(
false, values_, stride_, numRowCols_ );
601 template<
typename OrdinalType,
typename ScalarType>
605 if (fullMatrix ==
true) {
606 for(OrdinalType j = 0; j < numRowCols_; j++)
608 for(OrdinalType i = 0; i < numRowCols_; i++)
610 values_[i + j*stride_] = value_in;
617 for(OrdinalType j = 0; j < numRowCols_; j++) {
618 for(OrdinalType i = 0; i <= j; i++) {
619 values_[i + j*stride_] = value_in;
624 for(OrdinalType j = 0; j < numRowCols_; j++) {
625 for(OrdinalType i = j; i < numRowCols_; i++) {
626 values_[i + j*stride_] = value_in;
634 template<
typename OrdinalType,
typename ScalarType>
642 for(OrdinalType j = 0; j < numRowCols_; j++) {
643 for(OrdinalType i = 0; i < j; i++) {
651 for(OrdinalType j = 0; j < numRowCols_; j++) {
652 for(OrdinalType i = j+1; i < numRowCols_; i++) {
661 for(OrdinalType i = 0; i < numRowCols_; i++) {
662 values_[i + i*stride_] = diagSum[i] + bias;
667 template<
typename OrdinalType,
typename ScalarType>
673 if((!valuesCopied_) && (!Source.valuesCopied_) && (values_ == Source.values_)) {
674 upper_ = Source.upper_;
679 if (!Source.valuesCopied_) {
684 numRowCols_ = Source.numRowCols_;
685 stride_ = Source.stride_;
686 values_ = Source.values_;
687 upper_ = Source.upper_;
688 UPLO_ = Source.UPLO_;
693 numRowCols_ = Source.numRowCols_;
694 stride_ = Source.numRowCols_;
695 upper_ = Source.upper_;
696 UPLO_ = Source.UPLO_;
697 const OrdinalType newsize = stride_ * numRowCols_;
699 values_ =
new ScalarType[newsize];
700 valuesCopied_ =
true;
708 if((Source.numRowCols_ <= stride_) && (Source.numRowCols_ == numRowCols_)) {
709 numRowCols_ = Source.numRowCols_;
710 upper_ = Source.upper_;
711 UPLO_ = Source.UPLO_;
715 numRowCols_ = Source.numRowCols_;
716 stride_ = Source.numRowCols_;
717 upper_ = Source.upper_;
718 UPLO_ = Source.UPLO_;
719 const OrdinalType newsize = stride_ * numRowCols_;
721 values_ =
new ScalarType[newsize];
722 valuesCopied_ =
true;
726 copyMat(Source.upper_, Source.values_, Source.stride_, Source.numRowCols_, upper_, values_, stride_, 0);
731 template<
typename OrdinalType,
typename ScalarType>
738 template<
typename OrdinalType,
typename ScalarType>
742 if ((numRowCols_ != Source.numRowCols_))
744 TEUCHOS_CHK_REF(*
this);
750 template<
typename OrdinalType,
typename ScalarType>
754 if ((numRowCols_ != Source.numRowCols_))
756 TEUCHOS_CHK_REF(*
this);
762 template<
typename OrdinalType,
typename ScalarType>
766 if((!valuesCopied_) && (!Source.valuesCopied_) && (values_ == Source.values_)) {
767 upper_ = Source.upper_;
772 if ((numRowCols_ != Source.numRowCols_))
774 TEUCHOS_CHK_REF(*
this);
776 copyMat(Source.upper_, Source.values_, Source.stride_, numRowCols_, upper_, values_, stride_, 0 );
784 template<
typename OrdinalType,
typename ScalarType>
787 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
788 checkIndex( rowIndex, colIndex );
790 if ( rowIndex <= colIndex ) {
793 return(values_[colIndex * stride_ + rowIndex]);
795 return(values_[rowIndex * stride_ + colIndex]);
800 return(values_[rowIndex * stride_ + colIndex]);
802 return(values_[colIndex * stride_ + rowIndex]);
806 template<
typename OrdinalType,
typename ScalarType>
809 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
810 checkIndex( rowIndex, colIndex );
812 if ( rowIndex <= colIndex ) {
815 return(values_[colIndex * stride_ + rowIndex]);
817 return(values_[rowIndex * stride_ + colIndex]);
822 return(values_[rowIndex * stride_ + colIndex]);
824 return(values_[colIndex * stride_ + rowIndex]);
832 template<
typename OrdinalType,
typename ScalarType>
838 template<
typename OrdinalType,
typename ScalarType>
849 for (j=0; j<numRowCols_; j++) {
851 ptr = values_ + j*stride_;
852 for (i=0; i<j; i++) {
855 ptr = values_ + j + j*stride_;
856 for (i=j; i<numRowCols_; i++) {
860 anorm = TEUCHOS_MAX( anorm, sum );
864 for (j=0; j<numRowCols_; j++) {
866 ptr = values_ + j + j*stride_;
867 for (i=j; i<numRowCols_; i++) {
871 for (i=0; i<j; i++) {
875 anorm = TEUCHOS_MAX( anorm, sum );
881 template<
typename OrdinalType,
typename ScalarType>
891 for (j = 0; j < numRowCols_; j++) {
892 for (i = 0; i < j; i++) {
899 for (j = 0; j < numRowCols_; j++) {
901 for (i = j+1; i < numRowCols_; i++) {
914 template<
typename OrdinalType,
typename ScalarType>
918 if((numRowCols_ != Operand.numRowCols_)) {
923 for(i = 0; i < numRowCols_; i++) {
924 for(j = 0; j < numRowCols_; j++) {
925 if((*
this)(i, j) != Operand(i, j)) {
934 template<
typename OrdinalType,
typename ScalarType>
937 return !((*this) == Operand);
944 template<
typename OrdinalType,
typename ScalarType>
951 for (j=0; j<numRowCols_; j++) {
952 ptr = values_ + j*stride_;
953 for (i=0; i<= j; i++) { *ptr = alpha * (*ptr); ptr++; }
957 for (j=0; j<numRowCols_; j++) {
958 ptr = values_ + j*stride_ + j;
959 for (i=j; i<numRowCols_; i++) { *ptr = alpha * (*ptr); ptr++; }
993 template<
typename OrdinalType,
typename ScalarType>
998 os <<
"Values_copied : yes" << std::endl;
1000 os <<
"Values_copied : no" << std::endl;
1001 os <<
"Rows / Columns : " << numRowCols_ << std::endl;
1002 os <<
"LDA : " << stride_ << std::endl;
1004 os <<
"Storage: Upper" << std::endl;
1006 os <<
"Storage: Lower" << std::endl;
1007 if(numRowCols_ == 0) {
1008 os <<
"(matrix is empty, no values to display)" << std::endl;
1010 for(OrdinalType i = 0; i < numRowCols_; i++) {
1011 for(OrdinalType j = 0; j < numRowCols_; j++){
1012 os << (*this)(i,j) <<
" ";
1023 template<
typename OrdinalType,
typename ScalarType>
1026 "SerialSymDenseMatrix<T>::checkIndex: "
1027 "Row index " << rowIndex <<
" out of range [0, "<< numRowCols_ <<
")");
1029 "SerialSymDenseMatrix<T>::checkIndex: "
1030 "Col index " << colIndex <<
" out of range [0, "<< numRowCols_ <<
")");
1033 template<
typename OrdinalType,
typename ScalarType>
1034 void SerialSymDenseMatrix<OrdinalType, ScalarType>::deleteArrays(
void)
1040 valuesCopied_ =
false;
1044 template<
typename OrdinalType,
typename ScalarType>
1045 void SerialSymDenseMatrix<OrdinalType, ScalarType>::copyMat(
1046 bool inputUpper, ScalarType* inputMatrix,
1047 OrdinalType inputStride, OrdinalType numRowCols_in,
1048 bool outputUpper, ScalarType* outputMatrix,
1049 OrdinalType outputStride, OrdinalType startRowCol,
1054 ScalarType* ptr1 = 0;
1055 ScalarType* ptr2 = 0;
1057 for (j = 0; j < numRowCols_in; j++) {
1058 if (inputUpper ==
true) {
1060 ptr2 = inputMatrix + (j + startRowCol) * inputStride + startRowCol;
1061 if (outputUpper ==
true) {
1063 ptr1 = outputMatrix + j*outputStride;
1065 for(i = 0; i <= j; i++) {
1066 *ptr1++ += alpha*(*ptr2++);
1069 for(i = 0; i <= j; i++) {
1077 ptr1 = outputMatrix + j;
1079 for(i = 0; i <= j; i++) {
1080 *ptr1 += alpha*(*ptr2++);
1081 ptr1 += outputStride;
1084 for(i = 0; i <= j; i++) {
1086 ptr1 += outputStride;
1093 ptr2 = inputMatrix + (startRowCol+j) * inputStride + startRowCol + j;
1094 if (outputUpper ==
true) {
1097 ptr1 = outputMatrix + j*outputStride + j;
1099 for(i = j; i < numRowCols_in; i++) {
1100 *ptr1 += alpha*(*ptr2++);
1101 ptr1 += outputStride;
1104 for(i = j; i < numRowCols_in; i++) {
1106 ptr1 += outputStride;
1112 ptr1 = outputMatrix + j*outputStride + j;
1114 for(i = j; i < numRowCols_in; i++) {
1115 *ptr1++ += alpha*(*ptr2++);
1118 for(i = j; i < numRowCols_in; i++) {
1127 template<
typename OrdinalType,
typename ScalarType>
1128 void SerialSymDenseMatrix<OrdinalType, ScalarType>::copyUPLOMat(
1129 bool inputUpper, ScalarType* inputMatrix,
1130 OrdinalType inputStride, OrdinalType inputRows
1134 ScalarType * ptr1 = 0;
1135 ScalarType * ptr2 = 0;
1138 for (j=1; j<inputRows; j++) {
1139 ptr1 = inputMatrix + j;
1140 ptr2 = inputMatrix + j*inputStride;
1141 for (i=0; i<j; i++) {
1148 for (i=1; i<inputRows; i++) {
1149 ptr1 = inputMatrix + i;
1150 ptr2 = inputMatrix + i*inputStride;
1151 for (j=0; j<i; j++) {