-
-
Notifications
You must be signed in to change notification settings - Fork 5.4k
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
fix off-by-one in findn and findnz #5386
Conversation
``count`` was equal to the number of nonzero elements plus one, so the output vectors had an extra element with junk.
Yikes. Maybe a test for this would be good so that it can't happen in the future? |
Sure, I'll add a test. |
Done. |
That test passes for me without this patch. I'm not sure how to make a sparse matrix with stored zeros at this point. |
fix off-by-one in findn and findnz
Thanks! |
Oops, I had run the test on 0.2 where it failed. I fixed it now (rebased). Note that
I'm not sure the warning is really needed. |
@@ -182,3 +182,7 @@ mfe22 = eye(Float64, 2) | |||
|
|||
# issue #5169 | |||
@test nnz(sparse([1,1],[1,2],[0.0,-0.0])) == 0 | |||
|
|||
# issue #5386 | |||
I,J,V = findnz(SparseMatrixCSC(2,1,[1,3],[1,2],[1.0,0.0])) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Although this constructor is available, one should really not be creating sparse matrices with stored zeros. A lot of routines assume no stored zeros. Maybe we should not export SparseMatrixCSC?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Creating a sparse matrix with zeros should obviously be discouraged but I
don't think it should be an error. One should just have to pay for the cost
of the extra operations.
In JuMP the sparse constraint matrix follows the structure of the model,
and it's possible for some coefficients to be zero in some instances.
In test/sparse.jl:
@@ -182,3 +182,7 @@ mfe22 = eye(Float64, 2)
issue #5169
@test nnz(sparse([1,1],[1,2],[0.0,-0.0])) == 0
+
+# issue #5386
+I,J,V = findnz(SparseMatrixCSC(2,1,[1,3],[1,2],[1.0,0.0]))
Although this constructor is available, one should really not be creating
sparse matrices with stored zeros. A lot of routines assume no stored
zeros. Maybe we should not export SparseMatrixCSC?
—
Reply to this email directly or view it on
GitHubhttps://github.com//pull/5386/files#r8894990
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A lot of routines assume no stored zeros.
Do they run slower with stored zeros, or do they actually crash and burn? If the former, then I would agree with @mlubin; otherwise the routines for which stored zeros would be fatal should just trap any divide-by-zero errors and the like as they happen.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
AFAICT the only way to make a sparse matrix with stored zeros is to call SparseMatrixCSC
directly, so if nobody did that there would be no problem. @mlubin do you call it directly for performance reasons?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In JuMP we translate the constraint matrix into a SparseMatrixCSC
in a single pass. Yes, we could filter out zeros here.
But I think the point is, sparse matrix formats are definitely not one-size-fits-all, so trying to be strict about having no non-zeros is just going to make some applications more difficult. For example, a common trick is to leave "gaps" between columns if it's possible that we may need to occasionally insert new elements. This could be implemented with a SparseMatrixCSC
if zeros are accepted. If not, one would need to use a custom data structure and would instantly lose all of the functionality in Base that might be useful, such as sparse mat-vec products.
What's an example of a routine that crashes if explicit nonzeros are stored?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Once you start doing linear algebra, you always have to check for small values and cancellations. An algorithm that fails with zero entries is likely also going to fail with 1e-50
entries.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would keeping track of the number of filled entries and how many of those are zero be an option? You'd have to check each inserted value to see if it is zero, but I feel like that might be a trivial cost in the face of the rest of the operation.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure the extra complexity is worthwhile. When would one actually need the number of nonzero entries instead of the number of filled entries?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The dense nnz
actually counts the number of nonzeros.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Keeping track of the number of filled nonzeros is going to be expensive, and not practical. We already do not have good performance on sparse operations, and it would be good not to slow down further.
count
was equal to the number of nonzero elements plus one, so the output vectors had an extra element with junk.