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.Fri, 17 Apr 2020 22:25:58 +0200Computing the inverse of a function with parameter(s)https://ask.sagemath.org/question/50809/computing-the-inverse-of-a-function-with-parameters/ I have adapted the code shown in the answer of "Can Sagemath compute the inverse of a function?". In the original exemple, the function was $log(x)$ so there is no parameters. But I would like to do the same with $x^a$. But even if i insert an `assume` command, I finally fall on an error. Here is my code
var('x,y,a')
assume(0<=a<=1)
U(x) = x^a
V(x) = solve(x == U(y), y)[0].rhs()
show(V)
Is there a way to obtain the result $x^{1/a}$ ?Fri, 17 Apr 2020 12:29:27 +0200https://ask.sagemath.org/question/50809/computing-the-inverse-of-a-function-with-parameters/Answer by vdelecroix for <p>I have adapted the code shown in the answer of "Can Sagemath compute the inverse of a function?". In the original exemple, the function was $log(x)$ so there is no parameters. But I would like to do the same with $x^a$. But even if i insert an <code>assume</code> command, I finally fall on an error. Here is my code</p>
<pre><code>var('x,y,a')
assume(0<=a<=1)
U(x) = x^a
V(x) = solve(x == U(y), y)[0].rhs()
show(V)
</code></pre>
<p>Is there a way to obtain the result $x^{1/a}$ ?</p>
https://ask.sagemath.org/question/50809/computing-the-inverse-of-a-function-with-parameters/?answer=50822#post-id-50822First of all, as written in the documentation of the `assume` function: Instead of using chained comparison notation, each relationship should be passed as a separate assumption:
sage: x = SR.var('x')
sage: assume(0 < x, x < 1) # instead of assume(0 < x < 1)
Then, when you try to use solve the error message is relevant
TypeError: Computation failed since Maxima requested additional constraints; using the 'assume' command before evaluation *may* help (example of legal syntax is 'assume(a>0)', see `assume?` for more details)
Is a an integer?
(here the last line) If you add this additional assumption it works.
Here is the complete code
sage: a, x, y = SR.var('a,x,y')
sage: assume(a, 'real')
sage: assume(a, 'noninteger') # or alternatively 'integer'
sage: assume(x, 'real')
sage: assume(y, 'real')
sage: assume(x > 0)
sage: assume(y > 0)
sage: solve(x == y^a, [y])
[y == x^(1/a)]Fri, 17 Apr 2020 22:09:04 +0200https://ask.sagemath.org/question/50809/computing-the-inverse-of-a-function-with-parameters/?answer=50822#post-id-50822Answer by dsejas for <p>I have adapted the code shown in the answer of "Can Sagemath compute the inverse of a function?". In the original exemple, the function was $log(x)$ so there is no parameters. But I would like to do the same with $x^a$. But even if i insert an <code>assume</code> command, I finally fall on an error. Here is my code</p>
<pre><code>var('x,y,a')
assume(0<=a<=1)
U(x) = x^a
V(x) = solve(x == U(y), y)[0].rhs()
show(V)
</code></pre>
<p>Is there a way to obtain the result $x^{1/a}$ ?</p>
https://ask.sagemath.org/question/50809/computing-the-inverse-of-a-function-with-parameters/?answer=50823#post-id-50823Hello, @Cyrille! The problem here is that you misused the `assume` command. The `assume` command is used to establish restrictions on symbolic variables, and its use depends on the case you are solving. For example, consider the following integral:
$$
\int_1^a\frac{1}{x}\;dx
$$
This will converge or diverge depending on the value of $a$, so no specific answer can be given in this case, without knowing more information about $a$. The `assume` command takes care of this. If you suppose (assume) that $a>1$, for example, you get
$$
\int_1^a\frac{1}{x}\;dx=\log(a)
$$
The corresponding Sage code would be
var('a')
assume(a>1)
integrate(1/x, 1, a)
If you suppose (assume) $a<0$, the integral will diverge. The corresponding Sage code would be
var('a')
assume(a>1)
integrate(1/x, 1, a)
(Consider `assume` the equivalent of the hypotheses of a theorem, proposition, lemma, etc.: you can't prove or even apply the theorem without knowing the hypothesis are true.)
In the particular case of your question, it is of no help to know that $0\le a\le1$. For example, $a=1/2$ satisfies the assumption, but $x^{1/2}$ can't be computed for every real value of $x$, so extra restrictions should apply in order to invert the function.
However, consider the restriction (hypothesis or assumption) that $a\in\mathbb{Z}$. In that case, the function $x^n$ is meaningful on the whole set $\mathbb{R}$, except maybe for $x=0$, which Sage can handle, and thus it can be inverted.
**My suggestion: Don't worry about when to use the `assume` command; Sage will tell you when it needs it.**
For example, continuing with your question, the code
var('x,y,a')
U(x) = x^a
V(x) = solve(x == U(y), y)[0].rhs()
show(V)
will print a very long traceback, most of which is useless for you, except for the last line, which says:
TypeError: Computation failed since Maxima requested additional constraints; using the 'assume' command before evaluation *may* help (example of legal syntax is 'assume(a>0)', see `assume?` for more details)
Is a an integer?
There you go, the Maxima part of Sage is asking whether the variable $a$ is an integer or not. Then you declare $a$ to be indeed integer with an `assume`:
var('x,y,a')
assume(a, 'integer')
U(x) = x^a
V(x) = solve(x == U(y), y)[0].rhs()
show(V)
In general, when you get a large traceback, ignore most of it, except the last line, or perhaps the last three lines.Fri, 17 Apr 2020 22:21:15 +0200https://ask.sagemath.org/question/50809/computing-the-inverse-of-a-function-with-parameters/?answer=50823#post-id-50823Comment by dsejas for <p>Hello, <a href="/users/26565/cyrille/">@Cyrille</a>! The problem here is that you misused the <code>assume</code> command. The <code>assume</code> command is used to establish restrictions on symbolic variables, and its use depends on the case you are solving. For example, consider the following integral:</p>
<p>$$
\int_1^a\frac{1}{x}\;dx
$$</p>
<p>This will converge or diverge depending on the value of $a$, so no specific answer can be given in this case, without knowing more information about $a$. The <code>assume</code> command takes care of this. If you suppose (assume) that $a>1$, for example, you get</p>
<p>$$
\int_1^a\frac{1}{x}\;dx=\log(a)
$$</p>
<p>The corresponding Sage code would be</p>
<pre><code>var('a')
assume(a>1)
integrate(1/x, 1, a)
</code></pre>
<p>If you suppose (assume) $a<0$, the integral will diverge. The corresponding Sage code would be</p>
<pre><code>var('a')
assume(a>1)
integrate(1/x, 1, a)
</code></pre>
<p>(Consider <code>assume</code> the equivalent of the hypotheses of a theorem, proposition, lemma, etc.: you can't prove or even apply the theorem without knowing the hypothesis are true.)</p>
<p>In the particular case of your question, it is of no help to know that $0\le a\le1$. For example, $a=1/2$ satisfies the assumption, but $x^{1/2}$ can't be computed for every real value of $x$, so extra restrictions should apply in order to invert the function.</p>
<p>However, consider the restriction (hypothesis or assumption) that $a\in\mathbb{Z}$. In that case, the function $x^n$ is meaningful on the whole set $\mathbb{R}$, except maybe for $x=0$, which Sage can handle, and thus it can be inverted.</p>
<p><strong>My suggestion: Don't worry about when to use the <code>assume</code> command; Sage will tell you when it needs it.</strong></p>
<p>For example, continuing with your question, the code</p>
<pre><code>var('x,y,a')
U(x) = x^a
V(x) = solve(x == U(y), y)[0].rhs()
show(V)
</code></pre>
<p>will print a very long traceback, most of which is useless for you, except for the last line, which says:</p>
<pre><code>TypeError: Computation failed since Maxima requested additional constraints; using the 'assume' command before evaluation *may* help (example of legal syntax is 'assume(a>0)', see `assume?` for more details)
Is a an integer?
</code></pre>
<p>There you go, the Maxima part of Sage is asking whether the variable $a$ is an integer or not. Then you declare $a$ to be indeed integer with an <code>assume</code>:</p>
<pre><code>var('x,y,a')
assume(a, 'integer')
U(x) = x^a
V(x) = solve(x == U(y), y)[0].rhs()
show(V)
</code></pre>
<p>In general, when you get a large traceback, ignore most of it, except the last line, or perhaps the last three lines.</p>
https://ask.sagemath.org/question/50809/computing-the-inverse-of-a-function-with-parameters/?comment=50824#post-id-50824Oh, I see that @vdelecroix also wrote an answer while I was writing mine. It seems that our answers complement each other. I like how he solved the case for $a\notin\mathbb{Z}$, while I solved it for $a\in\mathbb{Z}$. In his answer, you can see the extra restrictions I mentioned you would need, when I was talking about the case $a=1/2$.Fri, 17 Apr 2020 22:25:58 +0200https://ask.sagemath.org/question/50809/computing-the-inverse-of-a-function-with-parameters/?comment=50824#post-id-50824