Пример #1
0
def generate_integration_tests():
    def print_(f, msg):
        f.write("\t" + msg + "\n")

    integrators_names = {"gauss_kronrod": "gauss_kronrod()",
                         "simplex": "simplex()", "gauss_legendre": "gauss_legendre()",
                         "gauss_lobatto": "gauss_lobatto()",
                         "simplex_steps_1000": "simplex(steps=1000)",
                         "gauss_legendre_steps_500": "gauss_legendre(steps=500)",
                         "gauss_lobatto_max_iter_127": "gauss_lobatto(max_number_of_iterations = 255, abs_tolerance = 1.0e-7)",
                         "gauss_kronrod_check_rel_tolerance": "gauss_kronrod(check_abs_tolerance = False, check_rel_tolerance = True)",
                         "gauss_kronrod_check_abs_tolerance_check_rel_tolerance": "gauss_kronrod(check_abs_tolerance = True, check_rel_tolerance = True)"
                         }
    fcts_names = ("lambda x:2", "lambda x:x*x", "lambda x:exp(-x*x)",
                  "lambda x: x*x*x if x > 1 else x-2")

    integrators = {'gauss_legendre': gauss_legendre(), 'gauss_lobatto': gauss_lobatto(),
                   'simplex_steps_1000': simplex(steps=1000), 'simplex': simplex(), 'gauss_kronrod': gauss_kronrod(),
                   'gauss_legendre_steps_500': gauss_legendre(steps=500),
                   'gauss_kronrod_check_abs_tolerance_check_rel_tolerance': gauss_kronrod(check_abs_tolerance=True,
                                                                                          check_rel_tolerance=True),
                   'gauss_lobatto_max_iter_127': gauss_lobatto(max_number_of_iterations=255, abs_tolerance=1.0e-7),
                   'gauss_kronrod_check_rel_tolerance': gauss_kronrod(check_abs_tolerance=False,
                                                                      check_rel_tolerance=True)}
    fcts = (lambda x: 2, lambda x: x * x, lambda x: exp(-x * x), lambda x: x * x * x if x > 1 else x - 2)

    '''
    s = []
    for int_name, i in integrators_names.items():
        s.append("'" + int_name + "':" + i)
    print("integrators = {" + ", ".join(s) + "}")
    print("fcts = (" + ", ".join(fcts_names) + ")")
        
    
    '''
    integration_boundaries = ((-1.0, 2.0), (0.0, 1.0), (-100.0, 99.0))
    file_name = r"/Users/MarcusSteinkamp/Documents/integrator_test_code.txt"
    fi = open(file_name, "w")
    c = 0
    i_fct = 0
    for n, i in integrators_names.items():
        i_fct = 0
        for f in fcts_names:
            for l, u in integration_boundaries:
                print_(fi, "def test_integrator_" + str(c).zfill(3) + "_" + n + "(self):")
                print_(fi, "\tfct = " + f)
                print_(fi, "\tintegrator = " + i)
                print_(fi, "\tresult = integrator(fct, " + str(l) + ", " + str(u) + ")")
                print_(fi, "\tbenchmark = " + "{:18.9f}".format(integrators[n](fcts[i_fct], l, u)))
                print_(fi, "\tself.assertAlmostEqual(benchmark, result, 8)")
                print_(fi, "")
                c = c + 1
            i_fct = i_fct + 1
Пример #2
0
 def test_integrator_063_gauss_legendre_steps_500(self):
     fct = lambda x: x * x
     integrator = gauss_legendre(steps=500)
     result = integrator(fct, -1.0, 2.0)
     benchmark = 1.74999999973
     self.assertAlmostEqual(benchmark, result, 8)
Пример #3
0
 def test_integrator_062_gauss_legendre_steps_500(self):
     fct = lambda x: 2
     integrator = gauss_legendre(steps=500)
     result = integrator(fct, -100.0, 99.0)
     benchmark = 397.99999997
     self.assertAlmostEqual(benchmark, result, 8)
Пример #4
0
 def test_integrator_061_gauss_legendre_steps_500(self):
     fct = lambda x: 2
     integrator = gauss_legendre(steps=500)
     result = integrator(fct, 0.0, 1.0)
     benchmark = 1.99999999985
     self.assertAlmostEqual(benchmark, result, 8)
Пример #5
0
 def test_integrator_001_gauss_legendre(self):
     fct = lambda x: 2
     integrator = gauss_legendre()
     result = integrator(fct, 0.0, 1.0)
     benchmark = 1.99999999998
     self.assertAlmostEqual(benchmark, result, 8)
Пример #6
0
 def test_integrator_070_gauss_legendre_steps_500(self):
     fct = lambda x: x * x * x if x > 1 else x - 2
     integrator = gauss_legendre(steps=500)
     result = integrator(fct, 0.0, 1.0)
     benchmark = -0.802422712344
     self.assertAlmostEqual(benchmark, result, 8)
Пример #7
0
 def test_integrator_068_gauss_legendre_steps_500(self):
     fct = lambda x: exp(-x * x)
     integrator = gauss_legendre(steps=500)
     result = integrator(fct, -100.0, 99.0)
     benchmark = 131.088205297
     self.assertAlmostEqual(benchmark, result, 8)
