Esempio n. 1
0
def check_feasible(p, var, assignment, expected):
    ok = True
    for sgn, expected in zip(sgns, expected):
        p_feasible = p.feasible_intervals(assignment, sgn)
        #         print "p =", p
        #         print "sgn =", sgn_name[sgn]
        #         print "expected =", expected
        #         print "p_feasible =", p_feasible
        ok = len(p_feasible) == expected
        if (not ok):
            print "expected =", expected
            print "p_feasible =", p_feasible
        if ok:
            for I in p_feasible:
                v = I.pick_value()
                ok = I.contains(v)
                if (not ok):
                    print "I =", I
                    print "v =", v
                    break
        if (not ok):
            print "p =", p
            print "assignment =", assignment
            print "sgn =", sgn_name[sgn]
        polypy_test.check(ok)
Esempio n. 2
0
def check_feasible(p, var, assignment, expected):
    ok = True
    for sgn, expected in zip(sgns, expected):
        p_feasible = p.feasible_intervals(assignment, sgn)        
#         print "p =", p
#         print "sgn =", sgn_name[sgn]            
#         print "expected =", expected
#         print "p_feasible =", p_feasible        
        ok = len(p_feasible) == expected
        if (not ok):
            print "expected =", expected
            print "p_feasible =", p_feasible        
        if ok:
            for I in p_feasible:             
                v = I.pick_value()
                ok = I.contains(v)
                if (not ok):
                    print "I =", I
                    print "v =", v
                    break 
        if (not ok):
            print "p =", p
            print "assignment =", assignment
            print "sgn =", sgn_name[sgn]            
        polypy_test.check(ok)
def check_unary(op, op_name, p, expected):
    result = op(p)
    ok = result == expected
    if (not ok):
        print "p =", p
        print op_name, "=", result
        print "expected =", expected
    polypy_test.check(ok)
Esempio n. 4
0
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)
def check_psc(p, q, expected):
    psc = p.psc(q)
    ok = cmp(psc, expected) == 0
    if (not ok):
        print "p =", p
        print "q =", q
        print "psc      =", psc
        print "expected =", expected
    polypy_test.check(ok)
Esempio n. 6
0
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)
Esempio n. 7
0
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)
Esempio n. 8
0
def check_lcm(p, q, expected):
    gcd = p.lcm(q)
    ok = gcd == expected
    if (not ok):
        print "p =", p
        print "q =", q
        print "expected =", expected
        print "gcd = ", gcd
    polypy_test.check(ok)
Esempio n. 9
0
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)
Esempio n. 10
0
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)
Esempio n. 11
0
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)
Esempio n. 12
0
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)
Esempio n. 13
0
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)
Esempio n. 14
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)
Esempio n. 15
0
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 = {0}".format(p))
        print("assignment = {0}".format(assignment))
        print("value = {0}".format(value))
        print("value_double = {0}".format(value_double))
        print("expected_value = {0}".format(expected_value))
    polypy_test.check(ok)
Esempio n. 16
0
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)
Esempio n. 17
0
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)
Esempio n. 18
0
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)
Esempio n. 19
0
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)
Esempio n. 20
0
def check_roots_isolate(p, expected):
    roots = p.roots_isolate()
    sorted = all(roots[i] < roots[i+1] for i in xrange(len(roots)-1))
    count = len(roots)
    if ((not sorted) or count != expected):
        print "p = ", p
        print "sorted = ", sorted
        print "count = ", count
        print "expected = ", expected
        polypy_test.check(False)
    else:
        polypy_test.check(True)
Esempio n. 21
0
def check_roots_isolate(p, expected):
    roots = p.roots_isolate()
    sorted = all(roots[i] < roots[i + 1] for i in range(len(roots) - 1))
    count = len(roots)
    if ((not sorted) or count != expected):
        print("p = {0}".format(p))
        print("sorted = {0}".format(sorted))
        print("count = {0}".format(count))
        print("expected = {0}".format(expected))
        polypy_test.check(False)
    else:
        polypy_test.check(True)
Esempio n. 22
0
def check_roots_count(p, expected, lb = None, ub = None):
    if (lb is None):
        count = p.roots_count()
    else:
        count = p.roots_count(lb, ub)
        
    if (count != expected):
        polypy_test.check(False)
        print "p =", p
        print "lb =", lb
        print "ub =", ub
        print "count =", count
        print "expected = ", expected 
    else:
        polypy_test.check(True)
Esempio n. 23
0
def check_roots_count(p, expected, lb=None, ub=None):
    if (lb is None):
        count = p.roots_count()
    else:
        count = p.roots_count(lb, ub)

    if (count != expected):
        polypy_test.check(False)
        print("p = {0}".format(p))
        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)
