Esempio n. 1
0
#!/usr/bin/python

import polypy
import polypy_test

import sys

polypy_test.init()

x = polypy.Variable("x")
y = polypy.Variable("y")

polypy.variable_order.set([y, x])

polypy_test.start("model-based GCD")

# polypy.trace_enable("coefficient::gcd")
# polypy.trace_enable("polynomial")
# polypy.trace_enable("coefficient")
# polypy.trace_enable("coefficient::order")
# polypy.trace_enable("coefficient::reduce")
polypy.trace_enable("coefficient::mgcd")

x0 = polypy.Variable("x0")
x1 = polypy.Variable("x1")
x2 = polypy.Variable("x2")
x3 = polypy.Variable("x3")
x4 = polypy.Variable("x4")

polypy.variable_order.set([x0, x1, x2, x3, x4])
def check_resultant(p, q, expected):
    resultant = p.resultant(q)
    ok = resultant == expected
    if (not ok):
        print "p =", p
        print "q =", q
        print "resultant =", resultant
        print "expected  =", expected

polypy_test.init()

# polypy.trace_enable("coefficient::resultant")
# polypy.trace_enable("polynomial::expensive")

polypy_test.start("Speed")

x5 = polypy.Variable("x5")     
x9 = polypy.Variable("x9")     
x10 = polypy.Variable("x10")     
x11 = polypy.Variable("x11")     
x12 = polypy.Variable("x12")     

polypy.variable_order.set([x9, x12, x11, x10, x5]);

A = (((1*x12**6 + (4*x9)*x12**5 + (10*x9**2)*x12**4 + (14*x9**3)*x12**3 + (13*x9**4)*x12**2 + (6*x9**5)*x12 + (1*x9**6))*x11**2)*x10**2 + (((2*x9**2)*x12**6 + (4*x9**3)*x12**5 + (6*x9**4)*x12**4 + (2*x9**5)*x12**3)*x11)*x10 + ((1*x9**4)*x12**6))*x5**2 + ((((-1*x9)*x12**3 + (-1*x9**2)*x12**2 + (-1*x9**3)*x12)*x11**4)*x10**4 + ((-2*x12**6 + (-9*x9)*x12**5 + (-17*x9**2)*x12**4 + (-21*x9**3)*x12**3 + (-12*x9**4)*x12**2 + (-4*x9**5)*x12)*x11**3)*x10**3 + (((-2*x9**2)*x12**6 + (-6*x9**3 - 2*x9)*x12**5 + (-4*x9**4 - 8*x9**2)*x12**4 + (-3*x9**5 - 16*x9**3)*x12**3 + (-1*x9**6 - 18*x9**4)*x12**2 + (-1*x9**7 - 10*x9**5)*x12 + (-2*x9**6))*x11**2)*x10**2 + (((-1*x9**5 - 2*x9**3)*x12**5 + (-1*x9**6 - 4*x9**4)*x12**4 + (-1*x9**7 - 2*x9**5)*x12**3)*x11)*x10)*x5 + ((((1*x9)*x12**3 + (1*x9**2)*x12**2)*x11**5)*x10**5 + ((1*x12**6 + (5*x9)*x12**5 + (7*x9**2)*x12**4 + (6*x9**3)*x12**3 + (3*x9**4)*x12**2 + (1*x9**3)*x12)*x11**4)*x10**4 + (((2*x9**3 + 2*x9)*x12**5 + (2*x9**4 + 7*x9**2)*x12**4 + (1*x9**5 + 13*x9**3)*x12**3 + (1*x9**6 + 8*x9**4)*x12**2 + (4*x9**5)*x12)*x11**3)*x10**3 + (((-1*x9**2)*x12**6 + (1*x9**5)*x12**5 + (1*x9**6 - 2*x9**4 + 1*x9**2)*x12**4 + (2*x9**5 + 4*x9**3)*x12**3 + (6*x9**4)*x12**2 + (1*x9**7 + 4*x9**5)*x12 + (1*x9**6))*x11**2)*x10**2 + (((-2*x9**4)*x12**6 + (-1*x9**6)*x12**4 + (1*x9**7)*x12**3)*x11)*x10 + ((-1*x9**6)*x12**6))
B = (((2*x12**6 + (8*x9)*x12**5 + (20*x9**2)*x12**4 + (28*x9**3)*x12**3 + (26*x9**4)*x12**2 + (12*x9**5)*x12 + (2*x9**6))*x11**2)*x10**2 + (((4*x9**2)*x12**6 + (8*x9**3)*x12**5 + (12*x9**4)*x12**4 + (4*x9**5)*x12**3)*x11)*x10 + ((2*x9**4)*x12**6))*x5 + ((((-1*x9)*x12**3 + (-1*x9**2)*x12**2 + (-1*x9**3)*x12)*x11**4)*x10**4 + ((-2*x12**6 + (-9*x9)*x12**5 + (-17*x9**2)*x12**4 + (-21*x9**3)*x12**3 + (-12*x9**4)*x12**2 + (-4*x9**5)*x12)*x11**3)*x10**3 + (((-2*x9**2)*x12**6 + (-6*x9**3 - 2*x9)*x12**5 + (-4*x9**4 - 8*x9**2)*x12**4 + (-3*x9**5 - 16*x9**3)*x12**3 + (-1*x9**6 - 18*x9**4)*x12**2 + (-1*x9**7 - 10*x9**5)*x12 + (-2*x9**6))*x11**2)*x10**2 + (((-1*x9**5 - 2*x9**3)*x12**5 + (-1*x9**6 - 4*x9**4)*x12**4 + (-1*x9**7 - 2*x9**5)*x12**3)*x11)*x10)
A.psc(B)

