API

Interface

InverseFunctions.inverseFunction
inverse(f)

Return the inverse of function f.

inverse supports mapped and broadcasted functions (via Base.Broadcast.BroadcastFunction or Base.Fix1) and function composition (requires Julia >= 1.6).

Examples

julia> foo(x) = inv(exp(-x) + 1);

julia> inv_foo(y) = log(y / (1 - y));

julia> InverseFunctions.inverse(::typeof(foo)) = inv_foo;

julia> InverseFunctions.inverse(::typeof(inv_foo)) = foo;

julia> x = 4.2;

julia> inverse(foo)(foo(x)) ≈ x
true

julia> inverse(inverse(foo)) === foo
true

julia> broadcast_foo = VERSION >= v"1.6" ? Base.Broadcast.BroadcastFunction(foo) : Base.Fix1(broadcast, foo);

julia> X = rand(10);

julia> inverse(broadcast_foo)(broadcast_foo(X)) ≈ X
true

julia> bar = log ∘ foo;

julia> VERSION < v"1.6" || inverse(bar)(bar(x)) ≈ x
true

Implementation

Implementations of inverse(::typeof(f)) have to satisfy

  • inverse(f)(f(x)) ≈ x for all x in the domain of f, and
  • inverse(inverse(f)) is defined and inverse(inverse(f))(x) ≈ f(x) for all x in the domain of f.

You can check your implementation with InverseFunctions.test_inverse.

source
InverseFunctions.setinverseFunction
setinverse(f, invf)

Return a function that behaves like f and uses invf as its inverse.

Useful in cases where no inverse is defined for f or to set an inverse that is only valid within a given context, e.g. only for a limited argument range that is guaranteed by the use case but not in general.

For example, asin is not a valid inverse of sin for arbitrary arguments of sin, but can be a valid inverse if the use case guarantees that the argument of sin will always be within and π:

julia> foo = setinverse(sin, asin);

julia> x = π/3;

julia> foo(x) == sin(x)
true

julia> inverse(foo)(foo(x)) ≈ x
true

julia> inverse(foo) === setinverse(asin, sin)
true
source

Test utility

InverseFunctions.test_inverseFunction
InverseFunctions.test_inverse(f, x; compare=isapprox, kwargs...)

Test if inverse(f) is implemented correctly.

The function tests (as a Test.@testset) if

  • compare(inverse(f)(f(x)), x) == true and
  • compare(inverse(inverse(f))(x), f(x)) == true.

kwargs... are forwarded to compare.

source

Additional functionality