Esempio n. 24
0
def check_roots(p, assignment, roots, expected_roots):
    ok = len(roots) == len(expected_roots)
    if ok: 
        for root, expected_root in zip(roots, expected_roots):
            root_double = root.to_double()
            ok = abs(root_double - expected_root) < 0.000001
            if (not ok):
                print "p =", p
                print "assignment =", assignment
                print "root =", root
                print "expected_root =", expected_root
                break
    else:
        print "p =", p
        print "assignment =", assignment
        print "roots =", roots
        print "expected_roots =", expected_roots
    polypy_test.check(ok)
Esempio n. 25
0
def check_roots(p, assignment, roots, expected_roots):
    ok = len(roots) == len(expected_roots)
    if ok:
        for root, expected_root in zip(roots, expected_roots):
            root_double = root.to_double()
            ok = abs(root_double - expected_root) < 0.000001
            if (not ok):
                print("p = {0}".format(p))
                print("assignment = {0}".format(assignment))
                print("root = {0}".format(root))
                print("expected_root = {0}".format(expected_root))
                break
    else:
        print("p = {0}".format(p))
        print("assignment = {0}".format(assignment))
        print("roots = {0}".format(roots))
        print("expected_roots = {0}".format(expected_roots))
    polypy_test.check(ok)
Esempio n. 26
0
def check_roots(p, assignment, roots, expected_roots):
    ok = len(roots) == len(expected_roots)
    if ok:
        for root, expected_root in zip(roots, expected_roots):
            root_double = root.to_double()
            ok = abs(root_double - expected_root) < 0.000001
            if (not ok):
                print "p =", p
                print "assignment =", assignment
                print "root =", root
                print "expected_root =", expected_root
                break
    else:
        print "p =", p
        print "assignment =", assignment
        print "roots =", roots
        print "expected_roots =", expected_roots
    polypy_test.check(ok)
Esempio n. 27
0
def check_feasible(p, var, assignment, expected):
    ok = True
    for sgn, expected in zip(sgns, expected):
        p_feasible = p.feasible_intervals(assignment, sgn)
        ok = len(p_feasible) == expected
        if (not ok):
            print("expected = {0}".format(expected))
            print("p_feasible = {0}".format(p_feasible))
        if ok:
            for I in p_feasible:
                v = I.pick_value()
                ok = I.contains(v)
                if (not ok):
                    print("I = {0}".format(I))
                    print("v = {0}".format(v))
                    break
        if (not ok):
            print("p = {0}".format(p))
            print("assignment = {0}".format(assignment))
            print("sgn = {0}".format(sgn_name[sgn]))
        polypy_test.check(ok)
Esempio n. 28
0
def check_factorization(p, debug=False, check_product = True):
    global polypy_test
    if debug:
        print "check_factorization:"
        print "p =", p
    # Do the factorization
    factorization = p.factor()
    C, factors = factorization[0], factorization[1:]
    if debug:
        print "C =", C
        print "factors =", factors
    # The constant should always be positive
    if C <= 0:
        print "Wrong factorization (constant)"
        print "p =", p
        print "C =", C
        print "factors =", factors
        polypy_test.check(False)
        return
    # Check if factorization multiplys to the input
    if check_product:
        product = C
        for (f, d) in factors:
            if (f.degree() == 0):
                print "Wrong factorization (constant factor)"
                print "p =", p 
                print "factors =", factors
                polypy_test.check(False)
            product = product * f**d    
        if (p != product):
            print "Wrong factorization (product mismatch)"
            print "p =", p 
            print "product =", product
            print "factors =", factors
            polypy_test.check(False)
            return
    # Done, we're OK
    polypy_test.check(True)        
Esempio n. 29
0
def check_factorization(p, debug=False, check_product=True):
    global polypy_test
    if debug:
        print("check_factorization:")
        print("p = {0}".format(p))
    # Do the factorization
    factorization = p.factor()
    C, factors = factorization[0], factorization[1:]
    if debug:
        print("C = {0}".format(C))
        print("factors = {0}".format(factors))
    # The constant should always be positive
    if C <= 0:
        print("Wrong factorization (constant)")
        print("p = {0}".format(p))
        print("C = {0}".format(C))
        print("factors = {0}".format(factors))
        polypy_test.check(False)
        return
    # Check if factorization multiplies to the input
    if check_product:
        product = C
        for (f, d) in factors:
            if (f.degree() == 0):
                print("Wrong factorization (constant factor)")
                print("p = {0}".format(p))
                print("factors = {0}".format(factors))
                polypy_test.check(False)
            product = product * f**d
        if (p != product):
            print("Wrong factorization (product mismatch)")
            print("p       =  {0}".format(p))
            print("product =  {0}".format(product))
            print("factors =  {0}".format(factors))
            polypy_test.check(False)
            return
    # Done, we're OK
    polypy_test.check(True)
Esempio n. 30
0
def check_factorization(p, debug=False, check_product=True):
    global polypy_test
    if debug:
        print "check_factorization:"
        print "p =", p
    # Do the factorization
    factorization = p.factor()
    C, factors = factorization[0], factorization[1:]
    if debug:
        print "C =", C
        print "factors =", factors
    # The constant should always be positive
    if C <= 0:
        print "Wrong factorization (constant)"
        print "p =", p
        print "C =", C
        print "factors =", factors
        polypy_test.check(False)
        return
    # Check if factorization multiplys to the input
    if check_product:
        product = C
        for (f, d) in factors:
            if (f.degree() == 0):
                print "Wrong factorization (constant factor)"
                print "p =", p
                print "factors =", factors
                polypy_test.check(False)
            product = product * f**d
        if (p != product):
            print "Wrong factorization (product mismatch)"
            print "p =", p
            print "product =", product
            print "factors =", factors
            polypy_test.check(False)
            return
    # Done, we're OK
    polypy_test.check(True)
Esempio n. 31
0
a = polypy.AlgebraicNumber(x**2 - 2, 0)
b = polypy.AlgebraicNumber(x**2 - 2, 1)
add = a + b
check_comparison(add, zero, "==", (add == zero), True)

a = polypy.AlgebraicNumber(x**2 - 2, 1)
b = polypy.AlgebraicNumber(x**2 - 3, 1)
ab = polypy.AlgebraicNumber(x**4 + -10 * x**2 + 1, 3)
add = a + b
check_comparison(add, ab, "==", (add == ab), True)

polypy_test.start("Division")

one = polypy.AlgebraicNumber(x - 1, 0)

a_list = [sqrt3_pos, sqrt3_neg, sqrt2_pos, sqrt2_neg]
z_list = [polypy.AlgebraicNumber(x - k, 0) for k in [-3, -1, 1, 3]]
q_list = [polypy.AlgebraicNumber(2 * x - k, 0) for k in [-3, -1, 1, 3]]
numbers = a_list + z_list + q_list
sum_list = [a + b for (a, b) in itertools.product(numbers, numbers)]
todo = [a for a in (numbers + sum_list) if a != zero]

for k in range(1, 500):
    sample = random.sample(todo, 3)
    random.shuffle(sample)
    p = functools.reduce(lambda x, y: x * y, sample, one)
    random.shuffle(sample)
    p = functools.reduce(lambda x, y: x / y, sample, p)
    polypy_test.check(p == 1)
Esempio n. 32
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()
assignment.set_value(y, 0)
assignment.set_value(z, 1)

p1 = (x**2 - y) * (x**2 - 2 * z)
p2 = (x**2 - y) * (x**2 - 3 * z)

S1 = p1.feasible_set(assignment, polypy.SGN_NE_0)
S2 = p2.feasible_set(assignment, polypy.SGN_LE_0)
P = S1.intersect(S2)
polypy_test.check(True)
Esempio n. 33
0
def check_gcd_single(p, q):
    global polypy_test
    gcd = p.gcd(q)
    ok = polypy_test.sympy_checker.check_gcd(p, q, gcd)
    polypy_test.check(ok)
Esempio n. 34
0
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)
Esempio n. 35
0
def check_str(o, expected):
    ok = str(o) == expected
    if (not ok):
        print "o =", o
        print "expected =", expected
    polypy_test.check(ok)
Esempio n. 36
0
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)
Esempio n. 37
0
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)
Esempio n. 38
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()
assignment.set_value(y, 0)
assignment.set_value(z, 1)

p1 = (x**2 - y)*(x**2 - 2*z)
p2 = (x**2 - y)*(x**2 - 3*z)

S1 = p1.feasible_set(assignment, polypy.SGN_NE_0);
S2 = p2.feasible_set(assignment, polypy.SGN_LE_0);
P = S1.intersect(S2);
polypy_test.check(True)
Esempio n. 39
0
def check_gcd_single(p, q):
    global polypy_test
    gcd = p.gcd(q)
    ok = polypy_test.sympy_checker.check_gcd(p, q, gcd)
    polypy_test.check(ok)
Esempio n. 40
0
def check_str(o, expected):
    ok = str(o) == expected
    if (not ok):
        print "o =", o
        print "expected =", expected
    polypy_test.check(ok)