Skip to content

Commit

Permalink
Merge pull request #36 from JuliaDiffEq/myb/depwarn
Browse files Browse the repository at this point in the history
Fix all depwarns for Julia 0.7
  • Loading branch information
ChrisRackauckas committed Jun 30, 2018
2 parents e500138 + 61789aa commit 681f48a
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 55 deletions.
2 changes: 2 additions & 0 deletions src/DiffEqDiffTools.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ __precompile__()

module DiffEqDiffTools

using LinearAlgebra

include("function_wrappers.jl")
include("finitediff.jl")
include("derivatives.jl")
Expand Down
12 changes: 6 additions & 6 deletions src/derivatives.jl
Original file line number Diff line number Diff line change
Expand Up @@ -37,22 +37,22 @@ function DerivativeCache(
end

if fdtype!=Val{:forward} && typeof(fx)!=Nothing
warn("Pre-computed function values are only useful for fdtype==Val{:forward}.")
@warn("Pre-computed function values are only useful for fdtype==Val{:forward}.")
_fx = nothing
else
# more runtime sanity checks?
_fx = fx
end

if typeof(epsilon)!=Nothing && typeof(x)<:StridedArray && typeof(fx)<:Union{Nothing,StridedArray} && 1==2
warn("StridedArrays don't benefit from pre-allocating epsilon.")
@warn("StridedArrays don't benefit from pre-allocating epsilon.")
_epsilon = nothing
elseif typeof(epsilon)!=Nothing && fdtype==Val{:complex}
warn("Val{:complex} makes the epsilon array redundant.")
@warn("Val{:complex} makes the epsilon array redundant.")
_epsilon = nothing
else
if typeof(epsilon)==Nothing || eltype(epsilon)!=real(eltype(x))
epsilon = zeros(real(eltype(x)), size(x))
epsilon = fill(zero(real(eltype(x))), size(x))
end
_epsilon = epsilon
end
Expand All @@ -70,7 +70,7 @@ function finite_difference_derivative(
fx :: Union{Nothing,AbstractArray{<:Number}} = nothing,
epsilon :: Union{Nothing,AbstractArray{<:Real}} = nothing) where {T1,T2}

df = zeros(returntype, size(x))
df = fill(zero(returntype), size(x))
finite_difference_derivative!(df, f, x, fdtype, returntype, fx, epsilon)
end

Expand Down Expand Up @@ -126,7 +126,7 @@ function finite_difference_derivative!(df::StridedArray, f, x::StridedArray,
@inbounds for i eachindex(x)
epsilon = compute_epsilon(Val{:forward}, x[i], epsilon_factor)
x_plus = x[i] + epsilon
if typeof(fx) == Void
if typeof(fx) == Nothing
df[i] = (f(x_plus) - f(x[i])) / epsilon
else
df[i] = (f(x_plus) - fx[i]) / epsilon
Expand Down
36 changes: 18 additions & 18 deletions src/gradients.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,21 @@ function GradientCache(
if fdtype!=Val{:complex} # complex-mode FD only needs one cache, for x+eps*im
if typeof(x)<:StridedVector
if eltype(df)<:Complex && !(eltype(x)<:Complex)
_c1 = zeros(Complex{eltype(x)}, size(x))
_c1 = fill(zero(Complex{eltype(x)}), size(x))
_c2 = nothing
else
_c1 = nothing
_c2 = nothing
end
else
_c1 = similar(x)
_c2 = zeros(real(eltype(x)), size(x))
_c2 = fill(zero(real(eltype(x))), size(x))
end
else
if !(returntype<:Real)
fdtype_error(returntype)
else
_c1 = x + 0*im
_c1 = x .+ 0*im
_c2 = nothing
end
end
Expand All @@ -39,7 +39,7 @@ function GradientCache(
_c1 = similar(df)
_c2 = similar(df)
else
_c1 = zeros(Complex{eltype(x)}, size(df))
_c1 = fill(zero(Complex{eltype(x)}), size(df))
_c2 = nothing
end
end
Expand All @@ -58,7 +58,7 @@ function GradientCache(
inplace :: Type{Val{T3}} = Val{true}) where {T1,T2,T3}

if fdtype!=Val{:forward} && typeof(fx)!=Nothing
warn("Pre-computed function values are only useful for fdtype == Val{:forward}.")
@warn("Pre-computed function values are only useful for fdtype == Val{:forward}.")
_fx = nothing
else
# more runtime sanity checks?
Expand All @@ -70,13 +70,13 @@ function GradientCache(
if fdtype!=Val{:complex} # complex-mode FD only needs one cache, for x+eps*im
if typeof(x)<:StridedVector
if eltype(df)<:Complex && !(eltype(x)<:Complex)
_c1 = zeros(Complex{eltype(x)}, size(x))
_c1 = fill(zero(Complex{eltype(x)}), size(x))
_c2 = nothing
else
_c1 = nothing
_c2 = nothing
if typeof(c1)!=Nothing || typeof(c2)!=Nothing
warn("For StridedVectors, neither c1 nor c2 are necessary.")
@warn("For StridedVectors, neither c1 nor c2 are necessary.")
end
end
else
Expand Down Expand Up @@ -112,7 +112,7 @@ function finite_difference_gradient(f, x, fdtype::Type{T1}=Val{:central},
c2::Union{Nothing,AbstractArray{<:Number}}=nothing) where {T1,T2,T3}

if typeof(x) <: AbstractArray
df = zeros(returntype, size(x))
df = fill(zero(returntype), size(x))
else
if inplace == Val{true}
if typeof(fx)==Nothing && typeof(c1)==Nothing && typeof(c2)==Nothing
Expand Down Expand Up @@ -146,9 +146,9 @@ function finite_difference_gradient(f,x,
cache::GradientCache{T1,T2,T3,fdtype,returntype,inplace}) where {T1,T2,T3,fdtype,returntype,inplace}

if typeof(x) <: AbstractArray
df = zeros(returntype, size(x))
df = fill(zero(returntype), size(x))
else
df = zeros(cache.c1)
df = zero(cache.c1)
end
finite_difference_gradient!(df,f,x,cache)
df
Expand All @@ -165,14 +165,14 @@ function finite_difference_gradient!(df::AbstractArray{<:Number}, f, x::Abstract
if fdtype != Val{:complex}
epsilon_factor = compute_epsilon_factor(fdtype, eltype(x))
@. c2 = compute_epsilon(fdtype, x, epsilon_factor)
copy!(c1,x)
copyto!(c1,x)
end
if fdtype == Val{:forward}
@inbounds for i eachindex(x)
epsilon = c2[i]
c1_old = c1[i]
c1[i] += epsilon
if typeof(fx) != Void
if typeof(fx) != Nothing
dfi = (f(c1) - fx) / epsilon
else
fx0 = f(x)
Expand All @@ -182,7 +182,7 @@ function finite_difference_gradient!(df::AbstractArray{<:Number}, f, x::Abstract
c1[i] = c1_old
if eltype(df)<:Complex
c1[i] += im * epsilon
if typeof(fx) != Void
if typeof(fx) != Nothing
dfi = (f(c1) - fx) / (im*epsilon)
else
dfi = (f(c1) - fx0) / (im*epsilon)
Expand Down Expand Up @@ -210,7 +210,7 @@ function finite_difference_gradient!(df::AbstractArray{<:Number}, f, x::Abstract
end
end
elseif fdtype == Val{:complex} && returntype <: Real
copy!(c1,x)
copyto!(c1,x)
epsilon_complex = eps(real(eltype(x)))
# we use c1 here to avoid typing issues with x
@inbounds for i eachindex(x)
Expand All @@ -228,13 +228,13 @@ end
function finite_difference_gradient!(df::StridedVector{<:Number}, f, x::StridedVector{<:Number},
cache::GradientCache{T1,T2,T3,fdtype,returntype,inplace}) where {T1,T2,T3,fdtype,returntype,inplace}

# c1 is x1 if we need a complex copy of x, otherwise Void
# c2 is Void
# c1 is x1 if we need a complex copy of x, otherwise Nothing
# c2 is Nothing
fx, c1, c2 = cache.fx, cache.c1, cache.c2
if fdtype != Val{:complex}
epsilon_factor = compute_epsilon_factor(fdtype, eltype(x))
if eltype(df)<:Complex && !(eltype(x)<:Complex)
copy!(c1,x)
copyto!(c1,x)
end
end
if fdtype == Val{:forward}
Expand Down Expand Up @@ -302,7 +302,7 @@ function finite_difference_gradient!(df::StridedVector{<:Number}, f, x::StridedV
end
end
elseif fdtype==Val{:complex} && returntype<:Real && eltype(df)<:Real && eltype(x)<:Real
copy!(c1,x)
copyto!(c1,x)
epsilon_complex = eps(real(eltype(x)))
# we use c1 here to avoid typing issues with x
@inbounds for i eachindex(x)
Expand Down
16 changes: 8 additions & 8 deletions src/jacobians.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ function JacobianCache(
returntype :: Type{T2} = eltype(x),
inplace :: Type{Val{T3}} = Val{true}) where {T1,T2,T3}
if eltype(x) <: Real && fdtype==Val{:complex}
x1 = zeros(Complex{eltype(x)}, size(x))
_fx = zeros(Complex{eltype(x)}, size(x))
x1 = fill(zero(Complex{eltype(x)}), size(x))
_fx = fill(zero(Complex{eltype(x)}), size(x))
else
x1 = similar(x)
_fx = similar(x)
Expand All @@ -34,13 +34,13 @@ function JacobianCache(
inplace :: Type{Val{T3}} = Val{true}) where {T1,T2,T3}

if eltype(x) <: Real && fdtype==Val{:complex}
x1 = zeros(Complex{eltype(x)}, size(x))
x1 = fill(zero(Complex{eltype(x)}), size(x))
else
x1 = similar(x)
end

if eltype(fx) <: Real && fdtype==Val{:complex}
_fx = zeros(Complex{eltype(x)}, size(fx))
_fx = fill(zero(Complex{eltype(x)}), size(fx))
else
_fx = similar(fx)
end
Expand All @@ -66,12 +66,12 @@ function JacobianCache(
!(returntype<:Real) && fdtype_error(returntype)

if eltype(fx) <: Real
_fx = zeros(Complex{eltype(x)}, size(fx))
_fx = fill(zero(Complex{eltype(x)}), size(fx))
else
_fx = fx
end
if eltype(x1) <: Real
_x1 = zeros(Complex{eltype(x)}, size(x1))
_x1 = fill(zero(Complex{eltype(x)}), size(x1))
else
_x1 = x1
end
Expand All @@ -94,7 +94,7 @@ function finite_difference_jacobian(f, x::AbstractArray{<:Number},
end

function finite_difference_jacobian(f,x,cache::JacobianCache)
J = zeros(eltype(x), length(x), length(x))
J = fill(zero(eltype(x)), length(x), length(x))
finite_difference_jacobian!(J,f,x,cache)
J
end
Expand All @@ -105,7 +105,7 @@ function finite_difference_jacobian!(J::AbstractMatrix{<:Number},

m, n = size(J)
x1, fx, fx1 = cache.x1, cache.fx, cache.fx1
copy!(x1, x)
copyto!(x1, x)
vfx = vec(fx)
if fdtype == Val{:forward}
vfx1 = vec(fx1)
Expand Down
40 changes: 20 additions & 20 deletions test/finitedifftests.jl
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
using DiffEqDiffTools, Test
using DiffEqDiffTools, Test, LinearAlgebra

# TODO: add tests for GPUArrays
# TODO: add tests for DEDataArrays

# Derivative tests
x = collect(range(-2π, stop=2π, length=100))
y = sin.(x)
df = zeros(100)
epsilon = zeros(100)
df = fill(0., 100)
epsilon = fill(0., 100)
df_ref = cos.(x)
forward_cache = DiffEqDiffTools.DerivativeCache(x, y, epsilon, Val{:forward})
central_cache = DiffEqDiffTools.DerivativeCache(x, nothing, epsilon, Val{:central})
Expand Down Expand Up @@ -46,7 +46,7 @@ end
x = x + im*x
f(x) = sin(x) + cos(x)
y = f.(x)
df = zeros(x)
df = zero(x)
epsilon = similar(real(x))
df_ref = cos.(x) - sin.(x)
forward_cache = DiffEqDiffTools.DerivativeCache(x, y, epsilon, Val{:forward})
Expand Down Expand Up @@ -83,7 +83,7 @@ end
x = collect(range(-2π, stop=2π, length=100))
f(x) = sin(x) + im*cos(x)
y = f.(x)
df = zeros(Complex{eltype(x)}, size(x))
df = fill(zero(Complex{eltype(x)}), size(x))
epsilon = similar(real(x))
df_ref = cos.(x) - im*sin.(x)
forward_cache = DiffEqDiffTools.DerivativeCache(x, y, epsilon, Val{:forward}, eltype(df))
Expand Down Expand Up @@ -121,7 +121,7 @@ end
x = x + im*x
f(x) = abs2(x)
y = f.(x)
df = zeros(eltype(x), size(x))
df = fill(zero(eltype(x)), size(x))
epsilon = similar(real(x))
df_ref = 2*conj.(x)
forward_cache = DiffEqDiffTools.DerivativeCache(x, y, epsilon, Val{:forward}, eltype(df))
Expand Down Expand Up @@ -161,7 +161,7 @@ end
f(x) = 2x[1] + x[2]^2
x = rand(2)
fx = f(x)
df = zeros(2)
df = fill(0.,2)
df_ref = [2., 2*x[2]]
forward_cache = DiffEqDiffTools.GradientCache(df,x,Val{:forward})
central_cache = DiffEqDiffTools.GradientCache(df,x,Val{:central})
Expand All @@ -184,7 +184,7 @@ end
f(x) = 2x[1] + im*2x[1] + x[2]^2
x = x + im*x
fx = f(x)
df = zeros(x)
df = zero(x)
df_ref = conj([2.0+2.0*im, 2.0*x[2]])
forward_cache = DiffEqDiffTools.GradientCache(df,x,Val{:forward})
central_cache = DiffEqDiffTools.GradientCache(df,x,Val{:central})
Expand All @@ -203,7 +203,7 @@ end
f(x) = sum(abs2, x)
x = ones(2) * (1 + im)
fx = f(x)
df = zeros(x)
df = zero(x)
df_ref = 2*x
forward_cache = DiffEqDiffTools.GradientCache(df,x,Val{:forward})
central_cache = DiffEqDiffTools.GradientCache(df,x,Val{:central})
Expand All @@ -222,7 +222,7 @@ end
f(x) = 2*x[1] + im*x[2]^2
x = ones(2)
fx = f(x)
df = zeros(eltype(fx), size(x))
df = fill(zero(eltype(fx)), size(x))
df_ref = [2.0, -im*2*x[2]]
forward_cache = DiffEqDiffTools.GradientCache(df,x,Val{:forward},eltype(df))
central_cache = DiffEqDiffTools.GradientCache(df,x,Val{:central},eltype(df))
Expand All @@ -240,9 +240,9 @@ end

f(df,x) = (df[1]=sin(x); df[2]=cos(x); df)
x = 2π * rand()
fx = zeros(2)
fx = fill(0.,2)
f(fx,x)
df = zeros(2)
df = fill(0.,2)
df_ref = [cos(x), -sin(x)]
forward_cache = DiffEqDiffTools.GradientCache(df,x,Val{:forward})
central_cache = DiffEqDiffTools.GradientCache(df,x,Val{:central})
Expand All @@ -266,9 +266,9 @@ end

f(df,x) = (df[1]=sin(x); df[2]=cos(x); df)
x = (2π * rand()) * (1 + im)
fx = zeros(typeof(x), 2)
fx = fill(zero(typeof(x)), 2)
f(fx,x)
df = zeros(fx)
df = zero(fx)
df_ref = [cos(x), -sin(x)]
forward_cache = DiffEqDiffTools.GradientCache(df,x,Val{:forward})
central_cache = DiffEqDiffTools.GradientCache(df,x,Val{:central})
Expand All @@ -292,10 +292,10 @@ end
x = rand(2); y = rand(2)
f(y,x)
J_ref = [[-7+x[2]^3 3*(3+x[1])*x[2]^2]; [exp(x[1])*x[2]*cos(1-exp(x[1])*x[2]) exp(x[1])*cos(1-exp(x[1])*x[2])]]
J = zeros(J_ref)
df = zeros(x)
J = zero(J_ref)
df = zero(x)
df_ref = diag(J_ref)
epsilon = zeros(x)
epsilon = zero(x)
forward_cache = DiffEqDiffTools.JacobianCache(x,Val{:forward})
central_cache = DiffEqDiffTools.JacobianCache(x)
complex_cache = DiffEqDiffTools.JacobianCache(x,Val{:complex})
Expand All @@ -315,10 +315,10 @@ x = rand(2) + im*rand(2)
y = similar(x)
f(y,x)
J_ref = [[im*(-7+x[2]^3) 3*(3+im*x[1])*x[2]^2]; [exp(x[1])*x[2]*cos(1-exp(x[1])*x[2]) exp(x[1])*cos(1-exp(x[1])*x[2])]]
J = zeros(J_ref)
df = zeros(x)
J = zero(J_ref)
df = zero(x)
df_ref = diag(J_ref)
epsilon = zeros(real.(x))
epsilon = zero(real.(x))
forward_cache = DiffEqDiffTools.JacobianCache(x,Val{:forward})
central_cache = DiffEqDiffTools.JacobianCache(x)

Expand Down
6 changes: 3 additions & 3 deletions test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using DiffEqDiffTools
using Test
using Test, LinearAlgebra

tic()
@time begin
include("finitedifftests.jl")
toc()
end

0 comments on commit 681f48a

Please sign in to comment.