-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Preliminary work on complex derivatives complete, everything should a…
…t least work.
- Loading branch information
1 parent
73d249a
commit 4803b91
Showing
4 changed files
with
159 additions
and
83 deletions.
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 |
---|---|---|
@@ -1,62 +1,90 @@ | ||
function finite_difference!(df::AbstractArray{<:Real}, f, x::AbstractArray{<:Real}, | ||
fdtype::DataType, ::Type{Val{:Real}}, ::Type{Val{:DiffEqDerivativeWrapper}}, | ||
fx::Union{Void,AbstractArray{<:Real}}=nothing, epsilon::Union{Void,AbstractArray{<:Real}}=nothing, return_type::DataType=eltype(x)) | ||
function finite_difference!(df::AbstractArray{<:Number}, f, x::Union{Number,AbstractArray{<:Number}}, | ||
fdtype::DataType, funtype::DataType, ::Type{Val{:DiffEqDerivativeWrapper}}, | ||
fx::Union{Void,AbstractArray{<:Number}}=nothing, epsilon::Union{Void,AbstractArray{<:Real}}=nothing, return_type::DataType=eltype(x)) | ||
|
||
# TODO: test this one, and figure out what happens with epsilon | ||
fx1 = f.fx1 | ||
if fdtype == Val{:forward} | ||
epsilon = compute_epsilon(Val{:forward}, x) | ||
f(fx, x) | ||
f(fx1, x+epsilon) | ||
@. df = (fx1 - fx) / epsilon | ||
elseif fdtype == Val{:central} | ||
epsilon = compute_epsilon(Val{:central}, x) | ||
f(fx, x-epsilon) | ||
f(fx1, x+epsilon) | ||
@. df = (fx1 - fx) / (2 * epsilon) | ||
elseif fdtype == Val{:complex} | ||
epsilon = eps(eltype(x)) | ||
f(fx, f(x+im*epsilon)) | ||
@. df = imag(fx) / epsilon | ||
end | ||
# TODO: optimized implementations for specific wrappers using the added DiffEq caching where appopriate | ||
|
||
finite_difference!(df, f, x, fdtype, funtype, Val{:Default}, fx, epsilon, return_type) | ||
df | ||
end | ||
|
||
# AbstractArray{T} should be OK if JacobianWrapper is provided | ||
function finite_difference_jacobian!(J::AbstractArray{T}, f, x::StridedArray{T}, ::Type{Val{:forward}}, fx::StridedArray{T}, ::Type{Val{:JacobianWrapper}}) where T<:Real | ||
function finite_difference_jacobian!(J::AbstractMatrix{<:Real}, f, x::AbstractArray{<:Real}, | ||
fdtype::DataType, ::Type{Val{:Real}}, ::Type{Val{:JacobianWrapper}}, | ||
fx::AbstractArray{<:Real}, epsilon::Union{Void,AbstractArray{<:Real}}=nothing, return_type::DataType=eltype(x)) | ||
|
||
m, n = size(J) | ||
epsilon_factor = compute_epsilon_factor(Val{:forward}, T) | ||
epsilon_elemtype = compute_epsilon_elemtype(epsilon, x) | ||
x1, fx1 = f.x1, f.fx1 | ||
copy!(x1, x) | ||
copy!(fx1, fx) | ||
@inbounds for i in 1:n | ||
epsilon = compute_epsilon(Val{:forward}, x[i], epsilon_factor) | ||
epsilon_inv = one(T) / epsilon | ||
x1[i] += epsilon | ||
f(fx, x) | ||
f(fx1, x1) | ||
@. J[:,i] = (fx-fx1) * epsilon_inv | ||
x1[i] -= epsilon | ||
if fdtype == Val{:forward} | ||
epsilon_factor = compute_epsilon_factor(Val{:forward}, epsilon_elemtype) | ||
@inbounds for i ∈ 1:n | ||
epsilon = compute_epsilon(Val{:forward}, x[i], epsilon_factor) | ||
x1[i] += epsilon | ||
f(fx1, x1) | ||
f(fx, x) | ||
@. J[:,i] = (fx1 - fx) / epsilon | ||
x1[i] -= epsilon | ||
end | ||
elseif fdtype == Val{:central} | ||
epsilon_factor = compute_epsilon_factor(Val{:central}, epsilon_elemtype) | ||
@inbounds for i ∈ 1:n | ||
epsilon = compute_epsilon(Val{:central}, x[i], epsilon_factor) | ||
x1[i] += epsilon | ||
x[i] -= epsilon | ||
f(fx1, x1) | ||
f(fx, x) | ||
@. J[:,i] = (fx1 - fx) / (2*epsilon) | ||
x1[i] -= epsilon | ||
x[i] += epsilon | ||
end | ||
elseif fdtype == Val{:complex} | ||
x0 = Complex{eltype(x)}(x) | ||
epsilon = eps(eltype(x)) | ||
@inbounds for i ∈ 1:n | ||
x0[i] += im * epsilon | ||
@. J[:,i] = imag(f(x0)) / epsilon | ||
x0[i] -= im * epsilon | ||
end | ||
else | ||
fdtype_error(Val{:Real}) | ||
end | ||
J | ||
end | ||
|
||
function finite_difference_jacobian!(J::AbstractArray{T}, f, x::StridedArray{T}, ::Type{Val{:central}}, fx::StridedArray{T}, ::Type{Val{:JacobianWrapper}}) where T<:Real | ||
function finite_difference_jacobian!(J::AbstractMatrix{<:Number}, f, x::AbstractArray{<:Number}, | ||
fdtype::DataType, ::Type{Val{:Complex}}, ::Type{Val{:JacobianWrapper}}, | ||
fx::AbstractArray{<:Number}, epsilon::Union{Void,AbstractArray{<:Real}}=nothing, return_type::DataType=eltype(x)) | ||
|
||
# TODO: test this | ||
m, n = size(J) | ||
epsilon_factor = compute_epsilon_factor(Val{:central}, T) | ||
epsilon_elemtype = compute_epsilon_elemtype(epsilon, x) | ||
x1, fx1 = f.x1, f.fx1 | ||
copy!(x1, x) | ||
copy!(fx1, fx) | ||
@inbounds for i in 1:n | ||
epsilon = compute_epsilon(Val{:central}, x[i], epsilon_factor) | ||
epsilon_double_inv = one(T) / (2 * epsilon) | ||
x[i] += epsilon | ||
x1[i] -= epsilon | ||
f(fx, x) | ||
f(fx1, x1) | ||
@. J[:,i] = (fx-fx1) * epsilon_double_inv | ||
x[i] -= epsilon | ||
x1[i] += epsilon | ||
if fdtype == Val{:forward} | ||
epsilon_factor = compute_epsilon_factor(Val{:forward}, epsilon_elemtype) | ||
@inbounds for i ∈ 1:n | ||
epsilon = compute_epsilon(Val{:forward}, real(x[i]), epsilon_factor) | ||
x1[i] += epsilon | ||
f(fx1, x1) | ||
f(fx, x) | ||
@. J[:,i] = ( real( (fx1 - fx) ) + im*imag( (fx1 - fx) ) ) / epsilon | ||
x1[i] -= epsilon | ||
end | ||
elseif fdtype == Val{:central} | ||
epsilon_factor = compute_epsilon_factor(Val{:central}, epsilon_elemtype) | ||
@inbounds for i ∈ 1:n | ||
epsilon = compute_epsilon(Val{:central}, real(x[i]), epsilon_factor) | ||
x1[i] += epsilon | ||
x[i] -= epsilon | ||
f(fx1, x1) | ||
f(fx, x) | ||
@. J[:,i] = ( real( (fx1 - fx) ) + im*imag( fx1 - fx ) ) / (2*epsilon) | ||
x1[i] -= epsilon | ||
x[i] += epsilon | ||
end | ||
else | ||
fdtype_error(Val{:Complex}) | ||
end | ||
J | ||
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
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