polypy.variable_order.set([w, z, y, x]);
Esempio n. 3
0
import polypy
import polypy_test

polypy_test.init()

def check_str(o, expected):
    ok = str(o) == expected
    if (not ok):
        print("o = {0}".format(o))
        print("expected = {0}".format(expected))
    polypy_test.check(ok)

x = polypy.Variable("x")
y = polypy.Variable("y")

polypy_test.start("Variable")

order = polypy.VariableOrder([x])
check_str(order, "[x]")

order = polypy.VariableOrder([x, y])
check_str(order, "[x, y]")

order = polypy.variable_order
check_str(order, "[]")

order.push(x)
check_str(order, "[x]")

order.push(y)
check_str(order, "[x, y]")
Esempio n. 4
0

def check_comparison(a1, a2, cmp, result, expected):
    if (result != expected):
        print("a1 = {0}".format(a1))
        print("a2 = {0}".format(a2))
        print("cmp = {0}".format(cmp))
        print("result = {0}".format(result))
        print("expected = {0}".format(expected))
        polypy_test.check(False)
    else:
        polypy_test.check(True)


polypy_test.init()
polypy_test.start("Construction and order")

a1 = polypy.AlgebraicNumber((x**2 - 2) * (x - 10), 1)
a2 = polypy.AlgebraicNumber((x**2 - 2), 1)

result = (a1 < a2)
check_comparison(a1, a2, "<", result, False)

result = (a1 == a2)
check_comparison(a1, a2, "==", result, True)

p = x**2
zero = polypy.AlgebraicNumber(p, 0)
result = zero.to_double() == 0.0
check_comparison(zero, 0, ".to_double ==", result, True)
Esempio n. 5
0
polypy.variable_order.set([z, y, x])


def check_value(p, assignment, value, expected_value):
    value_double = value.to_double()
    ok = abs(value_double - expected_value) < 0.000001
    if (not ok):
        print "p =", p
        print "assignment =", assignment
        print "value =", value
        print "value_double =", value_double
        print "expected_value =", expected_value
    polypy_test.check(ok)


polypy_test.start("Polynomial Evaluation")

# polypy.trace_enable("polynomial")
# polypy.trace_enable("factorization")
# polypy.trace_enable("algebraic_number")
# polypy.trace_enable("coefficient")
# polypy.trace_enable("coefficient::sgn")
# polypy.trace_enable("coefficient::roots")
# polypy.trace_enable("coefficient::arith")

sqrt2 = polypy.AlgebraicNumber(x**2 - 2, 1)
sqrt2_4 = polypy.AlgebraicNumber(x**4 - 2, 1)
sqrt3 = polypy.AlgebraicNumber(x**2 - 3, 1)

assignment = polypy.Assignment()
assignment.set_value(x, sqrt2)
Esempio n. 6
0
    check_gcd_single(-p, q)
    check_gcd_single(-p, -q)

def check_extended_gcd_single(p, q):
    global polypy_test
    (gcd, u, v) = p.extended_gcd(q)
    ok = polypy_test.sympy_checker.check_extended_gcd(p, q, gcd, u, v)
    polypy_test.check(ok)

def check_extended_gcd(p, q):
    check_extended_gcd_single(p, q)
    check_extended_gcd_single(p, -q)
    check_extended_gcd_single(-p, q)
    check_extended_gcd_single(-p, -q)
            
polypy_test.start("GCD in Z_13 (Knuth)")
        
# Example from Knuth (p 424)
K = polypy.CoefficientRing(13)
x = polypy.x.to_ring(K);

p = x**8 + x**6 + 10*x**4 + 10*x**3 + 8*x**2 + 2*x + 8
q = 3*x**6 + 5*x**4 + 9*x**2 + 4*x + 8
check_gcd(p, q)

polypy_test.start("Regressions (modular)")

K = polypy.CoefficientRing(7);
x = polypy.x.to_ring(K)

p = 2*x
 
polypy_test.init()
 
def check_factorization(p, p_factors, expected_size):
    mul = 1
    for (factor, multiplicity) in p_factors:
        mul = mul * (factor**multiplicity)
    ok = (p == mul) and (expected_size == len(p_factors))
    if (not ok):
        print "p =", p
        print "p_factors =", p_factors
        print "expected_size =", expected_size
        print "mul =", mul
    polypy_test.check(ok)

polypy_test.start("Square-Free Factorization")

# polypy.trace_enable("polynomial")
# polypy.trace_enable("factorization")
# polypy.trace_enable("coefficient::gcd")
# 
# x00 = polypy.Variable("x00")
# x01 = polypy.Variable("x01")
# x10 = polypy.Variable("x10")
# x11 = polypy.Variable("x11")
# x20 = polypy.Variable("x20")
# x21 = polypy.Variable("x21")
# x30 = polypy.Variable("x30")
# x31 = polypy.Variable("x31")
# 
# polypy.variable_order.set([x31, x30, x21, x20, x11])
Esempio n. 8
0
[x0, x1, x2, x3, x4, x5, x6, x7] = [polypy.Variable(name) for name in ['x0', 'x1', 'x2', 'x3', 'x4', 'x5', 'x6', 'x7']]

# polypy_test.start("Polynomial Root Isolation: Speed")
# 
# polypy.variable_order.set([y, x])
# 
# y_value_poly = 8192*x**6 + (-14336*x**5) + 75376*x**4 + (-32736*x**3) + 109496*x**2 + 133752*x + (-32441)
# y_value = polypy.AlgebraicNumber(y_value_poly, 1)
# 
# assignment = polypy.Assignment()
# assignment.set_value(y, y_value)
# 
# p = (1*y**2 + 3)*x**6 + (4*y**2 + 24*y + 20)*x**5 + (36*y**2 + 112*y + 44)*x**4 + (8*y**3 + 114*y**2 + 144*y + 30)*x**3 + (1*y**4 + 24*y**3 + 142*y**2 + 40*y - 15)*x**2 + (2*y**4 + 40*y**3 + 64*y**2 - 32*y - 26)*x + (5*y**4 + 16*y**3 + 7*y**2 - 16*y - 8)
# roots = p.roots_isolate(assignment)

polypy_test.start("Polynomial Root Isolation: Bugs");

polypy.variable_order.set([x0, x1, x2, x3, x4, x5, x6, x7])
p = ((16*x0**2 - 32*x0 + 16)*x5**2 + ((32*x0 - 32)*x1)*x5 + (16*x1**2))*x7**2 + ((8*x0**2 - 16*x0 + 8)*x5**2 + ((16*x0**2 - 32*x0 + 16)*x2**2 + (-8*x1**2)))*x7 + ((1*x0**2 - 2*x0 + 1)*x5**2 + ((-2*x0 + 2)*x1)*x5 + (1*x1**2))

