6
$\begingroup$

The problem I have is that Mathematica seems to play better with complex numbers so that the result of N[InverseFunction[Function[x,x^3]][-1]] is 0.5 + 0.866025 I and not -1 for example. I need to tell Mathematica to evaluate InverseFunction[Function[x,x^3]] as a function that simply draws a curve that is symmetric to $y=x$ from $y=x^3$, and the solution to make this happen must also be applicable to any invertible polynomial functions.

$\endgroup$
1
  • 1
    $\begingroup$ The documentation elaborates slightly on how the Wolfram Language chooses its branch cuts, but offers little in the way of how to change this behavior. I'd suggest toying around with Assuming and $Assmuptions, or perhaps using a more robust function than InverseFunction such as Solve or Reduce. $\endgroup$ – IPoiler Feb 1 '16 at 19:53
4
$\begingroup$

Reduce[expr, x, Reals] will be your friend here, but it can take a bit of work to parse its result.

Here's a solution that should work for any expression, not just polynomials (at least for the small set of examples I tried).

RealInverse[a_. x_^q_Integer?Positive + b_., x_] /; FreeQ[{a, b}, x] := 
  Surd[(x-b)/a, q]

RealInverse[expr_, x_] := 
  Module[{y, red},
    red = Reduce[x == (expr /. x -> y), y, Reals];
    ToPW[PWTerm[#, y]& /@ OrList[BooleanConvert[red, "DNF"]], x]
  ]

OrList[HoldPattern[Or][args__]] := {args}
OrList[e_] := {e}

PWTerm[And[cond1___, y_ == root_, cond2___], y_] /; FreeQ[{root, And[cond1, cond2]}, y] := {root, And[cond1, cond2]}
PWTerm[y_ == root_, y_] /; FreeQ[root, y] := {root, True}
PWTerm[___] = $Failed;

ToPW[lis_?MatrixQ, x_] /; Length[First[lis]] == 2 := 
  With[{condlist = Last[Transpose[lis]]},
    Piecewise[lis, Undefined] /; DisjointConditionsQ[condlist, x]
]
ToPW[___] = $Failed;

DisjointConditionsQ[{_}, _] = True;
DisjointConditionsQ[cond_List, x_]:=
  Reduce[And @@ Table[DisjointCondition[cond, i], {i, Length[cond]}], x]

DisjointCondition[cond_, i_] := 
  If[TrueQ[cond[[i]]],
    True,
    Implies[cond[[i]], Not[And @@ Delete[cond, i]]]
]

Now some tests:

RealInverse[x^3, x]
Surd[x, 3]
RealInverse[x^7 + x^4 + 3 x - 1, x]
Root[-1 - x + 3 #1 + #1^4 + #1^7 &, 1]
(* Not invertible over the real line *)
RealInverse[x^3 - 3 x + 1, x]
$Failed
RealInverse[x^(2/3) + Sqrt[x], x]
ConditionalExpression[
  Root[x^6 - 3 x^4 #1 + (3 x^2 - 2 x^3) #1^2 + (-1 - 6 x) #1^3 + #1^4 &, 1], 
  x >= 0
]
RealInverse[Sign[x] (Abs[x]^(2/3) + Sqrt[Abs[x]]), x]

enter image description here

$\endgroup$
0
$\begingroup$

What about this?

inverseFunc[x_] = x /. Solve[x^3 == y, x, Reals][[1]] /. y -> x;
N@inverseFunc[3]
N@inverseFunc[-1]
(*1.44225*)
(*-1.*)
$\endgroup$
0
$\begingroup$

One way to find and plot the real root of x^3 == y is

x /. Solve[x^3 == y, x, Reals]
(* {Root[-y + #1^3 &, 1]} *)
Plot[%, {y, -10, 10}, AxesLabel -> {y, x}]

enter image description here

This works for any polynomial f[x] == y that has a real root, as requested in the question. Is this what you had in mind?

Addendum

In response to the OP's comment below, a do-it-yourself version of InverseFunction that selects real branches, if any, is

invfun[g_] := N[x /. First@Solve[g[x] == #, x, Reals]] &

Then, the inverse function of

f[x_] := x^3

is simply

invfun[f][y]

as can be seen from

Plot[invfun[f][y], {y, -10, 10}, AxesLabel -> {y, x}]

which returns the plot above. Note that y is a dummy variable, and any symbol can be used in its place; e.g., invfun[f][w]. Note, also, that invfun evaluates the First real inverse of f returned by Solve, if there are more than one. Of course, all this works only for polynomials and (with a bit more work) other functions that can be handled by Solve.

$\endgroup$
2
  • $\begingroup$ Yours work if plotting the graph is all to achieve, but I explicitly need Mathematica to evaluate the inverse function itself which can be in place of any other function. $\endgroup$ – xiver77 Feb 2 '16 at 6:07
  • 1
    $\begingroup$ @xiver77 Just turn x /. Solve[x^3 == y, x, Reals] into a function, as thedude did above in his extension to my answer. I have added a more general response to my answer. $\endgroup$ – bbgodfrey Feb 2 '16 at 14:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.