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) p = x**2 - 1 one_neg = polypy.AlgebraicNumber(p, 0)
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) assignment.set_value(y, sqrt2_4) assignment.set_value(z, sqrt3) # print assignment p = x + y + z p_value = p.evaluate(assignment) check_value(p, assignment, p_value, 4.335471485) p = x**2
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) p = x**2 - 1 one_neg = polypy.AlgebraicNumber(p, 0)
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) assignment = polypy.Assignment() assignment.set_value(y, polypy.AlgebraicNumber(x**2 - 2, 1)) assignment.set_value(z, polypy.AlgebraicNumber(x**2 - 3, 1)) # [-0.809841] # -1 0 1 p_slow = ((2 * z) * y**2) * x**3 + ((1 * z**3) * y) * x**2 + ( (1 * z**2) * y**3) * x + (1 * z + 4) p_expected = [1, 1, 1, 2, 1, 1] check_feasible(p_slow, x, assignment, p_expected) p_slow = (1 * y**3 + (2 * z**3) * y) * x**3 + ((2 * z) * y**3) * x**2 + ( (2 * z**2) * y**2 + 3) # for random in xrange(1000): # start = time.time() # p = polypy_test.random_polynomial(3, 2, [x, y, z], 5)
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)) check_comparison(sqrt2_neg, -1.41421356237) check_comparison(sqrt2_pos, 1.41421356237) p = x**2 - 3 sqrt3_neg = polypy.Value(polypy.AlgebraicNumber(p, 0)) sqrt3_pos = polypy.Value(polypy.AlgebraicNumber(p, 1)) check_comparison(sqrt3_neg, -1.73205080757) check_comparison(sqrt3_pos, 1.73205080757) half_rat = one_int / two_int third_rat = one_int / three_int
# 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) roots = p.roots_isolate(assignment) check_roots( p, assignment, roots, [-166318.899794716301953293454440, -8961.48539760581031126063921576]) polypy.variable_order.set([x, w, y, z])
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) assignment.set_value(z, sqrt3) # print assignment p = (x**2 - y)*z check_sgn(p, assignment, 0) p = (x**2 - y + z)*x check_sgn(p, assignment, 1)