-
Notifications
You must be signed in to change notification settings - Fork 77
/
Copy pathderivative.jl
89 lines (83 loc) · 4.15 KB
/
derivative.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
function derivative(f::Function, ftype::Symbol, dtype::Symbol)
if ftype == :scalar
g(x::Number) = finite_difference(f, float(x), dtype)
elseif ftype == :vector
g(x::AbstractVector) = finite_difference(f, float(x), dtype)
else
error("ftype must :scalar or :vector")
end
return g
end
Compat.@compat derivative{T <: Number}(f::Function, x::Union{T, AbstractVector{T}}, dtype::Symbol = :central) = finite_difference(f, float(x), dtype)
derivative(f::Function, dtype::Symbol = :central) = derivative(f, :scalar, dtype)
Compat.@compat gradient{T <: Number}(f::Function, x::Union{T, AbstractVector{T}}, dtype::Symbol = :central) = finite_difference(f, float(x), dtype)
gradient(f::Function, dtype::Symbol = :central) = derivative(f, :vector, dtype)
Compat.@compat function Base.gradient{T <: Number}(f::Function, x::Union{T, AbstractVector{T}}, dtype::Symbol = :central)
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.")
Calculus.gradient(f,x,dtype)
end
function Base.gradient(f::Function, dtype::Symbol = :central)
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.")
Calculus.gradient(f,dtype)
end
ctranspose(f::Function) = derivative(f)
function jacobian{T <: Number}(f::Function, x::AbstractVector{T}, dtype::Symbol)
finite_difference_jacobian(f, x, dtype)
end
function jacobian(f::Function, dtype::Symbol)
g(x::AbstractVector) = finite_difference_jacobian(f, x, dtype)
return g
end
jacobian(f::Function) = jacobian(f, :central)
function second_derivative(f::Function, g::Function, ftype::Symbol, dtype::Symbol)
if ftype == :scalar
h(x::Number) = finite_difference_hessian(f, g, x, dtype)
elseif ftype == :vector
h(x::AbstractVector) = finite_difference_hessian(f, g, x, dtype)
else
error("ftype must :scalar or :vector")
end
return h
end
Compat.@compat function second_derivative{T <: Number}(f::Function, g::Function, x::Union{T, AbstractVector{T}}, dtype::Symbol)
finite_difference_hessian(f, g, x, dtype)
end
Compat.@compat function hessian{T <: Number}(f::Function, g::Function, x::Union{T, AbstractVector{T}}, dtype::Symbol)
finite_difference_hessian(f, g, x, dtype)
end
Compat.@compat function second_derivative{T <: Number}(f::Function, g::Function, x::Union{T, AbstractVector{T}})
finite_difference_hessian(f, g, x, :central)
end
Compat.@compat function hessian{T <: Number}(f::Function, g::Function, x::Union{T, AbstractVector{T}})
finite_difference_hessian(f, g, x, :central)
end
function second_derivative(f::Function, x::Number, dtype::Symbol)
finite_difference_hessian(f, derivative(f), x, dtype)
end
function hessian(f::Function, x::Number, dtype::Symbol)
finite_difference_hessian(f, derivative(f), x, dtype)
end
function second_derivative{T <: Number}(f::Function, x::AbstractVector{T}, dtype::Symbol)
finite_difference_hessian(f, gradient(f), x, dtype)
end
function hessian{T <: Number}(f::Function, x::AbstractVector{T}, dtype::Symbol)
finite_difference_hessian(f, gradient(f), x, dtype)
end
function second_derivative(f::Function, x::Number)
finite_difference_hessian(f, derivative(f), x, :central)
end
function hessian(f::Function, x::Number)
finite_difference_hessian(f, derivative(f), x, :central)
end
function second_derivative{T <: Number}(f::Function, x::AbstractVector{T})
finite_difference_hessian(f, gradient(f), x, :central)
end
function hessian{T <: Number}(f::Function, x::AbstractVector{T})
finite_difference_hessian(f, gradient(f), x, :central)
end
second_derivative(f::Function, g::Function, dtype::Symbol) = second_derivative(f, g, :scalar, dtype)
second_derivative(f::Function, g::Function) = second_derivative(f, g, :scalar, :central)
second_derivative(f::Function) = second_derivative(f, derivative(f), :scalar, :central)
hessian(f::Function, g::Function, dtype::Symbol) = second_derivative(f, g, :vector, dtype)
hessian(f::Function, g::Function) = second_derivative(f, g, :vector, :central)
hessian(f::Function) = second_derivative(f, gradient(f), :vector, :central)