cMHN 1.1
C++ library for learning MHNs with pRC
Loading...
Searching...
No Matches
assignable.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: BSD-2-Clause
2
3#ifndef pRC_CORE_TENSOR_VIEWS_ASSIGNABLE_H
4#define pRC_CORE_TENSOR_VIEWS_ASSIGNABLE_H
5
17
18namespace pRC::TensorViews
19{
20 template<class T, class N, class F>
21 class Assignable : public View<T, N, F>
22 {
23 private:
24 using Base = View<T, N, F>;
25
26 public:
27 constexpr auto &operator=(Zero<> const)
28 {
29 return *this = zero<F>();
30 }
31
32 constexpr auto &operator=(Unit<> const)
33 {
34 return *this = unit<F>();
35 }
36
37 constexpr auto &operator=(Identity<> const)
38 {
39 return *this = identity<F>();
40 }
41
42 template<class X, class R = RemoveReference<X>,
43 If<All<IsConvertible<R, T>,
44 IsSatisfied<(typename Base::Dimension() == 0)>>> = 0>
45 constexpr auto &operator=(X &&value)
46 {
47 (*this)() = forward<X>(value);
48 return this->self();
49 }
50
51 template<class X, class R = RemoveReference<X>, If<IsTensorish<R>> = 0,
52 If<IsSame<typename Base::Sizes, typename R::Sizes>> = 0,
53 If<IsConvertible<typename R::Type, T>> = 0>
54 constexpr auto &operator=(X &&rhs)
55 {
56 if constexpr(IsSubscriptable<Base>() && IsSubscriptable<R>())
57 {
59 [this, &rhs](auto const i)
60 {
61 (*this)[i] = forward<X>(rhs)[i];
62 });
63 }
64 else
65 {
67 [this, &rhs](auto const... indices)
68 {
69 (*this)(indices...) = forward<X>(rhs)(indices...);
70 });
71 }
72 return this->self();
73 }
74
75 template<class X, If<IsInvocable<Add, F, X>> = 0>
76 constexpr auto operator+=(X &&rhs)
77 {
78 this->self() = this->self() + forward<X>(rhs);
79 return this->self();
80 }
81
82 template<class X, If<IsInvocable<Sub, F, X>> = 0>
83 constexpr auto operator-=(X &&rhs)
84 {
85 this->self() = this->self() - forward<X>(rhs);
86 return this->self();
87 }
88
89 template<class X, If<IsInvocable<Mul, X, F>> = 0>
90 constexpr auto applyOnTheLeft(X &&lhs)
91 {
92 using U = RemoveReference<X>;
93
94 if constexpr(!IsTensorish<U>())
95 {
96 this->self() = forward<X>(lhs) * this->self();
97 return this->self();
98 }
99
100 if constexpr(IsTensorish<U>())
101 {
102 if constexpr(typename U::Dimension() == 0)
103 {
104 this->self() = eval(forward<X>(lhs)) * this->self();
105 return this->self();
106 }
107 }
108
109 this->self() = eval(forward<X>(lhs) * this->self());
110 return this->self();
111 }
112
113 template<class X, If<IsInvocable<Mul, F, X>> = 0>
114 constexpr auto applyOnTheRight(X &&rhs)
115 {
116 using U = RemoveReference<X>;
117
118 if constexpr(!IsTensorish<U>())
119 {
120 this->self() = this->self() * forward<X>(rhs);
121 return this->self();
122 }
123
124 if constexpr(IsTensorish<U>())
125 {
126 if constexpr(typename U::Dimension() == 0)
127 {
128 this->self() = this->self() * eval(forward<X>(rhs));
129 return this->self();
130 }
131 }
132
133 this->self() = eval(this->self() * forward<X>(rhs));
134 return this->self();
135 }
136
137 template<class X, If<IsInvocable<Mul, F, X>> = 0>
138 constexpr auto operator*=(X &&rhs)
139 {
140 return applyOnTheRight(forward<X>(rhs));
141 }
142
143 template<class X, If<IsInvocable<Div, F, X>> = 0>
144 constexpr auto operator/=(X &&rhs)
145 {
146 this->self() = this->self() / forward<X>(rhs);
147 return this->self();
148 }
149
150 protected:
151 ~Assignable() = default;
152 constexpr Assignable(Assignable const &) = default;
153 constexpr Assignable(Assignable &&) = default;
154 constexpr Assignable &operator=(Assignable const &) = delete;
155 constexpr Assignable &operator=(Assignable &&) = delete;
156 constexpr Assignable() = default;
157 };
158}
159#endif // pRC_CORE_TENSOR_VIEWS_ASSIGNABLE_H
Definition assignable.hpp:22
constexpr auto operator/=(X &&rhs)
Definition assignable.hpp:144
constexpr auto operator+=(X &&rhs)
Definition assignable.hpp:76
constexpr Assignable()=default
constexpr Assignable(Assignable const &)=default
constexpr Assignable(Assignable &&)=default
constexpr auto & operator=(Zero<> const)
Definition assignable.hpp:27
constexpr auto & operator=(X &&rhs)
Definition assignable.hpp:54
constexpr auto operator*=(X &&rhs)
Definition assignable.hpp:138
constexpr auto & operator=(Identity<> const)
Definition assignable.hpp:37
constexpr auto & operator=(X &&value)
Definition assignable.hpp:45
constexpr Assignable & operator=(Assignable &&)=delete
constexpr auto operator-=(X &&rhs)
Definition assignable.hpp:83
constexpr auto applyOnTheLeft(X &&lhs)
Definition assignable.hpp:90
constexpr auto & operator=(Unit<> const)
Definition assignable.hpp:32
constexpr Assignable & operator=(Assignable const &)=delete
constexpr auto applyOnTheRight(X &&rhs)
Definition assignable.hpp:114
Definition type_traits.hpp:32
Definition diagonal.hpp:11
static constexpr X eval(X &&a)
Definition eval.hpp:11
static constexpr auto makeConstantSequence()
Definition sequence.hpp:402
std::remove_reference_t< T > RemoveReference
Definition type_traits.hpp:56
Definition type_traits.hpp:262
Definition type_traits.hpp:265
Definition type_traits.hpp:268