MyraMath
dQRSolver


Source: tests/dense/QRSolver.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>
15 
16 // Algorithms.
17 #include <myramath/dense/gemm.h>
19 
20 // Solver class under test.
22 
23 // Reporting.
24 #include <tests/myratest.h>
25 
26 using namespace myra;
27 
28 ADD_TEST("dQRSolver","[dense][solver]")
29  {
30  // Fill A and b.
31  auto A = Matrix<double>::fill_rmajor(4,3,
32  { 1.0, 1.0, 1.0,
33  1.0, 2.0, 4.0,
34  1.0, 3.0, 9.0,
35  1.0, 4.0,16.0 } );
36  auto b = Vector<double>::fill(
37  { 1.0,
38  1.5,
39  3.0,
40  6.0 } );
41  myra::out() << "A = " << A << std::endl;
42  myra::out() << "b = " << b << std::endl;
43  // Find least squares solution to A*x=b
44  QRSolver<double> solver(A.add_const());
45  auto x = solver.solve(b);
46  myra::out() << "x = " << x << std::endl;
47  // Compare to reference solution.
48  auto r = Vector<double>::fill({1.875, -1.475, 0.625});
49  double error = euclidean(r-x);
50  myra::out() << "error = " << error << std::endl;
51  REQUIRE(error < 1.0e-12);
52  }
static Vector< Number > fill(int N, Number c)
Generates a Vector of specified size filled with constant c.
Definition: Vector.cpp:288
Routines for computing euclidean norm of a Vector/VectorRange, or normalizing a Vector/VectorRange to...
Factors a square matrix A into Q*R, presents solve methods.
Definition: QRSolver.h:31
Solves full-rank least squares problems, argmin x |A*x-b|, where A.I >= A.J (tall/skinny).
Tabulates an IxJ matrix. Allows random access, has column major layout to be compatible with BLAS/LAP...
Definition: bdsqr.h:20
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) ...
MatrixRange< Number > solve(const MatrixRange< Number > &B) const
Solves A*X=B in the least squares sense, each column independently.
Definition: QRSolver.cpp:66
Variety of routines all for dense Matrix*Matrix multiplies. Delegates to the BLAS.


Results: [PASS]

A = size 4 by 3 Matrix of double:
[ 1 1 1 ]
[ 1 2 4 ]
[ 1 3 9 ]
[ 1 4 16 ]
b = size 4 Vector of double:
[ 1 1.5 3 6 ]
x = size 3 Vector of double:
[ 1.875 -1.475 0.625 ]
error = 5.6436e-15


Go back to Summary of /test programs.