This macro shows several ways to invert a matrix .
Each method is a trade-off between accuracy of the inversion and speed. Which method to chose depends on "how well-behaved" the matrix is. This is best checked through a call to Condition(), available in each decomposition class. A second possibility (less preferred) would be to check the determinant
USAGE
This macro can be executed with Cling or ACLIC
- via the interpretor, do
double invertMatrix(const Matrix &matrix, Matrix &inverse)
- via ACLIC
R__EXTERN TSystem * gSystem
virtual int Load(const char *module, const char *entry="", Bool_t system=kFALSE)
Load a shared library.
--------------------------------------------------------
Inversion results for a (6,6) matrix
For each inversion procedure we check the maximum size
of the off-diagonal elements of Inv(A) * A
--------------------------------------------------------
1. Use .InvertFast(&det)
Maximum off-diagonal = 8.31175e-05
Determinant = 5.3673e-18
2. Use .Invert(&det)
Maximum off-diagonal = 1.74623e-10
Determinant = 5.3673e-18
3. Use TDecompLU
Maximum off-diagonal = 1.74623e-10
Determinant = 5.3673e-18
4. Use TDecompSVD on non-square matrix
Maximum off-diagonal = 5.45697e-12
Determinant = 1.34646e-11
#include <iostream>
{
if (msize < 2 || msize > 10) {
std::cout << "2 <= msize <= 10" <<std::endl;
return;
}
std::cout << "--------------------------------------------------------" <<std::endl;
std::cout << "Inversion results for a ("<<msize<<","<<msize<<") matrix" <<std::endl;
std::cout << "For each inversion procedure we check the maximum size " <<std::endl;
std::cout << "of the off-diagonal elements of Inv(A) * A " <<std::endl;
std::cout << "--------------------------------------------------------" <<std::endl;
std::cout << "1. Use .InvertFast(&det)" <<std::endl;
if (msize > 6)
std::cout << " for ("<<msize<<","<<msize<<") this is identical to .Invert(&det)" <<std::endl;
const Double_t U1_max_offdiag = (U1.Abs()).Max();
std::cout << " Maximum off-diagonal = " << U1_max_offdiag << std::endl;
std::cout << " Determinant = " << det1 << std::endl;
std::cout << "2. Use .Invert(&det)" << std::endl;
const Double_t U2_max_offdiag = (U2.Abs()).Max();
std::cout << " Maximum off-diagonal = " << U2_max_offdiag << std::endl;
std::cout << " Determinant = " << det2 << std::endl;
std::cout << "3. Use TDecompLU" << std::endl;
lu.Invert(H3);
lu.Det(d1_lu,d2_lu);
const Double_t U3_max_offdiag = (U3.Abs()).Max();
std::cout << " Maximum off-diagonal = " << U3_max_offdiag << std::endl;
std::cout << " Determinant = " << det3 << std::endl;
std::cout << "4. Use TDecompSVD on non-square matrix" << std::endl;
svd.Det(d1_svd,d2_svd);
const Double_t U4_max_offdiag = (U4.Abs()).Max();
std::cout << " Maximum off-diagonal = " << U4_max_offdiag << std::endl;
std::cout << " Determinant = " << det4 << std::endl;
}
THilbertMatrixT< Double_t > THilbertMatrixD
Single Value Decomposition class.
TMatrixT< Element > & InvertFast(Double_t *det=nullptr)
Invert the matrix and calculate its determinant, however upto (6x6) a fast Cramer inversion is used .
TMatrixT< Element > & Invert(Double_t *det=nullptr)
Invert the matrix and calculate its determinant.
LongDouble_t Power(LongDouble_t x, LongDouble_t y)
Returns x raised to the power y.
- Author
- Eddy Offermann
Definition in file invertMatrix.C.