Version: SMASH-2.2
fourvector.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2012-2021
3  * SMASH Team
4  *
5  * GNU General Public License (GPLv3 or later)
6  */
7 #ifndef SRC_INCLUDE_SMASH_FOURVECTOR_H_
8 #define SRC_INCLUDE_SMASH_FOURVECTOR_H_
9 
10 #include <array>
11 #include <cmath>
12 #include <iosfwd>
13 #include <stdexcept>
14 
15 #include "threevector.h"
16 
17 namespace smash {
18 
33 class FourVector {
34  public:
36  FourVector() : x_({0., 0., 0., 0.}) {}
37 
46  FourVector(double y0, double y1, double y2, double y3)
47  : x_({y0, y1, y2, y3}) {}
48 
55  FourVector(double y0, ThreeVector vec)
56  : x_({y0, vec.x1(), vec.x2(), vec.x3()}) {}
57 
65  double &operator[](std::size_t i) { return x_[i]; }
67  double operator[](std::size_t i) const { return x_[i]; }
68 
70  double inline x0() const;
72  void inline set_x0(double t);
74  double inline x1() const;
76  void inline set_x1(double x);
78  double inline x2() const;
80  void inline set_x2(double y);
82  double inline x3() const;
84  void inline set_x3(double z);
86  ThreeVector inline threevec() const;
87 
91  void inline reset();
92 
99  ThreeVector inline velocity() const;
100 
107  double inline Dot(const FourVector &a) const;
108 
114  double inline sqr() const;
115 
123  double inline abs() const;
124 
130  double inline sqr3() const;
131 
137  double inline abs3() const;
138 
143  double inline tau() const;
144 
149  double inline eta() const;
150 
185  FourVector lorentz_boost(const ThreeVector &v) const;
186 
194  bool operator==(const FourVector &a) const;
195 
203  bool inline operator!=(const FourVector &a) const;
204 
213  bool inline operator<(const FourVector &a) const;
214 
223  bool inline operator>(const FourVector &a) const;
224 
232  bool inline operator<=(const FourVector &a) const;
233 
241  bool inline operator>=(const FourVector &a) const;
242 
249  FourVector inline operator+=(const FourVector &a);
250 
257  FourVector inline operator-=(const FourVector &a);
258 
265  FourVector inline operator*=(const double &a);
266 
273  FourVector inline operator/=(const double &a);
274 
276  using iterator = std::array<double, 4>::iterator;
278  using const_iterator = std::array<double, 4>::const_iterator;
279 
286  iterator begin() { return x_.begin(); }
287 
289  iterator end() { return x_.end(); }
290 
292  const_iterator begin() const { return x_.begin(); }
294  const_iterator end() const { return x_.end(); }
295 
297  const_iterator cbegin() const { return x_.cbegin(); }
299  const_iterator cend() const { return x_.cend(); }
300 
301  private:
303  std::array<double, 4> x_;
304 };
305 
306 // Definitions of previous inline functions
307 
308 double inline FourVector::x0(void) const { return x_[0]; }
309 
310 void inline FourVector::set_x0(const double t) { x_[0] = t; }
311 
312 double inline FourVector::x1() const { return x_[1]; }
313 
314 void inline FourVector::set_x1(const double x) { x_[1] = x; }
315 
316 double inline FourVector::x2() const { return x_[2]; }
317 
318 void inline FourVector::set_x2(const double y) { x_[2] = y; }
319 
320 double inline FourVector::x3() const { return x_[3]; }
321 
322 void inline FourVector::set_x3(const double z) { x_[3] = z; }
323 
325  return ThreeVector(x_[1], x_[2], x_[3]);
326 }
327 
328 ThreeVector inline FourVector::velocity() const { return threevec() / x0(); }
329 
330 // use == operator for the inverse != check
331 bool inline FourVector::operator!=(const FourVector &a) const {
332  return !(*this == a);
333 }
334 
335 bool inline FourVector::operator<(const FourVector &a) const {
336  return (x_[0] < a.x_[0]) && (x_[1] < a.x_[1]) && (x_[2] < a.x_[2]) &&
337  (x_[3] < a.x_[3]);
338 }
339 
340 // use < operator for the inverse by switching arguments
341 bool inline FourVector::operator>(const FourVector &a) const {
342  return a < *this;
343 }
344 
345 // use > operator for less equal
346 bool inline FourVector::operator<=(const FourVector &a) const {
347  return !(*this > a);
348 }
349 
350 // use < operator for greater equal
351 bool inline FourVector::operator>=(const FourVector &a) const {
352  return !(*this < a);
353 }
354 
356  this->x_[0] += a.x_[0];
357  this->x_[1] += a.x_[1];
358  this->x_[2] += a.x_[2];
359  this->x_[3] += a.x_[3];
360  return *this;
361 }
362 
363 // addition +operator uses +=
372  a += b;
373  return a;
374 }
375 
377  this->x_[0] -= a.x_[0];
378  this->x_[1] -= a.x_[1];
379  this->x_[2] -= a.x_[2];
380  this->x_[3] -= a.x_[3];
381  return *this;
382 }
383 
384 // subtraction -operator uses -=
393  a -= b;
394  return a;
395 }
396 
397 FourVector inline FourVector::operator*=(const double &a) {
398  this->x_[0] *= a;
399  this->x_[1] *= a;
400  this->x_[2] *= a;
401  this->x_[3] *= a;
402  return *this;
403 }
404 
405 // factor multiplication uses *=
413 inline FourVector operator*(FourVector a, double b) {
414  a *= b;
415  return a;
416 }
424 inline FourVector operator*(double b, FourVector a) {
425  a *= b;
426  return a;
427 }
428 
429 FourVector inline FourVector::operator/=(const double &a) {
430  const double a_inv = 1.0 / a;
431  this->x_[0] *= a_inv;
432  this->x_[1] *= a_inv;
433  this->x_[2] *= a_inv;
434  this->x_[3] *= a_inv;
435  return *this;
436 }
437 
438 // factor division uses /=
446 inline FourVector operator/(FourVector a, const double &b) {
447  a /= b;
448  return a;
449 }
450 
451 double inline FourVector::Dot(const FourVector &a) const {
452  return x_[0] * a.x_[0] - x_[1] * a.x_[1] - x_[2] * a.x_[2] - x_[3] * a.x_[3];
453 }
454 
455 double inline FourVector::sqr() const {
456  return x_[0] * x_[0] - x_[1] * x_[1] - x_[2] * x_[2] - x_[3] * x_[3];
457 }
458 
459 double inline FourVector::abs() const {
460  if (this->sqr() > -really_small) {
461  return std::sqrt(std::abs(this->sqr()));
462  } else {
463  throw std::runtime_error(
464  "Absolute value of 4-vector could not be "
465  "determined, taking sqrt of negative value.");
466  }
467 }
468 
469 double inline FourVector::sqr3() const { return this->threevec().sqr(); }
470 
471 double inline FourVector::abs3() const { return this->threevec().abs(); }
472 
473 double inline FourVector::tau() const {
474  return std::sqrt(this->x0() * this->x0() - this->x3() * this->x3());
475 }
476 
477 double inline FourVector::eta() const {
478  return std::atanh(this->x3() / this->x0());
479 }
480 
481 void inline FourVector::reset() {
482  this->x_[0] = 0.;
483  this->x_[1] = 0.;
484  this->x_[2] = 0.;
485  this->x_[3] = 0.;
486 }
487 
494 std::ostream &operator<<(std::ostream &os, const FourVector &vec);
495 
496 } // namespace smash
497 
498 #endif // SRC_INCLUDE_SMASH_FOURVECTOR_H_
The FourVector class holds relevant values in Minkowski spacetime with (+, −, −, −) metric signature.
Definition: fourvector.h:33
bool operator==(const FourVector &a) const
Check if all four vector components are almost equal (accuracy ).
Definition: fourvector.cc:29
double & operator[](std::size_t i)
access the component at offset i.
Definition: fourvector.h:65
const_iterator end() const
Definition: fourvector.h:294
double x3() const
Definition: fourvector.h:320
const_iterator cbegin() const
Definition: fourvector.h:297
bool operator<(const FourVector &a) const
checks if for all (all four vector components are below comparison vector)
Definition: fourvector.h:335
double abs3() const
calculate the absolute value of the spatial three-vector
Definition: fourvector.h:471
double x2() const
Definition: fourvector.h:316
bool operator>=(const FourVector &a) const
logical complement to FourVector::operator<(const FourVector&) const
Definition: fourvector.h:351
void set_x2(double y)
Definition: fourvector.h:318
void reset()
Set all the 4-vector components to 0.
Definition: fourvector.h:481
FourVector operator-=(const FourVector &a)
subtracts
Definition: fourvector.h:376
std::array< double, 4 >::const_iterator const_iterator
iterates over the components
Definition: fourvector.h:278
double abs() const
calculate the lorentz invariant absolute value
Definition: fourvector.h:459
double sqr() const
calculate the square of the vector (which is a scalar)
Definition: fourvector.h:455
FourVector lorentz_boost(const ThreeVector &v) const
Returns the FourVector boosted with velocity v.
Definition: fourvector.cc:16
double sqr3() const
calculate the square of the spatial three-vector
Definition: fourvector.h:469
FourVector operator*=(const double &a)
multiplies by
Definition: fourvector.h:397
bool operator>(const FourVector &a) const
checks if for all (all four vector components are above comparison vector)
Definition: fourvector.h:341
bool operator<=(const FourVector &a) const
logical complement to FourVector::operator>(const FourVector&) const
Definition: fourvector.h:346
ThreeVector threevec() const
Definition: fourvector.h:324
const_iterator cend() const
Definition: fourvector.h:299
FourVector operator/=(const double &a)
divides by
Definition: fourvector.h:429
FourVector()
default constructor nulls the fourvector components
Definition: fourvector.h:36
double Dot(const FourVector &a) const
calculate the scalar product with another four-vector
Definition: fourvector.h:451
std::array< double, 4 > x_
internal storage of this vector's components
Definition: fourvector.h:303
double x0() const
Definition: fourvector.h:308
std::array< double, 4 >::iterator iterator
iterates over the components
Definition: fourvector.h:276
ThreeVector velocity() const
Get the velocity (3-vector divided by zero component).
Definition: fourvector.h:328
FourVector(double y0, double y1, double y2, double y3)
copy constructor
Definition: fourvector.h:46
void set_x3(double z)
Definition: fourvector.h:322
iterator end()
Definition: fourvector.h:289
void set_x1(double x)
Definition: fourvector.h:314
double x1() const
Definition: fourvector.h:312
FourVector(double y0, ThreeVector vec)
construct from time-like component and a ThreeVector.
Definition: fourvector.h:55
FourVector operator+=(const FourVector &a)
adds
Definition: fourvector.h:355
void set_x0(double t)
Definition: fourvector.h:310
bool operator!=(const FourVector &a) const
checks inequality (logical complement to FourVector::operator==(const FourVector&) const)
Definition: fourvector.h:331
double eta() const
calculate the space-time rapidity from the given four vector
Definition: fourvector.h:477
double operator[](std::size_t i) const
const overload of the [] operator
Definition: fourvector.h:67
const_iterator begin() const
Definition: fourvector.h:292
iterator begin()
Definition: fourvector.h:286
double tau() const
calculate the proper time from the given four vector
Definition: fourvector.h:473
The ThreeVector class represents a physical three-vector with the components .
Definition: threevector.h:31
double abs() const
Definition: threevector.h:261
double sqr() const
Definition: threevector.h:259
double x3() const
Definition: threevector.h:173
double x2() const
Definition: threevector.h:169
double x1() const
Definition: threevector.h:165
std::ostream & operator<<(std::ostream &out, const ActionPtr &action)
Convenience: dereferences the ActionPtr to Action.
Definition: action.h:532
Definition: action.h:24
EnergyMomentumTensor operator-(EnergyMomentumTensor a, const EnergyMomentumTensor &b)
Direct subtraction operator.
EnergyMomentumTensor operator*(EnergyMomentumTensor a, const double b)
Direct multiplication operator.
constexpr double really_small
Numerical error tolerance.
Definition: constants.h:37
EnergyMomentumTensor operator+(EnergyMomentumTensor a, const EnergyMomentumTensor &b)
Direct addition operator.
EnergyMomentumTensor operator/(EnergyMomentumTensor a, const double b)
Direct division operator.