Teuchos Package Browser (Single Doxygen Collection)  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 // Kris
43 // 06.18.03 -- Removed comments/documentation; file too hard to edit otherwise. Will replace later.
44 // -- Begin conversion from <ScalarType> template to <OrdinalType, ScalarType>
45 // 06.23.03 -- Finished conversion from <ScalarType> to <OrdinalType, ScalarType>
46 // -- Tpetra_DenseMatrix.cpp is now obsolete
47 // -- Added new constructor to allow construction of a submatrix
48 // -- Altered copyMat to enable its use in new constructor
49 // -- Commented out broken print() function
50 // -- Fixed oneNorm() (uninitialized return variable was causing erroneous results)
51 // 06.24.03 -- Minor formatting changes
52 // 07.01.03 -- Added TempPrint() function to temporarily take the place of print() and operator<< while I figure out how to fix them
53 // 07.02.03 -- Added operator== and operator!= to make testing programs easier to write/read. Implementation of == isn't the most
54 // efficient/robust, but it works. Will consider optimizing later.
55 // -- Warning! Constructor DenseMatrix(DataAccess, const DenseMatrix<OrdinalType, ScalarType> &, int, int, int, int) (the
56 // "submatrix grabber" constructor) does not work correctly when used with CV == View (always grabs submatrix from top
57 // left corner).
58 // 07.07.03 -- Constructor bug detailed above (07.02) is now corrected (hopefully).
59 // 07.08.03 -- Move into Teuchos package/namespace
60 
61 #ifndef _TEUCHOS_SERIALDENSEMATRIX_HPP_
62 #define _TEUCHOS_SERIALDENSEMATRIX_HPP_
63 
67 #include "Teuchos_CompObject.hpp"
68 #include "Teuchos_BLAS.hpp"
69 #include "Teuchos_ScalarTraits.hpp"
70 #include "Teuchos_DataAccess.hpp"
71 #include "Teuchos_ConfigDefs.hpp"
72 #include "Teuchos_Assert.hpp"
74 
83 namespace Teuchos {
84 
85 template<typename OrdinalType, typename ScalarType>
86 class SerialDenseMatrix : public CompObject, public Object, public BLAS<OrdinalType, ScalarType>
87 {
88 public:
89 
91  typedef OrdinalType ordinalType;
93  typedef ScalarType scalarType;
94 
96 
97 
99 
103 
105 
113  SerialDenseMatrix(OrdinalType numRows, OrdinalType numCols, bool zeroOut = true);
114 
116 
124  SerialDenseMatrix(DataAccess CV, ScalarType* values, OrdinalType stride, OrdinalType numRows, OrdinalType numCols);
125 
127 
133 
135 
147  SerialDenseMatrix(DataAccess CV, const SerialDenseMatrix<OrdinalType, ScalarType> &Source, OrdinalType numRows, OrdinalType numCols, OrdinalType startRow=0, OrdinalType startCol=0);
148 
150  virtual ~SerialDenseMatrix();
152 
154 
155 
166  int shape(OrdinalType numRows, OrdinalType numCols);
167 
169  int shapeUninitialized(OrdinalType numRows, OrdinalType numCols);
170 
172 
182  int reshape(OrdinalType numRows, OrdinalType numCols);
183 
184 
186 
188 
189 
191 
198 
200 
206 
208 
211  SerialDenseMatrix<OrdinalType, ScalarType>& operator= (const ScalarType value) { putScalar(value); return(*this); }
212 
214 
218  int putScalar( const ScalarType value = Teuchos::ScalarTraits<ScalarType>::zero() );
219 
221  int random();
222 
224 
226 
227 
229 
236  ScalarType& operator () (OrdinalType rowIndex, OrdinalType colIndex);
237 
239 
246  const ScalarType& operator () (OrdinalType rowIndex, OrdinalType colIndex) const;
247 
249 
256  ScalarType* operator [] (OrdinalType colIndex);
257 
259 
266  const ScalarType* operator [] (OrdinalType colIndex) const;
267 
269 
270  ScalarType* values() const { return(values_); }
271 
273 
275 
276 
278 
282 
284 
288 
290 
294 
296 
300  int scale ( const ScalarType alpha );
301 
303 
310 
312 
326  int multiply (ETransp transa, ETransp transb, ScalarType alpha, const SerialDenseMatrix<OrdinalType, ScalarType> &A, const SerialDenseMatrix<OrdinalType, ScalarType> &B, ScalarType beta);
327 
329 
340  int multiply (ESide sideA, ScalarType alpha, const SerialSymDenseMatrix<OrdinalType, ScalarType> &A, const SerialDenseMatrix<OrdinalType, ScalarType> &B, ScalarType beta);
341 
343 
345 
346 
348 
351  bool operator== (const SerialDenseMatrix<OrdinalType, ScalarType> &Operand) const;
352 
354 
357  bool operator!= (const SerialDenseMatrix<OrdinalType, ScalarType> &Operand) const;
358 
360 
362 
363 
365  OrdinalType numRows() const { return(numRows_); }
366 
368  OrdinalType numCols() const { return(numCols_); }
369 
371  OrdinalType stride() const { return(stride_); }
372 
374  bool empty() const { return(numRows_ == 0 || numCols_ == 0); }
376 
378 
379 
382 
385 
389 
391 
392  virtual void print(std::ostream& os) const;
394 
396 protected:
397  void copyMat(ScalarType* inputMatrix, OrdinalType strideInput,
398  OrdinalType numRows, OrdinalType numCols, ScalarType* outputMatrix,
399  OrdinalType strideOutput, OrdinalType startRow, OrdinalType startCol,
400  ScalarType alpha = ScalarTraits<ScalarType>::zero() );
401  void deleteArrays();
402  void checkIndex( OrdinalType rowIndex, OrdinalType colIndex = 0 ) const;
403  OrdinalType numRows_;
404  OrdinalType numCols_;
405  OrdinalType stride_;
407  ScalarType* values_;
408 
409 }; // class Teuchos_SerialDenseMatrix
410 
411 //----------------------------------------------------------------------------------------------------
412 // Constructors and Destructor
413 //----------------------------------------------------------------------------------------------------
414 
415 template<typename OrdinalType, typename ScalarType>
417  : CompObject(), Object("Teuchos::SerialDenseMatrix"), numRows_(0), numCols_(0), stride_(0), valuesCopied_(false), values_(0)
418 {}
419 
420 template<typename OrdinalType, typename ScalarType>
422  OrdinalType numRows_in, OrdinalType numCols_in, bool zeroOut
423  )
424  : CompObject(), Object("Teuchos::SerialDenseMatrix"), numRows_(numRows_in), numCols_(numCols_in), stride_(numRows_in)
425 {
426  values_ = new ScalarType[stride_*numCols_];
427  valuesCopied_ = true;
428  if (zeroOut == true)
429  putScalar();
430 }
431 
432 template<typename OrdinalType, typename ScalarType>
434  DataAccess CV, ScalarType* values_in, OrdinalType stride_in, OrdinalType numRows_in,
435  OrdinalType numCols_in
436  )
437  : CompObject(), Object("Teuchos::SerialDenseMatrix"), numRows_(numRows_in), numCols_(numCols_in), stride_(stride_in),
438  valuesCopied_(false), values_(values_in)
439 {
440  if(CV == Copy)
441  {
442  stride_ = numRows_;
443  values_ = new ScalarType[stride_*numCols_];
444  copyMat(values_in, stride_in, numRows_, numCols_, values_, stride_, 0, 0);
445  valuesCopied_ = true;
446  }
447 }
448 
449 template<typename OrdinalType, typename ScalarType>
450 SerialDenseMatrix<OrdinalType, ScalarType>::SerialDenseMatrix(const SerialDenseMatrix<OrdinalType, ScalarType> &Source, ETransp trans) : CompObject(), Object("Teuchos::SerialDenseMatrix"), BLAS<OrdinalType,ScalarType>(), numRows_(0), numCols_(0), stride_(0), valuesCopied_(true), values_(0)
451 {
452  if ( trans == Teuchos::NO_TRANS )
453  {
454  numRows_ = Source.numRows_;
455  numCols_ = Source.numCols_;
456  stride_ = numRows_;
457  values_ = new ScalarType[stride_*numCols_];
458  copyMat(Source.values_, Source.stride_, numRows_, numCols_, values_, stride_, 0, 0);
459  }
461  {
462  numRows_ = Source.numCols_;
463  numCols_ = Source.numRows_;
464  stride_ = numRows_;
465  values_ = new ScalarType[stride_*numCols_];
466  for (OrdinalType j=0; j<numCols_; j++) {
467  for (OrdinalType i=0; i<numRows_; i++) {
469  }
470  }
471  }
472  else
473  {
474  numRows_ = Source.numCols_;
475  numCols_ = Source.numRows_;
476  stride_ = numRows_;
477  values_ = new ScalarType[stride_*numCols_];
478  for (OrdinalType j=0; j<numCols_; j++) {
479  for (OrdinalType i=0; i<numRows_; i++) {
480  values_[j*stride_ + i] = Source.values_[i*Source.stride_ + j];
481  }
482  }
483  }
484 }
485 
486 template<typename OrdinalType, typename ScalarType>
489  OrdinalType numRows_in, OrdinalType numCols_in, OrdinalType startRow,
490  OrdinalType startCol
491  )
492  : CompObject(), Object("Teuchos::SerialDenseMatrix"), numRows_(numRows_in), numCols_(numCols_in), stride_(Source.stride_),
493  valuesCopied_(false), values_(Source.values_)
494 {
495  if(CV == Copy)
496  {
497  stride_ = numRows_in;
498  values_ = new ScalarType[stride_ * numCols_in];
499  copyMat(Source.values_, Source.stride_, numRows_in, numCols_in, values_, stride_, startRow, startCol);
500  valuesCopied_ = true;
501  }
502  else // CV == View
503  {
504  values_ = values_ + (stride_ * startCol) + startRow;
505  }
506 }
507 
508 template<typename OrdinalType, typename ScalarType>
510 {
511  deleteArrays();
512 }
513 
514 //----------------------------------------------------------------------------------------------------
515 // Shape methods
516 //----------------------------------------------------------------------------------------------------
517 
518 template<typename OrdinalType, typename ScalarType>
520  OrdinalType numRows_in, OrdinalType numCols_in
521  )
522 {
523  deleteArrays(); // Get rid of anything that might be already allocated
524  numRows_ = numRows_in;
525  numCols_ = numCols_in;
526  stride_ = numRows_;
527  values_ = new ScalarType[stride_*numCols_];
528  putScalar();
529  valuesCopied_ = true;
530  return(0);
531 }
532 
533 template<typename OrdinalType, typename ScalarType>
535  OrdinalType numRows_in, OrdinalType numCols_in
536  )
537 {
538  deleteArrays(); // Get rid of anything that might be already allocated
539  numRows_ = numRows_in;
540  numCols_ = numCols_in;
541  stride_ = numRows_;
542  values_ = new ScalarType[stride_*numCols_];
543  valuesCopied_ = true;
544  return(0);
545 }
546 
547 template<typename OrdinalType, typename ScalarType>
549  OrdinalType numRows_in, OrdinalType numCols_in
550  )
551 {
552  // Allocate space for new matrix
553  ScalarType* values_tmp = new ScalarType[numRows_in * numCols_in];
554  ScalarType zero = ScalarTraits<ScalarType>::zero();
555  for(OrdinalType k = 0; k < numRows_in * numCols_in; k++)
556  {
557  values_tmp[k] = zero;
558  }
559  OrdinalType numRows_tmp = TEUCHOS_MIN(numRows_, numRows_in);
560  OrdinalType numCols_tmp = TEUCHOS_MIN(numCols_, numCols_in);
561  if(values_ != 0)
562  {
563  copyMat(values_, stride_, numRows_tmp, numCols_tmp, values_tmp,
564  numRows_in, 0, 0); // Copy principal submatrix of A to new A
565  }
566  deleteArrays(); // Get rid of anything that might be already allocated
567  numRows_ = numRows_in;
568  numCols_ = numCols_in;
569  stride_ = numRows_;
570  values_ = values_tmp; // Set pointer to new A
571  valuesCopied_ = true;
572  return(0);
573 }
574 
575 //----------------------------------------------------------------------------------------------------
576 // Set methods
577 //----------------------------------------------------------------------------------------------------
578 
579 template<typename OrdinalType, typename ScalarType>
581 {
582  // Set each value of the dense matrix to "value".
583  for(OrdinalType j = 0; j < numCols_; j++)
584  {
585  for(OrdinalType i = 0; i < numRows_; i++)
586  {
587  values_[i + j*stride_] = value_in;
588  }
589  }
590  return 0;
591 }
592 
593 template<typename OrdinalType, typename ScalarType>
595 {
596  // Set each value of the dense matrix to a random value.
597  for(OrdinalType j = 0; j < numCols_; j++)
598  {
599  for(OrdinalType i = 0; i < numRows_; i++)
600  {
602  }
603  }
604  return 0;
605 }
606 
607 template<typename OrdinalType, typename ScalarType>
611  )
612 {
613  if(this == &Source)
614  return(*this); // Special case of source same as target
615  if((!valuesCopied_) && (!Source.valuesCopied_) && (values_ == Source.values_))
616  return(*this); // Special case of both are views to same data.
617 
618  // If the source is a view then we will return a view, else we will return a copy.
619  if (!Source.valuesCopied_) {
620  if(valuesCopied_) {
621  // Clean up stored data if this was previously a copy.
622  deleteArrays();
623  }
624  numRows_ = Source.numRows_;
625  numCols_ = Source.numCols_;
626  stride_ = Source.stride_;
627  values_ = Source.values_;
628  }
629  else {
630  // If we were a view, we will now be a copy.
631  if(!valuesCopied_) {
632  numRows_ = Source.numRows_;
633  numCols_ = Source.numCols_;
634  stride_ = Source.numRows_;
635  const OrdinalType newsize = stride_ * numCols_;
636  if(newsize > 0) {
637  values_ = new ScalarType[newsize];
638  valuesCopied_ = true;
639  }
640  else {
641  values_ = 0;
642  }
643  }
644  // If we were a copy, we will stay a copy.
645  else {
646  if((Source.numRows_ <= stride_) && (Source.numCols_ == numCols_)) { // we don't need to reallocate
647  numRows_ = Source.numRows_;
648  numCols_ = Source.numCols_;
649  }
650  else { // we need to allocate more space (or less space)
651  deleteArrays();
652  numRows_ = Source.numRows_;
653  numCols_ = Source.numCols_;
654  stride_ = Source.numRows_;
655  const OrdinalType newsize = stride_ * numCols_;
656  if(newsize > 0) {
657  values_ = new ScalarType[newsize];
658  valuesCopied_ = true;
659  }
660  }
661  }
662  copyMat(Source.values_, Source.stride_, numRows_, numCols_, values_, stride_, 0, 0);
663  }
664  return(*this);
665 }
666 
667 template<typename OrdinalType, typename ScalarType>
669 {
670  // Check for compatible dimensions
671  if ((numRows_ != Source.numRows_) || (numCols_ != Source.numCols_))
672  {
673  TEUCHOS_CHK_REF(*this); // Return *this without altering it.
674  }
676  return(*this);
677 }
678 
679 template<typename OrdinalType, typename ScalarType>
681 {
682  // Check for compatible dimensions
683  if ((numRows_ != Source.numRows_) || (numCols_ != Source.numCols_))
684  {
685  TEUCHOS_CHK_REF(*this); // Return *this without altering it.
686  }
688  return(*this);
689 }
690 
691 template<typename OrdinalType, typename ScalarType>
693  if(this == &Source)
694  return(*this); // Special case of source same as target
695  if((!valuesCopied_) && (!Source.valuesCopied_) && (values_ == Source.values_))
696  return(*this); // Special case of both are views to same data.
697 
698  // Check for compatible dimensions
699  if ((numRows_ != Source.numRows_) || (numCols_ != Source.numCols_))
700  {
701  TEUCHOS_CHK_REF(*this); // Return *this without altering it.
702  }
703  copyMat(Source.values_, Source.stride_, numRows_, numCols_, values_, stride_, 0, 0);
704  return(*this);
705 }
706 
707 //----------------------------------------------------------------------------------------------------
708 // Accessor methods
709 //----------------------------------------------------------------------------------------------------
710 
711 template<typename OrdinalType, typename ScalarType>
712 inline ScalarType& SerialDenseMatrix<OrdinalType, ScalarType>::operator () (OrdinalType rowIndex, OrdinalType colIndex)
713 {
714 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
715  checkIndex( rowIndex, colIndex );
716 #endif
717  return(values_[colIndex * stride_ + rowIndex]);
718 }
719 
720 template<typename OrdinalType, typename ScalarType>
721 inline const ScalarType& SerialDenseMatrix<OrdinalType, ScalarType>::operator () (OrdinalType rowIndex, OrdinalType colIndex) const
722 {
723 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
724  checkIndex( rowIndex, colIndex );
725 #endif
726  return(values_[colIndex * stride_ + rowIndex]);
727 }
728 
729 template<typename OrdinalType, typename ScalarType>
730 inline const ScalarType* SerialDenseMatrix<OrdinalType, ScalarType>::operator [] (OrdinalType colIndex) const
731 {
732 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
733  checkIndex( 0, colIndex );
734 #endif
735  return(values_ + colIndex * stride_);
736 }
737 
738 template<typename OrdinalType, typename ScalarType>
739 inline ScalarType* SerialDenseMatrix<OrdinalType, ScalarType>::operator [] (OrdinalType colIndex)
740 {
741 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
742  checkIndex( 0, colIndex );
743 #endif
744  return(values_ + colIndex * stride_);
745 }
746 
747 //----------------------------------------------------------------------------------------------------
748 // Norm methods
749 //----------------------------------------------------------------------------------------------------
750 
751 template<typename OrdinalType, typename ScalarType>
753 {
754  OrdinalType i, j;
757  ScalarType* ptr;
758  for(j = 0; j < numCols_; j++)
759  {
760  ScalarType sum = 0;
761  ptr = values_ + j * stride_;
762  for(i = 0; i < numRows_; i++)
763  {
765  }
767  if(absSum > anorm)
768  {
769  anorm = absSum;
770  }
771  }
772  updateFlops(numRows_ * numCols_);
773  return(anorm);
774 }
775 
776 template<typename OrdinalType, typename ScalarType>
778 {
779  OrdinalType i, j;
781 
782  for (i = 0; i < numRows_; i++) {
784  for (j=0; j< numCols_; j++) {
786  }
787  anorm = TEUCHOS_MAX( anorm, sum );
788  }
789  updateFlops(numRows_ * numCols_);
790  return(anorm);
791 }
792 
793 template<typename OrdinalType, typename ScalarType>
795 {
796  OrdinalType i, j;
798  for (j = 0; j < numCols_; j++) {
799  for (i = 0; i < numRows_; i++) {
801  }
802  }
804  updateFlops(numRows_ * numCols_);
805  return(anorm);
806 }
807 
808 //----------------------------------------------------------------------------------------------------
809 // Comparison methods
810 //----------------------------------------------------------------------------------------------------
811 
812 template<typename OrdinalType, typename ScalarType>
814 {
815  bool result = 1;
816  if((numRows_ != Operand.numRows_) || (numCols_ != Operand.numCols_))
817  {
818  result = 0;
819  }
820  else
821  {
822  OrdinalType i, j;
823  for(i = 0; i < numRows_; i++)
824  {
825  for(j = 0; j < numCols_; j++)
826  {
827  if((*this)(i, j) != Operand(i, j))
828  {
829  return 0;
830  }
831  }
832  }
833  }
834  return result;
835 }
836 
837 template<typename OrdinalType, typename ScalarType>
839 {
840  return !((*this) == Operand);
841 }
842 
843 //----------------------------------------------------------------------------------------------------
844 // Multiplication method
845 //----------------------------------------------------------------------------------------------------
846 
847 template<typename OrdinalType, typename ScalarType>
849 {
850  this->scale( alpha );
851  return(*this);
852 }
853 
854 template<typename OrdinalType, typename ScalarType>
856 {
857  OrdinalType i, j;
858  ScalarType* ptr;
859 
860  for (j=0; j<numCols_; j++) {
861  ptr = values_ + j*stride_;
862  for (i=0; i<numRows_; i++) { *ptr = alpha * (*ptr); ptr++; }
863  }
864  updateFlops( numRows_*numCols_ );
865  return(0);
866 }
867 
868 template<typename OrdinalType, typename ScalarType>
870 {
871  OrdinalType i, j;
872  ScalarType* ptr;
873 
874  // Check for compatible dimensions
875  if ((numRows_ != A.numRows_) || (numCols_ != A.numCols_))
876  {
877  TEUCHOS_CHK_ERR(-1); // Return error
878  }
879  for (j=0; j<numCols_; j++) {
880  ptr = values_ + j*stride_;
881  for (i=0; i<numRows_; i++) { *ptr = A(i,j) * (*ptr); ptr++; }
882  }
883  updateFlops( numRows_*numCols_ );
884  return(0);
885 }
886 
887 template<typename OrdinalType, typename ScalarType>
889 {
890  // Check for compatible dimensions
891  OrdinalType A_nrows = (ETranspChar[transa]!='N') ? A.numCols() : A.numRows();
892  OrdinalType A_ncols = (ETranspChar[transa]!='N') ? A.numRows() : A.numCols();
893  OrdinalType B_nrows = (ETranspChar[transb]!='N') ? B.numCols() : B.numRows();
894  OrdinalType B_ncols = (ETranspChar[transb]!='N') ? B.numRows() : B.numCols();
895  if ((numRows_ != A_nrows) || (A_ncols != B_nrows) || (numCols_ != B_ncols))
896  {
897  TEUCHOS_CHK_ERR(-1); // Return error
898  }
899  // Call GEMM function
900  this->GEMM(transa, transb, numRows_, numCols_, A_ncols, alpha, A.values(), A.stride(), B.values(), B.stride(), beta, values_, stride_);
901  double nflops = 2 * numRows_;
902  nflops *= numCols_;
903  nflops *= A_ncols;
904  updateFlops(nflops);
905  return(0);
906 }
907 
908 template<typename OrdinalType, typename ScalarType>
910 {
911  // Check for compatible dimensions
912  OrdinalType A_nrows = A.numRows(), A_ncols = A.numCols();
913  OrdinalType B_nrows = B.numRows(), B_ncols = B.numCols();
914 
915  if (ESideChar[sideA]=='L') {
916  if ((numRows_ != A_nrows) || (A_ncols != B_nrows) || (numCols_ != B_ncols)) {
917  TEUCHOS_CHK_ERR(-1); // Return error
918  }
919  } else {
920  if ((numRows_ != B_nrows) || (B_ncols != A_nrows) || (numCols_ != A_ncols)) {
921  TEUCHOS_CHK_ERR(-1); // Return error
922  }
923  }
924 
925  // Call SYMM function
927  this->SYMM(sideA, uplo, numRows_, numCols_, alpha, A.values(), A.stride(), B.values(), B.stride(), beta, values_, stride_);
928  double nflops = 2 * numRows_;
929  nflops *= numCols_;
930  nflops *= A_ncols;
931  updateFlops(nflops);
932  return(0);
933 }
934 
935 template<typename OrdinalType, typename ScalarType>
937 {
938  os << std::endl;
939  if(valuesCopied_)
940  os << "Values_copied : yes" << std::endl;
941  else
942  os << "Values_copied : no" << std::endl;
943  os << "Rows : " << numRows_ << std::endl;
944  os << "Columns : " << numCols_ << std::endl;
945  os << "LDA : " << stride_ << std::endl;
946  if(numRows_ == 0 || numCols_ == 0) {
947  os << "(matrix is empty, no values to display)" << std::endl;
948  } else {
949  for(OrdinalType i = 0; i < numRows_; i++) {
950  for(OrdinalType j = 0; j < numCols_; j++){
951  os << (*this)(i,j) << " ";
952  }
953  os << std::endl;
954  }
955  }
956 }
957 
958 //----------------------------------------------------------------------------------------------------
959 // Protected methods
960 //----------------------------------------------------------------------------------------------------
961 
962 template<typename OrdinalType, typename ScalarType>
963 inline void SerialDenseMatrix<OrdinalType, ScalarType>::checkIndex( OrdinalType rowIndex, OrdinalType colIndex ) const {
964  TEUCHOS_TEST_FOR_EXCEPTION(rowIndex < 0 || rowIndex >= numRows_, std::out_of_range,
965  "SerialDenseMatrix<T>::checkIndex: "
966  "Row index " << rowIndex << " out of range [0, "<< numRows_ << ")");
967  TEUCHOS_TEST_FOR_EXCEPTION(colIndex < 0 || colIndex >= numCols_, std::out_of_range,
968  "SerialDenseMatrix<T>::checkIndex: "
969  "Col index " << colIndex << " out of range [0, "<< numCols_ << ")");
970 }
971 
972 template<typename OrdinalType, typename ScalarType>
974 {
975  if (valuesCopied_)
976  {
977  delete [] values_;
978  values_ = 0;
979  valuesCopied_ = false;
980  }
981 }
982 
983 template<typename OrdinalType, typename ScalarType>
985  ScalarType* inputMatrix, OrdinalType strideInput, OrdinalType numRows_in,
986  OrdinalType numCols_in, ScalarType* outputMatrix, OrdinalType strideOutput,
987  OrdinalType startRow, OrdinalType startCol, ScalarType alpha
988  )
989 {
990  OrdinalType i, j;
991  ScalarType* ptr1 = 0;
992  ScalarType* ptr2 = 0;
993  for(j = 0; j < numCols_in; j++) {
994  ptr1 = outputMatrix + (j * strideOutput);
995  ptr2 = inputMatrix + (j + startCol) * strideInput + startRow;
996  if (alpha != Teuchos::ScalarTraits<ScalarType>::zero() ) {
997  for(i = 0; i < numRows_in; i++)
998  {
999  *ptr1++ += alpha*(*ptr2++);
1000  }
1001  } else {
1002  for(i = 0; i < numRows_in; i++)
1003  {
1004  *ptr1++ = *ptr2++;
1005  }
1006  }
1007  }
1008 }
1009 
1010 } // namespace Teuchos
1011 
1012 
1013 #endif /* _TEUCHOS_SERIALDENSEMATRIX_HPP_ */
ScalarTraits< ScalarType >::magnitudeType normOne() const
Returns the 1-norm of the matrix.
ScalarType * values() const
Data array access method.
bool upper() const
Returns true if upper triangular part of this matrix has and will be used.
void checkIndex(OrdinalType rowIndex, OrdinalType colIndex=0) const
bool operator==(const SerialDenseMatrix< OrdinalType, ScalarType > &Operand) const
Equality of two matrices.
int shapeUninitialized(OrdinalType numRows, OrdinalType numCols)
Same as shape() except leaves uninitialized.
virtual void print(std::ostream &os) const
Print method. Defines the behavior of the std::ostream << operator inherited from the Object class...
Templated interface class to BLAS routines.
SerialDenseMatrix< OrdinalType, ScalarType > & operator-=(const SerialDenseMatrix< OrdinalType, ScalarType > &Source)
Subtract another matrix from this matrix.
SerialDenseMatrix< OrdinalType, ScalarType > & operator*=(const ScalarType alpha)
Scale this matrix by alpha; *this = alpha**this.
ScalarType * values() const
Returns the pointer to the ScalarType data array contained in the object.
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.
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
Macro for throwing an exception with breakpointing to ease debugging.
Teuchos header file which uses auto-configuration information to include necessary C++ headers...
Templated BLAS wrapper.
This class creates and provides basic support for symmetric, positive-definite dense matrices of temp...
int scale(const ScalarType alpha)
Scale this matrix by alpha; *this = alpha**this.
OrdinalType numRows() const
Returns the row dimension of this matrix.
Object for storing data and providing functionality that is common to all computational classes...
ScalarTraits< ScalarType >::magnitudeType normFrobenius() const
Returns the Frobenius-norm of the matrix.
This structure defines some basic traits for a scalar field type.
SerialDenseMatrix< OrdinalType, ScalarType > & operator+=(const SerialDenseMatrix< OrdinalType, ScalarType > &Source)
Add another matrix to this matrix.
#define TEUCHOS_CHK_ERR(a)
void SYMM(ESide side, EUplo uplo, const OrdinalType m, const OrdinalType n, const alpha_type alpha, const A_type *A, const OrdinalType lda, const B_type *B, const OrdinalType ldb, const beta_type beta, ScalarType *C, const OrdinalType ldc) const
Performs the matrix-matrix operation: C <- alpha*A*B+beta*C or C <- alpha*B*A+beta*C where A is an m ...
SerialDenseMatrix< OrdinalType, ScalarType > & operator=(const SerialDenseMatrix< OrdinalType, ScalarType > &Source)
Copies values from one matrix to another.
Functionality and data that is common to all computational classes.
static T conjugate(T a)
Returns the conjugate of the scalar type a.
bool empty() const
Returns whether this matrix is empty.
ScalarType scalarType
Typedef for scalar type.
The base Teuchos class.
int putScalar(const ScalarType value=Teuchos::ScalarTraits< ScalarType >::zero())
Set all values in the matrix to a constant value.
void GEMM(ETransp transa, ETransp transb, const OrdinalType m, const OrdinalType n, const OrdinalType k, const alpha_type alpha, const A_type *A, const OrdinalType lda, const B_type *B, const OrdinalType ldb, const beta_type beta, ScalarType *C, const OrdinalType ldc) const
General matrix-matrix multiply.
#define TEUCHOS_MAX(x, y)
SerialDenseMatrix()
Default Constructor.
bool operator!=(const SerialDenseMatrix< OrdinalType, ScalarType > &Operand) const
Inequality of two matrices.
TEUCHOSNUMERICS_LIB_DLL_EXPORT const char ESideChar[]
ScalarType & operator()(OrdinalType rowIndex, OrdinalType colIndex)
Element access method (non-const).
void copyMat(ScalarType *inputMatrix, OrdinalType strideInput, OrdinalType numRows, OrdinalType numCols, ScalarType *outputMatrix, OrdinalType strideOutput, OrdinalType startRow, OrdinalType startCol, ScalarType alpha=ScalarTraits< ScalarType >::zero())
Templated serial, dense, symmetric matrix class.
OrdinalType ordinalType
Typedef for ordinal type.
ScalarType * operator[](OrdinalType colIndex)
Column access method (non-const).
static magnitudeType magnitude(T a)
Returns the magnitudeType of the scalar type a.
OrdinalType numCols() const
Returns the column dimension of this matrix.
ScalarTraits< ScalarType >::magnitudeType normInf() const
Returns the Infinity-norm of the matrix.
int random()
Set all values in the matrix to be random numbers.
int reshape(OrdinalType numRows, OrdinalType numCols)
Reshaping method for changing the size of a SerialDenseMatrix, keeping the entries.
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.
TEUCHOSNUMERICS_LIB_DLL_EXPORT const char ETranspChar[]
static T random()
Returns a random number (between -one() and +one()) of this scalar type.
OrdinalType stride() const
Returns the stride between the columns of this matrix in memory.
int shape(OrdinalType numRows, OrdinalType numCols)
Shape method for changing the size of a SerialDenseMatrix, initializing entries to zero...
OrdinalType numCols() const
Returns the column dimension of this matrix.
#define TEUCHOS_CHK_REF(a)
#define TEUCHOS_MIN(x, y)
SerialDenseMatrix< OrdinalType, ScalarType > & assign(const SerialDenseMatrix< OrdinalType, ScalarType > &Source)
Copies values from one matrix to another.
Teuchos::DataAccess Mode enumerable type.
OrdinalType stride() const
Returns the stride between the columns of this matrix in memory.
OrdinalType numRows() const
Returns the row dimension of this matrix.
This class creates and provides basic support for dense rectangular matrix of templated type...