Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

The trick is to use the roots method of the given symbolic expression as follows:

Example 1

sage: var('y')
y
sage: f(x) = log(x) - y
sage: f.roots(x)
[(e^y, 1)]
sage: f.roots(x, multiplicities=False)
[e^y]

Example 2

f does not need to be callable.

sage: var('y')
y
sage: f = log(x) - y
sage: f.roots(x)
[(e^y, 1)]

Example 3

Use a helper function so that the expression "hacking" can be contained neatly:

sage: def symbolic_inverse(f, x):
....:    y = SR.var('y')
....:    g = (f - y).roots(x, multiplicities=False)
....:    return [expr.subs(y=x) for expr in g]
....:
sage: symbolic_inverse(log(x), x)
[e^x]
sage: symbolic_inverse(sin(x), x)
[arcsin(x)]
sage: symbolic_inverse(x, x)
[x]
sage: symbolic_inverse(x^2, x)
[-sqrt(x), sqrt(x)]
sage: var('c')
c
sage: symbolic_inverse(c^3, c)
[1/2*(I*sqrt(3) - 1)*c^(1/3), 1/2*(-I*sqrt(3) - 1)*c^(1/3), c^(1/3)]

Note that multiple inverses are all listed and that any variable can be used. However, only one branch is returned for the inverse of sin(x), namely arcsin(x).

If an inverse cannot be found or does not exist, a RuntimeError error is raised:

sage: symbolic_inverse(x + sin(x), x)
(... Traceback ...)
RuntimeError: no explicit roots found