[SciPy-user] Sparse: Efficient metod to convert to CSC format

Robert Cimrman cimrman3 at ntc.zcu.cz
Mon Oct 2 09:13:05 EDT 2006


William Hunter wrote:
> I've been bugging Robert C. and Ed S. (thanks guys) for a while about
> this now, but I think it might be good if I share my experiences with
> other users.
> 
> When converting a sparse matrix to CSC format, one of the quickest
> ways to get there is to give <sparse.csc_matrix> three vectors
> containing;
> 
> (1) [values]: the values,
> (2) [rowind]: the row indices and
> (3) [colptr]: the column pointer.
> 
> If one times how long it takes to get your CSC matrix by doing
> sparse.csc_matrix((values, rowind, colptr)) compared to the other ways
> to get it, e.g., csc_matrix(dense_mtx), I've found that the first
> method is faster (enough to be significant for me).
> 
> One can solve a sparse system with <linsolve.spsolve>, if your matrix
> is in CSC format. And here's my question: Let's say I have a way (and
> I might :-)) to construct those 3 vectors very quickly, how can I use
> them directly as arguments in <linsolve.spsolve>? For example:
> 
> solution = linsolve.spsolve([values],[rowind],[colptr],[RHS])

If you have already (values, rowind, colptr) triplet, creating a CSC 
matrix is virtually a no-operation (besides some sanity checks).

Is it really so inconvenient to write:
solution = linsolve.spsolve( csc_matrix( (values, rowind, colptr), shape 
),[RHS])?

On the other hand, the syntax
solution = linsolve.spsolve( (values,rowind,colptr,'csc'), [RHS])
might be useful as a syntactic sugar, so I am not against adding it. 
Note however, that you must indicate somehow if your triplet is CSR or 
CSC or ... - it is not much shorter then writing the full CSC constructor.

r.



More information about the SciPy-User mailing list