Anasazi  Version of the Day
AnasaziBasicOrthoManager.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_BASIC_ORTHOMANAGER_HPP
35 #define ANASAZI_BASIC_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_BASIC_ORTHO_DEBUG
52 # include <Teuchos_FancyOStream.hpp>
53 #endif
54 
55 namespace Anasazi {
56 
57  template<class ScalarType, class MV, class OP>
58  class BasicOrthoManager : public MatOrthoManager<ScalarType,MV,OP> {
59 
60  private:
61  typedef typename Teuchos::ScalarTraits<ScalarType>::magnitudeType MagnitudeType;
66 
67  public:
68 
70 
71  BasicOrthoManager( Teuchos::RCP<const OP> Op = Teuchos::null,
73  typename Teuchos::ScalarTraits<ScalarType>::magnitudeType kappa = 1.41421356 /* sqrt(2) */,
76 
77 
81 
82 
84 
85 
86 
125  void projectMat (
126  MV &X,
129  = Teuchos::tuple(Teuchos::RCP< Teuchos::SerialDenseMatrix<int,ScalarType> >(Teuchos::null)),
130  Teuchos::RCP<MV> MX = Teuchos::null,
131  Teuchos::Array<Teuchos::RCP<const MV> > MQ = Teuchos::tuple(Teuchos::RCP<const MV>(Teuchos::null))
132  ) const;
133 
134 
173  int normalizeMat (
174  MV &X,
176  Teuchos::RCP<MV> MX = Teuchos::null
177  ) const;
178 
179 
240  MV &X,
243  = Teuchos::tuple(Teuchos::RCP< Teuchos::SerialDenseMatrix<int,ScalarType> >(Teuchos::null)),
245  Teuchos::RCP<MV> MX = Teuchos::null,
246  Teuchos::Array<Teuchos::RCP<const MV> > MQ = Teuchos::tuple(Teuchos::RCP<const MV>(Teuchos::null))
247  ) const;
248 
250 
252 
253 
259  orthonormErrorMat(const MV &X, Teuchos::RCP<const MV> MX = Teuchos::null) const;
260 
266  orthogErrorMat(const MV &X1, const MV &X2, Teuchos::RCP<const MV> MX1, Teuchos::RCP<const MV> MX2) const;
267 
269 
271 
272 
274  void setKappa( typename Teuchos::ScalarTraits<ScalarType>::magnitudeType kappa ) { kappa_ = kappa; }
275 
277  typename Teuchos::ScalarTraits<ScalarType>::magnitudeType getKappa() const { return kappa_; }
278 
280 
281  private:
283  MagnitudeType kappa_;
284  MagnitudeType eps_;
285  MagnitudeType tol_;
286 
287  // ! Routine to find an orthonormal basis
288  int findBasis(MV &X, Teuchos::RCP<MV> MX,
290  bool completeBasis, int howMany = -1 ) const;
291 
292  //
293  // Internal timers
294  //
295  Teuchos::RCP<Teuchos::Time> timerReortho_;
296 
297  };
298 
299 
301  // Constructor
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"))
310 #endif
311  {
312  TEUCHOS_TEST_FOR_EXCEPTION(eps_ < SCT::magnitude(SCT::zero()),std::invalid_argument,
313  "Anasazi::BasicOrthoManager::BasicOrthoManager(): argument \"eps\" must be non-negative.");
314  if (eps_ == 0) {
316  eps_ = lapack.LAMCH('E');
318  }
320  tol_ < SCT::magnitude(SCT::zero()) || tol_ > SCT::magnitude(SCT::one()),
321  std::invalid_argument,
322  "Anasazi::BasicOrthoManager::BasicOrthoManager(): argument \"tol\" must be in [0,1].");
323  }
324 
325 
326 
328  // Compute the distance from orthonormality
329  template<class ScalarType, class MV, class OP>
332  const ScalarType ONE = SCT::one();
333  int rank = MVT::GetNumberVecs(X);
336  for (int i=0; i<rank; i++) {
337  xTx(i,i) -= ONE;
338  }
339  return xTx.normFrobenius();
340  }
341 
342 
343 
345  // Compute the distance from orthogonality
346  template<class ScalarType, class MV, class OP>
349  int r1 = MVT::GetNumberVecs(X1);
350  int r2 = MVT::GetNumberVecs(X2);
353  return xTx.normFrobenius();
354  }
355 
356 
357 
359  template<class ScalarType, class MV, class OP>
361  MV &X,
364  Teuchos::RCP<MV> MX,
366  ) const {
367  // For the inner product defined by the operator Op or the identity (Op == 0)
368  // -> Orthogonalize X against each Q[i]
369  // Modify MX accordingly
370  //
371  // Note that when Op is 0, MX is not referenced
372  //
373  // Parameter variables
374  //
375  // X : Vectors to be transformed
376  //
377  // MX : Image of the block vector X by the mass matrix
378  //
379  // Q : Bases to orthogonalize against. These are assumed orthonormal, mutually and independently.
380  //
381 
382 #ifdef ANASAZI_BASIC_ORTHO_DEBUG
383  // Get a FancyOStream from out_arg or create a new one ...
385  out = Teuchos::getFancyOStream(Teuchos::rcpFromRef(std::cout));
386  out->setShowAllFrontMatter(false).setShowProcRank(true);
387  *out << "Entering Anasazi::BasicOrthoManager::projectMat(...)\n";
388 #endif
389 
390  ScalarType ONE = SCT::one();
391 
392  int xc = MVT::GetNumberVecs( X );
393  ptrdiff_t xr = MVText::GetGlobalLength( X );
394  int nq = Q.length();
395  std::vector<int> qcs(nq);
396  // short-circuit
397  if (nq == 0 || xc == 0 || xr == 0) {
398 #ifdef ANASAZI_BASIC_ORTHO_DEBUG
399  *out << "Leaving Anasazi::BasicOrthoManager::projectMat(...)\n";
400 #endif
401  return;
402  }
403  ptrdiff_t qr = MVText::GetGlobalLength ( *Q[0] );
404  // if we don't have enough C, expand it with null references
405  // if we have too many, resize to throw away the latter ones
406  // if we have exactly as many as we have Q, this call has no effect
407  C.resize(nq);
408 
409 
410  /****** DO NO MODIFY *MX IF _hasOp == false ******/
411  if (this->_hasOp) {
412 #ifdef ANASAZI_BASIC_ORTHO_DEBUG
413  *out << "Allocating MX...\n";
414 #endif
415  if (MX == Teuchos::null) {
416  // we need to allocate space for MX
417  MX = MVT::Clone(X,MVT::GetNumberVecs(X));
418  OPT::Apply(*(this->_Op),X,*MX);
419  this->_OpCounter += MVT::GetNumberVecs(X);
420  }
421  }
422  else {
423  // Op == I --> MX = X (ignore it if the user passed it in)
424  MX = Teuchos::rcpFromRef(X);
425  }
426  int mxc = MVT::GetNumberVecs( *MX );
427  ptrdiff_t mxr = MVText::GetGlobalLength( *MX );
428 
429  // check size of X and Q w.r.t. common sense
430  TEUCHOS_TEST_FOR_EXCEPTION( xc<0 || xr<0 || mxc<0 || mxr<0, std::invalid_argument,
431  "Anasazi::BasicOrthoManager::projectMat(): MVT returned negative dimensions for X,MX" );
432  // check size of X w.r.t. MX and Q
433  TEUCHOS_TEST_FOR_EXCEPTION( xc!=mxc || xr!=mxr || xr!=qr, std::invalid_argument,
434  "Anasazi::BasicOrthoManager::projectMat(): Size of X not consistent with MX,Q" );
435 
436  // tally up size of all Q and check/allocate C
437  int baslen = 0;
438  for (int i=0; i<nq; i++) {
439  TEUCHOS_TEST_FOR_EXCEPTION( MVText::GetGlobalLength( *Q[i] ) != qr, std::invalid_argument,
440  "Anasazi::BasicOrthoManager::projectMat(): Q lengths not mutually consistent" );
441  qcs[i] = MVT::GetNumberVecs( *Q[i] );
442  TEUCHOS_TEST_FOR_EXCEPTION( qr < static_cast<ptrdiff_t>(qcs[i]), std::invalid_argument,
443  "Anasazi::BasicOrthoManager::projectMat(): Q has less rows than columns" );
444  baslen += qcs[i];
445 
446  // check size of C[i]
447  if ( C[i] == Teuchos::null ) {
449  }
450  else {
451  TEUCHOS_TEST_FOR_EXCEPTION( C[i]->numRows() != qcs[i] || C[i]->numCols() != xc , std::invalid_argument,
452  "Anasazi::BasicOrthoManager::projectMat(): Size of Q not consistent with size of C" );
453  }
454  }
455 
456  // Perform the Gram-Schmidt transformation for a block of vectors
457 
458  // Compute the initial Op-norms
459  std::vector<ScalarType> oldDot( xc );
460  MVT::MvDot( X, *MX, oldDot );
461 #ifdef ANASAZI_BASIC_ORTHO_DEBUG
462  *out << "oldDot = { ";
463  std::copy(oldDot.begin(), oldDot.end(), std::ostream_iterator<ScalarType>(*out, " "));
464  *out << "}\n";
465 #endif
466 
467  MQ.resize(nq);
468  // Define the product Q^T * (Op*X)
469  for (int i=0; i<nq; i++) {
470  // Multiply Q' with MX
471  MatOrthoManager<ScalarType,MV,OP>::innerProdMat(*Q[i],X,*C[i],MQ[i],MX);
472  // Multiply by Q and subtract the result in X
473 #ifdef ANASAZI_BASIC_ORTHO_DEBUG
474  *out << "Applying projector P_Q[" << i << "]...\n";
475 #endif
476  MVT::MvTimesMatAddMv( -ONE, *Q[i], *C[i], ONE, X );
477 
478  // Update MX, with the least number of applications of Op as possible
479  // Update MX. If we have MQ, use it. Otherwise, just multiply by Op
480  if (this->_hasOp) {
481  if (MQ[i] == Teuchos::null) {
482 #ifdef ANASAZI_BASIC_ORTHO_DEBUG
483  *out << "Updating MX via M*X...\n";
484 #endif
485  OPT::Apply( *(this->_Op), X, *MX);
486  this->_OpCounter += MVT::GetNumberVecs(X);
487  }
488  else {
489 #ifdef ANASAZI_BASIC_ORTHO_DEBUG
490  *out << "Updating MX via M*Q...\n";
491 #endif
492  MVT::MvTimesMatAddMv( -ONE, *MQ[i], *C[i], ONE, *MX );
493  }
494  }
495  }
496 
497  // Compute new Op-norms
498  std::vector<ScalarType> newDot(xc);
499  MVT::MvDot( X, *MX, newDot );
500 #ifdef ANASAZI_BASIC_ORTHO_DEBUG
501  *out << "newDot = { ";
502  std::copy(newDot.begin(), newDot.end(), std::ostream_iterator<ScalarType>(*out, " "));
503  *out << "}\n";
504 #endif
505 
506  // determine (individually) whether to do another step of classical Gram-Schmidt
507  for (int j = 0; j < xc; ++j) {
508 
509  if ( SCT::magnitude(kappa_*newDot[j]) < SCT::magnitude(oldDot[j]) ) {
510 #ifdef ANASAZI_BASIC_ORTHO_DEBUG
511  *out << "kappa_*newDot[" <<j<< "] == " << kappa_*newDot[j] << "... another step of Gram-Schmidt.\n";
512 #endif
513 #ifdef ANASAZI_TEUCHOS_TIME_MONITOR
514  Teuchos::TimeMonitor lcltimer( *timerReortho_ );
515 #endif
516  for (int i=0; i<nq; i++) {
518 
519  // Apply another step of classical Gram-Schmidt
521  *C[i] += C2;
522 #ifdef ANASAZI_BASIC_ORTHO_DEBUG
523  *out << "Applying projector P_Q[" << i << "]...\n";
524 #endif
525  MVT::MvTimesMatAddMv( -ONE, *Q[i], C2, ONE, X );
526 
527  // Update MX as above
528  if (this->_hasOp) {
529  if (MQ[i] == Teuchos::null) {
530 #ifdef ANASAZI_BASIC_ORTHO_DEBUG
531  *out << "Updating MX via M*X...\n";
532 #endif
533  OPT::Apply( *(this->_Op), X, *MX);
534  this->_OpCounter += MVT::GetNumberVecs(X);
535  }
536  else {
537 #ifdef ANASAZI_BASIC_ORTHO_DEBUG
538  *out << "Updating MX via M*Q...\n";
539 #endif
540  MVT::MvTimesMatAddMv( -ONE, *MQ[i], C2, ONE, *MX );
541  }
542  }
543  }
544  break;
545  } // if (kappa_*newDot[j] < oldDot[j])
546  } // for (int j = 0; j < xc; ++j)
547 
548 #ifdef ANASAZI_BASIC_ORTHO_DEBUG
549  *out << "Leaving Anasazi::BasicOrthoManager::projectMat(...)\n";
550 #endif
551  }
552 
553 
554 
556  // Find an Op-orthonormal basis for span(X), with rank numvectors(X)
557  template<class ScalarType, class MV, class OP>
559  MV &X,
561  Teuchos::RCP<MV> MX) const {
562  // call findBasis(), with the instruction to try to generate a basis of rank numvecs(X)
563  // findBasis() requires MX
564 
565  int xc = MVT::GetNumberVecs(X);
566  ptrdiff_t xr = MVText::GetGlobalLength(X);
567 
568  // if Op==null, MX == X (via pointer)
569  // Otherwise, either the user passed in MX or we will allocated and compute it
570  if (this->_hasOp) {
571  if (MX == Teuchos::null) {
572  // we need to allocate space for MX
573  MX = MVT::Clone(X,xc);
574  OPT::Apply(*(this->_Op),X,*MX);
575  this->_OpCounter += MVT::GetNumberVecs(X);
576  }
577  }
578 
579  // if the user doesn't want to store the coefficients,
580  // allocate some local memory for them
581  if ( B == Teuchos::null ) {
583  }
584 
585  int mxc = (this->_hasOp) ? MVT::GetNumberVecs( *MX ) : xc;
586  ptrdiff_t mxr = (this->_hasOp) ? MVText::GetGlobalLength( *MX ) : xr;
587 
588  // check size of C, B
589  TEUCHOS_TEST_FOR_EXCEPTION( xc == 0 || xr == 0, std::invalid_argument,
590  "Anasazi::BasicOrthoManager::normalizeMat(): X must be non-empty" );
591  TEUCHOS_TEST_FOR_EXCEPTION( B->numRows() != xc || B->numCols() != xc, std::invalid_argument,
592  "Anasazi::BasicOrthoManager::normalizeMat(): Size of X not consistent with size of B" );
593  TEUCHOS_TEST_FOR_EXCEPTION( xc != mxc || xr != mxr, std::invalid_argument,
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" );
597 
598  return findBasis(X, MX, *B, true );
599  }
600 
601 
602 
604  // Find an Op-orthonormal basis for span(X) - span(W)
605  template<class ScalarType, class MV, class OP>
607  MV &X,
611  Teuchos::RCP<MV> MX,
613  ) const {
614 
615 #ifdef ANASAZI_BASIC_ORTHO_DEBUG
616  // Get a FancyOStream from out_arg or create a new one ...
618  out = Teuchos::getFancyOStream(Teuchos::rcpFromRef(std::cout));
619  out->setShowAllFrontMatter(false).setShowProcRank(true);
620  *out << "Entering Anasazi::BasicOrthoManager::projectAndNormalizeMat(...)\n";
621 #endif
622 
623  int nq = Q.length();
624  int xc = MVT::GetNumberVecs( X );
625  ptrdiff_t xr = MVText::GetGlobalLength( X );
626  int rank;
627 
628  /* if the user doesn't want to store the coefficients,
629  * allocate some local memory for them
630  */
631  if ( B == Teuchos::null ) {
633  }
634 
635  /****** DO NO MODIFY *MX IF _hasOp == false ******/
636  if (this->_hasOp) {
637  if (MX == Teuchos::null) {
638  // we need to allocate space for MX
639 #ifdef ANASAZI_BASIC_ORTHO_DEBUG
640  *out << "Allocating MX...\n";
641 #endif
642  MX = MVT::Clone(X,MVT::GetNumberVecs(X));
643  OPT::Apply(*(this->_Op),X,*MX);
644  this->_OpCounter += MVT::GetNumberVecs(X);
645  }
646  }
647  else {
648  // Op == I --> MX = X (ignore it if the user passed it in)
649  MX = Teuchos::rcpFromRef(X);
650  }
651 
652  int mxc = MVT::GetNumberVecs( *MX );
653  ptrdiff_t mxr = MVText::GetGlobalLength( *MX );
654 
655  TEUCHOS_TEST_FOR_EXCEPTION( xc == 0 || xr == 0, std::invalid_argument, "Anasazi::BasicOrthoManager::projectAndNormalizeMat(): X must be non-empty" );
656 
657  ptrdiff_t numbas = 0;
658  for (int i=0; i<nq; i++) {
659  numbas += MVT::GetNumberVecs( *Q[i] );
660  }
661 
662  // check size of B
663  TEUCHOS_TEST_FOR_EXCEPTION( B->numRows() != xc || B->numCols() != xc, std::invalid_argument,
664  "Anasazi::BasicOrthoManager::projectAndNormalizeMat(): Size of X must be consistent with size of B" );
665  // check size of X and MX
666  TEUCHOS_TEST_FOR_EXCEPTION( xc<0 || xr<0 || mxc<0 || mxr<0, std::invalid_argument,
667  "Anasazi::BasicOrthoManager::projectAndNormalizeMat(): MVT returned negative dimensions for X,MX" );
668  // check size of X w.r.t. 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" );
671  // check feasibility
672  TEUCHOS_TEST_FOR_EXCEPTION( numbas+xc > xr, std::invalid_argument,
673  "Anasazi::BasicOrthoManager::projectAndNormalizeMat(): Orthogonality constraints not feasible" );
674 
675  // orthogonalize all of X against Q
676 #ifdef ANASAZI_BASIC_ORTHO_DEBUG
677  *out << "Orthogonalizing X against Q...\n";
678 #endif
679  projectMat(X,Q,C,MX,MQ);
680 
682  // start working
683  rank = 0;
684  int numTries = 10; // each vector in X gets 10 random chances to escape degeneracy
685  int oldrank = -1;
686  do {
687  int curxsize = xc - rank;
688 
689  // orthonormalize X, but quit if it is rank deficient
690  // we can't let findBasis generated random vectors to complete the basis,
691  // because it doesn't know about Q; we will do this ourselves below
692 #ifdef ANASAZI_BASIC_ORTHO_DEBUG
693  *out << "Attempting to find orthonormal basis for X...\n";
694 #endif
695  rank = findBasis(X,MX,*B,false,curxsize);
696 
697  if (oldrank != -1 && rank != oldrank) {
698  // we had previously stopped before, after operating on vector oldrank
699  // we saved its coefficients, augmented it with a random vector, and
700  // then called findBasis() again, which proceeded to add vector oldrank
701  // to the basis.
702  // now, restore the saved coefficients into B
703  for (int i=0; i<xc; i++) {
704  (*B)(i,oldrank) = oldCoeff(i,0);
705  }
706  }
707 
708  if (rank < xc) {
709  if (rank != oldrank) {
710  // we quit on this vector and will augment it with random below
711  // this is the first time that we have quit on this vector
712  // therefor, (*B)(:,rank) contains the actual coefficients of the
713  // input vectors with respect to the previous vectors in the basis
714  // save these values, as (*B)(:,rank) will be overwritten by our next
715  // call to findBasis()
716  // we will restore it after we are done working on this vector
717  for (int i=0; i<xc; i++) {
718  oldCoeff(i,0) = (*B)(i,rank);
719  }
720  }
721  }
722 
723  if (rank == xc) {
724  // we are done
725 #ifdef ANASAZI_BASIC_ORTHO_DEBUG
726  *out << "Finished computing basis.\n";
727 #endif
728  break;
729  }
730  else {
731  TEUCHOS_TEST_FOR_EXCEPTION( rank < oldrank, OrthoError,
732  "Anasazi::BasicOrthoManager::projectAndNormalizeMat(): basis lost rank; this shouldn't happen");
733 
734  if (rank != oldrank) {
735  // we added a vector to the basis; reset the chance counter
736  numTries = 10;
737  // store old rank
738  oldrank = rank;
739  }
740  else {
741  // has this vector run out of chances to escape degeneracy?
742  if (numTries <= 0) {
743  break;
744  }
745  }
746  // use one of this vector's chances
747  numTries--;
748 
749  // randomize troubled direction
750 #ifdef ANASAZI_BASIC_ORTHO_DEBUG
751  *out << "Randomizing X[" << rank << "]...\n";
752 #endif
753  Teuchos::RCP<MV> curX, curMX;
754  std::vector<int> ind(1);
755  ind[0] = rank;
756  curX = MVT::CloneViewNonConst(X,ind);
757  MVT::MvRandom(*curX);
758  if (this->_hasOp) {
759 #ifdef ANASAZI_BASIC_ORTHO_DEBUG
760  *out << "Applying operator to random vector.\n";
761 #endif
762  curMX = MVT::CloneViewNonConst(*MX,ind);
763  OPT::Apply( *(this->_Op), *curX, *curMX );
764  this->_OpCounter += MVT::GetNumberVecs(*curX);
765  }
766 
767  // orthogonalize against Q
768  // if !this->_hasOp, the curMX will be ignored.
769  // we don't care about these coefficients
770  // on the contrary, we need to preserve the previous coeffs
771  {
773  projectMat(*curX,Q,dummyC,curMX,MQ);
774  }
775  }
776  } while (1);
777 
778  // this should never raise an exception; but our post-conditions oblige us to check
779  TEUCHOS_TEST_FOR_EXCEPTION( rank > xc || rank < 0, std::logic_error,
780  "Anasazi::BasicOrthoManager::projectAndNormalizeMat(): Debug error in rank variable." );
781 
782 #ifdef ANASAZI_BASIC_ORTHO_DEBUG
783  *out << "Leaving Anasazi::BasicOrthoManager::projectAndNormalizeMat(...)\n";
784 #endif
785 
786  return rank;
787  }
788 
789 
790 
792  // Find an Op-orthonormal basis for span(X), with the option of extending the subspace so that
793  // the rank is numvectors(X)
794  template<class ScalarType, class MV, class OP>
796  MV &X, Teuchos::RCP<MV> MX,
798  bool completeBasis, int howMany ) const {
799 
800  // For the inner product defined by the operator Op or the identity (Op == 0)
801  // -> Orthonormalize X
802  // Modify MX accordingly
803  //
804  // Note that when Op is 0, MX is not referenced
805  //
806  // Parameter variables
807  //
808  // X : Vectors to be orthonormalized
809  //
810  // MX : Image of the multivector X under the operator Op
811  //
812  // Op : Pointer to the operator for the inner product
813  //
814 
815 #ifdef ANASAZI_BASIC_ORTHO_DEBUG
816  // Get a FancyOStream from out_arg or create a new one ...
818  out = Teuchos::getFancyOStream(Teuchos::rcpFromRef(std::cout));
819  out->setShowAllFrontMatter(false).setShowProcRank(true);
820  *out << "Entering Anasazi::BasicOrthoManager::findBasis(...)\n";
821 #endif
822 
823  const ScalarType ONE = SCT::one();
824  const MagnitudeType ZERO = SCT::magnitude(SCT::zero());
825 
826  int xc = MVT::GetNumberVecs( X );
827 
828  if (howMany == -1) {
829  howMany = xc;
830  }
831 
832  /*******************************************************
833  * If _hasOp == false, we will not reference MX below *
834  *******************************************************/
835  TEUCHOS_TEST_FOR_EXCEPTION(this->_hasOp == true && MX == Teuchos::null, std::logic_error,
836  "Anasazi::BasicOrthoManager::findBasis(): calling routine did not specify MS.");
837  TEUCHOS_TEST_FOR_EXCEPTION( howMany < 0 || howMany > xc, std::logic_error,
838  "Anasazi::BasicOrthoManager::findBasis(): Invalid howMany parameter" );
839 
840  /* xstart is which column we are starting the process with, based on howMany
841  * columns before xstart are assumed to be Op-orthonormal already
842  */
843  int xstart = xc - howMany;
844 
845  for (int j = xstart; j < xc; j++) {
846 
847  // numX represents the number of currently orthonormal columns of X
848  int numX = j;
849  // j represents the index of the current column of X
850  // these are different interpretations of the same value
851 
852  //
853  // set the lower triangular part of B to zero
854  for (int i=j+1; i<xc; ++i) {
855  B(i,j) = ZERO;
856  }
857 
858  // Get a view of the vector currently being worked on.
859  std::vector<int> index(1);
860  index[0] = j;
861  Teuchos::RCP<MV> Xj = MVT::CloneViewNonConst( X, index );
862  Teuchos::RCP<MV> MXj;
863  if ((this->_hasOp)) {
864  // MXj is a view of the current vector in MX
865  MXj = MVT::CloneViewNonConst( *MX, index );
866  }
867  else {
868  // MXj is a pointer to Xj, and MUST NOT be modified
869  MXj = Xj;
870  }
871 
872  // Get a view of the previous vectors.
873  std::vector<int> prev_idx( numX );
874  Teuchos::RCP<const MV> prevX, prevMX;
875 
876  if (numX > 0) {
877  for (int i=0; i<numX; ++i) prev_idx[i] = i;
878  prevX = MVT::CloneViewNonConst( X, prev_idx );
879  if (this->_hasOp) {
880  prevMX = MVT::CloneViewNonConst( *MX, prev_idx );
881  }
882  }
883 
884  bool rankDef = true;
885  /* numTrials>0 will denote that the current vector was randomized for the purpose
886  * of finding a basis vector, and that the coefficients of that vector should
887  * not be stored in B
888  */
889  for (int numTrials = 0; numTrials < 10; numTrials++) {
890 #ifdef ANASAZI_BASIC_ORTHO_DEBUG
891  *out << "Trial " << numTrials << " for vector " << j << "\n";
892 #endif
893 
894  // Make storage for these Gram-Schmidt iterations.
896  std::vector<MagnitudeType> origNorm(1), newNorm(1), newNorm2(1);
897 
898  //
899  // Save old MXj vector and compute Op-norm
900  //
901  Teuchos::RCP<MV> oldMXj = MVT::CloneCopy( *MXj );
903 #ifdef ANASAZI_BASIC_ORTHO_DEBUG
904  *out << "origNorm = " << origNorm[0] << "\n";
905 #endif
906 
907  if (numX > 0) {
908  // Apply the first step of Gram-Schmidt
909 
910  // product <- prevX^T MXj
911  MatOrthoManager<ScalarType,MV,OP>::innerProdMat(*prevX,*Xj,product,Teuchos::null,MXj);
912 
913  // Xj <- Xj - prevX prevX^T MXj
914  // = Xj - prevX product
915 #ifdef ANASAZI_BASIC_ORTHO_DEBUG
916  *out << "Orthogonalizing X[" << j << "]...\n";
917 #endif
918  MVT::MvTimesMatAddMv( -ONE, *prevX, product, ONE, *Xj );
919 
920  // Update MXj
921  if (this->_hasOp) {
922  // MXj <- Op*Xj_new
923  // = Op*(Xj_old - prevX prevX^T MXj)
924  // = MXj - prevMX product
925 #ifdef ANASAZI_BASIC_ORTHO_DEBUG
926  *out << "Updating MX[" << j << "]...\n";
927 #endif
928  MVT::MvTimesMatAddMv( -ONE, *prevMX, product, ONE, *MXj );
929  }
930 
931  // Compute new Op-norm
933  MagnitudeType product_norm = product.normOne();
934 
935 #ifdef ANASAZI_BASIC_ORTHO_DEBUG
936  *out << "newNorm = " << newNorm[0] << "\n";
937  *out << "prodoct_norm = " << product_norm << "\n";
938 #endif
939 
940  // Check if a correction is needed.
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";
945  }
946  else {
947  *out << "eps*origNorm == " << eps_*origNorm[0] << "... another step of Gram-Schmidt.\n";
948  }
949 #endif
950  // Apply the second step of Gram-Schmidt
951  // This is the same as above
953  MatOrthoManager<ScalarType,MV,OP>::innerProdMat(*prevX,*Xj,P2,Teuchos::null,MXj);
954  product += P2;
955 #ifdef ANASAZI_BASIC_ORTHO_DEBUG
956  *out << "Orthogonalizing X[" << j << "]...\n";
957 #endif
958  MVT::MvTimesMatAddMv( -ONE, *prevX, P2, ONE, *Xj );
959  if ((this->_hasOp)) {
960 #ifdef ANASAZI_BASIC_ORTHO_DEBUG
961  *out << "Updating MX[" << j << "]...\n";
962 #endif
963  MVT::MvTimesMatAddMv( -ONE, *prevMX, P2, ONE, *MXj );
964  }
965  // Compute new Op-norms
967  product_norm = P2.normOne();
968 #ifdef ANASAZI_BASIC_ORTHO_DEBUG
969  *out << "newNorm2 = " << newNorm2[0] << "\n";
970  *out << "product_norm = " << product_norm << "\n";
971 #endif
972  if ( product_norm/newNorm2[0] >= tol_ || newNorm2[0] < eps_*origNorm[0] ) {
973  // we don't do another GS, we just set it to zero.
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";
977  }
978  else if (newNorm[0] < newNorm2[0]) {
979  *out << "newNorm2 > newNorm... setting vector to zero.\n";
980  }
981  else {
982  *out << "eps*origNorm == " << eps_*origNorm[0] << "... setting vector to zero.\n";
983  }
984 #endif
985  MVT::MvInit(*Xj,ZERO);
986  if ((this->_hasOp)) {
987 #ifdef ANASAZI_BASIC_ORTHO_DEBUG
988  *out << "Setting MX[" << j << "] to zero as well.\n";
989 #endif
990  MVT::MvInit(*MXj,ZERO);
991  }
992  }
993  }
994  } // if (numX > 0) do GS
995 
996  // save the coefficients, if we are working on the original vector and not a randomly generated one
997  if (numTrials == 0) {
998  for (int i=0; i<numX; i++) {
999  B(i,j) = product(i,0);
1000  }
1001  }
1002 
1003  // Check if Xj has any directional information left after the orthogonalization.
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";
1008 #endif
1009  // Normalize Xj.
1010  // Xj <- Xj / norm
1011  MVT::MvScale( *Xj, ONE/newNorm[0]);
1012  if (this->_hasOp) {
1013 #ifdef ANASAZI_BASIC_ORTHO_DEBUG
1014  *out << "Normalizing M*X[" << j << "]...\n";
1015 #endif
1016  // Update MXj.
1017  MVT::MvScale( *MXj, ONE/newNorm[0]);
1018  }
1019 
1020  // save it, if it corresponds to the original vector and not a randomly generated one
1021  if (numTrials == 0) {
1022  B(j,j) = newNorm[0];
1023  }
1024 
1025  // We are not rank deficient in this vector. Move on to the next vector in X.
1026  rankDef = false;
1027  break;
1028  }
1029  else {
1030 #ifdef ANASAZI_BASIC_ORTHO_DEBUG
1031  *out << "Not normalizing M*X[" << j << "]...\n";
1032 #endif
1033  // There was nothing left in Xj after orthogonalizing against previous columns in X.
1034  // X is rank deficient.
1035  // reflect this in the coefficients
1036  B(j,j) = ZERO;
1037 
1038  if (completeBasis) {
1039  // Fill it with random information and keep going.
1040 #ifdef ANASAZI_BASIC_ORTHO_DEBUG
1041  *out << "Inserting random vector in X[" << j << "]...\n";
1042 #endif
1043  MVT::MvRandom( *Xj );
1044  if (this->_hasOp) {
1045 #ifdef ANASAZI_BASIC_ORTHO_DEBUG
1046  *out << "Updating M*X[" << j << "]...\n";
1047 #endif
1048  OPT::Apply( *(this->_Op), *Xj, *MXj );
1049  this->_OpCounter += MVT::GetNumberVecs(*Xj);
1050  }
1051  }
1052  else {
1053  rankDef = true;
1054  break;
1055  }
1056  }
1057  } // for (numTrials = 0; numTrials < 10; ++numTrials)
1058 
1059  // if rankDef == true, then quit and notify user of rank obtained
1060  if (rankDef == true) {
1061  TEUCHOS_TEST_FOR_EXCEPTION( rankDef && completeBasis, OrthoError,
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";
1065 #endif
1066  return j;
1067  }
1068 
1069  } // for (j = 0; j < xc; ++j)
1070 
1071 #ifdef ANASAZI_BASIC_ORTHO_DEBUG
1072  *out << "Returning " << xc << " from Anasazi::BasicOrthoManager::findBasis(...)\n";
1073 #endif
1074  return xc;
1075  }
1076 
1077 } // namespace Anasazi
1078 
1079 #endif // ANASAZI_BASIC_ORTHOMANAGER_HPP
1080 
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)
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 T pow(T x, T y)
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&#39;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.