DGtal  0.9.2
testEigenDecomposition.cpp
1 
30 #include <iostream>
32 #include "DGtal/base/Common.h"
33 #include "DGtal/math/linalg/EigenDecomposition.h"
35 
36 using namespace std;
37 using namespace DGtal;
38 
40 // Functions for testing class testEigenDecomposition.
42 
46 bool testEigenDecomposition()
47 {
48  unsigned int nbok = 0;
49  unsigned int nb = 0;
50 
51  typedef EigenDecomposition<2,double> Eigen2;
52  typedef Eigen2::Vector Vector2;
53  typedef Eigen2::Matrix Matrix2;
54 
55  trace.beginBlock ( "Testing block ..." );
56  // [4 1]
57  // [1 2]
58  Matrix2 A;
59  A.setComponent( 0, 0, 4 );
60  A.setComponent( 0, 1, 1 );
61  A.setComponent( 1, 0, 1 );
62  A.setComponent( 1, 1, 2 );
63  Matrix2 P;
64  Vector2 v;
65  Eigen2::getEigenDecomposition( A, P, v );
66  trace.info() << "Input matrix: " << A << std::endl;
67  trace.info() << "Eigenvectors: " << P << std::endl;
68  trace.info() << "Eigenvalues: " << v << std::endl;
69  Vector2 V0 = P.column( 0 );
70  Vector2 V1 = P.column( 1 );
71  Vector2 V0_exp( 0.3826834323650898, -0.9238795325112868 );
72  Vector2 V1_exp( 0.9238795325112868, 0.3826834323650898);
73  double v0_exp = 1.585786437626905;
74  double v1_exp = 4.414213562373095;
75  double error_V0 = (V0-V0_exp).norm();
76  double error_V1 = (V1-V1_exp).norm();
77  double error_v0 = fabs( v[0] - v0_exp );
78  double error_v1 = fabs( v[1] - v1_exp );
79  trace.info() << "error_V0 = " << error_V0 << std::endl;
80  trace.info() << "error_V1 = " << error_V1 << std::endl;
81  trace.info() << "error_v0 = " << error_v0 << std::endl;
82  trace.info() << "error_v1 = " << error_v1 << std::endl;
83  double epsilon = 1e-10;
84  ++nb, nbok += error_V0 < epsilon ? 1 : 0;
85  trace.info() << "(" << nbok << "/" << nb << ") "
86  << "error_V0 < epsilon, i.e. " << error_V0 << " < " << epsilon << std::endl;
87  ++nb, nbok += error_V1 < epsilon ? 1 : 0;
88  trace.info() << "(" << nbok << "/" << nb << ") "
89  << "error_V1 < epsilon, i.e. " << error_V1 << " < " << epsilon << std::endl;
90  ++nb, nbok += error_v0 < epsilon ? 1 : 0;
91  trace.info() << "(" << nbok << "/" << nb << ") "
92  << "error_v0 < epsilon, i.e. " << error_v0 << " < " << epsilon << std::endl;
93  ++nb, nbok += error_v1 < epsilon ? 1 : 0;
94  trace.info() << "(" << nbok << "/" << nb << ") "
95  << "error_v1 < epsilon, i.e. " << error_v1 << " < " << epsilon << std::endl;
96  trace.endBlock();
97 
98  return nbok == nb;
99 }
100 
102 // Standard services - public :
103 
104 int main( int argc, char** argv )
105 {
106  trace.beginBlock ( "Testing class testEigenDecomposition" );
107  trace.info() << "Args:";
108  for ( int i = 0; i < argc; ++i )
109  trace.info() << " " << argv[ i ];
110  trace.info() << endl;
111 
112  bool res = testEigenDecomposition(); // && ... other tests
113  trace.emphase() << ( res ? "Passed." : "Error." ) << endl;
114  trace.endBlock();
115  return res ? 0 : 1;
116 }
117 // //
void beginBlock(const std::string &keyword="")
Trace trace
Definition: Common.h:130
STL namespace.
double endBlock()
Aim: This class provides methods to compute the eigen decomposition of a matrix. Its objective is to ...
std::ostream & emphase()
DGtal is the top-level namespace which contains all DGtal functions and types.
std::ostream & info()