Evaluator
Evaluator()An optimized evaluator for expressions.
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 that are flattened and return the flattened result. |
| evaluate_complex | 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,
number_type,
inline_asm='default',
optimization_level=3,
native=True,
compiler_path=None,
compiler_flags=None,
custom_header=None,
cuda_number_of_evaluations=1,
cuda_block_size=512,
)Compile the evaluator to a shared library using C++ and optionally inline assembly and load it.
evaluate
Evaluator.evaluate(inputs)Evaluate the expression for multiple inputs that are flattened and return the flattened result. This method has less overhead than evaluate.
evaluate_complex
Evaluator.evaluate_complex(inputs)Evaluate the expression for multiple inputs that are flattened and return the flattened result. This method has less overhead than evaluate_complex.
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]). - ('external_fun', ('temp', 1), f, [('param', 0)]) which means temp[1] = f(param[0]). - ('assign', ('out', 1), ('const', 2)) which means out[1] = const[2]. - ('if_else', ('temp', 0), 5) which means if temp[0] != 0 goto label 5. - ('goto', 10) which means goto label 10. - ('label', 3) which means label 3. - ('join', ('out', 0), ('temp', 0), 3, 7) which means out[0] = (temp[0] != 0) ? label 3 : label 7.
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]