00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048 #ifndef _TEUCHOS_SERIALDENSEMATRIX_HPP_
00049 #define _TEUCHOS_SERIALDENSEMATRIX_HPP_
00050
00054 #include "Teuchos_CompObject.hpp"
00055 #include "Teuchos_BLAS.hpp"
00056 #include "Teuchos_ScalarTraits.hpp"
00057 #include "Teuchos_DataAccess.hpp"
00058 #include "Teuchos_ConfigDefs.hpp"
00059 #include "Teuchos_TestForException.hpp"
00060
00069 namespace Teuchos {
00070
00071 template<typename OrdinalType, typename ScalarType>
00072 class SerialDenseMatrix : public CompObject, public Object, public BLAS<OrdinalType, ScalarType>
00073 {
00074 public:
00075
00077
00079
00082 SerialDenseMatrix();
00083
00085
00092 SerialDenseMatrix( int numRows, int numCols);
00093
00095
00103 SerialDenseMatrix(DataAccess CV, ScalarType* values, int stride, int numRows, int numCols);
00104
00106 SerialDenseMatrix(const SerialDenseMatrix<OrdinalType, ScalarType> &Source);
00107
00109
00121 SerialDenseMatrix(DataAccess CV, const SerialDenseMatrix<OrdinalType, ScalarType> &Source, int numRows, int numCols, int startRow=0, int startCol=0);
00122
00124 virtual ~SerialDenseMatrix();
00126
00128
00129
00139 int shape(int numRows, int numCols);
00140
00142
00152 int reshape(int numRows, int numCols);
00153
00155
00157
00159
00165 SerialDenseMatrix& operator= (const SerialDenseMatrix& Source);
00166
00168
00172 int putScalar( const ScalarType value = Teuchos::ScalarTraits<ScalarType>::zero() );
00173
00175 int random();
00176
00178
00180
00182
00189 ScalarType& operator () (int rowIndex, int colIndex);
00190
00192
00199 const ScalarType& operator () (int rowIndex, int colIndex) const;
00200
00202
00209 ScalarType* operator [] (int colIndex);
00210
00212
00219 const ScalarType* operator [] (int colIndex) const;
00220
00222
00223 ScalarType* values() const { return(values_); };
00224
00226
00228
00230
00233 SerialDenseMatrix& operator+= (const SerialDenseMatrix& Source);
00234
00236
00240 int scale ( const ScalarType alpha );
00241
00243
00257 int multiply (ETransp transa, ETransp transb, ScalarType alpha, const SerialDenseMatrix<OrdinalType, ScalarType> &A, const SerialDenseMatrix<OrdinalType, ScalarType> &B, ScalarType beta);
00259
00261
00263
00266 bool operator== (const SerialDenseMatrix<OrdinalType, ScalarType> &Operand);
00267
00269
00272 bool operator!= (const SerialDenseMatrix<OrdinalType, ScalarType> &Operand);
00273
00275
00277
00279 int numRows() const { return(numRows_); };
00280
00282 int numCols() const { return(numCols_); };
00283
00285 int stride() const { return(stride_); };
00287
00289
00291 typename ScalarTraits<ScalarType>::magnitudeType normOne() const;
00292
00294 typename ScalarTraits<ScalarType>::magnitudeType normInf() const;
00295
00297 typename ScalarTraits<ScalarType>::magnitudeType normFrobenius() const;
00299
00301
00302 virtual void print(ostream& os) const;
00303
00305 protected:
00306 void copyMat(ScalarType* inputMatrix, int strideInput, int numRows, int numCols, ScalarType* outputMatrix, int strideOutput, int startRow, int startCol, bool add);
00307 void deleteArrays();
00308 void checkIndex( int rowIndex, int colIndex = 0 ) const;
00309 int numRows_;
00310 int numCols_;
00311 int stride_;
00312 bool valuesCopied_;
00313 ScalarType* values_;
00314
00315 };
00316
00317
00318
00319
00320
00321 template<typename OrdinalType, typename ScalarType>
00322 SerialDenseMatrix<OrdinalType, ScalarType>::SerialDenseMatrix() : CompObject(), numRows_(0), numCols_(0), stride_(0), valuesCopied_(false), values_(0) {}
00323
00324 template<typename OrdinalType, typename ScalarType>
00325 SerialDenseMatrix<OrdinalType, ScalarType>::SerialDenseMatrix( int numRows, int numCols ) : CompObject(), numRows_(numRows), numCols_(numCols), stride_(numRows)
00326 {
00327 values_ = new ScalarType[stride_*numCols_];
00328 putScalar();
00329 valuesCopied_ = true;
00330 }
00331
00332 template<typename OrdinalType, typename ScalarType>
00333 SerialDenseMatrix<OrdinalType, ScalarType>::SerialDenseMatrix(DataAccess CV, ScalarType* values, int stride, int numRows, int numCols) : CompObject(), numRows_(numRows), numCols_(numCols), stride_(stride), valuesCopied_(false), values_(values)
00334 {
00335 if(CV == Copy)
00336 {
00337 stride_ = numRows_;
00338 values_ = new ScalarType[stride_*numCols_];
00339 copyMat(values, stride, numRows_, numCols_, values_, stride_, 0, 0, false);
00340 valuesCopied_ = true;
00341 }
00342 }
00343
00344 template<typename OrdinalType, typename ScalarType>
00345 SerialDenseMatrix<OrdinalType, ScalarType>::SerialDenseMatrix(const SerialDenseMatrix<OrdinalType, ScalarType> &Source) : CompObject(), numRows_(Source.numRows_), numCols_(Source.numCols_), stride_(Source.stride_), valuesCopied_(true), values_(Source.values_)
00346 {
00347 stride_ = numRows_;
00348 values_ = new ScalarType[stride_*numCols_];
00349 copyMat(Source.values_, Source.stride_, numRows_, numCols_, values_, stride_, 0, 0, false);
00350 }
00351
00352 template<typename OrdinalType, typename ScalarType>
00353 SerialDenseMatrix<OrdinalType, ScalarType>::SerialDenseMatrix(DataAccess CV, const SerialDenseMatrix<OrdinalType, ScalarType> &Source, int numRows, int numCols, int startRow, int startCol) : CompObject(), numRows_(numRows), numCols_(numCols), stride_(Source.stride_), valuesCopied_(false), values_(Source.values_)
00354 {
00355 if(CV == Copy)
00356 {
00357 stride_ = numRows;
00358 values_ = new ScalarType[stride_ * numCols];
00359 copyMat(Source.values_, Source.stride_, numRows, numCols, values_, stride_, startRow, startCol, false);
00360 valuesCopied_ = true;
00361 }
00362 else
00363 {
00364 values_ = values_ + (stride_ * startCol) + startRow;
00365 }
00366 }
00367
00368 template<typename OrdinalType, typename ScalarType>
00369 SerialDenseMatrix<OrdinalType, ScalarType>::~SerialDenseMatrix()
00370 {
00371 deleteArrays();
00372 }
00373
00374
00375
00376
00377
00378 template<typename OrdinalType, typename ScalarType>
00379 int SerialDenseMatrix<OrdinalType, ScalarType>::shape(int numRows, int numCols)
00380 {
00381 deleteArrays();
00382 numRows_ = numRows;
00383 numCols_ = numCols;
00384 stride_ = numRows_;
00385 values_ = new ScalarType[stride_*numCols_];
00386 putScalar();
00387 valuesCopied_ = true;
00388 return(0);
00389 }
00390
00391 template<typename OrdinalType, typename ScalarType>
00392 int SerialDenseMatrix<OrdinalType, ScalarType>::reshape(int numRows, int numCols)
00393 {
00394
00395 ScalarType* values_tmp = new ScalarType[numRows * numCols];
00396 ScalarType zero = ScalarTraits<ScalarType>::zero();
00397 for(int k = 0; k < numRows * numCols; k++)
00398 {
00399 values_tmp[k] = zero;
00400 }
00401 int numRows_tmp = TEUCHOS_MIN(numRows_, numRows);
00402 int numCols_tmp = TEUCHOS_MIN(numCols_, numCols);
00403 if(values_ != 0)
00404 {
00405 copyMat(values_, stride_, numRows_tmp, numCols_tmp, values_tmp, numRows, 0, 0, false);
00406 }
00407 deleteArrays();
00408 numRows_ = numRows;
00409 numCols_ = numCols;
00410 stride_ = numRows_;
00411 values_ = values_tmp;
00412 valuesCopied_ = true;
00413 return(0);
00414 }
00415
00416
00417
00418
00419
00420 template<typename OrdinalType, typename ScalarType>
00421 int SerialDenseMatrix<OrdinalType, ScalarType>::putScalar( const ScalarType value )
00422 {
00423
00424 for(int j = 0; j < numCols_; j++)
00425 {
00426 for(int i = 0; i < numRows_; i++)
00427 {
00428 values_[i + j*stride_] = value;
00429 }
00430 }
00431 return 0;
00432 }
00433
00434 template<typename OrdinalType, typename ScalarType>
00435 int SerialDenseMatrix<OrdinalType, ScalarType>::random()
00436 {
00437
00438 for(int j = 0; j < numCols_; j++)
00439 {
00440 for(int i = 0; i < numRows_; i++)
00441 {
00442 values_[i + j*stride_] = ScalarTraits<ScalarType>::random();
00443 }
00444 }
00445 return 0;
00446 }
00447
00448 template<typename OrdinalType, typename ScalarType>
00449 SerialDenseMatrix<OrdinalType,ScalarType>& SerialDenseMatrix<OrdinalType, ScalarType>::operator= (const SerialDenseMatrix<OrdinalType,ScalarType>& Source) {
00450 if(this == &Source)
00451 return(*this);
00452 if((!valuesCopied_) && (!Source.valuesCopied_) && (values_ == Source.values_))
00453 return(*this);
00454
00455
00456 if (!Source.valuesCopied_) {
00457 if(valuesCopied_) {
00458
00459 deleteArrays();
00460 }
00461 numRows_ = Source.numRows_;
00462 numCols_ = Source.numCols_;
00463 stride_ = Source.stride_;
00464 values_ = Source.values_;
00465 }
00466 else {
00467
00468 if(!valuesCopied_) {
00469 numRows_ = Source.numRows_;
00470 numCols_ = Source.numCols_;
00471 stride_ = Source.numRows_;
00472 const int newsize = stride_ * numCols_;
00473 if(newsize > 0) {
00474 values_ = new ScalarType[newsize];
00475 valuesCopied_ = true;
00476 }
00477 else {
00478 values_ = 0;
00479 }
00480 }
00481
00482 else {
00483 if((Source.numRows_ <= stride_) && (Source.numCols_ == numCols_)) {
00484 numRows_ = Source.numRows_;
00485 numCols_ = Source.numCols_;
00486 }
00487 else {
00488 deleteArrays();
00489 numRows_ = Source.numRows_;
00490 numCols_ = Source.numCols_;
00491 stride_ = Source.numRows_;
00492 const int newsize = stride_ * numCols_;
00493 if(newsize > 0) {
00494 values_ = new ScalarType[newsize];
00495 valuesCopied_ = true;
00496 }
00497 }
00498 }
00499 copyMat(Source.values_, Source.stride_, numRows_, numCols_, values_, stride_, 0, 0, false);
00500 }
00501 return(*this);
00502 }
00503
00504 template<typename OrdinalType, typename ScalarType>
00505 SerialDenseMatrix<OrdinalType, ScalarType>& SerialDenseMatrix<OrdinalType, ScalarType>::operator+= (const SerialDenseMatrix<OrdinalType,ScalarType>& Source )
00506 {
00507
00508 if ((numRows_ != Source.numRows_) || (numCols_ != Source.numCols_))
00509 {
00510 TEUCHOS_CHK_REF(*this);
00511 }
00512 copyMat(Source.values_, Source.stride_, numRows_, numCols_, values_, stride_, 0, 0, true);
00513 return(*this);
00514 }
00515
00516
00517
00518
00519
00520 template<typename OrdinalType, typename ScalarType>
00521 inline ScalarType& SerialDenseMatrix<OrdinalType, ScalarType>::operator () (int rowIndex, int colIndex)
00522 {
00523 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
00524 checkIndex( rowIndex, colIndex );
00525 #endif
00526 return(values_[colIndex * stride_ + rowIndex]);
00527 }
00528
00529 template<typename OrdinalType, typename ScalarType>
00530 inline const ScalarType& SerialDenseMatrix<OrdinalType, ScalarType>::operator () (int rowIndex, int colIndex) const
00531 {
00532 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
00533 checkIndex( rowIndex, colIndex );
00534 #endif
00535 return(values_[colIndex * stride_ + rowIndex]);
00536 }
00537
00538 template<typename OrdinalType, typename ScalarType>
00539 inline const ScalarType* SerialDenseMatrix<OrdinalType, ScalarType>::operator [] (int colIndex) const
00540 {
00541 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
00542 checkIndex( 0, colIndex );
00543 #endif
00544 return(values_ + colIndex * stride_);
00545 }
00546
00547 template<typename OrdinalType, typename ScalarType>
00548 inline ScalarType* SerialDenseMatrix<OrdinalType, ScalarType>::operator [] (int colIndex)
00549 {
00550 #ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK
00551 checkIndex( 0, colIndex );
00552 #endif
00553 return(values_ + colIndex * stride_);
00554 }
00555
00556
00557
00558
00559
00560 template<typename OrdinalType, typename ScalarType>
00561 typename ScalarTraits<ScalarType>::magnitudeType SerialDenseMatrix<OrdinalType, ScalarType>::normOne() const
00562 {
00563 int i, j;
00564 typename ScalarTraits<ScalarType>::magnitudeType anorm = ScalarTraits<ScalarType>::magnitude(ScalarTraits<ScalarType>::zero());
00565 typename ScalarTraits<ScalarType>::magnitudeType absSum = ScalarTraits<ScalarType>::magnitude(ScalarTraits<ScalarType>::zero());
00566 ScalarType* ptr;
00567 for(j = 0; j < numCols_; j++)
00568 {
00569 ScalarType sum = 0;
00570 ptr = values_ + j * stride_;
00571 for(i = 0; i < numRows_; i++)
00572 {
00573 sum += ScalarTraits<ScalarType>::magnitude(*ptr++);
00574 }
00575 absSum = ScalarTraits<ScalarType>::magnitude(sum);
00576 if(absSum > anorm)
00577 {
00578 anorm = absSum;
00579 }
00580 }
00581 updateFlops(numRows_ * numCols_);
00582 return(anorm);
00583 }
00584
00585 template<typename OrdinalType, typename ScalarType>
00586 typename ScalarTraits<ScalarType>::magnitudeType SerialDenseMatrix<OrdinalType, ScalarType>::normInf() const
00587 {
00588 int i, j;
00589 typename ScalarTraits<ScalarType>::magnitudeType sum, anorm = ScalarTraits<ScalarType>::magnitude(ScalarTraits<ScalarType>::zero());
00590
00591 for (i = 0; i < numRows_; i++) {
00592 sum = ScalarTraits<ScalarType>::magnitude(ScalarTraits<ScalarType>::zero());
00593 for (j=0; j< numCols_; j++) {
00594 sum += ScalarTraits<ScalarType>::magnitude(*(values_+i+j*stride_));
00595 }
00596 anorm = TEUCHOS_MAX( anorm, sum );
00597 }
00598 updateFlops(numRows_ * numCols_);
00599 return(anorm);
00600 }
00601
00602 template<typename OrdinalType, typename ScalarType>
00603 typename ScalarTraits<ScalarType>::magnitudeType SerialDenseMatrix<OrdinalType, ScalarType>::normFrobenius() const
00604 {
00605 int i, j;
00606 typename ScalarTraits<ScalarType>::magnitudeType anorm = ScalarTraits<ScalarType>::magnitude(ScalarTraits<ScalarType>::zero());
00607 for (j = 0; j < numCols_; j++) {
00608 for (i = 0; i < numRows_; i++) {
00609 anorm += ScalarTraits<ScalarType>::magnitude(values_[i+j*stride_]*values_[i+j*stride_]);
00610 }
00611 }
00612 anorm = ScalarTraits<ScalarType>::magnitude(ScalarTraits<ScalarType>::squareroot(anorm));
00613 updateFlops(numRows_ * numCols_);
00614 return(anorm);
00615 }
00616
00617
00618
00619
00620
00621 template<typename OrdinalType, typename ScalarType>
00622 bool SerialDenseMatrix<OrdinalType, ScalarType>::operator== (const SerialDenseMatrix<OrdinalType, ScalarType> &Operand)
00623 {
00624 bool result = 1;
00625 if((numRows_ != Operand.numRows_) || (numCols_ != Operand.numCols_))
00626 {
00627 result = 0;
00628 }
00629 else
00630 {
00631 int i, j;
00632 for(i = 0; i < numRows_; i++)
00633 {
00634 for(j = 0; j < numCols_; j++)
00635 {
00636 if((*this)(i, j) != Operand(i, j))
00637 {
00638 return 0;
00639 }
00640 }
00641 }
00642 }
00643 return result;
00644 }
00645
00646 template<typename OrdinalType, typename ScalarType>
00647 bool SerialDenseMatrix<OrdinalType, ScalarType>::operator!= (const SerialDenseMatrix<OrdinalType, ScalarType> &Operand)
00648 {
00649 return !((*this) == Operand);
00650 }
00651
00652
00653
00654
00655
00656 template<typename OrdinalType, typename ScalarType>
00657 int SerialDenseMatrix<OrdinalType, ScalarType>::scale( const ScalarType alpha )
00658 {
00659 int i, j;
00660 ScalarType* ptr;
00661
00662 for (j=0; j<numCols_; j++) {
00663 ptr = values_ + j*stride_;
00664 for (i=0; i<numRows_; i++) { *ptr = alpha * (*ptr); ptr++; }
00665 }
00666 updateFlops( numRows_*numCols_ );
00667 return(0);
00668 }
00669
00670
00671 template<typename OrdinalType, typename ScalarType>
00672 int SerialDenseMatrix<OrdinalType, ScalarType>::multiply(ETransp transa, ETransp transb, ScalarType alpha, const SerialDenseMatrix<OrdinalType, ScalarType> &A, const SerialDenseMatrix<OrdinalType, ScalarType> &B, ScalarType beta)
00673 {
00674
00675 int A_nrows = (ETranspChar[transa]=='T') ? A.numCols() : A.numRows();
00676 int A_ncols = (ETranspChar[transa]=='T') ? A.numRows() : A.numCols();
00677 int B_nrows = (ETranspChar[transb]=='T') ? B.numCols() : B.numRows();
00678 int B_ncols = (ETranspChar[transb]=='T') ? B.numRows() : B.numCols();
00679 if ((numRows_ != A_nrows) || (A_ncols != B_nrows) || (numCols_ != B_ncols))
00680 {
00681 TEUCHOS_CHK_ERR(-1);
00682 }
00683
00684 GEMM(transa, transb, numRows_, numCols_, A_ncols, alpha, A.values(), A.stride(), B.values(), B.stride(), beta, values_, stride_);
00685 double nflops = 2 * numRows_;
00686 nflops *= numCols_;
00687 nflops *= A_ncols;
00688 updateFlops(nflops);
00689 return(0);
00690 }
00691
00692
00693 template<typename OrdinalType, typename ScalarType>
00694 void SerialDenseMatrix<OrdinalType, ScalarType>::print(ostream& os) const
00695 {
00696 os << endl;
00697 if(valuesCopied_)
00698 os << "Values_copied : yes" << endl;
00699 else
00700 os << "Values_copied : no" << endl;
00701 os << "Rows : " << numRows_ << endl;
00702 os << "Columns : " << numCols_ << endl;
00703 os << "LDA : " << stride_ << endl;
00704 if(numRows_ == 0 || numCols_ == 0) {
00705 os << "(matrix is empty, no values to display)" << endl;
00706 } else {
00707 for(int i = 0; i < numRows_; i++) {
00708 for(int j = 0; j < numCols_; j++){
00709 os << (*this)(i,j) << " ";
00710 }
00711 os << endl;
00712 }
00713 }
00714 }
00715
00716
00717
00718
00719
00720 template<typename OrdinalType, typename ScalarType>
00721 inline void SerialDenseMatrix<OrdinalType, ScalarType>::checkIndex( int rowIndex, int colIndex ) const {
00722 TEST_FOR_EXCEPTION(rowIndex < 0 || rowIndex >= numRows_, std::out_of_range,
00723 "SerialDenseMatrix<T>::checkIndex: "
00724 "Row index " << rowIndex << " out of range [0, "<< numRows_ << ")");
00725 TEST_FOR_EXCEPTION(colIndex < 0 || colIndex >= numCols_, std::out_of_range,
00726 "SerialDenseMatrix<T>::checkIndex: "
00727 "Col index " << colIndex << " out of range [0, "<< numCols_ << ")");
00728 }
00729
00730 template<typename OrdinalType, typename ScalarType>
00731 void SerialDenseMatrix<OrdinalType, ScalarType>::deleteArrays(void)
00732 {
00733 if (valuesCopied_)
00734 {
00735 delete [] values_;
00736 values_ = 0;
00737 valuesCopied_ = false;
00738 }
00739 }
00740
00741 template<typename OrdinalType, typename ScalarType>
00742 void SerialDenseMatrix<OrdinalType, ScalarType>::copyMat(ScalarType* inputMatrix, int strideInput, int numRows, int numCols, ScalarType* outputMatrix, int strideOutput, int startRow, int startCol, bool add)
00743 {
00744 int i, j;
00745 ScalarType* ptr1;
00746 ScalarType* ptr2;
00747 for(j = 0; j < numCols; j++) {
00748 ptr1 = outputMatrix + (j * strideOutput);
00749 ptr2 = inputMatrix + (j + startCol) * strideInput + startRow;
00750 if (add) {
00751 for(i = 0; i < numRows; i++)
00752 {
00753 *ptr1++ += *ptr2++;
00754 }
00755 } else {
00756 for(i = 0; i < numRows; i++)
00757 {
00758 *ptr1++ = *ptr2++;
00759 }
00760 }
00761 }
00762 }
00763
00764 }
00765
00766
00767
00768 #endif