friendlysam.opt.Eq.evaluate

Eq.evaluate(replace=None, evaluators=None)

Evaluate the expression recursively.

Evaluating an expression:

1. Get an evaluating function. If the class of the present expression is in the evaluators dict, use that. Otherwise, take the create() classmethod of the present expression class.

2. Evaluate all the arguments. For each argument arg, first try to replace it by looking for replace[arg]. If it’s not there, try to evaluate it by calling arg.evaluate() with the same arguments supplied to this call. If arg.evaluate() is not present, leave the argument unchanged.

  1. Run the evaluating function func(*evaluated_args) and return the result.
Parameters:
  • replace (dict, optional) – Replacements for arguments. Arguments matching keys will be replaced by specified values.
  • evaluators (dict, optional) – Evaluating functions to use instead of the default (which is the create() classmethod of the argument’s class). An argument whose __class__ equals a key will be evaluated with the specified function.

Examples

>>> x = VariableCollection('x')
>>> expr = x(1) + x(2)
>>> print(expr.evaluate())
x(1) + x(2)
>>> expr.evaluate(replace={x(1): 10, x(2): 20})
<friendlysam.opt.Add at 0x...>
>>> print(_)
10 + 20
>>> expr.evaluate(replace={x(1): 10, x(2): 20}, evaluators=fs.CONCRETE_EVALUATORS)
30