# [Scipy-tickets] [SciPy] #326: coo_matrix.tocsr() and tocsc() are very slow

SciPy scipy-tickets at scipy.net
Fri Dec 1 05:16:24 CST 2006

```#326: coo_matrix.tocsr() and tocsc() are very slow
------------------------------------+---------------------------------------
Reporter:  wnbell                  |        Owner:  somebody
Type:  defect                  |       Status:  new
Priority:  high                    |    Milestone:
Component:  scipy.sparse            |      Version:  devel
Severity:  major                   |   Resolution:
Keywords:  sparse coo_matrix slow  |
------------------------------------+---------------------------------------
Changes (by wnbell):

* priority:  highest => high

Comment:

There was a typo in my previous implementation (the P should be Pr in the
first block).  I also realized that numpy.lexsort() is a slightly better
way to sort for this problem, so I've updated my code to the following:

{{{
def _normalize(self, rowfirst=False):
if rowfirst:
#sort by increasing rows first, columns second
if getattr(self, '_is_normalized', None):
#columns already sorted, use stable sort for rows
P = numpy.argsort(self.row,kind='mergesort')
return self.data[P], self.row[P], self.col[P]
else:
P  = numpy.lexsort(keys=(self.col,self.row))
return self.data[P], self.row[P], self.col[P]
if getattr(self, '_is_normalized', None):
return self.data, self.row, self.col
#sort by increasing rows first, columns second
P  = numpy.lexsort(keys=(self.row,self.col))
self.data,self.row,self.col = self.data[P], self.row[P],
self.col[P]
setattr(self, '_is_normalized', 1)
return self.data, self.row, self.col
}}}

I've attached a unittest to coo_matrix._normalize() and an updated SVN
diff.

--
Ticket URL: <http://projects.scipy.org/scipy/scipy/ticket/326#comment:1>
SciPy <http://www.scipy.org/>
SciPy is open-source software for mathematics, science, and engineering.
```