Help with Array dispatch (what methods do I need (or don't I need) ?

106 views
Skip to first unread message

Patrick Kofod Mogensen

unread,
Jun 2, 2016, 10:57:09 AM6/2/16
to julia-dev
I'm trying to fix https://github.com/JuliaLang/julia/issues/16661 but I have a dispatch problem.

Basically, I am trying to add functionality such that sparse vector and sparse matrix concatenation returns a sparse matrix, and I thought I would make sparse matrix and vector + sparse vector and vector return sparse as well while I was at it.

The sparse / dense, sparse / sparse combinations all work, but! For some reason concatenation on an Array of SparseVector s return a SparseMatrixCSC not a SparseVector. I don't really understand why. It is the following test that doesn't work


let m
= 80, n = 100
    A
= Array{SparseVector{Float64,Int}}(n)
    tnnz
= 0
   
for i = 1:length(A)
        A
[i] = sprand(m, 0.3)
        tnnz
+= nnz(A[i])
   
end

    H
= hcat(A...)
   
@test isa(H, SparseMatrixCSC{Float64,Int})
   
@test size(H) == (m, n)
   
@test nnz(H) == tnnz
   
Hr = zeros(m, n)
   
for j = 1:n
       
Hr[:,j] = full(A[j])
   
end
   
@test full(H) == Hr

    V
= vcat(A...)
   
@test isa(V, SparseVector{Float64,Int})
   
@test length(V) == m * n
   
Vr = vec(Hr)
   
@test full(V) == Vr
end


I am happy to explain anything that is not clear. Thanks

Patrick Kofod Mogensen

unread,
Jun 2, 2016, 11:44:30 AM6/2/16
to julia-dev
Oops, forgot the gist link. Will post when I'm at a computer.

Patrick Kofod Mogensen

unread,
Jun 2, 2016, 11:45:05 AM6/2/16
to julia-dev

Patrick Kofod Mogensen

unread,
Jun 2, 2016, 11:52:49 AM6/2/16
to julia-dev
Here are the changes I made in copy+pastable form. https://gist.github.com/pkofod/5536ae52c23db5f34ba35cedbf0b74e9

Patrick Kofod Mogensen

unread,
Jun 2, 2016, 12:09:00 PM6/2/16
to julia-dev
I think I figured it out. The hcat for sparse vector/matrix was more specific than the sparse vector only *cats, so it would convert them to SparseMatrixCSC in the method I defined. Very sorry for the noise. (btw, I know there are many superfluous allocations, I was just figured out which method definitions were needed).
Reply all
Reply to author
Forward
0 new messages