Ticket #11596: trac_11596-immutable.patch

File trac_11596-immutable.patch, 3.4 KB (added by jhpalmieri, 10 years ago)

a different take

  • sage/matrix/matrix2.pyx

    # HG changeset patch
    # User J. H. Palmieri <palmieri@math.washington.edu>
    # Date 1322961342 28800
    # Node ID f86080053208392cb8521655d17e03851ebdabc1
    # Parent  df5620969fbddef0cf191407d046c2ed522d6a49
    #11596: make sure eigenvalues and other cached data are immutable
    
    diff --git a/sage/matrix/matrix2.pyx b/sage/matrix/matrix2.pyx
    a b cdef class Matrix(matrix1.Matrix): 
    18921892            Traceback (most recent call last):
    18931893            ...
    18941894            TypeError: Hessenbergize only possible for matrices over a field
     1895
     1896        Test that the Hessenberg form matrix is immutable (#11596)::
     1897
     1898            sage: h[1,2] = 3
     1899            Traceback (most recent call last):
     1900            ...
     1901            ValueError: matrix is immutable; please change a copy instead...
    18951902        """
    18961903        X = self.fetch('hessenberg_form')
    18971904        if not X is None:
    cdef class Matrix(matrix1.Matrix): 
    19081915            H = self.__copy__()
    19091916            H.hessenbergize()
    19101917        #end if
     1918        H.set_immutable()
    19111919        self.cache('hessenberg_form', H)
    19121920        return H
    19131921
    cdef class Matrix(matrix1.Matrix): 
    47634771                        space = (ev.parent())**self.nrows()
    47644772                        evec_list = [(space)([m(i) for i in v]) for v in WB]
    47654773                        V.append((ev, space.span_of_basis(evec_list, already_echelonized=True), e))
    4766         V = Sequence(V, cr=True, check=False)
     4774        V = Sequence(V, cr=True, check=False, immutable=True)
    47674775        self.cache(key, V)
    47684776        if algebraic_multiplicity:
    47694777            return V
    cdef class Matrix(matrix1.Matrix): 
    50445052            sage: M.eigenvalues(extend=False)
    50455053            [2]
    50465054
     5055        Test that cached eigenvalues are immutable (#11596)::
     5056
     5057            sage: A = matrix(QQ, 2, 2, range(4))
     5058            sage: ev = A.eigenvalues(); ev
     5059            [-0.5615528128088302?, 3.561552812808830?]
     5060            sage: ev[0]=300
     5061            Traceback (most recent call last):
     5062            ...
     5063            ValueError: object is immutable; please change a copy instead.
    50475064        """
    50485065        x = self.fetch('eigenvalues')
    50495066        if not x is None:
    cdef class Matrix(matrix1.Matrix): 
    50765093                        raise NotImplementedError, "eigenvalues() is not implemented for matrices with eigenvalues that are not in the fraction field of the base ring or in QQbar"
    50775094                    V.extend(alpha*e)
    50785095            i+=1
    5079         V = Sequence(V)
     5096        V = Sequence(V, immutable=True)
    50805097        if extend:
    50815098            self.cache('eigenvalues', V)
    50825099        if not extend:
    cdef class Matrix(matrix1.Matrix): 
    73277344            sage: M * N
    73287345            [7363/1092         0]
    73297346            [        0 7363/1092]
    7330        
     7347
     7348        Test that the cached adjoint matrix is immutable (#11596)::
     7349
     7350            sage: N[1,1] = 12
     7351            Traceback (most recent call last):
     7352            ...
     7353            ValueError: matrix is immutable; please change a copy instead...
     7354
    73317355        AUTHORS:
    7332        
     7356
    73337357        - Unknown: No author specified in the file from 2009-06-25
    73347358        - Sebastian Pancratz (2009-06-25): Reflecting the change that
    73357359          ``_adjoint`` is now implemented in this class
    cdef class Matrix(matrix1.Matrix): 
    73437367            return X
    73447368       
    73457369        X = self._adjoint()
     7370        X.set_immutable()
    73467371        self.cache('adjoint', X)
    73477372        return X
    73487373