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?

+3


source to share


1 answer


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 on yn

    , so we can just define it as an expression rather than create a symbol for it on it.
  • The expression eq0

    is an equivalence equation yn

    from above, rearranged to have only 0

    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 decide y

    .
  • The results from solve

    are iterable solutions (a list

    ). Since SymPy only found one solution, the list is only 1 long. Other equations may come back for more.
+2


source







All Articles