def test_interval(): """Tests known special cases of the interval. In this particular case, interval 0 to 1 with step size .2 for f(x) = 2x""" def f(x): value = 2 * x return value actual = [f(0), f(.2), f(.4), f(.6), f(.8), f(1)] trial = gaussian.interval(f, 0, 1, .2) print("Testing f(0):", actual[0], " ?= ", trial[0]) nose.tools.assert_almost_equal(actual[0], trial[0], 4) print("Testing f(.2):", actual[1], " ?= ", trial[1]) nose.tools.assert_almost_equal(actual[1], trial[1], 4) print("Testing f(.4):", actual[2], " ?= ", trial[2]) nose.tools.assert_almost_equal(actual[2], trial[2], 4) print("Testing f(.6):", actual[3], " ?= ", trial[3]) nose.tools.assert_almost_equal(actual[3], trial[3], 4) print("Testing interval f(.8):", actual[4], " ?= ", trial[4]) nose.tools.assert_almost_equal(actual[4], trial[4], 4) print("Testing interval f(1.0):", actual[5], " ?= ", trial[5]) nose.tools.assert_almost_equal(actual[5], trial[5], 4) len_actual = len(actual) len_trial = len(trial) print("Testing length of intervals:", len_actual, " ?= ", len_trial) nose.tools.assert_almost_equal(len_actual, len_trial)
def test_gauss_norm(): """Tests how accurately interval and integrate functions work on g when dx is samll. - integrate(interval(g,0,4,.05),.05) """ actual = 0.49996821729336 #using online trapazoidal rule calcuator trial = gaussian.integrate(gaussian.interval(gaussian.g, 0, 4, .05), .05) print("Testing integrate(interval(g,0,4,.05),.05): ", actual, " ?= ", trial) nose.tools.assert_almost_equal(actual, trial, 4)
def test_interval(): """Tests interval() with the following trials: - interval(x**2,0,10,1) ?= [0,1,4,9,16,25,36,49,64,81,100] """ def f(x): return x**2 actual = [0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100] trial = gaussian.interval(f, 0, 10, 1) print("Testing interval(x**2,0,10,1): ", actual, " ?= ", trial) nose.tools.assert_almost_equal(actual, trial, 4)
def test_gauss_norm(): """Checks for correct integral of gaussian function when dx is small Test: interval from -5 to 5 with dx = .000001""" actual = .999999 # This value was calculated with an online definite integral solver. trial = gaussian.integrate(gaussian.interval(gaussian.g, -5, 5, .000001), .000001) print( "This tests the interval from -5 to 5 with small step size of .000001: ", actual, "?=", trial) nose.tools.assert_almost_equal(actual, trial, 3)
def test_integrate(): """Checks the integration for correctness Test: integrate f(x) = 2x from 0 to 2 with spacing of steps at .000001 (dx=.000001)""" actual = 4 def f(x): value = 2 * x return value # This value was calculated with an online definite integral solver. i = gaussian.interval(f, 0, 2, .000001) trial = gaussian.integrate(i, .000001) print( "This tests the integration of 2x from 0 to 2 with step size of .000001: ", actual, "?=", trial) nose.tools.assert_almost_equal(actual, trial, 3)