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 //
4 // Stokhos Package
5 // Copyright (2009) 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 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are
12 // met:
13 //
14 // 1. Redistributions of source code must retain the above copyright
15 // notice, this list of conditions and the following disclaimer.
16 //
17 // 2. Redistributions in binary form must reproduce the above copyright
18 // notice, this list of conditions and the following disclaimer in the
19 // documentation and/or other materials provided with the distribution.
20 //
21 // 3. Neither the name of the Corporation nor the names of the
22 // contributors may be used to endorse or promote products derived from
23 // this software without specific prior written permission.
24 //
25 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
26 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
29 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 //
37 // Questions? Contact Eric T. Phipps (etphipp@sandia.gov).
38 //
39 // ***********************************************************************
40 // @HEADER
41 
42 #ifndef STOKHOS_ORTHOGONALIZATION_FACTORY_HPP
43 #define STOKHOS_ORTHOGONALIZATION_FACTORY_HPP
44 
45 #include <string>
46 #include "Teuchos_Array.hpp"
48 #include "Stokhos_SDMUtils.hpp"
49 
50 namespace Stokhos {
51 
55  template <typename ordinal_type, typename value_type>
57  public:
58 
60 
64 
67 
69 
71  static ordinal_type
72  createOrthogonalBasis(const std::string& method, value_type threshold,
73  bool verbose, const SDM& A,
75  SDM& Q, SDM& R,
77 
78  ordinal_type m = A.numRows();
79  ordinal_type n = A.numCols();
80  ordinal_type rank = std::min(m,n);
81 
82  if (method == "SVD") { // A = U*diag(sigma)*V^T, Q = U, R = sigma*V^T
84  SDM Vt;
85  rank = svd_threshold(threshold, A, sigma, Q, Vt);
86  R.reshape(rank, Vt.numCols());
87  for (ordinal_type j=0; j<Vt.numCols(); j++)
88  for (ordinal_type i=0; i<rank; i++)
89  R(i,j) = sigma[i]*Vt(i,j);
90  piv.resize(n);
91  for (int i=0; i<n; i++)
92  piv[i] = i;
93 
94  if (verbose) {
95  // std::cout << "diag(sigma) = [ ";
96  // for (ordinal_type i=0; i<rank; i++)
97  // std::cout << sigma[i] << " ";
98  // std::cout << "]" << std::endl;
99 
100  std::cout << "rank = " << rank << std::endl;
101  }
102  }
103 
104  else { // All QR-based methods
105 
106  if (method == "Householder")
107  rank = CPQR_Householder_threshold(threshold, A, w, Q, R, piv);
108 
109  else if (method == "Householder without Pivoting") {
110  QR_Householder(rank, A, w, Q, R);
111  piv.resize(n);
112  for (int i=0; i<n; i++)
113  piv[i] = i;
114  }
115 
116  else if (method == "Modified Gram-Schmidt")
117  rank = CPQR_MGS_threshold(threshold, A, w, Q, R, piv);
118 
119  else if (method == "Modified Gram-Schmidt with Reorthogonalization")
120  rank = CPQR_MGS_reorthog_threshold(threshold, A, w, Q, R, piv);
121 
122  else if (method == "Modified Gram-Schmidt without Pivoting") {
123  QR_MGS(rank, A, w, Q, R);
124  piv.resize(n);
125  for (int i=0; i<n; i++)
126  piv[i] = i;
127  }
128 
129  else if (method == "Modified Gram-Schmidt without Pivoting with Reorthogonalization") {
130  QR_MGS2(rank, A, w, Q, R);
131  piv.resize(n);
132  for (int i=0; i<n; i++)
133  piv[i] = i;
134  }
135 
136  else
138  true, std::logic_error,
139  "Invalid orthogonalization method " << method);
140 
141  if (verbose) {
142  // std::cout << "piv = [";
143  // for (ordinal_type i=0; i<rank; i++)
144  // std::cout << piv[i] << " ";
145  // std::cout << "]" << std::endl;
146 
147  // std::cout << "diag(R) = [ ";
148  // for (ordinal_type i=0; i<rank; i++)
149  // std::cout << R(i,i) << " ";
150  // std::cout << "]" << std::endl;
151 
152  std::cout << "rank = " << rank << std::endl;
153 
154  // Check A*P = Q*R
155  std::cout << "||A*P-Q*R||_infty = "
156  << Stokhos::residualCPQRError(A,Q,R,piv) << std::endl;
157 
158  // Check Q^T*diag(w)*Q = I
159  std::cout << "||I - Q^T*diag(w)**Q||_infty = "
160  << weightedQROrthogonalizationError(Q,w) << std::endl;
161  }
162  }
163 
164  return rank;
165  }
166 
167  private:
168 
169  // Prohibit copying
171 
172  // Prohibit Assignment
174 
175  }; // class OrthogonalizationFactory
176 
177 } // Namespace Stokhos
178 
179 #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.