[SciPy-user] Type handling of matrices

Pearu Peterson pearu at scipy.org
Thu Nov 11 04:48:09 CST 2004



On Thu, 11 Nov 2004, Robert Kern wrote:

> Pearu Peterson wrote:
>> 
>> 
>> 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).
>
> Actually, now that I think some more on it, I'd probably make them keyword 
> arguments in __init__ and the corresponding properties read-only.
>
> A[1,2] = 1.
> A[2,1] = 2.
> A.ishermitian = True
>
> This should raise an error since I can't think of a reasonable action to 
> perform.

I think I like the idea of setting attributes in __init__ very much.
This actually simplifies the code: no need to test if a matrix still has 
a specific property everytime the matrix is changed.

Btw, sparse.dok_matrix should also have N,M optional arguments (like 
other spmatrix derivatives) to fix the shape. Otherwise, it is rather
difficult to perform matrix-matrix multiplication, for exmaple, with 
dok_matrix'ses if the shape depends on its contents.

Pearu



More information about the SciPy-user mailing list