Skip to content
Snippets Groups Projects
Commit b8b75d1e authored by Piotr Maślanka's avatar Piotr Maślanka
Browse files

added `clip` to `linear_interpolate`

parent 8ad0da8f
No related branches found
Tags v2.14.15
No related merge requests found
# v2.14.15
* added `Vector`
* added `clip` to `linear_interpolate`
__version__ = '2.14.15_a2'
__version__ = '2.14.15'
......@@ -3,7 +3,8 @@ from satella.coding.typing import U, K
import bisect
def linear_interpolate(series: tp.Sequence[tp.Tuple[K, U]], t: K) -> U:
def linear_interpolate(series: tp.Sequence[tp.Tuple[K, U]], t: K,
clip: bool = False) -> U:
"""
Given a sorted (ascending) series of (t_value, y_value) interpolating linearly a function of
y=f(t) compute a linear approximation of f at t
......@@ -13,13 +14,21 @@ def linear_interpolate(series: tp.Sequence[tp.Tuple[K, U]], t: K) -> U:
:param series: series of (t, y) sorted by t ascending
:param t: t to compute the value for
:param clip: if set to True, then values t: t<t_min f(t_min) will be returned
and for values t: t>t_max f(t_max) will be returned
:return: return value
:raise ValueError: t was smaller than t_min or greater than t_max
"""
if t < series[0][0]:
raise ValueError('t smaller than t_min')
if clip:
return series[0][1]
else:
raise ValueError('t smaller than t_min')
if t > series[-1][0]:
raise ValueError('t greater than t_max')
if clip:
return series[-1][1]
else:
raise ValueError('t greater than t_max')
if t == series[0][0]:
return series[0][1]
......
......@@ -17,6 +17,8 @@ class TestTransforms(unittest.TestCase):
self.assertEqual( linear_interpolate([(0, 10), (10, 20), (20, 10)], 15), 15)
self.assertRaises(ValueError, lambda: linear_interpolate([(0, 10), (10, 20)], 30))
self.assertRaises(ValueError, lambda: linear_interpolate([(0, 10), (10, 20)], -2))
self.assertEqual(linear_interpolate([(0, 10), (10, 20)], 30, True), 20)
self.assertEqual(linear_interpolate([(0, 10), (10, 20)], -2, True), 10)
def test_base64(self):
a = b64encode(b'test')
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment