Exemplo n.º 1
0
def test_squarefree():
    assert Poly(x-1, x).is_squarefree == True
    assert Poly((x-1)**2, x).is_squarefree == False

    assert Poly(3*x**2, x).as_squarefree() == Poly(3*x, x)
    assert Poly(x**2+2*x+1, x).as_squarefree() == Poly(x+1, x)
    assert Poly(x**5-x**4-x+1, x).as_squarefree() == Poly(x**4-1, x)

    assert poly_sqf(1, x) == [Poly(1, x)]
    assert poly_sqf(x, x) == [Poly(x, x)]

    assert poly_sqf(3*x**2, x) == [Poly(3, x), Poly(x, x)]
    assert poly_sqf(x**2+2*x+1, x) == [Poly(1, x), Poly(x+1, x)]

    assert poly_sqf(x**5-x**4-x+1, x) == \
        [Poly(x**3 + x**2 + x + 1, x), Poly(x-1, x)]
    assert poly_sqf(x**8+6*x**6+12*x**4+8*x**2, x) == \
        [Poly(1, x), Poly(x, x), Poly(x**2+2, x)]

    # Bronstein, Symbolic Integration, pp. 52

    A = Poly(x**4 - 3*x**2 + 6, x)
    D = Poly(x**6 - 5*x**4 + 5*x**2 + 4, x)

    f, g = D, A - D.diff(x).mul_term(t)

    res, R = poly_subresultants(f, g)
    S = poly_sqf(Poly(res, t))

    assert S == [Poly(45796, t), Poly(1, t), Poly(4*t**2 + 1, t)]
Exemplo n.º 2
0
def test_squarefree():
    assert Poly(x-1, x).is_squarefree == True
    assert Poly((x-1)**2, x).is_squarefree == False

    assert Poly(3*x**2, x).as_squarefree() == Poly(3*x, x)
    assert Poly(x**2+2*x+1, x).as_squarefree() == Poly(x+1, x)
    assert Poly(x**5-x**4-x+1, x).as_squarefree() == Poly(x**4-1, x)

    assert poly_sqf(1, x) == [Poly(1, x)]
    assert poly_sqf(x, x) == [Poly(x, x)]

    assert poly_sqf(3*x**2, x) == [Poly(3, x), Poly(x, x)]
    assert poly_sqf(x**2+2*x+1, x) == [Poly(1, x), Poly(x+1, x)]

    assert poly_sqf(x**5-x**4-x+1, x) == \
        [Poly(x**3 + x**2 + x + 1, x), Poly(x-1, x)]
    assert poly_sqf(x**8+6*x**6+12*x**4+8*x**2, x) == \
        [Poly(1, x), Poly(x, x), Poly(x**2+2, x)]

    # Bronstein, Symbolic Integration, pp. 52

    A = Poly(x**4 - 3*x**2 + 6, x)
    D = Poly(x**6 - 5*x**4 + 5*x**2 + 4, x)

    f, g = D, A - D.diff(x).mul_term(t)

    res, R = poly_subresultants(f, g)
    S = poly_sqf(Poly(res, t))

    assert S == [Poly(45796, t), Poly(1, t), Poly(4*t**2 + 1, t)]
Exemplo n.º 3
0
def test_diff():
    f = Poly(a*x**2 + b*x + 2, x)

    assert f.diff(x) == Poly(2*a*x + b, x)
    assert f.diff(y) == Poly(0, x)
    assert f.diff(a) == Poly(x**2, x)
    assert f.diff(b) == Poly(x, x)

    assert f.diff() == Poly(2*a*x + b, x)

    g = Poly(a*x**2 + b*x*y + 2, x, y)

    assert g.diff(x) == Poly(2*a*x + b*y, x, y)
    assert g.diff(y) == Poly(b*x, x, y)
    assert g.diff(a) == Poly(x**2, x, y)
    assert g.diff(b) == Poly(x*y, x, y)

    assert g.diff() == g
