From 95ca96d84c724b910d13f56009b6ced3fdc6beea Mon Sep 17 00:00:00 2001 From: ASPP Student Date: Tue, 29 Aug 2023 12:04:12 +0300 Subject: [PATCH 1/3] implement logistic map function f, add generic test, add nan test --- logistic.py | 5 +++++ test_logistic.py | 24 ++++++++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/logistic.py b/logistic.py index e49d1c2..fbc304f 100644 --- a/logistic.py +++ b/logistic.py @@ -1 +1,6 @@ # Your code goes here +def f(x,r): + """ + compute logistic map + """ + return r*x*(1-x) \ No newline at end of file diff --git a/test_logistic.py b/test_logistic.py index 9391bee..c40dc61 100644 --- a/test_logistic.py +++ b/test_logistic.py @@ -1,4 +1,5 @@ from numpy.testing import assert_allclose +import numpy as np from logistic import f @@ -14,3 +15,26 @@ def test_f_corner_cases(): for x, r, expected in cases: result = f(x, r) assert_allclose(result, expected) + + +def test_f_generic_cases(): + # Test cases are (x, r, expected) + cases = [ + (0.1, 2.2, 0.198), + (0.2, 3.4, 0.544), + (0.5, 2, 0.5) + ] + for x, r, expected in cases: + result = f(x, r) + assert_allclose(result, expected) + + +def test_f_nan(): + # Test cases are (x, r, expected) + cases = [ + (0.1, np.nan, np.nan), + (np.nan,2, np.nan) + ] + for x, r, expected in cases: + result = f(x, r) + assert_allclose(result, expected) \ No newline at end of file From 347615866be48e0238d315c0554958e369748d89 Mon Sep 17 00:00:00 2001 From: ASPP Student Date: Tue, 29 Aug 2023 12:30:36 +0300 Subject: [PATCH 2/3] Test with parametrize --- test_logistic.py | 40 +++++++++++++++------------------------- 1 file changed, 15 insertions(+), 25 deletions(-) diff --git a/test_logistic.py b/test_logistic.py index c40dc61..5a3375a 100644 --- a/test_logistic.py +++ b/test_logistic.py @@ -1,40 +1,30 @@ from numpy.testing import assert_allclose import numpy as np - +import pytest from logistic import f # Add here your test for the logistic map - -def test_f_corner_cases(): +@pytest.mark.parametrize('x, r, expected', [(0, 1.1, 0), (1, 3.7, 0)]) +def test_f_corner_cases(x, r, expected): # Test cases are (x, r, expected) - cases = [ - (0, 1.1, 0), - (1, 3.7, 0), - ] - for x, r, expected in cases: - result = f(x, r) - assert_allclose(result, expected) + result = f(x, r) + assert_allclose(result, expected) -def test_f_generic_cases(): - # Test cases are (x, r, expected) - cases = [ +@pytest.mark.parametrize('x, r, expected', [ (0.1, 2.2, 0.198), (0.2, 3.4, 0.544), (0.5, 2, 0.5) - ] - for x, r, expected in cases: - result = f(x, r) - assert_allclose(result, expected) + ]) +def test_f_generic_cases(x,r,expected): + # Test cases are (x, r, expected) + result = f(x, r) + assert_allclose(result, expected) -def test_f_nan(): +@pytest.mark.parametrize('x, r, expected', [(0.1, np.nan, np.nan), (np.nan,2, np.nan)]) +def test_f_nan(x, r, expected): # Test cases are (x, r, expected) - cases = [ - (0.1, np.nan, np.nan), - (np.nan,2, np.nan) - ] - for x, r, expected in cases: - result = f(x, r) - assert_allclose(result, expected) \ No newline at end of file + result = f(x, r) + assert_allclose(result, expected) From 5903f8669933aaa59516ca7dee5ca593d35bbea8 Mon Sep 17 00:00:00 2001 From: ASPP Student Date: Tue, 29 Aug 2023 12:53:38 +0300 Subject: [PATCH 3/3] Add iteration function and tests --- logistic.py | 13 ++++++++++++- test_logistic.py | 11 ++++++++++- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/logistic.py b/logistic.py index fbc304f..24e4d68 100644 --- a/logistic.py +++ b/logistic.py @@ -3,4 +3,15 @@ def f(x,r): """ compute logistic map """ - return r*x*(1-x) \ No newline at end of file + return r*x*(1-x) + + +def iterate_f(it, x, r): + """ + run f for it iterations + """ + res=[x] + for i in range(it): + x=f(x,r) + res.append(x) + return res \ No newline at end of file diff --git a/test_logistic.py b/test_logistic.py index 5a3375a..5ed12b0 100644 --- a/test_logistic.py +++ b/test_logistic.py @@ -1,7 +1,7 @@ from numpy.testing import assert_allclose import numpy as np import pytest -from logistic import f +from logistic import f, iterate_f # Add here your test for the logistic map @@ -28,3 +28,12 @@ def test_f_nan(x, r, expected): # Test cases are (x, r, expected) result = f(x, r) assert_allclose(result, expected) + + + +@pytest.mark.parametrize('it, x, r, expected', [(1,0.1,2.2,[0.1,0.198]), + (4,0.2,3.4,[0.2,0.544,0.843418,0.449019, 0.841163]), + (3,0.5,2,[0.5,0.5,0.5,0.5])]) +def test_iterate_f(it, x, r, expected): + result = iterate_f(it, x, r) + assert_allclose(result, expected, atol=0.00001) #adjusted tolerance