Version: SMASH-3.1
fourvector.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2013-2015,2017-2022
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 
190  FourVector lorentz_boost(const ThreeVector &v) const;
191 
199  bool operator==(const FourVector &a) const;
200 
208  bool inline operator!=(const FourVector &a) const;
209 
218  bool inline operator<(const FourVector &a) const;
219 
228  bool inline operator>(const FourVector &a) const;
229 
237  bool inline operator<=(const FourVector &a) const;
238 
246  bool inline operator>=(const FourVector &a) const;
247 
254  FourVector inline operator+=(const FourVector &a);
255 
262  FourVector inline operator-=(const FourVector &a);
263 
270  FourVector inline operator*=(const double &a);
271 
278  FourVector inline operator/=(const double &a);
279 
281  using iterator = std::array<double, 4>::iterator;
283  using const_iterator = std::array<double, 4>::const_iterator;
284 
291  iterator begin() { return x_.begin(); }
292 
294  iterator end() { return x_.end(); }
295 
297  const_iterator begin() const { return x_.begin(); }
299  const_iterator end() const { return x_.end(); }
300 
302  const_iterator cbegin() const { return x_.cbegin(); }
304  const_iterator cend() const { return x_.cend(); }
305 
306  private:
308  std::array<double, 4> x_;
309 };
310 
311 // Definitions of previous inline functions
312 
313 double inline FourVector::x0(void) const { return x_[0]; }
314 
315 void inline FourVector::set_x0(const double t) { x_[0] = t; }
316 
317 double inline FourVector::x1() const { return x_[1]; }
318 
319 void inline FourVector::set_x1(const double x) { x_[1] = x; }
320 
321 double inline FourVector::x2() const { return x_[2]; }
322 
323 void inline FourVector::set_x2(const double y) { x_[2] = y; }
324 
325 double inline FourVector::x3() const { return x_[3]; }
326 
327 void inline FourVector::set_x3(const double z) { x_[3] = z; }
328 
330  return ThreeVector(x_[1], x_[2], x_[3]);
331 }
332 
333 ThreeVector inline FourVector::velocity() const { return threevec() / x0(); }
334 
335 // use == operator for the inverse != check
336 bool inline FourVector::operator!=(const FourVector &a) const {
337  return !(*this == a);
338 }
339 
340 bool inline FourVector::operator<(const FourVector &a) const {
341  return (x_[0] < a.x_[0]) && (x_[1] < a.x_[1]) && (x_[2] < a.x_[2]) &&
342  (x_[3] < a.x_[3]);
343 }
344 
345 // use < operator for the inverse by switching arguments
346 bool inline FourVector::operator>(const FourVector &a) const {
347  return a < *this;
348 }
349 
350 // use > operator for less equal
351 bool inline FourVector::operator<=(const FourVector &a) const {
352  return !(*this > a);
353 }
354 
355 // use < operator for greater equal
356 bool inline FourVector::operator>=(const FourVector &a) const {
357  return !(*this < a);
358 }
359 
361  this->x_[0] += a.x_[0];
362  this->x_[1] += a.x_[1];
363  this->x_[2] += a.x_[2];
364  this->x_[3] += a.x_[3];
365  return *this;
366 }
367 
368 // addition +operator uses +=
377  a += b;
378  return a;
379 }
380 
382  this->x_[0] -= a.x_[0];
383  this->x_[1] -= a.x_[1];
384  this->x_[2] -= a.x_[2];
385  this->x_[3] -= a.x_[3];
386  return *this;
387 }
388 
389 // subtraction -operator uses -=
398  a -= b;
399  return a;
400 }
401 
402 FourVector inline FourVector::operator*=(const double &a) {
403  this->x_[0] *= a;
404  this->x_[1] *= a;
405  this->x_[2] *= a;
406  this->x_[3] *= a;
407  return *this;
408 }
409 
410 // factor multiplication uses *=
418 inline FourVector operator*(FourVector a, double b) {
419  a *= b;
420  return a;
421 }
429 inline FourVector operator*(double b, FourVector a) {
430  a *= b;
431  return a;
432 }
433 
434 FourVector inline FourVector::operator/=(const double &a) {
435  const double a_inv = 1.0 / a;
436  this->x_[0] *= a_inv;
437  this->x_[1] *= a_inv;
438  this->x_[2] *= a_inv;
439  this->x_[3] *= a_inv;
440  return *this;
441 }
442 
443 // factor division uses /=
451 inline FourVector operator/(FourVector a, const double &b) {
452  a /= b;
453  return a;
454 }
455 
456 double inline FourVector::Dot(const FourVector &a) const {
457  return x_[0] * a.x_[0] - x_[1] * a.x_[1] - x_[2] * a.x_[2] - x_[3] * a.x_[3];
458 }
459 
460 double inline FourVector::sqr() const {
461  return x_[0] * x_[0] - x_[1] * x_[1] - x_[2] * x_[2] - x_[3] * x_[3];
462 }
463 
464 double inline FourVector::abs() const {
465  if (this->sqr() > -really_small) {
466  return std::sqrt(std::abs(this->sqr()));
467  } else {
468  throw std::runtime_error(
469  "Absolute value of 4-vector could not be "
470  "determined, taking sqrt of negative value.");
471  }
472 }
473 
474 double inline FourVector::sqr3() const { return this->threevec().sqr(); }
475 
476 double inline FourVector::abs3() const { return this->threevec().abs(); }
477 
478 double inline FourVector::tau() const {
479  return std::sqrt(this->x0() * this->x0() - this->x3() * this->x3());
480 }
481 
482 double inline FourVector::eta() const {
483  return std::atanh(this->x3() / this->x0());
484 }
485 
486 void inline FourVector::reset() {
487  this->x_[0] = 0.;
488  this->x_[1] = 0.;
489  this->x_[2] = 0.;
490  this->x_[3] = 0.;
491 }
492 
499 std::ostream &operator<<(std::ostream &os, const FourVector &vec);
500 
501 } // namespace smash
502 
503 #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:30
double & operator[](std::size_t i)
access the component at offset i.
Definition: fourvector.h:65
const_iterator end() const
Definition: fourvector.h:299
double x3() const
Definition: fourvector.h:325
const_iterator cbegin() const
Definition: fourvector.h:302
bool operator<(const FourVector &a) const
checks if for all (all four vector components are below comparison vector)
Definition: fourvector.h:340
double abs3() const
calculate the absolute value of the spatial three-vector
Definition: fourvector.h:476
double x2() const
Definition: fourvector.h:321
bool operator>=(const FourVector &a) const
logical complement to FourVector::operator<(const FourVector&) const
Definition: fourvector.h:356
void set_x2(double y)
Definition: fourvector.h:323
void reset()
Set all the 4-vector components to 0.
Definition: fourvector.h:486
FourVector operator-=(const FourVector &a)
subtracts
Definition: fourvector.h:381
std::array< double, 4 >::const_iterator const_iterator
iterates over the components
Definition: fourvector.h:283
double abs() const
calculate the lorentz invariant absolute value
Definition: fourvector.h:464
double sqr() const
calculate the square of the vector (which is a scalar)
Definition: fourvector.h:460
FourVector lorentz_boost(const ThreeVector &v) const
Returns the FourVector boosted with velocity v.
Definition: fourvector.cc:17
double sqr3() const
calculate the square of the spatial three-vector
Definition: fourvector.h:474
FourVector operator*=(const double &a)
multiplies by
Definition: fourvector.h:402
bool operator>(const FourVector &a) const
checks if for all (all four vector components are above comparison vector)
Definition: fourvector.h:346
bool operator<=(const FourVector &a) const
logical complement to FourVector::operator>(const FourVector&) const
Definition: fourvector.h:351
ThreeVector threevec() const
Definition: fourvector.h:329
const_iterator cend() const
Definition: fourvector.h:304
FourVector operator/=(const double &a)
divides by
Definition: fourvector.h:434
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:456
std::array< double, 4 > x_
internal storage of this vector's components
Definition: fourvector.h:308
double x0() const
Definition: fourvector.h:313
std::array< double, 4 >::iterator iterator
iterates over the components
Definition: fourvector.h:281
ThreeVector velocity() const
Get the velocity (3-vector divided by zero component).
Definition: fourvector.h:333
FourVector(double y0, double y1, double y2, double y3)
copy constructor
Definition: fourvector.h:46
void set_x3(double z)
Definition: fourvector.h:327
iterator end()
Definition: fourvector.h:294
void set_x1(double x)
Definition: fourvector.h:319
double x1() const
Definition: fourvector.h:317
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:360
void set_x0(double t)
Definition: fourvector.h:315
bool operator!=(const FourVector &a) const
checks inequality (logical complement to FourVector::operator==(const FourVector&) const)
Definition: fourvector.h:336
double eta() const
calculate the space-time rapidity from the given four vector
Definition: fourvector.h:482
double operator[](std::size_t i) const
const overload of the [] operator
Definition: fourvector.h:67
const_iterator begin() const
Definition: fourvector.h:297
iterator begin()
Definition: fourvector.h:291
double tau() const
calculate the proper time from the given four vector
Definition: fourvector.h:478
The ThreeVector class represents a physical three-vector with the components .
Definition: threevector.h:31
double abs() const
Definition: threevector.h:268
double sqr() const
Definition: threevector.h:266
double x3() const
Definition: threevector.h:185
double x2() const
Definition: threevector.h:181
double x1() const
Definition: threevector.h:177
std::ostream & operator<<(std::ostream &out, const ActionPtr &action)
Convenience: dereferences the ActionPtr to Action.
Definition: action.h:547
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.