Jpp  18.6.0-rc.1
the software that should make you happy
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Functions
JMatrixNS.cc File Reference

Example program to test inversion of symmetric matrix. More...

#include <string>
#include <iostream>
#include <iomanip>
#include <chrono>
#include "TRandom3.h"
#include "TMatrixTSym.h"
#include "JLang/JException.hh"
#include "JMath/JVectorND.hh"
#include "JMath/JMatrixNS.hh"
#include "JMath/JMathTestkit.hh"
#include "Jeep/JParser.hh"
#include "Jeep/JMessage.hh"

Go to the source code of this file.

Functions

int main (int argc, char **argv)
 

Detailed Description

Example program to test inversion of symmetric matrix.

Author
mdejong

Definition in file JMatrixNS.cc.

Function Documentation

int main ( int  argc,
char **  argv 
)

Definition at line 24 of file JMatrixNS.cc.

25 {
26  using namespace std;
27  using namespace JPP;
28 
29  int N;
30  double precision;
31  size_t numberOfInversions;
32  UInt_t seed;
33  int debug;
34 
35  try {
36 
37  JParser<> zap("Example program to test inversion of symmetric matrix.");
38 
39  zap['N'] = make_field(N);
40  zap['e'] = make_field(precision) = 1.0e-6;
41  zap['n'] = make_field(numberOfInversions) = 1;
42  zap['S'] = make_field(seed) = 0;
43  zap['d'] = make_field(debug) = 3;
44 
45  zap(argc, argv);
46  }
47  catch(const exception &error) {
48  FATAL(error.what() << endl);
49  }
50 
51 
52  gRandom->SetSeed(seed);
53 
54  numberOfInversions += (numberOfInversions%2 == 0 ? 1 : 0);
55 
56  const Double_t xmin = -1.0;
57  const Double_t xmax = +1.0;
58 
59  JMatrixNS A(N);
60 
61  for (int i = 0; i != N; ++i) {
62 
63  A(i,i) = gRandom->Uniform(xmin,xmax);
64 
65  for (int j = 0; j != i; ++j) {
66  A(j,i) = A(i,j) = gRandom->Uniform(xmin,xmax);
67  }
68  }
69 
70  DEBUG("Matrix A" << endl);
71  DEBUG(A << endl);
72 
73  try {
74 
75  JMatrixNS B(A);
76 
77  const chrono::steady_clock::time_point t0 = chrono::steady_clock::now();
78 
79  for (size_t i = numberOfInversions; i != 0; --i) {
80  B.invert();
81  }
82 
83  const chrono::steady_clock::time_point t1 = chrono::steady_clock::now();
84 
85  NOTICE("Elapsed time (Jpp) [us] " << setw(8) << chrono::duration_cast<chrono::microseconds>(t1 - t0).count() << endl);
86  DEBUG("Matrix A^-1" << endl);
87  DEBUG(B << endl);
88 
89  JMatrixNS C(A);
90 
91  C.mul(B);
92 
93  DEBUG("Matrix A x A^-1" << endl);
94  DEBUG(C << endl);
95 
96  NOTICE("A x A^-1 = I ? " << C.isIdentity(precision) << endl);
97 
98  if (!C.isIdentity(precision)) {
99  ERROR("Matrix A x A^-1 /= I" << endl);
100  }
101 
102  ASSERT(C.isIdentity(precision));
103  }
104  catch (const JException& error) {
105  FATAL(error << endl);
106  }
107 
108  try {
109 
110  TMatrixTSym<double> B(A.size());
111 
112  for (size_t row = 0; row != A.size(); ++row) {
113  for (size_t col = 0; col != A.size(); ++col) {
114  B(row, col) = A(row, col);
115  }
116  }
117 
118  const chrono::steady_clock::time_point t0 = chrono::steady_clock::now();
119 
120  for (size_t i = numberOfInversions; i != 0; --i) {
121  B.InvertFast();
122  }
123 
124  const chrono::steady_clock::time_point t1 = chrono::steady_clock::now();
125 
126  NOTICE("Elapsed time (ROOT) [us] " << setw(8) << chrono::duration_cast<chrono::microseconds>(t1 - t0).count() << endl);
127  }
128  catch (const JException& error) {
129  FATAL(error << endl);
130  }
131 
132  try {
133 
134  JMatrixNS B(A);
135 
136  B.invert();
137 
138  JVectorND X(N);
139 
140  for (int i = 0; i != N; ++i) {
141  X[i] = gRandom->Uniform(xmin,xmax);
142  }
143 
144  JVectorND Y(N);
145 
146  for (int i = 0; i != N; ++i) {
147  for (int j = 0; j != N; ++j) {
148  Y[i] += B(i,j) * X[j];
149  }
150  }
151 
152  JMatrixNS C(A);
153 
154  C.solve(X);
155 
156  for (int i = 0; i != N; ++i) {
157 
158  DEBUG(setw(4) << i << ' ' << FIXED(9,5) << X[i] << ' ' << FIXED(9,5) << Y[i] << endl);
159 
160  ASSERT(fabs(X[i] - Y[i]) <= precision);
161  }
162  }
163  catch (const JException& error) {
164  FATAL(error << endl);
165  }
166 
167  try {
168 
169  JMatrixNS B(A);
170 
171  B.invert();
172 
173  const double big = 1.0e5;
174 
175  for (int i = 0; i != N; ++i) {
176 
177  A(i,i) += big;
178 
179  const chrono::steady_clock::time_point t0 = chrono::steady_clock::now();
180 
181  B.update(i, big);
182 
183  const chrono::steady_clock::time_point t1 = chrono::steady_clock::now();
184 
185  NOTICE("Elapsed time [us] " << setw(8) << chrono::duration_cast<chrono::microseconds>(t1 - t0).count() << endl);
186  NOTICE("Pivot " << setw(2) << i << '+' << big << ' ' << (JMatrixNS(A).mul(B).isIdentity(precision) ? "okay" : "error") << endl);
187 
188  ASSERT(JMatrixNS(A).mul(B).isIdentity(precision));
189 
190  A(i,i) -= big;
191 
192  B.update(i, -big);
193  }
194  }
195  catch (const JException& error) {
196  FATAL(error << endl);
197  }
198 
199  {
200  A.reset();
201 
202  for (int i = 0; i != N; ++i) {
203  for (int j = 0; j != N; ++j) {
204  ASSERT(A(i,j) == 0, "Test reset (" << i << "," << j << ")");
205  }
206  }
207  }
208 
209  return 0;
210 }
const double xmax
Definition: JQuadrature.cc:24
Utility class to parse command line options.
Definition: JParser.hh:1711
then usage $script< input file >[option] nPossible options count
Definition: JVolume1D.sh:31
then fatal Wrong number of arguments fi set_variable STRING $argv[1] set_variable DETECTORXY_TXT $WORKDIR $DETECTORXY_TXT tail read X Y CHI2 RMS printf optimum n $X $Y $CHI2 $RMS awk v Y
Auxiliary data structure for floating point format specification.
Definition: JManip.hh:446
static const double C
Physics constants.
#define ASSERT(A,...)
Assert macro.
Definition: JMessage.hh:90
#define make_field(A,...)
macro to convert parameter to JParserTemplateElement object
Definition: JParser.hh:2158
#define NOTICE(A)
Definition: JMessage.hh:64
#define ERROR(A)
Definition: JMessage.hh:66
#define FATAL(A)
Definition: JMessage.hh:67
const double xmin
Definition: JQuadrature.cc:23
then usage $script< input file >[option[primary[working directory]]] nWhere option can be N
Definition: JMuonPostfit.sh:40
no fit printf nominal n $STRING awk v X
int j
Definition: JPolint.hh:792
source $JPP_DIR setenv csh $JPP_DIR &dev null eval JShellParser o a A
int debug
debug level
#define DEBUG(A)
Message macros.
Definition: JMessage.hh:62