From 325e03f09e83500ae382420870205103330e0b86 Mon Sep 17 00:00:00 2001 From: Kristoffer Carlsson Date: Fri, 4 Aug 2017 22:25:20 +0200 Subject: [PATCH 1/2] fix deprecations --- REQUIRE | 4 +-- src/Calculus.jl | 14 ++++----- src/check_derivative.jl | 8 ++--- src/deparse.jl | 2 +- src/derivative.jl | 32 +++++++------------- src/differentiate.jl | 10 +++---- src/finite_difference.jl | 63 +++++++++++++++++++--------------------- src/symbolic.jl | 12 ++++---- 8 files changed, 65 insertions(+), 80 deletions(-) diff --git a/REQUIRE b/REQUIRE index c7eded8..9feebec 100644 --- a/REQUIRE +++ b/REQUIRE @@ -1,2 +1,2 @@ -julia 0.4 -Compat 0.17.0 +julia 0.6 +Compat v0.26.0 \ No newline at end of file diff --git a/src/Calculus.jl b/src/Calculus.jl index 5cd4fdf..563638e 100644 --- a/src/Calculus.jl +++ b/src/Calculus.jl @@ -1,7 +1,7 @@ isdefined(Base, :__precompile__) && __precompile__() module Calculus - import Compat + using Compat import Base.ctranspose export check_derivative, check_gradient, @@ -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 @@ -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") diff --git a/src/check_derivative.jl b/src/check_derivative.jl index d416c81..cd336ba 100644 --- a/src/check_derivative.jl +++ b/src/check_derivative.jl @@ -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 diff --git a/src/deparse.jl b/src/deparse.jl index 2cfc985..1ca0f89 100644 --- a/src/deparse.jl +++ b/src/deparse.jl @@ -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 diff --git a/src/derivative.jl b/src/derivative.jl index c7d9d31..5a90035 100644 --- a/src/derivative.jl +++ b/src/derivative.jl @@ -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) @@ -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) @@ -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) @@ -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) diff --git a/src/differentiate.jl b/src/differentiate.jl index 94e2138..80970d3 100644 --- a/src/differentiate.jl +++ b/src/differentiate.jl @@ -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) @@ -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 @@ -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)) diff --git a/src/finite_difference.jl b/src/finite_difference.jl index 44ce4f1..5a93a42 100644 --- a/src/finite_difference.jl +++ b/src/finite_difference.jl @@ -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 @@ -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 @@ -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) @@ -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)) @@ -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) @@ -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) @@ -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 @@ -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) @@ -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) @@ -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 diff --git a/src/symbolic.jl b/src/symbolic.jl index 8bdd0da..72e366d 100644 --- a/src/symbolic.jl +++ b/src/symbolic.jl @@ -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} @@ -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. @@ -66,7 +66,7 @@ end # ################################################################# -type SymbolParameter{T} +struct SymbolParameter{T} end SymbolParameter(s::Symbol) = SymbolParameter{s}() @@ -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) + return eval(@__MODULE__, ex) end new_ex = simplify(SymbolParameter(ex.args[1]), ex.args[2:end]) while !(isequal(new_ex, ex)) From 0c62a8b4cd94c931c82a22839877555f2dd44e56 Mon Sep 17 00:00:00 2001 From: Kristoffer Carlsson Date: Fri, 11 Aug 2017 14:22:01 +0200 Subject: [PATCH 2/2] remove testing on old versions --- .travis.yml | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index 6508998..dd5a7e5 100644 --- a/.travis.yml +++ b/.travis.yml @@ -3,8 +3,7 @@ os: - linux - osx julia: - - 0.4 - - 0.5 + - 0.6 - nightly notifications: email: false