Version: SMASH-2.0
fourvector.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2012-2020
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 
94  ThreeVector inline velocity() const;
95 
102  double inline Dot(const FourVector &a) const;
103 
109  double inline sqr() const;
110 
118  double inline abs() const;
119 
125  double inline sqr3() const;
126 
132  double inline abs3() const;
133 
138  double inline tau() const;
139 
144  double inline eta() const;
145 
180  FourVector lorentz_boost(const ThreeVector &v) const;
181 
189  bool operator==(const FourVector &a) const;
190 
198  bool inline operator!=(const FourVector &a) const;
199 
208  bool inline operator<(const FourVector &a) const;
209 
218  bool inline operator>(const FourVector &a) const;
219 
227  bool inline operator<=(const FourVector &a) const;
228 
236  bool inline operator>=(const FourVector &a) const;
237 
244  FourVector inline operator+=(const FourVector &a);
245 
252  FourVector inline operator-=(const FourVector &a);
253 
260  FourVector inline operator*=(const double &a);
261 
268  FourVector inline operator/=(const double &a);
269 
271  using iterator = std::array<double, 4>::iterator;
273  using const_iterator = std::array<double, 4>::const_iterator;
274 
281  iterator begin() { return x_.begin(); }
282 
284  iterator end() { return x_.end(); }
285 
287  const_iterator begin() const { return x_.begin(); }
289  const_iterator end() const { return x_.end(); }
290 
292  const_iterator cbegin() const { return x_.cbegin(); }
294  const_iterator cend() const { return x_.cend(); }
295 
296  private:
298  std::array<double, 4> x_;
299 };
300 
301 // Definitions of previous inline functions
302 
303 double inline FourVector::x0(void) const { return x_[0]; }
304 
305 void inline FourVector::set_x0(const double t) { x_[0] = t; }
306 
307 double inline FourVector::x1() const { return x_[1]; }
308 
309 void inline FourVector::set_x1(const double x) { x_[1] = x; }
310 
311 double inline FourVector::x2() const { return x_[2]; }
312 
313 void inline FourVector::set_x2(const double y) { x_[2] = y; }
314 
315 double inline FourVector::x3() const { return x_[3]; }
316 
317 void inline FourVector::set_x3(const double z) { x_[3] = z; }
318 
320  return ThreeVector(x_[1], x_[2], x_[3]);
321 }
322 
323 ThreeVector inline FourVector::velocity() const { return threevec() / x0(); }
324 
325 // use == operator for the inverse != check
326 bool inline FourVector::operator!=(const FourVector &a) const {
327  return !(*this == a);
328 }
329 
330 bool inline FourVector::operator<(const FourVector &a) const {
331  return (x_[0] < a.x_[0]) && (x_[1] < a.x_[1]) && (x_[2] < a.x_[2]) &&
332  (x_[3] < a.x_[3]);
333 }
334 
335 // use < operator for the inverse by switching arguments
336 bool inline FourVector::operator>(const FourVector &a) const {
337  return a < *this;
338 }
339 
340 // use > operator for less equal
341 bool inline FourVector::operator<=(const FourVector &a) const {
342  return !(*this > a);
343 }
344 
345 // use < operator for greater equal
346 bool inline FourVector::operator>=(const FourVector &a) const {
347  return !(*this < a);
348 }
349 
351  this->x_[0] += a.x_[0];
352  this->x_[1] += a.x_[1];
353  this->x_[2] += a.x_[2];
354  this->x_[3] += a.x_[3];
355  return *this;
356 }
357 
358 // addition +operator uses +=
367  a += b;
368  return a;
369 }
370 
372  this->x_[0] -= a.x_[0];
373  this->x_[1] -= a.x_[1];
374  this->x_[2] -= a.x_[2];
375  this->x_[3] -= a.x_[3];
376  return *this;
377 }
378 
379 // subtraction -operator uses -=
388  a -= b;
389  return a;
390 }
391 
392 FourVector inline FourVector::operator*=(const double &a) {
393  this->x_[0] *= a;
394  this->x_[1] *= a;
395  this->x_[2] *= a;
396  this->x_[3] *= a;
397  return *this;
398 }
399 
400 // factor multiplication uses *=
408 inline FourVector operator*(FourVector a, double b) {
409  a *= b;
410  return a;
411 }
419 inline FourVector operator*(double b, FourVector a) {
420  a *= b;
421  return a;
422 }
423 
424 FourVector inline FourVector::operator/=(const double &a) {
425  const double a_inv = 1.0 / a;
426  this->x_[0] *= a_inv;
427  this->x_[1] *= a_inv;
428  this->x_[2] *= a_inv;
429  this->x_[3] *= a_inv;
430  return *this;
431 }
432 
433 // factor division uses /=
441 inline FourVector operator/(FourVector a, const double &b) {
442  a /= b;
443  return a;
444 }
445 
446 double inline FourVector::Dot(const FourVector &a) const {
447  return x_[0] * a.x_[0] - x_[1] * a.x_[1] - x_[2] * a.x_[2] - x_[3] * a.x_[3];
448 }
449 
450 double inline FourVector::sqr() const {
451  return x_[0] * x_[0] - x_[1] * x_[1] - x_[2] * x_[2] - x_[3] * x_[3];
452 }
453 
454 double inline FourVector::abs() const {
455  if (this->sqr() > -really_small) {
456  return std::sqrt(std::abs(this->sqr()));
457  } else {
458  throw std::runtime_error(
459  "Absolute value of 4-vector could not be "
460  "determined, taking sqrt of negative value.");
461  }
462 }
463 
464 double inline FourVector::sqr3() const { return this->threevec().sqr(); }
465 
466 double inline FourVector::abs3() const { return this->threevec().abs(); }
467 
468 double inline FourVector::tau() const {
469  return std::sqrt(this->x0() * this->x0() - this->x3() * this->x3());
470 }
471 
472 double inline FourVector::eta() const {
473  return std::atanh(this->x3() / this->x0());
474 }
475 
482 std::ostream &operator<<(std::ostream &os, const FourVector &vec);
483 
484 } // namespace smash
485 
486 #endif // SRC_INCLUDE_SMASH_FOURVECTOR_H_
smash
Definition: action.h:24
smash::FourVector::Dot
double Dot(const FourVector &a) const
calculate the scalar product with another four-vector
Definition: fourvector.h:446
smash::FourVector::set_x1
void set_x1(double x)
Definition: fourvector.h:309
smash::operator+
EnergyMomentumTensor operator+(EnergyMomentumTensor a, const EnergyMomentumTensor &b)
Direct addition operator.
Definition: energymomentumtensor.h:162
smash::FourVector::const_iterator
std::array< double, 4 >::const_iterator const_iterator
iterates over the components
Definition: fourvector.h:273
smash::FourVector::operator[]
double & operator[](std::size_t i)
access the component at offset i.
Definition: fourvector.h:65
smash::FourVector::operator<=
bool operator<=(const FourVector &a) const
logical complement to FourVector::operator>(const FourVector&) const
Definition: fourvector.h:341
smash::FourVector::set_x2
void set_x2(double y)
Definition: fourvector.h:313
smash::operator/
EnergyMomentumTensor operator/(EnergyMomentumTensor a, const double b)
Direct division operator.
Definition: energymomentumtensor.h:206
smash::FourVector::operator-=
FourVector operator-=(const FourVector &a)
subtracts
Definition: fourvector.h:371
smash::FourVector::operator/=
FourVector operator/=(const double &a)
divides by
Definition: fourvector.h:424
smash::ThreeVector::x3
double x3() const
Definition: threevector.h:173
smash::FourVector::operator==
bool operator==(const FourVector &a) const
Check if all four vector components are almost equal (accuracy ).
Definition: fourvector.cc:29
smash::FourVector::x3
double x3() const
Definition: fourvector.h:315
smash::operator-
EnergyMomentumTensor operator-(EnergyMomentumTensor a, const EnergyMomentumTensor &b)
Direct subtraction operator.
Definition: energymomentumtensor.h:176
smash::operator<<
std::ostream & operator<<(std::ostream &out, const ActionPtr &action)
Definition: action.h:518
smash::FourVector::sqr
double sqr() const
calculate the square of the vector (which is a scalar)
Definition: fourvector.h:450
smash::FourVector::set_x0
void set_x0(double t)
Definition: fourvector.h:305
smash::ThreeVector::sqr
double sqr() const
Definition: threevector.h:259
smash::FourVector::begin
iterator begin()
Definition: fourvector.h:281
smash::FourVector::x_
std::array< double, 4 > x_
internal storage of this vector's components
Definition: fourvector.h:298
smash::FourVector::set_x3
void set_x3(double z)
Definition: fourvector.h:317
smash::FourVector::x1
double x1() const
Definition: fourvector.h:307
smash::FourVector::FourVector
FourVector(double y0, double y1, double y2, double y3)
copy constructor
Definition: fourvector.h:46
smash::FourVector::cend
const_iterator cend() const
Definition: fourvector.h:294
smash::really_small
constexpr double really_small
Numerical error tolerance.
Definition: constants.h:37
smash::FourVector::eta
double eta() const
calculate the space-time rapidity from the given four vector
Definition: fourvector.h:472
smash::FourVector::operator!=
bool operator!=(const FourVector &a) const
checks inequality (logical complement to FourVector::operator==(const FourVector&) const)
Definition: fourvector.h:326
smash::ThreeVector
Definition: threevector.h:31
smash::FourVector::FourVector
FourVector(double y0, ThreeVector vec)
construct from time-like component and a ThreeVector.
Definition: fourvector.h:55
smash::FourVector::iterator
std::array< double, 4 >::iterator iterator
iterates over the components
Definition: fourvector.h:271
smash::FourVector::x0
double x0() const
Definition: fourvector.h:303
smash::FourVector::tau
double tau() const
calculate the proper time from the given four vector
Definition: fourvector.h:468
threevector.h
smash::FourVector::operator<
bool operator<(const FourVector &a) const
checks if for all (all four vector components are below comparison vector)
Definition: fourvector.h:330
smash::FourVector::x2
double x2() const
Definition: fourvector.h:311
smash::ThreeVector::x1
double x1() const
Definition: threevector.h:165
smash::FourVector::operator*=
FourVector operator*=(const double &a)
multiplies by
Definition: fourvector.h:392
smash::FourVector::end
const_iterator end() const
Definition: fourvector.h:289
smash::FourVector::velocity
ThreeVector velocity() const
Get the velocity (3-vector divided by zero component).
Definition: fourvector.h:323
smash::FourVector::operator>
bool operator>(const FourVector &a) const
checks if for all (all four vector components are above comparison vector)
Definition: fourvector.h:336
smash::FourVector::end
iterator end()
Definition: fourvector.h:284
smash::FourVector::abs
double abs() const
calculate the lorentz invariant absolute value
Definition: fourvector.h:454
smash::FourVector::abs3
double abs3() const
calculate the absolute value of the spatial three-vector
Definition: fourvector.h:466
smash::ThreeVector::abs
double abs() const
Definition: threevector.h:261
smash::FourVector
Definition: fourvector.h:33
smash::FourVector::operator+=
FourVector operator+=(const FourVector &a)
adds
Definition: fourvector.h:350
smash::FourVector::cbegin
const_iterator cbegin() const
Definition: fourvector.h:292
smash::FourVector::lorentz_boost
FourVector lorentz_boost(const ThreeVector &v) const
Returns the FourVector boosted with velocity v.
Definition: fourvector.cc:16
smash::FourVector::begin
const_iterator begin() const
Definition: fourvector.h:287
smash::operator*
EnergyMomentumTensor operator*(EnergyMomentumTensor a, const double b)
Direct multiplication operator.
Definition: energymomentumtensor.h:189
smash::FourVector::sqr3
double sqr3() const
calculate the square of the spatial three-vector
Definition: fourvector.h:464
smash::FourVector::operator>=
bool operator>=(const FourVector &a) const
logical complement to FourVector::operator<(const FourVector&) const
Definition: fourvector.h:346
smash::FourVector::threevec
ThreeVector threevec() const
Definition: fourvector.h:319
smash::FourVector::operator[]
double operator[](std::size_t i) const
const overload of the [] operator
Definition: fourvector.h:67
smash::ThreeVector::x2
double x2() const
Definition: threevector.h:169
smash::FourVector::FourVector
FourVector()
default constructor nulls the fourvector components
Definition: fourvector.h:36