Skip to content

Commit

Permalink
Merge pull request #10033 from JuliaLang/jcb/diffdimerr
Browse files Browse the repository at this point in the history
throw error on invalid dimension argument for diff
  • Loading branch information
andreasnoack committed Feb 3, 2015
2 parents aabd23b + f4de7c5 commit e8355d4
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
4 changes: 3 additions & 1 deletion base/linalg/generic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,10 @@ diff(a::AbstractVector) = [ a[i+1] - a[i] for i=1:length(a)-1 ]
function diff(A::AbstractMatrix, dim::Integer)
if dim == 1
[A[i+1,j] - A[i,j] for i=1:size(A,1)-1, j=1:size(A,2)]
else
elseif dim == 2
[A[i,j+1] - A[i,j] for i=1:size(A,1), j=1:size(A,2)-1]
else
throw(ArgumentError("dimension dim must be 1 or 2, got $dim"))
end
end

Expand Down
9 changes: 9 additions & 0 deletions test/linalg1.jl
Original file line number Diff line number Diff line change
Expand Up @@ -302,3 +302,12 @@ end # for eltya
#6941
#@test (ones(10^7,4)*ones(4))[3] == 4.0

# test diff, throw ArgumentError for invalid dimension argument
let X = [3 9 5;
7 4 2;
2 1 10]
@test diff(X,1) == [4 -5 -3; -5 -3 8]
@test diff(X,2) == [6 -4; -3 -2; -1 9]
@test_throws ArgumentError diff(X,3)
@test_throws ArgumentError diff(X,-1)
end

0 comments on commit e8355d4

Please sign in to comment.