Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ os:
- linux
- osx
julia:
- 0.4
- 0.5
- 0.6
- nightly
notifications:
email: false
2 changes: 1 addition & 1 deletion REQUIRE
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
julia 0.4
julia 0.6
Compat 0.17.0
3 changes: 1 addition & 2 deletions src/Calculus.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
isdefined(Base, :__precompile__) && __precompile__()

__precompile__()
module Calculus
import Compat
export check_derivative,
Expand Down
8 changes: 4 additions & 4 deletions src/check_derivative.jl
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
Compat.@compat function check_derivative(f, g, x::Number)
function check_derivative(f, g, x::Number)
auto_g = derivative(f)
return maximum(abs.(g(x) - auto_g(x)))
end

Compat.@compat function check_gradient{T <: Number}(f, g, x::Vector{T})
function check_gradient(f, g, x::Vector{T}) where T <: Number
auto_g = gradient(f)
return maximum(abs.(g(x) - auto_g(x)))
end

Compat.@compat function check_second_derivative(f, h, x::Number)
function check_second_derivative(f, h, x::Number)
auto_h = second_derivative(f)
return maximum(abs.(h(x) - auto_h(x)))
end

Compat.@compat function check_hessian{T <: Number}(f, h, x::Vector{T})
function check_hessian(f, h, x::Vector{T}) where T <: Number
auto_h = hessian(f)
return maximum(abs.(h(x) - auto_h(x)))
end
24 changes: 12 additions & 12 deletions src/derivative.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ function derivative(f, ftype::Symbol, dtype::Symbol)
error("ftype must :scalar or :vector")
end
end
Compat.@compat derivative{T <: Number}(f, x::Union{T, Vector{T}}, dtype::Symbol = :central) = finite_difference(f, float(x), dtype)
derivative(f, x::Union{T, Vector{T}}, dtype::Symbol = :central) where {T <: Number} = finite_difference(f, float(x), dtype)
derivative(f, dtype::Symbol = :central) = derivative(f, :scalar, dtype)

Compat.@compat gradient{T <: Number}(f, x::Union{T, Vector{T}}, dtype::Symbol = :central) = finite_difference(f, float(x), dtype)
gradient(f, x::Union{T, Vector{T}}, dtype::Symbol = :central) where {T <: Number} = finite_difference(f, float(x), dtype)
gradient(f, dtype::Symbol = :central) = derivative(f, :vector, dtype)

Compat.@compat function Base.gradient{T <: Number}(f, x::Union{T, Vector{T}}, dtype::Symbol = :central)
function Base.gradient(f, x::Union{T, Vector{T}}, dtype::Symbol = :central) where T <: Number
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
Expand All @@ -29,7 +29,7 @@ else
Base.ctranspose(f::Function) = derivative(f)
end

