#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:                  |  
------------------------------+---------------------------------------------

Comment(by rossk):

 Applied the following patches (in the order below).[[BR]]

 trac_8276-MatrixSpace_one-fh.patch[[BR]]

 trac_8276-fix_sagelib-fh.patch[[BR]]

 trac_8276-fix_zero_matrix_creation-fh.patch[[BR]]

 trac_8294-matrix_2x2_copy_mutability_fix-fh.patch[[BR]]

 *Almost* everything seems straightforward and correct but... Seems that
 MM(0) which has a "mutable == True" property/value should be updatable but
 the example "MM(0)[1,2] = 3" doesnt update MM(0) (see example below) Is
 that right?[[BR]]

 {{{
 # prepatch responses
 sage: sage: MM = MatrixSpace(ZZ, 3,3)
 sage: sage: MM(0).is_mutable()
 True
 sage: sage: MM.zero_matrix().is_mutable()
 True
 sage: sage: MM(1).is_mutable()
 True
 sage: sage: MM.identity_matrix().is_mutable()
 True
 sage: sage: timeit("MM(0)")
 625 loops, best of 3: 51.1 µs per loop
 sage: sage: timeit("copy(MM.zero_matrix())")
 625 loops, best of 3: 19.3 µs per loop
 sage: sage: timeit("MM(1)")
 625 loops, best of 3: 65 µs per loop
 sage: sage: timeit("copy(MM.identity_matrix())")
 625 loops, best of 3: 60.7 µs per loop

 # postpatch responses
 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
 sage: timeit("MM(0)")
 625 loops, best of 3: 35.9 µs per loop
 sage: timeit("copy(MM.zero_matrix())")
 625 loops, best of 3: 29.3 µs per loop
 sage: timeit("MM(1)")
 625 loops, best of 3: 46.1 µs per loop
 sage: timeit("copy(MM.identity_matrix())")
 625 loops, best of 3: 29.9 µs per loop

 sage: MM(0)
 [0 0 0]
 [0 0 0]
 [0 0 0]

 sage: MM(0)[1,2]=3

 # this behavior is the same before and after patches were applied
 # is this right? isnt MM(0) mutable and should be = 3 at row/col=[1,2]?
 sage: MM(0)
 [0 0 0]
 [0 0 0]
 [0 0 0]

 sage: MM.zero_matrix()[1,2]=3
 ---------------------------------------------------------------------------
 ValueError                                Traceback (most recent call
 last)
 
/mnt/usb1/scratch/rossk/sage-4.3.3.alpha1-sage.math.washington.edu-x86_64-Linux/<ipython
 console> in <module>()
 
/mnt/usb1/scratch/rossk/sage-4.3.3.alpha1-sage.math.washington.edu-x86_64-Linux/local/lib/python2.6
 /site-packages/sage/matrix/matrix0.so in
 sage.matrix.matrix0.Matrix.__setitem__ (sage/matrix/matrix0.c:5496)()
 
/mnt/usb1/scratch/rossk/sage-4.3.3.alpha1-sage.math.washington.edu-x86_64-Linux/local/lib/python2.6
 /site-packages/sage/matrix/matrix0.so in
 sage.matrix.matrix0.Matrix.check_mutability (sage/matrix/matrix0.c:3686)()
 ValueError: matrix is immutable; please change a copy instead (i.e., use
 copy(M) to change a copy of M).

 # a copy is mutable
 sage: a=copy(MM.zero_matrix())
 sage: a[1,2]=3
 sage: a
 [0 0 0]
 [0 0 3]
 [0 0 0]

 # here, alias_zero is pointing to same object hence it is also immutable
 and correctly crashes also
 sage: alias_zero = MM.zero_matrix()
 sage: alias_zero[1,2]=3
 ---------------------------------------------------------------------------
 ValueError                                Traceback (most recent call
 last)

 
/mnt/usb1/scratch/rossk/sage-4.3.3.alpha1-sage.math.washington.edu-x86_64-Linux/<ipython
 console> in <module>()
 
/mnt/usb1/scratch/rossk/sage-4.3.3.alpha1-sage.math.washington.edu-x86_64-Linux/local/lib/python2.6
 /site-packages/sage/matrix/matrix0.so in
 sage.matrix.matrix0.Matrix.__setitem__ (sage/matrix/matrix0.c:5496)()
 
/mnt/usb1/scratch/rossk/sage-4.3.3.alpha1-sage.math.washington.edu-x86_64-Linux/local/lib/python2.6
 /site-packages/sage/matrix/matrix0.so in
 sage.matrix.matrix0.Matrix.check_mutability (sage/matrix/matrix0.c:3686)()
 ValueError: matrix is immutable; please change a copy instead (i.e., use
 copy(M) to change a copy of M).

 }}}

-- 
Ticket URL: <http://trac.sagemath.org/sage_trac/ticket/8276#comment:12>
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.

Reply via email to