From 6c0f966649c8dedd4115d6809e569732ee5709c9 Mon Sep 17 00:00:00 2001 From: Mario Mulansky Date: Mon, 19 Jan 2015 22:32:42 +0100 Subject: interval averages for discrete functions --- test/test_function.py | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) (limited to 'test') diff --git a/test/test_function.py b/test/test_function.py index ba87db8..da3d851 100644 --- a/test/test_function.py +++ b/test/test_function.py @@ -203,6 +203,33 @@ def test_pwl_avrg(): assert_array_almost_equal(f_avrg.y2, y2_expected, decimal=16) +def test_df(): + # testing discrete function + x = [0.0, 1.0, 2.0, 2.5, 4.0] + y = [0.0, 1.0, 1.0, 0.0, 1.0] + mp = [1.0, 2.0, 1.0, 2.0, 1.0] + f = spk.DiscreteFunction(x, y, mp) + xp, yp = f.get_plottable_data() + + xp_expected = [0.0, 1.0, 2.0, 2.5, 4.0] + yp_expected = [0.0, 0.5, 1.0, 0.0, 1.0] + assert_array_almost_equal(xp, xp_expected, decimal=16) + assert_array_almost_equal(yp, yp_expected, decimal=16) + + avrg_expected = 2.0 / 5.0 + assert_almost_equal(f.avrg(), avrg_expected, decimal=16) + + # interval averaging + a = f.avrg([0.5, 2.4]) + assert_almost_equal(a, 2.0/3.0, decimal=16) + a = f.avrg([1.5, 3.5]) + assert_almost_equal(a, 1.0/3.0, decimal=16) + a = f.avrg((0.9, 3.5)) + assert_almost_equal(a, 2.0/5.0, decimal=16) + a = f.avrg([1.1, 4.0]) + assert_almost_equal(a, 1.0/3.0, decimal=16) + + if __name__ == "__main__": test_pwc() test_pwc_add() -- cgit v1.2.3