diff --git a/README.md b/README.md index d891ca742832c82346793f0fb91166e6677f87bb..734d63f54b8df5bc9097971599859a5fa059d9b7 100644 --- a/README.md +++ b/README.md @@ -27,6 +27,45 @@ slice them and so on. # Usage + +## Series + +### DiscreteSeries + +To use a _DiscreteSeries_ you must give it a set of data to work with. These +will define intervals, left-closed, right-open. Ie. + + +```python +fs = DiscreteSeries([(0,1), (3, 4), (5, 6)]) +fs[0.5] == 1 +fs[3] == 4 +fs[5] == 6 +# fs[6] - NotInDomainError's +``` + +Datapoints given **must be already sorted**!. By default, the domain +will be both sides closed, from minimum to maximum given in data, but you can +specify a custom one: + +```python +fs = DiscreteSeries([(0,1), (3, 4), (5, 6)], '(0; 8>') +# fs[0] - NotInDomainError's ! +fs[6] == 6 +``` + +Although you can't specify a domain where it would be impossible to compute the value. +(ie. starting at smaller than zero). Doing so will throw a _ValueError_. + +### FunctionSeries + +Using _FunctionSeries_ is straightforward. Just give them a callable and +a domain: + +```python +fs = FunctionSeries(lambda x: x**2, '<-2;2>') +``` + ## Ranges ```python diff --git a/firanka/series/series.py b/firanka/series/series.py index b4a18e7b668c51791e07dc5cf1559c8e3a303005..8cdc7133016c0c43dc333ea5d2103ac9be8e5c72 100644 --- a/firanka/series/series.py +++ b/firanka/series/series.py @@ -149,6 +149,11 @@ class DiscreteSeries(Series): self.data = data super(DiscreteSeries, self).__init__(domain) + if len(data) > 0: + if self.domain.start < data[0][0]: + raise ValueError('some domain space is not covered by definition!') + + def apply(self, fun): return DiscreteSeries([(k, fun(v)) for k, v in self.data], self.domain) diff --git a/tests/test_series/test_series.py b/tests/test_series/test_series.py index f7ea5b1dc7b19f1f48792ee8f8a58a7ae65c794e..0bc2bb3553dda3b2976078b568b03e13dd2af2f5 100644 --- a/tests/test_series/test_series.py +++ b/tests/test_series/test_series.py @@ -7,6 +7,9 @@ from firanka.series import DiscreteSeries, FunctionSeries, Range, ModuloSeries, class TestDiscreteSeries(unittest.TestCase): + def test_uncov(self): + self.assertRaises(ValueError, lambda: DiscreteSeries([[0,0], [1,1], [2,2]], '<-5;2>')) + def test_base(self): s = DiscreteSeries([[0,0], [1,1], [2,2]]) @@ -85,8 +88,12 @@ class TestDiscreteSeries(unittest.TestCase): self.assertTrue(Range('<0;2)') in sc.domain) def test_discretize(self): - PTS = [0,1,2,3,4,5] + # note the invalid data for covering this domain + self.assertRaises(ValueError, lambda: FunctionSeries(lambda x: x**2, '<-10;10)').discretize([0,1,2,3,4,5], '(-1;6)')) + + PTS = [-1, 0,1,2,3,4,5] sa = FunctionSeries(lambda x: x**2, '<-10;10)').discretize(PTS, '(-1;6)') + self.assertIsInstance(sa, DiscreteSeries) self.assertEqual(sa.data, [(i, i**2) for i in PTS])