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

William Hunter willemjagter at gmail.com
Mon Oct 2 09:15:33 CDT 2006

On 02/10/06, Robert Cimrman <cimrman3 at ntc.zcu.cz> wrote:
> 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).

I suppose I'm really interested (curious) to know what happens behind
the scenes and why (except for the checks). I had a look at the
source, but gave up after a while. There's a method that creates an
instance (?) and I think I understand why it is useful to group the
vectors, but I can't see how this is done, so perhaps I shouldn't be
reading there in any case, but I'd still like to know what happens if
it doesn't require a lot of explanation. I know I shouldn't worry
about it, but I just can't help myself...

> 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.

No need, that's just syntax.

> 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.
> _______________________________________________
> SciPy-user mailing list
> SciPy-user at scipy.org
> http://projects.scipy.org/mailman/listinfo/scipy-user

More information about the SciPy-user mailing list