Find the value of the inverse of a function in some point

I have a particular function and I want to calculate its inverse. My function is locally injective and I want to calculate its inverse, more specifically, values. For example, suppose my function is f such that f(0.1) = 0.2. I would like to compute f^-1(0.21), f^-1(0.19) etc.

Is there any function that does this?

You want to minimize abs(f(y) - x) where x is fixed. You may want to look at some optimization packages, like Optim.jl. This will be even better if you know the gradient of f.

1 Like

Rather than expressing it as a minimization problem, you want to find the root (zero) of

x -> f(x)-y


using Roots
f(x) = x^2
finv(y) = fzero(x->f(x) - y, 1) # set initial guess here

julia> finv(2)

There are indeed some choices, depending on obtaining and evaluating your inverse.

  • If an analytic inverse exists (a symbolic calculator can help), can you evaluate it fast enough?
  • If you can search the inverse value as the solution of a (possibly) nonlinear equation, you must also take care of convergence to the wanted root.
  • If you want a fast inverse with reasonable accuracy, you might consider reverse interpolation:
# using Pkg
# Pkg.add("Interpolations")

using Interpolations

xs = 1:0.2:64
ys = log2.(xs) # our function with "difficult" inverse

# interp_fun = LinearInterpolation(xs, ys)
interp_inv = LinearInterpolation(ys, xs)

interp_inv(4.0) == 16

Depending on your storage and precision requirements, you will choose the number and location of grid points, choose other interpolation methods (quadratic, spline…), and take care of the ranges and extrapolation.

1 Like