15 #ifndef ANASAZI_BASIC_ORTHOMANAGER_HPP
16 #define ANASAZI_BASIC_ORTHOMANAGER_HPP
32 #ifdef ANASAZI_BASIC_ORTHO_DEBUG
33 # include <Teuchos_FancyOStream.hpp>
38 template<
class ScalarType,
class MV,
class OP>
263 MagnitudeType kappa_;
270 bool completeBasis,
int howMany = -1 )
const;
282 template<
class ScalarType,
class MV,
class OP>
287 MatOrthoManager<ScalarType,MV,OP>(Op), kappa_(kappa), eps_(eps), tol_(tol)
288 #ifdef ANASAZI_TEUCHOS_TIME_MONITOR
289 , timerReortho_(Teuchos::TimeMonitor::getNewTimer(
"Anasazi::BasicOrthoManager::Re-orthogonalization"))
293 "Anasazi::BasicOrthoManager::BasicOrthoManager(): argument \"eps\" must be non-negative.");
296 eps_ = lapack.
LAMCH(
'E');
301 std::invalid_argument,
302 "Anasazi::BasicOrthoManager::BasicOrthoManager(): argument \"tol\" must be in [0,1].");
309 template<
class ScalarType,
class MV,
class OP>
312 const ScalarType ONE = SCT::one();
313 int rank = MVT::GetNumberVecs(X);
316 for (
int i=0; i<rank; i++) {
326 template<
class ScalarType,
class MV,
class OP>
329 int r1 = MVT::GetNumberVecs(X1);
330 int r2 = MVT::GetNumberVecs(X2);
339 template<
class ScalarType,
class MV,
class OP>
362 #ifdef ANASAZI_BASIC_ORTHO_DEBUG
365 out = Teuchos::getFancyOStream(Teuchos::rcpFromRef(std::cout));
367 *out <<
"Entering Anasazi::BasicOrthoManager::projectMat(...)\n";
370 ScalarType ONE = SCT::one();
372 int xc = MVT::GetNumberVecs( X );
373 ptrdiff_t xr = MVT::GetGlobalLength( X );
375 std::vector<int> qcs(nq);
377 if (nq == 0 || xc == 0 || xr == 0) {
378 #ifdef ANASAZI_BASIC_ORTHO_DEBUG
379 *out <<
"Leaving Anasazi::BasicOrthoManager::projectMat(...)\n";
383 ptrdiff_t qr = MVT::GetGlobalLength ( *Q[0] );
392 #ifdef ANASAZI_BASIC_ORTHO_DEBUG
393 *out <<
"Allocating MX...\n";
395 if (MX == Teuchos::null) {
397 MX = MVT::Clone(X,MVT::GetNumberVecs(X));
398 OPT::Apply(*(this->_Op),X,*MX);
399 this->_OpCounter += MVT::GetNumberVecs(X);
404 MX = Teuchos::rcpFromRef(X);
406 int mxc = MVT::GetNumberVecs( *MX );
407 ptrdiff_t mxr = MVT::GetGlobalLength( *MX );
411 "Anasazi::BasicOrthoManager::projectMat(): MVT returned negative dimensions for X,MX" );
414 "Anasazi::BasicOrthoManager::projectMat(): Size of X not consistent with MX,Q" );
417 for (
int i=0; i<nq; i++) {
419 "Anasazi::BasicOrthoManager::projectMat(): Q lengths not mutually consistent" );
420 qcs[i] = MVT::GetNumberVecs( *Q[i] );
422 "Anasazi::BasicOrthoManager::projectMat(): Q has less rows than columns" );
424 if ( C[i] == Teuchos::null ) {
429 "Anasazi::BasicOrthoManager::projectMat(): Size of Q not consistent with size of C" );
436 std::vector<ScalarType> oldDot( xc );
437 MVT::MvDot( X, *MX, oldDot );
438 #ifdef ANASAZI_BASIC_ORTHO_DEBUG
439 *out <<
"oldDot = { ";
440 std::copy(oldDot.begin(), oldDot.end(), std::ostream_iterator<ScalarType>(*out,
" "));
446 for (
int i=0; i<nq; i++) {
450 #ifdef ANASAZI_BASIC_ORTHO_DEBUG
451 *out <<
"Applying projector P_Q[" << i <<
"]...\n";
453 MVT::MvTimesMatAddMv( -ONE, *Q[i], *C[i], ONE, X );
458 if (MQ[i] == Teuchos::null) {
459 #ifdef ANASAZI_BASIC_ORTHO_DEBUG
460 *out <<
"Updating MX via M*X...\n";
462 OPT::Apply( *(this->_Op), X, *MX);
463 this->_OpCounter += MVT::GetNumberVecs(X);
466 #ifdef ANASAZI_BASIC_ORTHO_DEBUG
467 *out <<
"Updating MX via M*Q...\n";
469 MVT::MvTimesMatAddMv( -ONE, *MQ[i], *C[i], ONE, *MX );
475 std::vector<ScalarType> newDot(xc);
476 MVT::MvDot( X, *MX, newDot );
477 #ifdef ANASAZI_BASIC_ORTHO_DEBUG
478 *out <<
"newDot = { ";
479 std::copy(newDot.begin(), newDot.end(), std::ostream_iterator<ScalarType>(*out,
" "));
484 for (
int j = 0; j < xc; ++j) {
486 if ( SCT::magnitude(kappa_*newDot[j]) < SCT::magnitude(oldDot[j]) ) {
487 #ifdef ANASAZI_BASIC_ORTHO_DEBUG
488 *out <<
"kappa_*newDot[" <<j<<
"] == " << kappa_*newDot[j] <<
"... another step of Gram-Schmidt.\n";
490 #ifdef ANASAZI_TEUCHOS_TIME_MONITOR
493 for (
int i=0; i<nq; i++) {
499 #ifdef ANASAZI_BASIC_ORTHO_DEBUG
500 *out <<
"Applying projector P_Q[" << i <<
"]...\n";
502 MVT::MvTimesMatAddMv( -ONE, *Q[i], C2, ONE, X );
506 if (MQ[i] == Teuchos::null) {
507 #ifdef ANASAZI_BASIC_ORTHO_DEBUG
508 *out <<
"Updating MX via M*X...\n";
510 OPT::Apply( *(this->_Op), X, *MX);
511 this->_OpCounter += MVT::GetNumberVecs(X);
514 #ifdef ANASAZI_BASIC_ORTHO_DEBUG
515 *out <<
"Updating MX via M*Q...\n";
517 MVT::MvTimesMatAddMv( -ONE, *MQ[i], C2, ONE, *MX );
525 #ifdef ANASAZI_BASIC_ORTHO_DEBUG
526 *out <<
"Leaving Anasazi::BasicOrthoManager::projectMat(...)\n";
534 template<
class ScalarType,
class MV,
class OP>
542 int xc = MVT::GetNumberVecs(X);
543 ptrdiff_t xr = MVT::GetGlobalLength(X);
548 if (MX == Teuchos::null) {
550 MX = MVT::Clone(X,xc);
551 OPT::Apply(*(this->_Op),X,*MX);
552 this->_OpCounter += MVT::GetNumberVecs(X);
558 if ( B == Teuchos::null ) {
562 int mxc = (this->_hasOp) ? MVT::GetNumberVecs( *MX ) : xc;
563 ptrdiff_t mxr = (this->_hasOp) ? MVT::GetGlobalLength( *MX ) : xr;
567 "Anasazi::BasicOrthoManager::normalizeMat(): X must be non-empty" );
569 "Anasazi::BasicOrthoManager::normalizeMat(): Size of X not consistent with size of B" );
571 "Anasazi::BasicOrthoManager::normalizeMat(): Size of X not consistent with size of MX" );
572 TEUCHOS_TEST_FOR_EXCEPTION( static_cast<ptrdiff_t>(xc) > xr, std::invalid_argument,
573 "Anasazi::BasicOrthoManager::normalizeMat(): Size of X not feasible for normalization" );
575 return findBasis(X, MX, *B,
true );
582 template<
class ScalarType,
class MV,
class OP>
592 #ifdef ANASAZI_BASIC_ORTHO_DEBUG
595 out = Teuchos::getFancyOStream(Teuchos::rcpFromRef(std::cout));
597 *out <<
"Entering Anasazi::BasicOrthoManager::projectAndNormalizeMat(...)\n";
601 int xc = MVT::GetNumberVecs( X );
602 ptrdiff_t xr = MVT::GetGlobalLength( X );
608 if ( B == Teuchos::null ) {
614 if (MX == Teuchos::null) {
616 #ifdef ANASAZI_BASIC_ORTHO_DEBUG
617 *out <<
"Allocating MX...\n";
619 MX = MVT::Clone(X,MVT::GetNumberVecs(X));
620 OPT::Apply(*(this->_Op),X,*MX);
621 this->_OpCounter += MVT::GetNumberVecs(X);
626 MX = Teuchos::rcpFromRef(X);
629 int mxc = MVT::GetNumberVecs( *MX );
630 ptrdiff_t mxr = MVT::GetGlobalLength( *MX );
632 TEUCHOS_TEST_FOR_EXCEPTION( xc == 0 || xr == 0, std::invalid_argument,
"Anasazi::BasicOrthoManager::projectAndNormalizeMat(): X must be non-empty" );
634 ptrdiff_t numbas = 0;
635 for (
int i=0; i<nq; i++) {
636 numbas += MVT::GetNumberVecs( *Q[i] );
641 "Anasazi::BasicOrthoManager::projectAndNormalizeMat(): Size of X must be consistent with size of B" );
644 "Anasazi::BasicOrthoManager::projectAndNormalizeMat(): MVT returned negative dimensions for X,MX" );
646 TEUCHOS_TEST_FOR_EXCEPTION( xc!=mxc || xr!=mxr, std::invalid_argument,
647 "Anasazi::BasicOrthoManager::projectAndNormalizeMat(): Size of X must be consistent with size of MX" );
649 TEUCHOS_TEST_FOR_EXCEPTION( numbas+xc > xr, std::invalid_argument,
650 "Anasazi::BasicOrthoManager::projectAndNormalizeMat(): Orthogonality constraints not feasible" );
653 #ifdef ANASAZI_BASIC_ORTHO_DEBUG
654 *out <<
"Orthogonalizing X against Q...\n";
656 projectMat(X,Q,C,MX,MQ);
664 int curxsize = xc - rank;
669 #ifdef ANASAZI_BASIC_ORTHO_DEBUG
670 *out <<
"Attempting to find orthonormal basis for X...\n";
672 rank = findBasis(X,MX,*B,
false,curxsize);
674 if (oldrank != -1 && rank != oldrank) {
680 for (
int i=0; i<xc; i++) {
681 (*B)(i,oldrank) = oldCoeff(i,0);
686 if (rank != oldrank) {
694 for (
int i=0; i<xc; i++) {
695 oldCoeff(i,0) = (*B)(i,rank);
702 #ifdef ANASAZI_BASIC_ORTHO_DEBUG
703 *out <<
"Finished computing basis.\n";
708 TEUCHOS_TEST_FOR_EXCEPTION( rank < oldrank,
OrthoError,
709 "Anasazi::BasicOrthoManager::projectAndNormalizeMat(): basis lost rank; this shouldn't happen");
711 if (rank != oldrank) {
727 #ifdef ANASAZI_BASIC_ORTHO_DEBUG
728 *out <<
"Randomizing X[" << rank <<
"]...\n";
731 std::vector<int> ind(1);
733 curX = MVT::CloneViewNonConst(X,ind);
734 MVT::MvRandom(*curX);
736 #ifdef ANASAZI_BASIC_ORTHO_DEBUG
737 *out <<
"Applying operator to random vector.\n";
739 curMX = MVT::CloneViewNonConst(*MX,ind);
740 OPT::Apply( *(this->_Op), *curX, *curMX );
741 this->_OpCounter += MVT::GetNumberVecs(*curX);
750 projectMat(*curX,Q,dummyC,curMX,MQ);
756 TEUCHOS_TEST_FOR_EXCEPTION( rank > xc || rank < 0, std::logic_error,
757 "Anasazi::BasicOrthoManager::projectAndNormalizeMat(): Debug error in rank variable." );
759 #ifdef ANASAZI_BASIC_ORTHO_DEBUG
760 *out <<
"Leaving Anasazi::BasicOrthoManager::projectAndNormalizeMat(...)\n";
771 template<
class ScalarType,
class MV,
class OP>
775 bool completeBasis,
int howMany )
const {
792 #ifdef ANASAZI_BASIC_ORTHO_DEBUG
795 out = Teuchos::getFancyOStream(Teuchos::rcpFromRef(std::cout));
797 *out <<
"Entering Anasazi::BasicOrthoManager::findBasis(...)\n";
800 const ScalarType ONE = SCT::one();
801 const MagnitudeType ZERO = SCT::magnitude(SCT::zero());
803 int xc = MVT::GetNumberVecs( X );
813 "Anasazi::BasicOrthoManager::findBasis(): calling routine did not specify MS.");
815 "Anasazi::BasicOrthoManager::findBasis(): Invalid howMany parameter" );
820 int xstart = xc - howMany;
822 for (
int j = xstart; j < xc; j++) {
831 for (
int i=j+1; i<xc; ++i) {
836 std::vector<int> index(1);
840 if ((this->_hasOp)) {
842 MXj = MVT::CloneViewNonConst( *MX, index );
850 std::vector<int> prev_idx( numX );
854 for (
int i=0; i<numX; ++i) prev_idx[i] = i;
855 prevX = MVT::CloneViewNonConst( X, prev_idx );
857 prevMX = MVT::CloneViewNonConst( *MX, prev_idx );
866 for (
int numTrials = 0; numTrials < 10; numTrials++) {
867 #ifdef ANASAZI_BASIC_ORTHO_DEBUG
868 *out <<
"Trial " << numTrials <<
" for vector " << j <<
"\n";
873 std::vector<MagnitudeType> origNorm(1), newNorm(1), newNorm2(1);
880 #ifdef ANASAZI_BASIC_ORTHO_DEBUG
881 *out <<
"origNorm = " << origNorm[0] <<
"\n";
892 #ifdef ANASAZI_BASIC_ORTHO_DEBUG
893 *out <<
"Orthogonalizing X[" << j <<
"]...\n";
895 MVT::MvTimesMatAddMv( -ONE, *prevX, product, ONE, *Xj );
902 #ifdef ANASAZI_BASIC_ORTHO_DEBUG
903 *out <<
"Updating MX[" << j <<
"]...\n";
905 MVT::MvTimesMatAddMv( -ONE, *prevMX, product, ONE, *MXj );
910 MagnitudeType product_norm = product.normOne();
912 #ifdef ANASAZI_BASIC_ORTHO_DEBUG
913 *out <<
"newNorm = " << newNorm[0] <<
"\n";
914 *out <<
"prodoct_norm = " << product_norm <<
"\n";
918 if ( product_norm/newNorm[0] >= tol_ || newNorm[0] < eps_*origNorm[0]) {
919 #ifdef ANASAZI_BASIC_ORTHO_DEBUG
920 if (product_norm/newNorm[0] >= tol_) {
921 *out <<
"product_norm/newNorm == " << product_norm/newNorm[0] <<
"... another step of Gram-Schmidt.\n";
924 *out <<
"eps*origNorm == " << eps_*origNorm[0] <<
"... another step of Gram-Schmidt.\n";
932 #ifdef ANASAZI_BASIC_ORTHO_DEBUG
933 *out <<
"Orthogonalizing X[" << j <<
"]...\n";
935 MVT::MvTimesMatAddMv( -ONE, *prevX, P2, ONE, *Xj );
936 if ((this->_hasOp)) {
937 #ifdef ANASAZI_BASIC_ORTHO_DEBUG
938 *out <<
"Updating MX[" << j <<
"]...\n";
940 MVT::MvTimesMatAddMv( -ONE, *prevMX, P2, ONE, *MXj );
944 product_norm = P2.normOne();
945 #ifdef ANASAZI_BASIC_ORTHO_DEBUG
946 *out <<
"newNorm2 = " << newNorm2[0] <<
"\n";
947 *out <<
"product_norm = " << product_norm <<
"\n";
949 if ( product_norm/newNorm2[0] >= tol_ || newNorm2[0] < eps_*origNorm[0] ) {
951 #ifdef ANASAZI_BASIC_ORTHO_DEBUG
952 if (product_norm/newNorm2[0] >= tol_) {
953 *out <<
"product_norm/newNorm2 == " << product_norm/newNorm2[0] <<
"... setting vector to zero.\n";
955 else if (newNorm[0] < newNorm2[0]) {
956 *out <<
"newNorm2 > newNorm... setting vector to zero.\n";
959 *out <<
"eps*origNorm == " << eps_*origNorm[0] <<
"... setting vector to zero.\n";
962 MVT::MvInit(*Xj,ZERO);
963 if ((this->_hasOp)) {
964 #ifdef ANASAZI_BASIC_ORTHO_DEBUG
965 *out <<
"Setting MX[" << j <<
"] to zero as well.\n";
967 MVT::MvInit(*MXj,ZERO);
974 if (numTrials == 0) {
975 for (
int i=0; i<numX; i++) {
976 B(i,j) = product(i,0);
982 if ( newNorm[0] != ZERO && newNorm[0] > SCT::sfmin() ) {
983 #ifdef ANASAZI_BASIC_ORTHO_DEBUG
984 *out <<
"Normalizing X[" << j <<
"], norm(X[" << j <<
"]) = " << newNorm[0] <<
"\n";
988 MVT::MvScale( *Xj, ONE/newNorm[0]);
990 #ifdef ANASAZI_BASIC_ORTHO_DEBUG
991 *out <<
"Normalizing M*X[" << j <<
"]...\n";
994 MVT::MvScale( *MXj, ONE/newNorm[0]);
998 if (numTrials == 0) {
1007 #ifdef ANASAZI_BASIC_ORTHO_DEBUG
1008 *out <<
"Not normalizing M*X[" << j <<
"]...\n";
1015 if (completeBasis) {
1017 #ifdef ANASAZI_BASIC_ORTHO_DEBUG
1018 *out <<
"Inserting random vector in X[" << j <<
"]...\n";
1020 MVT::MvRandom( *Xj );
1022 #ifdef ANASAZI_BASIC_ORTHO_DEBUG
1023 *out <<
"Updating M*X[" << j <<
"]...\n";
1025 OPT::Apply( *(this->_Op), *Xj, *MXj );
1026 this->_OpCounter += MVT::GetNumberVecs(*Xj);
1037 if (rankDef ==
true) {
1039 "Anasazi::BasicOrthoManager::findBasis(): Unable to complete basis" );
1040 #ifdef ANASAZI_BASIC_ORTHO_DEBUG
1041 *out <<
"Returning early, rank " << j <<
" from Anasazi::BasicOrthoManager::findBasis(...)\n";
1048 #ifdef ANASAZI_BASIC_ORTHO_DEBUG
1049 *out <<
"Returning " << xc <<
" from Anasazi::BasicOrthoManager::findBasis(...)\n";
1056 #endif // ANASAZI_BASIC_ORTHOMANAGER_HPP
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.
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 ...
ScalarType LAMCH(const char &CMACH) const
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.