def test_comparison(self): interval = (-3, 10) t = symengine.Symbol("t") spline = CubicHermiteSpline(n=2) spline.from_function( [symengine.sin(t), symengine.cos(t)], times_of_interest=interval, max_anchors=100, ) times = np.linspace(*interval, 100) evaluation = spline.get_state(times) control = np.vstack((np.sin(times), np.cos(times))).T assert_allclose(evaluation, control, atol=0.01)
class TestAdditions(unittest.TestCase): def setUp(self): interval = (-3, 2) self.times = np.linspace(*interval, 10) t = symengine.Symbol("t") self.sin_spline = CubicHermiteSpline(n=1) self.sin_spline.from_function( [symengine.sin(t)], times_of_interest=interval, max_anchors=100, ) self.sin_evaluation = self.sin_spline.get_state(self.times) self.exp_spline = CubicHermiteSpline(n=1) self.exp_spline.from_function( [symengine.exp(t)], times_of_interest=interval, max_anchors=100, ) self.exp_evaluation = self.exp_spline.get_state(self.times) def has_matched_times(self, spline): self.assertSetEqual( set(self.sin_spline.times) | set(self.exp_spline.times), set(spline.times), ) def test_plus(self): combined = self.sin_spline.copy() combined.plus(self.exp_spline) evaluation = combined.get_state(self.times) control = np.atleast_2d(np.sin(self.times) + np.exp(self.times)).T control_2 = self.sin_evaluation + self.exp_evaluation self.has_matched_times(combined) assert_allclose(control, control_2, atol=0.01) assert_allclose(evaluation, control, atol=0.01) def test_join(self): joined = join(self.sin_spline, self.exp_spline) evaluation = joined.get_state(self.times) evaluation = joined.get_state(self.times) control = np.vstack((np.sin(self.times), np.exp(self.times))).T control_2 = np.hstack((self.sin_evaluation, self.exp_evaluation)) self.has_matched_times(joined) assert_allclose(control, control_2, atol=0.01) assert_allclose(evaluation, control, atol=0.01)
def test_random_function(self): roots = np.sort(np.random.normal(size=5)) value = np.random.normal() t = symengine.Symbol("t") function = np.prod([t - root for root in roots]) + value i = 1 spline = CubicHermiteSpline(n=3) spline.from_function( [10, function, 10], times_of_interest=(min(roots) - 0.01, max(roots) + 0.01), max_anchors=1000, tol=7, ) solutions = spline.solve(i=i, value=value) sol_times = [sol[0] for sol in solutions] assert_allclose(spline.get_state(sol_times)[:, i], value) assert_allclose([sol[0] for sol in solutions], roots, atol=1e-3) for time, diff in solutions: true_diff = float(function.diff(t).subs({t: time})) self.assertAlmostEqual(true_diff, diff, places=5)