35 #ifndef ANASAZI_SVQB_ORTHOMANAGER_HPP
36 #define ANASAZI_SVQB_ORTHOMANAGER_HPP
55 template<
class ScalarType,
class MV,
class OP>
284 bool normalize_in )
const;
290 template<
class ScalarType,
class MV,
class OP>
292 :
MatOrthoManager<ScalarType,MV,OP>(Op), dbgstr(
" *** "), debug_(debug) {
295 eps_ = lapack.
LAMCH(
'E');
297 std::cout <<
"eps_ == " << eps_ << std::endl;
304 template<
class ScalarType,
class MV,
class OP>
307 const ScalarType ONE = SCT::one();
308 int rank = MVT::GetNumberVecs(X);
311 for (
int i=0; i<rank; i++) {
319 template<
class ScalarType,
class MV,
class OP>
327 int r1 = MVT::GetNumberVecs(X);
328 int r2 = MVT::GetNumberVecs(Y);
337 template<
class ScalarType,
class MV,
class OP>
345 findBasis(X,MX,C,Teuchos::null,Q,
false);
352 template<
class ScalarType,
class MV,
class OP>
359 return findBasis(X,MX,C,B,Q,
true);
366 template<
class ScalarType,
class MV,
class OP>
375 return findBasis(X,MX,C,B,Q,
true);
407 template<
class ScalarType,
class MV,
class OP>
413 bool normalize_in)
const {
415 const ScalarType ONE = SCT::one();
416 const MagnitudeType MONE = SCTM::one();
417 const MagnitudeType ZERO = SCTM::zero();
424 int xc = MVT::GetNumberVecs(X);
425 ptrdiff_t xr = MVText::GetGlobalLength( X );
429 ptrdiff_t qr = (nq == 0) ? 0 : MVText::GetGlobalLength(*Q[0]);
431 std::vector<int> qcs(nq);
432 for (
int i=0; i<nq; i++) {
433 qcs[i] = MVT::GetNumberVecs(*Q[i]);
437 if (normalize_in ==
true && qsize + xc > xr) {
440 "Anasazi::SVQBOrthoManager::findBasis(): Orthogonalization constraints not feasible" );
444 if (normalize_in ==
false && (qsize == 0 || xc == 0)) {
448 else if (normalize_in ==
true && (xc == 0 || xr == 0)) {
451 "Anasazi::SVQBOrthoManager::findBasis(): X must be non-empty" );
456 "Anasazi::SVQBOrthoManager::findBasis(): Size of X not consistant with size of Q" );
465 for (
int i=0; i<nq; i++) {
468 "Anasazi::SVQBOrthoManager::findBasis(): Size of Q not mutually consistant" );
470 "Anasazi::SVQBOrthoManager::findBasis(): Q has less rows than columns" );
472 if ( C[i] == Teuchos::null ) {
477 "Anasazi::SVQBOrthoManager::findBasis(): Size of Q not consistant with C" );
480 C[i]->putScalar(ZERO);
490 if (normalize_in ==
true) {
491 if ( B == Teuchos::null ) {
495 "Anasazi::SVQBOrthoManager::findBasis(): Size of B not consistant with X" );
498 for (
int i=0; i<xc; i++) {
512 workX = MVT::Clone(X,xc);
515 if (MX == Teuchos::null) {
517 MX = MVT::Clone(X,xc);
518 OPT::Apply(*(this->_Op),X,*MX);
519 this->_OpCounter += MVT::GetNumberVecs(X);
523 MX = Teuchos::rcpFromRef(X);
525 std::vector<ScalarType> normX(xc), invnormX(xc);
532 std::vector<ScalarType> work;
533 std::vector<MagnitudeType> lambda, lambdahi, rwork;
536 int lwork = lapack.
ILAENV(1,
"hetrd",
"VU",xc,-1,-1,-1);
540 "Anasazi::SVQBOrthoManager::findBasis(): Error code from ILAENV" );
542 lwork = (lwork+2)*xc;
545 lwork = (3*xc-2 > 1) ? 3*xc - 2 : 1;
550 workU.reshape(xc,xc);
554 int mxc = (this->_hasOp) ? MVT::GetNumberVecs( *MX ) : xc;
555 ptrdiff_t mxr = (this->_hasOp) ? MVText::GetGlobalLength( *MX ) : xr;
557 "Anasazi::SVQBOrthoManager::findBasis(): Size of X not consistant with MX" );
560 bool doGramSchmidt =
true;
562 MagnitudeType tolerance = MONE/SCTM::squareroot(eps_);
565 while (doGramSchmidt) {
575 std::vector<MagnitudeType> normX_mag(xc);
577 for (
int i=0; i<xc; ++i) {
578 normX[i] = normX_mag[i];
579 invnormX[i] = (normX_mag[i] == ZERO) ? ZERO : MONE/normX_mag[i];
583 MVT::MvScale(X,invnormX);
585 MVT::MvScale(*MX,invnormX);
589 std::vector<MagnitudeType> nrm2(xc);
590 std::cout << dbgstr <<
"max post-scale norm: (with/without MX) : ";
591 MagnitudeType maxpsnw = ZERO, maxpsnwo = ZERO;
593 for (
int i=0; i<xc; i++) {
594 maxpsnw = (nrm2[i] > maxpsnw ? nrm2[i] : maxpsnw);
597 for (
int i=0; i<xc; i++) {
598 maxpsnwo = (nrm2[i] > maxpsnwo ? nrm2[i] : maxpsnwo);
600 std::cout <<
"(" << maxpsnw <<
"," << maxpsnwo <<
")" << std::endl;
603 for (
int i=0; i<nq; i++) {
607 for (
int i=0; i<nq; i++) {
608 MVT::MvTimesMatAddMv(-ONE,*Q[i],*newC[i],ONE,X);
611 MVT::MvScale(X,normX);
614 OPT::Apply(*(this->_Op),X,*MX);
615 this->_OpCounter += MVT::GetNumberVecs(X);
623 MagnitudeType maxNorm = ZERO;
624 for (
int j=0; j<xc; j++) {
625 MagnitudeType sum = ZERO;
626 for (
int k=0; k<nq; k++) {
627 for (
int i=0; i<qcs[k]; i++) {
628 sum += SCT::magnitude((*newC[k])(i,j))*SCT::magnitude((*newC[k])(i,j));
631 maxNorm = (sum > maxNorm) ? sum : maxNorm;
635 if (maxNorm < 0.36) {
636 doGramSchmidt =
false;
640 for (
int k=0; k<nq; k++) {
641 for (
int j=0; j<xc; j++) {
642 for (
int i=0; i<qcs[k]; i++) {
643 (*newC[k])(i,j) *= normX[j];
651 for (
int i=0; i<nq; i++) {
653 TEUCHOS_TEST_FOR_EXCEPTION(info != 0, std::logic_error,
"Anasazi::SVQBOrthoManager::findBasis(): Input error to SerialDenseMatrix::multiply.");
658 for (
int i=0; i<nq; i++) {
665 doGramSchmidt =
false;
673 MagnitudeType condT = tolerance;
675 while (condT >= tolerance) {
683 std::vector<MagnitudeType> Dh(xc), Dhi(xc);
684 for (
int i=0; i<xc; i++) {
685 Dh[i] = SCT::magnitude(SCT::squareroot(XtMX(i,i)));
686 Dhi[i] = (Dh[i] == ZERO ? ZERO : MONE/Dh[i]);
689 for (
int i=0; i<xc; i++) {
690 for (
int j=0; j<xc; j++) {
691 XtMX(i,j) *= Dhi[i]*Dhi[j];
697 lapack.
HEEV(
'V',
'U', xc, XtMX.values(), XtMX.stride(), &lambda[0], &work[0], work.size(), &rwork[0], &info);
698 std::stringstream os;
699 os <<
"Anasazi::SVQBOrthoManager::findBasis(): Error code " << info <<
" from HEEV";
703 std::cout << dbgstr <<
"eigenvalues of XtMX: (";
704 for (
int i=0; i<xc-1; i++) {
705 std::cout << lambda[i] <<
",";
707 std::cout << lambda[xc-1] <<
")" << std::endl;
712 MagnitudeType maxLambda = lambda[xc-1],
713 minLambda = lambda[0];
715 for (
int i=0; i<xc; i++) {
716 if (lambda[i] <= 10*eps_*maxLambda) {
728 lambda[i] = SCTM::squareroot(lambda[i]);
729 lambdahi[i] = MONE/lambda[i];
736 std::vector<int> ind(xc);
737 for (
int i=0; i<xc; i++) {ind[i] = i;}
738 MVT::SetBlock(X,ind,*workX);
742 for (
int j=0; j<xc; j++) {
743 for (
int i=0; i<xc; i++) {
744 workU(i,j) *= Dhi[i]*lambdahi[j];
748 MVT::MvTimesMatAddMv(ONE,*workX,workU,ZERO,X);
754 if (maxLambda >= tolerance * minLambda) {
756 OPT::Apply(*(this->_Op),X,*MX);
757 this->_OpCounter += MVT::GetNumberVecs(X);
762 MVT::SetBlock(*MX,ind,*workX);
765 MVT::MvTimesMatAddMv(ONE,*workX,workU,ZERO,*MX);
771 for (
int j=0; j<xc; j++) {
772 for (
int i=0; i<xc; i++) {
773 workU(i,j) = Dh[i] * (*B)(i,j);
777 TEUCHOS_TEST_FOR_EXCEPTION(info != 0, std::logic_error,
"Anasazi::SVQBOrthoManager::findBasis(): Input error to SerialDenseMatrix::multiply.");
778 for (
int j=0; j<xc ;j++) {
779 for (
int i=0; i<xc; i++) {
780 (*B)(i,j) *= lambda[i];
787 std::cout << dbgstr <<
"augmenting multivec with " << iZeroMax+1 <<
" random directions" << std::endl;
792 std::vector<int> ind2(iZeroMax+1);
793 for (
int i=0; i<iZeroMax+1; i++) {
797 Xnull = MVT::CloneViewNonConst(X,ind2);
798 MVT::MvRandom(*Xnull);
800 MXnull = MVT::CloneViewNonConst(*MX,ind2);
801 OPT::Apply(*(this->_Op),*Xnull,*MXnull);
802 this->_OpCounter += MVT::GetNumberVecs(*Xnull);
803 MXnull = Teuchos::null;
805 Xnull = Teuchos::null;
807 doGramSchmidt =
true;
811 condT = SCTM::magnitude(maxLambda / minLambda);
813 std::cout << dbgstr <<
"condT: " << condT <<
", tolerance = " << tolerance << std::endl;
817 if ((doGramSchmidt ==
false) && (condT > SCTM::squareroot(tolerance))) {
818 doGramSchmidt =
true;
828 std::cout << dbgstr <<
"(numGS,numSVQB,numRand) : "
840 #endif // ANASAZI_SVQB_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 ...
Declaration of basic traits for the multivector type.
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 ...
OrdinalType ILAENV(const OrdinalType ispec, const std::string &NAME, const std::string &OPTS, const OrdinalType N1=-1, const OrdinalType N2=-1, const OrdinalType N3=-1, const OrdinalType N4=-1) const
#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
An implementation of the Anasazi::MatOrthoManager that performs orthogonalization using the SVQB iter...
ScalarTraits< ScalarType >::magnitudeType normFrobenius() const
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 ...
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)
Teuchos::ScalarTraits< ScalarType >::magnitudeType orthogErrorMat(const MV &X, const MV &Y, Teuchos::RCP< const MV > MX=Teuchos::null, Teuchos::RCP< const MV > MY=Teuchos::null) const
This method computes the error in orthogonality of two multivectors, measured as the Frobenius norm o...
Traits class which defines basic operations on multivectors.
Virtual base class which defines basic traits for the operator type.
Anasazi header file which uses auto-configuration information to include necessary C++ headers...
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 ...
void HEEV(const char JOBZ, const char UPLO, const OrdinalType n, ScalarType *A, const OrdinalType lda, MagnitudeType *W, ScalarType *WORK, const OrdinalType lwork, MagnitudeType *RWORK, OrdinalType *info) const
~SVQBOrthoManager()
Destructor.
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...
SVQBOrthoManager(Teuchos::RCP< const OP > Op=Teuchos::null, bool debug=false)
Constructor specifying re-orthogonalization tolerance.
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.