Exemplo n.º 4
0
def test_diff():
    f = Poly(a*x**2 + b*x + 2, x)

    assert f.diff(x) == Poly(2*a*x + b, x)
    assert f.diff(y) == Poly(0, x)
    assert f.diff(a) == Poly(x**2, x)
    assert f.diff(b) == Poly(x, x)

    assert f.diff() == Poly(2*a*x + b, x)

    g = Poly(a*x**2 + b*x*y + 2, x, y)

    assert g.diff(x) == Poly(2*a*x + b*y, x, y)
    assert g.diff(y) == Poly(b*x, x, y)
    assert g.diff(a) == Poly(x**2, x, y)
    assert g.diff(b) == Poly(x*y, x, y)

    assert g.diff() == g
Exemplo n.º 5
0
def ratint_ratpart(f, g, x):
    """Horowitz-Ostrogradsky algorithm.

       Given a field K and polynomials f and g in K[x], such that f and g
       are coprime and deg(f) < deg(g), returns fractions A and B in K(x),
       such that f/g = A' + B and B has square-free denominator.

    """
    f, g = Poly(f, x), Poly(g, x)

    u = poly_gcd(g, g.diff())
    v = poly_div(g, u)[0]

    n = u.degree - 1
    m = v.degree - 1
    d = g.degree

    A_coeff = [ Symbol('a' + str(n-i), dummy=True) for i in xrange(0, n+1) ]
    B_coeff = [ Symbol('b' + str(m-i), dummy=True) for i in xrange(0, m+1) ]

    symbols = A_coeff + B_coeff

    A = Poly(zip(A_coeff, xrange(n, -1, -1)), x)
    B = Poly(zip(B_coeff, xrange(m, -1, -1)), x)

    H = f - A.diff()*v + A*poly_div(u.diff()*v, u)[0] - B*u

    result = solve(H.coeffs, symbols)

    A = A.subs(result)
    B = B.subs(result)

    rat_part = Poly.cancel((A, u), x)
    log_part = Poly.cancel((B, v), x)

    return rat_part, log_part
Exemplo n.º 6
0
def ratint_ratpart(f, g, x):
    """Horowitz-Ostrogradsky algorithm.

       Given a field K and polynomials f and g in K[x], such that f and g
       are coprime and deg(f) < deg(g), returns fractions A and B in K(x),
       such that f/g = A' + B and B has square-free denominator.

    """
    f, g = Poly(f, x), Poly(g, x)

    u = poly_gcd(g, g.diff())
    v = poly_div(g, u)[0]

    n = u.degree - 1
    m = v.degree - 1
    d = g.degree

    A_coeff = [ Symbol('a' + str(n-i), dummy=True) for i in xrange(0, n+1) ]
    B_coeff = [ Symbol('b' + str(m-i), dummy=True) for i in xrange(0, m+1) ]

    symbols = A_coeff + B_coeff

    A = Poly(zip(A_coeff, xrange(n, -1, -1)), x)
    B = Poly(zip(B_coeff, xrange(m, -1, -1)), x)

    H = f - A.diff()*v + A*poly_div(u.diff()*v, u)[0] - B*u

    result = solve(H.coeffs, symbols)

    A = A.subs(result)
    B = B.subs(result)

    rat_part = Poly.cancel((A, u), x)
    log_part = Poly.cancel((B, v), x)

    return rat_part, log_part
