def test_integrate1d(): def f(x, y): return x * x + y def intf(x, y): return x * x * x / 3. + y * x bound = (-2., 1.) y = 3. integral = integrate1d(f, bound, 1000, tuple([y])) analytic = intf(bound[1], y) - intf(bound[0], y) assert_almost_equal(integral, analytic)
def test_integrate1d_analytic(): class temp: def __call__(self, x, m , c): return m * x ** 2 + c def integrate(self, bound, nint, m, c): a, b = bound return b - a # (wrong on purpose) bound = (0., 10.) f = temp() integral = integrate1d(f, bound, 10, (2., 3.)) assert_equal(integral, bound[1] - bound[0])
def test_integrate1d(): def f(x, y): return x * x + y def intf(x, y): return x * x * x / 3. + y * x bound = (-2., 1.) y = 3. integral = integrate1d(f, bound, 1000, tuple([y])) analytic = intf(bound[1], y) - intf(bound[0], y) assert_allclose(integral, analytic)
def test_integrate1d_analytic(): class temp: def __call__(self, x, m, c): return m * x ** 2 + c def integrate(self, bound, nint, m, c): a, b = bound return b - a # (wrong on purpose) bound = (0., 10.) f = temp() integral = integrate1d(f, bound, 10, (2., 3.)) assert_allclose(integral, bound[1] - bound[0])
def test_addpdfnorm_analytical_integrate(): def f(x, y, z): return x + 2 * y + 3 * z def g(x, z, p): return 4 * x + 5 * z + 6 * z def p(x, y, q): return 7 * x + 8 * y + 9 * q f.integrate = lambda bound, nint, y, z: 1. g.integrate = lambda bound, nint, z, p: 2. p.integrate = lambda bound, nint, y, q: 3. q = AddPdfNorm(f, g, p) assert_equal(describe(q), ['x', 'y', 'z', 'p', 'q', 'f_0', 'f_1']) integral = integrate1d(q, (-10., 10.), 100, (1., 2., 3., 4., 0.1, 0.2)) assert_almost_equal(integral, 0.1 * 1. + 0.2 * 2. + 0.7 * 3.)
def test_add_pdf(): def f(x, y, z): return x + y + z def g(x, a, b): return 2 * (x + a + b) def h(x, c, a): return 3 * (x + c + a) A = AddPdf(f, g, h) assert_equal(tuple(describe(A)), ('x', 'y', 'z', 'a', 'b', 'c')) ret = A(1, 2, 3, 4, 5, 6, 7) expected = f(1, 2, 3) + g(1, 4, 5) + h(1, 6, 4) assert_almost_equal(ret, expected) # wrong integral on purpose f.integrate = lambda bound, nint, y, z : 1. # unbound method works too g.integrate = lambda bound, nint, a, b : 2. h.integrate = lambda bound, nint, c, a : 3. assert_equal(integrate1d(A, (-10., 10.), 100, (1., 2., 3., 4., 5.)), 6.)
def test_addpdfnorm_analytical_integrate(): def f(x, y, z): return x + 2 * y + 3 * z def g(x, z, p): return 4 * x + 5 * z + 6 * z def p(x, y, q): return 7 * x + 8 * y + 9 * q f.integrate = lambda bound, nint, y, z: 1.0 g.integrate = lambda bound, nint, z, p: 2.0 p.integrate = lambda bound, nint, y, q: 3.0 q = AddPdfNorm(f, g, p) assert_equal(describe(q), ["x", "y", "z", "p", "q", "f_0", "f_1"]) integral = integrate1d(q, (-10.0, 10.0), 100, (1.0, 2.0, 3.0, 4.0, 0.1, 0.2)) assert_almost_equal(integral, 0.1 * 1.0 + 0.2 * 2.0 + 0.7 * 3.0)
def test_addpdfnorm_analytical_integrate(): def f(x, y, z): return x + 2 * y + 3 * z def g(x, z, p): return 4 * x + 5 * z + 6 * z def p(x, y, q): return 7 * x + 8 * y + 9 * q f.integrate = lambda bound, nint, y, z: 1.0 g.integrate = lambda bound, nint, z, p: 2.0 p.integrate = lambda bound, nint, y, q: 3.0 q = AddPdfNorm(f, g, p) assert describe(q) == ["x", "y", "z", "p", "q", "f_0", "f_1"] integral = integrate1d(q, (-10.0, 10.0), 100, (1.0, 2.0, 3.0, 4.0, 0.1, 0.2)) assert_almost_equal(integral, 0.1 * 1.0 + 0.2 * 2.0 + 0.7 * 3.0)
def test_add_pdf(): def f(x, y, z): return x + y + z def g(x, a, b): return 2 * (x + a + b) def h(x, c, a): return 3 * (x + c + a) A = AddPdf(f, g, h) assert_equal(tuple(describe(A)), ("x", "y", "z", "a", "b", "c")) ret = A(1, 2, 3, 4, 5, 6, 7) expected = f(1, 2, 3) + g(1, 4, 5) + h(1, 6, 4) assert_almost_equal(ret, expected) # wrong integral on purpose f.integrate = lambda bound, nint, y, z: 1.0 # unbound method works too g.integrate = lambda bound, nint, a, b: 2.0 h.integrate = lambda bound, nint, c, a: 3.0 assert_equal(integrate1d(A, (-10.0, 10.0), 100, (1.0, 2.0, 3.0, 4.0, 5.0)), 6.0)
def test_add_pdf(): def f(x, y, z): return x + y + z def g(x, a, b): return 2 * (x + a + b) def h(x, c, a): return 3 * (x + c + a) A = AddPdf(f, g, h) assert describe(A) == ["x", "y", "z", "a", "b", "c"] ret = A(1, 2, 3, 4, 5, 6, 7) expected = f(1, 2, 3) + g(1, 4, 5) + h(1, 6, 4) assert_almost_equal(ret, expected) # wrong integral on purpose f.integrate = lambda bound, nint, y, z: 1.0 # unbound method works too g.integrate = lambda bound, nint, a, b: 2.0 h.integrate = lambda bound, nint, c, a: 3.0 assert_equal(integrate1d(A, (-10.0, 10.0), 100, (1.0, 2.0, 3.0, 4.0, 5.0)), 6.0)
def test_Normalized(): f = ugaussian g = Normalized(f, (-1, 1)) norm = integrate1d(f, (-1.0, 1.0), 1000, (0.0, 1.0)) assert_almost_equal(g(1.0, 0.0, 1.0), f(1.0, 0.0, 1.0) / norm)
def test_Normalized(): f = ugaussian g = Normalized(f, (-1, 1)) norm = integrate1d(f, (-1., 1.), 1000, (0., 1.)) assert_almost_equal(g(1., 0., 1.), f(1., 0., 1.) / norm)