35 #ifndef ANASAZI_SVQB_ORTHOMANAGER_HPP
36 #define ANASAZI_SVQB_ORTHOMANAGER_HPP
51 #include "Teuchos_LAPACK.hpp"
55 template<
class ScalarType,
class MV,
class OP>
59 typedef typename Teuchos::ScalarTraits<ScalarType>::magnitudeType MagnitudeType;
60 typedef Teuchos::ScalarTraits<ScalarType> SCT;
61 typedef Teuchos::ScalarTraits<MagnitudeType> SCTM;
72 SVQBOrthoManager( Teuchos::RCP<const OP> Op = Teuchos::null,
bool debug =
false );
126 Teuchos::Array<Teuchos::RCP<const MV> > Q,
127 Teuchos::Array<Teuchos::RCP<Teuchos::SerialDenseMatrix<int,ScalarType> > > C
128 = Teuchos::tuple(Teuchos::RCP< Teuchos::SerialDenseMatrix<int,ScalarType> >(Teuchos::null)),
129 Teuchos::RCP<MV> MX = Teuchos::null,
130 Teuchos::Array<Teuchos::RCP<const MV> > MQ = Teuchos::tuple(Teuchos::RCP<const MV>(Teuchos::null))
174 Teuchos::RCP<Teuchos::SerialDenseMatrix<int,ScalarType> > B = Teuchos::null,
175 Teuchos::RCP<MV> MX = Teuchos::null
240 Teuchos::Array<Teuchos::RCP<const MV> > Q,
241 Teuchos::Array<Teuchos::RCP<Teuchos::SerialDenseMatrix<int,ScalarType> > > C
242 = Teuchos::tuple(Teuchos::RCP< Teuchos::SerialDenseMatrix<int,ScalarType> >(Teuchos::null)),
243 Teuchos::RCP<Teuchos::SerialDenseMatrix<int,ScalarType> > B = Teuchos::null,
244 Teuchos::RCP<MV> MX = Teuchos::null,
245 Teuchos::Array<Teuchos::RCP<const MV> > MQ = Teuchos::tuple(Teuchos::RCP<const MV>(Teuchos::null))
257 typename Teuchos::ScalarTraits<ScalarType>::magnitudeType
264 typename Teuchos::ScalarTraits<ScalarType>::magnitudeType
268 Teuchos::RCP<const MV> MX = Teuchos::null,
269 Teuchos::RCP<const MV> MY = Teuchos::null
280 int findBasis(MV &X, Teuchos::RCP<MV> MX,
281 Teuchos::Array<Teuchos::RCP<Teuchos::SerialDenseMatrix<int,ScalarType> > > C,
282 Teuchos::RCP<Teuchos::SerialDenseMatrix<int,ScalarType> > B,
283 Teuchos::Array<Teuchos::RCP<const MV> > Q,
284 bool normalize_in )
const;
290 template<
class ScalarType,
class MV,
class OP>
292 :
MatOrthoManager<ScalarType,MV,OP>(Op), dbgstr(
" *** "), debug_(debug) {
294 Teuchos::LAPACK<int,MagnitudeType> lapack;
295 eps_ = lapack.LAMCH(
'E');
297 std::cout <<
"eps_ == " << eps_ << std::endl;
304 template<
class ScalarType,
class MV,
class OP>
305 typename Teuchos::ScalarTraits<ScalarType>::magnitudeType
307 const ScalarType ONE = SCT::one();
308 int rank = MVT::GetNumberVecs(X);
309 Teuchos::SerialDenseMatrix<int,ScalarType> xTx(rank,rank);
311 for (
int i=0; i<rank; i++) {
314 return xTx.normFrobenius();
319 template<
class ScalarType,
class MV,
class OP>
320 typename Teuchos::ScalarTraits<ScalarType>::magnitudeType
324 Teuchos::RCP<const MV> MX,
325 Teuchos::RCP<const MV> MY
327 int r1 = MVT::GetNumberVecs(X);
328 int r2 = MVT::GetNumberVecs(Y);
329 Teuchos::SerialDenseMatrix<int,ScalarType> xTx(r1,r2);
331 return xTx.normFrobenius();
337 template<
class ScalarType,
class MV,
class OP>
340 Teuchos::Array<Teuchos::RCP<const MV> > Q,
341 Teuchos::Array<Teuchos::RCP<Teuchos::SerialDenseMatrix<int,ScalarType> > > C,
343 Teuchos::Array<Teuchos::RCP<const MV> > MQ)
const {
345 findBasis(X,MX,C,Teuchos::null,Q,
false);
352 template<
class ScalarType,
class MV,
class OP>
355 Teuchos::RCP<Teuchos::SerialDenseMatrix<int,ScalarType> > B,
356 Teuchos::RCP<MV> MX)
const {
357 Teuchos::Array<Teuchos::RCP<Teuchos::SerialDenseMatrix<int,ScalarType> > > C;
358 Teuchos::Array<Teuchos::RCP<const MV> > Q;
359 return findBasis(X,MX,C,B,Q,
true);
366 template<
class ScalarType,
class MV,
class OP>
369 Teuchos::Array<Teuchos::RCP<const MV> > Q,
370 Teuchos::Array<Teuchos::RCP<Teuchos::SerialDenseMatrix<int,ScalarType> > > C,
371 Teuchos::RCP<Teuchos::SerialDenseMatrix<int,ScalarType> > B,
373 Teuchos::Array<Teuchos::RCP<const MV> > MQ)
const {
375 return findBasis(X,MX,C,B,Q,
true);
407 template<
class ScalarType,
class MV,
class OP>
409 MV &X, Teuchos::RCP<MV> MX,
410 Teuchos::Array<Teuchos::RCP<Teuchos::SerialDenseMatrix<int,ScalarType> > > C,
411 Teuchos::RCP<Teuchos::SerialDenseMatrix<int,ScalarType> > B,
412 Teuchos::Array<Teuchos::RCP<const MV> > Q,
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) {
439 TEUCHOS_TEST_FOR_EXCEPTION(
true, std::invalid_argument,
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)) {
450 TEUCHOS_TEST_FOR_EXCEPTION(
true, std::invalid_argument,
451 "Anasazi::SVQBOrthoManager::findBasis(): X must be non-empty" );
455 TEUCHOS_TEST_FOR_EXCEPTION( qsize != 0 && qr != xr , std::invalid_argument,
456 "Anasazi::SVQBOrthoManager::findBasis(): Size of X not consistant with size of Q" );
463 Teuchos::Array<Teuchos::RCP<Teuchos::SerialDenseMatrix<int,ScalarType> > > newC(nq);
465 for (
int i=0; i<nq; i++) {
467 TEUCHOS_TEST_FOR_EXCEPTION( MVText::GetGlobalLength( *Q[i] ) != qr, std::invalid_argument,
468 "Anasazi::SVQBOrthoManager::findBasis(): Size of Q not mutually consistant" );
469 TEUCHOS_TEST_FOR_EXCEPTION( qr < qcs[i], std::invalid_argument,
470 "Anasazi::SVQBOrthoManager::findBasis(): Q has less rows than columns" );
472 if ( C[i] == Teuchos::null ) {
473 C[i] = Teuchos::rcp(
new Teuchos::SerialDenseMatrix<int,ScalarType>(qcs[i],xc) );
476 TEUCHOS_TEST_FOR_EXCEPTION( C[i]->numRows() != qcs[i] || C[i]->numCols() != xc, std::invalid_argument,
477 "Anasazi::SVQBOrthoManager::findBasis(): Size of Q not consistant with C" );
480 C[i]->putScalar(ZERO);
481 newC[i] = Teuchos::rcp(
new Teuchos::SerialDenseMatrix<int,ScalarType>(*C[i]) );
490 if (normalize_in ==
true) {
491 if ( B == Teuchos::null ) {
492 B = Teuchos::rcp(
new Teuchos::SerialDenseMatrix<int,ScalarType>(xc,xc) );
494 TEUCHOS_TEST_FOR_EXCEPTION( B->numRows() != xc || B->numCols() != xc, std::invalid_argument,
495 "Anasazi::SVQBOrthoManager::findBasis(): Size of B not consistant with X" );
498 for (
int i=0; i<xc; i++) {
510 Teuchos::RCP<MV> workX;
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);
526 Teuchos::SerialDenseMatrix<int,ScalarType> XtMX(xc,xc), workU(1,1);
527 Teuchos::LAPACK<int,ScalarType> lapack;
532 std::vector<ScalarType> work;
533 std::vector<MagnitudeType> lambda, lambdahi, rwork;
536 int lwork = lapack.ILAENV(1,
"hetrd",
"VU",xc,-1,-1,-1);
539 TEUCHOS_TEST_FOR_EXCEPTION( lwork < 0, OrthoError,
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;
556 TEUCHOS_TEST_FOR_EXCEPTION( xc != mxc || xr != mxr, std::invalid_argument,
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++) {
652 info = C[i]->multiply(Teuchos::NO_TRANS,Teuchos::NO_TRANS,ONE,*newC[i],*B,ONE);
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";
700 TEUCHOS_TEST_FOR_EXCEPTION( info != 0, OrthoError,
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);
776 info = B->multiply(Teuchos::CONJ_TRANS,Teuchos::NO_TRANS,ONE,XtMX,workU,ZERO);
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++) {
796 Teuchos::RCP<MV> Xnull,MXnull;
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 ...
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.
An implementation of the Anasazi::MatOrthoManager that performs orthogonalization using the SVQB iter...
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::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 ...
~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.