Exemplo n.º 7
0
def evtupoly(n,d):
    """
    Generates a random polynomial in n
    variables with largest degree d.
    """
    from scipy.misc import comb
    from sympy import var, Poly, Matrix
    from numpy import zeros, empty

    m = comb(d-1+n,n,exact=1)
    print 'number of terms: ', m, '+', n, 'leading terms'
    print ''
    (C,E) = randpoly(n,d-1)
    for i in range(0,n):
        C.append(10)
        t = [0 for j in range(0,n)]
        t[i] = d
        E.append(tuple(t))
    print 'coefficients:'
    print C
    print ''
    print 'exponents:'
    print E
    print ''

    # Set up variables
    S = ['x' + str(i) for i in range(1,n+1)]

    # Construct symbolic polynomial
    p = Poly(strpoly(S,C,E),var(S))
    print 'sympy expression:'
    print p
    print ''

    # Construct polynomial function
    def pf(x):
        return float(p.eval(zip(S,x)))

    # Calculate symbolic gradient
    grad = Matrix(n,1, lambda i,j: p.diff(S[i]))
    print 'sympy gradient:'
    print grad
    print ''

    # Construct gradient function
    def pg(x):
        gval = zeros(n)
        for i in range(0,n):
            gval[i] = float(grad[i].eval(zip(S,x)))
        return gval

    # Calculate symbolic Hessian
    H = Matrix(n,n, lambda i,j: (p.diff(S[i])).diff(S[j]))
    print 'sympy Hessian:'
    print H
    print ''

    # Construct Hessian function
    def pH(x):
        Hval = zeros((n,n))
        for i in range(0,n):
            for j in range(0,n):
                Hval[i,j] = float(H[i,j].eval(zip(S,x)))
        return Hval

    # Calculate symbolic derivative Tensor
    T = empty((n,n,n),dtype='object')
    for i in range(0,n):
        for j in range(0,n):
            for k in range(0,n):
                T[i,j,k] = ((p.diff(S[i])).diff(S[j])).diff(S[k])

    print 'sympy Tensor:'
    print T
    print ''

    # Construct Hessian bounding function
    def bndH(l,u):
        """
        Bound Hessian over l_i <= x_i <= u_i
        """
        LH = zeros((n,n))
        UH = zeros((n,n))

        # Lower bound
        # See which entries have even exponent
        for i in range(0,n):
            for j in range(0,n):

            # Get exponents and coefficients
                E = H[i,j].monoms()
                C = H[i,j].coeffs()

                # For each term
                for t in range(0,len(E)):

                # Containers
                    L = 1
                    U = 1

                    # For each variable
                    for p in range(0,n):

                        # Get interval approx.
                        Lp,Up = intpow(l[p],u[p],E[t][p])

                        # Multiply onto previous
                        L,U = intmult(L,U,Lp,Up)

                    LH[i,j] = LH[i,j] + C[t]*L
                    UH[i,j] = UH[i,j] + C[t]*U

        return LH, UH

    # Construct derivative tensor bounding function
    def bndT(l,u):
        """
        Bound derivative Tensor over l_i <= x_i <= u_i
        """
        LT = zeros((n,n,n))
        UT = zeros((n,n,n))

        # Lower bound
        # See which entries have even exponent
        for i in range(0,n):
            for j in range(0,n):
                for k in range(0,n):

                    # Get exponents and coefficients
                    E = T[i,j,k].monoms()
                    C = T[i,j,k].coeffs()

                    # For each term
                    for t in range(0,len(E)):

                        # Containers
                        L = 1
                        U = 1

                        # For each variable
                        for p in range(0,n):

                            # Get interval approx.
                            Lp,Up = intpow(l[p],u[p],E[t][p])

                            # Multiply onto previous
                            L,U = intmult(L,U,Lp,Up)

                        LT[i,j,k] = LT[i,j,k] + C[t]*L
                        UT[i,j,k] = UT[i,j,k] + C[t]*U

        return LT, UT

    # Return f(x), g(x), H(x) and H, T bounding functions
    return pf, pg, pH, bndH, bndT
