add generic test for logistics function fix2 #15
2 changed files with 16 additions and 1 deletions
|
@ -2,3 +2,10 @@
|
||||||
|
|
||||||
def f(x, r):
|
def f(x, r):
|
||||||
return r * x * (1-x)
|
return r * x * (1-x)
|
||||||
|
|
||||||
|
def iterate_f(it, x, r):
|
||||||
|
result = [x]
|
||||||
|
for i in range(it):
|
||||||
|
x = f(x, r)
|
||||||
|
result.append(x)
|
||||||
|
return result
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
from numpy.testing import assert_allclose
|
from numpy.testing import assert_allclose
|
||||||
import pytest
|
import pytest
|
||||||
from logistic import f
|
from logistic import f, iterate_f
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize('x,r,expected', [(0, 1.1, 0),(1, 3.7, 0)])
|
@pytest.mark.parametrize('x,r,expected', [(0, 1.1, 0),(1, 3.7, 0)])
|
||||||
|
@ -34,3 +34,11 @@ def test_f_normal_cases():
|
||||||
# x=0.1, r=2.2, it=1 => iterate_f(it, x, r)=[0.1, 0.198]
|
# x=0.1, r=2.2, it=1 => iterate_f(it, x, r)=[0.1, 0.198]
|
||||||
# x=0.2, r=3.4, it=4 => iterate_f(it, x, r)=[0.2, 0.544, 0.843418, 0.449019, 0.841163]
|
# x=0.2, r=3.4, it=4 => iterate_f(it, x, r)=[0.2, 0.544, 0.843418, 0.449019, 0.841163]
|
||||||
# x=0.5, r=2, it=3 => iterate_f(it, x, r)=[0.5, 0.5, 0.5]
|
# x=0.5, r=2, it=3 => iterate_f(it, x, r)=[0.5, 0.5, 0.5]
|
||||||
|
@pytest.mark.parametrize('x,r,it,expected', [
|
||||||
|
(0.1, 2.2, 1, [0.1, 0.198]),
|
||||||
|
(0.2, 3.4, 4, [0.2, 0.544, 0.843418, 0.449019, 0.841163]),
|
||||||
|
(0.5, 2, 3, [0.5, 0.5, 0.5, 0.5]),
|
||||||
|
])
|
||||||
|
def test_iterate_f(x, r, it, expected):
|
||||||
|
result = iterate_f(it, x, r)
|
||||||
|
assert_allclose(result, expected, rtol=1e-06)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue