rivet is hosted by Hepforge, IPPP Durham
Rivet  2.7.0
VectorN.hh
1 #ifndef RIVET_MATH_VECTORN
2 #define RIVET_MATH_VECTORN
3 
4 #include "Rivet/Math/MathHeader.hh"
5 #include "Rivet/Math/MathUtils.hh"
6 
7 #include "Rivet/Math/eigen/vector.h"
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.loadZero(); }
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  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);
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  Eigen::Vector<double,N> _vec;
134 
135  };
136 
137 
139 
140 
142 
143 
145  template <size_t N>
146  inline const string toString(const Vector<N>& v) {
147  ostringstream out;
148  out << "(";
149  for (size_t i = 0; i < v.size(); ++i) {
150  out << (fabs(v[i]) < 1E-30 ? 0.0 : v[i]);
151  if (i < v.size()-1) out << ", ";
152  }
153  out << ")";
154  return out.str();
155  }
156 
158  template <size_t N>
159  inline std::ostream& operator<<(std::ostream& out, const Vector<N>& v) {
160  out << toString(v);
161  return out;
162  }
163 
165 
166 
168 
169 
171  template <size_t N>
172  inline bool fuzzyEquals(const Vector<N>& va, const Vector<N>& vb, double tolerance=1E-5) {
173  for (size_t i = 0; i < N; ++i) {
174  const double a = va.get(i);
175  const double b = vb.get(i);
176  if (!Rivet::fuzzyEquals(a, b, tolerance)) return false;
177  }
178  return true;
179  }
180 
181 
183  template <size_t N>
184  inline bool isZero(const Vector<N>& v, double tolerance=1E-5) {
185  return v.isZero(tolerance);
186  }
187 
188 
189 }
190 
191 #endif
Definition: ALICE_2010_I880049.cc:13
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
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:45
A minimal base class for -dimensional vectors.
Definition: VectorN.hh:13
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
size_t size() const
Vector dimensionality.
Definition: VectorN.hh:70
std::string toString(const AnalysisInfo &ai)
String representation.
Definition: AnalysisInfo.cc:144
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