#include #include #include #include "TRandom3.h" #include "TMatrixTSym.h" #include "JLang/JException.hh" #include "JMath/JMatrixNS.hh" #include "JMath/JMathTestkit.hh" #include "Jeep/JTimer.hh" #include "Jeep/JParser.hh" #include "Jeep/JMessage.hh" /** * \file * * Example program to test inversion of symmetric matrix. * \author mdejong */ int main(int argc, char**argv) { using namespace std; using namespace JPP; int N; double precision; size_t numberOfInversions; UInt_t seed; int debug; try { JParser<> zap("Example program to test inversion of symmetric matrix."); zap['N'] = make_field(N); zap['e'] = make_field(precision) = 1.0e-6; zap['n'] = make_field(numberOfInversions) = 1; zap['S'] = make_field(seed) = 0; zap['d'] = make_field(debug) = 3; zap(argc, argv); } catch(const exception &error) { FATAL(error.what() << endl); } gRandom->SetSeed(seed); const Double_t xmin = -1.0; const Double_t xmax = +1.0; JMatrixNS A(N); for (int i = 0; i != N; ++i) { A(i,i) = gRandom->Uniform(xmin,xmax); for (int j = 0; j != i; ++j) { A(j,i) = A(i,j) = gRandom->Uniform(xmin,xmax); } } try { DEBUG("Matrix A" << endl); DEBUG(A << endl); JMatrixNS B(A); JTimer timer; timer.start(); for (size_t i = 2*(numberOfInversions/2) + 1; i != 0; --i) { B.invert(); } timer.stop(); NOTICE("Elapsed time (Jpp) [us] " << setw(8) << timer.usec_wall << endl); DEBUG("Matrix A^-1" << endl); DEBUG(B << endl); { TMatrixTSym R(B.size()); for (size_t row = 0; row != B.size(); ++row) { for (size_t col = 0; col != B.size(); ++col) { R(row, col) = B(row, col); } } timer.start(); for (size_t i = 2*(numberOfInversions/2) + 1; i != 0; --i) { R.InvertFast(); } timer.stop(); NOTICE("Elapsed time (ROOT) [us] " << setw(8) << timer.usec_wall << endl); } JMatrixNS C(A); C.mul(B); DEBUG("Matrix A x A^-1" << endl); DEBUG(C << endl); NOTICE("A x A^-1 = I ? " << C.isIdentity(precision) << endl); if (!C.isIdentity(precision)) { ERROR("Matrix A x A^-1 /= I" << endl); } ASSERT (C.isIdentity(precision)); const double big = 1.0e5; for (int i = 0; i != N; ++i) { A(i,i) += big; timer.reset(); timer.start(); B.update(i, big); timer.stop(); NOTICE("Elapsed time [us] " << setw(8) << timer.usec_wall << endl); NOTICE("Pivot " << setw(2) << i << '+' << big << ' ' << (JMatrixNS(A).mul(B).isIdentity(precision) ? "okay" : "error") << endl); ASSERT(JMatrixNS(A).mul(B).isIdentity(precision)); A(i,i) -= big; B.update(i, -big); } } catch (const JException& error) { FATAL(error << endl); } return 0; }