MyraMath
lanczos1


Source: tests/iterative/lanczos1.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>
21 
22 // Algorithms.
23 #include <myramath/dense/gemm.h>
24 #include <myramath/dense/dimm.h>
26 #include <myramath/sparse/gemm.h>
27 #include <myramath/sparse/gemv.h>
29 
30 // Iterative solve/action stuff.
34 
35 // Reporting.
37 #include <tests/myratest.h>
38 
39 using namespace myra;
40 using namespace myra_stlprint;
41 
42 namespace {
43 
44 template<class Precision> void test(int X, int Y, Precision tolerance)
45  {
46  myra::out() << typestring<Precision>() << std::endl;
47  // Construct 2D laplacian.
48  int N = X*Y;
49  auto A = laplacian2<Precision>(X,Y);
50  // Compute a few dominant eigenpairs.
51  int K = 8;
52  Matrix<Precision> V(N,K);
54  for (int k = 0; k < K; ++k)
55  {
56  auto v_d = lanczos1(make_GemmAction(A),V,tolerance,100);
57  V.vector(k).assign(v_d.first);
58  D(k) = v_d.second;
59  }
60  // Evaluate residual A*V-V*D
61  Matrix<Precision> R = A*V - V*D;
62  Precision R_error = frobenius(R);
63  myra::out() << " |A*V-V*D| = " << R_error << std::endl;
64  // Evaluate orthogonality V'V-I
65  auto I = Matrix<Precision>::identity(K);
66  Precision I_error = frobenius(gemm(V,'T',V)-I);
67  myra::out() << " |V'V-I| = " << I_error << std::endl;
68  REQUIRE(R_error < tolerance*10.0);
69  REQUIRE(I_error < tolerance*10.0);
70  }
71 
72 } // namespace
73 
74 ADD_TEST("lanczos1","[iterative]")
75  {
76  test<float> (20,15,1.0e-4f);
77  test<double>(20,15,1.0e-8);
78  }
Interface class for representing subranges of DiagonalMatrix&#39;s.
Interface class for representing subranges of dense Matrix&#39;s.
Interface class for representing subranges of dense Vector&#39;s.
Applies the "Action" of a linear operator, b := A*x, used in iterative solution algorithms.
Variety of routines for mixed dense*sparse or dense*sparse matrix multiplies. The dense*dense case is...
Routines for computing Frobenius norms of various algebraic containers.
Routines for multiplying by a DiagonalMatrix.
General purpose compressed-sparse-column (CSC) container.
Definition: syntax.dox:1
Definition: stlprint.h:32
Routines for printing the contents of various std::container&#39;s to a std::ostream using operator <<...
Various utility functions/classes related to scalar Number types.
static Matrix< Number > identity(int IJ)
Generates an identity Matrix of specified size.
Definition: Matrix.cpp:349
Signatures for sparse matrix * dense vector multiplies. All delegate to gemm() under the hood...
General purpose dense matrix container, O(i*j) storage.
Container for either a column vector or row vector (depends upon the usage context) ...
An Action for multiplying by a dense Matrix or SparseMatrix using gemm().
Finds one dominant eigenpair of a real symmetric Action.
Container for a diagonal matrix, O(n) storage. Used by SVD, row/column scaling, etc.
Helper routines for reordering/filling 2D structured grids. Used by many unit tests.
Variety of routines all for dense Matrix*Matrix multiplies. Delegates to the BLAS.
Range/Iterator types associated with SparseMatrix.


Results: [PASS]

float
|A*V-V*D| = 0.000239184
|V'V-I| = 1.59039e-06
double
|A*V-V*D| = 2.2652e-08
|V'V-I| = 4.40059e-15


Go back to Summary of /test programs.