function jacobian{T <: Number}(f, x::Vector{T}, dtype::Symbol)
function jacobian(f, x::Vector{T}, dtype::Symbol) where T <: Number
finite_difference_jacobian(f, x, dtype)
end
function jacobian(f, dtype::Symbol)
Expand All @@ -47,16 +47,16 @@ function second_derivative(f, g, ftype::Symbol, dtype::Symbol)
error("ftype must :scalar or :vector")
end
end
Compat.@compat function second_derivative{T <: Number}(f, g, x::Union{T, Vector{T}}, dtype::Symbol)
function second_derivative(f, g, x::Union{T, Vector{T}}, dtype::Symbol) where T <: Number
finite_difference_hessian(f, g, x, dtype)
end
Compat.@compat function hessian{T <: Number}(f, g, x::Union{T, Vector{T}}, dtype::Symbol)
function hessian(f, g, x::Union{T, Vector{T}}, dtype::Symbol) where T <: Number
finite_difference_hessian(f, g, x, dtype)
end
Compat.@compat function second_derivative{T <: Number}(f, g, x::Union{T, Vector{T}})
function second_derivative(f, g, x::Union{T, Vector{T}}) where T <: Number
finite_difference_hessian(f, g, x, :central)
end
Compat.@compat function hessian{T <: Number}(f, g, x::Union{T, Vector{T}})
function hessian(f, g, x::Union{T, Vector{T}}) where T <: Number
finite_difference_hessian(f, g, x, :central)
end
function second_derivative(f, x::Number, dtype::Symbol)
Expand All @@ -65,10 +65,10 @@ end
function hessian(f, x::Number, dtype::Symbol)
finite_difference_hessian(f, derivative(f), x, dtype)
end
function second_derivative{T <: Number}(f, x::Vector{T}, dtype::Symbol)
function second_derivative(f, x::Vector{T}, dtype::Symbol) where T <: Number
finite_difference_hessian(f, gradient(f), x, dtype)
end
function hessian{T <: Number}(f, x::Vector{T}, dtype::Symbol)
function hessian(f, x::Vector{T}, dtype::Symbol) where T <: Number
finite_difference_hessian(f, gradient(f), x, dtype)
end
function second_derivative(f, x::Number)
Expand All @@ -77,10 +77,10 @@ end
function hessian(f, x::Number)
finite_difference_hessian(f, derivative(f), x, :central)
end
function second_derivative{T <: Number}(f, x::Vector{T})
function second_derivative(f, x::Vector{T}) where T <: Number
finite_difference_hessian(f, gradient(f), x, :central)
end
function hessian{T <: Number}(f, x::Vector{T})
function hessian(f, x::Vector{T}) where T <: Number
finite_difference_hessian(f, gradient(f), x, :central)
end
second_derivative(f, g, dtype::Symbol) = second_derivative(f, g, :scalar, dtype)
Expand Down
11 changes: 2 additions & 9 deletions src/differentiate.jl
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ function differentiate(ex::Expr,wrt)
simplify(differentiate(SymbolParameter(ex.args[1]), ex.args[2:end], wrt))
end

differentiate{T}(x::SymbolParameter{T}, args, wrt) = error("Derivative of function " * string(T) * " not supported")
differentiate(x::SymbolParameter{T}, args, wrt) where {T} = error("Derivative of function " * string(T) * " not supported")

# The Power Rule:
function differentiate(::SymbolParameter{:^}, args, wrt)
Expand Down Expand Up @@ -184,13 +184,6 @@ symbolic_derivative_1arg_list = [
( :dawson, :( (1 - 2x * dawson(x)) ))
]

# `airy` and `airyprime` are deprecated in v0.6 (see JuliaLang/julia#18050)
if VERSION < v"0.6.0-dev.1767"
push!(symbolic_derivative_1arg_list, (:airy, :(airyprime(x))))
push!(symbolic_derivative_1arg_list, (:airyprime, :(x * airy(x))))
end


# This is the public interface for accessing the list of symbolic
# derivatives. The format is a list of (Symbol,Expr) tuples
# (:f, deriv_expr), where deriv_expr is a symbolic
Expand Down Expand Up @@ -280,4 +273,4 @@ end
differentiate(ex::Expr) = differentiate(ex, :x)
differentiate(s::Compat.AbstractString, target...) = differentiate(parse(s), target...)
differentiate(s::Compat.AbstractString, target::Compat.AbstractString) = differentiate(parse(s), symbol(target))
differentiate{T <: Compat.AbstractString}(s::Compat.AbstractString, targets::Vector{T}) = differentiate(parse(s), map(symbol, targets))
differentiate(s::Compat.AbstractString, targets::Vector{T}) where {T <: Compat.AbstractString} = differentiate(parse(s), map(symbol, targets))
66 changes: 33 additions & 33 deletions src/finite_difference.jl
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ macro complexrule(x, e)
end
end

function finite_difference{T <: Number}(f,
x::T,
dtype::Symbol = :central)
function finite_difference(f,
x::T,
dtype::Symbol = :central) where T <: Number
if dtype == :forward
@forwardrule x epsilon
xplusdx = x + epsilon
Expand Down Expand Up @@ -83,7 +83,7 @@ end
##
##############################################################################

function complex_differentiable_abs{T <: Complex}(z::T)
function complex_differentiable_abs(z::T) where T <: Complex
if real(z) < 0
return -real(z) - im * imag(z)
else
Expand All @@ -97,10 +97,10 @@ end
##
##############################################################################

