Teuchos Package Browser (Single Doxygen Collection)  Version of the Day
Teuchos_SerialSymDenseMatrix.hpp
Go to the documentation of this file.
1 
2 // @HEADER
3 // ***********************************************************************
4 //
5 // Teuchos: Common Tools Package
6 // Copyright (2004) Sandia Corporation
7 //
8 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
9 // license for use of this work by or on behalf of the U.S. Government.
10 //
11 // Redistribution and use in source and binary forms, with or without
12 // modification, are permitted provided that the following conditions are
13 // met:
14 //
15 // 1. Redistributions of source code must retain the above copyright
16 // notice, this list of conditions and the following disclaimer.
17 //
18 // 2. Redistributions in binary form must reproduce the above copyright
19 // notice, this list of conditions and the following disclaimer in the
20 // documentation and/or other materials provided with the distribution.
21 //
22 // 3. Neither the name of the Corporation nor the names of the
23 // contributors may be used to endorse or promote products derived from
24 // this software without specific prior written permission.
25 //
26 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
27 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
30 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
31 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
32 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
33 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
36 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 //
38 // Questions? Contact Michael A. Heroux (maherou@sandia.gov)
39 //
40 // ***********************************************************************
41 // @HEADER
42 
43 #ifndef _TEUCHOS_SERIALSYMDENSEMATRIX_HPP_
44 #define _TEUCHOS_SERIALSYMDENSEMATRIX_HPP_
45 
49 #include "Teuchos_CompObject.hpp"
50 #include "Teuchos_BLAS.hpp"
51 #include "Teuchos_ScalarTraits.hpp"
52 #include "Teuchos_DataAccess.hpp"
53 #include "Teuchos_ConfigDefs.hpp"
54 #include "Teuchos_Assert.hpp"
55 
118 namespace Teuchos {
119 
120 template<typename OrdinalType, typename ScalarType>
121 class SerialSymDenseMatrix : public CompObject, public Object, public BLAS<OrdinalType,ScalarType>
122 {
123  public:
124 
126  typedef OrdinalType ordinalType;
128  typedef ScalarType scalarType;
129 
131 
132 
142 
144 
154  SerialSymDenseMatrix(OrdinalType numRowsCols, bool zeroOut = true);
155 
157 
168  SerialSymDenseMatrix(DataAccess CV, bool upper, ScalarType* values, OrdinalType stride, OrdinalType numRowsCols);
169 
172 
174 
183  SerialSymDenseMatrix(DataAccess CV, const SerialSymDenseMatrix<OrdinalType, ScalarType> &Source, OrdinalType numRowCols, OrdinalType startRowCol=0);
184 
186  virtual ~SerialSymDenseMatrix ();
188 
190 
191 
193 
202  int shape(OrdinalType numRowsCols);
203 
205 
214  int shapeUninitialized(OrdinalType numRowsCols);
215 
217 
227  int reshape(OrdinalType numRowsCols);
228 
230 
232  void setLower();
233 
235 
237  void setUpper();
238 
240 
242 
243 
245 
252 
254 
259 
261 
264  SerialSymDenseMatrix<OrdinalType, ScalarType>& operator= (const ScalarType value) { putScalar(value); return(*this); }
265 
267 
272  int putScalar( const ScalarType value = Teuchos::ScalarTraits<ScalarType>::zero(), bool fullMatrix = false );
273 
275 
277  int random( const ScalarType bias = 0.1*Teuchos::ScalarTraits<ScalarType>::one() );
278 
280 
282 
283 
285 
292  ScalarType& operator () (OrdinalType rowIndex, OrdinalType colIndex);
293 
295 
302  const ScalarType& operator () (OrdinalType rowIndex, OrdinalType colIndex) const;
303 
305 
307  ScalarType* values() const { return(values_); }
308 
310 
312 
313 
315  bool upper() const {return(upper_);};
316 
318  char UPLO() const {return(UPLO_);};
320 
322 
323 
325 
332 
334 
338 
340 
344 
346 
348 
349 
351 
355 
357 
361 
363 
365 
366 
368  OrdinalType numRows() const { return(numRowCols_); }
369 
371  OrdinalType numCols() const { return(numRowCols_); }
372 
374  OrdinalType stride() const { return(stride_); }
375 
377  bool empty() const { return(numRowCols_ == 0); }
378 
380 
382 
383 
386 
389 
393 
395 
396  virtual void print(std::ostream& os) const;
398 
400 
401  protected:
402 
403  // In-place scaling of matrix.
404  void scale( const ScalarType alpha );
405 
406  // Copy the values from one matrix to the other.
407  void copyMat(bool inputUpper, ScalarType* inputMatrix, OrdinalType inputStride,
408  OrdinalType numRowCols, bool outputUpper, ScalarType* outputMatrix,
409  OrdinalType outputStride, OrdinalType startRowCol,
410  ScalarType alpha = ScalarTraits<ScalarType>::zero() );
411 
412  // Copy the values from the active triangle of the matrix to the other to make the matrix full symmetric.
413  void copyUPLOMat(bool inputUpper, ScalarType* inputMatrix,
414  OrdinalType inputStride, OrdinalType inputRows);
415 
416  void deleteArrays();
417  void checkIndex( OrdinalType rowIndex, OrdinalType colIndex = 0 ) const;
418  OrdinalType numRowCols_;
419  OrdinalType stride_;
421  ScalarType* values_;
422  bool upper_;
423  char UPLO_;
424 
425 
426 };
427 
428 //----------------------------------------------------------------------------------------------------
429 // Constructors and Destructor
430 //----------------------------------------------------------------------------------------------------
431 
432 template<typename OrdinalType, typename ScalarType>
434  : CompObject(), Object("Teuchos::SerialSymDenseMatrix"), numRowCols_(0), stride_(0), valuesCopied_(false), values_(0), upper_(false), UPLO_('L')
435 {}
436 
437 template<typename OrdinalType, typename ScalarType>
439  : CompObject(), Object("Teuchos::SerialSymDenseMatrix"), numRowCols_(numRowCols_in), stride_(numRowCols_in), valuesCopied_(false), upper_(false), UPLO_('L')
440 {
441  values_ = new ScalarType[stride_*numRowCols_];
442  valuesCopied_ = true;
443  if (zeroOut == true)
445 }
446 
447 template<typename OrdinalType, typename ScalarType>
449  DataAccess CV, bool upper_in, ScalarType* values_in, OrdinalType stride_in, OrdinalType numRowCols_in
450  )
451  : CompObject(), Object("Teuchos::SerialSymDenseMatrix"), numRowCols_(numRowCols_in), stride_(stride_in), valuesCopied_(false),
452  values_(values_in), upper_(upper_in)
453 {
454  if (upper_)
455  UPLO_ = 'U';
456  else
457  UPLO_ = 'L';
458 
459  if(CV == Copy)
460  {
462  values_ = new ScalarType[stride_*numRowCols_];
463  copyMat(upper_in, values_in, stride_in, numRowCols_, upper_, values_, stride_, 0);
464  valuesCopied_ = true;
465  }
466 }
467 
468 template<typename OrdinalType, typename ScalarType>
470 {
471  values_ = new ScalarType[stride_*numRowCols_];
472  copyMat(Source.upper_, Source.values_, Source.stride_, numRowCols_, upper_, values_, stride_, 0);
473  valuesCopied_ = true;
474 }
475 
476 template<typename OrdinalType, typename ScalarType>
478  DataAccess CV, const SerialSymDenseMatrix<OrdinalType,
479  ScalarType> &Source, OrdinalType numRowCols_in, OrdinalType startRowCol )
480  : CompObject(), Object("Teuchos::SerialSymDenseMatrix"), numRowCols_(numRowCols_in), stride_(Source.stride_), valuesCopied_(false), upper_(Source.upper_), UPLO_(Source.UPLO_)
481 {
482  if(CV == Copy)
483  {
484  stride_ = numRowCols_in;
485  values_ = new ScalarType[stride_ * numRowCols_in];
486  copyMat(Source.upper_, Source.values_, Source.stride_, numRowCols_in, upper_, values_, stride_, startRowCol);
487  valuesCopied_ = true;
488  }
489  else // CV == View
490  {
491  values_ = Source.values_ + (stride_ * startRowCol) + startRowCol;
492  }
493 }
494 
495 template<typename OrdinalType, typename ScalarType>
497 {
498  deleteArrays();
499 }
500 
501 //----------------------------------------------------------------------------------------------------
502 // Shape methods
503 //----------------------------------------------------------------------------------------------------
504 
505 template<typename OrdinalType, typename ScalarType>
507 {
508  deleteArrays(); // Get rid of anything that might be already allocated
509  numRowCols_ = numRowCols_in;
511  values_ = new ScalarType[stride_*numRowCols_];
513  valuesCopied_ = true;
514  return(0);
515 }
516 
517 template<typename OrdinalType, typename ScalarType>
519 {
520  deleteArrays(); // Get rid of anything that might be already allocated
521  numRowCols_ = numRowCols_in;
523  values_ = new ScalarType[stride_*numRowCols_];
524  valuesCopied_ = true;
525  return(0);
526 }
527 
528 template<typename OrdinalType, typename ScalarType>
530 {
531  // Allocate space for new matrix
532  ScalarType* values_tmp = new ScalarType[numRowCols_in * numRowCols_in];
533  ScalarType zero = ScalarTraits<ScalarType>::zero();
534  for(OrdinalType k = 0; k < numRowCols_in * numRowCols_in; k++)
535  {
536  values_tmp[k] = zero;
537  }
538  OrdinalType numRowCols_tmp = TEUCHOS_MIN(numRowCols_, numRowCols_in);
539  if(values_ != 0)
540  {
541  copyMat(upper_, values_, stride_, numRowCols_tmp, upper_, values_tmp, numRowCols_in, 0); // Copy principal submatrix of A to new A
542  }
543  deleteArrays(); // Get rid of anything that might be already allocated
544  numRowCols_ = numRowCols_in;
546  values_ = values_tmp; // Set pointer to new A
547  valuesCopied_ = true;
548  return(0);
549 }
550 
551 //----------------------------------------------------------------------------------------------------
552 // Set methods
553 //----------------------------------------------------------------------------------------------------
554 
555 template<typename OrdinalType, typename ScalarType>
557 {
558  // Do nothing if the matrix is already a lower triangular matrix
559  if (upper_ != false) {
561  upper_ = false;
562  UPLO_ = 'L';
563  }
564 }
565 
566 template<typename OrdinalType, typename ScalarType>
568 {
569  // Do nothing if the matrix is already an upper triangular matrix
570  if (upper_ == false) {
572  upper_ = true;
573  UPLO_ = 'U';
574  }
575 }
576 
577 template<typename OrdinalType, typename ScalarType>
578 int SerialSymDenseMatrix<OrdinalType, ScalarType>::putScalar( const ScalarType value_in, bool fullMatrix )
579 {
580  // Set each value of the dense matrix to "value".
581  if (fullMatrix == true) {
582  for(OrdinalType j = 0; j < numRowCols_; j++)
583  {
584  for(OrdinalType i = 0; i < numRowCols_; i++)
585  {
586  values_[i + j*stride_] = value_in;
587  }
588  }
589  }
590  // Set the active upper or lower triangular part of the matrix to "value"
591  else {
592  if (upper_) {
593  for(OrdinalType j = 0; j < numRowCols_; j++) {
594  for(OrdinalType i = 0; i <= j; i++) {
595  values_[i + j*stride_] = value_in;
596  }
597  }
598  }
599  else {
600  for(OrdinalType j = 0; j < numRowCols_; j++) {
601  for(OrdinalType i = j; i < numRowCols_; i++) {
602  values_[i + j*stride_] = value_in;
603  }
604  }
605  }
606  }
607  return 0;
608 }
609 
610 template<typename OrdinalType, typename ScalarType>
612 {
614 
615  // Set each value of the dense matrix to a random value.
616  std::vector<MT> diagSum( numRowCols_, Teuchos::ScalarTraits<MT>::zero() );
617  if (upper_) {
618  for(OrdinalType j = 0; j < numRowCols_; j++) {
619  for(OrdinalType i = 0; i < j; i++) {
622  diagSum[j] += Teuchos::ScalarTraits<ScalarType>::magnitude( values_[i + j*stride_] );
623  }
624  }
625  }
626  else {
627  for(OrdinalType j = 0; j < numRowCols_; j++) {
628  for(OrdinalType i = j+1; i < numRowCols_; i++) {
631  diagSum[j] += Teuchos::ScalarTraits<ScalarType>::magnitude( values_[i + j*stride_] );
632  }
633  }
634  }
635 
636  // Set the diagonal.
637  for(OrdinalType i = 0; i < numRowCols_; i++) {
638  values_[i + i*stride_] = diagSum[i] + bias;
639  }
640  return 0;
641 }
642 
643 template<typename OrdinalType, typename ScalarType>
646 {
647  if(this == &Source)
648  return(*this); // Special case of source same as target
649  if((!valuesCopied_) && (!Source.valuesCopied_) && (values_ == Source.values_)) {
650  upper_ = Source.upper_; // Might have to change the active part of the matrix.
651  return(*this); // Special case of both are views to same data.
652  }
653 
654  // If the source is a view then we will return a view, else we will return a copy.
655  if (!Source.valuesCopied_) {
656  if(valuesCopied_) {
657  // Clean up stored data if this was previously a copy.
658  deleteArrays();
659  }
660  numRowCols_ = Source.numRowCols_;
661  stride_ = Source.stride_;
662  values_ = Source.values_;
663  upper_ = Source.upper_;
664  UPLO_ = Source.UPLO_;
665  }
666  else {
667  // If we were a view, we will now be a copy.
668  if(!valuesCopied_) {
669  numRowCols_ = Source.numRowCols_;
670  stride_ = Source.numRowCols_;
671  upper_ = Source.upper_;
672  UPLO_ = Source.UPLO_;
673  const OrdinalType newsize = stride_ * numRowCols_;
674  if(newsize > 0) {
675  values_ = new ScalarType[newsize];
676  valuesCopied_ = true;
677  }
678  else {
679  values_ = 0;
680  }
681  }
682  // If we were a copy, we will stay a copy.
683  else {
684  if((Source.numRowCols_ <= stride_) && (Source.numRowCols_ == numRowCols_)) { // we don't need to reallocate
685  numRowCols_ = Source.numRowCols_;
686  upper_ = Source.upper_;
687  UPLO_ = Source.UPLO_;
688  }
689  else { // we need to allocate more space (or less space)
690  deleteArrays();
691  numRowCols_ = Source.numRowCols_;
692  stride_ = Source.numRowCols_;
693  upper_ = Source.upper_;
694  UPLO_ = Source.UPLO_;
695  const OrdinalType newsize = stride_ * numRowCols_;
696  if(newsize > 0) {
697  values_ = new ScalarType[newsize];
698  valuesCopied_ = true;
699  }
700  }
701  }
702  copyMat(Source.upper_, Source.values_, Source.stride_, Source.numRowCols_, upper_, values_, stride_, 0);
703  }
704  return(*this);
705 }
706 
707 template<typename OrdinalType, typename ScalarType>
709 {
710  this->scale(alpha);
711  return(*this);
712 }
713 
714 template<typename OrdinalType, typename ScalarType>
716 {
717  // Check for compatible dimensions
718  if ((numRowCols_ != Source.numRowCols_))
719  {
720  TEUCHOS_CHK_REF(*this); // Return *this without altering it.
721  }
723  return(*this);
724 }
725 
726 template<typename OrdinalType, typename ScalarType>
728 {
729  // Check for compatible dimensions
730  if ((numRowCols_ != Source.numRowCols_))
731  {
732  TEUCHOS_CHK_REF(*this); // Return *this without altering it.
733  }
735  return(*this);
736 }
737 
738 template<typename OrdinalType, typename ScalarType>
740  if(this == &Source)
741  return(*this); // Special case of source same as target
742  if((!valuesCopied_) && (!Source.valuesCopied_) && (values_ == Source.values_)) {
743  upper_ = Source.upper_; // We may have to change the active part of the matrix.
744  return(*this); // Special case of both are views to same data.
745  }
746 
747  // Check for compatible dimensions
748  if ((numRowCols_ != Source.numRowCols_))
749  {
750  TEUCHOS_CHK_REF(*this); // Return *this without altering it.
751  }
752  copyMat(Source.upper_, Source.values_, Source.stride_, numRowCols_, upper_, values_, stride_, 0 );
753  return(*this);
754 }
755 
756 //----------------------------------------------------------------------------------------------------
757 // Accessor methods
758 //----------------------------------------------------------------------------------------------------
759 
760 template<typename OrdinalType, typename ScalarType>
761 inline ScalarType& SerialSymDenseMatrix<OrdinalType, ScalarType>::operator () (OrdinalType rowIndex, OrdinalType colIndex)
762 {
763 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
764  checkIndex( rowIndex, colIndex );
765 #endif
766  if ( rowIndex <= colIndex ) {
767  // Accessing upper triangular part of matrix
768  if (upper_)
769  return(values_[colIndex * stride_ + rowIndex]);
770  else
771  return(values_[rowIndex * stride_ + colIndex]);
772  }
773  else {
774  // Accessing lower triangular part of matrix
775  if (upper_)
776  return(values_[rowIndex * stride_ + colIndex]);
777  else
778  return(values_[colIndex * stride_ + rowIndex]);
779  }
780 }
781 
782 template<typename OrdinalType, typename ScalarType>
783 inline const ScalarType& SerialSymDenseMatrix<OrdinalType, ScalarType>::operator () (OrdinalType rowIndex, OrdinalType colIndex) const
784 {
785 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
786  checkIndex( rowIndex, colIndex );
787 #endif
788  if ( rowIndex <= colIndex ) {
789  // Accessing upper triangular part of matrix
790  if (upper_)
791  return(values_[colIndex * stride_ + rowIndex]);
792  else
793  return(values_[rowIndex * stride_ + colIndex]);
794  }
795  else {
796  // Accessing lower triangular part of matrix
797  if (upper_)
798  return(values_[rowIndex * stride_ + colIndex]);
799  else
800  return(values_[colIndex * stride_ + rowIndex]);
801  }
802 }
803 
804 //----------------------------------------------------------------------------------------------------
805 // Norm methods
806 //----------------------------------------------------------------------------------------------------
807 
808 template<typename OrdinalType, typename ScalarType>
810 {
811  return(normInf());
812 }
813 
814 template<typename OrdinalType, typename ScalarType>
816 {
817  typedef typename ScalarTraits<ScalarType>::magnitudeType MT;
818 
819  OrdinalType i, j;
820 
821  MT sum, anorm = ScalarTraits<MT>::zero();
822  ScalarType* ptr;
823 
824  if (upper_) {
825  for (j=0; j<numRowCols_; j++) {
826  sum = ScalarTraits<MT>::zero();
827  ptr = values_ + j*stride_;
828  for (i=0; i<j; i++) {
829  sum += ScalarTraits<ScalarType>::magnitude( *ptr++ );
830  }
831  ptr = values_ + j + j*stride_;
832  for (i=j; i<numRowCols_; i++) {
834  ptr += stride_;
835  }
836  anorm = TEUCHOS_MAX( anorm, sum );
837  }
838  }
839  else {
840  for (j=0; j<numRowCols_; j++) {
841  sum = ScalarTraits<MT>::zero();
842  ptr = values_ + j + j*stride_;
843  for (i=j; i<numRowCols_; i++) {
844  sum += ScalarTraits<ScalarType>::magnitude( *ptr++ );
845  }
846  ptr = values_ + j;
847  for (i=0; i<j; i++) {
849  ptr += stride_;
850  }
851  anorm = TEUCHOS_MAX( anorm, sum );
852  }
853  }
854  return(anorm);
855 }
856 
857 template<typename OrdinalType, typename ScalarType>
859 {
860  typedef typename ScalarTraits<ScalarType>::magnitudeType MT;
861 
862  OrdinalType i, j;
863 
864  MT sum = ScalarTraits<MT>::zero(), anorm = ScalarTraits<MT>::zero();
865 
866  if (upper_) {
867  for (j = 0; j < numRowCols_; j++) {
868  for (i = 0; i < j; i++) {
870  }
872  }
873  }
874  else {
875  for (j = 0; j < numRowCols_; j++) {
877  for (i = j+1; i < numRowCols_; i++) {
878  sum += ScalarTraits<ScalarType>::magnitude(2.0*values_[i+j*stride_]*values_[i+j*stride_]);
879  }
880  }
881  }
883  return(anorm);
884 }
885 
886 //----------------------------------------------------------------------------------------------------
887 // Comparison methods
888 //----------------------------------------------------------------------------------------------------
889 
890 template<typename OrdinalType, typename ScalarType>
892 {
893  bool result = 1;
894  if((numRowCols_ != Operand.numRowCols_)) {
895  result = 0;
896  }
897  else {
898  OrdinalType i, j;
899  for(i = 0; i < numRowCols_; i++) {
900  for(j = 0; j < numRowCols_; j++) {
901  if((*this)(i, j) != Operand(i, j)) {
902  return 0;
903  }
904  }
905  }
906  }
907  return result;
908 }
909 
910 template<typename OrdinalType, typename ScalarType>
912 {
913  return !((*this) == Operand);
914 }
915 
916 //----------------------------------------------------------------------------------------------------
917 // Multiplication method
918 //----------------------------------------------------------------------------------------------------
919 
920 template<typename OrdinalType, typename ScalarType>
922 {
923  OrdinalType i, j;
924  ScalarType* ptr;
925 
926  if (upper_) {
927  for (j=0; j<numRowCols_; j++) {
928  ptr = values_ + j*stride_;
929  for (i=0; i<= j; i++) { *ptr = alpha * (*ptr); ptr++; }
930  }
931  }
932  else {
933  for (j=0; j<numRowCols_; j++) {
934  ptr = values_ + j*stride_ + j;
935  for (i=j; i<numRowCols_; i++) { *ptr = alpha * (*ptr); ptr++; }
936  }
937  }
938 }
939 
940 /*
941 template<typename OrdinalType, typename ScalarType>
942 int SerialSymDenseMatrix<OrdinalType, ScalarType>::scale( const SerialSymDenseMatrix<OrdinalType,ScalarType>& A )
943 {
944  OrdinalType i, j;
945  ScalarType* ptr;
946 
947  // Check for compatible dimensions
948  if ((numRowCols_ != A.numRowCols_)) {
949  TEUCHOS_CHK_ERR(-1); // Return error
950  }
951 
952  if (upper_) {
953  for (j=0; j<numRowCols_; j++) {
954  ptr = values_ + j*stride_;
955  for (i=0; i<= j; i++) { *ptr = A(i,j) * (*ptr); ptr++; }
956  }
957  }
958  else {
959  for (j=0; j<numRowCols_; j++) {
960  ptr = values_ + j*stride_;
961  for (i=j; i<numRowCols_; i++) { *ptr = A(i,j) * (*ptr); ptr++; }
962  }
963  }
964 
965  return(0);
966 }
967 */
968 
969 template<typename OrdinalType, typename ScalarType>
971 {
972  os << std::endl;
973  if(valuesCopied_)
974  os << "Values_copied : yes" << std::endl;
975  else
976  os << "Values_copied : no" << std::endl;
977  os << "Rows / Columns : " << numRowCols_ << std::endl;
978  os << "LDA : " << stride_ << std::endl;
979  if (upper_)
980  os << "Storage: Upper" << std::endl;
981  else
982  os << "Storage: Lower" << std::endl;
983  if(numRowCols_ == 0) {
984  os << "(matrix is empty, no values to display)" << std::endl;
985  } else {
986  for(OrdinalType i = 0; i < numRowCols_; i++) {
987  for(OrdinalType j = 0; j < numRowCols_; j++){
988  os << (*this)(i,j) << " ";
989  }
990  os << std::endl;
991  }
992  }
993 }
994 
995 //----------------------------------------------------------------------------------------------------
996 // Protected methods
997 //----------------------------------------------------------------------------------------------------
998 
999 template<typename OrdinalType, typename ScalarType>
1000 inline void SerialSymDenseMatrix<OrdinalType, ScalarType>::checkIndex( OrdinalType rowIndex, OrdinalType colIndex ) const {
1001  TEUCHOS_TEST_FOR_EXCEPTION(rowIndex < 0 || rowIndex >= numRowCols_, std::out_of_range,
1002  "SerialSymDenseMatrix<T>::checkIndex: "
1003  "Row index " << rowIndex << " out of range [0, "<< numRowCols_ << ")");
1004  TEUCHOS_TEST_FOR_EXCEPTION(colIndex < 0 || colIndex >= numRowCols_, std::out_of_range,
1005  "SerialSymDenseMatrix<T>::checkIndex: "
1006  "Col index " << colIndex << " out of range [0, "<< numRowCols_ << ")");
1007 }
1008 
1009 template<typename OrdinalType, typename ScalarType>
1011 {
1012  if (valuesCopied_)
1013  {
1014  delete [] values_;
1015  values_ = 0;
1016  valuesCopied_ = false;
1017  }
1018 }
1019 
1020 template<typename OrdinalType, typename ScalarType>
1022  bool inputUpper, ScalarType* inputMatrix,
1023  OrdinalType inputStride, OrdinalType numRowCols_in,
1024  bool outputUpper, ScalarType* outputMatrix,
1025  OrdinalType outputStride, OrdinalType startRowCol,
1026  ScalarType alpha
1027  )
1028 {
1029  OrdinalType i, j;
1030  ScalarType* ptr1 = 0;
1031  ScalarType* ptr2 = 0;
1032 
1033  for (j = 0; j < numRowCols_in; j++) {
1034  if (inputUpper == true) {
1035  // The input matrix is upper triangular, start at the beginning of each column.
1036  ptr2 = inputMatrix + (j + startRowCol) * inputStride + startRowCol;
1037  if (outputUpper == true) {
1038  // The output matrix matches the same pattern as the input matrix.
1039  ptr1 = outputMatrix + j*outputStride;
1040  if (alpha != Teuchos::ScalarTraits<ScalarType>::zero() ) {
1041  for(i = 0; i <= j; i++) {
1042  *ptr1++ += alpha*(*ptr2++);
1043  }
1044  } else {
1045  for(i = 0; i <= j; i++) {
1046  *ptr1++ = *ptr2++;
1047  }
1048  }
1049  }
1050  else {
1051  // The output matrix has the opposite pattern as the input matrix.
1052  // Copy down across rows of the output matrix, but down columns of the input matrix.
1053  ptr1 = outputMatrix + j;
1054  if (alpha != Teuchos::ScalarTraits<ScalarType>::zero() ) {
1055  for(i = 0; i <= j; i++) {
1056  *ptr1 += alpha*(*ptr2++);
1057  ptr1 += outputStride;
1058  }
1059  } else {
1060  for(i = 0; i <= j; i++) {
1061  *ptr1 = *ptr2++;
1062  ptr1 += outputStride;
1063  }
1064  }
1065  }
1066  }
1067  else {
1068  // The input matrix is lower triangular, start at the diagonal of each row.
1069  ptr2 = inputMatrix + (startRowCol+j) * inputStride + startRowCol + j;
1070  if (outputUpper == true) {
1071  // The output matrix has the opposite pattern as the input matrix.
1072  // Copy across rows of the output matrix, but down columns of the input matrix.
1073  ptr1 = outputMatrix + j*outputStride + j;
1074  if (alpha != Teuchos::ScalarTraits<ScalarType>::zero() ) {
1075  for(i = j; i < numRowCols_in; i++) {
1076  *ptr1 += alpha*(*ptr2++);
1077  ptr1 += outputStride;
1078  }
1079  } else {
1080  for(i = j; i < numRowCols_in; i++) {
1081  *ptr1 = *ptr2++;
1082  ptr1 += outputStride;
1083  }
1084  }
1085  }
1086  else {
1087  // The output matrix matches the same pattern as the input matrix.
1088  ptr1 = outputMatrix + j*outputStride + j;
1089  if (alpha != Teuchos::ScalarTraits<ScalarType>::zero() ) {
1090  for(i = j; i < numRowCols_in; i++) {
1091  *ptr1++ += alpha*(*ptr2++);
1092  }
1093  } else {
1094  for(i = j; i < numRowCols_in; i++) {
1095  *ptr1++ = *ptr2++;
1096  }
1097  }
1098  }
1099  }
1100  }
1101 }
1102 
1103 template<typename OrdinalType, typename ScalarType>
1105  bool inputUpper, ScalarType* inputMatrix,
1106  OrdinalType inputStride, OrdinalType inputRows
1107  )
1108 {
1109  OrdinalType i, j;
1110  ScalarType * ptr1 = 0;
1111  ScalarType * ptr2 = 0;
1112 
1113  if (inputUpper) {
1114  for (j=1; j<inputRows; j++) {
1115  ptr1 = inputMatrix + j;
1116  ptr2 = inputMatrix + j*inputStride;
1117  for (i=0; i<j; i++) {
1118  *ptr1 = *ptr2++;
1119  ptr1+=inputStride;
1120  }
1121  }
1122  }
1123  else {
1124  for (i=1; i<inputRows; i++) {
1125  ptr1 = inputMatrix + i;
1126  ptr2 = inputMatrix + i*inputStride;
1127  for (j=0; j<i; j++) {
1128  *ptr2++ = *ptr1;
1129  ptr1+=inputStride;
1130  }
1131  }
1132  }
1133 }
1134 
1135 } // namespace Teuchos
1136 
1137 #endif /* _TEUCHOS_SERIALSYMDENSEMATRIX_HPP_ */
bool upper() const
Returns true if upper triangular part of this matrix has and will be used.
void copyUPLOMat(bool inputUpper, ScalarType *inputMatrix, OrdinalType inputStride, OrdinalType inputRows)
T magnitudeType
Mandatory typedef for result of magnitude.
SerialSymDenseMatrix()
Default constructor; defines a zero size object.
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.
ScalarType * values() const
Returns the pointer to the ScalarType data array contained in the object.
ScalarType & operator()(OrdinalType rowIndex, OrdinalType colIndex)
Element access method (non-const).
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
Macro for throwing an exception with breakpointing to ease debugging.
int shape(OrdinalType numRowsCols)
Set dimensions of a Teuchos::SerialSymDenseMatrix object; init values to zero.
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...
OrdinalType numRows() const
Returns the row dimension of this matrix.
ScalarTraits< ScalarType >::magnitudeType normInf() const
Returns the Infinity-norm of the matrix.
Object for storing data and providing functionality that is common to all computational classes...
int reshape(OrdinalType numRowsCols)
Reshape a Teuchos::SerialSymDenseMatrix object.
This structure defines some basic traits for a scalar field type.
void setLower()
Specify that the lower triangle of the this matrix should be used.
void setUpper()
Specify that the upper triangle of the this matrix should be used.
Functionality and data that is common to all computational classes.
void checkIndex(OrdinalType rowIndex, OrdinalType colIndex=0) const
SerialSymDenseMatrix< OrdinalType, ScalarType > & operator*=(const ScalarType alpha)
Inplace scalar-matrix product A = alpha*A.
bool operator==(const SerialSymDenseMatrix< OrdinalType, ScalarType > &Operand) const
Equality of two matrices.
The base Teuchos class.
int putScalar(const ScalarType value=Teuchos::ScalarTraits< ScalarType >::zero(), bool fullMatrix=false)
Set all values in the matrix to a constant value.
SerialSymDenseMatrix< OrdinalType, ScalarType > & operator+=(const SerialSymDenseMatrix< OrdinalType, ScalarType > &Source)
Add another matrix to this matrix.
ScalarTraits< ScalarType >::magnitudeType normOne() const
Returns the 1-norm of the matrix.
#define TEUCHOS_MAX(x, y)
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...
char UPLO() const
Returns character value of UPLO used by LAPACK routines.
virtual ~SerialSymDenseMatrix()
Teuchos::SerialSymDenseMatrix destructor.
static magnitudeType magnitude(T a)
Returns the magnitudeType of the scalar type a.
SerialSymDenseMatrix< OrdinalType, ScalarType > & operator-=(const SerialSymDenseMatrix< OrdinalType, ScalarType > &Source)
Subtract another matrix from this matrix.
OrdinalType ordinalType
Typedef for ordinal type.
SerialSymDenseMatrix< OrdinalType, ScalarType > & operator=(const SerialSymDenseMatrix< OrdinalType, ScalarType > &Source)
Copies values from one matrix to another.
int shapeUninitialized(OrdinalType numRowsCols)
Set dimensions of a Teuchos::SerialSymDenseMatrix object; don&#39;t initialize values.
Defines basic traits for the scalar field type.
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.
OrdinalType stride() const
Returns the stride between the columns of this matrix in memory.
OrdinalType numCols() const
Returns the column dimension of this matrix.
ScalarTraits< ScalarType >::magnitudeType normFrobenius() const
Returns the Frobenius-norm of the matrix.
#define TEUCHOS_CHK_REF(a)
SerialSymDenseMatrix< OrdinalType, ScalarType > & assign(const SerialSymDenseMatrix< OrdinalType, ScalarType > &Source)
Copies values from one matrix to another.
bool operator!=(const SerialSymDenseMatrix< OrdinalType, ScalarType > &Operand) const
Inequality of two matrices.
#define TEUCHOS_MIN(x, y)
Teuchos::DataAccess Mode enumerable type.
bool empty() const
Returns whether this matrix is empty.
void copyMat(bool inputUpper, ScalarType *inputMatrix, OrdinalType inputStride, OrdinalType numRowCols, bool outputUpper, ScalarType *outputMatrix, OrdinalType outputStride, OrdinalType startRowCol, ScalarType alpha=ScalarTraits< ScalarType >::zero())
ScalarType scalarType
Typedef for scalar type.