wuzp
Wrong Unit? Zero Points! Compile-time checks for units, coordinate systems and points vs. vectors.
Loading...
Searching...
No Matches
PassiveLinearTransformation.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <ostream>
4#include <type_traits>
5#include <utility>
6
12#include "wuzp/core/helpers.hpp"
13#include "wuzp/core/traits.hpp"
15#include "wuzp/trafo/internal/apply_units.hpp"
16#include "wuzp/trafo/internal/pull_back.hpp"
17#include "wuzp/trafo/internal/push_forward.hpp"
18#include "wuzp/trafo/internal/with_quantities.hpp"
19#include "wuzp/trafo/traits.hpp"
20
21namespace wuzp {
22
23template <template <typename, int, int> typename ValuesT_, typename ValueT_, AnyCoordinateSystem FromT_, AnyCoordinateSystem ToT_,
24 int DimensionV_, typename... UnitsT_>
27
28namespace internal {
29
33template <template <typename, int, int> typename ValuesT_, typename FromT_, typename ToT_, typename QuantitiesT_>
34struct PassiveLinearTransformationOf {
35 template <typename... UnitsT_>
36 using WithUnits =
37 PassiveLinearTransformation<ValuesT_, typename QuantitiesT_::ValueT, FromT_, ToT_, Rows<QuantitiesT_>::value, UnitsT_...>;
38 using type = typename ApplyUnits<typename QuantitiesT_::UnitsT, WithUnits>::type;
39};
40
41template <template <typename, int, int> typename ValuesT_, typename FromT_, typename ToT_, typename QuantitiesT_>
42auto makePassiveLinearTransformation(QuantitiesT_ quantities) {
43 return typename PassiveLinearTransformationOf<ValuesT_, FromT_, ToT_, QuantitiesT_>::type{std::move(quantities)};
44}
45
46} // namespace internal
47
62template <template <typename, int, int> typename ValuesT_, typename ValueT_, AnyCoordinateSystem FromT_, AnyCoordinateSystem ToT_,
63 int DimensionV_, typename... UnitsT_>
66 : public PassiveTransformation<PassiveLinearTransformation<ValuesT_, ValueT_, FromT_, ToT_, DimensionV_, UnitsT_...>, FromT_, ToT_> {
67 public:
68 using ValueT = ValueT_;
69 using ValuesT = ValuesT_<ValueT_, DimensionV_, DimensionV_>;
70 using UnitsT = TypeMatrix<DimensionV_, DimensionV_, UnitsT_...>;
72
73 // Copies and moves are left to the implicit constructors, see CopiesOrMoves.
74 template <typename... Args>
76 explicit PassiveLinearTransformation(Args&&... args) : quantities_{std::forward<Args>(args)...} {}
77
81 const QuantitiesT& getQuantities() const { return quantities_; }
82
89 auto inverse() const { return internal::makePassiveLinearTransformation<ValuesT_, ToT_, FromT_>(quantities_.inverse()); }
90
93 template <template <typename, int, int> typename ValuesOtherT_, typename ValueOtherT_, typename FromOtherT_, typename ToOtherT_,
94 int DimensionOtherV_, typename... UnitsOtherT_>
97 const ValueT precision = Precision<ValueT>::value()) const {
98 static_assert(internal::SameFrames<PassiveLinearTransformation, std::remove_cvref_t<decltype(other)>>::value);
99 return quantities_.isApprox(other.getQuantities(), precision);
100 }
101
102 private:
105 QuantitiesT quantities_;
106};
107
108namespace internal {
109
110template <template <typename, int, int> typename ValuesT_, typename ValueT_, typename FromT_, typename ToT_, int DimensionV_,
111 typename... UnitsT_, typename NewFromT_, typename NewToT_>
112struct WithFrames<PassiveLinearTransformation<ValuesT_, ValueT_, FromT_, ToT_, DimensionV_, UnitsT_...>, NewFromT_, NewToT_> {
113 using type = PassiveLinearTransformation<ValuesT_, ValueT_, NewFromT_, NewToT_, DimensionV_, UnitsT_...>;
114};
115
116template <template <typename, int, int> typename ValuesT_, typename ValueT_, typename FromT_, typename ToT_, int DimensionV_,
117 typename... UnitsT_>
118struct IsChangeOfBasis<PassiveLinearTransformation<ValuesT_, ValueT_, FromT_, ToT_, DimensionV_, UnitsT_...>> : std::true_type {};
119
120} // namespace internal
121
122// The tensors are expressed in From (see PassiveTransformation::transform()). Their components are transformed, and the result
123// is expressed in To.
124// The transformations of tensors by a passive linear transformation, and its compositions, only use its matrix, getQuantities(),
125// and the matrix of its inverse, inverse().getQuantities(): they take any transformation with the matrix of the linear
126// transformation, so that those of the rotations and of the Lorentz transformations, which are linear transformations too,
127// inherit them (see PassiveSpatialRotation and LorentzTransformation). Their inverses, R^T and eta Lambda^T eta, need no matrix
128// inverse, so they transform covectors and tensors with lower indices without its rounding errors, and also for integer
129// matrices.
130
131template <template <typename, int, int> typename ValuesT_, typename ValueT_, typename FromT_, typename ToT_, int DimensionV_,
132 typename... UnitsT_, typename QuantitiesOtherT_, typename CoordinateSystemOtherT_>
133struct TransformationTraits<PassiveLinearTransformation<ValuesT_, ValueT_, FromT_, ToT_, DimensionV_, UnitsT_...>,
134 Tensor<Indices<Down>, QuantitiesOtherT_, CoordinateSystemOtherT_, Relative>> {
135 template <typename TransformationT_>
136 static auto transform(const TransformationT_& transformation,
137 const Tensor<Indices<Down>, QuantitiesOtherT_, CoordinateSystemOtherT_, Relative>& object) {
138 // A covector transforms with the inverse, w' = w A^-1: it is pulled back by A^-1, which maps the vectors of To into From.
139 return internal::pullBack<ToT_, FromT_>(transformation.inverse().getQuantities(), object);
140 }
141};
142
143template <template <typename, int, int> typename ValuesT_, typename ValueT_, typename FromT_, typename ToT_, int DimensionV_,
144 typename... UnitsT_, typename QuantitiesOtherT_, typename CoordinateSystemOtherT_>
145struct TransformationTraits<PassiveLinearTransformation<ValuesT_, ValueT_, FromT_, ToT_, DimensionV_, UnitsT_...>,
146 Tensor<Indices<Down, Down>, QuantitiesOtherT_, CoordinateSystemOtherT_, Relative>> {
147 template <typename TransformationT_>
148 static auto transform(const TransformationT_& transformation,
149 const Tensor<Indices<Down, Down>, QuantitiesOtherT_, CoordinateSystemOtherT_, Relative>& object) {
150 // A tensor with two lower indices, e.g. a mass matrix, transforms like M' = A^-T M A^-1: it is pulled back by A^-1.
151 return internal::pullBack<ToT_, FromT_>(transformation.inverse().getQuantities(), object);
152 }
153};
154
155template <template <typename, int, int> typename ValuesT_, typename ValueT_, typename FromT_, typename ToT_, int DimensionV_,
156 typename... UnitsT_, typename QuantitiesOtherT_, typename CoordinateSystemOtherT_, typename AbsoluteOtherT_>
157struct TransformationTraits<PassiveLinearTransformation<ValuesT_, ValueT_, FromT_, ToT_, DimensionV_, UnitsT_...>,
158 Tensor<Indices<Up>, QuantitiesOtherT_, CoordinateSystemOtherT_, AbsoluteOtherT_>> {
159 template <typename TransformationT_>
160 static auto transform(const TransformationT_& transformation,
161 const Tensor<Indices<Up>, QuantitiesOtherT_, CoordinateSystemOtherT_, AbsoluteOtherT_>& object) {
162 // A vector transforms like v' = A v, also a point, through its coordinates relative to the origin.
163 return internal::pushForward<FromT_, ToT_>(transformation.getQuantities(), object);
164 }
165};
166
167template <template <typename, int, int> typename ValuesT_, typename ValueT_, typename FromT_, typename ToT_, int DimensionV_,
168 typename... UnitsT_, typename QuantitiesOtherT_, typename CoordinateSystemOtherT_>
169struct TransformationTraits<PassiveLinearTransformation<ValuesT_, ValueT_, FromT_, ToT_, DimensionV_, UnitsT_...>,
170 Tensor<Indices<Up, Down>, QuantitiesOtherT_, CoordinateSystemOtherT_, Relative>> {
171 template <typename TransformationT_>
172 static auto transform(const TransformationT_& transformation,
173 const Tensor<Indices<Up, Down>, QuantitiesOtherT_, CoordinateSystemOtherT_, Relative>& object) {
174 // A linear map transforms like T' = A T A^-1.
175 return internal::withQuantities<FromT_, ToT_>(
176 object,
177 innerProduct(transformation.getQuantities(), innerProduct(object.getQuantities(), transformation.inverse().getQuantities())));
178 }
179};
180
181template <template <typename, int, int> typename ValuesT_, typename ValueT_, typename FromT_, typename ToT_, int DimensionV_,
182 typename... UnitsT_, typename QuantitiesOtherT_, typename CoordinateSystemOtherT_>
183struct TransformationTraits<PassiveLinearTransformation<ValuesT_, ValueT_, FromT_, ToT_, DimensionV_, UnitsT_...>,
184 Tensor<Indices<Up, Up>, QuantitiesOtherT_, CoordinateSystemOtherT_, Relative>> {
185 template <typename TransformationT_>
186 static auto transform(const TransformationT_& transformation,
187 const Tensor<Indices<Up, Up>, QuantitiesOtherT_, CoordinateSystemOtherT_, Relative>& object) {
188 // A tensor with two upper indices, e.g. a covariance matrix, transforms like T' = A T A^T.
189 return internal::pushForward<FromT_, ToT_>(transformation.getQuantities(), object);
190 }
191};
192
193// The composition X_AB * X_BC = X_AC of two linear transformations is the product of their matrices, A_lhs A_rhs.
194template <template <typename, int, int> typename ValuesLhsT_, typename ValueLhsT_, typename FromLhsT_, typename ToLhsT_, int DimensionLhsV_,
195 typename... UnitsLhsT_, template <typename, int, int> typename ValuesRhsT_, typename ValueRhsT_, typename FromRhsT_,
196 typename ToRhsT_, int DimensionRhsV_, typename... UnitsRhsT_>
197struct CompositionTraits<PassiveLinearTransformation<ValuesLhsT_, ValueLhsT_, FromLhsT_, ToLhsT_, DimensionLhsV_, UnitsLhsT_...>,
198 PassiveLinearTransformation<ValuesRhsT_, ValueRhsT_, FromRhsT_, ToRhsT_, DimensionRhsV_, UnitsRhsT_...>> {
199 template <typename LhsT_, typename RhsT_>
200 static auto compose(const LhsT_& lhs, const RhsT_& rhs) {
201 return internal::makePassiveLinearTransformation<ValuesLhsT_, FromRhsT_, ToLhsT_>(
202 innerProduct(lhs.getQuantities(), rhs.getQuantities()));
203 }
204};
205
206template <template <typename, int, int> typename ValuesLhsT_, typename ValueLhsT_, typename FromLhsT_, typename ToLhsT_, int DimensionLhsV_,
207 typename... UnitsLhsT_, template <typename, int, int> typename ValuesRhsT_, typename ValueRhsT_, typename FromRhsT_,
208 typename ToRhsT_, int DimensionRhsV_, typename... UnitsRhsT_>
211 static_assert(internal::SameFrames<std::remove_cvref_t<decltype(lhs)>, std::remove_cvref_t<decltype(rhs)>>::value);
212 return lhs.getQuantities() == rhs.getQuantities();
213}
214
215template <template <typename, int, int> typename ValuesLhsT_, typename ValueLhsT_, typename FromLhsT_, typename ToLhsT_, int DimensionLhsV_,
216 typename... UnitsLhsT_, template <typename, int, int> typename ValuesRhsT_, typename ValueRhsT_, typename FromRhsT_,
217 typename ToRhsT_, int DimensionRhsV_, typename... UnitsRhsT_>
222
223template <template <typename, int, int> typename ValuesT_, typename ValueT_, typename FromT_, typename ToT_, int DimensionV_,
224 typename... UnitsT_>
225std::ostream& operator<<(std::ostream& lhs,
227 lhs << rhs.getQuantities();
228 return lhs;
229}
230
231} // namespace wuzp
A passive linear transformation, i.e. a change of basis, from the coordinate system From to the coord...
const QuantitiesT & getQuantities() const
The matrix A, which transforms the components v of a vector in From into its components A v in To.
auto inverse() const
The inverse transformation, from To to From, whose matrix is the matrix inverse A^-1 (see Quantities:...
bool isApprox(const PassiveLinearTransformation< ValuesOtherT_, ValueOtherT_, FromOtherT_, ToOtherT_, DimensionOtherV_, UnitsOtherT_... > &other, const ValueT precision=Precision< ValueT >::value()) const
Whether both linear transformations, which must transform between the same coordinate systems,...
A passive transformation, which expresses the objects of the coordinate system From in the coordinate...
Container to store quantities.
A tensor of rank 0, 1 or 2, whose components are quantities, expressed in a coordinate system,...
Definition Tensor.hpp:99
Any coordinate system: a specialization of CoordinateSystem, including the intermediate ones of trans...
Definition concepts.hpp:30
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
bool operator!=(const Quantities< ValuesLhsT_, UnitsLhsT_ > &lhs, const Quantities< ValuesRhsT_, UnitsRhsT_ > &rhs)
std::ostream & operator<<(std::ostream &lhs, const Quantities< ValuesT_, UnitsT_ > &rhs)
auto innerProduct(const Quantities< ValuesLhsT_, UnitsLhsT_ > &lhs, const Quantities< ValuesRhsT_, UnitsRhsT_ > &rhs)
internal::AbsoluteImpl< false > Relative
Definition Absolute.hpp:18
bool operator==(const Quantities< ValuesLhsT_, UnitsLhsT_ > &lhs, const Quantities< ValuesRhsT_, UnitsRhsT_ > &rhs)
typename CreateTypeMatrix< RowsV_, ColsV_, Types... >::type TypeMatrix
A matrix storing types at compile-time.
The composition lhs * rhs of two transformations, which applies rhs first, then lhs: (lhs * rhs)....
Definition traits.hpp:39
A lower, or covariant, index, e.g. of a covector w_i, whose components transform with the inverse of ...
Definition Indices.hpp:23
The indices of a tensor, in their order, e.g. Indices<Up, Down> for a linear map T^i_j,...
Definition Indices.hpp:37
static constexpr ValueT_ value()
Definition traits.hpp:356
static auto transform(const TransformationT_ &transformation, const Tensor< Indices< Up >, QuantitiesOtherT_, CoordinateSystemOtherT_, AbsoluteOtherT_ > &object)
static auto transform(const TransformationT_ &transformation, const Tensor< Indices< Down, Down >, QuantitiesOtherT_, CoordinateSystemOtherT_, Relative > &object)
static auto transform(const TransformationT_ &transformation, const Tensor< Indices< Up, Down >, QuantitiesOtherT_, CoordinateSystemOtherT_, Relative > &object)
static auto transform(const TransformationT_ &transformation, const Tensor< Indices< Down >, QuantitiesOtherT_, CoordinateSystemOtherT_, Relative > &object)
static auto transform(const TransformationT_ &transformation, const Tensor< Indices< Up, Up >, QuantitiesOtherT_, CoordinateSystemOtherT_, Relative > &object)
How a transformation transforms an object: static auto transform(const TransformationT_&,...
Definition traits.hpp:19
An upper, or contravariant, index, e.g. of a vector v^i, whose components transform like the coordina...
Definition Indices.hpp:20