-
Notifications
You must be signed in to change notification settings - Fork 39
add finite_difference_jvp
#191
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
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
c37c302
add `finite_difference_jvp`
oscardssmith f0ac3bd
include file
oscardssmith 82fc83a
typo
oscardssmith b0922f6
typo
oscardssmith aa5b954
actually add cache
oscardssmith 37ccbee
fix sqrt sign
oscardssmith 7d4dfdb
typo
oscardssmith 02286bc
add tests
oscardssmith 062facc
don\'t assume mutability for out of place
oscardssmith c21186b
typo
oscardssmith 39767f4
Update src/jvp.jl
ChrisRackauckas 59b1ebb
Update src/jvp.jl
ChrisRackauckas 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,196 @@ | ||
mutable struct JVPCache{X1, FX1, FDType} | ||
x1 :: X1 | ||
fx1 :: FX1 | ||
end | ||
|
||
""" | ||
FiniteDiff.JVPCache( | ||
x, | ||
fdtype :: Type{T1} = Val{:forward}) | ||
|
||
Allocating Cache Constructor. | ||
""" | ||
function JVPCache( | ||
x, | ||
fdtype::Union{Val{FD},Type{FD}} = Val(:forward)) where {FD} | ||
fdtype isa Type && (fdtype = fdtype()) | ||
JVPCache{typeof(x), typeof(x), fdtype}(copy(x), copy(x)) | ||
end | ||
|
||
""" | ||
FiniteDiff.JVPCache( | ||
x, | ||
fx1, | ||
fdtype :: Type{T1} = Val{:forward}, | ||
|
||
Non-Allocating Cache Constructor. | ||
""" | ||
function JVPCache( | ||
x, | ||
fx, | ||
fdtype::Union{Val{FD},Type{FD}} = Val(:forward)) where {FD} | ||
fdtype isa Type && (fdtype = fdtype()) | ||
JVPCache{typeof(x), typeof(fx), fdtype}(x,fx) | ||
end | ||
|
||
""" | ||
FiniteDiff.finite_difference_jvp( | ||
f, | ||
x :: AbstractArray{<:Number}, | ||
v :: AbstractArray{<:Number}, | ||
fdtype :: Type{T1}=Val{:central}, | ||
relstep=default_relstep(fdtype, eltype(x)), | ||
absstep=relstep) | ||
|
||
Cache-less. | ||
""" | ||
function finite_difference_jvp(f, x, v, | ||
fdtype = Val(:forward), | ||
f_in = nothing; | ||
relstep=default_relstep(fdtype, eltype(x)), | ||
absstep=relstep, | ||
dir=true) | ||
|
||
if f_in isa Nothing | ||
fx = f(x) | ||
else | ||
fx = f_in | ||
end | ||
cache = JVPCache(x, fx, fdtype) | ||
finite_difference_jvp(f, x, v, cache, fx; relstep, absstep, dir) | ||
end | ||
|
||
""" | ||
FiniteDiff.finite_difference_jvp( | ||
f, | ||
x, | ||
v, | ||
cache::JVPCache; | ||
relstep=default_relstep(fdtype, eltype(x)), | ||
absstep=relstep, | ||
|
||
Cached. | ||
""" | ||
function finite_difference_jvp( | ||
f, | ||
x, | ||
v, | ||
cache::JVPCache{X1, FX1, fdtype}, | ||
f_in=nothing; | ||
relstep=default_relstep(fdtype, eltype(x)), | ||
absstep=relstep, | ||
dir=true) where {X1, FX1, fdtype} | ||
|
||
if fdtype == Val(:complex) | ||
ArgumentError("finite_difference_jvp doesn't support :complex-mode finite diff") | ||
end | ||
|
||
tmp = sqrt(abs(dot(_vec(x), _vec(v)))) | ||
epsilon = compute_epsilon(fdtype, tmp, relstep, absstep, dir) | ||
if fdtype == Val(:forward) | ||
fx = f_in isa Nothing ? f(x) : f_in | ||
x1 = @. x + epsilon * v | ||
fx1 = f(x1) | ||
fx1 = @. (fx1-fx)/epsilon | ||
elseif fdtype == Val(:central) | ||
x1 = @. x + epsilon * v | ||
fx1 = f(x1) | ||
x1 = @. x - epsilon * v | ||
fx = f(x1) | ||
fx1 = @. (fx1-fx)/(2epsilon) | ||
else | ||
fdtype_error(eltype(x)) | ||
end | ||
fx1 | ||
end | ||
|
||
""" | ||
finite_difference_jvp!( | ||
jvp::AbstractArray{<:Number}, | ||
f, | ||
x::AbstractArray{<:Number}, | ||
v::AbstractArray{<:Number}, | ||
fdtype :: Type{T1}=Val{:forward}, | ||
returntype :: Type{T2}=eltype(x), | ||
f_in :: Union{T2,Nothing}=nothing; | ||
relstep=default_relstep(fdtype, eltype(x)), | ||
absstep=relstep) | ||
|
||
Cache-less. | ||
""" | ||
function finite_difference_jvp!(jvp, | ||
f, | ||
x, | ||
v, | ||
fdtype = Val(:forward), | ||
f_in = nothing; | ||
relstep=default_relstep(fdtype, eltype(x)), | ||
absstep=relstep) | ||
if !isnothing(f_in) | ||
cache = JVPCache(x, f_in, fdtype) | ||
elseif fdtype == Val(:forward) | ||
fx = zero(x) | ||
f(fx,x) | ||
cache = JVPCache(x, fx, fdtype) | ||
else | ||
cache = JVPCache(x, fdtype) | ||
end | ||
finite_difference_jvp!(jvp, f, x, v, cache, cache.fx1; relstep, absstep) | ||
end | ||
|
||
""" | ||
FiniteDiff.finite_difference_jvp!( | ||
jvp::AbstractArray{<:Number}, | ||
f, | ||
x::AbstractArray{<:Number}, | ||
v::AbstractArray{<:Number}, | ||
cache::JVPCache; | ||
relstep=default_relstep(fdtype, eltype(x)), | ||
absstep=relstep,) | ||
|
||
Cached. | ||
""" | ||
function finite_difference_jvp!( | ||
jvp, | ||
f, | ||
x, | ||
v, | ||
cache::JVPCache{X1, FX1, fdtype}, | ||
f_in = nothing; | ||
relstep = default_relstep(fdtype, eltype(x)), | ||
absstep = relstep, | ||
dir = true) where {X1, FX1, fdtype} | ||
|
||
if fdtype == Val(:complex) | ||
ArgumentError("finite_difference_jvp doesn't support :complex-mode finite diff") | ||
end | ||
|
||
(;x1, fx1) = cache | ||
tmp = sqrt(abs(dot(_vec(x), _vec(v)))) | ||
epsilon = compute_epsilon(fdtype, tmp, relstep, absstep, dir) | ||
if fdtype == Val(:forward) | ||
if f_in isa Nothing | ||
f(fx1, x) | ||
else | ||
fx1 = f_in | ||
end | ||
@. x1 = x + epsilon * v | ||
f(jvp, x1) | ||
@. jvp = (jvp-fx1)/epsilon | ||
elseif fdtype == Val(:central) | ||
@. x1 = x - epsilon * v | ||
f(fx1, x1) | ||
@. x1 = x + epsilon * v | ||
f(jvp, x1) | ||
@. jvp = (jvp-fx1)/(2epsilon) | ||
else | ||
fdtype_error(eltype(x)) | ||
end | ||
nothing | ||
end | ||
|
||
function resize!(cache::JVPCache, i::Int) | ||
resize!(cache.x1, i) | ||
cache.fx1 !== nothing && resize!(cache.fx1, i) | ||
nothing | ||
end |
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
Oops, something went wrong.
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.
well it does make a copy of
x
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.
yes the non-allocating cache is expected to just use the arrays as given 😅