p_lc = ((16*x0**2 - 32*x0 + 16)*x5**2 + ((32*x0 - 32)*x1)*x5 + (16*x1**2))

assignment = polypy.Assignment()
v = polypy.AlgebraicNumber(32*x**2 + (-64*x) + 15, 0)
assignment.set_value(x0, v)
assignment.set_value(x1, 28);
assignment.set_value(x2, -1, 4)
assignment.set_value(x3, 3)
assignment.set_value(x4, 277771, 4096)
assignment.set_value(x5, 786743, 20480)
assignment.set_value(x6, -6437)
Esempio n. 9
0
import polypy
import polypy_test

polypy_test.init()

def check_str(o, expected):
    ok = str(o) == expected
    if (not ok):
        print "o =", o
        print "expected =", expected
    polypy_test.check(ok)

x = polypy.Variable("x")
y = polypy.Variable("y")

polypy_test.start("Variable")

order = polypy.VariableOrder([x])
check_str(order, "[x]")

order = polypy.VariableOrder([x, y])
check_str(order, "[x, y]")

order = polypy.variable_order
check_str(order, "[]")

order.push(x)
check_str(order, "[x]")

order.push(y)
check_str(order, "[x, y]")
Esempio n. 10
0
polypy_test.init()

[x, y, z] = [polypy.Variable(name) for name in ['x', 'y', 'z']]
polypy.variable_order.set([z, y, x])

def check_sgn(p, assignment, expected_sgn):
    sgn = p.sgn(assignment)
    ok = (sgn > 0 and expected_sgn > 0) or (sgn < 0 and expected_sgn < 0) or (sgn == 0 and expected_sgn == 0)
    if (not ok):
        print("p = {0}".format(p))
        print("assignment = {0}".format(assignment))
        print("sgn = {0}".format(sgn))
        print("expected_sgn = {0}".format(expected_sgn))
    polypy_test.check(ok)

polypy_test.start("Sign Determination")

# polypy.trace_enable("polynomial")
# polypy.trace_enable("factorization")
# polypy.trace_enable("algebraic_number")
# polypy.trace_enable("coefficient")
# polypy.trace_enable("coefficient::sgn")
# polypy.trace_enable("coefficient::arith")

sqrt2 = polypy.AlgebraicNumber(x**2 - 2, 1)
sqrt2_4 = polypy.AlgebraicNumber(x**4 - 2, 1)
sqrt3 = polypy.AlgebraicNumber(x**2 - 3, 1)

assignment = polypy.Assignment()
assignment.set_value(x, sqrt2_4)
assignment.set_value(y, sqrt2)
Esempio n. 11
0
#!/usr/bin/python

import polypy
import polypy_test

import sys
 
polypy_test.init()
 
x = polypy.Variable("x");
y = polypy.Variable("y");

polypy.variable_order.set([y, x])

polypy_test.start("model-based GCD")

# polypy.trace_enable("coefficient::gcd")
# polypy.trace_enable("polynomial")
# polypy.trace_enable("coefficient")
# polypy.trace_enable("coefficient::order")
# polypy.trace_enable("coefficient::reduce")
polypy.trace_enable("coefficient::mgcd")

x0 = polypy.Variable("x0");
x1 = polypy.Variable("x1");
x2 = polypy.Variable("x2");
x3 = polypy.Variable("x3");
x4 = polypy.Variable("x4");

polypy.variable_order.set([x0, x1, x2, x3, x4])
Esempio n. 12
0
polypy_test.init()
 
[x, y, z] = [polypy.Variable(name) for name in ['x', 'y', 'z']]
polypy.variable_order.set([z, y, x])

def check_sgn(p, assignment, expected_sgn):
    sgn = p.sgn(assignment)
    ok = (sgn > 0 and expected_sgn > 0) or (sgn < 0 and expected_sgn < 0) or (sgn == 0 and expected_sgn == 0)
    if (not ok):
        print "p =", p
        print "assignment =", assignment
        print "sgn =", sgn
        print "expected_sgn =", expected_sgn
    polypy_test.check(ok)

polypy_test.start("Sign Determination")

# polypy.trace_enable("polynomial")
# polypy.trace_enable("factorization")
# polypy.trace_enable("algebraic_number")
# polypy.trace_enable("coefficient")
# polypy.trace_enable("coefficient::sgn")
# polypy.trace_enable("coefficient::arith")

