-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
Diagonal broadcast results in SparseMatrixCSC #24317
Comments
@Sacha0 seems dually-qualified to explain/fix/???? this :) |
The sparse matrix can cope with a broadcast resulting in a dense result, but can also exploit the common situation of the broadcast giving a sparse (in particular: diagonal) result. |
example, please? |
julia> Diagonal(1:4) .+ Diagonal(1:4)
4×4 SparseMatrixCSC{Int64,Int64} with 4 stored entries:
[1, 1] = 2
[2, 2] = 4
[3, 3] = 6
[4, 4] = 8
julia> Diagonal(1:4) .* Bidiagonal(1:4, 1:3, :U)
4×4 SparseMatrixCSC{Int64,Int64} with 4 stored entries:
[1, 1] = 1
[2, 2] = 4
[3, 3] = 9
[4, 4] = 16
julia> Diagonal(1:4) .+ sprand(4, 4, 0.2)
4×4 SparseMatrixCSC{Float64,Int64} with 6 stored entries:
[1, 1] = 1.0
[3, 1] = 0.110989
[2, 2] = 2.88061
[3, 3] = 3.0
[3, 4] = 0.0425649
[4, 4] = 4.0 For the design discussion in all its detail, please see #18590 and related threads. Where the result happens to be dense in sparse form, we may yet avoid storing numerical zeros. But otherwise this behavior is expected :). Best! |
To give some examples, here's what I might have naively expected before reading #18590:
but after reading #18590 (comment) I no longer believe the effort it would take to make broadcasting on special matrices always return the "optimal" type is worth it. With this, I believe there are two questions that remain:
|
Sounds great! :)
Do you mean specifically to the results of operations that we cannot presently determine are zero-preserving? If so, yes, chances are we should do that implicitly in the future; a couple existing issues discuss this possibility. (Operations that we can presently determine are zero-preserving do this implicitly already.) Best! |
I really think this path is a bad one. If our only justification (from #18590) for breaking with user expectations in our API design is "We didn't feel like writing some trivial methods", that suggests that the API design is wrong. |
I'm going to close this. It will be great if a sentence is added to the Broadcasting section of the manual explaining the reasoning here, but this ticket currently serves as a poor reminder to do so. |
I was surprised to notice that broadcast on a
Diagonal
matrix results in a sparse matrix, not a dense one:In particular, the
0
in the above example is a "stored entry", so even in the case where there are many zeros in the result, it will still take more memory than a dense matrix.The text was updated successfully, but these errors were encountered: