MyraMath
smgs


Source: tests/dense/gramschmidt.cpp

1 // ========================================================================= //
2 // This file is part of MyraMath, copyright (c) 2014-2019 by Ryan A Chilton //
3 // and distributed by MyraCore, LLC. See LICENSE.txt for license terms. //
4 // ========================================================================= //
5 
11 // Containers.
13 #include <myramath/dense/Matrix.h>
14 #include <myramath/dense/Vector.h>
17 
18 // Algorithms.
19 #include <myramath/dense/gemm.h>
22 
23 // Reporting.
24 #include <tests/myratest.h>
25 
26 using namespace myra;
27 
28 namespace {
29 
30 template<class Number> void test(int I, int J1, int J2, typename ReflectPrecision<Number>::type tolerance)
31  {
32  typedef typename ReflectPrecision<Number>::type Precision;
33  myra::out() << typestring<Number>() << std::endl;
34 
35  // Use mgs() to factor a random A into Q*R
36  auto A = Matrix<Number>::random(I,J1);
37  auto QR = mgs(A);
38  const Matrix<Number>& Q = QR.first;
39  const Matrix<Number>& R = QR.second;
40 
41  // Verify Q'Q = I, A = QR
42  Precision Q_error = frobenius( gemm(Q,'H',Q)-Matrix<Number>::identity(J1) );
43  Precision A_error = frobenius( gemm(Q,R)-A );
44  myra::out() << " |Q'Q-I| = " << Q_error << std::endl;
45  REQUIRE(Q_error < tolerance);
46  myra::out() << " |Q*R-A| = " << A_error << std::endl;
47  REQUIRE(A_error < tolerance);
48 
49  // Make random v, deflate it by Q using cgs_inplace()
50  auto v = Matrix<Number>::random(I,J2);
51  cgs_inplace(Q,v);
52  Precision v_error = frobenius( gemm(Q,'H',v) );
53  myra::out() << " |Q'v| = " << v_error << std::endl;
54  REQUIRE(v_error < tolerance);
55  }
56 
57 } // namespace
58 
59 ADD_TEST("smgs","[dense]")
60  { test<NumberS>(10,4,5,1.0e-4f); }
61 
62 ADD_TEST("dmgs","[dense]")
63  { test<NumberD>(10,4,5,1.0e-10); }
64 
65 ADD_TEST("cmgs","[dense]")
66  { test<NumberC>(10,4,5,1.0e-4f); }
67 
68 ADD_TEST("zmgs","[dense]")
69  { test<NumberZ>(10,4,5,1.0e-10); }
70 
Interface class for representing subranges of dense Matrix&#39;s.
Routines for orthogonalizing column vectors via classical and modified gram-schmidt.
Interface class for representing subranges of dense Vector&#39;s.
Tabulates an IxJ matrix. Allows random access, has column major layout to be compatible with BLAS/LAP...
Definition: bdsqr.h:20
Routines for computing Frobenius norms of various algebraic containers.
static Matrix< Number > random(int I, int J)
Generates a random Matrix of specified size.
Definition: Matrix.cpp:353
Definition: syntax.dox:1
Various utility functions/classes related to scalar Number types.
General purpose dense matrix container, O(i*j) storage.
Container for either a column vector or row vector (depends upon the usage context) ...
Reflects Precision trait for a Number, scalar Number types should specialize it.
Definition: Number.h:33
Variety of routines all for dense Matrix*Matrix multiplies. Delegates to the BLAS.


Results: [PASS]

float
|Q'Q-I| = 2.22964e-07
|Q*R-A| = 1.92133e-07
|Q'v| = 3.05948e-07


Go back to Summary of /test programs.