Skip to content

Commit

Permalink
diagind(A,k): empty range for k == -size(A,1) and k == size(A,2).
Browse files Browse the repository at this point in the history
  • Loading branch information
StefanKarpinski committed Jun 30, 2014
1 parent 03294f8 commit 7dd97fa
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 16 deletions.
8 changes: 2 additions & 6 deletions base/linalg/dense.jl
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,8 @@ function gradient(F::Vector, h::Vector)
end

function diagind(m::Integer, n::Integer, k::Integer=0)
if 0 < k < n
return range(k*m+1, m+1, min(m, n-k))
elseif 0 <= -k < m
return range(1-k, m+1, min(m+k,n))
end
throw(BoundsError())
-m <= k <= n || throw(BoundsError())
k <= 0 ? range(1-k, m+1, min(m+k, n)) : range(k*m+1, m+1, min(m, n-k))
end

diagind(A::AbstractMatrix, k::Integer=0) = diagind(size(A,1), size(A,2), k)
Expand Down
39 changes: 29 additions & 10 deletions test/linalg3.jl
Original file line number Diff line number Diff line change
Expand Up @@ -202,13 +202,32 @@ Ai = int(ceil(Ar*100))
# issue #6450
@test dot({1.0,2.0},{3.5,4.5}) === 12.5

# Issue 7181
A = reshape([1:9], (3, 3))
@test_throws BoundsError diag(A, 3)
@test [7] == diag(A, 2)
@test [4, 8] == diag(A, 1)
@test [1,5,9] == diag(A, 0)
@test [2,6] == diag(A, -1)
@test [3] == diag(A, -2)
@test_throws BoundsError diag(A, -3)

# issue #7181
A = [ 1 5 9
2 6 10
3 7 11
4 8 12 ]
@test_throws BoundsError diag(A, -5)
@test diag(A,-4) == []
@test diag(A,-3) == [4]
@test diag(A,-2) == [3,8]
@test diag(A,-1) == [2,7,12]
@test diag(A, 0) == [1,6,11]
@test diag(A, 1) == [5,10]
@test diag(A, 2) == [9]
@test diag(A, 3) == []
@test_throws BoundsError diag(A, 4)

@test diag(zeros(0,0)) == []
@test_throws BoundsError diag(zeros(0,0),1)
@test_throws BoundsError diag(zeros(0,0),-1)

@test diag(zeros(1,0)) == []
@test diag(zeros(1,0),-1) == []
@test_throws BoundsError diag(zeros(1,0),1)
@test_throws BoundsError diag(zeros(1,0),-2)

@test diag(zeros(0,1)) == []
@test diag(zeros(0,1),1) == []
@test_throws BoundsError diag(zeros(0,1),-1)
@test_throws BoundsError diag(zeros(0,1),2)

0 comments on commit 7dd97fa

Please sign in to comment.