コード例 #1
0
ファイル: gp_simon.py プロジェクト: sajedel/testsage
def init():
    """
    Function to initialize the gp process
    """
    global gp
    if gp is None:
        gp = Gp(script_subdirectory='simon')
        gp.read("ell.gp")
        gp.read("ellQ.gp")
        gp.read("qfsolve.gp")
        gp.read("resultant3.gp")
コード例 #2
0
def init():
    """
    Function to initialize the gp process
    """
    global gp
    if gp is None:
        import os
        from sage.env import DOT_SAGE
        logfile = os.path.join(DOT_SAGE, 'gp-simon.log')
        gp = Gp(script_subdirectory='simon', logfile=logfile)
        gp.read("ellQ.gp")
        gp.read("ell.gp")
        gp.read("qfsolve.gp")
        gp.read("resultant3.gp")
コード例 #3
0
ファイル: qfsolve.py プロジェクト: bopopescu/sage-5
def _gp_for_simon():
    r"""
    Start a GP interpreter for the use of Denis Simon's Qfsolve and Qfparam
    if it is not started already.

    EXAMPLE ::

        sage: from sage.quadratic_forms.qfsolve import _gp_for_simon
        sage: _gp_for_simon()
        PARI/GP interpreter
    """
    global _gp_for_simon_interpreter
    if _gp_for_simon_interpreter is None:
        _gp_for_simon_interpreter = Gp(script_subdirectory='simon')
        _gp_for_simon_interpreter.read("qfsolve.gp")
    return _gp_for_simon_interpreter
コード例 #4
0
ファイル: qfsolve.py プロジェクト: Etn40ff/sage
def _gp_for_simon():
    r"""
    Start a GP interpreter for the use of Denis Simon's programs
    ``qfsolve`` and ``qfparam``, if it is not started already.

    EXAMPLE ::

        sage: from sage.quadratic_forms.qfsolve import _gp_for_simon
        sage: _gp_for_simon()
        PARI/GP interpreter
    """
    global _gp_for_simon_interpreter
    if _gp_for_simon_interpreter is None:
        _gp_for_simon_interpreter = Gp(script_subdirectory='simon')
        _gp_for_simon_interpreter.read("qfsolve.gp")
    return _gp_for_simon_interpreter
コード例 #5
0
ファイル: buzzard.py プロジェクト: mcognetta/sage
def gp():
    r"""
    Return a copy of the GP interpreter with the appropriate files loaded.

    EXAMPLES::

        sage: sage.modular.buzzard.gp()
        PARI/GP interpreter
    """

    global _gp
    if _gp is None:
        _gp = Gp(script_subdirectory='buzzard')
        _gp.read("DimensionSk.g")
        _gp.read("genusn.g")
        _gp.read("Tpprog.g")
    return _gp
コード例 #6
0
ファイル: gp_simon.py プロジェクト: DrXyzzy/sage
def init():
    """
    Function to initialize the gp process
    """
    global gp
    if gp is None:
        import os
        from sage.env import DOT_SAGE
        logfile = os.path.join(DOT_SAGE, 'gp-simon.log')
        gp = Gp(script_subdirectory='simon', logfile=logfile)
        gp.read("ell.gp")
        gp.read("ellQ.gp")
        gp.read("qfsolve.gp")
        gp.read("resultant3.gp")
コード例 #7
0
def gp():
    r"""
    Return a copy of the GP interpreter with the appropriate files loaded.

    EXAMPLES::

        sage: sage.modular.buzzard.gp()
        PARI/GP interpreter
    """

    global _gp
    if _gp is None:
        _gp = Gp(script_subdirectory='buzzard')
        _gp.read("DimensionSk.g")
        _gp.read("genusn.g")
        _gp.read("Tpprog.g")
    return _gp
