wuzp
Wrong Unit? Zero Points! Compile-time checks for units, coordinate systems and points vs. vectors.
Loading...
Searching...
No Matches
LorentzTransformation.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <ostream>
4#include <stdexcept>
5#include <type_traits>
6#include <utility>
7
11#include "wuzp/core/Tensor.hpp"
13#include "wuzp/core/helpers.hpp"
14#include "wuzp/core/traits.hpp"
17#include "wuzp/trafo/internal/validity.hpp"
18#include "wuzp/trafo/traits.hpp"
19
20namespace wuzp {
21
22namespace internal {
23
26template <typename ValuesT_>
27ValuesT_ minkowskiMetric() {
28 auto result = identity<ValuesT_>();
29 for (int index = 1; index < Rows<ValuesT_>::value; ++index) {
30 result.set(index, index, typename ValuesT_::ValueT{-1});
31 }
32 return result;
33}
34
35} // namespace internal
36
51template <template <typename, int, int> typename ValuesT_, typename ValueT_, MinkowskiCoordinateSystem FromT_,
52 MinkowskiCoordinateSystem ToT_, int DimensionV_>
55 : public PassiveTransformation<LorentzTransformation<ValuesT_, ValueT_, FromT_, ToT_, DimensionV_>, FromT_, ToT_> {
56 public:
58 using LinearTransformationT = PassiveLinearTransformation<ValuesT_, ValueT_, FromT_, ToT_, DimensionV_,
59 typename AccessUnits<AccessImplementation<ValueT_>>::Dimensionless>;
60 using ValueT = ValueT_;
64
66 LorentzTransformation() : quantities_{internal::identity<ValuesT>()} {}
67
71 template <typename... Args>
72 requires(sizeof...(Args) > 0 && !CopiesOrMoves<LorentzTransformation, Args...>)
73 explicit LorentzTransformation(Args&&... args) : quantities_{std::forward<Args>(args)...} {
74 internal::checkValidity(*this, "The matrix of a Lorentz transformation must preserve the Minkowski metric: Lambda^T eta Lambda = eta.");
75 }
76
79 template <typename... Args>
80 static LorentzTransformation makeUnchecked(Args&&... args) {
81 return LorentzTransformation{Unchecked{}, QuantitiesT{std::forward<Args>(args)...}};
82 }
83
85 const QuantitiesT& getQuantities() const { return quantities_; }
86
88 auto inverse() const {
89 const QuantitiesT metric{internal::minkowskiMetric<ValuesT>()};
91 innerProduct(metric, innerProduct(quantities_.transpose(), metric)));
92 }
93
98 bool isValid(const ValueT tolerance = ValidityTolerance<ValueT>::value()) const {
99 const auto metric = internal::minkowskiMetric<ValuesT>();
100 const auto& values = quantities_.getValues();
101 return internal::maxAbsDifference(ValuesT{values.transpose().innerProduct(metric.innerProduct(values))}, metric) <= tolerance;
102 }
103
106 template <template <typename, int, int> typename ValuesOtherT_, typename ValueOtherT_, typename FromOtherT_, typename ToOtherT_,
107 int DimensionOtherV_>
109 const ValueT precision = Precision<ValueT>::value()) const {
110 static_assert(internal::SameFrames<LorentzTransformation, std::remove_cvref_t<decltype(other)>>::value);
111 return quantities_.isApprox(other.getQuantities(), precision);
112 }
113
114 private:
115 struct Unchecked {};
116 LorentzTransformation(Unchecked /*tag*/, QuantitiesT quantities) : quantities_{std::move(quantities)} {}
117
119 QuantitiesT quantities_;
120};
121
122namespace internal {
123
124template <template <typename, int, int> typename ValuesT_, typename ValueT_, typename FromT_, typename ToT_, int DimensionV_>
125struct IsChangeOfBasis<LorentzTransformation<ValuesT_, ValueT_, FromT_, ToT_, DimensionV_>> : std::true_type {};
126
127} // namespace internal
128
129template <template <typename, int, int> typename ValuesT_, typename ValueT_, typename FromT_, typename ToT_>
131template <template <typename, int, int> typename ValuesT_, typename ValueT_, typename FromT_, typename ToT_>
133template <template <typename, int, int> typename ValuesT_, typename ValueT_, typename FromT_, typename ToT_>
135
136// A Lorentz transformation transforms tensors like the passive linear transformation with its matrix, but with eta Lambda^T eta
137// as the inverse, e.g. for covectors.
138template <template <typename, int, int> typename ValuesT_, typename ValueT_, typename FromT_, typename ToT_, int DimensionV_,
139 typename Object>
140struct TransformationTraits<LorentzTransformation<ValuesT_, ValueT_, FromT_, ToT_, DimensionV_>, Object>
141 : TransformationTraits<typename LorentzTransformation<ValuesT_, ValueT_, FromT_, ToT_, DimensionV_>::LinearTransformationT, Object> {};
142
143// The composition X_AB * X_BC = X_AC of two Lorentz transformations is the Lorentz transformation Lambda_AB Lambda_BC.
144template <template <typename, int, int> typename ValuesLhsT_, typename ValueLhsT_, typename FromLhsT_, typename ToLhsT_, int DimensionLhsV_,
145 template <typename, int, int> typename ValuesRhsT_, typename ValueRhsT_, typename FromRhsT_, typename ToRhsT_, int DimensionRhsV_>
154
155// A Lorentz transformation is a linear transformation: its compositions with linear transformations are those of the linear
156// transformation with its matrix, the products of the matrices.
157template <template <typename, int, int> typename ValuesLhsT_, typename ValueLhsT_, typename FromLhsT_, typename ToLhsT_, int DimensionLhsV_,
158 template <typename, int, int> typename ValuesRhsT_, typename ValueRhsT_, typename FromRhsT_, typename ToRhsT_, int DimensionRhsV_,
159 typename... UnitsRhsT_>
160struct CompositionTraits<LorentzTransformation<ValuesLhsT_, ValueLhsT_, FromLhsT_, ToLhsT_, DimensionLhsV_>,
161 PassiveLinearTransformation<ValuesRhsT_, ValueRhsT_, FromRhsT_, ToRhsT_, DimensionRhsV_, UnitsRhsT_...>>
162 : CompositionTraits<typename LorentzTransformation<ValuesLhsT_, ValueLhsT_, FromLhsT_, ToLhsT_, DimensionLhsV_>::LinearTransformationT,
163 PassiveLinearTransformation<ValuesRhsT_, ValueRhsT_, FromRhsT_, ToRhsT_, DimensionRhsV_, UnitsRhsT_...>> {};
164
165template <template <typename, int, int> typename ValuesLhsT_, typename ValueLhsT_, typename FromLhsT_, typename ToLhsT_, int DimensionLhsV_,
166 typename... UnitsLhsT_, template <typename, int, int> typename ValuesRhsT_, typename ValueRhsT_, typename FromRhsT_,
167 typename ToRhsT_, int DimensionRhsV_>
168struct CompositionTraits<PassiveLinearTransformation<ValuesLhsT_, ValueLhsT_, FromLhsT_, ToLhsT_, DimensionLhsV_, UnitsLhsT_...>,
169 LorentzTransformation<ValuesRhsT_, ValueRhsT_, FromRhsT_, ToRhsT_, DimensionRhsV_>>
171 PassiveLinearTransformation<ValuesLhsT_, ValueLhsT_, FromLhsT_, ToLhsT_, DimensionLhsV_, UnitsLhsT_...>,
172 typename LorentzTransformation<ValuesRhsT_, ValueRhsT_, FromRhsT_, ToRhsT_, DimensionRhsV_>::LinearTransformationT> {};
173
174template <template <typename, int, int> typename ValuesLhsT_, typename ValueLhsT_, typename FromLhsT_, typename ToLhsT_, int DimensionLhsV_,
175 template <typename, int, int> typename ValuesRhsT_, typename ValueRhsT_, typename FromRhsT_, typename ToRhsT_, int DimensionRhsV_>
178 static_assert(internal::SameFrames<std::remove_cvref_t<decltype(lhs)>, std::remove_cvref_t<decltype(rhs)>>::value);
179 return lhs.getQuantities() == rhs.getQuantities();
180}
181
182template <template <typename, int, int> typename ValuesLhsT_, typename ValueLhsT_, typename FromLhsT_, typename ToLhsT_, int DimensionLhsV_,
183 template <typename, int, int> typename ValuesRhsT_, typename ValueRhsT_, typename FromRhsT_, typename ToRhsT_, int DimensionRhsV_>
188
189template <template <typename, int, int> typename ValuesT_, typename ValueT_, typename FromT_, typename ToT_, int DimensionV_>
191 lhs << rhs.getQuantities();
192 return lhs;
193}
194
196template <template <typename, int, int> typename ValuesT_, typename ValueT_>
198 return Quantity<ValuesT_<ValueT_, 1, 1>, typename AccessUnits<AccessImplementation<ValueT_>>::Velocity>{static_cast<ValueT_>(299792458)};
199}
200
209template <typename QuantitiesT_, typename CoordinateSystemT_>
211 static_assert(internal::RejectBatch<Indices<Up>, QuantitiesT_>::value);
213 "getInvariant() computes the interval with the Minkowski metric, so the vector must be expressed in a Minkowski "
214 "coordinate system, e.g. CoordinateSystem<Spacetime, Minkowski>.");
215 constexpr int dimension = Rows<QuantitiesT_>::value;
216 const auto& quantities = vector.getQuantities();
217 return pow<2>(quantities.template get<0, 0>()) - quantities.template block<1, 0, dimension - 1, 1>().squaredNorm();
218}
219
237template <template <typename, int, int> typename ValuesT_, typename ValueT_, typename FromT_, typename ToT_, int DimensionV_>
241 using QuantityT = Quantity<ValuesT_<ValueT_, 1, 1>, typename AccessUnits<AccessImplementation<ValueT_>>::Dimensionless>;
242 // The velocity is expressed in From, a Minkowski coordinate system, in which squaredNorm() of a vector is rejected. The spatial
243 // part of the Minkowski metric is -I, so the squared magnitude of the velocity is the squared norm of its components.
244 const QuantityT squaredBeta{velocity.getQuantities().squaredNorm() / pow<2>(getSpeedOfLight<ValuesT_, ValueT_>())};
245 // Also rejects NaN.
246 if (!(squaredBeta.getValue() < 1)) {
247 throw std::invalid_argument("The magnitude of the velocity of a Lorentz boost must be smaller than the speed of light.");
248 }
249 const auto lorentzFactor{pow<-1, 2>(QuantityT{ValueT_{1}} - squaredBeta)};
250 // The spatial part uses (lorentzFactor - 1) / |v|^2 = lorentzFactor^2 / ((lorentzFactor + 1) c^2). The right-hand side is
251 // finite for v = 0, where the left-hand side divides zero by zero, and numerically more stable for small velocities.
252 const auto spatialFactor{pow<2>(lorentzFactor) /
253 ((lorentzFactor + QuantityT{ValueT_{1}}) * pow<2>(getSpeedOfLight<ValuesT_, ValueT_>()))};
254 QuantitiesT quantities;
255 // Set the lower left matrix inclusive the diagonal.
256 for (int row = 0; row < DimensionV_; ++row) {
257 for (int col = 0; col < row + 1; ++col) {
258 QuantityT quantity;
259 // For row 0, the column is 0 too.
260 if (row == 0) {
261 quantity = lorentzFactor;
262 } else if (col == 0) {
263 quantity = -lorentzFactor / getSpeedOfLight<ValuesT_, ValueT_>() * velocity.getQuantities().get(row - 1, 0);
264 } else if (col == row) {
265 quantity = QuantityT{ValueT_{1}} + spatialFactor * pow<2>(velocity.getQuantities().get(row - 1, 0));
266 } else {
267 quantity = spatialFactor * velocity.getQuantities().get(col - 1, 0) * velocity.getQuantities().get(row - 1, 0);
268 }
269 quantities.set(row, col, quantity);
270 }
271 }
272 // Upper right matrix is mirrored.
273 for (int row = 0; row < DimensionV_; ++row) {
274 for (int col = row + 1; col < DimensionV_; ++col) {
275 quantities.set(row, col, quantities.get(col, row));
276 }
277 }
278 // The boost preserves the metric by construction: it is not checked, which would fail for Lorentz factors so large that the
279 // rounding errors of gamma^2 exceed the tolerance.
281}
282
283} // namespace wuzp
A Lorentz transformation Lambda from the inertial frame From to the inertial frame To,...
bool isValid(const ValueT tolerance=ValidityTolerance< ValueT >::value()) const
Whether the matrix preserves the Minkowski metric eta: max_ij |(Lambda^T eta Lambda - eta)_ij| <= tol...
typename LinearTransformationT::QuantitiesT QuantitiesT
LorentzTransformation()
The identity: From and To are the same inertial frame.
typename LinearTransformationT::UnitsT UnitsT
typename LinearTransformationT::ValuesT ValuesT
auto inverse() const
The inverse Lorentz transformation, from To to From, whose matrix is eta Lambda^T eta = Lambda^-1: it...
bool isApprox(const LorentzTransformation< ValuesOtherT_, ValueOtherT_, FromOtherT_, ToOtherT_, DimensionOtherV_ > &other, const ValueT precision=Precision< ValueT >::value()) const
Whether both Lorentz transformations, which must transform between the same inertial frames,...
LorentzTransformation(Args &&... args)
The Lorentz transformation whose matrix is constructed from the arguments, e.g. its elements row by r...
static LorentzTransformation makeUnchecked(Args &&... args)
The Lorentz transformation whose matrix is constructed from the arguments, without checking it: the c...
const QuantitiesT & getQuantities() const
The matrix Lambda, which transforms the components v of a spacetime vector in From into its component...
PassiveLinearTransformation< ValuesT_, ValueT_, FromT_, ToT_, DimensionV_, typename AccessUnits< AccessImplementation< ValueT_ > >::Dimensionless > LinearTransformationT
The passive linear transformation with the matrix of the Lorentz transformation, whose transformation...
A passive linear transformation, i.e. a change of basis, from the coordinate system From to the coord...
ValuesT_< ValueT_, DimensionV_, DimensionV_ > ValuesT
TypeMatrix< DimensionV_, DimensionV_, UnitsT_... > UnitsT
A passive transformation, which expresses the objects of the coordinate system From in the coordinate...
const QuantitiesT & getQuantities() const &
Definition Tensor.hpp:140
The alias template ValueMatrix<ValueT_, RowsV_, ColsV_> of any value matrix backend,...
Definition concepts.hpp:89
Whether constructing a Class from the arguments copies or moves it, i.e. whether they are a single Cl...
Definition helpers.hpp:69
A coordinate system with the Minkowski metric, e.g. CoordinateSystem<Spacetime, Minkowski>: an inerti...
Definition concepts.hpp:40
LorentzTransformation< ValuesT_, ValueT_, FromT_, ToT_, 3 > LorentzTransformation3
auto getSpeedOfLight()
The speed of light, as a quantity of the value matrix backend ValuesT_, e.g. getSpeedOfLight<wuzp_eig...
Quantities< ValueT_, TypeMatrixImpl< MatrixSize< 1, 1 >, std::tuple< UnitT_ > > > Quantity
Container to store a single quantity.
Definition Quantity.hpp:29
bool operator!=(const Quantities< ValuesLhsT_, UnitsLhsT_ > &lhs, const Quantities< ValuesRhsT_, UnitsRhsT_ > &rhs)
LorentzTransformation< ValuesT_, ValueT_, FromT_, ToT_, DimensionV_ > createBoostMatrix(const VelocityVector< DimensionV_ - 1, ValuesT_, ValueT_, FromT_ > &velocity)
The passive Lorentz boost from From to To, in the standard form with the time component first and in ...
std::ostream & operator<<(std::ostream &lhs, const Quantities< ValuesT_, UnitsT_ > &rhs)
Tensor< Indices< Up >, QuantityVectorT_, CoordinateSystemT_, Relative > Vector
Definition Tensor.hpp:942
auto innerProduct(const Quantities< ValuesLhsT_, UnitsLhsT_ > &lhs, const Quantities< ValuesRhsT_, UnitsRhsT_ > &rhs)
auto getInvariant(const Vector< QuantitiesT_, CoordinateSystemT_ > &vector)
The Minkowski interval eta_ij v^i v^j = v_0^2 - v_1^2 - ... - v_n^2 of a vector whose first component...
bool operator==(const Quantities< ValuesLhsT_, UnitsLhsT_ > &lhs, const Quantities< ValuesRhsT_, UnitsRhsT_ > &rhs)
LorentzTransformation< ValuesT_, ValueT_, FromT_, ToT_, 2 > LorentzTransformation2
auto pow(const Quantities< ValuesT_, UnitsT_ > &quantities)
The quantities raised to a rational power P/Q, elementwise, e.g. pow<Rational<1, 2>>(area) is a lengt...
LorentzTransformation< ValuesT_, ValueT_, FromT_, ToT_, 4 > LorentzTransformation4
HomogeneousVector< DimensionV_, ValuesT_, ValueT_, typename AccessUnits< AccessImplementation< ValueT_ > >::Velocity, CoordinateSystemT_ > VelocityVector
Definition Tensor.hpp:1000
The units for values of the arithmetic type ValueT_, e.g. double, specialized for AccessImplementatio...
Definition traits.hpp:338
static auto compose(const LorentzTransformation< ValuesLhsT_, ValueLhsT_, FromLhsT_, ToLhsT_, DimensionLhsV_ > &lhs, const LorentzTransformation< ValuesRhsT_, ValueRhsT_, FromRhsT_, ToRhsT_, DimensionRhsV_ > &rhs)
The composition lhs * rhs of two transformations, which applies rhs first, then lhs: (lhs * rhs)....
Definition traits.hpp:39
static constexpr ValueT_ value()
Definition traits.hpp:356
How a transformation transforms an object: static auto transform(const TransformationT_&,...
Definition traits.hpp:19
static constexpr ValueT_ value()
Definition traits.hpp:58