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>
336 for (
int i=0; i<rank; i++) {
346 template<
class ScalarType,
class MV,
class OP>
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";
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";
412 #ifdef ANASAZI_BASIC_ORTHO_DEBUG 413 *out <<
"Allocating MX...\n";
415 if (MX == Teuchos::null) {
424 MX = Teuchos::rcpFromRef(X);
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" );
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 );
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";
481 if (MQ[i] == Teuchos::null) {
482 #ifdef ANASAZI_BASIC_ORTHO_DEBUG 483 *out <<
"Updating MX via M*X...\n";
489 #ifdef ANASAZI_BASIC_ORTHO_DEBUG 490 *out <<
"Updating MX via M*Q...\n";
498 std::vector<ScalarType> newDot(xc);
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) {
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";
529 if (MQ[i] == Teuchos::null) {
530 #ifdef ANASAZI_BASIC_ORTHO_DEBUG 531 *out <<
"Updating MX via M*X...\n";
537 #ifdef ANASAZI_BASIC_ORTHO_DEBUG 538 *out <<
"Updating MX via M*Q...\n";
548 #ifdef ANASAZI_BASIC_ORTHO_DEBUG 549 *out <<
"Leaving Anasazi::BasicOrthoManager::projectMat(...)\n";
557 template<
class ScalarType,
class MV,
class OP>
571 if (MX == Teuchos::null) {
581 if ( B == Teuchos::null ) {
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";
631 if ( B == Teuchos::null ) {
637 if (MX == Teuchos::null) {
639 #ifdef ANASAZI_BASIC_ORTHO_DEBUG 640 *out <<
"Allocating MX...\n";
649 MX = Teuchos::rcpFromRef(X);
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++) {
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";
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);
759 #ifdef ANASAZI_BASIC_ORTHO_DEBUG 760 *out <<
"Applying operator to random vector.\n";
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";
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)) {
873 std::vector<int> prev_idx( numX );
877 for (
int i=0; i<numX; ++i) prev_idx[i] = i;
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";
925 #ifdef ANASAZI_BASIC_ORTHO_DEBUG 926 *out <<
"Updating MX[" << j <<
"]...\n";
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";
959 if ((this->_hasOp)) {
960 #ifdef ANASAZI_BASIC_ORTHO_DEBUG 961 *out <<
"Updating MX[" << j <<
"]...\n";
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";
986 if ((this->_hasOp)) {
987 #ifdef ANASAZI_BASIC_ORTHO_DEBUG 988 *out <<
"Setting MX[" << j <<
"] to zero as well.\n";
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";
1013 #ifdef ANASAZI_BASIC_ORTHO_DEBUG 1014 *out <<
"Normalizing M*X[" << j <<
"]...\n";
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";
1045 #ifdef ANASAZI_BASIC_ORTHO_DEBUG 1046 *out <<
"Updating M*X[" << j <<
"]...\n";
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 ScalarTraits< ScalarType >::magnitudeType normOne() const
static void MvInit(MV &mv, const ScalarType alpha=Teuchos::ScalarTraits< ScalarType >::zero())
Replace each element of the vectors in mv with alpha.
static void MvRandom(MV &mv)
Replace the vectors in mv with random vectors.
static void MvTimesMatAddMv(const ScalarType alpha, const MV &A, const Teuchos::SerialDenseMatrix< int, ScalarType > &B, const ScalarType beta, MV &mv)
Update mv with .
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.
static ptrdiff_t GetGlobalLength(const MV &mv)
~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
static void MvDot(const MV &mv, const MV &A, std::vector< ScalarType > &b)
Compute a vector b where the components are the individual dot-products of the i-th columns of A and ...
static void Apply(const OP &Op, const MV &x, MV &y)
Application method which performs operation y = Op*x. An OperatorError exception is thrown if there i...
static magnitudeType sfmin()
ScalarTraits< ScalarType >::magnitudeType normFrobenius() const
Namespace Anasazi contains the classes, structs, enums and utilities used by the Anasazi package...
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.
static Teuchos::RCP< MV > CloneCopy(const MV &mv)
Creates a new MV and copies contents of mv into the new vector (deep copy).
Virtual base class which defines basic traits for the operator type.
static void MvScale(MV &mv, const ScalarType alpha)
Scale each element of the vectors in mv with alpha.
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 ...
static Teuchos::RCP< MV > CloneViewNonConst(MV &mv, const std::vector< int > &index)
Creates a new MV that shares the selected contents of mv (shallow copy).
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(ScalarType a)
static int GetNumberVecs(const MV &mv)
Obtain the number of vectors in mv.
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.
static Teuchos::RCP< MV > Clone(const MV &mv, const int numvecs)
Creates a new empty MV containing numvecs columns.