43 #ifndef _TEUCHOS_SERIALSYMDENSEMATRIX_HPP_
44 #define _TEUCHOS_SERIALSYMDENSEMATRIX_HPP_
54 #include "Teuchos_Assert.hpp"
122 template<
typename OrdinalType,
typename ScalarType>
204 int shape(OrdinalType numRowsCols);
229 int reshape(OrdinalType numRowsCols);
300 ScalarType&
operator () (OrdinalType rowIndex, OrdinalType colIndex);
310 const ScalarType&
operator () (OrdinalType rowIndex, OrdinalType colIndex)
const;
315 ScalarType*
values()
const {
return(values_); }
323 bool upper()
const {
return(upper_);};
326 char UPLO()
const {
return(UPLO_);};
376 OrdinalType
numRows()
const {
return(numRowCols_); }
379 OrdinalType
numCols()
const {
return(numRowCols_); }
382 OrdinalType
stride()
const {
return(stride_); }
385 bool empty()
const {
return(numRowCols_ == 0); }
404 #ifndef TEUCHOS_HIDE_DEPRECATED_CODE
406 virtual void print(std::ostream& os)
const;
408 virtual std::ostream&
print(std::ostream& os)
const;
416 void scale(
const ScalarType alpha );
419 void copyMat(
bool inputUpper, ScalarType* inputMatrix, OrdinalType inputStride,
420 OrdinalType numRowCols,
bool outputUpper, ScalarType* outputMatrix,
421 OrdinalType outputStride, OrdinalType startRowCol,
425 void copyUPLOMat(
bool inputUpper, ScalarType* inputMatrix,
426 OrdinalType inputStride, OrdinalType inputRows);
429 void checkIndex( OrdinalType rowIndex, OrdinalType colIndex = 0 )
const;
430 OrdinalType numRowCols_;
444 template<
typename OrdinalType,
typename ScalarType>
447 numRowCols_(0), stride_(0), valuesCopied_(false), values_(0), upper_(false), UPLO_(
'L')
450 template<
typename OrdinalType,
typename ScalarType>
453 numRowCols_(numRowCols_in), stride_(numRowCols_in), valuesCopied_(false), upper_(false), UPLO_(
'L')
455 values_ =
new ScalarType[stride_*numRowCols_];
456 valuesCopied_ =
true;
461 template<
typename OrdinalType,
typename ScalarType>
463 DataAccess CV,
bool upper_in, ScalarType* values_in, OrdinalType stride_in, OrdinalType numRowCols_in
466 numRowCols_(numRowCols_in), stride_(stride_in), valuesCopied_(false),
467 values_(values_in), upper_(upper_in)
476 stride_ = numRowCols_;
477 values_ =
new ScalarType[stride_*numRowCols_];
478 copyMat(upper_in, values_in, stride_in, numRowCols_, upper_, values_, stride_, 0);
479 valuesCopied_ =
true;
483 template<
typename OrdinalType,
typename ScalarType>
486 numRowCols_(Source.numRowCols_), stride_(0), valuesCopied_(true),
487 values_(0), upper_(Source.upper_), UPLO_(Source.UPLO_)
489 if (!Source.valuesCopied_)
491 stride_ = Source.stride_;
492 values_ = Source.values_;
493 valuesCopied_ =
false;
497 stride_ = numRowCols_;
498 const OrdinalType newsize = stride_ * numRowCols_;
500 values_ =
new ScalarType[newsize];
501 copyMat(Source.upper_, Source.values_, Source.stride_, numRowCols_, upper_, values_, stride_, 0);
504 numRowCols_ = 0; stride_ = 0;
505 valuesCopied_ =
false;
510 template<
typename OrdinalType,
typename ScalarType>
513 ScalarType> &Source, OrdinalType numRowCols_in, OrdinalType startRowCol )
515 numRowCols_(numRowCols_in), stride_(Source.stride_), valuesCopied_(false), upper_(Source.upper_), UPLO_(Source.UPLO_)
519 stride_ = numRowCols_in;
520 values_ =
new ScalarType[stride_ * numRowCols_in];
521 copyMat(Source.upper_, Source.values_, Source.stride_, numRowCols_in, upper_, values_, stride_, startRowCol);
522 valuesCopied_ =
true;
526 values_ = Source.values_ + (stride_ * startRowCol) + startRowCol;
530 template<
typename OrdinalType,
typename ScalarType>
540 template<
typename OrdinalType,
typename ScalarType>
544 numRowCols_ = numRowCols_in;
545 stride_ = numRowCols_;
546 values_ =
new ScalarType[stride_*numRowCols_];
548 valuesCopied_ =
true;
552 template<
typename OrdinalType,
typename ScalarType>
556 numRowCols_ = numRowCols_in;
557 stride_ = numRowCols_;
558 values_ =
new ScalarType[stride_*numRowCols_];
559 valuesCopied_ =
true;
563 template<
typename OrdinalType,
typename ScalarType>
567 ScalarType* values_tmp =
new ScalarType[numRowCols_in * numRowCols_in];
569 for(OrdinalType k = 0; k < numRowCols_in * numRowCols_in; k++)
571 values_tmp[k] = zero;
573 OrdinalType numRowCols_tmp = TEUCHOS_MIN(numRowCols_, numRowCols_in);
576 copyMat(upper_, values_, stride_, numRowCols_tmp, upper_, values_tmp, numRowCols_in, 0);
579 numRowCols_ = numRowCols_in;
580 stride_ = numRowCols_;
581 values_ = values_tmp;
582 valuesCopied_ =
true;
590 template<
typename OrdinalType,
typename ScalarType>
594 if (upper_ !=
false) {
595 copyUPLOMat(
true, values_, stride_, numRowCols_ );
601 template<
typename OrdinalType,
typename ScalarType>
605 if (upper_ ==
false) {
606 copyUPLOMat(
false, values_, stride_, numRowCols_ );
612 template<
typename OrdinalType,
typename ScalarType>
616 if (fullMatrix ==
true) {
617 for(OrdinalType j = 0; j < numRowCols_; j++)
619 for(OrdinalType i = 0; i < numRowCols_; i++)
621 values_[i + j*stride_] = value_in;
628 for(OrdinalType j = 0; j < numRowCols_; j++) {
629 for(OrdinalType i = 0; i <= j; i++) {
630 values_[i + j*stride_] = value_in;
635 for(OrdinalType j = 0; j < numRowCols_; j++) {
636 for(OrdinalType i = j; i < numRowCols_; i++) {
637 values_[i + j*stride_] = value_in;
645 template<
typename OrdinalType,
typename ScalarType>
void
649 std::swap(values_ , B.values_);
650 std::swap(numRowCols_, B.numRowCols_);
651 std::swap(stride_, B.stride_);
652 std::swap(valuesCopied_, B.valuesCopied_);
653 std::swap(upper_, B.upper_);
654 std::swap(UPLO_, B.UPLO_);
657 template<
typename OrdinalType,
typename ScalarType>
665 for(OrdinalType j = 0; j < numRowCols_; j++) {
666 for(OrdinalType i = 0; i < j; i++) {
674 for(OrdinalType j = 0; j < numRowCols_; j++) {
675 for(OrdinalType i = j+1; i < numRowCols_; i++) {
684 for(OrdinalType i = 0; i < numRowCols_; i++) {
685 values_[i + i*stride_] = diagSum[i] + bias;
690 template<
typename OrdinalType,
typename ScalarType>
696 if((!valuesCopied_) && (!Source.valuesCopied_) && (values_ == Source.values_)) {
697 upper_ = Source.upper_;
702 if (!Source.valuesCopied_) {
707 numRowCols_ = Source.numRowCols_;
708 stride_ = Source.stride_;
709 values_ = Source.values_;
710 upper_ = Source.upper_;
711 UPLO_ = Source.UPLO_;
716 numRowCols_ = Source.numRowCols_;
717 stride_ = Source.numRowCols_;
718 upper_ = Source.upper_;
719 UPLO_ = Source.UPLO_;
720 const OrdinalType newsize = stride_ * numRowCols_;
722 values_ =
new ScalarType[newsize];
723 valuesCopied_ =
true;
731 if((Source.numRowCols_ <= stride_) && (Source.numRowCols_ == numRowCols_)) {
732 numRowCols_ = Source.numRowCols_;
733 upper_ = Source.upper_;
734 UPLO_ = Source.UPLO_;
738 numRowCols_ = Source.numRowCols_;
739 stride_ = Source.numRowCols_;
740 upper_ = Source.upper_;
741 UPLO_ = Source.UPLO_;
742 const OrdinalType newsize = stride_ * numRowCols_;
744 values_ =
new ScalarType[newsize];
745 valuesCopied_ =
true;
749 copyMat(Source.upper_, Source.values_, Source.stride_, Source.numRowCols_, upper_, values_, stride_, 0);
754 template<
typename OrdinalType,
typename ScalarType>
761 template<
typename OrdinalType,
typename ScalarType>
765 if ((numRowCols_ != Source.numRowCols_))
767 TEUCHOS_CHK_REF(*
this);
773 template<
typename OrdinalType,
typename ScalarType>
777 if ((numRowCols_ != Source.numRowCols_))
779 TEUCHOS_CHK_REF(*
this);
785 template<
typename OrdinalType,
typename ScalarType>
789 if((!valuesCopied_) && (!Source.valuesCopied_) && (values_ == Source.values_)) {
790 upper_ = Source.upper_;
795 if ((numRowCols_ != Source.numRowCols_))
797 TEUCHOS_CHK_REF(*
this);
799 copyMat(Source.upper_, Source.values_, Source.stride_, numRowCols_, upper_, values_, stride_, 0 );
807 template<
typename OrdinalType,
typename ScalarType>
810 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
811 checkIndex( rowIndex, colIndex );
813 if ( rowIndex <= colIndex ) {
816 return(values_[colIndex * stride_ + rowIndex]);
818 return(values_[rowIndex * stride_ + colIndex]);
823 return(values_[rowIndex * stride_ + colIndex]);
825 return(values_[colIndex * stride_ + rowIndex]);
829 template<
typename OrdinalType,
typename ScalarType>
832 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
833 checkIndex( rowIndex, colIndex );
835 if ( rowIndex <= colIndex ) {
838 return(values_[colIndex * stride_ + rowIndex]);
840 return(values_[rowIndex * stride_ + colIndex]);
845 return(values_[rowIndex * stride_ + colIndex]);
847 return(values_[colIndex * stride_ + rowIndex]);
855 template<
typename OrdinalType,
typename ScalarType>
861 template<
typename OrdinalType,
typename ScalarType>
872 for (j=0; j<numRowCols_; j++) {
874 ptr = values_ + j*stride_;
875 for (i=0; i<j; i++) {
878 ptr = values_ + j + j*stride_;
879 for (i=j; i<numRowCols_; i++) {
883 anorm = TEUCHOS_MAX( anorm, sum );
887 for (j=0; j<numRowCols_; j++) {
889 ptr = values_ + j + j*stride_;
890 for (i=j; i<numRowCols_; i++) {
894 for (i=0; i<j; i++) {
898 anorm = TEUCHOS_MAX( anorm, sum );
904 template<
typename OrdinalType,
typename ScalarType>
914 for (j = 0; j < numRowCols_; j++) {
915 for (i = 0; i < j; i++) {
922 for (j = 0; j < numRowCols_; j++) {
924 for (i = j+1; i < numRowCols_; i++) {
937 template<
typename OrdinalType,
typename ScalarType>
941 if((numRowCols_ != Operand.numRowCols_)) {
946 for(i = 0; i < numRowCols_; i++) {
947 for(j = 0; j < numRowCols_; j++) {
948 if((*
this)(i, j) != Operand(i, j)) {
957 template<
typename OrdinalType,
typename ScalarType>
960 return !((*this) == Operand);
967 template<
typename OrdinalType,
typename ScalarType>
974 for (j=0; j<numRowCols_; j++) {
975 ptr = values_ + j*stride_;
976 for (i=0; i<= j; i++) { *ptr = alpha * (*ptr); ptr++; }
980 for (j=0; j<numRowCols_; j++) {
981 ptr = values_ + j*stride_ + j;
982 for (i=j; i<numRowCols_; i++) { *ptr = alpha * (*ptr); ptr++; }
1016 template<
typename OrdinalType,
typename ScalarType>
1017 #ifndef TEUCHOS_HIDE_DEPRECATED_CODE
1025 os <<
"Values_copied : yes" << std::endl;
1027 os <<
"Values_copied : no" << std::endl;
1028 os <<
"Rows / Columns : " << numRowCols_ << std::endl;
1029 os <<
"LDA : " << stride_ << std::endl;
1031 os <<
"Storage: Upper" << std::endl;
1033 os <<
"Storage: Lower" << std::endl;
1034 if(numRowCols_ == 0) {
1035 os <<
"(matrix is empty, no values to display)" << std::endl;
1037 for(OrdinalType i = 0; i < numRowCols_; i++) {
1038 for(OrdinalType j = 0; j < numRowCols_; j++){
1039 os << (*this)(i,j) <<
" ";
1044 #ifdef TEUCHOS_HIDE_DEPRECATED_CODE
1053 template<
typename OrdinalType,
typename ScalarType>
1056 "SerialSymDenseMatrix<T>::checkIndex: "
1057 "Row index " << rowIndex <<
" out of range [0, "<< numRowCols_ <<
")");
1059 "SerialSymDenseMatrix<T>::checkIndex: "
1060 "Col index " << colIndex <<
" out of range [0, "<< numRowCols_ <<
")");
1063 template<
typename OrdinalType,
typename ScalarType>
1064 void SerialSymDenseMatrix<OrdinalType, ScalarType>::deleteArrays(
void)
1070 valuesCopied_ =
false;
1074 template<
typename OrdinalType,
typename ScalarType>
1075 void SerialSymDenseMatrix<OrdinalType, ScalarType>::copyMat(
1076 bool inputUpper, ScalarType* inputMatrix,
1077 OrdinalType inputStride, OrdinalType numRowCols_in,
1078 bool outputUpper, ScalarType* outputMatrix,
1079 OrdinalType outputStride, OrdinalType startRowCol,
1084 ScalarType* ptr1 = 0;
1085 ScalarType* ptr2 = 0;
1087 for (j = 0; j < numRowCols_in; j++) {
1088 if (inputUpper ==
true) {
1090 ptr2 = inputMatrix + (j + startRowCol) * inputStride + startRowCol;
1091 if (outputUpper ==
true) {
1093 ptr1 = outputMatrix + j*outputStride;
1095 for(i = 0; i <= j; i++) {
1096 *ptr1++ += alpha*(*ptr2++);
1099 for(i = 0; i <= j; i++) {
1107 ptr1 = outputMatrix + j;
1109 for(i = 0; i <= j; i++) {
1110 *ptr1 += alpha*(*ptr2++);
1111 ptr1 += outputStride;
1114 for(i = 0; i <= j; i++) {
1116 ptr1 += outputStride;
1123 ptr2 = inputMatrix + (startRowCol+j) * inputStride + startRowCol + j;
1124 if (outputUpper ==
true) {
1127 ptr1 = outputMatrix + j*outputStride + j;
1129 for(i = j; i < numRowCols_in; i++) {
1130 *ptr1 += alpha*(*ptr2++);
1131 ptr1 += outputStride;
1134 for(i = j; i < numRowCols_in; i++) {
1136 ptr1 += outputStride;
1142 ptr1 = outputMatrix + j*outputStride + j;
1144 for(i = j; i < numRowCols_in; i++) {
1145 *ptr1++ += alpha*(*ptr2++);
1148 for(i = j; i < numRowCols_in; i++) {
1157 template<
typename OrdinalType,
typename ScalarType>
1158 void SerialSymDenseMatrix<OrdinalType, ScalarType>::copyUPLOMat(
1159 bool inputUpper, ScalarType* inputMatrix,
1160 OrdinalType inputStride, OrdinalType inputRows
1164 ScalarType * ptr1 = 0;
1165 ScalarType * ptr2 = 0;
1168 for (j=1; j<inputRows; j++) {
1169 ptr1 = inputMatrix + j;
1170 ptr2 = inputMatrix + j*inputStride;
1171 for (i=0; i<j; i++) {
1178 for (i=1; i<inputRows; i++) {
1179 ptr1 = inputMatrix + i;
1180 ptr2 = inputMatrix + i*inputStride;
1181 for (j=0; j<i; j++) {
1189 #ifndef TEUCHOS_HIDE_DEPRECATED_CODE
1190 template<
typename OrdinalType,
typename ScalarType>
1200 template<
typename OrdinalType,
typename ScalarType>
1210 template<
typename OrdinalType,
typename ScalarType>
1212 operator<<(std::ostream &out,
1215 printer.obj.print(out);
1220 template<
typename OrdinalType,
typename ScalarType>
1221 SerialSymDenseMatrixPrinter<OrdinalType,ScalarType>