Skip to content

Commit

Permalink
deprecate/remove gradient from base, fix #16113
Browse files Browse the repository at this point in the history
  • Loading branch information
fredrikekre committed Sep 22, 2017
1 parent 5ad2246 commit 761dee6
Show file tree
Hide file tree
Showing 9 changed files with 37 additions and 72 deletions.
37 changes: 37 additions & 0 deletions base/deprecated.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1846,6 +1846,43 @@ function toc()
return t
end

# PR #23816: deprecation of gradient
export gradient
@eval Base.LinAlg begin
export gradient

function gradient(args...)
Base.depwarn("gradient will be removed from base in the next release.", :gradient)
return _gradient(args...)
end

_gradient(F::BitVector) = _gradient(Array(F))
_gradient(F::BitVector, h::Real) = _gradient(Array(F), h)
_gradient(F::Vector, h::BitVector) = _gradient(F, Array(h))
_gradient(F::BitVector, h::Vector) = _gradient(Array(F), h)
_gradient(F::BitVector, h::BitVector) = _gradient(Array(F), Array(h))

function _gradient(F::AbstractVector, h::Vector)
n = length(F)
T = typeof(oneunit(eltype(F))/oneunit(eltype(h)))
g = similar(F, T)
if n == 1
g[1] = zero(T)
elseif n > 1
g[1] = (F[2] - F[1]) / (h[2] - h[1])
g[n] = (F[n] - F[n-1]) / (h[end] - h[end-1])
if n > 2
h = h[3:n] - h[1:n-2]
g[2:n-1] = (F[3:n] - F[1:n-2]) ./ h
end
end
g
end

_gradient(F::AbstractVector) = _gradient(F, [1:length(F);])
_gradient(F::AbstractVector, h::Real) = _gradient(F, [h*(1:length(F));])
end

@noinline function getaddrinfo(callback::Function, host::AbstractString)
depwarn("getaddrinfo with a callback function is deprecated, wrap code in @async instead for deferred execution", :getaddrinfo)
@async begin
Expand Down
1 change: 0 additions & 1 deletion base/exports.jl
Original file line number Diff line number Diff line change
Expand Up @@ -458,7 +458,6 @@ export
findnz,
first,
flipdim,
gradient,
hcat,
hvcat,
ind2sub,
Expand Down
9 changes: 0 additions & 9 deletions base/linalg/bitarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -74,15 +74,6 @@ function tril(B::BitMatrix, k::Integer=0)
A
end

## diff and gradient

# TODO: this could be improved (is it worth it?)
gradient(F::BitVector) = gradient(Array(F))
gradient(F::BitVector, h::Real) = gradient(Array(F), h)
gradient(F::Vector, h::BitVector) = gradient(F, Array(h))
gradient(F::BitVector, h::Vector) = gradient(Array(F), h)
gradient(F::BitVector, h::BitVector) = gradient(Array(F), Array(h))

## diag and related

function diag(B::BitMatrix)
Expand Down
17 changes: 0 additions & 17 deletions base/linalg/dense.jl
Original file line number Diff line number Diff line change
Expand Up @@ -215,23 +215,6 @@ function tril!(M::AbstractMatrix, k::Integer)
end
tril(M::Matrix, k::Integer) = tril!(copy(M), k)

function gradient(F::AbstractVector, h::Vector)
n = length(F)
T = typeof(oneunit(eltype(F))/oneunit(eltype(h)))
g = similar(F, T)
if n == 1
g[1] = zero(T)
elseif n > 1
g[1] = (F[2] - F[1]) / (h[2] - h[1])
g[n] = (F[n] - F[n-1]) / (h[end] - h[end-1])
if n > 2
h = h[3:n] - h[1:n-2]
g[2:n-1] = (F[3:n] - F[1:n-2]) ./ h
end
end
g
end

function diagind(m::Integer, n::Integer, k::Integer=0)
if !(-m <= k <= n)
throw(ArgumentError(string("requested diagonal, $k, must be at least $(-m) and ",
Expand Down
25 changes: 0 additions & 25 deletions base/linalg/generic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -271,33 +271,8 @@ function diff(A::AbstractMatrix, dim::Integer=1)
end
end


gradient(F::AbstractVector) = gradient(F, [1:length(F);])

"""
gradient(F::AbstractVector, [h::Real])
Compute differences along vector `F`, using `h` as the spacing between points. The default
spacing is one.
# Examples
```jldoctest
julia> a = [2,4,6,8];
julia> gradient(a)
4-element Array{Float64,1}:
2.0
2.0
2.0
2.0
```
"""
gradient(F::AbstractVector, h::Real) = gradient(F, [h*(1:length(F));])

diag(A::AbstractVector) = throw(ArgumentError("use diagm instead of diag to construct a diagonal matrix"))

#diagm(v::AbstractVecOrMat{T}) where {T}

###########################################################################################
# Inner products and norms

Expand Down
1 change: 0 additions & 1 deletion base/linalg/linalg.jl
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@ export
eye,
factorize,
givens,
gradient,
hessfact,
hessfact!,
isdiag,
Expand Down
1 change: 0 additions & 1 deletion doc/src/stdlib/arrays.md
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,6 @@ Base.cumsum!
Base.cumsum_kbn
Base.crc32c
Base.LinAlg.diff
Base.LinAlg.gradient
Base.repeat(::AbstractArray)
Base.rot180
Base.rotl90
Expand Down
4 changes: 0 additions & 4 deletions test/bitarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1401,10 +1401,6 @@ timesofar("cat")
@check_bit_operation svd(b1)
@check_bit_operation qr(b1)

b1 = bitrand(v1)
@check_bit_operation gradient(b1)
@check_bit_operation gradient(b1, 1.0)

b1 = bitrand(v1)
@check_bit_operation diagm(b1) BitMatrix

Expand Down
14 changes: 0 additions & 14 deletions test/linalg/dense.jl
Original file line number Diff line number Diff line change
Expand Up @@ -156,20 +156,6 @@ end # for eltya
end
end

@testset "Test gradient for $elty" for elty in (Int32, Int64, Float32, Float64, Complex64, Complex128)
if elty <: Real
x = convert(Vector{elty}, [1:3;])
g = ones(elty, 3)
else
x = convert(Vector{elty}, complex.([1:3;], [1:3;]))
g = convert(Vector{elty}, complex.(ones(3), ones(3)))
end
xsub = view(x, 1:size(x, 1))
@test gradient(x) g
@test gradient(xsub) g # Test gradient on SubArray
@test gradient(ones(elty,1)) == zeros(elty,1)
end

@testset "Tests norms" begin
nnorm = 10
mmat = 10
Expand Down

0 comments on commit 761dee6

Please sign in to comment.