How can I get this to give me x = z*y/a ?
from sympy import *
x,y,a,z = symbols('x y a z')
z = a*x/y
solve(z,x) # returns 0!
# would like to get z*y/a
solve(z,x)
correctly returns 0 because your code is effectively asking:
What's the value of x that would cause z to become 0?
What you really want to do (as described here ) is solve a*x/y==z
which can be done as follows:
from sympy import *
x,y,a,z = symbols('x y a z')
equation = a*x/y
new_eq = solve(equation - z, x) # its value is [y*z/a]
Don't assign z = a*x/y
, and don't pass z
to solve
.
solve(expr, symbol)
determines what values of symbol
will make expr
equal 0. If you want to figure out what value of x
makes z
equal a*x/y
, you want z - a*x/y
to equal 0:
solve(z - a*x/y, x)
You do not want to assign z = a*x/y
. =
means something entirely different from equality.
I think the answer to this question can be of help. Applied to your example, this gives:
>>> from sympy import *
>>> x,y,a,z = symbols('x y a z')
>>> l = z
>>> r = a*x/y
>>> solve(l-r,x)
[y*z/a]
As all the other answers points out the solution,
I would like to emphasize on the use of Eq
instances here.
An Eq
object represents An equal relation between two objects.
For using an Eq
object, your code should look something like this:
In []: a, x, y, z = symbols('a, x, y, z')
In []: foo = Eq(z, a*x/y)
In []: solve(foo, x)
Out[]: [y*z/a]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.