Version: SMASH-2.0
smash::ThreeVector Class Reference

#include <threevector.h>

The ThreeVector class represents a physical three-vector \( \vec{x} = (x_1,x_2,x_3)\) with the components \( x_1,x_2,x_3 \). It is related to the classes FourVector and Angles, both of which can be converted into a ThreeVector using the 'threevec()' method.

Definition at line 31 of file threevector.h.

Collaboration diagram for smash::ThreeVector:
[legend]

Public Types

using iterator = std::array< double, 3 >::iterator
 iterates over the components More...
 
using const_iterator = std::array< double, 3 >::const_iterator
 iterates over the components More...
 

Public Member Functions

 ThreeVector ()
 default constructor (nulls all components) More...
 
 ThreeVector (double y1, double y2, double y3)
 Constructor for ThreeVector that takes 3 doubles to set up a ThreeVector with desired values for the components. More...
 
double & operator[] (std::size_t i)
 access the component at offset i. More...
 
double operator[] (std::size_t i) const
 const overload of the above. More...
 
double x1 () const
 
void set_x1 (double x)
 set first component More...
 
double x2 () const
 
void set_x2 (double y)
 set second component More...
 
double x3 () const
 
void set_x3 (double z)
 set third component More...
 
double sqr () const
 
double abs () const
 
double get_phi () const
 
double get_theta () const
 
void rotate (double phi, double theta, double psi)
 Rotate vector by the given Euler angles phi, theta, psi. More...
 
void rotate_around_y (double theta)
 Rotate the vector around the y axis by the given angle theta. More...
 
void rotate_around_z (double theta)
 Rotate the vector around the z axis by the given angle theta. More...
 
void rotate_z_axis_to (ThreeVector &r)
 Rotate the z-axis onto the vector r. More...
 
ThreeVector operator- () const
 negation: Returns \(-\vec x\) More...
 
ThreeVector operator+= (const ThreeVector &v)
 increase this vector by \(\vec v: \vec x^\prime = \vec x + \vec v\) More...
 
ThreeVector operator-= (const ThreeVector &v)
 decrease this vector by \(\vec v: \vec x^\prime = \vec x - \vec v\) More...
 
ThreeVector operator*= (const double &a)
 scale this vector by \(a: \vec x^\prime = a \cdot \vec x\) More...
 
ThreeVector operator/= (const double &a)
 divide this vector by \(a: \vec x^\prime = \frac{1}{a} \cdot \vec x\) More...
 
bool operator== (const ThreeVector &rhs) const
 
bool operator!= (const ThreeVector &rhs) const
 
ThreeVector cross_product (const ThreeVector &b) const
 
iterator begin ()
 
iterator end ()
 
const_iterator begin () const
 const overload of the above More...
 
const_iterator end () const
 const overload of the above More...
 
const_iterator cbegin () const
 
const_iterator cend () const
 

Private Attributes

std::array< double, 3 > x_
 the internal storage of the components. More...
 

Member Typedef Documentation

◆ iterator

using smash::ThreeVector::iterator = std::array<double, 3>::iterator

iterates over the components

Definition at line 129 of file threevector.h.

◆ const_iterator

using smash::ThreeVector::const_iterator = std::array<double, 3>::const_iterator

iterates over the components

Definition at line 131 of file threevector.h.

Constructor & Destructor Documentation

◆ ThreeVector() [1/2]

smash::ThreeVector::ThreeVector ( )
inline

default constructor (nulls all components)

Definition at line 34 of file threevector.h.

34 : x_({0., 0., 0.}) {}
Here is the caller graph for this function:

◆ ThreeVector() [2/2]

smash::ThreeVector::ThreeVector ( double  y1,
double  y2,
double  y3 
)
inline

Constructor for ThreeVector that takes 3 doubles to set up a ThreeVector with desired values for the components.

Parameters
[in]y1value of the first component
[in]y2value of the second component
[in]y3value of the third component

Definition at line 44 of file threevector.h.

44 : x_({y1, y2, y3}) {}

Member Function Documentation

