'Sympy Inverse a function: y = ax+b into x = (y-b)/a

This should be easy, and hopefully doable in Sympy.

I have a function: Y = 0.05*X + 0.15, which I define using Sympy:

from sympy import *
Y = 0.05*X + 0.15

How do I get the inverse, where I express X as a function of Y:

X = (Y-0.15)/0.15


Solution 1:[1]

You can use the solve function, but first, put your equation in the form f(x,y)=0:

Y = 0.05*X + 0.15
==> 0.05*X + 0.15 - Y = 0

So, you can solve it using:

 solve( 0.05*X + 0.15 - Y, X)

Which will give the solution:

[20.0*Y - 3.0]

Alternatively, you can solve the equation directly using the Eq function (which is used to define a symbolic equality):

solve( Eq(Y, 0.05*X + 0.15), X)

which will give the same answer:

[20.0*Y - 3.0]

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1