wuzp
Wrong Unit? Zero Points! Compile-time checks for units, coordinate systems and points vs. vectors.
Loading...
Searching...
No Matches
MatrixSize.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <algorithm>
4#include <type_traits>
5#include <utility>
6
8
9namespace wuzp {
10
11template <int... SizesV_>
12struct MatrixSize {
13 using SizesT = std::integer_sequence<int, SizesV_...>;
14 static constexpr int NumElementsV = std::integral_constant<int, (SizesV_ * ...)>::value;
15};
16
17template <int RowsV_, int ColsV_>
18struct Rows<MatrixSize<RowsV_, ColsV_>> {
19 static constexpr int value = RowsV_;
20};
21
22template <int RowsV_, int ColsV_>
23struct Cols<MatrixSize<RowsV_, ColsV_>> {
24 static constexpr int value = ColsV_;
25};
26
27template <int RowsLhsV_, int ColsLhsV_, int RowsRhsV_, int ColsRhsV_>
28struct Sum<MatrixSize<RowsLhsV_, ColsLhsV_>, MatrixSize<RowsRhsV_, ColsRhsV_>> {
29 static_assert(RowsLhsV_ == RowsRhsV_, "Number of rows do not match.");
30 static_assert(ColsLhsV_ == ColsRhsV_, "Number of cols do not match.");
32};
33
34template <int RowsLhsV_, int ColsLhsV_, int RowsRhsV_, int ColsRhsV_>
35struct Difference<MatrixSize<RowsLhsV_, ColsLhsV_>, MatrixSize<RowsRhsV_, ColsRhsV_>> {
36 static_assert(RowsLhsV_ == RowsRhsV_, "Number of rows do not match.");
37 static_assert(ColsLhsV_ == ColsRhsV_, "Number of cols do not match.");
39};
40
41// Product of 1x1 matrix and 1x1 matrix.
42template <>
43struct Product<MatrixSize<1, 1>, MatrixSize<1, 1>> {
45};
46// Product of matrix and 1x1 matrix.
47template <int RowsLhsV_, int ColsLhsV_>
48struct Product<MatrixSize<RowsLhsV_, ColsLhsV_>, MatrixSize<1, 1>> {
50};
51// Product of 1x1 matrix and matrix.
52template <int RowsRhsV_, int ColsRhsV_>
53struct Product<MatrixSize<1, 1>, MatrixSize<RowsRhsV_, ColsRhsV_>> {
54 using type = typename Product<MatrixSize<RowsRhsV_, ColsRhsV_>, MatrixSize<1, 1>>::type;
55};
56// Product of matrix and matrix.
57template <int RowsLhsV_, int ColsLhsV_, int RowsRhsV_, int ColsRhsV_>
58struct Product<MatrixSize<RowsLhsV_, ColsLhsV_>, MatrixSize<RowsRhsV_, ColsRhsV_>> {
59 static_assert(RowsLhsV_ == RowsRhsV_, "Number of rows do not match.");
60 static_assert(ColsLhsV_ == ColsRhsV_, "Number of cols do not match.");
62};
63
64template <int RowsLhsV_, int ColsLhsV_, int RowsRhsV_, int ColsRhsV_>
65struct InnerProduct<MatrixSize<RowsLhsV_, ColsLhsV_>, MatrixSize<RowsRhsV_, ColsRhsV_>> {
66 static_assert(ColsLhsV_ == RowsRhsV_, "Number of LHS cols does not match number of RHS rows.");
68};
69
70template <int RowsLhsV_, int ColsLhsV_, int RowsRhsV_, int ColsRhsV_>
71struct OuterProduct<MatrixSize<RowsLhsV_, ColsLhsV_>, MatrixSize<RowsRhsV_, ColsRhsV_>> {
72 static_assert(std::min(RowsLhsV_, RowsRhsV_) <= 1,
73 "Hypermatrices are not supported. At least one of the matrices must be a scalar or a vector.");
74 static_assert(std::min(ColsLhsV_, ColsRhsV_) <= 1,
75 "Hypermatrices are not supported. At least one of the matrices must be a scalar or a vector.");
76 using type = MatrixSize<RowsLhsV_ + RowsRhsV_ - 1, ColsLhsV_ + ColsRhsV_ - 1>;
77};
78
79template <int RowsLhsV_, int ColsLhsV_, int RowsRhsV_, int ColsRhsV_>
80struct DotProduct<MatrixSize<RowsLhsV_, ColsLhsV_>, MatrixSize<RowsRhsV_, ColsRhsV_>> {
81 static_assert(RowsLhsV_ == RowsRhsV_, "Matrix sizes do not match.");
82 static_assert(ColsLhsV_ == 1, "Dot product is only defined for vectors.");
83 static_assert(ColsRhsV_ == 1, "Dot product is only defined for vectors.");
85};
86
87template <int RowsLhsV_, int ColsLhsV_, int RowsRhsV_, int ColsRhsV_>
88struct CrossProduct<MatrixSize<RowsLhsV_, ColsLhsV_>, MatrixSize<RowsRhsV_, ColsRhsV_>> {
89 static_assert(RowsLhsV_ == 3 && RowsRhsV_ == 3 && ColsLhsV_ == 1 && ColsRhsV_ == 1,
90 "Cross product is only defined for three-dimensional vectors.");
92};
93
94template <int RowsLhsV_, int ColsLhsV_, int RowsRhsV_, int ColsRhsV_>
95struct Division<MatrixSize<RowsLhsV_, ColsLhsV_>, MatrixSize<RowsRhsV_, ColsRhsV_>> {
96 static_assert(RowsLhsV_ == RowsRhsV_, "Number of rows do not match.");
97 static_assert(ColsLhsV_ == ColsRhsV_, "Number of cols do not match.");
99};
100template <int RowsLhsV_, int ColsLhsV_>
101struct Division<MatrixSize<RowsLhsV_, ColsLhsV_>, MatrixSize<1, 1>> {
103};
104
105template <int RowsV_, int ColsV_>
106struct Transpose<MatrixSize<RowsV_, ColsV_>> {
108};
109
111template <int RowsV_, int ColsV_>
112struct Inverse<MatrixSize<RowsV_, ColsV_>> {
113 static_assert(RowsV_ == ColsV_, "Matrix is not square.");
115};
116
117template <int RowsV_, int ColsV_, int BlockRowIdV_, int BlockColIdV_, int BlockRowsV_, int BlockColsV_>
118struct Block<MatrixSize<RowsV_, ColsV_>, BlockRowIdV_, BlockColIdV_, BlockRowsV_, BlockColsV_> {
119 static_assert(BlockRowIdV_ >= 0 && BlockColIdV_ >= 0, "Block top left corner is out of range.");
120 static_assert(BlockRowIdV_ + BlockRowsV_ <= RowsV_ && BlockColIdV_ + BlockColsV_ <= ColsV_, "Block bottom right corner is out of range.");
122};
123
124namespace internal {
125
129template <typename MatrixSizeT_, int RowIdV_, int ColIdV_>
130struct RequireElementInRange {
131 static_assert(RowIdV_ >= 0 && RowIdV_ < Rows<MatrixSizeT_>::value, "Element row id is out of range.");
132 static_assert(ColIdV_ >= 0 && ColIdV_ < Cols<MatrixSizeT_>::value, "Element columns id is out of range.");
133 static constexpr bool value = true;
134};
135
143template <typename ObjectT_>
144constexpr int vectorElementRowId(const int id) {
145 return Cols<ObjectT_>::value == 1 ? id : 0;
146}
147
149template <typename ObjectT_>
150constexpr int vectorElementColId(const int id) {
151 return Cols<ObjectT_>::value == 1 ? 0 : id;
152}
153
154} // namespace internal
155
156} // namespace wuzp
The inverse: the matrix inverse of a matrix, like Eigen's MatrixBase::inverse(), and the reciprocal o...
Definition traits.hpp:188
std::integer_sequence< int, SizesV_... > SizesT
MatrixSize< RowsLhsV_+RowsRhsV_ - 1, ColsLhsV_+ColsRhsV_ - 1 > type
typename Product< MatrixSize< RowsRhsV_, ColsRhsV_ >, MatrixSize< 1, 1 > >::type type