The decision is symbolic. Selecting symbols in the final representation
Consider the following three simple expressions:
from sympy import * x1,y1,x2,y2,x,y,a,xn,yn = symbols('x1 y1 x2 y2 x y a xn yn') yn = (1 - xn)/(1 - a*xn) xn = (x - x1)/(x2 - x1) yn = (y - y1)/(y2 - y1)
I would like to express y
as a function of x
, x1
, x2
, y1
, y2
and a
.
How should I do it? Can sub
extension / simplification be used for this type?
source to share
Assuming your equations are equality and not an assignment variable, then your system of equations is:
xn = (x - x1) (x2 - x1)
(1 - xn) / (1 - a * xn) = (y - y1) / (y2 - y1)
This can be solved in SymPy as follows:
from sympy import * x1, y1, x2, y2, x, y, a = symbols('x1 y1 x2 y2 x y a') xn = (x - x1)/(x2 - x1) yn1 = (1 - xn)/(1 - a*xn) yn2 = (y - y1)/(y2 - y1) eq0 = yn1 - yn2 solve(eq0, y)
which returns:
[(a*x*y1 - a*x1*y1 - x*y1 + x*y2 + x1*y1 - x2*y2)/(a*x - a*x1 + x1 - x2)]
A bit of explanation:
-
xn
does not depend onyn
, so we can just define it as an expression rather than create a symbol for it on it. - The expression
eq0
is an equivalence equationyn
from above, rearranged to have only0
on the right side. Many numeric solvers share the same interface, and sympy takes it up here. -
solve
accepts an expression equivalent to 0 and symbols to resolve. Here we only want to decidey
. - The results from
solve
are iterable solutions (alist
). Since SymPy only found one solution, the list is only 1 long. Other equations may come back for more.
source to share