Skip to content

Commit 25162e1

Browse files
authored
Merge pull request #100 from JuliaDiff/myb/fix
Remove unnecessary type constraints
2 parents 4f3778e + eccf189 commit 25162e1

6 files changed

Lines changed: 76 additions & 128 deletions

File tree

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name = "FiniteDiff"
22
uuid = "6a86dc24-6348-571c-b903-95158fe2bd41"
3-
version = "2.3.1"
3+
version = "2.3.2"
44

55
[deps]
66
ArrayInterface = "4fba245c-0d91-5ea0-9b3e-6abc04ee57a9"

src/derivatives.jl

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@ Single-point derivatives of scalar->scalar maps.
44
function finite_difference_derivative(
55
f,
66
x::T,
7-
fdtype::Type{T1}=Val{:central},
8-
returntype::Type{T2}=eltype(x),
9-
f_x::Union{Nothing,T}=nothing;
7+
fdtype=Val{:central},
8+
returntype=eltype(x),
9+
f_x=nothing;
1010
relstep=default_relstep(fdtype, T),
1111
absstep=relstep,
12-
dir=true) where {T<:Number,T1,T2}
12+
dir=true) where {T<:Number}
1313

1414
epsilon = compute_epsilon(fdtype, x, relstep, absstep, dir)
1515
if fdtype==Val{:forward}
@@ -71,37 +71,37 @@ Compute the derivative df of a scalar-valued map f at a collection of points x.
7171
=#
7272
function finite_difference_derivative(
7373
f,
74-
x :: AbstractArray{<:Number},
75-
fdtype :: Type{T1} = Val{:central},
76-
returntype :: Type{T2} = eltype(x), # return type of f
77-
fx :: Union{Nothing,AbstractArray{<:Number}} = nothing,
78-
epsilon :: Union{Nothing,AbstractArray{<:Real}} = nothing;
74+
x,
75+
fdtype = Val{:central},
76+
returntype = eltype(x), # return type of f
77+
fx = nothing,
78+
epsilon = nothing;
7979
relstep=default_relstep(fdtype, eltype(x)),
80-
absstep=relstep) where {T1,T2}
80+
absstep=relstep)
8181

8282
df = fill(zero(returntype), size(x))
8383
finite_difference_derivative!(df, f, x, fdtype, returntype, fx, epsilon; relstep=relstep, absstep=absstep)
8484
end
8585

8686
function finite_difference_derivative!(
87-
df :: AbstractArray{<:Number},
87+
df,
8888
f,
89-
x :: AbstractArray{<:Number},
90-
fdtype :: Type{T1} = Val{:central},
91-
returntype :: Type{T2} = eltype(x),
92-
fx :: Union{Nothing,AbstractArray{<:Number}} = nothing,
93-
epsilon :: Union{Nothing,AbstractArray{<:Real}} = nothing;
89+
x,
90+
fdtype = Val{:central},
91+
returntype = eltype(x),
92+
fx = nothing,
93+
epsilon = nothing;
9494
relstep=default_relstep(fdtype, eltype(x)),
95-
absstep=relstep) where {T1,T2}
95+
absstep=relstep)
9696

9797
cache = DerivativeCache(x, fx, epsilon, fdtype, returntype)
9898
finite_difference_derivative!(df, f, x, cache; relstep=relstep, absstep=absstep)
9999
end
100100

