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
2 changes: 1 addition & 1 deletion REQUIRE
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
julia 0.6
Compat 0.17.0
Compat 0.62.0
6 changes: 3 additions & 3 deletions src/Calculus.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
__precompile__()
module Calculus
import Compat
using Compat
export check_derivative,
check_gradient,
check_hessian,
Expand Down Expand Up @@ -54,8 +54,8 @@ 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]
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
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
12 changes: 8 additions & 4 deletions src/derivative.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,17 @@ derivative(f, dtype::Symbol = :central) = derivative(f, :scalar, 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)

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.")
function Compat.LinearAlgebra.gradient(f, x::Union{T, Vector{T}}, dtype::Symbol = :central) where T <: Number
Base.depwarn("The finite difference methods from Calculus.jl no longer extend " *
"Base.gradient and should be called as Calculus.gradient instead. " *
"This usage is deprecated.", :gradient)
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.")
function Compat.LinearAlgebra.gradient(f, dtype::Symbol = :central)
Base.depwarn("The finite difference methods from Calculus.jl no longer extend " *
"Base.gradient and should be called as Calculus.gradient instead. " *
"This usage is deprecated.", :gradient)
Calculus.gradient(f,dtype)
end

Expand Down
16 changes: 9 additions & 7 deletions src/differentiate.jl
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,9 @@ end
# d/dx (f * g * h) = (d/dx f) * g * h + f * (d/dx g) * h + ...
function differentiate(::SymbolParameter{:*}, args, wrt)
n = length(args)
res_args = Vector{Any}(n)
res_args = Vector{Any}(undef, n)
for i in 1:n
new_args = Vector{Any}(n)
new_args = Vector{Any}(undef, n)
for j in 1:n
if j == i
new_args[j] = differentiate(args[j], wrt)
Expand Down Expand Up @@ -196,7 +196,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}}()
for (s,ex) in symbolic_derivative_1arg_list
push!(derivative_rules, (s, :(xp*$ex)))
end
Expand Down Expand Up @@ -263,14 +263,16 @@ end

function differentiate(ex::Expr, targets::Vector{Symbol})
n = length(targets)
exprs = Vector{Any}(n)
exprs = Vector{Any}(undef, n)
for i in 1:n
exprs[i] = differentiate(ex, targets[i])
end
return exprs
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(s::Compat.AbstractString, targets::Vector{T}) where {T <: Compat.AbstractString} = differentiate(parse(s), map(symbol, targets))
differentiate(s::AbstractString, target...) = differentiate(Compat.Meta.parse(s), target...)
differentiate(s::AbstractString, target::AbstractString) =
differentiate(Compat.Meta.parse(s), Symbol(target))
differentiate(s::AbstractString, targets::Vector{T}) where {T <: AbstractString} =
differentiate(Compat.Meta.parse(s), map(Symbol, targets))
6 changes: 3 additions & 3 deletions src/finite_difference.jl
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ function finite_difference(f,
x::AbstractVector{T},
dtype::Symbol = :central) where T <: Number
# Allocate memory for gradient
g = Vector{Float64}(length(x))
g = Vector{Float64}(undef, length(x))

# Mutate allocated gradient
finite_difference!(f, float(x), g, dtype)
Expand Down Expand Up @@ -261,15 +261,15 @@ function finite_difference_hessian!(f,
end
xpp[i], xpm[i], xmp[i], xmm[i] = xi, xi, xi, xi
end
Base.LinAlg.copytri!(H,'U')
Compat.LinearAlgebra.copytri!(H,'U')
end
function finite_difference_hessian(f,
x::AbstractVector{T}) where T <: Number
# What is the dimension of x?
n = length(x)

# Allocate an empty Hessian
H = Matrix{Float64}(n, n)
H = Matrix{Float64}(undef, n, n)

# Mutate the allocated Hessian
finite_difference_hessian!(f, x, H)
Expand Down
6 changes: 3 additions & 3 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 Down Expand Up @@ -93,7 +93,7 @@ function simplify(ex::Expr)
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))
Expand Down
12 changes: 6 additions & 6 deletions test/deparse.jl
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
@test isequal(deparse(:(cos(x) + sin(x))), "cos(x) + sin(x)")
@test isequal(deparse(:(cos(x) + sin(x) + exp(-x))), "cos(x) + sin(x) + exp(-x)")
@test isequal(deparse(parse("x+y*z")), "x + y * z")
@test isequal(deparse(parse("(x+y)*z")), "(x + y) * z")
@test isequal(deparse(parse("1/(x/y)")), "1 / (x / y)")
@test isequal(deparse(parse("1/(x*y)")), "1 / (x * y)")
@test isequal(deparse(parse("z^(x+y)")), "z ^ (x + y)")
@test isequal(deparse(parse("z^(x*y)")), "z ^ (x * y)")
@test isequal(deparse(Compat.Meta.parse("x+y*z")), "x + y * z")
@test isequal(deparse(Compat.Meta.parse("(x+y)*z")), "(x + y) * z")
@test isequal(deparse(Compat.Meta.parse("1/(x/y)")), "1 / (x / y)")
@test isequal(deparse(Compat.Meta.parse("1/(x*y)")), "1 / (x * y)")
@test isequal(deparse(Compat.Meta.parse("z^(x+y)")), "z ^ (x + y)")
@test isequal(deparse(Compat.Meta.parse("z^(x*y)")), "z ^ (x * y)")
8 changes: 4 additions & 4 deletions test/derivative.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ f2(x::Vector) = sin(x[1])
#

f3(x::Real) = sin(x)
for x in linspace(0.0, 0.1, 11) # seq()
@test norm(f3'(x) - cos(x)) < 10e-4
for x in Compat.range(0.0, stop=0.1, length=11) # seq()
@test norm(f3'(x) - cos(x)) < 10e-4
end

#
Expand All @@ -35,8 +35,8 @@ f4(x::Vector) = (100.0 - x[1])^2 + (50.0 - x[2])^2
# jacobian()
#

@test norm(Calculus.jacobian(identity, rand(3), :forward) - eye(3)) < 10e-4
@test norm(Calculus.jacobian(identity, rand(3), :central) - eye(3)) < 10e-4
@test norm(Calculus.jacobian(identity, rand(3), :forward) - Matrix(1.0I, 3, 3)) < 10e-4
@test norm(Calculus.jacobian(identity, rand(3), :central) - Matrix(1.0I, 3, 3)) < 10e-4

#
# second_derivative()
Expand Down
4 changes: 3 additions & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
#

using Calculus
using Base.Test
using Compat
using Compat.Test
using Compat.LinearAlgebra

tests = ["finite_difference",
"derivative",
Expand Down