-
-
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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:
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.
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.