◆ operator[]() [1/2]

double& smash::ThreeVector::operator[] ( std::size_t  i)
inline

access the component at offset i.

Definition at line 47 of file threevector.h.

47 { return x_[i]; }

◆ operator[]() [2/2]

double smash::ThreeVector::operator[] ( std::size_t  i) const
inline

const overload of the above.

Definition at line 49 of file threevector.h.

49 { return x_[i]; }

◆ x1()

double smash::ThreeVector::x1 ( ) const
inline
Returns
first component

Definition at line 165 of file threevector.h.

165 { return x_[0]; }
Here is the caller graph for this function:

◆ set_x1()

void smash::ThreeVector::set_x1 ( double  x)
inline

set first component

Definition at line 167 of file threevector.h.

167 { x_[0] = x; }
Here is the caller graph for this function:

◆ x2()

double smash::ThreeVector::x2 ( ) const
inline
Returns
second component

Definition at line 169 of file threevector.h.

169 { return x_[1]; }
Here is the caller graph for this function:

◆ set_x2()

void smash::ThreeVector::set_x2 ( double  y)
inline

set second component

Definition at line 171 of file threevector.h.

171 { x_[1] = y; }
Here is the caller graph for this function:

◆ x3()

double smash::ThreeVector::x3 ( ) const
inline
Returns
third component

Definition at line 173 of file threevector.h.

173 { return x_[2]; }
Here is the caller graph for this function:

◆ set_x3()

void smash::ThreeVector::set_x3 ( double  z)
inline

set third component

Definition at line 175 of file threevector.h.

175 { x_[2] = z; }
Here is the caller graph for this function:

◆ sqr()

double smash::ThreeVector::sqr ( ) const
inline
Returns
the square of the vector (which is a scalar)

Definition at line 259 of file threevector.h.

259 { return (*this) * (*this); }
Here is the caller graph for this function:

◆ abs()

double smash::ThreeVector::abs ( ) const
inline
Returns
the absolute value

Definition at line 261 of file threevector.h.

261 { return std::sqrt((*this) * (*this)); }
Here is the caller graph for this function:

◆ get_phi()

double smash::ThreeVector::get_phi ( ) const
inline
Returns
the azimuthal angle phi

Definition at line 263 of file threevector.h.

263  {
264  if (std::abs(x1()) < really_small && std::abs(x2()) < really_small) {
265  return 0.;
266  } else {
267  return std::atan2(x2(), x1());
268  }
269 }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ get_theta()

double smash::ThreeVector::get_theta ( ) const
inline
Returns
the polar angle theta

Definition at line 271 of file threevector.h.

271  {
272  double r = abs();
273  return (r > 0.) ? std::acos(x3() / r) : 0.;
274 }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ rotate()

void smash::ThreeVector::rotate ( double  phi,
double  theta,
double  psi 
)
inline

Rotate vector by the given Euler angles phi, theta, psi.

If we assume the standard basis x, y, z, then this means applying the matrix for a rotation by phi about the z-axis, followed by the matrix for a rotation by theta about the rotated x-axis. Last, psi is a rotation about the rotated z-axis. To reverse the rotation one has to therefore exchange phi and psi and use the negative values for all angles.

Parameters
[in]phiangle by which the first rotation is done about the z-axis. Range: [0,2&pi]
[in]thetaangle by which the second rotation is done about the rotated x-axis. Range: [0,&pi]
[in]psiangle by which the third rotation is done about the rotated z-axis. Range: [0,2&pi]

Euler angles are used to make rotation of several (different) position vectors belonging to one rigid body easy. A ThreeVector could be rotated via only two angles, but then the angles for rotating a rigid body consisting of multiple particles would require a different pair of rotation angles for every position.

Definition at line 276 of file threevector.h.

