Rivet  3.1.0
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 
9 namespace 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  template <size_t M>
25  friend Vector<M> multiply(const Matrix<M>& a, const Vector<M>& b);
26 
27  public:
28  Vector() : _vec(EVector::Zero()) { }
29 
30  Vector(const Vector<N>& other)
31  : _vec(other._vec) { }
32 
33  const double& get(const size_t index) const {
34  if (index >= N) {
35  throw std::runtime_error("Tried to access an invalid vector index.");
36  } else {
37  return _vec(index);
38  }
39  }
40 
41  double& get(const size_t index) {
42  if (index >= N) {
43  throw std::runtime_error("Tried to access an invalid vector index.");
44  } else {
45  return _vec(index);
46  }
47  }
48 
50  const double& operator[](const size_t index) const {
51  return get(index);
52  }
53 
55  double& operator[](const size_t index) {
56  return get(index);
57  }
58 
60  Vector<N>& set(const size_t index, const double value) {
61  if (index >= N) {
62  throw std::runtime_error("Tried to access an invalid vector index.");
63  } else {
64  _vec[index] = value;
65  }
66  return *this;
67  }
68 
70  constexpr size_t size() const {
71  return N;
72  }
73 
75  bool isZero(double tolerance=1E-5) const {
76  for (size_t i=0; i < N; ++i) {
77  if (! Rivet::isZero(_vec[i], tolerance) ) return false;
78  }
79  return true;
80  }
81 
84  double mod2() const {
85  double mod2 = 0.0;
86  for (size_t i = 0; i < size(); ++i) {
87  const double element = get(i);
88  mod2 += element*element;
89  }
90  return mod2;
91  }
92 
95  double mod() const {
96  const double norm = mod2();
97  //assert(norm >= 0); //< *should* be impossible
98  return sqrt(norm);
99  }
100 
103  Vector<N> rtn;
104  rtn._vec = -_vec;
105  return rtn;
106  }
107 
108  bool operator==(const Vector<N>& a) const {
109  return _vec == a._vec;
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 
133  using EVector = Eigen::Matrix<double,N,1>;
134  EVector _vec;
135 
136  };
137 
138 
140 
141 
143 
144 
146  template <size_t N>
147  inline const string toString(const Vector<N>& v) {
148  std::ostringstream out;
149  out << "(";
150  for (size_t i = 0; i < v.size(); ++i) {
151  out << (fabs(v[i]) < 1E-30 ? 0.0 : v[i]);
152  if (i < v.size()-1) out << ", ";
153  }
154  out << ")";
155  return out.str();
156  }
157 
159  template <size_t N>
160  inline std::ostream& operator<<(std::ostream& out, const Vector<N>& v) {
161  out << toString(v);
162  return out;
163  }
164 
166 
167 
169 
170 
172  template <size_t N>
173  inline bool fuzzyEquals(const Vector<N>& va, const Vector<N>& vb, double tolerance=1E-5) {
174  for (size_t i = 0; i < N; ++i) {
175  const double a = va.get(i);
176  const double b = vb.get(i);
177  if (!Rivet::fuzzyEquals(a, b, tolerance)) return false;
178  }
179  return true;
180  }
181 
182 
184  template <size_t N>
185  inline bool isZero(const Vector<N>& v, double tolerance=1E-5) {
186  return v.isZero(tolerance);
187  }
188 
189 
190 }
191 
192 #endif
Definition: MC_Cent_pPb.hh:10
bool isZero(double tolerance=1E-5) const
Check for nullness, allowing for numerical precision.
Definition: VectorN.hh:75
double mod2() const
Calculate the modulus-squared of a vector. .
Definition: VectorN.hh:84
double & operator[](const size_t index)
Direct access to vector elements by index.
Definition: VectorN.hh:55
constexpr size_t size() const
Vector dimensionality.
Definition: VectorN.hh:70
General -dimensional mathematical matrix object.
Definition: MatrixN.hh:14
std::enable_if< std::is_arithmetic< N1 >::value &&std::is_arithmetic< N2 >::value &&(std::is_floating_point< N1 >::value||std::is_floating_point< N2 >::value), bool >::type fuzzyEquals(N1 a, N2 b, double tolerance=1e-5)
Compare two numbers for equality with a degree of fuzziness.
Definition: MathUtils.hh:54
A minimal base class for -dimensional vectors.
Definition: VectorN.hh:13
Eigen::Matrix< double, N, 1 > EVector
Vector.
Definition: VectorN.hh:133
std::string toString(const AnalysisInfo &ai)
String representation.
std::enable_if< std::is_floating_point< NUM >::value, bool >::type isZero(NUM val, double tolerance=1e-8)
Compare a number to zero.
Definition: MathUtils.hh:21
const double & operator[](const size_t index) const
Direct access to vector elements by index.
Definition: VectorN.hh:50
Vector< N > operator-() const
Invert the vector.
Definition: VectorN.hh:102
double mod() const
Calculate the modulus of a vector. .
Definition: VectorN.hh:95