wuzp
Wrong Unit? Zero Points! Compile-time checks for units, coordinate systems and points vs. vectors.
Loading...
Searching...
No Matches
concepts.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <concepts>
4#include <ostream>
5#include <tuple>
6#include <type_traits>
7#include <utility>
8
11#include "wuzp/core/Integer.hpp"
15#include "wuzp/core/Unit.hpp"
16#include "wuzp/core/helpers.hpp"
17#include "wuzp/core/traits.hpp"
18
19// Concepts for the template parameters of the core. They check that a parameter has the right kind of type, e.g. that a
20// coordinate system is a coordinate system. Invalid combinations of valid types, e.g. matrix sizes that do not match, are
21// rejected with a static_assert and a clear message instead.
22//
23// The second part of this file defines the interface of the backends: ValueMatrixBackend and InvertibleValueMatrixBackend for
24// value matrix backends, UnitSystem for units backends. See https://en.cppreference.com/w/cpp/language/constraints for concepts.
25
26namespace wuzp {
27
29template <typename T>
31
34template <typename T>
35concept EuclideanCoordinateSystem = AnyCoordinateSystem<T> && std::same_as<typename Metric<T>::type, Euclidean>;
36
39template <typename T>
40concept MinkowskiCoordinateSystem = AnyCoordinateSystem<T> && std::same_as<typename Metric<T>::type, Minkowski>;
41
43template <typename T>
44concept AbsoluteOrRelative = std::same_as<T, Absolute> || std::same_as<T, Relative>;
45
47template <typename T>
49
50namespace internal {
51
52template <typename T>
53struct IsUnitMatrix : std::false_type {};
54template <int RowsV_, int ColsV_, typename... UnitsT_>
55struct IsUnitMatrix<TypeMatrixImpl<MatrixSize<RowsV_, ColsV_>, std::tuple<UnitsT_...>>> : std::bool_constant<(AnyUnit<UnitsT_> && ...)> {};
56
57template <typename T>
58struct IsMatrixSize : std::false_type {};
59template <int RowsV_, int ColsV_>
60struct IsMatrixSize<MatrixSize<RowsV_, ColsV_>> : std::true_type {};
61
62} // namespace internal
63
65template <typename T>
66concept AnyUnitMatrix = internal::IsUnitMatrix<T>::value;
67
69template <typename T>
70concept AnyMatrixSize = internal::IsMatrixSize<T>::value;
71
73template <typename T>
74concept ArithmeticValue = std::is_arithmetic_v<T>;
75
76// The following class templates are defined in their own headers, which use the concepts above. Their definitions repeat the
77// constraints of these declarations.
78template <AnyMatrixSize MatrixSizeT_, ArithmeticValue ValueT_, typename ImplementationT_>
79class ValuesWrapper;
80
82template <typename T>
84
88template <template <typename, int, int> typename ValueMatrixT_>
90
91template <AnyValuesWrapper ValuesT_, AnyUnitMatrix UnitsT_>
92class Quantities;
93
95template <typename T>
97
98// An upper and a lower index of a tensor, defined in Indices.hpp.
99struct Up;
100struct Down;
101
103template <typename T>
104concept UpOrDown = std::same_as<T, Up> || std::same_as<T, Down>;
105
106template <UpOrDown... VariancesT_>
107struct Indices;
108
110template <typename T>
112
113template <AnyIndices IndicesT_, AnyQuantities QuantitiesT_, AnyCoordinateSystem CoordinateSystemT_, AbsoluteOrRelative AbsoluteT_>
114class Tensor;
115
117template <typename T>
119
120// ---------------------------------------------------------------------------------------------------------------------------------
121// The backend interface
122// ---------------------------------------------------------------------------------------------------------------------------------
123//
124// A value matrix backend adapts a linear algebra library, e.g. Eigen, by specializing the traits of traits.hpp for
125// AccessImplementation<ImplementationT_>, where ImplementationT_ is a matrix or an expression of the library (its
126// implementation types), and provides an alias template ValueMatrix<ValueT_, RowsV_, ColsV_> of the ValuesWrapper of its
127// matrices. Its specializations are constrained to its own implementation types, so that several value matrix backends can be
128// used in one program. A units backend specializes the arithmetic traits for its units, e.g. Product<Unit<...>, Unit<...>>, and
129// AccessUnits for its units. A program uses exactly one units backend. The core uses nothing beyond these concepts: the tests
130// pass with the archetypes, minimal backends that provide exactly what the concepts require (tests/src/archetype_values and
131// tests/src/archetype_units). See "Writing a backend" in CONTRIBUTING.md.
132//
133// The concepts check the syntax of the traits, on representative matrices and units; the tests check their semantics, e.g. that
134// Copy does not share the values. A backend checks the concepts in its tests, e.g.
135// `static_assert(wuzp::ValueMatrixBackend<wuzp_eigen::ValueMatrix>);`, so that a backend that misses a trait fails there,
136// rather than deep inside the core in the code of its users. The checks instantiate many expressions of the backend, so the
137// backends do not check them in their headers, which would cost compile time in every translation unit of their users. A trait
138// that is not specialized at all fails with the message of its primary template, e.g. "'Norm' trait not specialized for given
139// type."; a specialization with the wrong interface makes the concept unsatisfied, and the notes of the compiler name it.
140
141namespace internal {
142
144template <template <typename, int, int> typename ValueMatrixT_, typename ValueT_, int RowsV_, int ColsV_>
145using ImplementationOf = typename ValueMatrixT_<ValueT_, RowsV_, ColsV_>::ImplementationT;
146
152template <typename ResultT_, typename TraitT_, typename EvaluatedT_>
153concept TraitResult =
154 std::same_as<ResultT_, typename TraitT_::type> && std::same_as<typename Evaluate<AccessImplementation<ResultT_>>::type, EvaluatedT_> &&
155 std::move_constructible<ResultT_> &&
156 (std::same_as<ResultT_, EvaluatedT_> ||
157 (std::constructible_from<EvaluatedT_, ResultT_> && std::constructible_from<EvaluatedT_, const ResultT_&>));
158
160template <typename ImplementationT_, typename ArgsTupleT_>
161struct IsInitializable : std::false_type {};
162template <typename ImplementationT_, typename... ArgsT_>
163 requires requires {
164 Initialize<ImplementationT_, std::tuple<ArgsT_...>>::run(std::declval<ImplementationT_&>(), std::declval<ArgsT_>()...);
165 }
166struct IsInitializable<ImplementationT_, std::tuple<ArgsT_...>> : std::true_type {};
167
173template <typename ImplementationT_, typename ValueT_>
174concept BackendFloatingPointMatrix =
175 !std::floating_point<ValueT_> || requires(const ImplementationT_& impl, ImplementationT_& target, const ValueT_ value) {
177 {
179 } -> std::same_as<bool>;
180 };
181
184template <typename ValueT_>
185struct IdentityFunction {
186 ValueT_ operator()(const ValueT_ value) const { return value; }
187};
188
193template <typename ImplementationT_, template <typename, int, int> typename ValueMatrixT_, typename ValueT_, int RowsV_, int ColsV_>
194concept BackendMatrix =
195 std::same_as<ValueMatrixT_<ValueT_, RowsV_, ColsV_>, ValuesWrapper<MatrixSize<RowsV_, ColsV_>, ValueT_, ImplementationT_>> &&
196 // The core default constructs a matrix before it initializes it, and moves the results of the traits. It copies and assigns
197 // matrices only with Copy and Move, so they need not be copyable or assignable.
198 std::default_initializable<ImplementationT_> && std::move_constructible<ImplementationT_> &&
199 requires(const ImplementationT_& impl, ImplementationT_& target,
200 const ImplementationOf<ValueMatrixT_, ValueT_, ColsV_, RowsV_>& transposed, const ValueT_ value, const int index,
201 std::ostream& stream) {
203 requires IsInitializable<ImplementationT_, HomogeneousTuple<RowsV_ * ColsV_, ValueT_>>::value;
204 {
206 } -> TraitResult<Evaluate<AccessImplementation<ImplementationT_>>, ImplementationT_>;
207 requires std::same_as<typename Rebind<AccessImplementation<ImplementationT_>, ValueT_, RowsV_, ColsV_>::type, ImplementationT_>;
208 requires std::same_as<typename Rebind<AccessImplementation<ImplementationT_>, ValueT_, 1, 1>::type,
209 ImplementationOf<ValueMatrixT_, ValueT_, 1, 1>>;
210 { Copy<AccessImplementation<ImplementationT_>>::value(impl) } -> std::same_as<ImplementationT_>;
211 { Move<AccessImplementation<ImplementationT_>>::construct(target) } -> std::same_as<ImplementationT_>;
212 Move<AccessImplementation<ImplementationT_>>::assign(target, target);
213 { Coefficient<AccessImplementation<ImplementationT_>>::get(impl, index, index) } -> std::same_as<ValueT_>;
214 Coefficient<AccessImplementation<ImplementationT_>>::set(target, index, index, value);
216 {
218 } -> std::same_as<bool>;
219 {
220 Sum<AccessImplementation<ImplementationT_>, AccessImplementation<ImplementationT_>>::value(impl, impl)
221 } -> TraitResult<Sum<AccessImplementation<ImplementationT_>, AccessImplementation<ImplementationT_>>, ImplementationT_>;
222 // Rebind also takes the expressions of the backend, e.g. for the type of an element of an unevaluated sum.
223 requires std::same_as<
224 typename Rebind<
226 ValueT_, RowsV_, ColsV_>::type,
227 ImplementationT_>;
228 {
229 Difference<AccessImplementation<ImplementationT_>, AccessImplementation<ImplementationT_>>::value(impl, impl)
230 } -> TraitResult<Difference<AccessImplementation<ImplementationT_>, AccessImplementation<ImplementationT_>>, ImplementationT_>;
231 {
233 } -> TraitResult<Negation<AccessImplementation<ImplementationT_>>, ImplementationT_>;
234 {
235 Product<AccessImplementation<ImplementationT_>, ValueT_>::value(impl, value)
236 } -> TraitResult<Product<AccessImplementation<ImplementationT_>, ValueT_>, ImplementationT_>;
237 {
238 Product<AccessImplementation<ImplementationT_>, AccessImplementation<ImplementationT_>>::value(impl, impl)
239 } -> TraitResult<Product<AccessImplementation<ImplementationT_>, AccessImplementation<ImplementationT_>>, ImplementationT_>;
240 {
241 Division<AccessImplementation<ImplementationT_>, ValueT_>::value(impl, value)
242 } -> TraitResult<Division<AccessImplementation<ImplementationT_>, ValueT_>, ImplementationT_>;
243 {
244 Division<AccessImplementation<ImplementationT_>, AccessImplementation<ImplementationT_>>::value(impl, impl)
245 } -> TraitResult<Division<AccessImplementation<ImplementationT_>, AccessImplementation<ImplementationT_>>, ImplementationT_>;
246 {
248 } -> TraitResult<CwiseInverse<AccessImplementation<ImplementationT_>>, ImplementationT_>;
249 {
250 CwiseFunction<AccessImplementation<ImplementationT_>, IdentityFunction<ValueT_>>::value(impl, IdentityFunction<ValueT_>{})
251 } -> TraitResult<CwiseFunction<AccessImplementation<ImplementationT_>, IdentityFunction<ValueT_>>, ImplementationT_>;
252 {
254 } -> TraitResult<Transpose<AccessImplementation<ImplementationT_>>, ImplementationOf<ValueMatrixT_, ValueT_, ColsV_, RowsV_>>;
255 {
256 Block<AccessImplementation<ImplementationT_>, 0, 0, RowsV_, 1>::value(impl)
257 } -> TraitResult<Block<AccessImplementation<ImplementationT_>, 0, 0, RowsV_, 1>, ImplementationOf<ValueMatrixT_, ValueT_, RowsV_, 1>>;
258 {
259 InnerProduct<AccessImplementation<ImplementationT_>,
261 } -> TraitResult<InnerProduct<AccessImplementation<ImplementationT_>,
263 ImplementationOf<ValueMatrixT_, ValueT_, RowsV_, RowsV_>>;
264 { Norm<AccessImplementation<ImplementationT_>>::value(impl) } -> std::same_as<ValueT_>;
265 { SquaredNorm<AccessImplementation<ImplementationT_>>::value(impl) } -> std::same_as<ValueT_>;
266 stream << impl;
267 } &&
268 BackendFloatingPointMatrix<ImplementationT_, ValueT_>;
269
271template <template <typename, int, int> typename ValueMatrixT_, typename ValueT_, int RowsV_, int ColsV_>
272concept BackendMatrixOfSize =
274 BackendMatrix<ImplementationOf<ValueMatrixT_, ValueT_, RowsV_, ColsV_>, ValueMatrixT_, ValueT_, RowsV_, ColsV_>;
275
278template <template <typename, int, int> typename ValueMatrixT_, typename ValueT_>
279concept BackendVectorProducts =
280 requires(const ImplementationOf<ValueMatrixT_, ValueT_, 3, 1>& vector, const ImplementationOf<ValueMatrixT_, ValueT_, 1, 3>& covector) {
281 {
284 } -> TraitResult<CrossProduct<AccessImplementation<ImplementationOf<ValueMatrixT_, ValueT_, 3, 1>>,
286 ImplementationOf<ValueMatrixT_, ValueT_, 3, 1>>;
287 {
288 OuterProduct<AccessImplementation<ImplementationOf<ValueMatrixT_, ValueT_, 3, 1>>,
290 } -> TraitResult<OuterProduct<AccessImplementation<ImplementationOf<ValueMatrixT_, ValueT_, 3, 1>>,
292 ImplementationOf<ValueMatrixT_, ValueT_, 3, 3>>;
293 };
294
296template <template <typename, int, int> typename ValueMatrixT_, typename ValueT_>
297concept BackendValues = BackendMatrixOfSize<ValueMatrixT_, ValueT_, 1, 1> && BackendMatrixOfSize<ValueMatrixT_, ValueT_, 3, 1> &&
298 BackendMatrixOfSize<ValueMatrixT_, ValueT_, 1, 3> && BackendMatrixOfSize<ValueMatrixT_, ValueT_, 3, 3> &&
299 BackendVectorProducts<ValueMatrixT_, ValueT_>;
300
302template <template <typename, int, int> typename ValueMatrixT_, typename ValueT_, int DimensionV_>
303concept BackendInverse = requires(const ImplementationOf<ValueMatrixT_, ValueT_, DimensionV_, DimensionV_>& impl) {
304 {
306 } -> TraitResult<Inverse<AccessImplementation<ImplementationOf<ValueMatrixT_, ValueT_, DimensionV_, DimensionV_>>>,
307 ImplementationOf<ValueMatrixT_, ValueT_, DimensionV_, DimensionV_>>;
308};
309
311template <template <typename, int, int> typename ValueMatrixT_, typename ValueT_, int DimensionV_>
312concept BackendDeterminant = requires(const ImplementationOf<ValueMatrixT_, ValueT_, DimensionV_, DimensionV_>& impl) {
313 {
315 } -> std::same_as<ValueT_>;
316};
317
318} // namespace internal
319
337template <template <typename, int, int> typename ValueMatrixT_>
339 AnyValueMatrix<ValueMatrixT_> && internal::BackendValues<ValueMatrixT_, double> && internal::BackendValues<ValueMatrixT_, int>;
340
345template <template <typename, int, int> typename ValueMatrixT_>
347 ValueMatrixBackend<ValueMatrixT_> && internal::BackendInverse<ValueMatrixT_, double, 1> &&
348 internal::BackendInverse<ValueMatrixT_, double, 3> && internal::BackendDeterminant<ValueMatrixT_, double, 1> &&
349 internal::BackendDeterminant<ValueMatrixT_, double, 3> && internal::BackendDeterminant<ValueMatrixT_, int, 1> &&
350 internal::BackendDeterminant<ValueMatrixT_, int, 3>;
351
352namespace internal {
353
355template <typename LhsT_, typename RhsT_>
356using UnitProduct = typename Product<LhsT_, RhsT_>::type;
357
359template <typename LhsT_, typename RhsT_>
360using UnitQuotient = typename Division<LhsT_, RhsT_>::type;
361
363template <typename UnitsT_>
364concept ConsistentUnits =
370 // Angles are dimensionless (see "Angles are dimensionless" in DESIGN.md).
371 std::same_as<typename UnitsT_::Angle, typename UnitsT_::Dimensionless> &&
372 std::same_as<typename Identity<typename UnitsT_::Length>::type, typename UnitsT_::Dimensionless> &&
373 std::same_as<typename Power<typename UnitsT_::Length, Integer<2>>::type, typename UnitsT_::Area> &&
374 // Exponents are rational, e.g. for pow<1, 2>().
375 std::same_as<typename Power<typename UnitsT_::Area, Rational<1, 2>>::type, typename UnitsT_::Length> &&
376 std::same_as<UnitProduct<typename UnitsT_::Area, typename UnitsT_::Length>, typename UnitsT_::Volume> &&
377 std::same_as<UnitQuotient<typename UnitsT_::Length, typename UnitsT_::Time>, typename UnitsT_::Velocity> &&
378 std::same_as<UnitQuotient<typename UnitsT_::Velocity, typename UnitsT_::Time>, typename UnitsT_::Acceleration> &&
379 std::same_as<typename Inverse<typename UnitsT_::Time>::type, typename UnitsT_::Frequency> &&
380 std::same_as<UnitQuotient<typename UnitsT_::Angle, typename UnitsT_::Time>, typename UnitsT_::AngularVelocity> &&
381 std::same_as<UnitQuotient<typename UnitsT_::AngularVelocity, typename UnitsT_::Time>, typename UnitsT_::AngularAcceleration> &&
382 // The cross products of the transport formulas of rigid body kinematics, e.g. w x r for the velocity of a point of a rotating
383 // body, w x (w x r) for its centripetal acceleration, 2 w x v for the Coriolis acceleration, and w1 x w2 for the angular
384 // acceleration of a frame that rotates in a rotating frame (see wuzp/trafo/kinematics.hpp).
385 std::same_as<UnitProduct<typename UnitsT_::AngularVelocity, typename UnitsT_::Length>, typename UnitsT_::Velocity> &&
386 std::same_as<UnitProduct<typename UnitsT_::AngularAcceleration, typename UnitsT_::Length>, typename UnitsT_::Acceleration> &&
387 std::same_as<UnitProduct<typename UnitsT_::AngularVelocity, typename UnitsT_::Velocity>, typename UnitsT_::Acceleration> &&
388 std::same_as<UnitProduct<typename UnitsT_::AngularVelocity, typename UnitsT_::AngularVelocity>,
389 typename UnitsT_::AngularAcceleration> &&
390 std::same_as<UnitProduct<typename UnitsT_::Mass, typename UnitsT_::Acceleration>, typename UnitsT_::Force> &&
391 std::same_as<UnitProduct<typename UnitsT_::Force, typename UnitsT_::Length>, typename UnitsT_::Energy> &&
392 // Torque is N m, the unit of energy, so that the cross product of a length and a force is a torque.
393 std::same_as<UnitProduct<typename UnitsT_::Length, typename UnitsT_::Force>, typename UnitsT_::Torque> &&
394 std::same_as<typename UnitsT_::Torque, typename UnitsT_::Energy>;
395
396} // namespace internal
397
408template <typename UnitsT_>
409concept UnitSystem = requires {
410 typename UnitsT_::Dimensionless;
411 typename UnitsT_::Angle;
412 typename UnitsT_::Length;
413 typename UnitsT_::Mass;
414 typename UnitsT_::Time;
415 typename UnitsT_::Area;
416 typename UnitsT_::Volume;
417 typename UnitsT_::Velocity;
418 typename UnitsT_::Acceleration;
419 typename UnitsT_::Frequency;
420 typename UnitsT_::AngularVelocity;
421 typename UnitsT_::AngularAcceleration;
422 typename UnitsT_::Force;
423 typename UnitsT_::Energy;
424 typename UnitsT_::Torque;
425} && internal::ConsistentUnits<UnitsT_>;
426
427} // namespace wuzp
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
Wrapper for the actual implementation of a value container.
Absolute or Relative: whether a tensor is a point or a vector.
Definition concepts.hpp:44
Any coordinate system: a specialization of CoordinateSystem, including the intermediate ones of trans...
Definition concepts.hpp:30
Any indices of a tensor: a specialization of Indices, e.g. Indices<Up, Down>.
Definition concepts.hpp:111
Any size of a matrix: a MatrixSize with rows and columns.
Definition concepts.hpp:70
Any quantity matrix: a specialization of Quantities, including Quantity.
Definition concepts.hpp:96
Any tensor: a specialization of Tensor, including points, vectors and their aliases.
Definition concepts.hpp:118
Any unit matrix: a TypeMatrix of units.
Definition concepts.hpp:66
Any unit of any units backend: a specialization of Unit.
Definition concepts.hpp:48
The alias template ValueMatrix<ValueT_, RowsV_, ColsV_> of any value matrix backend,...
Definition concepts.hpp:89
Any value matrix: a specialization of ValuesWrapper.
Definition concepts.hpp:83
An arithmetic type, e.g. int, float or double: the type of the elements of value matrices.
Definition concepts.hpp:74
A coordinate system with the Euclidean metric, the default, e.g. CoordinateSystem<World>: the coordin...
Definition concepts.hpp:35
A value matrix backend that also provides the matrix inverse (Inverse) of floating point matrices,...
Definition concepts.hpp:346
A coordinate system with the Minkowski metric, e.g. CoordinateSystem<Spacetime, Minkowski>: an inerti...
Definition concepts.hpp:40
The units of a units backend, e.g. AccessUnits<AccessImplementation<double>>: the base and derived un...
Definition concepts.hpp:409
Up or Down: whether an index of a tensor is upper (contravariant) or lower (covariant).
Definition concepts.hpp:104
A value matrix backend, given as its alias template ValueMatrix<ValueT_, RowsV_, ColsV_>,...
Definition concepts.hpp:338
Type wrapper which is used to access traits partially specialized in the implementation header files.
Definition helpers.hpp:114
Element access of a matrix of a value matrix backend: get(impl, row, col) returns an element,...
Definition traits.hpp:321
Copies a matrix of a value matrix backend: value returns the copy, which must not share its values wi...
Definition traits.hpp:246
A function applied to every element of a matrix, like Eigen's DenseBase::unaryExpr(): value(impl,...
Definition traits.hpp:203
The elementwise reciprocal of a matrix, like Eigen's ArrayBase::inverse() or cwiseInverse().
Definition traits.hpp:194
A lower, or covariant, index, e.g. of a covector w_i, whose components transform with the inverse of ...
Definition Indices.hpp:23
The Euclidean metric delta_ij of a coordinate system with orthonormal axes, e.g. of space in Cartesia...
The indices of a tensor, in their order, e.g. Indices<Up, Down> for a linear map T^i_j,...
Definition Indices.hpp:37
Initializes an allocated matrix of a value matrix backend with its elements, row by row: run(impl,...
Definition traits.hpp:316
The inverse: the matrix inverse of a matrix, like Eigen's MatrixBase::inverse(), and the reciprocal o...
Definition traits.hpp:188
Whether T is a specialization of Template.
Definition helpers.hpp:74
The Minkowski metric eta = diag(1, -1, ..., -1) of an inertial frame of special relativity,...
Moves a matrix of a value matrix backend: construct returns the moved matrix, assign moves it into an...
Definition traits.hpp:255
The negation is performed elementwise.
Definition traits.hpp:101
The evaluated type of another value type and size, of the same backend as T: e.g. the 1x1 values of a...
Definition traits.hpp:330
Allocates a default constructed matrix of a value matrix backend for the size MatrixSizeT_: run(impl)...
Definition traits.hpp:310
A matrix of the types in the std::tuple Types, row by row, of the size MatrixSizeT_,...
An upper, or contravariant, index, e.g. of a vector v^i, whose components transform like the coordina...
Definition Indices.hpp:20