Пример #8
0
 def test_integrator_066_gauss_legendre_steps_500(self):
     fct = lambda x: exp(-x * x)
     integrator = gauss_legendre(steps=500)
     result = integrator(fct, -1.0, 2.0)
     benchmark = 1.97620409996
     self.assertAlmostEqual(benchmark, result, 8)
Пример #9
0
 def test_integrator_007_gauss_legendre(self):
     fct = lambda x: exp(-x * x)
     integrator = gauss_legendre()
     result = integrator(fct, 0.0, 1.0)
     benchmark = 0.658734700012
     self.assertAlmostEqual(benchmark, result, 8)
Пример #10
0
 def test_integrator_006_gauss_legendre(self):
     fct = lambda x: exp(-x * x)
     integrator = gauss_legendre()
     result = integrator(fct, -1.0, 2.0)
     benchmark = 1.97620410004
     self.assertAlmostEqual(benchmark, result, 8)
Пример #11
0
 def test_integrator_005_gauss_legendre(self):
     fct = lambda x: x * x
     integrator = gauss_legendre()
     result = integrator(fct, -100.0, 99.0)
     benchmark = 116.083333332
     self.assertAlmostEqual(benchmark, result, 8)
Пример #12
0
 def test_integrator_004_gauss_legendre(self):
     fct = lambda x: x * x
     integrator = gauss_legendre()
     result = integrator(fct, 0.0, 1.0)
     benchmark = 0.583333333327
     self.assertAlmostEqual(benchmark, result, 8)
Пример #13
0
 def test_integrator_003_gauss_legendre(self):
     fct = lambda x: x * x
     integrator = gauss_legendre()
     result = integrator(fct, -1.0, 2.0)
     benchmark = 1.74999999998
     self.assertAlmostEqual(benchmark, result, 8)
Пример #14
0
 def test_integrator_002_gauss_legendre(self):
     fct = lambda x: 2
     integrator = gauss_legendre()
     result = integrator(fct, -100.0, 99.0)
     benchmark = 397.999999996
     self.assertAlmostEqual(benchmark, result, 8)
Пример #15
0
 def test_integrator_064_gauss_legendre_steps_500(self):
     fct = lambda x: x * x
     integrator = gauss_legendre(steps=500)
     result = integrator(fct, 0.0, 1.0)
     benchmark = 0.583333333242
     self.assertAlmostEqual(benchmark, result, 8)
Пример #16
0
 def test_integrator_065_gauss_legendre_steps_500(self):
     fct = lambda x: x * x
     integrator = gauss_legendre(steps=500)
     result = integrator(fct, -100.0, 99.0)
     benchmark = 116.083333315
     self.assertAlmostEqual(benchmark, result, 8)
Пример #17
0
 def test_integrator_008_gauss_legendre(self):
     fct = lambda x: exp(-x * x)
     integrator = gauss_legendre()
     result = integrator(fct, -100.0, 99.0)
     benchmark = 131.088205302
     self.assertAlmostEqual(benchmark, result, 8)
Пример #18
0
 def test_integrator_067_gauss_legendre_steps_500(self):
     fct = lambda x: exp(-x * x)
     integrator = gauss_legendre(steps=500)
     result = integrator(fct, 0.0, 1.0)
     benchmark = 0.658734699986
     self.assertAlmostEqual(benchmark, result, 8)
Пример #19
0
 def test_integrator_009_gauss_legendre(self):
     fct = lambda x: x * x * x if x > 1 else x - 2
     integrator = gauss_legendre()
     result = integrator(fct, -1.0, 2.0)
     benchmark = -2.43433980503
     self.assertAlmostEqual(benchmark, result, 8)
Пример #20
0
 def test_integrator_069_gauss_legendre_steps_500(self):
     fct = lambda x: x * x * x if x > 1 else x - 2
     integrator = gauss_legendre(steps=500)
     result = integrator(fct, -1.0, 2.0)
     benchmark = -2.40726813703
     self.assertAlmostEqual(benchmark, result, 8)
Пример #21
0
 def test_integrator_010_gauss_legendre(self):
     fct = lambda x: x * x * x if x > 1 else x - 2
     integrator = gauss_legendre()
     result = integrator(fct, 0.0, 1.0)
     benchmark = -0.811446601677
     self.assertAlmostEqual(benchmark, result, 8)
Пример #22
0
 def test_integrator_071_gauss_legendre_steps_500(self):
     fct = lambda x: x * x * x if x > 1 else x - 2
     integrator = gauss_legendre(steps=500)
     result = integrator(fct, -100.0, 99.0)
     benchmark = -497.499999963
     self.assertAlmostEqual(benchmark, result, 8)
Пример #23
0
 def test_integrator_000_gauss_legendre(self):
     fct = lambda x: 2
     integrator = gauss_legendre()
     result = integrator(fct, -1.0, 2.0)
     benchmark = 5.99999999994
     self.assertAlmostEqual(benchmark, result, 8)