6

Here is the Mathematica code I'm asking about:

fn = InverseFunction[ Log@#+Exp@#& ];
Print[  fn'                 [1.]]
Print[  D[fn[x],x]      /.x->1. ]
Print[  D[fn[x],{x,2}]  /.x->1. ]
Print[  fn''                [1.]]
Print[  D[D[fn[x],x],x] /.x->1. ]
Print[ Normal @ Series[fn@x, {x, 1., 1}] ]
Print[ Normal @ Series[fn@x, {x, 1., 2}] ]

Try it online!

As you can see:

fn is a function defined using InverseFunction. We can assume that this function has no closed-form.

It has first-order and second-order numerical derivative at the point 1, being 0.27614597273176456 and 0.04511431748742336 respectively.

However, calculate double derivative using Derivative (fn''[1.]) or Series (degree $\geq 2$) does not behave as expected, resolves to InverseFunction' object.

So, what's the best way to get the Series of that (and other similar) function?

J. M.'s missing motivation
  • 124,525
  • 11
  • 401
  • 574
user202729
  • 502
  • 3
  • 11

2 Answers2

5

The problem with using an anonymous pure function inside of InverseFunction is that you get nested anonymous pure functions when evaluating the second derivative. Compare the following 2 TracePrint outputs:

TracePrint[InverseFunction[Function[x, Log[x]+Exp[x]]]'', Derivative[1][_]];
TracePrint[InverseFunction[Log[#]+Exp[#]&]'', Derivative[1][_]];

((1/(Function[x,Log[x]+Exp[x]]^[Prime])[InverseFunction[Function[x,Log[x]+Exp[x]]][#1]]&)^[Prime])

((1/((Log[#1]+Exp[#1]&)^[Prime])[InverseFunction[Log[#1]+Exp[#1]&][#1]]&)^[Prime])

Notice how the the first trace output has a mixture of x and #1, while the second trace output replaces x with #1. The latter trace is an example of a nested pure function using anonymous pure functions, which is why it doesn't work. The issue of nested anonymous pure functions has come up before, see:

  1. Is anonymous pure function a scoping construct?
  2. Pure function inside another pure function

Addendum

You also ask:

So, what's the best way to get the Series of that (and other similar) function?

Instead of taking the series of the inverse function, you can apply InverseSeries to the series expansion of the function. For example:

InverseSeries @ Series[Log[x]+Exp[x], {x, 1, 2}] //TeXForm

$1+\frac{x-e}{1+e}-\frac{(e-1) (x-e)^2}{2 (1+e)^3}+O\left((x-e)^3\right)$

Series[
    InverseFunction[Function[x, Log[x]+Exp[x]]][x],
    {x, E, 2}
] //TeXForm

$1+\frac{x-e}{1+e}+\frac{(1-e) (x-e)^2}{2 (1+e)^3}+O\left((x-e)^3\right)$

For clarity, I used an expansion point of 1 for the function, which corresponds to an expansion point of E for the inverse function. To get the expansion of the inverse function around 1 you could do:

pt = x /. First @ Solve[Log[x] + Exp[x]==1 && 0<x<1, x];
Simplify[
    InverseSeries @ Series[Log[x] + Exp[x], {x, a, 1}],
    Exp[a]+Log[a]==1
] /. a->pt //TeXForm

$\operatorname{Root}\left[\left\{e^{\#1}+\log (\#1)-1\&,0.51222243303322994816075176697026116734`20.304437036448245\right\}\right]+\frac{x-1}{e^{\operatorname{Root}\left[\left\{e^{\#1}+\log (\#1)-1\&,0.51222243303322994816075176697026116734`20.304437036448245\right\}\right]}+\frac{1}{\operatorname{Root}\left[\left\{e ^{\#1}+\log (\#1)-1\&,0.51222243303322994816075176697026116734`20.304437036448245\right\}\right]}}+O\left((x-1)^2\right)$

which yields the same result:

Series[
    InverseFunction[Function[x, Log[x]+Exp[x]]][x],
    {x, 1, 1}
] //TeXForm

$\operatorname{Root}\left[\left\{e^{\#1}+\log (\#1)-1\&,0.5122224330332299481607867201842576827690521506497275993993`30.\right\}\right]+\frac{x-1}{e^{\operatorname{Root}\left [\left\{e^{\#1}+\log (\#1)-1\&,0.5122224330332299481607867201842576827690521506497275993993`30.\right\}\right]}+\frac{1}{\operatorname{Root}\left[\left\{e^{\#1}+\log (\#1)-1\&,0.5122224330332299481607867201842576827690521506497275993993`30.\right\}\right]}}+O\left((x-1)^2\right)$

Carl Woll
  • 130,679
  • 6
  • 243
  • 355
4

InverseFunction and Derivative[1] operate on functions. So, supply them with functions:

h=.
f = InverseFunction[h];
f'[x]

Derivative[1][h][InverseFunction[h][x]]^(-1)

In your case, this leads to:

h = x \[Function] Log[x] + Exp[x];
f'[x]

1/(E^InverseFunction[Function[x, Log[x] + Exp[x]]][x] + 1/ InverseFunction[Function[x, Log[x] + Exp[x]]][x])

Now also Series works. I use machine precision numbers in order to make the output readible:

Series[f[x], {x, 1., 4}] // TeXForm

$$0.512222+0.276146 (x-1.)+0.0225572 (x-1.)^2-0.0123554 (x-1.)^3-0.0000787926 (x-1.)^4+O\left((x-1.)^5\right)$$

Henrik Schumacher
  • 106,770
  • 7
  • 179
  • 309
  • In conclusion: Anonymous function (#&) doesn't work, while explicit function (Function[y,y]) does work. Why? Is that a bug? They are both functions. – user202729 Dec 03 '17 at 14:00
  • Also the wrap around InverseFunction is unnecessary - just InverseFunction[...] instead of Function[x, InverseFunction[...][x]] works, InverseFunction is supposed to return a function. – user202729 Dec 03 '17 at 14:02
  • I have cleaned it up a bit. To my own surprise, lambda calculus seems not work. As a mathematician, I prefer Function anyway. It was actually the one thing that convinced me to use Mathematica... – Henrik Schumacher Dec 03 '17 at 14:06
  • If I were you, I would file that as a bug to Wolfram support. My impression used to be that lambda calculus and Function are supposed to be equivalent... – Henrik Schumacher Dec 03 '17 at 14:09
  • Actually, lambda and Function are not exactly equivalent, as lambda can receive any number of argument while Function can only accept a fixed number of argument. However this also doesn't work. – user202729 Dec 03 '17 at 14:15
  • Functions also can receive more arguments than in its declaration. The remaining arguments are just ignored. Note that Function has the attribute HoldAll, so you often have to apply Evaluate to its second argument, i.e., your example works with fn = InverseFunction[Function[y, Evaluate[Log[#] + Exp[#] &[y]]]]; – Henrik Schumacher Dec 03 '17 at 14:23
  • 1
    Moreover, I'd like to say that it would be more transparent for others if you would simply copy small code snippets directly into StackExchange... – Henrik Schumacher Dec 03 '17 at 14:32