-
-
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
SparseMatrixCSC() allows creating invalid matrices #14489
Comments
I actually need to be able to do this at times. I think |
Why is this an invalid matrix? The OP constructs a valid 4x4 sparse matrix of zeros. I think the error is that we don't display an empty matrix correctly. |
@jiahao the column pointers should never be empty (in a valid sparse matrix). |
And why not? How else can one represent a zero matrix with no stored zeros? Edit: The original papers on the Harwell-Boeing format do not mention whether or not an empty matrix is allowed. Matlab, the de facto reference implementation, allows an empty sparse matrix to be created with |
Oh, I see the problem now. The column pointers should be a vector of all zeros or something like that, terminated by |
Looking at the documentation of the Yale Sparse Matrix Package here (upon which CSC and CSR are based), |
Sorry for the noise; getting rusty. @tkelman what's your use case for allowing invalid matrices to be constructed? |
Constructing them incrementally, or one of the constituent vectors at a time. Remember the Julia column pointers are 1-based. |
If |
Originally, SparseMatrixCSC was designed to just be an internal constructor, but it is quite popular, and I suspect we want to have a check. Perhaps do it by default, but disable it internally within the sparse codes? |
I consider using the |
The problem is that most types can be constructed directly via their constructor, except for |
You can construct a |
Yes, but we have this naming issue in many places - arrays, rngs, etc. The real issue, I think, is that SparseMatrixCSC is an exported function, and perhaps for that reason should have the default be the safe version. |
It's an exported type (doesn't have to be though), and only really documented in terms of its fields. The problem with "safe" is how much checking are you proposing? Verifying the sortedness of the row indices in each column? That's really expensive to do on every single call to the constructor, you'd absolutely need an escape hatch way of disabling that level of checking, and to use it everywhere the constructor gets called in base if you don't want to slow down all sparse code. I don't think protecting users from the possibility of creating invalid matrices is worth the trouble here. |
Maybe a |
Having a generic 'check that this data structure complies with the applicable standards' function in Base could be useful, whether or not it is called by constructors. Personally, I would prefer the constructor not be checked, and let users call the checker function if they want to. Sometimes creating an initially invalid data structure is ok (for example, pre-allocating the structure of the matrix) because it will be made valid later. |
It's difficult to find the right balance between protecting the user from potential segfaults and have minimal overhead when the user knows what he is doing. There are quite a few However, I'm wondering if a check in the |
@andreasnoack Make |
There is related problem with julia> sparsevec([1,2,3], [1,1,2],2)
ERROR: ArgumentError: An index is out of bound.
[inlined code] from essentials.jl:61
in sparsevec at sparse/sparsevector.jl:145
in sparsevec at sparse/sparsevector.jl:153
julia> x = SparseVector(2, [1,2,3], [1,1,2])
Sparse vector of length 2, with 3 Int64 nonzero entries:
[1] = 1
[2] = 1
[3] = 2 As one more data point, I also found myself wanting to work with temporarily invalid sparse matrices, so I agree with the above statement that the behavior should be allowed (it is a mutable type at any rate, so there is room to shoot yourself in the foot after constructing things), but agree that the non-safe constructor method should not be exported. |
…ing SparseVector) For indices that are non positive or exceed the size of the vector, we should throw an error.
Consensus appears to have formed that: (1) being able to construct invalid Consensus has yet to form around: (3) whether Discussion in #16371, particularly the use case Tangentially, I like
|
I think #22529 implements all the bits we need. This issue by itself can be closed, since we do need the ability to create invalid structures and then fill them up to eventually be valid (which is how the internal implementations often work). Perhaps we can have an optional validity checking parameter, that can over time become default. |
Looks like the constructor should throw an error when trying to create invalid
SparseMatrixCSC
objects:The text was updated successfully, but these errors were encountered: