libMesh
Loading...
Searching...
No Matches
Public Member Functions | Private Member Functions | List of all members
DenseMatrixTest Class Reference
Inheritance diagram for DenseMatrixTest:
[legend]

Public Member Functions

void setUp ()
 
void tearDown ()
 
 LIBMESH_CPPUNIT_TEST_SUITE (DenseMatrixTest)
 
 CPPUNIT_TEST (testClassifiers)
 
 CPPUNIT_TEST (testNorms)
 
 CPPUNIT_TEST (testOuterProduct)
 
 CPPUNIT_TEST (testSVD)
 
 CPPUNIT_TEST (testEVDreal)
 
 CPPUNIT_TEST (testEVDcomplex)
 
 CPPUNIT_TEST (testComplexSVD)
 
 CPPUNIT_TEST (testSubMatrix)
 
 CPPUNIT_TEST_SUITE_END ()
 

Private Member Functions

void testClassifiers ()
 
void testNorms ()
 
void testOuterProduct ()
 
void testSVD ()
 
void testEVD_helper (DenseMatrix< Real > &A, std::vector< Real > true_lambda_real, std::vector< Real > true_lambda_imag, Real tol)
 
void testEVDreal ()
 
void testEVDcomplex ()
 
void testComplexSVD ()
 
void testSubMatrix ()
 

Detailed Description

Definition at line 16 of file dense_matrix_test.C.

Member Function Documentation

◆ CPPUNIT_TEST() [1/8]

DenseMatrixTest::CPPUNIT_TEST ( testClassifiers  )

◆ CPPUNIT_TEST() [2/8]

DenseMatrixTest::CPPUNIT_TEST ( testComplexSVD  )

◆ CPPUNIT_TEST() [3/8]

DenseMatrixTest::CPPUNIT_TEST ( testEVDcomplex  )

◆ CPPUNIT_TEST() [4/8]

DenseMatrixTest::CPPUNIT_TEST ( testEVDreal  )

◆ CPPUNIT_TEST() [5/8]

DenseMatrixTest::CPPUNIT_TEST ( testNorms  )

◆ CPPUNIT_TEST() [6/8]

DenseMatrixTest::CPPUNIT_TEST ( testOuterProduct  )

◆ CPPUNIT_TEST() [7/8]

DenseMatrixTest::CPPUNIT_TEST ( testSubMatrix  )

◆ CPPUNIT_TEST() [8/8]

DenseMatrixTest::CPPUNIT_TEST ( testSVD  )

◆ CPPUNIT_TEST_SUITE_END()

DenseMatrixTest::CPPUNIT_TEST_SUITE_END ( )

◆ LIBMESH_CPPUNIT_TEST_SUITE()

DenseMatrixTest::LIBMESH_CPPUNIT_TEST_SUITE ( DenseMatrixTest  )

◆ setUp()

void DenseMatrixTest::setUp ( )
inline

Definition at line 19 of file dense_matrix_test.C.

19{}

◆ tearDown()

void DenseMatrixTest::tearDown ( )
inline

Definition at line 21 of file dense_matrix_test.C.

21{}

◆ testClassifiers()

void DenseMatrixTest::testClassifiers ( )
inlineprivate

Definition at line 39 of file dense_matrix_test.C.

