diff options
author | Mario Mulansky <mario.mulansky@gmx.net> | 2014-09-27 18:37:42 +0200 |
---|---|---|
committer | Mario Mulansky <mario.mulansky@gmx.net> | 2014-09-27 18:37:42 +0200 |
commit | 604c7cfe89ebaa37bc51fbfabb656b8aa7829554 (patch) | |
tree | 78a0673cec946efc78f0671526a3533561f8e7b6 /test | |
parent | 03440863ae92c14abae25f54bad9349119b0ec64 (diff) |
+ addition for piece wise linear function with tests
Diffstat (limited to 'test')
-rw-r--r-- | test/test_function.py | 49 |
1 files changed, 47 insertions, 2 deletions
diff --git a/test/test_function.py b/test/test_function.py index 386b999..014ecac 100644 --- a/test/test_function.py +++ b/test/test_function.py @@ -22,8 +22,8 @@ def test_pwc(): xp_expected = [0.0, 1.0, 1.0, 2.0, 2.0, 2.5, 2.5, 4.0] yp_expected = [1.0, 1.0, -0.5, -0.5, 1.5, 1.5, 0.75, 0.75] - assert_array_almost_equal(xp, xp_expected) - assert_array_almost_equal(yp, yp_expected) + assert_array_almost_equal(xp, xp_expected, decimal=16) + assert_array_almost_equal(yp, yp_expected, decimal=16) assert_almost_equal(f.avrg(), (1.0-0.5+0.5*1.5+1.5*0.75)/4.0, decimal=16) assert_almost_equal(f.abs_avrg(), (1.0+0.5+0.5*1.5+1.5*0.75)/4.0, @@ -49,5 +49,50 @@ def test_pwc(): assert_array_almost_equal(f1.y, 2*f2.y, decimal=16) +def test_pwl(): + x = [0.0, 1.0, 2.0, 2.5, 4.0] + y1 = [1.0, -0.5, 1.5, 0.75] + y2 = [1.5, -0.4, 1.5, 0.25] + f = spk.PieceWiseLinFunc(x, y1, y2) + xp, yp = f.get_plottable_data() + + xp_expected = [0.0, 1.0, 1.0, 2.0, 2.0, 2.5, 2.5, 4.0] + yp_expected = [1.0, 1.5, -0.5, -0.4, 1.5, 1.5, 0.75, 0.25] + assert_array_almost_equal(xp, xp_expected, decimal=16) + assert_array_almost_equal(yp, yp_expected, decimal=16) + + avrg_expected = (1.25 - 0.45 + 0.75 + 1.5*0.5) / 4.0 + assert_almost_equal(f.avrg(), avrg_expected, decimal=16) + + abs_avrg_expected = (1.25 + 0.45 + 0.75 + 1.5*0.5) / 4.0 + assert_almost_equal(f.abs_avrg(), abs_avrg_expected, decimal=16) + + f1 = copy(f) + x = [0.0, 0.75, 2.0, 2.5, 2.7, 4.0] + y1 = [0.5, 1.0, -0.25, 0.0, 1.5] + y2 = [0.8, 0.2, -1.0, 0.0, 2.0] + f2 = spk.PieceWiseLinFunc(x, y1, y2) + f1.add(f2) + x_expected = [0.0, 0.75, 1.0, 2.0, 2.5, 2.7, 4.0] + y1_expected = [1.5, 1.0+1.0+0.5*0.75, -0.5+1.0-0.8*0.25/1.25, 1.5-0.25, + 0.75, 1.5+0.75-0.5*0.2/1.5] + y2_expected = [0.8+1.0+0.5*0.75, 1.5+1.0-0.8*0.25/1.25, -0.4+0.2, 1.5-1.0, + 0.75-0.5*0.2/1.5, 2.25] + assert_array_almost_equal(f1.x, x_expected, decimal=16) + assert_array_almost_equal(f1.y1, y1_expected, decimal=16) + assert_array_almost_equal(f1.y2, y2_expected, decimal=16) + + f2.add(f) + assert_array_almost_equal(f2.x, x_expected, decimal=16) + assert_array_almost_equal(f2.y1, y1_expected, decimal=16) + assert_array_almost_equal(f2.y2, y2_expected, decimal=16) + + f1.add(f2) + # same x, but y doubled + assert_array_almost_equal(f1.x, f2.x, decimal=16) + assert_array_almost_equal(f1.y1, 2*f2.y1, decimal=16) + assert_array_almost_equal(f1.y2, 2*f2.y2, decimal=16) + + if __name__ == "__main__": test_pwc() |