codac 2.0.0
Loading...
Searching...
No Matches
codac2_analytic_flat_input_layout.h
Go to the documentation of this file.
1
9
10#pragma once
11
12#include <typeindex>
13#include <unordered_map>
14
16#include "codac2_AnalyticType.h"
17#include "codac2_ExprBase.h"
19
20namespace codac2
21{
22 class ScalarVar;
23 class VectorVar;
24 class MatrixVar;
25
39 {
40 std::type_index type = typeid(ScalarType);
41 Index offset = 0;
42 Index rows = 1;
43 Index cols = 1;
44
50 bool is_scalar() const;
51
57 bool is_vector() const;
58
64 bool is_matrix() const;
65 };
66
75 {
76 public:
77
83 explicit FlatInputLayout(const FunctionArgsList& args);
84
90 Index size() const;
91
100 bool same_domain_as(const FlatInputLayout& other) const;
101
108 bool same_domain_as(const FunctionArgsList& other_args) const;
109
119 Index flat_index_of(const ScalarVar& x) const;
120
128 Index flat_index_of(const VectorVar& x, Index i) const;
129
138 Index flat_index_of(const MatrixVar& x, Index i, Index j) const;
139
152 bool flat_index_of(const ScalarExpr& x, Index& flat_index) const;
153
160 const FlatInputBinding& binding_of(const ExprID& id) const;
161
168 const FlatInputBinding* find_binding(const ExprID& id) const;
169
170 private:
171
172 std::unordered_map<Index,FlatInputBinding> _bindings;
173 Index _size = 0;
174 };
175
176 template<typename T>
177 static std::shared_ptr<ExprBase> as_expr_base(const AnalyticExprWrapper<T>& e)
178 {
179 return std::static_pointer_cast<ExprBase>(
180 std::shared_ptr<AnalyticExpr<T>>(e)
181 );
182 }
183
184 template<typename T>
185 requires std::is_base_of_v<AnalyticTypeBase,T>
186 class AnalyticFunction;
187
188 template<typename T>
189 inline AnalyticFunction<T> unaryize_function(const AnalyticFunction<T>& f)
190 {
191 if(f.nb_args() == 0 || (f.nb_args() == 1 && std::dynamic_pointer_cast<VectorVar>(f.args()[0])))
192 return f;
193
194 FlatInputLayout layout(f.args());
195 VectorVar flat_x(layout.size(), "x");
196
197 auto y = std::dynamic_pointer_cast<AnalyticExpr<T>>(f.expr()->copy());
198 assert(y && "unaryize_function: unable to copy analytic expression");
199
200 for(const auto& arg : f.args())
201 {
202 const auto& b = layout.binding_of(arg->unique_id());
203
204 if(std::dynamic_pointer_cast<ScalarVar>(arg))
205 y->replace_arg(arg->unique_id(), as_expr_base(flat_x[b.offset]));
206
207 else if(std::dynamic_pointer_cast<VectorVar>(arg))
208 {
209 assert(b.cols == 1 && "unaryize_function: invalid flat binding for vector input");
210 y->replace_arg(
211 arg->unique_id(),
212 as_expr_base(flat_x.subvector(b.offset, b.offset + b.rows - 1))
213 );
214 }
215
216 else
217 {
218 assert(false && "unaryize_function: only scalar/vector input arguments are currently supported");
219 }
220 }
221
222 return AnalyticFunction<T>({flat_x}, y);
223 }
224}
A class representing a unique identifier for expressions.
Definition codac2_ExprBase.h:34
bool same_domain_as(const FlatInputLayout &other) const
Tests whether two layouts describe the same flattened input domain.
Index flat_index_of(const VectorVar &x, Index i) const
Returns the flat index associated with a direct component of a vector input variable.
FlatInputLayout(const FunctionArgsList &args)
Builds the flattened layout associated with a function argument list.
Index flat_index_of(const ScalarVar &x) const
Returns the flat index associated with a scalar input variable.
Index size() const
Returns the total number of scalar inputs in the flattened domain.
Index flat_index_of(const MatrixVar &x, Index i, Index j) const
Returns the flat index associated with a direct component of a matrix input variable.
bool flat_index_of(const ScalarExpr &x, Index &flat_index) const
Tries to resolve a scalar input expression into a flat input index.
const FlatInputBinding * find_binding(const ExprID &id) const
Returns the binding associated with an input expression identifier, if any.
bool same_domain_as(const FunctionArgsList &other_args) const
Tests whether this layout matches the flattened domain induced by a function argument list.
const FlatInputBinding & binding_of(const ExprID &id) const
Returns the binding associated with an input expression identifier.
A container class to manage a collection of function arguments.
Definition codac2_FunctionArgsList.h:25
Definition codac2_OctaSym.h:21
Binding information associated with one input argument in a flattened input domain.
Definition codac2_analytic_flat_input_layout.h:39
bool is_vector() const
Tests whether the binding corresponds to a vector input argument.
Index rows
Number of rows of the argument block.
Definition codac2_analytic_flat_input_layout.h:42
std::type_index type
Analytic type of the bound input argument.
Definition codac2_analytic_flat_input_layout.h:40
bool is_scalar() const
Tests whether the binding corresponds to a scalar input argument.
Index cols
Number of columns of the argument block.
Definition codac2_analytic_flat_input_layout.h:43
bool is_matrix() const
Tests whether the binding corresponds to a matrix input argument.
Index offset
First scalar index of the argument in the flattened domain.
Definition codac2_analytic_flat_input_layout.h:41