Search code examples
sympyequationsolverequation-solving

sympy solve one equation in 2 unknowns and symbols


import sympy
from sympy.abc import x, y, z, a, b

I want to find the roots in x and y for the following equation: f(x,y) = 2(x - a) + (y - b), whose roots are x=a, y=b. Using sympy I get instead

sym.solve((x - a)*2 + (y - b), [x, y])

$\left[ \left( a + \frac{b}{2} - \frac{y}{2}, y\right)\right]$

In other words, sympy treats y as a symbol (or scalar) instead of variable even if I ask it to solve for y as well. How can I overcome this issue?


Solution

  • The roots you have there are not the only possible roots. Looking at your

    2(x - a) + (y - b)

    formula, if we equate it to 0 to find the roots:

    2(x - a) + (y - b) = 0

    then it's clear that

    x = (2a + b - y) / 2

    and

    y = b - 2(x - a)

    so there are infinitely many possible roots. besides the roots you have given (x=a, y=b) we could define other roots, like (x = b/2 and y = 2a), having

    2(b/2 - a) + 2a - b = 0

    b - 2a + 2a - b = 0

    0 = 0

    as the result and infinitely many possible cases, you take an arbitrary x and compute its corresponding y or take an arbitrary y and compute its corresponding x to generate as many possible root pairs as you like. This is why finding the roots will not lead you to a single tuple, but infinitely many ones.