40 {
41 LOG_UNIT_TEST;
42
43 DenseMatrix<Real> a(2, 2, {1, 2,
44 3, 4});
45 CPPUNIT_ASSERT(isfinite(a));
46 CPPUNIT_ASSERT(!isinf(a));
47 CPPUNIT_ASSERT(!isnan(a));
48
49 DenseVector<Real> diag = a.diagonal();
50 CPPUNIT_ASSERT(isfinite(diag));
51 CPPUNIT_ASSERT(!isinf(diag));
52 CPPUNIT_ASSERT(!isnan(diag));
53
54 DenseSubMatrix suba1(a,0,0,1,1);
55 CPPUNIT_ASSERT(isfinite(suba1));
56 CPPUNIT_ASSERT(!isinf(suba1));
57 CPPUNIT_ASSERT(!isnan(suba1));
58
59 DenseSubMatrix suba2(a,1,1,1,1);
60 CPPUNIT_ASSERT(isfinite(suba2));
61 CPPUNIT_ASSERT(!isinf(suba2));
62 CPPUNIT_ASSERT(!isnan(suba2));
63
64 a(0, 0) = std::numeric_limits<Real>::infinity();
65 CPPUNIT_ASSERT(!isfinite(a));
66 CPPUNIT_ASSERT(!isfinite(suba1));
67 CPPUNIT_ASSERT(isfinite(suba2));
68 CPPUNIT_ASSERT(isinf(a));
69 CPPUNIT_ASSERT(isinf(suba1));
70 CPPUNIT_ASSERT(!isinf(suba2));
71 CPPUNIT_ASSERT(!isnan(a));
72 CPPUNIT_ASSERT(!isnan(suba1));
73 CPPUNIT_ASSERT(!isnan(suba2));
74
75 CPPUNIT_ASSERT(isfinite(diag));
76 CPPUNIT_ASSERT(!isinf(diag));
77 CPPUNIT_ASSERT(!isnan(diag));
78 diag = a.diagonal();
79 CPPUNIT_ASSERT(!isfinite(diag));
80 CPPUNIT_ASSERT(isinf(diag));
81 CPPUNIT_ASSERT(!isnan(diag));
82
83 a(0, 0) = 3;
84 a(1, 1) = std::numeric_limits<Real>::quiet_NaN();
85 CPPUNIT_ASSERT(!isfinite(a));
86 CPPUNIT_ASSERT(isfinite(suba1));
87 CPPUNIT_ASSERT(!isfinite(suba2));
88 CPPUNIT_ASSERT(!isinf(a));
89 CPPUNIT_ASSERT(!isinf(suba1));
90 CPPUNIT_ASSERT(!isinf(suba2));
91 CPPUNIT_ASSERT(isnan(a));
92 CPPUNIT_ASSERT(!isnan(suba1));
93 CPPUNIT_ASSERT(isnan(suba2));
94
95 diag = a.diagonal();
96 CPPUNIT_ASSERT(!isfinite(diag));
97 CPPUNIT_ASSERT(!isinf(diag));
98 CPPUNIT_ASSERT(isnan(diag));
99 }
Defines a dense matrix for use in Finite Element-type computations.
Defines a dense submatrix for use in Finite Element-type computations.
Defines a dense vector for use in Finite Element-type computations.
LIBMESH_DEVICE_INLINE bool isfinite(std::complex< T > a)
LIBMESH_DEVICE_INLINE bool isnan(std::complex< T > a)
LIBMESH_DEVICE_INLINE bool isinf(std::complex< T > a)

References libMesh::isfinite(), libMesh::isinf(), and libMesh::isnan().

◆ testComplexSVD()

void DenseMatrixTest::testComplexSVD ( )
inlineprivate

Definition at line 426 of file dense_matrix_test.C.

427 {
428#ifdef LIBMESH_USE_COMPLEX_NUMBERS
429 LOG_UNIT_TEST;
430
432
433 A(0,0) = Complex(2.18904,4.44523e-18); A(0,1) = Complex(-3.20491,-0.136699); A(0,2) = Complex(0.716316,-0.964802);
434 A(1,0) = Complex(-3.20491,0.136699); A(1,1) = Complex(4.70076,-3.25261e-18); A(1,2) = Complex(-0.98849,1.45727);
435 A(2,0) = Complex(0.716316,0.964802); A(2,1) = Complex(-0.98849,-1.45727); A(2,2) = Complex(0.659629,-4.01155e-18);
436
437 DenseVector<Real> sigma;
438 A.svd(sigma);
439
440 DenseVector<Real> true_sigma(3);
441 true_sigma(0) = 7.54942516052;
442 true_sigma(1) = 3.17479511368e-06;
443 true_sigma(2) = 6.64680908281e-07;
444
445 for (unsigned i=0; i<sigma.size(); ++i)
446 LIBMESH_ASSERT_FP_EQUAL(sigma(i), true_sigma(i), 1.e-10);
447#endif
448 }
virtual unsigned int size() const override final
std::complex< Real > Complex

