cMHN 1.2
C++ library for learning MHNs with pRC
Loading...
Searching...
No Matches
reduce.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: BSD-2-Clause
2
3#ifndef pRC_CORE_TENSOR_VIEWS_REDUCE_H
4#define pRC_CORE_TENSOR_VIEWS_REDUCE_H
5
10
11namespace pRC::TensorViews
12{
13 template<class T, class N, class F, class R, class V>
14 class Reduce;
15
16 template<class T, class N, class F, Index... Rs, class V>
17 requires IsTensorView<V>
18 class Reduce<T, N, F, Sequence<Index, Rs...>, V>
19 : public View<T, N, Reduce<T, N, F, Sequence<Index, Rs...>, V>>
20 {
21 private:
23
24 public:
25 template<class X>
27 Reduce(X &&a)
28 : mA(forward<X>(a))
29 {
30 }
31
32 template<IsConvertible<Index>... Is>
33 requires(sizeof...(Is) == Base::Dimension)
34 constexpr decltype(auto) operator()(Is const... indices)
35 {
36 auto c = F::template Identity<T>();
37
38 if constexpr(Base::Dimension == 0)
39 {
40 if constexpr(IsSubscriptable<V>)
41 {
43 [this, &c](auto const i)
44 {
45 c = F()(c, mA[i]);
46 });
47 }
48 else
49 {
51 [this, &c](auto const... loop)
52 {
53 c = F()(c, mA(loop...));
54 });
55 }
56 }
57 else
58 {
59 range<Sizes<V::size(Rs)...>>(
60 [this, &c, indices...](auto const... loop)
61 {
62 c = F()(c, chip<Rs...>(mA, loop...)(indices...));
63 });
64 }
65
66 return c;
67 }
68
69 template<IsConvertible<Index>... Is>
70 requires(sizeof...(Is) == Base::Dimension)
71 constexpr decltype(auto) operator()(Is const... indices) const
72 {
73 auto c = F::template Identity<T>();
74
75 if constexpr(Base::Dimension == 0)
76 {
77 if constexpr(IsSubscriptable<V>)
78 {
80 [this, &c](auto const i)
81 {
82 c = F()(c, mA[i]);
83 });
84 }
85 else
86 {
88 [this, &c](auto const... loop)
89 {
90 c = F()(c, mA(loop...));
91 });
92 }
93 }
94 else
95 {
96 range<Sizes<V::size(Rs)...>>(
97 [this, &c, indices...](auto const... loop)
98 {
99 c = F()(c, chip<Rs...>(mA, loop...)(indices...));
100 });
101 }
102
103 return c;
104 }
105
106 constexpr decltype(auto) operator()(
107 typename Base::Subscripts const &subscripts)
108 {
109 return this->call(subscripts);
110 }
111
112 constexpr decltype(auto) operator()(
113 typename Base::Subscripts const &subscripts) const
114 {
115 return this->call(subscripts);
116 }
117
118 constexpr decltype(auto) operator[](Index const index) = delete;
119 constexpr decltype(auto) operator[](Index const index) const = delete;
120
121 private:
122 V mA;
123 };
124}
125#endif // pRC_CORE_TENSOR_VIEWS_REDUCE_H
Definition value.hpp:12
Definition sequence.hpp:29
Definition reduce.hpp:14
Definition declarations.hpp:20
Definition concepts.hpp:28
Definition subscript.hpp:21
Definition declarations.hpp:36
pRC::Float<> T
Definition externs_nonTT.hpp:1
int i
Definition gmock-matchers-comparisons_test.cc:603
Definition declarations.hpp:18
Size Index
Definition basics.hpp:32
static constexpr auto loop(F &&f, Xs &&...args)
Definition loop.hpp:20
static constexpr auto chip(Sequence< T, Is... > const)
Definition sequence.hpp:584
static constexpr auto range(F &&f, Xs &&...args)
Definition range.hpp:18
Definition identity.hpp:11