Anasazi  Version of the Day
AnasaziMultiVec.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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
23 // USA
24 // Questions? Contact Michael A. Heroux (maherou@sandia.gov)
25 //
26 // ***********************************************************************
27 // @HEADER
28 
29 #ifndef ANASAZI_MULTI_VEC_HPP
30 #define ANASAZI_MULTI_VEC_HPP
31 
46 
47 #include "AnasaziConfigDefs.hpp"
49 
50 namespace Anasazi {
51 
76 template <class ScalarType>
77 class MultiVec {
78 public:
80 
81  MultiVec() {}
83 
85  virtual ~MultiVec () {}
86 
88 
90 
93  virtual MultiVec<ScalarType> * Clone ( const int numvecs ) const = 0;
94 
97  virtual MultiVec<ScalarType> * CloneCopy () const = 0;
98 
105  virtual MultiVec<ScalarType> * CloneCopy ( const std::vector<int>& index ) const = 0;
106 
113  virtual MultiVec<ScalarType> * CloneViewNonConst ( const std::vector<int>& index ) = 0;
114 
121  virtual const MultiVec<ScalarType> * CloneView ( const std::vector<int>& index ) const = 0;
122 
124 
126 
128  virtual ptrdiff_t GetGlobalLength () const = 0;
129 
131  virtual int GetNumberVecs () const = 0;
132 
134 
136 
138  virtual void
139  MvTimesMatAddMv (ScalarType alpha,
140  const MultiVec<ScalarType>& A,
141  const Teuchos::SerialDenseMatrix<int,ScalarType>& B, ScalarType beta) = 0;
142 
144  virtual void MvAddMv ( ScalarType alpha, const MultiVec<ScalarType>& A, ScalarType beta, const MultiVec<ScalarType>& B ) = 0;
145 
147  virtual void MvScale ( ScalarType alpha ) = 0;
148 
150  virtual void MvScale ( const std::vector<ScalarType>& alpha ) = 0;
151 
155  virtual void MvTransMv ( ScalarType alpha, const MultiVec<ScalarType>& A, Teuchos::SerialDenseMatrix<int,ScalarType>& B
156 #ifdef HAVE_ANASAZI_EXPERIMENTAL
157  , ConjType conj = Anasazi::CONJ
158 #endif
159  ) const = 0;
160 
166  virtual void MvDot ( const MultiVec<ScalarType>& A, std::vector<ScalarType> & b
167 #ifdef HAVE_ANASAZI_EXPERIMENTAL
168  , ConjType conj = Anasazi::CONJ
169 #endif
170  ) const = 0;
171 
173 
175 
180  virtual void MvNorm ( std::vector<typename Teuchos::ScalarTraits<ScalarType>::magnitudeType> & normvec ) const = 0;
181 
183 
185 
190  virtual void SetBlock ( const MultiVec<ScalarType>& A, const std::vector<int>& index ) = 0;
191 
193  virtual void MvRandom () = 0;
194 
196  virtual void MvInit ( ScalarType alpha ) = 0;
197 
199 
201 
203  virtual void MvPrint ( std::ostream& os ) const = 0;
205 
206 #ifdef HAVE_ANASAZI_TSQR
207 
209 
232  virtual void
233  factorExplicit (MultiVec<ScalarType>& Q,
234  Teuchos::SerialDenseMatrix<int, ScalarType>& R,
235  const bool forceNonnegativeDiagonal=false)
236  {
237  TEUCHOS_TEST_FOR_EXCEPTION(true, std::logic_error, "The Anasazi::MultiVec<"
238  << Teuchos::TypeNameTraits<ScalarType>::name() << "> subclass which you "
239  "are using does not implement the TSQR-related method factorExplicit().");
240  }
241 
276  virtual int
277  revealRank (Teuchos::SerialDenseMatrix<int, ScalarType>& R,
278  const typename Teuchos::ScalarTraits<ScalarType>::magnitudeType& tol)
279  {
280  TEUCHOS_TEST_FOR_EXCEPTION(true, std::logic_error, "The Anasazi::MultiVec<"
281  << Teuchos::TypeNameTraits<ScalarType>::name() << "> subclass which you "
282  "are using does not implement the TSQR-related method revealRank().");
283  }
284 
286 #endif // HAVE_ANASAZI_TSQR
287 };
288 
289 namespace details {
307 template<class ScalarType>
309 public:
310  typedef MultiVec<ScalarType> MV;
311  typedef ScalarType scalar_type;
312  typedef int ordinal_type; // This doesn't matter either
313  typedef int node_type; // Nor does this
314  typedef Teuchos::SerialDenseMatrix<ordinal_type, scalar_type> dense_matrix_type;
315  typedef typename Teuchos::ScalarTraits<scalar_type>::magnitudeType magnitude_type;
316 
318  void
320  MV& Q,
321  dense_matrix_type& R,
322  const bool forceNonnegativeDiagonal=false)
323  {
324  A.factorExplicit (Q, R, forceNonnegativeDiagonal);
325  }
326 
328  int
329  revealRank (MV& Q,
330  dense_matrix_type& R,
331  const magnitude_type& tol)
332  {
333  return Q.revealRank (R, tol);
334  }
335 };
336 } // namespace details
337 
347  template<class ScalarType>
348  class MultiVecTraits<ScalarType,MultiVec<ScalarType> > {
349  public:
351 
352 
355  static Teuchos::RCP<MultiVec<ScalarType> >
356  Clone (const MultiVec<ScalarType>& mv, const int numvecs) {
357  return Teuchos::rcp (const_cast<MultiVec<ScalarType>&> (mv).Clone (numvecs));
358  }
359 
364  static Teuchos::RCP<MultiVec<ScalarType> > CloneCopy( const MultiVec<ScalarType>& mv )
365  { return Teuchos::rcp( const_cast<MultiVec<ScalarType>&>(mv).CloneCopy() ); }
366 
372  static Teuchos::RCP<MultiVec<ScalarType> > CloneCopy( const MultiVec<ScalarType>& mv, const std::vector<int>& index )
373  { return Teuchos::rcp( const_cast<MultiVec<ScalarType>&>(mv).CloneCopy(index) ); }
374 
380  static Teuchos::RCP<MultiVec<ScalarType> > CloneViewNonConst( MultiVec<ScalarType>& mv, const std::vector<int>& index )
381  { return Teuchos::rcp( mv.CloneViewNonConst(index) ); }
382 
388  static Teuchos::RCP<const MultiVec<ScalarType> > CloneView( const MultiVec<ScalarType>& mv, const std::vector<int>& index )
389  { return Teuchos::rcp( const_cast<MultiVec<ScalarType>&>(mv).CloneView(index) ); }
390 
392 
394 
395 
397  static ptrdiff_t GetGlobalLength( const MultiVec<ScalarType>& mv )
398  { return mv.GetGlobalLength(); }
399 
401  static int GetNumberVecs( const MultiVec<ScalarType>& mv )
402  { return mv.GetNumberVecs(); }
403 
405 
407 
408 
411  static void MvTimesMatAddMv( ScalarType alpha, const MultiVec<ScalarType>& A,
412  const Teuchos::SerialDenseMatrix<int,ScalarType>& B,
413  ScalarType beta, MultiVec<ScalarType>& mv )
414  { mv.MvTimesMatAddMv(alpha, A, B, beta); }
415 
418  static void MvAddMv( ScalarType alpha, const MultiVec<ScalarType>& A, ScalarType beta, const MultiVec<ScalarType>& B, MultiVec<ScalarType>& mv )
419  { mv.MvAddMv(alpha, A, beta, B); }
420 
423  static void MvTransMv( ScalarType alpha, const MultiVec<ScalarType>& A, const MultiVec<ScalarType>& mv, Teuchos::SerialDenseMatrix<int,ScalarType>& B
424 #ifdef HAVE_ANASAZI_EXPERIMENTAL
425  , ConjType conj = Anasazi::CONJ
426 #endif
427  )
428  { mv.MvTransMv(alpha, A, B
429 #ifdef HAVE_ANASAZI_EXPERIMENTAL
430  , conj
431 #endif
432  ); }
433 
436  static void MvDot( const MultiVec<ScalarType>& mv, const MultiVec<ScalarType>& A, std::vector<ScalarType> & b
437 #ifdef HAVE_ANASAZI_EXPERIMENTAL
438  , ConjType conj = Anasazi::CONJ
439 #endif
440  )
441  { mv.MvDot( A, b
442 #ifdef HAVE_ANASAZI_EXPERIMENTAL
443  , conj
444 #endif
445  ); }
446 
448  static void MvScale ( MultiVec<ScalarType>& mv, ScalarType alpha )
449  { mv.MvScale( alpha ); }
450 
452  static void MvScale ( MultiVec<ScalarType>& mv, const std::vector<ScalarType>& alpha )
453  { mv.MvScale( alpha ); }
454 
456 
458 
462  static void MvNorm( const MultiVec<ScalarType>& mv, std::vector<typename Teuchos::ScalarTraits<ScalarType>::magnitudeType> & normvec )
463  { mv.MvNorm(normvec); }
464 
466 
468 
473  static void SetBlock( const MultiVec<ScalarType>& A, const std::vector<int>& index, MultiVec<ScalarType>& mv )
474  { mv.SetBlock(A, index); }
475 
478  static void MvRandom( MultiVec<ScalarType>& mv )
479  { mv.MvRandom(); }
480 
483  static void MvInit( MultiVec<ScalarType>& mv, ScalarType alpha = Teuchos::ScalarTraits<ScalarType>::zero() )
484  { mv.MvInit(alpha); }
485 
487 
489 
491  static void MvPrint( const MultiVec<ScalarType>& mv, std::ostream& os )
492  { mv.MvPrint(os); }
493 
495 
496 #ifdef HAVE_ANASAZI_TSQR
497  typedef details::MultiVecTsqrAdapter<ScalarType> tsqr_adaptor_type;
505 #endif // HAVE_ANASAZI_TSQR
506  };
507 
508 } // namespace Anasazi
509 
510 #endif
511 
512 // end of file AnasaziMultiVec.hpp
virtual void MvRandom()=0
Fill all the vectors in *this with random numbers.
static void MvScale(MultiVec< ScalarType > &mv, ScalarType alpha)
Scale each element of the vectors in *this with alpha.
static void MvPrint(const MultiVec< ScalarType > &mv, std::ostream &os)
Print the mv multi-vector to the os output stream.
void factorExplicit(MV &A, MV &Q, dense_matrix_type &R, const bool forceNonnegativeDiagonal=false)
Compute QR factorization A = QR, using TSQR.
virtual void MvInit(ScalarType alpha)=0
Replace each element of the vectors in *this with alpha.
static void MvInit(MultiVec< ScalarType > &mv, ScalarType alpha=Teuchos::ScalarTraits< ScalarType >::zero())
Replace each element of the vectors in mv with alpha.
virtual void SetBlock(const MultiVec< ScalarType > &A, const std::vector< int > &index)=0
Copy the vectors in A to a set of vectors in *this.
virtual void MvDot(const MultiVec< ScalarType > &A, std::vector< ScalarType > &b) const =0
Compute the dot product of each column of *this with the corresponding column of A.
virtual void MvAddMv(ScalarType alpha, const MultiVec< ScalarType > &A, ScalarType beta, const MultiVec< ScalarType > &B)=0
Replace *this with alpha * A + beta * B.
virtual void MvPrint(std::ostream &os) const =0
Print *this multivector to the os output stream.
virtual ptrdiff_t GetGlobalLength() const =0
The number of rows in the multivector.
Declaration of basic traits for the multivector type.
static void SetBlock(const MultiVec< ScalarType > &A, const std::vector< int > &index, MultiVec< ScalarType > &mv)
Copy the vectors in A to a set of vectors in mv indicated by the indices given in index...
static Teuchos::RCP< MultiVec< ScalarType > > Clone(const MultiVec< ScalarType > &mv, const int numvecs)
Create a new empty MultiVec containing numvecs columns.
static void MvAddMv(ScalarType alpha, const MultiVec< ScalarType > &A, ScalarType beta, const MultiVec< ScalarType > &B, MultiVec< ScalarType > &mv)
Replace mv with .
static void MvNorm(const MultiVec< ScalarType > &mv, std::vector< typename Teuchos::ScalarTraits< ScalarType >::magnitudeType > &normvec)
Compute the 2-norm of each individual vector of mv. Upon return, normvec[i] holds the value of ...
static void MvRandom(MultiVec< ScalarType > &mv)
Replace the vectors in mv with random vectors.
virtual const MultiVec< ScalarType > * CloneView(const std::vector< int > &index) const =0
Creates a new Anasazi::MultiVec that shares the selected contents of *this. The index of the numvecs ...
Namespace Anasazi contains the classes, structs, enums and utilities used by the Anasazi package...
int revealRank(MV &Q, dense_matrix_type &R, const magnitude_type &tol)
Compute rank-revealing decomposition using results of factorExplicit().
static void MvDot(const MultiVec< ScalarType > &mv, const MultiVec< ScalarType > &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 ...
virtual void MvNorm(std::vector< typename Teuchos::ScalarTraits< ScalarType >::magnitudeType > &normvec) const =0
Compute the 2-norm of each vector in *this.
static Teuchos::RCP< MultiVec< ScalarType > > CloneViewNonConst(MultiVec< ScalarType > &mv, const std::vector< int > &index)
Creates a new Anasazi::MultiVec that shares the selected contents of mv (shallow copy).
ConjType
Enumerated types used to specify conjugation arguments.
virtual int GetNumberVecs() const =0
The number of vectors (i.e., columns) in the multivector.
virtual void MvTransMv(ScalarType alpha, const MultiVec< ScalarType > &A, Teuchos::SerialDenseMatrix< int, ScalarType > &B) const =0
Compute a dense matrix B through the matrix-matrix multiply alpha * A^T * (*this).
MultiVec()
Default constructor.
Traits class which defines basic operations on multivectors.
static Teuchos::RCP< const MultiVec< ScalarType > > CloneView(const MultiVec< ScalarType > &mv, const std::vector< int > &index)
Creates a new const Anasazi::MultiVec that shares the selected contents of mv (shallow copy)...
static int GetNumberVecs(const MultiVec< ScalarType > &mv)
Obtain the number of vectors in mv.
Anasazi header file which uses auto-configuration information to include necessary C++ headers...
static void MvTimesMatAddMv(ScalarType alpha, const MultiVec< ScalarType > &A, const Teuchos::SerialDenseMatrix< int, ScalarType > &B, ScalarType beta, MultiVec< ScalarType > &mv)
Update mv with .
static Teuchos::RCP< MultiVec< ScalarType > > CloneCopy(const MultiVec< ScalarType > &mv)
Creates a new Anasazi::MultiVec and copies contents of mv into the new vector (deep copy)...
static void MvTransMv(ScalarType alpha, const MultiVec< ScalarType > &A, const MultiVec< ScalarType > &mv, Teuchos::SerialDenseMatrix< int, ScalarType > &B)
Compute a dense matrix B through the matrix-matrix multiply .
virtual void MvScale(ScalarType alpha)=0
Scale each element of the vectors in *this with alpha.
virtual MultiVec< ScalarType > * CloneViewNonConst(const std::vector< int > &index)=0
Creates a new Anasazi::MultiVec that shares the selected contents of *this. The index of the numvecs ...
virtual MultiVec< ScalarType > * CloneCopy() const =0
Create a new MultiVec and copy contents of *this into it (deep copy).
virtual void MvTimesMatAddMv(ScalarType alpha, const MultiVec< ScalarType > &A, const Teuchos::SerialDenseMatrix< int, ScalarType > &B, ScalarType beta)=0
Update *this with alpha * A * B + beta * (*this).
virtual ~MultiVec()
Destructor (virtual for memory safety of derived classes).
virtual MultiVec< ScalarType > * Clone(const int numvecs) const =0
Create a new MultiVec with numvecs columns.
static Teuchos::RCP< MultiVec< ScalarType > > CloneCopy(const MultiVec< ScalarType > &mv, const std::vector< int > &index)
Creates a new Anasazi::MultiVec and copies the selected contents of mv into the new vector (deep copy...
static void MvScale(MultiVec< ScalarType > &mv, const std::vector< ScalarType > &alpha)
Scale each element of the i-th vector in *this with alpha[i].
Interface for multivectors used by Anasazi&#39;s linear solvers.
static ptrdiff_t GetGlobalLength(const MultiVec< ScalarType > &mv)
Obtain the vector length of mv.