Skip to content
This repository
Newer
Older
100644 481 lines (404 sloc) 15.734 kb
e835bd3c »
2008-04-20 ran reindent
1 """LInked List sparse matrix class
33e29d75 »
2007-12-14 split sparse matrix classes and functions into separate files
2 """
3
4e0da1a1 »
2008-03-07 updated sparse docstrings
4 __docformat__ = "restructuredtext en"
5
b8437186 »
2007-12-15 changed sparsetools interface.
6 __all__ = ['lil_matrix','isspmatrix_lil']
7
33e29d75 »
2007-12-14 split sparse matrix classes and functions into separate files
8 from bisect import bisect_left
9
764696b7 »
2008-10-25 cleaned up lil_matrix imports
10 import numpy as np
33e29d75 »
2007-12-14 split sparse matrix classes and functions into separate files
11
12 from base import spmatrix, isspmatrix
764696b7 »
2008-10-25 cleaned up lil_matrix imports
13 from sputils import getdtype, isshape, issequence, isscalarlike
33e29d75 »
2007-12-14 split sparse matrix classes and functions into separate files
14
2dea8aa1 »
2011-11-03 ENH: Raise an efficiencywarning when indexing into a lil matrix using…
15 from warnings import warn
16 from base import SparseEfficiencyWarning
17
18
33e29d75 »
2007-12-14 split sparse matrix classes and functions into separate files
19 class lil_matrix(spmatrix):
dc0c2ab0 »
2008-10-25 sparse matrices now conform to spmatrix( (M,N) ) -> empty M-by-N matrix
20 """Row-based linked list sparse matrix
4e0da1a1 »
2008-03-07 updated sparse docstrings
21
d7afea67 »
2008-10-26 ran reindent
22 This is an efficient structure for constructing sparse
dc0c2ab0 »
2008-10-25 sparse matrices now conform to spmatrix( (M,N) ) -> empty M-by-N matrix
23 matrices incrementally.
e835bd3c »
2008-04-20 ran reindent
24
4e0da1a1 »
2008-03-07 updated sparse docstrings
25 This can be instantiated in several ways:
dc0c2ab0 »
2008-10-25 sparse matrices now conform to spmatrix( (M,N) ) -> empty M-by-N matrix
26 lil_matrix(D)
4e0da1a1 »
2008-03-07 updated sparse docstrings
27 with a dense matrix or rank-2 ndarray D
28
dc0c2ab0 »
2008-10-25 sparse matrices now conform to spmatrix( (M,N) ) -> empty M-by-N matrix
29 lil_matrix(S)
df1c6fb4 »
2012-02-16 DOC: typo in lil_matrix
30 with another sparse matrix S (equivalent to S.tolil())
4e0da1a1 »
2008-03-07 updated sparse docstrings
31
dc0c2ab0 »
2008-10-25 sparse matrices now conform to spmatrix( (M,N) ) -> empty M-by-N matrix
32 lil_matrix((M, N), [dtype])
4e0da1a1 »
2008-03-07 updated sparse docstrings
33 to construct an empty matrix with shape (M, N)
34 dtype is optional, defaulting to dtype='d'.
33e29d75 »
2007-12-14 split sparse matrix classes and functions into separate files
35
d19c9639 »
2011-06-25 DOC: sparse: document attributes of the sparse matrices, and mention …
36 Attributes
37 ----------
38 dtype : dtype
39 Data type of the matrix
40 shape : 2-tuple
41 Shape of the matrix
42 ndim : int
43 Number of dimensions (this is always 2)
44 nnz
45 Number of nonzero elements
46 data
47 LIL format data array of the matrix
48 rows
49 LIL format row index array of the matrix
50
8bdf0440 »
2007-12-28 updated sparse docstrings
51 Notes
4e0da1a1 »
2008-03-07 updated sparse docstrings
52 -----
53
d19c9639 »
2011-06-25 DOC: sparse: document attributes of the sparse matrices, and mention …
54 Sparse matrices can be used in arithmetic operations: they support
55 addition, subtraction, multiplication, division, and matrix power.
56
4e0da1a1 »
2008-03-07 updated sparse docstrings
57 Advantages of the LIL format
58 - supports flexible slicing
59 - changes to the matrix sparsity structure are efficient
e835bd3c »
2008-04-20 ran reindent
60
4e0da1a1 »
2008-03-07 updated sparse docstrings
61 Disadvantages of the LIL format
62 - arithmetic operations LIL + LIL are slow (consider CSR or CSC)
63 - slow column slicing (consider CSC)
d2a18de9 »
2008-07-08 refactored sparse matrix multiplication handlers
64 - slow matrix vector products (consider CSR or CSC)
e835bd3c »
2008-04-20 ran reindent
65
4e0da1a1 »
2008-03-07 updated sparse docstrings
66 Intended Usage
e835bd3c »
2008-04-20 ran reindent
67 - LIL is a convenient format for constructing sparse matrices
68 - once a matrix has been constructed, convert to CSR or
4e0da1a1 »
2008-03-07 updated sparse docstrings
69 CSC format for fast arithmetic and matrix vector operations
70 - consider using the COO format when constructing large matrices
e835bd3c »
2008-04-20 ran reindent
71
4e0da1a1 »
2008-03-07 updated sparse docstrings
72 Data Structure
73 - An array (``self.rows``) of rows, each of which is a sorted
e835bd3c »
2008-04-20 ran reindent
74 list of column indices of non-zero elements.
4e0da1a1 »
2008-03-07 updated sparse docstrings
75 - The corresponding nonzero values are stored in similar
76 fashion in ``self.data``.
77
78
33e29d75 »
2007-12-14 split sparse matrix classes and functions into separate files
79 """
80
dc0c2ab0 »
2008-10-25 sparse matrices now conform to spmatrix( (M,N) ) -> empty M-by-N matrix
81 def __init__(self, arg1, shape=None, dtype=None, copy=False):
33e29d75 »
2007-12-14 split sparse matrix classes and functions into separate files
82 spmatrix.__init__(self)
dc0c2ab0 »
2008-10-25 sparse matrices now conform to spmatrix( (M,N) ) -> empty M-by-N matrix
83 self.dtype = getdtype(dtype, arg1, default=float)
33e29d75 »
2007-12-14 split sparse matrix classes and functions into separate files
84
85 # First get the shape
dc0c2ab0 »
2008-10-25 sparse matrices now conform to spmatrix( (M,N) ) -> empty M-by-N matrix
86 if isspmatrix(arg1):
87 if isspmatrix_lil(arg1) and copy:
88 A = arg1.copy()
e1e0f378 »
2007-12-31 consolidated bsr with _cs_matrix class
89 else:
dc0c2ab0 »
2008-10-25 sparse matrices now conform to spmatrix( (M,N) ) -> empty M-by-N matrix
90 A = arg1.tolil()
f4146a7d »
2008-10-27 fixed handling of dtype= constructor argument in sparse case
91
92 if dtype is not None:
93 A = A.astype(dtype)
94
e1e0f378 »
2007-12-31 consolidated bsr with _cs_matrix class
95 self.shape = A.shape
96 self.dtype = A.dtype
97 self.rows = A.rows
98 self.data = A.data
dc0c2ab0 »
2008-10-25 sparse matrices now conform to spmatrix( (M,N) ) -> empty M-by-N matrix
99 elif isinstance(arg1,tuple):
100 if isshape(arg1):
263790b7 »
2008-02-16 check that shape= is not specified when first arg is (M,N)
101 if shape is not None:
102 raise ValueError('invalid use of shape parameter')
dc0c2ab0 »
2008-10-25 sparse matrices now conform to spmatrix( (M,N) ) -> empty M-by-N matrix
103 M, N = arg1
33e29d75 »
2007-12-14 split sparse matrix classes and functions into separate files
104 self.shape = (M,N)
764696b7 »
2008-10-25 cleaned up lil_matrix imports
105 self.rows = np.empty((M,), dtype=object)
106 self.data = np.empty((M,), dtype=object)
33e29d75 »
2007-12-14 split sparse matrix classes and functions into separate files
107 for i in range(M):
108 self.rows[i] = []
109 self.data[i] = []
110 else:
dc0c2ab0 »
2008-10-25 sparse matrices now conform to spmatrix( (M,N) ) -> empty M-by-N matrix
111 raise TypeError('unrecognized lil_matrix constructor usage')
e1e0f378 »
2007-12-31 consolidated bsr with _cs_matrix class
112 else:
113 #assume A is dense
114 try:
764696b7 »
2008-10-25 cleaned up lil_matrix imports
115 A = np.asmatrix(arg1)
e1e0f378 »
2007-12-31 consolidated bsr with _cs_matrix class
116 except TypeError:
764696b7 »
2008-10-25 cleaned up lil_matrix imports
117 raise TypeError('unsupported matrix type')
e1e0f378 »
2007-12-31 consolidated bsr with _cs_matrix class
118 else:
119 from csr import csr_matrix
40e1c9a4 »
2008-10-27 added scipy.sparse.find()
120 A = csr_matrix(A, dtype=dtype).tolil()
e835bd3c »
2008-04-20 ran reindent
121
33e29d75 »
2007-12-14 split sparse matrix classes and functions into separate files
122 self.shape = A.shape
123 self.dtype = A.dtype
124 self.rows = A.rows
125 self.data = A.data
126
127 def __iadd__(self,other):
128 self[:,:] = self + other
129 return self
130
131 def __isub__(self,other):
132 self[:,:] = self - other
133 return self
134
135 def __imul__(self,other):
136 if isscalarlike(other):
137 self[:,:] = self * other
138 return self
139 else:
140 raise NotImplementedError
141
142 def __itruediv__(self,other):
143 if isscalarlike(other):
144 self[:,:] = self / other
145 return self
146 else:
147 raise NotImplementedError
148
149 # Whenever the dimensions change, empty lists should be created for each
150 # row
151
152 def getnnz(self):
153 return sum([len(rowvals) for rowvals in self.data])
199b69f0 »
2008-02-03 add nnz properties to all sparse matrices
154 nnz = property(fget=getnnz)
33e29d75 »
2007-12-14 split sparse matrix classes and functions into separate files
155
156 def __str__(self):
157 val = ''
158 for i, row in enumerate(self.rows):
159 for pos, j in enumerate(row):
160 val += " %s\t%s\n" % (str((i, j)), str(self.data[i][pos]))
161 return val[:-1]
162
163 def getrowview(self, i):
164 """Returns a view of the 'i'th row (without copying).
165 """
166 new = lil_matrix((1, self.shape[1]), dtype=self.dtype)
167 new.rows[0] = self.rows[i]
168 new.data[0] = self.data[i]
169 return new
170
171 def getrow(self, i):
172 """Returns a copy of the 'i'th row.
173 """
174 new = lil_matrix((1, self.shape[1]), dtype=self.dtype)
175 new.rows[0] = self.rows[i][:]
176 new.data[0] = self.data[i][:]
177 return new
178
179 def _get1(self, i, j):
39871ac2 »
2008-11-16 ran reindent for upcoming release
180
baa9e6a7 »
2008-11-15 added more rigorous checking of __getitem__ and __setitem__
181 if i < 0:
182 i += self.shape[0]
183 if i < 0 or i >= self.shape[0]:
184 raise IndexError('row index out of bounds')
33e29d75 »
2007-12-14 split sparse matrix classes and functions into separate files
185
186 if j < 0:
187 j += self.shape[1]
baa9e6a7 »
2008-11-15 added more rigorous checking of __getitem__ and __setitem__
188 if j < 0 or j >= self.shape[1]:
764696b7 »
2008-10-25 cleaned up lil_matrix imports
189 raise IndexError('column index out of bounds')
39871ac2 »
2008-11-16 ran reindent for upcoming release
190
baa9e6a7 »
2008-11-15 added more rigorous checking of __getitem__ and __setitem__
191 row = self.rows[i]
192 data = self.data[i]
33e29d75 »
2007-12-14 split sparse matrix classes and functions into separate files
193
194 pos = bisect_left(row, j)
195 if pos != len(data) and row[pos] == j:
196 return data[pos]
197 else:
198 return 0
199
200 def _slicetoseq(self, j, shape):
201 if j.start is not None and j.start < 0:
202 start = shape + j.start
203 elif j.start is None:
204 start = 0
205 else:
206 start = j.start
207 if j.stop is not None and j.stop < 0:
208 stop = shape + j.stop
209 elif j.stop is None:
210 stop = shape
211 else:
212 stop = j.stop
213 j = range(start, stop, j.step or 1)
214 return j
215
216
217 def __getitem__(self, index):
218 """Return the element(s) index=(i, j), where j may be a slice.
219 This always returns a copy for consistency, since slices into
220 Python lists return copies.
221 """
222 try:
223 i, j = index
224 except (AssertionError, TypeError):
764696b7 »
2008-10-25 cleaned up lil_matrix imports
225 raise IndexError('invalid index')
33e29d75 »
2007-12-14 split sparse matrix classes and functions into separate files
226
2dea8aa1 »
2011-11-03 ENH: Raise an efficiencywarning when indexing into a lil matrix using…
227 if not np.isscalar(i) and np.isscalar(j):
228 warn('Indexing into a lil_matrix with multiple indices is slow. '
229 'Pre-converting to CSC or CSR beforehand is more efficient.',
230 SparseEfficiencyWarning)
231
764696b7 »
2008-10-25 cleaned up lil_matrix imports
232 if np.isscalar(i):
233 if np.isscalar(j):
33e29d75 »
2007-12-14 split sparse matrix classes and functions into separate files
234 return self._get1(i, j)
235 if isinstance(j, slice):
236 j = self._slicetoseq(j, self.shape[1])
237 if issequence(j):
238 return self.__class__([[self._get1(i, jj) for jj in j]])
239 elif issequence(i) and issequence(j):
240 return self.__class__([[self._get1(ii, jj) for (ii, jj) in zip(i, j)]])
241 elif issequence(i) or isinstance(i, slice):
242 if isinstance(i, slice):
243 i = self._slicetoseq(i, self.shape[0])
764696b7 »
2008-10-25 cleaned up lil_matrix imports
244 if np.isscalar(j):
33e29d75 »
2007-12-14 split sparse matrix classes and functions into separate files
245 return self.__class__([[self._get1(ii, j)] for ii in i])
246 if isinstance(j, slice):
247 j = self._slicetoseq(j, self.shape[1])
248 if issequence(j):
249 return self.__class__([[self._get1(ii, jj) for jj in j] for ii in i])
250 else:
251 raise IndexError
252
253 def _insertat2(self, row, data, j, x):
254 """ helper for __setitem__: insert a value in the given row/data at
255 column j. """
256
257 if j < 0: #handle negative column indices
258 j += self.shape[1]
259
260 if j < 0 or j >= self.shape[1]:
764696b7 »
2008-10-25 cleaned up lil_matrix imports
261 raise IndexError('column index out of bounds')
f57eec9d »
2011-08-30 MAINT: run reindent on all .py files.
262
121d9534 »
2009-03-21 lil_matrix now rejects sequences where scalars are expected
263 if not np.isscalar(x):
264 raise ValueError('setting an array element with a sequence')
265
266 try:
267 x = self.dtype.type(x)
268 except:
269 raise TypeError('Unable to convert value (%s) to dtype [%s]' % (x,self.dtype.name))
33e29d75 »
2007-12-14 split sparse matrix classes and functions into separate files
270
271 pos = bisect_left(row, j)
272 if x != 0:
273 if pos == len(row):
274 row.append(j)
275 data.append(x)
276 elif row[pos] != j:
277 row.insert(pos, j)
278 data.insert(pos, x)
279 else:
280 data[pos] = x
281 else:
282 if pos < len(row) and row[pos] == j:
283 del row[pos]
284 del data[pos]
285
525efb73 »
2009-11-27 merged patch contribued by Tim Victor
286 def _setitem_setrow(self, row, data, j, xrow, xdata, xcols):
33e29d75 »
2007-12-14 split sparse matrix classes and functions into separate files
287 if isinstance(j, slice):
288 j = self._slicetoseq(j, self.shape[1])
289 if issequence(j):
525efb73 »
2009-11-27 merged patch contribued by Tim Victor
290 if xcols == len(j):
291 for jj, xi in zip(j, xrange(xcols)):
f57eec9d »
2011-08-30 MAINT: run reindent on all .py files.
292 pos = bisect_left(xrow, xi)
293 if pos != len(xdata) and xrow[pos] == xi:
294 self._insertat2(row, data, jj, xdata[pos])
295 else:
296 self._insertat2(row, data, jj, 0)
525efb73 »
2009-11-27 merged patch contribued by Tim Victor
297 elif xcols == 1: # OK, broadcast across row
298 if len(xdata) > 0 and xrow[0] == 0:
299 val = xdata[0]
300 else:
301 val = 0
33e29d75 »
2007-12-14 split sparse matrix classes and functions into separate files
302 for jj in j:
525efb73 »
2009-11-27 merged patch contribued by Tim Victor
303 self._insertat2(row, data, jj,val)
33e29d75 »
2007-12-14 split sparse matrix classes and functions into separate files
304 else:
525efb73 »
2009-11-27 merged patch contribued by Tim Victor
305 raise IndexError('invalid index')
764696b7 »
2008-10-25 cleaned up lil_matrix imports
306 elif np.isscalar(j):
525efb73 »
2009-11-27 merged patch contribued by Tim Victor
307 if not xcols == 1:
308 raise ValueError('array dimensions are not compatible for copy')
309 if len(xdata) > 0 and xrow[0] == 0:
310 self._insertat2(row, data, j, xdata[0])
311 else:
312 self._insertat2(row, data, j, 0)
33e29d75 »
2007-12-14 split sparse matrix classes and functions into separate files
313 else:
764696b7 »
2008-10-25 cleaned up lil_matrix imports
314 raise ValueError('invalid column value: %s' % str(j))
33e29d75 »
2007-12-14 split sparse matrix classes and functions into separate files
315
316 def __setitem__(self, index, x):
317 try:
318 i, j = index
319 except (ValueError, TypeError):
764696b7 »
2008-10-25 cleaned up lil_matrix imports
320 raise IndexError('invalid index')
33e29d75 »
2007-12-14 split sparse matrix classes and functions into separate files
321
525efb73 »
2009-11-27 merged patch contribued by Tim Victor
322 # shortcut for common case of single entry assign:
323 if np.isscalar(x) and np.isscalar(i) and np.isscalar(j):
324 self._insertat2(self.rows[i], self.data[i], j, x)
325 return
326
327 # shortcut for common case of full matrix assign:
befacfa4 »
2008-09-05 fixed lil_matrix fancy indexing problem pointed out by RC
328 if isspmatrix(x):
f57eec9d »
2011-08-30 MAINT: run reindent on all .py files.
329 if isinstance(i, slice) and i == slice(None) and \
330 isinstance(j, slice) and j == slice(None):
331 x = lil_matrix(x)
332 self.rows = x.rows
333 self.data = x.data
334 return
525efb73 »
2009-11-27 merged patch contribued by Tim Victor
335
336 if isinstance(i, tuple): # can't index lists with tuple
337 i = list(i)
befacfa4 »
2008-09-05 fixed lil_matrix fancy indexing problem pointed out by RC
338
764696b7 »
2008-10-25 cleaned up lil_matrix imports
339 if np.isscalar(i):
525efb73 »
2009-11-27 merged patch contribued by Tim Victor
340 rows = [self.rows[i]]
341 datas = [self.data[i]]
342 else:
33e29d75 »
2007-12-14 split sparse matrix classes and functions into separate files
343 rows = self.rows[i]
344 datas = self.data[i]
525efb73 »
2009-11-27 merged patch contribued by Tim Victor
345
346 x = lil_matrix(x, copy=False)
347 xrows, xcols = x.shape
348 if xrows == len(rows): # normal rectangular copy
349 for row, data, xrow, xdata in zip(rows, datas, x.rows, x.data):
350 self._setitem_setrow(row, data, j, xrow, xdata, xcols)
351 elif xrows == 1: # OK, broadcast down column
352 for row, data in zip(rows, datas):
353 self._setitem_setrow(row, data, j, x.rows[0], x.data[0], xcols)
354
355 # needed to pass 'test_lil_sequence_assignement' unit test:
356 # -- set row from column of entries --
357 elif xcols == len(rows):
358 x = x.T
359 for row, data, xrow, xdata in zip(rows, datas, x.rows, x.data):
360 self._setitem_setrow(row, data, j, xrow, xdata, xrows)
33e29d75 »
2007-12-14 split sparse matrix classes and functions into separate files
361 else:
525efb73 »
2009-11-27 merged patch contribued by Tim Victor
362 raise IndexError('invalid index')
33e29d75 »
2007-12-14 split sparse matrix classes and functions into separate files
363
d2a18de9 »
2008-07-08 refactored sparse matrix multiplication handlers
364 def _mul_scalar(self, other):
365 if other == 0:
366 # Multiply by zero: return the zero matrix
dc0c2ab0 »
2008-10-25 sparse matrices now conform to spmatrix( (M,N) ) -> empty M-by-N matrix
367 new = lil_matrix(self.shape, dtype=self.dtype)
33e29d75 »
2007-12-14 split sparse matrix classes and functions into separate files
368 else:
d2a18de9 »
2008-07-08 refactored sparse matrix multiplication handlers
369 new = self.copy()
370 # Multiply this scalar by every element.
7cdfb343 »
2011-11-02 BUG: Unable to delete row from lil matrix after scalar multiplication…
371 new.data[:] = [[val*other for val in rowvals] for
372 rowvals in new.data]
d2a18de9 »
2008-07-08 refactored sparse matrix multiplication handlers
373 return new
79a60197 »
2008-08-08 ran reindent
374
33e29d75 »
2007-12-14 split sparse matrix classes and functions into separate files
375 def __truediv__(self, other): # self / other
376 if isscalarlike(other):
377 new = self.copy()
378 # Divide every element by this scalar
7cdfb343 »
2011-11-02 BUG: Unable to delete row from lil matrix after scalar multiplication…
379 new.data = [[val/other for val in rowvals] for
380 rowvals in new.data]
33e29d75 »
2007-12-14 split sparse matrix classes and functions into separate files
381 return new
382 else:
383 return self.tocsr() / other
384
8ba469d9 »
2008-05-21 resolves ticket #671, division error in complex_ops.h
385 ## This code doesn't work with complex matrices
386 # def multiply(self, other):
387 # """Point-wise multiplication by another lil_matrix.
388 #
389 # """
764696b7 »
2008-10-25 cleaned up lil_matrix imports
390 # if np.isscalar(other):
8ba469d9 »
2008-05-21 resolves ticket #671, division error in complex_ops.h
391 # return self.__mul__(other)
392 #
393 # if isspmatrix_lil(other):
394 # reference,target = self,other
395 #
396 # if reference.shape != target.shape:
397 # raise ValueError("Dimensions do not match.")
398 #
399 # if len(reference.data) > len(target.data):
400 # reference,target = target,reference
401 #
402 # new = lil_matrix(reference.shape)
403 # for r,row in enumerate(reference.rows):
404 # tr = target.rows[r]
405 # td = target.data[r]
406 # rd = reference.data[r]
407 # L = len(tr)
408 # for c,column in enumerate(row):
409 # ix = bisect_left(tr,column)
410 # if ix < L and tr[ix] == column:
411 # new.rows[r].append(column)
412 # new.data[r].append(rd[c] * td[ix])
413 # return new
414 # else:
415 # raise ValueError("Point-wise multiplication only allowed "
416 # "with another lil_matrix.")
33e29d75 »
2007-12-14 split sparse matrix classes and functions into separate files
417
418 def copy(self):
764696b7 »
2008-10-25 cleaned up lil_matrix imports
419 from copy import deepcopy
33e29d75 »
2007-12-14 split sparse matrix classes and functions into separate files
420 new = lil_matrix(self.shape, dtype=self.dtype)
764696b7 »
2008-10-25 cleaned up lil_matrix imports
421 new.data = deepcopy(self.data)
422 new.rows = deepcopy(self.rows)
33e29d75 »
2007-12-14 split sparse matrix classes and functions into separate files
423 return new
424
425 def reshape(self,shape):
764696b7 »
2008-10-25 cleaned up lil_matrix imports
426 new = lil_matrix(shape, dtype=self.dtype)
33e29d75 »
2007-12-14 split sparse matrix classes and functions into separate files
427 j_max = self.shape[1]
428 for i,row in enumerate(self.rows):
429 for col,j in enumerate(row):
764696b7 »
2008-10-25 cleaned up lil_matrix imports
430 new_r,new_c = np.unravel_index(i*j_max + j,shape)
33e29d75 »
2007-12-14 split sparse matrix classes and functions into separate files
431 new[new_r,new_c] = self[i,j]
432 return new
433
53786613 »
2012-06-04 ENH: Added an out= argument to sparse toarray().
434 def toarray(self, order=None, out=None):
0ceeefa7 »
2012-06-04 DOC: added pointers to superclass docstring.
435 """See the docstring for `spmatrix.toarray`."""
53786613 »
2012-06-04 ENH: Added an out= argument to sparse toarray().
436 d = self._process_toarray_args(order, out)
33e29d75 »
2007-12-14 split sparse matrix classes and functions into separate files
437 for i, row in enumerate(self.rows):
438 for pos, j in enumerate(row):
439 d[i, j] = self.data[i][pos]
440 return d
441
442 def transpose(self):
c004831e »
2007-12-14 deprecated dims argument in CSR/CSC/COO constructors
443 return self.tocsr().transpose().tolil()
33e29d75 »
2007-12-14 split sparse matrix classes and functions into separate files
444
445 def tolil(self, copy=False):
446 if copy:
447 return self.copy()
448 else:
449 return self
450
451 def tocsr(self):
452 """ Return Compressed Sparse Row format arrays for this matrix.
453 """
e835bd3c »
2008-04-20 ran reindent
454
764696b7 »
2008-10-25 cleaned up lil_matrix imports
455 indptr = np.asarray([len(x) for x in self.rows], dtype=np.intc)
456 indptr = np.concatenate( (np.array([0], dtype=np.intc), np.cumsum(indptr)) )
e835bd3c »
2008-04-20 ran reindent
457
33e29d75 »
2007-12-14 split sparse matrix classes and functions into separate files
458 nnz = indptr[-1]
e835bd3c »
2008-04-20 ran reindent
459
33e29d75 »
2007-12-14 split sparse matrix classes and functions into separate files
460 indices = []
461 for x in self.rows:
462 indices.extend(x)
764696b7 »
2008-10-25 cleaned up lil_matrix imports
463 indices = np.asarray(indices, dtype=np.intc)
33e29d75 »
2007-12-14 split sparse matrix classes and functions into separate files
464
465 data = []
466 for x in self.data:
467 data.extend(x)
764696b7 »
2008-10-25 cleaned up lil_matrix imports
468 data = np.asarray(data, dtype=self.dtype)
33e29d75 »
2007-12-14 split sparse matrix classes and functions into separate files
469
035869db »
2007-12-14 moved csr_matrix and csc_matrix to individual files
470 from csr import csr_matrix
c004831e »
2007-12-14 deprecated dims argument in CSR/CSC/COO constructors
471 return csr_matrix((data, indices, indptr), shape=self.shape)
33e29d75 »
2007-12-14 split sparse matrix classes and functions into separate files
472
473 def tocsc(self):
474 """ Return Compressed Sparse Column format arrays for this matrix.
475 """
476 return self.tocsr().tocsc()
477
478
479 def isspmatrix_lil( x ):
06cd6e5d »
2011-10-01 ENH: sparse: reduce overheads in sparse matrix multiplication
480 return isinstance(x, lil_matrix)
Something went wrong with that request. Please try again.