Ticket #12173: slice.patch

File slice.patch, 1.8 KB (added by jpflori, 10 years ago)

Make getitem for slices faster

  • sage/rings/polynomial/polynomial_rational_flint.pyx

    # HG changeset patch
    # User Jean-Pierre Flori <jean-pierre.flor@ssi.gouv.fr>
    # Date 1338995192 -7200
    # Node ID 0e54bdd5e7ffdb6a1af41950eed4031e7a1e7353
    # Parent  cf7dd793632be4de03ca3621bbe4da44aa29706d
    #12173: Make __getitem__ for slices faster
    
    diff --git a/sage/rings/polynomial/polynomial_rational_flint.pyx b/sage/rings/polynomial/polynomial_rational_flint.pyx
    a b  
    6161        sage: g = 2/3 + t^2
    6262        sage: _ = f * g      # indirect doctest
    6363    """
     64    # Trac #12173: check that the degree is greater than 1000 before computing
     65    # the max limb size
    6466    return fmpq_poly_length(op) > 0 and \
    65        (_fmpz_vec_max_limbs(fmpq_poly_numref(op), fmpq_poly_length(op)) > 1 \
    66                                                  or fmpq_poly_degree(op) > 1000)
     67       (fmpq_poly_degree(op) > 1000 or
     68        _fmpz_vec_max_limbs(fmpq_poly_numref(op), fmpq_poly_length(op)) > 1)
    6769
    6870cdef class Polynomial_rational_flint(Polynomial):
    6971    """
     
    189191
    190192            sage: f = ZZ['x']([1..10^6])
    191193            sage: g = f.change_ring(QQ)
    192             sage: g[:10]  # long time (5s on sage.math, 2012)
     194            sage: g[:10] # not long anymore thanks to trac #12173
    193195            10*x^9 + 9*x^8 + 8*x^7 + 7*x^6 + 6*x^5 + 5*x^4 + 4*x^3 + 3*x^2 + 2*x + 1           
    194196        """
    195197        cdef long deg
     
    390392        cdef Polynomial_rational_flint res = self._new()
    391393        cdef bint do_sig = _do_sig(self.__poly)
    392394        if isinstance(n, slice):
    393             start, stop, step = n.indices(len(list(self)))
     395            start, stop, step = n.indices(self.degree() + 1)
    394396            if do_sig: sig_on()
    395397            fmpq_poly_get_slice(res.__poly, self.__poly, start, stop)
    396398            if do_sig: sig_off()