|
Teuchos - Trilinos Tools Package
Version of the Day
|
00001 // @HEADER 00002 // *********************************************************************** 00003 // 00004 // Teuchos: Common Tools Package 00005 // Copyright (2004) Sandia Corporation 00006 // 00007 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive 00008 // license for use of this work by or on behalf of the U.S. Government. 00009 // 00010 // Redistribution and use in source and binary forms, with or without 00011 // modification, are permitted provided that the following conditions are 00012 // met: 00013 // 00014 // 1. Redistributions of source code must retain the above copyright 00015 // notice, this list of conditions and the following disclaimer. 00016 // 00017 // 2. Redistributions in binary form must reproduce the above copyright 00018 // notice, this list of conditions and the following disclaimer in the 00019 // documentation and/or other materials provided with the distribution. 00020 // 00021 // 3. Neither the name of the Corporation nor the names of the 00022 // contributors may be used to endorse or promote products derived from 00023 // this software without specific prior written permission. 00024 // 00025 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY 00026 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00027 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 00028 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE 00029 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 00030 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 00031 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 00032 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 00033 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 00034 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 00035 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00036 // 00037 // Questions? Contact Michael A. Heroux (maherou@sandia.gov) 00038 // 00039 // *********************************************************************** 00040 // @HEADER 00041 00042 00043 #ifndef _TEUCHOS_SERIALTRIDIMATRIX_HPP_ 00044 #define _TEUCHOS_SERIALTRIDIMATRIX_HPP_ 00045 00049 #include "Teuchos_CompObject.hpp" 00050 #include "Teuchos_BLAS.hpp" 00051 #include "Teuchos_ScalarTraits.hpp" 00052 #include "Teuchos_DataAccess.hpp" 00053 #include "Teuchos_ConfigDefs.hpp" 00054 #include "Teuchos_Assert.hpp" 00055 00064 namespace Teuchos { 00065 00066 template<typename OrdinalType, typename ScalarType> 00067 class SerialTriDiMatrix : public CompObject, public Object, public BLAS<OrdinalType, ScalarType > { 00068 public: 00069 00071 typedef OrdinalType ordinalType; 00073 typedef ScalarType scalarType; 00074 00076 00077 00079 00082 SerialTriDiMatrix(); 00083 00085 00093 SerialTriDiMatrix(OrdinalType numRows, OrdinalType numCols, bool zeroOut = true); 00094 00096 00102 SerialTriDiMatrix(DataAccess CV, ScalarType* values, OrdinalType numRowsCols); 00103 00105 00110 SerialTriDiMatrix(const SerialTriDiMatrix<OrdinalType, ScalarType> &Source, ETransp trans = Teuchos::NO_TRANS); 00111 00113 00124 SerialTriDiMatrix(DataAccess CV, const SerialTriDiMatrix<OrdinalType, ScalarType> &Source, OrdinalType numRowsCols, OrdinalType startRowCols=0); 00125 00127 virtual ~SerialTriDiMatrix(); 00129 00131 00132 00133 00142 int shape(OrdinalType numRows); 00143 00145 int shapeUninitialized(OrdinalType numRows); 00146 00148 00157 int reshape(OrdinalType numRowsCols); 00158 00160 00162 00163 00165 00171 SerialTriDiMatrix<OrdinalType, ScalarType>& operator= (const SerialTriDiMatrix<OrdinalType, ScalarType>& Source); 00172 00174 00179 SerialTriDiMatrix<OrdinalType, ScalarType>& assign (const SerialTriDiMatrix<OrdinalType, ScalarType>& Source); 00180 00182 00185 SerialTriDiMatrix<OrdinalType, ScalarType>& operator= (const ScalarType value) { putScalar(value); return(*this); } 00186 00188 00192 int putScalar( const ScalarType value = Teuchos::ScalarTraits<ScalarType>::zero() ); 00193 00195 // int random(); 00196 00198 00200 00201 00203 00210 ScalarType& operator () (OrdinalType rowIndex, OrdinalType colIndex); 00211 00213 00220 const ScalarType& operator () (OrdinalType rowIndex, OrdinalType colIndex) const; 00221 00223 00230 // ScalarType* operator [] (OrdinalType colIndex); 00231 00233 00240 // const ScalarType* operator [] (OrdinalType colIndex) const; 00241 00243 00244 ScalarType* values() const { return(values_); } 00245 00246 ScalarType* D() const { return D_;} 00247 ScalarType* DL() const { return DL_;} 00248 ScalarType* DU() const { return DU_;} 00249 ScalarType* DU2() const { return DU2_;} 00250 00252 00254 00255 00257 00260 SerialTriDiMatrix<OrdinalType, ScalarType>& operator+= (const SerialTriDiMatrix<OrdinalType, ScalarType>& Source); 00261 00263 00266 SerialTriDiMatrix<OrdinalType, ScalarType>& operator-= (const SerialTriDiMatrix<OrdinalType, ScalarType>& Source); 00267 00269 00272 SerialTriDiMatrix<OrdinalType, ScalarType>& operator*= (const ScalarType alpha); 00273 00275 00279 int scale ( const ScalarType alpha ); 00280 00282 00288 int scale ( const SerialTriDiMatrix<OrdinalType, ScalarType>& A ); 00289 00291 00305 //int multiply (ETransp transa, ETransp transb, ScalarType alpha, const SerialTriDiMatrix<OrdinalType, ScalarType> &A, const SerialTriDiMatrix<OrdinalType, ScalarType> &B, ScalarType beta); 00306 00308 00319 //int multiply (ESide sideA, ScalarType alpha, const SerialSymTriDiMatrix<OrdinalType, ScalarType> &A, const SerialTriDiMatrix<OrdinalType, ScalarType> &B, ScalarType beta); 00320 00322 00324 00325 00327 00330 bool operator== (const SerialTriDiMatrix<OrdinalType, ScalarType> &Operand) const; 00331 00333 00336 bool operator!= (const SerialTriDiMatrix<OrdinalType, ScalarType> &Operand) const; 00337 00339 00341 00342 00344 OrdinalType numRowsCols() const { return(numRowsCols_); } 00345 00347 // OrdinalType numCols() const { return(numRowsCols_); } 00348 00350 // OrdinalType stride() const { return(stride_); } 00351 00353 bool empty() const { return(numRowsCols_ == 0); } 00355 00357 00358 00360 typename ScalarTraits<ScalarType>::magnitudeType normOne() const; 00361 00363 typename ScalarTraits<ScalarType>::magnitudeType normInf() const; 00364 00366 typename ScalarTraits<ScalarType>::magnitudeType normFrobenius() const; 00368 00370 00371 00372 virtual void print(std::ostream& os) const; 00373 00375 protected: 00376 void copyMat(SerialTriDiMatrix<OrdinalType, ScalarType> matrix, 00377 OrdinalType startCol, 00378 ScalarType alpha = ScalarTraits<ScalarType>::zero() ); 00379 void deleteArrays(); 00380 void checkIndex( OrdinalType rowIndex, OrdinalType colIndex = 0 ) const; 00381 OrdinalType numRowsCols_; 00382 00383 bool valuesCopied_; 00384 ScalarType* values_; 00385 ScalarType* DL_; 00386 ScalarType* D_; 00387 ScalarType* DU_; 00388 ScalarType* DU2_; 00389 00390 }; // class Teuchos_SerialTriDiMatrix 00391 00392 //---------------------------------------------------------------------------------------------------- 00393 // Constructors and Destructor 00394 //---------------------------------------------------------------------------------------------------- 00395 00396 template<typename OrdinalType, typename ScalarType> 00397 SerialTriDiMatrix<OrdinalType, ScalarType>::SerialTriDiMatrix() 00398 : 00399 CompObject(), 00400 Object("Teuchos::SerialTriDiMatrix"), 00401 numRowsCols_(0), 00402 valuesCopied_(false), 00403 values_(0), 00404 DL_(NULL), 00405 D_(NULL), 00406 DU_(NULL), 00407 DU2_(NULL) 00408 {} 00409 00410 template<typename OrdinalType, typename ScalarType> 00411 SerialTriDiMatrix<OrdinalType, ScalarType>::SerialTriDiMatrix( OrdinalType numRowsCols_in, OrdinalType numCols_in, bool zeroOut) 00412 : CompObject(), Object("Teuchos::SerialTriDiMatrix"), numRowsCols_(numRowsCols_in) { 00413 00414 OrdinalType numvals = (numRowsCols_ == 1) ? 1 : 4*(numRowsCols_-1); 00415 values_ = new ScalarType [numvals]; 00416 DL_ = values_; 00417 D_ = DL_ + (numRowsCols_-1); 00418 DU_ = D_ + numRowsCols_; 00419 DU2_ = DU_ + (numRowsCols_-1); 00420 00421 valuesCopied_ = true; 00422 if (zeroOut == true) 00423 putScalar(); 00424 } 00425 00426 template<typename OrdinalType, typename ScalarType> 00427 SerialTriDiMatrix<OrdinalType, ScalarType>::SerialTriDiMatrix(DataAccess CV, ScalarType* values_in, OrdinalType numRowsCols_in ) 00428 : CompObject(), Object("Teuchos::SerialTriDiMatrix"), numRowsCols_(numRowsCols_in), 00429 valuesCopied_(false), values_(values_in) 00430 { 00431 if(CV == Copy) { 00432 const OrdinalType numvals = (numRowsCols_ == 1) ? 1 : 4*(numRowsCols_-1); 00433 values_ = new ScalarType[numvals]; 00434 DL_ = values_; 00435 D_ = DL_ + (numRowsCols_-1); 00436 DU_ = D_ + numRowsCols_; 00437 DU2_ = DU_ + (numRowsCols_-1); 00438 valuesCopied_ = true; 00439 00440 for(OrdinalType i = 0 ; i < numRowsCols_ ; ++i ) 00441 values_[i] = values_in[i]; 00442 } 00443 } 00444 00445 template<typename OrdinalType, typename ScalarType> 00446 SerialTriDiMatrix<OrdinalType, ScalarType>::SerialTriDiMatrix(const SerialTriDiMatrix<OrdinalType, ScalarType> &Source, ETransp trans) : CompObject(), Object("Teuchos::SerialTriDiMatrix"), BLAS<OrdinalType,ScalarType>(), numRowsCols_(0), valuesCopied_(true), values_(0) 00447 { 00448 if ( trans == Teuchos::NO_TRANS ) { 00449 numRowsCols_ = Source.numRowsCols_; 00450 00451 const OrdinalType numvals = (numRowsCols_ == 1) ? 1 : 4*(numRowsCols_-1); 00452 values_ = new ScalarType[numvals]; 00453 DL_ = values_; 00454 D_ = DL_+ (numRowsCols_-1); 00455 DU_ = D_ + numRowsCols_; 00456 DU2_ = DU_ + (numRowsCols_-1); 00457 00458 copyMat(Source, 0, 0); 00459 } 00460 else if ( trans == Teuchos::CONJ_TRANS && ScalarTraits<ScalarType>::isComplex ) 00461 { 00462 numRowsCols_ = Source.numRowsCols_; 00463 const OrdinalType numvals = (numRowsCols_ == 1) ? 1 : 4*(numRowsCols_-1); 00464 values_ = new ScalarType[numvals]; 00465 DL_ = values_; 00466 D_ = DL_+(numRowsCols_-1); 00467 DU_ = D_ + numRowsCols_; 00468 DU2_ = DU_ + (numRowsCols_-1); 00469 00470 OrdinalType min = numRowsCols_; 00471 if(min > Source.numRowsCols_) min = Source.numRowsCols_; 00472 00473 for(OrdinalType i = 0 ; i< min ; ++i) { 00474 D_ = Teuchos::ScalarTraits<ScalarType>::conjugate(Source.D_[i]); 00475 if(i < (min-1)) { 00476 DL_ = Teuchos::ScalarTraits<ScalarType>::conjugate(Source.DL_[i]); 00477 DU_ = Teuchos::ScalarTraits<ScalarType>::conjugate(Source.DU_[i]); 00478 } 00479 if(i < (min-2)) { 00480 DU2_ = Teuchos::ScalarTraits<ScalarType>::conjugate(Source.DU2_[i]); 00481 } 00482 } 00483 } 00484 else 00485 { 00486 numRowsCols_ = Source.numCols_; 00487 const OrdinalType numvals = (numRowsCols_ == 1) ? 1 : 4*(numRowsCols_-1); 00488 values_ = new ScalarType[numvals]; 00489 OrdinalType min = numRowsCols_; 00490 if(min > Source.numRowsCols_) min = Source.numRowsCols_; 00491 for(OrdinalType i = 0 ; i< min ; ++i) { 00492 D_ = Source.D_[i]; 00493 if(i < (min-1)) { 00494 DL_ = Source.DL_[i]; 00495 DU_ = Source.DU_[i]; 00496 } 00497 if(i < (min-2)) { 00498 DU2_ = Source.DU2_[i]; 00499 } 00500 } 00501 } 00502 } 00503 00504 template<typename OrdinalType, typename ScalarType> 00505 SerialTriDiMatrix<OrdinalType, ScalarType>::SerialTriDiMatrix( 00506 DataAccess CV, const SerialTriDiMatrix<OrdinalType, ScalarType> &Source, 00507 OrdinalType numRowsCols_in, OrdinalType startRow ) 00508 : CompObject(), Object("Teuchos::SerialTriDiMatrix"), numRowsCols_(numRowsCols_in), 00509 valuesCopied_(false), values_(Source.values_) { 00510 if(CV == Copy) 00511 { 00512 const OrdinalType numvals = (numRowsCols_ == 1) ? 1 : 4*(numRowsCols_-1); 00513 values_ = new ScalarType[numvals]; 00514 copyMat(Source, startRow); 00515 valuesCopied_ = true; 00516 } 00517 else // CV == View 00518 { 00519 // \todo ??? 00520 // values_ = values_ + (stride_ * startCol) + startRow; 00521 } 00522 } 00523 00524 template<typename OrdinalType, typename ScalarType> 00525 SerialTriDiMatrix<OrdinalType, ScalarType>::~SerialTriDiMatrix() 00526 { 00527 deleteArrays(); 00528 } 00529 00530 //---------------------------------------------------------------------------------------------------- 00531 // Shape methods 00532 //---------------------------------------------------------------------------------------------------- 00533 00534 template<typename OrdinalType, typename ScalarType> 00535 int SerialTriDiMatrix<OrdinalType, ScalarType>::shape( OrdinalType numRowsCols_in ) 00536 { 00537 deleteArrays(); // Get rid of anything that might be already allocated 00538 numRowsCols_ = numRowsCols_in; 00539 const OrdinalType numvals = ( numRowsCols_ == 1) ? 1 : 4*(numRowsCols_-1); 00540 values_ = new ScalarType[numvals]; 00541 00542 putScalar(); 00543 valuesCopied_ = true; 00544 return(0); 00545 } 00546 00547 template<typename OrdinalType, typename ScalarType> 00548 int SerialTriDiMatrix<OrdinalType, ScalarType>::shapeUninitialized( OrdinalType numRowsCols_in ) 00549 { 00550 deleteArrays(); // Get rid of anything that might be already allocated 00551 numRowsCols_ = numRowsCols_in; 00552 const OrdinalType numvals = ( numRowsCols_ == 1) ? 1 : 4*(numRowsCols_-1); 00553 values_ = new ScalarType[numvals]; 00554 valuesCopied_ = true; 00555 return(0); 00556 } 00557 00558 template<typename OrdinalType, typename ScalarType> 00559 int SerialTriDiMatrix<OrdinalType, ScalarType>::reshape( OrdinalType numRowsCols_in ) 00560 { 00561 if(numRowsCols_in <1 ) { 00562 deleteArrays(); 00563 return 0; 00564 } 00565 // Allocate space for new matrix 00566 const OrdinalType numvals = ( numRowsCols_in == 1) ? 1 : 4*(numRowsCols_in - 1); 00567 ScalarType* values_tmp = new ScalarType[numvals]; 00568 ScalarType zero = ScalarTraits<ScalarType>::zero(); 00569 for(OrdinalType i= 0; i<numvals ; ++i) 00570 values_tmp[i] = zero; 00571 00572 OrdinalType min = TEUCHOS_MIN(numRowsCols_, numRowsCols_in); 00573 ScalarType* dl = values_tmp; 00574 ScalarType* d = values_tmp + (numRowsCols_in-1); 00575 ScalarType* du = d+(numRowsCols_in); 00576 ScalarType* du2 = du+(numRowsCols_in - 1); 00577 00578 if(values_ != 0) { 00579 for(OrdinalType i = 0 ; i< min ; ++i) { 00580 dl[i] = DL_[i]; 00581 d[i] = D_[i]; 00582 du[i] = DU_[i]; 00583 du2[i] = DU2_[i]; 00584 } 00585 } 00586 00587 deleteArrays(); // Get rid of anything that might be already allocated 00588 numRowsCols_ = numRowsCols_in; 00589 00590 values_ = values_tmp; // Set pointer to new A 00591 DL_ = dl; 00592 D_ = d; 00593 DU_ = du; 00594 DU2_ = du2; 00595 00596 valuesCopied_ = true; 00597 return(0); 00598 } 00599 00600 //---------------------------------------------------------------------------------------------------- 00601 // Set methods 00602 //---------------------------------------------------------------------------------------------------- 00603 00604 template<typename OrdinalType, typename ScalarType> 00605 int SerialTriDiMatrix<OrdinalType, ScalarType>::putScalar( const ScalarType value_in ) { 00606 // Set each value of the TriDi matrix to "value". 00607 const OrdinalType numvals = (numRowsCols_ == 1) ? 1 : 4*(numRowsCols_-1); 00608 00609 for(OrdinalType i = 0; i<numvals ; ++i) 00610 { 00611 values_[i] = value_in; 00612 } 00613 return 0; 00614 } 00615 00616 // template<typename OrdinalType, typename ScalarType> 00617 // int SerialTriDiMatrix<OrdinalType, ScalarType>::random() 00618 // { 00619 // // Set each value of the TriDi matrix to a random value. 00620 // for(OrdinalType j = 0; j < numCols_; j++) 00621 // { 00622 // for(OrdinalType i = 0; i < numRowsCols_; i++) 00623 // { 00624 // values_[i + j*stride_] = ScalarTraits<ScalarType>::random(); 00625 // } 00626 // } 00627 // return 0; 00628 // } 00629 00630 template<typename OrdinalType, typename ScalarType> 00631 SerialTriDiMatrix<OrdinalType,ScalarType>& 00632 SerialTriDiMatrix<OrdinalType, ScalarType>::operator=(const SerialTriDiMatrix<OrdinalType,ScalarType>& Source ) 00633 { 00634 if(this == &Source) 00635 return(*this); // Special case of source same as target 00636 if((!valuesCopied_) && (!Source.valuesCopied_) && (values_ == Source.values_)) 00637 return(*this); // Special case of both are views to same data. 00638 00639 // If the source is a view then we will return a view, else we will return a copy. 00640 if (!Source.valuesCopied_) { 00641 if(valuesCopied_) { 00642 // Clean up stored data if this was previously a copy. 00643 deleteArrays(); 00644 } 00645 numRowsCols_ = Source.numRowsCols_; 00646 values_ = Source.values_; 00647 } 00648 else { 00649 // If we were a view, we will now be a copy. 00650 if(!valuesCopied_) { 00651 numRowsCols_ = Source.numRowsCols_; 00652 const OrdinalType numvals = ( Source.numRowsCols_ == 1) ? 1 : 4*(Source.numRowsCols_ - 1); 00653 if(numvals > 0) { 00654 values_ = new ScalarType[numvals]; 00655 valuesCopied_ = true; 00656 } 00657 else { 00658 values_ = 0; 00659 } 00660 } 00661 // If we were a copy, we will stay a copy. 00662 else { 00663 // we need to allocate more space (or less space) 00664 deleteArrays(); 00665 numRowsCols_ = Source.numRowsCols_; 00666 const OrdinalType numvals = ( Source.numRowsCols_ == 1) ? 1 : 4*(Source.numRowsCols_ - 1); 00667 if(numvals > 0) { 00668 values_ = new ScalarType[numvals]; 00669 valuesCopied_ = true; 00670 } 00671 } 00672 DL_ = values_; 00673 if(values_ != 0) { 00674 D_ = DL_ + (numRowsCols_-1); 00675 DU_ = D_ + numRowsCols_; 00676 DU2_ = DU_ + (numRowsCols_-1); 00677 00678 OrdinalType min = TEUCHOS_MIN(numRowsCols_, Source.numRowsCols_); 00679 for(OrdinalType i = 0 ; i < min ; ++i ) { 00680 D_[i] = Source.D()[i]; 00681 if(i< (min-1 ) ) 00682 { 00683 DL_[i] = Source.DL()[i]; 00684 DU_[i] = Source.DU()[i]; 00685 } 00686 if(i< (min-2)) 00687 DU2_[i] = Source.DU2()[i]; 00688 } 00689 00690 } 00691 else { 00692 D_ = DU_ = DU2_ = 0; 00693 } 00694 } 00695 return(*this); 00696 } 00697 00698 template<typename OrdinalType, typename ScalarType> 00699 SerialTriDiMatrix<OrdinalType, ScalarType>& SerialTriDiMatrix<OrdinalType, ScalarType>::operator+= (const SerialTriDiMatrix<OrdinalType,ScalarType>& Source ) 00700 { 00701 // Check for compatible dimensions 00702 if ((numRowsCols_ != Source.numRowsCols_) ) 00703 { 00704 TEUCHOS_CHK_REF(*this); // Return *this without altering it. 00705 } 00706 copyMat(Source, 0, ScalarTraits<ScalarType>::one()); 00707 return(*this); 00708 } 00709 00710 template<typename OrdinalType, typename ScalarType> 00711 SerialTriDiMatrix<OrdinalType, ScalarType>& SerialTriDiMatrix<OrdinalType, ScalarType>::operator-= (const SerialTriDiMatrix<OrdinalType,ScalarType>& Source ) 00712 { 00713 // Check for compatible dimensions 00714 if ((numRowsCols_ != Source.numRowsCols_) ) 00715 { 00716 TEUCHOS_CHK_REF(*this); // Return *this without altering it. 00717 } 00718 copyMat(Source, 0, -ScalarTraits<ScalarType>::one()); 00719 return(*this); 00720 } 00721 00722 template<typename OrdinalType,typename ScalarType> 00723 SerialTriDiMatrix<OrdinalType,ScalarType> & SerialTriDiMatrix<OrdinalType,ScalarType>::assign(const SerialTriDiMatrix<OrdinalType,ScalarType> & Source) 00724 { 00725 if(this == &Source) 00726 return(*this); // Special case of source same as target 00727 if((!valuesCopied_) && (!Source.valuesCopied_) && (values_ == Source.values_)) 00728 return(*this); // Special case of both are views to same data. 00729 00730 // Check for compatible dimensions 00731 if ((numRowsCols_ != Source.numRowsCols_) ) 00732 { 00733 TEUCHOS_CHK_REF(*this); // Return *this without altering it. 00734 } 00735 copyMat(Source, 0, 0); 00736 return(*this); 00737 } 00738 00739 //---------------------------------------------------------------------------------------------------- 00740 // Accessor methods 00741 //---------------------------------------------------------------------------------------------------- 00742 00743 template<typename OrdinalType,typename ScalarType> 00744 inline const ScalarType& SerialTriDiMatrix<OrdinalType,ScalarType>::operator () (OrdinalType rowIndex, OrdinalType colIndex) const 00745 { 00746 OrdinalType diff = colIndex - rowIndex; 00747 00748 //#ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK 00749 checkIndex( rowIndex, colIndex ); 00750 //#endif 00751 switch (diff) { 00752 case -1: 00753 return DL_[colIndex]; 00754 break; 00755 case 0: 00756 return D_[colIndex]; 00757 break; 00758 case 1: 00759 return DU_[rowIndex]; 00760 break; 00761 case 2: 00762 return DU2_[rowIndex]; 00763 break; 00764 default: 00765 TEUCHOS_TEST_FOR_EXCEPTION(true, std::out_of_range, 00766 "SerialTriDiMatrix<T>::operator (row,col) " 00767 "Index (" << rowIndex <<","<<colIndex<<") out of range "); 00768 } 00769 TEUCHOS_TEST_FOR_EXCEPTION(true, std::out_of_range, 00770 "SerialTriDiMatrix<T>::operator (row,col) " 00771 "Index (" << rowIndex <<","<<colIndex<<") out of range "); 00772 return D_[0]; 00773 } 00774 00775 template<typename OrdinalType,typename ScalarType> 00776 inline ScalarType& Teuchos::SerialTriDiMatrix<OrdinalType,ScalarType>::operator () (OrdinalType rowIndex, OrdinalType colIndex) 00777 { 00778 OrdinalType diff = colIndex - rowIndex; 00779 //#ifdef HAVE_TEUCHOS_ARRAY_BOUNDSCHECK 00780 checkIndex( rowIndex, colIndex ); 00781 //#endif 00782 switch (diff) { 00783 case -1: 00784 return DL_[colIndex]; 00785 break; 00786 case 0: 00787 return D_[colIndex]; 00788 break; 00789 case 1: 00790 return DU_[rowIndex]; 00791 break; 00792 case 2: 00793 return DU2_[rowIndex]; 00794 break; 00795 default: 00796 TEUCHOS_TEST_FOR_EXCEPTION(true, std::out_of_range, 00797 "SerialTriDiMatrix<T>::operator (row,col) " 00798 "Index (" << rowIndex <<","<<colIndex<<") out of range "); 00799 } 00800 TEUCHOS_TEST_FOR_EXCEPTION(true, std::out_of_range, 00801 "SerialTriDiMatrix<T>::operator (row,col) " 00802 "Index (" << rowIndex <<","<<colIndex<<") out of range "); 00803 // TEUCHOS_CHK_ERR(-1); 00804 return D_[0]; 00805 } 00806 00807 //---------------------------------------------------------------------------------------------------- 00808 // Norm methods 00809 //---------------------------------------------------------------------------------------------------- 00810 00811 template<typename OrdinalType,typename ScalarType> 00812 typename ScalarTraits<ScalarType>::magnitudeType SerialTriDiMatrix<OrdinalType,ScalarType>::normOne() const 00813 { 00814 OrdinalType i, j; 00815 typename ScalarTraits<ScalarType>::magnitudeType anorm = ScalarTraits<ScalarType>::magnitude(ScalarTraits<ScalarType>::zero()); 00816 typename ScalarTraits<ScalarType>::magnitudeType absSum = ScalarTraits<ScalarType>::magnitude(ScalarTraits<ScalarType>::zero()); 00817 ScalarType* ptr; 00818 00819 // Fix this for Tri DI 00820 00821 for(j = 0; j < numRowsCols_; j++) 00822 { 00823 ScalarType sum = 0; 00824 if(j-1>=0) sum += ScalarTraits<ScalarType>::magnitude((*this)(j-1,j)); 00825 sum+= ScalarTraits<ScalarType>::magnitude((*this)(j,j)); 00826 if(j+1<numRowsCols_) sum+= ScalarTraits<ScalarType>::magnitude((*this)(j+1,j)); 00827 absSum = ScalarTraits<ScalarType>::magnitude(sum); 00828 if(absSum > anorm) 00829 { 00830 anorm = absSum; 00831 } 00832 } 00833 updateFlops(numRowsCols_ * numRowsCols_); 00834 return(anorm); 00835 } 00836 00837 template<typename OrdinalType, typename ScalarType> 00838 typename ScalarTraits<ScalarType>::magnitudeType SerialTriDiMatrix<OrdinalType, ScalarType>::normInf() const 00839 { 00840 OrdinalType i,j; 00841 typename ScalarTraits<ScalarType>::magnitudeType sum, anorm = ScalarTraits<ScalarType>::magnitude(ScalarTraits<ScalarType>::zero()); 00842 00843 for (i = 0; i < numRowsCols_; i++) { 00844 sum = ScalarTraits<ScalarType>::magnitude(ScalarTraits<ScalarType>::zero()); 00845 for (j=i-1; j<= i+1; j++) { 00846 if(j >= 0 && j < numRowsCols_) sum += ScalarTraits<ScalarType>::magnitude((*this)(i,j)); 00847 } 00848 anorm = TEUCHOS_MAX( anorm, sum ); 00849 } 00850 updateFlops(numRowsCols_ * numRowsCols_); 00851 return(anorm); 00852 } 00853 00854 template<typename OrdinalType, typename ScalarType> 00855 typename ScalarTraits<ScalarType>::magnitudeType SerialTriDiMatrix<OrdinalType, ScalarType>::normFrobenius() const 00856 { 00857 // \todo fix this 00858 OrdinalType i, j; 00859 typename ScalarTraits<ScalarType>::magnitudeType anorm = ScalarTraits<ScalarType>::magnitude(ScalarTraits<ScalarType>::zero()); 00860 for (j = 0; j < numRowsCols_; j++) { 00861 for (i = j-1; i <= j+1; i++) { 00862 if(i >= 0 && i < numRowsCols_ ) anorm += ScalarTraits<ScalarType>::magnitude((*this)(i,j)); 00863 } 00864 } 00865 anorm = ScalarTraits<ScalarType>::magnitude(ScalarTraits<ScalarType>::squareroot(anorm)); 00866 updateFlops( (numRowsCols_ == 1) ? 1 : 4*(numRowsCols_-1) ); 00867 return(anorm); 00868 } 00869 00870 //---------------------------------------------------------------------------------------------------- 00871 // Comparison methods 00872 //---------------------------------------------------------------------------------------------------- 00873 00874 template<typename OrdinalType, typename ScalarType> 00875 bool SerialTriDiMatrix<OrdinalType, ScalarType>::operator== (const SerialTriDiMatrix<OrdinalType, ScalarType> &Operand) const 00876 { 00877 bool allmatch = true; 00878 bool result = 1; 00879 if((numRowsCols_ != Operand.numRowsCols_) ) 00880 { 00881 result = 0; 00882 } 00883 else 00884 { 00885 OrdinalType numvals = (numRowsCols_ == 1)? 1 : 4*(numRowsCols_ -1 ); 00886 00887 for(OrdinalType i = 0; i< numvals; ++i) 00888 allmatch &= (Operand.values_[i] == values_[i]); 00889 } 00890 return allmatch; 00891 } 00892 00893 template<typename OrdinalType, typename ScalarType> 00894 bool SerialTriDiMatrix<OrdinalType, ScalarType>::operator!= (const SerialTriDiMatrix<OrdinalType, ScalarType> &Operand) const { 00895 return !((*this) == Operand); 00896 } 00897 00898 //---------------------------------------------------------------------------------------------------- 00899 // Multiplication method 00900 //---------------------------------------------------------------------------------------------------- 00901 00902 template<typename OrdinalType, typename ScalarType> 00903 SerialTriDiMatrix<OrdinalType, ScalarType>& SerialTriDiMatrix<OrdinalType, ScalarType>::operator*= (const ScalarType alpha ) 00904 { 00905 this->scale( alpha ); 00906 return(*this); 00907 } 00908 00909 template<typename OrdinalType, typename ScalarType> 00910 int SerialTriDiMatrix<OrdinalType, ScalarType>::scale( const ScalarType alpha ) 00911 { 00912 OrdinalType i; 00913 OrdinalType numvals = (numRowsCols_ == 1)? 1 : 4*(numRowsCols_ -1 ); 00914 for (i=0; i < numvals ; ++i ) { 00915 values_[i] *= alpha; 00916 } 00917 return(0); 00918 } 00919 00920 template<typename OrdinalType, typename ScalarType> 00921 int SerialTriDiMatrix<OrdinalType, ScalarType>::scale( const SerialTriDiMatrix<OrdinalType,ScalarType>& A ) 00922 { 00923 OrdinalType j; 00924 00925 // Check for compatible dimensions 00926 if ((numRowsCols_ != A.numRowsCols_) ) 00927 { 00928 TEUCHOS_CHK_ERR(-1); // Return error 00929 } 00930 OrdinalType numvals = (numRowsCols_ == 1)? 1 : 4*(numRowsCols_ -1 ); 00931 for (j=0; j<numvals; j++) { 00932 values_[j] = A.values_ * values_[j]; 00933 } 00934 updateFlops( numvals ); 00935 return(0); 00936 } 00937 00938 template<typename OrdinalType, typename ScalarType> 00939 void SerialTriDiMatrix<OrdinalType, ScalarType>::print(std::ostream& os) const 00940 { 00941 os << std::endl; 00942 if(valuesCopied_) 00943 os << "A_Copied: yes" << std::endl; 00944 else 00945 os << "A_Copied: no" << std::endl; 00946 os << "Rows and Columns: " << numRowsCols_ << std::endl; 00947 if(numRowsCols_ == 0) { 00948 os << "(matrix is empty, no values to display)" << std::endl; 00949 return; 00950 } 00951 else 00952 { 00953 os << "DL: "<<std::endl; 00954 for(int i=0;i<numRowsCols_-1;++i) 00955 os << DL_[i]<<" "; 00956 os << std::endl; 00957 os << "D: "<<std::endl; 00958 for(int i=0;i<numRowsCols_;++i) 00959 os << D_[i]<<" "; 00960 os << std::endl; 00961 os << "DU: "<<std::endl; 00962 for(int i=0;i<numRowsCols_-1;++i) 00963 os << DU_[i]<<" "; 00964 os << std::endl; 00965 os << "DU2: "<<std::endl; 00966 for(int i=0;i<numRowsCols_-2;++i) 00967 os << DU2_[i]<<" "; 00968 os << std::endl; 00969 } 00970 00971 os <<" square format:"<<std::endl; 00972 for(int i=0 ; i < numRowsCols_ ; ++i ) { 00973 for(int j=0;j<numRowsCols_;++j) { 00974 if ( j >= i-1 && j <= i+1) { 00975 os << (*this)(i,j)<<" "; 00976 } 00977 else 00978 os <<". "; 00979 } 00980 os << std::endl; 00981 } 00982 00983 } 00984 00985 //---------------------------------------------------------------------------------------------------- 00986 // Protected methods 00987 //---------------------------------------------------------------------------------------------------- 00988 00989 template<typename OrdinalType, typename ScalarType> 00990 inline void SerialTriDiMatrix<OrdinalType, ScalarType>::checkIndex( OrdinalType rowIndex, OrdinalType colIndex ) const 00991 { 00992 OrdinalType diff = colIndex - rowIndex; 00993 TEUCHOS_TEST_FOR_EXCEPTION(rowIndex < 0 || rowIndex >= numRowsCols_, std::out_of_range, 00994 "SerialTriDiMatrix<T>::checkIndex: " 00995 "Row index " << rowIndex << " out of range [0, "<< numRowsCols_ << "]"); 00996 TEUCHOS_TEST_FOR_EXCEPTION(colIndex < 0 || colIndex >= numRowsCols_, 00997 std::out_of_range, 00998 "SerialTriDiMatrix<T>::checkIndex: " 00999 "Col index " << colIndex << " out of range [0, "<< numRowsCols_ << "]"); 01000 TEUCHOS_TEST_FOR_EXCEPTION(diff > 2 || diff < -1 , std::out_of_range, 01001 "SerialTriDiMatrix<T>::checkIndex: " 01002 "index difference " << diff << " out of range [-1, 2]"); 01003 } 01004 01005 template<typename OrdinalType, typename ScalarType> 01006 void SerialTriDiMatrix<OrdinalType, ScalarType>::deleteArrays(void) 01007 { 01008 if (valuesCopied_) 01009 { 01010 delete [] values_; 01011 values_ = 0; 01012 valuesCopied_ = false; 01013 } 01014 } 01015 01016 template<typename OrdinalType, typename ScalarType> 01017 void SerialTriDiMatrix<OrdinalType, ScalarType>::copyMat(SerialTriDiMatrix<OrdinalType, ScalarType> inputMatrix, 01018 OrdinalType startRowCol, 01019 ScalarType alpha ) 01020 { 01021 OrdinalType i; 01022 OrdinalType max = inputMatrix.numRowsCols_; 01023 if(max > numRowsCols_ ) max = numRowsCols_; 01024 if(startRowCol > max ) return; // 01025 01026 for(i = startRowCol ; i < max ; ++i) { 01027 01028 if (alpha != Teuchos::ScalarTraits<ScalarType>::zero() ) { 01029 // diagonal 01030 D()[i] += inputMatrix.D()[i]; 01031 if(i<(max-1) && (i-1) >= startRowCol) { 01032 DL()[i] += inputMatrix.DL()[i]; 01033 DU()[i] += inputMatrix.DU()[i]; 01034 } 01035 if(i<(max-2) && (i-2) >= startRowCol) { 01036 DU2()[i] += inputMatrix.DU2()[i]; 01037 } 01038 } 01039 else { 01040 // diagonal 01041 D()[i] = inputMatrix.D()[i]; 01042 if(i<(max-1) && (i-1) >= startRowCol) { 01043 DL()[i] = inputMatrix.DL()[i]; 01044 DU()[i] = inputMatrix.DU()[i]; 01045 } 01046 if(i<(max-2) && (i-2) >= startRowCol) { 01047 DU2()[i] = inputMatrix.DU2()[i]; 01048 } 01049 } 01050 } 01051 } 01052 01053 } 01054 01055 01056 #endif /* _TEUCHOS_SERIALTRIDIMATRIX_HPP_ */
1.7.6.1