API
Interface
InverseFunctions.inverse — Functioninverse(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
trueImplementation
Implementations of inverse(::typeof(f)) have to satisfy
inverse(f)(f(x)) ≈ xfor allxin the domain off, andinverse(inverse(f))is defined andinverse(inverse(f))(x) ≈ f(x)for allxin the domain off.
You can check your implementation with InverseFunctions.test_inverse.
InverseFunctions.NoInverse — Typestruct NoInverse{F}An instance NoInverse(f) signifies that inverse(f) is not defined.
InverseFunctions.setinverse — Functionsetinverse(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)
trueTest utility
InverseFunctions.test_inverse — FunctionInverseFunctions.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) == trueandcompare(inverse(inverse(f))(x), f(x)) == true.
kwargs... are forwarded to compare.
!!! Note On Julia >= 1.9, you have to load the Test standard library to be able to use this function.
Additional functionality
InverseFunctions.square — Functionsquare(x::Real)Inverse of sqrt(x) for non-negative x.
InverseFunctions.FunctionWithInverse — Typestruct FunctionWithInverse{F,InvF} <: FunctionA function with an inverse.
Do not construct directly, use setinverse(f, invf) instead.