Why is nlogn so hard to invert?

2019-07-27 11:22发布

Lets say I have a function that is nlogn in space requirements, I want to work out the maximum size of input for that function for a given available space. i.e. I want to find n where nlogn=c.

I followed an approach to calculate n, that looks like this in R:

step = function(R, z) { log(log(R)-z)} 
guess = function(R) log(log(R))

inverse_nlogn = function(R, accuracy=1e-10) {
 zi_1 = 0
 z = guess(R)
 while(abs(z - zi_1)>accuracy) { 
  zi_1 = z
  z = step(R, z)
 }
 exp(exp(z))
}

But I can't get understand why it must be solved iteratively. For the range we are interested (n>1), the function is non singular.

3条回答
放我归山
2楼-- · 2019-07-27 11:59

As Gareth hinted the Lambert W function (eg here) gets you almost there, indeed n = c/W(c)

A wee google found this, which might be helpful.

查看更多
Explosion°爆炸
3楼-- · 2019-07-27 12:03

Following up (being completely explicit):

library(emdbook)

n <- 2.5

c <- 2.5*log(2.5)
exp(lambertW(c))  ## 2.5

library(gsl)
exp(lambert_W0(c)) ## 2.5

There are probably minor differences in speed, accuracy, etc. of the two implementations. I haven't tested/benchmarked them extensively. (Now that I tried

library(sos)
findFn("lambert W")

I discover that it's implemented all over the place: the games package, and a whole package that's called LambertW ...

查看更多
The star\"
4楼-- · 2019-07-27 12:04

There's nothing special about n log n — nearly all elementary functions fail to have elementary inverses, and so have to be solved by some other means: bisection, Newton's method, Lagrange inversion theorem, series reversion, Lambert W function...

查看更多
登录 后发表回答