[Scipy-svn] r3625 - in trunk/scipy/sparse: . tests
scipy-svn@scip...
scipy-svn@scip...
Sat Dec 8 15:58:02 CST 2007
Author: wnbell
Date: 2007-12-08 15:57:23 -0600 (Sat, 08 Dec 2007)
New Revision: 3625
Modified:
trunk/scipy/sparse/sparse.py
trunk/scipy/sparse/tests/test_sparse.py
Log:
added todok()
Modified: trunk/scipy/sparse/sparse.py
===================================================================
--- trunk/scipy/sparse/sparse.py 2007-12-08 21:23:31 UTC (rev 3624)
+++ trunk/scipy/sparse/sparse.py 2007-12-08 21:57:23 UTC (rev 3625)
@@ -403,6 +403,9 @@
csr = self.tocsr()
return csr.toarray()
+ def todok(self):
+ return self.tocoo().todok()
+
def tocoo(self):
return self.tocsr().tocoo()
@@ -1404,7 +1407,7 @@
structure for constructing sparse matrices for conversion to other
sparse matrix types.
"""
- def __init__(self, A=None, shape=None, dtype=None):
+ def __init__(self, A=None, shape=None, dtype=None, copy=False):
""" Create a new dictionary-of-keys sparse matrix. An optional
argument A is accepted, which initializes the dok_matrix with it.
This can be a tuple of dimensions (M, N) or a (dense) array
@@ -1423,21 +1426,17 @@
elif isspmatrix(A):
# For sparse matrices, this is too inefficient; we need
# something else.
- raise NotImplementedError, "initializing a dok_matrix with " \
- "a sparse matrix is not yet supported"
+ if isspmatrix_dok(A) and copy:
+ A = A.copy()
+ else:
+ A = A.todok()
+ self.update( A )
+ self.shape = A.shape
+ self.dtype = A.dtype
elif isdense(A):
- # Convert to a (1 x n) row vector
- if rank(A) == 1:
- A = A.reshape(1, len(A))
- if rank(A) == 2:
- M, N = A.shape
- self.shape = (M, N)
- for i in xrange(M):
- for j in xrange(N):
- if A[i, j] != 0:
- self[i, j] = A[i, j]
- else:
- raise ValueError, "dense array must have rank 1 or 2"
+ self.update( coo_matrix(A).todok() )
+ self.shape = A.shape
+ self.dtype = A.dtype
else:
raise TypeError, "argument should be a tuple of dimensions " \
"or a sparse or dense matrix"
@@ -1928,6 +1927,11 @@
indices = asarray(self.keys(),dtype=intc).T
return coo_matrix((data,indices),dims=self.shape,dtype=self.dtype)
+ def todok(self,copy=False):
+ if copy:
+ return self.copy()
+ else:
+ return self
def tocsr(self):
""" Return a copy of this matrix in Compressed Sparse Row format"""
@@ -2115,7 +2119,12 @@
def tocoo(self, copy=False):
return self.toself(copy)
+ def todok(self):
+ dok = dok_matrix((self.shape),dtype=self.dtype)
+ dok.update( zip(zip(self.row,self.col),self.data) )
+ return dok
+
class lil_matrix(spmatrix):
"""Row-based linked list matrix, by Ed Schofield.
Modified: trunk/scipy/sparse/tests/test_sparse.py
===================================================================
--- trunk/scipy/sparse/tests/test_sparse.py 2007-12-08 21:23:31 UTC (rev 3624)
+++ trunk/scipy/sparse/tests/test_sparse.py 2007-12-08 21:57:23 UTC (rev 3625)
@@ -284,6 +284,10 @@
a = self.datsp.tolil()
assert_array_almost_equal(a.todense(), self.dat)
+ def check_todok(self):
+ a = self.datsp.todok()
+ assert_array_almost_equal(a.todense(), self.dat)
+
def check_tocsc(self):
a = self.datsp.tocsc()
assert_array_almost_equal(a.todense(), self.dat)
@@ -1338,7 +1342,6 @@
print '===================================================================='
print ' format | tocsr() | tocsc() | tocoo() | tolil() | todok() '
print '--------------------------------------------------------------------'
- fmt = ' %3s | %5.2fs | %5.2fs | %5.2fs | %5.2fs | %5.2fs | %5.2fs '
for fromfmt in formats:
#base = getattr(A,'to' + fromfmt)()
@@ -1366,7 +1369,7 @@
if t is None:
output += '| n/a '
else:
- output += '| %9.6f ' % t
+ output += '| %9.7f ' % t
print output
More information about the Scipy-svn
mailing list