Bug Report

30 views
Skip to first unread message

Barış Bulut

unread,
Feb 9, 2020, 3:36:18 PM2/9/20
to sympy
x,y = symbols('x,y')

y = x**3
y.subs(x,5)
125

x = 5
y.subs(x,x)
x**3
y.subs(x,10)
x**3
y.subs(x,3)
x**3

x = 3
y.subs(x,125)
x**125
y.subs(x,10)
x**10

David Bailey

unread,
Feb 9, 2020, 6:12:34 PM2/9/20
to sy...@googlegroups.com

Hi Barış,

It isn't a bug!

Unfortunately as soon as you execute x=5 you destroy the symbol in x, replacing it by 5 .

That means that y.subs(x,10)  becomes

(x**3).subs(5,3)

No replacements are possible, so this return x**3

Then you set x to 3 so that y.subs(x,125) becomes

(x**3).subs(3,125) which yields

x**125

etc.

The correct code would be

x=symbols('x')

y=x**3

y.subs(x,5)

y.subs(x,10)

etc.

Notice that y never needed to be a variable, because you never used it - y got overwritten just as x did.

If you want to store an expression like x**3 in a python variable, I find it useful to use a longer name that wouldn't normally be used in a mathematical expression. If you set up a variable as a symbol, then it is best not to assign to it later until perhaps you understand what is going on.

Cheers,

David

Barış Bulut

unread,
Feb 11, 2020, 1:49:05 PM2/11/20
to sympy
Hi David,

Thank you for your answer and suggestions. It was really helpful.

10 Şubat 2020 Pazartesi 02:12:34 UTC+3 tarihinde David Bailey yazdı:
Reply all
Reply to author
Forward
0 new messages