codac 2.0.0
Loading...
Searching...
No Matches
codac2_sympy_bridge.h
Go to the documentation of this file.
1
9
10#pragma once
11
12#include <functional>
13#include <pybind11/embed.h>
14
15#include "codac2_sympy.h"
17
18namespace codac2::symbolic::detail
19{
28 {
29 public:
30
36 explicit FlatSymbolTable(const FunctionArgsList& args);
37
44 pybind11::object by_flat_index(Index k) const;
45
52 ScalarExpr codac_expr_by_name(const std::string& name) const;
53
59 Index size() const;
60
67 pybind11::object for_scalar_var(const ScalarVar& x) const;
68
76 pybind11::object for_vector_component(const VectorVar& x, Index i) const;
77
86 pybind11::object for_matrix_component(const MatrixVar& x, Index i, Index j) const;
87
88 private:
89
96 static std::string make_symbol_name(Index flat_index);
97
104 const FlatInputBinding& binding_of(const ExprID& id) const;
105
106 FlatInputLayout _layout;
107 std::vector<std::string> _names;
108 std::unordered_map<std::string,ScalarExpr> _codac_scalars;
109 };
110
115 {
116 public:
117
123 explicit SympyExporter(const FlatSymbolTable& symbols);
124
131 pybind11::object export_scalar(const ScalarExpr& e) const;
132
133 private:
134
135 pybind11::object export_node(const std::shared_ptr<ExprBase>& e) const;
136 pybind11::object export_vector_component(const std::shared_ptr<ExprBase>& e, Index i) const;
137 pybind11::object export_matrix_component(const std::shared_ptr<ExprBase>& e, Index i, Index j) const;
138 static double scalar_const_value(const ConstValueExpr<ScalarType>& c);
139 static std::shared_ptr<ExprBase> child_at(const std::vector<std::shared_ptr<ExprBase>>& children, Index i);
140
141 const FlatSymbolTable& _symbols;
142 };
143
148 {
149 public:
150
156 explicit SympyImporter(const FlatSymbolTable& symbols);
157
164 ScalarExpr import_scalar(const pybind11::handle& obj) const;
165
166 private:
167
168 ScalarExpr import_add(const pybind11::handle& obj) const;
169 ScalarExpr import_mul(const pybind11::handle& obj) const;
170 ScalarExpr import_pow(const pybind11::handle& obj) const;
171 ScalarExpr import_function(const pybind11::handle& obj) const;
172
173 const FlatSymbolTable& _symbols;
174 };
175
179 using SympyTransform = std::function<pybind11::object(
180 const pybind11::object&, const pybind11::object&, const FlatSymbolTable&)>;
181
186 {
187 public:
188
195
201 const pybind11::object& sympy() const;
202
208 const FlatSymbolTable& symbols() const;
209
216 pybind11::object export_scalar(const ScalarExpr& y) const;
217
224 ScalarExpr import_scalar(const pybind11::handle& obj) const;
225
234 ScalarExpr transform_scalar_expr(const ScalarExpr& y, const SympyTransform& transform, bool do_expand = true) const;
235
236 private:
237
238 FlatSymbolTable _symbols;
239 SympyExporter _exporter;
240 SympyImporter _importer;
241 };
242
247
256 const pybind11::object& import_module(const char* module_name);
257
263 const pybind11::object& import_sympy();
264
270 const pybind11::object& import_polyfuncs();
271
277 const pybind11::object& import_builtins();
278
287 pybind11::object normalize_sympy_expr(const pybind11::object& sympy, pybind11::object expr, bool do_expand = true);
288
299 const FunctionArgsList& args,
300 const ScalarExpr& y,
301 const SympyTransform& transform,
302 bool do_expand = true);
303}
A class representing a unique identifier for expressions.
Definition codac2_ExprBase.h:34
Flattened layout associated with an analytic function input domain.
Definition codac2_analytic_flat_input_layout.h:75
A container class to manage a collection of function arguments.
Definition codac2_FunctionArgsList.h:25
Symbol table associated with a flattened analytic input domain.
Definition codac2_sympy_bridge.h:28
pybind11::object for_scalar_var(const ScalarVar &x) const
Returns the SymPy symbol associated with a scalar variable.
ScalarExpr codac_expr_by_name(const std::string &name) const
Returns the Codac scalar expression associated with a symbol name.
pybind11::object by_flat_index(Index k) const
Returns the SymPy symbol associated with a flat input index.
FlatSymbolTable(const FunctionArgsList &args)
Builds the symbol table associated with a function argument list.
pybind11::object for_vector_component(const VectorVar &x, Index i) const
Returns the SymPy symbol associated with a vector component.
Index size() const
Returns the number of scalar entries in the flattened input domain.
pybind11::object for_matrix_component(const MatrixVar &x, Index i, Index j) const
Returns the SymPy symbol associated with a matrix component.
ScalarExpr transform_scalar_expr(const ScalarExpr &y, const SympyTransform &transform, bool do_expand=true) const
Applies a SymPy transformation to a Codac scalar expression.
pybind11::object export_scalar(const ScalarExpr &y) const
Exports a Codac scalar expression to SymPy.
ScalarExpr import_scalar(const pybind11::handle &obj) const
Imports a SymPy scalar expression into Codac.
const pybind11::object & sympy() const
Returns the imported SymPy module.
ScalarBridgeContext(const FunctionArgsList &args)
Builds a scalar bridge context for a given function argument list.
const FlatSymbolTable & symbols() const
Returns the associated flat symbol table.
Exports Codac scalar expressions to SymPy expressions.
Definition codac2_sympy_bridge.h:115
SympyExporter(const FlatSymbolTable &symbols)
Builds an exporter associated with a symbol table.
pybind11::object export_scalar(const ScalarExpr &e) const
Exports a Codac scalar expression to SymPy.
Imports SymPy scalar expressions into Codac scalar expressions.
Definition codac2_sympy_bridge.h:148
SympyImporter(const FlatSymbolTable &symbols)
Builds an importer associated with a symbol table.
ScalarExpr import_scalar(const pybind11::handle &obj) const
Imports a SymPy scalar expression.
pybind11::object normalize_sympy_expr(const pybind11::object &sympy, pybind11::object expr, bool do_expand=true)
Normalizes a SymPy expression before import into Codac.
const pybind11::object & import_polyfuncs()
Imports the SymPy polyfuncs module.
const pybind11::object & import_module(const char *module_name)
Imports a Python module.
std::function< pybind11::object( const pybind11::object &, const pybind11::object &, const FlatSymbolTable &)> SympyTransform
SymPy transformation applied to an exported scalar expression.
Definition codac2_sympy_bridge.h:179
const pybind11::object & import_builtins()
Imports the Python builtins module.
ScalarExpr transform_scalar_expr(const FunctionArgsList &args, const ScalarExpr &y, const SympyTransform &transform, bool do_expand=true)
Applies a SymPy transformation to a Codac scalar expression.
const pybind11::object & import_sympy()
Imports the SymPy module.
void ensure_python_runtime()
Ensures that the embedded Python runtime is initialized.
Binding information associated with one input argument in a flattened input domain.
Definition codac2_analytic_flat_input_layout.h:39