-
-
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
RFC: AbstractSparseMatrixCSC interface #33054
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,62 @@ | ||||||||||||
AbstractSparseMatrixCSC{Tv,Ti<:Integer} <: AbstractSparseMatrix{Tv,Ti} | ||||||||||||
|
||||||||||||
Supertype for matrix with compressed sparse column (CSC). | ||||||||||||
|
||||||||||||
!!! compat "Julia 1.4" | ||||||||||||
`AbstractSparseMatrixCSC` requires at least Julia 1.4. | ||||||||||||
|
||||||||||||
## `AbstractSparseMatrixCSC` interface | ||||||||||||
|
||||||||||||
In addition to the [Abstract array interface](@ref man-interface-array), every | ||||||||||||
`AbstractSparseMatrixCSC` subtype `TS` must provide the following methods: | ||||||||||||
|
||||||||||||
* [`size(::TS)`](@ref size) | ||||||||||||
* [`coloffsets(::TS) :: AbstractVector{<:Ti}`](@ref coloffsets) | ||||||||||||
* [`rowvals(::TS) :: AbstractVector{<:Ti}`](@ref rowvals) | ||||||||||||
* [`nonzeros(::TS) :: AbstractVector{<:Tv}`](@ref nonzeros) | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In #25118 @simonbyrne suggested Unfortunately, There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As a reference, #22907 is the PR where There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So it would be breaking for functions like There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||||||||||||
|
||||||||||||
Other sparse matrix methods such as [`nzrange`](@ref) and [`nnz`](@ref) are automatically | ||||||||||||
defined in terms of above functions. | ||||||||||||
|
||||||||||||
## Assumed invariance | ||||||||||||
|
||||||||||||
To use algorithms defined in SparseArrays, a matrix `A` of type `AbstractSparseMatrixCSC` | ||||||||||||
must satisfy the following constraints. | ||||||||||||
|
||||||||||||
### Matrix `A` and all vectors constituting `A` have one-based indexing | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think one-based indexing is necessary, in principle. But I think most of the code in SparseArrays.jl requires it so I think it's better to specify it. |
||||||||||||
|
||||||||||||
```julia | ||||||||||||
@assert !has_offset_axes(A) | ||||||||||||
@assert !has_offset_axes(coloffsets(A)) | ||||||||||||
@assert !has_offset_axes(rowvals(A)) | ||||||||||||
@assert !has_offset_axes(nonzeros(A)) | ||||||||||||
``` | ||||||||||||
|
||||||||||||
### Row indices in `rowval(A)` for each column are sorted | ||||||||||||
|
||||||||||||
```julia | ||||||||||||
for col in axes(A, 2) | ||||||||||||
@assert issorted(rowval(A)[nzrange(A, col)]) | ||||||||||||
end | ||||||||||||
``` | ||||||||||||
|
||||||||||||
### Column pointers in `coloffsets(A)` are increasing, started at 1 | ||||||||||||
|
||||||||||||
```julia | ||||||||||||
@assert coloffsets(A)[1] === 1 | ||||||||||||
@assert all(diff(coloffsets(A)) .>= 0) | ||||||||||||
``` | ||||||||||||
|
||||||||||||
### Row index vector `rowvals(A)` and non-zero value vector `nonzeros(A)` are long enough | ||||||||||||
|
||||||||||||
```julia | ||||||||||||
@assert nnz(A) <= length(rowvals(A)) | ||||||||||||
@assert nnz(A) <= length(nonzeros(A)) | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The comment (below) says
|
||||||||||||
``` | ||||||||||||
|
||||||||||||
### Indices for rows, `rowvals(A)` and `nonzeros(A)` are representable by `Ti` | ||||||||||||
|
||||||||||||
```julia | ||||||||||||
@assert size(A, 1) <= typemax(Ti) | ||||||||||||
@assert nnz(A) <= typemax(Ti) | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So this is something I don't understand in #31724. It was mentioned that both Lines 88 to 90 in fb9cd34
I understand that Also regarding the current comment: julia/stdlib/SparseArrays/src/sparsematrix.jl Lines 8 to 9 in 19a2b2e
[1] But technically |
||||||||||||
``` |
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 #25118 @simonbyrne suggested
rowindices
. It is a better name butrowvals
is already a documented public API so I don't know if we want to change this at this point. But this may be a good opportunity to do so?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.
Again agree. But documented APIs should not be changed. We can do this for 2.0, or whenever we can version stdlib / or move sparse out of stdlib.
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 principle, I think we can define
rowvals
to be an alias torowindices
like this:and then use
rowindices
internally everywhere.In the documentation, we can mention that the use of
rowvals
is highly discouraged and will be deprecated in the next major release.This means that for end-users
rowvals
keep working as-is but for package authors of custom array types they are forced to userowindices
from the get-go.