Exemplo n.º 8
0
def evtupoly(n, d):
    """
    Generates a random polynomial in n
    variables with largest degree d.
    """
    from scipy.misc import comb
    from sympy import var, Poly, Matrix
    from numpy import zeros, empty

    m = comb(d - 1 + n, n, exact=1)
    print 'number of terms: ', m, '+', n, 'leading terms'
    print ''
    (C, E) = randpoly(n, d - 1)
    for i in range(0, n):
        C.append(10)
        t = [0 for j in range(0, n)]
        t[i] = d
        E.append(tuple(t))
    print 'coefficients:'
    print C
    print ''
    print 'exponents:'
    print E
    print ''

    # Set up variables
    S = ['x' + str(i) for i in range(1, n + 1)]

    # Construct symbolic polynomial
    p = Poly(strpoly(S, C, E), var(S))
    print 'sympy expression:'
    print p
    print ''

    # Construct polynomial function
    def pf(x):
        return float(p.eval(zip(S, x)))

    # Calculate symbolic gradient
    grad = Matrix(n, 1, lambda i, j: p.diff(S[i]))
    print 'sympy gradient:'
    print grad
    print ''

    # Construct gradient function
    def pg(x):
        gval = zeros(n)
        for i in range(0, n):
            gval[i] = float(grad[i].eval(zip(S, x)))
        return gval

    # Calculate symbolic Hessian
    H = Matrix(n, n, lambda i, j: (p.diff(S[i])).diff(S[j]))
    print 'sympy Hessian:'
    print H
    print ''

    # Construct Hessian function
    def pH(x):
        Hval = zeros((n, n))
        for i in range(0, n):
            for j in range(0, n):
                Hval[i, j] = float(H[i, j].eval(zip(S, x)))
        return Hval

    # Calculate symbolic derivative Tensor
    T = empty((n, n, n), dtype='object')
    for i in range(0, n):
        for j in range(0, n):
            for k in range(0, n):
                T[i, j, k] = ((p.diff(S[i])).diff(S[j])).diff(S[k])

    print 'sympy Tensor:'
    print T
    print ''

    # Construct Hessian bounding function
    def bndH(l, u):
        """
        Bound Hessian over l_i <= x_i <= u_i
        """
        LH = zeros((n, n))
        UH = zeros((n, n))

        # Lower bound
        # See which entries have even exponent
        for i in range(0, n):
            for j in range(0, n):

                # Get exponents and coefficients
                E = H[i, j].monoms()
                C = H[i, j].coeffs()

                # For each term
                for t in range(0, len(E)):

                    # Containers
                    L = 1
                    U = 1

                    # For each variable
                    for p in range(0, n):

                        # Get interval approx.
                        Lp, Up = intpow(l[p], u[p], E[t][p])

                        # Multiply onto previous
                        L, U = intmult(L, U, Lp, Up)

                    LH[i, j] = LH[i, j] + C[t] * L
                    UH[i, j] = UH[i, j] + C[t] * U

        return LH, UH

    # Construct derivative tensor bounding function
    def bndT(l, u):
        """
        Bound derivative Tensor over l_i <= x_i <= u_i
        """
        LT = zeros((n, n, n))
        UT = zeros((n, n, n))

        # Lower bound
        # See which entries have even exponent
        for i in range(0, n):
            for j in range(0, n):
                for k in range(0, n):

                    # Get exponents and coefficients
                    E = T[i, j, k].monoms()
                    C = T[i, j, k].coeffs()

                    # For each term
                    for t in range(0, len(E)):

                        # Containers
                        L = 1
                        U = 1

                        # For each variable
                        for p in range(0, n):

                            # Get interval approx.
                            Lp, Up = intpow(l[p], u[p], E[t][p])

                            # Multiply onto previous
                            L, U = intmult(L, U, Lp, Up)

                        LT[i, j, k] = LT[i, j, k] + C[t] * L
                        UT[i, j, k] = UT[i, j, k] + C[t] * U

        return LT, UT

    # Return f(x), g(x), H(x) and H, T bounding functions
    return pf, pg, pH, bndH, bndT
Exemplo n.º 9
0
        els.append(LC(r, v))

    return els


if __name__ == '__main__':
    from sympy import Poly

    P = Poly('x*(x^3 - x)')
    Q = Poly('x^3 + x')

    print(sRes(P, Q))

    P = Poly('x^2 + y^2 + z^2 -1')
    x, y, z = P.gens
    P1 = P.diff(z)
    print(SyHa(P, P1, 0, z))  # from [SPR, example 5.17]
    for j in range(2):
        print(j, sRes(P, P1, j).simplify())

    Q = Poly('x^2 + y^2 -1')
    x, y = Q.gens
    Q1 = Q.diff(y)
    print(SyHa(Q, Q1, 0, y))

    P = Poly('x^2 + y^2 + z^2 -1')
    x, y, z = P.gens

    for j in range(2):
        print(j, sRes(Q, Q1, j).simplify())