1#ifndef RIVET_MATH_MATRIXN
2#define RIVET_MATH_MATRIXN
4#include "Rivet/Math/MathConstants.hh"
5#include "Rivet/Math/MathUtils.hh"
6#include "Rivet/Math/Vectors.hh"
8#include "Rivet/Math/eigen3/Dense"
33 friend Matrix<M> add(
const Matrix<M>&,
const Matrix<M>&);
35 friend Matrix<M> multiply(
const double,
const Matrix<M>&);
37 friend Matrix<M> multiply(
const Matrix<M>&,
const Matrix<M>&);
41 friend Matrix<M> divide(
const Matrix<M>&,
const double);
46 static Matrix<N> mkZero() {
53 for (
size_t i = 0; i < N; ++i) {
54 rtn.set(i, i, diag[i]);
59 static Matrix<N> mkIdentity() {
61 for (
size_t i = 0; i < N; ++i) {
71 : _matrix(EMatrix::Zero()) { }
73 Matrix& set(
const size_t i,
const size_t j,
const double value) {
75 _matrix(i, j) = value;
78 throw std::runtime_error(
"Attempted set access outside matrix bounds.");
83 double get(
const size_t i,
const size_t j)
const {
88 throw std::runtime_error(
"Attempted get access outside matrix bounds.");
92 Vector<N> getRow(
const size_t row)
const {
94 for (
size_t i = 0; i < N; ++i) {
95 rtn.
set(i, _matrix(row, i));
100 Matrix<N>& setRow(
const size_t row,
const Vector<N>& r) {
101 for (
size_t i = 0; i < N; ++i) {
102 _matrix(row, i) = r.get(i);
107 Vector<N> getColumn(
const size_t col)
const {
109 for (
size_t i = 0; i < N; ++i) {
110 rtn.
set(i, _matrix(i, col));
115 Matrix<N>& setColumn(
const size_t col,
const Vector<N>& c) {
116 for (
size_t i = 0; i < N; ++i) {
117 _matrix(i, col) = c.get(i);
122 Matrix<N> transpose()
const {
124 tmp._matrix = _matrix.transpose();
136 tmp._matrix = _matrix.
inverse();
142 return _matrix.determinant();
148 for (
size_t i = 0; i < N; ++i) {
158 rtn._matrix = -_matrix;
163 constexpr size_t size()
const {
169 for (
size_t i = 0; i < N; ++i) {
170 for (
size_t j = 0; j < N; ++j) {
179 for (
size_t i = 0; i < N; ++i) {
180 for (
size_t j = i; j < N; ++j) {
181 if (!
Rivet::isZero(_matrix(i, j) - other._matrix(i, j)))
return false;
189 return isEqual(this->transpose());
194 for (
size_t i = 0; i < N; ++i) {
195 for (
size_t j = 0; j < N; ++j) {
196 if (i == j)
continue;
204 return _matrix == a._matrix;
207 bool operator!=(
const Matrix<N>& a)
const {
208 return _matrix != a._matrix;
227 Matrix<N>& operator*=(
const Matrix<N>& m) {
228 _matrix *= m._matrix;
232 Matrix<N>& operator*=(
const double a) {
237 Matrix<N>& operator/=(
const double a) {
242 Matrix<N>& operator+=(
const Matrix<N>& m) {
243 _matrix += m._matrix;
247 Matrix<N>& operator-=(
const Matrix<N>& m) {
248 _matrix -= m._matrix;
254 using EMatrix = RivetEigen::Matrix<double, N, N>;
265 result._matrix = a._matrix + b._matrix;
287 rtn._matrix = a * m._matrix;
293 return multiply(a, m);
298 return multiply(1 / a, m);
303 return multiply(a, m);
308 return multiply(a, m);
314 tmp._matrix = a._matrix * b._matrix;
320 return multiply(a, b);
327 tmp._vec = a._matrix * b._vec;
333 return multiply(a, b);
345 return m.transpose();
355 return m.determinant();
359 inline double trace(
const Matrix<N>& m) {
370 std::ostringstream ss;
372 for (
size_t i = 0; i < m.
size(); ++i) {
374 for (
size_t j = 0; j < m.
size(); ++j) {
375 const double e = m.get(i, j);
399 for (
size_t i = 0; i < N; ++i) {
400 for (
size_t j = 0; j < N; ++j) {
401 const double a = ma.get(i, j);
402 const double b = mb.get(i, j);
413 return m.
isZero(tolerance);
General -dimensional mathematical matrix object.
Definition MatrixN.hh:30
double trace() const
Calculate trace.
Definition MatrixN.hh:146
double det() const
Calculate determinant.
Definition MatrixN.hh:141
Matrix< N > operator-() const
Negate.
Definition MatrixN.hh:156
bool isZero(double tolerance=1E-5) const
Index-wise check for nullness, allowing for numerical precision.
Definition MatrixN.hh:168
constexpr size_t size() const
Get dimensionality.
Definition MatrixN.hh:163
bool isDiag() const
Check that all off-diagonal elements are zero, allowing for numerical precision.
Definition MatrixN.hh:193
Matrix< N > inverse() const
Calculate inverse.
Definition MatrixN.hh:134
bool isSymm() const
Check for symmetry under transposition.
Definition MatrixN.hh:188
bool isEqual(Matrix< N > other) const
Check for index-wise equality, allowing for numerical precision.
Definition MatrixN.hh:178
A minimal base class for -dimensional vectors.
Definition VectorN.hh:23
Vector< N > & set(const size_t index, const double value)
Set indexed value.
Definition VectorN.hh:63
double E(const ParticleBase &p)
Unbound function access to E.
Definition ParticleBaseUtils.hh:829
Definition LHCbCommon.hh:9
double subtract(double a, double b, double tolerance=1e-5)
Subtract two numbers with FP fuzziness.
Definition MathUtils.hh:242
bool operator==(const Cut &a, const Cut &b)
Compare two cuts for equality, forwards to the cut-specific implementation.
Definition Cuts.hh:45
std::ostream & operator<<(std::ostream &os, const AnalysisInfo &ai)
Stream an AnalysisInfo as a text description.
Definition AnalysisInfo.hh:463
std::enable_if_t< std::is_floating_point_v< NUM >, bool > isZero(NUM val, double tolerance=1e-8)
Compare a number to zero.
Definition MathUtils.hh:23
double add(double a, double b, double tolerance=1e-5)
Add two numbers with FP fuzziness.
Definition MathUtils.hh:248
std::string toString(const AnalysisInfo &ai)
String representation.
std::enable_if_t< std::is_arithmetic_v< N1 > &&std::is_arithmetic_v< N2 > &&(std::is_floating_point_v< N1 >||std::is_floating_point_v< N2 >), bool > fuzzyEquals(N1 a, N2 b, double tolerance=1e-5)
Compare two numbers for equality with a degree of fuzziness.
Definition MathUtils.hh:66