101101
function finite_difference_derivative!(
102-
df::AbstractArray{<:Number},
102+
df,
103103
f,
104-
x::AbstractArray{<:Number},
104+
x,
105105
cache::DerivativeCache{T1,T2,fdtype,returntype};
106106
relstep=default_relstep(fdtype, eltype(x)),
107107
absstep=relstep,

src/gradients.jl

Lines changed: 22 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ struct GradientCache{CacheType1, CacheType2, CacheType3, fdtype, returntype, inp
55
end
66

77
function GradientCache(
8-
df :: Union{<:Number, AbstractArray{<:Number}},
9-
x :: Union{<:Number, AbstractArray{<:Number}},
10-
fdtype :: Type{T1} = Val{:central},
11-
returntype :: Type{T2} = eltype(df),
12-
inplace :: Type{Val{T3}} = Val{true}) where {T1,T2,T3}
8+
df,
9+
x,
10+
fdtype = Val{:central},
11+
returntype = eltype(df),
12+
inplace = Val{true})
1313

1414
if typeof(x)<:AbstractArray # the vector->scalar case
1515
if fdtype!=Val{:complex} # complex-mode FD only needs one cache, for x+eps*im
@@ -49,74 +49,18 @@ function GradientCache(
4949

5050
end
5151

52-
function GradientCache(
53-
c1 :: Union{Nothing,AbstractArray{<:Number}},
54-
c2 :: Union{Nothing,AbstractArray{<:Number}},
55-
fx :: Union{Nothing,<:Number,AbstractArray{<:Number}} = nothing,
56-
fdtype :: Type{T1} = Val{:central},
57-
returntype :: Type{T2} = eltype(c1),
58-
inplace :: Type{Val{T3}} = Val{true}) where {T1,T2,T3}
59-
60-
if fdtype!=Val{:forward} && typeof(fx)!=Nothing
61-
@warn("Pre-computed function values are only useful for fdtype == Val{:forward}.")
62-
_fx = nothing
63-
else
64-
# more runtime sanity checks?
65-
_fx = fx
66-
end
67-
68-
if typeof(x)<:AbstractArray # the vector->scalar case
69-
# need cache arrays for x1 (c1) and epsilon (c2) (both only if non-StridedArray)
70-
if fdtype!=Val{:complex} # complex-mode FD only needs one cache, for x+eps*im
71-
if typeof(x)<:StridedVector
72-
if eltype(df)<:Complex && !(eltype(x)<:Complex)
73-
_c1 = zero(Complex{eltype(x)}) .* x
74-
_c2 = nothing
75-
else
76-
_c1 = nothing
77-
_c2 = nothing
78-
if typeof(c1)!=Nothing || typeof(c2)!=Nothing
79-
@warn("For StridedVectors, neither c1 nor c2 are necessary.")
80-
end
81-
end
82-
else
83-
_c1 = c1
84-
_c2 = c2
85-
end
86-
else
87-
if !(returntype<:Real)
88-
fdtype_error(returntype)
89-
else
90-
_c1 = x + zero(eltype(x)) .* im
91-
_c2 = nothing
92-
end
93-
end
94-
95-
else # the scalar->vector case
96-
# need cache arrays for fx1 and fx2, except in complex mode, which needs one complex array
97-
if fdtype != Val{:complex}
98-
_c1 = c1
99-
_c2 = c2
100-
else
101-
_c1 = c1
102-
_c2 = nothing
103-
end
104-
end
105-
GradientCache{typeof(_fx),typeof(_c1),typeof(_c2),fdtype,returntype,inplace}(_fx,_c1,_c2)
106-
end
107-
10852
function finite_difference_gradient(
10953
f,
11054
x,
111-
fdtype::Type{T1}=Val{:central},
112-
returntype::Type{T2}=eltype(x),
113-
inplace::Type{Val{T3}}=Val{true},
114-
fx::Union{Nothing,AbstractArray{<:Number}}=nothing,
115-
c1::Union{Nothing,AbstractArray{<:Number}}=nothing,
116-
c2::Union{Nothing,AbstractArray{<:Number}}=nothing;
55+
fdtype = Val{:central},
56+
returntype = eltype(x),
57+
inplace = Val{true},
58+
fx = nothing,
59+
c1 = nothing,
60+
c2 = nothing;
11761
relstep=default_relstep(fdtype, eltype(x)),
11862
absstep=relstep,
119-
dir=true) where {T1,T2,T3}
63+
dir=true)
12064

12165
if typeof(x) <: AbstractArray
12266
df = zero(returntype) .* x
@@ -142,14 +86,14 @@ function finite_difference_gradient!(
14286
df,
14387
f,
14488
x,
145-
fdtype::Type{T1}=Val{:central},
146-
returntype::Type{T2}=eltype(df),
147-
inplace::Type{Val{T3}}=Val{true},
148-
fx::Union{Nothing,AbstractArray{<:Number}}=nothing,
149-
c1::Union{Nothing,AbstractArray{<:Number}}=nothing,
150-
c2::Union{Nothing,AbstractArray{<:Number}}=nothing;
89+
fdtype=Val{:central},
90+
returntype=eltype(df),
91+
inplace=Val{true},
92+
fx=nothing,
93+
c1=nothing,
94+
c2=nothing;
15195
relstep=default_relstep(fdtype, eltype(x)),
152-
absstep=relstep) where {T1,T2,T3}
96+
absstep=relstep)
15397

