Anasazi  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
AnasaziICGSOrthoManager.hpp
Go to the documentation of this file.
1 // @HEADER
2 // ***********************************************************************
3 //
4 // Anasazi: Block Eigensolvers Package
5 // Copyright (2004) Sandia Corporation
6 //
7 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
8 // license for use of this work by or on behalf of the U.S. Government.
9 //
10 // This library is free software; you can redistribute it and/or modify
11 // it under the terms of the GNU Lesser General Public License as
12 // published by the Free Software Foundation; either version 2.1 of the
13 // License, or (at your option) any later version.
14 //
15 // This library is distributed in the hope that it will be useful, but
16 // WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 // Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public
21 // License along with this library; if not, write to the Free Software
22 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
23 // USA
24 // Questions? Contact Michael A. Heroux (maherou@sandia.gov)
25 //
26 // ***********************************************************************
27 // @HEADER
28 
29 
34 #ifndef ANASAZI_ICSG_ORTHOMANAGER_HPP
35 #define ANASAZI_ICSG_ORTHOMANAGER_HPP
36 
44 #include "AnasaziConfigDefs.hpp"
48 #include "Teuchos_TimeMonitor.hpp"
49 #include "Teuchos_LAPACK.hpp"
50 #include "Teuchos_BLAS.hpp"
51 #ifdef ANASAZI_ICGS_DEBUG
52 # include <Teuchos_FancyOStream.hpp>
53 #endif
54 
55 namespace Anasazi {
56 
57  template<class ScalarType, class MV, class OP>
58  class ICGSOrthoManager : public GenOrthoManager<ScalarType,MV,OP> {
59 
60  private:
61  typedef typename Teuchos::ScalarTraits<ScalarType>::magnitudeType MagnitudeType;
66 
67  public:
68 
70 
71  ICGSOrthoManager( Teuchos::RCP<const OP> Op = Teuchos::null, int numIters = 2,
75 
76 
80 
81 
83 
84 
166  void projectGen(
167  MV &S,
170  bool isBiOrtho,
172  = Teuchos::tuple(Teuchos::RCP< Teuchos::SerialDenseMatrix<int,ScalarType> >(Teuchos::null)),
173  Teuchos::RCP<MV> MS = Teuchos::null,
174  Teuchos::Array<Teuchos::RCP<const MV> > MX = Teuchos::tuple(Teuchos::RCP<const MV>(Teuchos::null)),
175  Teuchos::Array<Teuchos::RCP<const MV> > MY = Teuchos::tuple(Teuchos::RCP<const MV>(Teuchos::null))
176  ) const;
177 
178 
271  MV &S,
274  bool isBiOrtho,
276  = Teuchos::tuple(Teuchos::RCP< Teuchos::SerialDenseMatrix<int,ScalarType> >(Teuchos::null)),
278  Teuchos::RCP<MV> MS = Teuchos::null,
279  Teuchos::Array<Teuchos::RCP<const MV> > MX = Teuchos::tuple(Teuchos::RCP<const MV>(Teuchos::null)),
280  Teuchos::Array<Teuchos::RCP<const MV> > MY = Teuchos::tuple(Teuchos::RCP<const MV>(Teuchos::null))
281  ) const;
282 
283 
285 
286 
288 
289 
290 
302  void projectMat (
303  MV &X,
306  = Teuchos::tuple(Teuchos::RCP< Teuchos::SerialDenseMatrix<int,ScalarType> >(Teuchos::null)),
307  Teuchos::RCP<MV> MX = Teuchos::null,
308  Teuchos::Array<Teuchos::RCP<const MV> > MQ = Teuchos::tuple(Teuchos::RCP<const MV>(Teuchos::null))
309  ) const;
310 
311 
320  int normalizeMat (
321  MV &X,
323  Teuchos::RCP<MV> MX = Teuchos::null
324  ) const;
325 
326 
336  MV &X,
339  = Teuchos::tuple(Teuchos::RCP< Teuchos::SerialDenseMatrix<int,ScalarType> >(Teuchos::null)),
341  Teuchos::RCP<MV> MX = Teuchos::null,
342  Teuchos::Array<Teuchos::RCP<const MV> > MQ = Teuchos::tuple(Teuchos::RCP<const MV>(Teuchos::null))
343  ) const;
344 
346 
348 
349 
355  orthonormErrorMat(const MV &X, Teuchos::RCP<const MV> MX = Teuchos::null) const;
356 
362  orthogErrorMat(const MV &X1, const MV &X2, Teuchos::RCP<const MV> MX1, Teuchos::RCP<const MV> MX2) const;
363 
365 
367 
368 
370  void setNumIters( int numIters ) {
371  numIters_ = numIters;
372  TEUCHOS_TEST_FOR_EXCEPTION(numIters_ < 1,std::invalid_argument,
373  "Anasazi::ICGSOrthoManager::setNumIters(): input must be >= 1.");
374  }
375 
377  int getNumIters() const { return numIters_; }
378 
380 
381  private:
382  MagnitudeType eps_;
383  MagnitudeType tol_;
384 
386  int numIters_;
387 
388  // ! Routine to find an orthonormal basis
389  int findBasis(MV &X, Teuchos::RCP<MV> MX,
391  bool completeBasis, int howMany = -1) const;
392  };
393 
394 
395 
397  // Constructor
398  template<class ScalarType, class MV, class OP>
400  int numIters,
403  GenOrthoManager<ScalarType,MV,OP>(Op), eps_(eps), tol_(tol)
404  {
405  setNumIters(numIters);
406  TEUCHOS_TEST_FOR_EXCEPTION(eps_ < SCT::magnitude(SCT::zero()),std::invalid_argument,
407  "Anasazi::ICGSOrthoManager::ICGSOrthoManager(): argument \"eps\" must be non-negative.");
408  if (eps_ == 0) {
410  eps_ = lapack.LAMCH('E');
412  }
414  tol_ < SCT::magnitude(SCT::zero()) || tol_ > SCT::magnitude(SCT::one()),
415  std::invalid_argument,
416  "Anasazi::ICGSOrthoManager::ICGSOrthoManager(): argument \"tol\" must be in [0,1].");
417  }
418 
419 
420 
422  // Compute the distance from orthonormality
423  template<class ScalarType, class MV, class OP>
426  const ScalarType ONE = SCT::one();
427  int rank = MVT::GetNumberVecs(X);
430  for (int i=0; i<rank; i++) {
431  xTx(i,i) -= ONE;
432  }
433  return xTx.normFrobenius();
434  }
435 
436 
437 
439  // Compute the distance from orthogonality
440  template<class ScalarType, class MV, class OP>
443  int r1 = MVT::GetNumberVecs(X1);
444  int r2 = MVT::GetNumberVecs(X2);
447  return xTx.normFrobenius();
448  }
449 
450 
451 
453  template<class ScalarType, class MV, class OP>
455  MV &X,
458  Teuchos::RCP<MV> MX,
460  ) const
461  {
462  projectGen(X,Q,Q,true,C,MX,MQ,MQ);
463  }
464 
465 
466 
468  // Find an Op-orthonormal basis for span(X), with rank numvectors(X)
469  template<class ScalarType, class MV, class OP>
471  MV &X,
473  Teuchos::RCP<MV> MX) const
474  {
475  // call findBasis(), with the instruction to try to generate a basis of rank numvecs(X)
476  // findBasis() requires MX
477 
478  int xc = MVT::GetNumberVecs(X);
479  ptrdiff_t xr = MVText::GetGlobalLength(X);
480 
481  // if Op==null, MX == X (via pointer)
482  // Otherwise, either the user passed in MX or we will allocated and compute it
483  if (this->_hasOp) {
484  if (MX == Teuchos::null) {
485  // we need to allocate space for MX
486  MX = MVT::Clone(X,xc);
487  OPT::Apply(*(this->_Op),X,*MX);
488  this->_OpCounter += MVT::GetNumberVecs(X);
489  }
490  }
491 
492  // if the user doesn't want to store the coefficients,
493  // allocate some local memory for them
494  if ( B == Teuchos::null ) {
496  }
497 
498  int mxc = (this->_hasOp) ? MVT::GetNumberVecs( *MX ) : xc;
499  ptrdiff_t mxr = (this->_hasOp) ? MVText::GetGlobalLength( *MX ) : xr;
500 
501  // check size of C, B
502  TEUCHOS_TEST_FOR_EXCEPTION( xc == 0 || xr == 0, std::invalid_argument,
503  "Anasazi::ICGSOrthoManager::normalizeMat(): X must be non-empty" );
504  TEUCHOS_TEST_FOR_EXCEPTION( B->numRows() != xc || B->numCols() != xc, std::invalid_argument,
505  "Anasazi::ICGSOrthoManager::normalizeMat(): Size of X not consistent with size of B" );
506  TEUCHOS_TEST_FOR_EXCEPTION( xc != mxc || xr != mxr, std::invalid_argument,
507  "Anasazi::ICGSOrthoManager::normalizeMat(): Size of X not consistent with size of MX" );
508  TEUCHOS_TEST_FOR_EXCEPTION( static_cast<ptrdiff_t>(xc) > xr, std::invalid_argument,
509  "Anasazi::ICGSOrthoManager::normalizeMat(): Size of X not feasible for normalization" );
510 
511  return findBasis(X, MX, *B, true );
512  }
513 
514 
515 
517  // Find an Op-orthonormal basis for span(X) - span(W)
518  template<class ScalarType, class MV, class OP>
520  MV &X,
524  Teuchos::RCP<MV> MX,
526  ) const
527  {
528  return projectAndNormalizeGen(X,Q,Q,true,C,B,MX,MQ,MQ);
529  }
530 
531 
532 
534  template<class ScalarType, class MV, class OP>
536  MV &S,
539  bool isBiortho,
541  Teuchos::RCP<MV> MS,
544  ) const
545  {
546  // For the inner product defined by the operator Op or the identity (Op == 0)
547  // -> Orthogonalize S against each Y[i], modifying it in the range of X[i]
548  // Modify MS accordingly
549  //
550  // Note that when Op is 0, MS is not referenced
551  //
552  // Parameter variables
553  //
554  // S : Multivector to be transformed
555  //
556  // MS : Image of the block vector S by the mass matrix
557  //
558  // X,Y: Bases to orthogonalize against/via.
559  //
560 
561 #ifdef ANASAZI_ICGS_DEBUG
562  // Get a FancyOStream from out_arg or create a new one ...
564  out = Teuchos::getFancyOStream(Teuchos::rcpFromRef(std::cout));
565  out->setShowAllFrontMatter(false).setShowProcRank(true);
566  *out << "Entering Anasazi::ICGSOrthoManager::projectGen(...)\n";
567 #endif
568 
569  const ScalarType ONE = SCT::one();
570  const MagnitudeType ZERO = SCT::magnitude(SCT::zero());
573 
574  int sc = MVT::GetNumberVecs( S );
575  ptrdiff_t sr = MVText::GetGlobalLength( S );
576  int numxy = X.length();
577  TEUCHOS_TEST_FOR_EXCEPTION(X.length() != Y.length(),std::invalid_argument,
578  "Anasazi::ICGSOrthoManager::projectGen(): X and Y must contain the same number of multivectors.");
579  std::vector<int> xyc(numxy);
580  // short-circuit
581  if (numxy == 0 || sc == 0 || sr == 0) {
582 #ifdef ANASAZI_ICGS_DEBUG
583  *out << "Leaving Anasazi::ICGSOrthoManager::projectGen(...)\n";
584 #endif
585  return;
586  }
587  // if we don't have enough C, expand it with null references
588  // if we have too many, resize to throw away the latter ones
589  // if we have exactly as many as we have X,Y this call has no effect
590  //
591  // same for MX, MY
592  C.resize(numxy);
593  MX.resize(numxy);
594  MY.resize(numxy);
595 
596  // check size of S w.r.t. common sense
597  TEUCHOS_TEST_FOR_EXCEPTION( sc<0 || sr<0, std::invalid_argument,
598  "Anasazi::ICGSOrthoManager::projectGen(): MVT returned negative dimensions for S." );
599 
600  // check size of MS
601  if (this->_hasOp == true) {
602  if (MS != Teuchos::null) {
603  TEUCHOS_TEST_FOR_EXCEPTION( MVText::GetGlobalLength(*MS) != sr, std::invalid_argument,
604  "Anasazi::ICGSOrthoManager::projectGen(): MS length not consistent with S." );
605  TEUCHOS_TEST_FOR_EXCEPTION( MVT::GetNumberVecs(*MS) != sc, std::invalid_argument,
606  "Anasazi::ICGSOrthoManager::projectGen(): MS width not consistent with S." );
607  }
608  }
609 
610  // tally up size of all X,Y and check/allocate C
611  ptrdiff_t sumxyc = 0;
612  int MYmissing = 0;
613  int MXmissing = 0;
614  for (int i=0; i<numxy; i++) {
615  if (X[i] != Teuchos::null && Y[i] != Teuchos::null) {
616  TEUCHOS_TEST_FOR_EXCEPTION( MVText::GetGlobalLength(*X[i]) != sr, std::invalid_argument,
617  "Anasazi::ICGSOrthoManager::projectGen(): X[" << i << "] length not consistent with S." );
618  TEUCHOS_TEST_FOR_EXCEPTION( MVText::GetGlobalLength(*Y[i]) != sr, std::invalid_argument,
619  "Anasazi::ICGSOrthoManager::projectGen(): Y[" << i << "] length not consistent with S." );
620  TEUCHOS_TEST_FOR_EXCEPTION( MVT::GetNumberVecs(*X[i]) != MVT::GetNumberVecs(*Y[i]), std::invalid_argument,
621  "Anasazi::ICGSOrthoManager::projectGen(): X[" << i << "] and Y[" << i << "] widths not consistent." );
622 
623  xyc[i] = MVT::GetNumberVecs( *X[i] );
624  TEUCHOS_TEST_FOR_EXCEPTION( sr < static_cast<ptrdiff_t>(xyc[i]), std::invalid_argument,
625  "Anasazi::ICGSOrthoManager::projectGen(): X[" << i << "],Y[" << i << "] have less rows than columns, and therefore cannot be full rank." );
626  sumxyc += xyc[i];
627 
628  // check size of C[i]
629  if ( C[i] == Teuchos::null ) {
631  }
632  else {
633  TEUCHOS_TEST_FOR_EXCEPTION( C[i]->numRows() != xyc[i] || C[i]->numCols() != sc , std::invalid_argument,
634  "Anasazi::ICGSOrthoManager::projectGen(): Size of Q not consistent with size of C." );
635  }
636  // check sizes of MX[i], MY[i] if present
637  // if not present, count their absence
638  if (MX[i] != Teuchos::null) {
639  TEUCHOS_TEST_FOR_EXCEPTION( MVText::GetGlobalLength(*MX[i]) != sr || MVT::GetNumberVecs(*MX[i]) != xyc[i], std::invalid_argument,
640  "Anasazi::ICGSOrthoManager::projectGen(): Size of MX[" << i << "] not consistent with size of X[" << i << "]." );
641  }
642  else {
643  MXmissing += xyc[i];
644  }
645  if (MY[i] != Teuchos::null) {
646  TEUCHOS_TEST_FOR_EXCEPTION( MVText::GetGlobalLength(*MY[i]) != sr || MVT::GetNumberVecs(*MY[i]) != xyc[i], std::invalid_argument,
647  "Anasazi::ICGSOrthoManager::projectGen(): Size of MY[" << i << "] not consistent with size of Y[" << i << "]." );
648  }
649  else {
650  MYmissing += xyc[i];
651  }
652  }
653  else {
654  // if one is null and the other is not... the user may have made a mistake
655  TEUCHOS_TEST_FOR_EXCEPTION(X[i] != Teuchos::null || Y[i] != Teuchos::null, std::invalid_argument,
656  "Anasazi::ICGSOrthoManager::projectGen(): "
657  << (X[i] == Teuchos::null ? "Y[" : "X[") << i << "] was provided but "
658  << (X[i] == Teuchos::null ? "X[" : "Y[") << i << "] was not.");
659  }
660  }
661 
662  // is this operation even feasible?
663  TEUCHOS_TEST_FOR_EXCEPTION(sumxyc > sr, std::invalid_argument,
664  "Anasazi::ICGSOrthoManager::projectGen(): dimension of all X[i],Y[i] is "
665  << sumxyc << ", but length of vectors is only " << sr << ". This is infeasible.");
666 
667 
668  /****** DO NO MODIFY *MS IF _hasOp == false
669  * if _hasOp == false, we don't need MS, MX or MY
670  *
671  * if _hasOp == true, we need MS (for S M-norms) and
672  * therefore, we must also update MS, regardless of whether
673  * it gets returned to the user (i.e., whether the user provided it)
674  * we may need to allocate and compute MX or MY
675  *
676  * let BXY denote:
677  * "X" - we have all M*X[i]
678  * "Y" - we have all M*Y[i]
679  * "B" - we have biorthogonality (for all X[i],Y[i])
680  * Encode these as values
681  * X = 1
682  * Y = 2
683  * B = 4
684  * We have 8 possibilities, 0-7
685  *
686  * We must allocate storage and computer the following, lest
687  * innerProdMat do it for us:
688  * none (0) - allocate MX, for inv(<X,Y>) and M*S
689  *
690  * for the following, we will compute M*S manually before returning
691  * B(4) BY(6) Y(2) --> updateMS = 1
692  * for the following, we will update M*S as we go, using M*X
693  * XY(3) X(1) none(0) BXY(7) BX(5) --> updateMS = 2
694  * these choices favor applications of M over allocation of memory
695  *
696  */
697  int updateMS = -1;
698  if (this->_hasOp) {
699  int whichAlloc = 0;
700  if (MXmissing == 0) {
701  whichAlloc += 1;
702  }
703  if (MYmissing == 0) {
704  whichAlloc += 2;
705  }
706  if (isBiortho) {
707  whichAlloc += 4;
708  }
709 
710  switch (whichAlloc) {
711  case 2:
712  case 4:
713  case 6:
714  updateMS = 1;
715  break;
716  case 0:
717  case 1:
718  case 3:
719  case 5:
720  case 7:
721  updateMS = 2;
722  break;
723  }
724 
725  // produce MS
726  if (MS == Teuchos::null) {
727 #ifdef ANASAZI_ICGS_DEBUG
728  *out << "Allocating MS...\n";
729 #endif
730  MS = MVT::Clone(S,MVT::GetNumberVecs(S));
731  OPT::Apply(*(this->_Op),S,*MS);
732  this->_OpCounter += MVT::GetNumberVecs(S);
733  }
734 
735  // allocate the rest
736  if (whichAlloc == 0) {
737  // allocate and compute missing MX
738  for (int i=0; i<numxy; i++) {
739  if (MX[i] == Teuchos::null) {
740 #ifdef ANASAZI_ICGS_DEBUG
741  *out << "Allocating MX[" << i << "]...\n";
742 #endif
743  Teuchos::RCP<MV> tmpMX = MVT::Clone(*X[i],xyc[i]);
744  OPT::Apply(*(this->_Op),*X[i],*tmpMX);
745  MX[i] = tmpMX;
746  this->_OpCounter += xyc[i];
747  }
748  }
749  }
750  }
751  else {
752  // Op == I --> MS == S
753  MS = Teuchos::rcpFromRef(S);
754  updateMS = 0;
755  }
756  TEUCHOS_TEST_FOR_EXCEPTION(updateMS == -1,std::logic_error,
757  "Anasazi::ICGSOrthoManager::projectGen(): Error in updateMS logic.");
758 
759 
761  // Perform the Gram-Schmidt transformation for a block of vectors
763 
764  // Compute Cholesky factorizations for the Y'*M*X
765  // YMX stores the YMX (initially) and their Cholesky factorizations (utlimately)
767  if (isBiortho == false) {
768  for (int i=0; i<numxy; i++) {
769 #ifdef ANASAZI_ICGS_DEBUG
770  *out << "Computing YMX[" << i << "] and its Cholesky factorization...\n";
771 #endif
772  YMX[i] = Teuchos::rcp( new Teuchos::SerialDenseMatrix<int,ScalarType>(xyc[i],xyc[i]) );
773  MatOrthoManager<ScalarType,MV,OP>::innerProdMat(*Y[i],*X[i],*YMX[i],MY[i],MX[i]);
774 #ifdef ANASAZI_ICGS_DEBUG
775  // YMX should be symmetric positive definite
776  // the cholesky will check the positive definiteness, but it looks only at the upper half
777  // we will check the symmetry by removing the upper half from the lower half, which should
778  // result in zeros
779  // also, diagonal of YMX should be real; consider the complex part as error
780  {
781  MagnitudeType err = ZERO;
782  for (int jj=0; jj<YMX[i]->numCols(); jj++) {
783  err =+ SCT::magnitude(SCT::imag((*YMX[i])(jj,jj)));
784  for (int ii=jj; ii<YMX[i]->numRows(); ii++) {
785  err += SCT::magnitude( (*YMX[i])(ii,jj) - SCT::conjugate((*YMX[i])(jj,ii)) );
786  }
787  }
788  *out << "Symmetry error in YMX[" << i << "] == " << err << "\n";
789  }
790 #endif
791  // take the LU factorization
792  int info;
793  lapack.POTRF('U',YMX[i]->numRows(),YMX[i]->values(),YMX[i]->stride(),&info);
794  TEUCHOS_TEST_FOR_EXCEPTION(info != 0,std::logic_error,
795  "Anasazi::ICGSOrthoManager::projectGen(): Error computing Cholesky factorization of Y[i]^T * M * X[i] using POTRF: returned info " << info);
796  }
797  }
798 
799  // Compute the initial Op-norms
800 #ifdef ANASAZI_ICGS_DEBUG
801  std::vector<MagnitudeType> oldNorms(sc);
803  *out << "oldNorms = { ";
804  std::copy(oldNorms.begin(), oldNorms.end(), std::ostream_iterator<MagnitudeType>(*out, " "));
805  *out << "}\n";
806 #endif
807 
808 
809  // clear the C[i] and allocate Ccur
811  for (int i=0; i<numxy; i++) {
812  C[i]->putScalar(ZERO);
813  Ccur[i].reshape(C[i]->numRows(),C[i]->numCols());
814  }
815 
816  for (int iter=0; iter < numIters_; iter++) {
817 #ifdef ANASAZI_ICGS_DEBUG
818  *out << "beginning iteration " << iter+1 << "\n";
819 #endif
820 
821  // Define the product Y[i]'*Op*S
822  for (int i=0; i<numxy; i++) {
823  // Compute Y[i]'*M*S
824  MatOrthoManager<ScalarType,MV,OP>::innerProdMat(*Y[i],S,Ccur[i],MY[i],MS);
825  if (isBiortho == false) {
826  // C[i] = inv(YMX[i])*(Y[i]'*M*S)
827  int info;
828  lapack.POTRS('U',YMX[i]->numCols(),Ccur[i].numCols(),
829  YMX[i]->values(),YMX[i]->stride(),
830  Ccur[i].values(),Ccur[i].stride(), &info);
831  TEUCHOS_TEST_FOR_EXCEPTION(info != 0, std::logic_error,
832  "Anasazi::ICGSOrthoManager::projectGen(): Error code " << info << " from lapack::POTRS." );
833  }
834 
835  // Multiply by X[i] and subtract the result in S
836 #ifdef ANASAZI_ICGS_DEBUG
837  *out << "Applying projector P_{X[" << i << "],Y[" << i << "]}...\n";
838 #endif
839  MVT::MvTimesMatAddMv( -ONE, *X[i], Ccur[i], ONE, S );
840 
841  // Accumulate coeffs into previous step
842  *C[i] += Ccur[i];
843 
844  // Update MS as required
845  if (updateMS == 1) {
846 #ifdef ANASAZI_ICGS_DEBUG
847  *out << "Updating MS...\n";
848 #endif
849  OPT::Apply( *(this->_Op), S, *MS);
850  this->_OpCounter += sc;
851  }
852  else if (updateMS == 2) {
853 #ifdef ANASAZI_ICGS_DEBUG
854  *out << "Updating MS...\n";
855 #endif
856  MVT::MvTimesMatAddMv( -ONE, *MX[i], Ccur[i], ONE, *MS );
857  }
858  }
859 
860  // Compute new Op-norms
861 #ifdef ANASAZI_ICGS_DEBUG
862  std::vector<MagnitudeType> newNorms(sc);
864  *out << "newNorms = { ";
865  std::copy(newNorms.begin(), newNorms.end(), std::ostream_iterator<MagnitudeType>(*out, " "));
866  *out << "}\n";
867 #endif
868  }
869 
870 #ifdef ANASAZI_ICGS_DEBUG
871  *out << "Leaving Anasazi::ICGSOrthoManager::projectGen(...)\n";
872 #endif
873  }
874 
875 
876 
878  // Find an Op-orthonormal basis for span(S) - span(Y)
879  template<class ScalarType, class MV, class OP>
881  MV &S,
884  bool isBiortho,
887  Teuchos::RCP<MV> MS,
890  ) const {
891  // For the inner product defined by the operator Op or the identity (Op == 0)
892  // -> Orthogonalize S against each Y[i], modifying it in the range of X[i]
893  // Modify MS accordingly
894  // Then construct a M-orthonormal basis for the remaining part
895  //
896  // Note that when Op is 0, MS is not referenced
897  //
898  // Parameter variables
899  //
900  // S : Multivector to be transformed
901  //
902  // MS : Image of the block vector S by the mass matrix
903  //
904  // X,Y: Bases to orthogonalize against/via.
905  //
906 
907 #ifdef ANASAZI_ICGS_DEBUG
908  // Get a FancyOStream from out_arg or create a new one ...
910  out = Teuchos::getFancyOStream(Teuchos::rcpFromRef(std::cout));
911  out->setShowAllFrontMatter(false).setShowProcRank(true);
912  *out << "Entering Anasazi::ICGSOrthoManager::projectAndNormalizeGen(...)\n";
913 #endif
914 
915  int rank;
916  int sc = MVT::GetNumberVecs( S );
917  ptrdiff_t sr = MVText::GetGlobalLength( S );
918  int numxy = X.length();
919  TEUCHOS_TEST_FOR_EXCEPTION(X.length() != Y.length(),std::invalid_argument,
920  "Anasazi::ICGSOrthoManager::projectAndNormalizeGen(): X and Y must contain the same number of multivectors.");
921  std::vector<int> xyc(numxy);
922  // short-circuit
923  if (sc == 0 || sr == 0) {
924 #ifdef ANASAZI_ICGS_DEBUG
925  *out << "Leaving Anasazi::ICGSOrthoManager::projectGen(...)\n";
926 #endif
927  return 0;
928  }
929  // if we don't have enough C, expand it with null references
930  // if we have too many, resize to throw away the latter ones
931  // if we have exactly as many as we have X,Y this call has no effect
932  //
933  // same for MX, MY
934  C.resize(numxy);
935  MX.resize(numxy);
936  MY.resize(numxy);
937 
938  // check size of S w.r.t. common sense
939  TEUCHOS_TEST_FOR_EXCEPTION( sc<0 || sr<0, std::invalid_argument,
940  "Anasazi::ICGSOrthoManager::projectAndNormalizeGen(): MVT returned negative dimensions for S." );
941 
942  // check size of MS
943  if (this->_hasOp == true) {
944  if (MS != Teuchos::null) {
945  TEUCHOS_TEST_FOR_EXCEPTION( MVText::GetGlobalLength(*MS) != sr, std::invalid_argument,
946  "Anasazi::ICGSOrthoManager::projectAndNormalizeGen(): MS length not consistent with S." );
947  TEUCHOS_TEST_FOR_EXCEPTION( MVT::GetNumberVecs(*MS) != sc, std::invalid_argument,
948  "Anasazi::ICGSOrthoManager::projectAndNormalizeGen(): MS width not consistent with S." );
949  }
950  }
951 
952  // tally up size of all X,Y and check/allocate C
953  ptrdiff_t sumxyc = 0;
954  int MYmissing = 0;
955  int MXmissing = 0;
956  for (int i=0; i<numxy; i++) {
957  if (X[i] != Teuchos::null && Y[i] != Teuchos::null) {
958  TEUCHOS_TEST_FOR_EXCEPTION( MVText::GetGlobalLength(*X[i]) != sr, std::invalid_argument,
959  "Anasazi::ICGSOrthoManager::projectAndNormalizeGen(): X[" << i << "] length not consistent with S." );
960  TEUCHOS_TEST_FOR_EXCEPTION( MVText::GetGlobalLength(*Y[i]) != sr, std::invalid_argument,
961  "Anasazi::ICGSOrthoManager::projectAndNormalizeGen(): Y[" << i << "] length not consistent with S." );
962  TEUCHOS_TEST_FOR_EXCEPTION( MVT::GetNumberVecs(*X[i]) != MVT::GetNumberVecs(*Y[i]), std::invalid_argument,
963  "Anasazi::ICGSOrthoManager::projectAndNormalizeGen(): X[" << i << "] and Y[" << i << "] widths not consistent." );
964 
965  xyc[i] = MVT::GetNumberVecs( *X[i] );
966  TEUCHOS_TEST_FOR_EXCEPTION( sr < static_cast<ptrdiff_t>(xyc[i]), std::invalid_argument,
967  "Anasazi::ICGSOrthoManager::projectAndNormalizeGen(): X[" << i << "],Y[" << i << "] have less rows than columns, and therefore cannot be full rank." );
968  sumxyc += xyc[i];
969 
970  // check size of C[i]
971  if ( C[i] == Teuchos::null ) {
973  }
974  else {
975  TEUCHOS_TEST_FOR_EXCEPTION( C[i]->numRows() != xyc[i] || C[i]->numCols() != sc , std::invalid_argument,
976  "Anasazi::ICGSOrthoManager::projectAndNormalizeGen(): Size of Q not consistent with size of C." );
977  }
978  // check sizes of MX[i], MY[i] if present
979  // if not present, count their absence
980  if (MX[i] != Teuchos::null) {
981  TEUCHOS_TEST_FOR_EXCEPTION( MVText::GetGlobalLength(*MX[i]) != sr || MVT::GetNumberVecs(*MX[i]) != xyc[i], std::invalid_argument,
982  "Anasazi::ICGSOrthoManager::projectAndNormalizeGen(): Size of MX[" << i << "] not consistent with size of X[" << i << "]." );
983  }
984  else {
985  MXmissing += xyc[i];
986  }
987  if (MY[i] != Teuchos::null) {
988  TEUCHOS_TEST_FOR_EXCEPTION( MVText::GetGlobalLength(*MY[i]) != sr || MVT::GetNumberVecs(*MY[i]) != xyc[i], std::invalid_argument,
989  "Anasazi::ICGSOrthoManager::projectAndNormalizeGen(): Size of MY[" << i << "] not consistent with size of Y[" << i << "]." );
990  }
991  else {
992  MYmissing += xyc[i];
993  }
994  }
995  else {
996  // if one is null and the other is not... the user may have made a mistake
997  TEUCHOS_TEST_FOR_EXCEPTION(X[i] != Teuchos::null || Y[i] != Teuchos::null, std::invalid_argument,
998  "Anasazi::ICGSOrthoManager::projectAndNormalizeGen(): "
999  << (X[i] == Teuchos::null ? "Y[" : "X[") << i << "] was provided but "
1000  << (X[i] == Teuchos::null ? "X[" : "Y[") << i << "] was not.");
1001  }
1002  }
1003 
1004  // is this operation even feasible?
1005  TEUCHOS_TEST_FOR_EXCEPTION(sumxyc + sc > sr, std::invalid_argument,
1006  "Anasazi::ICGSOrthoManager::projectAndNormalizeGen(): dimension of all X[i],Y[i] is "
1007  << sumxyc << " and requested " << sc << "-dimensional basis, but length of vectors is only "
1008  << sr << ". This is infeasible.");
1009 
1010 
1011  /****** DO NO MODIFY *MS IF _hasOp == false
1012  * if _hasOp == false, we don't need MS, MX or MY
1013  *
1014  * if _hasOp == true, we need MS (for S M-norms and normalization) and
1015  * therefore, we must also update MS, regardless of whether
1016  * it gets returned to the user (i.e., whether the user provided it)
1017  * we may need to allocate and compute MX or MY
1018  *
1019  * let BXY denote:
1020  * "X" - we have all M*X[i]
1021  * "Y" - we have all M*Y[i]
1022  * "B" - we have biorthogonality (for all X[i],Y[i])
1023  * Encode these as values
1024  * X = 1
1025  * Y = 2
1026  * B = 4
1027  * We have 8 possibilities, 0-7
1028  *
1029  * We must allocate storage and computer the following, lest
1030  * innerProdMat do it for us:
1031  * none (0) - allocate MX, for inv(<X,Y>) and M*S
1032  *
1033  * for the following, we will compute M*S manually before returning
1034  * B(4) BY(6) Y(2) --> updateMS = 1
1035  * for the following, we will update M*S as we go, using M*X
1036  * XY(3) X(1) none(0) BXY(7) BX(5) --> updateMS = 2
1037  * these choices favor applications of M over allocation of memory
1038  *
1039  */
1040  int updateMS = -1;
1041  if (this->_hasOp) {
1042  int whichAlloc = 0;
1043  if (MXmissing == 0) {
1044  whichAlloc += 1;
1045  }
1046  if (MYmissing == 0) {
1047  whichAlloc += 2;
1048  }
1049  if (isBiortho) {
1050  whichAlloc += 4;
1051  }
1052 
1053  switch (whichAlloc) {
1054  case 2:
1055  case 4:
1056  case 6:
1057  updateMS = 1;
1058  break;
1059  case 0:
1060  case 1:
1061  case 3:
1062  case 5:
1063  case 7:
1064  updateMS = 2;
1065  break;
1066  }
1067 
1068  // produce MS
1069  if (MS == Teuchos::null) {
1070 #ifdef ANASAZI_ICGS_DEBUG
1071  *out << "Allocating MS...\n";
1072 #endif
1073  MS = MVT::Clone(S,MVT::GetNumberVecs(S));
1074  OPT::Apply(*(this->_Op),S,*MS);
1075  this->_OpCounter += MVT::GetNumberVecs(S);
1076  }
1077 
1078  // allocate the rest
1079  if (whichAlloc == 0) {
1080  // allocate and compute missing MX
1081  for (int i=0; i<numxy; i++) {
1082  if (MX[i] == Teuchos::null) {
1083 #ifdef ANASAZI_ICGS_DEBUG
1084  *out << "Allocating MX[" << i << "]...\n";
1085 #endif
1086  Teuchos::RCP<MV> tmpMX = MVT::Clone(*X[i],xyc[i]);
1087  OPT::Apply(*(this->_Op),*X[i],*tmpMX);
1088  MX[i] = tmpMX;
1089  this->_OpCounter += xyc[i];
1090  }
1091  }
1092  }
1093  }
1094  else {
1095  // Op == I --> MS == S
1096  MS = Teuchos::rcpFromRef(S);
1097  updateMS = 0;
1098  }
1099  TEUCHOS_TEST_FOR_EXCEPTION(updateMS == -1,std::logic_error,
1100  "Anasazi::ICGSOrthoManager::projectGen(): Error in updateMS logic.");
1101 
1102 
1103  // if the user doesn't want to store the coefficients,
1104  // allocate some local memory for them
1105  if ( B == Teuchos::null ) {
1107  }
1108  else {
1109  // check size of B
1110  TEUCHOS_TEST_FOR_EXCEPTION( B->numRows() != sc || B->numCols() != sc, std::invalid_argument,
1111  "Anasazi::ICGSOrthoManager::projectAndNormalizeGen(): Size of S must be consistent with size of B" );
1112  }
1113 
1114 
1115  // orthogonalize all of S against X,Y
1116  projectGen(S,X,Y,isBiortho,C,MS,MX,MY);
1117 
1119  // start working
1120  rank = 0;
1121  int numTries = 10; // each vector in X gets 10 random chances to escape degeneracy
1122  int oldrank = -1;
1123  do {
1124  int curssize = sc - rank;
1125 
1126  // orthonormalize S, but quit if it is rank deficient
1127  // we can't let findBasis generated random vectors to complete the basis,
1128  // because it doesn't know about Q; we will do this ourselves below
1129 #ifdef ANASAZI_ICGS_DEBUG
1130  *out << "Attempting to find orthonormal basis for X...\n";
1131 #endif
1132  rank = findBasis(S,MS,*B,false,curssize);
1133 
1134  if (oldrank != -1 && rank != oldrank) {
1135  // we had previously stopped before, after operating on vector oldrank
1136  // we saved its coefficients, augmented it with a random vector, and
1137  // then called findBasis() again, which proceeded to add vector oldrank
1138  // to the basis.
1139  // now, restore the saved coefficients into B
1140  for (int i=0; i<sc; i++) {
1141  (*B)(i,oldrank) = oldCoeff(i,0);
1142  }
1143  }
1144 
1145  if (rank < sc) {
1146  if (rank != oldrank) {
1147  // we quit on this vector and will augment it with random below
1148  // this is the first time that we have quit on this vector
1149  // therefor, (*B)(:,rank) contains the actual coefficients of the
1150  // input vectors with respect to the previous vectors in the basis
1151  // save these values, as (*B)(:,rank) will be overwritten by our next
1152  // call to findBasis()
1153  // we will restore it after we are done working on this vector
1154  for (int i=0; i<sc; i++) {
1155  oldCoeff(i,0) = (*B)(i,rank);
1156  }
1157  }
1158  }
1159 
1160  if (rank == sc) {
1161  // we are done
1162 #ifdef ANASAZI_ICGS_DEBUG
1163  *out << "Finished computing basis.\n";
1164 #endif
1165  break;
1166  }
1167  else {
1168  TEUCHOS_TEST_FOR_EXCEPTION( rank < oldrank, OrthoError,
1169  "Anasazi::ICGSOrthoManager::projectAndNormalizeGen(): basis lost rank; this shouldn't happen");
1170 
1171  if (rank != oldrank) {
1172  // we added a basis vector from random info; reset the chance counter
1173  numTries = 10;
1174  // store old rank
1175  oldrank = rank;
1176  }
1177  else {
1178  // has this vector run out of chances to escape degeneracy?
1179  if (numTries <= 0) {
1180  break;
1181  }
1182  }
1183  // use one of this vector's chances
1184  numTries--;
1185 
1186  // randomize troubled direction
1187 #ifdef ANASAZI_ICGS_DEBUG
1188  *out << "Inserting random vector in X[" << rank << "]. Attempt " << 10-numTries << ".\n";
1189 #endif
1190  Teuchos::RCP<MV> curS, curMS;
1191  std::vector<int> ind(1);
1192  ind[0] = rank;
1193  curS = MVT::CloneViewNonConst(S,ind);
1194  MVT::MvRandom(*curS);
1195  if (this->_hasOp) {
1196 #ifdef ANASAZI_ICGS_DEBUG
1197  *out << "Applying operator to random vector.\n";
1198 #endif
1199  curMS = MVT::CloneViewNonConst(*MS,ind);
1200  OPT::Apply( *(this->_Op), *curS, *curMS );
1201  this->_OpCounter += MVT::GetNumberVecs(*curS);
1202  }
1203 
1204  // orthogonalize against X,Y
1205  // if !this->_hasOp, the curMS will be ignored.
1206  // we don't care about these coefficients
1207  // on the contrary, we need to preserve the previous coeffs
1208  {
1210  projectGen(*curS,X,Y,isBiortho,dummyC,curMS,MX,MY);
1211  }
1212  }
1213  } while (1);
1214 
1215  // this should never raise an exception; but our post-conditions oblige us to check
1216  TEUCHOS_TEST_FOR_EXCEPTION( rank > sc || rank < 0, std::logic_error,
1217  "Anasazi::ICGSOrthoManager::projectAndNormalizeGen(): Debug error in rank variable." );
1218 
1219 #ifdef ANASAZI_ICGS_DEBUG
1220  *out << "Returning " << rank << " from Anasazi::ICGSOrthoManager::projectAndNormalizeGen(...)\n";
1221 #endif
1222 
1223  return rank;
1224  }
1225 
1226 
1227 
1229  // Find an Op-orthonormal basis for span(X), with the option of extending the subspace so that
1230  // the rank is numvectors(X)
1231  template<class ScalarType, class MV, class OP>
1233  MV &X, Teuchos::RCP<MV> MX,
1235  bool completeBasis, int howMany ) const {
1236 
1237  // For the inner product defined by the operator Op or the identity (Op == 0)
1238  // -> Orthonormalize X
1239  // Modify MX accordingly
1240  //
1241  // Note that when Op is 0, MX is not referenced
1242  //
1243  // Parameter variables
1244  //
1245  // X : Vectors to be orthonormalized
1246  //
1247  // MX : Image of the multivector X under the operator Op
1248  //
1249  // Op : Pointer to the operator for the inner product
1250  //
1251 
1252 #ifdef ANASAZI_ICGS_DEBUG
1253  // Get a FancyOStream from out_arg or create a new one ...
1255  out = Teuchos::getFancyOStream(Teuchos::rcpFromRef(std::cout));
1256  out->setShowAllFrontMatter(false).setShowProcRank(true);
1257  *out << "Entering Anasazi::ICGSOrthoManager::findBasis(...)\n";
1258 #endif
1259 
1260  const ScalarType ONE = SCT::one();
1261  const MagnitudeType ZERO = SCT::magnitude(SCT::zero());
1262 
1263  int xc = MVT::GetNumberVecs( X );
1264 
1265  if (howMany == -1) {
1266  howMany = xc;
1267  }
1268 
1269  /*******************************************************
1270  * If _hasOp == false, we will not reference MX below *
1271  *******************************************************/
1272  TEUCHOS_TEST_FOR_EXCEPTION(this->_hasOp == true && MX == Teuchos::null, std::logic_error,
1273  "Anasazi::ICGSOrthoManager::findBasis(): calling routine did not specify MS.");
1274  TEUCHOS_TEST_FOR_EXCEPTION( howMany < 0 || howMany > xc, std::logic_error,
1275  "Anasazi::ICGSOrthoManager::findBasis(): Invalid howMany parameter" );
1276 
1277  /* xstart is which column we are starting the process with, based on howMany
1278  * columns before xstart are assumed to be Op-orthonormal already
1279  */
1280  int xstart = xc - howMany;
1281 
1282  for (int j = xstart; j < xc; j++) {
1283 
1284  // numX represents the number of currently orthonormal columns of X
1285  int numX = j;
1286  // j represents the index of the current column of X
1287  // these are different interpretations of the same value
1288 
1289  //
1290  // set the lower triangular part of B to zero
1291  for (int i=j+1; i<xc; ++i) {
1292  B(i,j) = ZERO;
1293  }
1294 
1295  // Get a view of the vector currently being worked on.
1296  std::vector<int> index(1);
1297  index[0] = j;
1298  Teuchos::RCP<MV> Xj = MVT::CloneViewNonConst( X, index );
1299  Teuchos::RCP<MV> MXj;
1300  if ((this->_hasOp)) {
1301  // MXj is a view of the current vector in MX
1302  MXj = MVT::CloneViewNonConst( *MX, index );
1303  }
1304  else {
1305  // MXj is a pointer to Xj, and MUST NOT be modified
1306  MXj = Xj;
1307  }
1308 
1309  // Get a view of the previous vectors.
1310  std::vector<int> prev_idx( numX );
1311  Teuchos::RCP<const MV> prevX, prevMX;
1312 
1313  if (numX > 0) {
1314  for (int i=0; i<numX; ++i) prev_idx[i] = i;
1315  prevX = MVT::CloneView( X, prev_idx );
1316  if (this->_hasOp) {
1317  prevMX = MVT::CloneView( *MX, prev_idx );
1318  }
1319  }
1320 
1321  bool rankDef = true;
1322  /* numTrials>0 will denote that the current vector was randomized for the purpose
1323  * of finding a basis vector, and that the coefficients of that vector should
1324  * not be stored in B
1325  */
1326  for (int numTrials = 0; numTrials < 10; numTrials++) {
1327 #ifdef ANASAZI_ICGS_DEBUG
1328  *out << "Trial " << numTrials << " for vector " << j << "\n";
1329 #endif
1330 
1331  // Make storage for these Gram-Schmidt iterations.
1333  std::vector<MagnitudeType> origNorm(1), newNorm(1), newNorm2(1);
1334 
1335  //
1336  // Save old MXj vector and compute Op-norm
1337  //
1338  Teuchos::RCP<MV> oldMXj = MVT::CloneCopy( *MXj );
1340 #ifdef ANASAZI_ICGS_DEBUG
1341  *out << "origNorm = " << origNorm[0] << "\n";
1342 #endif
1343 
1344  if (numX > 0) {
1345  // Apply the first step of Gram-Schmidt
1346 
1347  // product <- prevX^T MXj
1348  MatOrthoManager<ScalarType,MV,OP>::innerProdMat(*prevX,*Xj,product,Teuchos::null,MXj);
1349 
1350  // Xj <- Xj - prevX prevX^T MXj
1351  // = Xj - prevX product
1352 #ifdef ANASAZI_ICGS_DEBUG
1353  *out << "Orthogonalizing X[" << j << "]...\n";
1354 #endif
1355  MVT::MvTimesMatAddMv( -ONE, *prevX, product, ONE, *Xj );
1356 
1357  // Update MXj
1358  if (this->_hasOp) {
1359  // MXj <- Op*Xj_new
1360  // = Op*(Xj_old - prevX prevX^T MXj)
1361  // = MXj - prevMX product
1362 #ifdef ANASAZI_ICGS_DEBUG
1363  *out << "Updating MX[" << j << "]...\n";
1364 #endif
1365  MVT::MvTimesMatAddMv( -ONE, *prevMX, product, ONE, *MXj );
1366  }
1367 
1368  // Compute new Op-norm
1370  MagnitudeType product_norm = product.normOne();
1371 
1372 #ifdef ANASAZI_ICGS_DEBUG
1373  *out << "newNorm = " << newNorm[0] << "\n";
1374  *out << "prodoct_norm = " << product_norm << "\n";
1375 #endif
1376 
1377  // Check if a correction is needed.
1378  if ( product_norm/newNorm[0] >= tol_ || newNorm[0] < eps_*origNorm[0]) {
1379 #ifdef ANASAZI_ICGS_DEBUG
1380  if (product_norm/newNorm[0] >= tol_) {
1381  *out << "product_norm/newNorm == " << product_norm/newNorm[0] << "... another step of Gram-Schmidt.\n";
1382  }
1383  else {
1384  *out << "eps*origNorm == " << eps_*origNorm[0] << "... another step of Gram-Schmidt.\n";
1385  }
1386 #endif
1387  // Apply the second step of Gram-Schmidt
1388  // This is the same as above
1390  MatOrthoManager<ScalarType,MV,OP>::innerProdMat(*prevX,*Xj,P2,Teuchos::null,MXj);
1391  product += P2;
1392 #ifdef ANASAZI_ICGS_DEBUG
1393  *out << "Orthogonalizing X[" << j << "]...\n";
1394 #endif
1395  MVT::MvTimesMatAddMv( -ONE, *prevX, P2, ONE, *Xj );
1396  if ((this->_hasOp)) {
1397 #ifdef ANASAZI_ICGS_DEBUG
1398  *out << "Updating MX[" << j << "]...\n";
1399 #endif
1400  MVT::MvTimesMatAddMv( -ONE, *prevMX, P2, ONE, *MXj );
1401  }
1402  // Compute new Op-norms
1404  product_norm = P2.normOne();
1405 #ifdef ANASAZI_ICGS_DEBUG
1406  *out << "newNorm2 = " << newNorm2[0] << "\n";
1407  *out << "product_norm = " << product_norm << "\n";
1408 #endif
1409  if ( product_norm/newNorm2[0] >= tol_ || newNorm2[0] < eps_*origNorm[0] ) {
1410  // we don't do another GS, we just set it to zero.
1411 #ifdef ANASAZI_ICGS_DEBUG
1412  if (product_norm/newNorm2[0] >= tol_) {
1413  *out << "product_norm/newNorm2 == " << product_norm/newNorm2[0] << "... setting vector to zero.\n";
1414  }
1415  else if (newNorm[0] < newNorm2[0]) {
1416  *out << "newNorm2 > newNorm... setting vector to zero.\n";
1417  }
1418  else {
1419  *out << "eps*origNorm == " << eps_*origNorm[0] << "... setting vector to zero.\n";
1420  }
1421 #endif
1422  MVT::MvInit(*Xj,ZERO);
1423  if ((this->_hasOp)) {
1424 #ifdef ANASAZI_ICGS_DEBUG
1425  *out << "Setting MX[" << j << "] to zero as well.\n";
1426 #endif
1427  MVT::MvInit(*MXj,ZERO);
1428  }
1429  }
1430  }
1431  } // if (numX > 0) do GS
1432 
1433  // save the coefficients, if we are working on the original vector and not a randomly generated one
1434  if (numTrials == 0) {
1435  for (int i=0; i<numX; i++) {
1436  B(i,j) = product(i,0);
1437  }
1438  }
1439 
1440  // Check if Xj has any directional information left after the orthogonalization.
1442  if ( newNorm[0] != ZERO && newNorm[0] > SCT::sfmin() ) {
1443 #ifdef ANASAZI_ICGS_DEBUG
1444  *out << "Normalizing X[" << j << "], norm(X[" << j << "]) = " << newNorm[0] << "\n";
1445 #endif
1446  // Normalize Xj.
1447  // Xj <- Xj / norm
1448  MVT::MvScale( *Xj, ONE/newNorm[0]);
1449  if (this->_hasOp) {
1450 #ifdef ANASAZI_ICGS_DEBUG
1451  *out << "Normalizing M*X[" << j << "]...\n";
1452 #endif
1453  // Update MXj.
1454  MVT::MvScale( *MXj, ONE/newNorm[0]);
1455  }
1456 
1457  // save it, if it corresponds to the original vector and not a randomly generated one
1458  if (numTrials == 0) {
1459  B(j,j) = newNorm[0];
1460  }
1461 
1462  // We are not rank deficient in this vector. Move on to the next vector in X.
1463  rankDef = false;
1464  break;
1465  }
1466  else {
1467 #ifdef ANASAZI_ICGS_DEBUG
1468  *out << "Not normalizing M*X[" << j << "]...\n";
1469 #endif
1470  // There was nothing left in Xj after orthogonalizing against previous columns in X.
1471  // X is rank deficient.
1472  // reflect this in the coefficients
1473  B(j,j) = ZERO;
1474 
1475  if (completeBasis) {
1476  // Fill it with random information and keep going.
1477 #ifdef ANASAZI_ICGS_DEBUG
1478  *out << "Inserting random vector in X[" << j << "]...\n";
1479 #endif
1480  MVT::MvRandom( *Xj );
1481  if (this->_hasOp) {
1482 #ifdef ANASAZI_ICGS_DEBUG
1483  *out << "Updating M*X[" << j << "]...\n";
1484 #endif
1485  OPT::Apply( *(this->_Op), *Xj, *MXj );
1486  this->_OpCounter += MVT::GetNumberVecs(*Xj);
1487  }
1488  }
1489  else {
1490  rankDef = true;
1491  break;
1492  }
1493  }
1494  } // for (numTrials = 0; numTrials < 10; ++numTrials)
1495 
1496  // if rankDef == true, then quit and notify user of rank obtained
1497  if (rankDef == true) {
1498  TEUCHOS_TEST_FOR_EXCEPTION( rankDef && completeBasis, OrthoError,
1499  "Anasazi::ICGSOrthoManager::findBasis(): Unable to complete basis" );
1500 #ifdef ANASAZI_ICGS_DEBUG
1501  *out << "Returning early, rank " << j << " from Anasazi::ICGSOrthoManager::findBasis(...)\n";
1502 #endif
1503  return j;
1504  }
1505 
1506  } // for (j = 0; j < xc; ++j)
1507 
1508 #ifdef ANASAZI_ICGS_DEBUG
1509  *out << "Returning " << xc << " from Anasazi::ICGSOrthoManager::findBasis(...)\n";
1510 #endif
1511  return xc;
1512  }
1513 
1514 } // namespace Anasazi
1515 
1516 #endif // ANASAZI_ICSG_ORTHOMANAGER_HPP
1517 
An extension of the MultiVecTraits class that adds a new vector length method.This traits class provi...
Declaration of basic traits for the multivector type.
basic_FancyOStream & setShowProcRank(const bool showProcRank)
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...
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
Virtual base class which defines basic traits for the operator type.
void projectGen(MV &S, Teuchos::Array< Teuchos::RCP< const MV > > X, Teuchos::Array< Teuchos::RCP< const MV > > Y, bool isBiOrtho, Teuchos::Array< Teuchos::RCP< Teuchos::SerialDenseMatrix< int, ScalarType > > > C=Teuchos::tuple(Teuchos::RCP< Teuchos::SerialDenseMatrix< int, ScalarType > >(Teuchos::null)), Teuchos::RCP< MV > MS=Teuchos::null, Teuchos::Array< Teuchos::RCP< const MV > > MX=Teuchos::tuple(Teuchos::RCP< const MV >(Teuchos::null)), Teuchos::Array< Teuchos::RCP< const MV > > MY=Teuchos::tuple(Teuchos::RCP< const MV >(Teuchos::null))) const
Applies a series of generic projectors.
An implementation of the Anasazi::GenOrthoManager that performs orthogonalization using iterated clas...
ICGSOrthoManager(Teuchos::RCP< const OP > Op=Teuchos::null, int numIters=2, typename Teuchos::ScalarTraits< ScalarType >::magnitudeType eps=0.0, typename Teuchos::ScalarTraits< ScalarType >::magnitudeType tol=0.20)
Constructor specifying the operator defining the inner product as well as the number of orthogonaliza...
Templated virtual class for providing orthogonalization/orthonormalization methods with matrix-based ...
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 T pow(T x, T y)
ScalarTraits< ScalarType >::magnitudeType normFrobenius() const
void POTRF(const char UPLO, const OrdinalType n, ScalarType *A, const OrdinalType lda, OrdinalType *info) const
void POTRS(const char UPLO, const OrdinalType n, const OrdinalType nrhs, const ScalarType *A, const OrdinalType lda, ScalarType *B, const OrdinalType ldb, OrdinalType *info) const
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
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 ...
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...
Exception thrown to signal error in an orthogonalization manager method.
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 ...
static magnitudeType magnitude(ScalarTypea)
int projectAndNormalizeGen(MV &S, Teuchos::Array< Teuchos::RCP< const MV > > X, Teuchos::Array< Teuchos::RCP< const MV > > Y, bool isBiOrtho, 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 > MS=Teuchos::null, Teuchos::Array< Teuchos::RCP< const MV > > MX=Teuchos::tuple(Teuchos::RCP< const MV >(Teuchos::null)), Teuchos::Array< Teuchos::RCP< const MV > > MY=Teuchos::tuple(Teuchos::RCP< const MV >(Teuchos::null))) const
Applies a series of generic projectors and returns an orthonormal basis for the residual data...
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 ...
int getNumIters() const
Return parameter for re-orthogonalization threshold.
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.
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...
void setNumIters(int numIters)
Set parameter for re-orthogonalization threshold.