sympy
sympy¶
import tensorwaves.function.sympy
Lambdify sympy
expression trees to a Function
.
- create_function(expression: Expr, backend: str, max_complexity: Optional[int] = None, use_cse: bool = True) PositionalArgumentFunction [source]¶
- create_parametrized_function(expression: Expr, parameters: Mapping[Symbol, Union[complex, float]], backend: str, max_complexity: Optional[int] = None, use_cse: bool = True) ParametrizedBackendFunction [source]¶
- extract_constant_sub_expressions(expression: Expr, free_symbols: Iterable[Symbol], fix_order: bool = False) Tuple[Expr, Dict[Symbol, Expr]] [source]¶
Collapse and extract constant sub-expressions.
Along with
prepare_caching()
, this function prepares asympy.Expr
for caching during a fit procedure. The function returns a top expression where the constant sub-expressions have been substituted by new symbols \(f_i\) for each substituted sub-expression, and adict
that gives the sub-expressions that those symbols represent. The top expression can be given tocreate_parametrized_function()
, while thedict
of sub-expressions can be given to aSympyDataTransformer.from_sympy
.- Parameters
expression – The
Expr
from which to extract constant sub-expressions.free_symbols –
Symbol
instance in the mainexpression
that are not constant.fix_order – If
False
, the generated symbols for the sub-expressions are not deterministic, because they depend on the hashes of those sub-expressions. Setting this toTrue
makes the order deterministic, but this is slower, because requires lambdifying each sub-expression tostr
first.
- fast_lambdify(expression: Expr, symbols: Sequence[Symbol], backend: str, *, min_complexity: int = 0, max_complexity: int, use_cse: bool = True) Callable [source]¶
Speed up
lambdify()
withsplit_expression()
.For a simple example of the reasoning behind this, see Speed up lambdifying.
- lambdify(expression: Expr, symbols: Sequence[Symbol], backend: str, use_cse: bool = True) Callable [source]¶
A wrapper around
lambdify()
.- Parameters
expression – the
sympy.Expr
that you want to express as a function in a certain computation back-end.symbols – The
Symbol
instances in the expression that you want to serve as positional arguments in the lambdified function. Note that positional arguments are ordered.backend – Computational back-end in which to express the lambdified function.
use_cse – Lambdify with common sub-expressions (see
cse
argument inlambdify()
).
- prepare_caching(expression: Expr, parameters: Mapping[Symbol, ParameterValue], free_parameters: Iterable[Symbol], fix_order: bool = False) Tuple[Expr, Dict[Symbol, Expr]] [source]¶
Prepare an expression for optimizing with caching.
When fitting a
ParametrizedFunction
, only its freeParametrizedFunction.parameters
are updated on each iteration. This allows for an optimization: all sub-expressions that are unaffected by these free parameters can be cached as a constantDataSample
. The strategy here is to create a top expression that contains only the parameters that are to be optimized.Along with
extract_constant_sub_expressions()
, this function prepares asympy.Expr
for this caching procedure. The function returns a top expression where the constant sub-expressions have been substituted by new symbols \(f_i\) for each substituted sub-expression and adict
that gives the sub-expressions that those symbols represent.The top expression can be given to
create_parametrized_function()
, while thedict
of sub-expressions can be given to aSympyDataTransformer.from_sympy
.- Parameters
expression – The
Expr
from which to extract constant sub-expressions.parameters – A mapping of values for each of the parameter symbols in the
expression
. Parameters that are notfree_parameters
are substituted in the returned expressions withsubs()
.free_parameters –
Symbol
instances in the mainexpression
that are to be considered parameters and that will be optimized by anOptimizer
later on.fix_order – If
False
, the generated symbols for the sub-expressions are not deterministic, because they depend on the hashes of those sub-expressions. Setting this toTrue
makes the order deterministic, but this is slower, because requires lambdifying each sub-expression tostr
first.
- split_expression(expression: Expr, max_complexity: int, min_complexity: int = 1) Tuple[Expr, Dict[Symbol, Expr]] [source]¶
Split an expression into a ‘top expression’ and several sub-expressions.
Replace nodes in the expression tree of a
sympy.Expr
that lie within a certain complexity range (seecount_ops()
) with symbols and keep a mapping of each to these symbols to the sub-expressions that they replaced.See also