11#ifndef KF_UTILS_KfMatrixSym_h
12#define KF_UTILS_KfMatrixSym_h 1
18#include <boost/serialization/access.hpp>
24#if !defined(NO_ROOT) && !XPU_IS_HIP_CUDA
35 template<
typename T,
int N>
36 class MatrixSym :
public std::array<T, N*(N + 1) / 2> {
57 template<
int i,
int j>
74 template<
int i,
int j>
84 template<
typename... Args>
88 "MatrixSym::Reset: number of arguments does not match matrix dimension ");
112 template<DoPr
intDebug FlagPr
intDebug = DoPr
intDebug::Y>
116 template<DoPr
intDebug FlagPr
intDebug = DoPr
intDebug::Y>
122 template<DoPr
intDebug FlagPr
intDebug = DoPr
intDebug::Y>
128 template<
class Archive>
131 const std::array<T, kNofElements>& self = *
this;
137 template<
int index,
typename... Args>
148#if !defined(NO_ROOT) && !XPU_IS_HIP_CUDA
167 template<
typename T,
int N>
171 s.setf(std::ios::scientific, std::ios::floatfield);
172 s << std::setprecision(6);
174 if constexpr (std::is_same_v<T, fvec>) {
179 for (
int j = 0; j <= i; j++) {
187 for (
unsigned int ivec = 0; ivec < fvec::Size; ++ivec) {
188 s <<
"Vector component " << ivec <<
":" << std::endl;
196 for (
int j = 0; j <= i; j++) {
206 template<
typename T,
int N>
210 s.setf(std::ios::scientific, std::ios::floatfield);
211 s << std::setprecision(6);
213 if constexpr (std::is_same_v<T, fvec>) {
218 double sigi =
sqrt(
operator()(i, i)[iv]);
219 for (
int j = 0; j < i; j++) {
220 double sigj =
sqrt(
operator()(j, j)[iv]);
222 double corr = cij / (sigi * sigj);
225 s <<
" " << sigi << std::endl;
230 for (
unsigned int ivec = 0; ivec < fvec::Size; ++ivec) {
231 s <<
"Vector component " << ivec <<
":" << std::endl;
239 double sigi =
sqrt(
operator()(i, i));
240 for (
int j = 0; j < i; j++) {
241 double sigj =
sqrt(
operator()(j, j));
243 double corr = cij / (sigi * sigj);
246 s <<
" " << sigi << std::endl;
253 template<
typename T,
int N>
254 template<DoPr
intDebug FlagPr
intDebug>
261 for (
int j = 0; j <= i; j++) {
267 (*ss) <<
" Cov matrix element (" << i <<
"," << j <<
") is undefined: " << val << std::endl;
277 template<
typename T,
int N>
278 template<DoPr
intDebug FlagPr
intDebug>
293 (*ss) <<
" C[" << i <<
"," << i <<
"] is not positive: " << val << std::endl;
304 for (
int j = 0; j < i; j++) {
305 double tolerance = 1.0;
309 if (cij * cij > tolerance * (cii * cjj)) {
313 (*ss) <<
" Correlation [" << i <<
"," << j <<
"] is too large: " << cij /
sqrt(cii * cjj) << std::endl;
324 for (
int j = 1; j < i; j++) {
325 for (
int m = 0; m < j; m++) {
326 double tolerance = 1.0;
333 if (Cxx * Cyz * Cyz + Cyy * Cxz * Cxz + Czz * Cxy * Cxy
334 > tolerance * (Cxx * Cyy * Czz + 2. * Cxy * Cyz * Cxz)) {
337 double Kxy = Cxy /
sqrt(Cxx * Cyy);
338 double Kxz = Cxz /
sqrt(Cxx * Czz);
339 double Kyz = Cyz /
sqrt(Cyy * Czz);
341 (*ss) <<
" Correlations between parameters " << i <<
", " << j <<
", " << m <<
" are wrong: " << Kxy
342 <<
" " << Kxz <<
" " << Kyz << std::endl
343 <<
" inequation: " << Kxy * Kxy + Kxz * Kxz + Kyz * Kyz <<
" > " << 1 + 2 * Kxy * Kxz * Kyz
355 (*ss) <<
"Matrix elements are not consistent: " << std::endl << std::endl;
364 template<
typename T,
int N>
365 template<DoPr
intDebug FlagPr
intDebug>
370 if constexpr (std::is_same_v<T, fvec>) {
374 assert(nSimdFilled <=
size);
375 if (nSimdFilled < 0) {
380 for (
int i = 0; i < nSimdFilled; ++i) {
387 (*ss) <<
"MatrixSym parameters are not consistent: " << std::endl << std::endl;
388 if (nSimdFilled ==
size) {
389 (*ss) <<
" All vector elements are filled " << std::endl << std::endl;
392 (*ss) <<
" Only first " << nSimdFilled <<
" vector elements are filled " << std::endl << std::endl;
std::string ToString(CbmCutId id)
Convert CbmCutId to a string representation.
Common constant definitions for the Kalman Filter library.
Collection of generic mathematical methods.
friend fvec sqrt(const fvec &a)
static constexpr size_t size()
The class describes a symmetric N x N matrix stored in a low-triangular way.
~MatrixSym()=default
Default destructor.
T & operator()(Tag< i, j >)
Get matrix element, ensuring that indices are known at compile time.
static constexpr int kNofElements
bool IsConsistent(int nSimdFilled=-1) const
Checks whether the covariance matrix elements are consistent.
T operator()(Tag< i, j >) const
Get matrix element, ensuring that indices are known at compile time.
std::string ToString(int iv=-1) const
Prints parameters to a string.
static constexpr int kDimension
MatrixSym()
default constructor
T & operator()(int i, int j)
Get matrix element, indices can be runtime values.
T operator()(int i, int j) const
Get matrix element, indices can be runtime values.
bool IsSimdEntryConsistent(int iv) const
Checks whether SIMD entry iv is consistent.
void SetDiagonal(T val, Args... args)
Helper method to set diagonal elements recursively.
bool IsFinite(std::stringstream *ss) const
Checks whether some parameters are finite.
std::string ToStringCorrelations(int iv=-1) const
Prints correlations to a string (assuming the matrix is a covariance matrix)
void serialize(Archive &ar, const unsigned int)
bool IsSimdEntryConsistent(std::stringstream *ss, int iv) const
Checks whether SIMD entry iv is consistent.
bool IsFinite() const
Checks whether some parameters are finite.
bool IsConsistent(std::stringstream *ss, int nSimdFilled=-1) const
Checks whether the covariance matrix elements are consistent.
void Reset(Args... args)
Resets to 0, diagonal elements to given values.
static constexpr size_t size()
constexpr int SymIndex(int i, int j)
Get matrix element for a symmetrix matrix stored in a low-triangular array.
double GetSimdEntry(DataT v, int k)
bool IsFinite(const T &val)
Checks whether a variable of a particular type is finite.
MatrixSym< fscal, N > MatrixSymS
@ Y
Print debug information.
MatrixSym< double, N > MatrixSymD
@ N
Do not fit the time component.
MatrixSym< float, N > MatrixSymF
A generic tag for tag dispatching.