Lambdify, namespaces, puzzling

2 views
Skip to first unread message

Matthew Brett

unread,
Jun 17, 2010, 12:42:38 PM6/17/10
to sympy
Hello,

I am sorry if this is obvious, but I find myself stuck in
understanding some odd behavior of sympy.lambdify.

The problem is that, if I pass a dictionary, and a module, as a
namespace, to sympy lambdify, then I can apparently overwrite the
namespace of a function that has already been instantiated. I
realize that's not very clear, but I hope this script helps to
explain:

n1 = {'f': lambda x:'first f',
'g': lambda x:'first g'}
n2 = {'f': lambda x:'second f',
'g': lambda x:'second g'}

f = sympy.Function('f')
g = sympy.Function('g')
x = sympy.Symbol('x')

if1 = sympy.lambdify(x, f(x), modules=(n1, "sympy"))
print 'f(x) before dict, string;', if1(1)
if2 = sympy.lambdify(x, g(x), modules=(n2, "sympy"))
print 'f(x) after dict, string;', if1(1)

- gives this output:

f(x) before dict, string; first f
f(x) after dict, string; second f

- meaning that the original generated lambda changes as a result of a
second call to lambdify.

The attached script shows that this occurs only when the first and
second calls have a dictionary and some other type of namespace.

I am sure that this is caused by some python thing I don't understand,
but I would be very grateful for any insight...

Thanks a lot,

Matthew

test_a2.py

Matthew Brett

unread,
Jul 2, 2010, 8:13:33 AM7/2/10
to sympy
Hi,

> The problem is that, if I pass a dictionary, and a module, as a
> namespace, to sympy lambdify, then I can apparently overwrite the
> namespace of a function that has already been instantiated.     I
> realize that's not very clear, but I hope this script helps to
> explain:
>
> n1 = {'f': lambda x:'first f',
>      'g': lambda x:'first g'}
> n2 = {'f': lambda x:'second f',
>      'g': lambda x:'second g'}
>
> f = sympy.Function('f')
> g = sympy.Function('g')
> x = sympy.Symbol('x')
>
> if1 = sympy.lambdify(x, f(x), modules=(n1, "sympy"))
> print 'f(x) before dict, string;', if1(1)
> if2 = sympy.lambdify(x, g(x), modules=(n2, "sympy"))
> print 'f(x) after dict, string;', if1(1)
>
> - gives this output:
>
> f(x) before dict, string; first f
> f(x) after dict, string; second f

In case anyone ends up following this thread, I posted a bugfix for
this on sympy-patches. I hope:

http://groups.google.com/group/sympy-patches/browse_thread/thread/cf2f122b3d36d7f0#

Cheers,

Matthew

Reply all
Reply to author
Forward
0 new messages