def test_simple_polynomial(self): T = symengine.Symbol("T") poly = 2 * T**3 - 3 * T**2 - 36 * T + 17 arg_extremes = [-2, 3] arrify = lambda expr, t: np.atleast_1d(float(expr.subs({T: t}))) spline = CubicHermiteSpline( 1, [(t, arrify(poly, t), arrify(poly.diff(T), t)) for t in arg_extremes]) result = extrema_from_anchors(spline) assert_allclose(result.minima, arrify(poly, arg_extremes[1])) assert_allclose(result.maxima, arrify(poly, arg_extremes[0])) assert_allclose(result.arg_min, arg_extremes[1]) assert_allclose(result.arg_max, arg_extremes[0])
def test_given_extrema(self): n = 100 positions = sorted(np.random.random(2)) state = np.random.random(n) spline = CubicHermiteSpline(n, [ (positions[0], state, np.zeros(n)), (positions[1], state + np.random.uniform(0, 5), np.zeros(n)), ]) result = extrema_from_anchors(spline) assert_allclose(result.arg_min, spline[0].time) assert_allclose(result.arg_max, spline[1].time) assert_allclose(result.minima, spline[0][1]) assert_allclose(result.maxima, spline[1][1])
def test_arbitrary_anchors(self): n = 100 spline = CubicHermiteSpline( n, [(time, np.random.normal(0, 1, n), np.random.normal(0, 0.1, n)) for time in sorted(np.random.uniform(-10, 10, 2))]) times = np.linspace(spline[0].time, spline[1].time, 10000) values = np.vstack([spline.get_recent_state(time) for time in times]) result = extrema_from_anchors(spline[-2:]) assert_allclose(result.minima, np.min(values, axis=0), atol=1e-3) assert_allclose(result.maxima, np.max(values, axis=0), atol=1e-3) assert_allclose(result.arg_min, times[np.argmin(values, axis=0)], atol=1e-3) assert_allclose(result.arg_max, times[np.argmax(values, axis=0)], atol=1e-3)
def extrema_in_last_step(self): extrema = extrema_from_anchors(self[-2:]) return extrema.minima, extrema.maxima