codac 1.5.6
Loading...
Searching...
No Matches
codac2_AnalyticFunction.h
Go to the documentation of this file.
1
9
10#pragma once
11
12#include <map>
13#include "codac2_AnalyticExpr.h"
14#include "codac2_Domain.h"
16#include "codac2_FunctionBase.h"
19#include "codac2_operators.h"
20#include "codac2_cart_prod.h"
21#include "codac2_vec.h"
22
23namespace codac2
24{
25 enum class EvalMode
26 {
27 NATURAL = 0x01,
28 CENTERED = 0x02,
29 DEFAULT = 0x03 // corresponds to (NATURAL|CENTERED)
30 };
31
32 inline EvalMode operator&(EvalMode a, EvalMode b)
33 { return static_cast<EvalMode>(static_cast<int>(a) & static_cast<int>(b)); }
34
35 inline EvalMode operator|(EvalMode a, EvalMode b)
36 { return static_cast<EvalMode>(static_cast<int>(a) | static_cast<int>(b)); }
37
38 struct ScalarExprList : public AnalyticExprWrapper<VectorType>
39 {
40 // Mainly used to take advantage of initializer lists in AnalyticFunction constructors.
41 template<typename... S>
42 requires (std::is_same_v<typename ExprType<S>::Type,ScalarType> && ...)
43 ScalarExprList(const S&... y)
44 : AnalyticExprWrapper<VectorType>(vec(y...))
45 { }
46 };
47
48 template<typename T>
49 requires std::is_base_of_v<AnalyticTypeBase,T>
50 class AnalyticFunction : public FunctionBase<AnalyticExpr<T>>
51 {
52 public:
53
54 AnalyticFunction(const FunctionArgsList& args, const ScalarExprList& y)
55 requires(std::is_same_v<T,VectorType>)
56 : FunctionBase<AnalyticExpr<T>>(args, y)
57 {
58 assert_release(y->belongs_to_args_list(this->args()) &&
59 "Invalid argument: variable not present in input arguments");
60 update_var_names();
61 }
62
63 AnalyticFunction(const FunctionArgsList& args, const AnalyticExprWrapper<T>& y)
64 : FunctionBase<AnalyticExpr<T>>(args, y)
65 {
66 assert_release(y->belongs_to_args_list(this->args()) &&
67 "Invalid argument: variable not present in input arguments");
68 update_var_names();
69 }
70
71 AnalyticFunction(const AnalyticFunction<T>& f)
72 : FunctionBase<AnalyticExpr<T>>(f)
73 { }
74
75 template<typename... X>
76 AnalyticExprWrapper<T> operator()(const X&... x) const
77 {
78 return { this->FunctionBase<AnalyticExpr<T>>::operator()(x...) };
79 }
80
81 template<typename... Args>
82 auto real_eval(const Args&... x) const
83 {
84 return eval(x...).mid();
85 }
86
87 template<typename... Args>
88 typename T::Domain eval(const EvalMode& m, const Args&... x) const
89 {
90 check_valid_inputs(x...);
91
92 switch(m)
93 {
94 case EvalMode::NATURAL:
95 {
96 return eval_<true>(x...).a;
97 }
98
99 case EvalMode::CENTERED:
100 {
101 auto x_ = eval_<false>(x...);
102 auto flatten_x = IntervalVector(cart_prod(x...));
103 assert(x_.da.rows() == x_.a.size() && x_.da.cols() == flatten_x.size());
104
105 if constexpr(std::is_same_v<T,ScalarType>)
106 return x_.m + (x_.da*(flatten_x-flatten_x.mid()))[0];
107
108 else if constexpr(std::is_same_v<T,VectorType>)
109 return x_.m + (x_.da*(flatten_x-flatten_x.mid())).col(0);
110
111 else
112 {
113 static_assert(std::is_same_v<T,MatrixType>);
114 return x_.m + (x_.da*(flatten_x-flatten_x.mid()))
115 .reshaped(x_.m.rows(), x_.m.cols());
116 }
117 }
118
119 case EvalMode::DEFAULT:
120 default:
121 {
122 auto x_ = eval_<false>(x...);
123
124 // If the centered form is not available for this expression...
125 if(x_.da.size() == 0 // .. because some parts have not yet been implemented,
126 || !x_.def_domain) // .. or due to restrictions in the derivative definition domain
127 return eval(EvalMode::NATURAL, x...);
128
129 else
130 {
131 auto flatten_x = IntervalVector(cart_prod(x...));
132
133 if constexpr(std::is_same_v<T,ScalarType>)
134 return x_.a & (x_.m + (x_.da*(flatten_x-flatten_x.mid()))[0]);
135
136 else if constexpr(std::is_same_v<T,VectorType>)
137 {
138 assert(x_.da.rows() == x_.a.size() && x_.da.cols() == flatten_x.size());
139 return x_.a & (x_.m + (x_.da*(flatten_x-flatten_x.mid())).col(0));
140 }
141
142 else
143 {
144 static_assert(std::is_same_v<T,MatrixType>);
145 assert(x_.da.rows() == x_.a.size() && x_.da.cols() == flatten_x.size());
146 return x_.a & (x_.m +(x_.da*(flatten_x-flatten_x.mid()))
147 .reshaped(x_.m.rows(),x_.m.cols()));
148 }
149 }
150 }
151 }
152 }
153
154 template<typename... Args>
155 typename T::Domain eval(const Args&... x) const
156 {
157 return eval(EvalMode::NATURAL | EvalMode::CENTERED, x...);
158 }
159
160 template<typename... Args>
161 auto diff(const Args&... x) const
162 {
163 check_valid_inputs(x...);
164 return eval_<false>(x...).da;
165 }
166
167 Index output_size() const
168 {
169 if constexpr(std::is_same_v<T,ScalarType>)
170 return 1;
171
172 else {
173 std::pair<Index,Index> oshape = output_shape();
174 return oshape.first * oshape.second;
175 }
176 }
177
178 std::pair<Index,Index> output_shape() const
179 {
180 if constexpr(std::is_same_v<T,ScalarType>)
181 return {1,1};
182 else return this->expr()->output_shape();
183 }
184
185 friend std::ostream& operator<<(std::ostream& os, [[maybe_unused]] const AnalyticFunction<T>& f)
186 {
187 os << "(";
188 for(size_t i = 0 ; i < f.args().size() ; i++)
189 os << (i!=0 ? "," : "") << f.args()[i]->name();
190 os << ") ↦ " << f.expr()->str();
191 return os;
192 }
193
194 // not working with Clang: template<typename Y, typename... X>
195 // not working with Clang: requires (sizeof...(X) > 0)
196 // not working with Clang: friend class CtcInverse;
197
198 // So, the following methods are temporarily public
199
200 // protected:
201
202 template<typename... Args>
203 void fill_from_args(ValuesMap& v, const Args&... x) const
204 {
205 Index i = 0;
206 (add_value_to_arg_map(v, x, i++), ...);
207 }
208
209 template<typename... Args>
210 void intersect_from_args(const ValuesMap& v, Args&... x) const
211 {
212 Index i = 0;
213 (intersect_value_from_arg_map(v, x, i++), ...);
214 }
215
216 protected:
217
218 template<typename D>
219 void add_value_to_arg_map(ValuesMap& v, const D& x, Index i) const
220 {
221 assert(i >= 0 && i < (Index)this->args().size());
222 assert_release(size_of(x) == this->args()[i]->size() && "provided arguments do not match function inputs");
223
224 using D_TYPE = typename ExprType<D>::Type;
225
226 IntervalMatrix d = IntervalMatrix::zero(size_of(x), this->args().total_size());
227
228 Index p = 0;
229 for(Index j = 0 ; j < i ; j++)
230 p += this->args()[j]->size();
231
232 for(Index k = p ; k < p+size_of(x) ; k++)
233 d(k-p,k) = 1.;
234
235 v[this->args()[i]->unique_id()] =
236 std::make_shared<D_TYPE>(typename D_TYPE::Domain(x).mid(), x, d, true);
237 }
238
239 template<typename D>
240 void intersect_value_from_arg_map(const ValuesMap& v, D& x, Index i) const
241 {
242 assert(v.find(this->args()[i]->unique_id()) != v.end() && "argument cannot be found");
243 x &= std::dynamic_pointer_cast<typename ExprType<D>::Type>(v.at(this->args()[i]->unique_id()))->a;
244 }
245
246 template<bool NATURAL_EVAL,typename... Args>
247 auto eval_(const Args&... x) const
248 {
249 ValuesMap v;
250
251 if constexpr(sizeof...(Args) == 0)
252 return this->expr()->fwd_eval(v, 0, NATURAL_EVAL);
253
254 else
255 {
256 fill_from_args(v, x...);
257 return this->expr()->fwd_eval(v, cart_prod(x...).size(), NATURAL_EVAL); // todo: improve size computation
258 }
259 }
260
261 template<typename... Args>
262 void check_valid_inputs(const Args&... x) const
263 {
264 [[maybe_unused]] Index n = 0;
265 ((n += size_of(x)), ...);
266
267 assert_release(this->_args.total_size() == n &&
268 "Invalid arguments: wrong number of input arguments");
269 }
270
271 inline void update_var_names()
272 {
273 for(const auto& v : this->_args) // variable names are automatically computed in FunctionArgsList,
274 // so we propagate them to the expression
275 this->_y->replace_arg(v->unique_id(), std::dynamic_pointer_cast<ExprBase>(v));
276 }
277 };
278
279 AnalyticFunction(const FunctionArgsList&, std::initializer_list<ScalarExpr>) ->
280 AnalyticFunction<VectorType>;
281
282 template<typename T>
283 AnalyticFunction(const FunctionArgsList&, const T&) ->
284 AnalyticFunction<typename ExprType<T>::Type>;
285
286}
A container class to manage a collection of function arguments.
Definition codac2_FunctionArgsList.h:25
A base class for functions (either analytic functions, or set functions).
Definition codac2_FunctionBase.h:41
const FunctionArgsList & args() const
Definition codac2_FunctionBase.h:93
const FunctionArgsList _args
Definition codac2_FunctionBase.h:208
FunctionBase(const std::vector< std::reference_wrapper< VarBase > > &args, const std::shared_ptr< AnalyticExpr< T > > &y)
Definition codac2_FunctionBase.h:52
const std::shared_ptr< AnalyticExpr< T > > _y
Definition codac2_FunctionBase.h:207
const std::shared_ptr< AnalyticExpr< T > > & expr() const
Definition codac2_FunctionBase.h:103