Stokhos Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Stokhos_SDMUtilsUnitTest.cpp
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 
14 
15 #include "Stokhos_SDMUtils.hpp"
17 
18 // Need to test pre-pivot
19 
20 namespace SDMUtilsUnitTest {
21 
22  typedef int ordinal_type;
23  typedef double scalar_type;
25  typedef void (*qr_func_type)(ordinal_type, const SDM&, const Teuchos::Array<scalar_type>&, SDM&, SDM&);
26  typedef void (*cpqr_func_type)(const SDM&, SDM&, SDM&, Teuchos::Array<ordinal_type>&);
28 
29  scalar_type rtol = 1.0e-11;
30  scalar_type atol = 1.0e-11;
31 
33  ordinal_type m = A.numRows();
34  ordinal_type n = A.numCols();
35  ordinal_type k = std::min(m,n);
36  SDM B(m,m), C(n,n), S(m,n), T(m,n);
37  B.random();
38  C.random();
39  S.putScalar(0.0);
40  if (rank > k)
41  rank = k;
42  for (ordinal_type i=0; i<rank; i++)
43  S(i,i) = 1.0;
46  }
47 
50  Teuchos::FancyOStream& out) {
51  bool success;
52 
53  SDM A(m,n);
54  A.random();
55  SDM Q, R;
57  ordinal_type k = std::min(m,n);
58  qr_func(k, A, w, Q, R);
59 
60  TEUCHOS_TEST_EQUALITY(Q.numRows(), m, out, success);
61  TEUCHOS_TEST_EQUALITY(Q.numCols(), k, out, success);
62  TEUCHOS_TEST_EQUALITY(R.numRows(), k, out, success);
63  TEUCHOS_TEST_EQUALITY(R.numCols(), k, out, success);
64 
65  // Test A = Q*R
66  SDM QR(m,k);
67  QR.multiply(Teuchos::NO_TRANS, Teuchos::NO_TRANS, 1.0, Q, R, 0.0);
68  SDM AA(Teuchos::View, A, m, k);
69  success = Stokhos::compareSDM(AA, "A", QR, "Q*R", rtol, atol, out);
70 
71  // Test Q^T*Q = I
72  SDM eye(k,k), QTQ(k,k);
73  for (ordinal_type i=0; i<k; i++)
74  eye(i,i) = 1.0;
75  QTQ.multiply(Teuchos::TRANS, Teuchos::NO_TRANS, 1.0, Q, Q, 0.0);
76  success = Stokhos::compareSDM(eye, "eye", QTQ, "Q^T*Q", rtol, atol, out);
77 
78  return success;
79  }
80 
83  Teuchos::FancyOStream& out) {
84  bool success;
85 
86  SDM A(m,n);
87  ordinal_type k = std::min(m,n);
89  SDM Q, R;
91  qr_func(A, Q, R, piv);
92 
93  TEUCHOS_TEST_EQUALITY(Q.numRows(), m, out, success);
94  TEUCHOS_TEST_EQUALITY(Q.numCols(), k, out, success);
95  TEUCHOS_TEST_EQUALITY(R.numRows(), k, out, success);
96  TEUCHOS_TEST_EQUALITY(R.numCols(), n, out, success);
97  TEUCHOS_TEST_EQUALITY(piv.size(), n, out, success);
98 
99  // Test A*P = Q*R
100  SDM AP(m,n), QR(m,n);
101  for (ordinal_type j=0; j<n; j++)
102  for (ordinal_type i=0; i<m; i++)
103  AP(i,j) = A(i,piv[j]);
104  QR.multiply(Teuchos::NO_TRANS, Teuchos::NO_TRANS, 1.0, Q, R, 0.0);
105  success = Stokhos::compareSDM(AP, "A*P", QR, "Q*R", rtol, atol, out);
106 
107  // Test Q^T*Q = I
108  SDM eye(k,k), QTQ(k,k);
109  for (ordinal_type i=0; i<k; i++)
110  eye(i,i) = 1.0;
111  QTQ.multiply(Teuchos::TRANS, Teuchos::NO_TRANS, 1.0, Q, Q, 0.0);
112  success = Stokhos::compareSDM(eye, "eye", QTQ, "Q^T*Q", rtol, atol, out);
113 
114  return success;
115  }
116 
120  Teuchos::FancyOStream& out) {
121  bool success;
122 
123  SDM A(m,n);
124  generateRandomMatrix(A, k);
125  SDM Q, R;
127  for (ordinal_type i=0; i<5; i++)
128  piv[i] = 1;
129  Teuchos::Array<scalar_type> w(m, 1.0);
130  ordinal_type rank = qr_func(1.0e-12, A, w, Q, R, piv);
131 
132  TEUCHOS_TEST_EQUALITY(rank, k, out, success);
133  TEUCHOS_TEST_EQUALITY(Q.numRows(), m, out, success);
134  TEUCHOS_TEST_EQUALITY(Q.numCols(), k, out, success);
135  TEUCHOS_TEST_EQUALITY(R.numRows(), k, out, success);
136  TEUCHOS_TEST_EQUALITY(R.numCols(), k, out, success);
137  TEUCHOS_TEST_EQUALITY(piv.size(), n, out, success);
138 
139  // Test A*P = Q*R
140  SDM AP(m,k), QR(m,k);
141  for (ordinal_type j=0; j<k; j++)
142  for (ordinal_type i=0; i<m; i++)
143  AP(i,j) = A(i,piv[j]);
144  QR.multiply(Teuchos::NO_TRANS, Teuchos::NO_TRANS, 1.0, Q, R, 0.0);
145  success = Stokhos::compareSDM(AP, "A*P", QR, "Q*R", rtol, atol, out);
146 
147  // Test Q^T*Q = I
148  SDM eye(k,k), Qt(m,k), QTQ(k,k);
149  for (ordinal_type j=0; j<k; j++) {
150  eye(j,j) = 1.0;
151  for (ordinal_type i=0; i<m; i++)
152  Qt(i,j) = w[i]*Q(i,j);
153  }
154  QTQ.multiply(Teuchos::TRANS, Teuchos::NO_TRANS, 1.0, Qt, Q, 0.0);
155  success = Stokhos::compareSDM(eye, "eye", QTQ, "Q^T*Q", rtol, atol, out);
156 
157  return success;
158  }
159 
160  TEUCHOS_UNIT_TEST( Stokhos_SDMUtils, QR_CGS_TallSkinny ) {
161  ordinal_type m = 100;
162  ordinal_type n = 35;
163  success = test_QR(Stokhos::QR_CGS, m, n, rtol, atol, out);
164  }
165 
166  TEUCHOS_UNIT_TEST( Stokhos_SDMUtils, QR_CGS_ShortFat ) {
167  ordinal_type n = 100;
168  ordinal_type m = 35;
169  success = test_QR(Stokhos::QR_CGS, m, n, rtol, atol, out);
170  }
171 
172  TEUCHOS_UNIT_TEST( Stokhos_SDMUtils, QR_MGS_TallSkinny ) {
173  ordinal_type m = 100;
174  ordinal_type n = 35;
175  success = test_QR(Stokhos::QR_MGS, m, n, rtol, atol, out);
176  }
177 
178  TEUCHOS_UNIT_TEST( Stokhos_SDMUtils, QR_MGS_ShortFat ) {
179  ordinal_type n = 100;
180  ordinal_type m = 35;
181  success = test_QR(Stokhos::QR_MGS, m, n, rtol, atol, out);
182  }
183 
184  TEUCHOS_UNIT_TEST( Stokhos_SDMUtils, QR_MGS2_TallSkinny ) {
185  ordinal_type m = 100;
186  ordinal_type n = 35;
187  success = test_QR(Stokhos::QR_MGS2, m, n, rtol, atol, out);
188  }
189 
190  TEUCHOS_UNIT_TEST( Stokhos_SDMUtils, QR_MGS2_ShortFat ) {
191  ordinal_type n = 100;
192  ordinal_type m = 35;
193  success = test_QR(Stokhos::QR_MGS2, m, n, rtol, atol, out);
194  }
195 
196  TEUCHOS_UNIT_TEST( Stokhos_SDMUtils, QR_Householder_TallSkinny ) {
197  ordinal_type m = 100;
198  ordinal_type n = 35;
199  success = test_QR(Stokhos::QR_Householder, m, n, rtol, atol, out);
200  }
201 
202  TEUCHOS_UNIT_TEST( Stokhos_SDMUtils, QR_Householder_ShortFat ) {
203  ordinal_type n = 100;
204  ordinal_type m = 35;
205  success = test_QR(Stokhos::QR_Householder, m, n, rtol, atol, out);
206  }
207 
208  TEUCHOS_UNIT_TEST( Stokhos_SDMUtils, CPQR_Householder3_TallSkinny ) {
209  ordinal_type m = 100;
210  ordinal_type n = 35;
211  success = test_CPQR(Stokhos::CPQR_Householder3, m, n, rtol, atol, out);
212  }
213 
214  TEUCHOS_UNIT_TEST( Stokhos_SDMUtils, CPQR_Householder3_ShortFat ) {
215  ordinal_type n = 100;
216  ordinal_type m = 35;
217  success = test_CPQR(Stokhos::CPQR_Householder3, m, n, rtol, atol, out);
218  }
219 
220  TEUCHOS_UNIT_TEST( Stokhos_SDMUtils, CPQR_Householder_threshold_TallSkinny ) {
221  ordinal_type m = 100;
222  ordinal_type n = 35;
223  ordinal_type k = 20;
225  m, n, k, rtol, atol, out);
226  }
227 
228  TEUCHOS_UNIT_TEST( Stokhos_SDMUtils, CPQR_Householder_threshold_ShortFat ) {
229  ordinal_type n = 100;
230  ordinal_type m = 35;
231  ordinal_type k = 20;
233  m, n, k, rtol, atol, out);
234  }
235 
236  TEUCHOS_UNIT_TEST( Stokhos_SDMUtils, CPQR_MGS_threshold_TallSkinny ) {
237  ordinal_type m = 100;
238  ordinal_type n = 35;
239  ordinal_type k = 20;
241  m, n, k, rtol, atol, out);
242  }
243 
244  TEUCHOS_UNIT_TEST( Stokhos_SDMUtils, CPQR_MGS_threshold_ShortFat ) {
245  ordinal_type n = 100;
246  ordinal_type m = 35;
247  ordinal_type k = 20;
249  m, n, k, rtol, atol, out);
250  }
251 
252  TEUCHOS_UNIT_TEST( Stokhos_SDMUtils, CPQR_MGS_reorthog_threshold_TallSkinny ) {
253  ordinal_type m = 100;
254  ordinal_type n = 35;
255  ordinal_type k = 20;
257  m, n, k, rtol, atol, out);
258  }
259 
260  TEUCHOS_UNIT_TEST( Stokhos_SDMUtils, CPQR_MGS_reorthog_threshold_ShortFat ) {
261  ordinal_type n = 100;
262  ordinal_type m = 35;
263  ordinal_type k = 20;
265  m, n, k, rtol, atol, out);
266  }
267 
268 }
269 
270 int main( int argc, char* argv[] ) {
271  Teuchos::GlobalMPISession mpiSession(&argc, &argv);
273 }
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_CGS(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 classical Gram-Schmidt.
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.
int multiply(ETransp transa, ETransp transb, ScalarType alpha, const SerialDenseMatrix< OrdinalType, ScalarType > &A, const SerialDenseMatrix< OrdinalType, ScalarType > &B, ScalarType beta)
ordinal_type(* wcpqr_func_type)(const scalar_type &, const SDM &, const Teuchos::Array< scalar_type > &, SDM &, SDM &, Teuchos::Array< ordinal_type > &)
void generateRandomMatrix(SDM &A, ordinal_type rank)
bool test_weighted_CPQR(wcpqr_func_type qr_func, ordinal_type m, ordinal_type n, ordinal_type k, scalar_type rtol, scalar_type atol, Teuchos::FancyOStream &out)
void CPQR_Householder3(const Teuchos::SerialDenseMatrix< ordinal_type, scalar_type > &A, 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.
bool test_CPQR(cpqr_func_type qr_func, ordinal_type m, ordinal_type n, scalar_type rtol, scalar_type atol, Teuchos::FancyOStream &out)
KOKKOS_INLINE_FUNCTION PCE< Storage > min(const typename PCE< Storage >::value_type &a, const PCE< Storage > &b)
static int runUnitTestsFromMain(int argc, char *argv[])
bool compareSDM(const Teuchos::SerialDenseMatrix< ordinal_type, scalar_type > &a1, const std::string &a1_name, const Teuchos::SerialDenseMatrix< ordinal_type, scalar_type > &a2, const std::string &a2_name, const scalar_type &rel_tol, const scalar_type &abs_tol, Teuchos::FancyOStream &out)
void(* qr_func_type)(ordinal_type, const SDM &, const Teuchos::Array< scalar_type > &, SDM &, SDM &)
bool test_QR(qr_func_type qr_func, ordinal_type m, ordinal_type n, scalar_type rtol, scalar_type atol, Teuchos::FancyOStream &out)
void(* cpqr_func_type)(const SDM &, SDM &, SDM &, Teuchos::Array< ordinal_type > &)
OrdinalType numCols() const
int main(int argc, char **argv)
#define TEUCHOS_TEST_EQUALITY(v1, v2, out, success)
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.
size_type size() const
Teuchos::SerialDenseMatrix< ordinal_type, scalar_type > SDM
TEUCHOS_UNIT_TEST(Stokhos_SDMUtils, QR_CGS_TallSkinny)
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