lambdify not match shape of input array if zero function?

33 views
Skip to first unread message

Reckoner

unread,
May 5, 2008, 8:12:06 AM5/5/08
to sympy

Hi,

I have a program that creates a lot of lambdify-ed functions from
sympy expressions. The problem is sometimes the function turns out to
be the number zero and when I use the lambdify-ed version of that
function the input shape and output shape do not match. In other
words, the function zero returns exactly 1 zero regardless of how many
items were in the input array. This is inconsistent since if the
function is not zero, then the input/output shapes match.

I hope that made sense.

Currently, I have a bit of logic filter out this case, I would like to
avoid that if there is a way.

Thanks in advance.

Ondrej Certik

unread,
May 5, 2008, 9:36:36 AM5/5/08
to sy...@googlegroups.com
Hi Reckoner,

Thanks very much for the bug report, this is a bug. Could you please
post here a simple use case where it fails, and/or possibly even a
patch fixing it?
We'll push it in if it looks good.

Thanks,
Ondrej

Reckoner

unread,
May 5, 2008, 10:16:52 AM5/5/08
to sympy
>> x=sympy.Symbol('x')
>> f=x*0
>> lf=sympy.lambdify(f,[x])
>> lf(numpy.array(10))

returns 0 instead of [0,0,...,0]

I hope that helps.

Ondrej Certik

unread,
May 5, 2008, 10:23:12 AM5/5/08
to sy...@googlegroups.com
On Mon, May 5, 2008 at 4:16 PM, Reckoner <reck...@gmail.com> wrote:
>
>>> x=sympy.Symbol('x')
>>> f=x*0
>>> lf=sympy.lambdify(f,[x])
>>> lf(numpy.array(10))
>
> returns 0 instead of [0,0,...,0]
>
> I hope that helps.

Thanks, that helps, here is the issue for it:

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

Ondrej

basti

unread,
May 5, 2008, 12:13:32 PM5/5/08
to sympy
You will have to give the function as a string to lambdify:

>>f = lambdify("x*0", "x")
>>f(array([1,2,3]))
array([0,0])

The problem is that 0*x is automatically simplified to 0:

>>x = Symbol("x")
>>x*0
0

The solution would be to use a not simplified expression. I'm not sure
if that's implemented.

Sebastian

Ondrej Certik

unread,
May 5, 2008, 1:07:24 PM5/5/08
to sy...@googlegroups.com

Friedrich also sent a patch to sympy-patches:

http://groups.google.com/group/sympy-patches/browse_thread/thread/78bf61123b3d7405

that fixes the problem by doing:

+ >>> lambdastr(3*x, [x,y,z])
+ 'lambda x,y,z: (3*x+0*y+0*z)'


my question is -- is it acceptable? I.e. do we want lambdify to return
stuff like 0*x? Is there some other way to fix the problem?

Ondrej

Friedrich Hagedorn

unread,
May 6, 2008, 4:18:31 AM5/6/08
to sy...@googlegroups.com
On Mon, May 05, 2008 at 07:16:52AM -0700, Reckoner wrote:
> >> x=sympy.Symbol('x')
> >> f=x*0
> >> lf=sympy.lambdify(f,[x])
> >> lf(numpy.array(10))
>
> returns 0 instead of [0,0,...,0]

With the current revision from the mercurial repository you can do

[friedrich@wuschel:~] % isympy -q
Python 2.5.1 console for SymPy 0.5.14-hg

In [1]: F=lambdify(0,[x])

In [2]: F=vectorize(0)(F)

In [3]: from numpy import array

In [4]: F(array([0,1,2]))
Out[4]: [0, 0, 0]

The argument (0) in In[2] means that the first argument of the function F
is used for vectorizeing.

By,

Friedrich

Reply all
Reply to author
Forward
0 new messages