Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Merge 2 algebraic expression using Sympy

How to merge 2 or more expressions
example: 3x + 2y – 2 and 2x + 3y -1 (3x + 2y -2 = 2x + 3y -1)
which gives x-y-1

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

from sympy import *
# declare symbols
var("x, y")

# write the two expressions
expr1 = 3*x + 2*y - 2 
expr2 = 2*x + 3*y - 1

# subtract them
result = expr1 - expr2
result
# out: x - y - 1

EDIT: Alternatively, we can also create an Equality object and later rewrite it as an addition:

eqaulity = Eq(expr1, expr2)
# out: 3*x + 2*y - 2 = 2*x + 3*y - 1
eqaulity.rewrite(Add)
# out: x - y - 1
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading