wuzp
Wrong Unit? Zero Points! Compile-time checks for units, coordinate systems and points vs. vectors.
Loading...
Searching...
No Matches
ValuesWrapper.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <ostream>
4#include <tuple>
5#include <type_traits>
6#include <utility>
7
10#include "wuzp/core/helpers.hpp"
11#include "wuzp/core/traits.hpp"
12
13namespace wuzp {
14
21template <AnyMatrixSize MatrixSizeT_, ArithmeticValue ValueT_, typename ImplementationT_>
23 public:
24 using MatrixSizeT = MatrixSizeT_;
25 using ValueT = ValueT_;
26 using ImplementationT = std::decay_t<ImplementationT_>;
28
30
33 ValuesWrapper(const ValuesWrapper& other) : implementation_(copyImplementation(other.implementation_)) {}
36 ValuesWrapper(ValuesWrapper&& other) noexcept
37 : implementation_(Move<AccessImplementation<ImplementationT>>::construct(other.implementation_)) {}
38
41 if (this != &other) {
42 *this = ValuesWrapper(other);
43 }
44 return *this;
45 }
47 static_assert(std::is_same_v<ImplementationT, typename Evaluate<AccessImplementation<ImplementationT>>::type>,
48 "Unevaluated values cannot be assigned to. Evaluate them first.");
49 if (this != &other) {
50 Move<AccessImplementation<ImplementationT>>::assign(implementation_, other.implementation_);
51 }
52 return *this;
53 }
54 ~ValuesWrapper() = default;
55
56 // Copies and moves are left to the constructors above, see CopiesOrMoves.
57 template <typename T0, typename... Ts>
59 // Allow implicit construction from similar values of the same size. NOLINTNEXTLINE(google-explicit-constructor)
60 explicit(!IsValidConversion<ThisT, std::remove_cvref_t<T0>, std::remove_cvref_t<Ts>...>::value) ValuesWrapper(T0&& t0, Ts&&... ts)
61 : ValuesWrapper(typename GetTag<ThisT, T0, Ts...>::type{}, std::forward<T0>(t0), std::forward<Ts>(ts)...) {}
62
64 static ThisT zero() {
65 ThisT result;
66 Zero<AccessImplementation<ImplementationT>>::run(result.implementation_);
67 return result;
68 }
69
71 static ThisT random() {
72 static_assert(std::is_floating_point_v<ValueT>, "Random values are only supported for floating point types.");
73 ThisT result;
74 Random<AccessImplementation<ImplementationT>>::run(result.implementation_);
75 return result;
76 }
77
79 template <int RowIdV_, int ColIdV_>
80 auto get() const {
81 static_assert(internal::RequireElementInRange<MatrixSizeT, RowIdV_, ColIdV_>::value);
82 return Coefficient<AccessImplementation<ImplementationT>>::get(implementation_, RowIdV_, ColIdV_);
83 }
84
88 template <int IdV_>
89 auto get() const
90 requires(Dimension<ThisT>::value <= 1)
91 {
92 return get<internal::vectorElementRowId<ThisT>(IdV_), internal::vectorElementColId<ThisT>(IdV_)>();
93 }
94
95 auto get() const
96 requires(Dimension<ThisT>::value == 0)
97 {
98 return get<0, 0>();
99 }
100
101 auto get(int rowId, int colId) const { return Coefficient<AccessImplementation<ImplementationT>>::get(implementation_, rowId, colId); }
102
104 auto get(int id) const
105 requires(Dimension<ThisT>::value <= 1)
106 {
107 return get(internal::vectorElementRowId<ThisT>(id), internal::vectorElementColId<ThisT>(id));
108 }
109
112 template <int RowIdV_, int ColIdV_, typename ValueOtherT_>
113 ThisT& set(ValueOtherT_ value) {
114 static_assert(internal::RequireElementInRange<MatrixSizeT, RowIdV_, ColIdV_>::value);
115 return set(RowIdV_, ColIdV_, value);
116 }
117
120 template <int IdV_, typename ValueOtherT_>
121 ThisT& set(ValueOtherT_ value)
122 requires(Dimension<ThisT>::value <= 1)
123 {
124 return set<internal::vectorElementRowId<ThisT>(IdV_), internal::vectorElementColId<ThisT>(IdV_)>(value);
125 }
126
127 template <typename ValueOtherT_>
128 ThisT& set(ValueOtherT_ value)
129 requires(Dimension<ThisT>::value == 0)
130 {
131 return set<0, 0>(value);
132 }
133
137 template <typename ValueOtherT_>
138 ThisT& set(int rowId, int colId, ValueOtherT_ value) {
140 "The common type of the element and the value type is not the value type, e.g. a double element with int values.");
141 Coefficient<AccessImplementation<ImplementationT>>::set(implementation_, rowId, colId, static_cast<ValueT>(value));
142 return *this;
143 }
144
147 template <typename ValueOtherT_>
148 ThisT& set(int id, ValueOtherT_ value)
149 requires(Dimension<ThisT>::value <= 1)
150 {
151 return set(internal::vectorElementRowId<ThisT>(id), internal::vectorElementColId<ThisT>(id), value);
152 }
153
154 // NOLINTNEXTLINE(google-explicit-constructor)
155 operator typename GetValue<ThisT>::type() const
156 requires(Dimension<ThisT>::value == 0)
157 {
158 return get<0, 0>();
159 }
160
169 template <typename MatrixSizeOtherT_, typename ValueOtherT_, typename ImplementationOtherT_>
171 [[maybe_unused]] const ValueT precision = Precision<ValueT>::value()) const {
172 if constexpr (std::is_integral_v<ValueT> || std::is_integral_v<ValueOtherT_>) {
173 return *this == other;
174 } else {
176 implementation_, other.getImplementation(), precision);
177 }
178 }
179
180 // The member functions and operators return evaluated results, which do not refer to their operands, so that they can be
181 // stored, e.g. in `auto` variables. The traits, e.g. Block<ThisT, ...>::type, are the lazy types they evaluate.
182
183 template <int BlockRowIdV_, int BlockColIdV_, int BlockRowsV_, int BlockColsV_>
184 auto getBlock() const {
186 Block<AccessImplementation<ImplementationT>, BlockRowIdV_, BlockColIdV_, BlockRowsV_, BlockColsV_>::value(implementation_))
187 .evaluate();
188 }
189
190 auto transpose() const {
191 return typename Transpose<ThisT>::type{Transpose<AccessImplementation<ImplementationT>>::value(implementation_)}.evaluate();
192 }
193
194 template <typename MatrixSizeOtherT_, typename ValueOtherT_, typename ImplementationOtherT_>
196 return typename InnerProduct<ThisT, ValuesWrapper<MatrixSizeOtherT_, ValueOtherT_, ImplementationOtherT_>>::type(
198 implementation_, other.getImplementation()))
199 .evaluate();
200 }
201
202 template <typename MatrixSizeOtherT_, typename ValueOtherT_, typename ImplementationOtherT_>
204 return typename OuterProduct<ThisT, ValuesWrapper<MatrixSizeOtherT_, ValueOtherT_, ImplementationOtherT_>>::type(
206 implementation_, other.getImplementation()))
207 .evaluate();
208 }
209
210 template <typename MatrixSizeOtherT_, typename ValueOtherT_, typename ImplementationOtherT_>
212 // Reuse the innerProduct instead of delegating it to the implementation.
214 transpose().innerProduct(other));
215 }
216
217 template <typename MatrixSizeOtherT_, typename ValueOtherT_, typename ImplementationOtherT_>
224
225 auto norm() const { return Norm<AccessImplementation<ImplementationT>>::value(implementation_); }
226 auto squaredNorm() const { return SquaredNorm<AccessImplementation<ImplementationT>>::value(implementation_); }
227 auto determinant() const { return Determinant<AccessImplementation<ImplementationT>>::value(implementation_); }
228
232 auto inverse() const {
233 return typename Inverse<ThisT>::type{Inverse<AccessImplementation<ImplementationT>>::value(implementation_)}.evaluate();
234 }
235
236 auto cwiseInverse() const {
237 return typename CwiseInverse<ThisT>::type{CwiseInverse<AccessImplementation<ImplementationT>>::value(implementation_)}.evaluate();
238 }
239
241 template <typename FunctionT_>
242 auto cwiseFunction(const FunctionT_& function) const {
243 static_assert(std::is_same_v<std::invoke_result_t<const FunctionT_&, ValueT>, ValueT>,
244 "The function must map a value of the value type to a value of the value type.");
246 CwiseFunction<AccessImplementation<ImplementationT>, FunctionT_>::value(implementation_, function)}
247 .evaluate();
248 }
249
251 implementation_ = implementation;
252 return *this;
253 }
254 ImplementationT& implementation() { return implementation_; }
255 const ImplementationT& getImplementation() const { return implementation_; }
256
257 auto evaluate() const& { return typename Evaluate<ThisT>::type{Evaluate<AccessImplementation<ImplementationT>>::value(implementation_)}; }
259 auto evaluate() && {
260 if constexpr (std::is_same_v<typename Evaluate<ThisT>::type, ThisT>) {
261 return std::move(*this);
262 } else {
263 return std::as_const(*this).evaluate();
264 }
265 }
266
267 private:
268 template <typename MatrixSizeOtherT_, typename ValueOtherT_, typename ImplementationOtherT_>
269 // Allow other implementation types to support lazy evaluation.
271 : implementation_(copyImplementation(other.getImplementation())) {
273 static_assert(ConversionT::RowsMatchV, "Number of rows do not match.");
274 static_assert(ConversionT::ColsMatchV, "Number of cols do not match.");
275 }
276
278 template <typename T0>
279 requires(!ArithmeticValue<std::remove_cvref_t<T0>>)
280 ValuesWrapper(FromImplementationArgs /*tag*/, T0&& t0) : implementation_(copyImplementation(std::forward<T0>(t0))) {}
286 template <typename T0, typename... Ts>
287 ValuesWrapper(FromImplementationArgs /*tag*/, T0&& t0, Ts&&... ts) {
288 static_assert(sizeof...(Ts) + 1 == MatrixSizeT::NumElementsV, "Number of arguments does not match size of matrix.");
289 static_assert(
291 "The common type of the element and the value type is not the value type, e.g. a double element with int values.");
292 // Only reports the static_assert above if the number does not match, not also that Initialize takes another number.
293 if constexpr (sizeof...(Ts) + 1 == MatrixSizeT::NumElementsV) {
294 ReserveMemory<ImplementationT_, MatrixSizeT_>::run(implementation_);
295 Initialize<ImplementationT_, HomogeneousTuple<MatrixSizeT::NumElementsV, ValueT>>::run(
296 implementation_, static_cast<ValueT>(std::forward<T0>(t0)), static_cast<ValueT>(std::forward<Ts>(ts))...);
297 }
298 }
299
302 template <typename ImplementationOtherT_>
303 static ImplementationT copyImplementation(ImplementationOtherT_&& other) {
304 if constexpr (std::is_lvalue_reference_v<ImplementationOtherT_> &&
305 std::is_same_v<std::remove_cvref_t<ImplementationOtherT_>, ImplementationT>) {
306 return Copy<AccessImplementation<ImplementationT>>::value(other);
307 } else {
308 return ImplementationT(std::forward<ImplementationOtherT_>(other));
309 }
310 }
311
312 ImplementationT implementation_;
313};
314
315template <typename MatrixSizeT_, typename ValueT_, typename ImplementationT_, typename MatrixSizeOtherT_, typename ValueOtherT_,
316 typename ImplementationOtherT_>
317struct IsValidConversion<ValuesWrapper<MatrixSizeT_, ValueT_, ImplementationT_>,
318 ValuesWrapper<MatrixSizeOtherT_, ValueOtherT_, ImplementationOtherT_>> {
319 // The converting constructor checks these with static_asserts, which explain why a conversion fails.
322 static constexpr bool value = RowsMatchV && ColsMatchV;
323};
324
325template <typename... Xs, typename... Ts>
326struct GetTag<ValuesWrapper<Xs...>, Ts...> {
327 private:
328 using first = std::decay_t<std::tuple_element_t<0, std::tuple<Ts...>>>;
329
330 public:
331 using type =
334};
335
336template <typename MatrixSizeT_, typename ValueT_, typename ImplementationT_>
337struct GetValue<ValuesWrapper<MatrixSizeT_, ValueT_, ImplementationT_>> {
338 using type = ValueT_;
339};
340
342template <typename MatrixSizeT_, typename ValueT_, typename ImplementationT_, typename ValueOtherT_, int RowsV_, int ColsV_>
343struct Rebind<ValuesWrapper<MatrixSizeT_, ValueT_, ImplementationT_>, ValueOtherT_, RowsV_, ColsV_> {
345 typename Rebind<AccessImplementation<ImplementationT_>, ValueOtherT_, RowsV_, ColsV_>::type>;
346};
347
350template <typename ValuesT_, typename ValueT_>
352
353template <typename MatrixSizeT_, typename ValueT_, typename ImplementationT_>
354struct Rows<ValuesWrapper<MatrixSizeT_, ValueT_, ImplementationT_>> {
355 static constexpr int value = Rows<MatrixSizeT_>::value;
356};
357
358template <typename MatrixSizeT_, typename ValueT_, typename ImplementationT_>
359struct Cols<ValuesWrapper<MatrixSizeT_, ValueT_, ImplementationT_>> {
360 static constexpr int value = Cols<MatrixSizeT_>::value;
361};
362
363template <typename MatrixSizeLhsT_, typename ValueLhsT_, typename ImplementationLhsT_, typename MatrixSizeRhsT_, typename ValueRhsT_,
364 typename ImplementationRhsT_>
365struct Sum<ValuesWrapper<MatrixSizeLhsT_, ValueLhsT_, ImplementationLhsT_>,
366 ValuesWrapper<MatrixSizeRhsT_, ValueRhsT_, ImplementationRhsT_>> {
367 using type =
369 typename Sum<AccessImplementation<ImplementationLhsT_>, AccessImplementation<ImplementationRhsT_>>::type>;
370};
371
372template <typename MatrixSizeLhsT_, typename ValueLhsT_, typename ImplementationLhsT_, typename MatrixSizeRhsT_, typename ValueRhsT_,
373 typename ImplementationRhsT_>
374struct Difference<ValuesWrapper<MatrixSizeLhsT_, ValueLhsT_, ImplementationLhsT_>,
375 ValuesWrapper<MatrixSizeRhsT_, ValueRhsT_, ImplementationRhsT_>> {
376 using type =
378 typename Difference<ValueType<ValueLhsT_>, ValueType<ValueRhsT_>>::type,
379 typename Difference<AccessImplementation<ImplementationLhsT_>, AccessImplementation<ImplementationRhsT_>>::type>;
380};
381
382template <typename MatrixSizeT_, typename ValueT_, typename ImplementationT_>
386
387// Rules to implement product:
388// 1. If the LHS is a scalar (dimension == 0), revert the order and reuse the implementation where the scalar is on the RHS.
389// 2. To avoid overhead, do not create temporary objects to reuse implementation, e.g. creating a 1x1 matrix from a scalar.
390template <typename ValueLhsT_, typename MatrixSizeRhsT_, typename ValueRhsT_, typename ImplementationRhsT_>
391struct Product<ValueLhsT_, ValuesWrapper<MatrixSizeRhsT_, ValueRhsT_, ImplementationRhsT_>> {
392 using type = typename Product<ValuesWrapper<MatrixSizeRhsT_, ValueRhsT_, ImplementationRhsT_>, ValueLhsT_>::type;
393};
394template <typename MatrixSizeLhsT_, typename ValueLhsT_, typename ImplementationLhsT_, typename ValueRhsT_>
395struct Product<ValuesWrapper<MatrixSizeLhsT_, ValueLhsT_, ImplementationLhsT_>, ValueRhsT_> {
397 typename Product<AccessImplementation<ImplementationLhsT_>, ValueRhsT_>::type>;
398};
399template <typename ValueLhsT_, typename ImplementationLhsT_, typename ValueRhsT_, typename ImplementationRhsT_>
400struct Product<ValuesWrapper<MatrixSize<1, 1>, ValueLhsT_, ImplementationLhsT_>,
401 ValuesWrapper<MatrixSize<1, 1>, ValueRhsT_, ImplementationRhsT_>> {
402 // If both matrices have size 1x1, we keep the order lhs * rhs.
404 typename Product<ValueType<ValueLhsT_>, ValueType<ValueRhsT_>>::type,
405 typename Product<AccessImplementation<ImplementationLhsT_>, ValueRhsT_>::type>;
406};
407template <typename ValueLhsT_, typename ImplementationLhsT_, typename MatrixSizeRhsT_, typename ValueRhsT_, typename ImplementationRhsT_>
408struct Product<ValuesWrapper<MatrixSize<1, 1>, ValueLhsT_, ImplementationLhsT_>,
409 ValuesWrapper<MatrixSizeRhsT_, ValueRhsT_, ImplementationRhsT_>> {
410 using type = typename Product<ValueLhsT_, ValuesWrapper<MatrixSizeRhsT_, ValueRhsT_, ImplementationRhsT_>>::type;
411};
412template <typename MatrixSizeLhsT_, typename ValueLhsT_, typename ImplementationLhsT_, typename ValueRhsT_, typename ImplementationRhsT_>
413struct Product<ValuesWrapper<MatrixSizeLhsT_, ValueLhsT_, ImplementationLhsT_>,
414 ValuesWrapper<MatrixSize<1, 1>, ValueRhsT_, ImplementationRhsT_>> {
416 typename Product<ValueType<ValueLhsT_>, ValueType<ValueRhsT_>>::type,
417 typename Product<AccessImplementation<ImplementationLhsT_>, ValueRhsT_>::type>;
418};
419template <typename MatrixSizeLhsT_, typename ValueLhsT_, typename ImplementationLhsT_, typename MatrixSizeRhsT_, typename ValueRhsT_,
420 typename ImplementationRhsT_>
421struct Product<ValuesWrapper<MatrixSizeLhsT_, ValueLhsT_, ImplementationLhsT_>,
422 ValuesWrapper<MatrixSizeRhsT_, ValueRhsT_, ImplementationRhsT_>> {
424 typename Product<ValueType<ValueLhsT_>, ValueType<ValueRhsT_>>::type,
425 typename Product<AccessImplementation<ImplementationLhsT_>, AccessImplementation<ImplementationRhsT_>>::type>;
426};
427
428template <typename MatrixSizeLhsT_, typename ValueLhsT_, typename ImplementationLhsT_, typename MatrixSizeRhsT_, typename ValueRhsT_,
429 typename ImplementationRhsT_>
430struct InnerProduct<ValuesWrapper<MatrixSizeLhsT_, ValueLhsT_, ImplementationLhsT_>,
431 ValuesWrapper<MatrixSizeRhsT_, ValueRhsT_, ImplementationRhsT_>> {
432 using type =
434 typename Product<ValueType<ValueLhsT_>, ValueType<ValueRhsT_>>::type,
435 typename InnerProduct<AccessImplementation<ImplementationLhsT_>, AccessImplementation<ImplementationRhsT_>>::type>;
436};
437
438template <typename MatrixSizeLhsT_, typename ValueLhsT_, typename ImplementationLhsT_, typename MatrixSizeRhsT_, typename ValueRhsT_,
439 typename ImplementationRhsT_>
440struct OuterProduct<ValuesWrapper<MatrixSizeLhsT_, ValueLhsT_, ImplementationLhsT_>,
441 ValuesWrapper<MatrixSizeRhsT_, ValueRhsT_, ImplementationRhsT_>> {
442 using type =
444 typename Product<ValueType<ValueLhsT_>, ValueType<ValueRhsT_>>::type,
445 typename OuterProduct<AccessImplementation<ImplementationLhsT_>, AccessImplementation<ImplementationRhsT_>>::type>;
446};
447
448template <typename MatrixSizeLhsT_, typename ValueLhsT_, typename ImplementationLhsT_, typename MatrixSizeRhsT_, typename ValueRhsT_,
449 typename ImplementationRhsT_>
456
457template <typename MatrixSizeLhsT_, typename ValueLhsT_, typename ImplementationLhsT_, typename MatrixSizeRhsT_, typename ValueRhsT_,
458 typename ImplementationRhsT_>
459struct CrossProduct<ValuesWrapper<MatrixSizeLhsT_, ValueLhsT_, ImplementationLhsT_>,
460 ValuesWrapper<MatrixSizeRhsT_, ValueRhsT_, ImplementationRhsT_>> {
461 using type =
463 typename Product<ValueType<ValueLhsT_>, ValueType<ValueRhsT_>>::type,
465};
466
467template <typename MatrixSizeLhsT_, typename ValueLhsT_, typename ImplementationLhsT_, typename ValueRhsT_>
468struct Division<ValuesWrapper<MatrixSizeLhsT_, ValueLhsT_, ImplementationLhsT_>, ValueRhsT_> {
470 typename Division<AccessImplementation<ImplementationLhsT_>, ValueRhsT_>::type>;
471};
472template <typename MatrixSizeLhsT_, typename ValueLhsT_, typename ImplementationLhsT_, typename ValueRhsT_, typename ImplementationRhsT_>
473struct Division<ValuesWrapper<MatrixSizeLhsT_, ValueLhsT_, ImplementationLhsT_>,
474 ValuesWrapper<MatrixSize<1, 1>, ValueRhsT_, ImplementationRhsT_>> {
476 typename Division<ValueType<ValueLhsT_>, ValueType<ValueRhsT_>>::type,
477 typename Division<AccessImplementation<ImplementationLhsT_>, ValueRhsT_>::type>;
478};
479template <typename MatrixSizeLhsT_, typename ValueLhsT_, typename ImplementationLhsT_, typename MatrixSizeRhsT_, typename ValueRhsT_,
480 typename ImplementationRhsT_>
481struct Division<ValuesWrapper<MatrixSizeLhsT_, ValueLhsT_, ImplementationLhsT_>,
482 ValuesWrapper<MatrixSizeRhsT_, ValueRhsT_, ImplementationRhsT_>> {
484 typename Division<ValueType<ValueLhsT_>, ValueType<ValueRhsT_>>::type,
485 typename Division<AccessImplementation<ImplementationLhsT_>, AccessImplementation<ImplementationRhsT_>>::type>;
486};
487
488template <typename MatrixSizeT_, typename ValueT_, typename ImplementationT_>
489struct Transpose<ValuesWrapper<MatrixSizeT_, ValueT_, ImplementationT_>> {
490 using type =
492};
493
497template <typename MatrixSizeT_, typename ValueT_, typename ImplementationT_>
498struct Inverse<ValuesWrapper<MatrixSizeT_, ValueT_, ImplementationT_>> {
499 static_assert(std::is_floating_point_v<ValueT_>,
500 "The inverse of an integer matrix is not an integer matrix. Use a floating-point value type.");
502};
503
504template <typename MatrixSizeT_, typename ValueT_, typename ImplementationT_>
508
509template <typename MatrixSizeT_, typename ValueT_, typename ImplementationT_, typename FunctionT_>
510struct CwiseFunction<ValuesWrapper<MatrixSizeT_, ValueT_, ImplementationT_>, FunctionT_> {
512};
513
514template <typename MatrixSizeT_, typename ValueT_, typename ImplementationT_, int BlockRowIdV_, int BlockColIdV_, int BlockRowsV_,
515 int BlockColsV_>
516struct Block<ValuesWrapper<MatrixSizeT_, ValueT_, ImplementationT_>, BlockRowIdV_, BlockColIdV_, BlockRowsV_, BlockColsV_> {
517 using type =
519 typename Block<AccessImplementation<ImplementationT_>, BlockRowIdV_, BlockColIdV_, BlockRowsV_, BlockColsV_>::type>;
520};
521
522template <typename MatrixSizeT_, typename ValueT_, typename ImplementationT_>
526
527template <typename MatrixSizeT_, typename ValueT_, typename ImplementationT_, typename... Args>
529
530template <typename MatrixSizeLhsT_, typename ValueLhsT_, typename ImplementationLhsT_, typename MatrixSizeRhsT_, typename ValueRhsT_,
531 typename ImplementationRhsT_>
540template <typename MatrixSizeLhsT_, typename ValueLhsT_, typename ImplementationLhsT_, typename MatrixSizeRhsT_, typename ValueRhsT_,
541 typename ImplementationRhsT_>
546
547template <typename MatrixSizeLhsT_, typename ValueLhsT_, typename ImplementationLhsT_, typename MatrixSizeRhsT_, typename ValueRhsT_,
548 typename ImplementationRhsT_>
557template <typename MatrixSizeLhsT_, typename ValueLhsT_, typename ImplementationLhsT_, typename MatrixSizeRhsT_, typename ValueRhsT_,
558 typename ImplementationRhsT_>
563
564template <typename MatrixSizeT_, typename ValueT_, typename ImplementationT_>
570template <typename MatrixSizeT_, typename ValueT_, typename ImplementationT_>
574
575template <typename MatrixSizeLhsT_, typename ValueLhsT_, typename ImplementationLhsT_, typename ValueRhsT_>
577 return typename Product<ValuesWrapper<MatrixSizeLhsT_, ValueLhsT_, ImplementationLhsT_>, ValueRhsT_>::type(
578 Product<AccessImplementation<ImplementationLhsT_>, ValueRhsT_>::value(lhs.getImplementation(), rhs))
579 .evaluate();
580}
581template <typename ValueLhsT_, typename MatrixSizeRhsT_, typename ValueRhsT_, typename ImplementationRhsT_>
583 return product(rhs, lhs);
584}
585template <typename MatrixSizeLhsT_, typename ValueLhsT_, typename ImplementationLhsT_, typename MatrixSizeRhsT_, typename ValueRhsT_,
586 typename ImplementationRhsT_>
589 using Result = typename Product<ValuesWrapper<MatrixSizeLhsT_, ValueLhsT_, ImplementationLhsT_>,
591 // If both matrices have size 1x1, we keep the order lhs * rhs.
592 if constexpr (std::is_same_v<MatrixSizeRhsT_, MatrixSize<1, 1>>) {
593 return Result(Product<AccessImplementation<ImplementationLhsT_>, ValueRhsT_>::value(lhs.getImplementation(), rhs.get())).evaluate();
594 } else if constexpr (std::is_same_v<MatrixSizeLhsT_, MatrixSize<1, 1>>) {
595 return Result(Product<AccessImplementation<ImplementationRhsT_>, ValueLhsT_>::value(rhs.getImplementation(), lhs.get())).evaluate();
596 } else {
599 .evaluate();
600 }
601}
602template <typename MatrixSizeLhsT_, typename ValueLhsT_, typename ImplementationLhsT_, typename ValueRhsT_>
604 return product(lhs, rhs);
605}
606
607// Eigen::CwiseBinaryOp<Eigen::internal::scalar_product_op<int, int>,
608// const Eigen::CwiseBinaryOp<
609// Eigen::internal::scalar_product_op<int, int>, const Eigen::Array<int, 1, 1, 0, 1, 1>,
610// const Eigen::CwiseNullaryOp<Eigen::internal::scalar_constant_op<int>, const Eigen::Array<int, 1, 1, 0, 1, 1>>>,
611// const Eigen::CwiseNullaryOp<Eigen::internal::scalar_constant_op<int>, const Eigen::Array<int, 1, 1, 0, 1, 1>>>::
612// CwiseBinaryOp(
613// Eigen::CwiseBinaryOp<Eigen::internal::scalar_product_op<int, int>, const Eigen::Array<int, 1, 1, 0, 1, 1>,
614// const Eigen::CwiseNullaryOp<Eigen::internal::scalar_constant_op<int>, const Eigen::Array<int, 1, 1, 0, 1,
615// 1>>>)
616
617// Eigen::CwiseBinaryOp<
618// Eigen::internal::scalar_product_op<double, double>,
619// const Eigen::CwiseBinaryOp<
620// Eigen::internal::scalar_sum_op<double, double>,
621// const Eigen::CwiseBinaryOp<
622// Eigen::internal::scalar_product_op<double, double>, const Eigen::Array<double, 1, 1, 0, 1, 1>,
623// const Eigen::CwiseNullaryOp<Eigen::internal::scalar_constant_op<double>, const Eigen::Array<double, 1, 1, 0, 1, 1>>>,
624// const Eigen::CwiseBinaryOp<
625// Eigen::internal::scalar_product_op<double, double>, const Eigen::Array<double, 1, 1, 0, 1, 1>,
626// const Eigen::CwiseNullaryOp<Eigen::internal::scalar_constant_op<double>, const Eigen::Array<double, 1, 1, 0, 1, 1>>>>,
627// const Eigen::CwiseNullaryOp<Eigen::internal::scalar_constant_op<double>, const Eigen::Array<double, 1, 1, 0, 1, 1>>>::
628// CwiseBinaryOp(Eigen::CwiseBinaryOp<
629// Eigen::internal::scalar_product_op<double, double>, const Eigen::Array<double, 1, 1, 0, 1, 1>,
630// const Eigen::CwiseNullaryOp<Eigen::internal::scalar_constant_op<double>, const Eigen::Array<double, 1, 1, 0, 1,
631// 1>>>)
632
633template <typename ValueLhsT_, typename MatrixSizeRhsT_, typename ValueRhsT_, typename ImplementationRhsT_>
635 return product(lhs, rhs);
636}
637template <typename MatrixSizeLhsT_, typename ValueLhsT_, typename ImplementationLhsT_, typename MatrixSizeRhsT_, typename ValueRhsT_,
638 typename ImplementationRhsT_>
643
644template <typename MatrixSizeLhsT_, typename ValueLhsT_, typename ImplementationLhsT_, typename MatrixSizeRhsT_, typename ValueRhsT_,
645 typename ImplementationRhsT_>
650
651template <typename MatrixSizeLhsT_, typename ValueLhsT_, typename ImplementationLhsT_, typename MatrixSizeRhsT_, typename ValueRhsT_,
652 typename ImplementationRhsT_>
657
658template <typename MatrixSizeLhsT_, typename ValueLhsT_, typename ImplementationLhsT_, typename ValueRhsT_>
660 return typename Division<ValuesWrapper<MatrixSizeLhsT_, ValueLhsT_, ImplementationLhsT_>, ValueRhsT_>::type(
661 Division<AccessImplementation<ImplementationLhsT_>, ValueRhsT_>::value(lhs.getImplementation(), rhs))
662 .evaluate();
663}
664template <typename MatrixSizeLhsT_, typename ValueLhsT_, typename ImplementationLhsT_, typename MatrixSizeRhsT_, typename ValueRhsT_,
665 typename ImplementationRhsT_>
668 using Result = typename Division<ValuesWrapper<MatrixSizeLhsT_, ValueLhsT_, ImplementationLhsT_>,
670 if constexpr (std::is_same_v<MatrixSizeRhsT_, MatrixSize<1, 1>>) {
671 return Result(Division<AccessImplementation<ImplementationLhsT_>, ValueRhsT_>::value(lhs.getImplementation(), rhs.get())).evaluate();
672 } else {
675 .evaluate();
676 }
677}
678template <typename MatrixSizeLhsT_, typename ValueLhsT_, typename ImplementationLhsT_, typename ValueRhsT_>
680 return division(lhs, rhs);
681}
682template <typename MatrixSizeLhsT_, typename ValueLhsT_, typename ImplementationLhsT_, typename MatrixSizeRhsT_, typename ValueRhsT_,
683 typename ImplementationRhsT_>
688
689template <typename MatrixSizeLhsT_, typename ValueLhsT_, typename ImplementationLhsT_, typename MatrixSizeRhsT_, typename ValueRhsT_,
690 typename ImplementationRhsT_>
696
697template <typename MatrixSizeLhsT_, typename ValueLhsT_, typename ImplementationLhsT_, typename MatrixSizeRhsT_, typename ValueRhsT_,
698 typename ImplementationRhsT_>
703
704template <typename MatrixSizeT_, typename ValueT_, typename ImplementationT_>
705std::ostream& operator<<(std::ostream& lhs, const ValuesWrapper<MatrixSizeT_, ValueT_, ImplementationT_>& rhs) {
706 lhs << rhs.evaluate().getImplementation();
707 return lhs;
708}
709
710} // namespace wuzp
Wrapper for the actual implementation of a value container.
ValuesWrapper(const ValuesWrapper &other)
Copies the values.
ThisT & set(ValueOtherT_ value)
auto get() const
The element in the row RowIdV_ and the column ColIdV_, which are checked at compile time.
MatrixSizeT_ MatrixSizeT
auto inverse() const
The matrix inverse of a square matrix, like Eigen's MatrixBase::inverse().
auto dotProduct(const ValuesWrapper< MatrixSizeOtherT_, ValueOtherT_, ImplementationOtherT_ > &other) const
std::decay_t< ImplementationT_ > ImplementationT
ValuesWrapper & operator=(const ValuesWrapper &other)
Only evaluated values can be assigned to: an unevaluated value, e.g. the lazy sum of two matrices of ...
ValuesWrapper & operator=(ValuesWrapper &&other) noexcept
static ThisT random()
Random values, uniformly distributed in [-1, 1].
ThisT & set(ValueOtherT_ value)
Sets the element in the row RowIdV_ and the column ColIdV_, which are checked at compile time,...
auto evaluate() const &
auto evaluate() &&
Evaluated values of a temporary, e.g. of the result of an operation: if they are evaluated already,...
ValuesWrapper< MatrixSizeT, ValueT, ImplementationT > ThisT
auto get(int id) const
The element with the single index id of values of dimension 0 or 1, like get<IdV_>(),...
ThisT & setImplementation(const ImplementationT &implementation)
static ThisT zero()
Values that are all zero.
auto innerProduct(const ValuesWrapper< MatrixSizeOtherT_, ValueOtherT_, ImplementationOtherT_ > &other) const
operator typename GetValue< ThisT >::type() const
auto crossProduct(const ValuesWrapper< MatrixSizeOtherT_, ValueOtherT_, ImplementationOtherT_ > &other) const
ThisT & set(int rowId, int colId, ValueOtherT_ value)
Sets an element.
auto outerProduct(const ValuesWrapper< MatrixSizeOtherT_, ValueOtherT_, ImplementationOtherT_ > &other) const
auto get() const
The element with the single index IdV_ of values of dimension 0 or 1: the row of a column,...
auto cwiseFunction(const FunctionT_ &function) const
The function applied to every element, like Eigen's DenseBase::unaryExpr(): the values function(value...
ThisT & set(int id, ValueOtherT_ value)
Sets the element with the single index id of values of dimension 0 or 1, like set<IdV_>(value),...
bool isApprox(const ValuesWrapper< MatrixSizeOtherT_, ValueOtherT_, ImplementationOtherT_ > &other, const ValueT precision=Precision< ValueT >::value()) const
Whether the values are approximately equal to the other values: relatively, like Eigen's isApprox(),...
const ImplementationT & getImplementation() const
ValuesWrapper(ValuesWrapper &&other) noexcept
Moves the values.
auto get(int rowId, int colId) const
~ValuesWrapper()=default
auto cwiseInverse() const
The elementwise reciprocal, like Eigen's ArrayBase::inverse() or MatrixBase::cwiseInverse().
ThisT & set(ValueOtherT_ value)
Sets the element with the single index IdV_ of values of dimension 0 or 1 (see get<IdV_>()),...
Any value matrix: a specialization of ValuesWrapper.
Definition concepts.hpp:83
Whether constructing a Class from the arguments copies or moves it, i.e. whether they are a single Cl...
Definition helpers.hpp:69
auto operator*(const Quantities< ValuesLhsT_, UnitsLhsT_ > &lhs, const ValueRhsT_ rhs)
auto product(const Quantities< ValuesLhsT_, UnitsLhsT_ > &lhs, const ValueRhsT_ rhs)
void initialize(ValuesWrapper< MatrixSizeT_, ValueT_, ImplementationT_ > &values, Args &&... args)
auto negation(const Quantities< ValuesT_, UnitsT_ > &rhs)
auto outerProduct(const Quantities< ValuesLhsT_, UnitsLhsT_ > &lhs, const Quantities< ValuesRhsT_, UnitsRhsT_ > &rhs)
auto operator+(const Quantities< ValuesLhsT_, UnitsLhsT_ > &lhs, const Quantities< ValuesRhsT_, UnitsRhsT_ > &rhs)
bool operator!=(const Quantities< ValuesLhsT_, UnitsLhsT_ > &lhs, const Quantities< ValuesRhsT_, UnitsRhsT_ > &rhs)
std::ostream & operator<<(std::ostream &lhs, const Quantities< ValuesT_, UnitsT_ > &rhs)
constexpr bool isConvertedToValueType
Whether an arithmetic scalar of the type ScalarT_ is converted into the value type ValueT_: if their ...
Definition helpers.hpp:97
auto operator-(const Quantities< ValuesLhsT_, UnitsLhsT_ > &lhs, const Quantities< ValuesRhsT_, UnitsRhsT_ > &rhs)
typename Rebind< ValuesT_, ValueT_, 1, 1 >::type ScalarValues
Evaluated 1x1 values of the value matrix backend of ValuesT_, with the value type ValueT_: e....
auto innerProduct(const Quantities< ValuesLhsT_, UnitsLhsT_ > &lhs, const Quantities< ValuesRhsT_, UnitsRhsT_ > &rhs)
Tag< FromImplementationArgsLabel > FromImplementationArgs
Definition traits.hpp:41
bool operator==(const Quantities< ValuesLhsT_, UnitsLhsT_ > &lhs, const Quantities< ValuesRhsT_, UnitsRhsT_ > &rhs)
Tag< FromSimilarLabel > FromSimilar
Definition traits.hpp:37
auto division(const Quantities< ValuesLhsT_, UnitsLhsT_ > &lhs, const ValueRhsT_ rhs)
auto difference(const Quantities< ValuesLhsT_, UnitsLhsT_ > &lhs, const Quantities< ValuesRhsT_, UnitsRhsT_ > &rhs)
auto sum(const Quantities< ValuesLhsT_, UnitsLhsT_ > &lhs, const Quantities< ValuesRhsT_, UnitsRhsT_ > &rhs)
auto operator/(const Quantities< ValuesLhsT_, UnitsLhsT_ > &lhs, const ValueRhsT_ rhs)
Type wrapper which is used to access traits partially specialized in the implementation header files.
Definition helpers.hpp:114
ValuesWrapper< typename Block< MatrixSizeT_, BlockRowIdV_, BlockColIdV_, BlockRowsV_, BlockColsV_ >::type, ValueT_, typename Block< AccessImplementation< ImplementationT_ >, BlockRowIdV_, BlockColIdV_, BlockRowsV_, BlockColsV_ >::type > type
Element access of a matrix of a value matrix backend: get(impl, row, col) returns an element,...
Definition traits.hpp:321
ValuesWrapper< typename CrossProduct< MatrixSizeLhsT_, MatrixSizeRhsT_ >::type, typename Product< ValueType< ValueLhsT_ >, ValueType< ValueRhsT_ > >::type, typename CrossProduct< AccessImplementation< ImplementationLhsT_ >, AccessImplementation< ImplementationRhsT_ > >::type > type
ValuesWrapper< MatrixSizeT_, ValueT_, typename CwiseFunction< AccessImplementation< ImplementationT_ >, FunctionT_ >::type > type
A function applied to every element of a matrix, like Eigen's DenseBase::unaryExpr(): value(impl,...
Definition traits.hpp:203
ValuesWrapper< MatrixSizeT_, ValueT_, typename CwiseInverse< AccessImplementation< ImplementationT_ > >::type > type
The elementwise reciprocal of a matrix, like Eigen's ArrayBase::inverse() or cwiseInverse().
Definition traits.hpp:194
ValuesWrapper< typename Difference< MatrixSizeLhsT_, MatrixSizeRhsT_ >::type, typename Difference< ValueType< ValueLhsT_ >, ValueType< ValueRhsT_ > >::type, typename Difference< AccessImplementation< ImplementationLhsT_ >, AccessImplementation< ImplementationRhsT_ > >::type > type
static constexpr int value
Definition traits.hpp:70
ValuesWrapper< typename Division< MatrixSizeLhsT_, MatrixSize< 1, 1 > >::type, typename Division< ValueType< ValueLhsT_ >, ValueType< ValueRhsT_ > >::type, typename Division< AccessImplementation< ImplementationLhsT_ >, ValueRhsT_ >::type > type
ValuesWrapper< MatrixSizeLhsT_, typename Division< ValueType< ValueLhsT_ >, ValueType< ValueRhsT_ > >::type, typename Division< AccessImplementation< ImplementationLhsT_ >, ValueRhsT_ >::type > type
ValuesWrapper< typename Division< MatrixSizeLhsT_, MatrixSizeRhsT_ >::type, typename Division< ValueType< ValueLhsT_ >, ValueType< ValueRhsT_ > >::type, typename Division< AccessImplementation< ImplementationLhsT_ >, AccessImplementation< ImplementationRhsT_ > >::type > type
typename Evaluate< typename InnerProduct< typename Transpose< ValuesWrapper< MatrixSizeLhsT_, ValueLhsT_, ImplementationLhsT_ > >::type, ValuesWrapper< MatrixSizeRhsT_, ValueRhsT_, ImplementationRhsT_ > >::type >::type type
ValuesWrapper< MatrixSizeT_, ValueT_, typename Evaluate< AccessImplementation< ImplementationT_ > >::type > type
std::decay_t< T > type
Definition traits.hpp:224
typename IfThenElse< sizeof...(Ts)==1, typename IfThenElse< AnyValuesWrapper< first >, FromSimilar, FromImplementationArgs >::type, FromImplementationArgs >::type type
Trait to select one type or another depending on a boolean. Note that both types must be defined even...
Definition helpers.hpp:51
ValuesWrapper< typename InnerProduct< MatrixSizeLhsT_, MatrixSizeRhsT_ >::type, typename Product< ValueType< ValueLhsT_ >, ValueType< ValueRhsT_ > >::type, typename InnerProduct< AccessImplementation< ImplementationLhsT_ >, AccessImplementation< ImplementationRhsT_ > >::type > type
ValuesWrapper< typename Inverse< MatrixSizeT_ >::type, ValueT_, typename Inverse< AccessImplementation< ImplementationT_ > >::type > type
The inverse: the matrix inverse of a matrix, like Eigen's MatrixBase::inverse(), and the reciprocal o...
Definition traits.hpp:188
Whether T is a specialization of Template.
Definition helpers.hpp:74
Whether converting a From into a To passes the checks of the converting constructor of To,...
Definition traits.hpp:265
Moves a matrix of a value matrix backend: construct returns the moved matrix, assign moves it into an...
Definition traits.hpp:255
ValuesWrapper< MatrixSizeT_, ValueT_, typename Negation< AccessImplementation< ImplementationT_ > >::type > type
The negation is performed elementwise.
Definition traits.hpp:101
ValuesWrapper< typename OuterProduct< MatrixSizeLhsT_, MatrixSizeRhsT_ >::type, typename Product< ValueType< ValueLhsT_ >, ValueType< ValueRhsT_ > >::type, typename OuterProduct< AccessImplementation< ImplementationLhsT_ >, AccessImplementation< ImplementationRhsT_ > >::type > type
static constexpr ValueT_ value()
Definition traits.hpp:356
typename Product< ValuesWrapper< MatrixSizeRhsT_, ValueRhsT_, ImplementationRhsT_ >, ValueLhsT_ >::type type
ValuesWrapper< typename Product< MatrixSizeLhsT_, MatrixSize< 1, 1 > >::type, typename Product< ValueType< ValueLhsT_ >, ValueType< ValueRhsT_ > >::type, typename Product< AccessImplementation< ImplementationLhsT_ >, ValueRhsT_ >::type > type
ValuesWrapper< typename Product< MatrixSizeLhsT_, MatrixSizeRhsT_ >::type, typename Product< ValueType< ValueLhsT_ >, ValueType< ValueRhsT_ > >::type, typename Product< AccessImplementation< ImplementationLhsT_ >, AccessImplementation< ImplementationRhsT_ > >::type > type
ValuesWrapper< MatrixSizeLhsT_, typename Product< ValueType< ValueLhsT_ >, ValueType< ValueRhsT_ > >::type, typename Product< AccessImplementation< ImplementationLhsT_ >, ValueRhsT_ >::type > type
ValuesWrapper< typename Product< MatrixSize< 1, 1 >, MatrixSize< 1, 1 > >::type, typename Product< ValueType< ValueLhsT_ >, ValueType< ValueRhsT_ > >::type, typename Product< AccessImplementation< ImplementationLhsT_ >, ValueRhsT_ >::type > type
ValuesWrapper< MatrixSize< RowsV_, ColsV_ >, ValueOtherT_, typename Rebind< AccessImplementation< ImplementationT_ >, ValueOtherT_, RowsV_, ColsV_ >::type > 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
ValuesWrapper< typename Sum< MatrixSizeLhsT_, MatrixSizeRhsT_ >::type, typename Sum< ValueType< ValueLhsT_ >, ValueType< ValueRhsT_ > >::type, typename Sum< AccessImplementation< ImplementationLhsT_ >, AccessImplementation< ImplementationRhsT_ > >::type > type
ValuesWrapper< typename Transpose< MatrixSizeT_ >::type, ValueT_, typename Transpose< AccessImplementation< ImplementationT_ > >::type > type
Wrapper for partial specializations of trait for values types.
Definition helpers.hpp:110