wuzp
Wrong Unit? Zero Points! Compile-time checks for units, coordinate systems and points vs. vectors.
Loading...
Searching...
No Matches
Units.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <cstddef>
4#include <tuple>
5#include <type_traits>
6#include <utility>
7
10#include "wuzp/core/Unit.hpp"
11#include "wuzp/core/traits.hpp"
12
13// The units of the matrix inverse and of the determinant. Both are only defined for matrices whose units describe a linear map,
14// M_ij = a_i / b_j for some units a_i and b_j, i.e. whose unit matrix is the outer product of a column of units and a row of
15// inverse units (Hart, Multidimensional Analysis, 1995, sections 3.1 and 3.3, https://doi.org/10.1007/978-1-4612-4208-6).
16// E.g. a mass matrix maps accelerations to forces and torques, so its units are
17// [kg, kg m; kg m, kg m^2] = [N, N m]^T [1 / (m / s^2), 1 / (1 / s^2)].
18
19namespace wuzp {
20
21namespace internal {
22
23template <typename UnitsT_, typename IndexSequenceT_>
24struct UnitsDescribeLinearMapImpl;
25template <typename UnitsT_, std::size_t... Is>
26struct UnitsDescribeLinearMapImpl<UnitsT_, std::index_sequence<Is...>> {
27 private:
28 static constexpr int ColsV = Cols<UnitsT_>::value;
29 template <int RowIdV_, int ColIdV_>
30 using ElementT = typename Element<UnitsT_, RowIdV_, ColIdV_>::type;
31 template <int RowIdV_, int ColIdV_>
32 static constexpr bool IsLinearV = std::is_same_v<typename Product<ElementT<RowIdV_, ColIdV_>, ElementT<0, 0>>::type,
33 typename Product<ElementT<RowIdV_, 0>, ElementT<0, ColIdV_>>::type>;
34
35 public:
36 static constexpr bool value = (IsLinearV<static_cast<int>(Is) / ColsV, static_cast<int>(Is) % ColsV> && ...);
37};
38
39} // namespace internal
40
47template <typename UnitsT_>
49 static constexpr bool value =
50 internal::UnitsDescribeLinearMapImpl<UnitsT_,
51 std::make_index_sequence<std::size_t{Rows<UnitsT_>::value * Cols<UnitsT_>::value}>>::value;
52};
53
54namespace internal {
55
59template <typename UnitsT_>
60struct RequireSquareLinearMap {
61 static_assert(Rows<UnitsT_>::value == Cols<UnitsT_>::value, "Matrix is not square.");
62 static_assert(Rows<UnitsT_>::value != Cols<UnitsT_>::value || UnitsDescribeLinearMap<UnitsT_>::value,
63 "The units of the matrix do not describe a linear map (M_ij = a_i / b_j).");
64 static constexpr bool value = true;
65};
66
67} // namespace internal
68
79template <int RowsV_, int ColsV_, typename... UnitImplementationsT_>
80struct Inverse<TypeMatrixImpl<MatrixSize<RowsV_, ColsV_>, std::tuple<Unit<UnitImplementationsT_>...>>> {
81 private:
82 using UnitsT = TypeMatrixImpl<MatrixSize<RowsV_, ColsV_>, std::tuple<Unit<UnitImplementationsT_>...>>;
83
84 public:
85 static_assert(internal::RequireSquareLinearMap<UnitsT>::value);
87};
88
89template <typename Matrix, typename IndexSequence>
91template <typename Matrix, size_t... Is>
92struct DiagonalProductImpl<Matrix, std::index_sequence<Is...>> {
93 using type = typename Product<typename Element<Matrix, static_cast<int>(Is), static_cast<int>(Is)>::type...>::type;
94};
95
103template <int RowsV_, int ColsV_, typename... UnitImplementationsT_>
104struct Determinant<TypeMatrixImpl<MatrixSize<RowsV_, ColsV_>, std::tuple<Unit<UnitImplementationsT_>...>>> {
105 private:
106 using UnitsT = TypeMatrixImpl<MatrixSize<RowsV_, ColsV_>, std::tuple<Unit<UnitImplementationsT_>...>>;
107
108 public:
109 static_assert(internal::RequireSquareLinearMap<UnitsT>::value);
110 using type = typename DiagonalProductImpl<UnitsT, std::make_index_sequence<std::size_t{RowsV_}>>::type;
111};
112
113} // namespace wuzp
typename DiagonalProductImpl< UnitsT, std::make_index_sequence< std::size_t{RowsV_}> >::type type
Definition Units.hpp:110
typename Product< typename Element< Matrix, static_cast< int >(Is), static_cast< int >(Is)>::type... >::type type
Definition Units.hpp:93
The inverse: the matrix inverse of a matrix, like Eigen's MatrixBase::inverse(), and the reciprocal o...
Definition traits.hpp:188
A matrix of the types in the std::tuple Types, row by row, of the size MatrixSizeT_,...
Whether the units of a matrix describe a linear map, M_ij = a_i / b_j (Hart, section 3....
Definition Units.hpp:48
static constexpr bool value
Definition Units.hpp:49