25{
28
29 int N;
30 double precision;
31 size_t numberOfInversions;
32 ULong_t seed;
34
35 try {
36
37 JParser<> zap(
"Example program to test inversion of symmetric matrix.");
38
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
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);
72
73 try {
74
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);
88
90
91 C.mul(B);
92
93 DEBUG(
"Matrix A x A^-1" << 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 }
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 }
129 FATAL(error << endl);
130 }
131
132 try {
133
135
136 B.invert();
137
139
140 for (int i = 0; i != N; ++i) {
141 X[i] = gRandom->Uniform(xmin,xmax);
142 }
143
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
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 }
164 FATAL(error << endl);
165 }
166
167 try {
168
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
189
190 A(i,i) -= big;
191
192 B.update(i, -big);
193 }
194 }
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}
#define DEBUG(A)
Message macros.
#define ASSERT(A,...)
Assert macro.
#define make_field(A,...)
macro to convert parameter to JParserTemplateElement object
Utility class to parse command line options.
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
Auxiliary data structure for floating point format specification.