Skip to content

Commit c6719c7

Browse files
committed
Change all Array and Vector to AbstractArray and AbstractVector
1 parent cb42f36 commit c6719c7

6 files changed

+58
-30
lines changed

src/check_derivative.jl

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ function check_derivative(f::Function, g::Function, x::Number)
33
return maximum(abs(g(x) - auto_g(x)))
44
end
55

6-
function check_gradient{T <: Number}(f::Function, g::Function, x::Vector{T})
6+
function check_gradient{T <: Number}(f::Function, g::Function, x::AbstractVector{T})
77
auto_g = gradient(f)
88
return maximum(abs(g(x) - auto_g(x)))
99
end
@@ -13,7 +13,7 @@ function check_second_derivative(f::Function, h::Function, x::Number)
1313
return maximum(abs(h(x) - auto_h(x)))
1414
end
1515

16-
function check_hessian{T <: Number}(f::Function, h::Function, x::Vector{T})
16+
function check_hessian{T <: Number}(f::Function, h::Function, x::AbstractVector{T})
1717
auto_h = hessian(f)
1818
return maximum(abs(h(x) - auto_h(x)))
1919
end

src/derivative.jl

+15-15
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,19 @@ function derivative(f::Function, ftype::Symbol, dtype::Symbol)
22
if ftype == :scalar
33
g(x::Number) = finite_difference(f, float(x), dtype)
44
elseif ftype == :vector
5-
g(x::Vector) = finite_difference(f, float(x), dtype)
5+
g(x::AbstractVector) = finite_difference(f, float(x), dtype)
66
else
77
error("ftype must :scalar or :vector")
88
end
99
return g
1010
end
11-
Compat.@compat derivative{T <: Number}(f::Function, x::Union{T, Vector{T}}, dtype::Symbol = :central) = finite_difference(f, float(x), dtype)
11+
Compat.@compat derivative{T <: Number}(f::Function, x::Union{T, AbstractVector{T}}, dtype::Symbol = :central) = finite_difference(f, float(x), dtype)
1212
derivative(f::Function, dtype::Symbol = :central) = derivative(f, :scalar, dtype)
1313

14-
Compat.@compat gradient{T <: Number}(f::Function, x::Union{T, Vector{T}}, dtype::Symbol = :central) = finite_difference(f, float(x), dtype)
14+
Compat.@compat gradient{T <: Number}(f::Function, x::Union{T, AbstractVector{T}}, dtype::Symbol = :central) = finite_difference(f, float(x), dtype)
1515
gradient(f::Function, dtype::Symbol = :central) = derivative(f, :vector, dtype)
1616

17-
Compat.@compat function Base.gradient{T <: Number}(f::Function, x::Union{T, Vector{T}}, dtype::Symbol = :central)
17+
Compat.@compat function Base.gradient{T <: Number}(f::Function, x::Union{T, AbstractVector{T}}, dtype::Symbol = :central)
1818
Base.warn_once("The finite difference methods from Calculus.jl no longer extend Base.gradient and should be called as Calculus.gradient instead. This usage is deprecated.")
1919
Calculus.gradient(f,x,dtype)
2020
end
@@ -26,11 +26,11 @@ end
2626

2727
ctranspose(f::Function) = derivative(f)
2828

29-
function jacobian{T <: Number}(f::Function, x::Vector{T}, dtype::Symbol)
29+
function jacobian{T <: Number}(f::Function, x::AbstractVector{T}, dtype::Symbol)
3030
finite_difference_jacobian(f, x, dtype)
3131
end
3232
function jacobian(f::Function, dtype::Symbol)
33-
g(x::Vector) = finite_difference_jacobian(f, x, dtype)
33+
g(x::AbstractVector) = finite_difference_jacobian(f, x, dtype)
3434
return g
3535
end
3636
jacobian(f::Function) = jacobian(f, :central)
@@ -39,22 +39,22 @@ function second_derivative(f::Function, g::Function, ftype::Symbol, dtype::Symbo
3939
if ftype == :scalar
4040
h(x::Number) = finite_difference_hessian(f, g, x, dtype)
4141
elseif ftype == :vector
42-
h(x::Vector) = finite_difference_hessian(f, g, x, dtype)
42+
h(x::AbstractVector) = finite_difference_hessian(f, g, x, dtype)
4343
else
4444
error("ftype must :scalar or :vector")
4545
end
4646
return h
4747
end
48-
Compat.@compat function second_derivative{T <: Number}(f::Function, g::Function, x::Union{T, Vector{T}}, dtype::Symbol)
48+
Compat.@compat function second_derivative{T <: Number}(f::Function, g::Function, x::Union{T, AbstractVector{T}}, dtype::Symbol)
4949
finite_difference_hessian(f, g, x, dtype)
5050
end
51-
Compat.@compat function hessian{T <: Number}(f::Function, g::Function, x::Union{T, Vector{T}}, dtype::Symbol)
51+
Compat.@compat function hessian{T <: Number}(f::Function, g::Function, x::Union{T, AbstractVector{T}}, dtype::Symbol)
5252
finite_difference_hessian(f, g, x, dtype)
5353
end
54-
Compat.@compat function second_derivative{T <: Number}(f::Function, g::Function, x::Union{T, Vector{T}})
54+
Compat.@compat function second_derivative{T <: Number}(f::Function, g::Function, x::Union{T, AbstractVector{T}})
5555
finite_difference_hessian(f, g, x, :central)
5656
end
57-
Compat.@compat function hessian{T <: Number}(f::Function, g::Function, x::Union{T, Vector{T}})
57+
Compat.@compat function hessian{T <: Number}(f::Function, g::Function, x::Union{T, AbstractVector{T}})
5858
finite_difference_hessian(f, g, x, :central)
5959
end
6060
function second_derivative(f::Function, x::Number, dtype::Symbol)
@@ -63,10 +63,10 @@ end
6363
function hessian(f::Function, x::Number, dtype::Symbol)
6464
finite_difference_hessian(f, derivative(f), x, dtype)
6565
end
66-
function second_derivative{T <: Number}(f::Function, x::Vector{T}, dtype::Symbol)
66+
function second_derivative{T <: Number}(f::Function, x::AbstractVector{T}, dtype::Symbol)
6767
finite_difference_hessian(f, gradient(f), x, dtype)
6868
end
69-
function hessian{T <: Number}(f::Function, x::Vector{T}, dtype::Symbol)
69+
function hessian{T <: Number}(f::Function, x::AbstractVector{T}, dtype::Symbol)
7070
finite_difference_hessian(f, gradient(f), x, dtype)
7171
end
7272
function second_derivative(f::Function, x::Number)
@@ -75,10 +75,10 @@ end
7575
function hessian(f::Function, x::Number)
7676
finite_difference_hessian(f, derivative(f), x, :central)
7777
end
78-
function second_derivative{T <: Number}(f::Function, x::Vector{T})
78+
function second_derivative{T <: Number}(f::Function, x::AbstractVector{T})
7979
finite_difference_hessian(f, gradient(f), x, :central)
8080
end
81-
function hessian{T <: Number}(f::Function, x::Vector{T})
81+
function hessian{T <: Number}(f::Function, x::AbstractVector{T})
8282
finite_difference_hessian(f, gradient(f), x, :central)
8383
end
8484
second_derivative(f::Function, g::Function, dtype::Symbol) = second_derivative(f, g, :scalar, dtype)

src/finite_difference.jl

+11-11
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,8 @@ end
9898
##############################################################################
9999

100100
function finite_difference!{S <: Number, T <: Number}(f::Function,
101-
x::Vector{S},
102-
g::Vector{T},
101+
x::AbstractVector{S},
102+
g::AbstractVector{T},
103103
dtype::Symbol)
104104
# What is the dimension of x?
105105
n = length(x)
@@ -136,7 +136,7 @@ function finite_difference!{S <: Number, T <: Number}(f::Function,
136136
return
137137
end
138138
function finite_difference{T <: Number}(f::Function,
139-
x::Vector{T},
139+
x::AbstractVector{T},
140140
dtype::Symbol = :central)
141141
# Allocate memory for gradient
142142
g = Array(Float64, length(x))
@@ -157,9 +157,9 @@ end
157157
function finite_difference_jacobian!{R <: Number,
158158
S <: Number,
159159
T <: Number}(f::Function,
160-
x::Vector{R},
161-
f_x::Vector{S},
162-
J::Array{T},
160+
x::AbstractVector{R},
161+
f_x::AbstractVector{S},
162+
J::AbstractArray{T},
163163
dtype::Symbol = :central)
164164
# What is the dimension of x?
165165
m, n = size(J)
@@ -192,7 +192,7 @@ function finite_difference_jacobian!{R <: Number,
192192
return
193193
end
194194
function finite_difference_jacobian{T <: Number}(f::Function,
195-
x::Vector{T},
195+
x::AbstractVector{T},
196196
dtype::Symbol = :central)
197197
# Establish a baseline for f_x
198198
f_x = f(x)
@@ -233,8 +233,8 @@ end
233233

234234
function finite_difference_hessian!{S <: Number,
235235
T <: Number}(f::Function,
236-
x::Vector{S},
237-
H::Array{T})
236+
x::AbstractVector{S},
237+
H::AbstractArray{T})
238238
# What is the dimension of x?
239239
n = length(x)
240240

@@ -265,7 +265,7 @@ function finite_difference_hessian!{S <: Number,
265265
Base.LinAlg.copytri!(H,'U')
266266
end
267267
function finite_difference_hessian{T <: Number}(f::Function,
268-
x::Vector{T})
268+
x::AbstractVector{T})
269269
# What is the dimension of x?
270270
n = length(x)
271271

@@ -280,7 +280,7 @@ function finite_difference_hessian{T <: Number}(f::Function,
280280
end
281281
function finite_difference_hessian{T <: Number}(f::Function,
282282
g::Function,
283-
x::Vector{T},
283+
x::AbstractVector{T},
284284
dtype::Symbol = :central)
285285
finite_difference_jacobian(g, x, dtype)
286286
end

test/check_derivative.jl

+6
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,9 @@
2121
@test check_hessian(x -> sin(x[1]) + cos(x[2]), x -> [-sin(x[1]) 0.0; 0.0 -cos(x[2])], [10.0, 10.0]) < 10e-4
2222
@test check_hessian(x -> sin(x[1]) + cos(x[2]), x -> [-sin(x[1]) 0.0; 0.0 -cos(x[2])], [100.0, 100.0]) < 10e-4
2323
@test check_hessian(x -> sin(x[1]) + cos(x[2]), x -> [-sin(x[1]) 0.0; 0.0 -cos(x[2])], [1000.0, 1000.0]) < 10e-4
24+
25+
# Test functionality for other AbstractArray types
26+
@test check_gradient(x -> sin(x[1]) + cos(x[2]), x -> [cos(x[1]), -sin(x[2])],
27+
sub([0.0, 0.0],:)) < 10e-4
28+
@test check_hessian(x -> sin(x[1]) + cos(x[2]), x -> [-sin(x[1]) 0.0; 0.0 -cos(x[2])],
29+
sub([0.0, 0.0],:)) < 10e-4

test/derivative.jl

+15-2
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,14 @@ f1(x::Real) = sin(x)
99
@test norm(derivative(f1, :central)(0.0) - cos(0.0)) < 10e-4
1010
@test norm(derivative(f1)(0.0) - cos(0.0)) < 10e-4
1111

12-
f2(x::Vector) = sin(x[1])
12+
f2(x::AbstractVector) = sin(x[1])
1313
@test norm(derivative(f2, :vector, :forward)([0.0]) .- cos(0.0)) < 10e-4
1414
@test norm(derivative(f2, :vector, :central)([0.0]) .- cos(0.0)) < 10e-4
1515

16+
# Test functionality for SubArrays
17+
@test norm(derivative(f2, :vector, :forward)(sub([0.0],:)) .- cos(0.0)) < 10e-4
18+
@test norm(derivative(f2, :vector, :central)(sub([0.0],:)) .- cos(0.0)) < 10e-4
19+
1620
#
1721
# ctranspose overloading
1822
#
@@ -26,11 +30,16 @@ end
2630
# gradient()
2731
#
2832

29-
f4(x::Vector) = (100.0 - x[1])^2 + (50.0 - x[2])^2
33+
f4(x::AbstractVector) = (100.0 - x[1])^2 + (50.0 - x[2])^2
3034
@test norm(Calculus.gradient(f4, :forward)([100.0, 50.0]) - [0.0, 0.0]) < 10e-4
3135
@test norm(Calculus.gradient(f4, :central)([100.0, 50.0]) - [0.0, 0.0]) < 10e-4
3236
@test norm(Calculus.gradient(f4)([100.0, 50.0]) - [0.0, 0.0]) < 10e-4
3337

38+
# Test for SubArrays
39+
@test norm(Calculus.gradient(f4, :forward)(sub([100.0, 50.0],:)) - [0.0, 0.0]) < 10e-4
40+
@test norm(Calculus.gradient(f4, :central)(sub([100.0, 50.0],:)) - [0.0, 0.0]) < 10e-4
41+
@test norm(Calculus.gradient(f4)(sub([100.0, 50.0],:)) - [0.0, 0.0]) < 10e-4
42+
3443
#
3544
# second_derivative()
3645
#
@@ -47,3 +56,7 @@ f4(x::Vector) = (100.0 - x[1])^2 + (50.0 - x[2])^2
4756
f5(x) = sin(x[1]) + cos(x[2])
4857
@test norm(Calculus.gradient(f5)([0.0, 0.0]) - [cos(0.0), -sin(0.0)]) < 10e-4
4958
@test norm(hessian(f5)([0.0, 0.0]) - [-sin(0.0) 0.0; 0.0 -cos(0.0)]) < 10e-4
59+
60+
# And for SubArrays again
61+
@test norm(Calculus.gradient(f5)(sub([0.0, 0.0],:)) - [cos(0.0), -sin(0.0)]) < 10e-4
62+
@test norm(hessian(f5)(sub([0.0, 0.0],:)) - [-sin(0.0) 0.0; 0.0 -cos(0.0)]) < 10e-4

test/finite_difference.jl

+9
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@
3030
@test norm(Calculus.finite_difference(x -> exp(-x[1]), [1.0], :central) - [-exp(-1.0)]) < 10e-4
3131
@test norm(Calculus.finite_difference(x -> exp(-x[1]), [1.0]) - [-exp(-1.0)]) < 10e-4
3232

33+
# Gradients of f when x is a SubArrays
34+
@test norm(Calculus.finite_difference(x -> exp(-x[1]), sub([1.0],:), :forward) - [-exp(-1.0)]) < 10e-4
35+
@test norm(Calculus.finite_difference(x -> exp(-x[1]), sub([1.0],:), :central) - [-exp(-1.0)]) < 10e-4
36+
@test norm(Calculus.finite_difference(x -> exp(-x[1]), sub([1.0],:)) - [-exp(-1.0)]) < 10e-4
37+
3338
#
3439
# Second derivatives of f: R -> R
3540
#
@@ -52,6 +57,10 @@ gx = Calculus.gradient(fx)
5257
@test norm(Calculus.finite_difference_hessian(fx, gx, [0.0, 0.0], :central) - [-sin(0.0) 0.0; 0.0 -cos(0.0)]) < 10e-4
5358
@test norm(Calculus.finite_difference_hessian(fx, [0.0, 0.0]) - [-sin(0.0) 0.0; 0.0 -cos(0.0)]) < 10e-4
5459

60+
@test norm(gx(sub([0.0, 0.0],:)) - [cos(0.0), -sin(0.0)]) < 10e-4
61+
@test norm(Calculus.finite_difference_hessian(fx, gx, sub([0.0, 0.0],:), :central) - [-sin(0.0) 0.0; 0.0 -cos(0.0)]) < 10e-4
62+
@test norm(Calculus.finite_difference_hessian(fx, sub([0.0, 0.0],:)) - [-sin(0.0) 0.0; 0.0 -cos(0.0)]) < 10e-4
63+
5564
#
5665
# Taylor Series first derivatives
5766
#

0 commit comments

Comments
 (0)