Hi Jason,

>> However, I really wonder whether this fix is necessary. I understand the
>> the bug pointed out is a bad bug, but I think that when I call
>> zero_matrix() I will usually quickly modify the returned matrix. Are
>> there many useful situations where it is important that M =
>> zero_matrix() return very fast and where we _aren't_ going to modify M?
>>
>
> If M is a matrix space, will M(0) return an immutable matrix with this 
> change?

Here is the current behavior with the patch applied:

sage: MM = MatrixSpace(ZZ, 3,3)
sage: MM(0).is_mutable()
True
sage: MM.zero_matrix().is_mutable()
False
sage: MM(1).is_mutable()
True
sage: MM.identity_matrix().is_mutable()
False

However, calling MM(0) or MM(1) is a bad idea:

sage: timeit("MM(0)")
625 loops, best of 3: 72.4 µs per loop
sage: timeit("copy(MM.zero_matrix())")
625 loops, best of 3: 16 µs per loop

sage: timeit("MM(1)")
625 loops, best of 3: 67.4 µs per loop
age: timeit("copy(MM.identity_matrix())")
625 loops, best of 3: 41.1 µs per loop

Note that, not surprisingly, for sparse matrices copying zero is a bad idea:
sage: MMS = MatrixSpace(ZZ, 3,3, sparse=True)
sage: timeit("MM(0)")
625 loops, best of 3: 75 µs per loop
sage: timeit("copy(MMS.zero_matrix())")
625 loops, best of 3: 103 µs per loop

sage: timeit("MMS(1)")
625 loops, best of 3: 189 µs per loop
sage: timeit("copy(MMS.identity_matrix())")
625 loops, best of 3: 144 µs per loop


I really think this is a very reasonable behavior. Moreover the performance
difference should be documented. Though I don't know exactly where is the
better place. Any Idea ?

Cheers,

Florent

-- 
To post to this group, send an email to [email protected]
To unsubscribe from this group, send an email to 
[email protected]
For more options, visit this group at http://groups.google.com/group/sage-devel
URL: http://www.sagemath.org

Reply via email to