Skip to content
Closed
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
4 changes: 2 additions & 2 deletions REQUIRE
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
julia 0.4
Compat 0.17.0
julia 0.6
Compat v0.26.0
14 changes: 6 additions & 8 deletions src/Calculus.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
isdefined(Base, :__precompile__) && __precompile__()

module Calculus
import Compat
using Compat
import Base.ctranspose
export check_derivative,
check_gradient,
Expand All @@ -25,26 +25,26 @@ module Calculus
# abstract type HessianEstimator end

# const NonDifferentiableFunction = Function
# type DifferentiableFunction
# struct DifferentiableFunction
# f
# g
# end
# type TwiceDifferentiableFunction
# struct TwiceDifferentiableFunction
# f
# g
# h
# end
# type NonDifferentiableBundledFunction <: BundledFunction
# struct NonDifferentiableBundledFunction <: BundledFunction
# f
# fstorage::Any
# end
# type DifferentiableBundledFunction <: BundledFunction
# struct DifferentiableBundledFunction <: BundledFunction
# f
# g
# fstorage::Any
# gstorage::Any
# end
# type TwiceDifferentiableBundledFunction <: BundledFunction
# struct TwiceDifferentiableBundledFunction <: BundledFunction
# f
# g
# h
Expand All @@ -56,8 +56,6 @@ module Calculus
include("finite_difference.jl")
include("derivative.jl")
include("check_derivative.jl")
@Base.deprecate integrate(f,a,b) quadgk(f,a,b)[1]
@Base.deprecate integrate(f,a,b,method) quadgk(f,a,b)[1]
include("symbolic.jl")
include("differentiate.jl")
include("deparse.jl")
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
2 changes: 1 addition & 1 deletion src/deparse.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const op_precedence = Compat.@compat Dict(:+ => 1, :- => 1, :* => 2, :/ => 2, :^ => 3)
const op_precedence = Dict(:+ => 1, :- => 1, :* => 2, :/ => 2, :^ => 3)

function deparse(ex::Expr, outer_precedence=0)
if ex.head != :call
Expand Down
32 changes: 11 additions & 21 deletions src/derivative.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,15 @@ 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)
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, 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, 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 @@ -43,16 +33,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 @@ -61,10 +51,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 @@ -73,10 +63,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
10 changes: 5 additions & 5 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 @@ -203,7 +203,7 @@ export symbolic_derivatives_1arg

# deprecated: for backward compatibility with packages that used
# this unexported interface.
derivative_rules = Vector{Compat.@compat(Tuple{Symbol,Expr})}(0)
derivative_rules = Vector{Tuple{Symbol,Expr}}(0)
for (s,ex) in symbolic_derivative_1arg_list
push!(derivative_rules, (s, :(xp*$ex)))
end
Expand Down Expand Up @@ -278,6 +278,6 @@ function differentiate(ex::Expr, targets::Vector{Symbol})
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::String, target...) = differentiate(parse(s), target...)
differentiate(s::String, target::String) = differentiate(parse(s), Symbol(target))
differentiate(s::String, targets::Vector{T}) where {T <: String} = differentiate(parse(s), map(Symbol, targets))
63 changes: 30 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,11 @@ 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 +188,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 +210,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 +228,9 @@ 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 +260,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 +274,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
12 changes: 6 additions & 6 deletions src/symbolic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import Base.show, Base.(==)
#
#################################################################

Compat.@compat abstract type Symbolic end
Compat.@compat abstract type AbstractVariable <: Symbolic end
abstract type Symbolic end
abstract type AbstractVariable <: Symbolic end
const SymbolicVariable = Union{Symbol, AbstractVariable}


Expand All @@ -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}
struct SymbolParameter{T}
end
SymbolParameter(s::Symbol) = SymbolParameter{s}()

Expand All @@ -86,14 +86,14 @@ 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
return ex
end
if all(isnumber, ex.args[2:end]) && length(ex.args) > 1
return eval(current_module(), ex)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that this is wrong.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok. Do you know where it is documented how to upgrade code like this?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mentioned it in #118. There will be no dynamically scoped current module so you need to thread it through manually.

return eval(@__MODULE__, ex)
end
new_ex = simplify(SymbolParameter(ex.args[1]), ex.args[2:end])
while !(isequal(new_ex, ex))
Expand Down