Ticket #9541: trac_9451-part2-flint.patch

File trac_9451-part2-flint.patch, 10.1 KB (added by spancratz, 7 years ago)

Including basic support for flint

  • module_list.py

    # HG changeset patch
    # User Sebastian Pancratz <sage@pancratz.org>
    # Date 1279539143 -7200
    # Node ID b4b10bca7f2992873aadb5f6347050120ef94810
    # Parent  f282099ab07d1acaee13ccfb1696a8c5f64fd3c4
    trac 9451:  basic support for using flint
    
    diff -r f282099ab07d -r b4b10bca7f29 module_list.py
    a b  
    12061206              sources = ['sage/rings/number_field/number_field_element_generic.pyx']),
    12071207   
    12081208    Extension('sage.rings.number_field.number_field_element_flint',
    1209               sources = ['sage/rings/number_field/number_field_element_flint.pyx'],
    1210               libraries=['flint']),
     1209              sources = ['sage/rings/number_field/number_field_element_flint.pyx', "sage/libs/flint/fmpq_poly.c"],
     1210              include_dirs = [SAGE_ROOT+'/local/include/FLINT/', SAGE_ROOT+'/devel/sage/sage/libs/flint'],
     1211              extra_compile_args=["-std=c99", "-D_XPG6"],
     1212              libraries=['flint'],
     1213              depends = [SAGE_ROOT + "/local/include/FLINT/flint.h"]),
    12111214
    12121215    Extension('sage.rings.number_field.number_field_element_libsingular',
    12131216              sources = ['sage/rings/number_field/number_field_element_libsingular.pyx']),
  • sage/libs/flint/fmpq_poly.pxd

    diff -r f282099ab07d -r b4b10bca7f29 sage/libs/flint/fmpq_poly.pxd
    a b  
    11#include "fmpz_poly.pxi"
    22#include "fmpz.pxi"
    33
     4cdef extern from "gmp.h":
     5    ctypedef void * mpq_t
     6
    47cdef extern from "fmpq_poly.h":
    5     ctypedef void* fmpz_t
    6     ctypedef void* fmpz_poly_p
     8    ctypedef void * fmpz_t
     9    ctypedef void * fmpz_poly_p
    710    struct fmpq_poly:
    811        fmpz_poly_p num
    912        fmpz_t den
     
    1215    ctypedef fmpq_poly_struct fmpq_poly_t[1]
    1316
    1417    void fmpq_poly_init(fmpq_poly_t)
     18    void fmpq_poly_clear(fmpq_poly_t)
     19   
     20    long fmpq_poly_degree(fmpq_poly_t)
     21    unsigned long fmpq_poly_length(fmpq_poly_t)
     22   
     23    void fmpq_poly_set(fmpq_poly_t, fmpq_poly_t)
     24    void fmpq_poly_set_si(fmpq_poly_t, long)
     25    void fmpq_poly_neg(fmpq_poly_t, fmpq_poly_t)
     26   
    1527    void fmpq_poly_from_string(fmpq_poly_t, char*)
    16     char* fmpq_poly_to_string_pretty(fmpq_poly_t, char*)
     28    char * fmpq_poly_to_string_pretty(fmpq_poly_t, char*)
     29   
     30    int fmpq_poly_equal(fmpq_poly_t, fmpq_poly_t)
     31    int fmpq_poly_cmp(fmpq_poly_t, fmpq_poly_t)
     32    int fmpq_poly_is_zero(fmpq_poly_t)
     33   
     34    void fmpq_poly_get_coeff_mpq(mpq_t, fmpq_poly_t, unsigned long)
     35    void fmpq_poly_set_coeff_si(fmpq_poly_t, unsigned long, long)
     36   
     37    void fmpq_poly_add(fmpq_poly_t, fmpq_poly_t, fmpq_poly_t)
     38    void fmpq_poly_sub(fmpq_poly_t, fmpq_poly_t, fmpq_poly_t)
     39   
    1740    void fmpq_poly_mul(fmpq_poly_t, fmpq_poly_t, fmpq_poly_t)
    18     void fmpq_poly_clear(fmpq_poly_t)
     41    void fmpq_poly_power(fmpq_poly_t, fmpq_poly_t, unsigned long)
     42   
     43    void fmpq_poly_divrem(fmpq_poly_t, fmpq_poly_t, fmpq_poly_t, fmpq_poly_t)
     44    void fmpq_poly_floordiv(fmpq_poly_t, fmpq_poly_t, fmpq_poly_t)
     45    void fmpq_poly_mod(fmpq_poly_t, fmpq_poly_t, fmpq_poly_t)
     46   
     47    void fmpq_poly_gcd(fmpq_poly_t, fmpq_poly_t, fmpq_poly_t)
     48    void fmpq_poly_xgcd(fmpq_poly_t, fmpq_poly_t, fmpq_poly_t, fmpq_poly_t, fmpq_poly_t)
     49   
     50    void fmpq_poly_getslice(fmpq_poly_t, fmpq_poly_t, long, long)
     51
  • sage/rings/number_field/number_field.py

    diff -r f282099ab07d -r b4b10bca7f29 sage/rings/number_field/number_field.py
    a b  
    47384738        """
    47394739        Function to initialize an absolute number field.
    47404740        """
     4741        import number_field_element_flint
    47414742        NumberField_generic.__init__(self, polynomial, name, latex_name, check, embedding)
    4742         self._element_class = number_field_element_generic.NumberFieldElement_generic
     4743        self._element_class = number_field_element_flint.NumberFieldElement_flint
    47434744        self._zero_element = self(0)
    47444745        self._one_element =  self(1)
    47454746
  • new file sage/rings/number_field/number_field_element_flint.pxd

    diff -r f282099ab07d -r b4b10bca7f29 sage/rings/number_field/number_field_element_flint.pxd
    - +  
     1###############################################################################
     2#       Copyright (C) 2004, 2007, 2010 William Stein <wstein@gmail.com>
     3#
     4#  Distributed under the terms of the GNU General Public License (GPL)
     5#
     6#    This code is distributed in the hope that it will be useful,
     7#    but WITHOUT ANY WARRANTY; without even the implied warranty of
     8#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     9#    General Public License for more details.
     10#
     11#  The full text of the GPL is available at:
     12#
     13#                  http://www.gnu.org/licenses/
     14###############################################################################
     15
     16include "../../ext/stdsage.pxi"
     17include "../../libs/flint/fmpq_poly.pxd"
     18
     19from sage.libs.all import pari_gen
     20from sage.rings.fraction_field_element import FractionFieldElement
     21from sage.rings.integer cimport Integer
     22from sage.rings.rational cimport Rational
     23from sage.rings.polynomial.polynomial_element cimport Polynomial
     24from sage.structure.element cimport RingElement, ModuleElement, Element
     25
     26from number_field_element_base cimport NumberFieldElement_absolute
     27
     28cdef class NumberFieldElement_flint(NumberFieldElement_absolute):
     29
     30    cdef fmpq_poly_t _f
     31    cdef fmpq_poly_t _m
     32
  • sage/rings/number_field/number_field_element_flint.pyx

    diff -r f282099ab07d -r b4b10bca7f29 sage/rings/number_field/number_field_element_flint.pyx
    a b  
    1 ###############################################################################
     1##############################################################################
    22#       Copyright (C) 2004, 2007, 2010 William Stein <wstein@gmail.com>
    33#
    44#  Distributed under the terms of the GNU General Public License (GPL)
     
    1313#                  http://www.gnu.org/licenses/
    1414###############################################################################
    1515
     16cdef inline fmpq_poly_pow(fmpq_poly_t f, fmpq_poly_t g, unsigned long n):
     17    fmpq_poly_power(f, g, n)
     18
     19cdef class NumberFieldElement_flint(NumberFieldElement_absolute):
     20
     21    def __init__(self, parent, f): # FIXME
     22        self._parent = parent
     23        g = parent.defining_polynomial()
     24        f = g.parent()(f)
     25        s = str(f.degree()+1)+ '  ' + ' '.join([str(x) for x in f.list()])
     26        t = str(g.degree()+1)+ '  ' + ' '.join([str(x) for x in g.list()])
     27        fmpq_poly_init(self._f)
     28        fmpq_poly_from_string(self._f, s)
     29        fmpq_poly_init(self._m)
     30        fmpq_poly_from_string(self._m, t)
     31        #self._m = <fmpq_poly_struct *> self._parent.__fmpq_polynomial
     32
     33    cdef _new(self): # FIXME
     34        cdef NumberFieldElement_flint res = PY_NEW(NumberFieldElement_flint)
     35        res._parent = self._parent
     36        fmpq_poly_init(res._f)
     37        fmpq_poly_init(res._m)
     38        fmpq_poly_set(res._m, self._m)
     39        #res._m = self._m
     40        return res
     41
     42    def __dealloc__(self):
     43        fmpq_poly_clear(self._f)
     44        fmpq_poly_clear(self._m) # FIXME
     45
     46    cdef void _randomize(self, num_bound, den_bound, distribution):
     47        # TODO
     48        pass
     49
     50    def __getitem__(self, long n):
     51        cdef Rational res = PY_NEW(Rational)
     52        if 0 <= n and n < fmpq_poly_length(self._f):
     53            fmpq_poly_get_coeff_mpq(res.value, self._f, n)
     54        return res
     55
     56    def __getslice__(self, long i, long j):
     57        cdef NumberFieldElement_flint res = self._new()
     58        fmpq_poly_getslice(res._f, self._f, i, j)
     59        return res
     60
     61    def polynomial(self): # FIXME
     62        from sage.rings.rational_field import QQ
     63        from sage.rings.polynomial.polynomial_ring import polygen
     64        t = polygen(QQ)
     65        cdef unsigned long i
     66        cdef unsigned long n = fmpq_poly_length(self._f)
     67        res = self[0]
     68        for i in xrange(1, n):
     69            res = res + self[i] * t**i
     70        return res
     71
     72    cpdef RingElement _mul_(left, RingElement right):
     73        pass
     74        cdef NumberFieldElement_flint res = left._new()
     75        fmpq_poly_mul(res._f, left._f, (<NumberFieldElement_flint> right)._f)
     76        fmpq_poly_mod(res._f, res._f, left._m)
     77        return res
     78
     79    cpdef ModuleElement _add_(left, ModuleElement right):
     80        cdef NumberFieldElement_flint res = left._new()
     81        fmpq_poly_add(res._f, left._f, (<NumberFieldElement_flint> right)._f)
     82        return res
     83
     84    cpdef ModuleElement _sub_(left, ModuleElement right):
     85        cdef NumberFieldElement_flint res = left._new()
     86        fmpq_poly_sub(res._f, left._f, (<NumberFieldElement_flint> right)._f)
     87        return res
     88
     89    cpdef RingElement _div_(left, RingElement right):
     90        return left * (~right) # FIXME
     91
     92    def __invert__(self):
     93        cdef NumberFieldElement_flint u
     94        cdef fmpq_poly_t d, v
     95        if fmpq_poly_is_zero(self._f):
     96            raise ValueError, "cannot invert zero"
     97        u = self._new()
     98        fmpq_poly_init(d)
     99        fmpq_poly_init(v)
     100        fmpq_poly_xgcd(d, u._f, v, self._f, self._m)
     101        fmpq_poly_mod(u._f, u._f, self._m)
     102        fmpq_poly_clear(d)
     103        fmpq_poly_clear(v)
     104        return u
     105
     106    def __richcmp__(left, right, int op):
     107        return (<Element>left)._richcmp(right, op)
     108
     109    cdef int _cmp_c_impl(left, Element right) except -2:
     110        return fmpq_poly_cmp(left._f, (<NumberFieldElement_flint> right)._f)
     111
     112    def _integer_(self, Z=None):
     113        return Z(self)
     114
     115    def _rational_(self):
     116        cdef Rational res
     117        cdef unsigned long len = fmpq_poly_length(self._f)
     118        if len == 0:
     119            res = PY_NEW(Rational)
     120            return res
     121        elif len == 1:
     122            res = PY_NEW(Rational)
     123            fmpq_poly_get_coeff_mpq(<mpq_t>res.value, self._f, 0)
     124            return res
     125        else:
     126            raise TypeError, "cannot coerce nonconstant polynomial"
     127