ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Sat, 23 Apr 2011 05:12:53 +0200Can sage compute the inverse of a function?https://ask.sagemath.org/question/8071/can-sage-compute-the-inverse-of-a-function/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
Mon, 11 Apr 2011 13:41:14 +0200https://ask.sagemath.org/question/8071/can-sage-compute-the-inverse-of-a-function/Answer by Kelvin Li for <p>Hello Sage community,</p>
<p>is it possible to compute the inverse of a function in one variable with sage? So say I have</p>
<pre><code>f(x) = log(x)
</code></pre>
<p>and I want to compute</p>
<pre><code>f.inverse()
</code></pre>
<p>Thank you
Paul</p>
https://ask.sagemath.org/question/8071/can-sage-compute-the-inverse-of-a-function/?answer=12286#post-id-12286Here 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:
- Sage can compute the inverse of a power series. See:
- link:[Reference Manual](http://www.sagemath.org/doc/reference/sage/rings/power_series_poly.html?highlight=reversion#sage.rings.power_series_poly.PowerSeries_poly.reversion)
- link:[sage-support discussion](http://groups.google.com/group/sage-support/browse_thread/thread/34fdf02add8100b6)
- link:[trac #7644 (closed)](http://trac.sagemath.org/sage_trac/ticket/7644)
- Somebody once asked for the derivative of the inverse of a general symbolic function, but nobody could figure out how to find just the inverse:
- link:[sage-support discussion](http://groups.google.com/group/sage-support/browse_thread/thread/bf0e281bef7424b1)
- Mathematica has what you are looking for:
- link:[InverseFunction](http://reference.wolfram.com/mathematica/ref/InverseFunction.html)
Mon, 11 Apr 2011 15:22:02 +0200https://ask.sagemath.org/question/8071/can-sage-compute-the-inverse-of-a-function/?answer=12286#post-id-12286Comment by kcrisman for <p>Here is a stupid method:</p>
<pre><code>var('x,y')
f(x) = log(x)
g(x) = solve(x == f(y), y)[0].rhs()
print g
</code></pre>
<p>Here is a compilation from several scattered sources:</p>
<ul>
<li>Sage can compute the inverse of a power series. See:
<ul>
<li>link:<a href="http://www.sagemath.org/doc/reference/sage/rings/power_series_poly.html?highlight=reversion#sage.rings.power_series_poly.PowerSeries_poly.reversion">Reference Manual</a></li>
<li>link:<a href="http://groups.google.com/group/sage-support/browse_thread/thread/34fdf02add8100b6">sage-support discussion</a></li>
<li>link:<a href="http://trac.sagemath.org/sage_trac/ticket/7644">trac #7644 (closed)</a></li>
</ul></li>
<li>Somebody once asked for the derivative of the inverse of a general symbolic function, but nobody could figure out how to find just the inverse:
<ul>
<li>link:<a href="http://groups.google.com/group/sage-support/browse_thread/thread/bf0e281bef7424b1">sage-support discussion</a></li>
</ul></li>
<li>Mathematica has what you are looking for:
<ul>
<li>link:<a href="http://reference.wolfram.com/mathematica/ref/InverseFunction.html">InverseFunction</a></li>
</ul></li>
</ul>
https://ask.sagemath.org/question/8071/can-sage-compute-the-inverse-of-a-function/?comment=21860#post-id-21860@Kelvin Li: Why don't you open a Trac ticket for this :) if one doesn't already exist, which I think it may. Thanks!Mon, 11 Apr 2011 23:58:28 +0200https://ask.sagemath.org/question/8071/can-sage-compute-the-inverse-of-a-function/?comment=21860#post-id-21860Comment by Kelvin Li for <p>Here is a stupid method:</p>
<pre><code>var('x,y')
f(x) = log(x)
g(x) = solve(x == f(y), y)[0].rhs()
print g
</code></pre>
<p>Here is a compilation from several scattered sources:</p>
<ul>
<li>Sage can compute the inverse of a power series. See:
<ul>
<li>link:<a href="http://www.sagemath.org/doc/reference/sage/rings/power_series_poly.html?highlight=reversion#sage.rings.power_series_poly.PowerSeries_poly.reversion">Reference Manual</a></li>
<li>link:<a href="http://groups.google.com/group/sage-support/browse_thread/thread/34fdf02add8100b6">sage-support discussion</a></li>
<li>link:<a href="http://trac.sagemath.org/sage_trac/ticket/7644">trac #7644 (closed)</a></li>
</ul></li>
<li>Somebody once asked for the derivative of the inverse of a general symbolic function, but nobody could figure out how to find just the inverse:
<ul>
<li>link:<a href="http://groups.google.com/group/sage-support/browse_thread/thread/bf0e281bef7424b1">sage-support discussion</a></li>
</ul></li>
<li>Mathematica has what you are looking for:
<ul>
<li>link:<a href="http://reference.wolfram.com/mathematica/ref/InverseFunction.html">InverseFunction</a></li>
</ul></li>
</ul>
https://ask.sagemath.org/question/8071/can-sage-compute-the-inverse-of-a-function/?comment=21862#post-id-21862Sage 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.Mon, 11 Apr 2011 17:41:56 +0200https://ask.sagemath.org/question/8071/can-sage-compute-the-inverse-of-a-function/?comment=21862#post-id-21862Comment by Kelvin Li for <p>Here is a stupid method:</p>
<pre><code>var('x,y')
f(x) = log(x)
g(x) = solve(x == f(y), y)[0].rhs()
print g
</code></pre>
<p>Here is a compilation from several scattered sources:</p>
<ul>
<li>Sage can compute the inverse of a power series. See:
<ul>
<li>link:<a href="http://www.sagemath.org/doc/reference/sage/rings/power_series_poly.html?highlight=reversion#sage.rings.power_series_poly.PowerSeries_poly.reversion">Reference Manual</a></li>
<li>link:<a href="http://groups.google.com/group/sage-support/browse_thread/thread/34fdf02add8100b6">sage-support discussion</a></li>
<li>link:<a href="http://trac.sagemath.org/sage_trac/ticket/7644">trac #7644 (closed)</a></li>
</ul></li>
<li>Somebody once asked for the derivative of the inverse of a general symbolic function, but nobody could figure out how to find just the inverse:
<ul>
<li>link:<a href="http://groups.google.com/group/sage-support/browse_thread/thread/bf0e281bef7424b1">sage-support discussion</a></li>
</ul></li>
<li>Mathematica has what you are looking for:
<ul>
<li>link:<a href="http://reference.wolfram.com/mathematica/ref/InverseFunction.html">InverseFunction</a></li>
</ul></li>
</ul>
https://ask.sagemath.org/question/8071/can-sage-compute-the-inverse-of-a-function/?comment=21841#post-id-21841@kcrisman: This is now Trac #11202.Thu, 14 Apr 2011 16:47:56 +0200https://ask.sagemath.org/question/8071/can-sage-compute-the-inverse-of-a-function/?comment=21841#post-id-21841Comment by paulkoer for <p>Here is a stupid method:</p>
<pre><code>var('x,y')
f(x) = log(x)
g(x) = solve(x == f(y), y)[0].rhs()
print g
</code></pre>
<p>Here is a compilation from several scattered sources:</p>
<ul>
<li>Sage can compute the inverse of a power series. See:
<ul>
<li>link:<a href="http://www.sagemath.org/doc/reference/sage/rings/power_series_poly.html?highlight=reversion#sage.rings.power_series_poly.PowerSeries_poly.reversion">Reference Manual</a></li>
<li>link:<a href="http://groups.google.com/group/sage-support/browse_thread/thread/34fdf02add8100b6">sage-support discussion</a></li>
<li>link:<a href="http://trac.sagemath.org/sage_trac/ticket/7644">trac #7644 (closed)</a></li>
</ul></li>
<li>Somebody once asked for the derivative of the inverse of a general symbolic function, but nobody could figure out how to find just the inverse:
<ul>
<li>link:<a href="http://groups.google.com/group/sage-support/browse_thread/thread/bf0e281bef7424b1">sage-support discussion</a></li>
</ul></li>
<li>Mathematica has what you are looking for:
<ul>
<li>link:<a href="http://reference.wolfram.com/mathematica/ref/InverseFunction.html">InverseFunction</a></li>
</ul></li>
</ul>
https://ask.sagemath.org/question/8071/can-sage-compute-the-inverse-of-a-function/?comment=21864#post-id-21864Thank 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.Mon, 11 Apr 2011 17:26:05 +0200https://ask.sagemath.org/question/8071/can-sage-compute-the-inverse-of-a-function/?comment=21864#post-id-21864Answer by Kelvin Li for <p>Hello Sage community,</p>
<p>is it possible to compute the inverse of a function in one variable with sage? So say I have</p>
<pre><code>f(x) = log(x)
</code></pre>
<p>and I want to compute</p>
<pre><code>f.inverse()
</code></pre>
<p>Thank you
Paul</p>
https://ask.sagemath.org/question/8071/can-sage-compute-the-inverse-of-a-function/?answer=12313#post-id-12313The 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 foundSat, 23 Apr 2011 05:12:53 +0200https://ask.sagemath.org/question/8071/can-sage-compute-the-inverse-of-a-function/?answer=12313#post-id-12313Answer by benjaminfjones for <p>Hello Sage community,</p>
<p>is it possible to compute the inverse of a function in one variable with sage? So say I have</p>
<pre><code>f(x) = log(x)
</code></pre>
<p>and I want to compute</p>
<pre><code>f.inverse()
</code></pre>
<p>Thank you
Paul</p>
https://ask.sagemath.org/question/8071/can-sage-compute-the-inverse-of-a-function/?answer=12287#post-id-12287It 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)
Mon, 11 Apr 2011 15:37:29 +0200https://ask.sagemath.org/question/8071/can-sage-compute-the-inverse-of-a-function/?answer=12287#post-id-12287Comment by paulkoer for <p>It may also be useful to note that you can make assumptions about the domain using the <code>assume</code> function since a given function <code>f(x)</code> may not have an inverse on its entire domain, or it may have different inverse functions on different subdomains:</p>
<pre><code>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)
</code></pre>
https://ask.sagemath.org/question/8071/can-sage-compute-the-inverse-of-a-function/?comment=21863#post-id-21863Benjamin, thanks for this hint, very useful!Mon, 11 Apr 2011 17:27:39 +0200https://ask.sagemath.org/question/8071/can-sage-compute-the-inverse-of-a-function/?comment=21863#post-id-21863Comment by benjaminfjones for <p>It may also be useful to note that you can make assumptions about the domain using the <code>assume</code> function since a given function <code>f(x)</code> may not have an inverse on its entire domain, or it may have different inverse functions on different subdomains:</p>
<pre><code>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)
</code></pre>
https://ask.sagemath.org/question/8071/can-sage-compute-the-inverse-of-a-function/?comment=21865#post-id-21865yep, was thinking of the domain of "f". Thanks.Mon, 11 Apr 2011 15:51:25 +0200https://ask.sagemath.org/question/8071/can-sage-compute-the-inverse-of-a-function/?comment=21865#post-id-21865