JuliaMath / Calculus.jl

Calculus functions in Julia
Other
275 stars 78 forks source link

Calculus breaks Base.gradient function with ambiguous methods #123

Open david-pl opened 6 years ago

david-pl commented 6 years ago

Since a recent update, there seems to be a problem with the gradient fucntion.

The following works fine:

julia> gradient([1:3;])
3-element Array{Float64,1}:
 1.0
 1.0
 1.0

julia> using Calculus

julia> gradient([1:3;])
3-element Array{Float64,1}:
 1.0
 1.0
 1.0

However, if Calculus is used before calling gradient, you get

julia> using Calculus

julia> gradient([1:3;])
ERROR: MethodError: gradient(::Array{Int64,1}, ::Array{Int64,1}) is ambiguous. Candidates:
  gradient(f, x::Union{Array{T,1}, T}) where T<:Number in Calculus at /home/david/.julia/v0.6/Calculus/src/derivative.jl:17
  gradient(F::AbstractArray{T,1} where T, h::Array{T,1} where T) in Base.LinAlg at linalg/dense.jl:186
Possible fix, define
  gradient(::AbstractArray{T,1} where T, ::Array{T<:Number,1})
Stacktrace:
 [1] gradient(::Array{Int64,1}) at ./linalg/generic.jl:282

Even directly calling the function from base with Base.LinAlg.gradient throws the above error. I am using v"0.3.1", but this also happens when pinning to v"0.3.0". Older version fail to pre-compile with Julia 0.6.2.

mlubin commented 6 years ago

This is a bug. Calculus should not be extending Base.gradient.