Skip to content

Commit 61789aa

Browse files
committed
Fix all depwarns for Julia 0.7
1 parent e500138 commit 61789aa

File tree

6 files changed

+57
-55
lines changed

6 files changed

+57
-55
lines changed

src/DiffEqDiffTools.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ __precompile__()
22

33
module DiffEqDiffTools
44

5+
using LinearAlgebra
6+
57
include("function_wrappers.jl")
68
include("finitediff.jl")
79
include("derivatives.jl")

src/derivatives.jl

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,22 +37,22 @@ function DerivativeCache(
3737
end
3838

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

4747
if typeof(epsilon)!=Nothing && typeof(x)<:StridedArray && typeof(fx)<:Union{Nothing,StridedArray} && 1==2
48-
warn("StridedArrays don't benefit from pre-allocating epsilon.")
48+
@warn("StridedArrays don't benefit from pre-allocating epsilon.")
4949
_epsilon = nothing
5050
elseif typeof(epsilon)!=Nothing && fdtype==Val{:complex}
51-
warn("Val{:complex} makes the epsilon array redundant.")
51+
@warn("Val{:complex} makes the epsilon array redundant.")
5252
_epsilon = nothing
5353
else
5454
if typeof(epsilon)==Nothing || eltype(epsilon)!=real(eltype(x))
55-
epsilon = zeros(real(eltype(x)), size(x))
55+
epsilon = fill(zero(real(eltype(x))), size(x))
5656
end
5757
_epsilon = epsilon
5858
end
@@ -70,7 +70,7 @@ function finite_difference_derivative(
7070
fx :: Union{Nothing,AbstractArray{<:Number}} = nothing,
7171
epsilon :: Union{Nothing,AbstractArray{<:Real}} = nothing) where {T1,T2}
7272

73-
df = zeros(returntype, size(x))
73+
df = fill(zero(returntype), size(x))
7474
finite_difference_derivative!(df, f, x, fdtype, returntype, fx, epsilon)
7575
end
7676

@@ -126,7 +126,7 @@ function finite_difference_derivative!(df::StridedArray, f, x::StridedArray,
126126
@inbounds for i eachindex(x)
127127
epsilon = compute_epsilon(Val{:forward}, x[i], epsilon_factor)
128128
x_plus = x[i] + epsilon
129-
if typeof(fx) == Void
129+
if typeof(fx) == Nothing
130130
df[i] = (f(x_plus) - f(x[i])) / epsilon
131131
else
132132
df[i] = (f(x_plus) - fx[i]) / epsilon

src/gradients.jl

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,21 +15,21 @@ function GradientCache(
1515
if fdtype!=Val{:complex} # complex-mode FD only needs one cache, for x+eps*im
1616
if typeof(x)<:StridedVector
1717
if eltype(df)<:Complex && !(eltype(x)<:Complex)
18-
_c1 = zeros(Complex{eltype(x)}, size(x))
18+
_c1 = fill(zero(Complex{eltype(x)}), size(x))
1919
_c2 = nothing
2020
else
2121
_c1 = nothing
2222
_c2 = nothing
2323
end
2424
else
2525
_c1 = similar(x)
26-
_c2 = zeros(real(eltype(x)), size(x))
26+
_c2 = fill(zero(real(eltype(x))), size(x))
2727
end
2828
else
2929
if !(returntype<:Real)
3030
fdtype_error(returntype)
3131
else
32-
_c1 = x + 0*im
32+
_c1 = x .+ 0*im
3333
_c2 = nothing
3434
end
3535
end
@@ -39,7 +39,7 @@ function GradientCache(
3939
_c1 = similar(df)
4040
_c2 = similar(df)
4141
else
42-
_c1 = zeros(Complex{eltype(x)}, size(df))
42+
_c1 = fill(zero(Complex{eltype(x)}), size(df))
4343
_c2 = nothing
4444
end
4545
end
@@ -58,7 +58,7 @@ function GradientCache(
5858
inplace :: Type{Val{T3}} = Val{true}) where {T1,T2,T3}
5959

6060
if fdtype!=Val{:forward} && typeof(fx)!=Nothing
61-
warn("Pre-computed function values are only useful for fdtype == Val{:forward}.")
61+
@warn("Pre-computed function values are only useful for fdtype == Val{:forward}.")
6262
_fx = nothing
6363
else
6464
# more runtime sanity checks?
@@ -70,13 +70,13 @@ function GradientCache(
7070
if fdtype!=Val{:complex} # complex-mode FD only needs one cache, for x+eps*im
7171
if typeof(x)<:StridedVector
7272
if eltype(df)<:Complex && !(eltype(x)<:Complex)
73-
_c1 = zeros(Complex{eltype(x)}, size(x))
73+
_c1 = fill(zero(Complex{eltype(x)}), size(x))
7474
_c2 = nothing
7575
else
7676
_c1 = nothing
7777
_c2 = nothing
7878
if typeof(c1)!=Nothing || typeof(c2)!=Nothing
79-
warn("For StridedVectors, neither c1 nor c2 are necessary.")
79+
@warn("For StridedVectors, neither c1 nor c2 are necessary.")
8080
end
8181
end
8282
else
@@ -112,7 +112,7 @@ function finite_difference_gradient(f, x, fdtype::Type{T1}=Val{:central},
112112
c2::Union{Nothing,AbstractArray{<:Number}}=nothing) where {T1,T2,T3}
113113

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

148148
if typeof(x) <: AbstractArray
149-
df = zeros(returntype, size(x))
149+
df = fill(zero(returntype), size(x))
150150
else
151-
df = zeros(cache.c1)
151+
df = zero(cache.c1)
152152
end
153153
finite_difference_gradient!(df,f,x,cache)
154154
df
@@ -165,14 +165,14 @@ function finite_difference_gradient!(df::AbstractArray{<:Number}, f, x::Abstract
165165
if fdtype != Val{:complex}
166166
epsilon_factor = compute_epsilon_factor(fdtype, eltype(x))
167167
@. c2 = compute_epsilon(fdtype, x, epsilon_factor)
168-
copy!(c1,x)
168+
copyto!(c1,x)
169169
end
170170
if fdtype == Val{:forward}
171171
@inbounds for i eachindex(x)
172172
epsilon = c2[i]
173173
c1_old = c1[i]
174174
c1[i] += epsilon
175-
if typeof(fx) != Void
175+
if typeof(fx) != Nothing
176176
dfi = (f(c1) - fx) / epsilon
177177
else
178178
fx0 = f(x)
@@ -182,7 +182,7 @@ function finite_difference_gradient!(df::AbstractArray{<:Number}, f, x::Abstract
182182
c1[i] = c1_old
183183
if eltype(df)<:Complex
184184
c1[i] += im * epsilon
185-
if typeof(fx) != Void
185+
if typeof(fx) != Nothing
186186
dfi = (f(c1) - fx) / (im*epsilon)
187187
else
188188
dfi = (f(c1) - fx0) / (im*epsilon)
@@ -210,7 +210,7 @@ function finite_difference_gradient!(df::AbstractArray{<:Number}, f, x::Abstract
210210
end
211211
end
212212
elseif fdtype == Val{:complex} && returntype <: Real
213-
copy!(c1,x)
213+
copyto!(c1,x)
214214
epsilon_complex = eps(real(eltype(x)))
215215
# we use c1 here to avoid typing issues with x
216216
@inbounds for i eachindex(x)
@@ -228,13 +228,13 @@ end
228228
function finite_difference_gradient!(df::StridedVector{<:Number}, f, x::StridedVector{<:Number},
229229
cache::GradientCache{T1,T2,T3,fdtype,returntype,inplace}) where {T1,T2,T3,fdtype,returntype,inplace}
230230

231-
# c1 is x1 if we need a complex copy of x, otherwise Void
232-
# c2 is Void
231+
# c1 is x1 if we need a complex copy of x, otherwise Nothing
232+
# c2 is Nothing
233233
fx, c1, c2 = cache.fx, cache.c1, cache.c2
234234
if fdtype != Val{:complex}
235235
epsilon_factor = compute_epsilon_factor(fdtype, eltype(x))
236236
if eltype(df)<:Complex && !(eltype(x)<:Complex)
237-
copy!(c1,x)
237+
copyto!(c1,x)
238238
end
239239
end
240240
if fdtype == Val{:forward}
@@ -302,7 +302,7 @@ function finite_difference_gradient!(df::StridedVector{<:Number}, f, x::StridedV
302302
end
303303
end
304304
elseif fdtype==Val{:complex} && returntype<:Real && eltype(df)<:Real && eltype(x)<:Real
305-
copy!(c1,x)
305+
copyto!(c1,x)
306306
epsilon_complex = eps(real(eltype(x)))
307307
# we use c1 here to avoid typing issues with x
308308
@inbounds for i eachindex(x)

src/jacobians.jl

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ function JacobianCache(
1010
returntype :: Type{T2} = eltype(x),
1111
inplace :: Type{Val{T3}} = Val{true}) where {T1,T2,T3}
1212
if eltype(x) <: Real && fdtype==Val{:complex}
13-
x1 = zeros(Complex{eltype(x)}, size(x))
14-
_fx = zeros(Complex{eltype(x)}, size(x))
13+
x1 = fill(zero(Complex{eltype(x)}), size(x))
14+
_fx = fill(zero(Complex{eltype(x)}), size(x))
1515
else
1616
x1 = similar(x)
1717
_fx = similar(x)
@@ -34,13 +34,13 @@ function JacobianCache(
3434
inplace :: Type{Val{T3}} = Val{true}) where {T1,T2,T3}
3535

3636
if eltype(x) <: Real && fdtype==Val{:complex}
37-
x1 = zeros(Complex{eltype(x)}, size(x))
37+
x1 = fill(zero(Complex{eltype(x)}), size(x))
3838
else
3939
x1 = similar(x)
4040
end
4141

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

6868
if eltype(fx) <: Real
69-
_fx = zeros(Complex{eltype(x)}, size(fx))
69+
_fx = fill(zero(Complex{eltype(x)}), size(fx))
7070
else
7171
_fx = fx
7272
end
7373
if eltype(x1) <: Real
74-
_x1 = zeros(Complex{eltype(x)}, size(x1))
74+
_x1 = fill(zero(Complex{eltype(x)}), size(x1))
7575
else
7676
_x1 = x1
7777
end
@@ -94,7 +94,7 @@ function finite_difference_jacobian(f, x::AbstractArray{<:Number},
9494
end
9595

9696
function finite_difference_jacobian(f,x,cache::JacobianCache)
97-
J = zeros(eltype(x), length(x), length(x))
97+
J = fill(zero(eltype(x)), length(x), length(x))
9898
finite_difference_jacobian!(J,f,x,cache)
9999
J
100100
end
@@ -105,7 +105,7 @@ function finite_difference_jacobian!(J::AbstractMatrix{<:Number},
105105

106106
m, n = size(J)
107107
x1, fx, fx1 = cache.x1, cache.fx, cache.fx1
108-
copy!(x1, x)
108+
copyto!(x1, x)
109109
vfx = vec(fx)
110110
if fdtype == Val{:forward}
111111
vfx1 = vec(fx1)

test/finitedifftests.jl

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
using DiffEqDiffTools, Test
1+
using DiffEqDiffTools, Test, LinearAlgebra
22

33
# TODO: add tests for GPUArrays
44
# TODO: add tests for DEDataArrays
55

66
# Derivative tests
77
x = collect(range(-2π, stop=2π, length=100))
88
y = sin.(x)
9-
df = zeros(100)
10-
epsilon = zeros(100)
9+
df = fill(0., 100)
10+
epsilon = fill(0., 100)
1111
df_ref = cos.(x)
1212
forward_cache = DiffEqDiffTools.DerivativeCache(x, y, epsilon, Val{:forward})
1313
central_cache = DiffEqDiffTools.DerivativeCache(x, nothing, epsilon, Val{:central})
@@ -46,7 +46,7 @@ end
4646
x = x + im*x
4747
f(x) = sin(x) + cos(x)
4848
y = f.(x)
49-
df = zeros(x)
49+
df = zero(x)
5050
epsilon = similar(real(x))
5151
df_ref = cos.(x) - sin.(x)
5252
forward_cache = DiffEqDiffTools.DerivativeCache(x, y, epsilon, Val{:forward})
@@ -83,7 +83,7 @@ end
8383
x = collect(range(-2π, stop=2π, length=100))
8484
f(x) = sin(x) + im*cos(x)
8585
y = f.(x)
86-
df = zeros(Complex{eltype(x)}, size(x))
86+
df = fill(zero(Complex{eltype(x)}), size(x))
8787
epsilon = similar(real(x))
8888
df_ref = cos.(x) - im*sin.(x)
8989
forward_cache = DiffEqDiffTools.DerivativeCache(x, y, epsilon, Val{:forward}, eltype(df))
@@ -121,7 +121,7 @@ end
121121
x = x + im*x
122122
f(x) = abs2(x)
123123
y = f.(x)
124-
df = zeros(eltype(x), size(x))
124+
df = fill(zero(eltype(x)), size(x))
125125
epsilon = similar(real(x))
126126
df_ref = 2*conj.(x)
127127
forward_cache = DiffEqDiffTools.DerivativeCache(x, y, epsilon, Val{:forward}, eltype(df))
@@ -161,7 +161,7 @@ end
161161
f(x) = 2x[1] + x[2]^2
162162
x = rand(2)
163163
fx = f(x)
164-
df = zeros(2)
164+
df = fill(0.,2)
165165
df_ref = [2., 2*x[2]]
166166
forward_cache = DiffEqDiffTools.GradientCache(df,x,Val{:forward})
167167
central_cache = DiffEqDiffTools.GradientCache(df,x,Val{:central})
@@ -184,7 +184,7 @@ end
184184
f(x) = 2x[1] + im*2x[1] + x[2]^2
185185
x = x + im*x
186186
fx = f(x)
187-
df = zeros(x)
187+
df = zero(x)
188188
df_ref = conj([2.0+2.0*im, 2.0*x[2]])
189189
forward_cache = DiffEqDiffTools.GradientCache(df,x,Val{:forward})
190190
central_cache = DiffEqDiffTools.GradientCache(df,x,Val{:central})
@@ -203,7 +203,7 @@ end
203203
f(x) = sum(abs2, x)
204204
x = ones(2) * (1 + im)
205205
fx = f(x)
206-
df = zeros(x)
206+
df = zero(x)
207207
df_ref = 2*x
208208
forward_cache = DiffEqDiffTools.GradientCache(df,x,Val{:forward})
209209
central_cache = DiffEqDiffTools.GradientCache(df,x,Val{:central})
@@ -222,7 +222,7 @@ end
222222
f(x) = 2*x[1] + im*x[2]^2
223223
x = ones(2)
224224
fx = f(x)
225-
df = zeros(eltype(fx), size(x))
225+
df = fill(zero(eltype(fx)), size(x))
226226
df_ref = [2.0, -im*2*x[2]]
227227
forward_cache = DiffEqDiffTools.GradientCache(df,x,Val{:forward},eltype(df))
228228
central_cache = DiffEqDiffTools.GradientCache(df,x,Val{:central},eltype(df))
@@ -240,9 +240,9 @@ end
240240

241241
f(df,x) = (df[1]=sin(x); df[2]=cos(x); df)
242242
x = 2π * rand()
243-
fx = zeros(2)
243+
fx = fill(0.,2)
244244
f(fx,x)
245-
df = zeros(2)
245+
df = fill(0.,2)
246246
df_ref = [cos(x), -sin(x)]
247247
forward_cache = DiffEqDiffTools.GradientCache(df,x,Val{:forward})
248248
central_cache = DiffEqDiffTools.GradientCache(df,x,Val{:central})
@@ -266,9 +266,9 @@ end
266266

267267
f(df,x) = (df[1]=sin(x); df[2]=cos(x); df)
268268
x = (2π * rand()) * (1 + im)
269-
fx = zeros(typeof(x), 2)
269+
fx = fill(zero(typeof(x)), 2)
270270
f(fx,x)
271-
df = zeros(fx)
271+
df = zero(fx)
272272
df_ref = [cos(x), -sin(x)]
273273
forward_cache = DiffEqDiffTools.GradientCache(df,x,Val{:forward})
274274
central_cache = DiffEqDiffTools.GradientCache(df,x,Val{:central})
@@ -292,10 +292,10 @@ end
292292
x = rand(2); y = rand(2)
293293
f(y,x)
294294
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])]]
295-
J = zeros(J_ref)
296-
df = zeros(x)
295+
J = zero(J_ref)
296+
df = zero(x)
297297
df_ref = diag(J_ref)
298-
epsilon = zeros(x)
298+
epsilon = zero(x)
299299
forward_cache = DiffEqDiffTools.JacobianCache(x,Val{:forward})
300300
central_cache = DiffEqDiffTools.JacobianCache(x)
301301
complex_cache = DiffEqDiffTools.JacobianCache(x,Val{:complex})
@@ -315,10 +315,10 @@ x = rand(2) + im*rand(2)
315315
y = similar(x)
316316
f(y,x)
317317
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])]]
318-
J = zeros(J_ref)
319-
df = zeros(x)
318+
J = zero(J_ref)
319+
df = zero(x)
320320
df_ref = diag(J_ref)
321-
epsilon = zeros(real.(x))
321+
epsilon = zero(real.(x))
322322
forward_cache = DiffEqDiffTools.JacobianCache(x,Val{:forward})
323323
central_cache = DiffEqDiffTools.JacobianCache(x)
324324

test/runtests.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
using DiffEqDiffTools
2-
using Test
2+
using Test, LinearAlgebra
33

4-
tic()
4+
@time begin
55
include("finitedifftests.jl")
6-
toc()
6+
end

0 commit comments

Comments
 (0)