コード例 #1
0
ファイル: test.py プロジェクト: pbmst/mathtoolspy
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
ファイル: test.py プロジェクト: pbmst/mathtoolspy
 def test_integrator_compare(self):
     i0 = simplex(steps=5000)
     i1 = gauss_kronrod()
     # i2 = gauss_legendre(steps = 1000) # gauss_legendre calculates bad.
     i3 = gauss_lobatto()
     fct = lambda x: cos(x)
     l = 0
     u = PI / 2.0
     r0 = i0(fct, l, u)
     r1 = i1(fct, l, u)
     # r2 = i2(fct, l, u)
     r3 = i3(fct, l, u)
     benchmark = 1.0
     self.assertAlmostEqual(r0, benchmark, 6)
     self.assertAlmostEqual(r1, benchmark, 6)
     self.assertAlmostEqual(r3, benchmark, 6)
コード例 #3
0
ファイル: test.py プロジェクト: pbmst/mathtoolspy
 def test_integrator_093_gauss_lobatto_max_iter_127(self):
     fct = lambda x: x * x * x if x > 1 else x - 2
     integrator = gauss_lobatto(max_number_of_iterations=255, abs_tolerance=1.0e-7)
     result = integrator(fct, -1.0, 2.0)
     benchmark = -0.250000000002
     self.assertAlmostEqual(benchmark, result, 8)
コード例 #4
0
ファイル: test.py プロジェクト: pbmst/mathtoolspy
 def test_integrator_092_gauss_lobatto_max_iter_127(self):
     fct = lambda x: exp(-x * x)
     integrator = gauss_lobatto(max_number_of_iterations=255, abs_tolerance=1.0e-7)
     result = integrator(fct, -100.0, 99.0)
     benchmark = 1.77245385091
     self.assertAlmostEqual(benchmark, result, 8)
コード例 #5
0
ファイル: test.py プロジェクト: pbmst/mathtoolspy
 def test_integrator_091_gauss_lobatto_max_iter_127(self):
     fct = lambda x: exp(-x * x)
     integrator = gauss_lobatto(max_number_of_iterations=255, abs_tolerance=1.0e-7)
     result = integrator(fct, 0.0, 1.0)
     benchmark = 0.746824132812
     self.assertAlmostEqual(benchmark, result, 8)
コード例 #6
0
ファイル: test.py プロジェクト: pbmst/mathtoolspy
 def test_integrator_090_gauss_lobatto_max_iter_127(self):
     fct = lambda x: exp(-x * x)
     integrator = gauss_lobatto(max_number_of_iterations=255, abs_tolerance=1.0e-7)
     result = integrator(fct, -1.0, 2.0)
     benchmark = 1.62890552357
     self.assertAlmostEqual(benchmark, result, 8)
コード例 #7
0
ファイル: test.py プロジェクト: pbmst/mathtoolspy
 def test_integrator_089_gauss_lobatto_max_iter_127(self):
     fct = lambda x: x * x
     integrator = gauss_lobatto(max_number_of_iterations=255, abs_tolerance=1.0e-7)
     result = integrator(fct, -100.0, 99.0)
     benchmark = 656766.3333333333
     self.assertAlmostEqual(benchmark, result, 8)
コード例 #8
0
ファイル: test.py プロジェクト: pbmst/mathtoolspy
 def test_integrator_085_gauss_lobatto_max_iter_127(self):
     fct = lambda x: 2
     integrator = gauss_lobatto(max_number_of_iterations=255, abs_tolerance=1.0e-7)
     result = integrator(fct, 0.0, 1.0)
     benchmark = 2.0
     self.assertAlmostEqual(benchmark, result, 8)
コード例 #9
0
ファイル: test.py プロジェクト: pbmst/mathtoolspy
 def test_integrator_015_gauss_lobatto(self):
     fct = lambda x: x * x
     integrator = gauss_lobatto()
     result = integrator(fct, -1.0, 2.0)
     benchmark = 3.0
     self.assertAlmostEqual(benchmark, result, 8)
コード例 #10
0
ファイル: test.py プロジェクト: pbmst/mathtoolspy
 def test_integrator_022_gauss_lobatto(self):
     fct = lambda x: x * x * x if x > 1 else x - 2
     integrator = gauss_lobatto()
     result = integrator(fct, 0.0, 1.0)
     benchmark = -1.5
     self.assertAlmostEqual(benchmark, result, 8)
コード例 #11
0
ファイル: test.py プロジェクト: pbmst/mathtoolspy
 def test_integrator_020_gauss_lobatto(self):
     fct = lambda x: exp(-x * x)
     integrator = gauss_lobatto()
     result = integrator(fct, -100.0, 99.0)
     benchmark = 1.77245385091
     self.assertAlmostEqual(benchmark, result, 8)
コード例 #12
0
ファイル: test.py プロジェクト: pbmst/mathtoolspy
 def test_integrator_019_gauss_lobatto(self):
     fct = lambda x: exp(-x * x)
     integrator = gauss_lobatto()
     result = integrator(fct, 0.0, 1.0)
     benchmark = 0.746824132812
     self.assertAlmostEqual(benchmark, result, 8)
コード例 #13
0
ファイル: test.py プロジェクト: pbmst/mathtoolspy
 def test_integrator_018_gauss_lobatto(self):
     fct = lambda x: exp(-x * x)
     integrator = gauss_lobatto()
     result = integrator(fct, -1.0, 2.0)
     benchmark = 1.62890552357
     self.assertAlmostEqual(benchmark, result, 8)
コード例 #14
0
ファイル: test.py プロジェクト: pbmst/mathtoolspy
 def test_integrator_017_gauss_lobatto(self):
     fct = lambda x: x * x
     integrator = gauss_lobatto()
     result = integrator(fct, -100.0, 99.0)
     benchmark = 656766.3333333333
     self.assertAlmostEqual(benchmark, result, 8)
コード例 #15
0
ファイル: test.py プロジェクト: pbmst/mathtoolspy
 def test_integrator_016_gauss_lobatto(self):
     fct = lambda x: x * x
     integrator = gauss_lobatto()
     result = integrator(fct, 0.0, 1.0)
     benchmark = 0.333333333333
     self.assertAlmostEqual(benchmark, result, 8)
コード例 #16
0
ファイル: test.py プロジェクト: pbmst/mathtoolspy
 def test_integrator_095_gauss_lobatto_max_iter_127(self):
     fct = lambda x: x * x * x if x > 1 else x - 2
     integrator = gauss_lobatto(max_number_of_iterations=255, abs_tolerance=1.0e-7)
     result = integrator(fct, -100.0, 99.0)
     benchmark = 24009698.499997977
     self.assertAlmostEqual(benchmark, result, 8)
コード例 #17
0
ファイル: test.py プロジェクト: pbmst/mathtoolspy
 def test_integrator_023_gauss_lobatto(self):
     fct = lambda x: x * x * x if x > 1 else x - 2
     integrator = gauss_lobatto()
     result = integrator(fct, -100.0, 99.0)
     benchmark = 24009698.5
     self.assertAlmostEqual(benchmark, result, 8)
コード例 #18
0
ファイル: test.py プロジェクト: pbmst/mathtoolspy
 def test_integrator_014_gauss_lobatto(self):
     fct = lambda x: 2
     integrator = gauss_lobatto()
     result = integrator(fct, -100.0, 99.0)
     benchmark = 398.0
     self.assertAlmostEqual(benchmark, result, 8)