References libMesh::DenseVector< T >::size(), and libMesh::DenseMatrix< T >::svd().

◆ testEVD_helper()

void DenseMatrixTest::testEVD_helper ( DenseMatrix< Real > &  A,
std::vector< Real >  true_lambda_real,
std::vector< Real >  true_lambda_imag,
Real  tol 
)
inlineprivate

Definition at line 186 of file dense_matrix_test.C.

190 {
191 // Note: see bottom of this file, we only do this test if PETSc is
192 // available, but this function currently only exists if we're
193 // using real numbers.
194#ifdef LIBMESH_USE_REAL_NUMBERS
195 // Let's compute the eigenvalues on a copy of A, so that we can
196 // use the original to check the computation.
197 DenseMatrix<Real> A_copy = A;
198
199 DenseVector<Real> lambda_real, lambda_imag;
200 DenseMatrix<Real> VR; // right eigenvectors
201 DenseMatrix<Real> VL; // left eigenvectors
202 A_copy.evd_left_and_right(lambda_real, lambda_imag, VL, VR);
203
204 // The matrix is square and of size N x N.
205 const unsigned N = A.m();
206
207 // Verify left eigen-values.
208 // Test that the right eigenvalues are self-consistent by computing
209 // u_j**H * A = lambda_j * u_j**H
210 // Note that we have to handle real and complex eigenvalues
211 // differently, since complex eigenvectors share their storage.
212 for (unsigned eigenval=0; eigenval<N; ++eigenval)
213 {
214 // Only check real eigenvalues
215 if (std::abs(lambda_imag(eigenval)) < tol*tol)
216 {
217 // remove print libMesh::out << "Checking eigenvalue: " << eigenval << std::endl;
218 DenseVector<Real> lhs(N), rhs(N);
219 for (unsigned i=0; i<N; ++i)
220 {
221 rhs(i) = lambda_real(eigenval) * VL(i, eigenval);
222 for (unsigned j=0; j<N; ++j)
223 lhs(i) += A(j, i) * VL(j, eigenval); // Note: A(j,i)
224 }
225
226 // Subtract and assert that the norm of the difference is
227 // below some tolerance.
228 lhs -= rhs;
229 LIBMESH_ASSERT_FP_EQUAL(/*expected=*/0., /*actual=*/lhs.l2_norm(), std::sqrt(tol)*tol);
230 }
231 else
232 {
233 // This is a complex eigenvalue, so:
234 // a.) It occurs in a complex-conjugate pair
235 // b.) the real part of the eigenvector is stored is VL(:,eigenval)
236 // c.) the imag part of the eigenvector is stored in VL(:,eigenval+1)
237 //
238 // Equating the real and imaginary parts of Ax=lambda*x leads to two sets
239 // of relations that must hold:
240 // 1.) A^T x_r = lambda_r*x_r + lambda_i*x_i
241 // 2.) A^T x_i = -lambda_i*x_r + lambda_r*x_i
242 // which we can verify.
243
244 // 1.)
245 DenseVector<Real> lhs(N), rhs(N);
246 for (unsigned i=0; i<N; ++i)
247 {
248 rhs(i) = lambda_real(eigenval) * VL(i, eigenval) + lambda_imag(eigenval) * VL(i, eigenval+1);
249 for (unsigned j=0; j<N; ++j)
250 lhs(i) += A(j, i) * VL(j, eigenval); // Note: A(j,i)
251 }
252
253 lhs -= rhs;
254 LIBMESH_ASSERT_FP_EQUAL(/*expected=*/0., /*actual=*/lhs.l2_norm(), std::sqrt(tol)*tol);
255
256 // libMesh::out << "lhs=" << std::endl;
257 // lhs.print_scientific(libMesh::out, /*precision=*/15);
258 //
259 // libMesh::out << "rhs=" << std::endl;
260 // rhs.print_scientific(libMesh::out, /*precision=*/15);
261
262 // 2.)
263 lhs.zero();
264 rhs.zero();
265 for (unsigned i=0; i<N; ++i)
266 {
267 rhs(i) = -lambda_imag(eigenval) * VL(i, eigenval) + lambda_real(eigenval) * VL(i, eigenval+1);
268 for (unsigned j=0; j<N; ++j)
269 lhs(i) += A(j, i) * VL(j, eigenval+1); // Note: A(j,i)
270 }
271
272 lhs -= rhs;
273 LIBMESH_ASSERT_FP_EQUAL(/*expected=*/0., /*actual=*/lhs.l2_norm(), std::sqrt(tol)*tol);
274
275 // libMesh::out << "lhs=" << std::endl;
276 // lhs.print_scientific(libMesh::out, /*precision=*/15);
277 //
278 // libMesh::out << "rhs=" << std::endl;
279 // rhs.print_scientific(libMesh::out, /*precision=*/15);
280
281 // We'll skip the second member of the complex conjugate
282 // pair. If the first one worked, the second one should
283 // as well...
284 eigenval += 1;
285 }
286 }
287
288 // Verify right eigen-values.
289 // Test that the right eigenvalues are self-consistent by computing
290 // A * v_j - lambda_j * v_j
291 // Note that we have to handle real and complex eigenvalues
292 // differently, since complex eigenvectors share their storage.
293 for (unsigned eigenval=0; eigenval<N; ++eigenval)
294 {
295 // Only check real eigenvalues
296 if (std::abs(lambda_imag(eigenval)) < tol*tol)
297 {
298 // remove print libMesh::out << "Checking eigenvalue: " << eigenval << std::endl;
299 DenseVector<Real> lhs(N), rhs(N);
300 for (unsigned i=0; i<N; ++i)
301 {
302 rhs(i) = lambda_real(eigenval) * VR(i, eigenval);
303 for (unsigned j=0; j<N; ++j)
304 lhs(i) += A(i, j) * VR(j, eigenval);
305 }
306
307 lhs -= rhs;
308 LIBMESH_ASSERT_FP_EQUAL(/*expected=*/0., /*actual=*/lhs.l2_norm(), std::sqrt(tol)*tol);
309 }
310 else
311 {
312 // This is a complex eigenvalue, so:
313 // a.) It occurs in a complex-conjugate pair
314 // b.) the real part of the eigenvector is stored is VR(:,eigenval)
315 // c.) the imag part of the eigenvector is stored in VR(:,eigenval+1)
316 //
317 // Equating the real and imaginary parts of Ax=lambda*x leads to two sets
318 // of relations that must hold:
319 // 1.) Ax_r = lambda_r*x_r - lambda_i*x_i
320 // 2.) Ax_i = lambda_i*x_r + lambda_r*x_i
321 // which we can verify.
322
323 // 1.)
324 DenseVector<Real> lhs(N), rhs(N);
325 for (unsigned i=0; i<N; ++i)
326 {
327 rhs(i) = lambda_real(eigenval) * VR(i, eigenval) - lambda_imag(eigenval) * VR(i, eigenval+1);
328 for (unsigned j=0; j<N; ++j)
329 lhs(i) += A(i, j) * VR(j, eigenval);
330 }
331
332 lhs -= rhs;
333 LIBMESH_ASSERT_FP_EQUAL(/*expected=*/0., /*actual=*/lhs.l2_norm(), std::sqrt(tol)*tol);
334
335 // 2.)
336 lhs.zero();
337 rhs.zero();
338 for (unsigned i=0; i<N; ++i)
339 {
340 rhs(i) = lambda_imag(eigenval) * VR(i, eigenval) + lambda_real(eigenval) * VR(i, eigenval+1);
341 for (unsigned j=0; j<N; ++j)
342 lhs(i) += A(i, j) * VR(j, eigenval+1);
343 }
344
345 lhs -= rhs;
346 LIBMESH_ASSERT_FP_EQUAL(/*expected=*/0., /*actual=*/lhs.l2_norm(), std::sqrt(tol)*tol);
347
348 // We'll skip the second member of the complex conjugate
349 // pair. If the first one worked, the second one should
350 // as well...
351 eigenval += 1;
352 }
353 }
354
355 // Sort the results from Lapack *individually*.
356 std::sort(lambda_real.get_values().begin(), lambda_real.get_values().end());
357 std::sort(lambda_imag.get_values().begin(), lambda_imag.get_values().end());
358
359 // Sort the true eigenvalues *individually*.
360 std::sort(true_lambda_real.begin(), true_lambda_real.end());
361 std::sort(true_lambda_imag.begin(), true_lambda_imag.end());
362
363 // Compare the individually-sorted values.
364 for (unsigned i=0; i<lambda_real.size(); ++i)
365 {
366 // Note: I initially verified the results with TOLERANCE**2,
367 // but that turned out to be just a bit too tight for some of
368 // the test problems. I'm not sure what controls the accuracy
369 // of the eigenvalue computation in LAPACK, there is no way to
370 // set a tolerance in the LAPACKgeev_ interface.
371 LIBMESH_ASSERT_FP_EQUAL(/*expected=*/true_lambda_real[i], /*actual=*/lambda_real(i), std::sqrt(tol)*tol);
372 LIBMESH_ASSERT_FP_EQUAL(/*expected=*/true_lambda_imag[i], /*actual=*/lambda_imag(i), std::sqrt(tol)*tol);
373 }
374#endif
375 }
void evd_left_and_right(DenseVector< T > &lambda_real, DenseVector< T > &lambda_imag, DenseMatrix< T > &VL, DenseMatrix< T > &VR)
Compute the eigenvalues (both real and imaginary parts) as well as the left and right eigenvectors of...
std::vector< T > & get_values()

