wuzp
Wrong Unit? Zero Points! Compile-time checks for units, coordinate systems and points vs. vectors.
Loading...
Searching...
No Matches
traits.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <type_traits>
4#include <utility>
5
7
8namespace wuzp {
9
10// Some operations (sums, differences, products, divisions) require the corresponding trait to be defined for an arbitrary number of
11// arguments.
12#define WUZP_DEFINE_RECURSIVE_TRAIT_IMPL(Name, NameStr) \
13 template <typename... Ts> \
14 struct Name; \
15 template <typename T0> \
16 struct Name<T0> { \
17 using type = T0; \
18 }; \
19 template <typename T0, typename T1> \
20 struct Name<T0, T1> { \
21 /* NOLINTNEXTLINE(bugprone-macro-parentheses) */ \
22 static_assert(FailOnInstantiation<Name<T0, T1>>::value, "'" NameStr "' trait not specialized for given type."); \
23 }; \
24 template <typename T0, typename T1, typename T2, typename... Ts> \
25 struct Name<T0, T1, T2, Ts...> { \
26 /* NOLINTNEXTLINE(bugprone-macro-parentheses) */ \
27 using type = typename Name<typename Name<T0, T1>::type, T2, Ts...>::type; \
28 };
29#define WUZP_DEFINE_RECURSIVE_TRAIT(Name) WUZP_DEFINE_RECURSIVE_TRAIT_IMPL(Name, #Name)
30
31template <typename Label>
32struct Tag {};
33template <typename T, typename... Args>
34struct GetTag;
35
42
43template <typename T>
44struct GetTypes {
45 static_assert(FailOnInstantiation<GetTypes<T>>::value, "'GetTypes' trait not specialized for given type.");
46};
47
48template <typename T>
49struct GetType {
50 static_assert(FailOnInstantiation<GetType<T>>::value, "'GetType' trait not specialized for given type.");
51};
52
53template <typename T>
54struct GetValue {
55 static_assert(FailOnInstantiation<GetValue<T>>::value, "'GetValue' trait not specialized for given type.");
56};
57
58template <typename T>
59struct Rows {
60 static_assert(FailOnInstantiation<Rows<T>>::value, "'Rows' trait not specialized for given type.");
61};
62
63template <typename T>
64struct Cols {
65 static_assert(FailOnInstantiation<Cols<T>>::value, "'Cols' trait not specialized for given type.");
66};
67
68template <typename T>
69struct Dimension {
70 static constexpr int value = static_cast<int>(Rows<T>::value > 1) + static_cast<int>(Cols<T>::value > 1);
71};
72
73template <typename Lhs, typename Rhs>
74struct Comparison {
75 static_assert(FailOnInstantiation<Comparison<Lhs, Rhs>>::value, "'Comparison' trait not specialized for given type.");
76};
77
80template <typename Lhs, typename Rhs>
81struct Sum<ValueType<Lhs>, ValueType<Rhs>> {
82 static_assert(std::is_same<Lhs, Rhs>::value, "Value types mismatch.");
83 using type = Lhs;
84};
85template <typename Lhs, typename Rhs>
86auto sum(const Lhs& lhs, const Rhs& rhs);
87
90template <typename Lhs, typename Rhs>
91struct Difference<ValueType<Lhs>, ValueType<Rhs>> {
92 static_assert(std::is_same<Lhs, Rhs>::value, "Value types mismatch.");
93 using type = Lhs;
94};
95// Using `auto` as return type results in compiler error: function is used before deduction of `auto`.
96template <typename Lhs, typename Rhs>
97auto difference(const Lhs& lhs, const Rhs& rhs);
98
100template <typename Rhs>
101struct Negation {
102 static_assert(FailOnInstantiation<Negation<Rhs>>::value, "'Negation' trait not specialized for given type.");
103};
104template <typename Rhs>
105auto negation(const Rhs& rhs);
106
109template <typename Lhs, typename Rhs>
110struct Product<ValueType<Lhs>, ValueType<Rhs>> {
111 static_assert(std::is_same<Lhs, Rhs>::value, "Value types mismatch.");
112 using type = Lhs;
113};
114template <typename Lhs, typename Rhs>
115auto product(const Lhs& lhs, const Rhs& rhs);
116
117WUZP_DEFINE_RECURSIVE_TRAIT(InnerProduct)
118template <typename Lhs, typename Rhs>
119auto innerProduct(const Lhs& lhs, const Rhs& rhs);
120
121WUZP_DEFINE_RECURSIVE_TRAIT(OuterProduct)
122template <typename Lhs, typename Rhs>
123auto outerProduct(const Lhs& lhs, const Rhs& rhs);
124
125template <typename Lhs, typename Rhs>
127 static_assert(FailOnInstantiation<DotProduct<Lhs, Rhs>>::value, "'DotProduct' trait not specialized for given type.");
128};
129template <typename Lhs, typename Rhs>
130auto dotProduct(const Lhs& lhs, const Rhs& rhs);
131
132template <typename Lhs, typename Rhs>
134 static_assert(FailOnInstantiation<CrossProduct<Lhs, Rhs>>::value, "'CrossProduct' trait not specialized for given type.");
135};
136template <typename Lhs, typename Rhs>
137auto crossProduct(const Lhs& lhs, const Rhs& rhs);
138
141template <typename Lhs, typename Rhs>
142struct Division<ValueType<Lhs>, ValueType<Rhs>> {
143 static_assert(std::is_same<Lhs, Rhs>::value, "Value types mismatch.");
144 using type = Lhs;
145};
146template <typename Lhs, typename Rhs>
147auto division(const Lhs& lhs, const Rhs& rhs);
148
149template <typename T, typename ExponentT_>
150struct Power {
151 static_assert(FailOnInstantiation<Power<T, ExponentT_>>::value, "'Power' trait not specialized for given type.");
152};
153
154template <typename T>
155struct Transpose {
156 static_assert(FailOnInstantiation<Transpose<T>>::value, "'Transpose' trait not specialized for given type.");
157};
158
159template <typename Object, int ElementRowIdV_, int ElementColIdV_>
160struct Element {
162 "'Element' trait not specialized for given type.");
163};
164
165template <typename Object, int BlockRowIdV_, int BlockColIdV_, int BlockRowsV_, int BlockColsV_>
166struct Block {
168 "'Block' trait not specialized for given type.");
169};
170
171template <typename T>
172struct Norm {
173 static_assert(FailOnInstantiation<Norm<T>>::value, "'Norm' trait not specialized for given type.");
174};
175
176template <typename T>
178 static_assert(FailOnInstantiation<SquaredNorm<T>>::value, "'SquaredNorm' trait not specialized for given type.");
179};
180
181template <typename T>
183 static_assert(FailOnInstantiation<Determinant<T>>::value, "'Determinant' trait not specialized for given type.");
184};
185
187template <typename T>
188struct Inverse {
189 static_assert(FailOnInstantiation<Inverse<T>>::value, "'Inverse' trait not specialized for given type.");
190};
191
193template <typename T>
195 static_assert(FailOnInstantiation<CwiseInverse<T>>::value, "'CwiseInverse' trait not specialized for given type.");
196};
197
202template <typename T, typename FunctionT_>
204 static_assert(FailOnInstantiation<CwiseFunction<T, FunctionT_>>::value, "'CwiseFunction' trait not specialized for given type.");
205};
206
207template <typename T>
208struct Identity {
209 static_assert(FailOnInstantiation<Identity<T>>::value, "'Identity' trait not specialized for given type.");
210};
211
212template <typename T>
213struct Zero {
214 static_assert(FailOnInstantiation<Zero<T>>::value, "'Zero' trait not specialized for given type.");
215};
216
217template <typename T>
218struct Random {
219 static_assert(FailOnInstantiation<Random<T>>::value, "'Random' trait not specialized for given type.");
220};
221
222template <typename T>
223struct Evaluate {
224 using type = std::decay_t<T>; // Default e.g. for arithmetic types.
225};
226
227// A value matrix backend adapts a linear algebra library by specializing the traits of this file, e.g. Sum, Transpose or Copy,
228// for AccessImplementation<ImplementationT_>, where ImplementationT_ is a matrix or an expression of the library. ValueMatrixBackend
229// in concepts.hpp lists the traits the core needs. The specializations are constrained to the backend's own implementation types,
230// e.g. `template <EigenArray ImplementationT_>`, so that several value matrix backends can be used in one program (see "The backend
231// interface" in DESIGN.md). Specializations that apply a C++ operator of the library, e.g. `implLhs + implRhs` for Sum, inherit
232// it from internal/operator_traits.hpp, e.g. from internal::OperatorSum.
233
234// Value matrices, quantities and tensors have value semantics with every backend: they never share their values with another
235// object (see "Results of operations are evaluated" in DESIGN.md). A value matrix backend provides this with Copy and Move,
236// which it specializes for AccessImplementation<ImplementationT_> of all its implementation types, including expressions. The
237// results of its other traits, e.g. Transpose<AccessImplementation<ImplementationT_>>::value() or Evaluate, may be lazy
238// expressions, which the core evaluates into new matrices, but must not be matrices that share the values of their operands:
239// the core moves such results instead of copying them. E.g. Blitz++'s transpose() and slices are arrays that share the values
240// of the original, so the Blitz++ backend copies them.
241
245template <typename T>
246struct Copy {
247 static_assert(FailOnInstantiation<Copy<T>>::value, "'Copy' trait not specialized for given type.");
248};
249
254template <typename T>
255struct Move {
256 static_assert(FailOnInstantiation<Move<T>>::value, "'Move' trait not specialized for given type.");
257};
258
264template <typename To, typename... From>
265struct IsValidConversion : std::false_type {};
266
267template <typename T>
269 static_assert(FailOnInstantiation<IsAbsolute<T>>::value, "'IsAbsolute' trait not specialized for given type.");
270};
271
272template <typename T>
274 static_assert(FailOnInstantiation<MakeRelative<T>>::value, "'MakeRelative' trait not specialized for given type.");
275};
276
277template <typename T>
278auto makeRelative(T t) {
279 return MakeRelative<T>::make(std::move(t));
280}
281
282template <typename T>
284 static_assert(FailOnInstantiation<MakeAbsolute<T>>::value, "'MakeAbsolute' trait not specialized for given type.");
285};
286
287template <typename T>
288auto makeAbsolute(T t) {
289 return MakeAbsolute<T>::make(std::move(t));
290}
291
292template <typename T>
294 static_assert(FailOnInstantiation<GetCoordinateSystem<T>>::value, "'GetCoordinateSystem' trait not specialized for given type.");
295};
296
297template <typename T, typename NewCoordinateSystemT_>
300 "'CastCoordinateSystem' trait not specialized for given types.");
301};
302template <typename To, typename T>
304 return typename Evaluate<typename CastCoordinateSystem<T, To>::type>::type{std::move(t).getValues()};
305}
306
309template <typename ImplementationT_, typename MatrixSizeT_>
311
315template <typename ImplementationT_, typename ArgsTupleT_>
317
320template <typename T>
322 static_assert(FailOnInstantiation<Coefficient<T>>::value, "'Coefficient' trait not specialized for given type.");
323};
324
329template <typename T, typename ValueT_, int RowsV_, int ColsV_>
330struct Rebind {
331 static_assert(FailOnInstantiation<Rebind<T, ValueT_, RowsV_, ColsV_>>::value, "'Rebind' trait not specialized for given type.");
332};
333
337template <typename UnitT_>
339 static_assert(FailOnInstantiation<AccessUnits<UnitT_>>::value,
340 "'AccessUnits' trait not specialized for given type. Make sure you included the necessary unit headers.");
341};
342
353template <typename ValueT_>
354struct Precision {
355 static_assert(std::is_integral_v<ValueT_>, "'Precision' trait not specialized for given types.");
356 static constexpr ValueT_ value() { return 0; }
357};
358
359template <>
360struct Precision<float> {
361 static constexpr float value() { return 1e-5f; }
362};
363
364template <>
365struct Precision<double> {
366 static constexpr double value() { return 1e-12; }
367};
368
369template <>
370struct Precision<long double> {
371 static constexpr long double value() { return Precision<double>::value(); }
372};
373
374} // namespace wuzp
#define WUZP_DEFINE_RECURSIVE_TRAIT(Name)
Definition traits.hpp:29
auto product(const Quantities< ValuesLhsT_, UnitsLhsT_ > &lhs, const ValueRhsT_ rhs)
Tag< FromImplementationLabel > FromImplementation
Definition traits.hpp:39
auto negation(const Quantities< ValuesT_, UnitsT_ > &rhs)
auto outerProduct(const Quantities< ValuesLhsT_, UnitsLhsT_ > &lhs, const Quantities< ValuesRhsT_, UnitsRhsT_ > &rhs)
auto dotProduct(const Lhs &lhs, const Rhs &rhs)
auto innerProduct(const Quantities< ValuesLhsT_, UnitsLhsT_ > &lhs, const Quantities< ValuesRhsT_, UnitsRhsT_ > &rhs)
auto crossProduct(const Lhs &lhs, const Rhs &rhs)
auto makeRelative(T t)
The vector from the origin of the coordinate system to a point, p - o: the same components,...
Definition traits.hpp:278
Tag< FromImplementationArgsLabel > FromImplementationArgs
Definition traits.hpp:41
auto castCoordinateSystem(T t)
Definition traits.hpp:303
Tag< FromSimilarLabel > FromSimilar
Definition traits.hpp:37
auto division(const Quantities< ValuesLhsT_, UnitsLhsT_ > &lhs, const ValueRhsT_ rhs)
auto difference(const Quantities< ValuesLhsT_, UnitsLhsT_ > &lhs, const Quantities< ValuesRhsT_, UnitsRhsT_ > &rhs)
auto sum(const Quantities< ValuesLhsT_, UnitsLhsT_ > &lhs, const Quantities< ValuesRhsT_, UnitsRhsT_ > &rhs)
auto makeAbsolute(T t)
The point at a vector from the origin of the coordinate system, o + v: the same components,...
Definition traits.hpp:288
The units for values of the arithmetic type ValueT_, e.g. double, specialized for AccessImplementatio...
Definition traits.hpp:338
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
static constexpr int value
Definition traits.hpp:70
std::decay_t< T > type
Definition traits.hpp:224
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
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 converting a From into a To passes the checks of the converting constructor of To,...
Definition traits.hpp:265
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
static constexpr double value()
Definition traits.hpp:366
static constexpr float value()
Definition traits.hpp:361
static constexpr long double value()
Definition traits.hpp:371
The default precision of isApprox() for values of the type ValueT_, which compares relatively: 1e-12 ...
Definition traits.hpp:354
static constexpr ValueT_ value()
Definition traits.hpp:356
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
Wrapper for partial specializations of trait for values types.
Definition helpers.hpp:110