Rivet API documentation

Rivet 4.1.3
VectorN.hh
1#ifndef RIVET_MATH_VECTORN
2#define RIVET_MATH_VECTORN
3
4#include "Rivet/Math/MathConstants.hh"
5#include "Rivet/Math/MathUtils.hh"
6
7#include "Rivet/Math/eigen3/Dense"
8
9namespace Rivet {
10
11
12 template <size_t N>
13 class Vector;
14 template <size_t N>
15 class Matrix;
16
17 template <size_t N>
18 Vector<N> multiply(const Matrix<N>& a, const Vector<N>& b);
19
20
22 template <size_t N>
23 class Vector {
24
25 template <size_t M>
26 friend Vector<M> multiply(const Matrix<M>& a, const Vector<M>& b);
27
28
29 public:
30
31 Vector()
32 : _vec(EVector::Zero()) { }
33
34 const double& get(const size_t index) const {
35 if (index >= N) {
36 throw std::runtime_error("Tried to access an invalid vector index.");
37 }
38 else {
39 return _vec(index);
40 }
41 }
42
43 double& get(const size_t index) {
44 if (index >= N) {
45 throw std::runtime_error("Tried to access an invalid vector index.");
46 }
47 else {
48 return _vec(index);
49 }
50 }
51
53 const double& operator[](const size_t index) const {
54 return get(index);
55 }
56
58 double& operator[](const size_t index) {
59 return get(index);
60 }
61
63 Vector<N>& set(const size_t index, const double value) {
64 if (index >= N) {
65 throw std::runtime_error("Tried to access an invalid vector index.");
66 }
67 else {
68 _vec[index] = value;
69 }
70 return *this;
71 }
72
74 constexpr size_t size() const {
75 return N;
76 }
77
79 bool isZero(double tolerance = 1E-5) const {
80 for (size_t i = 0; i < N; ++i) {
81 if (!Rivet::isZero(_vec[i], tolerance)) return false;
82 }
83 return true;
84 }
85
88 double mod2() const {
89 double mod2 = 0.0;
90 for (size_t i = 0; i < size(); ++i) {
91 const double element = get(i);
92 mod2 += element * element;
93 }
94 return mod2;
95 }
96
99 double mod() const {
100 const double norm = mod2();
101 //assert(norm >= 0); //< *should* be impossible
102 return sqrt(norm);
103 }
104
106 Vector<N> operator-() const {
107 Vector<N> rtn;
108 rtn._vec = -_vec;
109 return rtn;
110 }
111
112 bool operator==(const Vector<N>& a) const {
113 return _vec == a._vec;
114 }
115
116 bool operator!=(const Vector<N>& a) const {
117 return _vec != a._vec;
118 }
119
120 // bool operator<(const Vector<N>& a) const {
121 // return _vec < a._vec;
122 // }
123
124 // bool operator<=(const Vector<N>& a) const {
125 // return _vec <= a._vec;
126 // }
127
128 // bool operator>(const Vector<N>& a) const {
129 // return _vec > a._vec;
130 // }
131
132 // bool operator>=(const Vector<N>& a) const {
133 // return _vec >= a._vec;
134 // }
135
137 using EVector = RivetEigen::Matrix<double, N, 1>;
138 EVector _vec;
139 };
140
141
143
144
147
149 template <size_t N>
150 inline string toString(const Vector<N>& v) {
151 std::ostringstream out;
152 out << "(";
153 for (size_t i = 0; i < v.size(); ++i) {
154 out << (fabs(v[i]) < 1E-30 ? 0.0 : v[i]);
155 if (i < v.size() - 1) out << ", ";
156 }
157 out << ")";
158 return out.str();
159 }
160
162 template <size_t N>
163 inline std::ostream& operator<<(std::ostream& out, const Vector<N>& v) {
164 out << toString(v);
165 return out;
166 }
167
169
170
172
173
175 template <size_t N>
176 inline bool fuzzyEquals(const Vector<N>& va, const Vector<N>& vb, double tolerance = 1E-5) {
177 for (size_t i = 0; i < N; ++i) {
178 const double a = va.get(i);
179 const double b = vb.get(i);
180 if (!Rivet::fuzzyEquals(a, b, tolerance)) return false;
181 }
182 return true;
183 }
184
185
187 template <size_t N>
188 inline bool isZero(const Vector<N>& v, double tolerance = 1E-5) {
189 return v.isZero(tolerance);
190 }
191
192
193}
194
195#endif
General -dimensional mathematical matrix object.
Definition MatrixN.hh:30
A minimal base class for -dimensional vectors.
Definition VectorN.hh:23
double mod() const
Calculate the modulus of a vector. .
Definition VectorN.hh:99
double mod2() const
Calculate the modulus-squared of a vector. .
Definition VectorN.hh:88
RivetEigen::Matrix< double, N, 1 > EVector
Vector.
Definition VectorN.hh:137
constexpr size_t size() const
Vector dimensionality.
Definition VectorN.hh:74
Vector< N > & set(const size_t index, const double value)
Set indexed value.
Definition VectorN.hh:63
bool isZero(double tolerance=1E-5) const
Check for nullness, allowing for numerical precision.
Definition VectorN.hh:79
const double & operator[](const size_t index) const
Direct access to vector elements by index.
Definition VectorN.hh:53
double & operator[](const size_t index)
Direct access to vector elements by index.
Definition VectorN.hh:58
Vector< N > operator-() const
Invert the vector.
Definition VectorN.hh:106
double E(const ParticleBase &p)
Unbound function access to E.
Definition ParticleBaseUtils.hh:829
Definition LHCbCommon.hh:9
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
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