-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Should hcat of dense and sparse matrix be sparse? #13130
Comments
Definitely not a bug, in some cases it's the reasonable thing to do. It really depends on the:
None of the three above are properties of the types themselves, so to do something intelligent (if that's possible at all) you would lose type stability. I vote for the simple and predictable behavior: sparse + dense = dense. |
Matlab and Octave return sparse in this case. Would have to check scipy. edit: Julia's currently going to a generic fallback here: julia> @which hcat(ones(5), speye(5,5))
hcat{T}(A::Union{AbstractArray{T,1},AbstractArray{T,2}}...) at abstractarray.jl:742 |
While there are different things for different situations, I think the default should be to preserve sparsity in cases of concatenation. |
I tend to agree with Viral. If you're using sparse matrices then you On Mon, Sep 14, 2015 at 11:00 PM, Viral B. Shah [email protected]
|
This requires adding a new definition that calls |
Out of curiosity (I would love to do it myself, but I'm not sure how it would work), how would you do that with varargs? I see something like |
|
Well, of course :) I'll have a go at it. |
Well, I guess I do not totally understand your suggestion @ViralBShah . If I add a definition of hcat and/or vcat as |
I don't know if we can currently express that signature in the fully general case. If Varargs were allowed at any part of a signature and covariant then you could maybe do something like For now you could likely just list a few permutations up to a certain number of inputs? There does get to be a point where if you're concatenating N-1 dense matrices with 1 sparse matrix you may not want to promote to sparse any more, but it depends on your data sizes and there's a bit of a type stability issue. |
I thought |
You're right @ViralBShah , I must have made some sort of mistake. I had an attempt, but that seemed to cause all my hcats to be sparse. I must have a bug. The following works fine, though. function attempt(args::Int64...)
for arg in args
println(arg)
end
end
function attempt(args::Float64...)
for arg in args
println(arg)
end
end
function attempt(args::Union{Int64, Float64}...)
for arg in args
println(Float64(arg))
end
end
attempt(3)
attempt(3,4)
attempt(3.)
attempt(3.,4.)
attempt(3, 4.)
attempt(3., 4) |
Fix #13130 - concatenation of dense and sparse should be sparse.
I wanted to hcat a dense vector B and a sparse matrix A. I expected
To return a sparse matrix C, but instead A is converted to it's dense representation, and this means C is obviously also dense. I then did
But as @tavert pointed out over at reddit, it is better to.
My question is, it is intended that users should do the latter, or is the current behaviour a bug or at least unintended?
The text was updated successfully, but these errors were encountered: