wuzp
Wrong Unit? Zero Points! Compile-time checks for units, coordinate systems and points vs. vectors.
Loading...
Searching...
No Matches
ActiveLinearTransformation.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"
16#include "wuzp/trafo/internal/apply_units.hpp"
17#include "wuzp/trafo/internal/pull_back.hpp"
18#include "wuzp/trafo/internal/push_forward.hpp"
19#include "wuzp/trafo/internal/with_quantities.hpp"
20#include "wuzp/trafo/traits.hpp"
21
22namespace wuzp {
23
24template <template <typename, int, int> typename ValuesT_, typename ValueT_, AnyCoordinateSystem CoordinateSystemT_, int DimensionFromV_,
25 int DimensionToV_, typename... UnitsT_>
28
29namespace internal {
30
34template <template <typename, int, int> typename ValuesT_, typename CoordinateSystemT_, typename QuantitiesT_>
35struct ActiveLinearTransformationOf {
36 template <typename... UnitsT_>
37 using WithUnits = ActiveLinearTransformation<ValuesT_, typename QuantitiesT_::ValueT, CoordinateSystemT_, Cols<QuantitiesT_>::value,
38 Rows<QuantitiesT_>::value, UnitsT_...>;
39 using type = typename ApplyUnits<typename QuantitiesT_::UnitsT, WithUnits>::type;
40};
41
42template <template <typename, int, int> typename ValuesT_, typename CoordinateSystemT_, typename QuantitiesT_>
43auto makeActiveLinearTransformation(QuantitiesT_ quantities) {
44 return typename ActiveLinearTransformationOf<ValuesT_, CoordinateSystemT_, QuantitiesT_>::type{std::move(quantities)};
45}
46
47} // namespace internal
48
49template <template <typename, int, int> typename ValuesT_, typename ValueT_, AnyCoordinateSystem CoordinateSystemT_, int DimensionFromV_,
50 int DimensionToV_, typename... UnitsT_>
53 : public ActiveTransformation<
54 ActiveLinearTransformation<ValuesT_, ValueT_, CoordinateSystemT_, DimensionFromV_, DimensionToV_, UnitsT_...>> {
55 public:
56 using ValueT = ValueT_;
57 using ValuesT = ValuesT_<ValueT_, DimensionToV_, DimensionFromV_>;
58 using UnitsT = TypeMatrix<DimensionToV_, DimensionFromV_, UnitsT_...>;
60 using CoordinateSystemT = CoordinateSystemT_;
62
63 // Copies and moves are left to the implicit constructors, see CopiesOrMoves.
64 template <typename... Args>
65 requires(!CopiesOrMoves<ActiveLinearTransformation, Args...>)
66 explicit ActiveLinearTransformation(Args&&... args) : quantities_{std::forward<Args>(args)...} {}
67
69 const QuantitiesT& getQuantities() const { return quantities_; }
72 requires(DimensionFromV_ == DimensionToV_)
73 {
74 return TensorT{quantities_};
75 }
76
82 auto inverse() const { return internal::makeActiveLinearTransformation<ValuesT_, CoordinateSystemT_>(quantities_.inverse()); }
83
86 template <template <typename, int, int> typename ValuesOtherT_, typename ValueOtherT_, typename CoordinateSystemOtherT_,
87 int DimensionFromOtherV_, int DimensionToOtherV_, typename... UnitsOtherT_>
88 bool isApprox(const ActiveLinearTransformation<ValuesOtherT_, ValueOtherT_, CoordinateSystemOtherT_, DimensionFromOtherV_,
89 DimensionToOtherV_, UnitsOtherT_...>& other,
90 const ValueT precision = Precision<ValueT>::value()) const {
91 static_assert(std::is_same_v<CoordinateSystemT_, CoordinateSystemOtherT_>, "Coordinate systems do not match.");
92 return quantities_.isApprox(other.getQuantities(), precision);
93 }
94
95 private:
98 QuantitiesT quantities_;
99};
100
101template <template <typename, int, int> typename ValuesT_, typename ValueT_, typename CoordinateSystemT_, int DimensionFromV_,
102 int DimensionToV_, typename... UnitsT_>
103struct GetCoordinateSystem<ActiveLinearTransformation<ValuesT_, ValueT_, CoordinateSystemT_, DimensionFromV_, DimensionToV_, UnitsT_...>> {
104 using type = CoordinateSystemT_;
105};
106
107// The transformations of tensors and the conjugations by an active linear transformation, and its compositions, only use its
108// matrix, getQuantities(), and the matrix of its inverse, inverse().getQuantities(): they take any transformation with the matrix
109// of the linear transformation, so that those of the rotations, which are linear transformations too, inherit them (see
110// ActiveSpatialRotation). The inverse of a rotation is its transpose, so a rotation transforms covectors and tensors with lower
111// indices without a matrix inverse: without its rounding errors, and also for integer matrices.
112// Like a passive linear transformation, it pushes vectors and tensors with two upper indices forward by its matrix, and pulls
113// covectors and tensors with two lower indices back by its inverse (see internal::pushForward() and internal::pullBack()), within
114// its coordinate system.
115
116template <template <typename, int, int> typename ValuesT_, typename ValueT_, typename CoordinateSystemT_, int DimensionFromV_,
117 int DimensionToV_, typename... UnitsT_, typename QuantitiesOtherT_, typename CoordinateSystemOtherT_>
118struct TransformationTraits<ActiveLinearTransformation<ValuesT_, ValueT_, CoordinateSystemT_, DimensionFromV_, DimensionToV_, UnitsT_...>,
119 Tensor<Indices<Down>, QuantitiesOtherT_, CoordinateSystemOtherT_, Relative>> {
120 template <typename TransformationT_>
121 static auto transform(const TransformationT_& transformation,
122 const Tensor<Indices<Down>, QuantitiesOtherT_, CoordinateSystemOtherT_, Relative>& object) {
123 // A covector transforms with the inverse, w' = w A^-1: it is pulled back by A^-1.
124 return internal::pullBack<CoordinateSystemT_, CoordinateSystemT_>(transformation.inverse().getQuantities(), object);
125 }
126};
127
128template <template <typename, int, int> typename ValuesT_, typename ValueT_, typename CoordinateSystemT_, int DimensionFromV_,
129 int DimensionToV_, typename... UnitsT_, typename QuantitiesOtherT_, typename CoordinateSystemOtherT_>
130struct TransformationTraits<ActiveLinearTransformation<ValuesT_, ValueT_, CoordinateSystemT_, DimensionFromV_, DimensionToV_, UnitsT_...>,
131 Tensor<Indices<Down, Down>, QuantitiesOtherT_, CoordinateSystemOtherT_, Relative>> {
132 template <typename TransformationT_>
133 static auto transform(const TransformationT_& transformation,
134 const Tensor<Indices<Down, Down>, QuantitiesOtherT_, CoordinateSystemOtherT_, Relative>& object) {
135 // 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.
136 return internal::pullBack<CoordinateSystemT_, CoordinateSystemT_>(transformation.inverse().getQuantities(), object);
137 }
138};
139
140template <template <typename, int, int> typename ValuesT_, typename ValueT_, typename CoordinateSystemT_, int DimensionFromV_,
141 int DimensionToV_, typename... UnitsT_, typename QuantitiesOtherT_, typename CoordinateSystemOtherT_, typename AbsoluteOtherT_>
142struct TransformationTraits<ActiveLinearTransformation<ValuesT_, ValueT_, CoordinateSystemT_, DimensionFromV_, DimensionToV_, UnitsT_...>,
143 Tensor<Indices<Up>, QuantitiesOtherT_, CoordinateSystemOtherT_, AbsoluteOtherT_>> {
144 template <typename TransformationT_>
145 static auto transform(const TransformationT_& transformation,
146 const Tensor<Indices<Up>, QuantitiesOtherT_, CoordinateSystemOtherT_, AbsoluteOtherT_>& object) {
147 // A vector transforms like v' = A v, also a point, through its coordinates relative to the origin.
148 return internal::pushForward<CoordinateSystemT_, CoordinateSystemT_>(transformation.getQuantities(), object);
149 }
150};
151
152template <template <typename, int, int> typename ValuesT_, typename ValueT_, typename CoordinateSystemT_, int DimensionFromV_,
153 int DimensionToV_, typename... UnitsT_, typename QuantitiesOtherT_, typename CoordinateSystemOtherT_>
154struct TransformationTraits<ActiveLinearTransformation<ValuesT_, ValueT_, CoordinateSystemT_, DimensionFromV_, DimensionToV_, UnitsT_...>,
155 Tensor<Indices<Up, Down>, QuantitiesOtherT_, CoordinateSystemOtherT_, Relative>> {
156 template <typename TransformationT_>
157 static auto transform(const TransformationT_& transformation,
158 const Tensor<Indices<Up, Down>, QuantitiesOtherT_, CoordinateSystemOtherT_, Relative>& object) {
159 // A linear map transforms like T' = A T A^-1.
160 return internal::withQuantities<CoordinateSystemT_>(
161 object,
162 innerProduct(transformation.getQuantities(), innerProduct(object.getQuantities(), transformation.inverse().getQuantities())));
163 }
164};
165
166template <template <typename, int, int> typename ValuesT_, typename ValueT_, typename CoordinateSystemT_, int DimensionFromV_,
167 int DimensionToV_, typename... UnitsT_, typename QuantitiesOtherT_, typename CoordinateSystemOtherT_>
168struct TransformationTraits<ActiveLinearTransformation<ValuesT_, ValueT_, CoordinateSystemT_, DimensionFromV_, DimensionToV_, UnitsT_...>,
169 Tensor<Indices<Up, Up>, QuantitiesOtherT_, CoordinateSystemOtherT_, Relative>> {
170 template <typename TransformationT_>
171 static auto transform(const TransformationT_& transformation,
172 const Tensor<Indices<Up, Up>, QuantitiesOtherT_, CoordinateSystemOtherT_, Relative>& object) {
173 // A tensor with two upper indices, e.g. a covariance matrix, transforms like T' = A T A^T.
174 return internal::pushForward<CoordinateSystemT_, CoordinateSystemT_>(transformation.getQuantities(), object);
175 }
176};
177
178// The conjugation A T_t A^-1 of a translation by a linear transformation is the translation by A t: A (A^-1 x + t) = x + A t.
179template <template <typename, int, int> typename ValuesT_, typename ValueT_, typename CoordinateSystemT_, int DimensionFromV_,
180 int DimensionToV_, typename... UnitsT_, template <typename, int, int> typename ValuesOtherT_, int DimensionOtherV_,
181 typename ValueOtherT_, typename CoordinateSystemOtherT_, typename... UnitsOtherT_>
182struct TransformationTraits<ActiveLinearTransformation<ValuesT_, ValueT_, CoordinateSystemT_, DimensionFromV_, DimensionToV_, UnitsT_...>,
183 ActiveTranslation<ValuesOtherT_, DimensionOtherV_, ValueOtherT_, CoordinateSystemOtherT_, UnitsOtherT_...>> {
184 template <typename TransformationT_>
185 static auto transform(
186 const TransformationT_& transformation,
188 return ActiveTranslation<ValuesOtherT_, DimensionOtherV_, ValueOtherT_, CoordinateSystemOtherT_, UnitsOtherT_...>(
189 transformation.transform(object.getTensor()));
190 }
191};
192
193// The conjugation A B A^-1 of a linear transformation by a linear transformation, like the transformation of a linear map.
194template <template <typename, int, int> typename ValuesT_, typename ValueT_, typename CoordinateSystemT_, int DimensionFromV_,
195 int DimensionToV_, typename... UnitsT_, template <typename, int, int> typename ValuesOtherT_, typename ValueOtherT_,
196 typename CoordinateSystemOtherT_, int DimensionFromOtherV_, int DimensionToOtherV_, typename... UnitsOtherT_>
197struct TransformationTraits<ActiveLinearTransformation<ValuesT_, ValueT_, CoordinateSystemT_, DimensionFromV_, DimensionToV_, UnitsT_...>,
198 ActiveLinearTransformation<ValuesOtherT_, ValueOtherT_, CoordinateSystemOtherT_, DimensionFromOtherV_,
199 DimensionToOtherV_, UnitsOtherT_...>> {
200 template <typename TransformationT_>
201 static auto transform(const TransformationT_& transformation,
202 const ActiveLinearTransformation<ValuesOtherT_, ValueOtherT_, CoordinateSystemOtherT_, DimensionFromOtherV_,
203 DimensionToOtherV_, UnitsOtherT_...>& object) {
204 return ActiveLinearTransformation<ValuesOtherT_, ValueOtherT_, CoordinateSystemOtherT_, DimensionFromOtherV_, DimensionToOtherV_,
205 UnitsOtherT_...>(transformation.transform(object.getTensor()).getQuantities());
206 }
207};
208
209// The composition of two linear transformations is the product of their matrices, A_lhs A_rhs.
210template <template <typename, int, int> typename ValuesLhsT_, typename ValueLhsT_, typename CoordinateSystemLhsT_, int DimensionFromLhsV_,
211 int DimensionToLhsV_, typename... UnitsLhsT_, template <typename, int, int> typename ValuesRhsT_, typename ValueRhsT_,
212 typename CoordinateSystemRhsT_, int DimensionFromRhsV_, int DimensionToRhsV_, typename... UnitsRhsT_>
214 ActiveLinearTransformation<ValuesLhsT_, ValueLhsT_, CoordinateSystemLhsT_, DimensionFromLhsV_, DimensionToLhsV_, UnitsLhsT_...>,
215 ActiveLinearTransformation<ValuesRhsT_, ValueRhsT_, CoordinateSystemRhsT_, DimensionFromRhsV_, DimensionToRhsV_, UnitsRhsT_...>> {
216 template <typename LhsT_, typename RhsT_>
217 static auto compose(const LhsT_& lhs, const RhsT_& rhs) {
218 return internal::makeActiveLinearTransformation<ValuesLhsT_, CoordinateSystemLhsT_>(
219 innerProduct(lhs.getQuantities(), rhs.getQuantities()));
220 }
221};
222
223template <template <typename, int, int> typename ValuesLhsT_, typename ValueLhsT_, typename CoordinateSystemLhsT_, int DimensionFromLhsV_,
224 int DimensionToLhsV_, typename... UnitsLhsT_, template <typename, int, int> typename ValuesRhsT_, typename ValueRhsT_,
225 typename CoordinateSystemRhsT_, int DimensionFromRhsV_, int DimensionToRhsV_, typename... UnitsRhsT_>
226bool operator==(const ActiveLinearTransformation<ValuesLhsT_, ValueLhsT_, CoordinateSystemLhsT_, DimensionFromLhsV_, DimensionToLhsV_,
227 UnitsLhsT_...>& lhs,
228 const ActiveLinearTransformation<ValuesRhsT_, ValueRhsT_, CoordinateSystemRhsT_, DimensionFromRhsV_, DimensionToRhsV_,
229 UnitsRhsT_...>& rhs) {
230 static_assert(std::is_same_v<CoordinateSystemLhsT_, CoordinateSystemRhsT_>, "Coordinate systems do not match.");
231 return lhs.getQuantities() == rhs.getQuantities();
232}
233
234template <template <typename, int, int> typename ValuesLhsT_, typename ValueLhsT_, typename CoordinateSystemLhsT_, int DimensionFromLhsV_,
235 int DimensionToLhsV_, typename... UnitsLhsT_, template <typename, int, int> typename ValuesRhsT_, typename ValueRhsT_,
236 typename CoordinateSystemRhsT_, int DimensionFromRhsV_, int DimensionToRhsV_, typename... UnitsRhsT_>
237bool operator!=(const ActiveLinearTransformation<ValuesLhsT_, ValueLhsT_, CoordinateSystemLhsT_, DimensionFromLhsV_, DimensionToLhsV_,
238 UnitsLhsT_...>& lhs,
239 const ActiveLinearTransformation<ValuesRhsT_, ValueRhsT_, CoordinateSystemRhsT_, DimensionFromRhsV_, DimensionToRhsV_,
240 UnitsRhsT_...>& rhs) {
241 return !(lhs == rhs);
242}
243
244template <template <typename, int, int> typename ValuesT_, typename ValueT_, typename CoordinateSystemT_, int DimensionFromV_,
245 int DimensionToV_, typename... UnitsT_>
246std::ostream& operator<<(
247 std::ostream& lhs,
249 lhs << rhs.getQuantities();
250 return lhs;
251}
252
253} // namespace wuzp
TensorT getTensor() const
The matrix as a linear map, a tensor with an upper and a lower index; only for a square matrix.
auto inverse() const
The inverse transformation, whose matrix is the matrix inverse A^-1 (see Quantities::inverse()): A mu...
bool isApprox(const ActiveLinearTransformation< ValuesOtherT_, ValueOtherT_, CoordinateSystemOtherT_, DimensionFromOtherV_, DimensionToOtherV_, UnitsOtherT_... > &other, const ValueT precision=Precision< ValueT >::value()) const
Whether both linear transformations, which must be expressed in the same coordinate system,...
const QuantitiesT & getQuantities() const
The matrix A, which transforms a vector v into A v.
An active transformation, which moves objects within its coordinate system (see Transformation).
An active translation, which moves points by a vector t, p' = p + t, and leaves vectors,...
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 ActiveLinearTransformation< ValuesOtherT_, ValueOtherT_, CoordinateSystemOtherT_, DimensionFromOtherV_, DimensionToOtherV_, UnitsOtherT_... > &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