def test_eval(self): zero = RatPoly() one = poly(1,0) _X = poly(1,1) _2X = poly(2, 1) _XSq = poly(1, 2) assert_approx(0.0, zero.eval(0.0), 0.0001) assert_approx(0.0, zero.eval(1.0), 0.0001) assert_approx(0.0, zero.eval(2.0), 0.0001) assert_approx(1.0, one.eval(0.0), 0.0001) assert_approx(1.0, one.eval(1.0), 0.0001) assert_approx(1.0, one.eval(2.0), 0.0001) assert_approx(0.0, _X.eval(0.0), 0.0001) assert_approx(1.0, _X.eval(1.0), 0.0001) assert_approx(2.0, _X.eval(2.0), 0.0001) assert_approx(0.0, _2X.eval(0.0), 0.0001) assert_approx(2.0, _2X.eval(1.0), 0.0001) assert_approx(4.0, _2X.eval(2.0), 0.0001) assert_approx(0.0, _XSq.eval(0.0), 0.0001) assert_approx(1.0, _XSq.eval(1.0), 0.0001) assert_approx(4.0, _XSq.eval(2.0), 0.0001) _XSq_minus_2X = _XSq - _2X assert_approx(0.0, _XSq_minus_2X.eval(0.0), 0.0001) assert_approx(-1.0, _XSq_minus_2X.eval(1.0), 0.0001) assert_approx(0.0, _XSq_minus_2X.eval(2.0), 0.0001) assert_approx(3.0, _XSq_minus_2X.eval(3.0), 0.0001)
def ops_with_nan(self, p): nan = RatPoly.from_str("nan") assert_eq_str(p + nan, "nan") assert_eq_str(nan + p, "nan") assert_eq_str(p - nan, "nan") assert_eq_str(nan - p, "nan") assert_eq_str(p * nan, "nan") assert_eq_str(nan * p, "nan") assert_eq_str(p / nan, "nan") assert_eq_str(nan / p, "nan")
def parse(s): return RatPoly.from_str(s)