13 #ifndef FASTMATRIXMATHHELPER_H 14 #define FASTMATRIXMATHHELPER_H 1 32 template <
typename T,
unsigned int DIM>
36 static constexpr
unsigned int Dim = DIM;
38 using Matrix_t = std::array<Data_t, Dim * Dim>;
82 template <
typename T,
unsigned int DIM>
101 return InvertMatrix(mat, Determinant(mat));
107 return InvertSymmetricMatrix(mat, Determinant(mat));
124 template <
unsigned int NCols>
127 return row * NCols +
col;
130 template <
unsigned int N>
132 static constexpr
size_t index(
unsigned int row,
unsigned int col)
134 return MatrixIndex<N>(row,
col);
138 template <
typename T,
145 static T compute(T
const* data);
149 template <
typename T,
unsigned int N,
unsigned int R,
unsigned int C>
152 static_assert(R < N,
"invalid row index specified");
153 static_assert(C < N,
"invalid column index specified");
155 static T
compute(T
const* data) {
return data[index(R, C)]; }
159 template <
typename T,
167 static_assert(R1 < N,
"invalid row index specified");
168 static_assert(R2 < N,
"invalid row index specified");
169 static_assert(C1 < N,
"invalid column index specified");
170 static_assert(C2 < N,
"invalid column index specified");
172 static constexpr T
sqr(T v) {
return v * v; }
176 return data[index(R1, C1)] * data[index(R2, C2)] -
177 data[index(R1, C2)] * data[index(R2, C1)];
186 template <
typename T,
196 static_assert(R1 < N,
"invalid row index specified");
197 static_assert(R2 < N,
"invalid row index specified");
198 static_assert(R3 < N,
"invalid row index specified");
199 static_assert(C1 < N,
"invalid column index specified");
200 static_assert(C2 < N,
"invalid column index specified");
201 static_assert(C3 < N,
"invalid column index specified");
202 template <
unsigned int sR1,
unsigned int sR2,
unsigned int sC1,
unsigned int sC2>
213 template <
typename T,
226 static_assert(R1 < N,
"invalid row index specified");
227 static_assert(R2 < N,
"invalid row index specified");
228 static_assert(R3 < N,
"invalid row index specified");
229 static_assert(R4 < N,
"invalid row index specified");
230 static_assert(C1 < N,
"invalid column index specified");
231 static_assert(C2 < N,
"invalid column index specified");
232 static_assert(C3 < N,
"invalid column index specified");
233 static_assert(C4 < N,
"invalid column index specified");
234 template <
unsigned int sR1,
251 template <
typename T>
254 static constexpr
unsigned int Dim = Base_t::Dim;
274 return InvertMatrix(mat, Determinant(mat));
280 return InvertSymmetricMatrix(mat, Determinant(mat));
286 template <
typename T>
289 static constexpr
unsigned int Dim = Base_t::Dim;
309 return InvertMatrix(mat, Determinant(mat));
315 return InvertSymmetricMatrix(mat, Determinant(mat));
321 template <
typename T>
324 static constexpr
unsigned int Dim = Base_t::Dim;
344 return InvertMatrix(mat, Determinant(mat));
350 return InvertSymmetricMatrix(mat, Determinant(mat));
359 template <
typename T,
unsigned int DIM>
367 for (
size_t r = 0;
r <
Dim; ++
r) {
369 for (
size_t c = 0; c <
Dim; ++c)
370 elem += *(mat_row++) * vec[c];
376 template <
typename T>
381 Inverse[0 *
Dim + 0] =
mat[1 *
Dim + 1] / det;
382 Inverse[0 *
Dim + 1] = -
mat[0 *
Dim + 1] / det;
383 Inverse[1 *
Dim + 0] = -
mat[1 *
Dim + 0] / det;
384 Inverse[1 *
Dim + 1] =
mat[0 *
Dim + 0] / det;
388 template <
typename T>
393 Inverse[0 *
Dim + 0] =
mat[1 *
Dim + 1] / det;
394 Inverse[0 *
Dim + 1] = Inverse[1 *
Dim + 0] = -
mat[1 *
Dim + 0] / det;
395 Inverse[1 *
Dim + 1] =
mat[0 *
Dim + 0] / det;
399 template <
typename T>
425 template <
typename T>
436 Inverse[0 *
Dim + 1] = Inverse[1 *
Dim + 0] =
438 Inverse[0 *
Dim + 2] = Inverse[2 *
Dim + 0] =
441 Inverse[1 *
Dim + 2] = Inverse[2 *
Dim + 1] =
447 template <
typename T>
480 template <
typename T>
491 Inverse[0 *
Dim + 1] = Inverse[1 *
Dim + 0] =
493 Inverse[0 *
Dim + 2] = Inverse[2 *
Dim + 0] =
495 Inverse[0 *
Dim + 3] = Inverse[3 *
Dim + 0] =
498 Inverse[1 *
Dim + 2] = Inverse[2 *
Dim + 1] =
500 Inverse[1 *
Dim + 3] = Inverse[3 *
Dim + 1] =
503 Inverse[2 *
Dim + 3] = Inverse[3 *
Dim + 2] =
509 #endif // FASTMATRIXMATHHELPER_H
typename Base_t::Matrix_t Matrix_t
static Data_t Determinant(Matrix_t const &mat)
Computes the determinant of a matrix.
Namespace for general, non-LArSoft-specific utilities.
static Matrix_t InvertSymmetricMatrix(Matrix_t const &mat)
Computes the determinant of a matrix.
typename Base_t::Data_t Data_t
static constexpr T sqr(T v)
static Matrix_t InvertMatrix(Matrix_t const &mat)
Computes the determinant of a matrix.
static T compute(T const *data)
std::array< Data_t, Dim *Dim > Matrix_t
typename Base_t::Data_t Data_t
static T compute(T const *data)
static constexpr size_t index(unsigned int row, unsigned int col)
static constexpr Data_t sqr(Data_t v)
static Matrix_t InvertMatrix(Matrix_t const &mat)
Computes the determinant of a matrix.
static T compute(T const *data)
Classes gathering simple statistics.
static Matrix_t InvertMatrix(Matrix_t const &mat, Data_t det)
Computes the determinant of a matrix, using the provided determinant.
typename Base_t::Matrix_t Matrix_t
static Matrix_t InvertSymmetricMatrix(Matrix_t const &mat, Data_t det)
Base class with common definitions for "fast" matrix operations.
static T compute(T const *data)
Provides "fast" matrix operations.
static T compute(T const *data)
typename Base_t::Data_t Data_t
typename Base_t::Matrix_t Matrix_t
static Matrix_t InvertSymmetricMatrix(Matrix_t const &mat)
Computes the determinant of a matrix.
static Matrix_t InvertMatrix(Matrix_t const &mat)
Computes the determinant of a matrix.
static Matrix_t InvertSymmetricMatrix(Matrix_t const &mat)
Computes the determinant of a matrix.
static constexpr unsigned int Dim
matrix dimensions
static Data_t Determinant(Matrix_t const &mat)
Computes the determinant of a matrix.
static Vector_t MatrixVectorProduct(Matrix_t const &mat, Vector_t const &vec)
Returns the product of a square matrix times a column vector.
static Matrix_t InvertMatrix(Matrix_t const &mat)
Computes the determinant of a matrix.
constexpr size_t MatrixIndex(unsigned int row, unsigned int col)
static Matrix_t InvertSymmetricMatrix(Matrix_t const &mat)
Computes the determinant of a matrix.
std::array< Data_t, Dim > Vector_t
LArSoft-specific namespace.
static Data_t Determinant(Matrix_t const &mat)
Computes the determinant of a matrix.