wuzp
Wrong Unit? Zero Points! Compile-time checks for units, coordinate systems and points vs. vectors.
Loading...
Searching...
No Matches
ValueMatrix.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <tuple>
4#include <type_traits>
5#include <utility>
6
7#include <Eigen/Core>
8#include <Eigen/Geometry>
9#include <Eigen/LU>
10
16#include "wuzp/core/helpers.hpp"
17#include "wuzp/core/internal/operator_traits.hpp"
18#include "wuzp/core/traits.hpp"
19
20namespace wuzp_eigen {
21
22template <typename ValueT_, int RowsV_, int ColsV_>
23using ValueMatrix = wuzp::ValuesWrapper<wuzp::MatrixSize<RowsV_, ColsV_>, ValueT_, Eigen::Array<ValueT_, RowsV_, ColsV_>>;
24
25template <int RowsV_, int ColsV_>
27template <int RowsV_, int ColsV_>
29template <int RowsV_, int ColsV_>
31
32template <typename ValueT_, int RowsV_>
34
35template <int RowsV_>
37template <int RowsV_>
39template <int RowsV_>
41
42template <typename ValueT_>
44
48
50template <typename UnitT_>
52template <typename UnitT_>
54template <typename UnitT_>
56
60template <typename T>
61concept EigenArray = std::is_base_of_v<Eigen::ArrayBase<T>, T>;
62
63} // namespace wuzp_eigen
64
65namespace wuzp {
66
67template <wuzp_eigen::EigenArray ImplementationT_, int... SizesV_>
68struct ReserveMemory<ImplementationT_, MatrixSize<SizesV_...>> {
69 static void run(ImplementationT_& /*impl*/) {}
70};
71
72template <wuzp_eigen::EigenArray ImplementationT_, typename Arg0T_, typename... ArgsT_>
73struct Initialize<ImplementationT_, std::tuple<Arg0T_, ArgsT_...>> {
74 static void run(ImplementationT_& impl, Arg0T_&& arg0, ArgsT_&&... args) {
75 applyCommas((impl << std::forward<Arg0T_>(arg0)), std::forward<ArgsT_>(args)...);
76 }
77};
78
79// The evaluated arrays have Eigen's default storage order, whatever the storage order of the expression, e.g. of a transpose,
80// so that results have one type: Eigen::Array<ValueT_, RowsV_, ColsV_>, which stores matrices column by column and row vectors
81// row by row (see https://eigen.tuxfamily.org/dox/group__TopicStorageOrders.html).
82template <wuzp_eigen::EigenArray ImplementationT_, typename ValueT_, int RowsV_, int ColsV_>
83struct Rebind<AccessImplementation<ImplementationT_>, ValueT_, RowsV_, ColsV_> {
84 using type = Eigen::Array<ValueT_, RowsV_, ColsV_>;
85};
86
87// Eigen accesses elements with the parenthesis operator (see "Accessing values inside an Array" in
88// https://eigen.tuxfamily.org/dox/group__TutorialArrayClass.html).
89template <wuzp_eigen::EigenArray ImplementationT_>
90struct Coefficient<AccessImplementation<ImplementationT_>> {
91 static auto get(const ImplementationT_& impl, const int row, const int col) { return impl(row, col); }
92 static void set(ImplementationT_& impl, const int row, const int col, const typename ImplementationT_::Scalar value) {
93 impl(row, col) = value;
94 }
95};
96
97template <wuzp_eigen::EigenArray ImplementationLhsT_, wuzp_eigen::EigenArray ImplementationRhsT_>
98struct Comparison<AccessImplementation<ImplementationLhsT_>, AccessImplementation<ImplementationRhsT_>> {
99 static bool equal(const ImplementationLhsT_& lhs, const ImplementationRhsT_& rhs) { return lhs.matrix() == rhs.matrix(); }
100 static bool approxEqual(const ImplementationLhsT_& lhs, const ImplementationRhsT_& rhs,
101 const typename ImplementationLhsT_::Scalar tolerance) {
102 return lhs.matrix().isApprox(rhs.matrix(), tolerance);
103 }
104};
105
106template <wuzp_eigen::EigenArray ImplementationT_>
107struct Transpose<AccessImplementation<ImplementationT_>> {
108 static auto value(const ImplementationT_& impl) { return impl.transpose(); }
109 // Use decltype/declval to allow for lazy evaluation.
110 using type = decltype(value(std::declval<const ImplementationT_>()));
111};
112
113template <wuzp_eigen::EigenArray ImplementationLhsT_, wuzp_eigen::EigenArray ImplementationRhsT_>
114struct Sum<AccessImplementation<ImplementationLhsT_>, AccessImplementation<ImplementationRhsT_>>
115 : internal::OperatorSum<ImplementationLhsT_, ImplementationRhsT_> {};
116
117template <wuzp_eigen::EigenArray ImplementationLhsT_, wuzp_eigen::EigenArray ImplementationRhsT_>
118struct Difference<AccessImplementation<ImplementationLhsT_>, AccessImplementation<ImplementationRhsT_>>
119 : internal::OperatorDifference<ImplementationLhsT_, ImplementationRhsT_> {};
120
121template <wuzp_eigen::EigenArray ImplementationT_>
122struct Negation<AccessImplementation<ImplementationT_>> : internal::OperatorNegation<ImplementationT_> {};
123
124template <wuzp_eigen::EigenArray ImplementationLhsT_, ArithmeticValue ValueRhsT_>
125struct Product<AccessImplementation<ImplementationLhsT_>, ValueRhsT_> : internal::OperatorProduct<ImplementationLhsT_, ValueRhsT_> {};
126template <wuzp_eigen::EigenArray ImplementationLhsT_, wuzp_eigen::EigenArray ImplementationRhsT_>
127struct Product<AccessImplementation<ImplementationLhsT_>, AccessImplementation<ImplementationRhsT_>>
128 : internal::OperatorProduct<ImplementationLhsT_, ImplementationRhsT_> {};
129
130template <wuzp_eigen::EigenArray ImplementationLhsT_, wuzp_eigen::EigenArray ImplementationRhsT_>
131struct InnerProduct<AccessImplementation<ImplementationLhsT_>, AccessImplementation<ImplementationRhsT_>> {
132 static auto value(const ImplementationLhsT_& implLhs, const ImplementationRhsT_& implRhs) {
133 return (implLhs.matrix() * implRhs.matrix()).array();
134 }
135 // Use decltype/declval to allow for lazy evaluation.
136 using type = decltype(value(std::declval<const ImplementationLhsT_>(), std::declval<const ImplementationRhsT_>()));
137};
138
139template <wuzp_eigen::EigenArray ImplementationLhsT_, wuzp_eigen::EigenArray ImplementationRhsT_>
140struct OuterProduct<AccessImplementation<ImplementationLhsT_>, AccessImplementation<ImplementationRhsT_>> {
141 static auto value(const ImplementationLhsT_& implLhs, const ImplementationRhsT_& implRhs) {
142 if constexpr (ImplementationLhsT_::RowsAtCompileTime == 1 && ImplementationLhsT_::ColsAtCompileTime == 1) {
143 return implLhs(0, 0) * implRhs;
144 } else if constexpr (ImplementationRhsT_::RowsAtCompileTime == 1 && ImplementationRhsT_::ColsAtCompileTime == 1) {
145 return implLhs * implRhs(0, 0);
146 } else if constexpr (ImplementationLhsT_::RowsAtCompileTime == 1) {
147 return (implRhs.matrix() * implLhs.matrix()).array();
148 } else {
149 return (implLhs.matrix() * implRhs.matrix()).array();
150 }
151 }
152 // Use decltype/declval to allow for lazy evaluation.
153 using type = decltype(value(std::declval<const ImplementationLhsT_>(), std::declval<const ImplementationRhsT_>()));
154};
155
156template <wuzp_eigen::EigenArray ImplementationLhsT_, wuzp_eigen::EigenArray ImplementationRhsT_>
157struct CrossProduct<AccessImplementation<ImplementationLhsT_>, AccessImplementation<ImplementationRhsT_>> {
158 static auto value(const ImplementationLhsT_& implLhs, const ImplementationRhsT_& implRhs) {
159 // Evaluate expression to avoid returning a temporary object.
160 return (implLhs.matrix().cross(implRhs.matrix())).array().eval();
161 }
162 // Use decltype/declval to allow for lazy evaluation.
163 using type = decltype(value(std::declval<const ImplementationLhsT_>(), std::declval<const ImplementationRhsT_>()));
164};
165
166template <wuzp_eigen::EigenArray ImplementationLhsT_, wuzp_eigen::EigenArray ImplementationRhsT_>
167struct Division<AccessImplementation<ImplementationLhsT_>, AccessImplementation<ImplementationRhsT_>>
168 : internal::OperatorDivision<ImplementationLhsT_, ImplementationRhsT_> {};
169template <wuzp_eigen::EigenArray ImplementationLhsT_, ArithmeticValue ValueRhsT_>
170struct Division<AccessImplementation<ImplementationLhsT_>, ValueRhsT_> : internal::OperatorDivision<ImplementationLhsT_, ValueRhsT_> {};
171
172template <wuzp_eigen::EigenArray ImplementationT_>
173struct Norm<AccessImplementation<ImplementationT_>> {
174 static auto value(const ImplementationT_& impl) { return impl.matrix().norm(); }
175 // Use decltype/declval to allow for lazy evaluation.
176 using type = decltype(value(std::declval<const ImplementationT_>()));
177};
178
179template <wuzp_eigen::EigenArray ImplementationT_>
180struct SquaredNorm<AccessImplementation<ImplementationT_>> {
181 static auto value(const ImplementationT_& impl) { return impl.matrix().squaredNorm(); }
182 // Use decltype/declval to allow for lazy evaluation.
183 using type = decltype(value(std::declval<const ImplementationT_>()));
184};
185
186template <wuzp_eigen::EigenArray ImplementationT_>
187struct Determinant<AccessImplementation<ImplementationT_>> {
188 static auto value(const ImplementationT_& impl) { return impl.matrix().determinant(); }
189 // Use decltype/declval to allow for lazy evaluation.
190 using type = decltype(value(std::declval<const ImplementationT_>()));
191};
192
193// The matrix inverse. Eigen's arrays name the elementwise reciprocal inverse(), its matrices the matrix inverse.
194template <wuzp_eigen::EigenArray ImplementationT_>
195struct Inverse<AccessImplementation<ImplementationT_>> {
196 static auto value(const ImplementationT_& impl) { return impl.matrix().inverse().array(); }
197 // Use decltype/declval to allow for lazy evaluation.
198 using type = decltype(value(std::declval<const ImplementationT_>()));
199};
200
201template <wuzp_eigen::EigenArray ImplementationT_>
202struct CwiseInverse<AccessImplementation<ImplementationT_>> {
203 static auto value(const ImplementationT_& impl) { return impl.inverse(); }
204 // Use decltype/declval to allow for lazy evaluation.
205 using type = decltype(value(std::declval<const ImplementationT_>()));
206};
207
208// Eigen's unaryExpr() applies a function object to every element, lazily: the expression stores a copy of the function (see
209// DenseBase::unaryExpr() in https://eigen.tuxfamily.org/dox/classEigen_1_1DenseBase.html).
210template <wuzp_eigen::EigenArray ImplementationT_, typename FunctionT_>
211struct CwiseFunction<AccessImplementation<ImplementationT_>, FunctionT_> {
212 static auto value(const ImplementationT_& impl, const FunctionT_& function) { return impl.unaryExpr(function); }
213 // Use decltype/declval to allow for lazy evaluation.
214 using type = decltype(value(std::declval<const ImplementationT_>(), std::declval<const FunctionT_>()));
215};
216
217template <wuzp_eigen::EigenArray ImplementationT_, int BlockRowIdV_, int BlockColIdV_, int BlockRowsV_, int BlockColsV_>
218struct Block<AccessImplementation<ImplementationT_>, BlockRowIdV_, BlockColIdV_, BlockRowsV_, BlockColsV_> {
219 static auto value(const ImplementationT_& impl) { return impl.template block<BlockRowsV_, BlockColsV_>(BlockRowIdV_, BlockColIdV_); }
220 // Use decltype/declval to allow for lazy evaluation.
221 using type = decltype(value(std::declval<const ImplementationT_>()));
222};
223
224template <wuzp_eigen::EigenArray ImplementationT_>
225struct Zero<AccessImplementation<ImplementationT_>> {
226 static void run(ImplementationT_& impl) { impl.setZero(); }
227};
228
229template <wuzp_eigen::EigenArray ImplementationT_>
230struct Random<AccessImplementation<ImplementationT_>> {
231 // Uniformly distributed in [-1, 1].
232 static void run(ImplementationT_& impl) { impl.setRandom(); }
233};
234
235// Eigen's matrices have value semantics: their copies do not share the values. Copies of expressions refer to the same
236// operands, like the expressions themselves.
237template <wuzp_eigen::EigenArray ImplementationT_>
238struct Copy<AccessImplementation<ImplementationT_>> {
239 static ImplementationT_ value(const ImplementationT_& impl) { return impl; }
240};
241
242template <wuzp_eigen::EigenArray ImplementationT_>
243struct Move<AccessImplementation<ImplementationT_>> {
244 static ImplementationT_ construct(ImplementationT_& source) { return std::move(source); }
245 static void assign(ImplementationT_& target, ImplementationT_& source) { target = std::move(source); }
246};
247
248// Evaluates an array or an expression into an array with the default storage order (see Rebind). Eigen's eval() keeps the
249// storage order of the expression instead, e.g. the transpose of a column-major matrix is row-major.
250template <wuzp_eigen::EigenArray ImplementationT_>
251struct Evaluate<AccessImplementation<ImplementationT_>> {
252 using type = typename Rebind<AccessImplementation<ImplementationT_>, typename ImplementationT_::Scalar,
253 ImplementationT_::RowsAtCompileTime, ImplementationT_::ColsAtCompileTime>::type;
254 static type value(const ImplementationT_& impl) { return type(impl); }
255};
256
257} // namespace wuzp
Wrapper for the actual implementation of a value container.
The implementation types of the backend: Eigen's arrays and the expressions of arrays,...
Value< int > ValueI
ValueMatrix< int, RowsV_, ColsV_ > ValueMatrixI
ValueVector< float, RowsV_ > ValueVectorF
ValueVector< int, RowsV_ > ValueVectorI
wuzp::Quantity< ValueD, UnitT_ > QuantityD
wuzp::Quantity< ValueI, UnitT_ > QuantityI
A quantity with a value of this backend, e.g. QuantityD<wuzp_units::Length>.
Value< double > ValueD
ValueVector< double, RowsV_ > ValueVectorD
ValueMatrix< double, RowsV_, ColsV_ > ValueMatrixD
ValueMatrix< float, RowsV_, ColsV_ > ValueMatrixF
ValueMatrix< ValueT_, RowsV_, 1 > ValueVector
ValueMatrix< ValueT_, 1, 1 > Value
wuzp::ValuesWrapper< wuzp::MatrixSize< RowsV_, ColsV_ >, ValueT_, Eigen::Array< ValueT_, RowsV_, ColsV_ > > ValueMatrix
Value< float > ValueF
wuzp::Quantity< ValueF, UnitT_ > QuantityF
Quantities< ValueT_, TypeMatrixImpl< MatrixSize< 1, 1 >, std::tuple< UnitT_ > > > Quantity
Container to store a single quantity.
Definition Quantity.hpp:29
void applyCommas(First &&first, Rest &&... rest)
Applies the comma operator from left to right: applyCommas(a, b, c) evaluates ((a,...
Definition helpers.hpp:134
Type wrapper which is used to access traits partially specialized in the implementation header files.
Definition helpers.hpp:114
static auto get(const ImplementationT_ &impl, const int row, const int col)
static void set(ImplementationT_ &impl, const int row, const int col, const typename ImplementationT_::Scalar value)
Element access of a matrix of a value matrix backend: get(impl, row, col) returns an element,...
Definition traits.hpp:321
static bool equal(const ImplementationLhsT_ &lhs, const ImplementationRhsT_ &rhs)
static bool approxEqual(const ImplementationLhsT_ &lhs, const ImplementationRhsT_ &rhs, const typename ImplementationLhsT_::Scalar tolerance)
static ImplementationT_ value(const ImplementationT_ &impl)
Copies a matrix of a value matrix backend: value returns the copy, which must not share its values wi...
Definition traits.hpp:246
static auto value(const ImplementationLhsT_ &implLhs, const ImplementationRhsT_ &implRhs)
decltype(value(std::declval< const ImplementationLhsT_ >(), std::declval< const ImplementationRhsT_ >())) type
static auto value(const ImplementationT_ &impl, const FunctionT_ &function)
decltype(value(std::declval< const ImplementationT_ >(), std::declval< const FunctionT_ >())) type
A function applied to every element of a matrix, like Eigen's DenseBase::unaryExpr(): value(impl,...
Definition traits.hpp:203
decltype(value(std::declval< const ImplementationT_ >())) type
The elementwise reciprocal of a matrix, like Eigen's ArrayBase::inverse() or cwiseInverse().
Definition traits.hpp:194
static auto value(const ImplementationT_ &impl)
decltype(value(std::declval< const ImplementationT_ >())) type
typename Rebind< AccessImplementation< ImplementationT_ >, typename ImplementationT_::Scalar, ImplementationT_::RowsAtCompileTime, ImplementationT_::ColsAtCompileTime >::type type
static type value(const ImplementationT_ &impl)
static void run(ImplementationT_ &impl, Arg0T_ &&arg0, ArgsT_ &&... args)
Initializes an allocated matrix of a value matrix backend with its elements, row by row: run(impl,...
Definition traits.hpp:316
decltype(value(std::declval< const ImplementationLhsT_ >(), std::declval< const ImplementationRhsT_ >())) type
static auto value(const ImplementationLhsT_ &implLhs, const ImplementationRhsT_ &implRhs)
static auto value(const ImplementationT_ &impl)
decltype(value(std::declval< const ImplementationT_ >())) type
The inverse: the matrix inverse of a matrix, like Eigen's MatrixBase::inverse(), and the reciprocal o...
Definition traits.hpp:188
static void assign(ImplementationT_ &target, ImplementationT_ &source)
static ImplementationT_ construct(ImplementationT_ &source)
Moves a matrix of a value matrix backend: construct returns the moved matrix, assign moves it into an...
Definition traits.hpp:255
The negation is performed elementwise.
Definition traits.hpp:101
static auto value(const ImplementationT_ &impl)
decltype(value(std::declval< const ImplementationT_ >())) type
static auto value(const ImplementationLhsT_ &implLhs, const ImplementationRhsT_ &implRhs)
decltype(value(std::declval< const ImplementationLhsT_ >(), std::declval< const ImplementationRhsT_ >())) type
The evaluated type of another value type and size, of the same backend as T: e.g. the 1x1 values of a...
Definition traits.hpp:330
Allocates a default constructed matrix of a value matrix backend for the size MatrixSizeT_: run(impl)...
Definition traits.hpp:310
static auto value(const ImplementationT_ &impl)
decltype(value(std::declval< const ImplementationT_ >())) type
decltype(value(std::declval< const ImplementationT_ >())) type
static auto value(const ImplementationT_ &impl)