wuzp
Wrong Unit? Zero Points! Compile-time checks for units, coordinate systems and points vs. vectors.
Loading...
Searching...
No Matches
Quantities.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <algorithm>
4#include <cmath>
5#include <ostream>
6#include <tuple>
7#include <type_traits>
8#include <utility>
9
10#include "wuzp/core/Integer.hpp"
15#include "wuzp/core/Units.hpp"
18#include "wuzp/core/helpers.hpp"
19#include "wuzp/core/internal/powers.hpp"
20#include "wuzp/core/traits.hpp"
21
22namespace wuzp {
23
24namespace internal {
25
29template <int ShapeV_>
30struct PseudoInverse;
31
32} // namespace internal
33
41template <AnyValuesWrapper ValuesT_, AnyUnitMatrix UnitsT_>
43 public:
44 using ValuesT = ValuesT_;
45 using UnitsT = UnitsT_;
46 using ValueT = typename ValuesT::ValueT;
48
49 static_assert(Rows<ValuesT>::value == Rows<UnitsT>::value, "Number of rows do not match.");
50 static_assert(Cols<ValuesT>::value == Cols<UnitsT>::value, "Number of columns do not match.");
51
52 Quantities() = default;
53
54 // Copies and moves are left to the implicit constructors, see CopiesOrMoves.
55 template <typename T0, typename... Ts>
57 // Allow implicit construction from similar quantity matrices with the same units. NOLINTNEXTLINE(google-explicit-constructor)
58 explicit(!IsValidConversion<ThisT, std::remove_cvref_t<T0>, std::remove_cvref_t<Ts>...>::value) Quantities(T0&& t0, Ts&&... ts)
59 : Quantities(typename GetTag<ThisT, T0, Ts...>::type{}, std::forward<T0>(t0), std::forward<Ts>(ts)...) {}
60
61 static ThisT zero() { return ThisT(ValuesT::zero()); }
62 static ThisT random() { return ThisT(ValuesT::random()); }
63
64 auto get() const
65 requires(Dimension<ThisT>::value == 0)
66 {
67 return get<0, 0>();
68 }
69
70 template <int RowIdV_, int ColIdV_>
71 auto get() const {
72 return typename Element<ThisT, RowIdV_, ColIdV_>::type{values_.template get<RowIdV_, ColIdV_>()};
73 }
74
78 template <int IdV_>
79 auto get() const
80 requires(Dimension<ThisT>::value <= 1)
81 {
82 return get<internal::vectorElementRowId<ThisT>(IdV_), internal::vectorElementColId<ThisT>(IdV_)>();
83 }
84
85 auto get(const int rowId, const int colId) const { return typename GetType<ThisT>::type{values_.get(rowId, colId)}; }
86
89 auto get(const int id) const
90 requires(Dimension<ThisT>::value <= 1)
91 {
92 return get(internal::vectorElementRowId<ThisT>(id), internal::vectorElementColId<ThisT>(id));
93 }
94
95 template <typename ValueT_, typename UnitT_>
96 void set(const Quantity<ValueT_, UnitT_>& quantity)
97 requires(Dimension<ThisT>::value == 0)
98 {
99 return set<0, 0>(quantity);
100 }
101
102 template <int RowIdV_, int ColIdV_, typename ValueT_, typename UnitT_>
103 void set(const Quantity<ValueT_, UnitT_>& quantity) {
104 static_assert(std::is_same<typename Element<UnitsT, RowIdV_, ColIdV_>::type, UnitT_>::value, "Units do not match.");
105 values_.template set<RowIdV_, ColIdV_>(quantity.getValue());
106 }
107
110 template <int IdV_, typename ValueT_, typename UnitT_>
111 void set(const Quantity<ValueT_, UnitT_>& quantity)
112 requires(Dimension<ThisT>::value <= 1)
113 {
114 set<internal::vectorElementRowId<ThisT>(IdV_), internal::vectorElementColId<ThisT>(IdV_)>(quantity);
115 }
116
117 template <typename ValueT_, typename UnitT_>
118 void set(const int rowId, const int colId, const Quantity<ValueT_, UnitT_>& quantity) {
119 static_assert(IsHomogeneous<ThisT>::value, "Quantities are not homogeneous.");
120 static_assert(std::is_same<typename Element<UnitsT, 0, 0>::type, UnitT_>::value, "Units do not match.");
121 values_.set(rowId, colId, quantity.getValue());
122 }
123
126 template <typename ValueT_, typename UnitT_>
127 void set(const int id, const Quantity<ValueT_, UnitT_>& quantity)
128 requires(Dimension<ThisT>::value <= 1)
129 {
130 set(internal::vectorElementRowId<ThisT>(id), internal::vectorElementColId<ThisT>(id), quantity);
131 }
132
139 template <typename ValuesOtherT_>
140 void setValues(const ValuesOtherT_& values) {
141 // Direct initialization checks the size, see the converting constructor of ValuesWrapper.
142 values_ = ValuesT(values);
143 }
144
147 template <typename ValueOtherT_>
148 void setValue(ValueOtherT_ value)
149 requires(Dimension<ThisT>::value == 0)
150 {
151 values_.set(value);
152 }
153
160 const ValuesT& getValues() const& { return values_; }
162 ValuesT getValues() && { return std::move(values_); }
163
165 requires(Dimension<ThisT>::value == 0)
166 {
167 return getValues().get();
168 }
169
170 template <typename ValuesOtherT_, typename UnitsOtherT_>
172 // Note: Allow other value types to support lazy evaluation.
173 static_assert(std::is_same<UnitsT, UnitsOtherT_>::value, "Units do not match.");
174 return values_.isApprox(other.getValues(), precision);
175 }
176
177 // The member functions and operators return evaluated results (see ValuesWrapper), of the evaluated types of the traits.
178
179 template <int BlockRowIdV_, int BlockColIdV_, int BlockRowsV_, int BlockColsV_>
180 auto block() const {
182 values_.template getBlock<BlockRowIdV_, BlockColIdV_, BlockRowsV_, BlockColsV_>());
183 }
184
185 auto transpose() const { return typename Evaluate<typename Transpose<ThisT>::type>::type{values_.transpose()}; }
186
187 template <typename ValuesOtherT_, typename UnitsOtherT_>
190 values_.innerProduct(other.getValues())};
191 }
192
193 template <typename ValuesOtherT_, typename UnitsOtherT_>
196 values_.outerProduct(other.getValues())};
197 }
198
199 template <typename ValuesOtherT_, typename UnitsOtherT_>
202 values_.dotProduct(other.getValues())};
203 }
204
205 template <typename ValuesOtherT_, typename UnitsOtherT_>
208 values_.crossProduct(other.getValues())};
209 }
210
213 auto inverse() const { return typename Evaluate<typename Inverse<ThisT>::type>::type{values_.inverse()}; }
214
228 auto pseudoInverse() const {
229 constexpr int rows = Rows<ThisT>::value;
230 constexpr int cols = Cols<ThisT>::value;
231 // 1, -1 or 0 (see internal::PseudoInverse), as a difference rather than with conditional operators, whose branches the
232 // compiler evaluates, so that it leaves no code that coverage reports as never executed.
233 constexpr int shape = static_cast<int>(rows < cols) - static_cast<int>(rows > cols);
234 return internal::PseudoInverse<shape>::value(*this);
235 }
236
238 auto cwiseInverse() const { return typename Evaluate<typename CwiseInverse<ThisT>::type>::type{values_.cwiseInverse()}; }
239
240 auto norm() const { return typename GetType<ThisT>::type{values_.norm()}; }
241
242 auto squaredNorm() const { return typename Power<typename GetType<ThisT>::type, Integer<2>>::type{values_.squaredNorm()}; }
243
246 auto determinant() const
247 requires(Dimension<ThisT>::value != 1)
248 {
249 return typename Determinant<ThisT>::type{values_.determinant()};
250 }
251
252 auto evaluate() const& { return typename Evaluate<ThisT>::type{values_.evaluate()}; }
253 auto evaluate() && { return typename Evaluate<ThisT>::type{std::move(values_).evaluate()}; }
254
255 private:
256 template <typename ValuesOtherT_, typename UnitsOtherT_>
257 // Allow other values types to support lazy evaluation.
258 Quantities(FromSimilar /*tag*/, const Quantities<ValuesOtherT_, UnitsOtherT_>& other) : values_(other.getValues()) {
259 static_assert(IsValidConversion<ThisT, Quantities<ValuesOtherT_, UnitsOtherT_>>::UnitsMatchV, "Units do not match.");
260 }
261
267 template <typename... QuantityValueT_, typename... QuantityUnitsT_>
268 explicit Quantities(FromImplementation /*tag*/, const Quantity<QuantityValueT_, QuantityUnitsT_>&... quantities)
269 : Quantities(FromImplementationArgs(), quantities.getValue()...) {
270 static_assert(std::is_same<std::tuple<QuantityUnitsT_...>, typename GetTypes<UnitsT>::type>::value, "Units do not match.");
271 }
272
273 template <typename... Ts>
274 explicit Quantities(FromImplementationArgs /*tag*/, Ts&&... ts) : values_(std::forward<Ts>(ts)...) {}
275
276 ValuesT values_;
277};
278
279template <typename ValuesT_, typename UnitsT_, typename ValuesOtherT_, typename UnitsOtherT_>
280struct IsValidConversion<Quantities<ValuesT_, UnitsT_>, Quantities<ValuesOtherT_, UnitsOtherT_>> {
281 // The converting constructor checks this with a static_assert, which explains why a conversion fails. The value matrices
282 // have the sizes of the unit matrices, so they match if the units match.
283 static constexpr bool UnitsMatchV = std::is_same_v<UnitsT_, UnitsOtherT_>;
284 static constexpr bool value = UnitsMatchV;
285};
286
287template <typename ValuesT_, typename UnitsT_, typename... Ts>
288struct GetTag<Quantities<ValuesT_, UnitsT_>, Ts...> {
289 private:
290 template <typename T>
291 struct IsQuantity : std::false_type {};
292 template <typename ValueT_, typename UnitT_>
293 struct IsQuantity<Quantity<ValueT_, UnitT_>> : std::true_type {};
294 using first = std::decay_t<std::tuple_element_t<0, std::tuple<Ts...>>>;
295
296 public:
297 using type = typename IfThenElse<
298 sizeof...(Ts) == 1,
299 typename IfThenElse<
301 typename IfThenElse<(IsQuantity<std::decay_t<Ts>>::value && ...), FromImplementation, FromImplementationArgs>::type>::type,
302 typename IfThenElse<(IsQuantity<std::decay_t<Ts>>::value && ...), FromImplementation, FromImplementationArgs>::type>::type;
303};
304
305// Quantities are homogeneous if all elements have the same unit.
306template <typename ValuesT_, typename UnitsT_>
307struct IsHomogeneous<Quantities<ValuesT_, UnitsT_>> {
308 static constexpr bool value = IsHomogeneous<UnitsT_>::value;
309};
310
311template <typename ValuesT_, typename UnitsT_>
312struct GetType<Quantities<ValuesT_, UnitsT_>> {
313 static_assert(IsHomogeneous<Quantities<ValuesT_, UnitsT_>>::value, "Quantities are not homogeneous.");
315};
316
317template <typename ValuesT_, typename UnitsT_>
318struct Dimension<Quantities<ValuesT_, UnitsT_>> {
319 static constexpr int value = Dimension<ValuesT_>::value;
320};
321
322template <typename ValuesT_, typename UnitsT_>
323struct Rows<Quantities<ValuesT_, UnitsT_>> {
324 static constexpr int value = Rows<ValuesT_>::value;
325};
326
327template <typename ValuesT_, typename UnitsT_>
328struct Cols<Quantities<ValuesT_, UnitsT_>> {
329 static constexpr int value = Cols<ValuesT_>::value;
330};
331
332template <typename ValuesLhsT_, typename UnitsLhsT_, typename ValuesRhsT_, typename UnitsRhsT_>
333struct Sum<Quantities<ValuesLhsT_, UnitsLhsT_>, Quantities<ValuesRhsT_, UnitsRhsT_>> {
334 using type = Quantities<typename Sum<ValuesLhsT_, ValuesRhsT_>::type, typename Sum<UnitsLhsT_, UnitsRhsT_>::type>;
335};
336
337template <typename ValuesLhsT_, typename UnitsLhsT_, typename ValuesRhsT_, typename UnitsRhsT_>
338struct Difference<Quantities<ValuesLhsT_, UnitsLhsT_>, Quantities<ValuesRhsT_, UnitsRhsT_>> {
339 using type = Quantities<typename Difference<ValuesLhsT_, ValuesRhsT_>::type, typename Difference<UnitsLhsT_, UnitsRhsT_>::type>;
340};
341
342template <typename ValuesT_, typename UnitsT_>
343struct Negation<Quantities<ValuesT_, UnitsT_>> {
345};
346
347template <typename ValuesLhsT_, typename UnitsLhsT_, typename ValuesRhsT_, typename UnitsRhsT_>
348struct Product<Quantities<ValuesLhsT_, UnitsLhsT_>, Quantities<ValuesRhsT_, UnitsRhsT_>> {
349 using type = Quantities<typename Product<ValuesLhsT_, ValuesRhsT_>::type, typename Product<UnitsLhsT_, UnitsRhsT_>::type>;
350};
351
352template <typename ValuesLhsT_, typename UnitsLhsT_, typename ValuesRhsT_, typename UnitsRhsT_>
353struct InnerProduct<Quantities<ValuesLhsT_, UnitsLhsT_>, Quantities<ValuesRhsT_, UnitsRhsT_>> {
354 using type = Quantities<typename InnerProduct<ValuesLhsT_, ValuesRhsT_>::type, typename InnerProduct<UnitsLhsT_, UnitsRhsT_>::type>;
355};
356
357template <typename ValuesLhsT_, typename UnitsLhsT_, typename ValuesRhsT_, typename UnitsRhsT_>
358struct OuterProduct<Quantities<ValuesLhsT_, UnitsLhsT_>, Quantities<ValuesRhsT_, UnitsRhsT_>> {
359 using type = Quantities<typename OuterProduct<ValuesLhsT_, ValuesRhsT_>::type, typename OuterProduct<UnitsLhsT_, UnitsRhsT_>::type>;
360};
361
362template <typename ValuesLhsT_, typename UnitsLhsT_, typename ValuesRhsT_, typename UnitsRhsT_>
366
367template <typename ValuesLhsT_, typename UnitsLhsT_, typename ValuesRhsT_, typename UnitsRhsT_>
371
372template <typename ValuesLhsT_, typename UnitsLhsT_, typename ValueRhsT_, typename UnitRhsT_>
373struct Division<Quantities<ValuesLhsT_, UnitsLhsT_>, Quantity<ValueRhsT_, UnitRhsT_>> {
374 using type =
375 Quantities<typename Division<ValuesLhsT_, typename ValueRhsT_::ValueT>::type, typename Division<UnitsLhsT_, UnitRhsT_>::type>;
376};
377
378template <typename ValuesLhsT_, typename UnitsLhsT_, typename ValuesRhsT_, typename UnitsRhsT_>
379struct Division<Quantities<ValuesLhsT_, UnitsLhsT_>, Quantities<ValuesRhsT_, UnitsRhsT_>> {
380 using type = Quantities<typename Division<ValuesLhsT_, ValuesRhsT_>::type, typename Division<UnitsLhsT_, UnitsRhsT_>::type>;
381};
382
384template <typename ValuesT_, typename UnitsT_, typename ExponentT_>
385struct Power<Quantities<ValuesT_, UnitsT_>, ExponentT_> {
387};
388
389template <typename ValuesT_, typename UnitsT_>
393
394template <typename ValuesT_, typename UnitsT_, int ElementRowIdV_, int ElementColIdV_>
398
399template <typename ValuesT_, typename UnitsT_>
400struct Inverse<Quantities<ValuesT_, UnitsT_>> {
401 // The units first, which also check that the matrix is square.
404};
405
406template <typename ValuesT_, typename UnitsT_>
410
411template <typename ValuesT_, typename UnitsT_>
415
416template <typename ValuesT_, typename UnitsT_, int BlockRowIdV_, int BlockColIdV_, int BlockRowsV_, int BlockColsV_>
421
422template <typename ValuesT_, typename UnitsT_>
423struct Evaluate<Quantities<ValuesT_, UnitsT_>> {
425};
426
427namespace internal {
428
435template <typename ValuesT_, ArithmeticValue ScalarT_>
436auto toDimensionlessQuantity(const ScalarT_ scalar) {
437 using ValueT = typename ValuesT_::ValueT;
439 "The common type of the scalar and the value type is not the value type, e.g. a double scalar with int values.");
441 static_cast<ValueT>(scalar));
442}
443
444} // namespace internal
445
446template <typename ValuesLhsT_, typename UnitsLhsT_, typename ValuesRhsT_, typename UnitsRhsT_>
451template <typename ValuesLhsT_, typename UnitsLhsT_, typename ValuesRhsT_, typename UnitsRhsT_>
453 return sum(lhs, rhs);
454}
455
456template <typename ValuesLhsT_, typename UnitsLhsT_, typename ValuesRhsT_, typename UnitsRhsT_>
461template <typename ValuesLhsT_, typename UnitsLhsT_, typename ValuesRhsT_, typename UnitsRhsT_>
463 return difference(lhs, rhs);
464}
465
466template <typename ValuesT_, typename UnitsT_>
470template <typename ValuesT_, typename UnitsT_>
472 return negation(rhs);
473}
474
475template <typename ValuesLhsT_, typename UnitsLhsT_, typename ValueRhsT_>
476auto product(const Quantities<ValuesLhsT_, UnitsLhsT_>& lhs, const ValueRhsT_ rhs) {
477 // The scalar has the value matrix backend and the value type of the quantities.
478 return product(lhs, internal::toDimensionlessQuantity<ValuesLhsT_>(rhs));
479}
480template <typename ValueLhsT_, typename ValuesRhsT_, typename UnitsRhsT_>
481auto product(const ValueLhsT_ lhs, const Quantities<ValuesRhsT_, UnitsRhsT_>& rhs) {
482 return product(rhs, lhs);
483}
484template <typename ValuesLhsT_, typename UnitsLhsT_, typename MatrixSizeRhsT_, typename ValueRhsT_, typename ImplementationRhsT_>
490template <typename MatrixSizeLhsT_, typename ValueLhsT_, typename ImplementationLhsT_, typename ValuesRhsT_, typename UnitsRhsT_>
494
497template <typename ValuesLhsT_, typename UnitsLhsT_, typename ValuesRhsT_, typename UnitsRhsT_>
502template <typename ValuesLhsT_, typename UnitsLhsT_, typename ValueRhsT_>
503auto operator*(const Quantities<ValuesLhsT_, UnitsLhsT_>& lhs, const ValueRhsT_ rhs) {
504 return product(lhs, rhs);
505}
506template <typename ValueLhsT_, typename ValuesRhsT_, typename UnitsRhsT_>
507auto operator*(const ValueLhsT_ lhs, const Quantities<ValuesRhsT_, UnitsRhsT_>& rhs) {
508 return product(lhs, rhs);
509}
510template <typename ValuesLhsT_, typename UnitsLhsT_, typename MatrixSizeRhsT_, typename ValueRhsT_, typename ImplementationRhsT_>
514template <typename MatrixSizeLhsT_, typename ValueLhsT_, typename ImplementationLhsT_, typename ValuesRhsT_, typename UnitsRhsT_>
518template <typename ValuesLhsT_, typename UnitsLhsT_, typename ValuesRhsT_, typename UnitsRhsT_>
520 return product(lhs, rhs);
521}
522
523template <typename ValuesLhsT_, typename UnitsLhsT_, typename ValuesRhsT_, typename UnitsRhsT_>
527
528template <typename ValuesLhsT_, typename UnitsLhsT_, typename ValuesRhsT_, typename UnitsRhsT_>
532
533namespace internal {
534
536template <>
537struct PseudoInverse<1> {
538 template <typename QuantitiesT_>
539 static auto value(const QuantitiesT_& matrix) {
540 const auto transpose = matrix.transpose();
541 return innerProduct(transpose, innerProduct(matrix, transpose).inverse());
542 }
543};
544
546template <>
547struct PseudoInverse<-1> {
548 template <typename QuantitiesT_>
549 static auto value(const QuantitiesT_& matrix) {
550 const auto transpose = matrix.transpose();
551 return innerProduct(innerProduct(transpose, matrix).inverse(), transpose);
552 }
553};
554
556template <>
557struct PseudoInverse<0> {
558 template <typename QuantitiesT_>
559 static auto value(const QuantitiesT_& matrix) {
560 return matrix.inverse();
561 }
562};
563
564} // namespace internal
565
566template <typename ValuesLhsT_, typename UnitsLhsT_, typename ValueRhsT_>
567auto division(const Quantities<ValuesLhsT_, UnitsLhsT_>& lhs, const ValueRhsT_ rhs) {
568 // The scalar has the value matrix backend and the value type of the quantities.
569 return division(lhs, internal::toDimensionlessQuantity<ValuesLhsT_>(rhs));
570}
571template <typename ValuesLhsT_, typename UnitsLhsT_, typename MatrixSizeRhsT_, typename ValueRhsT_, typename ImplementationRhsT_>
576template <typename ValuesLhsT_, typename UnitsLhsT_, typename ValueRhsT_, typename UnitRhsT_>
581
583template <typename ValuesLhsT_, typename UnitsLhsT_, typename ValuesRhsT_, typename UnitsRhsT_>
588template <typename ValuesLhsT_, typename UnitsLhsT_, typename ValueRhsT_>
589auto operator/(const Quantities<ValuesLhsT_, UnitsLhsT_>& lhs, const ValueRhsT_ rhs) {
590 return division(lhs, rhs);
591}
592template <typename ValuesLhsT_, typename UnitsLhsT_, typename MatrixSizeRhsT_, typename ValueRhsT_, typename ImplementationRhsT_>
596template <typename ValuesLhsT_, typename UnitsLhsT_, typename ValueRhsT_, typename UnitRhsT_>
598 return division(lhs, rhs);
599}
600
601template <typename ValuesLhsT_, typename UnitsLhsT_, typename ValuesRhsT_, typename UnitsRhsT_>
603 static_assert(std::is_same<UnitsLhsT_, UnitsRhsT_>::value, "Units do not match.");
604 return lhs.getValues() == rhs.getValues();
605}
606
607template <typename ValuesLhsT_, typename UnitsLhsT_, typename ValuesRhsT_, typename UnitsRhsT_>
609 return !(lhs == rhs);
610}
611
612template <typename ValuesT_, typename UnitsT_>
613std::ostream& operator<<(std::ostream& lhs, const Quantities<ValuesT_, UnitsT_>& rhs) {
614 lhs << rhs.getValues();
615 return lhs;
616}
617
618// Functions of quantities. The functions of one argument, e.g. sin(), sqrt() and pow<P, Q>(), apply elementwise to quantities of
619// any size, like the functions of Eigen's arrays and of std::valarray (https://en.cppreference.com/w/cpp/numeric/valarray), which
620// is consistent with the elementwise product of quantities: e.g. exp() of a matrix is not its matrix exponential, and pow<2>() of
621// a matrix squares every element and its unit. They also work with heterogeneous units, e.g. sqrt() of an area and a squared time. The
622// functions of two arguments, atan2(), hypot(), min() and max(), take single quantities.
623
624namespace internal {
625
629template <typename ResultUnitsT_, typename ValuesT_, typename UnitsT_, typename FunctionT_>
630auto applyCwise(const Quantities<ValuesT_, UnitsT_>& quantities, const FunctionT_& function) {
631 using ValueT = typename ValuesT_::ValueT;
632 return Quantities<typename Evaluate<ValuesT_>::type, ResultUnitsT_>(
633 quantities.getValues().cwiseFunction([&function](const ValueT value) { return static_cast<ValueT>(function(value)); }));
634}
635
639template <typename ValueT_>
640struct RequireFloatingPoint {
641 static_assert(std::is_floating_point_v<ValueT_>,
642 "sin, cos, tan, asin, acos, atan, atan2, exp and log need floating-point values: their results are not integers in "
643 "general.");
644 static constexpr bool value = true;
645};
646
650template <typename ValuesT_, typename UnitsT_, typename FunctionT_>
651auto dimensionlessFunction(const Quantities<ValuesT_, UnitsT_>& quantities, const FunctionT_& function) {
652 using ValueT = typename ValuesT_::ValueT;
653 using DimensionlessT =
654 TypeMatrix<Rows<UnitsT_>::value, Cols<UnitsT_>::value, typename AccessUnits<AccessImplementation<ValueT>>::Dimensionless>;
655 static_assert(std::is_same_v<UnitsT_, DimensionlessT>,
656 "sin, cos, tan, asin, acos, atan, exp and log need dimensionless quantities, e.g. angles or ratios of lengths.");
657 static_assert(RequireFloatingPoint<ValueT>::value);
658 return applyCwise<UnitsT_>(quantities, function);
659}
660
665template <typename ResultUnitT_, typename ValuesLhsT_, typename UnitLhsT_, typename ValuesRhsT_, typename UnitRhsT_, typename FunctionT_>
666auto binaryFunction(const Quantity<ValuesLhsT_, UnitLhsT_>& lhs, const Quantity<ValuesRhsT_, UnitRhsT_>& rhs, const FunctionT_& function) {
667 using ValuesT = typename Evaluate<ValuesLhsT_>::type;
668 static_assert(std::is_same_v<UnitLhsT_, UnitRhsT_>, "atan2, hypot, min and max need quantities with the same unit.");
669 static_assert(std::is_same_v<ValuesT, typename Evaluate<ValuesRhsT_>::type>,
670 "atan2, hypot, min and max need quantities with the same value type and value matrix backend.");
671 return Quantity<ValuesT, ResultUnitT_>(static_cast<typename ValuesT::ValueT>(function(lhs.getValue(), rhs.getValue())));
672}
673
675struct Min {
676 template <typename ValueT_>
677 ValueT_ operator()(const ValueT_ lhs, const ValueT_ rhs) const {
678 return std::min(lhs, rhs);
679 }
680};
681struct Max {
682 template <typename ValueT_>
683 ValueT_ operator()(const ValueT_ lhs, const ValueT_ rhs) const {
684 return std::max(lhs, rhs);
685 }
686};
687
688} // namespace internal
689
704template <typename ExponentT_, typename ValuesT_, typename UnitsT_>
705auto pow(const Quantities<ValuesT_, UnitsT_>& quantities) {
706 return internal::applyCwise<typename Power<UnitsT_, ExponentT_>::type>(
707 quantities, [](const auto value) { return internal::realPower<ExponentT_>(value); });
708}
709
710// Convenience definition, so one can write pow<2>() to get pow<Integer<2>>().
711template <int ExponentV_, typename ValuesT_, typename UnitsT_>
712auto pow(const Quantities<ValuesT_, UnitsT_>& quantities) {
713 return pow<Integer<ExponentV_>>(quantities);
714}
715
716// Convenience definition, so one can write pow<1, 2>() to get pow<Rational<1, 2>>().
717template <int ExponentDividendV_, int ExponentDivisorV_, typename ValuesT_, typename UnitsT_>
718auto pow(const Quantities<ValuesT_, UnitsT_>& quantities) {
720}
721
726template <typename ValuesT_, typename UnitsT_>
727auto sqrt(const Quantities<ValuesT_, UnitsT_>& quantities) {
728 return pow<1, 2>(quantities);
729}
730
734template <typename ValuesT_, typename UnitsT_>
735auto cbrt(const Quantities<ValuesT_, UnitsT_>& quantities) {
736 return pow<1, 3>(quantities);
737}
738
742template <typename ValuesT_, typename UnitsT_>
743auto abs(const Quantities<ValuesT_, UnitsT_>& quantities) {
744 using ValueT = typename ValuesT_::ValueT;
745 return internal::applyCwise<UnitsT_>(quantities, [](const ValueT value) {
746 if constexpr (std::is_unsigned_v<ValueT>) {
747 return value;
748 } else {
749 return std::abs(value);
750 }
751 });
752}
753
756template <typename ValuesT_, typename UnitsT_>
758 return internal::dimensionlessFunction(angles, [](const auto value) { return std::sin(value); });
759}
760
762template <typename ValuesT_, typename UnitsT_>
764 return internal::dimensionlessFunction(angles, [](const auto value) { return std::cos(value); });
765}
766
768template <typename ValuesT_, typename UnitsT_>
770 return internal::dimensionlessFunction(angles, [](const auto value) { return std::tan(value); });
771}
772
776template <typename ValuesT_, typename UnitsT_>
777auto asin(const Quantities<ValuesT_, UnitsT_>& quantities) {
778 return internal::dimensionlessFunction(quantities, [](const auto value) { return std::asin(value); });
779}
780
783template <typename ValuesT_, typename UnitsT_>
784auto acos(const Quantities<ValuesT_, UnitsT_>& quantities) {
785 return internal::dimensionlessFunction(quantities, [](const auto value) { return std::acos(value); });
786}
787
790template <typename ValuesT_, typename UnitsT_>
791auto atan(const Quantities<ValuesT_, UnitsT_>& quantities) {
792 return internal::dimensionlessFunction(quantities, [](const auto value) { return std::atan(value); });
793}
794
797template <typename ValuesT_, typename UnitsT_>
798auto exp(const Quantities<ValuesT_, UnitsT_>& quantities) {
799 return internal::dimensionlessFunction(quantities, [](const auto value) { return std::exp(value); });
800}
801
804template <typename ValuesT_, typename UnitsT_>
805auto log(const Quantities<ValuesT_, UnitsT_>& quantities) {
806 return internal::dimensionlessFunction(quantities, [](const auto value) { return std::log(value); });
807}
808
815template <typename ValuesYT_, typename UnitYT_, typename ValuesXT_, typename UnitXT_>
817 using ValueT = typename ValuesYT_::ValueT;
818 static_assert(internal::RequireFloatingPoint<ValueT>::value);
819 return internal::binaryFunction<typename AccessUnits<AccessImplementation<ValueT>>::Angle>(
820 y, x, [](const auto yValue, const auto xValue) { return std::atan2(yValue, xValue); });
821}
822
827template <typename ValuesXT_, typename UnitXT_, typename ValuesYT_, typename UnitYT_>
829 return internal::binaryFunction<UnitXT_>(x, y, [](const auto xValue, const auto yValue) { return internal::realHypot(xValue, yValue); });
830}
831
834template <typename ValuesLhsT_, typename UnitLhsT_, typename ValuesRhsT_, typename UnitRhsT_>
836 return internal::binaryFunction<UnitLhsT_>(lhs, rhs, internal::Min{});
837}
838
840template <typename ValuesT_, typename UnitT_>
842 return internal::binaryFunction<UnitT_>(lhs, rhs, internal::Min{});
843}
844
847template <typename ValuesLhsT_, typename UnitLhsT_, typename ValuesRhsT_, typename UnitRhsT_>
849 return internal::binaryFunction<UnitLhsT_>(lhs, rhs, internal::Max{});
850}
851
852template <typename ValuesT_, typename UnitT_>
854 return internal::binaryFunction<UnitT_>(lhs, rhs, internal::Max{});
855}
856
857} // namespace wuzp
Container to store quantities.
ValueT getValue() const
auto get() const
The quantity with the single index IdV_ of quantities of dimension 0 or 1: the row of a column,...
ValuesT getValues() &&
The values of a temporary are moved out of it, which avoids a copy and does not refer to the destroye...
auto cwiseInverse() const
The elementwise reciprocal, whose units are the reciprocals of the units.
auto determinant() const
Get the determinant of a square matrix, including a 1x1 matrix, whose units must describe a linear ma...
void set(const Quantity< ValueT_, UnitT_ > &quantity)
void set(const int rowId, const int colId, const Quantity< ValueT_, UnitT_ > &quantity)
bool isApprox(const Quantities< ValuesOtherT_, UnitsOtherT_ > &other, const ValueT precision=Precision< ValueT >::value()) const
const ValuesT & getValues() const &
Get the values of the quantities.
auto norm() const
auto get() const
Quantities()=default
void set(const int id, const Quantity< ValueT_, UnitT_ > &quantity)
Sets the quantity with the single index id of quantities of dimension 0 or 1, like set<IdV_>(),...
void setValues(const ValuesOtherT_ &values)
Set the values of the quantities.
auto innerProduct(const Quantities< ValuesOtherT_, UnitsOtherT_ > &other) const
auto block() const
auto transpose() const
auto outerProduct(const Quantities< ValuesOtherT_, UnitsOtherT_ > &other) const
auto pseudoInverse() const
The Moore-Penrose pseudo-inverse A^+ of a matrix A of full rank: A^T (A A^T)^-1 if it has fewer rows ...
auto crossProduct(const Quantities< ValuesOtherT_, UnitsOtherT_ > &other) const
auto get(const int rowId, const int colId) const
void set(const Quantity< ValueT_, UnitT_ > &quantity)
void set(const Quantity< ValueT_, UnitT_ > &quantity)
Sets the quantity with the single index IdV_ of quantities of dimension 0 or 1 (see get<IdV_>()),...
static ThisT zero()
void setValue(ValueOtherT_ value)
Sets the value of a quantity.
Quantities< ValuesT, UnitsT > ThisT
auto squaredNorm() const
auto get(const int id) const
The quantity with the single index id of quantities of dimension 0 or 1, like get<IdV_>(),...
static ThisT random()
auto evaluate() const &
typename ValuesT::ValueT ValueT
auto inverse() const
The matrix inverse of a square matrix, whose units must describe a linear map, M_ij = a_i / b_j: the ...
auto get() const
auto dotProduct(const Quantities< ValuesOtherT_, UnitsOtherT_ > &other) const
Wrapper for the actual implementation of a value container.
Any quantity matrix: a specialization of Quantities, including Quantity.
Definition concepts.hpp:96
Whether constructing a Class from the arguments copies or moves it, i.e. whether they are a single Cl...
Definition helpers.hpp:69
auto operator*(const Quantities< ValuesLhsT_, UnitsLhsT_ > &lhs, const ValueRhsT_ rhs)
auto log(const Quantities< ValuesT_, UnitsT_ > &quantities)
The natural logarithm of dimensionless quantities, elementwise, like std::log (https://en....
auto product(const Quantities< ValuesLhsT_, UnitsLhsT_ > &lhs, const ValueRhsT_ rhs)
Tag< FromImplementationLabel > FromImplementation
Definition traits.hpp:39
auto abs(const Quantities< ValuesT_, UnitsT_ > &quantities)
The absolute value, elementwise, of the same unit, like std::abs (https://en.cppreference....
auto negation(const Quantities< ValuesT_, UnitsT_ > &rhs)
auto min(const Quantity< ValuesLhsT_, UnitLhsT_ > &lhs, const Quantity< ValuesRhsT_, UnitRhsT_ > &rhs)
The smaller of two quantities with the same unit, like std::min (https://en.cppreference....
auto cos(const Quantities< ValuesT_, UnitsT_ > &angles)
The cosine of angles in radians, elementwise, like std::cos (https://en.cppreference....
Quantities< ValueT_, TypeMatrixImpl< MatrixSize< 1, 1 >, std::tuple< UnitT_ > > > Quantity
Container to store a single quantity.
Definition Quantity.hpp:29
auto asin(const Quantities< ValuesT_, UnitsT_ > &quantities)
The arc sine of dimensionless quantities, elementwise, like std::asin (https://en....
auto outerProduct(const Quantities< ValuesLhsT_, UnitsLhsT_ > &lhs, const Quantities< ValuesRhsT_, UnitsRhsT_ > &rhs)
auto operator+(const Quantities< ValuesLhsT_, UnitsLhsT_ > &lhs, const Quantities< ValuesRhsT_, UnitsRhsT_ > &rhs)
bool operator!=(const Quantities< ValuesLhsT_, UnitsLhsT_ > &lhs, const Quantities< ValuesRhsT_, UnitsRhsT_ > &rhs)
auto exp(const Quantities< ValuesT_, UnitsT_ > &quantities)
The exponential function e^x of dimensionless quantities, elementwise, like std::exp (https://en....
std::ostream & operator<<(std::ostream &lhs, const Quantities< ValuesT_, UnitsT_ > &rhs)
constexpr bool isConvertedToValueType
Whether an arithmetic scalar of the type ScalarT_ is converted into the value type ValueT_: if their ...
Definition helpers.hpp:97
auto hypot(const Quantity< ValuesXT_, UnitXT_ > &x, const Quantity< ValuesYT_, UnitYT_ > &y)
The length sqrt(x^2 + y^2) of two quantities with the same unit, e.g. the components of a vector,...
auto operator-(const Quantities< ValuesLhsT_, UnitsLhsT_ > &lhs, const Quantities< ValuesRhsT_, UnitsRhsT_ > &rhs)
auto innerProduct(const Quantities< ValuesLhsT_, UnitsLhsT_ > &lhs, const Quantities< ValuesRhsT_, UnitsRhsT_ > &rhs)
auto cbrt(const Quantities< ValuesT_, UnitsT_ > &quantities)
The cube root, elementwise: pow<1, 3>(), computed with std::cbrt (https://en.cppreference....
auto sqrt(const Quantities< ValuesT_, UnitsT_ > &quantities)
The square root, elementwise: pow<1, 2>(), computed with std::sqrt (https://en.cppreference....
auto acos(const Quantities< ValuesT_, UnitsT_ > &quantities)
The arc cosine of dimensionless quantities, elementwise, like std::acos (https://en....
Tag< FromImplementationArgsLabel > FromImplementationArgs
Definition traits.hpp:41
auto tan(const Quantities< ValuesT_, UnitsT_ > &angles)
The tangent of angles in radians, elementwise, like std::tan (https://en.cppreference....
bool operator==(const Quantities< ValuesLhsT_, UnitsLhsT_ > &lhs, const Quantities< ValuesRhsT_, UnitsRhsT_ > &rhs)
auto atan(const Quantities< ValuesT_, UnitsT_ > &quantities)
The arc tangent of dimensionless quantities, elementwise, like std::atan (https://en....
Tag< FromSimilarLabel > FromSimilar
Definition traits.hpp:37
typename CreateTypeMatrix< RowsV_, ColsV_, Types... >::type TypeMatrix
A matrix storing types at compile-time.
auto atan2(const Quantity< ValuesYT_, UnitYT_ > &y, const Quantity< ValuesXT_, UnitXT_ > &x)
The angle of the point (x, y) from the positive x axis, in [-pi, pi] in radians, of two quantities wi...
auto pow(const Quantities< ValuesT_, UnitsT_ > &quantities)
The quantities raised to a rational power P/Q, elementwise, e.g. pow<Rational<1, 2>>(area) is a lengt...
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)
Rational< ValueV_, 1 > Integer
Definition Integer.hpp:9
auto operator/(const Quantities< ValuesLhsT_, UnitsLhsT_ > &lhs, const ValueRhsT_ rhs)
auto sin(const Quantities< ValuesT_, UnitsT_ > &angles)
The sine of angles in radians, elementwise, like std::sin (https://en.cppreference....
auto max(const Quantity< ValuesLhsT_, UnitLhsT_ > &lhs, const Quantity< ValuesRhsT_, UnitRhsT_ > &rhs)
The larger of two quantities with the same unit, like std::max (https://en.cppreference....
Type wrapper which is used to access traits partially specialized in the implementation header files.
Definition helpers.hpp:114
The units for values of the arithmetic type ValueT_, e.g. double, specialized for AccessImplementatio...
Definition traits.hpp:338
Quantities< typename Block< ValuesT_, BlockRowIdV_, BlockColIdV_, BlockRowsV_, BlockColsV_ >::type, typename Block< UnitsT_, BlockRowIdV_, BlockColIdV_, BlockRowsV_, BlockColsV_ >::type > type
Quantities< typename CrossProduct< ValuesLhsT_, ValuesRhsT_ >::type, typename CrossProduct< UnitsLhsT_, UnitsRhsT_ >::type > type
Quantities< typename CwiseInverse< ValuesT_ >::type, typename CwiseInverse< UnitsT_ >::type > type
The elementwise reciprocal of a matrix, like Eigen's ArrayBase::inverse() or cwiseInverse().
Definition traits.hpp:194
Quantity< ScalarValues< ValuesT_, typename ValuesT_::ValueT >, typename Determinant< UnitsT_ >::type > type
Quantities< typename Difference< ValuesLhsT_, ValuesRhsT_ >::type, typename Difference< UnitsLhsT_, UnitsRhsT_ >::type > type
static constexpr int value
Definition traits.hpp:70
Quantities< typename Division< ValuesLhsT_, ValuesRhsT_ >::type, typename Division< UnitsLhsT_, UnitsRhsT_ >::type > type
Quantities< typename Division< ValuesLhsT_, typename ValueRhsT_::ValueT >::type, typename Division< UnitsLhsT_, UnitRhsT_ >::type > type
Quantities< typename DotProduct< ValuesLhsT_, ValuesRhsT_ >::type, typename DotProduct< UnitsLhsT_, UnitsRhsT_ >::type > type
Quantity< ScalarValues< ValuesT_, typename ValuesT_::ValueT >, typename Element< UnitsT_, ElementRowIdV_, ElementColIdV_ >::type > type
Quantities< typename Evaluate< ValuesT_ >::type, UnitsT_ > type
std::decay_t< T > type
Definition traits.hpp:224
typename IfThenElse< sizeof...(Ts)==1, typename IfThenElse< AnyQuantities< first >, FromSimilar, typename IfThenElse<(IsQuantity< std::decay_t< Ts > >::value &&...), FromImplementation, FromImplementationArgs >::type >::type, typename IfThenElse<(IsQuantity< std::decay_t< Ts > >::value &&...), FromImplementation, FromImplementationArgs >::type >::type type
Quantity< ScalarValues< ValuesT_, typename ValuesT_::ValueT >, typename GetType< UnitsT_ >::type > type
Trait to select one type or another depending on a boolean. Note that both types must be defined even...
Definition helpers.hpp:51
Quantities< typename InnerProduct< ValuesLhsT_, ValuesRhsT_ >::type, typename InnerProduct< UnitsLhsT_, UnitsRhsT_ >::type > type
Quantities< typename Inverse< ValuesT_ >::type, UnitsT > type
typename Inverse< UnitsT_ >::type UnitsT
The inverse: the matrix inverse of a matrix, like Eigen's MatrixBase::inverse(), and the reciprocal o...
Definition traits.hpp:188
static constexpr bool value
Definition helpers.hpp:101
Whether T is a specialization of Template.
Definition helpers.hpp:74
Whether converting a From into a To passes the checks of the converting constructor of To,...
Definition traits.hpp:265
Quantities< typename Negation< ValuesT_ >::type, UnitsT_ > type
The negation is performed elementwise.
Definition traits.hpp:101
Quantities< typename OuterProduct< ValuesLhsT_, ValuesRhsT_ >::type, typename OuterProduct< UnitsLhsT_, UnitsRhsT_ >::type > type
Quantities< ValuesT_, typename Power< UnitsT_, ExponentT_ >::type > type
static constexpr ValueT_ value()
Definition traits.hpp:356
Quantities< typename Product< ValuesLhsT_, ValuesRhsT_ >::type, typename Product< UnitsLhsT_, UnitsRhsT_ >::type > type
Quantities< typename Sum< ValuesLhsT_, ValuesRhsT_ >::type, typename Sum< UnitsLhsT_, UnitsRhsT_ >::type > type
Quantities< typename Transpose< ValuesT_ >::type, typename Transpose< UnitsT_ >::type > type