sqrt2 = polypy.AlgebraicNumber(x**2 - 2, 1)
sqrt2_4 = polypy.AlgebraicNumber(x**4 - 2, 1)
sqrt3 = polypy.AlgebraicNumber(x**2 - 3, 1)

assignment = polypy.Assignment()
assignment.set_value(x, sqrt2_4)
assignment.set_value(y, sqrt2)
Esempio n. 13
0
polypy_test.init()

def check_factorization(p, p_factors, expected_size):
    mul = 1
    for (factor, multiplicity) in p_factors:
        mul = mul * (factor**multiplicity)
    ok = (p == mul) and (expected_size == len(p_factors))
    if (not ok):
        print("p = {0}".format(p))
        print("p_factors = {0}".format(p_factors))
        print("expected_size = {0}".format(expected_size))
        print("mul = {0}".format(mul))
    polypy_test.check(ok)

polypy_test.start("Square-Free Factorization")

# polypy.trace_enable("polynomial")
# polypy.trace_enable("factorization")
# polypy.trace_enable("coefficient::gcd")
#
# x00 = polypy.Variable("x00")
# x01 = polypy.Variable("x01")
# x10 = polypy.Variable("x10")
# x11 = polypy.Variable("x11")
# x20 = polypy.Variable("x20")
# x21 = polypy.Variable("x21")
# x30 = polypy.Variable("x30")
# x31 = polypy.Variable("x31")
#
# polypy.variable_order.set([x31, x30, x21, x20, x11])
Esempio n. 14
0
def cyclotomic(n):
    if (n == 1):
        P1 = polypy.x - 1
        return [P1]
    else:
        # Get up to 1 polynomials
        L = cyclotomic(n-1)
        # Compute Pn
        Pn = polypy.x**n - 1
        for d, Pd in enumerate(L):
            if (n % (d+1) == 0):
                Pn = Pn / Pd
        L.append(Pn)
        return L
        
polypy_test.start("Factorization in Z (Regressions)")
    
# polypy.trace_enable("factorization")
# polypy.trace_enable("hensel")
# polypy.trace_enable("arithmetic")

x = polypy.x

p = (x - 1)*(x - 2)*(x - 3)*(x - 4)*(x - 5);
check_factorization(p)

p = 1*x**4 + 94*x**3 + 107*x**2 + 771*x + (-690)
check_factorization(p)

p= x**4 + 1
check_factorization(p)
Esempio n. 15
0
from polypy import x

def check_comparison(a1, a2, cmp, result, expected):
    if (result != expected):
        print "a1 =", a1
        print "a2 =", a2
        print "cmp =", cmp
        print "result =", result
        print "expected =", expected
        polypy_test.check(False)
    else:
        polypy_test.check(True)
        
polypy_test.init()
polypy_test.start("Construction and order")

a1 = polypy.AlgebraicNumber((x**2 - 2)*(x - 10), 1)
a2 = polypy.AlgebraicNumber((x**2 - 2), 1)

result = (a1 < a2)
check_comparison(a1, a2, "<", result, False)

result = (a1 == a2)
check_comparison(a1, a2, "==", result, True)

p = x**2
zero = polypy.AlgebraicNumber(p, 0);
result = zero.to_double() == 0.0
check_comparison(zero, 0, ".to_double ==", result, True)
Esempio n. 16
0
]

# polypy_test.start("Polynomial Root Isolation: Speed")
#
# polypy.variable_order.set([y, x])
#
# y_value_poly = 8192*x**6 + (-14336*x**5) + 75376*x**4 + (-32736*x**3) + 109496*x**2 + 133752*x + (-32441)
# y_value = polypy.AlgebraicNumber(y_value_poly, 1)
#
# assignment = polypy.Assignment()
# assignment.set_value(y, y_value)
#
# p = (1*y**2 + 3)*x**6 + (4*y**2 + 24*y + 20)*x**5 + (36*y**2 + 112*y + 44)*x**4 + (8*y**3 + 114*y**2 + 144*y + 30)*x**3 + (1*y**4 + 24*y**3 + 142*y**2 + 40*y - 15)*x**2 + (2*y**4 + 40*y**3 + 64*y**2 - 32*y - 26)*x + (5*y**4 + 16*y**3 + 7*y**2 - 16*y - 8)
# roots = p.roots_isolate(assignment)

