#8276: Make the one(), identity_matrix() and zero_matrix() cached and immutable.
------------------------------+---------------------------------------------
Reporter: hivert | Owner: hivert
Type: defect | Status: needs_review
Priority: major | Milestone: sage-4.3.3
Component: linear algebra | Keywords: One mutable.
Author: Florent Hivert | Upstream: N/A
Reviewer: | Merged:
Work_issues: |
------------------------------+---------------------------------------------
Description changed by hivert:
Old description:
> After I found the following bug, it was decided on sage-devel that in a
> {{{MatrixSpace}}} the methods {{{.one()}}}, {{{.identity_matrix()}}} and
> {{{.zero_matrix()}}} should returns a cached immutable matrix. I had to
> update sage's library accordingly.
>
> {{{
> sage: A = MatrixSpace(ZZ, 3)
> sage: A.one()
> [1 0 0]
> [0 1 0]
> [0 0 1]
> sage: A.one()[1,2] = 1
> sage: A.one()
> [1 0 0]
> [0 1 1]
> [0 0 1]
> }}}
>
> So here is now the current behavior:
> {{{
> 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
> }}}
> Note that 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: 15.6 µs per loop
> }}}
> And for identity:
> {{{
> sage: timeit("MM(1)")
> 625 loops, best of 3: 67.4 µs per loop
> sage: timeit("copy(MM.identity_matrix())")
> 625 loops, best of 3: 41.1 µs per loop
> }}}
New description:
After I found the following bug, it was decided on sage-devel that in a
{{{MatrixSpace}}} the methods {{{.one()}}}, {{{.identity_matrix()}}} and
{{{.zero_matrix()}}} should returns a cached immutable matrix. I had to
update sage's library accordingly.
{{{
sage: A = MatrixSpace(ZZ, 3)
sage: A.one()
[1 0 0]
[0 1 0]
[0 0 1]
sage: A.one()[1,2] = 1
sage: A.one()
[1 0 0]
[0 1 1]
[0 0 1]
}}}
So here is now the current behavior:
{{{
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
}}}
Note that calling {{{MM(0)}}} or {{{MM(1)}}} was 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: 15.6 µs per loop
}}}
And for identity:
{{{
sage: timeit("MM(1)")
625 loops, best of 3: 67.4 µs per loop
sage: timeit("copy(MM.identity_matrix())")
625 loops, best of 3: 41.1 µs per loop
}}}
I took the chance to optimize those. The extra cost of calling {{{MM()}}}
or {{{M(0)}}} instead of {{{copy(MM.zero_matrix())}}} is now very small.
--
--
Ticket URL: <http://trac.sagemath.org/sage_trac/ticket/8276#comment:9>
Sage <http://www.sagemath.org>
Sage: Creating a Viable Open Source Alternative to Magma, Maple, Mathematica,
and MATLAB
--
You received this message because you are subscribed to the Google Groups
"sage-trac" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/sage-trac?hl=en.