Changeset 7873:d295f1317dda


Ignore:
Timestamp:
12/22/07 19:24:31 (5 years ago)
Author:
Robert Miller <rlmillster@…>
Branch:
default
Tags:
2.9.1.rc2
Message:

Joel Mohler's diff for trac #1558

Location:
sage
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • sage/libs/ntl/decl.pxi

    r7467 r7873  
    1010    ctypedef struct ZZ_c "struct ZZ": 
    1111        pass 
     12 
     13    ctypedef struct vec_ZZ_c "vec_ZZ": 
     14        ZZ_c RawGet(long i) 
     15        ZZ_c *elts() 
     16        long length() 
    1217 
    1318    void del_charstar(char*) 
     
    168173    # really, this is from NTL/ZZX.h 
    169174    ctypedef struct ZZX_c "struct ZZX": 
    170         pass 
     175        vec_ZZ_c rep 
     176 
     177    ctypedef struct pair_ZZX_long_c "pair_ZZX_long": 
     178        ZZX_c a 
     179        long b 
     180 
     181    ctypedef struct vec_pair_ZZX_long_c "vec_pair_ZZX_long": 
     182        pair_ZZX_long_c RawGet(long i) 
     183        long length() 
    171184 
    172185    # Some boiler-plate 
     
    199212    void ZZX_XGCD "XGCD"(ZZ_c r, ZZX_c s, ZZX_c t, ZZX_c a, ZZX_c b, long deterministic) 
    200213    void ZZX_content "content"(ZZ_c d, ZZX_c f) 
     214    void ZZX_factor "factor"(ZZ_c c, vec_pair_ZZX_long_c factors, ZZX_c f, long verbose, long bnd) 
    201215 
    202216    void ZZX_squarefree_decomposition(ZZX_c*** v, long** e, long* n, ZZX_c* x) 
  • sage/rings/polynomial/polynomial_integer_dense_ntl.pyx

    r7514 r7873  
    1818 
    1919include "../../ext/stdsage.pxi" 
    20  
     20include "sage/ext/interrupt.pxi" 
     21                                                                                                                                     
    2122from sage.rings.polynomial.polynomial_element cimport Polynomial 
    2223from sage.structure.element cimport ModuleElement, RingElement 
     
    235236            0 
    236237        """ 
    237         # todo: this is performing an unnecessary copy. We need 
    238         # a function that returns a (const!) pointer to the coefficient 
    239         # of the NTL polynomial. 
    240         cdef ZZ_c temp = ZZX_coeff(self.__poly, n) 
    241238        cdef Integer z = PY_NEW(Integer) 
    242         ZZ_to_mpz(&z.value, &temp) 
    243         return z 
    244  
     239        if n < 0 or n > ZZX_deg(self.__poly): 
     240            return z 
     241        else: 
     242            # Note that the NTL documentation blesses this direct access of the "rep" member in ZZX.txt. 
     243            #  Check the "Miscellany" section. 
     244            ZZ_to_mpz(&z.value, &self.__poly.rep.elts()[n]) 
     245            return z 
    245246 
    246247    def __getslice__(self, long i, long j): 
     
    696697        free(e) 
    697698        return Factorization(F, unit=c, sort=False) 
    698      
     699 
     700    def _factor_pari(self): 
     701        return Polynomial.factor(self) # uses pari for integers over ZZ 
     702 
     703    def _factor_ntl(self): 
     704        """ 
     705        There are ample doc-tests elsewhere that test this functionality. 
     706        AUTHOR: 
     707            -- Joel B. Mohler 
     708        """ 
     709        cdef Polynomial_integer_dense_ntl fac_py 
     710        cdef ZZ_c content 
     711        cdef vec_pair_ZZX_long_c factors 
     712        cdef long i 
     713        cdef int sig_me = ZZX_deg(self.__poly) 
     714        if sig_me > 10: 
     715            _sig_on 
     716        ZZX_factor(content, factors, self.__poly, 0, 0) 
     717        if sig_me > 10: 
     718            _sig_off 
     719        results = [] 
     720        unit = None 
     721        if not ZZ_IsOne(content): 
     722            fac_py = self._new() 
     723            ZZX_SetCoeff(fac_py.__poly, 0, content) 
     724            if ZZX_deg(fac_py.__poly) == 0 and ZZ_to_int(fac_py.__poly.rep.elts())==-1: 
     725                unit = fac_py 
     726            else: 
     727                results.append( (fac_py,1) ) 
     728        for i from 0 <= i < factors.length(): 
     729            fac_py = self._new() 
     730            fac_py.__poly = factors.RawGet(i).a 
     731            results.append( (fac_py,factors.RawGet(i).b) ) 
     732        return Factorization(results, unit = unit) 
     733 
     734    def factor(self): 
     735        """ 
     736        This function overrides the generic polynomial factorization to  
     737        make a somewhat intelligent decision to use Pari or NTL based on  
     738        some benchmarking. 
     739 
     740        EXAMPLES: 
     741            sage: R.<x>=ZZ[] 
     742            sage: f=x^4-1 
     743            sage: f.factor() 
     744            (x - 1) * (x + 1) * (x^2 + 1) 
     745            sage: f=1-x 
     746            sage: f.factor() 
     747            (-1) * (x - 1) 
     748            sage: f.factor().unit() 
     749            -1 
     750        """ 
     751        cdef int i 
     752        cdef int deg = ZZX_deg(self.__poly) 
     753        # it appears that pari has a window from about degrees 30 and 300 in which it beats NTL. 
     754        if deg < 30 or deg > 300: 
     755            return self._factor_ntl() 
     756        else: 
     757            return self._factor_pari() 
    699758 
    700759    def factor_mod(self, p): 
Note: See TracChangeset for help on using the changeset viewer.