Ticket #12173: 15-integer_to_mod.patch

File 15-integer_to_mod.patch, 6.8 KB (added by jpflori, 10 years ago)

Faster conversion between integer and mod polys.

  • module_list.py

    # HG changeset patch
    # User Jean-Pierre Flori <jean-pierre.flor@ssi.gouv.fr>
    # Date 1343669787 -7200
    # Node ID 7618bca3828c84134e9a3a0434dd36f4bcdbc4dc
    # Parent  be48f3c58b14641f470a363012cf2a77f5d9c820
    #12173: Faster conversion from interger polynomial to mod polynomial
    
    diff --git a/module_list.py b/module_list.py
    a b  
    13571357   
    13581358    Extension('sage.rings.fraction_field_FpT',
    13591359              sources = ['sage/rings/fraction_field_FpT.pyx'],
     1360              language = 'c++',
    13601361              libraries = ["csage", "flint", "gmp", "gmpxx", "ntl", "zn_poly"],
    13611362              extra_compile_args=["-std=c99", "-D_XPG6"],
    13621363              include_dirs = [SAGE_INC + 'flint/'],
     
    16271628
    16281629    Extension('sage.rings.polynomial.polynomial_zmod_flint',
    16291630              sources = ['sage/rings/polynomial/polynomial_zmod_flint.pyx'],
     1631              language = 'c++',
    16301632              libraries = ["csage", "flint", "gmp", "gmpxx", "ntl", "zn_poly"],
    16311633              extra_compile_args=["-std=c99", "-D_XPG6"],
    16321634              include_dirs = [SAGE_INC + 'flint/'],
  • sage/libs/flint/fmpz_poly.pxi

    diff --git a/sage/libs/flint/fmpz_poly.pxi b/sage/libs/flint/fmpz_poly.pxi
    a b  
    33
    44cdef extern from "flint/fmpz_poly.h":
    55   
    6     ctypedef void* fmpz_poly_t
    7    
     6    ctypedef struct fmpz_poly_struct:
     7        fmpz * coeffs
     8
     9    ctypedef void * fmpz_poly_t
     10
    811    void fmpz_poly_init(fmpz_poly_t poly)
    912    void fmpz_poly_init2(fmpz_poly_t poly, unsigned long alloc)
    1013    void fmpz_poly_realloc(fmpz_poly_t poly, unsigned long alloc)
  • sage/libs/flint/zmod_poly_linkage.pxi

    diff --git a/sage/libs/flint/zmod_poly_linkage.pxi b/sage/libs/flint/zmod_poly_linkage.pxi
    a b  
    11r"""
    2 Linkage for arithmetic with FLINT's zmod_poly_t elements.
     2Linkage for arithmetic with FLINT's nmod_poly_t elements.
    33
    44This file provides the backend for \class{Polynomial_zmod_flint} via
    55templating.
  • sage/rings/polynomial/polynomial_zmod_flint.pxd

    diff --git a/sage/rings/polynomial/polynomial_zmod_flint.pxd b/sage/rings/polynomial/polynomial_zmod_flint.pxd
    a b  
    11from sage.libs.flint.zmod_poly cimport nmod_poly_t, nmod_poly_struct
    22from sage.structure.parent cimport Parent
     3from sage.rings.polynomial.polynomial_integer_dense_flint cimport Polynomial_integer_dense_flint
    34
    45ctypedef nmod_poly_struct celement
    56
     
    78
    89cdef class Polynomial_zmod_flint(Polynomial_template):
    910    cdef Polynomial_template _new(self)
    10     cdef _set_list(self, x)
     11    cdef void _set_list(self, x)
     12    cdef void _set_fmpz_poly(self, Polynomial_integer_dense_flint, bint)
    1113    cpdef _mul_trunc(self, Polynomial_zmod_flint other, length)
    1214    cpdef _mul_trunc_opposite(self, Polynomial_zmod_flint other, length)
    1315    cpdef rational_reconstruct(self, m, n_deg=?, d_deg=?)
  • sage/rings/polynomial/polynomial_zmod_flint.pyx

    diff --git a/sage/rings/polynomial/polynomial_zmod_flint.pyx b/sage/rings/polynomial/polynomial_zmod_flint.pyx
    a b  
    3636from sage.libs.ntl.ntl_lzz_pX import ntl_zz_pX
    3737from sage.structure.factorization import Factorization
    3838from sage.structure.element import coerce_binop, parent
     39from sage.rings.polynomial.polynomial_integer_dense_flint cimport Polynomial_integer_dense_flint
    3940
    4041# We need to define this stuff before including the templating stuff
    4142# to make sure the function get_cparent is found since it is used in
     
    6263    cdef void zn_mod_clear(zn_mod_struct *mod)
    6364    cdef void zn_array_mul(unsigned long* res, unsigned long* op1, size_t n1, unsigned long* op2, size_t n2, zn_mod_struct *mod)
    6465
     66include "../../libs/flint/fmpz.pxi"
     67
     68cdef extern from "flint/fmpz.h":
     69    cdef unsigned long fmpz_get_ui(fmpz_t)
     70    cdef unsigned long fmpz_fdiv_ui(fmpz_t, unsigned long)
     71
     72include "../../libs/flint/fmpz_poly.pxi"
     73
    6574cdef class Polynomial_zmod_flint(Polynomial_template):
    6675    def __init__(self, parent, x=None, check=True, is_gen=False, construct=False):
    6776        """
     
    8695            Polynomial_template.__init__(self, parent, 0, check, is_gen, construct)
    8796            self._set_list(lst)
    8897            return
     98        elif PY_TYPE_CHECK(x, Polynomial_integer_dense_flint):
     99            Polynomial_template.__init__(self, parent, 0, check, is_gen, construct)
     100            self._set_fmpz_poly(x, check)
     101            return
    89102        else:
    90103            if PY_TYPE_CHECK(x, ntl_zz_pX):
    91104                x = x.list()
     
    139152        celement_set_si(&r.x, int(x), get_cparent(P))
    140153        return r
    141154
    142     cdef _set_list(self, x):
     155    cdef void _set_list(self, x):
    143156        """
     157        Set the coefficients of ``self`` from a list of coefficients.
     158
     159        INPUT:
     160
     161        - ``x`` - a list of coefficients
     162
    144163        EXAMPLES::
    145164
    146165            sage: P.<a>=GF(7)[]
     
    164183        nmod_poly_realloc(&self.x, length)
    165184        sig_off()
    166185
     186        sig_on()
    167187        for i from 0 <= i < length:
    168188            nmod_poly_set_coeff_ui(&self.x, i, l_in[i])
     189        sig_off()
     190
     191    cdef void _set_fmpz_poly(self, Polynomial_integer_dense_flint x, bint check):
     192        """
     193        Set the coefficients of ``self`` from the coefficients of a :class:`Polynomial_integer_dense_flint` element.
     194
     195        INPUT:
     196
     197        - ``x`` - a :class:`Polynomial_integer_dense_flint` element
     198
     199        - ``check`` - whether to reduce the coefficients of `x` or not
     200
     201        EXAMPLES::
     202
     203            sage: a = ZZ['x'](range(17))
     204            sage: R = Integers(7)['x']
     205            sage: R(a)
     206            2*x^16 + x^15 + 6*x^13 + 5*x^12 + 4*x^11 + 3*x^10 + 2*x^9 + x^8 + 6*x^6 + 5*x^5 + 4*x^4 + 3*x^3 + 2*x^2 + x
     207
     208        TESTS:
     209
     210        The following test from :trac:`12173` used to be horribly slow::
     211
     212            sage: a = ZZ['x'](range(100000))
     213            sage: R = Integers(3)['x']
     214            sage: R(a)
     215            2*x^99998 + ... + x
     216        """
     217        cdef unsigned long length = fmpz_poly_length(x.__poly)
     218        cdef unsigned long modulus = nmod_poly_modulus(&self.x)
     219        cdef fmpz *coeffs = (<fmpz_poly_struct>(x.__poly[0])).coeffs
     220        cdef int i
     221        if length == 0:
     222            nmod_poly_zero(&self.x)
     223            return
     224
     225        # resize to length of list
     226        sig_on()
     227        nmod_poly_realloc(&self.x, length)
     228        sig_off()
     229
     230        if check:
     231            sig_on()
     232            for i from 0 <= i < length:
     233                nmod_poly_set_coeff_ui(&self.x, i, fmpz_fdiv_ui(&coeffs[i], modulus))
     234            sig_off()
     235            return
     236        else:
     237            sig_on()
     238            for i from 0 <= i < length:
     239                nmod_poly_set_coeff_ui(&self.x, i, fmpz_get_ui(&coeffs[i]))
     240            sig_off()
     241            return
    169242
    170243    def __getitem__(self, i):
    171244        """