References libMesh::DenseMatrix< T >::evd_left_and_right(), libMesh::DenseVector< T >::get_values(), libMesh::DenseMatrixBase< T >::m(), libMesh::DenseVector< T >::size(), and libMesh::DenseVector< T >::zero().

Referenced by testEVDcomplex(), and testEVDreal().

◆ testEVDcomplex()

void DenseMatrixTest::testEVDcomplex ( )
inlineprivate

Definition at line 398 of file dense_matrix_test.C.

399 {
400 LOG_UNIT_TEST;
401
402 // This test is also from a Matlab example, and has complex eigenvalues.
403 // http://www.mathworks.com/help/matlab/math/eigenvalues.html?s_tid=gn_loc_drop
404 DenseMatrix<Real> A(3, 3);
405 A(0,0) = 0; A(0,1) = -6; A(0,2) = -1;
406 A(1,0) = 6; A(1,1) = 2; A(1,2) = -16;
407 A(2,0) = -5; A(2,1) = 20; A(2,2) = -10;
408
409 std::vector<Real> true_lambda_real(3);
410 true_lambda_real[0] = -3.070950351248293;
411 true_lambda_real[1] = -2.464524824375853;
412 true_lambda_real[2] = -2.464524824375853;
413 std::vector<Real> true_lambda_imag(3);
414 true_lambda_imag[0] = 0.;
415 true_lambda_imag[1] = 17.60083096447099;
416 true_lambda_imag[2] = -17.60083096447099;
417
418 // We're good up to double precision (due to the truncated
419 // literals above) or Real precision, whichever is worse
420 const Real tol = std::max(Real(1e-6), TOLERANCE);
421
422 // call helper function to compute and verify results
423 testEVD_helper(A, true_lambda_real, true_lambda_imag, tol);
424 }
void testEVD_helper(DenseMatrix< Real > &A, std::vector< Real > true_lambda_real, std::vector< Real > true_lambda_imag, Real tol)
static constexpr Real TOLERANCE
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real

