42 if (!(mat<1.E100) || !(mat>-1.E100)){
43 Exception e(
"Tools::invertMatrix() - cannot invert matrix, entries too big (>1e100)",
49 if (mat.GetNrows() == 1){
50 if (determinant != NULL) *determinant = mat(0,0);
51 inv(0,0) = 1./mat(0,0);
55 if (mat.GetNrows() == 2){
56 double det = mat(0,0)*mat(1,1) - mat(1,0)*mat(1,0);
57 if (determinant != NULL) *determinant = det;
58 if(fabs(det) < 1E-50){
59 Exception e(
"Tools::invertMatrix() - cannot invert matrix , determinant = 0",
65 inv(0,0) = det * mat(1,1);
66 inv(0,1) = inv(1,0) = -det * mat(1,0);
67 inv(1,1) = det * mat(0,0);
72 if (useCramer && mat.GetNrows() <= 6){
73 Bool_t (*inversion)(TMatrixDSym&, Double_t*) = 0;
76 switch (mat.GetNrows()) {
78 inversion = TMatrixTSymCramerInv::Inv3x3;
break;
80 inversion = TMatrixTSymCramerInv::Inv4x4;
break;
82 inversion = TMatrixTSymCramerInv::Inv5x5;
break;
84 inversion = TMatrixTSymCramerInv::Inv6x6;
break;
87 Bool_t success = inversion(inv, determinant);
89 Exception e(
"Tools::invertMatrix() - cannot invert matrix, determinant = 0",
99 TDecompChol invertAlgo(mat, 1E-50);
101 status = invertAlgo.Invert(inv);
103 Exception e(
"Tools::invertMatrix() - cannot invert matrix, status = 0",
109 if (determinant != NULL) {
111 invertAlgo.Det(d1, d2);
112 *determinant = ldexp(d1, d2);
118 if (!(mat<1.E100) || !(mat>-1.E100)){
119 Exception e(
"Tools::invertMatrix() - cannot invert matrix, entries too big (>1e100)",
125 if (mat.GetNrows() == 1){
126 if (determinant != NULL) *determinant = mat(0,0);
127 mat(0,0) = 1./mat(0,0);
131 if (mat.GetNrows() == 2){
132 double *arr = mat.GetMatrixArray();
133 double det = arr[0]*arr[3] - arr[1]*arr[1];
134 if (determinant != NULL) *determinant = det;
135 if(fabs(det) < 1E-50){
136 Exception e(
"Tools::invertMatrix() - cannot invert matrix, determinant = 0",
143 temp[0] = det * arr[3];
144 temp[1] = -det * arr[1];
145 temp[2] = det * arr[0];
148 arr[1] = arr[2] = temp[1];
153 if (useCramer && mat.GetNrows() <= 6){
154 Bool_t (*inversion)(TMatrixDSym&, Double_t*) = 0;
155 switch (mat.GetNrows()) {
157 inversion = TMatrixTSymCramerInv::Inv3x3;
break;
159 inversion = TMatrixTSymCramerInv::Inv4x4;
break;
161 inversion = TMatrixTSymCramerInv::Inv5x5;
break;
163 inversion = TMatrixTSymCramerInv::Inv6x6;
break;
166 Bool_t success = inversion(mat, determinant);
168 Exception e(
"Tools::invertMatrix() - cannot invert matrix, determinant = 0",
178 TDecompChol invertAlgo(mat, 1E-50);
180 status = invertAlgo.Invert(mat);
182 Exception e(
"Tools::invertMatrix() - cannot invert matrix, status = 0",
188 if (determinant != NULL) {
190 invertAlgo.Det(d1, d2);
191 *determinant = ldexp(d1, d2);
217 size_t n = R.GetNrows();
218 for (
unsigned int i = 0; i < n; ++i) {
219 double sum = b(i, nCol);
220 for (
unsigned int j = 0; j < i; ++j) {
221 sum -= b(j, nCol)*R(j,i);
225 b(i, nCol) = sum / R(i,i);
251 int nCols = A.GetNcols();
252 int nRows = A.GetNrows();
253 assert(nRows >= nCols);
259 double *
const ak = A.GetMatrixArray();
261 double *
const u = (
double *)alloca(
sizeof(
double)*nRows);
264 for (
int k = 0; k < nCols; ++k) {
265 double akk = ak[k*nCols + k];
267 double sum = akk*akk;
269 for (
int i = k + 1; i < nRows; ++i) {
270 sum += ak[i*nCols + k]*ak[i*nCols + k];
271 u[i] = ak[i*nCols + k];
273 double sigma = sqrt(sum);
274 double beta = 1/(sum + sigma*fabs(akk));
276 u[k] = copysign(sigma + fabs(akk), akk);
280 for (
int i = k; i < nCols; ++i) {
282 for (
int j = k; j < nRows; ++j)
283 y += u[j]*ak[j*nCols + i];
286 for (
int j = k; j < nRows; ++j)
287 ak[j*nCols + i] -= u[j]*y;
292 for (
int i = 1; i < nCols; ++i)
293 for (
int j = 0; j < i; ++j)
294 ak[i*nCols + j] = 0.;
295 for (
int i = nCols; i < nRows; ++i)
296 for (
int j = 0; j < nCols; ++j)
297 ak[i*nCols + j] = 0.;
324 TDecompChol dec1(C1);
326 TDecompChol dec2(C2);
329 const TMatrixD& S1 = dec1.GetU();
330 const TMatrixD& S2 = dec2.GetU();
332 TMatrixD S1inv, S2inv;
336 TMatrixD A(2 * S1.GetNrows(), S1.GetNcols());
337 for (
int i = 0; i < S1.GetNrows(); ++i) {
338 for (
int j = 0; j < S2.GetNcols(); ++j) {
339 A(i, j) = S1inv(i, j);
340 A(i + S1.GetNrows(), j) = S2inv(i, j);
345 A.ResizeTo(S1.GetNrows(), S1.GetNrows());
350 result.ResizeTo(inv.GetNcols(), inv.GetNcols());
351 result = TMatrixDSym(TMatrixDSym::kAtA, inv);