-
Notifications
You must be signed in to change notification settings - Fork 8
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 setinverse #25
Merged
Merged
Add setinverse #25
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
e6826aa
Add setinverse
oschulz c84aa36
Fix docstring of setinverse
oschulz f3c7053
Fix docstring of setinverse
oschulz c4c2c83
Fix docs gen
oschulz 18051bd
Improve setinverse docs and implementation
oschulz 62bca9b
Shorten definition of setinverse
oschulz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
# This file is a part of InverseFunctions.jl, licensed under the MIT License (MIT). | ||
|
||
|
||
""" | ||
struct FunctionWithInverse{F,InvF} <: Function | ||
|
||
A function with an inverse. | ||
|
||
Do not construct directly, use [`setinverse(f, invf)`](@ref) instead. | ||
""" | ||
struct FunctionWithInverse{F,InvF} <: Function | ||
f::F | ||
invf::InvF | ||
end | ||
|
||
|
||
(f::FunctionWithInverse)(x) = f.f(x) | ||
|
||
inverse(f::FunctionWithInverse) = setinverse(f.invf, f.f) | ||
|
||
|
||
""" | ||
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 `π`: | ||
|
||
```jldoctest | ||
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 | ||
``` | ||
""" | ||
setinverse(f, invf) = FunctionWithInverse(_unwrap_f(f), _unwrap_f(invf)) | ||
export setinverse | ||
|
||
_unwrap_f(f) = f | ||
_unwrap_f(f::FunctionWithInverse) = f.f |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# This file is a part of InverseFunctions.jl, licensed under the MIT License (MIT). | ||
|
||
using Test | ||
using InverseFunctions | ||
|
||
|
||
@testset "setinverse" begin | ||
@test @inferred(setinverse(sin, asin)) === InverseFunctions.FunctionWithInverse(sin, asin) | ||
@test @inferred(setinverse(sin, setinverse(asin, sqrt))) === InverseFunctions.FunctionWithInverse(sin, asin) | ||
@test @inferred(setinverse(setinverse(sin, sqrt), asin)) === InverseFunctions.FunctionWithInverse(sin, asin) | ||
@test @inferred(setinverse(setinverse(sin, asin), setinverse(asin, sqrt))) === InverseFunctions.FunctionWithInverse(sin, asin) | ||
|
||
InverseFunctions.test_inverse(setinverse(sin, asin), π/4) | ||
InverseFunctions.test_inverse(setinverse(asin, sin), 0.5) | ||
end |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since it's not exported and not intended to be constructed directly, I wonder if it should be omitted from the docs?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I thought people might want to have the option to dispatch on it, in low-level uses cases and so on, that's why I made it party of the official API.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I assumed that this was the motivation. I wonder, however, how often one would actually want to dispatch on
FunctionWithInverse
. I assume for many cases it would be simpler to just useunwrap_f
(or something equivalent of the same name) andinverse
- similar to thesetinverse
case in this PR.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We may actually have occasion to do that in the pushforward measure in MeasureBase (in the context of JuliaMath/MeasureBase.jl#89, since
PushforwardMeasure
stores both forward and inverse function for performance reasons). Not sure yet - but it's an interesting option (we may want to extract forward and inverse from FunctionWithInverse). So there's at least one possible use case, so there may be more. And it doesn't cost us anything, I wouldn't expectFunctionWithInverse
to change in breaking ways in the future.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To obtain these you don't have to specialize on
FunctionWithInverse
though. You could just store_unwrap_f(f)
andinverse(f)
if an arbitraryf
is provided.