Version: SMASH-3.1
smash::ThreeVector Class Reference

#include <threevector.h>

The ThreeVector class represents a physical three-vector \( \mathbf{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.

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 \(-\mathbf{x}\). More...
 
ThreeVector operator+= (const ThreeVector &v)
 Increase this vector by \(\mathbf{v}\): \( \mathbf{x}^\prime = \mathbf{x} + \mathbf{v} \). More...
 
ThreeVector operator-= (const ThreeVector &v)
 Decrease this vector by \(\mathbf{v}\): \( \mathbf{x}^\prime = \mathbf{x} - \mathbf{v} \). More...
 
ThreeVector operator*= (const double &a)
 Scale this vector by \(a\): \( \mathbf{x}^\prime = a \cdot \mathbf{x} \). More...
 
ThreeVector operator/= (const double &a)
 Divide this vector by \(a\): \( \mathbf x^\prime = \frac{1}{a} \cdot \mathbf{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 141 of file threevector.h.

◆ const_iterator

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

iterates over the components

Definition at line 143 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.}) {}
std::array< double, 3 > x_
the internal storage of the components.
Definition: threevector.h:168

◆ 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 177 of file threevector.h.

177 { return x_[0]; }

◆ set_x1()

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

set first component

Definition at line 179 of file threevector.h.

179 { x_[0] = x; }

◆ x2()

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

Definition at line 181 of file threevector.h.

181 { return x_[1]; }

◆ set_x2()

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

set second component

Definition at line 183 of file threevector.h.

183 { x_[1] = y; }

◆ x3()

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

Definition at line 185 of file threevector.h.

185 { return x_[2]; }

◆ set_x3()

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

set third component

Definition at line 187 of file threevector.h.

187 { x_[2] = z; }

◆ sqr()

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

Definition at line 266 of file threevector.h.

266 { return (*this) * (*this); }

◆ abs()

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

Definition at line 268 of file threevector.h.

268 { return std::sqrt((*this) * (*this)); }

◆ get_phi()

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

Definition at line 270 of file threevector.h.

270  {
271  if (std::abs(x1()) < really_small && std::abs(x2()) < really_small) {
272  return 0.;
273  } else {
274  return std::atan2(x2(), x1());
275  }
276 }
double x2() const
Definition: threevector.h:181
double x1() const
Definition: threevector.h:177
constexpr double really_small
Numerical error tolerance.
Definition: constants.h:37

◆ get_theta()

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

Definition at line 278 of file threevector.h.

278  {
279  double r = abs();
280  return (r > 0.) ? std::acos(x3() / r) : 0.;
281 }
double abs() const
Definition: threevector.h:268
double x3() const
Definition: threevector.h:185

◆ 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 283 of file threevector.h.

283  {
284  // Compute the cosine and sine for each angle.
285  const double cos_phi = std::cos(phi);
286  const double sin_phi = std::sin(phi);
287  const double cos_theta = std::cos(theta);
288  const double sin_theta = std::sin(theta);
289  const double cos_psi = std::cos(psi);
290  const double sin_psi = std::sin(psi);
291  // Get original coordinates.
292  std::array<double, 3> x_old = x_;
293  // Compute new coordinates.
294  x_[0] = (cos_phi * cos_psi - sin_phi * cos_theta * sin_psi) * x_old[0] +
295  (-cos_phi * sin_psi - sin_phi * cos_theta * cos_psi) * x_old[1] +
296  (sin_phi * sin_theta) * x_old[2];
297  x_[1] = (sin_phi * cos_psi + cos_phi * cos_theta * sin_psi) * x_old[0] +
298  (-sin_phi * sin_psi + cos_phi * cos_theta * cos_psi) * x_old[1] +
299  (-cos_phi * sin_theta) * x_old[2];
300  x_[2] = (sin_theta * sin_psi) * x_old[0] + (sin_theta * cos_psi) * x_old[1] +
301  (cos_theta)*x_old[2];
302 }

◆ 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 304 of file threevector.h.

304  {
305  const double cost = std::cos(theta);
306  const double sint = std::sin(theta);
307  // Get original coordinates.
308  std::array<double, 3> x_old = x_;
309  // Compute new coordinates.
310  x_[0] = cost * x_old[0] + sint * x_old[2];
311  // x_[1] is unchanged
312  x_[2] = -sint * x_old[0] + cost * x_old[2];
313 }

◆ 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 315 of file threevector.h.

315  {
316  const double cost = std::cos(theta);
317  const double sint = std::sin(theta);
318  // Get original coordinates.
319  std::array<double, 3> x_old = x_;
320  // Compute new coordinates.
321  x_[0] = cost * x_old[0] - sint * x_old[1];
322  x_[1] = sint * x_old[0] + cost * x_old[1];
323  // x_[2] is unchanged
324 }

◆ 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 326 of file threevector.h.

326  {
327  rotate_around_y(r.get_theta());
328  rotate_around_z(r.get_phi());
329 }
void rotate_around_y(double theta)
Rotate the vector around the y axis by the given angle theta.
Definition: threevector.h:304
void rotate_around_z(double theta)
Rotate the vector around the z axis by the given angle theta.
Definition: threevector.h:315

◆ operator-()

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

Negation: Returns \(-\mathbf{x}\).

Definition at line 189 of file threevector.h.

189  {
190  ThreeVector neg(-x_[0], -x_[1], -x_[2]);
191  return neg;
192 }
ThreeVector()
default constructor (nulls all components)
Definition: threevector.h:34

◆ operator+=()

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

Increase this vector by \(\mathbf{v}\): \( \mathbf{x}^\prime = \mathbf{x} + \mathbf{v} \).

Definition at line 194 of file threevector.h.

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

◆ operator-=()

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

Decrease this vector by \(\mathbf{v}\): \( \mathbf{x}^\prime = \mathbf{x} - \mathbf{v} \).

Definition at line 207 of file threevector.h.

207  {
208  x_[0] -= v.x_[0];
209  x_[1] -= v.x_[1];
210  x_[2] -= v.x_[2];
211  return *this;
212 }

◆ operator*=()

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

Scale this vector by \(a\): \( \mathbf{x}^\prime = a \cdot \mathbf{x} \).

Definition at line 220 of file threevector.h.

220  {
221  x_[0] *= a;
222  x_[1] *= a;
223  x_[2] *= a;
224  return *this;
225 }

◆ operator/=()

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

Divide this vector by \(a\): \( \mathbf x^\prime = \frac{1}{a} \cdot \mathbf{x}\).

Definition at line 252 of file threevector.h.

252  {
253  const double a_inv = 1.0 / a;
254  x_[0] *= a_inv;
255  x_[1] *= a_inv;
256  x_[2] *= a_inv;
257  return *this;
258 }

◆ operator==()

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

Definition at line 131 of file threevector.h.

131 { 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 133 of file threevector.h.

133 { return x_ != rhs.x_; }

◆ cross_product()

ThreeVector smash::ThreeVector::cross_product ( const ThreeVector b) const
inline
Returns
cross product of this vector \(\mathbf{x}\) and another vector: \( \mathbf{x} \times \mathbf{b} \)

Definition at line 246 of file threevector.h.

246  {
247  return ThreeVector(x_[1] * b.x3() - x_[2] * b.x2(),
248  x_[2] * b.x1() - x_[0] * b.x3(),
249  x_[0] * b.x2() - x_[1] * b.x1());
250 }

◆ 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 151 of file threevector.h.

151 { return x_.begin(); }

◆ end() [1/2]

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

Definition at line 154 of file threevector.h.

154 { return x_.end(); }

◆ begin() [2/2]

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

const overload of the above

Definition at line 157 of file threevector.h.

157 { return x_.begin(); }

◆ end() [2/2]

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

const overload of the above

Definition at line 159 of file threevector.h.

159 { return x_.end(); }

◆ cbegin()

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

Definition at line 162 of file threevector.h.

162 { return x_.cbegin(); }

◆ cend()

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

Definition at line 164 of file threevector.h.

164 { return x_.cend(); }

Member Data Documentation

◆ x_

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

the internal storage of the components.

Definition at line 168 of file threevector.h.


The documentation for this class was generated from the following file: