Skip to content

Commit e8dd087

Browse files
abstraction merge with function changes
2 parents ba5daf4 + 2806caa commit e8dd087

File tree

6 files changed

+76
-64
lines changed

6 files changed

+76
-64
lines changed

src/Calculus.jl

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,28 +26,28 @@ module Calculus
2626

2727
# const NonDifferentiableFunction = Function
2828
# type DifferentiableFunction
29-
# f::Function
30-
# g::Function
29+
# f
30+
# g
3131
# end
3232
# type TwiceDifferentiableFunction
33-
# f::Function
34-
# g::Function
35-
# h::Function
33+
# f
34+
# g
35+
# h
3636
# end
3737
# type NonDifferentiableBundledFunction <: BundledFunction
38-
# f::Function
38+
# f
3939
# fstorage::Any
4040
# end
4141
# type DifferentiableBundledFunction <: BundledFunction
42-
# f::Function
43-
# g::Function
42+
# f
43+
# g
4444
# fstorage::Any
4545
# gstorage::Any
4646
# end
4747
# type TwiceDifferentiableBundledFunction <: BundledFunction
48-
# f::Function
49-
# g::Function
50-
# h::Function
48+
# f
49+
# g
50+
# h
5151
# fstorage::Any
5252
# gstorage::Any
5353
# hstorage::Any

src/check_derivative.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
1-
Compat.@compat function check_derivative(f::Function, g::Function, x::Number)
1+
Compat.@compat function check_derivative(f, g, x::Number)
22
auto_g = derivative(f)
33
return maximum(abs.(g(x) - auto_g(x)))
44
end
55

6-
Compat.@compat function check_gradient{T <: Number}(f::Function, g::Function, x::Vector{T})
6+
Compat.@compat function check_gradient{T <: Number}(f, g, x::Vector{T})
77
auto_g = gradient(f)
88
return maximum(abs.(g(x) - auto_g(x)))
99
end
1010

11-
Compat.@compat function check_second_derivative(f::Function, h::Function, x::Number)
11+
Compat.@compat function check_second_derivative(f, h, x::Number)
1212
auto_h = second_derivative(f)
1313
return maximum(abs.(h(x) - auto_h(x)))
1414
end
1515

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

src/derivative.jl

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
function derivative(f::Function, ftype::Symbol, dtype::Symbol)
1+
function derivative(f, ftype::Symbol, dtype::Symbol)
22
if ftype == :scalar
33
return x::Number -> finite_difference(f, float(x), dtype)
44
elseif ftype == :vector
@@ -7,34 +7,34 @@ function derivative(f::Function, ftype::Symbol, dtype::Symbol)
77
error("ftype must :scalar or :vector")
88
end
99
end
10-
Compat.@compat derivative{T <: Number}(f::Function, x::Union{T, Vector{T}}, dtype::Symbol = :central) = finite_difference(f, float(x), dtype)
11-
derivative(f::Function, dtype::Symbol = :central) = derivative(f, :scalar, dtype)
10+
Compat.@compat derivative{T <: Number}(f, x::Union{T, Vector{T}}, dtype::Symbol = :central) = finite_difference(f, float(x), dtype)
11+
derivative(f, dtype::Symbol = :central) = derivative(f, :scalar, dtype)
1212

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

16-
Compat.@compat function Base.gradient{T <: Number}(f::Function, x::Union{T, Vector{T}}, dtype::Symbol = :central)
16+
Compat.@compat function Base.gradient{T <: Number}(f, x::Union{T, Vector{T}}, dtype::Symbol = :central)
1717
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.")
1818
Calculus.gradient(f,x,dtype)
1919
end
2020

21-
function Base.gradient(f::Function, dtype::Symbol = :central)
21+
function Base.gradient(f, dtype::Symbol = :central)
2222
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.")
2323
Calculus.gradient(f,dtype)
2424
end
2525

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

28-
function jacobian{T <: Number}(f::Function, x::Vector{T}, dtype::Symbol)
28+
function jacobian{T <: Number}(f, x::Vector{T}, dtype::Symbol)
2929
finite_difference_jacobian(f, x, dtype)
3030
end
31-
function jacobian(f::Function, dtype::Symbol)
31+
function jacobian(f, dtype::Symbol)
3232
g(x::Vector) = finite_difference_jacobian(f, x, dtype)
3333
return g
3434
end
35-
jacobian(f::Function) = jacobian(f, :central)
35+
jacobian(f) = jacobian(f, :central)
3636

37-
function second_derivative(f::Function, g::Function, ftype::Symbol, dtype::Symbol)
37+
function second_derivative(f, g, ftype::Symbol, dtype::Symbol)
3838
if ftype == :scalar
3939
return x::Number -> finite_difference_hessian(f, g, x, dtype)
4040
elseif ftype == :vector
@@ -43,45 +43,45 @@ function second_derivative(f::Function, g::Function, ftype::Symbol, dtype::Symbo
4343
error("ftype must :scalar or :vector")
4444
end
4545
end
46-
Compat.@compat function second_derivative{T <: Number}(f::Function, g::Function, x::Union{T, Vector{T}}, dtype::Symbol)
46+
Compat.@compat function second_derivative{T <: Number}(f, g, x::Union{T, Vector{T}}, dtype::Symbol)
4747
finite_difference_hessian(f, g, x, dtype)
4848
end
49-
Compat.@compat function hessian{T <: Number}(f::Function, g::Function, x::Union{T, Vector{T}}, dtype::Symbol)
49+
Compat.@compat function hessian{T <: Number}(f, g, x::Union{T, Vector{T}}, dtype::Symbol)
5050
finite_difference_hessian(f, g, x, dtype)
5151
end
52-
Compat.@compat function second_derivative{T <: Number}(f::Function, g::Function, x::Union{T, Vector{T}})
52+
Compat.@compat function second_derivative{T <: Number}(f, g, x::Union{T, Vector{T}})
5353
finite_difference_hessian(f, g, x, :central)
5454
end
55-
Compat.@compat function hessian{T <: Number}(f::Function, g::Function, x::Union{T, Vector{T}})
55+
Compat.@compat function hessian{T <: Number}(f, g, x::Union{T, Vector{T}})
5656
finite_difference_hessian(f, g, x, :central)
5757
end
58-
function second_derivative(f::Function, x::Number, dtype::Symbol)
58+
function second_derivative(f, x::Number, dtype::Symbol)
5959
finite_difference_hessian(f, derivative(f), x, dtype)
6060
end
61-
function hessian(f::Function, x::Number, dtype::Symbol)
61+
function hessian(f, x::Number, dtype::Symbol)
6262
finite_difference_hessian(f, derivative(f), x, dtype)
6363
end
64-
function second_derivative{T <: Number}(f::Function, x::Vector{T}, dtype::Symbol)
64+
function second_derivative{T <: Number}(f, x::Vector{T}, dtype::Symbol)
6565
finite_difference_hessian(f, gradient(f), x, dtype)
6666
end
67-
function hessian{T <: Number}(f::Function, x::Vector{T}, dtype::Symbol)
67+
function hessian{T <: Number}(f, x::Vector{T}, dtype::Symbol)
6868
finite_difference_hessian(f, gradient(f), x, dtype)
6969
end
70-
function second_derivative(f::Function, x::Number)
70+
function second_derivative(f, x::Number)
7171
finite_difference_hessian(f, derivative(f), x, :central)
7272
end
73-
function hessian(f::Function, x::Number)
73+
function hessian(f, x::Number)
7474
finite_difference_hessian(f, derivative(f), x, :central)
7575
end
76-
function second_derivative{T <: Number}(f::Function, x::Vector{T})
76+
function second_derivative{T <: Number}(f, x::Vector{T})
7777
finite_difference_hessian(f, gradient(f), x, :central)
7878
end
79-
function hessian{T <: Number}(f::Function, x::Vector{T})
79+
function hessian{T <: Number}(f, x::Vector{T})
8080
finite_difference_hessian(f, gradient(f), x, :central)
8181
end
82-
second_derivative(f::Function, g::Function, dtype::Symbol) = second_derivative(f, g, :scalar, dtype)
83-
second_derivative(f::Function, g::Function) = second_derivative(f, g, :scalar, :central)
84-
second_derivative(f::Function) = second_derivative(f, derivative(f), :scalar, :central)
85-
hessian(f::Function, g::Function, dtype::Symbol) = second_derivative(f, g, :vector, dtype)
86-
hessian(f::Function, g::Function) = second_derivative(f, g, :vector, :central)
87-
hessian(f::Function) = second_derivative(f, gradient(f), :vector, :central)
82+
second_derivative(f, g, dtype::Symbol) = second_derivative(f, g, :scalar, dtype)
83+
second_derivative(f, g) = second_derivative(f, g, :scalar, :central)
84+
second_derivative(f) = second_derivative(f, derivative(f), :scalar, :central)
85+
hessian(f, g, dtype::Symbol) = second_derivative(f, g, :vector, dtype)
86+
hessian(f, g) = second_derivative(f, g, :vector, :central)
87+
hessian(f) = second_derivative(f, gradient(f), :vector, :central)

src/finite_difference.jl

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ macro complexrule(x, e)
4242
end
4343
end
4444

45-
function finite_difference{T <: Number}(f::Function,
45+
function finite_difference{T <: Number}(f,
4646
x::T,
4747
dtype::Symbol = :central)
4848
if dtype == :forward
@@ -97,7 +97,7 @@ end
9797
##
9898
##############################################################################
9999

100-
function finite_difference!{S <: Number, T <: Number}(f::Function,
100+
function finite_difference!{S <: Number, T <: Number}(f,
101101
x::AbstractVector{S},
102102
g::AbstractVector{T},
103103
dtype::Symbol)
@@ -135,7 +135,7 @@ function finite_difference!{S <: Number, T <: Number}(f::Function,
135135

136136
return
137137
end
138-
function finite_difference{T <: Number}(f::Function,
138+
function finite_difference{T <: Number}(f,
139139
x::AbstractVector{T},
140140
dtype::Symbol = :central)
141141
# Allocate memory for gradient
@@ -156,7 +156,7 @@ end
156156

157157
function finite_difference_jacobian!{R <: Number,
158158
S <: Number,
159-
T <: Number}(f::Function,
159+
T <: Number}(f,
160160
x::AbstractVector{R},
161161
f_x::AbstractVector{S},
162162
J::Array{T},
@@ -190,7 +190,7 @@ function finite_difference_jacobian!{R <: Number,
190190

191191
return
192192
end
193-
function finite_difference_jacobian{T <: Number}(f::Function,
193+
function finite_difference_jacobian{T <: Number}(f,
194194
x::AbstractVector{T},
195195
dtype::Symbol = :central)
196196
# Establish a baseline for f_x
@@ -212,13 +212,13 @@ end
212212
##
213213
##############################################################################
214214

215-
function finite_difference_hessian{T <: Number}(f::Function,
215+
function finite_difference_hessian{T <: Number}(f,
216216
x::T)
217217
@hessianrule x epsilon
218218
(f(x + epsilon) - 2*f(x) + f(x - epsilon))/epsilon^2
219219
end
220-
function finite_difference_hessian(f::Function,
221-
g::Function,
220+
function finite_difference_hessian(f,
221+
g,
222222
x::Number,
223223
dtype::Symbol = :central)
224224
finite_difference(g, x, dtype)
@@ -231,7 +231,7 @@ end
231231
##############################################################################
232232

233233
function finite_difference_hessian!{S <: Number,
234-
T <: Number}(f::Function,
234+
T <: Number}(f,
235235
x::AbstractVector{S},
236236
H::Array{T})
237237
# What is the dimension of x?
@@ -263,7 +263,7 @@ function finite_difference_hessian!{S <: Number,
263263
end
264264
Base.LinAlg.copytri!(H,'U')
265265
end
266-
function finite_difference_hessian{T <: Number}(f::Function,
266+
function finite_difference_hessian{T <: Number}(f,
267267
x::AbstractVector{T})
268268
# What is the dimension of x?
269269
n = length(x)
@@ -277,8 +277,8 @@ function finite_difference_hessian{T <: Number}(f::Function,
277277
# Return the Hessian
278278
return H
279279
end
280-
function finite_difference_hessian{T <: Number}(f::Function,
281-
g::Function,
280+
function finite_difference_hessian{T <: Number}(f,
281+
g,
282282
x::AbstractVector{T},
283283
dtype::Symbol = :central)
284284
finite_difference_jacobian(g, x, dtype)
@@ -294,7 +294,7 @@ end
294294

295295
# Higher precise finite difference method based on Taylor series approximation.
296296
# h is the stepsize
297-
function taylor_finite_difference(f::Function,
297+
function taylor_finite_difference(f,
298298
x::Real,
299299
dtype::Symbol = :central,
300300
h::Real = 10e-4)
@@ -313,7 +313,7 @@ function taylor_finite_difference(f::Function,
313313
return d
314314
end
315315

316-
function taylor_finite_difference_hessian(f::Function,
316+
function taylor_finite_difference_hessian(f,
317317
x::Real,
318318
h::Real)
319319
f_x = f(x)
@@ -329,16 +329,15 @@ end
329329
##############################################################################
330330

331331
# The function "dirderivative" calculates directional derivatives in the direction v.
332-
# The function supplied must have the form AbstractVector{Float64} -> Float64
333-
# function dirderivative(f::Function, v::AbstractVector{Float64}, x0::AbstractVector{Float64}, h::Float64, twoside::Bool)
332+
# function dirderivative(f, v::AbstractVector{Float64}, x0::AbstractVector{Float64}, h::Float64, twoside::Bool)
334333
# derivative(t::Float64 -> f(x0 + v*t) / norm(v), 0.0, h, twoside)
335334
# end
336-
# function dirderivative(f::Function, v::AbstractVector{Float64}, x0::AbstractVector{Float64}, h::Float64)
335+
# function dirderivative(f, v::AbstractVector{Float64}, x0::AbstractVector{Float64}, h::Float64)
337336
# dirderivative(f, v, x0, h, true)
338337
# end
339-
# function dirderivative(f::Function, v::AbstractVector{Float64}, x0::AbstractVector{Float64}, )
338+
# function dirderivative(f, v::AbstractVector{Float64}, x0::AbstractVector{Float64}, )
340339
# derivative(f, v, x0, 0.0001)
341340
# end
342-
# function dirderivative(f::Function, v::AbstractVector{Float64})
341+
# function dirderivative(f, v::AbstractVector{Float64})
343342
# x -> dirderivative(f, v, x)
344343
# end

src/symbolic.jl

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ function simplify(ex::Expr)
9696
return eval(current_module(), ex)
9797
end
9898
new_ex = simplify(SymbolParameter(ex.args[1]), ex.args[2:end])
99-
while new_ex != ex
99+
while !(isequal(new_ex, ex))
100100
new_ex, ex = simplify(new_ex), new_ex
101101
end
102102
return new_ex
@@ -149,6 +149,14 @@ isminus(ex) = false
149149

150150
# Assume length(args) == 3
151151
function simplify(::SymbolParameter{:-}, args)
152+
# Special Case: simplify(:(0 - x)) == -x
153+
if length(args) == 2 && args[1] == 0
154+
return Expr(:call, :-, args[2])
155+
# Special Case: simplify(:(x - 0)) == x
156+
elseif length(args) == 2 && args[2] == 0
157+
return args[1]
158+
end
159+
152160
# Remove any 0's in a subtraction
153161
args = map(simplify, filter(x -> x != 0, args))
154162
if length(args) == 0

test/symbolic.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,9 @@ end
8282
# Simplify tests
8383
#
8484

85+
@test isequal(simplify(:(x-0)), :x)
86+
@test isequal(simplify(:(0-x)), :(-x))
87+
8588
@test isequal(simplify(:(x+y)), :(+(x,y)))
8689
@test isequal(simplify(:(x+3)), :(+(3,x)))
8790
@test isequal(simplify(:(x+3+4)), :(+(7,x)))
@@ -92,6 +95,8 @@ end
9295
@test isequal(simplify(:(x*3*4)), :(*(12,x)))
9396
@test isequal(simplify(:(2*y*x*3)), :(*(6,y,x)))
9497

98+
@test isequal(simplify(:(sin((1*(sin(0/0)))))), NaN)
99+
95100
#
96101
# Tests with ifelse
97102
#

0 commit comments

Comments
 (0)