wuzp
Wrong Unit? Zero Points! Compile-time checks for units, coordinate systems and points vs. vectors.
Toggle main menu visibility
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
8
#include "
wuzp/core/MatrixSize.hpp
"
9
#include "
wuzp/core/TypeMatrix.hpp
"
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
19
namespace
wuzp
{
20
21
namespace
internal {
22
23
template
<
typename
UnitsT_,
typename
IndexSequenceT_>
24
struct
UnitsDescribeLinearMapImpl;
25
template
<
typename
UnitsT_, std::size_t... Is>
26
struct
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
47
template
<
typename
UnitsT_>
48
struct
UnitsDescribeLinearMap
{
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
54
namespace
internal {
55
59
template
<
typename
UnitsT_>
60
struct
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
79
template
<
int
RowsV_,
int
ColsV_,
typename
... UnitImplementationsT_>
80
struct
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);
86
using
type
=
typename
Transpose<typename CwiseInverse<UnitsT>::type
>
::type
;
87
};
88
89
template
<
typename
Matrix,
typename
IndexSequence>
90
struct
DiagonalProductImpl
;
91
template
<
typename
Matrix,
size_t
... Is>
92
struct
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
103
template
<
int
RowsV_,
int
ColsV_,
typename
... UnitImplementationsT_>
104
struct
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
MatrixSize.hpp
TypeMatrix.hpp
traits.hpp
Unit.hpp
wuzp
Definition
Absolute.hpp:15
wuzp::Cols
Definition
traits.hpp:64
wuzp::Determinant< TypeMatrixImpl< MatrixSize< RowsV_, ColsV_ >, std::tuple< Unit< UnitImplementationsT_ >... > > >::type
typename DiagonalProductImpl< UnitsT, std::make_index_sequence< std::size_t{RowsV_}> >::type type
Definition
Units.hpp:110
wuzp::Determinant
Definition
traits.hpp:182
wuzp::DiagonalProductImpl< Matrix, std::index_sequence< Is... > >::type
typename Product< typename Element< Matrix, static_cast< int >(Is), static_cast< int >(Is)>::type... >::type type
Definition
Units.hpp:93
wuzp::DiagonalProductImpl
Definition
Units.hpp:90
wuzp::Inverse< TypeMatrixImpl< MatrixSize< RowsV_, ColsV_ >, std::tuple< Unit< UnitImplementationsT_ >... > > >::type
typename Transpose< typename CwiseInverse< UnitsT >::type >::type type
Definition
Units.hpp:86
wuzp::Inverse
The inverse: the matrix inverse of a matrix, like Eigen's MatrixBase::inverse(), and the reciprocal o...
Definition
traits.hpp:188
wuzp::MatrixSize
Definition
MatrixSize.hpp:12
wuzp::Rows
Definition
traits.hpp:59
wuzp::Transpose
Definition
traits.hpp:155
wuzp::TypeMatrixImpl
A matrix of the types in the std::tuple Types, row by row, of the size MatrixSizeT_,...
Definition
TypeMatrix.hpp:110
wuzp::UnitsDescribeLinearMap
Whether the units of a matrix describe a linear map, M_ij = a_i / b_j (Hart, section 3....
Definition
Units.hpp:48
wuzp::UnitsDescribeLinearMap::value
static constexpr bool value
Definition
Units.hpp:49
wuzp
include
wuzp
core
Units.hpp
Generated by
1.18.0