Version: SMASH-1.5
fourvector.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2012-2018
3  * SMASH Team
4  *
5  * GNU General Public License (GPLv3 or later)
6  */
7 #ifndef SRC_INCLUDE_FOURVECTOR_H_
8 #define SRC_INCLUDE_FOURVECTOR_H_
9 
10 #include <array>
11 #include <cmath>
12 #include <iosfwd>
13 
14 #include "threevector.h"
15 
16 namespace smash {
17 
32 class FourVector {
33  public:
35  FourVector() : x_({0., 0., 0., 0.}) {}
36 
45  FourVector(double y0, double y1, double y2, double y3)
46  : x_({y0, y1, y2, y3}) {}
47 
54  FourVector(double y0, ThreeVector vec)
55  : x_({y0, vec.x1(), vec.x2(), vec.x3()}) {}
56 
64  double &operator[](std::size_t i) { return x_[i]; }
66  double operator[](std::size_t i) const { return x_[i]; }
67 
69  double inline x0() const;
71  void inline set_x0(double t);
73  double inline x1() const;
75  void inline set_x1(double x);
77  double inline x2() const;
79  void inline set_x2(double y);
81  double inline x3() const;
83  void inline set_x3(double z);
85  ThreeVector inline threevec() const;
86 
93  ThreeVector inline velocity() const;
94 
101  double inline Dot(const FourVector &a) const;
102 
108  double inline sqr() const;
109 
117  double inline abs() const;
118 
124  double inline sqr3() const;
125 
131  double inline abs3() const;
132 
167  FourVector LorentzBoost(const ThreeVector &v) const;
168 
176  bool operator==(const FourVector &a) const;
177 
185  bool inline operator!=(const FourVector &a) const;
186 
195  bool inline operator<(const FourVector &a) const;
196 
205  bool inline operator>(const FourVector &a) const;
206 
214  bool inline operator<=(const FourVector &a) const;
215 
223  bool inline operator>=(const FourVector &a) const;
224 
231  FourVector inline operator+=(const FourVector &a);
232 
239  FourVector inline operator-=(const FourVector &a);
240 
247  FourVector inline operator*=(const double &a);
248 
255  FourVector inline operator/=(const double &a);
256 
258  using iterator = std::array<double, 4>::iterator;
260  using const_iterator = std::array<double, 4>::const_iterator;
261 
268  iterator begin() { return x_.begin(); }
269 
271  iterator end() { return x_.end(); }
272 
274  const_iterator begin() const { return x_.begin(); }
276  const_iterator end() const { return x_.end(); }
277 
279  const_iterator cbegin() const { return x_.cbegin(); }
281  const_iterator cend() const { return x_.cend(); }
282 
283  private:
285  std::array<double, 4> x_;
286 };
287 
288 // Definitions of previous inline functions
289 
290 double inline FourVector::x0(void) const { return x_[0]; }
291 
292 void inline FourVector::set_x0(const double t) { x_[0] = t; }
293 
294 double inline FourVector::x1() const { return x_[1]; }
295 
296 void inline FourVector::set_x1(const double x) { x_[1] = x; }
297 
298 double inline FourVector::x2() const { return x_[2]; }
299 
300 void inline FourVector::set_x2(const double y) { x_[2] = y; }
301 
302 double inline FourVector::x3() const { return x_[3]; }
303 
304 void inline FourVector::set_x3(const double z) { x_[3] = z; }
305 
307  return ThreeVector(x_[1], x_[2], x_[3]);
308 }
309 
310 ThreeVector inline FourVector::velocity() const { return threevec() / x0(); }
311 
312 // use == operator for the inverse != check
313 bool inline FourVector::operator!=(const FourVector &a) const {
314  return !(*this == a);
315 }
316 
317 bool inline FourVector::operator<(const FourVector &a) const {
318  return (x_[0] < a.x_[0]) && (x_[1] < a.x_[1]) && (x_[2] < a.x_[2]) &&
319  (x_[3] < a.x_[3]);
320 }
321 
322 // use < operator for the inverse by switching arguments
323 bool inline FourVector::operator>(const FourVector &a) const {
324  return a < *this;
325 }
326 
327 // use > operator for less equal
328 bool inline FourVector::operator<=(const FourVector &a) const {
329  return !(*this > a);
330 }
331 
332 // use < operator for greater equal
333 bool inline FourVector::operator>=(const FourVector &a) const {
334  return !(*this < a);
335 }
336 
338  this->x_[0] += a.x_[0];
339  this->x_[1] += a.x_[1];
340  this->x_[2] += a.x_[2];
341  this->x_[3] += a.x_[3];
342  return *this;
343 }
344 
345 // addition +operator uses +=
354  a += b;
355  return a;
356 }
357 
359  this->x_[0] -= a.x_[0];
360  this->x_[1] -= a.x_[1];
361  this->x_[2] -= a.x_[2];
362  this->x_[3] -= a.x_[3];
363  return *this;
364 }
365 
366 // subtraction -operator uses -=
375  a -= b;
376  return a;
377 }
378 
379 FourVector inline FourVector::operator*=(const double &a) {
380  this->x_[0] *= a;
381  this->x_[1] *= a;
382  this->x_[2] *= a;
383  this->x_[3] *= a;
384  return *this;
385 }
386 
387 // factor multiplication uses *=
395 inline FourVector operator*(FourVector a, double b) {
396  a *= b;
397  return a;
398 }
406 inline FourVector operator*(double b, FourVector a) {
407  a *= b;
408  return a;
409 }
410 
411 FourVector inline FourVector::operator/=(const double &a) {
412  const double a_inv = 1.0 / a;
413  this->x_[0] *= a_inv;
414  this->x_[1] *= a_inv;
415  this->x_[2] *= a_inv;
416  this->x_[3] *= a_inv;
417  return *this;
418 }
419 
420 // factor division uses /=
428 inline FourVector operator/(FourVector a, const double &b) {
429  a /= b;
430  return a;
431 }
432 
433 double inline FourVector::Dot(const FourVector &a) const {
434  return x_[0] * a.x_[0] - x_[1] * a.x_[1] - x_[2] * a.x_[2] - x_[3] * a.x_[3];
435 }
436 
437 double inline FourVector::sqr() const {
438  return x_[0] * x_[0] - x_[1] * x_[1] - x_[2] * x_[2] - x_[3] * x_[3];
439 }
440 
441 double inline FourVector::abs() const {
442  if (this->sqr() > -really_small) {
443  return std::sqrt(std::abs(this->sqr()));
444  } else {
445  throw std::runtime_error(
446  "Absolute value of 4-vector could not be "
447  "determined, taking sqrt of negative value.");
448  }
449 }
450 
451 double inline FourVector::sqr3() const { return this->threevec().sqr(); }
452 
453 double inline FourVector::abs3() const { return this->threevec().abs(); }
454 
461 std::ostream &operator<<(std::ostream &os, const FourVector &vec);
462 
463 } // namespace smash
464 
465 #endif // SRC_INCLUDE_FOURVECTOR_H_
bool operator>(const FourVector &a) const
checks if for all (all four vector components are above comparison vector)
Definition: fourvector.h:323
FourVector operator*=(const double &a)
multiplies by
Definition: fourvector.h:379
The ThreeVector class represents a physical three-vector with the components .
Definition: threevector.h:30
double x3() const
Definition: threevector.h:163
constexpr double really_small
Numerical error tolerance.
Definition: constants.h:34
double & operator[](std::size_t i)
access the component at offset i.
Definition: fourvector.h:64
ThreeVector velocity() const
Get the velocity (3-vector divided by zero component).
Definition: fourvector.h:310
EnergyMomentumTensor operator/(EnergyMomentumTensor a, const double b)
Direct division operator.
FourVector LorentzBoost(const ThreeVector &v) const
Returns the FourVector boosted with velocity v.
Definition: fourvector.cc:16
std::array< double, 4 > x_
internal storage of this vector&#39;s components
Definition: fourvector.h:285
bool operator>=(const FourVector &a) const
logical complement to FourVector::operator<(const FourVector&) const
Definition: fourvector.h:333
EnergyMomentumTensor operator+(EnergyMomentumTensor a, const EnergyMomentumTensor &b)
Direct addition operator.
bool operator<=(const FourVector &a) const
logical complement to FourVector::operator>(const FourVector&) const
Definition: fourvector.h:328
double x1() const
Definition: threevector.h:155
iterator end()
Definition: fourvector.h:271
double x3() const
Definition: fourvector.h:302
EnergyMomentumTensor operator-(EnergyMomentumTensor a, const EnergyMomentumTensor &b)
Direct subtraction operator.
const_iterator cbegin() const
Definition: fourvector.h:279
double x0() const
Definition: fourvector.h:290
double abs() const
Definition: threevector.h:251
void set_x1(double x)
Definition: fourvector.h:296
bool operator<(const FourVector &a) const
checks if for all (all four vector components are below comparison vector)
Definition: fourvector.h:317
double sqr() const
calculate the square of the vector (which is a scalar)
Definition: fourvector.h:437
double sqr3() const
calculate the square of the spatial three-vector
Definition: fourvector.h:451
void set_x3(double z)
Definition: fourvector.h:304
ThreeVector threevec() const
Definition: fourvector.h:306
double x1() const
Definition: fourvector.h:294
void set_x0(double t)
Definition: fourvector.h:292
const_iterator end() const
Definition: fourvector.h:276
double operator[](std::size_t i) const
const overload of the [] operator
Definition: fourvector.h:66
std::array< double, 4 >::const_iterator const_iterator
iterates over the components
Definition: fourvector.h:260
double sqr() const
Definition: threevector.h:249
FourVector operator/=(const double &a)
divides by
Definition: fourvector.h:411
bool operator!=(const FourVector &a) const
checks inequality (logical complement to FourVector::operator==(const FourVector&) const) ...
Definition: fourvector.h:313
EnergyMomentumTensor operator*(EnergyMomentumTensor a, const double b)
Direct multiplication operator.
FourVector(double y0, ThreeVector vec)
construct from time-like component and a ThreeVector.
Definition: fourvector.h:54
FourVector operator-=(const FourVector &a)
subtracts
Definition: fourvector.h:358
FourVector operator+=(const FourVector &a)
adds
Definition: fourvector.h:337
double x2() const
Definition: fourvector.h:298
FourVector(double y0, double y1, double y2, double y3)
copy constructor
Definition: fourvector.h:45
std::array< double, 4 >::iterator iterator
iterates over the components
Definition: fourvector.h:258
double abs3() const
calculate the absolute value of the spatial three-vector
Definition: fourvector.h:453
const_iterator cend() const
Definition: fourvector.h:281
bool operator==(const FourVector &a) const
Check if all four vector components are almost equal (accuracy ).
Definition: fourvector.cc:29
void set_x2(double y)
Definition: fourvector.h:300
double Dot(const FourVector &a) const
calculate the scalar product with another four-vector
Definition: fourvector.h:433
std::ostream & operator<<(std::ostream &out, const ActionPtr &action)
Convenience: dereferences the ActionPtr to Action.
Definition: action.h:457
The FourVector class holds relevant values in Minkowski spacetime with (+, −, −, −) metric signature.
Definition: fourvector.h:32
FourVector()
default constructor nulls the fourvector components
Definition: fourvector.h:35
double abs() const
calculate the lorentz invariant absolute value
Definition: fourvector.h:441
double x2() const
Definition: threevector.h:159
Definition: action.h:24
iterator begin()
Definition: fourvector.h:268
const_iterator begin() const
Definition: fourvector.h:274