Skip to content

Commit

Permalink
fix off by one in csc_premute and add test
Browse files Browse the repository at this point in the history
  • Loading branch information
KristofferC committed Aug 21, 2015
1 parent 50cb6be commit 9a7e8ff
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
2 changes: 1 addition & 1 deletion base/sparse/csparse.jl
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ function csc_permute{Tv,Ti}(A::SparseMatrixCSC{Tv,Ti}, pinv::Vector{Ti}, q::Vect
end
if !isperm(pinv) || !isperm(q) error("both pinv and q must be permutations") end
C = copy(A); Cp = C.colptr; Ci = C.rowval; Cx = C.nzval
nz = zero(Ti)
nz = one(Ti)
for k in 1:n
Cp[k] = nz
j = q[k]
Expand Down
18 changes: 12 additions & 6 deletions test/sparsedir/sparse.jl
Original file line number Diff line number Diff line change
Expand Up @@ -909,7 +909,6 @@ A = sparse(ones(5,5))
@test_throws DimensionMismatch one(sprand(5,6,0.2))

#istriu/istril

A = sparse(triu(rand(5,5)))
@test istriu(A)
@test !istriu(sparse(ones(5,5)))
Expand All @@ -918,29 +917,36 @@ A = sparse(tril(rand(5,5)))
@test !istril(sparse(ones(5,5)))

# symperm

srand(1234321)
A = triu(sprand(10,10,0.2)) # symperm operates on upper triangle
perm = randperm(10)
@test symperm(A,perm).colptr == [1,2,3,3,3,4,5,5,7,9,10]

# droptol
#csc_permute
A = sprand(10,10,0.2)
pinv = randperm(10)
p = zeros(Int, 10)
# Invert pinv
for (i, j) in enumerate(pinv)
p[j] = i
end
q = randperm(10)
@test csc_permute(A, pinv, q) == full(A)[p, q]

# droptol
@test Base.droptol!(A,0.01).colptr == [1,1,1,2,2,3,4,6,6,7,9]
@test Base.droptol(A,0.01).colptr == [1,1,1,2,2,3,4,6,6,7,9]

#trace

@test_throws DimensionMismatch trace(sparse(ones(5,6)))
@test trace(speye(5)) == 5

#diagm on a matrix

@test_throws DimensionMismatch diagm(sparse(ones(5,2)))
@test_throws DimensionMismatch diagm(sparse(ones(2,5)))
@test diagm(sparse(ones(1,5))) == speye(5)

# triu/tril

A = sprand(5,5,0.2)
AF = full(A)
@test full(triu(A,1)) == triu(AF,1)
Expand Down

0 comments on commit 9a7e8ff

Please sign in to comment.