11#include <blitz/array.h>
12#include <blitz/range.h>
20#include "wuzp/core/internal/operator_traits.hpp"
25template <
typename ValueT_,
int RowsV_,
int ColsV_>
28template <
int RowsV_,
int ColsV_>
30template <
int RowsV_,
int ColsV_>
32template <
int RowsV_,
int ColsV_>
35template <
typename ValueT_,
int RowsV_>
45template <
typename ValueT_>
53template <
typename UnitT_>
55template <
typename UnitT_>
57template <
typename UnitT_>
63struct IsBlitzArray : std::false_type {};
64template <
typename ValueT_>
65struct IsBlitzArray<blitz::Array<ValueT_, 2>> : std::true_type {};
66template <
typename ExpressionT_>
67struct IsBlitzArray<blitz::_bz_ArrayExpr<ExpressionT_>> : std::true_type {};
70template <
typename ImplementationT_>
71using ArrayOf = blitz::Array<typename ImplementationT_::T_numtype, 2>;
73template <
typename ImplementationT_>
74inline constexpr bool isArray = std::is_same_v<ImplementationT_, ArrayOf<ImplementationT_>>;
79template <
typename ImplementationT_>
80ArrayOf<ImplementationT_> copyToArray(
const ImplementationT_& impl) {
85 ArrayOf<ImplementationT_> result(impl.ubound(0) - impl.lbound(0) + 1, impl.ubound(1) - impl.lbound(1) + 1);
92template <
typename ImplementationT_>
93decltype(
auto) asArray(
const ImplementationT_& impl) {
94 if constexpr (isArray<ImplementationT_>) {
97 return copyToArray(impl);
102template <
typename ValueT_>
103void swapRows(blitz::Array<ValueT_, 2>& matrix,
const int row,
const int otherRow) {
104 for (
int col = 0; col < matrix.extent(1); ++col) {
105 std::swap(matrix(row, col), matrix(otherRow, col));
122template <
typename WorkT_,
typename ImplementationT_>
123std::pair<blitz::Array<WorkT_, 2>, WorkT_> gaussJordan(
const ImplementationT_& impl) {
124 const auto& matrix = asArray(impl);
125 const int size = matrix.extent(0);
126 blitz::Array<WorkT_, 2> reduced(size, size);
127 reduced = blitz::cast<WorkT_>(matrix);
128 blitz::Array<WorkT_, 2> inverse(size, size);
130 for (
int index = 0; index < size; ++index) {
131 inverse(index, index) = WorkT_{1};
133 WorkT_ determinant{1};
134 for (
int col = 0; col < size; ++col) {
136 for (
int row = col + 1; row < size; ++row) {
137 if (std::abs(reduced(row, col)) > std::abs(reduced(pivot, col))) {
141 if (reduced(pivot, col) == WorkT_{0}) {
142 inverse = std::numeric_limits<WorkT_>::quiet_NaN();
143 return {inverse, WorkT_{0}};
146 swapRows(reduced, pivot, col);
147 swapRows(inverse, pivot, col);
148 determinant = -determinant;
150 const WorkT_ pivotValue = reduced(col, col);
151 determinant *= pivotValue;
153 for (
int otherCol = col; otherCol < size; ++otherCol) {
154 reduced(col, otherCol) /= pivotValue;
156 for (
int otherCol = 0; otherCol < size; ++otherCol) {
157 inverse(col, otherCol) /= pivotValue;
159 for (
int row = 0; row < size; ++row) {
161 const WorkT_ factor = reduced(row, col);
162 for (
int otherCol = col; otherCol < size; ++otherCol) {
163 reduced(row, otherCol) -= factor * reduced(col, otherCol);
165 for (
int otherCol = 0; otherCol < size; ++otherCol) {
166 inverse(row, otherCol) -= factor * inverse(col, otherCol);
171 return {inverse, determinant};
187struct ReserveMemory<ImplementationT_, MatrixSize<SizesV_...>> {
188 static void run(ImplementationT_& impl) { impl.resize(SizesV_...); }
192struct Initialize<ImplementationT_, std::tuple<Arg0T_, ArgsT_...>> {
193 static void run(ImplementationT_& impl, Arg0T_&& arg0, ArgsT_&&... args) {
194 applyCommas((impl = std::forward<Arg0T_>(arg0)), std::forward<ArgsT_>(args)...);
199template <wuzp_blitz::BlitzArray ImplementationT_,
typename ValueT_,
int RowsV_,
int ColsV_>
200struct Rebind<AccessImplementation<ImplementationT_>, ValueT_, RowsV_, ColsV_> {
201 using type = blitz::Array<ValueT_, 2>;
206template <wuzp_blitz::BlitzArray ImplementationT_>
208 static auto get(
const ImplementationT_& impl,
const int row,
const int col) {
return impl(row, col); }
210 static void set(ImplementationT_& impl,
const int row,
const int col,
const typename ImplementationT_::T_numtype value) {
211 impl(row, col) = value;
215template <wuzp_blitz::BlitzArray ImplementationLhsT_, wuzp_blitz::BlitzArray ImplementationRhsT_>
216struct Comparison<AccessImplementation<ImplementationLhsT_>, AccessImplementation<ImplementationRhsT_>> {
217 static bool equal(
const ImplementationLhsT_& lhs,
const ImplementationRhsT_& rhs) {
218 return blitz::all(blitz::Array<bool, 2>(lhs == rhs));
221 static bool approxEqual(
const ImplementationLhsT_& lhs,
const ImplementationRhsT_& rhs,
222 const typename ImplementationLhsT_::T_numtype tolerance) {
223 const auto differenceNorm = std::sqrt(blitz::sum(blitz::pow2(lhs - rhs)));
224 const auto lhsNorm = std::sqrt(blitz::sum(blitz::pow2(lhs)));
225 const auto rhsNorm = std::sqrt(blitz::sum(blitz::pow2(rhs)));
226 return differenceNorm <= tolerance * std::min(lhsNorm, rhsNorm);
232template <wuzp_blitz::BlitzArray ImplementationT_>
233struct Copy<AccessImplementation<ImplementationT_>> {
234 static ImplementationT_
value(
const ImplementationT_& impl) {
235 if constexpr (wuzp_blitz::internal::isArray<ImplementationT_>) {
236 return wuzp_blitz::internal::copyToArray(impl);
247template <wuzp_blitz::BlitzArray ImplementationT_>
248struct Move<AccessImplementation<ImplementationT_>> {
249 static ImplementationT_
construct(ImplementationT_& source) {
250 if constexpr (wuzp_blitz::internal::isArray<ImplementationT_>) {
251 ImplementationT_ result(source);
259 static void assign(ImplementationT_& target, ImplementationT_& source) {
260 target.reference(source);
265template <wuzp_blitz::BlitzArray ImplementationT_>
266struct Transpose<AccessImplementation<ImplementationT_>> {
269 static auto value(
const ImplementationT_& impl) {
270 return wuzp_blitz::internal::copyToArray(wuzp_blitz::internal::asArray(impl).transpose(1, 0));
273 using type =
decltype(
value(std::declval<const ImplementationT_>()));
276template <wuzp_blitz::BlitzArray ImplementationLhsT_, wuzp_blitz::BlitzArray ImplementationRhsT_>
278 : internal::OperatorSum<ImplementationLhsT_, ImplementationRhsT_> {};
280template <wuzp_blitz::BlitzArray ImplementationLhsT_, wuzp_blitz::BlitzArray ImplementationRhsT_>
281struct Difference<AccessImplementation<ImplementationLhsT_>, AccessImplementation<ImplementationRhsT_>>
282 : internal::OperatorDifference<ImplementationLhsT_, ImplementationRhsT_> {};
284template <wuzp_blitz::BlitzArray ImplementationT_>
287template <wuzp_blitz::BlitzArray ImplementationLhsT_, ArithmeticValue ValueRhsT_>
288struct Product<
AccessImplementation<ImplementationLhsT_>, ValueRhsT_> : internal::OperatorProduct<ImplementationLhsT_, ValueRhsT_> {};
289template <wuzp_blitz::BlitzArray ImplementationLhsT_, wuzp_blitz::BlitzArray ImplementationRhsT_>
291 : internal::OperatorProduct<ImplementationLhsT_, ImplementationRhsT_> {};
293template <wuzp_blitz::BlitzArray ImplementationLhsT_, wuzp_blitz::BlitzArray ImplementationRhsT_>
295 static auto value(
const ImplementationLhsT_& implLhs,
const ImplementationRhsT_& implRhs) {
302 const auto& lhs = wuzp_blitz::internal::asArray(implLhs);
303 const auto& rhs = wuzp_blitz::internal::asArray(implRhs);
304 blitz::Array<ValueT, 2> result(lhs.extent(0), rhs.extent(1));
305 result = blitz::cast<ValueT>(
306 blitz::sum(lhs(blitz::tensor::i, blitz::tensor::k) * rhs(blitz::tensor::k, blitz::tensor::j), blitz::tensor::k));
310 using type =
decltype(
value(std::declval<const ImplementationLhsT_>(), std::declval<const ImplementationRhsT_>()));
313template <wuzp_blitz::BlitzArray ImplementationLhsT_, wuzp_blitz::BlitzArray ImplementationRhsT_>
315 static auto value(
const ImplementationLhsT_& implLhs,
const ImplementationRhsT_& implRhs) {
316 return implLhs(blitz::tensor::i, blitz::tensor::j) * implRhs(blitz::tensor::i, blitz::tensor::j);
319 using type =
decltype(
value(std::declval<const ImplementationLhsT_>(), std::declval<const ImplementationRhsT_>()));
322template <wuzp_blitz::BlitzArray ImplementationLhsT_, wuzp_blitz::BlitzArray ImplementationRhsT_>
324 static auto value(
const ImplementationLhsT_& implLhs,
const ImplementationRhsT_& implRhs) {
325 static_assert(std::is_same_v<typename ImplementationLhsT_::T_numtype, typename ImplementationRhsT_::T_numtype>,
326 "Numeric types do not match.");
329 const auto& lhs = wuzp_blitz::internal::asArray(implLhs);
330 const auto& rhs = wuzp_blitz::internal::asArray(implRhs);
331 blitz::Array<typename ImplementationLhsT_::T_numtype, 2> result{3, 1};
332 result(0, 0) = lhs(1, 0) * rhs(2, 0) - lhs(2, 0) * rhs(1, 0);
333 result(1, 0) = lhs(2, 0) * rhs(0, 0) - lhs(0, 0) * rhs(2, 0);
334 result(2, 0) = lhs(0, 0) * rhs(1, 0) - lhs(1, 0) * rhs(0, 0);
338 using type =
decltype(
value(std::declval<const ImplementationLhsT_>(), std::declval<const ImplementationRhsT_>()));
341template <wuzp_blitz::BlitzArray ImplementationLhsT_, wuzp_blitz::BlitzArray ImplementationRhsT_>
343 : internal::OperatorDivision<ImplementationLhsT_, ImplementationRhsT_> {};
344template <wuzp_blitz::BlitzArray ImplementationLhsT_, ArithmeticValue ValueRhsT_>
345struct Division<AccessImplementation<ImplementationLhsT_>, ValueRhsT_> : internal::OperatorDivision<ImplementationLhsT_, ValueRhsT_> {};
347template <wuzp_blitz::BlitzArray ImplementationT_>
349 static auto value(
const ImplementationT_& impl) {
354 using type =
decltype(
value(std::declval<const ImplementationT_>()));
357template <wuzp_blitz::BlitzArray ImplementationT_>
359 static auto value(
const ImplementationT_& impl) {
361 return static_cast<typename ImplementationT_::T_numtype
>(blitz::sum(impl * impl));
364 using type =
decltype(
value(std::declval<const ImplementationT_>()));
372template <wuzp_blitz::BlitzArray ImplementationT_>
374 static auto value(
const ImplementationT_& impl) {
375 using ValueT =
typename ImplementationT_::T_numtype;
376 if constexpr (std::is_floating_point_v<ValueT>) {
377 return wuzp_blitz::internal::gaussJordan<ValueT>(impl).second;
379 return static_cast<ValueT
>(std::llround(wuzp_blitz::internal::gaussJordan<double>(impl).second));
382 using type =
decltype(
value(std::declval<const ImplementationT_>()));
386template <wuzp_blitz::BlitzArray ImplementationT_>
388 static auto value(
const ImplementationT_& impl) {
389 return wuzp_blitz::internal::gaussJordan<typename ImplementationT_::T_numtype>(impl).first;
392 using type =
decltype(
value(std::declval<const ImplementationT_>()));
395template <wuzp_blitz::BlitzArray ImplementationT_>
397 static auto value(
const ImplementationT_& impl) {
return static_cast<typename ImplementationT_::T_numtype
>(1) / impl; }
399 using type =
decltype(
value(std::declval<const ImplementationT_>()));
405template <wuzp_blitz::BlitzArray ImplementationT_,
typename FunctionT_>
407 static auto value(
const ImplementationT_& impl,
const FunctionT_& function) {
409 const auto& array = wuzp_blitz::internal::asArray(impl);
410 wuzp_blitz::internal::ArrayOf<ImplementationT_> result(array.extent(0), array.extent(1));
411 for (
int row = 0; row < array.extent(0); ++row) {
412 for (
int col = 0; col < array.extent(1); ++col) {
413 result(row, col) = function(array(row, col));
419 using type =
decltype(
value(std::declval<const ImplementationT_>(), std::declval<const FunctionT_>()));
422template <wuzp_blitz::BlitzArray ImplementationT_,
int BlockRowIdV_,
int BlockColIdV_,
int BlockRowsV_,
int BlockColsV_>
425 static auto value(
const ImplementationT_& impl) {
426 return wuzp_blitz::internal::copyToArray(wuzp_blitz::internal::asArray(impl)(
427 blitz::Range(BlockRowIdV_, BlockRowIdV_ + BlockRowsV_ - 1), blitz::Range(BlockColIdV_, BlockColIdV_ + BlockColsV_ - 1)));
430 using type =
decltype(
value(std::declval<const ImplementationT_>()));
433template <wuzp_blitz::BlitzArray ImplementationT_>
435 static void run(ImplementationT_& impl) { impl = 0; }
438template <wuzp_blitz::BlitzArray ImplementationT_>
439struct Random<AccessImplementation<ImplementationT_>> {
441 static void run(ImplementationT_& impl) {
442 using ValueT =
typename ImplementationT_::T_numtype;
443 for (
int row = 0; row < impl.rows(); ++row) {
444 for (
int col = 0; col < impl.cols(); ++col) {
446 static_cast<ValueT
>(-1) +
static_cast<ValueT
>(2) *
static_cast<ValueT
>(std::rand()) /
static_cast<ValueT
>(RAND_MAX);
452template <wuzp_blitz::BlitzArray ImplementationT_>
453struct Evaluate<AccessImplementation<ImplementationT_>> {
455 static auto value(
const ImplementationT_& impl) {
return wuzp_blitz::internal::copyToArray(impl); }
456 using type =
decltype(
value(std::declval<const ImplementationT_>()));
Wrapper for the actual implementation of a value container.
The implementation types of the backend: Blitz++'s two-dimensional arrays and the expressions of arra...
wuzp::Quantity< ValueF, UnitT_ > QuantityF
ValueMatrix< double, RowsV_, ColsV_ > ValueMatrixD
wuzp::ValuesWrapper< wuzp::MatrixSize< RowsV_, ColsV_ >, ValueT_, blitz::Array< ValueT_, 2 > > ValueMatrix
ValueMatrix< int, RowsV_, ColsV_ > ValueMatrixI
ValueMatrix< ValueT_, 1, 1 > Value
wuzp::Quantity< ValueI, UnitT_ > QuantityI
A quantity with a value of this backend, e.g. QuantityD<wuzp_units::Length>.
ValueVector< double, RowsV_ > ValueVectorD
ValueVector< int, RowsV_ > ValueVectorI
ValueMatrix< float, RowsV_, ColsV_ > ValueMatrixF
ValueMatrix< ValueT_, RowsV_, 1 > ValueVector
ValueVector< float, RowsV_ > ValueVectorF
wuzp::Quantity< ValueD, UnitT_ > QuantityD
Quantities< ValueT_, TypeMatrixImpl< MatrixSize< 1, 1 >, std::tuple< UnitT_ > > > Quantity
Container to store a single quantity.
void applyCommas(First &&first, Rest &&... rest)
Applies the comma operator from left to right: applyCommas(a, b, c) evaluates ((a,...
Type wrapper which is used to access traits partially specialized in the implementation header files.
decltype(value(std::declval< const ImplementationT_ >())) type
static auto value(const ImplementationT_ &impl)
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_::T_numtype value)
Element access of a matrix of a value matrix backend: get(impl, row, col) returns an element,...
static bool equal(const ImplementationLhsT_ &lhs, const ImplementationRhsT_ &rhs)
static bool approxEqual(const ImplementationLhsT_ &lhs, const ImplementationRhsT_ &rhs, const typename ImplementationLhsT_::T_numtype tolerance)
static ImplementationT_ value(const ImplementationT_ &impl)
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,...
static auto value(const ImplementationT_ &impl)
decltype(value(std::declval< const ImplementationT_ >())) type
The elementwise reciprocal of a matrix, like Eigen's ArrayBase::inverse() or cwiseInverse().
static auto value(const ImplementationT_ &impl)
decltype(value(std::declval< const ImplementationT_ >())) type
static auto value(const ImplementationT_ &impl)
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)
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...
static void assign(ImplementationT_ &target, ImplementationT_ &source)
static ImplementationT_ construct(ImplementationT_ &source)
The negation is performed elementwise.
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
static void run(ImplementationT_ &impl)
Eigen::Array< ValueT_, RowsV_, ColsV_ > type
static void run(ImplementationT_ &impl)
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)
Wrapper for partial specializations of trait for values types.
static void run(ImplementationT_ &impl)