polypy_test.start("Polynomial Root Isolation: Bugs")

polypy.variable_order.set([x0, x1, x2, x3, x4, x5, x6, x7])
p = ((16 * x0**2 - 32 * x0 + 16) * x5**2 + ((32 * x0 - 32) * x1) * x5 +
     (16 * x1**2)) * x7**2 + ((8 * x0**2 - 16 * x0 + 8) * x5**2 + (
         (16 * x0**2 - 32 * x0 + 16) * x2**2 +
         (-8 * x1**2))) * x7 + ((1 * x0**2 - 2 * x0 + 1) * x5**2 +
                                ((-2 * x0 + 2) * x1) * x5 + (1 * x1**2))

p_lc = ((16 * x0**2 - 32 * x0 + 16) * x5**2 + ((32 * x0 - 32) * x1) * x5 +
        (16 * x1**2))

assignment = polypy.Assignment()
v = polypy.AlgebraicNumber(32 * x**2 + (-64 * x) + 15, 0)
assignment.set_value(x0, v)
assignment.set_value(x1, 28)
Esempio n. 17
0
 
[x, y, z] = [polypy.Variable(name) for name in ['x', 'y', 'z']]
polypy.variable_order.set([z, y, x])

def check_value(p, assignment, value, expected_value):
    value_double = value.to_double()
    ok = abs(value_double - expected_value) < 0.000001
    if (not ok):
        print "p =", p
        print "assignment =", assignment
        print "value =", value
        print "value_double =", value_double
        print "expected_value =", expected_value
    polypy_test.check(ok)

polypy_test.start("Polynomial Evaluation")

# polypy.trace_enable("polynomial")
# polypy.trace_enable("factorization")
# polypy.trace_enable("algebraic_number")
# polypy.trace_enable("coefficient")
# polypy.trace_enable("coefficient::sgn")
# polypy.trace_enable("coefficient::roots")
# polypy.trace_enable("coefficient::arith")

sqrt2 = polypy.AlgebraicNumber(x**2 - 2, 1)
sqrt2_4 = polypy.AlgebraicNumber(x**4 - 2, 1)
sqrt3 = polypy.AlgebraicNumber(x**2 - 3, 1)

assignment = polypy.Assignment()
assignment.set_value(x, sqrt2)
Esempio n. 18
0
    polypy_test.check(ok)


def check_unary(op, op_name, p, expected):
    result = op(p)
    ok = result == expected
    if (not ok):
        print("p = {0}".format(p))
        print("{0} = {1}".format(op_name, result))
        print("expected = {0}".format(expected))
    polypy_test.check(ok)


polypy_test.init()

polypy_test.start("Addition")

x = polypy.Variable("x")
y = polypy.Variable("y")
z = polypy.Variable("z")

polypy.variable_order.set([z, y, x])

# polypy.trace_enable("polynomial")
# polypy.trace_enable("coefficient")
# polypy.trace_enable("coefficient::reduce")


def poly_plus(p, q):
    return p + q
Esempio n. 19
0
# All signs
sgns = [
    polypy.SGN_LT_0, polypy.SGN_LE_0, polypy.SGN_EQ_0, polypy.SGN_NE_0,
    polypy.SGN_GT_0, polypy.SGN_GE_0
]

sgn_name = {
    polypy.SGN_LT_0: "<  0",
    polypy.SGN_LE_0: "<= 0",
    polypy.SGN_EQ_0: "== 0",
    polypy.SGN_NE_0: "!= 0",
    polypy.SGN_GT_0: ">  0",
    polypy.SGN_GE_0: ">= 0"
}

polypy_test.start("Polynomial Feasibility Integer")

assignment = polypy.Assignment()

p1 = x**2 - 7
S1 = p1.feasible_set(assignment, polypy.SGN_GE_0)  # (-inf, -2.6], [2.6, +inf)
p2 = x * (x - 4)
S2 = p2.feasible_set(assignment, polypy.SGN_LT_0)  # (0, 4)