276  {
277  // Compute the cosine and sine for each angle.
278  const double cos_phi = std::cos(phi);
279  const double sin_phi = std::sin(phi);
280  const double cos_theta = std::cos(theta);
281  const double sin_theta = std::sin(theta);
282  const double cos_psi = std::cos(psi);
283  const double sin_psi = std::sin(psi);
284  // Get original coordinates.
285  std::array<double, 3> x_old = x_;
286  // Compute new coordinates.
287  x_[0] = (cos_phi * cos_psi - sin_phi * cos_theta * sin_psi) * x_old[0] +
288  (-cos_phi * sin_psi - sin_phi * cos_theta * cos_psi) * x_old[1] +
289  (sin_phi * sin_theta) * x_old[2];
290  x_[1] = (sin_phi * cos_psi + cos_phi * cos_theta * sin_psi) * x_old[0] +
291  (-sin_phi * sin_psi + cos_phi * cos_theta * cos_psi) * x_old[1] +
292  (-cos_phi * sin_theta) * x_old[2];
293  x_[2] = (sin_theta * sin_psi) * x_old[0] + (sin_theta * cos_psi) * x_old[1] +
294  (cos_theta)*x_old[2];
295 }
Here is the caller graph for this function:

◆ rotate_around_y()

void smash::ThreeVector::rotate_around_y ( double  theta)
inline

Rotate the vector around the y axis by the given angle theta.

Parameters
[in]thetaangle by which the rotation is done about y axis.

Definition at line 297 of file threevector.h.

297  {
298  const double cost = std::cos(theta);
299  const double sint = std::sin(theta);
300  // Get original coordinates.
301  std::array<double, 3> x_old = x_;
302  // Compute new coordinates.
303  x_[0] = cost * x_old[0] + sint * x_old[2];
304  // x_[1] is unchanged
305  x_[2] = -sint * x_old[0] + cost * x_old[2];
306 }
Here is the caller graph for this function:

◆ rotate_around_z()

void smash::ThreeVector::rotate_around_z ( double  theta)
inline

Rotate the vector around the z axis by the given angle theta.

Parameters
[in]thetaangle by which the rotation is done about z axis.

Definition at line 308 of file threevector.h.

308  {
309  const double cost = std::cos(theta);
310  const double sint = std::sin(theta);
311  // Get original coordinates.
312  std::array<double, 3> x_old = x_;
313  // Compute new coordinates.
314  x_[0] = cost * x_old[0] - sint * x_old[1];
315  x_[1] = sint * x_old[0] + cost * x_old[1];
316  // x_[2] is unchanged
317 }
Here is the caller graph for this function:

◆ rotate_z_axis_to()

void smash::ThreeVector::rotate_z_axis_to ( ThreeVector r)
inline

Rotate the z-axis onto the vector r.

Parameters
[in]rdirection in which new z-axis is aligned

Definition at line 319 of file threevector.h.

319  {
320  rotate_around_y(r.get_theta());
321  rotate_around_z(r.get_phi());
322 }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ operator-()

ThreeVector smash::ThreeVector::operator- ( ) const
inline

negation: Returns \(-\vec x\)

Definition at line 177 of file threevector.h.

177  {
178  ThreeVector neg(-x_[0], -x_[1], -x_[2]);
179  return neg;
180 }

◆ operator+=()

ThreeVector smash::ThreeVector::operator+= ( const ThreeVector v)
inline

increase this vector by \(\vec v: \vec x^\prime = \vec x + \vec v\)

Definition at line 182 of file threevector.h.

182  {
183  x_[0] += v.x_[0];
184  x_[1] += v.x_[1];
185  x_[2] += v.x_[2];
186  return *this;
187 }

◆ operator-=()

ThreeVector smash::ThreeVector::operator-= ( const ThreeVector v)
inline

decrease this vector by \(\vec v: \vec x^\prime = \vec x - \vec v\)

Definition at line 195 of file threevector.h.

195  {
196  x_[0] -= v.x_[0];
197  x_[1] -= v.x_[1];
198  x_[2] -= v.x_[2];
199  return *this;
200 }

◆ operator*=()

ThreeVector smash::ThreeVector::operator*= ( const double &  a)
inline

scale this vector by \(a: \vec x^\prime = a \cdot \vec x\)

Definition at line 208 of file threevector.h.

