MyraMath
power2


Source: tests/iterative/power.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/dimm.h>
24 #include <myramath/dense/gemm.h>
25 #include <myramath/dense/gemv.h>
26 #include <myramath/dense/inverse.h>
29 #include <myramath/sparse/gemm.h>
30 #include <myramath/sparse/gemv.h>
32 
33 // Iterative solve/action stuff.
37 
38 // Reporting.
40 #include <tests/myratest.h>
41 
42 using namespace myra;
43 using namespace myra_stlprint;
44 
45 namespace {
46 
47 template<class Precision> void test1(int N, Precision tolerance)
48  {
49  myra::out() << typestring<Precision>() << std::endl;
50  // Construct input A
51  auto D = DiagonalMatrix<Precision>::logspace(1,-2,N);
52  auto Y = Matrix<Precision>::random(N,N);
53  auto A = Y*D*inverse(Y);
54  // Extract eigenpair (lambda,x)
55  auto x = Vector<Precision>::random(N);
56  Precision lambda = power(make_GemmAction(A),x,tolerance,200);
57  // Evaluate residual A*x-lambda*x
58  Vector<Precision> r = A*x - x*lambda;
59  Precision r_error = euclidean(r);
60  myra::out() << " |A*x-x*lambda| = " << r_error << std::endl;
61  REQUIRE(r_error < tolerance);
62  }
63 
64 template<class Precision> void test2(int N, int K, Precision tolerance)
65  {
66  myra::out() << typestring<Precision>() << std::endl;
67  // Construct input A
68  auto D = DiagonalMatrix<Precision>::logspace(1,-2,N);
69  auto Y = Matrix<Precision>::random(N,N);
70  auto A = Y*D*inverse(Y);
71  // Extract eigenpair (Lambda,X)
72  auto X = Matrix<Precision>::random(N,K);
73  auto Lambda = power(make_GemmAction(A),X,tolerance,200);
74  // Evaluate residual A*X-X*Lambda
75  Matrix<Precision> R = A*X - X*Lambda;
76  Precision R_error = frobenius(R);
77  myra::out() << " |A*X-X*Lambda| = " << R_error << std::endl;
78  REQUIRE(R_error < tolerance*10);
79  }
80 
81 } // namespace
82 
83 ADD_TEST("power1","[iterative]")
84  {
85  test1<float> (20,1.0e-3f);
86  test1<double>(20,1.0e-5);
87  }
88 
89 ADD_TEST("power2","[iterative]")
90  {
91  test2<float> (20,5,1.0e-4f);
92  test2<double>(20,5,1.0e-8);
93  }
Applies the power method to extract dominant eigenpair.
Routines for computing euclidean norm of a Vector/VectorRange, or normalizing a Vector/VectorRange to...
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.
static Matrix< Number > random(int I, int J)
Generates a random Matrix of specified size.
Definition: Matrix.cpp:353
Routines for multiplying by a DiagonalMatrix.
static DiagonalMatrix< Number > logspace(Precision x0, Precision x1, int N)
Generates a Vector filled with N Number&#39;s logarithmically spaced between (10^x0,10^x1).
General purpose compressed-sparse-column (CSC) container.
Variety of routines all for dense Matrix*Vector multiplies. All just delegate to gemm() ...
static Vector< Number > random(int N)
Generates a random Vector of specified size.
Definition: Vector.cpp:276
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.
Signatures for sparse matrix * dense vector multiplies. All delegate to gemm() under the hood...
General purpose dense matrix container, O(i*j) storage.
Tabulates a vector of length N, allows random access.
Definition: conjugate.h:21
Container for either a column vector or row vector (depends upon the usage context) ...
Overwrites a LowerMatrix, DiagonalMatrix, or square Matrix with its own inverse. Or, returns it as a copy.
An Action for multiplying by a dense Matrix or SparseMatrix using gemm().
Container for a diagonal matrix, O(n) storage. Used by SVD, row/column scaling, etc.
Variety of routines all for dense Matrix*Matrix multiplies. Delegates to the BLAS.
Range/Iterator types associated with SparseMatrix.
Helper routines for reordering/filling 1D structured grids. Used by many unit tests.


Results: [PASS]

float
|A*X-X*Lambda| = 0.000517606
double
|A*X-X*Lambda| = 4.2926e-08


Go back to Summary of /test programs.