256ABC
256ABC

Reputation: 143

Sympy does not update series coefficients

aa = list(symbols('a0:2'))
q1= series(aa[0]/(1-x) + aa[1]/(1-x**2),x,n=6)
q1.subs(aa[0],1)  
print(q1)
Output: x**2*(a0 + a1) + x**4*(a0 + a1) + a1 + a0 + a0*x + a0*x**3 + a0*x**5 + O(x**6)

But what I would like for all the a0's in the series to be substitued by the value of 1:

Output: x**2*(1 + a1) + x**4*(1 + a1) + a1 + 1 + 1*x + 1*x**3 + 1*x**5 + O(x**6)

My understanding is that:

q1.subs(aa[0],1)  

would do exactly that. Is there any other way to do the same ? Thanks!

Upvotes: 3

Views: 71

Answers (1)

user6655984
user6655984

Reputation:

With the exception of mutable matrices, SymPy objects are immutable. Their methods do not modify them; a new object is returned instead. This object needs to be assigned to something (or printed, or returned):

q2 = q1.subs(...)
print(q1.subs(...))
return q1.subs(...)  

all make sense; the lonely q1.subs(...) is useless.

This is covered in the "Gotchas and Pitfalls" article under Immutability of Expressions; I recommend reading the rest of that page too.

Upvotes: 2

Related Questions