Applying sympy expressions on numpy arrays

528 views
Skip to first unread message

Gael Varoquaux

unread,
Mar 20, 2008, 11:49:07 AM3/20/08
to sy...@googlegroups.com
If I have:

from sympy import Symbol, Integrate
x = Symbol('x')
f = x**2 + x
g = Integrate(f, x)

how can I apply g to a numpy array? In other words, how can I "numpify"
the g expression, injecting in it x = numpy.linspace(1, 9, 9)? What would
be even nicer would be to be able to retrieve a lambda using numpy
functions for g as a function of x (that way I don't have the overhead of
numpifying it each time I want to apply it to an array).

Cheers,

Gaël

Ondrej Certik

unread,
Mar 21, 2008, 9:40:34 AM3/21/08
to sy...@googlegroups.com
On Thu, Mar 20, 2008 at 4:49 PM, Gael Varoquaux
<gael.va...@normalesup.org> wrote:
>
> If I have:
>
> from sympy import Symbol, Integrate
> x = Symbol('x')
> f = x**2 + x
> g = Integrate(f, x)
>
> how can I apply g to a numpy array? In other words, how can I "numpify"

First, I think you mean "integrate", right?

In [1]: f = x**2 + x

In [2]: integrate(f, x)
Out[2]:
2 3
x x
── + ──
2 3

In [3]: Integral(f, x)
Out[3]:

⎮ ⎛ 2⎞
⎮ ⎝x + x ⎠ dx

In [4]: Integrate(f, x)
---------------------------------------------------------------------------
NameError Traceback (most recent call last)

/home/ondra/sympy/<ipython console>

NameError: name 'Integrate' is not defined


> the g expression, injecting in it x = numpy.linspace(1, 9, 9)? What would
> be even nicer would be to be able to retrieve a lambda using numpy
> functions for g as a function of x (that way I don't have the overhead of
> numpifying it each time I want to apply it to an array).

Right, what you want is to create a function numpify, that does almost
the same as lambdify(), but in numpy namespace. I.e:

In [1]: f = x**2 + x

In [2]: g = integrate(f, x)

In [3]: g
Out[3]:
2 3
x x
── + ──
2 3

In [4]: h = lambdify(g, [x])

In [5]: h
Out[5]: <function <lambda> at 0xb787e4c4>

In [6]: h(1)
Out[6]: 0.833333333333

In [7]: h(2)
Out[7]: 4.66666666667

In [8]: h(0)
Out[8]: 0.0


I.e.:

In [10]: from sympy.utilities.lambdify import lambdastr

In [11]: lambdastr(g, [x])
Out[11]: lambda x: ((1/2)*x**2 + (1/3)*x**3)

So you just do something like

eval(lambdastr(g, [x])) but in numpy namespace. Does this do what you want?


If you figure out how to make this convenient and fast, please go back
to us so that we can integrate it in sympy.

Ondrej


P.S. All above examples were done in "bin/isympy" in the sympy distribution.

Gael Varoquaux

unread,
Mar 21, 2008, 10:54:58 AM3/21/08
to sy...@googlegroups.com
On Fri, Mar 21, 2008 at 02:40:34PM +0100, Ondrej Certik wrote:
> Right, what you want is to create a function numpify, that does almost
> the same as lambdify(), but in numpy namespace. I.e:

Exactly. We had a quick look on how to do this. More below.


> In [10]: from sympy.utilities.lambdify import lambdastr

> In [11]: lambdastr(g, [x])
> Out[11]: lambda x: ((1/2)*x**2 + (1/3)*x**3)

> So you just do something like

> eval(lambdastr(g, [x])) but in numpy namespace. Does this do what you want?

Exactly, lambdastr is exactly what we need.

> If you figure out how to make this convenient and fast, please go back
> to us so that we can integrate it in sympy.

I don't have time to do it (this is actually not exactly my problem, but
the problem of the guy who is sitting next to me, I am just curious about
it. A quick example of how to make such a feature (this is not code that
has matured at all):


from __builtin__ import __import__ as import_module
import __future__
from sympy.utilities.lambdify import lambdastr

def numpify(expr, vars):
np = import_module('numpy')
str_expr = lamdbastr(expr, vars)
return eval(compile(str_expr, '<string>', 'eval',
__future__.division.compiler_flag, 1), np.__dict__)

Now the big problem is to handle the name mismatches between sympy and
numpy. I am not to sure how you want to address that. Probably using a
table giving the correspondances.

Cheers,

Gaël


Ondrej Certik

unread,
Mar 21, 2008, 11:16:09 AM3/21/08
to sy...@googlegroups.com


Yes, just a simple dictionary with the differences.

http://code.google.com/p/sympy/issues/detail?id=756

Ondrej

Reply all
Reply to author
Forward
0 new messages