nevaluate
Numerically evaluate an expression.
Given a scalar expression of one or more variables, evaluate it for a range of values
of those variables. This algorithm accepts a Cadabra expression and one or more numpy arrays
containing the values of the variables, in the form of a dictionary. A simple example with
an expression of one variable:ex:= \cos(x) \exp(-x**2/4);
import numpy as np
xv = np.linspace(0, 3, 100)
z = np.array( nevaluate(ex, {$x$: xv} ) )
\(\displaystyle{}\cos{x} \exp\left( - \frac{1}{4}{x}^{2}\right)\)
\cos(x) \exp( - 1/4 (x)**2)
z[0:10];
[1. 0.99931146 0.99724785 0.99381515 0.98902334 0.98288631 0.97542182 0.96665142 0.95660038 0.94529757]
The
nevaluate
function thus takes as its second argument a Python
dictionary which maps each variable in the expression to a list of values.
For expressions of multiple variables, the logic is the same: just list all
the variables in the dictionary, as in the example below.ex:= \cos(x) \sin(y);
\(\displaystyle{}\cos{x} \sin{y}\)
\cos(x) \sin(y)
xv = np.linspace(0, np.pi, 100)
yv = np.linspace(0, np.pi, 100)
z = np.array( nevaluate(ex, {$x$: xv, $y$: yv}) )
z[3,10];
0.3106205340965772