#include #include #include #include "TRandom3.h" #include "JLang/JException.hh" #include "JMath/JMatrix3S.hh" #include "JMath/JMathTestkit.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; double precision; int debug; try { JParser<> zap("Example program to test inversion and eigenvalue decomposition of 3D symmetric matrix."); zap['e'] = make_field(precision) = 1.0e-6; zap['d'] = make_field(debug) = 3; zap(argc, argv); } catch(const exception &error) { FATAL(error.what() << endl); } gRandom->SetSeed(0); JMatrix3S A = getRandom(); DEBUG("Matrix A" << endl); DEBUG(A << endl); NOTICE("Determinant A " << A.getDeterminant() << endl); try { JMatrix3S B(A); B.invert(); DEBUG("Matrix A^-1" << endl); DEBUG(B << endl); NOTICE("Determinant A^-1 = " << B.getDeterminant() << endl); JMatrix3D C(A * B); DEBUG("Matrix A x A^-1" << endl); DEBUG(C << endl); NOTICE("Determinant (A x A^-1) = " << C.getDeterminant() << endl); NOTICE("Determinant A x Determinant A^-1 = " << A.getDeterminant() * B.getDeterminant() << endl); NOTICE("A x A^-1 = I ? " << C.isIdentity(precision) << endl); if (!C.isIdentity(precision)) { ERROR("Matrix A x A^-1 /= I" << endl); } JMatrix3D D = C - JMatrix3D::getIdentity(); DEBUG("Matrix D = C - I" << endl); DEBUG(D << endl); DEBUG("Eigenvalues v of matrix A" << endl); JMatrix3S::eigen_values v = A.getEigenValues(); for (JMatrix3S::eigen_values::const_iterator i = v.cbegin(); i != v.cend(); ++i) { DEBUG(SCIENTIFIC(12,3) << *i); } DEBUG(endl); ASSERT(fabs(v[0]) > fabs(v[1])); ASSERT(fabs(v[1]) > fabs(v[2])); ASSERT(JMatrix3S(A).sub(v[0] * JMatrix3D::getIdentity()).getDeterminant() < precision); ASSERT(JMatrix3S(A).sub(v[1] * JMatrix3D::getIdentity()).getDeterminant() < precision); ASSERT(JMatrix3S(A).sub(v[2] * JMatrix3D::getIdentity()).getDeterminant() < precision); ASSERT(C.isIdentity(precision)); } catch (const JException& error) { FATAL(error << endl); } return 0; }