Ask Your Question
4

Can sage compute the inverse of a function?

asked 2011-04-11 06:41:14 -0500

paulkoer gravatar image

Hello Sage community,

is it possible to compute the inverse of a function in one variable with sage? So say I have

f(x) = log(x)

and I want to compute

f.inverse()

Thank you Paul

edit retag flag offensive close merge delete

3 answers

Sort by ยป oldest newest most voted
5

answered 2011-04-11 08:22:02 -0500

Kelvin Li gravatar image

updated 2011-04-11 08:24:01 -0500

Here is a stupid method:

var('x,y')
f(x) = log(x)
g(x) = solve(x == f(y), y)[0].rhs()
print g

Here is a compilation from several scattered sources:

edit flag offensive delete link more

Comments

Thank you Kelvin! Your 'solve' method works for me. As for the Mathematica method, by using Sage I hope to avoid having to pay for a Mathematica license.

paulkoer gravatar imagepaulkoer ( 2011-04-11 10:26:05 -0500 )edit
1

Sage aims to be an alternative to Mathematica (and the other M's). Replicating the functionality of those software has motivated the development of several features in Sage (case in point: revolution_plot). I mentioned Mathematica's InverseFunction because it is one of those features for which Sage does not have an obvious counterpart. Someone might want to look into this.

Kelvin Li gravatar imageKelvin Li ( 2011-04-11 10:41:56 -0500 )edit

@Kelvin Li: Why don't you open a Trac ticket for this :) if one doesn't already exist, which I think it may. Thanks!

kcrisman gravatar imagekcrisman ( 2011-04-11 16:58:28 -0500 )edit

@kcrisman: This is now Trac #11202.

Kelvin Li gravatar imageKelvin Li ( 2011-04-14 09:47:56 -0500 )edit
3

answered 2011-04-11 08:37:29 -0500

benjaminfjones gravatar image

updated 2011-04-11 08:52:30 -0500

It may also be useful to note that you can make assumptions about the domain using the assume function since a given function f(x) may not have an inverse on its entire domain, or it may have different inverse functions on different subdomains:

sage: f(x) = x^2
sage: assume(y<0)
sage: solve( x == f(y), y)[0].rhs()
-sqrt(x)
sage: forget()
sage: assume(y>0)
sage: solve( x == f(y), y)[0].rhs()
sqrt(x)
edit flag offensive delete link more

Comments

yep, was thinking of the domain of "f". Thanks.

benjaminfjones gravatar imagebenjaminfjones ( 2011-04-11 08:51:25 -0500 )edit

Benjamin, thanks for this hint, very useful!

paulkoer gravatar imagepaulkoer ( 2011-04-11 10:27:39 -0500 )edit
1

answered 2011-04-22 22:12:53 -0500

Kelvin Li gravatar image

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
edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

Stats

Asked: 2011-04-11 06:41:14 -0500

Seen: 3,574 times

Last updated: Apr 22 '11