S = S1.intersect(S2)
v = S.pick_value()
polypy_test.check(v.to_double() == 3)

polypy_test.start("Feasibility Intervals Intersection")

assignment = polypy.Assignment()
Esempio n. 20
0
polypy_test.init()


def check_comparison(x, expected, precision=0.0000001):
    diff = x.to_double() - expected
    if (diff < -precision or diff > precision):
        print("x = {0}".format(x))
        print("expected = {0}".format(expected))
        print("precision = {0}".format(precision))
        polypy_test.check(False)
    else:
        polypy_test.check(True)


polypy_test.start("Construction")

zero_int = polypy.Value(0)
one_int = polypy.Value(1)
two_int = polypy.Value(2)
three_int = polypy.Value(3)

check_comparison(zero_int, 0)
check_comparison(one_int, 1)
check_comparison(two_int, 2)
check_comparison(three_int, 3)

p = x**2 - 2
sqrt2_neg = polypy.Value(polypy.AlgebraicNumber(p, 0))
sqrt2_pos = polypy.Value(polypy.AlgebraicNumber(p, 1))
Esempio n. 21
0
        print("lb = {0}".format(lb))
        print("ub = {0}".format(ub))
        print("count = {0}".format(count))
        print("expected = {0}".format(expected))
    else:
        polypy_test.check(True)


# polypy.trace_enable("roots")
# polypy.trace_enable("algebraic_number")
# polypy.trace_enable("division")
# polypy.trace_enable("sturm_sequence_check")

x = polypy.x

polypy_test.start("Root isolation")

p = x**2 - 1
check_roots_isolate(p, 2)

p = (x - 1) * (x + 1) * (x - 2)
check_roots_isolate(p, 3)

p = 1 * x**5 + (-23 * x**4) + (-57 * x**3) + 85 * x**2 + 64 * x + (-63)
check_roots_isolate(p, 5)

p = 41 * x**5 + (-79 * x**4) + 44 * x**3 + 56 * x**2 + (-10 * x) + 41
check_roots_isolate(p, 1)

p = 9 * x**13 - 18 * x**11 - 33 * x**10 + 102 * x**8 + 7 * x**7 - 36 * x**6 - 122 * x**5 + 49 * x**4 + 93 * x**3 - 42 * x**2 - 18 * x**+9
check_roots_isolate(p, 6)
Esempio n. 22
0
        print("gcd = {0}".format(gcd))
    polypy_test.check(ok)


def check_lcm(p, q, expected):
    gcd = p.lcm(q)
    ok = gcd == expected
    if (not ok):
        print("p = {0}".format(p))
        print("q = {0}".format(q))
        print("expected = {0}".format(expected))
        print("gcd = {0}".format(gcd))
    polypy_test.check(ok)


polypy_test.start("GCD")

# polypy.trace_enable("coefficient::gcd")
# polypy.trace_enable("polynomial")
# polypy.trace_enable("coefficient")
# polypy.trace_enable("coefficient::order")
# polypy.trace_enable("coefficient::reduce")

polypy.variable_order.set([z, y, x])

# p = ((1*z**68)*y**71 + (-2*z**48)*y**52 + (26896*z**118 + 26896*z**116 + 6724*z**114)*y**48 + (1*z**28)*y**33 + (-172200*z**59 - 86100*z**57)*y**24 + 275625)
# q = ((-328*z**129 + 164*z**127 + 82*z**125)*y**95 + (656*z**109 - 328*z**107 - 164*z**105)*y**76 + (4410944*z**177 + 6616416*z**175 + 3308208*z**173 + 551368*z**171)*y**72 + (-525*z**68)*y**71 + (-328*z**89 + 164*z**87 + 82*z**85)*y**57 + (1050*z**48)*y**52 + (-42361200*z**118 - 42361200*z**116 - 10590300*z**114)*y**48 + (-525*z**28)*y**33 + (135607500*z**59 + 67803750*z**57)*y**24 - 144703125)
# p.gcd(q)

