Skip to content

Commit 9a7e8ff

Browse files
committed
fix off by one in csc_premute and add test
1 parent 50cb6be commit 9a7e8ff

File tree

2 files changed

+13
-7
lines changed

2 files changed

+13
-7
lines changed

base/sparse/csparse.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ function csc_permute{Tv,Ti}(A::SparseMatrixCSC{Tv,Ti}, pinv::Vector{Ti}, q::Vect
269269
end
270270
if !isperm(pinv) || !isperm(q) error("both pinv and q must be permutations") end
271271
C = copy(A); Cp = C.colptr; Ci = C.rowval; Cx = C.nzval
272-
nz = zero(Ti)
272+
nz = one(Ti)
273273
for k in 1:n
274274
Cp[k] = nz
275275
j = q[k]

test/sparsedir/sparse.jl

+12-6
Original file line numberDiff line numberDiff line change
@@ -909,7 +909,6 @@ A = sparse(ones(5,5))
909909
@test_throws DimensionMismatch one(sprand(5,6,0.2))
910910

911911
#istriu/istril
912-
913912
A = sparse(triu(rand(5,5)))
914913
@test istriu(A)
915914
@test !istriu(sparse(ones(5,5)))
@@ -918,29 +917,36 @@ A = sparse(tril(rand(5,5)))
918917
@test !istril(sparse(ones(5,5)))
919918

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

927-
# droptol
925+
#csc_permute
926+
A = sprand(10,10,0.2)
927+
pinv = randperm(10)
928+
p = zeros(Int, 10)
929+
# Invert pinv
930+
for (i, j) in enumerate(pinv)
931+
p[j] = i
932+
end
933+
q = randperm(10)
934+
@test csc_permute(A, pinv, q) == full(A)[p, q]
928935

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

931940
#trace
932-
933941
@test_throws DimensionMismatch trace(sparse(ones(5,6)))
934942
@test trace(speye(5)) == 5
935943

936944
#diagm on a matrix
937-
938945
@test_throws DimensionMismatch diagm(sparse(ones(5,2)))
939946
@test_throws DimensionMismatch diagm(sparse(ones(2,5)))
940947
@test diagm(sparse(ones(1,5))) == speye(5)
941948

942949
# triu/tril
943-
944950
A = sprand(5,5,0.2)
945951
AF = full(A)
946952
@test full(triu(A,1)) == triu(AF,1)

0 commit comments

Comments
 (0)