Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to lambdify a SymPy expression containing the erf function for use with NumPy

I would like to lambdify a symbolic expression containing the erf function with SymPy. This can be done for scalar arguments as follows:

log_normal = 0.5 + 0.5 * sym.erf((sym.log(x) - mu) / sym.sqrt(2 * sigma**2))
F = sym.lambdify([x, mu, sigma], log_normal)
F(1.0, 0.0, 1.0)

I would like to vectorize the above. Normally I would do as follows...

log_normal = 0.5 + 0.5 * sym.erf((sym.log(x) - mu) / sym.sqrt(2 * sigma**2))
vector_F = sym.lambdify([x, mu, sigma], log_normal, modules='numpy')
vector_F(1.0, 0.0, 1.0)

However the above raises a NameError...

---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-29-14adde48d4a1> in <module>()
----> 1 vector_F(1.0, 0.0, 1.0)

/Users/drpugh/anaconda/lib/python2.7/site-packages/numpy/__init__.pyc in <lambda>(x, mu,     sigma)

NameError: global name 'erf' is not defined

Is this a bug, or am I missing something trivial?

like image 355
davidrpugh Avatar asked Sep 30 '14 18:09

davidrpugh


2 Answers

You told lambdify it only had numpy as a module to play with; give it a source for erf. IOW, you have

>>> vector_F = sym.lambdify([x, mu, sigma], log_normal, modules=['numpy'])
>>> vector_F(1.0, 0.0, 1.0)
Traceback (most recent call last):
  File "<ipython-input-10-14adde48d4a1>", line 1, in <module>
    vector_F(1.0, 0.0, 1.0)
  File "<string>", line 1, in <lambda>
NameError: global name 'erf' is not defined

but

>>> vector_F = sym.lambdify([x, mu, sigma], log_normal, modules=['numpy', 'sympy'])
>>> vector_F(1.0, 0.0, 1.0)
0.500000000000000

or

>>> vector_F = sym.lambdify([x, mu, sigma], log_normal, modules=['numpy', 'math'])
>>> vector_F(1.0, 0.0, 1.0)
0.5

or whichever erf you prefer, depending on whether you want a sympy.core.numbers.Float or a float.

like image 122
DSM Avatar answered Nov 14 '22 23:11

DSM


As of SymPy 1.3, scipy is automatically supported in lambdify. If you omit the modules argument, it adds scipy automatically. Or you can use modules=['numpy', 'scipy'].

>>> log_normal = 0.5 + 0.5 * sym.erf((sym.log(x) - mu) / sym.sqrt(2 * sigma**2))
>>> vector_F = sym.lambdify([x, mu, sigma], log_normal)
>>> vector_F(1.0, 0.0, 1.0)
0.5

In general, to support a function that lambdify doesn't know about, add it as a dictionary to the modules argument. The modules argument determines the namespace that the lambdified function is run against. See the lambdify documentation. For instance, to support erf in SymPy < 1.3:

>>> import scipy.special
>>> vector_F = sym.lambdify([x, mu, sigma], log_normal, modules=['numpy', {'erf': scipy.special.erf}])
>>> vector_F(1.0, 0.0, 1.0)
0.5
like image 22
asmeurer Avatar answered Nov 14 '22 22:11

asmeurer