Maik Klein
Maik Klein

Reputation: 16158

How do I use operations such as -, +, *, / on equations in sympy?

from sympy import *
nx, ny, nz = symbols('nx ny nz')
ax, ay, az = symbols('ax ay az')
bx, by, bz = symbols('bx by bz')
e1 = Eq(ax * nx + ay * ny + az * nz, 0)
e2 = Eq(bx * nx + by * ny + bz * nz, 0)
e3 = Eq(nx**2 + ny**2 + nz**2, 1)
pprint(e1)
pprint(e2)
e4 = e1 * bx
pprint(e4)
e5 = e2 * ax
pprint(e5)
e6 = e4 - e5
pprint(e6)

Output:

ax⋅nx + ay⋅ny + az⋅nz = 0
bx⋅nx + by⋅ny + bz⋅nz = 0
bx⋅(ax⋅nx + ay⋅ny + az⋅nz = 0)
ax⋅(bx⋅nx + by⋅ny + bz⋅nz = 0)
-ax⋅(bx⋅nx + by⋅ny + bz⋅nz = 0) + bx⋅(ax⋅nx + ay⋅ny + az⋅nz = 0)

For

e4 = e1 * bx
pprint(e4)

I would have expected the output to the similar to this equation

bx⋅ax⋅nx + bx⋅ay⋅ny + bx⋅az⋅nz = 0

Also no operation seems to really work on multiplied equations. For example in e6.simplify() I would have expected the output lose the first term.

How do I correctly use operations such as -, +, *, / on equations?

Upvotes: 2

Views: 70

Answers (1)

Stelios
Stelios

Reputation: 5531

You can manipulate the left- or right-hand side of an equation by invoking the lhs ,rhs attributes, respectively. For example,

e4 = Eq((bx * e1.lhs).expand(), 0)
pprint(e4)

ax⋅bx⋅nx + ay⋅bx⋅ny + az⋅bx⋅nz = 0

Upvotes: 2

Related Questions