Jpp  15.0.3
the software that should make you happy
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JMatrixNS.cc
Go to the documentation of this file.
1 #include <string>
2 #include <iostream>
3 #include <iomanip>
4 
5 #include "TRandom3.h"
6 #include "TMatrixTSym.h"
7 
8 #include "JLang/JException.hh"
9 #include "JMath/JMatrixNS.hh"
10 #include "JMath/JMathTestkit.hh"
11 
12 #include "Jeep/JTimer.hh"
13 #include "Jeep/JParser.hh"
14 #include "Jeep/JMessage.hh"
15 
16 
17 /**
18  * \file
19  *
20  * Example program to test inversion of symmetric matrix.
21  * \author mdejong
22  */
23 int main(int argc, char**argv)
24 {
25  using namespace std;
26  using namespace JPP;
27 
28  int N;
29  double precision;
30  size_t numberOfInversions;
31  UInt_t seed;
32  int debug;
33 
34  try {
35 
36  JParser<> zap("Example program to test inversion of symmetric matrix.");
37 
38  zap['N'] = make_field(N);
39  zap['e'] = make_field(precision) = 1.0e-6;
40  zap['n'] = make_field(numberOfInversions) = 1;
41  zap['S'] = make_field(seed) = 0;
42  zap['d'] = make_field(debug) = 3;
43 
44  zap(argc, argv);
45  }
46  catch(const exception &error) {
47  FATAL(error.what() << endl);
48  }
49 
50 
51  gRandom->SetSeed(seed);
52 
53  const Double_t xmin = -1.0;
54  const Double_t xmax = +1.0;
55 
56  JMatrixNS A(N);
57 
58  for (int i = 0; i != N; ++i) {
59 
60  A(i,i) = gRandom->Uniform(xmin,xmax);
61 
62  for (int j = 0; j != i; ++j) {
63  A(j,i) = A(i,j) = gRandom->Uniform(xmin,xmax);
64  }
65  }
66 
67 
68  try {
69 
70  DEBUG("Matrix A" << endl);
71  DEBUG(A << endl);
72 
73  JMatrixNS B(A);
74 
75  JTimer timer;
76 
77  timer.start();
78 
79  for (size_t i = 2*(numberOfInversions/2) + 1; i != 0; --i) {
80  B.invert();
81  }
82 
83  timer.stop();
84 
85  NOTICE("Elapsed time [us] " << setw(8) << timer.usec_wall << endl);
86  DEBUG("Matrix A^-1" << endl);
87  DEBUG(B << endl);
88 
89  {
90  TMatrixTSym<double> R(B.size());
91 
92  for (size_t row = 0; row != B.size(); ++row) {
93  for (size_t col = 0; col != B.size(); ++col) {
94  R(row, col) = B(row, col);
95  }
96  }
97 
98  timer.start();
99 
100  for (size_t i = 2*(numberOfInversions/2) + 1; i != 0; --i) {
101  R.InvertFast();
102  }
103 
104  timer.stop();
105 
106  NOTICE("Elapsed time (ROOT) [us] " << setw(8) << timer.usec_wall << endl);
107  }
108 
109  JMatrixNS C(A);
110 
111  C.mul(B);
112 
113  DEBUG("Matrix A x A^-1" << endl);
114  DEBUG(C << endl);
115 
116  NOTICE("A x A^-1 = I ? " << C.isIdentity(precision) << endl);
117 
118  if (!C.isIdentity(precision)) {
119  ERROR("Matrix A x A^-1 /= I" << endl);
120  }
121 
122  ASSERT (C.isIdentity(precision));
123 
124  const double big = 1.0e5;
125 
126  for (int i = 0; i != N; ++i) {
127 
128  A(i,i) += big;
129 
130  timer.reset();
131  timer.start();
132 
133  B.update(i, big);
134 
135  timer.stop();
136 
137  NOTICE("Elapsed time [us] " << setw(8) << timer.usec_wall << endl);
138  NOTICE("Pivot " << setw(2) << i << '+' << big << ' ' << (JMatrixNS(A).mul(B).isIdentity(precision) ? "okay" : "error") << endl);
139 
140  ASSERT(JMatrixNS(A).mul(B).isIdentity(precision));
141 
142  A(i,i) -= big;
143 
144  B.update(i, -big);
145  }
146 
147  }
148  catch (const JException& error) {
149  FATAL(error << endl);
150  }
151 
152  return 0;
153 }
Utility class to parse command line options.
Definition: JParser.hh:1500
Exceptions.
int main(int argc, char *argv[])
Definition: Main.cc:15
then JShowerPostfit f $INPUT_FILE o $OUTPUT_FILE N
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:1961
#define NOTICE(A)
Definition: JMessage.hh:64
#define ERROR(A)
Definition: JMessage.hh:66
int debug
debug level
Definition: JSirene.cc:63
then usage $script[distance] fi case set_variable R
Definition: JDrawLED.sh:43
General purpose messaging.
#define FATAL(A)
Definition: JMessage.hh:67
Utility class to parse command line options.
then usage $script< input_file >< detector_file > fi set_variable OUTPUT_DIR set_variable SELECTOR JDAQTimesliceL1 set_variable DEBUG case set_variable DEBUG
int j
Definition: JPolint.hh:666
source $JPP_DIR setenv csh $JPP_DIR &dev null eval JShellParser o a A