What is "colptr" in Julia and its counterpart in Python?

Viewed 297

I'm trying to rewrite some Julia code into python code. And I just found colptr attached to sparse matrix. I searched it but I still don't understand what it is. Could someone provide me the information about it and counterpart in python 3? Thank you in advance.

[edit] this is from Julia's references

struct SparseMatrixCSC{Tv,Ti<:Integer} <: AbstractSparseMatrix{Tv,Ti}
m::Int                  # Number of rows
n::Int                  # Number of columns
colptr::Vector{Ti}      # Column j is in colptr[j]:(colptr[j+1]-1)
rowval::Vector{Ti}      # Row indices of stored values
nzval::Vector{Tv}       # Stored values, typically nonzeros

for instance, A.colptr[j] is referring all the elements of j-th column of CSC matrix A?

I tried to figure it out by running some simple code below,

A = sparse([1, 1, 2, 3], [1, 3, 2, 3], [0, 1, 2, 0])

for i=1:4
    println(A.colptr[i])
end

and the result was

1 2 3 5

I still have no idea why the result would be like this. explanation says

Ti is the integer type for storing column pointers

1 Answers

You're looking at a compressed sparse column (CSC) representation of a matrix. Instead of, for example, storing all the values of a matrix in sequence in memory, this allows only storing nonzero values. For example, the matrix

5  0  0
6  0  7
1  0  3
0  2  0

can either be stored in memory as the column major sequence 5 6 1 0 0 0 0 2 0 7 3 0 or you could do something smarter.

If you only store the column major sequence of nonzero elements, you end up with a much shorter list: 5 6 1 2 7 3! But now you need a way to map these values back to their locations in the matrix. You need a column index and a row index. Thus, we have two more lists:

The row indices for each stored value can also be stored in a one-to-one fashion: 1 2 3 4 2 3.

Now, I could store the column indices in a similar one-to-one fashion: 1 1 1 2 3 3. Were I to do so, this would be a sparse coordinate (COO) format. But note that there's a lot of redundant information here: look at all those repeated values! The common CSC format compresses this further. I already know I have three columns; I could simply store where each column starts. This is the colptr: it has one value per column and points to where that column starts. Thus instead of storing six values, it only need to store three: column one starts at index one (of course). Column two starts at index 4, and column three starts at index 5. It turns out to make life a bit easier if we also store a final fourth value representing one past the end, because then we can describe the nonzero values in a particular column simply by saying that the stored values in column j can be found in nzval[colptr[j]:colptr[j+1]-1].

The typical Python equivalent is in scipy: scipy.sparse.csc_matrix; simply substitutue colptr -> indptr, indices -> rowval, nzval -> data and accommodate the 0-based indexing.

Related