# Computing the inverse of a function with parameter(s)

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).rhs()
show(V)


Is there a way to obtain the result $x^{1/a}$ ?

edit retag close merge delete

Sort by » oldest newest most voted

First 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)]

more

Hello, @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).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).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.

more

Oh, 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$.