diff --git a/src/SparseArrays.jl b/src/SparseArrays.jl index a3ef1767..7ca5103d 100644 --- a/src/SparseArrays.jl +++ b/src/SparseArrays.jl @@ -29,10 +29,10 @@ import Base: adjoint, argmin, argmax, Array, broadcast, circshift!, complex, Com using Random: default_rng, AbstractRNG, randsubseq, randsubseq! -export AbstractSparseArray, AbstractSparseMatrix, AbstractSparseVector, +export AbstractSparseArray, AbstractSparseMatrix, AbstractSparseVector, AbstractSparseMatrixCSC, SparseMatrixCSC, SparseVector, blockdiag, droptol!, dropzeros!, dropzeros, - issparse, nonzeros, nzrange, rowvals, sparse, sparsevec, spdiagm, - sprand, sprandn, spzeros, nnz, permute, findnz, fkeep!, ftranspose!, + issparse, nonzeros, nzrange, rowvals, getcolptr, sparse, sparsevec, spdiagm, + sprand, sprandn, spzeros, nnz, indtype, permute, findnz, fkeep!, ftranspose!, sparse_hcat, sparse_vcat, sparse_hvcat public sparse!, spzeros! diff --git a/src/abstractsparse.jl b/src/abstractsparse.jl index 962759a0..322bde4a 100644 --- a/src/abstractsparse.jl +++ b/src/abstractsparse.jl @@ -75,6 +75,11 @@ end issparse(A::DenseArray) = false issparse(S::AbstractSparseArray) = true +""" + indtype(S) + +Return the type used to index sparse array entries. +""" indtype(S::AbstractSparseArray{<:Any,Ti}) where {Ti} = Ti indtype(T::UpperOrLowerTriangular{<:Any,<:AbstractSparseArray}) = indtype(parent(T)) diff --git a/src/sparsematrix.jl b/src/sparsematrix.jl index 6c138edb..7f3bf754 100644 --- a/src/sparsematrix.jl +++ b/src/sparsematrix.jl @@ -191,6 +191,31 @@ const SparseMatrixCSCColumnSubset{Tv,Ti} = Tuple{Base.Slice{Base.OneTo{Int}},I}} where {I<:AbstractVector{<:Integer}} const SparseMatrixCSCUnion2{Tv,Ti} = Union{AbstractSparseMatrixCSC{Tv,Ti}, SparseMatrixCSCColumnSubset{Tv,Ti}} +""" + getcolptr(S) + +Return the vector of column start indices in an `AbstractSparseMatrixCSC` pointing +into [`nonzeros`](@ref) and [`rowvals`](@ref). The returned vector aliases `S`, +but implementations with fixed sparsity may make it read-only. When it is +writable, modifications to it mutate `S`. Providing access to the column start +indices can be useful in preconditioners and sparse direct solvers. + +# 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(S.indices[2]):(last(S.indices[2]) + 1)) getcolptr(S::SparseMatrixCSCColumnSubset) = error("getcolptr not well-defined for $(typeof(S))")