function finite_difference!{S <: Number, T <: Number}(f,
x::AbstractVector{S},
g::AbstractVector{T},
dtype::Symbol)
function finite_difference!(f,
x::AbstractVector{S},
g::AbstractVector{T},
dtype::Symbol) where {S <: Number, T <: Number}
# What is the dimension of x?
n = length(x)

Expand Down Expand Up @@ -135,9 +135,9 @@ function finite_difference!{S <: Number, T <: Number}(f,

return
end
function finite_difference{T <: Number}(f,
x::AbstractVector{T},
dtype::Symbol = :central)
function finite_difference(f,
x::AbstractVector{T},
dtype::Symbol = :central) where T <: Number
# Allocate memory for gradient
g = Vector{Float64}(length(x))

Expand All @@ -154,13 +154,13 @@ end
##
##############################################################################

function finite_difference_jacobian!{R <: Number,
S <: Number,
T <: Number}(f,
x::AbstractVector{R},
f_x::AbstractVector{S},
J::Array{T},
dtype::Symbol = :central)
function finite_difference_jacobian!(f,
x::AbstractVector{R},
f_x::AbstractVector{S},
J::Array{T},
dtype::Symbol = :central) where {R <: Number,
S <: Number,
T <: Number}
# What is the dimension of x?
m, n = size(J)

Expand Down Expand Up @@ -190,9 +190,9 @@ function finite_difference_jacobian!{R <: Number,

return
end
function finite_difference_jacobian{T <: Number}(f,
x::AbstractVector{T},
dtype::Symbol = :central)
function finite_difference_jacobian(f,
x::AbstractVector{T},
dtype::Symbol = :central) where T <: Number
# Establish a baseline for f_x
f_x = f(x)

Expand All @@ -212,8 +212,8 @@ end
##
##############################################################################

function finite_difference_hessian{T <: Number}(f,
x::T)
function finite_difference_hessian(f,
x::T) where T <: Number
@hessianrule x epsilon
(f(x + epsilon) - 2*f(x) + f(x - epsilon))/epsilon^2
end
Expand All @@ -230,10 +230,10 @@ end
##
##############################################################################

function finite_difference_hessian!{S <: Number,
T <: Number}(f,
x::AbstractVector{S},
H::Array{T})
function finite_difference_hessian!(f,
x::AbstractVector{S},
H::Array{T}) where {S <: Number,
T <: Number}
# What is the dimension of x?
n = length(x)

Expand Down Expand Up @@ -263,8 +263,8 @@ function finite_difference_hessian!{S <: Number,
end
Base.LinAlg.copytri!(H,'U')
end
function finite_difference_hessian{T <: Number}(f,
x::AbstractVector{T})
function finite_difference_hessian(f,
x::AbstractVector{T}) where T <: Number
# What is the dimension of x?
n = length(x)

Expand All @@ -277,10 +277,10 @@ function finite_difference_hessian{T <: Number}(f,
# Return the Hessian
return H
end
function finite_difference_hessian{T <: Number}(f,
g,
x::AbstractVector{T},
dtype::Symbol = :central)
function finite_difference_hessian(f,
g,
x::AbstractVector{T},
dtype::Symbol = :central) where T <: Number
finite_difference_jacobian(g, x, dtype)
end

Expand Down
6 changes: 3 additions & 3 deletions src/symbolic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const SymbolicVariable = Union{Symbol, AbstractVariable}
#
#################################################################

type BasicVariable <: AbstractVariable
mutable struct BasicVariable <: AbstractVariable
sym::Symbol
end
# The following is probably too plain.
Expand Down Expand Up @@ -66,7 +66,7 @@ end
#
#################################################################

type SymbolParameter{T}
mutable struct SymbolParameter{T}
end
SymbolParameter(s::Symbol) = SymbolParameter{s}()

Expand All @@ -86,7 +86,7 @@ simplify(n::Number) = n
simplify(s::SymbolicVariable) = s

# The default is just to simplify arguments.
simplify{T}(x::SymbolParameter{T}, args) = Expr(:call, T, map(simplify, args)...)
simplify(x::SymbolParameter{T}, args) where {T} = Expr(:call, T, map(simplify, args)...)

function simplify(ex::Expr)
if ex.head != :call
Expand Down