Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue #27957: diff(ones(6),dims=1) gives error #28896

Closed
Closed
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
18 changes: 18 additions & 0 deletions base/multidimensional.jl
Original file line number Diff line number Diff line change
Expand Up @@ -657,10 +657,18 @@ function diff(a::AbstractVector)
[ a[i+1] - a[i] for i=1:length(a)-1 ]
end

function diff(a::AbstractArray)
@assert !has_offset_axes(a)
[ a[i+1] - a[i] for i=1:length(a)-1 ]
end

"""
diff(A::AbstractVector)
diff(A::AbstractMatrix; dims::Integer)

diff(A::AbstractArray)
diff(A::AbstractArray; dims::Integer)

Finite difference operator of matrix or vector `A`. If `A` is a matrix,
specify the dimension over which to operate with the `dims` keyword argument.

Expand Down Expand Up @@ -693,6 +701,16 @@ function diff(A::AbstractMatrix; dims::Integer)
end
end

function diff(A::AbstractArray; dims::Integer)
if dims == 1
[A[i+1,j] - A[i,j] for i=1:size(A,1)-1, j=1:size(A,2)]
elseif dims == 2
[A[i,j+1] - A[i,j] for i=1:size(A,1), j=1:size(A,2)-1]
else
throw(ArgumentError("dimension must be 1 or 2, got $dims"))
end
end

### from abstractarray.jl

# In the common case where we have two views into the same parent, aliasing checks
Expand Down