wuzp
Wrong Unit? Zero Points! Compile-time checks for units, coordinate systems and points vs. vectors.
Loading...
Searching...
No Matches
Jacobian.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <ostream>
4#include <type_traits>
5#include <utility>
6
10#include "wuzp/core/Tensor.hpp"
13#include "wuzp/core/helpers.hpp"
14#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/traits.hpp"
20
21namespace wuzp {
22
23template <template <typename, int, int> typename ValuesT_, typename ValueT_, AnyCoordinateSystem FromT_, AnyCoordinateSystem ToT_,
24 int DimensionFromV_, int DimensionToV_, typename... UnitsT_>
26class Jacobian;
27
28namespace internal {
29
32template <template <typename, int, int> typename ValuesT_, typename FromT_, typename ToT_, typename QuantitiesT_>
33struct JacobianOf {
34 template <typename... UnitsT_>
35 using WithUnits =
36 Jacobian<ValuesT_, typename QuantitiesT_::ValueT, FromT_, ToT_, Cols<QuantitiesT_>::value, Rows<QuantitiesT_>::value, UnitsT_...>;
37 using type = typename ApplyUnits<typename QuantitiesT_::UnitsT, WithUnits>::type;
38};
39
40template <template <typename, int, int> typename ValuesT_, typename FromT_, typename ToT_, typename QuantitiesT_>
41auto makeJacobian(QuantitiesT_ quantities) {
42 return typename JacobianOf<ValuesT_, FromT_, ToT_, QuantitiesT_>::type{std::move(quantities)};
43}
44
47template <template <typename, int, int> typename ValuesT_, typename LhsT_, typename RhsT_>
48auto composeJacobians(const LhsT_& lhs, const RhsT_& rhs) {
49 return makeJacobian<ValuesT_, typename RhsT_::From, typename LhsT_::To>(innerProduct(lhs.getQuantities(), rhs.getQuantities()));
50}
51
52} // namespace internal
53
82template <template <typename, int, int> typename ValuesT_, typename ValueT_, AnyCoordinateSystem FromT_, AnyCoordinateSystem ToT_,
83 int DimensionFromV_, int DimensionToV_, typename... UnitsT_>
86 : public PassiveTransformation<Jacobian<ValuesT_, ValueT_, FromT_, ToT_, DimensionFromV_, DimensionToV_, UnitsT_...>, FromT_, ToT_> {
87 public:
88 using ValueT = ValueT_;
89 using ValuesT = ValuesT_<ValueT_, DimensionToV_, DimensionFromV_>;
90 using UnitsT = TypeMatrix<DimensionToV_, DimensionFromV_, UnitsT_...>;
92
93 // Copies and moves are left to the implicit constructors, see CopiesOrMoves.
94 template <typename... Args>
95 requires(!CopiesOrMoves<Jacobian, Args...>)
96 explicit Jacobian(Args&&... args) : quantities_{std::forward<Args>(args)...} {}
97
99 const QuantitiesT& getQuantities() const { return quantities_; }
100
106 template <typename ObjectT_>
107 auto pullback(const ObjectT_& object) const {
108 static_assert(std::is_same_v<typename GetCoordinateSystem<ObjectT_>::type, ToT_>, "Coordinate systems do not match.");
109 return internal::pullBack<FromT_, ToT_>(quantities_, object);
110 }
111
117 auto inverse() const {
118 static_assert(DimensionFromV_ == DimensionToV_,
119 "Only a square Jacobian, between coordinate systems of the same dimension, has an inverse.");
120 return internal::makeJacobian<ValuesT_, ToT_, FromT_>(quantities_.inverse());
121 }
122
132 auto pseudoInverse() const { return internal::makeJacobian<ValuesT_, ToT_, FromT_>(quantities_.pseudoInverse()); }
133
136 template <template <typename, int, int> typename ValuesOtherT_, typename ValueOtherT_, typename FromOtherT_, typename ToOtherT_,
137 int DimensionFromOtherV_, int DimensionToOtherV_, typename... UnitsOtherT_>
140 const ValueT precision = Precision<ValueT>::value()) const {
141 static_assert(internal::SameFrames<Jacobian, std::remove_cvref_t<decltype(other)>>::value);
142 return quantities_.isApprox(other.getQuantities(), precision);
143 }
144
145 private:
147 QuantitiesT quantities_;
148};
149
150// The tensors that transform() takes are expressed in From (see PassiveTransformation::transform()). A Jacobian pushes forward
151// relative vectors, v' = J v, and tensors with two upper indices, T' = J T J^T, like a passive linear transformation (see
152// internal::pushForward()), and rejects every other tensor.
153
154template <template <typename, int, int> typename ValuesT_, typename ValueT_, typename FromT_, typename ToT_, int DimensionFromV_,
155 int DimensionToV_, typename... UnitsT_, typename QuantitiesOtherT_, typename CoordinateSystemOtherT_>
156struct TransformationTraits<Jacobian<ValuesT_, ValueT_, FromT_, ToT_, DimensionFromV_, DimensionToV_, UnitsT_...>,
157 Tensor<Indices<Up>, QuantitiesOtherT_, CoordinateSystemOtherT_, Relative>> {
159 const Tensor<Indices<Up>, QuantitiesOtherT_, CoordinateSystemOtherT_, Relative>& object) {
160 return internal::pushForward<FromT_, ToT_>(jacobian.getQuantities(), object);
161 }
162};
163
164template <template <typename, int, int> typename ValuesT_, typename ValueT_, typename FromT_, typename ToT_, int DimensionFromV_,
165 int DimensionToV_, typename... UnitsT_, typename QuantitiesOtherT_, typename CoordinateSystemOtherT_>
166struct TransformationTraits<Jacobian<ValuesT_, ValueT_, FromT_, ToT_, DimensionFromV_, DimensionToV_, UnitsT_...>,
167 Tensor<Indices<Up, Up>, QuantitiesOtherT_, CoordinateSystemOtherT_, Relative>> {
169 const Tensor<Indices<Up, Up>, QuantitiesOtherT_, CoordinateSystemOtherT_, Relative>& object) {
170 return internal::pushForward<FromT_, ToT_>(jacobian.getQuantities(), object);
171 }
172};
173
174// A point is mapped by the change of coordinates itself, which is not linear in general: e.g. the Jacobian of the Cartesian
175// coordinates with respect to the polar coordinates at (2 m, pi/2) would map that point to (-3.14, 2) m instead of (0, 2) m.
176template <template <typename, int, int> typename ValuesT_, typename ValueT_, typename FromT_, typename ToT_, int DimensionFromV_,
177 int DimensionToV_, typename... UnitsT_, typename QuantitiesOtherT_, typename CoordinateSystemOtherT_>
178struct TransformationTraits<Jacobian<ValuesT_, ValueT_, FromT_, ToT_, DimensionFromV_, DimensionToV_, UnitsT_...>,
179 Tensor<Indices<Up>, QuantitiesOtherT_, CoordinateSystemOtherT_, Absolute>> {
181 "A Jacobian maps vectors, not points: the change of coordinates whose derivative it is maps the points.");
182};
183
184template <template <typename, int, int> typename ValuesT_, typename ValueT_, typename FromT_, typename ToT_, int DimensionFromV_,
185 int DimensionToV_, typename... UnitsT_, typename IndicesOtherT_, typename QuantitiesOtherT_, typename CoordinateSystemOtherT_,
186 typename AbsoluteOtherT_>
187struct TransformationTraits<Jacobian<ValuesT_, ValueT_, FromT_, ToT_, DimensionFromV_, DimensionToV_, UnitsT_...>,
188 Tensor<IndicesOtherT_, QuantitiesOtherT_, CoordinateSystemOtherT_, AbsoluteOtherT_>> {
190 "A Jacobian pushes forward vectors and tensors with two upper indices. Use pullback() for covectors and tensors with two "
191 "lower indices.");
192};
193
194// The composition J_AB * J_BC = J_AC of two Jacobians is the chain rule, the product of their matrices.
195template <template <typename, int, int> typename ValuesLhsT_, typename ValueLhsT_, typename FromLhsT_, typename ToLhsT_,
196 int DimensionFromLhsV_, int DimensionToLhsV_, typename... UnitsLhsT_, template <typename, int, int> typename ValuesRhsT_,
197 typename ValueRhsT_, typename FromRhsT_, typename ToRhsT_, int DimensionFromRhsV_, int DimensionToRhsV_, typename... UnitsRhsT_>
198struct CompositionTraits<Jacobian<ValuesLhsT_, ValueLhsT_, FromLhsT_, ToLhsT_, DimensionFromLhsV_, DimensionToLhsV_, UnitsLhsT_...>,
199 Jacobian<ValuesRhsT_, ValueRhsT_, FromRhsT_, ToRhsT_, DimensionFromRhsV_, DimensionToRhsV_, UnitsRhsT_...>> {
205};
206
207// A change of basis, i.e. a passive linear transformation, a rotation or a Lorentz transformation, is the Jacobian of the identity
208// map between two coordinate systems, so its compositions with Jacobians, in both orders, are Jacobians, by the chain rule: e.g.
209// R_EW * J_W expresses the task space Jacobian J_W in E instead of W.
210template <template <typename, int, int> typename ValuesLhsT_, typename ValueLhsT_, typename FromLhsT_, typename ToLhsT_,
211 int DimensionFromLhsV_, int DimensionToLhsV_, typename... UnitsLhsT_, typename RhsT_>
212 requires internal::IsChangeOfBasis<RhsT_>::value
213struct CompositionTraits<Jacobian<ValuesLhsT_, ValueLhsT_, FromLhsT_, ToLhsT_, DimensionFromLhsV_, DimensionToLhsV_, UnitsLhsT_...>,
214 RhsT_> {
216 const RhsT_& rhs) {
217 return internal::composeJacobians<ValuesLhsT_>(lhs, rhs);
218 }
219};
220
221template <typename LhsT_, template <typename, int, int> typename ValuesRhsT_, typename ValueRhsT_, typename FromRhsT_, typename ToRhsT_,
222 int DimensionFromRhsV_, int DimensionToRhsV_, typename... UnitsRhsT_>
223 requires internal::IsChangeOfBasis<LhsT_>::value
224struct CompositionTraits<LhsT_,
225 Jacobian<ValuesRhsT_, ValueRhsT_, FromRhsT_, ToRhsT_, DimensionFromRhsV_, DimensionToRhsV_, UnitsRhsT_...>> {
226 static auto compose(
227 const LhsT_& lhs,
229 return internal::composeJacobians<ValuesRhsT_>(lhs, rhs);
230 }
231};
232
233template <template <typename, int, int> typename ValuesLhsT_, typename ValueLhsT_, typename FromLhsT_, typename ToLhsT_,
234 int DimensionFromLhsV_, int DimensionToLhsV_, typename... UnitsLhsT_, template <typename, int, int> typename ValuesRhsT_,
235 typename ValueRhsT_, typename FromRhsT_, typename ToRhsT_, int DimensionFromRhsV_, int DimensionToRhsV_, typename... UnitsRhsT_>
238 static_assert(internal::SameFrames<std::remove_cvref_t<decltype(lhs)>, std::remove_cvref_t<decltype(rhs)>>::value);
239 return lhs.getQuantities() == rhs.getQuantities();
240}
241
242template <template <typename, int, int> typename ValuesLhsT_, typename ValueLhsT_, typename FromLhsT_, typename ToLhsT_,
243 int DimensionFromLhsV_, int DimensionToLhsV_, typename... UnitsLhsT_, template <typename, int, int> typename ValuesRhsT_,
244 typename ValueRhsT_, typename FromRhsT_, typename ToRhsT_, int DimensionFromRhsV_, int DimensionToRhsV_, typename... UnitsRhsT_>
249
250template <template <typename, int, int> typename ValuesT_, typename ValueT_, typename FromT_, typename ToT_, int DimensionFromV_,
251 int DimensionToV_, typename... UnitsT_>
252std::ostream& operator<<(std::ostream& lhs,
254 lhs << rhs.getQuantities();
255 return lhs;
256}
257
258} // namespace wuzp
The Jacobian J = d(To)/d(From) of a change of coordinates from the coordinate system From to the coor...
Definition Jacobian.hpp:86
Jacobian(Args &&... args)
Definition Jacobian.hpp:96
auto pseudoInverse() const
The pseudo-inverse J^+, from To to From, of a Jacobian of full rank (see Quantities::pseudoInverse())...
Definition Jacobian.hpp:132
bool isApprox(const Jacobian< ValuesOtherT_, ValueOtherT_, FromOtherT_, ToOtherT_, DimensionFromOtherV_, DimensionToOtherV_, UnitsOtherT_... > &other, const ValueT precision=Precision< ValueT >::value()) const
Whether both Jacobians, which must transform between the same coordinate systems, are approximately e...
Definition Jacobian.hpp:138
auto inverse() const
The inverse Jacobian, from To to From, the Jacobian of the inverse change of coordinates,...
Definition Jacobian.hpp:117
const QuantitiesT & getQuantities() const
The matrix J, whose element J_ij is the derivative of the coordinate x_i of To with respect to the co...
Definition Jacobian.hpp:99
auto pullback(const ObjectT_ &object) const
The pullback of a covector w in To, w J, e.g. of a force into joint torques, or of a tensor M in To w...
Definition Jacobian.hpp:107
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
internal::AbsoluteImpl< true > Absolute
Definition Absolute.hpp:17
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.
static auto compose(const Jacobian< ValuesLhsT_, ValueLhsT_, FromLhsT_, ToLhsT_, DimensionFromLhsV_, DimensionToLhsV_, UnitsLhsT_... > &lhs, const Jacobian< ValuesRhsT_, ValueRhsT_, FromRhsT_, ToRhsT_, DimensionFromRhsV_, DimensionToRhsV_, UnitsRhsT_... > &rhs)
Definition Jacobian.hpp:200
static auto compose(const Jacobian< ValuesLhsT_, ValueLhsT_, FromLhsT_, ToLhsT_, DimensionFromLhsV_, DimensionToLhsV_, UnitsLhsT_... > &lhs, const RhsT_ &rhs)
Definition Jacobian.hpp:215
static auto compose(const LhsT_ &lhs, const Jacobian< ValuesRhsT_, ValueRhsT_, FromRhsT_, ToRhsT_, DimensionFromRhsV_, DimensionToRhsV_, UnitsRhsT_... > &rhs)
Definition Jacobian.hpp:226
The composition lhs * rhs of two transformations, which applies rhs first, then lhs: (lhs * rhs)....
Definition traits.hpp:39
False for every type, so that a static_assert on it fails only when the template that names it is ins...
Definition helpers.hpp:21
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 Jacobian< ValuesT_, ValueT_, FromT_, ToT_, DimensionFromV_, DimensionToV_, UnitsT_... > &jacobian, const Tensor< Indices< Up >, QuantitiesOtherT_, CoordinateSystemOtherT_, Relative > &object)
Definition Jacobian.hpp:158
static auto transform(const Jacobian< ValuesT_, ValueT_, FromT_, ToT_, DimensionFromV_, DimensionToV_, UnitsT_... > &jacobian, const Tensor< Indices< Up, Up >, QuantitiesOtherT_, CoordinateSystemOtherT_, Relative > &object)
Definition Jacobian.hpp:168
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