[SciPy-user] Type handling of matrices
Robert Kern
rkern at ucsd.edu
Thu Nov 11 04:31:05 CST 2004
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.
> Hmm, should traits package or Python property features be used here?
traits may be overkill, but properties certainly can be used here.
--
Robert Kern
rkern at ucsd.edu
"In the fields of hell where the grass grows high
Are the graves of dreams allowed to die."
-- Richard Harter
More information about the SciPy-user
mailing list