예제 #1
0
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)}"
    )
예제 #2
0
 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)
예제 #3
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)
예제 #4
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)
예제 #5
0
 def test_fraction_reject(self):
     """reject negative input with fractional exponents"""
     with self.assertRaises(ValueError):
         polynomial_reject_fraction = auc.Polynomial({2.5: 1})
         polynomial_reject_fraction.evaluate(-2)
예제 #6
0
 def test_frac_ok(self):
     """correctly evaluate valid polynomial with fraction"""
     polynomial_ok = auc.Polynomial({1.5: 1})
     assert polynomial_ok.evaluate(0) == 0
     assert polynomial_ok.evaluate(2) == 2 * math.sqrt(2)
예제 #7
0
 def test_constant_ok(self):
     """correctly evaluate valid polynomial f(x)=c"""
     polynomial_ok = auc.Polynomial({0: 5})
     assert polynomial_ok.evaluate(3) == 5
     assert str(polynomial_ok) == "f(x)=5"
예제 #8
0
 def test_string_rep_ok_2(self):
     """test string reprentation of polynomial"""
     polynomial_1 = auc.Polynomial({0: 5})
     assert str(polynomial_1) == "f(x)=5"
예제 #9
0
 def test_zero_ok(self):
     """correctly evaluate valid polynomial f(x)=0"""
     polynomial_ok = auc.Polynomial({0: 0})
     assert polynomial_ok.evaluate(5) == 0
     assert str(polynomial_ok) == "f(x)=0"
예제 #10
0
 def test_string_rep_ok_1(self):
     """test string reprentation of polynomial"""
     polynomial_1 = auc.Polynomial({0: -2.5, 1: 1.5, 3: 2, 4: 1})
     assert str(polynomial_1) == "f(x)=x^4 + 2x^3 + 1.5x + -2.5"
예제 #11
0
 def test_int_ok(self):
     """correctly evaluate valid polynomial"""
     polynomial_ok = auc.Polynomial({2: 3, 1: 4, 0: 5})
     assert polynomial_ok.evaluate(-2) == 9
     assert polynomial_ok.evaluate(0) == 5
     assert polynomial_ok.evaluate(2) == 25
예제 #12
0
 def test_negative_exp_frac_reject(self):
     """reject negative fractional exponents"""
     with self.assertRaises(ValueError):
         auc.Polynomial({-2.5: 1})
예제 #13
0
 def test_negative_exp_reject(self):
     """reject negative exponents"""
     with self.assertRaises(ValueError):
         auc.Polynomial({-2: 1})