Oscar Gargiulo
Oscar Gargiulo

Reputation: 48

How to lambdify elliptic functions in sympy

I want to use the elliptic function embedded in the sympy library, but something is wrong when I try to lambdify elliptic(f(z)), where f is a symbolic function:

import sympy as sym
import numpy as np

r = sym.S('r')
_f = 500.0*r
f=sym.lambdify(r,_f,'numpy')

f(np.arange(0,1.1,0.1) )

The ouput is correct:

array([   0.,   50.,  100.,  150.,  200.,  250.,  300.,  350.,  400.,
    450.,  500.])

Then I try a f2(f(r)):

_f2 = _f/10
f2= sym.lambdify(r,_f2,'numpy')

f2(np.arange(0,1.1,0.1) )

And it works:

array([  0.,   5.,  10.,  15.,  20.,  25.,  30.,  35.,  40.,  45.,  50.])

But when I try:

_ek=sym.elliptic_k(_f)
ek=sym.lambdify(r,_ek,'numpy')

ek(0)

I get:

Traceback (most recent call last):

File "<ipython-input-17-35ab6a3fc36f>", line 1, in <module>
EK(0)

File "<string>", line 1, in <lambda>

NameError: name 'elliptic_k' is not defined

Any ideas how to solve it? Of course I would like to not rewrite all the elliptic_k functions if possible. Thank you

Upvotes: 1

Views: 415

Answers (3)

asmeurer
asmeurer

Reputation: 91620

NumPy does not have elliptic functions, but SciPy does. lambdify does not yet include SciPy functions automatically, so you'll need to do

lambdify(r, _ek, modules=['numpy', {'elliptic_k': scipy.special.ellipk}]) 

See the lambdify documentation for more information.

Upvotes: 1

Emilien
Emilien

Reputation: 2455

The answer is in the documentation of lambdify:

Attention: Functions that are not in the math module will throw a name error when the lambda function is evaluated!.

Thus you need to pass to lambdify the name of the corresponding module (which is sympy and not numpy for the elliptic_k function).

ek=sym.lambdify(r,_ek,'sympy')

Upvotes: 1

Severin Pappadeux
Severin Pappadeux

Reputation: 20130

sympy.functions.elliptic_k perhaps?

Upvotes: -1

Related Questions