Skip to content

Commit 5dddb15

Browse files
oschulzdevmotion
andcommitted
Add NoInverse
Co-authored-by: David Widmann <[email protected]>
1 parent f85a496 commit 5dddb15

File tree

3 files changed

+28
-0
lines changed

3 files changed

+28
-0
lines changed

docs/src/api.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
```@docs
66
inverse
7+
NoInverse
78
```
89

910
## Test utility

src/inverse.jl

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,27 @@ You can check your implementation with [`InverseFunctions.test_inverse`](@ref).
5252
inverse(f)
5353
export inverse
5454

55+
56+
57+
"""
58+
struct NoInverse{F}
59+
60+
An instance `NoInverse(f)` signifies that that `inverse(f)` is not
61+
defined.
62+
"""
63+
struct NoInverse{F}
64+
f::F
65+
end
66+
export NoInverse
67+
68+
(f::NoInverse)(x) = error("inverse of ", f.f, " is not defined")
69+
70+
inverse(f) = NoInverse(f)
71+
72+
inverse(f::NoInverse) = f.f
73+
74+
75+
5576
inverse(::typeof(inverse)) = inverse
5677

5778
@static if VERSION >= v"1.6"

test/test_inverse.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,12 @@ InverseFunctions.inverse(f::Bar) = Bar(inv(f.A))
2020

2121

2222
@testset "inverse" begin
23+
f_without_inverse(x) = x^2
24+
@test inverse(f_without_inverse) isa NoInverse
25+
@test_throws ErrorException inverse(f_without_inverse)(42)
26+
@test inverse(inverse(f_without_inverse)) === f_without_inverse
27+
28+
2329
InverseFunctions.test_inverse(inverse, log, compare = ===)
2430

2531
x = rand()

0 commit comments

Comments
 (0)