References libMesh::Real, testEVD_helper(), and libMesh::TOLERANCE.

◆ testEVDreal()

void DenseMatrixTest::testEVDreal ( )
inlineprivate

Definition at line 377 of file dense_matrix_test.C.

378 {
379 LOG_UNIT_TEST;
380
381 // This is an example from Matlab's gallery(3) which is a
382 // non-symmetric 3x3 matrix with eigen values lambda = 1, 2, 3.
383 DenseMatrix<Real> A(3, 3);
384 A(0,0) = -149; A(0,1) = -50; A(0,2) = -154;
385 A(1,0) = 537; A(1,1) = 180; A(1,2) = 546;
386 A(2,0) = -27; A(2,1) = -9; A(2,2) = -25;
387
388 std::vector<Real> true_lambda_real(3);
389 true_lambda_real[0] = 1.;
390 true_lambda_real[1] = 2.;
391 true_lambda_real[2] = 3.;
392 std::vector<Real> true_lambda_imag(3); // all zero
393
394 // call helper function to compute and verify results.
395 testEVD_helper(A, true_lambda_real, true_lambda_imag, TOLERANCE);
396 }

References testEVD_helper(), and libMesh::TOLERANCE.

◆ testNorms()

void DenseMatrixTest::testNorms ( )
inlineprivate

