Skip to content
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

Export AbstractSparseMatrixCSC + interface #395

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
2 changes: 2 additions & 0 deletions docs/src/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ section of the standard library reference.
SparseArrays.AbstractSparseArray
SparseArrays.AbstractSparseVector
SparseArrays.AbstractSparseMatrix
SparseArrays.AbstractSparseMatrixCSC
SparseArrays.SparseVector
SparseArrays.SparseMatrixCSC
SparseArrays.sparse
Expand All @@ -229,6 +230,7 @@ SparseArrays.sprandn
SparseArrays.nonzeros
SparseArrays.rowvals
SparseArrays.nzrange
SparseArrays.getcolptr
SparseArrays.droptol!
SparseArrays.dropzeros!
SparseArrays.dropzeros
Expand Down
4 changes: 2 additions & 2 deletions src/SparseArrays.jl
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ import Base: adjoint, argmin, argmax, Array, broadcast, circshift!, complex, Com
using Random: default_rng, AbstractRNG, randsubseq, randsubseq!

export AbstractSparseArray, AbstractSparseMatrix, AbstractSparseVector,
SparseMatrixCSC, SparseVector, blockdiag, droptol!, dropzeros!, dropzeros,
issparse, nonzeros, nzrange, rowvals, sparse, sparsevec, spdiagm,
AbstractSparseMatrixCSC, SparseMatrixCSC, SparseVector, blockdiag, droptol!, dropzeros!, dropzeros,
issparse, nonzeros, nzrange, rowvals, getcolptr, sparse, sparsevec, spdiagm,
sprand, sprandn, spzeros, nnz, permute, findnz, fkeep!, ftranspose!,
sparse_hcat, sparse_vcat, sparse_hvcat

Expand Down
2 changes: 2 additions & 0 deletions src/abstractsparse.jl
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ const AbstractSparseVecOrMat = Union{AbstractSparseVector,AbstractSparseMatrix}
AbstractSparseMatrixCSC{Tv,Ti<:Integer} <: AbstractSparseMatrix{Tv,Ti}

Supertype for matrix with compressed sparse column (CSC).
Subtypes of this type are expected to follow the `AbstractSparseMatrixCSC` interface
consisting of [`nnz`](@ref), [`rowvals`](@ref), [`nzrange`](@ref), [`nonzeros`](@ref) and [`getcolptr`](@ref).
"""
abstract type AbstractSparseMatrixCSC{Tv,Ti<:Integer} <: AbstractSparseMatrix{Tv,Ti} end

Expand Down
36 changes: 31 additions & 5 deletions src/sparsematrix.jl
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,30 @@ const SparseMatrixCSCView{Tv,Ti} =
Tuple{Base.Slice{Base.OneTo{Int}},I}} where {I<:AbstractUnitRange}
const SparseMatrixCSCUnion{Tv,Ti} = Union{AbstractSparseMatrixCSC{Tv,Ti}, SparseMatrixCSCView{Tv,Ti}}

"""
getcolptr(A::AbstractSparseMatrixCSC)

Return a vector of column pointers of `A`. Any modifications to the returned
vector will mutate `A` as well. Providing access to how the column pointers are
stored internally can be useful in conjunction with passing data to factorizations
and preconditioners. See also [`rowvals`](@ref), [`nonzeros`](@ref) and [`nzrange`](@ref).

# Examples
```jldoctest
julia> A = sparse(2I, 3, 3)
3×3 SparseMatrixCSC{Int64, Int64} with 3 stored entries:
2 ⋅ ⋅
⋅ 2 ⋅
⋅ ⋅ 2

julia> getcolptr(A)
4-element Vector{Int64}:
1
2
3
4
```
"""
getcolptr(S::SorF) = getfield(S, :colptr)
getcolptr(S::SparseMatrixCSCView) = view(getcolptr(parent(S)), first(axes(S, 2)):(last(axes(S, 2)) + 1))
getrowval(S::AbstractSparseMatrixCSC) = rowvals(S)
Expand Down Expand Up @@ -226,7 +250,7 @@ Return a vector of the structural nonzero values in sparse array `A`. This
includes zeros that are explicitly stored in the sparse array. The returned
vector points directly to the internal nonzero storage of `A`, and any
modifications to the returned vector will mutate `A` as well. See
[`rowvals`](@ref) and [`nzrange`](@ref).
[`rowvals`](@ref), [`getcolptr`](@ref) and [`nzrange`](@ref).

# Examples
```jldoctest
Expand Down Expand Up @@ -254,7 +278,7 @@ nonzeros(S::LowerTriangular{<:Any,<:SparseMatrixCSCUnion}) = nonzeros(S.data)
Return a vector of the row indices of `A`. Any modifications to the returned
vector will mutate `A` as well. Providing access to how the row indices are
stored internally can be useful in conjunction with iterating over structural
nonzero values. See also [`nonzeros`](@ref) and [`nzrange`](@ref).
nonzero values. See also [`getcolptr`](@ref), [`nonzeros`](@ref) and [`nzrange`](@ref).

# Examples
```jldoctest
Expand Down Expand Up @@ -725,11 +749,13 @@ function Base.sizehint!(S::SparseMatrixCSC, n::Integer)
end

# converting between SparseMatrixCSC types
SparseMatrixCSC(S::AbstractSparseMatrixCSC) = copy(S)

AbstractMatrix{Tv}(A::AbstractSparseMatrixCSC) where {Tv} = SparseMatrixCSC{Tv}(A)
SparseMatrixCSC{Tv}(S::AbstractSparseMatrixCSC{Tv}) where {Tv} = copy(S)
SparseMatrixCSC{Tv}(S::AbstractSparseMatrixCSC) where {Tv} = SparseMatrixCSC{Tv}(S)
SparseMatrixCSC{Tv}(S::AbstractSparseMatrixCSC) where {Tv} = SparseMatrixCSC{Tv,eltype(getcolptr(S))}(S)
SparseMatrixCSC{Tv,Ti}(S::AbstractSparseMatrixCSC{Tv,Ti}) where {Tv,Ti} = copy(S)
SobhanMP marked this conversation as resolved.
Show resolved Hide resolved
SparseMatrixCSC(S::SparseMatrixCSC)=copy(S)


function SparseMatrixCSC{Tv,Ti}(S::AbstractSparseMatrixCSC) where {Tv,Ti}
eltypeTicolptr = Vector{Ti}(getcolptr(S))
eltypeTirowval = Vector{Ti}(rowvals(S))
Expand Down