34 #ifndef ANASAZI_BASIC_ORTHOMANAGER_HPP
35 #define ANASAZI_BASIC_ORTHOMANAGER_HPP
51 #ifdef ANASAZI_BASIC_ORTHO_DEBUG
52 # include <Teuchos_FancyOStream.hpp>
57 template<
class ScalarType,
class MV,
class OP>
283 MagnitudeType kappa_;
290 bool completeBasis,
int howMany = -1 )
const;
302 template<
class ScalarType,
class MV,
class OP>
307 MatOrthoManager<ScalarType,MV,OP>(Op), kappa_(kappa), eps_(eps), tol_(tol)
308 #ifdef ANASAZI_TEUCHOS_TIME_MONITOR
309 , timerReortho_(Teuchos::TimeMonitor::getNewTimer(
"Anasazi::BasicOrthoManager::Re-orthogonalization"))
313 "Anasazi::BasicOrthoManager::BasicOrthoManager(): argument \"eps\" must be non-negative.");
316 eps_ = lapack.
LAMCH(
'E');
321 std::invalid_argument,
322 "Anasazi::BasicOrthoManager::BasicOrthoManager(): argument \"tol\" must be in [0,1].");
329 template<
class ScalarType,
class MV,
class OP>
332 const ScalarType ONE = SCT::one();
333 int rank = MVT::GetNumberVecs(X);
336 for (
int i=0; i<rank; i++) {
346 template<
class ScalarType,
class MV,
class OP>
349 int r1 = MVT::GetNumberVecs(X1);
350 int r2 = MVT::GetNumberVecs(X2);
359 template<
class ScalarType,
class MV,
class OP>
382 #ifdef ANASAZI_BASIC_ORTHO_DEBUG
385 out = Teuchos::getFancyOStream(Teuchos::rcpFromRef(std::cout));
387 *out <<
"Entering Anasazi::BasicOrthoManager::projectMat(...)\n";
390 ScalarType ONE = SCT::one();
392 int xc = MVT::GetNumberVecs( X );
393 ptrdiff_t xr = MVText::GetGlobalLength( X );
395 std::vector<int> qcs(nq);
397 if (nq == 0 || xc == 0 || xr == 0) {
398 #ifdef ANASAZI_BASIC_ORTHO_DEBUG
399 *out <<
"Leaving Anasazi::BasicOrthoManager::projectMat(...)\n";
403 ptrdiff_t qr = MVText::GetGlobalLength ( *Q[0] );
412 #ifdef ANASAZI_BASIC_ORTHO_DEBUG
413 *out <<
"Allocating MX...\n";
415 if (MX == Teuchos::null) {
417 MX = MVT::Clone(X,MVT::GetNumberVecs(X));
418 OPT::Apply(*(this->_Op),X,*MX);
419 this->_OpCounter += MVT::GetNumberVecs(X);
424 MX = Teuchos::rcpFromRef(X);
426 int mxc = MVT::GetNumberVecs( *MX );
427 ptrdiff_t mxr = MVText::GetGlobalLength( *MX );
431 "Anasazi::BasicOrthoManager::projectMat(): MVT returned negative dimensions for X,MX" );
434 "Anasazi::BasicOrthoManager::projectMat(): Size of X not consistent with MX,Q" );
438 for (
int i=0; i<nq; i++) {
440 "Anasazi::BasicOrthoManager::projectMat(): Q lengths not mutually consistent" );
441 qcs[i] = MVT::GetNumberVecs( *Q[i] );
443 "Anasazi::BasicOrthoManager::projectMat(): Q has less rows than columns" );
447 if ( C[i] == Teuchos::null ) {
452 "Anasazi::BasicOrthoManager::projectMat(): Size of Q not consistent with size of C" );
459 std::vector<ScalarType> oldDot( xc );
460 MVT::MvDot( X, *MX, oldDot );
461 #ifdef ANASAZI_BASIC_ORTHO_DEBUG
462 *out <<
"oldDot = { ";
463 std::copy(oldDot.begin(), oldDot.end(), std::ostream_iterator<ScalarType>(*out,
" "));
469 for (
int i=0; i<nq; i++) {
473 #ifdef ANASAZI_BASIC_ORTHO_DEBUG
474 *out <<
"Applying projector P_Q[" << i <<
"]...\n";
476 MVT::MvTimesMatAddMv( -ONE, *Q[i], *C[i], ONE, X );
481 if (MQ[i] == Teuchos::null) {
482 #ifdef ANASAZI_BASIC_ORTHO_DEBUG
483 *out <<
"Updating MX via M*X...\n";
485 OPT::Apply( *(this->_Op), X, *MX);
486 this->_OpCounter += MVT::GetNumberVecs(X);
489 #ifdef ANASAZI_BASIC_ORTHO_DEBUG
490 *out <<
"Updating MX via M*Q...\n";
492 MVT::MvTimesMatAddMv( -ONE, *MQ[i], *C[i], ONE, *MX );
498 std::vector<ScalarType> newDot(xc);
499 MVT::MvDot( X, *MX, newDot );
500 #ifdef ANASAZI_BASIC_ORTHO_DEBUG
501 *out <<
"newDot = { ";
502 std::copy(newDot.begin(), newDot.end(), std::ostream_iterator<ScalarType>(*out,
" "));
507 for (
int j = 0; j < xc; ++j) {
509 if ( SCT::magnitude(kappa_*newDot[j]) < SCT::magnitude(oldDot[j]) ) {
510 #ifdef ANASAZI_BASIC_ORTHO_DEBUG
511 *out <<
"kappa_*newDot[" <<j<<
"] == " << kappa_*newDot[j] <<
"... another step of Gram-Schmidt.\n";
513 #ifdef ANASAZI_TEUCHOS_TIME_MONITOR
516 for (
int i=0; i<nq; i++) {
522 #ifdef ANASAZI_BASIC_ORTHO_DEBUG
523 *out <<
"Applying projector P_Q[" << i <<
"]...\n";
525 MVT::MvTimesMatAddMv( -ONE, *Q[i], C2, ONE, X );
529 if (MQ[i] == Teuchos::null) {
530 #ifdef ANASAZI_BASIC_ORTHO_DEBUG
531 *out <<
"Updating MX via M*X...\n";
533 OPT::Apply( *(this->_Op), X, *MX);
534 this->_OpCounter += MVT::GetNumberVecs(X);
537 #ifdef ANASAZI_BASIC_ORTHO_DEBUG
538 *out <<
"Updating MX via M*Q...\n";
540 MVT::MvTimesMatAddMv( -ONE, *MQ[i], C2, ONE, *MX );
548 #ifdef ANASAZI_BASIC_ORTHO_DEBUG
549 *out <<
"Leaving Anasazi::BasicOrthoManager::projectMat(...)\n";
557 template<
class ScalarType,
class MV,
class OP>
565 int xc = MVT::GetNumberVecs(X);
566 ptrdiff_t xr = MVText::GetGlobalLength(X);
571 if (MX == Teuchos::null) {
573 MX = MVT::Clone(X,xc);
574 OPT::Apply(*(this->_Op),X,*MX);
575 this->_OpCounter += MVT::GetNumberVecs(X);
581 if ( B == Teuchos::null ) {
585 int mxc = (this->_hasOp) ? MVT::GetNumberVecs( *MX ) : xc;
586 ptrdiff_t mxr = (this->_hasOp) ? MVText::GetGlobalLength( *MX ) : xr;
590 "Anasazi::BasicOrthoManager::normalizeMat(): X must be non-empty" );
592 "Anasazi::BasicOrthoManager::normalizeMat(): Size of X not consistent with size of B" );
594 "Anasazi::BasicOrthoManager::normalizeMat(): Size of X not consistent with size of MX" );
595 TEUCHOS_TEST_FOR_EXCEPTION( static_cast<ptrdiff_t>(xc) > xr, std::invalid_argument,
596 "Anasazi::BasicOrthoManager::normalizeMat(): Size of X not feasible for normalization" );
598 return findBasis(X, MX, *B,
true );
605 template<
class ScalarType,
class MV,
class OP>
615 #ifdef ANASAZI_BASIC_ORTHO_DEBUG
618 out = Teuchos::getFancyOStream(Teuchos::rcpFromRef(std::cout));
620 *out <<
"Entering Anasazi::BasicOrthoManager::projectAndNormalizeMat(...)\n";
624 int xc = MVT::GetNumberVecs( X );
625 ptrdiff_t xr = MVText::GetGlobalLength( X );
631 if ( B == Teuchos::null ) {
637 if (MX == Teuchos::null) {
639 #ifdef ANASAZI_BASIC_ORTHO_DEBUG
640 *out <<
"Allocating MX...\n";
642 MX = MVT::Clone(X,MVT::GetNumberVecs(X));
643 OPT::Apply(*(this->_Op),X,*MX);
644 this->_OpCounter += MVT::GetNumberVecs(X);
649 MX = Teuchos::rcpFromRef(X);
652 int mxc = MVT::GetNumberVecs( *MX );
653 ptrdiff_t mxr = MVText::GetGlobalLength( *MX );
655 TEUCHOS_TEST_FOR_EXCEPTION( xc == 0 || xr == 0, std::invalid_argument,
"Anasazi::BasicOrthoManager::projectAndNormalizeMat(): X must be non-empty" );
657 ptrdiff_t numbas = 0;
658 for (
int i=0; i<nq; i++) {
659 numbas += MVT::GetNumberVecs( *Q[i] );
664 "Anasazi::BasicOrthoManager::projectAndNormalizeMat(): Size of X must be consistent with size of B" );
667 "Anasazi::BasicOrthoManager::projectAndNormalizeMat(): MVT returned negative dimensions for X,MX" );
669 TEUCHOS_TEST_FOR_EXCEPTION( xc!=mxc || xr!=mxr, std::invalid_argument,
670 "Anasazi::BasicOrthoManager::projectAndNormalizeMat(): Size of X must be consistent with size of MX" );
672 TEUCHOS_TEST_FOR_EXCEPTION( numbas+xc > xr, std::invalid_argument,
673 "Anasazi::BasicOrthoManager::projectAndNormalizeMat(): Orthogonality constraints not feasible" );
676 #ifdef ANASAZI_BASIC_ORTHO_DEBUG
677 *out <<
"Orthogonalizing X against Q...\n";
679 projectMat(X,Q,C,MX,MQ);
687 int curxsize = xc - rank;
692 #ifdef ANASAZI_BASIC_ORTHO_DEBUG
693 *out <<
"Attempting to find orthonormal basis for X...\n";
695 rank = findBasis(X,MX,*B,
false,curxsize);
697 if (oldrank != -1 && rank != oldrank) {
703 for (
int i=0; i<xc; i++) {
704 (*B)(i,oldrank) = oldCoeff(i,0);
709 if (rank != oldrank) {
717 for (
int i=0; i<xc; i++) {
718 oldCoeff(i,0) = (*B)(i,rank);
725 #ifdef ANASAZI_BASIC_ORTHO_DEBUG
726 *out <<
"Finished computing basis.\n";
731 TEUCHOS_TEST_FOR_EXCEPTION( rank < oldrank,
OrthoError,
732 "Anasazi::BasicOrthoManager::projectAndNormalizeMat(): basis lost rank; this shouldn't happen");
734 if (rank != oldrank) {
750 #ifdef ANASAZI_BASIC_ORTHO_DEBUG
751 *out <<
"Randomizing X[" << rank <<
"]...\n";
754 std::vector<int> ind(1);
756 curX = MVT::CloneViewNonConst(X,ind);
757 MVT::MvRandom(*curX);
759 #ifdef ANASAZI_BASIC_ORTHO_DEBUG
760 *out <<
"Applying operator to random vector.\n";
762 curMX = MVT::CloneViewNonConst(*MX,ind);
763 OPT::Apply( *(this->_Op), *curX, *curMX );
764 this->_OpCounter += MVT::GetNumberVecs(*curX);
773 projectMat(*curX,Q,dummyC,curMX,MQ);
779 TEUCHOS_TEST_FOR_EXCEPTION( rank > xc || rank < 0, std::logic_error,
780 "Anasazi::BasicOrthoManager::projectAndNormalizeMat(): Debug error in rank variable." );
782 #ifdef ANASAZI_BASIC_ORTHO_DEBUG
783 *out <<
"Leaving Anasazi::BasicOrthoManager::projectAndNormalizeMat(...)\n";
794 template<
class ScalarType,
class MV,
class OP>
798 bool completeBasis,
int howMany )
const {
815 #ifdef ANASAZI_BASIC_ORTHO_DEBUG
818 out = Teuchos::getFancyOStream(Teuchos::rcpFromRef(std::cout));
820 *out <<
"Entering Anasazi::BasicOrthoManager::findBasis(...)\n";
823 const ScalarType ONE = SCT::one();
824 const MagnitudeType ZERO = SCT::magnitude(SCT::zero());
826 int xc = MVT::GetNumberVecs( X );
836 "Anasazi::BasicOrthoManager::findBasis(): calling routine did not specify MS.");
838 "Anasazi::BasicOrthoManager::findBasis(): Invalid howMany parameter" );
843 int xstart = xc - howMany;
845 for (
int j = xstart; j < xc; j++) {
854 for (
int i=j+1; i<xc; ++i) {
859 std::vector<int> index(1);
863 if ((this->_hasOp)) {
865 MXj = MVT::CloneViewNonConst( *MX, index );
873 std::vector<int> prev_idx( numX );
877 for (
int i=0; i<numX; ++i) prev_idx[i] = i;
878 prevX = MVT::CloneViewNonConst( X, prev_idx );
880 prevMX = MVT::CloneViewNonConst( *MX, prev_idx );
889 for (
int numTrials = 0; numTrials < 10; numTrials++) {
890 #ifdef ANASAZI_BASIC_ORTHO_DEBUG
891 *out <<
"Trial " << numTrials <<
" for vector " << j <<
"\n";
896 std::vector<MagnitudeType> origNorm(1), newNorm(1), newNorm2(1);
903 #ifdef ANASAZI_BASIC_ORTHO_DEBUG
904 *out <<
"origNorm = " << origNorm[0] <<
"\n";
915 #ifdef ANASAZI_BASIC_ORTHO_DEBUG
916 *out <<
"Orthogonalizing X[" << j <<
"]...\n";
918 MVT::MvTimesMatAddMv( -ONE, *prevX, product, ONE, *Xj );
925 #ifdef ANASAZI_BASIC_ORTHO_DEBUG
926 *out <<
"Updating MX[" << j <<
"]...\n";
928 MVT::MvTimesMatAddMv( -ONE, *prevMX, product, ONE, *MXj );
933 MagnitudeType product_norm = product.normOne();
935 #ifdef ANASAZI_BASIC_ORTHO_DEBUG
936 *out <<
"newNorm = " << newNorm[0] <<
"\n";
937 *out <<
"prodoct_norm = " << product_norm <<
"\n";
941 if ( product_norm/newNorm[0] >= tol_ || newNorm[0] < eps_*origNorm[0]) {
942 #ifdef ANASAZI_BASIC_ORTHO_DEBUG
943 if (product_norm/newNorm[0] >= tol_) {
944 *out <<
"product_norm/newNorm == " << product_norm/newNorm[0] <<
"... another step of Gram-Schmidt.\n";
947 *out <<
"eps*origNorm == " << eps_*origNorm[0] <<
"... another step of Gram-Schmidt.\n";
955 #ifdef ANASAZI_BASIC_ORTHO_DEBUG
956 *out <<
"Orthogonalizing X[" << j <<
"]...\n";
958 MVT::MvTimesMatAddMv( -ONE, *prevX, P2, ONE, *Xj );
959 if ((this->_hasOp)) {
960 #ifdef ANASAZI_BASIC_ORTHO_DEBUG
961 *out <<
"Updating MX[" << j <<
"]...\n";
963 MVT::MvTimesMatAddMv( -ONE, *prevMX, P2, ONE, *MXj );
967 product_norm = P2.normOne();
968 #ifdef ANASAZI_BASIC_ORTHO_DEBUG
969 *out <<
"newNorm2 = " << newNorm2[0] <<
"\n";
970 *out <<
"product_norm = " << product_norm <<
"\n";
972 if ( product_norm/newNorm2[0] >= tol_ || newNorm2[0] < eps_*origNorm[0] ) {
974 #ifdef ANASAZI_BASIC_ORTHO_DEBUG
975 if (product_norm/newNorm2[0] >= tol_) {
976 *out <<
"product_norm/newNorm2 == " << product_norm/newNorm2[0] <<
"... setting vector to zero.\n";
978 else if (newNorm[0] < newNorm2[0]) {
979 *out <<
"newNorm2 > newNorm... setting vector to zero.\n";
982 *out <<
"eps*origNorm == " << eps_*origNorm[0] <<
"... setting vector to zero.\n";
985 MVT::MvInit(*Xj,ZERO);
986 if ((this->_hasOp)) {
987 #ifdef ANASAZI_BASIC_ORTHO_DEBUG
988 *out <<
"Setting MX[" << j <<
"] to zero as well.\n";
990 MVT::MvInit(*MXj,ZERO);
997 if (numTrials == 0) {
998 for (
int i=0; i<numX; i++) {
999 B(i,j) = product(i,0);
1005 if ( newNorm[0] != ZERO && newNorm[0] > SCT::sfmin() ) {
1006 #ifdef ANASAZI_BASIC_ORTHO_DEBUG
1007 *out <<
"Normalizing X[" << j <<
"], norm(X[" << j <<
"]) = " << newNorm[0] <<
"\n";
1011 MVT::MvScale( *Xj, ONE/newNorm[0]);
1013 #ifdef ANASAZI_BASIC_ORTHO_DEBUG
1014 *out <<
"Normalizing M*X[" << j <<
"]...\n";
1017 MVT::MvScale( *MXj, ONE/newNorm[0]);
1021 if (numTrials == 0) {
1022 B(j,j) = newNorm[0];
1030 #ifdef ANASAZI_BASIC_ORTHO_DEBUG
1031 *out <<
"Not normalizing M*X[" << j <<
"]...\n";
1038 if (completeBasis) {
1040 #ifdef ANASAZI_BASIC_ORTHO_DEBUG
1041 *out <<
"Inserting random vector in X[" << j <<
"]...\n";
1043 MVT::MvRandom( *Xj );
1045 #ifdef ANASAZI_BASIC_ORTHO_DEBUG
1046 *out <<
"Updating M*X[" << j <<
"]...\n";
1048 OPT::Apply( *(this->_Op), *Xj, *MXj );
1049 this->_OpCounter += MVT::GetNumberVecs(*Xj);
1060 if (rankDef ==
true) {
1062 "Anasazi::BasicOrthoManager::findBasis(): Unable to complete basis" );
1063 #ifdef ANASAZI_BASIC_ORTHO_DEBUG
1064 *out <<
"Returning early, rank " << j <<
" from Anasazi::BasicOrthoManager::findBasis(...)\n";
1071 #ifdef ANASAZI_BASIC_ORTHO_DEBUG
1072 *out <<
"Returning " << xc <<
" from Anasazi::BasicOrthoManager::findBasis(...)\n";
1079 #endif // ANASAZI_BASIC_ORTHOMANAGER_HPP
An extension of the MultiVecTraits class that adds a new vector length method.This traits class provi...
Templated virtual class for providing orthogonalization/orthonormalization methods with matrix-based ...
Teuchos::ScalarTraits< ScalarType >::magnitudeType getKappa() const
Return parameter for re-orthogonalization threshold.
~BasicOrthoManager()
Destructor.
Declaration of basic traits for the multivector type.
basic_FancyOStream & setShowProcRank(const bool showProcRank)
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
Virtual base class which defines basic traits for the operator type.
void innerProdMat(const MV &X, const MV &Y, Teuchos::SerialDenseMatrix< int, ScalarType > &Z, Teuchos::RCP< const MV > MX=Teuchos::null, Teuchos::RCP< const MV > MY=Teuchos::null) const
Provides a matrix-based inner product.
ScalarType LAMCH(const char CMACH) const
ScalarTraits< ScalarType >::magnitudeType normFrobenius() const
void projectMat(MV &X, Teuchos::Array< Teuchos::RCP< const MV > > Q, Teuchos::Array< Teuchos::RCP< Teuchos::SerialDenseMatrix< int, ScalarType > > > C=Teuchos::tuple(Teuchos::RCP< Teuchos::SerialDenseMatrix< int, ScalarType > >(Teuchos::null)), Teuchos::RCP< MV > MX=Teuchos::null, Teuchos::Array< Teuchos::RCP< const MV > > MQ=Teuchos::tuple(Teuchos::RCP< const MV >(Teuchos::null))) const
Given a list of mutually orthogonal and internally orthonormal bases Q, this method projects a multiv...
Teuchos::ScalarTraits< ScalarType >::magnitudeType orthogErrorMat(const MV &X1, const MV &X2, Teuchos::RCP< const MV > MX1, Teuchos::RCP< const MV > MX2) const
This method computes the error in orthogonality of two multivectors, measured as the Frobenius norm o...
Anasazi's templated virtual class for providing routines for orthogonalization and orthonormalization...
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
void setKappa(typename Teuchos::ScalarTraits< ScalarType >::magnitudeType kappa)
Set parameter for re-orthogonalization threshold.
Traits class which defines basic operations on multivectors.
Virtual base class which defines basic traits for the operator type.
basic_FancyOStream & setShowAllFrontMatter(const bool showAllFrontMatter)
Anasazi header file which uses auto-configuration information to include necessary C++ headers...
int normalizeMat(MV &X, Teuchos::RCP< Teuchos::SerialDenseMatrix< int, ScalarType > > B=Teuchos::null, Teuchos::RCP< MV > MX=Teuchos::null) const
This method takes a multivector X and attempts to compute an orthonormal basis for ...
Exception thrown to signal error in an orthogonalization manager method.
Teuchos::ScalarTraits< ScalarType >::magnitudeType orthonormErrorMat(const MV &X, Teuchos::RCP< const MV > MX=Teuchos::null) const
This method computes the error in orthonormality of a multivector, measured as the Frobenius norm of ...
static magnitudeType magnitude(ScalarTypea)
BasicOrthoManager(Teuchos::RCP< const OP > Op=Teuchos::null, typename Teuchos::ScalarTraits< ScalarType >::magnitudeType kappa=1.41421356, typename Teuchos::ScalarTraits< ScalarType >::magnitudeType eps=0.0, typename Teuchos::ScalarTraits< ScalarType >::magnitudeType tol=0.20)
Constructor specifying re-orthogonalization tolerance.
An implementation of the Anasazi::MatOrthoManager that performs orthogonalization using (potentially)...
int projectAndNormalizeMat(MV &X, Teuchos::Array< Teuchos::RCP< const MV > > Q, Teuchos::Array< Teuchos::RCP< Teuchos::SerialDenseMatrix< int, ScalarType > > > C=Teuchos::tuple(Teuchos::RCP< Teuchos::SerialDenseMatrix< int, ScalarType > >(Teuchos::null)), Teuchos::RCP< Teuchos::SerialDenseMatrix< int, ScalarType > > B=Teuchos::null, Teuchos::RCP< MV > MX=Teuchos::null, Teuchos::Array< Teuchos::RCP< const MV > > MQ=Teuchos::tuple(Teuchos::RCP< const MV >(Teuchos::null))) const
Given a set of bases Q[i] and a multivector X, this method computes an orthonormal basis for ...
void normMat(const MV &X, std::vector< typename Teuchos::ScalarTraits< ScalarType >::magnitudeType > &normvec, Teuchos::RCP< const MV > MX=Teuchos::null) const
Provides the norm induced by the matrix-based inner product.