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., 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 #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 int GetVecLength () const = 0;
129 
132  virtual ptrdiff_t GetGlobalLength () const { return static_cast<ptrdiff_t>( this->GetVecLength() ); }
133 
135  virtual int GetNumberVecs () const = 0;
136 
138 
140 
142  virtual void
143  MvTimesMatAddMv (ScalarType alpha,
144  const MultiVec<ScalarType>& A,
145  const Teuchos::SerialDenseMatrix<int,ScalarType>& B, ScalarType beta) = 0;
146 
148  virtual void MvAddMv ( ScalarType alpha, const MultiVec<ScalarType>& A, ScalarType beta, const MultiVec<ScalarType>& B ) = 0;
149 
151  virtual void MvScale ( ScalarType alpha ) = 0;
152 
154  virtual void MvScale ( const std::vector<ScalarType>& alpha ) = 0;
155 
159  virtual void MvTransMv ( ScalarType alpha, const MultiVec<ScalarType>& A, Teuchos::SerialDenseMatrix<int,ScalarType>& B
160 #ifdef HAVE_ANASAZI_EXPERIMENTAL
161  , ConjType conj = Anasazi::CONJ
162 #endif
163  ) const = 0;
164 
170  virtual void MvDot ( const MultiVec<ScalarType>& A, std::vector<ScalarType> & b
171 #ifdef HAVE_ANASAZI_EXPERIMENTAL
172  , ConjType conj = Anasazi::CONJ
173 #endif
174  ) const = 0;
175 
177 
179 
184  virtual void MvNorm ( std::vector<typename Teuchos::ScalarTraits<ScalarType>::magnitudeType> & normvec ) const = 0;
185 
187 
189 
194  virtual void SetBlock ( const MultiVec<ScalarType>& A, const std::vector<int>& index ) = 0;
195 
197  virtual void MvRandom () = 0;
198 
200  virtual void MvInit ( ScalarType alpha ) = 0;
201 
203 
205 
207  virtual void MvPrint ( std::ostream& os ) const = 0;
209 
210 #ifdef HAVE_ANASAZI_TSQR
211 
213 
236  virtual void
237  factorExplicit (MultiVec<ScalarType>& Q,
239  const bool forceNonnegativeDiagonal=false)
240  {
241  TEUCHOS_TEST_FOR_EXCEPTION(true, std::logic_error, "The Anasazi::MultiVec<"
242  << Teuchos::TypeNameTraits<ScalarType>::name() << "> subclass which you "
243  "are using does not implement the TSQR-related method factorExplicit().");
244  }
245 
280  virtual int
283  {
284  TEUCHOS_TEST_FOR_EXCEPTION(true, std::logic_error, "The Anasazi::MultiVec<"
285  << Teuchos::TypeNameTraits<ScalarType>::name() << "> subclass which you "
286  "are using does not implement the TSQR-related method revealRank().");
287  }
288 
290 #endif // HAVE_ANASAZI_TSQR
291 };
292 
293 namespace details {
311 template<class ScalarType>
313 public:
314  typedef MultiVec<ScalarType> MV;
315  typedef ScalarType scalar_type;
316  typedef int ordinal_type; // This doesn't matter either
317  typedef int node_type; // Nor does this
319  typedef typename Teuchos::ScalarTraits<scalar_type>::magnitudeType magnitude_type;
320 
322  void
324  MV& Q,
325  dense_matrix_type& R,
326  const bool forceNonnegativeDiagonal=false)
327  {
328  A.factorExplicit (Q, R, forceNonnegativeDiagonal);
329  }
330 
332  int
333  revealRank (MV& Q,
334  dense_matrix_type& R,
335  const magnitude_type& tol)
336  {
337  return Q.revealRank (R, tol);
338  }
339 };
340 } // namespace details
341 
351  template<class ScalarType>
352  class MultiVecTraits<ScalarType,MultiVec<ScalarType> > {
353  public:
355 
356 
360  Clone (const MultiVec<ScalarType>& mv, const int numvecs) {
361  return Teuchos::rcp (const_cast<MultiVec<ScalarType>&> (mv).Clone (numvecs));
362  }
363 
369  { return Teuchos::rcp( const_cast<MultiVec<ScalarType>&>(mv).CloneCopy() ); }
370 
376  static Teuchos::RCP<MultiVec<ScalarType> > CloneCopy( const MultiVec<ScalarType>& mv, const std::vector<int>& index )
377  { return Teuchos::rcp( const_cast<MultiVec<ScalarType>&>(mv).CloneCopy(index) ); }
378 
384  static Teuchos::RCP<MultiVec<ScalarType> > CloneViewNonConst( MultiVec<ScalarType>& mv, const std::vector<int>& index )
385  { return Teuchos::rcp( mv.CloneViewNonConst(index) ); }
386 
392  static Teuchos::RCP<const MultiVec<ScalarType> > CloneView( const MultiVec<ScalarType>& mv, const std::vector<int>& index )
393  { return Teuchos::rcp( const_cast<MultiVec<ScalarType>&>(mv).CloneView(index) ); }
394 
396 
398 
399 
401  static int GetVecLength( const MultiVec<ScalarType>& mv )
402  { return mv.GetVecLength(); }
403 
405  static int GetNumberVecs( const MultiVec<ScalarType>& mv )
406  { return mv.GetNumberVecs(); }
407 
409 
411 
412 
415  static void MvTimesMatAddMv( ScalarType alpha, const MultiVec<ScalarType>& A,
417  ScalarType beta, MultiVec<ScalarType>& mv )
418  { mv.MvTimesMatAddMv(alpha, A, B, beta); }
419 
422  static void MvAddMv( ScalarType alpha, const MultiVec<ScalarType>& A, ScalarType beta, const MultiVec<ScalarType>& B, MultiVec<ScalarType>& mv )
423  { mv.MvAddMv(alpha, A, beta, B); }
424 
428 #ifdef HAVE_ANASAZI_EXPERIMENTAL
429  , ConjType conj = Anasazi::CONJ
430 #endif
431  )
432  { mv.MvTransMv(alpha, A, B
433 #ifdef HAVE_ANASAZI_EXPERIMENTAL
434  , conj
435 #endif
436  ); }
437 
440  static void MvDot( const MultiVec<ScalarType>& mv, const MultiVec<ScalarType>& A, std::vector<ScalarType> & b
441 #ifdef HAVE_ANASAZI_EXPERIMENTAL
442  , ConjType conj = Anasazi::CONJ
443 #endif
444  )
445  { mv.MvDot( A, b
446 #ifdef HAVE_ANASAZI_EXPERIMENTAL
447  , conj
448 #endif
449  ); }
450 
452  static void MvScale ( MultiVec<ScalarType>& mv, ScalarType alpha )
453  { mv.MvScale( alpha ); }
454 
456  static void MvScale ( MultiVec<ScalarType>& mv, const std::vector<ScalarType>& alpha )
457  { mv.MvScale( alpha ); }
458 
460 
462 
466  static void MvNorm( const MultiVec<ScalarType>& mv, std::vector<typename Teuchos::ScalarTraits<ScalarType>::magnitudeType> & normvec )
467  { mv.MvNorm(normvec); }
468 
470 
472 
477  static void SetBlock( const MultiVec<ScalarType>& A, const std::vector<int>& index, MultiVec<ScalarType>& mv )
478  { mv.SetBlock(A, index); }
479 
482  static void MvRandom( MultiVec<ScalarType>& mv )
483  { mv.MvRandom(); }
484 
487  static void MvInit( MultiVec<ScalarType>& mv, ScalarType alpha = Teuchos::ScalarTraits<ScalarType>::zero() )
488  { mv.MvInit(alpha); }
489 
491 
493 
495  static void MvPrint( const MultiVec<ScalarType>& mv, std::ostream& os )
496  { mv.MvPrint(os); }
497 
499 
500 #ifdef HAVE_ANASAZI_TSQR
501  typedef details::MultiVecTsqrAdapter<ScalarType> tsqr_adaptor_type;
509 #endif // HAVE_ANASAZI_TSQR
510  };
511 
528  template<class ScalarType>
529  class MultiVecTraitsExt<ScalarType, MultiVec<ScalarType> > {
530  public:
532 
533 
536  static ptrdiff_t GetGlobalLength( const MultiVec<ScalarType>& mv )
537  { return mv.GetGlobalLength(); }
538 
540  };
541 
542 } // namespace Anasazi
543 
544 #endif
545 
546 // 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.
An extension of the MultiVecTraits class that adds a new vector length method.This traits class provi...
virtual int GetNumberVecs() const =0
The number of vectors (i.e., columns) in the multivector.
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 MvAddMv(ScalarType alpha, const MultiVec< ScalarType > &A, ScalarType beta, const MultiVec< ScalarType > &B)=0
Replace *this with alpha * A + beta * B.
virtual MultiVec< ScalarType > * CloneCopy() const =0
Create a new MultiVec and copy contents of *this into it (deep copy).
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 ...
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 ptrdiff_t GetGlobalLength(const MultiVec< ScalarType > &mv)
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
virtual MultiVec< ScalarType > * Clone(const int numvecs) const =0
Create a new MultiVec with numvecs columns.
static Teuchos::RCP< MultiVec< ScalarType > > Clone(const MultiVec< ScalarType > &mv, const int numvecs)
Create a new empty MultiVec containing numvecs columns.
static int GetVecLength(const MultiVec< ScalarType > &mv)
Obtain the vector length of mv.
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 ptrdiff_t GetGlobalLength() const
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().
virtual int GetVecLength() const =0
The number of rows in the multivector.
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 ...
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).
virtual void MvPrint(std::ostream &os) const =0
Print *this multivector to the os output stream.
ConjType
Enumerated types used to specify conjugation arguments.
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
MultiVec()
Default constructor.
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.
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.
virtual void MvNorm(std::vector< typename Teuchos::ScalarTraits< ScalarType >::magnitudeType > &normvec) const =0
Compute the 2-norm of each vector in *this.
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 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).
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...
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).
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.