コード例 #8
0
    def guess(self, sequence, algorithm='sage'):
        """
        Return the minimal CFiniteSequence that generates the sequence.

        Assume the first value has index 0.

        INPUT:

        - ``sequence`` -- list of integers
        - ``algorithm`` -- string
            - 'sage' - the default is to use Sage's matrix kernel function
            - 'pari' - use Pari's implementation of LLL
            - 'bm' - use Sage's Berlekamp-Massey algorithm

        OUTPUT:

        - a CFiniteSequence, or 0 if none could be found

        With the default kernel method, trailing zeroes are chopped
        off before a guessing attempt. This may reduce the data
        below the accepted length of six values.

        EXAMPLES::

            sage: C.<x> = CFiniteSequences(QQ)
            sage: C.guess([1,2,4,8,16,32])
            C-finite sequence, generated by -1/2/(x - 1/2)
            sage: r = C.guess([1,2,3,4,5])
            Traceback (most recent call last):
            ...
            ValueError: Sequence too short for guessing.

        With Berlekamp-Massey, if an odd number of values is given, the last one is dropped.
        So with an odd number of values the result may not generate the last value::

            sage: r = C.guess([1,2,4,8,9], algorithm='bm'); r
            C-finite sequence, generated by -1/2/(x - 1/2)
            sage: r[0:5]
            [1, 2, 4, 8, 16]
        """
        S = self.polynomial_ring()
        if algorithm == 'bm':
            from sage.matrix.berlekamp_massey import berlekamp_massey
            if len(sequence) < 2:
                raise ValueError('Sequence too short for guessing.')
            R = PowerSeriesRing(QQ, 'x')
            if len(sequence) % 2:
                sequence.pop()
            l = len(sequence) - 1
            denominator = S(berlekamp_massey(sequence).reverse())
            numerator = R(S(sequence) * denominator, prec=l).truncate()

            return CFiniteSequence(numerator / denominator)
        elif algorithm == 'pari':
            global _gp
            if len(sequence) < 6:
                raise ValueError('Sequence too short for guessing.')
            if _gp is None:
                _gp = Gp()
                _gp("ggf(v)=local(l,m,p,q,B);l=length(v);B=floor(l/2);\
                if(B<3,return(0));m=matrix(B,B,x,y,v[x-y+B+1]);\
                q=qflll(m,4)[1];if(length(q)==0,return(0));\
                p=sum(k=1,B,x^(k-1)*q[k,1]);\
                q=Pol(Pol(vector(l,n,v[l-n+1]))*p+O(x^(B+1)));\
                if(polcoeff(p,0)<0,q=-q;p=-p);q=q/p;p=Ser(q+O(x^(l+1)));\
                for(m=1,l,if(polcoeff(p,m-1)!=v[m],return(0)));q")
            _gp.set('gf', sequence)
            _gp("gf=ggf(gf)")
            num = S(sage_eval(_gp.eval("Vec(numerator(gf))"))[::-1])
            den = S(sage_eval(_gp.eval("Vec(denominator(gf))"))[::-1])
            if num == 0:
                return 0
            else:
                return CFiniteSequence(num / den)
        else:
            from sage.matrix.constructor import matrix
            from sage.functions.other import ceil
            from numpy import trim_zeros
            seq = sequence[:]
            while seq and sequence[-1] == 0:
                seq.pop()
            l = len(seq)
            if l == 0:
                return 0
            if l < 6:
                raise ValueError('Sequence too short for guessing.')

            hl = ceil(ZZ(l) / 2)
            A = matrix([sequence[k:k + hl] for k in range(hl)])
            K = A.kernel()
            if K.dimension() == 0:
                return 0
            R = PolynomialRing(QQ, 'x')
            den = R(trim_zeros(K.basis()[-1].list()[::-1]))
            if den == 1:
                return 0
            offset = next((i for i, x in enumerate(sequence) if x), None)
            S = PowerSeriesRing(QQ, 'x', default_prec=l - offset)
            num = S(R(sequence) * den).truncate(ZZ(l) // 2 + 1)
            if num == 0 or sequence != S(num / den).list():
                return 0
            else:
                return CFiniteSequence(num / den)
コード例 #9
0
ファイル: cfinite_sequence.py プロジェクト: akoutsianas/sage
    def guess(self, sequence, algorithm="sage"):
        """
        Return the minimal CFiniteSequence that generates the sequence.

        Assume the first value has index 0.

        INPUT:

        - ``sequence`` -- list of integers
        - ``algorithm`` -- string
            - 'sage' - the default is to use Sage's matrix kernel function
            - 'pari' - use Pari's implementation of LLL
            - 'bm' - use Sage's Berlekamp-Massey algorithm

        OUTPUT:

        - a CFiniteSequence, or 0 if none could be found

        With the default kernel method, trailing zeroes are chopped
        off before a guessing attempt. This may reduce the data
        below the accepted length of six values.

        EXAMPLES::

            sage: C.<x> = CFiniteSequences(QQ)
            sage: C.guess([1,2,4,8,16,32])
            C-finite sequence, generated by 1/(-2*x + 1)
            sage: r = C.guess([1,2,3,4,5])
            Traceback (most recent call last):
            ...
            ValueError: Sequence too short for guessing.

        With Berlekamp-Massey, if an odd number of values is given, the last one is dropped.
        So with an odd number of values the result may not generate the last value::

            sage: r = C.guess([1,2,4,8,9], algorithm='bm'); r
            C-finite sequence, generated by 1/(-2*x + 1)
            sage: r[0:5]
            [1, 2, 4, 8, 16]
        """
        S = self.polynomial_ring()
        if algorithm == "bm":
            from sage.matrix.berlekamp_massey import berlekamp_massey

            if len(sequence) < 2:
                raise ValueError("Sequence too short for guessing.")
            R = PowerSeriesRing(QQ, "x")
            if len(sequence) % 2 == 1:
                sequence = sequence[:-1]
            l = len(sequence) - 1
            denominator = S(berlekamp_massey(sequence).list()[::-1])
            numerator = R(S(sequence) * denominator, prec=l).truncate()

            return CFiniteSequence(numerator / denominator)
        elif algorithm == "pari":
            global _gp
            if len(sequence) < 6:
                raise ValueError("Sequence too short for guessing.")
            if _gp is None:
                _gp = Gp()
                _gp(
                    "ggf(v)=local(l,m,p,q,B);l=length(v);B=floor(l/2);\
                if(B<3,return(0));m=matrix(B,B,x,y,v[x-y+B+1]);\
                q=qflll(m,4)[1];if(length(q)==0,return(0));\
                p=sum(k=1,B,x^(k-1)*q[k,1]);\
                q=Pol(Pol(vector(l,n,v[l-n+1]))*p+O(x^(B+1)));\
                if(polcoeff(p,0)<0,q=-q;p=-p);q=q/p;p=Ser(q+O(x^(l+1)));\
                for(m=1,l,if(polcoeff(p,m-1)!=v[m],return(0)));q"
                )
            _gp.set("gf", sequence)
            _gp("gf=ggf(gf)")
            num = S(sage_eval(_gp.eval("Vec(numerator(gf))"))[::-1])
            den = S(sage_eval(_gp.eval("Vec(denominator(gf))"))[::-1])
            if num == 0:
                return 0
            else:
                return CFiniteSequence(num / den)
        else:
            from sage.matrix.constructor import matrix
            from sage.functions.other import floor, ceil
            from numpy import trim_zeros

            l = len(sequence)
            while l > 0 and sequence[l - 1] == 0:
                l -= 1
            sequence = sequence[:l]
            if l == 0:
                return 0
            if l < 6:
                raise ValueError("Sequence too short for guessing.")

            hl = ceil(ZZ(l) / 2)
            A = matrix([sequence[k : k + hl] for k in range(hl)])
            K = A.kernel()
            if K.dimension() == 0:
                return 0
            R = PolynomialRing(QQ, "x")
            den = R(trim_zeros(K.basis()[-1].list()[::-1]))
            if den == 1:
                return 0
            offset = next((i for i, x in enumerate(sequence) if x != 0), None)
            S = PowerSeriesRing(QQ, "x", default_prec=l - offset)
            num = S(R(sequence) * den).add_bigoh(floor(ZZ(l) / 2 + 1)).truncate()
            if num == 0 or sequence != S(num / den).list():
                return 0
            else:
                return CFiniteSequence(num / den)
コード例 #10
0
ファイル: cfinite_sequence.py プロジェクト: rwst/misc
    def guess(sequence, algorithm='pari'):
        """
        Return the minimal CFiniteSequence that generates the sequence. Assume the first
        value has index 0.

        INPUT:

        - ``sequence`` -- list of integers
        
        - ``algorithm`` -- string
            - 'pari' - use Pari's implementation of LLL (fast)
            - 'bm' - use Sage's Berlekamp-Massey algorithm
            
        OUTPUT:
        
        - a CFiniteSequence, or 0 if none could be found
        
        EXAMPLES::

            sage: CFiniteSequence.guess([1,2,4,8,16,32])
            C-finite sequence, generated by 1/(-2*x + 1)
            sage: r = CFiniteSequence.guess([1,2,3,4,5])
            Traceback (most recent call last):
            ...
            ValueError: Sequence too short for guessing.
            sage: CFiniteSequence.guess([1,0,0,0,0,0])
            Finite sequence [1], offset = 0
            
        With Pari LLL, all values are taken into account, and if no o.g.f.
        can be found, `0` is returned::
        
            sage: CFiniteSequence.guess([1,0,0,0,0,1])
            0
            
        With Berlekamp-Massey, if an odd number of values is given, the last one is dropped.
        So with an odd number of values the result may not generate the last value::
        
            sage: r = CFiniteSequence.guess([1,2,4,8,9], algorithm='bm'); r
            C-finite sequence, generated by 1/(-2*x + 1)
            sage: r[0:5]
            [1, 2, 4, 8, 16]
        """
        S = PolynomialRing(QQ, 'x')
        if algorithm == 'bm':
            if len(sequence) < 2:
                raise ValueError('Sequence too short for guessing.')
            R = PowerSeriesRing(QQ, 'x')
            if len(sequence) % 2 == 1: sequence = sequence[:-1]
            l = len(sequence) - 1
            denominator = S(berlekamp_massey(sequence).list()[::-1])
            numerator = R(S(sequence) * denominator, prec=l).truncate()

            return CFiniteSequence(numerator / denominator)
        else:
            global _gp
            if len(sequence) < 6:
                raise ValueError('Sequence too short for guessing.')
            if _gp is None:
                _gp = Gp()
                _gp("ggf(v)=local(l,m,p,q,B);l=length(v);B=floor(l/2);\
                if(B<3,return(0));m=matrix(B,B,x,y,v[x-y+B+1]);\
                q=qflll(m,4)[1];if(length(q)==0,return(0));\
                p=sum(k=1,B,x^(k-1)*q[k,1]);\
                q=Pol(Pol(vector(l,n,v[l-n+1]))*p+O(x^(B+1)));\
                if(polcoeff(p,0)<0,q=-q;p=-p);q=q/p;p=Ser(q+O(x^(l+1)));\
                for(m=1,l,if(polcoeff(p,m-1)!=v[m],return(0)));q")
            _gp.set('gf', sequence)
            _gp("gf=ggf(gf)")
            num = S(sage_eval(_gp.eval("Vec(numerator(gf))"))[::-1])
            den = S(sage_eval(_gp.eval("Vec(denominator(gf))"))[::-1])
            if num == 0:
                return 0
            else:
                return CFiniteSequence(num / den)
        
        """