Teuchos - Trilinos Tools Package  Version of the Day
Teuchos_SerialBandDenseMatrix.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_SERIALBANDDENSEMATRIX_HPP_
43 #define _TEUCHOS_SERIALBANDDENSEMATRIX_HPP_
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_RCP.hpp"
54 #include "Teuchos_Assert.hpp"
55 
130 namespace Teuchos {
131 
132 template<typename OrdinalType, typename ScalarType>
133 class SerialBandDenseMatrix : public CompObject, public BLAS<OrdinalType, ScalarType>
134 {
135 public:
136 
138  typedef OrdinalType ordinalType;
140  typedef ScalarType scalarType;
141 
143 
144 
146 
150 
152 
162  SerialBandDenseMatrix(OrdinalType numRows, OrdinalType numCols, OrdinalType kl, OrdinalType ku, bool zeroOut = true);
163 
165 
175  SerialBandDenseMatrix(DataAccess CV, ScalarType* values, OrdinalType stride, OrdinalType numRows, OrdinalType numCols, OrdinalType kl, OrdinalType ku);
176 
178 
184 
186 
200  SerialBandDenseMatrix(DataAccess CV, const SerialBandDenseMatrix<OrdinalType, ScalarType> &Source, OrdinalType numRows, OrdinalType numCols, OrdinalType startCol=0);
201 
203  virtual ~SerialBandDenseMatrix();
205 
207 
208 
221  int shape(OrdinalType numRows, OrdinalType numCols, OrdinalType kl, OrdinalType ku);
222 
224  int shapeUninitialized(OrdinalType numRows, OrdinalType numCols, OrdinalType kl, OrdinalType ku);
225 
227 
239  int reshape(OrdinalType numRows, OrdinalType numCols, OrdinalType kl, OrdinalType ku);
240 
241 
243 
245 
246 
248 
255 
257 
263 
265 
268  SerialBandDenseMatrix<OrdinalType, ScalarType>& operator= (const ScalarType value) { putScalar(value); return(*this); }
269 
271 
275  int putScalar( const ScalarType value = Teuchos::ScalarTraits<ScalarType>::zero() );
276 
278  int random();
279 
281 
283 
284 
286 
293  ScalarType& operator () (OrdinalType rowIndex, OrdinalType colIndex);
294 
296 
303  const ScalarType& operator () (OrdinalType rowIndex, OrdinalType colIndex) const;
304 
306 
313  ScalarType* operator [] (OrdinalType colIndex);
314 
316 
323  const ScalarType* operator [] (OrdinalType colIndex) const;
324 
326 
327  ScalarType* values() const { return(values_); }
328 
330 
332 
333 
335 
339 
341 
345 
347 
351 
353 
357  int scale ( const ScalarType alpha );
358 
360 
367 
369 
371 
372 
374 
378 
380 
384 
386 
388 
389 
391  OrdinalType numRows() const { return(numRows_); }
392 
394  OrdinalType numCols() const { return(numCols_); }
395 
397  OrdinalType lowerBandwidth() const { return(kl_); }
398 
400  OrdinalType upperBandwidth() const { return(ku_); }
401 
403  OrdinalType stride() const { return(stride_); }
404 
406  bool empty() const { return(numRows_ == 0 || numCols_ == 0); }
408 
410 
411 
414 
417 
421 
423 
424 #ifndef TEUCHOS_HIDE_DEPRECATED_CODE
426  virtual void print(std::ostream& os) const;
427 #else
428  virtual std::ostream& print(std::ostream& os) const;
429 #endif
430 
432 protected:
433  void copyMat(ScalarType* inputMatrix, OrdinalType strideInput,
434  OrdinalType numRows, OrdinalType numCols, ScalarType* outputMatrix,
435  OrdinalType strideOutput, OrdinalType startCol, ScalarType alpha = ScalarTraits<ScalarType>::zero() );
436  void deleteArrays();
437  void checkIndex( OrdinalType rowIndex, OrdinalType colIndex = 0 ) const;
438  OrdinalType numRows_;
439  OrdinalType numCols_;
440  OrdinalType stride_;
441  OrdinalType kl_;
442  OrdinalType ku_;
443  bool valuesCopied_;
444  ScalarType* values_;
445 
446 }; // class Teuchos_SerialBandDenseMatrix
447 
448 //----------------------------------------------------------------------------------------------------
449 // Constructors and Destructor
450 //----------------------------------------------------------------------------------------------------
451 
452 template<typename OrdinalType, typename ScalarType>
454  : CompObject (),
455  BLAS<OrdinalType,ScalarType>(),
456  numRows_ (0),
457  numCols_ (0),
458  stride_ (0),
459  kl_ (0),
460  ku_ (0),
461  valuesCopied_ (false),
462  values_ (0)
463 {}
464 
465 template<typename OrdinalType, typename ScalarType>
467 SerialBandDenseMatrix (OrdinalType numRows_in,
468  OrdinalType numCols_in,
469  OrdinalType kl_in,
470  OrdinalType ku_in,
471  bool zeroOut)
472  : CompObject (),
473  BLAS<OrdinalType,ScalarType>(),
474  numRows_ (numRows_in),
475  numCols_ (numCols_in),
476  stride_ (kl_in+ku_in+1),
477  kl_ (kl_in),
478  ku_ (ku_in),
479  valuesCopied_ (true),
480  values_ (NULL) // for safety, in case allocation fails below
481 {
482  values_ = new ScalarType[stride_ * numCols_];
483  if (zeroOut) {
484  putScalar ();
485  }
486 }
487 
488 template<typename OrdinalType, typename ScalarType>
491  ScalarType* values_in,
492  OrdinalType stride_in,
493  OrdinalType numRows_in,
494  OrdinalType numCols_in,
495  OrdinalType kl_in,
496  OrdinalType ku_in)
497  : CompObject (),
498  BLAS<OrdinalType,ScalarType>(),
499  numRows_ (numRows_in),
500  numCols_ (numCols_in),
501  stride_ (stride_in),
502  kl_ (kl_in),
503  ku_ (ku_in),
504  valuesCopied_ (false),
505  values_ (values_in)
506 {
507  if (CV == Copy) {
508  stride_ = kl_+ku_+1;
509  values_ = new ScalarType[stride_*numCols_];
510  copyMat (values_in, stride_in, numRows_, numCols_, values_, stride_, 0);
511  valuesCopied_ = true;
512  }
513 }
514 
515 template<typename OrdinalType, typename ScalarType>
518  : CompObject (),
519  BLAS<OrdinalType,ScalarType>(),
520  numRows_ (0),
521  numCols_ (0),
522  stride_ (0),
523  kl_ (0),
524  ku_ (0),
525  valuesCopied_ (true),
526  values_ (NULL)
527 {
528  if (trans == NO_TRANS) {
529  numRows_ = Source.numRows_;
530  numCols_ = Source.numCols_;
531  kl_ = Source.kl_;
532  ku_ = Source.ku_;
533  stride_ = kl_+ku_+1;
534  values_ = new ScalarType[stride_*numCols_];
535  copyMat (Source.values_, Source.stride_, numRows_, numCols_,
536  values_, stride_, 0);
537  }
538  else if (trans == CONJ_TRANS && ScalarTraits<ScalarType>::isComplex) {
539  numRows_ = Source.numCols_;
540  numCols_ = Source.numRows_;
541  kl_ = Source.ku_;
542  ku_ = Source.kl_;
543  stride_ = kl_+ku_+1;
544  values_ = new ScalarType[stride_*numCols_];
545  for (OrdinalType j = 0; j < numCols_; ++j) {
546  for (OrdinalType i = TEUCHOS_MAX(0,j-ku_);
547  i <= TEUCHOS_MIN(numRows_-1,j+kl_); ++i) {
548  values_[j*stride_ + (ku_+i-j)] =
549  ScalarTraits<ScalarType>::conjugate (Source.values_[i*Source.stride_ + (Source.ku_+j-i)]);
550  }
551  }
552  }
553  else {
554  numRows_ = Source.numCols_;
555  numCols_ = Source.numRows_;
556  kl_ = Source.ku_;
557  ku_ = Source.kl_;
558  stride_ = kl_+ku_+1;
559  values_ = new ScalarType[stride_*numCols_];
560  for (OrdinalType j=0; j<numCols_; j++) {
561  for (OrdinalType i = TEUCHOS_MAX(0,j-ku_);
562  i <= TEUCHOS_MIN(numRows_-1,j+kl_); ++i) {
563  values_[j*stride_ + (ku_+i-j)] = Source.values_[i*Source.stride_ + (Source.ku_+j-i)];
564  }
565  }
566  }
567 }
568 
569 template<typename OrdinalType, typename ScalarType>
573  OrdinalType numRows_in,
574  OrdinalType numCols_in,
575  OrdinalType startCol)
576  : CompObject (),
577  BLAS<OrdinalType,ScalarType>(),
578  numRows_ (numRows_in),
579  numCols_ (numCols_in),
580  stride_ (Source.stride_),
581  kl_ (Source.kl_),
582  ku_ (Source.ku_),
583  valuesCopied_ (false),
584  values_ (Source.values_)
585 {
586  if (CV == Copy) {
587  values_ = new ScalarType[stride_ * numCols_in];
588  copyMat (Source.values_, Source.stride_, numRows_in, numCols_in,
589  values_, stride_, startCol);
590  valuesCopied_ = true;
591  } else { // CV = View
592  values_ = values_ + (stride_ * startCol);
593  }
594 }
595 
596 template<typename OrdinalType, typename ScalarType>
598 {
599  deleteArrays();
600 }
601 
602 //----------------------------------------------------------------------------------------------------
603 // Shape methods
604 //----------------------------------------------------------------------------------------------------
605 
606 template<typename OrdinalType, typename ScalarType>
608  OrdinalType numRows_in, OrdinalType numCols_in, OrdinalType kl_in, OrdinalType ku_in
609  )
610 {
611  deleteArrays(); // Get rid of anything that might be already allocated
612  numRows_ = numRows_in;
613  numCols_ = numCols_in;
614  kl_ = kl_in;
615  ku_ = ku_in;
616  stride_ = kl_+ku_+1;
617  values_ = new ScalarType[stride_*numCols_];
618  putScalar();
619  valuesCopied_ = true;
620 
621  return(0);
622 }
623 
624 template<typename OrdinalType, typename ScalarType>
626  OrdinalType numRows_in, OrdinalType numCols_in, OrdinalType kl_in, OrdinalType ku_in
627  )
628 {
629  deleteArrays(); // Get rid of anything that might be already allocated
630  numRows_ = numRows_in;
631  numCols_ = numCols_in;
632  kl_ = kl_in;
633  ku_ = ku_in;
634  stride_ = kl_+ku_+1;
635  values_ = new ScalarType[stride_*numCols_];
636  valuesCopied_ = true;
637 
638  return(0);
639 }
640 
641 template<typename OrdinalType, typename ScalarType>
643  OrdinalType numRows_in, OrdinalType numCols_in, OrdinalType kl_in, OrdinalType ku_in
644  )
645 {
646 
647  // Allocate space for new matrix
648  ScalarType* values_tmp = new ScalarType[(kl_in+ku_in+1) * numCols_in];
649  ScalarType zero = ScalarTraits<ScalarType>::zero();
650  for(OrdinalType k = 0; k < (kl_in+ku_in+1) * numCols_in; k++) {
651  values_tmp[k] = zero;
652  }
653  OrdinalType numRows_tmp = TEUCHOS_MIN(numRows_, numRows_in);
654  OrdinalType numCols_tmp = TEUCHOS_MIN(numCols_, numCols_in);
655  if(values_ != 0) {
656  copyMat(values_, stride_, numRows_tmp, numCols_tmp, values_tmp,
657  kl_in+ku_in+1, 0); // Copy principal submatrix of A to new A
658  }
659  deleteArrays(); // Get rid of anything that might be already allocated
660  numRows_ = numRows_in;
661  numCols_ = numCols_in;
662  kl_ = kl_in;
663  ku_ = ku_in;
664  stride_ = kl_+ku_+1;
665  values_ = values_tmp; // Set pointer to new A
666  valuesCopied_ = true;
667 
668  return(0);
669 }
670 
671 //----------------------------------------------------------------------------------------------------
672 // Set methods
673 //----------------------------------------------------------------------------------------------------
674 
675 template<typename OrdinalType, typename ScalarType>
677 {
678 
679  // Set each value of the dense matrix to "value".
680  for(OrdinalType j = 0; j < numCols_; j++) {
681  for (OrdinalType i=TEUCHOS_MAX(0,j-ku_); i<=TEUCHOS_MIN(numRows_-1,j+kl_); i++) {
682  values_[(ku_+i-j) + j*stride_] = value_in;
683  }
684  }
685 
686  return 0;
687 }
688 
689 template<typename OrdinalType, typename ScalarType>
691 {
692 
693  // Set each value of the dense matrix to a random value.
694  for(OrdinalType j = 0; j < numCols_; j++) {
695  for (OrdinalType i=TEUCHOS_MAX(0,j-ku_); i<=TEUCHOS_MIN(numRows_-1,j+kl_); i++) {
696  values_[(ku_+i-j) + j*stride_] = ScalarTraits<ScalarType>::random();
697  }
698  }
699 
700  return 0;
701 }
702 
703 template<typename OrdinalType, typename ScalarType>
707  )
708 {
709 
710  if(this == &Source)
711  return(*this); // Special case of source same as target
712  if((!valuesCopied_) && (!Source.valuesCopied_) && (values_ == Source.values_))
713  return(*this); // Special case of both are views to same data.
714 
715  // If the source is a view then we will return a view, else we will return a copy.
716  if (!Source.valuesCopied_) {
717  if(valuesCopied_) {
718  // Clean up stored data if this was previously a copy.
719  deleteArrays();
720  }
721  numRows_ = Source.numRows_;
722  numCols_ = Source.numCols_;
723  kl_ = Source.kl_;
724  ku_ = Source.ku_;
725  stride_ = Source.stride_;
726  values_ = Source.values_;
727  } else {
728  // If we were a view, we will now be a copy.
729  if(!valuesCopied_) {
730  numRows_ = Source.numRows_;
731  numCols_ = Source.numCols_;
732  kl_ = Source.kl_;
733  ku_ = Source.ku_;
734  stride_ = kl_+ku_+1;
735  const OrdinalType newsize = stride_ * numCols_;
736  if(newsize > 0) {
737  values_ = new ScalarType[newsize];
738  valuesCopied_ = true;
739  } else {
740  values_ = 0;
741  }
742  } else {
743  // If we were a copy, we will stay a copy.
744  if((Source.numRows_ <= stride_) && (Source.numCols_ == numCols_)) { // we don't need to reallocate
745  numRows_ = Source.numRows_;
746  numCols_ = Source.numCols_;
747  kl_ = Source.kl_;
748  ku_ = Source.ku_;
749  } else {
750  // we need to allocate more space (or less space)
751  deleteArrays();
752  numRows_ = Source.numRows_;
753  numCols_ = Source.numCols_;
754  kl_ = Source.kl_;
755  ku_ = Source.ku_;
756  stride_ = kl_+ku_+1;
757  const OrdinalType newsize = stride_ * numCols_;
758  if(newsize > 0) {
759  values_ = new ScalarType[newsize];
760  valuesCopied_ = true;
761  }
762  }
763  }
764  copyMat(Source.values_, Source.stride_, numRows_, numCols_, values_, stride_, 0);
765  }
766  return(*this);
767 
768 }
769 
770 template<typename OrdinalType, typename ScalarType>
772 {
773 
774  // Check for compatible dimensions
775  if ((numRows_ != Source.numRows_) || (numCols_ != Source.numCols_) || (kl_ != Source.kl_) || (ku_ != Source.ku_)) {
776  TEUCHOS_CHK_REF(*this); // Return *this without altering it.
777  }
778  copyMat(Source.values_, Source.stride_, numRows_, numCols_, values_, stride_, 0, ScalarTraits<ScalarType>::one());
779  return(*this);
780 
781 }
782 
783 template<typename OrdinalType, typename ScalarType>
785 {
786 
787  // Check for compatible dimensions
788  if ((numRows_ != Source.numRows_) || (numCols_ != Source.numCols_) || (kl_ != Source.kl_) || (ku_ != Source.ku_)) {
789  TEUCHOS_CHK_REF(*this); // Return *this without altering it.
790  }
791  copyMat(Source.values_, Source.stride_, numRows_, numCols_, values_, stride_, 0, -ScalarTraits<ScalarType>::one());
792  return(*this);
793 
794 }
795 
796 template<typename OrdinalType, typename ScalarType>
798 
799  if(this == &Source)
800  return(*this); // Special case of source same as target
801  if((!valuesCopied_) && (!Source.valuesCopied_) && (values_ == Source.values_))
802  return(*this); // Special case of both are views to same data.
803 
804  // Check for compatible dimensions
805  if ((numRows_ != Source.numRows_) || (numCols_ != Source.numCols_) || (kl_ != Source.kl_) || (ku_ != Source.ku_)) {
806  TEUCHOS_CHK_REF(*this); // Return *this without altering it.
807  }
808  copyMat(Source.values_, Source.stride_, numRows_, numCols_, values_, stride_, 0);
809  return(*this);
810 
811 }
812 
813 //----------------------------------------------------------------------------------------------------
814 // Accessor methods
815 //----------------------------------------------------------------------------------------------------
816 
817 template<typename OrdinalType, typename ScalarType>
818 inline ScalarType& SerialBandDenseMatrix<OrdinalType, ScalarType>::operator () (OrdinalType rowIndex, OrdinalType colIndex)
819 {
820 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
821  checkIndex( rowIndex, colIndex );
822 #endif
823  return(values_[colIndex * stride_ + ku_+rowIndex-colIndex]);
824 }
825 
826 template<typename OrdinalType, typename ScalarType>
827 inline const ScalarType& SerialBandDenseMatrix<OrdinalType, ScalarType>::operator () (OrdinalType rowIndex, OrdinalType colIndex) const
828 {
829 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
830  checkIndex( rowIndex, colIndex );
831 #endif
832  return(values_[colIndex * stride_ + ku_+rowIndex-colIndex]);
833 }
834 
835 template<typename OrdinalType, typename ScalarType>
836 inline const ScalarType* SerialBandDenseMatrix<OrdinalType, ScalarType>::operator [] (OrdinalType colIndex) const
837 {
838 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
839  checkIndex( 0, colIndex );
840 #endif
841  return(values_ + colIndex * stride_);
842 }
843 
844 template<typename OrdinalType, typename ScalarType>
845 inline ScalarType* SerialBandDenseMatrix<OrdinalType, ScalarType>::operator [] (OrdinalType colIndex)
846 {
847 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
848  checkIndex( 0, colIndex );
849 #endif
850  return(values_ + colIndex * stride_);
851 }
852 
853 //----------------------------------------------------------------------------------------------------
854 // Norm methods
855 //----------------------------------------------------------------------------------------------------
856 
857 template<typename OrdinalType, typename ScalarType>
859 {
860  OrdinalType i, j;
863 
864  ScalarType* ptr;
865  for(j = 0; j < numCols_; j++) {
866  ScalarType sum = 0;
867  ptr = values_ + j * stride_ + TEUCHOS_MAX(0, ku_-j);
868  for (i=TEUCHOS_MAX(0,j-ku_); i<=TEUCHOS_MIN(numRows_-1,j+kl_); i++) {
870  }
872  if(absSum > anorm) {
873  anorm = absSum;
874  }
875  }
876  updateFlops((kl_+ku_+1) * numCols_);
877 
878  return(anorm);
879 }
880 
881 template<typename OrdinalType, typename ScalarType>
883 {
884  OrdinalType i, j;
886 
887  for (i = 0; i < numRows_; i++) {
889  for (j=TEUCHOS_MAX(0,i-kl_); j<=TEUCHOS_MIN(numCols_-1,i+ku_); j++) {
890  sum += ScalarTraits<ScalarType>::magnitude(*(values_+(ku_+i-j)+j*stride_));
891  }
892  anorm = TEUCHOS_MAX( anorm, sum );
893  }
894  updateFlops((kl_+ku_+1) * numCols_);
895 
896  return(anorm);
897 }
898 
899 template<typename OrdinalType, typename ScalarType>
901 {
902  OrdinalType i, j;
904 
905  for (j = 0; j < numCols_; j++) {
906  for (i=TEUCHOS_MAX(0,j-ku_); i<=TEUCHOS_MIN(numRows_-1,j+kl_); i++) {
907  anorm += ScalarTraits<ScalarType>::magnitude(values_[(ku_+i-j)+j*stride_]*values_[(ku_+i-j)+j*stride_]);
908  }
909  }
911  updateFlops((kl_+ku_+1) * numCols_);
912 
913  return(anorm);
914 }
915 
916 //----------------------------------------------------------------------------------------------------
917 // Comparison methods
918 //----------------------------------------------------------------------------------------------------
919 
920 template<typename OrdinalType, typename ScalarType>
922 {
923  bool result = 1;
924 
925  if((numRows_ != Operand.numRows_) || (numCols_ != Operand.numCols_) || (kl_ != Operand.kl_) || (ku_ != Operand.ku_)) {
926  result = 0;
927  } else {
928  OrdinalType i, j;
929  for(j = 0; j < numCols_; j++) {
930  for (i=TEUCHOS_MAX(0,j-ku_); i<=TEUCHOS_MIN(numRows_-1,j+kl_); i++) {
931  if((*this)(i, j) != Operand(i, j)) {
932  return 0;
933  }
934  }
935  }
936  }
937 
938  return result;
939 }
940 
941 template<typename OrdinalType, typename ScalarType>
943 {
944  return !((*this) == Operand);
945 }
946 
947 //----------------------------------------------------------------------------------------------------
948 // Multiplication method
949 //----------------------------------------------------------------------------------------------------
950 
951 template<typename OrdinalType, typename ScalarType>
953 {
954  this->scale( alpha );
955  return(*this);
956 }
957 
958 template<typename OrdinalType, typename ScalarType>
960 {
961 
962  OrdinalType i, j;
963  ScalarType* ptr;
964 
965  for (j=0; j<numCols_; j++) {
966  ptr = values_ + j*stride_ + TEUCHOS_MAX(0, ku_-j);
967  for (i=TEUCHOS_MAX(0,j-ku_); i<=TEUCHOS_MIN(numRows_-1,j+kl_); i++) {
968  *ptr = alpha * (*ptr); ptr++;
969  }
970  }
971  updateFlops( (kl_+ku_+1)*numCols_ );
972 
973  return(0);
974 }
975 
976 template<typename OrdinalType, typename ScalarType>
978 {
979 
980  OrdinalType i, j;
981  ScalarType* ptr;
982 
983  // Check for compatible dimensions
984  if ((numRows_ != A.numRows_) || (numCols_ != A.numCols_) || (kl_ != A.kl_) || (ku_ != A.ku_)) {
985  TEUCHOS_CHK_ERR(-1); // Return error
986  }
987  for (j=0; j<numCols_; j++) {
988  ptr = values_ + j*stride_ + TEUCHOS_MAX(0, ku_-j);
989  for (i=TEUCHOS_MAX(0,j-ku_); i<=TEUCHOS_MIN(numRows_-1,j+kl_); i++) {
990  *ptr = A(i,j) * (*ptr); ptr++;
991  }
992  }
993  updateFlops( (kl_+ku_+1)*numCols_ );
994 
995  return(0);
996 }
997 
998 template<typename OrdinalType, typename ScalarType>
999 #ifndef TEUCHOS_HIDE_DEPRECATED_CODE
1001 #else
1002 std::ostream& SerialBandDenseMatrix<OrdinalType, ScalarType>::print(std::ostream& os) const
1003 #endif
1004 {
1005  os << std::endl;
1006  if(valuesCopied_)
1007  os << "Values_copied : yes" << std::endl;
1008  else
1009  os << "Values_copied : no" << std::endl;
1010  os << "Rows : " << numRows_ << std::endl;
1011  os << "Columns : " << numCols_ << std::endl;
1012  os << "Lower Bandwidth : " << kl_ << std::endl;
1013  os << "Upper Bandwidth : " << ku_ << std::endl;
1014  os << "LDA : " << stride_ << std::endl;
1015  if(numRows_ == 0 || numCols_ == 0) {
1016  os << "(matrix is empty, no values to display)" << std::endl;
1017  } else {
1018 
1019  for(OrdinalType i = 0; i < numRows_; i++) {
1020  for (OrdinalType j=TEUCHOS_MAX(0,i-kl_); j<=TEUCHOS_MIN(numCols_-1,i+ku_); j++) {
1021  os << (*this)(i,j) << " ";
1022  }
1023  os << std::endl;
1024  }
1025  }
1026 #ifdef TEUCHOS_HIDE_DEPRECATED_CODE
1027  return os;
1028 #endif
1029 }
1030 
1031 //----------------------------------------------------------------------------------------------------
1032 // Protected methods
1033 //----------------------------------------------------------------------------------------------------
1034 
1035 template<typename OrdinalType, typename ScalarType>
1036 inline void SerialBandDenseMatrix<OrdinalType, ScalarType>::checkIndex( OrdinalType rowIndex, OrdinalType colIndex ) const {
1037 
1038  TEUCHOS_TEST_FOR_EXCEPTION(rowIndex < 0 || rowIndex >= numRows_ ||
1039  rowIndex < TEUCHOS_MAX(0,colIndex-ku_) || rowIndex > TEUCHOS_MIN(numRows_-1,colIndex+kl_),
1040  std::out_of_range,
1041  "SerialBandDenseMatrix<T>::checkIndex: "
1042  "Row index " << rowIndex << " out of range [0, "<< numRows_ << ")");
1043  TEUCHOS_TEST_FOR_EXCEPTION(colIndex < 0 || colIndex >= numCols_, std::out_of_range,
1044  "SerialBandDenseMatrix<T>::checkIndex: "
1045  "Col index " << colIndex << " out of range [0, "<< numCols_ << ")");
1046 
1047 }
1048 
1049 template<typename OrdinalType, typename ScalarType>
1050 void SerialBandDenseMatrix<OrdinalType, ScalarType>::deleteArrays(void)
1051 {
1052  if (valuesCopied_) {
1053  delete [] values_;
1054  values_ = 0;
1055  valuesCopied_ = false;
1056  }
1057 }
1058 
1059 template<typename OrdinalType, typename ScalarType>
1060 void SerialBandDenseMatrix<OrdinalType, ScalarType>::copyMat(
1061  ScalarType* inputMatrix, OrdinalType strideInput, OrdinalType numRows_in,
1062  OrdinalType numCols_in, ScalarType* outputMatrix, OrdinalType strideOutput, OrdinalType startCol, ScalarType alpha
1063  )
1064 {
1065  OrdinalType i, j;
1066  ScalarType* ptr1 = 0;
1067  ScalarType* ptr2 = 0;
1068 
1069  for(j = 0; j < numCols_in; j++) {
1070  ptr1 = outputMatrix + (j * strideOutput) + TEUCHOS_MAX(0, ku_-j);
1071  ptr2 = inputMatrix + (j + startCol) * strideInput + TEUCHOS_MAX(0, ku_-j);
1072  if (alpha != Teuchos::ScalarTraits<ScalarType>::zero() ) {
1073  for (i=TEUCHOS_MAX(0,j-ku_); i<=TEUCHOS_MIN(numRows_in-1,j+kl_); i++) {
1074  *ptr1++ += alpha*(*ptr2++);
1075  }
1076  } else {
1077  for (i=TEUCHOS_MAX(0,j-ku_); i<=TEUCHOS_MIN(numRows_in-1,j+kl_); i++) {
1078  *ptr1++ = *ptr2++;
1079  }
1080  }
1081  }
1082 }
1083 
1084 #ifndef TEUCHOS_HIDE_DEPRECATED_CODE
1086 template<typename OrdinalType, typename ScalarType>
1087 std::ostream& operator<< (std::ostream& os, const Teuchos::SerialBandDenseMatrix<OrdinalType, ScalarType>& obj)
1088 {
1089  obj.print (os);
1090  return os;
1091 }
1092 #endif
1093 
1095 template<typename OrdinalType, typename ScalarType>
1097 public:
1101  : obj(obj_in) {}
1102 };
1103 
1105 template<typename OrdinalType, typename ScalarType>
1106 std::ostream&
1107 operator<<(std::ostream &out,
1109 {
1110  printer.obj.print(out);
1111  return out;
1112 }
1113 
1115 template<typename OrdinalType, typename ScalarType>
1116 SerialBandDenseMatrixPrinter<OrdinalType,ScalarType>
1118 {
1120 }
1121 
1122 
1123 } // namespace Teuchos
1124 
1125 
1126 #endif /* _TEUCHOS_SERIALBANDDENSEMATRIX_HPP_ */
Templated interface class to BLAS routines.
Object for storing data and providing functionality that is common to all computational classes.
Teuchos header file which uses auto-configuration information to include necessary C++ headers.
Teuchos::DataAccess Mode enumerable type.
Reference-counted pointer class and non-member templated function implementations.
Defines basic traits for the scalar field type.
Templated BLAS wrapper.
Functionality and data that is common to all computational classes.
This class creates and provides basic support for banded dense matrices of templated type.
SerialBandDenseMatrix< OrdinalType, ScalarType > & operator+=(const SerialBandDenseMatrix< OrdinalType, ScalarType > &Source)
Add another matrix to this matrix.
int shapeUninitialized(OrdinalType numRows, OrdinalType numCols, OrdinalType kl, OrdinalType ku)
Same as shape() except leaves uninitialized.
int random()
Set all values in the matrix to be random numbers.
OrdinalType numCols() const
Returns the column dimension of this matrix.
ScalarTraits< ScalarType >::magnitudeType normFrobenius() const
Returns the Frobenius-norm of the matrix.
ScalarType scalarType
Typedef for scalar type.
SerialBandDenseMatrix< OrdinalType, ScalarType > & operator=(const SerialBandDenseMatrix< OrdinalType, ScalarType > &Source)
Copies values from one matrix to another.
bool empty() const
Returns whether this matrix is empty.
OrdinalType ordinalType
Typedef for ordinal type.
OrdinalType numRows() const
Returns the row dimension of this matrix.
int scale(const ScalarType alpha)
Scale this matrix by alpha; *this = alpha**this.
SerialBandDenseMatrix< OrdinalType, ScalarType > & operator-=(const SerialBandDenseMatrix< OrdinalType, ScalarType > &Source)
Subtract another matrix from this matrix.
ScalarType * operator[](OrdinalType colIndex)
Column access method (non-const).
int putScalar(const ScalarType value=Teuchos::ScalarTraits< ScalarType >::zero())
Set all values in the matrix to a constant value.
SerialBandDenseMatrix< OrdinalType, ScalarType > & operator*=(const ScalarType alpha)
Scale this matrix by alpha; *this = alpha**this.
OrdinalType lowerBandwidth() const
Returns the lower bandwidth of this matrix.
bool operator==(const SerialBandDenseMatrix< OrdinalType, ScalarType > &Operand) const
Equality of two matrices.
int reshape(OrdinalType numRows, OrdinalType numCols, OrdinalType kl, OrdinalType ku)
Reshaping method for changing the size of a SerialBandDenseMatrix, keeping the entries.
OrdinalType stride() const
Returns the stride between the columns of this matrix in memory.
SerialBandDenseMatrix< OrdinalType, ScalarType > & assign(const SerialBandDenseMatrix< OrdinalType, ScalarType > &Source)
Copies values from one matrix to another.
ScalarType * values() const
Data array access method.
bool operator!=(const SerialBandDenseMatrix< OrdinalType, ScalarType > &Operand) const
Inequality of two matrices.
ScalarType & operator()(OrdinalType rowIndex, OrdinalType colIndex)
Element access method (non-const).
int shape(OrdinalType numRows, OrdinalType numCols, OrdinalType kl, OrdinalType ku)
Shape method for changing the size of a SerialBandDenseMatrix, initializing entries to zero.
virtual void print(std::ostream &os) const
Print method. Defines the behavior of the std::ostream << operator.
OrdinalType upperBandwidth() const
Returns the upper bandwidth of this matrix.
ScalarTraits< ScalarType >::magnitudeType normInf() const
Returns the Infinity-norm of the matrix.
ScalarTraits< ScalarType >::magnitudeType normOne() const
Returns the 1-norm of the matrix.
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
Macro for throwing an exception with breakpointing to ease debugging.
The Teuchos namespace contains all of the classes, structs and enums used by Teuchos,...
SerialBandDenseMatrixPrinter< OrdinalType, ScalarType > printMat(const SerialBandDenseMatrix< OrdinalType, ScalarType > &obj)
Return SerialBandDenseMatrix ostream manipulator Use as:
This structure defines some basic traits for a scalar field type.
static magnitudeType magnitude(T a)
Returns the magnitudeType of the scalar type a.
static T zero()
Returns representation of zero for this scalar type.
Ostream manipulator for SerialBandDenseMatrix.