# Wrong gcd
p = 4 * z**4 + 4 * z**2 + 1
Esempio n. 23
0
    if (n == 1):
        P1 = polypy.x - 1
        return [P1]
    else:
        # Get up to 1 polynomials
        L = cyclotomic(n - 1)
        # Compute Pn
        Pn = polypy.x**n - 1
        for d, Pd in enumerate(L):
            if (n % (d + 1) == 0):
                Pn = Pn / Pd
        L.append(Pn)
        return L


polypy_test.start("Factorization in Z (Regressions)")

# polypy.trace_enable("factorization")
# polypy.trace_enable("hensel")
# polypy.trace_enable("arithmetic")

x = polypy.x

p = (x - 1) * (x - 2) * (x - 3) * (x - 4) * (x - 5)
check_factorization(p)

p = 1 * x**4 + 94 * x**3 + 107 * x**2 + 771 * x + (-690)
check_factorization(p)

p = x**4 + 1
check_factorization(p)
Esempio n. 24
0
        print "p =", p
        print "lb =", lb
        print "ub =", ub
        print "count =", count
        print "expected = ", expected 
    else:
        polypy_test.check(True)

# polypy.trace_enable("roots")
# polypy.trace_enable("algebraic_number")
# polypy.trace_enable("division")
# polypy.trace_enable("sturm_sequence_check")

x = polypy.x 

polypy_test.start("Root isolation")

p = x**2 - 1
check_roots_isolate(p, 2)

p = (x - 1)*(x + 1)*(x - 2)
check_roots_isolate(p, 3)

p = 1*x**5 + (-23*x**4) + (-57*x**3) + 85*x**2 + 64*x + (-63)
check_roots_isolate(p, 5)

p = 41*x**5 + (-79*x**4) + 44*x**3 + 56*x**2 + (-10*x) + 41
check_roots_isolate(p, 1)

p = 9*x**13 - 18*x**11 - 33*x**10 + 102*x**8 + 7*x**7 - 36*x**6 - 122*x**5 + 49*x**4 + 93*x**3 - 42*x**2 - 18*x** + 9
check_roots_isolate(p, 6)
Esempio n. 25
0
    check_gcd_single(-p, q)
    check_gcd_single(-p, -q)

def check_extended_gcd_single(p, q):
    global polypy_test
    (gcd, u, v) = p.extended_gcd(q)
    ok = polypy_test.sympy_checker.check_extended_gcd(p, q, gcd, u, v)
    polypy_test.check(ok)

def check_extended_gcd(p, q):
    check_extended_gcd_single(p, q)
    check_extended_gcd_single(p, -q)
    check_extended_gcd_single(-p, q)
    check_extended_gcd_single(-p, -q)
            
polypy_test.start("GCD in Z_13 (Knuth)")
        
# Example from Knuth (p 424)
K = polypy.CoefficientRing(13)
x = polypy.x.to_ring(K);

p = x**8 + x**6 + 10*x**4 + 10*x**3 + 8*x**2 + 2*x + 8
q = 3*x**6 + 5*x**4 + 9*x**2 + 4*x + 8
check_gcd(p, q)

polypy_test.start("Regressions (modular)")

K = polypy.CoefficientRing(7);
x = polypy.x.to_ring(K)

p = 2*x
Esempio n. 26
0
        polypy.SGN_EQ_0, 
        polypy.SGN_NE_0, 
        polypy.SGN_GT_0, 
        polypy.SGN_GE_0
        ]

sgn_name = {
            polypy.SGN_LT_0 : "<  0",
            polypy.SGN_LE_0 : "<= 0", 
            polypy.SGN_EQ_0 : "== 0",
            polypy.SGN_NE_0 : "!= 0",
            polypy.SGN_GT_0 : ">  0",
            polypy.SGN_GE_0 : ">= 0"
}

polypy_test.start("Polynomial Feasibility Integer")

assignment = polypy.Assignment()

p1 = x**2 - 7
S1 = p1.feasible_set(assignment, polypy.SGN_GE_0) # (-inf, -2.6], [2.6, +inf)
p2 = x*(x - 4)
S2 = p2.feasible_set(assignment, polypy.SGN_LT_0) # (0, 4)

S = S1.intersect(S2)
v = S.pick_value();
polypy_test.check(v.to_double() == 3)

polypy_test.start("Feasibility Intervals Intersection")

assignment = polypy.Assignment()