Stokhos Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Stokhos_OrthogonalizationFactory.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 STOKHOS_ORTHOGONALIZATION_FACTORY_HPP
11 #define STOKHOS_ORTHOGONALIZATION_FACTORY_HPP
12 
13 #include <string>
14 #include "Teuchos_Array.hpp"
16 #include "Stokhos_SDMUtils.hpp"
17 
18 namespace Stokhos {
19 
23  template <typename ordinal_type, typename value_type>
25  public:
26 
28 
32 
35 
37 
39  static ordinal_type
40  createOrthogonalBasis(const std::string& method, value_type threshold,
41  bool verbose, const SDM& A,
43  SDM& Q, SDM& R,
45 
46  ordinal_type m = A.numRows();
47  ordinal_type n = A.numCols();
48  ordinal_type rank = std::min(m,n);
49 
50  if (method == "SVD") { // A = U*diag(sigma)*V^T, Q = U, R = sigma*V^T
52  SDM Vt;
53  rank = svd_threshold(threshold, A, sigma, Q, Vt);
54  R.reshape(rank, Vt.numCols());
55  for (ordinal_type j=0; j<Vt.numCols(); j++)
56  for (ordinal_type i=0; i<rank; i++)
57  R(i,j) = sigma[i]*Vt(i,j);
58  piv.resize(n);
59  for (int i=0; i<n; i++)
60  piv[i] = i;
61 
62  if (verbose) {
63  // std::cout << "diag(sigma) = [ ";
64  // for (ordinal_type i=0; i<rank; i++)
65  // std::cout << sigma[i] << " ";
66  // std::cout << "]" << std::endl;
67 
68  std::cout << "rank = " << rank << std::endl;
69  }
70  }
71 
72  else { // All QR-based methods
73 
74  if (method == "Householder")
75  rank = CPQR_Householder_threshold(threshold, A, w, Q, R, piv);
76 
77  else if (method == "Householder without Pivoting") {
78  QR_Householder(rank, A, w, Q, R);
79  piv.resize(n);
80  for (int i=0; i<n; i++)
81  piv[i] = i;
82  }
83 
84  else if (method == "Modified Gram-Schmidt")
85  rank = CPQR_MGS_threshold(threshold, A, w, Q, R, piv);
86 
87  else if (method == "Modified Gram-Schmidt with Reorthogonalization")
88  rank = CPQR_MGS_reorthog_threshold(threshold, A, w, Q, R, piv);
89 
90  else if (method == "Modified Gram-Schmidt without Pivoting") {
91  QR_MGS(rank, A, w, Q, R);
92  piv.resize(n);
93  for (int i=0; i<n; i++)
94  piv[i] = i;
95  }
96 
97  else if (method == "Modified Gram-Schmidt without Pivoting with Reorthogonalization") {
98  QR_MGS2(rank, A, w, Q, R);
99  piv.resize(n);
100  for (int i=0; i<n; i++)
101  piv[i] = i;
102  }
103 
104  else
106  true, std::logic_error,
107  "Invalid orthogonalization method " << method);
108 
109  if (verbose) {
110  // std::cout << "piv = [";
111  // for (ordinal_type i=0; i<rank; i++)
112  // std::cout << piv[i] << " ";
113  // std::cout << "]" << std::endl;
114 
115  // std::cout << "diag(R) = [ ";
116  // for (ordinal_type i=0; i<rank; i++)
117  // std::cout << R(i,i) << " ";
118  // std::cout << "]" << std::endl;
119 
120  std::cout << "rank = " << rank << std::endl;
121 
122  // Check A*P = Q*R
123  std::cout << "||A*P-Q*R||_infty = "
124  << Stokhos::residualCPQRError(A,Q,R,piv) << std::endl;
125 
126  // Check Q^T*diag(w)*Q = I
127  std::cout << "||I - Q^T*diag(w)**Q||_infty = "
128  << weightedQROrthogonalizationError(Q,w) << std::endl;
129  }
130  }
131 
132  return rank;
133  }
134 
135  private:
136 
137  // Prohibit copying
139 
140  // Prohibit Assignment
142 
143  }; // class OrthogonalizationFactory
144 
145 } // Namespace Stokhos
146 
147 #endif
ordinal_type CPQR_MGS_threshold(const scalar_type &rank_threshold, const Teuchos::SerialDenseMatrix< ordinal_type, scalar_type > &A, const Teuchos::Array< scalar_type > &w, Teuchos::SerialDenseMatrix< ordinal_type, scalar_type > &Q, Teuchos::SerialDenseMatrix< ordinal_type, scalar_type > &R, Teuchos::Array< ordinal_type > &piv)
Compute column-pivoted QR using modified Gram-Schmidt.
ordinal_type CPQR_Householder_threshold(const scalar_type &rank_threshold, const Teuchos::SerialDenseMatrix< ordinal_type, scalar_type > &A, const Teuchos::Array< scalar_type > &w, Teuchos::SerialDenseMatrix< ordinal_type, scalar_type > &Q, Teuchos::SerialDenseMatrix< ordinal_type, scalar_type > &R, Teuchos::Array< ordinal_type > &piv)
Compute column-pivoted QR using Householder reflections.
void QR_MGS(ordinal_type k, const Teuchos::SerialDenseMatrix< ordinal_type, scalar_type > &A, const Teuchos::Array< scalar_type > &w, Teuchos::SerialDenseMatrix< ordinal_type, scalar_type > &Q, Teuchos::SerialDenseMatrix< ordinal_type, scalar_type > &R)
Compute thin QR using modified Gram-Schmidt.
Teuchos::SerialDenseMatrix< ordinal_type, value_type > SDM
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
ordinal_type svd_threshold(const scalar_type &rank_threshold, const Teuchos::SerialDenseMatrix< ordinal_type, scalar_type > &A, Teuchos::Array< scalar_type > &s, Teuchos::SerialDenseMatrix< ordinal_type, scalar_type > &U, Teuchos::SerialDenseMatrix< ordinal_type, scalar_type > &Vt)
KOKKOS_INLINE_FUNCTION PCE< Storage > min(const typename PCE< Storage >::value_type &a, const PCE< Storage > &b)
scalar_type residualCPQRError(const Teuchos::SerialDenseMatrix< ordinal_type, scalar_type > &A, const Teuchos::SerialDenseMatrix< ordinal_type, scalar_type > &Q, const Teuchos::SerialDenseMatrix< ordinal_type, scalar_type > &R, const Teuchos::Array< ordinal_type > &piv)
Compute column-pivoted QR residual error.
scalar_type weightedQROrthogonalizationError(const Teuchos::SerialDenseMatrix< ordinal_type, scalar_type > &Q, const Teuchos::Array< scalar_type > &w)
Compute weighted QR orthogonalization error.
OrthogonalizationFactory & operator=(const OrthogonalizationFactory &)
void resize(size_type new_size, const value_type &x=value_type())
OrdinalType numCols() const
void QR_MGS2(ordinal_type k, const Teuchos::SerialDenseMatrix< ordinal_type, scalar_type > &A, const Teuchos::Array< scalar_type > &w, Teuchos::SerialDenseMatrix< ordinal_type, scalar_type > &Q, Teuchos::SerialDenseMatrix< ordinal_type, scalar_type > &R)
Compute thin QR using modified Gram-Schmidt with reorthogonalization.
int reshape(OrdinalType numRows, OrdinalType numCols)
static ordinal_type createOrthogonalBasis(const std::string &method, value_type threshold, bool verbose, const SDM &A, const Teuchos::Array< value_type > &w, SDM &Q, SDM &R, Teuchos::Array< ordinal_type > &piv)
Create orthogonal basis via the method indicated by method.
void QR_Householder(ordinal_type k, const Teuchos::SerialDenseMatrix< ordinal_type, scalar_type > &A, const Teuchos::Array< scalar_type > &w, Teuchos::SerialDenseMatrix< ordinal_type, scalar_type > &Q, Teuchos::SerialDenseMatrix< ordinal_type, scalar_type > &R)
Compute thin QR using Householder reflections.
int n
ordinal_type CPQR_MGS_reorthog_threshold(const scalar_type &rank_threshold, const Teuchos::SerialDenseMatrix< ordinal_type, scalar_type > &A, const Teuchos::Array< scalar_type > &w, Teuchos::SerialDenseMatrix< ordinal_type, scalar_type > &Q, Teuchos::SerialDenseMatrix< ordinal_type, scalar_type > &R, Teuchos::Array< ordinal_type > &piv)
Compute column-pivoted QR using modified Gram-Schmidt and reorthogonalization.
OrdinalType numRows() const
Encapsulate various orthogonalization (ie QR) methods.