[SciPy-user] Type handling of matrices

Pearu Peterson pearu at scipy.org
Thu Nov 11 03:49:18 CST 2004

On Thu, 11 Nov 2004, Robert Kern wrote:

> Fernando Perez wrote:
>> How about just using functions for everything?  I'd really hate to have to 
>> remember that if A is sparse, A.is<foo> works, but if it's dense I need to 
>> instead use is<foo>(A).
> The big benefit for a property (not method) on sparse matrices is that one 
> can set A.ishermitian on initialization and all get/set operations will obey 
> that property.
> A = sparse.dok_matrix()
> A.ishermitian = True
> A[1,2] = 1.
> assert A[2,1] == 1.

The problem with this approach is that it is not safe. It is too easy
to make the following error:

   A.ishermitian = True
   A[1,2] = 1.
   A[2,1] = 2.

I would introduce sethermitian(flag=True) method that for an empty matrix
sets private A._ishermitian and for a non-empty matrix it first checks
if A can be hermitian (if not, it raises an exception).

Hmm, should traits package or Python property features be used here?

> Some sparse formats may be able to hold the property information and use it 
> effectively without duplicating the individual elements themselves.

I agree.

> There should definitely be a function interface for querying these properties 
> that will work for both dense and sparse matrices.



More information about the SciPy-user mailing list