2010-07-17 55 views
5

對於預定義方程式,將新值分配給變量不會改變方程式的值。 我如何分配新值的變量,這樣我會得到方程的適當的值,而不是以前的一個如何將新值賦給預定義方程中的變量?

a,b,c,d,e,f=sympy.symbols('abcdef') 
a,b=c,d 

e=a+b #equation 
print e 
c+d #value of eqn 
a,b=d,f 
print e 
c+d #not d+f 
+1

您的問題的討論: //docs.sympy.org/gotchas.html#variables-assignment-does-not-create-a-relation-between-expressions – 2010-07-17 18:38:04

+0

@PreludeAndFugue:鏈接現在已被打破。也許你有任何新的參考? – Bach 2014-09-26 14:27:10

+0

更新後的鏈接:http://docs.sympy.org/latest/gotchas.html#variables-assignment-does-not-create-a-relation-between-expressions – 2014-09-28 12:37:21

回答

5

也許使用substitution,而不是平等的:HTTP:

import sympy 
a,b,c,d,e,f=sympy.symbols('abcdef') 
e=a+b #equation 
print e.subs([(a,c),(b,d)]) 
# c + d 
print e.subs([(a,d),(b,f)]) 
# d + f 
+0

得到這個 TypeError:'instancemethod'對象不是可下載的 – user394706 2010-07-17 16:30:32

+2

上面的代碼在我的最後工作正常:sympy 0.6.5,python 2.5 – ars 2010-07-17 18:26:41

+0

thanx!由於我的語法錯誤,它沒有工作 – user394706 2010-07-18 02:26:30