Definition at line 101 of file dense_matrix_test.C.

102 {
103 LOG_UNIT_TEST;
104
105 DenseVector<Real> a = {3.0, -4.0};
106 LIBMESH_ASSERT_FP_EQUAL(a.l1_norm(), 7, TOLERANCE*TOLERANCE);
107 LIBMESH_ASSERT_FP_EQUAL(a.l2_norm(), 5, TOLERANCE*TOLERANCE);
108 LIBMESH_ASSERT_FP_EQUAL(a.linfty_norm(), 4, TOLERANCE*TOLERANCE);
109
110 DenseVector<Real> b = {1.0, -2.0};
111 LIBMESH_ASSERT_FP_EQUAL(b.l1_norm(), 3, TOLERANCE*TOLERANCE);
112 LIBMESH_ASSERT_FP_EQUAL(b.l2_norm(), std::sqrt(Real(5)), TOLERANCE*TOLERANCE);
113 LIBMESH_ASSERT_FP_EQUAL(b.linfty_norm(), 2, TOLERANCE*TOLERANCE);
114
115 DenseMatrix<Real> a_times_b; // [3 -6; -4 8]
116 a_times_b.outer_product(a, b);
117 LIBMESH_ASSERT_FP_EQUAL(a_times_b.l1_norm(), 14, TOLERANCE*TOLERANCE);
118 LIBMESH_ASSERT_FP_EQUAL(a_times_b.linfty_norm(), 12, TOLERANCE*TOLERANCE);
119 }
auto linfty_norm() const
void outer_product(const DenseVector< T > &a, const DenseVector< T > &b)
Computes the outer (dyadic) product of two vectors and stores in (*this).
Real linfty_norm() const
static const Real b

References b, libMesh::DenseMatrix< T >::l1_norm(), libMesh::DenseVector< T >::l1_norm(), libMesh::DenseVector< T >::l2_norm(), libMesh::DenseMatrix< T >::linfty_norm(), libMesh::DenseVector< T >::linfty_norm(), libMesh::DenseMatrix< T >::outer_product(), libMesh::Real, and libMesh::TOLERANCE.

◆ testOuterProduct()

void DenseMatrixTest::testOuterProduct ( )
inlineprivate

Definition at line 121 of file dense_matrix_test.C.

122 {
123 LOG_UNIT_TEST;
124
125 DenseVector<Real> a = {1.0, 2.0};
126 DenseVector<Real> b = {3.0, 4.0, 5.0};
127
128 DenseMatrix<Real> a_times_b;
129 a_times_b.outer_product(a, b);
130
131 DenseMatrix<Real> a_times_b_correct(2, 3,
132 {3., 4., 5.,
133 6., 8., 10.});
134
135 for (unsigned int i = 0; i < a.size(); ++i)
136 for (unsigned int j = 0; j < b.size(); ++j)
137 LIBMESH_ASSERT_NUMBERS_EQUAL
138 (a_times_b(i,j), a_times_b_correct(i,j), TOLERANCE*TOLERANCE);
139 }

