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) {
70 Matrix() : _matrix(EMatrix::Zero()) {}
72 Matrix& set(
const size_t i,
const size_t j,
const double value) {
74 _matrix(i, j) = value;
76 throw std::runtime_error(
"Attempted set access outside matrix bounds.");
81 double get(
const size_t i,
const size_t j)
const {
85 throw std::runtime_error(
"Attempted get access outside matrix bounds.");
89 Vector<N> getRow(
const size_t row)
const {
91 for (
size_t i = 0; i < N; ++i) {
92 rtn.
set(i, _matrix(row,i));
97 Matrix<N>& setRow(
const size_t row,
const Vector<N>& r) {
98 for (
size_t i = 0; i < N; ++i) {
99 _matrix(row,i) = r.get(i);
104 Vector<N> getColumn(
const size_t col)
const {
106 for (
size_t i = 0; i < N; ++i) {
107 rtn.
set(i, _matrix(i,col));
112 Matrix<N>& setColumn(
const size_t col,
const Vector<N>& c) {
113 for (
size_t i = 0; i < N; ++i) {
114 _matrix(i,col) = c.get(i);
119 Matrix<N> transpose()
const {
121 tmp._matrix = _matrix.transpose();
133 tmp._matrix = _matrix.
inverse();
139 return _matrix.determinant();
145 for (
size_t i = 0; i < N; ++i) {
155 rtn._matrix = -_matrix;
160 constexpr size_t size()
const {
166 for (
size_t i=0; i < N; ++i) {
167 for (
size_t j=0; j < N; ++j) {
176 for (
size_t i=0; i < N; ++i) {
177 for (
size_t j=i; j < N; ++j) {
178 if (!
Rivet::isZero(_matrix(i,j) - other._matrix(i,j)) )
return false;
186 return isEqual(this->transpose());
191 for (
size_t i=0; i < N; ++i) {
192 for (
size_t j=0; j < N; ++j) {
193 if (i == j)
continue;
200 bool operator == (
const Matrix<N>& a)
const {
201 return _matrix == a._matrix;
204 bool operator != (
const Matrix<N>& a)
const {
205 return _matrix != a._matrix;
224 Matrix<N>& operator *= (
const Matrix<N>& m) {
225 _matrix *= m._matrix;
229 Matrix<N>& operator *= (
const double a) {
234 Matrix<N>& operator /= (
const double a) {
239 Matrix<N>& operator += (
const Matrix<N>& m) {
240 _matrix += m._matrix;
244 Matrix<N>& operator -= (
const Matrix<N>& m) {
245 _matrix -= m._matrix;
251 using EMatrix = RivetEigen::Matrix<double,N,N>;
263 result._matrix = a._matrix + b._matrix;
285 rtn._matrix = a * m._matrix;
291 return multiply(a, m);
296 return multiply(1/a, m);
301 return multiply(a, m);
306 return multiply(a, m);
312 tmp._matrix = a._matrix * b._matrix;
318 return multiply(a, b);
325 tmp._vec = a._matrix * b._vec;
331 return multiply(a, b);
343 return m.transpose();
353 return m.determinant();
357 inline double trace(
const Matrix<N>& m) {
368 std::ostringstream ss;
370 for (
size_t i = 0; i < m.
size(); ++i) {
372 for (
size_t j = 0; j < m.
size(); ++j) {
373 const double e = m.get(i, j);
397 for (
size_t i = 0; i < N; ++i) {
398 for (
size_t j = 0; j < N; ++j) {
399 const double a = ma.get(i, j);
400 const double b = mb.get(i, j);
411 return m.
isZero(tolerance);
General -dimensional mathematical matrix object.
Definition MatrixN.hh:30
double trace() const
Calculate trace.
Definition MatrixN.hh:143
double det() const
Calculate determinant.
Definition MatrixN.hh:138
Matrix< N > operator-() const
Negate.
Definition MatrixN.hh:153
bool isZero(double tolerance=1E-5) const
Index-wise check for nullness, allowing for numerical precision.
Definition MatrixN.hh:165
constexpr size_t size() const
Get dimensionality.
Definition MatrixN.hh:160
bool isDiag() const
Check that all off-diagonal elements are zero, allowing for numerical precision.
Definition MatrixN.hh:190
Matrix< N > inverse() const
Calculate inverse.
Definition MatrixN.hh:131
bool isSymm() const
Check for symmetry under transposition.
Definition MatrixN.hh:185
bool isEqual(Matrix< N > other) const
Check for index-wise equality, allowing for numerical precision.
Definition MatrixN.hh:175
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:60
double E(const ParticleBase &p)
Unbound function access to E.
Definition ParticleBaseUtils.hh:659
Definition MC_CENT_PPB_Projections.hh:10
double subtract(double a, double b, double tolerance=1e-5)
Subtract two numbers with FP fuzziness.
Definition MathUtils.hh:223
std::ostream & operator<<(std::ostream &os, const AnalysisInfo &ai)
Stream an AnalysisInfo as a text description.
Definition AnalysisInfo.hh:362
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:24
double add(double a, double b, double tolerance=1e-5)
Add two numbers with FP fuzziness.
Definition MathUtils.hh:229
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:61