Ticket #9541: trac_9541-part8-libsingular-first_steps.patch

File trac_9541-part8-libsingular-first_steps.patch, 14.5 KB (added by was, 7 years ago)
  • module_list.py

    # HG changeset patch
    # User William Stein <wstein@gmail.com>
    # Date 1279763013 -7200
    # Node ID 6f449c1dcdaad8f3c26a286205d2fa6308feb27d
    # Parent  53c4551ed34f4ee655eb78c0b9c87fb3c45a9d51
    [mq]: trac_9541-part8
    
    diff -r 53c4551ed34f -r 6f449c1dcdaa module_list.py
    a b  
    12041204              sources = ['sage/rings/number_field/number_field_base.pyx'],
    12051205              include_dirs = [SAGE_ROOT+'/local/include/FLINT/', SAGE_ROOT+'/devel/sage/sage/libs/flint'],
    12061206              extra_compile_args=["-std=c99", "-D_XPG6"],
    1207               ),
     1207              libraries=['flint'],
     1208              depends = [SAGE_ROOT + "/local/include/FLINT/flint.h"]),
    12081209
    12091210    Extension('sage.rings.number_field.implementation',
    12101211              sources = ['sage/rings/number_field/implementation.pyx' , "sage/libs/flint/fmpq_poly.c"],
     
    12421243              depends = [SAGE_ROOT + "/local/include/FLINT/flint.h"]),
    12431244
    12441245    Extension('sage.rings.number_field.number_field_element_libsingular',
    1245               sources = ['sage/rings/number_field/number_field_element_libsingular.pyx']),
     1246              sources = ['sage/rings/number_field/number_field_element_libsingular.pyx'],
     1247              include_dirs = [SAGE_ROOT+'/local/include/FLINT/', SAGE_ROOT+'/devel/sage/sage/libs/flint',
     1248                              SAGE_ROOT +'/local/include/singular'],
     1249              libraries=['flint', 'singular'],
     1250              depends = [SAGE_ROOT + "/local/include/FLINT/flint.h"],
     1251              language = 'c++'
     1252              ),
    12461253   
    12471254    Extension('sage.rings.number_field.number_field_element_quadratic',
    12481255              sources = ['sage/rings/number_field/number_field_element_quadratic.pyx'],
  • sage/rings/number_field/implementation.pxd

    diff -r 53c4551ed34f -r 6f449c1dcdaa sage/rings/number_field/implementation.pxd
    a b  
    1212
    1313# FLINT-based implementation
    1414include "../../libs/flint/fmpq_poly.pxd"
     15
    1516cdef class ArithmeticImplementation_flint(ArithmeticImplementation):
    1617    cdef fmpq_poly_t modulus
    1718
    18 # Singular-based implementation
    19 cdef class ArithmeticImplementation_singular(ArithmeticImplementation):
    20     pass
     19cdef class ArithmeticImplementation_libsingular(ArithmeticImplementation):
     20    cdef object ring
     21    cdef object ideal
     22    cdef object groebner_strategy
    2123
    2224cdef class ArithmeticImplementation_quadratic(ArithmeticImplementation):
    2325    pass
  • sage/rings/number_field/implementation.pyx

    diff -r 53c4551ed34f -r 6f449c1dcdaa sage/rings/number_field/implementation.pyx
    a b  
    178178        """
    179179        return True
    180180
    181 cdef class ArithmeticImplementation_singular(ArithmeticImplementation):
    182     def __init__(self):
     181cdef class ArithmeticImplementation_libsingular(ArithmeticImplementation):
     182    def __init__(self, ring, ideal):
    183183        """
    184184        EXAMPLES::
    185185
    186             sage: sage.rings.number_field.implementation.ArithmeticImplementation_singular()
     186            sage: sage.rings.number_field.implementation.ArithmeticImplementation_libsingular()
    187187            Arithmetic implementation 'singular'
    188188        """
    189189        ArithmeticImplementation.__init__(self, 'singular')
     190        self.ring = ring
     191        self.ideal = ideal
     192        self.groebner_strategy = ideal._groebner_strategy()
    190193
    191194cdef class ArithmeticImplementation_quadratic(ArithmeticImplementation):
    192195    def __init__(self):
  • sage/rings/number_field/number_field.py

    diff -r 53c4551ed34f -r 6f449c1dcdaa sage/rings/number_field/number_field.py
    a b  
    643643    perfect square.
    644644   
    645645    INPUT:
    646    
    647    
    648     -  ``D`` - a rational number
    649    
    650     -  ``names`` - variable name
    651    
    652     -  ``check`` - bool (default: True)
    653    
    654     -  ``embedding`` - bool or square root of D in an
    655        ambient field (default: True)
    656    
    657     - ``latex_name`` - latex variable name (defalt: \sqrt{D})
    658    
     646        - ``D`` - a rational number
     647        - ``names`` - variable name
     648        - ``check`` - bool (default: True)
     649        - ``embedding`` - bool or square root of D in an
     650          ambient field (default: True)
     651        - ``latex_name`` - latex variable name (defalt: \sqrt{D})
    659652   
    660653    OUTPUT: A number field defined by a quadratic polynomial. Unless
    661654    otherwise specified, it has an embedding into `\RR` or
     
    48114804        """
    48124805        NumberField_generic.__init__(self, polynomial, name, latex_name, check, embedding)
    48134806        if implementation == 'default':
    4814             implementation = 'ntl'
     4807            implementation = DEFAULT_IMPLEMENTATION['absolute']
     4808
     4809        if implementation == 'singular':
     4810            implementation = 'flint'
    48154811
    48164812        if implementation == 'quadratic':
    48174813            self._element_class = number_field_element_quadratic.NumberFieldElement_quadratic
  • sage/rings/number_field/number_field_element_libsingular.pxd

    diff -r 53c4551ed34f -r 6f449c1dcdaa sage/rings/number_field/number_field_element_libsingular.pxd
    a b  
    11from number_field_element cimport NumberFieldElement
    22
     3from sage.libs.singular.decl cimport poly, ring
     4
     5from sage.rings.polynomial.multi_polynomial_libsingular cimport MPolynomialRing_libsingular, MPolynomial_libsingular
     6
     7from sage.libs.singular.groebner_strategy cimport GroebnerStrategy
     8
    39cdef class NumberFieldElement_libsingular(NumberFieldElement):
     10    cdef poly *_poly
     11
    412    cdef _new(self)
     13    cdef ring* ring(self)
     14    cdef MPolynomialRing_libsingular mpoly_ring(self)
     15    cdef GroebnerStrategy groebner_strategy(self)
     16    cdef poly* normal_form(self, poly* p)
     17   
     18   
  • sage/rings/number_field/number_field_element_libsingular.pyx

    diff -r 53c4551ed34f -r 6f449c1dcdaa sage/rings/number_field/number_field_element_libsingular.pyx
    a b  
    1313#                  http://www.gnu.org/licenses/
    1414###############################################################################
    1515
     16cdef extern from "":
     17    int unlikely(int)
     18    int likely(int)
     19
    1620include "../../ext/stdsage.pxi"
    1721
    1822from sage.structure.element cimport RingElement, ModuleElement, Element
     23from number_field_base cimport NumberField
     24
     25from implementation cimport ArithmeticImplementation_libsingular
     26
     27from sage.libs.singular.polynomial cimport singular_polynomial_str
     28
     29from sage.libs.singular.decl cimport p_Copy, currRing, rChangeCurrRing, redNF, redtailBba, p_Delete
     30
     31# singular poly arith
     32from sage.libs.singular.polynomial cimport singular_polynomial_call, singular_polynomial_cmp, singular_polynomial_add, singular_polynomial_sub, singular_polynomial_neg
     33from sage.libs.singular.polynomial cimport singular_polynomial_rmul, singular_polynomial_mul, singular_polynomial_div_coeff, singular_polynomial_pow
     34from sage.libs.singular.polynomial cimport singular_polynomial_str, singular_polynomial_latex, singular_polynomial_str_with_changed_varnames
     35from sage.libs.singular.polynomial cimport singular_polynomial_deg
     36from sage.libs.singular.polynomial cimport singular_polynomial_length_bounded
    1937
    2038cdef class NumberFieldElement_libsingular(NumberFieldElement):
     39
     40    def bench(self):
     41        self._parent
     42       
     43    def bench2(self):
     44        (<NumberField>self._parent)
     45           
     46    def bench3(self):
     47        (<NumberField>self._parent)._implementation
     48
     49    def bench4(self):
     50        cdef ArithmeticImplementation_libsingular A
     51        A = <ArithmeticImplementation_libsingular> (<NumberField>self._parent)._implementation
     52
     53    def bench5(self):
     54        cdef ArithmeticImplementation_libsingular A
     55        A = <ArithmeticImplementation_libsingular> (<NumberField>self._parent)._implementation
     56        cdef MPolynomialRing_libsingular R = <MPolynomialRing_libsingular>(A.ring)
     57
     58    def bench6(self):
     59        self.the_ring()
     60
     61    def bench7(self):
     62        self._new()
     63
     64    def bench8(self):
     65        self.mpoly_ring()
     66
     67    def bench9(self):
     68        self.groebner_strategy()
     69       
    2170    def __init__(self, parent, f):
    2271        self._parent = parent
     72        cdef MPolynomial_libsingular g = self.mpoly_ring().gen(0)
     73        self._poly = p_Copy(g._poly, self.ring())
     74       
     75    cdef inline ring* ring(self):
     76        return self.mpoly_ring()._ring
    2377
    24     cdef _new(self):
     78    cdef inline MPolynomialRing_libsingular mpoly_ring(self):
     79        cdef ArithmeticImplementation_libsingular A
     80        A = <ArithmeticImplementation_libsingular> (<NumberField>self._parent)._implementation
     81        cdef MPolynomialRing_libsingular R = <MPolynomialRing_libsingular>(A.ring)
     82        return R
     83
     84    cdef GroebnerStrategy groebner_strategy(self):
     85        cdef ArithmeticImplementation_libsingular A
     86        A = <ArithmeticImplementation_libsingular> (<NumberField>self._parent)._implementation
     87        return A.groebner_strategy
     88
     89    cdef poly* normal_form(self, poly* p):
     90        if unlikely(self.ring() != currRing):
     91            rChangeCurrRing(self.ring())
     92        cdef int max_ind
     93        cdef poly *_p = redNF(p_Copy(p, self.ring()), max_ind, 0, self.groebner_strategy()._strat)
     94        if likely(_p!=NULL):
     95            _p = redtailBba(_p, max_ind, self.groebner_strategy()._strat)
     96        return _p
     97   
     98    cdef inline _new(self):
    2599        cdef NumberFieldElement_libsingular x = PY_NEW(NumberFieldElement_libsingular)
    26100        x._parent = self._parent
     101        x._poly = NULL
    27102        return x
    28103
    29104    cdef void _randomize(self, num_bound, den_bound, distribution):
     
    32107    def polynomial(self):
    33108        raise NotImplementedError
    34109
    35     cpdef RingElement _mul_(left, RingElement right):
     110    def __repr__(self):
     111        return singular_polynomial_str(self._poly, self.ring())
     112
     113    cpdef RingElement _mul_(left, RingElement r):
     114        cdef NumberFieldElement_libsingular right = r
     115        cdef poly* p
     116        singular_polynomial_mul(&p, left._poly, right._poly, left.ring())
     117        cdef NumberFieldElement_libsingular x = left._new()
     118        x._poly = left.normal_form(p)
     119        if p != NULL: p_Delete(&p, left.ring())
     120        return x
     121   
     122    cpdef ModuleElement _add_(left, ModuleElement r):
     123        cdef NumberFieldElement_libsingular x = left._new(), right = r       
     124        singular_polynomial_add(&x._poly, left._poly, right._poly, left.ring())
     125        return x
     126
     127    cpdef ModuleElement _sub_(left, ModuleElement r):
     128        cdef NumberFieldElement_libsingular x = left._new(), right = r               
     129        singular_polynomial_sub(&x._poly, left._poly, right._poly, left.ring())
     130        return x
     131
     132    cpdef RingElement _div_(left, RingElement r):
     133        cdef NumberFieldElement_libsingular x = left._new(), right = r
    36134        raise NotImplementedError
    37    
    38     cpdef ModuleElement _add_(left, ModuleElement right):
    39         raise NotImplementedError       
    40 
    41     cpdef ModuleElement _sub_(left, ModuleElement right):
    42         raise NotImplementedError               
    43 
    44     cpdef RingElement _div_(left, RingElement right):
    45         raise NotImplementedError                       
    46135
    47136    def __invert__(self):
    48137        raise NotImplementedError                               
     
    59148        raise NotImplementedError                                       
    60149
    61150   
    62    
     151cdef class OrderElement_libsingular(NumberFieldElement_libsingular):
     152    pass
  • sage/rings/number_field/number_field_rel.py

    diff -r 53c4551ed34f -r 6f449c1dcdaa sage/rings/number_field/number_field_rel.py
    a b  
    292292            self._order_element_class = number_field_element_generic.OrderElement_generic
    293293            self._set_implementation(arithmetic_implementation.ArithmeticImplementation_generic(polynomial))
    294294        elif implementation == 'singular':
    295             import number_field_element_singular
    296             self._element_class = number_field_element_singular.NumberFieldElement_singular
    297             self._order_element_class = number_field_element_singular.OrderElement_singular
    298             self._set_implementation(arithmetic_implementation.ArithmeticImplementation_singular())
     295            import number_field_element_libsingular
     296            self._element_class = number_field_element_libsingular.NumberFieldElement_libsingular
     297            self._order_element_class = number_field_element_libsingular.OrderElement_libsingular
     298            ring, ideal = self._libsingular_ring(polynomial)
     299            self._set_implementation(arithmetic_implementation.ArithmeticImplementation_libsingular(ring, ideal))
    299300        else:
    300301            raise ValueError, "invalid relative number field implementation '%s'"%implementation
    301302        assert self.implementation() is not None
     
    360361        self._zero_element = self(0)
    361362        self._one_element =  self(1)
    362363
     364    def _libsingular_ring(self, polynomial):
     365        """
     366        Return the libsingular ring that we use to implement arithmetic in
     367        the relative extension defined by the given polynomial.
     368
     369        INPUT:
     370            - ``polynomial`` -- a polynomial over a number field
     371
     372        OUTPUT:
     373            - a singular multivariate polynomial ring
     374            - an ideal
     375        """
     376        from sage.rings.all import PolynomialRing, QQ
     377        ring = PolynomialRing(QQ, 2, names=('x','y'))
     378        x,y = ring.gens()
     379        ideal = ring.ideal([x**3+7,y**3+2])
     380        return ring, ideal
     381
    363382    def change_names(self, names):
    364383        r"""
    365384        Return relative number field isomorphic to self but with the
  • sage/rings/number_field/todo.txt

    diff -r 53c4551ed34f -r 6f449c1dcdaa sage/rings/number_field/todo.txt
    a b  
    3838   [x] make sure the _cmp_c_impl's are compatible across number field element implementations.
    3939   [x] make default implementation computation be centralized somehow??
    4040
    41    [ ] libsingular elements
     41   [ ] libsingular elements:
     42       [ ] constructing the corresponding ring
     43
     44
    4245   [ ] implement OrderElement_singular
    4346
    4447   [ ] write general implementation of algorithm for computing absolute_field