question about how to substitute an expression with a variable

38 views
Skip to first unread message

Rafael Rocha

unread,
Jun 12, 2015, 1:25:00 PM6/12/15
to sy...@googlegroups.com
Hi everyone.

I am learning sympy and I have a question. I have 3 equations, the third is the sum of the two first

I = c1+v1+m1 = w1
II = c2+v2+m2 = w2
I + II = C + V + M = W

So, C = c1+c2, V=v1+v2, M=m1+m2 and W=w1+w2

I defined everything in symbols:

c1,v1,m1,w1 = symbols('c1 v1 m1 w1')
c2,v2,m2,w2 = symbols('c2 v2 m2 w2')
w1 = c1 + v1 + m1
print ('w1=',w1)

w2 = c2 + v2 + m2
print ('w2=',w2)

C,V,M,W = symbols ('C V M W')

C = c1 + c2
V = v1 + v2
M = m1 + m2
W = w1 + w2

After all this, I want to be a able to print W and get C+V+M... But I can only get c1+c2+v1+v2+m1+m2

I Am trying to use the subs and sympify function to do this:

A = W.subs({c1+c2:C, v1+v2:V, m1+m2:M, w1+w1:W})

print (sympify(A))


But It stills return

c1+c2+v1+v2+m1+m2

How can I make sympy understand I want it to substitute c1+c2 for C and so on?

Thanks,

Rafael





TIANJIAO SUN

unread,
Jun 12, 2015, 1:36:24 PM6/12/15
to sy...@googlegroups.com
Try W.replace() and W.xreplace()
Reply all
Reply to author
Forward
0 new messages