Anasazi  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
AnasaziMatOrthoManager.hpp
Go to the documentation of this file.
1 // @HEADER
2 // *****************************************************************************
3 // Anasazi: Block Eigensolvers Package
4 //
5 // Copyright 2004 NTESS and the Anasazi contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9 
15 #ifndef ANASAZI_MATORTHOMANAGER_HPP
16 #define ANASAZI_MATORTHOMANAGER_HPP
17 
35 #include "AnasaziConfigDefs.hpp"
36 #include "AnasaziTypes.hpp"
37 #include "AnasaziOrthoManager.hpp"
40 
41 namespace Anasazi {
42 
43  template <class ScalarType, class MV, class OP>
44  class MatOrthoManager : public OrthoManager<ScalarType,MV> {
45  public:
47 
48  MatOrthoManager(Teuchos::RCP<const OP> Op = Teuchos::null);
50 
52  virtual ~MatOrthoManager() {};
54 
56 
57 
59  virtual void setOp( Teuchos::RCP<const OP> Op );
60 
62  virtual Teuchos::RCP<const OP> getOp() const;
63 
65 
70  int getOpCounter() const;
71 
73 
75  void resetOpCounter();
76 
78 
80 
81 
91  void innerProdMat(
92  const MV& X, const MV& Y,
94  Teuchos::RCP<const MV> MX = Teuchos::null,
95  Teuchos::RCP<const MV> MY = Teuchos::null
96  ) const;
97 
106  void normMat(
107  const MV& X,
108  std::vector< typename Teuchos::ScalarTraits<ScalarType>::magnitudeType > &normvec,
109  Teuchos::RCP<const MV> MX = Teuchos::null
110  ) const;
111 
122  virtual void projectMat (
123  MV &X,
126  = Teuchos::tuple(Teuchos::RCP< Teuchos::SerialDenseMatrix<int,ScalarType> >(Teuchos::null)),
127  Teuchos::RCP<MV> MX = Teuchos::null,
129  = Teuchos::tuple(Teuchos::RCP<const MV>(Teuchos::null))
130  ) const = 0;
131 
144  virtual int normalizeMat (
145  MV &X,
147  Teuchos::RCP<MV> MX = Teuchos::null
148  ) const = 0;
149 
150 
165  virtual int projectAndNormalizeMat (
166  MV &X,
169  = Teuchos::tuple(Teuchos::RCP< Teuchos::SerialDenseMatrix<int,ScalarType> >(Teuchos::null)),
171  Teuchos::RCP<MV> MX = Teuchos::null,
173  = Teuchos::tuple(Teuchos::RCP<const MV>(Teuchos::null))
174  ) const = 0;
175 
181  orthonormErrorMat(const MV &X, Teuchos::RCP<const MV> MX = Teuchos::null) const = 0;
182 
189  const MV &X,
190  const MV &Y,
191  Teuchos::RCP<const MV> MX = Teuchos::null,
192  Teuchos::RCP<const MV> MY = Teuchos::null
193  ) const = 0;
194 
196 
198 
199 
207  void innerProd( const MV& X, const MV& Y, Teuchos::SerialDenseMatrix<int,ScalarType>& Z ) const;
208 
216  void norm( const MV& X, std::vector< typename Teuchos::ScalarTraits<ScalarType>::magnitudeType > &normvec ) const;
217 
225  void project (
226  MV &X,
229  = Teuchos::tuple(Teuchos::RCP< Teuchos::SerialDenseMatrix<int,ScalarType> >(Teuchos::null))
230  ) const;
231 
239  int normalize ( MV &X, Teuchos::RCP<Teuchos::SerialDenseMatrix<int,ScalarType> > B = Teuchos::null) const;
240 
248  int projectAndNormalize (
249  MV &X,
252  = Teuchos::tuple(Teuchos::RCP< Teuchos::SerialDenseMatrix<int,ScalarType> >(Teuchos::null)),
254  ) const;
255 
264  orthonormError(const MV &X) const;
265 
274  orthogError(const MV &X1, const MV &X2) const;
275 
277 
278  protected:
280  bool _hasOp;
281  mutable int _OpCounter;
282 
283  };
284 
285  template <class ScalarType, class MV, class OP>
287  : _Op(Op), _hasOp(Op!=Teuchos::null), _OpCounter(0) {}
288 
289  template <class ScalarType, class MV, class OP>
291  {
292  _Op = Op;
293  _hasOp = (_Op != Teuchos::null);
294  }
295 
296  template <class ScalarType, class MV, class OP>
298  {
299  return _Op;
300  }
301 
302  template <class ScalarType, class MV, class OP>
304  {
305  return _OpCounter;
306  }
307 
308  template <class ScalarType, class MV, class OP>
310  {
311  _OpCounter = 0;
312  }
313 
314  template <class ScalarType, class MV, class OP>
316  const MV& X, const MV& Y, Teuchos::SerialDenseMatrix<int,ScalarType>& Z ) const
317  {
319  typedef MultiVecTraits<ScalarType,MV> MVT;
321 
324 
325  if (_hasOp) {
326  // attempt to minimize the amount of work in applying
327  if ( MVT::GetNumberVecs(X) < MVT::GetNumberVecs(Y) ) {
328  R = MVT::Clone(X,MVT::GetNumberVecs(X));
329  OPT::Apply(*_Op,X,*R);
330  _OpCounter += MVT::GetNumberVecs(X);
331  P = R;
332  Q = Teuchos::rcpFromRef(Y);
333  }
334  else {
335  P = Teuchos::rcpFromRef(X);
336  R = MVT::Clone(Y,MVT::GetNumberVecs(Y));
337  OPT::Apply(*_Op,Y,*R);
338  _OpCounter += MVT::GetNumberVecs(Y);
339  Q = R;
340  }
341  }
342  else {
343  P = Teuchos::rcpFromRef(X);
344  Q = Teuchos::rcpFromRef(Y);
345  }
346 
347  MVT::MvTransMv(SCT::one(),*P,*Q,Z);
348  }
349 
350  template <class ScalarType, class MV, class OP>
353  {
354  (void) MX;
356  typedef MultiVecTraits<ScalarType,MV> MVT;
357  // typedef OperatorTraits<ScalarType,MV,OP> OPT; // unused
358 
359  Teuchos::RCP<MV> P,Q;
360 
361  if ( MY == Teuchos::null ) {
362  innerProd(X,Y,Z);
363  }
364  else if ( _hasOp ) {
365  // the user has done the matrix vector for us
366  MVT::MvTransMv(SCT::one(),X,*MY,Z);
367  }
368  else {
369  // there is no matrix vector
370  MVT::MvTransMv(SCT::one(),X,Y,Z);
371  }
372 #ifdef TEUCHOS_DEBUG
373  for (int j=0; j<Z.numCols(); j++) {
374  for (int i=0; i<Z.numRows(); i++) {
375  TEUCHOS_TEST_FOR_EXCEPTION(SCT::isnaninf(Z(i,j)), std::logic_error,
376  "Anasazi::MatOrthoManager::innerProdMat(): detected NaN/inf.");
377  }
378  }
379 #endif
380  }
381 
382  template <class ScalarType, class MV, class OP>
384  const MV& X, std::vector< typename Teuchos::ScalarTraits<ScalarType>::magnitudeType > &normvec ) const
385  {
386  this->normMat(X,normvec);
387  }
388 
389  template <class ScalarType, class MV, class OP>
391  const MV& X,
392  std::vector< typename Teuchos::ScalarTraits<ScalarType>::magnitudeType > &normvec,
393  Teuchos::RCP<const MV> MX) const
394  {
397  typedef MultiVecTraits<ScalarType,MV> MVT;
399 
400  int nvecs = MVT::GetNumberVecs(X);
401 
402  // Make sure that normvec has enough entries to hold the norms
403  // of all the columns of X. std::vector<T>::size_type is
404  // unsigned, so do the appropriate cast to avoid signed/unsigned
405  // comparisons that trigger compiler warnings.
406  if (normvec.size() < static_cast<size_t>(nvecs))
407  normvec.resize (nvecs);
408 
409  if (!_hasOp) {
410  // X == MX, since the operator M is the identity.
411  MX = Teuchos::rcp(&X, false);
412  MVT::MvNorm(X, normvec);
413  }
414  else {
415  // The caller didn't give us a previously computed MX, so
416  // apply the operator. We assign to MX only after applying
417  // the operator, so that if the application fails, MX won't be
418  // modified.
419  if(MX == Teuchos::null) {
420  Teuchos::RCP<MV> tempVec = MVT::Clone(X,nvecs);
421  OPT::Apply(*_Op,X,*tempVec);
422  _OpCounter += nvecs;
423  MX = tempVec;
424  }
425  else {
426  // The caller gave us a previously computed MX. Make sure
427  // that it has at least as many columns as X.
428  const int numColsMX = MVT::GetNumberVecs(*MX);
429  TEUCHOS_TEST_FOR_EXCEPTION(numColsMX < nvecs, std::invalid_argument,
430  "MatOrthoManager::norm(X, MX, normvec): "
431  "MX has fewer columns than X: "
432  "MX has " << numColsMX << " columns, "
433  "and X has " << nvecs << " columns.");
434  }
435 
436  std::vector<ScalarType> dotvec(nvecs);
437  MVT::MvDot(X,*MX,dotvec);
438  for (int i=0; i<nvecs; i++) {
439  normvec[i] = MT::squareroot( SCT::magnitude(dotvec[i]) );
440  }
441  }
442  }
443 
444  template <class ScalarType, class MV, class OP>
446  MV &X,
449  ) const
450  {
451  this->projectMat(X,Q,C);
452  }
453 
454  template <class ScalarType, class MV, class OP>
457  {
458  return this->normalizeMat(X,B);
459  }
460 
461  template <class ScalarType, class MV, class OP>
463  MV &X,
467  ) const
468  {
469  return this->projectAndNormalizeMat(X,Q,C,B);
470  }
471 
472  template <class ScalarType, class MV, class OP>
475  {
476  return this->orthonormErrorMat(X,Teuchos::null);
477  }
478 
479  template <class ScalarType, class MV, class OP>
481  MatOrthoManager<ScalarType,MV,OP>::orthogError(const MV &X1, const MV &X2) const
482  {
483  return this->orthogErrorMat(X1,X2);
484  }
485 
486 } // end of Anasazi namespace
487 
488 
489 #endif
490 
491 // end of file AnasaziMatOrthoManager.hpp
void norm(const MV &X, std::vector< typename Teuchos::ScalarTraits< ScalarType >::magnitudeType > &normvec) const
Implements the interface OrthoManager::norm().
virtual void setOp(Teuchos::RCP< const OP > Op)
Set operator used for inner product.
Declaration of basic traits for the multivector type.
int normalize(MV &X, Teuchos::RCP< Teuchos::SerialDenseMatrix< int, ScalarType > > B=Teuchos::null) const
Implements the interface OrthoManager::normalize().
#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.
Teuchos::ScalarTraits< ScalarType >::magnitudeType orthonormError(const MV &X) const
Implements the interface OrthoManager::orthonormError().
virtual 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 =0
Provides matrix-based projection method.
virtual Teuchos::RCP< const OP > getOp() const
Get operator used for inner product.
Anasazi&#39;s templated virtual class for providing routines for orthogonalization and orthonormalization...
void innerProd(const MV &X, const MV &Y, Teuchos::SerialDenseMatrix< int, ScalarType > &Z) const
Implements the interface OrthoManager::innerProd().
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
virtual Teuchos::ScalarTraits< ScalarType >::magnitudeType orthonormErrorMat(const MV &X, Teuchos::RCP< const MV > MX=Teuchos::null) const =0
This method computes the error in orthonormality of a multivector.
MatOrthoManager(Teuchos::RCP< const OP > Op=Teuchos::null)
Default constructor.
Templated virtual class for providing orthogonalization/orthonormalization methods.
Traits class which defines basic operations on multivectors.
Virtual base class which defines basic traits for the operator type.
virtual ~MatOrthoManager()
Destructor.
Anasazi header file which uses auto-configuration information to include necessary C++ headers...
void resetOpCounter()
Reset the operator counter to zero.
int projectAndNormalize(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) const
Implements the interface OrthoManager::projectAndNormalize().
int getOpCounter() const
Retrieve operator counter.
OrdinalType numCols() const
Teuchos::ScalarTraits< ScalarType >::magnitudeType orthogError(const MV &X1, const MV &X2) const
Implements the interface OrthoManager::orthogError().
virtual int normalizeMat(MV &X, Teuchos::RCP< Teuchos::SerialDenseMatrix< int, ScalarType > > B=Teuchos::null, Teuchos::RCP< MV > MX=Teuchos::null) const =0
Provides matrix-based orthonormalization method.
Types and exceptions used within Anasazi solvers and interfaces.
Anasazi&#39;s templated virtual class for providing routines for orthogonalization and orthonormalization...
void project(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))) const
Implements the interface OrthoManager::project().
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.
virtual 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 =0
This method computes the error in orthogonality of two multivectors.
OrdinalType numRows() const
virtual 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 =0
Provides matrix-based projection/orthonormalization method.