Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add test_inverse #2

Merged
merged 4 commits into from
Oct 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
name = "InverseFunctions"
uuid = "3587e190-3f89-42d0-90ee-14403ec27112"
version = "0.1.0"
version = "0.1.1"

[deps]
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
oschulz marked this conversation as resolved.
Show resolved Hide resolved

[compat]
julia = "1"

[extras]
Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

[targets]
test = ["Documenter", "Test"]
test = ["Documenter"]
31 changes: 6 additions & 25 deletions docs/src/api.md
Original file line number Diff line number Diff line change
@@ -1,32 +1,13 @@
# API

```@meta
DocTestSetup = quote
using InverseFunctions
end
```

## Modules

```@index
Order = [:module]
```

## Types and constants

```@index
Order = [:type, :constant]
```

## Functions and macros
## Interface

```@index
Order = [:macro, :function]
```@docs
inverse
```

# Documentation
## Test utility

```@autodocs
Modules = [InverseFunctions]
Order = [:module, :type, :constant, :macro, :function]
```@docs
InverseFunctions.test_inverse
```
4 changes: 4 additions & 0 deletions docs/src/index.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# InverseFunctions.jl

```@docs
InverseFunctions
```
oschulz marked this conversation as resolved.
Show resolved Hide resolved

This package defines the function [`inverse`](@ref). `inverse(f)` returns the inverse function of a function `f`, so that `inverse(f)(f(x)) ≈ x`.

`inverse` supports mapped/broadcasted functions (via `Base.Fix1`) and (on Julia >=v1.6) function composition.
Expand Down
3 changes: 3 additions & 0 deletions src/InverseFunctions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ Lightweight package that defines an interface to invert functions.
"""
module InverseFunctions

using Test

include("inverse.jl")
include("test.jl")

end # module
2 changes: 2 additions & 0 deletions src/inverse.jl
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ true
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)) === f`.

You can check your implementation with [`InverseFunctions.test_inverse`](@ref).
"""
inverse(f)
export inverse
Expand Down
19 changes: 19 additions & 0 deletions src/test.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
"""
InverseFunctions.test_inverse(f, x; kwargs...)

Check if [`inverse(f)`](@ref) is implemented correctly.

The function checks if
- `inverse(f)(f(x)) ≈ x` and
- `inverse(inverse(f)) === f`.

All keyword arguments are passed to `isapprox`.
"""
function test_inverse(f, x; kwargs...)
oschulz marked this conversation as resolved.
Show resolved Hide resolved
@testset "test_inverse: $f with input $x" begin
inverse_f = inverse(f)
@test isapprox(inverse_f(f(x)), x; kwargs...)
@test inverse(inverse_f) === f
end
return nothing
end
55 changes: 14 additions & 41 deletions test/test_inverse.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,52 +6,25 @@ inv_foo(y) = log(y / (1 - y))
InverseFunctions.inverse(::typeof(foo)) = inv_foo
InverseFunctions.inverse(::typeof(inv_foo)) = foo


@testset "inverse" begin
@test inverse(inverse)(inverse(log)) == log
x = rand()
for f in (foo, inv_foo, exp, log, exp2, log2, exp10, log10, expm1, log1p)
InverseFunctions.test_inverse(f, x)
end

x = 4.2
X = rand(10)
A = rand(5, 5)

@test @inferred(inverse(foo)(foo(x))) ≈ x
@test @inferred(inverse(inverse(foo))) == foo

@static if VERSION >= v"1.6"
log_foo = log ∘ foo
@test inverse(log_foo)(log_foo(x)) ≈ x
@test @inferred(inverse(log_foo)) == inv_foo ∘ exp
for f in (identity, inv, adjoint, transpose)
InverseFunctions.test_inverse(f, A)
end

X = rand(5)
for f in (Base.Fix1(broadcast, foo), Base.Fix1(map, foo))
for x in (x, fill(x, 3), X)
InverseFunctions.test_inverse(f, x)
end
end

broadcasted_foo = Base.Fix1(broadcast, foo)
@test @inferred(inverse(inverse(broadcasted_foo))) == broadcasted_foo
@test @inferred(inverse(broadcasted_foo)(broadcasted_foo(x))) ≈ x
@test @inferred(inverse(broadcasted_foo)(broadcasted_foo(fill(x)))) ≈ x
@test @inferred(inverse(broadcasted_foo)(broadcasted_foo(Ref(x)))) ≈ x
@test @inferred(inverse(broadcasted_foo)(broadcasted_foo((x,))))[1] ≈ x
oschulz marked this conversation as resolved.
Show resolved Hide resolved
@test @inferred(inverse(broadcasted_foo)(broadcasted_foo(X))) ≈ X

mapped_foo = Base.Fix1(map, foo)
@test @inferred(inverse(inverse(mapped_foo))) == mapped_foo
@test @inferred(inverse(mapped_foo)(mapped_foo(x))) ≈ x
@test @inferred(inverse(mapped_foo)(mapped_foo(fill(x)))) ≈ fill(x)
@test @inferred(inverse(mapped_foo)(mapped_foo(Ref(x)))) ≈ fill(x)
@test @inferred(inverse(mapped_foo)(mapped_foo((x,))))[1] ≈ x
@test @inferred(inverse(mapped_foo)(mapped_foo(X))) ≈ X


mapped_foo = Base.Fix1(broadcast, foo)
Y = mapped_foo(X)
@test @inferred(inverse(mapped_foo)(Y)) ≈ X

for f in (identity, inv, adjoint, transpose)
@test @inferred(inverse(f)(f(A))) ≈ A
@test @inferred(inverse(f)) == f
end

for f in (exp, log, exp2, log2, exp10, log10, expm1, log1p)
@test @inferred(inverse(f)(f(x))) ≈ x
@test @inferred(inverse(inverse(f))) == f
@static if VERSION >= v"1.6"
InverseFunctions.test_inverse(log ∘ foo, x)
end
end