Teuchos - Trilinos Tools Package Version of the Day
Loading...
Searching...
No Matches
Teuchos_SerialSymDenseMatrix.hpp
Go to the documentation of this file.
1
2// @HEADER
3// *****************************************************************************
4// Teuchos: Common Tools Package
5//
6// Copyright 2004 NTESS and the Teuchos contributors.
7// SPDX-License-Identifier: BSD-3-Clause
8// *****************************************************************************
9// @HEADER
10
11#ifndef _TEUCHOS_SERIALSYMDENSEMATRIX_HPP_
12#define _TEUCHOS_SERIALSYMDENSEMATRIX_HPP_
18#include "Teuchos_BLAS.hpp"
22#include "Teuchos_Assert.hpp"
23#include <utility>
24#include <vector>
25
88namespace Teuchos {
89
90template<typename OrdinalType, typename ScalarType>
91class SerialSymDenseMatrix : public CompObject, public BLAS<OrdinalType,ScalarType>
92{
93 public:
94
99
101
102
112
114
124 SerialSymDenseMatrix(OrdinalType numRowsCols, bool zeroOut = true);
125
127
139
142
144
154
156 virtual ~SerialSymDenseMatrix ();
158
160
161
163
172 int shape(OrdinalType numRowsCols);
173
175
184 int shapeUninitialized(OrdinalType numRowsCols);
185
187
197 int reshape(OrdinalType numRowsCols);
198
200
202 void setLower();
203
205
207 void setUpper();
208
210
212
213
215
222
224
229
231
235
237
242 int putScalar( const ScalarType value = Teuchos::ScalarTraits<ScalarType>::zero(), bool fullMatrix = false );
243
245
249
251
254
256
258
259
261
268 ScalarType& operator () (OrdinalType rowIndex, OrdinalType colIndex);
269
271
278 const ScalarType& operator () (OrdinalType rowIndex, OrdinalType colIndex) const;
279
281
283 ScalarType* values() const { return(values_); }
284
286
288
289
291 bool upper() const {return(upper_);};
292
294 char UPLO() const {return(UPLO_);};
296
298
299
301
308
310
314
316
320
322
324
325
327
331
333
337
339
341
342
344 OrdinalType numRows() const { return(numRowCols_); }
345
347 OrdinalType numCols() const { return(numRowCols_); }
348
350 OrdinalType stride() const { return(stride_); }
351
353 bool empty() const { return(numRowCols_ == 0); }
354
356
358
359
362
365
369
371
372
373 virtual std::ostream& print(std::ostream& os) const;
374
376
377 protected:
378
379 // In-place scaling of matrix.
380 void scale( const ScalarType alpha );
381
382 // Copy the values from one matrix to the other.
387
388 // Copy the values from the active triangle of the matrix to the other to make the matrix full symmetric.
389 void copyUPLOMat(bool inputUpper, ScalarType* inputMatrix,
391
392 void deleteArrays();
393 void checkIndex( OrdinalType rowIndex, OrdinalType colIndex = 0 ) const;
394
395 static ScalarType*
396 allocateValues(const OrdinalType numRows,
397 const OrdinalType numCols)
398 {
399 const size_t size = size_t(numRows) * size_t(numCols);
400 return new ScalarType[size];
401 }
402
403 OrdinalType numRowCols_ = 0;
404 OrdinalType stride_ = 0;
405 bool valuesCopied_ = false;
406 ScalarType* values_ = nullptr;
407 bool upper_ = false;
408 char UPLO_ {'L'};
409};
410
411//----------------------------------------------------------------------------------------------------
412// Constructors and Destructor
413//----------------------------------------------------------------------------------------------------
414
415template<typename OrdinalType, typename ScalarType>
417 : numRowCols_(numRowCols_in), stride_(numRowCols_in), valuesCopied_(false), upper_(false), UPLO_('L')
418{
419 values_ = allocateValues(stride_, numRowCols_);
420 valuesCopied_ = true;
421 if (zeroOut) {
423 putScalar(ZERO, true);
424 }
425}
426
427template<typename OrdinalType, typename ScalarType>
430 )
431 : numRowCols_(numRowCols_in), stride_(stride_in), valuesCopied_(false),
432 values_(values_in), upper_(upper_in)
433{
434 if (upper_)
435 UPLO_ = 'U';
436 else
437 UPLO_ = 'L';
438
439 if(CV == Copy)
440 {
441 stride_ = numRowCols_;
442 values_ = allocateValues(stride_, numRowCols_);
443 copyMat(upper_in, values_in, stride_in, numRowCols_, upper_, values_, stride_, 0);
444 valuesCopied_ = true;
445 }
446}
447
448template<typename OrdinalType, typename ScalarType>
450 : numRowCols_(Source.numRowCols_), stride_(0), valuesCopied_(true),
451 values_(0), upper_(Source.upper_), UPLO_(Source.UPLO_)
452{
453 if (!Source.valuesCopied_)
454 {
455 stride_ = Source.stride_;
456 values_ = Source.values_;
457 valuesCopied_ = false;
458 }
459 else
460 {
461 stride_ = numRowCols_;
462 if(stride_ > 0 && numRowCols_ > 0) {
463 values_ = allocateValues(stride_, numRowCols_);
464 copyMat(Source.upper_, Source.values_, Source.stride_, numRowCols_, upper_, values_, stride_, 0);
465 }
466 else {
467 numRowCols_ = 0;
468 stride_ = 0;
469 valuesCopied_ = false;
470 }
471 }
472}
473
474template<typename OrdinalType, typename ScalarType>
479 numRowCols_(numRowCols_in), stride_(Source.stride_), valuesCopied_(false), upper_(Source.upper_), UPLO_(Source.UPLO_)
480{
481 if(CV == Copy)
482 {
483 stride_ = numRowCols_in;
484 values_ = allocateValues(stride_, numRowCols_in);
485 copyMat(Source.upper_, Source.values_, Source.stride_, numRowCols_in, upper_, values_, stride_, startRowCol);
486 valuesCopied_ = true;
487 }
488 else // CV == View
489 {
490 values_ = Source.values_ + (stride_ * startRowCol) + startRowCol;
491 }
492}
493
494template<typename OrdinalType, typename ScalarType>
499
500//----------------------------------------------------------------------------------------------------
501// Shape methods
502//----------------------------------------------------------------------------------------------------
503
504template<typename OrdinalType, typename ScalarType>
506{
507 deleteArrays(); // Get rid of anything that might be already allocated
508 numRowCols_ = numRowCols_in;
509 stride_ = numRowCols_;
510 values_ = allocateValues(stride_, numRowCols_);
511 putScalar( Teuchos::ScalarTraits<ScalarType>::zero(), true );
512 valuesCopied_ = true;
513 return(0);
514}
515
516template<typename OrdinalType, typename ScalarType>
518{
519 deleteArrays(); // Get rid of anything that might be already allocated
520 numRowCols_ = numRowCols_in;
521 stride_ = numRowCols_;
522 values_ = allocateValues(stride_, numRowCols_);
523 valuesCopied_ = true;
524 return(0);
525}
526
527template<typename OrdinalType, typename ScalarType>
529{
530 // Allocate space for new matrix
533 for(OrdinalType k = 0; k < numRowCols_in * numRowCols_in; k++)
534 {
535 values_tmp[k] = zero;
536 }
537 OrdinalType numRowCols_tmp = TEUCHOS_MIN(numRowCols_, numRowCols_in);
538 if(values_ != 0)
539 {
540 copyMat(upper_, values_, stride_, numRowCols_tmp, upper_, values_tmp, numRowCols_in, 0); // Copy principal submatrix of A to new A
541 }
542 deleteArrays(); // Get rid of anything that might be already allocated
543 numRowCols_ = numRowCols_in;
544 stride_ = numRowCols_;
545 values_ = values_tmp; // Set pointer to new A
546 valuesCopied_ = true;
547 return(0);
548}
549
550//----------------------------------------------------------------------------------------------------
551// Set methods
552//----------------------------------------------------------------------------------------------------
553
554template<typename OrdinalType, typename ScalarType>
556{
557 // Do nothing if the matrix is already a lower triangular matrix
558 if (upper_ != false) {
559 copyUPLOMat( true, values_, stride_, numRowCols_ );
560 upper_ = false;
561 UPLO_ = 'L';
562 }
563}
564
565template<typename OrdinalType, typename ScalarType>
567{
568 // Do nothing if the matrix is already an upper triangular matrix
569 if (upper_ == false) {
570 copyUPLOMat( false, values_, stride_, numRowCols_ );
571 upper_ = true;
572 UPLO_ = 'U';
573 }
574}
575
576template<typename OrdinalType, typename ScalarType>
578{
579 // Set each value of the dense matrix to "value".
580 if (fullMatrix == true) {
581 for(OrdinalType j = 0; j < numRowCols_; j++)
582 {
583 for(OrdinalType i = 0; i < numRowCols_; i++)
584 {
585 values_[i + j*stride_] = value_in;
586 }
587 }
588 }
589 // Set the active upper or lower triangular part of the matrix to "value"
590 else {
591 if (upper_) {
592 for(OrdinalType j = 0; j < numRowCols_; j++) {
593 for(OrdinalType i = 0; i <= j; i++) {
594 values_[i + j*stride_] = value_in;
595 }
596 }
597 }
598 else {
599 for(OrdinalType j = 0; j < numRowCols_; j++) {
600 for(OrdinalType i = j; i < numRowCols_; i++) {
601 values_[i + j*stride_] = value_in;
602 }
603 }
604 }
605 }
606 return 0;
607}
608
609template<typename OrdinalType, typename ScalarType> void
612{
613 std::swap(values_ , B.values_);
614 std::swap(numRowCols_, B.numRowCols_);
615 std::swap(stride_, B.stride_);
616 std::swap(valuesCopied_, B.valuesCopied_);
617 std::swap(upper_, B.upper_);
618 std::swap(UPLO_, B.UPLO_);
619}
620
621template<typename OrdinalType, typename ScalarType>
623{
625
626 // Set each value of the dense matrix to a random value.
627 std::vector<MT> diagSum( numRowCols_, Teuchos::ScalarTraits<MT>::zero() );
628 if (upper_) {
629 for(OrdinalType j = 0; j < numRowCols_; j++) {
630 for(OrdinalType i = 0; i < j; i++) {
631 values_[i + j*stride_] = ScalarTraits<ScalarType>::random();
632 diagSum[i] += Teuchos::ScalarTraits<ScalarType>::magnitude( values_[i + j*stride_] );
633 diagSum[j] += Teuchos::ScalarTraits<ScalarType>::magnitude( values_[i + j*stride_] );
634 }
635 }
636 }
637 else {
638 for(OrdinalType j = 0; j < numRowCols_; j++) {
639 for(OrdinalType i = j+1; i < numRowCols_; i++) {
640 values_[i + j*stride_] = ScalarTraits<ScalarType>::random();
641 diagSum[i] += Teuchos::ScalarTraits<ScalarType>::magnitude( values_[i + j*stride_] );
642 diagSum[j] += Teuchos::ScalarTraits<ScalarType>::magnitude( values_[i + j*stride_] );
643 }
644 }
645 }
646
647 // Set the diagonal.
648 for(OrdinalType i = 0; i < numRowCols_; i++) {
649 values_[i + i*stride_] = diagSum[i] + bias;
650 }
651 return 0;
652}
653
654template<typename OrdinalType, typename ScalarType>
657{
658 if(this == &Source)
659 return(*this); // Special case of source same as target
660 if((!valuesCopied_) && (!Source.valuesCopied_) && (values_ == Source.values_)) {
661 upper_ = Source.upper_; // Might have to change the active part of the matrix.
662 return(*this); // Special case of both are views to same data.
663 }
664
665 // If the source is a view then we will return a view, else we will return a copy.
666 if (!Source.valuesCopied_) {
667 if(valuesCopied_) {
668 // Clean up stored data if this was previously a copy.
669 deleteArrays();
670 }
671 numRowCols_ = Source.numRowCols_;
672 stride_ = Source.stride_;
673 values_ = Source.values_;
674 upper_ = Source.upper_;
675 UPLO_ = Source.UPLO_;
676 }
677 else {
678 // If we were a view, we will now be a copy.
679 if(!valuesCopied_) {
680 numRowCols_ = Source.numRowCols_;
681 stride_ = Source.numRowCols_;
682 upper_ = Source.upper_;
683 UPLO_ = Source.UPLO_;
684 if(stride_ > 0 && numRowCols_ > 0) {
685 values_ = allocateValues(stride_, numRowCols_);
686 valuesCopied_ = true;
687 }
688 else {
689 values_ = 0;
690 }
691 }
692 // If we were a copy, we will stay a copy.
693 else {
694 if((Source.numRowCols_ <= stride_) && (Source.numRowCols_ == numRowCols_)) { // we don't need to reallocate
695 numRowCols_ = Source.numRowCols_;
696 upper_ = Source.upper_;
697 UPLO_ = Source.UPLO_;
698 }
699 else { // we need to allocate more space (or less space)
700 deleteArrays();
701 numRowCols_ = Source.numRowCols_;
702 stride_ = Source.numRowCols_;
703 upper_ = Source.upper_;
704 UPLO_ = Source.UPLO_;
705 if(stride_ > 0 && numRowCols_ > 0) {
706 values_ = allocateValues(stride_, numRowCols_);
707 valuesCopied_ = true;
708 }
709 }
710 }
711 copyMat(Source.upper_, Source.values_, Source.stride_, Source.numRowCols_, upper_, values_, stride_, 0);
712 }
713 return(*this);
714}
715
716template<typename OrdinalType, typename ScalarType>
722
723template<typename OrdinalType, typename ScalarType>
725{
726 // Check for compatible dimensions
727 if ((numRowCols_ != Source.numRowCols_))
728 {
729 TEUCHOS_CHK_REF(*this); // Return *this without altering it.
730 }
731 copyMat(Source.upper_, Source.values_, Source.stride_, numRowCols_, upper_, values_, stride_, 0, ScalarTraits<ScalarType>::one());
732 return(*this);
733}
734
735template<typename OrdinalType, typename ScalarType>
737{
738 // Check for compatible dimensions
739 if ((numRowCols_ != Source.numRowCols_))
740 {
741 TEUCHOS_CHK_REF(*this); // Return *this without altering it.
742 }
743 copyMat(Source.upper_, Source.values_, Source.stride_, numRowCols_, upper_, values_, stride_, 0, -ScalarTraits<ScalarType>::one());
744 return(*this);
745}
746
747template<typename OrdinalType, typename ScalarType>
749 if(this == &Source)
750 return(*this); // Special case of source same as target
751 if((!valuesCopied_) && (!Source.valuesCopied_) && (values_ == Source.values_)) {
752 upper_ = Source.upper_; // We may have to change the active part of the matrix.
753 return(*this); // Special case of both are views to same data.
754 }
755
756 // Check for compatible dimensions
757 if ((numRowCols_ != Source.numRowCols_))
758 {
759 TEUCHOS_CHK_REF(*this); // Return *this without altering it.
760 }
761 copyMat(Source.upper_, Source.values_, Source.stride_, numRowCols_, upper_, values_, stride_, 0 );
762 return(*this);
763}
764
765//----------------------------------------------------------------------------------------------------
766// Accessor methods
767//----------------------------------------------------------------------------------------------------
768
769template<typename OrdinalType, typename ScalarType>
771{
772#ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
773 checkIndex( rowIndex, colIndex );
774#endif
775 if ( rowIndex <= colIndex ) {
776 // Accessing upper triangular part of matrix
777 if (upper_)
778 return(values_[colIndex * stride_ + rowIndex]);
779 else
780 return(values_[rowIndex * stride_ + colIndex]);
781 }
782 else {
783 // Accessing lower triangular part of matrix
784 if (upper_)
785 return(values_[rowIndex * stride_ + colIndex]);
786 else
787 return(values_[colIndex * stride_ + rowIndex]);
788 }
789}
790
791template<typename OrdinalType, typename ScalarType>
793{
794#ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
795 checkIndex( rowIndex, colIndex );
796#endif
797 if ( rowIndex <= colIndex ) {
798 // Accessing upper triangular part of matrix
799 if (upper_)
800 return(values_[colIndex * stride_ + rowIndex]);
801 else
802 return(values_[rowIndex * stride_ + colIndex]);
803 }
804 else {
805 // Accessing lower triangular part of matrix
806 if (upper_)
807 return(values_[rowIndex * stride_ + colIndex]);
808 else
809 return(values_[colIndex * stride_ + rowIndex]);
810 }
811}
812
813//----------------------------------------------------------------------------------------------------
814// Norm methods
815//----------------------------------------------------------------------------------------------------
816
817template<typename OrdinalType, typename ScalarType>
822
823template<typename OrdinalType, typename ScalarType>
825{
827
828 OrdinalType i, j;
829
832
833 if (upper_) {
834 for (j=0; j<numRowCols_; j++) {
836 ptr = values_ + j*stride_;
837 for (i=0; i<j; i++) {
839 }
840 ptr = values_ + j + j*stride_;
841 for (i=j; i<numRowCols_; i++) {
843 ptr += stride_;
844 }
845 anorm = TEUCHOS_MAX( anorm, sum );
846 }
847 }
848 else {
849 for (j=0; j<numRowCols_; j++) {
851 ptr = values_ + j + j*stride_;
852 for (i=j; i<numRowCols_; i++) {
854 }
855 ptr = values_ + j;
856 for (i=0; i<j; i++) {
858 ptr += stride_;
859 }
860 anorm = TEUCHOS_MAX( anorm, sum );
861 }
862 }
863 return(anorm);
864}
865
866template<typename OrdinalType, typename ScalarType>
868{
870
871 OrdinalType i, j;
872
874
875 if (upper_) {
876 for (j = 0; j < numRowCols_; j++) {
877 for (i = 0; i < j; i++) {
878 sum += ScalarTraits<ScalarType>::magnitude(2.0*values_[i+j*stride_]*values_[i+j*stride_]);
879 }
880 sum += ScalarTraits<ScalarType>::magnitude(values_[j + j*stride_]*values_[j + j*stride_]);
881 }
882 }
883 else {
884 for (j = 0; j < numRowCols_; j++) {
885 sum += ScalarTraits<ScalarType>::magnitude(values_[j + j*stride_]*values_[j + j*stride_]);
886 for (i = j+1; i < numRowCols_; i++) {
887 sum += ScalarTraits<ScalarType>::magnitude(2.0*values_[i+j*stride_]*values_[i+j*stride_]);
888 }
889 }
890 }
892 return(anorm);
893}
894
895//----------------------------------------------------------------------------------------------------
896// Comparison methods
897//----------------------------------------------------------------------------------------------------
898
899template<typename OrdinalType, typename ScalarType>
901{
902 bool result = 1;
903 if((numRowCols_ != Operand.numRowCols_)) {
904 result = 0;
905 }
906 else {
907 OrdinalType i, j;
908 for(i = 0; i < numRowCols_; i++) {
909 for(j = 0; j < numRowCols_; j++) {
910 if((*this)(i, j) != Operand(i, j)) {
911 return 0;
912 }
913 }
914 }
915 }
916 return result;
917}
918
919template<typename OrdinalType, typename ScalarType>
924
925//----------------------------------------------------------------------------------------------------
926// Multiplication method
927//----------------------------------------------------------------------------------------------------
928
929template<typename OrdinalType, typename ScalarType>
931{
932 OrdinalType i, j;
934
935 if (upper_) {
936 for (j=0; j<numRowCols_; j++) {
937 ptr = values_ + j*stride_;
938 for (i=0; i<= j; i++) { *ptr = alpha * (*ptr); ptr++; }
939 }
940 }
941 else {
942 for (j=0; j<numRowCols_; j++) {
943 ptr = values_ + j*stride_ + j;
944 for (i=j; i<numRowCols_; i++) { *ptr = alpha * (*ptr); ptr++; }
945 }
946 }
947}
948
949/*
950template<typename OrdinalType, typename ScalarType>
951int SerialSymDenseMatrix<OrdinalType, ScalarType>::scale( const SerialSymDenseMatrix<OrdinalType,ScalarType>& A )
952{
953 OrdinalType i, j;
954 ScalarType* ptr;
955
956 // Check for compatible dimensions
957 if ((numRowCols_ != A.numRowCols_)) {
958 TEUCHOS_CHK_ERR(-1); // Return error
959 }
960
961 if (upper_) {
962 for (j=0; j<numRowCols_; j++) {
963 ptr = values_ + j*stride_;
964 for (i=0; i<= j; i++) { *ptr = A(i,j) * (*ptr); ptr++; }
965 }
966 }
967 else {
968 for (j=0; j<numRowCols_; j++) {
969 ptr = values_ + j*stride_;
970 for (i=j; i<numRowCols_; i++) { *ptr = A(i,j) * (*ptr); ptr++; }
971 }
972 }
973
974 return(0);
975}
976*/
977
978template<typename OrdinalType, typename ScalarType>
979std::ostream& SerialSymDenseMatrix<OrdinalType, ScalarType>::print(std::ostream& os) const
980{
981 os << std::endl;
982 if(valuesCopied_)
983 os << "Values_copied : yes" << std::endl;
984 else
985 os << "Values_copied : no" << std::endl;
986 os << "Rows / Columns : " << numRowCols_ << std::endl;
987 os << "LDA : " << stride_ << std::endl;
988 if (upper_)
989 os << "Storage: Upper" << std::endl;
990 else
991 os << "Storage: Lower" << std::endl;
992 if(numRowCols_ == 0) {
993 os << "(matrix is empty, no values to display)" << std::endl;
994 } else {
995 for(OrdinalType i = 0; i < numRowCols_; i++) {
996 for(OrdinalType j = 0; j < numRowCols_; j++){
997 os << (*this)(i,j) << " ";
998 }
999 os << std::endl;
1000 }
1001 }
1002 return os;
1003}
1004
1005//----------------------------------------------------------------------------------------------------
1006// Protected methods
1007//----------------------------------------------------------------------------------------------------
1008
1009template<typename OrdinalType, typename ScalarType>
1011 TEUCHOS_TEST_FOR_EXCEPTION(rowIndex < 0 || rowIndex >= numRowCols_, std::out_of_range,
1012 "SerialSymDenseMatrix<T>::checkIndex: "
1013 "Row index " << rowIndex << " out of range [0, "<< numRowCols_ << ")");
1014 TEUCHOS_TEST_FOR_EXCEPTION(colIndex < 0 || colIndex >= numRowCols_, std::out_of_range,
1015 "SerialSymDenseMatrix<T>::checkIndex: "
1016 "Col index " << colIndex << " out of range [0, "<< numRowCols_ << ")");
1017}
1018
1019template<typename OrdinalType, typename ScalarType>
1020void SerialSymDenseMatrix<OrdinalType, ScalarType>::deleteArrays(void)
1021{
1022 if (valuesCopied_)
1023 {
1024 delete [] values_;
1025 values_ = 0;
1026 valuesCopied_ = false;
1027 }
1028}
1029
1030template<typename OrdinalType, typename ScalarType>
1031void SerialSymDenseMatrix<OrdinalType, ScalarType>::copyMat(
1032 bool inputUpper, ScalarType* inputMatrix,
1033 OrdinalType inputStride, OrdinalType numRowCols_in,
1034 bool outputUpper, ScalarType* outputMatrix,
1035 OrdinalType outputStride, OrdinalType startRowCol,
1036 ScalarType alpha
1037 )
1038{
1039 OrdinalType i, j;
1040 ScalarType* ptr1 = 0;
1041 ScalarType* ptr2 = 0;
1042
1043 for (j = 0; j < numRowCols_in; j++) {
1044 if (inputUpper == true) {
1045 // The input matrix is upper triangular, start at the beginning of each column.
1046 ptr2 = inputMatrix + (j + startRowCol) * inputStride + startRowCol;
1047 if (outputUpper == true) {
1048 // The output matrix matches the same pattern as the input matrix.
1049 ptr1 = outputMatrix + j*outputStride;
1051 for(i = 0; i <= j; i++) {
1052 *ptr1++ += alpha*(*ptr2++);
1053 }
1054 } else {
1055 for(i = 0; i <= j; i++) {
1056 *ptr1++ = *ptr2++;
1057 }
1058 }
1059 }
1060 else {
1061 // The output matrix has the opposite pattern as the input matrix.
1062 // Copy down across rows of the output matrix, but down columns of the input matrix.
1063 ptr1 = outputMatrix + j;
1065 for(i = 0; i <= j; i++) {
1066 *ptr1 += alpha*(*ptr2++);
1067 ptr1 += outputStride;
1068 }
1069 } else {
1070 for(i = 0; i <= j; i++) {
1071 *ptr1 = *ptr2++;
1072 ptr1 += outputStride;
1073 }
1074 }
1075 }
1076 }
1077 else {
1078 // The input matrix is lower triangular, start at the diagonal of each row.
1079 ptr2 = inputMatrix + (startRowCol+j) * inputStride + startRowCol + j;
1080 if (outputUpper == true) {
1081 // The output matrix has the opposite pattern as the input matrix.
1082 // Copy across rows of the output matrix, but down columns of the input matrix.
1083 ptr1 = outputMatrix + j*outputStride + j;
1085 for(i = j; i < numRowCols_in; i++) {
1086 *ptr1 += alpha*(*ptr2++);
1087 ptr1 += outputStride;
1088 }
1089 } else {
1090 for(i = j; i < numRowCols_in; i++) {
1091 *ptr1 = *ptr2++;
1092 ptr1 += outputStride;
1093 }
1094 }
1095 }
1096 else {
1097 // The output matrix matches the same pattern as the input matrix.
1098 ptr1 = outputMatrix + j*outputStride + j;
1100 for(i = j; i < numRowCols_in; i++) {
1101 *ptr1++ += alpha*(*ptr2++);
1102 }
1103 } else {
1104 for(i = j; i < numRowCols_in; i++) {
1105 *ptr1++ = *ptr2++;
1106 }
1107 }
1108 }
1109 }
1110 }
1111}
1112
1113template<typename OrdinalType, typename ScalarType>
1114void SerialSymDenseMatrix<OrdinalType, ScalarType>::copyUPLOMat(
1115 bool inputUpper, ScalarType* inputMatrix,
1116 OrdinalType inputStride, OrdinalType inputRows
1117 )
1118{
1119 OrdinalType i, j;
1120 ScalarType * ptr1 = 0;
1121 ScalarType * ptr2 = 0;
1122
1123 if (inputUpper) {
1124 for (j=1; j<inputRows; j++) {
1125 ptr1 = inputMatrix + j;
1126 ptr2 = inputMatrix + j*inputStride;
1127 for (i=0; i<j; i++) {
1128 *ptr1 = *ptr2++;
1129 ptr1+=inputStride;
1130 }
1131 }
1132 }
1133 else {
1134 for (i=1; i<inputRows; i++) {
1135 ptr1 = inputMatrix + i;
1136 ptr2 = inputMatrix + i*inputStride;
1137 for (j=0; j<i; j++) {
1138 *ptr2++ = *ptr1;
1139 ptr1+=inputStride;
1140 }
1141 }
1142 }
1143}
1144
1146template<typename OrdinalType, typename ScalarType>
1154
1156template<typename OrdinalType, typename ScalarType>
1157std::ostream&
1158operator<<(std::ostream &out,
1160{
1161 printer.obj.print(out);
1162 return out;
1163}
1164
1166template<typename OrdinalType, typename ScalarType>
1167SerialSymDenseMatrixPrinter<OrdinalType,ScalarType>
1172
1173
1174} // namespace Teuchos
1175
1176#endif /* _TEUCHOS_SERIALSYMDENSEMATRIX_HPP_ */
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 BLAS wrapper.
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 symmetric, positive-definite dense matrices of temp...
SerialSymDenseMatrix()=default
Default constructor; defines a zero size object.
bool operator==(const SerialSymDenseMatrix< OrdinalType, ScalarType > &Operand) const
Equality of two matrices.
int shapeUninitialized(OrdinalType numRowsCols)
Set dimensions of a Teuchos::SerialSymDenseMatrix object; don't initialize values.
int shape(OrdinalType numRowsCols)
Set dimensions of a Teuchos::SerialSymDenseMatrix object; init values to zero.
ScalarTraits< ScalarType >::magnitudeType normFrobenius() const
Returns the Frobenius-norm of the matrix.
ScalarType & operator()(OrdinalType rowIndex, OrdinalType colIndex)
Element access method (non-const).
OrdinalType stride() const
Returns the stride between the columns of this matrix in memory.
SerialSymDenseMatrix< OrdinalType, ScalarType > & operator-=(const SerialSymDenseMatrix< OrdinalType, ScalarType > &Source)
Subtract another matrix from this matrix.
virtual std::ostream & print(std::ostream &os) const
Print method. Defines the behavior of the std::ostream << operator.
SerialSymDenseMatrix< OrdinalType, ScalarType > & operator+=(const SerialSymDenseMatrix< OrdinalType, ScalarType > &Source)
Add another matrix to this matrix.
bool operator!=(const SerialSymDenseMatrix< OrdinalType, ScalarType > &Operand) const
Inequality of two matrices.
void swap(SerialSymDenseMatrix< OrdinalType, ScalarType > &B)
Swap values between this matrix and incoming matrix.
OrdinalType numCols() const
Returns the column dimension of this matrix.
int putScalar(const ScalarType value=Teuchos::ScalarTraits< ScalarType >::zero(), bool fullMatrix=false)
Set all values in the matrix to a constant value.
virtual ~SerialSymDenseMatrix()
Teuchos::SerialSymDenseMatrix destructor.
char UPLO() const
Returns character value of UPLO used by LAPACK routines.
SerialSymDenseMatrix< OrdinalType, ScalarType > & operator*=(const ScalarType alpha)
Inplace scalar-matrix product A = alpha*A.
int random(const ScalarType bias=0.1 *Teuchos::ScalarTraits< ScalarType >::one())
Set all values in the active area (upper/lower triangle) of this matrix to be random numbers.
ScalarType scalarType
Typedef for scalar type.
bool upper() const
Returns true if upper triangular part of this matrix has and will be used.
ScalarTraits< ScalarType >::magnitudeType normInf() const
Returns the Infinity-norm of the matrix.
ScalarType * values() const
Returns the pointer to the ScalarType data array contained in the object.
SerialSymDenseMatrix< OrdinalType, ScalarType > & operator=(const SerialSymDenseMatrix< OrdinalType, ScalarType > &Source)
Copies values from one matrix to another.
bool empty() const
Returns whether this matrix is empty.
SerialSymDenseMatrix< OrdinalType, ScalarType > & assign(const SerialSymDenseMatrix< OrdinalType, ScalarType > &Source)
Copies values from one matrix to another.
void setLower()
Specify that the lower triangle of the this matrix should be used.
int reshape(OrdinalType numRowsCols)
Reshape a Teuchos::SerialSymDenseMatrix object.
OrdinalType numRows() const
Returns the row dimension of this matrix.
void setUpper()
Specify that the upper triangle of the this matrix should be used.
ScalarTraits< ScalarType >::magnitudeType normOne() const
Returns the 1-norm of the matrix.
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 random()
Returns a random number (between -one() and +one()) of this scalar type.
Ostream manipulator for SerialSymDenseMatrix.