References b, libMesh::DenseMatrix< T >::outer_product(), libMesh::DenseVector< T >::size(), and libMesh::TOLERANCE.

◆ testSubMatrix()

void DenseMatrixTest::testSubMatrix ( )
inlineprivate

Definition at line 450 of file dense_matrix_test.C.

451 {
452 LOG_UNIT_TEST;
453
454 DenseMatrix<Number> A(4, 3);
455 A(0,0) = 1.0; A(0,1) = 2.0; A(0,2) = 3.0;
456 A(1,0) = 4.0; A(1,1) = 5.0; A(1,2) = 6.0;
457 A(2,0) = 7.0; A(2,1) = 8.0; A(2,2) = 9.0;
458 A(3,0) =10.0; A(3,1) =11.0; A(3,2) =12.0;
459
461 B(0,0) = 7.0; B(0,1) = 8.0;
462 B(1,0) =10.0; B(1,1) =11.0;
463
464 DenseMatrix<Number> C = A.sub_matrix(2, 2, 0, 2);
465 CPPUNIT_ASSERT(B == C);
466 }
DenseMatrix sub_matrix(unsigned int row_id, unsigned int row_size, unsigned int col_id, unsigned int col_size) const
Get submatrix with the smallest row and column indices and the submatrix size.
Definition assembly.h:39

References libMesh::DenseMatrix< T >::sub_matrix().

◆ testSVD()

void DenseMatrixTest::testSVD ( )
inlineprivate

Definition at line 141 of file dense_matrix_test.C.

142 {
143 LOG_UNIT_TEST;
144
146 DenseVector<Real> sigma;
147 DenseMatrix<Number> A(3, 2, {
148 1.0, 2.0,
149 3.0, 4.0,
150 5.0, 6.0});
151
152 A.svd(sigma, U, VT);
153
154 // Solution for this case is (verified with numpy)
155 DenseMatrix<Number> true_U(3, 2, {
156 -2.298476964000715e-01, 8.834610176985250e-01,
157 -5.247448187602936e-01, 2.407824921325463e-01,
158 -8.196419411205157e-01, -4.018960334334318e-01});
159
160 DenseMatrix<Number> true_VT(2, 2, {
161 -6.196294838293402e-01, -7.848944532670524e-01,
162 -7.848944532670524e-01, 6.196294838293400e-01});
163
164 DenseVector<Real> true_sigma = {9.525518091565109e+00, 5.143005806586446e-01};
165
166 // Tolerance is bounded by the double literals above and by using Real
167 const Real tol = std::max(Real(1e-12), TOLERANCE*TOLERANCE);
168
169 for (unsigned i=0; i<U.m(); ++i)
170 for (unsigned j=0; j<U.n(); ++j)
171 LIBMESH_ASSERT_NUMBERS_EQUAL( U(i,j), true_U(i,j), tol);
172
173 for (unsigned i=0; i<VT.m(); ++i)
174 for (unsigned j=0; j<VT.n(); ++j)
175 LIBMESH_ASSERT_NUMBERS_EQUAL( VT(i,j), true_VT(i,j), tol);
176
177 for (unsigned i=0; i<sigma.size(); ++i)
178 LIBMESH_ASSERT_FP_EQUAL(sigma(i), true_sigma(i), tol);
179 }
void svd(DenseVector< Real > &sigma)
Compute the singular value decomposition of the matrix.

References libMesh::DenseMatrixBase< T >::m(), libMesh::DenseMatrixBase< T >::n(), libMesh::Real, libMesh::DenseVector< T >::size(), libMesh::DenseMatrix< T >::svd(), and libMesh::TOLERANCE.


The documentation for this class was generated from the following file: