[SciPy-User] symmetric lil_matrix

David Warde-Farley dwf at cs.toronto.edu
Sat Aug 29 22:32:20 EDT 2009


On 29-Aug-09, at 6:21 PM, T J wrote:

> In constructing a scipy.sparse.lil_matrix, is there any built-in
> functionality to support symmetric matrices?

No, but such functionality would be easy to add by subclassing.

import scipy.sparse

class sym_lil_matrix(scipy.sparse.lil_matrix):
	def __setitem__(self, index, x):
		super(sym_lil_matrix, self).__setitem__(index, x)
		super(sym_lil_matrix, self).__setitem__(index[::-1], x)


You could add a custom constructor but it's unclear what the semantics  
ought to be there.

> Was wondering if m[2,1] could be automatically added if one declared
> the lil_matrix as symmetric.  Also, the data structure says it has 2
> stored elements.  Is it actually storing two elements?

Yes.

> My hope is that additional "sparseness" could be gained if it is known
> that the matrix is symmetric.

That would require considerably more thought (in order to implement  
all of the interface that needs to be aware of this duplication) but  
is certainly possible. It hardly seems worth it unless your matrices  
are positively gigantic and can't fit into memory.

David



More information about the SciPy-User mailing list