Ticket #10319: trac_10319_sparse_echelon_fix.patch

File trac_10319_sparse_echelon_fix.patch, 2.1 KB (added by Johan Bosman, 11 years ago)
  • sage/matrix/matrix_rational_sparse.pyx

    # HG changeset patch
    # User Johan Bosman <johan.g.bosman@gmail.com>
    # Date 1302450250 -3600
    # Node ID 98e70ab3988fcab24afa55fcb9cd891e65a3dc66
    # Parent  361a4ad7d52c69b64ae2e658ffd0820af0d87e93
    Trac 10319: fix segfault echelonising sparse matrices
    
    diff -r 361a4ad7d52c -r 98e70ab3988f sage/matrix/matrix_rational_sparse.pyx
    a b  
    488488            [      0       1       0  -5/157]
    489489            [      0       0       1 238/157]
    490490            [      0       0       0       0]
     491       
     492        Trac #10319 has been fixed:
     493           
     494            sage: m = Matrix(QQ, [1], sparse=True); m.echelonize()
     495            sage: m = Matrix(QQ, [1], sparse=True); m.echelonize(); m
     496            [1]
    491497        """
    492498                 
    493499        x = self.fetch('in_echelon_form')
     
    537543
    538544    # Multimodular echelonization algorithms
    539545    def _echelonize_multimodular(self, height_guess=None, proof=True, **kwds):
     546        cdef Py_ssize_t i, j
    540547        cdef Matrix_rational_sparse E
     548        cdef mpq_vector* v
     549        cdef mpq_vector* w
    541550        E = self._echelon_form_multimodular(height_guess, proof=proof, **kwds)
    542551        # Get rid of self's data
    543552        self._dealloc()
    544        
    545         # Change self's data to point to E's.
    546         self._matrix = E._matrix
    547 
    548         # Make sure that E's destructor doesn't delete self's data.
    549         E._matrix = NULL
    550         E._initialized = False
     553        # Copy E's data to self's data.
     554        self._matrix = <mpq_vector*> sage_malloc(E._nrows * sizeof(mpq_vector))
     555        if self._matrix == NULL:
     556            raise MemoryError, "error allocating sparse matrix"
     557        for i from 0 <= i < E._nrows:
     558            v = &self._matrix[i]
     559            w = &E._matrix[i]
     560            mpq_vector_init(v, E._ncols, w.num_nonzero)
     561            for j from 0 <= j < w.num_nonzero:
     562                mpq_set(v.entries[j], w.entries[j])
     563                v.positions[j] = w.positions[j]
    551564        return E.pivots()
    552565
    553566