[Scipy-svn] r2362 - in trunk/Lib/sparse: . tests
scipy-svn at scipy.org
scipy-svn at scipy.org
Thu Dec 7 07:39:06 EST 2006
Author: rc
Date: 2006-12-07 06:39:01 -0600 (Thu, 07 Dec 2006)
New Revision: 2362
Modified:
trunk/Lib/sparse/sparse.py
trunk/Lib/sparse/tests/test_sparse.py
Log:
coo._normalize() update by Nathan Bell (ticket 326)
Modified: trunk/Lib/sparse/sparse.py
===================================================================
--- trunk/Lib/sparse/sparse.py 2006-12-07 01:35:50 UTC (rev 2361)
+++ trunk/Lib/sparse/sparse.py 2006-12-07 12:39:01 UTC (rev 2362)
@@ -2276,19 +2276,20 @@
def _normalize(self, rowfirst=False):
if rowfirst:
- l = zip(self.row, self.col, self.data)
- l.sort()
- row, col, data = list(itertools.izip(*l))
- return data, row, col
+ #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:
+ #nothing already sorted
+ 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
- l = zip(self.col, self.row, self.data)
- l.sort()
- # This breaks when len(self.data) etc == 0. Does this matter?
- col, row, data = list(itertools.izip(*l))
- self.col = asarray(col, intc)
- self.row = asarray(row, intc)
- self.data = array(data, self.dtype)
+ #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
Modified: trunk/Lib/sparse/tests/test_sparse.py
===================================================================
--- trunk/Lib/sparse/tests/test_sparse.py 2006-12-07 01:35:50 UTC (rev 2361)
+++ trunk/Lib/sparse/tests/test_sparse.py 2006-12-07 12:39:01 UTC (rev 2362)
@@ -21,8 +21,8 @@
import random
from numpy.testing import *
set_package_path()
-from scipy.sparse import csc_matrix, csr_matrix, dok_matrix, spidentity, \
- speye, lil_matrix
+from scipy.sparse import csc_matrix, csr_matrix, dok_matrix, coo_matrix, \
+ spidentity, speye, lil_matrix
from scipy.linsolve import splu
restore_path()
@@ -638,6 +638,38 @@
b = array([[1, 0, 0], [0, 1, 0], [0, 0, 1]], dtype='d')
assert_array_equal(a.toarray(), b)
+class test_coo(ScipyTestCase):
+ def check_normalize( self ):
+
+ row = numpy.array([2, 3, 1, 3, 0, 1, 3, 0, 2, 1, 2])
+ col = numpy.array([0, 1, 0, 0, 1, 1, 2, 2, 2, 2, 1])
+ data = numpy.array([ 6., 10., 3., 9., 1., 4.,
+ 11., 2., 8., 5., 7.])
+
+ # coo.todense()
+ # matrix([[ 0., 1., 2.],
+ # [ 3., 4., 5.],
+ # [ 6., 7., 8.],
+ # [ 9., 10., 11.]])
+ coo = coo_matrix((data,(row,col)),(4,3))
+
+ ndata,nrow,ncol = coo._normalize(rowfirst=True)
+ assert(zip(nrow,ncol,ndata) == sorted(zip(row,col,data))) #should sort by rows, then cols
+ assert_array_equal(coo.data, data) #coo.data has not changed
+ assert_array_equal(coo.row, row) #coo.row has not changed
+ assert_array_equal(coo.col, col) #coo.col has not changed
+
+
+ ndata,nrow,ncol = coo._normalize(rowfirst=False)
+ assert(zip(ncol,nrow,ndata) == sorted(zip(col,row,data))) #should sort by cols, then rows
+ assert_array_equal(coo.data, ndata) #coo.data has changed
+ assert_array_equal(coo.row, nrow) #coo.row has changed
+ assert_array_equal(coo.col, ncol) #coo.col has changed
+
+ assert_array_equal(coo.tocsr().todense(), coo.todense())
+ assert_array_equal(coo.tocsc().todense(), coo.todense())
+
+
if __name__ == "__main__":
ScipyTest().run()
More information about the Scipy-svn
mailing list