Evaluator

Evaluator()

An optimized evaluator of an expression.

Methods

Name Description
compile Compile the evaluator to a shared library using C++ and optionally inline assembly and load it.
evaluate Evaluate the expression for multiple inputs and return the result.
evaluate_complex Evaluate the expression for multiple inputs and return the result.
evaluate_complex_flat Evaluate the expression for multiple inputs that are flattened and return the flattened result.
evaluate_flat Evaluate the expression for multiple inputs that are flattened and return the flattened result.
get_instructions Return the instructions for efficiently evaluating the expression, the length of the list

compile

Evaluator.compile(
    function_name,
    filename,
    library_name,
    inline_asm='default',
    optimization_level=3,
    compiler_path=None,
)

Compile the evaluator to a shared library using C++ and optionally inline assembly and load it. The inline ASM option can be set to ‘default’, ‘x64’, ‘aarch64’ or ‘none’.

evaluate

Evaluator.evaluate(inputs)

Evaluate the expression for multiple inputs and return the result.

evaluate_complex

Evaluator.evaluate_complex(inputs)

Evaluate the expression for multiple inputs and return the result.

evaluate_complex_flat

Evaluator.evaluate_complex_flat(inputs)

Evaluate the expression for multiple inputs that are flattened and return the flattened result. This method has less overhead than evaluate_complex.

evaluate_flat

Evaluator.evaluate_flat(inputs)

Evaluate the expression for multiple inputs that are flattened and return the flattened result. This method has less overhead than evaluate.

get_instructions

Evaluator.get_instructions()

Return the instructions for efficiently evaluating the expression, the length of the list of temporary variables, and the list of constants. This can be used to generate code for the expression evaluation in any programming language.

There are four lists that are used in the evaluation instructions: - param: the list of input parameters. - temp: the list of temporary slots. The size of it is provided as the second return value. - const: the list of constants. - out: the list of outputs.

The instructions are of the form: - ('add', ('out', 0), [('const', 1), ('param', 0)]) which means out[0] = const[1] + param[0]. - ('mul', ('out', 0), [('temp', 0), ('param', 0)]) which means out[0] = temp[0] * param[0]. - ('pow', ('out', 0), ('param', 0), -1) which means out[0] = param[0]^-1. - ('powf', ('out', 0), ('param', 0), ('param', 1)) which means out[0] = param[0]^param[1]. - ('fun', ('temp', 1), cos, ('param', 0)) which means temp[1] = cos(param[0]).

Examples

>>> from symbolica import *
>>> (ins, m, c) = E('x^2+5/3+cos(x)').evaluator({}, {}, [S('x')]).get_instructions()
>>>
>>> for x in ins:
>>>     print(x)
>>> print('temp list length:', m)
>>> print('constants:', c)

yields

('mul', ('out', 0), [('param', 0), ('param', 0)])
('fun', ('temp', 1), cos, ('param', 0))
('add', ('out', 0), [('const', 0), ('out', 0), ('temp', 1)])
temp list length: 2
constants: [5/3]