1#!/usr/bin/env python 2 3import polypy 4import polypy_test 5 6polypy_test.init() 7 8[x, y, z] = [polypy.Variable(name) for name in ['x', 'y', 'z']] 9polypy.variable_order.set([z, y, x]) 10 11def check_value(p, assignment, value, expected_value): 12 value_double = value.to_double() 13 ok = abs(value_double - expected_value) < 0.000001 14 if (not ok): 15 print("p = {0}".format(p)) 16 print("assignment = {0}".format(assignment)) 17 print("value = {0}".format(value)) 18 print("value_double = {0}".format(value_double)) 19 print("expected_value = {0}".format(expected_value)) 20 polypy_test.check(ok) 21 22polypy_test.start("Polynomial Evaluation") 23 24# polypy.trace_enable("polynomial") 25# polypy.trace_enable("factorization") 26# polypy.trace_enable("algebraic_number") 27# polypy.trace_enable("coefficient") 28# polypy.trace_enable("coefficient::sgn") 29# polypy.trace_enable("coefficient::roots") 30# polypy.trace_enable("coefficient::arith") 31 32sqrt2 = polypy.AlgebraicNumber(x**2 - 2, 1) 33sqrt2_4 = polypy.AlgebraicNumber(x**4 - 2, 1) 34sqrt3 = polypy.AlgebraicNumber(x**2 - 3, 1) 35 36assignment = polypy.Assignment() 37assignment.set_value(x, sqrt2) 38assignment.set_value(y, sqrt2_4) 39assignment.set_value(z, sqrt3) 40 41# print assignment 42 43p = x + y + z 44p_value = p.evaluate(assignment) 45check_value(p, assignment, p_value, 4.335471485) 46 47p = x**2 48p_value = p.evaluate(assignment) 49check_value(p, assignment, p_value, 2) 50 51p = x**2 + y**4 52p_value = p.evaluate(assignment) 53check_value(p, assignment, p_value, 4) 54 55p = x**2 + y**4 + z**2 56p_value = p.evaluate(assignment) 57check_value(p, assignment, p_value, 7) 58