15498
cache = GradientCache(df, x, fdtype, returntype, inplace)
15599
finite_difference_gradient!(df, f, x, cache, relstep=relstep, absstep=absstep)
@@ -175,9 +119,9 @@ end
175119
# vector of derivatives of a vector->scalar map by each component of a vector x
176120
# this ignores the value of "inplace", because it doesn't make much sense
177121
function finite_difference_gradient!(
178-
df::AbstractArray{<:Number},
122+
df,
179123
f,
180-
x::AbstractArray{<:Number},
124+
x,
181125
cache::GradientCache{T1,T2,T3,fdtype,returntype,inplace};
182126
relstep=default_relstep(fdtype, eltype(x)),
183127
absstep=relstep,
@@ -359,7 +303,7 @@ end
359303
# vector of derivatives of a scalar->vector map
360304
# this is effectively a vector of partial derivatives, but we still call it a gradient
361305
function finite_difference_gradient!(
362-
df::AbstractArray{<:Number},
306+
df,
363307
f,
364308
x::Number,
365309
cache::GradientCache{T1,T2,T3,fdtype,returntype,inplace};

src/hessians.jl

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,21 @@ struct HessianCache{T,fdtype,inplace}
66
end
77

88
function HessianCache(xpp,xpm,xmp,xmm,
9-
fdtype::Type{T1}=Val{:hcentral},
10-
inplace::Type{Val{T2}} = x isa StaticArray ? Val{false} : Val{true}) where {T1,T2}
9+
fdtype=Val{:hcentral},
10+
inplace = x isa StaticArray ? Val{false} : Val{true})
1111
HessianCache{typeof(xpp),fdtype,inplace}(xpp,xpm,xmp,xmm)
1212
end
1313

14-
function HessianCache(x,fdtype::Type{T1}=Val{:hcentral},
15-
inplace::Type{Val{T2}} = x isa StaticArray ? Val{false} : Val{true}) where {T1,T2}
14+
function HessianCache(x,fdtype=Val{:hcentral},
15+
inplace = x isa StaticArray ? Val{false} : Val{true})
1616
HessianCache{typeof(x),fdtype,inplace}(copy(x),copy(x),copy(x),copy(x))
1717
end
1818

19-
function finite_difference_hessian(f, x::AbstractArray{<:Number},
20-
fdtype :: Type{T1}=Val{:hcentral},
21-
inplace :: Type{Val{T2}} = x isa StaticArray ? Val{false} : Val{true};
22-
relstep=default_relstep(fdtype, eltype(x)),
23-
absstep=relstep) where {T1,T2}
19+
function finite_difference_hessian(f, x,
20+
fdtype = Val{:hcentral},
21+
inplace = x isa StaticArray ? Val{false} : Val{true};
22+
relstep = default_relstep(fdtype, eltype(x)),
23+
absstep = relstep)
2424

2525
cache = HessianCache(x, fdtype, inplace)
2626
finite_difference_hessian(f, x, cache; relstep=relstep, absstep=absstep)
@@ -37,12 +37,12 @@ function finite_difference_hessian(
3737
Symmetric(_H isa SMatrix ? SArray(H) : H)
3838
end
3939

40-
function finite_difference_hessian!(H::AbstractMatrix,f,
41-
x::AbstractArray{<:Number},
42-
fdtype :: Type{T1}=Val{:hcentral},
43-
inplace :: Type{Val{T2}} = x isa StaticArray ? Val{false} : Val{true};
40+
function finite_difference_hessian!(H,f,
41+
x,
42+
fdtype = Val{:hcentral},
43+
inplace = x isa StaticArray ? Val{false} : Val{true};
4444
relstep=default_relstep(fdtype, eltype(x)),
45-
absstep=relstep) where {T1,T2}
45+
absstep=relstep)
4646

4747
cache = HessianCache(x,fdtype,inplace)
4848
finite_difference_hessian!(H, f, x, cache; relstep=relstep, absstep=absstep)

src/jacobians.jl

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -124,16 +124,16 @@ function _make_Ji(::AbstractArray, xtype, dx, color_i, nrows, ncols)
124124
size(Ji)!=(nrows, ncols) ? reshape(Ji,(nrows,ncols)) : Ji #branch when size(dx) == (1,) => size(Ji) == (1,) while size(J) == (1,1)
125125
end
126126

127-
function finite_difference_jacobian(f, x::AbstractArray{<:Number},
128-
fdtype :: Type{T1}=Val{:forward},
129-
returntype :: Type{T2}=eltype(x),
130-
f_in :: Union{AbstractArray{<:T2},Nothing}=nothing;
127+
function finite_difference_jacobian(f, x,
128+
fdtype = Val{:forward},
129+
returntype = eltype(x),
130+
f_in = nothing;
131131
relstep=default_relstep(fdtype, eltype(x)),
132132
absstep=relstep,
133133
colorvec = 1:length(x),
134134
sparsity = nothing,
135135
jac_prototype = nothing,
136-
dir=true) where {T1,T2,T3}
136+
dir=true)
137137

138138
if f_in isa Nothing
139139
fx = f(x)
@@ -254,16 +254,16 @@ function finite_difference_jacobian(
254254
J
255255
end
256256

257-
function finite_difference_jacobian!(J::AbstractMatrix,
257+
function finite_difference_jacobian!(J,
258258
f,
259-
x::AbstractArray{<:Number},
260-
fdtype :: Type{T1}=Val{:forward},
261-
returntype :: Type{T2}=eltype(x),
262-
f_in :: Union{AbstractArray{<:T2},Nothing}=nothing;
259+
x,
260+
fdtype = Val{:forward},
261+
returntype = eltype(x),
262+
f_in = nothing;
263263
relstep=default_relstep(fdtype, eltype(x)),
264264
absstep=relstep,
265265
colorvec = 1:length(x),
266-
sparsity = ArrayInterface.has_sparsestruct(J) ? J : nothing) where {T1,T2}
266+
sparsity = ArrayInterface.has_sparsestruct(J) ? J : nothing)
267267
if f_in isa Nothing && fdtype == Val{:forward}
268268
if size(J,1) == length(x)
269269
fx = zero(x)
@@ -281,15 +281,15 @@ function finite_difference_jacobian!(J::AbstractMatrix,
281281
end
282282

283283
function finite_difference_jacobian!(
284-
J::AbstractMatrix{<:Number},
284+
J,
285285
f,
286-
x::AbstractArray{<:Number},
286+
x,
287287
cache::JacobianCache{T1,T2,T3,cType,sType,fdtype,returntype},
288-
f_in::Union{T2,Nothing}=nothing;
288+
f_in = nothing;
289289
relstep = default_relstep(fdtype, eltype(x)),
290-
absstep=relstep,
290+
absstep = relstep,
291291
colorvec = cache.colorvec,
292-
sparsity::Union{AbstractArray,Nothing} = cache.sparsity,
292+
sparsity = cache.sparsity,
293293
dir = true) where {T1,T2,T3,cType,sType,fdtype,returntype}
294294

295295
m, n = size(J)

test/finitedifftests.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
using FiniteDiff, Test, LinearAlgebra
22

3+
# code quality guards
4+
@test isempty(detect_unbound_args(FiniteDiff))
5+
@test isempty(detect_ambiguities(FiniteDiff) )
6+
37
# TODO: add tests for GPUArrays
48
# TODO: add tests for DEDataArrays
59

0 commit comments

Comments
 (0)