def main(): """Simple demo""" logging.info( "Try out some sample polynomials, bounds, step sizes, and algorithms.") trapezoid = auc.get_algorithm("trapezoid") midpoint = auc.get_algorithm("midpoint") simpson = auc.get_algorithm("simpson") bounds_simple_1 = auc.Bounds(0, 10, .1) bounds_simple_2 = auc.Bounds(0, 10, 1) bounds_symmetric_1 = auc.Bounds(-5, 5, .1) polynomial_simple_cubic = auc.Polynomial({3: 1}) polynomial_simple_fractional = auc.Polynomial({.5: 1}) LOGGER.info("-Demo 1") LOGGER.info( f"Area={auc.area_under_curve(polynomial_simple_cubic, bounds_simple_1, midpoint)}" ) LOGGER.info("\n-Demo 2 -- larger step size, lower accuracy") LOGGER.info( f"Area={auc.area_under_curve(polynomial_simple_cubic, bounds_simple_2, midpoint)}" ) LOGGER.info( "\n-Demo 3 -- symmetric bounds and a symmetric function (net area close to zero)" ) LOGGER.info( f"Area={auc.area_under_curve(polynomial_simple_cubic, bounds_symmetric_1, simpson)}" ) LOGGER.info("\n-Demo 4 -- fractional exponents") # integral of f(x)=x^.5 is (x^1.5)1.5 + sc, or (10*sqrt(10))/1.5 with these bounds LOGGER.info( f"Area={auc.area_under_curve(polynomial_simple_fractional, bounds_simple_1, trapezoid)}" )
def test_bounds_ok3(self): """Check that large step sizes don't cause roundoff error problems""" bounds_ok = auc.Bounds(0, 5, 1) assert bounds_ok.lower_bound == 0 assert bounds_ok.upper_bound == 5 assert bounds_ok.step_size == 1 assert len(bounds_ok.full_range) == 6 #[0,1,2,3,4,5]
def test_simple_area_5(self): """area test 5""" bounds = auc.Bounds(-5, 5, .01) polynomial = auc.Polynomial({3: 1}) # f(x) = x^3 algorithm = auc.get_algorithm("midpoint") area = auc.area_under_curve(polynomial, bounds, algorithm) self.assertAlmostEqual(area, 0)
def test_simple_area_2(self): """area test 2""" bounds = auc.Bounds(0, 10, .1) polynomial = auc.Polynomial({2: 1}) # f(x) = x^2 algorithm = auc.get_algorithm("simpson") area = auc.area_under_curve(polynomial, bounds, algorithm) self.assertAlmostEqual(area, 1000.0 / 3.0)
def test_simple_area_1(self): """area test 1""" bounds = auc.Bounds(0, 10, .1) polynomial = auc.Polynomial({1: 1}) # f(x) = x algorithm = auc.get_algorithm("trapezoid") area = auc.area_under_curve(polynomial, bounds, algorithm) self.assertAlmostEqual(area, 50)
def test_bounds_ok2(self): """basic bounds check""" bounds_ok = auc.Bounds(0, 100, .01) assert bounds_ok.lower_bound == 0 assert bounds_ok.upper_bound == 100 assert bounds_ok.step_size == .01 assert len(bounds_ok.full_range) == 10001
def test_bounds_ok(self): """basic bounds check""" bounds_ok = auc.Bounds(2, 4, .1) assert bounds_ok.lower_bound == 2 assert bounds_ok.upper_bound == 4 assert bounds_ok.step_size == .1 assert len(bounds_ok.full_range) == 21
def test_bounds_string_rep(self): """test string representation of bounds""" bounds = auc.Bounds(-2, 2.5, .1) bounds_str = str(bounds) assert bounds_str == "Bounds: [-2 - 2.5], step_size: 0.1"
def test_bad_bounds_2(self): """reject if lower bound <= upper bound""" with self.assertRaises(ValueError): auc.Bounds(2, 1, 1)
def test_bad_step_size_2(self): """reject invalid step size""" with self.assertRaises(ValueError): auc.Bounds(2, 4, -.1)