-
-
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
Division of a sparse matrix by row vector causes 0's to become NaN #23857
Comments
minimal more concise cases to demonstrate the same effect:
In the first case, the result of
This shows, that the wrong behaviour does not happen, it the zero is not structural. |
Another bug in the same realm, maybe even more severe, as this example shows:
The expression |
Yet another surprise / bug:
Wonder why it works for |
Thanks for the additional examples! Sparse broadcast over combinations including dense objects indeed needs a good bit of attention after 1.0 :). Best! |
I just created a PR for the original issue: #24806 |
Fixed by #24806. |
There were two parts to this bug: The NaNs and the creation of, i believe the term is, 'non-structural zeros' (i.e. zeros stored in the array rather than being implied by the structure of the object). Based on a quick test of the lastest nightly, only the NaNs are fixed. But the NaNs, at least in some instances, have been replaced by these non-structural zeros. As a user, I would not expect broadcast operations to add structural zeros when unnecessary. It might be reasonable to expect them to remove generated zeros, but I understand that can get iffy with floating point comparisons to zero. That said, it looks like non-structural zeros are being pruned in at least some circumstances. So that's nice, but the system isn't perfect. Here's an example set of tests: using SparseArrays
using Test
@test size(sparse([1.0; 0.0]).nzval, 1) == 1
@test_broken size((sparse([1.0; 0.0]) ./ [1.0]).nzval, 1) == 1 # is 2, should be 1
@test size((sparse([1.0; 0.0]) .* [1.0]).nzval, 1) == 1 # expected
@test size((sparse([1.0; 0.0]) .+ [0.0]).nzval, 1) == 1 # expected
@test size((sparse([2.0; 1.0]) .- [1.0]).nzval, 1) == 1 # nice, pruning! |
I believe that is fairly well covered by #26561. |
indeed it is! Sorry, I missed that issue. |
I think the title about says it all. This example creates a sparse CSC matrix with 7 stored entries, but then the division creates 12 stored entries, with the 5 extra being NaN. None of the column means are 0 or near 0, so this should not be happening.
division by a column vector doesn't create NaNs, but it does cause explicit 0s to be stored:
scalar division works as expected
(even if this was correct behavior, does it make sense to allow the result of an operation on a sparse matrix to return a dense matrix if the storage requirements will be lower or close to the same?)
The text was updated successfully, but these errors were encountered: