/usr/include/root/Math/Dinv.h is in libroot-math-smatrix-dev 5.34.30-0ubuntu8.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 | // @(#)root/smatrix:$Id$
// Authors: T. Glebe, L. Moneta 2005
#ifndef ROOT_Math_Dinv
#define ROOT_Math_Dinv
// ********************************************************************
//
// source:
//
// type: source code
//
// created: 03. Apr 2001
//
// author: Thorsten Glebe
// HERA-B Collaboration
// Max-Planck-Institut fuer Kernphysik
// Saupfercheckweg 1
// 69117 Heidelberg
// Germany
// E-mail: T.Glebe@mpi-hd.mpg.de
//
// Description: square Matrix inversion
// Code was taken from CERNLIB::kernlib dfinv function, translated
// from FORTRAN to C++ and optimized.
// n: Order of the square matrix
// idim: First dimension of array A
//
// changes:
// 03 Apr 2001 (TG) creation
//
// ********************************************************************
#ifdef OLD_IMPL
#include "Math/Dfactir.h"
#include "Math/Dfinv.h"
#include "Math/Dsinv.h"
#endif
#ifndef ROOT_Math_CholeskyDecomp
#include "Math/CholeskyDecomp.h"
#endif
// #ifndef ROOT_Math_QRDecomposition
// #include "Math/QRDecomposition.h"
// #endif
namespace ROOT {
namespace Math {
/**
Matrix Inverter class
Class to specialize calls to Dinv. Dinv computes the inverse of a square
matrix if dimension idim and order n. The content of the matrix will be
replaced by its inverse. In case the inversion fails, the matrix content is
destroyed. Invert specializes Dinv by the matrix order. E.g. if the order
of the matrix is two, the routine Inverter<2> is called which implements
Cramers rule.
@author T. Glebe
*/
//==============================================================================
// Inverter class
//==============================================================================
template <unsigned int idim, unsigned int n = idim>
class Inverter {
public:
/// matrix inversion for a generic square matrix using LU factorization
/// (code originally from CERNLIB and then ported in C++ for CLHEP)
/// implementation is in file Math/MatrixInversion.icc
template <class MatrixRep>
static bool Dinv(MatrixRep& rhs) {
/* Initialized data */
unsigned int work[n+1] = {0};
typename MatrixRep::value_type det(0.0);
if (DfactMatrix(rhs,det,work) != 0) {
std::cerr << "Dfact_matrix failed!!" << std::endl;
return false;
}
int ifail = DfinvMatrix(rhs,work);
if (ifail == 0) return true;
return false;
} // Dinv
/// symmetric matrix inversion using
/// Bunch-kaufman pivoting method
/// implementation in Math/MatrixInversion.icc
template <class T>
static bool Dinv(MatRepSym<T,idim> & rhs) {
int ifail = 0;
InvertBunchKaufman(rhs,ifail);
if (ifail == 0) return true;
return false;
}
/**
LU Factorization method for inversion of general square matrices
(see implementation in Math/MatrixInversion.icc)
*/
template <class T>
static int DfactMatrix(MatRepStd<T,idim,n> & rhs, T & det, unsigned int * work);
/**
LU inversion of general square matrices. To be called after DFactMatrix
(see implementation in Math/MatrixInversion.icc)
*/
template <class T>
static int DfinvMatrix(MatRepStd<T,idim,n> & rhs, unsigned int * work);
/**
Bunch-Kaufman method for inversion of symmetric matrices
*/
template <class T>
static void InvertBunchKaufman(MatRepSym<T,idim> & rhs, int &ifail);
}; // class Inverter
// fast inverter class using Cramer inversion
// by default use other default inversion
/**
Fast Matrix Inverter class
Class to specialize calls to Dinv. Dinv computes the inverse of a square
matrix if dimension idim and order n. The content of the matrix will be
replaced by its inverse. In case the inversion fails, the matrix content is
destroyed. Invert specializes Dinv by the matrix order. E.g. if the order
of the matrix is less than 5 , the class implements
Cramers rule.
Be careful that for matrix with high condition the accuracy of the Cramer rule is much poorer
@author L. Moneta
*/
template <unsigned int idim, unsigned int n = idim>
class FastInverter {
public:
///
template <class MatrixRep>
static bool Dinv(MatrixRep& rhs) {
return Inverter<idim,n>::Dinv(rhs);
}
template <class T>
static bool Dinv(MatRepSym<T,idim> & rhs) {
return Inverter<idim,n>::Dinv(rhs);
}
};
/** Inverter<0>.
In case of zero order, do nothing.
@author T. Glebe
*/
//==============================================================================
// Inverter<0>
//==============================================================================
template <>
class Inverter<0> {
public:
///
template <class MatrixRep>
inline static bool Dinv(MatrixRep&) { return true; }
};
/**
1x1 matrix inversion \f$a_{11} \to 1/a_{11}\f$
@author T. Glebe
*/
//==============================================================================
// Inverter<1>
//==============================================================================
template <>
class Inverter<1> {
public:
///
template <class MatrixRep>
static bool Dinv(MatrixRep& rhs) {
if (rhs[0] == 0.) {
return false;
}
rhs[0] = 1. / rhs[0];
return true;
}
};
/**
2x2 matrix inversion using Cramers rule.
@author T. Glebe
*/
//==============================================================================
// Inverter<2>: Cramers rule
//==============================================================================
template <>
class Inverter<2> {
public:
///
template <class MatrixRep>
static bool Dinv(MatrixRep& rhs) {
typedef typename MatrixRep::value_type T;
T det = rhs[0] * rhs[3] - rhs[2] * rhs[1];
if (det == T(0.) ) { return false; }
T s = T(1.0) / det;
T c11 = s * rhs[3];
rhs[2] = -s * rhs[2];
rhs[1] = -s * rhs[1];
rhs[3] = s * rhs[0];
rhs[0] = c11;
return true;
}
// specialization for the symmetric matrices
template <class T>
static bool Dinv(MatRepSym<T,2> & rep) {
T * rhs = rep.Array();
T det = rhs[0] * rhs[2] - rhs[1] * rhs[1];
if (det == T(0.)) { return false; }
T s = T(1.0) / det;
T c11 = s * rhs[2];
rhs[1] = -s * rhs[1];
rhs[2] = s * rhs[0];
rhs[0] = c11;
return true;
}
};
/**
3x3 direct matrix inversion using Cramer Rule
use only for FastInverter
*/
//==============================================================================
// FastInverter<3>
//==============================================================================
template <>
class FastInverter<3> {
public:
///
// use Cramer Rule
template <class MatrixRep>
static bool Dinv(MatrixRep& rhs);
template <class T>
static bool Dinv(MatRepSym<T,3> & rhs);
};
/**
4x4 matrix inversion using Cramers rule.
*/
template <>
class FastInverter<4> {
public:
///
template <class MatrixRep>
static bool Dinv(MatrixRep& rhs);
template <class T>
static bool Dinv(MatRepSym<T,4> & rhs);
};
/**
5x5 Matrix inversion using Cramers rule.
*/
template <>
class FastInverter<5> {
public:
///
template <class MatrixRep>
static bool Dinv(MatrixRep& rhs);
template <class T>
static bool Dinv(MatRepSym<T,5> & rhs);
};
// inverter for Cholesky
// works only for symmetric matrices and will produce a
// compilation error otherwise
template <unsigned int idim>
class CholInverter {
public:
///
template <class MatrixRep>
static bool Dinv(MatrixRep&) {
STATIC_CHECK( false, Error_cholesky_SMatrix_type_is_not_symmetric );
return false;
}
template <class T>
inline static bool Dinv(MatRepSym<T,idim> & rhs) {
CholeskyDecomp<T, idim> decomp(rhs);
return decomp.Invert(rhs);
}
};
} // namespace Math
} // namespace ROOT
#ifndef ROOT_Math_CramerInversion_icc
#include "CramerInversion.icc"
#endif
#ifndef ROOT_Math_CramerInversionSym_icc
#include "CramerInversionSym.icc"
#endif
#ifndef ROOT_Math_MatrixInversion_icc
#include "MatrixInversion.icc"
#endif
#endif /* ROOT_Math_Dinv */
|