Teuchos - Trilinos Tools Package  Version of the Day
Teuchos_SerialDenseMatrix.hpp
Go to the documentation of this file.
1 // @HEADER
2 // ***********************************************************************
3 //
4 // Teuchos: Common Tools Package
5 // Copyright (2004) Sandia Corporation
6 //
7 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
8 // license for use of this work by or on behalf of the U.S. Government.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are
12 // met:
13 //
14 // 1. Redistributions of source code must retain the above copyright
15 // notice, this list of conditions and the following disclaimer.
16 //
17 // 2. Redistributions in binary form must reproduce the above copyright
18 // notice, this list of conditions and the following disclaimer in the
19 // documentation and/or other materials provided with the distribution.
20 //
21 // 3. Neither the name of the Corporation nor the names of the
22 // contributors may be used to endorse or promote products derived from
23 // this software without specific prior written permission.
24 //
25 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
26 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
29 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 //
37 // Questions? Contact Michael A. Heroux (maherou@sandia.gov)
38 //
39 // ***********************************************************************
40 // @HEADER
41 
42 #ifndef _TEUCHOS_SERIALDENSEMATRIX_HPP_
43 #define _TEUCHOS_SERIALDENSEMATRIX_HPP_
44 
48 #include "Teuchos_CompObject.hpp"
49 #include "Teuchos_BLAS.hpp"
50 #include "Teuchos_ScalarTraits.hpp"
51 #include "Teuchos_DataAccess.hpp"
52 #include "Teuchos_ConfigDefs.hpp"
53 #include "Teuchos_Assert.hpp"
55 
56 #include <utility>
57 
66 namespace Teuchos {
67 
68 template<typename OrdinalType, typename ScalarType>
69 class SerialDenseMatrix : public CompObject, public BLAS<OrdinalType, ScalarType>
70 {
71 public:
72 
74  typedef OrdinalType ordinalType;
76  typedef ScalarType scalarType;
77 
79 
80 
82 
86 
88 
96  SerialDenseMatrix(OrdinalType numRows, OrdinalType numCols, bool zeroOut = true);
97 
99 
107  SerialDenseMatrix(DataAccess CV, ScalarType* values, OrdinalType stride, OrdinalType numRows, OrdinalType numCols);
108 
110 
116 
118 
121 
123 
135  SerialDenseMatrix(DataAccess CV, const SerialDenseMatrix<OrdinalType, ScalarType> &Source, OrdinalType numRows, OrdinalType numCols, OrdinalType startRow=0, OrdinalType startCol=0);
136 
138  virtual ~SerialDenseMatrix();
140 
142 
143 
154  int shape(OrdinalType numRows, OrdinalType numCols);
155 
157  int shapeUninitialized(OrdinalType numRows, OrdinalType numCols);
158 
160 
170  int reshape(OrdinalType numRows, OrdinalType numCols);
171 
172 
174 
176 
177 
179 
186 
188 
194 
196 
199  SerialDenseMatrix<OrdinalType, ScalarType>& operator= (const ScalarType value) { putScalar(value); return(*this); }
200 
202 
206  int putScalar( const ScalarType value = Teuchos::ScalarTraits<ScalarType>::zero() );
207 
209 
213 
215  int random();
216 
218 
220 
221 
223 
230  ScalarType& operator () (OrdinalType rowIndex, OrdinalType colIndex);
231 
233 
240  const ScalarType& operator () (OrdinalType rowIndex, OrdinalType colIndex) const;
241 
243 
250  ScalarType* operator [] (OrdinalType colIndex);
251 
253 
260  const ScalarType* operator [] (OrdinalType colIndex) const;
261 
263 
264  ScalarType* values() const { return(values_); }
265 
267 
269 
270 
272 
276 
278 
282 
284 
288 
290 
294  int scale ( const ScalarType alpha );
295 
297 
304 
306 
320  int multiply (ETransp transa, ETransp transb, ScalarType alpha, const SerialDenseMatrix<OrdinalType, ScalarType> &A, const SerialDenseMatrix<OrdinalType, ScalarType> &B, ScalarType beta);
321 
323 
334  int multiply (ESide sideA, ScalarType alpha, const SerialSymDenseMatrix<OrdinalType, ScalarType> &A, const SerialDenseMatrix<OrdinalType, ScalarType> &B, ScalarType beta);
335 
337 
339 
340 
342 
345  bool operator== (const SerialDenseMatrix<OrdinalType, ScalarType> &Operand) const;
346 
348 
351  bool operator!= (const SerialDenseMatrix<OrdinalType, ScalarType> &Operand) const;
352 
354 
356 
357 
359  OrdinalType numRows() const { return(numRows_); }
360 
362  OrdinalType numCols() const { return(numCols_); }
363 
365  OrdinalType stride() const { return(stride_); }
366 
368  bool empty() const { return(numRows_ == 0 || numCols_ == 0); }
370 
372 
373 
376 
379 
383 
385 
386 #ifndef TEUCHOS_HIDE_DEPRECATED_CODE
388  virtual void print(std::ostream& os) const;
389 #else
390  virtual std::ostream& print(std::ostream& os) const;
391 #endif
392 
394 protected:
395  void copyMat(ScalarType* inputMatrix, OrdinalType strideInput,
396  OrdinalType numRows, OrdinalType numCols, ScalarType* outputMatrix,
397  OrdinalType strideOutput, OrdinalType startRow, OrdinalType startCol,
398  ScalarType alpha = ScalarTraits<ScalarType>::zero() );
399  void deleteArrays();
400  void checkIndex( OrdinalType rowIndex, OrdinalType colIndex = 0 ) const;
401  OrdinalType numRows_;
402  OrdinalType numCols_;
403  OrdinalType stride_;
404  bool valuesCopied_;
405  ScalarType* values_;
406 
407 }; // class Teuchos_SerialDenseMatrix
408 
409 //----------------------------------------------------------------------------------------------------
410 // Constructors and Destructor
411 //----------------------------------------------------------------------------------------------------
412 
413 template<typename OrdinalType, typename ScalarType>
415  : CompObject(), BLAS<OrdinalType,ScalarType>(), numRows_(0), numCols_(0), stride_(0), valuesCopied_(false), values_(0)
416 {}
417 
418 template<typename OrdinalType, typename ScalarType>
420  OrdinalType numRows_in, OrdinalType numCols_in, bool zeroOut
421  )
422  : CompObject(), BLAS<OrdinalType,ScalarType>(), numRows_(numRows_in), numCols_(numCols_in), stride_(numRows_in)
423 {
424  values_ = new ScalarType[stride_*numCols_];
425  valuesCopied_ = true;
426  if (zeroOut == true)
427  putScalar();
428 }
429 
430 template<typename OrdinalType, typename ScalarType>
432  DataAccess CV, ScalarType* values_in, OrdinalType stride_in, OrdinalType numRows_in,
433  OrdinalType numCols_in
434  )
435  : CompObject(), BLAS<OrdinalType,ScalarType>(), numRows_(numRows_in), numCols_(numCols_in), stride_(stride_in),
436  valuesCopied_(false), values_(values_in)
437 {
438  if(CV == Copy)
439  {
440  stride_ = numRows_;
441  values_ = new ScalarType[stride_*numCols_];
442  copyMat(values_in, stride_in, numRows_, numCols_, values_, stride_, 0, 0);
443  valuesCopied_ = true;
444  }
445 }
446 
447 template<typename OrdinalType, typename ScalarType>
449  : CompObject(),BLAS<OrdinalType,ScalarType>(), numRows_(0), numCols_(0), stride_(0), valuesCopied_(true), values_(0)
450 {
451  if ( trans == Teuchos::NO_TRANS )
452  {
453  numRows_ = Source.numRows_;
454  numCols_ = Source.numCols_;
455 
456  if (!Source.valuesCopied_)
457  {
458  stride_ = Source.stride_;
459  values_ = Source.values_;
460  valuesCopied_ = false;
461  }
462  else
463  {
464  stride_ = numRows_;
465  const OrdinalType newsize = stride_ * numCols_;
466  if(newsize > 0) {
467  values_ = new ScalarType[newsize];
468  copyMat(Source.values_, Source.stride_, numRows_, numCols_, values_, stride_, 0, 0);
469  }
470  else {
471  numRows_ = 0; numCols_ = 0; stride_ = 0;
472  valuesCopied_ = false;
473  }
474  }
475  }
477  {
478  numRows_ = Source.numCols_;
479  numCols_ = Source.numRows_;
480  stride_ = numRows_;
481  values_ = new ScalarType[stride_*numCols_];
482  for (OrdinalType j=0; j<numCols_; j++) {
483  for (OrdinalType i=0; i<numRows_; i++) {
484  values_[j*stride_ + i] = Teuchos::ScalarTraits<ScalarType>::conjugate(Source.values_[i*Source.stride_ + j]);
485  }
486  }
487  }
488  else
489  {
490  numRows_ = Source.numCols_;
491  numCols_ = Source.numRows_;
492  stride_ = numRows_;
493  values_ = new ScalarType[stride_*numCols_];
494  for (OrdinalType j=0; j<numCols_; j++) {
495  for (OrdinalType i=0; i<numRows_; i++) {
496  values_[j*stride_ + i] = Source.values_[i*Source.stride_ + j];
497  }
498  }
499  }
500 }
501 
502 
503 template<typename OrdinalType, typename ScalarType>
506  )
507  : CompObject(), numRows_(Source.numRows_), numCols_(Source.numCols_), stride_(Source.stride_),
508  valuesCopied_(false), values_(Source.values_)
509 {
510  if(CV == Copy)
511  {
512  stride_ = numRows_;
513  values_ = new ScalarType[stride_ * numCols_];
514  copyMat(Source.values_, Source.stride_, numRows_, numCols_, values_, stride_, 0, 0);
515  valuesCopied_ = true;
516  }
517 }
518 
519 
520 template<typename OrdinalType, typename ScalarType>
523  OrdinalType numRows_in, OrdinalType numCols_in, OrdinalType startRow,
524  OrdinalType startCol
525  )
526  : CompObject(), numRows_(numRows_in), numCols_(numCols_in), stride_(Source.stride_),
527  valuesCopied_(false), values_(Source.values_)
528 {
529  if(CV == Copy)
530  {
531  stride_ = numRows_in;
532  values_ = new ScalarType[stride_ * numCols_in];
533  copyMat(Source.values_, Source.stride_, numRows_in, numCols_in, values_, stride_, startRow, startCol);
534  valuesCopied_ = true;
535  }
536  else // CV == View
537  {
538  values_ = values_ + (stride_ * startCol) + startRow;
539  }
540 }
541 
542 template<typename OrdinalType, typename ScalarType>
544 {
545  deleteArrays();
546 }
547 
548 //----------------------------------------------------------------------------------------------------
549 // Shape methods
550 //----------------------------------------------------------------------------------------------------
551 
552 template<typename OrdinalType, typename ScalarType>
554  OrdinalType numRows_in, OrdinalType numCols_in
555  )
556 {
557  deleteArrays(); // Get rid of anything that might be already allocated
558  numRows_ = numRows_in;
559  numCols_ = numCols_in;
560  stride_ = numRows_;
561  values_ = new ScalarType[stride_*numCols_];
562  putScalar();
563  valuesCopied_ = true;
564  return(0);
565 }
566 
567 template<typename OrdinalType, typename ScalarType>
569  OrdinalType numRows_in, OrdinalType numCols_in
570  )
571 {
572  deleteArrays(); // Get rid of anything that might be already allocated
573  numRows_ = numRows_in;
574  numCols_ = numCols_in;
575  stride_ = numRows_;
576  values_ = new ScalarType[stride_*numCols_];
577  valuesCopied_ = true;
578  return(0);
579 }
580 
581 template<typename OrdinalType, typename ScalarType>
583  OrdinalType numRows_in, OrdinalType numCols_in
584  )
585 {
586  // Allocate space for new matrix
587  ScalarType* values_tmp = new ScalarType[numRows_in * numCols_in];
588  ScalarType zero = ScalarTraits<ScalarType>::zero();
589  for(OrdinalType k = 0; k < numRows_in * numCols_in; k++)
590  {
591  values_tmp[k] = zero;
592  }
593  OrdinalType numRows_tmp = TEUCHOS_MIN(numRows_, numRows_in);
594  OrdinalType numCols_tmp = TEUCHOS_MIN(numCols_, numCols_in);
595  if(values_ != 0)
596  {
597  copyMat(values_, stride_, numRows_tmp, numCols_tmp, values_tmp,
598  numRows_in, 0, 0); // Copy principal submatrix of A to new A
599  }
600  deleteArrays(); // Get rid of anything that might be already allocated
601  numRows_ = numRows_in;
602  numCols_ = numCols_in;
603  stride_ = numRows_;
604  values_ = values_tmp; // Set pointer to new A
605  valuesCopied_ = true;
606  return(0);
607 }
608 
609 //----------------------------------------------------------------------------------------------------
610 // Set methods
611 //----------------------------------------------------------------------------------------------------
612 
613 template<typename OrdinalType, typename ScalarType>
615 {
616  // Set each value of the dense matrix to "value".
617  for(OrdinalType j = 0; j < numCols_; j++)
618  {
619  for(OrdinalType i = 0; i < numRows_; i++)
620  {
621  values_[i + j*stride_] = value_in;
622  }
623  }
624  return 0;
625 }
626 
627 template<typename OrdinalType, typename ScalarType> void
630 {
631  // Notes:
632  // > DefaultBLASImpl::SWAP() uses a deep copy. This fn uses a pointer swap.
633  // > this fn covers both Vector and Matrix, such that some care must be
634  // employed to not swap across types (creating a Vector with non-unitary
635  // numCols_)
636  // > Inherited data that is not currently swapped (since inactive/deprecated):
637  // >> Teuchos::CompObject:
638  // Flops *flopCounter_ [Note: all SerialDenseMatrix ctors initialize a
639  // NULL flop-counter using CompObject(), such that any flop increments
640  // that are computed are not accumulated.]
641  // >> Teuchos::Object: (now removed from inheritance list)
642  // static int tracebackMode (no swap for statics)
643  // std::string label_ (has been reported as a cause of memory overhead)
644 
645  std::swap(values_ , B.values_);
646  std::swap(numRows_, B.numRows_);
647  std::swap(numCols_, B.numCols_);
648  std::swap(stride_, B.stride_);
649  std::swap(valuesCopied_, B.valuesCopied_);
650 }
651 
652 template<typename OrdinalType, typename ScalarType>
654 {
655  // Set each value of the dense matrix to a random value.
656  for(OrdinalType j = 0; j < numCols_; j++)
657  {
658  for(OrdinalType i = 0; i < numRows_; i++)
659  {
660  values_[i + j*stride_] = ScalarTraits<ScalarType>::random();
661  }
662  }
663  return 0;
664 }
665 
666 template<typename OrdinalType, typename ScalarType>
670  )
671 {
672  if(this == &Source)
673  return(*this); // Special case of source same as target
674  if((!valuesCopied_) && (!Source.valuesCopied_) && (values_ == Source.values_))
675  return(*this); // Special case of both are views to same data.
676 
677  // If the source is a view then we will return a view, else we will return a copy.
678  if (!Source.valuesCopied_) {
679  if(valuesCopied_) {
680  // Clean up stored data if this was previously a copy.
681  deleteArrays();
682  }
683  numRows_ = Source.numRows_;
684  numCols_ = Source.numCols_;
685  stride_ = Source.stride_;
686  values_ = Source.values_;
687  }
688  else {
689  // If we were a view, we will now be a copy.
690  if(!valuesCopied_) {
691  numRows_ = Source.numRows_;
692  numCols_ = Source.numCols_;
693  stride_ = Source.numRows_;
694  const OrdinalType newsize = stride_ * numCols_;
695  if(newsize > 0) {
696  values_ = new ScalarType[newsize];
697  valuesCopied_ = true;
698  }
699  else {
700  values_ = 0;
701  }
702  }
703  // If we were a copy, we will stay a copy.
704  else {
705  if((Source.numRows_ <= stride_) && (Source.numCols_ == numCols_)) { // we don't need to reallocate
706  numRows_ = Source.numRows_;
707  numCols_ = Source.numCols_;
708  }
709  else { // we need to allocate more space (or less space)
710  deleteArrays();
711  numRows_ = Source.numRows_;
712  numCols_ = Source.numCols_;
713  stride_ = Source.numRows_;
714  const OrdinalType newsize = stride_ * numCols_;
715  if(newsize > 0) {
716  values_ = new ScalarType[newsize];
717  valuesCopied_ = true;
718  }
719  }
720  }
721  copyMat(Source.values_, Source.stride_, numRows_, numCols_, values_, stride_, 0, 0);
722  }
723  return(*this);
724 }
725 
726 template<typename OrdinalType, typename ScalarType>
728 {
729  // Check for compatible dimensions
730  if ((numRows_ != Source.numRows_) || (numCols_ != Source.numCols_))
731  {
732  TEUCHOS_CHK_REF(*this); // Return *this without altering it.
733  }
734  copyMat(Source.values_, Source.stride_, numRows_, numCols_, values_, stride_, 0, 0, ScalarTraits<ScalarType>::one());
735  return(*this);
736 }
737 
738 template<typename OrdinalType, typename ScalarType>
740 {
741  // Check for compatible dimensions
742  if ((numRows_ != Source.numRows_) || (numCols_ != Source.numCols_))
743  {
744  TEUCHOS_CHK_REF(*this); // Return *this without altering it.
745  }
746  copyMat(Source.values_, Source.stride_, numRows_, numCols_, values_, stride_, 0, 0, -ScalarTraits<ScalarType>::one());
747  return(*this);
748 }
749 
750 template<typename OrdinalType, typename ScalarType>
752  if(this == &Source)
753  return(*this); // Special case of source same as target
754  if((!valuesCopied_) && (!Source.valuesCopied_) && (values_ == Source.values_))
755  return(*this); // Special case of both are views to same data.
756 
757  // Check for compatible dimensions
758  if ((numRows_ != Source.numRows_) || (numCols_ != Source.numCols_))
759  {
760  TEUCHOS_CHK_REF(*this); // Return *this without altering it.
761  }
762  copyMat(Source.values_, Source.stride_, numRows_, numCols_, values_, stride_, 0, 0);
763  return(*this);
764 }
765 
766 //----------------------------------------------------------------------------------------------------
767 // Accessor methods
768 //----------------------------------------------------------------------------------------------------
769 
770 template<typename OrdinalType, typename ScalarType>
771 inline ScalarType& SerialDenseMatrix<OrdinalType, ScalarType>::operator () (OrdinalType rowIndex, OrdinalType colIndex)
772 {
773 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
774  checkIndex( rowIndex, colIndex );
775 #endif
776  return(values_[colIndex * stride_ + rowIndex]);
777 }
778 
779 template<typename OrdinalType, typename ScalarType>
780 inline const ScalarType& SerialDenseMatrix<OrdinalType, ScalarType>::operator () (OrdinalType rowIndex, OrdinalType colIndex) const
781 {
782 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
783  checkIndex( rowIndex, colIndex );
784 #endif
785  return(values_[colIndex * stride_ + rowIndex]);
786 }
787 
788 template<typename OrdinalType, typename ScalarType>
789 inline const ScalarType* SerialDenseMatrix<OrdinalType, ScalarType>::operator [] (OrdinalType colIndex) const
790 {
791 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
792  checkIndex( 0, colIndex );
793 #endif
794  return(values_ + colIndex * stride_);
795 }
796 
797 template<typename OrdinalType, typename ScalarType>
798 inline ScalarType* SerialDenseMatrix<OrdinalType, ScalarType>::operator [] (OrdinalType colIndex)
799 {
800 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
801  checkIndex( 0, colIndex );
802 #endif
803  return(values_ + colIndex * stride_);
804 }
805 
806 //----------------------------------------------------------------------------------------------------
807 // Norm methods
808 //----------------------------------------------------------------------------------------------------
809 
810 template<typename OrdinalType, typename ScalarType>
812 {
813  OrdinalType i, j;
816  ScalarType* ptr;
817  for(j = 0; j < numCols_; j++)
818  {
819  ScalarType sum = 0;
820  ptr = values_ + j * stride_;
821  for(i = 0; i < numRows_; i++)
822  {
824  }
826  if(absSum > anorm)
827  {
828  anorm = absSum;
829  }
830  }
831  // don't compute flop increment unless there is an accumulator
832  if (flopCounter_!=0) updateFlops(numRows_ * numCols_);
833  return(anorm);
834 }
835 
836 template<typename OrdinalType, typename ScalarType>
838 {
839  OrdinalType i, j;
841 
842  for (i = 0; i < numRows_; i++) {
844  for (j=0; j< numCols_; j++) {
845  sum += ScalarTraits<ScalarType>::magnitude(*(values_+i+j*stride_));
846  }
847  anorm = TEUCHOS_MAX( anorm, sum );
848  }
849  // don't compute flop increment unless there is an accumulator
850  if (flopCounter_!=0) updateFlops(numRows_ * numCols_);
851  return(anorm);
852 }
853 
854 template<typename OrdinalType, typename ScalarType>
856 {
857  OrdinalType i, j;
859  for (j = 0; j < numCols_; j++) {
860  for (i = 0; i < numRows_; i++) {
861  anorm += ScalarTraits<ScalarType>::magnitude(values_[i+j*stride_]*values_[i+j*stride_]);
862  }
863  }
865  // don't compute flop increment unless there is an accumulator
866  if (flopCounter_!=0) updateFlops(numRows_ * numCols_);
867  return(anorm);
868 }
869 
870 //----------------------------------------------------------------------------------------------------
871 // Comparison methods
872 //----------------------------------------------------------------------------------------------------
873 
874 template<typename OrdinalType, typename ScalarType>
876 {
877  bool result = 1;
878  if((numRows_ != Operand.numRows_) || (numCols_ != Operand.numCols_))
879  {
880  result = 0;
881  }
882  else
883  {
884  OrdinalType i, j;
885  for(i = 0; i < numRows_; i++)
886  {
887  for(j = 0; j < numCols_; j++)
888  {
889  if((*this)(i, j) != Operand(i, j))
890  {
891  return 0;
892  }
893  }
894  }
895  }
896  return result;
897 }
898 
899 template<typename OrdinalType, typename ScalarType>
901 {
902  return !((*this) == Operand);
903 }
904 
905 //----------------------------------------------------------------------------------------------------
906 // Multiplication method
907 //----------------------------------------------------------------------------------------------------
908 
909 template<typename OrdinalType, typename ScalarType>
911 {
912  this->scale( alpha );
913  return(*this);
914 }
915 
916 template<typename OrdinalType, typename ScalarType>
918 {
919  OrdinalType i, j;
920  ScalarType* ptr;
921 
922  for (j=0; j<numCols_; j++) {
923  ptr = values_ + j*stride_;
924  for (i=0; i<numRows_; i++) { *ptr = alpha * (*ptr); ptr++; }
925  }
926  // don't compute flop increment unless there is an accumulator
927  if (flopCounter_!=0) updateFlops( numRows_*numCols_ );
928  return(0);
929 }
930 
931 template<typename OrdinalType, typename ScalarType>
933 {
934  OrdinalType i, j;
935  ScalarType* ptr;
936 
937  // Check for compatible dimensions
938  if ((numRows_ != A.numRows_) || (numCols_ != A.numCols_))
939  {
940  TEUCHOS_CHK_ERR(-1); // Return error
941  }
942  for (j=0; j<numCols_; j++) {
943  ptr = values_ + j*stride_;
944  for (i=0; i<numRows_; i++) { *ptr = A(i,j) * (*ptr); ptr++; }
945  }
946  // don't compute flop increment unless there is an accumulator
947  if (flopCounter_!=0) updateFlops( numRows_*numCols_ );
948  return(0);
949 }
950 
951 template<typename OrdinalType, typename ScalarType>
953 {
954  // Check for compatible dimensions
955  OrdinalType A_nrows = (ETranspChar[transa]!='N') ? A.numCols() : A.numRows();
956  OrdinalType A_ncols = (ETranspChar[transa]!='N') ? A.numRows() : A.numCols();
957  OrdinalType B_nrows = (ETranspChar[transb]!='N') ? B.numCols() : B.numRows();
958  OrdinalType B_ncols = (ETranspChar[transb]!='N') ? B.numRows() : B.numCols();
959  if ((numRows_ != A_nrows) || (A_ncols != B_nrows) || (numCols_ != B_ncols))
960  {
961  TEUCHOS_CHK_ERR(-1); // Return error
962  }
963  // Call GEMM function
964  this->GEMM(transa, transb, numRows_, numCols_, A_ncols, alpha, A.values(), A.stride(), B.values(), B.stride(), beta, values_, stride_);
965 
966  // don't compute flop increment unless there is an accumulator
967  if (flopCounter_!=0) {
968  double nflops = 2 * numRows_;
969  nflops *= numCols_;
970  nflops *= A_ncols;
971  updateFlops(nflops);
972  }
973  return(0);
974 }
975 
976 template<typename OrdinalType, typename ScalarType>
978 {
979  // Check for compatible dimensions
980  OrdinalType A_nrows = A.numRows(), A_ncols = A.numCols();
981  OrdinalType B_nrows = B.numRows(), B_ncols = B.numCols();
982 
983  if (ESideChar[sideA]=='L') {
984  if ((numRows_ != A_nrows) || (A_ncols != B_nrows) || (numCols_ != B_ncols)) {
985  TEUCHOS_CHK_ERR(-1); // Return error
986  }
987  } else {
988  if ((numRows_ != B_nrows) || (B_ncols != A_nrows) || (numCols_ != A_ncols)) {
989  TEUCHOS_CHK_ERR(-1); // Return error
990  }
991  }
992 
993  // Call SYMM function
995  this->SYMM(sideA, uplo, numRows_, numCols_, alpha, A.values(), A.stride(), B.values(), B.stride(), beta, values_, stride_);
996 
997  // don't compute flop increment unless there is an accumulator
998  if (flopCounter_!=0) {
999  double nflops = 2 * numRows_;
1000  nflops *= numCols_;
1001  nflops *= A_ncols;
1002  updateFlops(nflops);
1003  }
1004  return(0);
1005 }
1006 
1007 template<typename OrdinalType, typename ScalarType>
1008 #ifndef TEUCHOS_HIDE_DEPRECATED_CODE
1010 #else
1011 std::ostream& SerialDenseMatrix<OrdinalType, ScalarType>::print(std::ostream& os) const
1012 #endif
1013 {
1014  os << std::endl;
1015  if(valuesCopied_)
1016  os << "Values_copied : yes" << std::endl;
1017  else
1018  os << "Values_copied : no" << std::endl;
1019  os << "Rows : " << numRows_ << std::endl;
1020  os << "Columns : " << numCols_ << std::endl;
1021  os << "LDA : " << stride_ << std::endl;
1022  if(numRows_ == 0 || numCols_ == 0) {
1023  os << "(matrix is empty, no values to display)" << std::endl;
1024  } else {
1025  for(OrdinalType i = 0; i < numRows_; i++) {
1026  for(OrdinalType j = 0; j < numCols_; j++){
1027  os << (*this)(i,j) << " ";
1028  }
1029  os << std::endl;
1030  }
1031  }
1032 #ifdef TEUCHOS_HIDE_DEPRECATED_CODE
1033  return os;
1034 #endif
1035 }
1036 
1037 //----------------------------------------------------------------------------------------------------
1038 // Protected methods
1039 //----------------------------------------------------------------------------------------------------
1040 
1041 template<typename OrdinalType, typename ScalarType>
1042 inline void SerialDenseMatrix<OrdinalType, ScalarType>::checkIndex( OrdinalType rowIndex, OrdinalType colIndex ) const {
1043  TEUCHOS_TEST_FOR_EXCEPTION(rowIndex < 0 || rowIndex >= numRows_, std::out_of_range,
1044  "SerialDenseMatrix<T>::checkIndex: "
1045  "Row index " << rowIndex << " out of range [0, "<< numRows_ << ")");
1046  TEUCHOS_TEST_FOR_EXCEPTION(colIndex < 0 || colIndex >= numCols_, std::out_of_range,
1047  "SerialDenseMatrix<T>::checkIndex: "
1048  "Col index " << colIndex << " out of range [0, "<< numCols_ << ")");
1049 }
1050 
1051 template<typename OrdinalType, typename ScalarType>
1052 void SerialDenseMatrix<OrdinalType, ScalarType>::deleteArrays(void)
1053 {
1054  if (valuesCopied_)
1055  {
1056  delete [] values_;
1057  values_ = 0;
1058  valuesCopied_ = false;
1059  }
1060 }
1061 
1062 template<typename OrdinalType, typename ScalarType>
1063 void SerialDenseMatrix<OrdinalType, ScalarType>::copyMat(
1064  ScalarType* inputMatrix, OrdinalType strideInput, OrdinalType numRows_in,
1065  OrdinalType numCols_in, ScalarType* outputMatrix, OrdinalType strideOutput,
1066  OrdinalType startRow, OrdinalType startCol, ScalarType alpha
1067  )
1068 {
1069  OrdinalType i, j;
1070  ScalarType* ptr1 = 0;
1071  ScalarType* ptr2 = 0;
1072  for(j = 0; j < numCols_in; j++) {
1073  ptr1 = outputMatrix + (j * strideOutput);
1074  ptr2 = inputMatrix + (j + startCol) * strideInput + startRow;
1075  if (alpha != Teuchos::ScalarTraits<ScalarType>::zero() ) {
1076  for(i = 0; i < numRows_in; i++)
1077  {
1078  *ptr1++ += alpha*(*ptr2++);
1079  }
1080  } else {
1081  for(i = 0; i < numRows_in; i++)
1082  {
1083  *ptr1++ = *ptr2++;
1084  }
1085  }
1086  }
1087 }
1088 
1089 #ifndef TEUCHOS_HIDE_DEPRECATED_CODE
1090 template<typename OrdinalType, typename ScalarType>
1092 std::ostream& operator<< (std::ostream& os, const Teuchos::SerialDenseMatrix<OrdinalType, ScalarType>& obj)
1093 {
1094  obj.print (os);
1095  return os;
1096 }
1097 #endif
1098 
1100 template<typename OrdinalType, typename ScalarType>
1102 public:
1106  : obj(obj_in) {}
1107 };
1108 
1110 template<typename OrdinalType, typename ScalarType>
1111 std::ostream&
1112 operator<<(std::ostream &out,
1114 {
1115  printer.obj.print(out);
1116  return out;
1117 }
1118 
1120 template<typename OrdinalType, typename ScalarType>
1121 SerialDenseMatrixPrinter<OrdinalType,ScalarType>
1123 {
1125 }
1126 
1127 
1128 } // namespace Teuchos
1129 
1130 
1131 #endif /* _TEUCHOS_SERIALDENSEMATRIX_HPP_ */
Teuchos::SerialDenseMatrix::operator==
bool operator==(const SerialDenseMatrix< OrdinalType, ScalarType > &Operand) const
Equality of two matrices.
Definition: Teuchos_SerialDenseMatrix.hpp:875
Teuchos::SerialDenseMatrix::swap
void swap(SerialDenseMatrix< OrdinalType, ScalarType > &B)
Swap values between this matrix and incoming matrix.
Definition: Teuchos_SerialDenseMatrix.hpp:628
Teuchos::SerialDenseMatrix::operator+=
SerialDenseMatrix< OrdinalType, ScalarType > & operator+=(const SerialDenseMatrix< OrdinalType, ScalarType > &Source)
Add another matrix to this matrix.
Definition: Teuchos_SerialDenseMatrix.hpp:727
Teuchos::SerialDenseMatrix::normOne
ScalarTraits< ScalarType >::magnitudeType normOne() const
Returns the 1-norm of the matrix.
Definition: Teuchos_SerialDenseMatrix.hpp:811
Teuchos_DataAccess.hpp
Teuchos::DataAccess Mode enumerable type.
Teuchos::printMat
SerialBandDenseMatrixPrinter< OrdinalType, ScalarType > printMat(const SerialBandDenseMatrix< OrdinalType, ScalarType > &obj)
Return SerialBandDenseMatrix ostream manipulator Use as:
Definition: Teuchos_SerialBandDenseMatrix.hpp:1117
Teuchos::SerialDenseMatrix::scalarType
ScalarType scalarType
Typedef for scalar type.
Definition: Teuchos_SerialDenseMatrix.hpp:76
Teuchos::ScalarTraits::magnitude
static magnitudeType magnitude(T a)
Returns the magnitudeType of the scalar type a.
Definition: Teuchos_ScalarTraitsDecl.hpp:132
Teuchos::ScalarTraits::zero
static T zero()
Returns representation of zero for this scalar type.
Definition: Teuchos_ScalarTraitsDecl.hpp:134
Teuchos::SerialDenseMatrix::numRows
OrdinalType numRows() const
Returns the row dimension of this matrix.
Definition: Teuchos_SerialDenseMatrix.hpp:359
Teuchos::DataAccess
DataAccess
Definition: Teuchos_DataAccess.hpp:60
Teuchos::ScalarTraits::random
static T random()
Returns a random number (between -one() and +one()) of this scalar type.
Definition: Teuchos_ScalarTraitsDecl.hpp:150
Teuchos::SerialDenseMatrix::operator=
SerialDenseMatrix< OrdinalType, ScalarType > & operator=(const SerialDenseMatrix< OrdinalType, ScalarType > &Source)
Copies values from one matrix to another.
Definition: Teuchos_SerialDenseMatrix.hpp:668
Teuchos::NO_TRANS
@ NO_TRANS
Definition: Teuchos_BLAS_types.hpp:94
Teuchos::SerialDenseMatrix::values
ScalarType * values() const
Data array access method.
Definition: Teuchos_SerialDenseMatrix.hpp:264
Teuchos::SerialDenseMatrix::operator!=
bool operator!=(const SerialDenseMatrix< OrdinalType, ScalarType > &Operand) const
Inequality of two matrices.
Definition: Teuchos_SerialDenseMatrix.hpp:900
Teuchos::BLAS
Templated BLAS wrapper.
Definition: Teuchos_BLAS.hpp:245
Teuchos::SerialDenseMatrixPrinter
Ostream manipulator for SerialDenseMatrix.
Definition: Teuchos_SerialDenseMatrix.hpp:1101
Teuchos::SerialSymDenseMatrix::numRows
OrdinalType numRows() const
Returns the row dimension of this matrix.
Definition: Teuchos_SerialSymDenseMatrix.hpp:376
Teuchos::ScalarTraits::conjugate
static T conjugate(T a)
Returns the conjugate of the scalar type a.
Definition: Teuchos_ScalarTraitsDecl.hpp:142
Teuchos::SerialSymDenseMatrix
This class creates and provides basic support for symmetric, positive-definite dense matrices of temp...
Definition: Teuchos_SerialSymDenseMatrix.hpp:124
Teuchos::SerialSymDenseMatrix::values
ScalarType * values() const
Returns the pointer to the ScalarType data array contained in the object.
Definition: Teuchos_SerialSymDenseMatrix.hpp:315
Teuchos::SerialDenseMatrix::putScalar
int putScalar(const ScalarType value=Teuchos::ScalarTraits< ScalarType >::zero())
Set all values in the matrix to a constant value.
Definition: Teuchos_SerialDenseMatrix.hpp:614
Teuchos::SerialDenseMatrix::operator()
ScalarType & operator()(OrdinalType rowIndex, OrdinalType colIndex)
Element access method (non-const).
Definition: Teuchos_SerialDenseMatrix.hpp:771
Teuchos::LOWER_TRI
@ LOWER_TRI
Definition: Teuchos_BLAS_types.hpp:101
Teuchos::SerialDenseMatrix::empty
bool empty() const
Returns whether this matrix is empty.
Definition: Teuchos_SerialDenseMatrix.hpp:368
Teuchos_CompObject.hpp
Object for storing data and providing functionality that is common to all computational classes.
Teuchos::SerialDenseMatrix::stride
OrdinalType stride() const
Returns the stride between the columns of this matrix in memory.
Definition: Teuchos_SerialDenseMatrix.hpp:365
Teuchos::SerialDenseMatrix::normFrobenius
ScalarTraits< ScalarType >::magnitudeType normFrobenius() const
Returns the Frobenius-norm of the matrix.
Definition: Teuchos_SerialDenseMatrix.hpp:855
Teuchos::SerialDenseMatrix::shape
int shape(OrdinalType numRows, OrdinalType numCols)
Shape method for changing the size of a SerialDenseMatrix, initializing entries to zero.
Definition: Teuchos_SerialDenseMatrix.hpp:553
Teuchos::SerialDenseMatrix::reshape
int reshape(OrdinalType numRows, OrdinalType numCols)
Reshaping method for changing the size of a SerialDenseMatrix, keeping the entries.
Definition: Teuchos_SerialDenseMatrix.hpp:582
Teuchos::SerialDenseMatrix::ordinalType
OrdinalType ordinalType
Typedef for ordinal type.
Definition: Teuchos_SerialDenseMatrix.hpp:74
Teuchos::SerialSymDenseMatrix::stride
OrdinalType stride() const
Returns the stride between the columns of this matrix in memory.
Definition: Teuchos_SerialSymDenseMatrix.hpp:382
Teuchos::SerialSymDenseMatrix::upper
bool upper() const
Returns true if upper triangular part of this matrix has and will be used.
Definition: Teuchos_SerialSymDenseMatrix.hpp:323
Teuchos::SerialDenseMatrix::~SerialDenseMatrix
virtual ~SerialDenseMatrix()
Destructor.
Definition: Teuchos_SerialDenseMatrix.hpp:543
Teuchos::SerialDenseMatrix::numCols
OrdinalType numCols() const
Returns the column dimension of this matrix.
Definition: Teuchos_SerialDenseMatrix.hpp:362
Teuchos::ScalarTraits
This structure defines some basic traits for a scalar field type.
Definition: Teuchos_ScalarTraitsDecl.hpp:91
Teuchos::CompObject
Functionality and data that is common to all computational classes.
Definition: Teuchos_CompObject.hpp:66
Teuchos_ConfigDefs.hpp
Teuchos header file which uses auto-configuration information to include necessary C++ headers.
Teuchos::SerialDenseMatrix::operator*=
SerialDenseMatrix< OrdinalType, ScalarType > & operator*=(const ScalarType alpha)
Scale this matrix by alpha; *this = alpha**this.
Definition: Teuchos_SerialDenseMatrix.hpp:910
Teuchos::SerialDenseMatrix::assign
SerialDenseMatrix< OrdinalType, ScalarType > & assign(const SerialDenseMatrix< OrdinalType, ScalarType > &Source)
Copies values from one matrix to another.
Definition: Teuchos_SerialDenseMatrix.hpp:751
Teuchos::SerialDenseMatrix::normInf
ScalarTraits< ScalarType >::magnitudeType normInf() const
Returns the Infinity-norm of the matrix.
Definition: Teuchos_SerialDenseMatrix.hpp:837
Teuchos::SerialDenseMatrix::scale
int scale(const ScalarType alpha)
Scale this matrix by alpha; *this = alpha**this.
Definition: Teuchos_SerialDenseMatrix.hpp:917
Teuchos::CONJ_TRANS
@ CONJ_TRANS
Definition: Teuchos_BLAS_types.hpp:96
Teuchos::SerialDenseMatrix::print
virtual void print(std::ostream &os) const
Print method. Defines the behavior of the std::ostream << operator.
Definition: Teuchos_SerialDenseMatrix.hpp:1009
Teuchos::SerialDenseMatrix::shapeUninitialized
int shapeUninitialized(OrdinalType numRows, OrdinalType numCols)
Same as shape() except leaves uninitialized.
Definition: Teuchos_SerialDenseMatrix.hpp:568
Teuchos_BLAS.hpp
Templated interface class to BLAS routines.
Teuchos_SerialSymDenseMatrix.hpp
Templated serial, dense, symmetric matrix class.
Teuchos::SerialDenseMatrix::operator-=
SerialDenseMatrix< OrdinalType, ScalarType > & operator-=(const SerialDenseMatrix< OrdinalType, ScalarType > &Source)
Subtract another matrix from this matrix.
Definition: Teuchos_SerialDenseMatrix.hpp:739
Teuchos::ESide
ESide
Definition: Teuchos_BLAS_types.hpp:88
Teuchos::SerialDenseMatrix
This class creates and provides basic support for dense rectangular matrix of templated type.
Definition: Teuchos_SerialDenseMatrix.hpp:70
Teuchos::SerialDenseMatrix::multiply
int multiply(ETransp transa, ETransp transb, ScalarType alpha, const SerialDenseMatrix< OrdinalType, ScalarType > &A, const SerialDenseMatrix< OrdinalType, ScalarType > &B, ScalarType beta)
Multiply A * B and add them to this; this = beta * this + alpha*A*B.
Definition: Teuchos_SerialDenseMatrix.hpp:952
Teuchos_ScalarTraits.hpp
Defines basic traits for the scalar field type.
Teuchos
The Teuchos namespace contains all of the classes, structs and enums used by Teuchos,...
Teuchos::EUplo
EUplo
Definition: Teuchos_BLAS_types.hpp:99
Teuchos::SerialDenseMatrix::operator[]
ScalarType * operator[](OrdinalType colIndex)
Column access method (non-const).
Definition: Teuchos_SerialDenseMatrix.hpp:798
TEUCHOS_TEST_FOR_EXCEPTION
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
Macro for throwing an exception with breakpointing to ease debugging.
Definition: Teuchos_TestForException.hpp:170
Teuchos::Copy
@ Copy
Definition: Teuchos_DataAccess.hpp:61
Teuchos::ETransp
ETransp
Definition: Teuchos_BLAS_types.hpp:93
Teuchos::SerialDenseMatrix::SerialDenseMatrix
SerialDenseMatrix()
Default Constructor.
Definition: Teuchos_SerialDenseMatrix.hpp:414
Teuchos::UPPER_TRI
@ UPPER_TRI
Definition: Teuchos_BLAS_types.hpp:100
Teuchos::SerialDenseMatrix::random
int random()
Set all values in the matrix to be random numbers.
Definition: Teuchos_SerialDenseMatrix.hpp:653
Teuchos::SerialSymDenseMatrix::numCols
OrdinalType numCols() const
Returns the column dimension of this matrix.
Definition: Teuchos_SerialSymDenseMatrix.hpp:379