Skip to content

Commit 9624f10

Browse files
mbaumanfredrikekre
authored andcommitted
Deprecate diff(::AbstractMatrix), require a dim argument (#25457)
* Deprecate diff(::AbstractMatrix), require a dim argument Akin to `cumsum`, `diff` now requires a `dim` argument in cases where the array has more than one dimension. Final piece of #20041 for the 1.0 milestone. * Add reference to this PR in NEWS.md
1 parent 19cd026 commit 9624f10

File tree

5 files changed

+18
-7
lines changed

5 files changed

+18
-7
lines changed

NEWS.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -856,8 +856,9 @@ Deprecated or removed
856856
* `workspace` is discontinued, check out [Revise.jl](https://github.com/timholy/Revise.jl)
857857
for an alternative workflow ([#25046]).
858858

859-
* `cumsum`, `cumprod`, `accumulate`, and their mutating versions now require a `dim`
860-
argument instead of defaulting to using the first dimension ([#24684]).
859+
* `cumsum`, `cumprod`, `accumulate`, their mutating versions, and `diff` all now require a `dim`
860+
argument instead of defaulting to using the first dimension unless there is only
861+
one dimension ([#24684], [#25457]).
861862

862863
* The `sum_kbn` and `cumsum_kbn` functions have been moved to the
863864
[KahanSummation](https://github.com/JuliaMath/KahanSummation.jl) package ([#24869]).

base/deprecated.jl

+2
Original file line numberDiff line numberDiff line change
@@ -1382,6 +1382,8 @@ end
13821382

13831383
@deprecate cumsum(A::AbstractArray) cumsum(A, 1)
13841384
@deprecate cumprod(A::AbstractArray) cumprod(A, 1)
1385+
import .LinAlg: diff
1386+
@deprecate diff(A::AbstractMatrix) diff(A, 1)
13851387

13861388
# issue #16307
13871389
@deprecate finalizer(o, f::Function) finalizer(f, o)

base/linalg/generic.jl

+10-3
Original file line numberDiff line numberDiff line change
@@ -243,10 +243,11 @@ tril!(M::AbstractMatrix) = tril!(M,0)
243243
diff(a::AbstractVector) = [ a[i+1] - a[i] for i=1:length(a)-1 ]
244244

245245
"""
246-
diff(A, [dim::Integer=1])
246+
diff(A::AbstractVector)
247+
diff(A::AbstractMatrix, dim::Integer)
247248
248249
Finite difference operator of matrix or vector `A`. If `A` is a matrix,
249-
compute the finite difference over a dimension `dim` (default `1`).
250+
specify the dimension over which to operate with the `dim` argument.
250251
251252
# Examples
252253
```jldoctest
@@ -259,9 +260,15 @@ julia> diff(a,2)
259260
2×1 Array{Int64,2}:
260261
2
261262
10
263+
264+
julia> diff(vec(a))
265+
3-element Array{Int64,1}:
266+
4
267+
-2
268+
12
262269
```
263270
"""
264-
function diff(A::AbstractMatrix, dim::Integer=1)
271+
function diff(A::AbstractMatrix, dim::Integer)
265272
if dim == 1
266273
[A[i+1,j] - A[i,j] for i=1:size(A,1)-1, j=1:size(A,2)]
267274
elseif dim == 2

test/bitarray.jl

+2-1
Original file line numberDiff line numberDiff line change
@@ -1406,7 +1406,8 @@ timesofar("cat")
14061406
@check_bit_operation diff(b1) Vector{Int}
14071407

14081408
b1 = bitrand(n1, n2)
1409-
@check_bit_operation diff(b1) Matrix{Int}
1409+
@check_bit_operation diff(b1, 1) Matrix{Int}
1410+
@check_bit_operation diff(b1, 2) Matrix{Int}
14101411

14111412
b1 = bitrand(n1, n1)
14121413
@check_bit_operation svd(b1)

test/linalg/lapack.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ end
203203
Bvs = eigvecs(B)
204204
Avs = eigvecs(A)
205205
Bvs = LAPACK.gebak!('S','R',ilo,ihi,scale,Bvs)
206-
@test norm(diff(Avs ./ Bvs)) < 100 * eps(abs(float(one(elty))))
206+
@test norm(diff(Avs ./ Bvs, 1)) < 100 * eps(abs(float(one(elty))))
207207
end
208208
end
209209

0 commit comments

Comments
 (0)