Stokhos Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Teuchos_SerialQRDenseSolver_UQ_PCE.hpp
Go to the documentation of this file.
1 // @HEADER
2 // *****************************************************************************
3 // Stokhos Package
4 //
5 // Copyright 2009 NTESS and the Stokhos contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9 
10 #ifndef _TEUCHOS_SERIALQRDENSESOLVER_UQ_PCE_HPP_
11 #define _TEUCHOS_SERIALQRDENSESOLVER_UQ_PCE_HPP_
12 
16 #include <new>
18 
19 #include "Sacado_UQ_PCE.hpp"
20 
25 namespace Teuchos {
26 
27  template<typename OrdinalType, typename Storage>
28  class SerialQRDenseSolver<OrdinalType, Sacado::UQ::PCE<Storage> >
29  : public CompObject,
30  public Object
31  {
32 
33  public:
34 
37 
39 
42 
44  virtual ~SerialQRDenseSolver() {}
46 
48 
49 
51 
54 
56 
62 
64 
65 
67 
69  void factorWithEquilibration(bool flag) {
70  base_QR_.factorWithEquilibration(flag);
71  }
72 
74  void solveWithTranspose(bool flag) {
75  base_QR_.solveWithTranspose(flag);
76  }
77 
80  base_QR_.solveWithTranspose(trans);
81  }
82 
84 
86 
87 
89 
92  int factor() { return base_QR_.factor(); }
93 
95 
98  int solve() { return base_QR_.solve(); }
99 
101 
105  return base_QR_.computeEquilibrateScaling();
106  }
107 
109 
113  int equilibrateMatrix() { return base_QR_.equilibrateMatrix(); }
114 
116 
120  int equilibrateRHS() { return base_QR_.equilibrateRHS(); }
121 
123 
127  int unequilibrateLHS() { return base_QR_.unequilibrateLHS(); }
128 
130 
133  int formQ();
134 
136 
139  int formR();
140 
142 
146 
148 
153 
155 
156 
158  bool transpose() { return base_QR_.transpose(); }
159 
161  bool factored() { return base_QR_.factored(); }
162 
164  bool equilibratedA() { return base_QR_.equilibratedA(); }
165 
167  bool equilibratedB() { return base_QR_.equilibratedB(); }
168 
170  bool shouldEquilibrate() { return base_QR_.shouldEquilibrate(); }
171 
173  bool solved() { return base_QR_.solved(); }
174 
176  bool formedQ() { return base_QR_.formedQ(); }
177 
179  bool formedR() { return base_QR_.formedR();}
180 
182 
184 
185 
188 
191 
194 
197 
200 
203 
205  OrdinalType numRows() const {return(M_);}
206 
208  OrdinalType numCols() const {return(N_);}
209 
211  std::vector<ScalarType> tau() const { return base_QR_.tau(); }
212 
214  MagnitudeType ANORM() const { return base_QR_.ANORM(); }
215 
217 
219 
220  void Print(std::ostream& os) const;
223  protected:
224 
229 
230  void resetMatrix();
231  void resetVectors();
232  RCP<BaseMatrixType> createBaseMatrix(const RCP<MatrixType>& mat) const;
233  RCP<MatrixType> createMatrix(const RCP<BaseMatrixType>& base_mat) const;
235 
236  OrdinalType M_;
237  OrdinalType N_;
238  OrdinalType SacadoSize_;
239 
246 
253 
254  private:
255 
256  // SerialQRDenseSolver copy constructor (put here because we don't want user access)
259 
260  };
261 
262  namespace details {
263 
264  // Helper traits class for converting between arrays of
265  // Sacado::UQ::PCE and its corresponding value type
266  template <typename Storage> struct PCEArrayHelper;
267 
268  template <typename Ordinal, typename Value, typename Device>
269  struct PCEArrayHelper< Stokhos::DynamicStorage<Ordinal,Value,Device> >
270  {
273  typedef typename pce_type::cijk_type cijk_type;
274 
275  static Value* getValueArray(pce_type* v, Ordinal len) {
276  if (len == 0)
277  return 0;
278  return v[0].coeff(); // Assume data is contiguous
279  }
280 
281  static pce_type* getPCEArray(Value *v, Ordinal len, Ordinal vector_size){
282  if (len == 0)
283  return 0;
284  pce_type* v_pce =
285  static_cast<pce_type*>(operator new(len*sizeof(pce_type)));
286  cijk_type cijk = Kokkos::getGlobalCijkTensor<cijk_type>();
287  for (Ordinal i=0; i<len; ++i)
288  new (v_pce+i) pce_type(cijk, vector_size, v+i*vector_size, false);
289  return v_pce;
290  }
291 
292  };
293 
294  }
295 
296  // Helper traits to distinguish work arrays for real and complex-valued datatypes.
297  using namespace details;
298 
299 //=============================================================================
300 
301 template<typename OrdinalType, typename Storage>
302 SerialQRDenseSolver<OrdinalType,Sacado::UQ::PCE<Storage> >::
303 SerialQRDenseSolver()
304  : CompObject(),
305  Object("Teuchos::SerialQRDenseSolver"),
306  M_(0),
307  N_(0)
308 {
309  resetMatrix();
310 }
311 
312 //=============================================================================
313 
314 template<typename OrdinalType, typename Storage>
316 resetVectors()
317 {
318  LHS_ = Teuchos::null;
319  RHS_ = Teuchos::null;
320 }
321 //=============================================================================
322 
323 template<typename OrdinalType, typename Storage>
325 resetMatrix()
326 {
327  resetVectors();
328  M_ = 0;
329  N_ = 0;
330 }
331 //=============================================================================
332 
333 template<typename OrdinalType, typename Storage>
336 createBaseMatrix(
338 {
339  BaseScalarType* base_ptr =
341  mat->values(), mat->stride()*mat->numCols());
342  RCP<BaseMatrixType> base_mat =
344  base_ptr,
345  mat->stride()*SacadoSize_,
346  mat->numRows()*SacadoSize_,
347  mat->numCols()));
348  return base_mat;
349 }
350 //=============================================================================
351 
352 template<typename OrdinalType, typename Storage>
355 createMatrix(
356  const RCP<SerialDenseMatrix<OrdinalType,BaseScalarType> >& base_mat) const
357 {
358  ScalarType* ptr =
360  base_mat->values(), base_mat->stride()*base_mat->numCols(), SacadoSize_);
361  RCP<MatrixType> mat =
363  ptr,
364  base_mat->stride()/SacadoSize_,
365  base_mat->numRows()/SacadoSize_,
366  base_mat->numCols()));
367  return mat;
368 }
369 //=============================================================================
370 
371 template<typename OrdinalType, typename Storage>
374 {
376  A->numRows() < A->numCols(), std::invalid_argument,
377  "SerialQRDenseSolver<T>::setMatrix: the matrix A must have A.numRows() >= A.numCols()!");
378 
379  resetMatrix();
380  Matrix_ = A;
381  Factor_ = A;
382  FactorQ_ = A;
383  FactorR_ = A;
384  M_ = A->numRows();
385  N_ = A->numCols();
386  if (Storage::is_static)
387  SacadoSize_ = Storage::static_size;
388  else if (M_ > 0 && N_ > 0)
389  SacadoSize_ = (*A)(0,0).size();
390  else
391  SacadoSize_ = 0;
392 
393  return base_QR_.setMatrix( createBaseMatrix(A) );
394 }
395 //=============================================================================
396 
397 template<typename OrdinalType, typename Storage>
399 setVectors(
402 {
403  // Check that these new vectors are consistent
405  B->numCols() != X->numCols(), std::invalid_argument,
406  "SerialQRDenseSolver<T>::setVectors: X and B have different numbers of columns!");
408  B->values()==0, std::invalid_argument,
409  "SerialQRDenseSolver<T>::setVectors: B is an empty SerialDenseMatrix<T>!");
411  X->values()==0, std::invalid_argument,
412  "SerialQRDenseSolver<T>::setVectors: X is an empty SerialDenseMatrix<T>!");
414  B->stride()<1, std::invalid_argument,
415  "SerialQRDenseSolver<T>::setVectors: B has an invalid stride!");
417  X->stride()<1, std::invalid_argument,
418  "SerialQRDenseSolver<T>::setVectors: X has an invalid stride!");
419 
420  resetVectors();
421  LHS_ = X;
422  RHS_ = B;
423 
424  return base_QR_.setVectors( createBaseMatrix(X), createBaseMatrix(B) );
425 }
426 
427 //=============================================================================
428 
429 template<typename OrdinalType, typename Storage>
431 formQ() {
432  int ret = base_QR_.formQ();
433  FactorQ_ = createMatrix( base_QR_.getQ() );
434  return(ret);
435 }
436 
437 //=============================================================================
438 
439 template<typename OrdinalType, typename Storage>
441 formR() {
442  int ret = base_QR_.formR();
443  FactorR_ = createMatrix( base_QR_.getR() );
444  Factor_ = createMatrix( base_QR_.getFactoredMatrix() );
445  return(ret);
446 }
447 
448 //=============================================================================
449 
450 template<typename OrdinalType, typename Storage>
453 {
454  return base_QR_.multiplyQ(transq, createBaseMatrix(C));
455 }
456 
457 //=============================================================================
458 
459 template<typename OrdinalType, typename Storage>
462 {
463  return base_QR_.solveR(transr, createBaseMatrix(C));
464 }
465 
466 //=============================================================================
467 
468 template<typename OrdinalType, typename Storage>
470 Print(std::ostream& os) const {
471 
472  if (Matrix_!=Teuchos::null) os << "Solver Matrix" << std::endl << *Matrix_ << std::endl;
473  if (Factor_!=Teuchos::null) os << "Solver Factored Matrix" << std::endl << *Factor_ << std::endl;
474  if (LHS_ !=Teuchos::null) os << "Solver LHS" << std::endl << *LHS_ << std::endl;
475  if (RHS_ !=Teuchos::null) os << "Solver RHS" << std::endl << *RHS_ << std::endl;
476 
477 }
478 
479 } // namespace Teuchos
480 
481 #endif /* _TEUCHOS_SERIALQRDENSESOLVER_UQ_PCE_HPP_ */
RCP< SerialDenseMatrix< OrdinalType, ScalarType > > FactorR_
RCP< SerialDenseMatrix< OrdinalType, ScalarType > > getLHS() const
Returns pointer to current LHS.
int setMatrix(const RCP< SerialDenseMatrix< OrdinalType, ScalarType > > &A)
RCP< SerialDenseMatrix< OrdinalType, ScalarType > > getR() const
Returns pointer to R (assuming factorization has been performed).
bool equilibratedA()
Returns true if factor is equilibrated (factor available via getFactoredMatrix()).
Sacado::ETPCE::OrthogPoly< double, Stokhos::StandardStorage< int, double > > pce_type
int multiplyQ(ETransp transq, SerialDenseMatrix< OrdinalType, ScalarType > &C)
int solveR(ETransp transr, SerialDenseMatrix< OrdinalType, ScalarType > &C)
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
int factor()
Computes the in-place QR factorization of the matrix using the LAPACK routine _GETRF or the Eigen cla...
std::vector< ScalarType > tau() const
Returns pointer to pivot vector (if factorization has been computed), zero otherwise.
RCP< SerialDenseMatrix< OrdinalType, ScalarType > > LHS_
bool transpose()
Returns true if adjoint of this matrix has and will be used.
SerialQRDenseSolver & operator=(const SerialQRDenseSolver< OrdinalType, ScalarType > &Source)
Templated class for solving dense linear problems.
bool solved()
Returns true if the current set of vectors has been solved.
bool factored()
Returns true if matrix is factored (factor available via getFactoredMatrix()).
MagnitudeType ANORM() const
Returns the absolute value of the largest element of this matrix (returns -1 if not yet computed)...
Specialization for Sacado::UQ::PCE&lt; Storage&lt;...&gt; &gt;
RCP< SerialDenseMatrix< OrdinalType, ScalarType > > Matrix_
int unequilibrateLHS()
Unscales the solution vectors if equilibration was used to solve the system.
RCP< SerialDenseMatrix< OrdinalType, ScalarType > > getRHS() const
Returns pointer to current RHS.
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
bool equilibratedB()
Returns true if RHS is equilibrated (RHS available via getRHS()).
void factorWithEquilibration(bool flag)
Causes equilibration to be called just before the matrix factorization as part of the call to factor...
RCP< SerialDenseMatrix< OrdinalType, ScalarType > > FactorQ_
void Print(std::ostream &os) const
RCP< SerialDenseMatrix< OrdinalType, ScalarType > > Factor_
RCP< SerialDenseMatrix< OrdinalType, ScalarType > > getMatrix() const
Returns pointer to current matrix.
RCP< SerialDenseMatrix< OrdinalType, ScalarType > > RHS_
RCP< SerialDenseMatrix< OrdinalType, ScalarType > > getQ() const
Returns pointer to Q (assuming factorization has been performed).
KOKKOS_INLINE_FUNCTION constexpr std::enable_if< is_view_uq_pce< view_type >::value, typename CijkType< view_type >::type >::type cijk(const view_type &view)
int solve()
Computes the solution X to AX = B for the this matrix and the B provided to SetVectors()..
void solveWithTranspose(bool flag)
If flag is true, causes all subsequent function calls to work with the adjoint of this matrix...
int setVectors(const RCP< SerialDenseMatrix< OrdinalType, ScalarType > > &X, const RCP< SerialDenseMatrix< OrdinalType, ScalarType > > &B)
bool shouldEquilibrate()
Returns true if the LAPACK general rules for equilibration suggest you should equilibrate the system...
RCP< SerialDenseMatrix< OrdinalType, ScalarType > > getFactoredMatrix() const
Returns pointer to factored matrix (assuming factorization has been performed).
void solveWithTransposeFlag(Teuchos::ETransp trans)
All subsequent function calls will work with the transpose-type set by this method (Teuchos::NO_TRANS...