Column pointers of sparse array in case of empty columns

Hi there,

I’ve noticed something that isn’t well-documented imho.

When a sparse matrix has an empty column i, what is colptr[i]?

Example:

sparse(Matrix(S))
3×3 SparseMatrixCSC{Float64, Int64} with 3 stored entries:
 0.360646   ⋅    ⋅ 
  ⋅         ⋅   0.688464
 0.230095   ⋅    ⋅

gives

S.colptr
4-element Vector{Int64}:
 1
 3
 3
 4

The Sparse Arrays documentation says

colptr::Vector{Ti}      # Column j is in colptr[j]:(colptr[j+1]-1)

Can anyone elucidate?

The number of elements in column i is colptr[i+1]-colptr[i], so for an empty column you simply have colptr[i+1] == colptr[i].

1 Like