208  {
209  x_[0] *= a;
210  x_[1] *= a;
211  x_[2] *= a;
212  return *this;
213 }

◆ operator/=()

ThreeVector smash::ThreeVector::operator/= ( const double &  a)
inline

divide this vector by \(a: \vec x^\prime = \frac{1}{a} \cdot \vec x\)

Definition at line 245 of file threevector.h.

245  {
246  const double a_inv = 1.0 / a;
247  x_[0] *= a_inv;
248  x_[1] *= a_inv;
249  x_[2] *= a_inv;
250  return *this;
251 }

◆ operator==()

bool smash::ThreeVector::operator== ( const ThreeVector rhs) const
inline
Returns
whether the vector is identical to another vector

Definition at line 119 of file threevector.h.

119 { return x_ == rhs.x_; }

◆ operator!=()

bool smash::ThreeVector::operator!= ( const ThreeVector rhs) const
inline
Returns
whether the vector is different from another vector

Definition at line 121 of file threevector.h.

121 { return x_ != rhs.x_; }

◆ cross_product()

ThreeVector smash::ThreeVector::cross_product ( const ThreeVector b) const
inline
Returns
cross product of this vector and another vector \( \vec{this} \times \vec{b} \)
cross product of two three-vectors \( \vec{a} \times \vec{b} \)

Definition at line 239 of file threevector.h.

239  {
240  return ThreeVector(x_[1] * b.x3() - x_[2] * b.x2(),
241  x_[2] * b.x1() - x_[0] * b.x3(),
242  x_[0] * b.x2() - x_[1] * b.x1());
243 }
Here is the call graph for this function:

◆ begin() [1/2]

iterator smash::ThreeVector::begin ( )
inline
Returns
an iterator starting at the 0th component.

The iterator implements the randomIterator concept. Thus, you can simply write begin() + 1 to get an iterator that points to the 1st component.

Definition at line 139 of file threevector.h.

139 { return x_.begin(); }

◆ end() [1/2]

iterator smash::ThreeVector::end ( )
inline
Returns
an iterator pointing after the 4th component.

Definition at line 142 of file threevector.h.

142 { return x_.end(); }

◆ begin() [2/2]

const_iterator smash::ThreeVector::begin ( ) const
inline

const overload of the above

Definition at line 145 of file threevector.h.

145 { return x_.begin(); }

◆ end() [2/2]

const_iterator smash::ThreeVector::end ( ) const
inline

const overload of the above

Definition at line 147 of file threevector.h.

147 { return x_.end(); }

◆ cbegin()

const_iterator smash::ThreeVector::cbegin ( ) const
inline
See also
begin

Definition at line 150 of file threevector.h.

150 { return x_.cbegin(); }

◆ cend()

const_iterator smash::ThreeVector::cend ( ) const
inline
See also
end

Definition at line 152 of file threevector.h.

152 { return x_.cend(); }

Member Data Documentation

◆ x_

std::array<double, 3> smash::ThreeVector::x_
private

the internal storage of the components.

Definition at line 156 of file threevector.h.


The documentation for this class was generated from the following file:
smash::ThreeVector::x3
double x3() const
Definition: threevector.h:173
smash::ThreeVector::x_
std::array< double, 3 > x_
the internal storage of the components.
Definition: threevector.h:156
smash::ThreeVector::rotate_around_y
void rotate_around_y(double theta)
Rotate the vector around the y axis by the given angle theta.
Definition: threevector.h:297
smash::ThreeVector::rotate_around_z
void rotate_around_z(double theta)
Rotate the vector around the z axis by the given angle theta.
Definition: threevector.h:308
smash::really_small
constexpr double really_small
Numerical error tolerance.
Definition: constants.h:37
smash::ThreeVector::x1
double x1() const
Definition: threevector.h:165
smash::ThreeVector::abs
double abs() const
Definition: threevector.h:261
smash::ThreeVector::ThreeVector
ThreeVector()
default constructor (nulls all components)
Definition: threevector.h:34
smash::ThreeVector::x2
double x2() const
Definition: threevector.h:169