-
-
Notifications
You must be signed in to change notification settings - Fork 14
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
diag(sparse matrix) should be a sparse vector? #411
Comments
As usual with sparse matrices, it depends on the sparsity pattern. Sparse matrices from physical simulations would typically have a banded structure such that the diagonal is dense. For sparse matrix representations of graphs, it is probably less likely. We could do a survey based on Tim Davis' sparse matrix collection. |
Putting dense data into a |
I guess that argument applies to all arrays, i.e. even dense arrays. The safe choice that minimizes the worst-case relative memory waste for a dense matrix is to always produce a sparse vector. If the vector happens to have no zeros, there is a 2x waste. Returning a |
I think dense is a reasonable choice here. My impression is that the sparse world generally considers O(n) size small enough. |
Given that our matrices are CSC, we're already storing a dense list of size O(n). The story might be different with a COO format. |
A SparseVector result would not be O(n). |
off-diagonals using the 2-arg form aren't usually expected to be dense the way the main diagonal will more commonly be. so there's a pretty good argument for returning a sparse vector from |
julia> diag(A)
5-element Array{Float64,1}:
0.774844
0.0
0.443253
0.0
0.0
julia> diag(A,0)
5-element SparseVector{Float64,Int64} with 2 stored entries:
[1] = 0.774844
[3] = 0.44325 I got hit by this inconsistency in JuliaLang/julia#22925 |
Would be great if a decision could be made here about which direction to go for consistency and make the different forms of |
The current situation is not completely unreasonable, though, since the diagonal will often be quite dense whereas many off-diagonals will be very sparse but I can also see that it would be nice if the two methods returned the same type. Following up on @martinholters comment, I'd argue that the safest choice would be the one with the smallest absolute worst case memory usage. It is not the only concern or necessarily the main concern but I'd also argue that Bottomline is that we disagree. Specifying a return type seems like a general problem. Maybe a solution could be to define |
Possible alternative: make |
diagonal views would be a good idea, but indexing into that would be more expensive than indexing into an eager sparse or dense diagonal slice (copy) |
|
@tkelman makes the argument that if we go with always sparse:
When someone writes Resolved: always return a sparse vector here. |
Might be worth browsing through the thumbnails at https://www.cise.ufl.edu/research/sparse/matrices/list_by_id.html to get an idea of typical density patterns of diagonals of typical sparse matrices. |
Yes, it's definitely true that the diagonal is often dense. Of course, if O(n) storage is ok for your data (as opposed to O(n^2)) then 2n is still ok, and that's the worst case for a dense diagonal. I would add that in the applications where I've used sparse matrices a lot, the diagonal is almost never dense (they're also usually not square, which may be related). |
Looks like so might need to generalize the SpDiagIterator here https://github.com/JuliaLang/julia/blob/8c7f01e974de7ae779f22343086fdd400efe16f2/base/sparse/sparsematrix.jl#L3385-L3402 to handle off-diagonals. Haven't looked at it very closely but I think that shouldn't be too complicated? |
Just tested this and deleting https://github.com/JuliaLang/julia/blob/8c7f01e974de7ae779f22343086fdd400efe16f2/base/sparse/sparsematrix.jl#L3415 seems to be all that is required. |
That would fix the return type, but the fallback is probably less efficient? Worth comparing. |
Currently, when you take the diagonal of a sparse matrix, you get a dense vector:
This should arguably be a sparse vector since for a generic sparse matrix, the diagonal is sparse.
The text was updated successfully, but these errors were encountered: