wuzp
Wrong Unit? Zero Points! Compile-time checks for units, coordinate systems and points vs. vectors.
Toggle main menu visibility
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
7
#include "
wuzp/core/traits.hpp
"
8
9
namespace
wuzp
{
10
11
template
<
int
... SizesV_>
12
struct
MatrixSize
{
13
using
SizesT
= std::integer_sequence<int, SizesV_...>;
14
static
constexpr
int
NumElementsV
= std::integral_constant<int, (SizesV_ * ...)>::value;
15
};
16
17
template
<
int
RowsV_,
int
ColsV_>
18
struct
Rows
<
MatrixSize
<RowsV_, ColsV_>> {
19
static
constexpr
int
value
= RowsV_;
20
};
21
22
template
<
int
RowsV_,
int
ColsV_>
23
struct
Cols
<
MatrixSize
<RowsV_, ColsV_>> {
24
static
constexpr
int
value
= ColsV_;
25
};
26
27
template
<
int
RowsLhsV_,
int
ColsLhsV_,
int
RowsRhsV_,
int
ColsRhsV_>
28
struct
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."
);
31
using
type
=
MatrixSize<RowsLhsV_, ColsLhsV_>
;
32
};
33
34
template
<
int
RowsLhsV_,
int
ColsLhsV_,
int
RowsRhsV_,
int
ColsRhsV_>
35
struct
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."
);
38
using
type
=
MatrixSize<RowsLhsV_, ColsLhsV_>
;
39
};
40
41
// Product of 1x1 matrix and 1x1 matrix.
42
template
<>
43
struct
Product<
MatrixSize
<1, 1>,
MatrixSize
<1, 1>> {
44
using
type
=
MatrixSize<1, 1>
;
45
};
46
// Product of matrix and 1x1 matrix.
47
template
<
int
RowsLhsV_,
int
ColsLhsV_>
48
struct
Product<
MatrixSize
<RowsLhsV_, ColsLhsV_>,
MatrixSize
<1, 1>> {
49
using
type
=
MatrixSize<RowsLhsV_, ColsLhsV_>
;
50
};
51
// Product of 1x1 matrix and matrix.
52
template
<
int
RowsRhsV_,
int
ColsRhsV_>
53
struct
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.
57
template
<
int
RowsLhsV_,
int
ColsLhsV_,
int
RowsRhsV_,
int
ColsRhsV_>
58
struct
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."
);
61
using
type
=
MatrixSize<RowsLhsV_, ColsLhsV_>
;
62
};
63
64
template
<
int
RowsLhsV_,
int
ColsLhsV_,
int
RowsRhsV_,
int
ColsRhsV_>
65
struct
InnerProduct<
MatrixSize
<RowsLhsV_, ColsLhsV_>,
MatrixSize
<RowsRhsV_, ColsRhsV_>> {
66
static_assert
(ColsLhsV_ == RowsRhsV_,
"Number of LHS cols does not match number of RHS rows."
);
67
using
type
=
MatrixSize<RowsLhsV_, ColsRhsV_>
;
68
};
69
70
template
<
int
RowsLhsV_,
int
ColsLhsV_,
int
RowsRhsV_,
int
ColsRhsV_>
71
struct
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
79
template
<
int
RowsLhsV_,
int
ColsLhsV_,
int
RowsRhsV_,
int
ColsRhsV_>
80
struct
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."
);
84
using
type
=
MatrixSize<1, 1>
;
85
};
86
87
template
<
int
RowsLhsV_,
int
ColsLhsV_,
int
RowsRhsV_,
int
ColsRhsV_>
88
struct
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."
);
91
using
type
=
MatrixSize<3, 1>
;
92
};
93
94
template
<
int
RowsLhsV_,
int
ColsLhsV_,
int
RowsRhsV_,
int
ColsRhsV_>
95
struct
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."
);
98
using
type
=
MatrixSize<RowsLhsV_, ColsLhsV_>
;
99
};
100
template
<
int
RowsLhsV_,
int
ColsLhsV_>
101
struct
Division<
MatrixSize
<RowsLhsV_, ColsLhsV_>,
MatrixSize
<1, 1>> {
102
using
type
=
MatrixSize<RowsLhsV_, ColsLhsV_>
;
103
};
104
105
template
<
int
RowsV_,
int
ColsV_>
106
struct
Transpose
<
MatrixSize
<RowsV_, ColsV_>> {
107
using
type
=
MatrixSize<ColsV_, RowsV_>
;
108
};
109
111
template
<
int
RowsV_,
int
ColsV_>
112
struct
Inverse
<
MatrixSize
<RowsV_, ColsV_>> {
113
static_assert
(RowsV_ == ColsV_,
"Matrix is not square."
);
114
using
type
=
MatrixSize<RowsV_, ColsV_>
;
115
};
116
117
template
<
int
RowsV_,
int
ColsV_,
int
BlockRowIdV_,
int
BlockColIdV_,
int
BlockRowsV_,
int
BlockColsV_>
118
struct
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."
);
121
using
type
=
MatrixSize<BlockRowsV_, BlockColsV_>
;
122
};
123
124
namespace
internal {
125
129
template
<
typename
MatrixSizeT_,
int
RowIdV_,
int
ColIdV_>
130
struct
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
143
template
<
typename
ObjectT_>
144
constexpr
int
vectorElementRowId(
const
int
id
) {
145
return
Cols<ObjectT_>::value == 1 ? id : 0;
146
}
147
149
template
<
typename
ObjectT_>
150
constexpr
int
vectorElementColId(
const
int
id
) {
151
return
Cols<ObjectT_>::value == 1 ? 0 : id;
152
}
153
154
}
// namespace internal
155
156
}
// namespace wuzp
traits.hpp
wuzp
Definition
Absolute.hpp:15
wuzp::Block< MatrixSize< RowsV_, ColsV_ >, BlockRowIdV_, BlockColIdV_, BlockRowsV_, BlockColsV_ >::type
MatrixSize< BlockRowsV_, BlockColsV_ > type
Definition
MatrixSize.hpp:121
wuzp::Block
Definition
traits.hpp:166
wuzp::Cols< MatrixSize< RowsV_, ColsV_ > >::value
static constexpr int value
Definition
MatrixSize.hpp:24
wuzp::Cols
Definition
traits.hpp:64
wuzp::CrossProduct< MatrixSize< RowsLhsV_, ColsLhsV_ >, MatrixSize< RowsRhsV_, ColsRhsV_ > >::type
MatrixSize< 3, 1 > type
Definition
MatrixSize.hpp:91
wuzp::CrossProduct
Definition
traits.hpp:133
wuzp::Difference< MatrixSize< RowsLhsV_, ColsLhsV_ >, MatrixSize< RowsRhsV_, ColsRhsV_ > >::type
MatrixSize< RowsLhsV_, ColsLhsV_ > type
Definition
MatrixSize.hpp:38
wuzp::Division< MatrixSize< RowsLhsV_, ColsLhsV_ >, MatrixSize< 1, 1 > >::type
MatrixSize< RowsLhsV_, ColsLhsV_ > type
Definition
MatrixSize.hpp:102
wuzp::Division< MatrixSize< RowsLhsV_, ColsLhsV_ >, MatrixSize< RowsRhsV_, ColsRhsV_ > >::type
MatrixSize< RowsLhsV_, ColsLhsV_ > type
Definition
MatrixSize.hpp:98
wuzp::DotProduct< MatrixSize< RowsLhsV_, ColsLhsV_ >, MatrixSize< RowsRhsV_, ColsRhsV_ > >::type
MatrixSize< 1, 1 > type
Definition
MatrixSize.hpp:84
wuzp::DotProduct
Definition
traits.hpp:126
wuzp::InnerProduct< MatrixSize< RowsLhsV_, ColsLhsV_ >, MatrixSize< RowsRhsV_, ColsRhsV_ > >::type
MatrixSize< RowsLhsV_, ColsRhsV_ > type
Definition
MatrixSize.hpp:67
wuzp::Inverse< MatrixSize< RowsV_, ColsV_ > >::type
MatrixSize< RowsV_, ColsV_ > type
Definition
MatrixSize.hpp:114
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::MatrixSize::SizesT
std::integer_sequence< int, SizesV_... > SizesT
Definition
MatrixSize.hpp:13
wuzp::MatrixSize< RowsLhsV_, ColsLhsV_ >::NumElementsV
static constexpr int NumElementsV
Definition
MatrixSize.hpp:14
wuzp::OuterProduct< MatrixSize< RowsLhsV_, ColsLhsV_ >, MatrixSize< RowsRhsV_, ColsRhsV_ > >::type
MatrixSize< RowsLhsV_+RowsRhsV_ - 1, ColsLhsV_+ColsRhsV_ - 1 > type
Definition
MatrixSize.hpp:76
wuzp::Product< MatrixSize< 1, 1 >, MatrixSize< 1, 1 > >::type
MatrixSize< 1, 1 > type
Definition
MatrixSize.hpp:44
wuzp::Product< MatrixSize< 1, 1 >, MatrixSize< RowsRhsV_, ColsRhsV_ > >::type
typename Product< MatrixSize< RowsRhsV_, ColsRhsV_ >, MatrixSize< 1, 1 > >::type type
Definition
MatrixSize.hpp:54
wuzp::Product< MatrixSize< RowsLhsV_, ColsLhsV_ >, MatrixSize< 1, 1 > >::type
MatrixSize< RowsLhsV_, ColsLhsV_ > type
Definition
MatrixSize.hpp:49
wuzp::Product< MatrixSize< RowsLhsV_, ColsLhsV_ >, MatrixSize< RowsRhsV_, ColsRhsV_ > >::type
MatrixSize< RowsLhsV_, ColsLhsV_ > type
Definition
MatrixSize.hpp:61
wuzp::Rows< MatrixSize< RowsV_, ColsV_ > >::value
static constexpr int value
Definition
MatrixSize.hpp:19
wuzp::Rows
Definition
traits.hpp:59
wuzp::Sum< MatrixSize< RowsLhsV_, ColsLhsV_ >, MatrixSize< RowsRhsV_, ColsRhsV_ > >::type
MatrixSize< RowsLhsV_, ColsLhsV_ > type
Definition
MatrixSize.hpp:31
wuzp::Transpose< MatrixSize< RowsV_, ColsV_ > >::type
MatrixSize< ColsV_, RowsV_ > type
Definition
MatrixSize.hpp:107
wuzp::Transpose
Definition
traits.hpp:155
wuzp
include
wuzp
core
MatrixSize.hpp
Generated by
1.18.0