def tolil(self): from lil import lil_matrix lil = lil_matrix(self.shape,dtype=self.dtype) self.sort_indices() #lil_matrix needs sorted column indices ptr,ind,dat = self.indptr,self.indices,self.data rows, data = lil.rows, lil.data for n in xrange(self.shape[0]): start = ptr[n] end = ptr[n+1] rows[n] = ind[start:end].tolist() data[n] = dat[start:end].tolist() return lil
def tolil(self): from lil import lil_matrix lil = lil_matrix(self.shape, dtype=self.dtype) self.sort_indices() #lil_matrix needs sorted column indices ptr, ind, dat = self.indptr, self.indices, self.data rows, data = lil.rows, lil.data for n in xrange(self.shape[0]): start = ptr[n] end = ptr[n + 1] rows[n] = ind[start:end].tolist() data[n] = dat[start:end].tolist() return lil
>>> lil_diags([[1,2,3],[4,5],[6]],[0,1,2],(3,3)).todense() matrix([[ 1., 4., 6.], [ 0., 2., 5.], [ 0., 0., 3.]]) """ offsets_unsorted = list(offsets) diags_unsorted = list(diags) if len(diags) != len(offsets): raise ValueError("Number of diagonals provided should " "agree with offsets.") sort_indices = np.argsort(offsets_unsorted) diags = [diags_unsorted[k] for k in sort_indices] offsets = [offsets_unsorted[k] for k in sort_indices] for i, k in enumerate(offsets): if len(diags[i]) < m - abs(k): raise ValueError("Not enough values specified to fill " "diagonal %s." % k) out = lil_matrix((m, n), dtype=dtype) from itertools import izip for k, diag in izip(offsets, diags): for ix, c in enumerate(xrange(np.clip(k, 0, n), np.clip(m + k, 0, n))): out.rows[c - k].append(c) out.data[c - k].append(diag[ix]) return out
>>> lil_diags([[1,2,3],[4,5],[6]],[0,1,2],(3,3)).todense() matrix([[ 1., 4., 6.], [ 0., 2., 5.], [ 0., 0., 3.]]) """ offsets_unsorted = list(offsets) diags_unsorted = list(diags) if len(diags) != len(offsets): raise ValueError("Number of diagonals provided should " "agree with offsets.") sort_indices = np.argsort(offsets_unsorted) diags = [diags_unsorted[k] for k in sort_indices] offsets = [offsets_unsorted[k] for k in sort_indices] for i,k in enumerate(offsets): if len(diags[i]) < m-abs(k): raise ValueError("Not enough values specified to fill " "diagonal %s." % k) out = lil_matrix((m,n),dtype=dtype) from itertools import izip for k,diag in izip(offsets,diags): for ix,c in enumerate(xrange(np.clip(k,0,n),np.clip(m+k,0,n))): out.rows[c-k].append(c) out.data[c-k].append(diag[ix]) return out