Exemplo n.º 1
0
 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)
Exemplo n.º 2
0
 def as_cubic_splines(self, duration, dt):
     """
     Return as cubic Hermite splines after checking stimulus bounds.
     """
     self._get_times(duration, dt)
     stim_input = self._trim_stim_input(self.generate_input(duration, dt))
     return CubicHermiteSpline.from_data(self.times, stim_input)
Exemplo n.º 3
0
 def as_cubic_splines(self):
     """
     Return as cubic Hermite splines after checking stimulus bounds.
     """
     stim_input = self.generate_input()
     stim_input[self.times < self.stim_start] = 0.0
     stim_input[self.times > self.stim_end] = 0.0
     return CubicHermiteSpline.from_data(self.times, stim_input)
Exemplo n.º 4
0
    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)
Exemplo n.º 5
0
    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)
Exemplo n.º 6
0
    def as_cubic_splines(self, duration, dt):
        """
        Return as cubic Hermite splines.

        :param duration: duration of the input, in miliseconds
        :type duration: float
        :param dt: some reasonable "speed" of input, in miliseconds
        :type dt: float
        """
        self._get_times(duration, dt)
        return CubicHermiteSpline.from_data(self.times, self.generate_input(duration, dt))
Exemplo n.º 7
0
 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])
Exemplo n.º 8
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])
Exemplo n.º 9
0
    def test_multiple_anchors(self):
        for _ in range(10):
            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, 3))])

            beginning, end = sorted(
                np.random.uniform(spline[0].time, spline[-1].time, 2))
            times = np.linspace(beginning, end, 10000)
            values = np.vstack([spline.get_state(time) for time in times])

            result = spline.extrema(beginning, end)
            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)
Exemplo n.º 10
0
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)
Exemplo n.º 11
0
    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)
Exemplo n.º 12
0
 def test_wrong_shape(self):
     with self.assertRaises(ValueError):
         CubicHermiteSpline(1, [(1, [2, 3], [4, 5])])
Exemplo n.º 13
0
                atol=atol,
                rtol=rtol)
t = sol.t
y = sol.y[0, :]
yp = sol.yp[0, :]

# #jitcdde
from symengine import exp
ts = [1 - c, 1, 2 - c, tf]
fs = [[-r * y_jit(0) * 0.4 * (1 - t_jit)],
      [-r * y_jit(0) * (0.4 * (1 - t_jit) + 10.0 - exp(mu) * y_jit(0))],
      [-r * y_jit(0) * (10. - exp(mu) * y_jit(0))],
      [-r * exp(mu) * y_jit(0) * (y_jit(0, t_jit - tau) - y_jit(0))]]

from chspy import CubicHermiteSpline
histo = CubicHermiteSpline(n=1)
histo.constant(y0)

print(ts)
y_jit = []
dt_jit = []
t_jit = []
for target_time, f in zip(ts, fs):
    DDE = jitcdde(f, max_delay=tau)
    DDE.set_integration_parameters(atol=atol, rtol=rtol)
    DDE.add_past_points(histo)
    DDE.adjust_diff()
    for ti in np.linspace(DDE.t, target_time, 100):
        t_jit.append(ti)
        y_jit.append(DDE.integrate(ti)[0])
        dt_jit.append(DDE.dt)
Exemplo n.º 14
0
 def as_cubic_splines(self):
     """
     Return as cubic Hermite splines.
     """
     return CubicHermiteSpline.from_data(self.times, self.generate_input())
Exemplo n.º 15
0
 def test_replace_with_same(self):
     spline = CubicHermiteSpline(1, [(0, [0], [0])])
     spline[-1] = (0, [1], [2])
Exemplo n.º 16
0
 def test_wrong_time(self):
     with self.assertRaises(ValueError):
         CubicHermiteSpline(1, [(0, [0], [0]), (0, [1], [2])])
Exemplo n.º 17
0
 def test_wrong_diff_shape(self):
     with self.assertRaises(ValueError):
         CubicHermiteSpline(1, [(1, [2], [3, 4])])
Exemplo n.º 18
0
from chspy import CubicHermiteSpline
from matplotlib.pyplot import subplots

spline = CubicHermiteSpline(n=3)

#            time   state    slope
spline.add((0, [1, 3, 0], [0, 1, 0]))
spline.add((1, [3, 2, 0], [0, 4, 0]))
spline.add((4, [0, 1, 3], [0, 4, 0]))

fig, axes = subplots(figsize=(7, 2))
spline.plot(axes)
axes.set_xlabel("time")
axes.set_ylabel("state")
fig.legend(loc="right", labelspacing=1)
fig.subplots_adjust(right=0.7)
Exemplo n.º 19
0
    lambda x, j=j: sum(np.arange(1, 4) * coeff[j, 1:] * (x**np.arange(3)))
    for j in range(m)
]

assert poly[0](1.0) != poly[1](1.0)
assert diff[0](1.0) != diff[1](1.0)

spline = CubicHermiteSpline(m, [
    (
        0.0,
        np.array([poly[j](0.0) for j in range(m)]),
        np.array([diff[j](0.0) for j in range(m)]),
    ),
    (
        0.5,
        np.array([poly[j](0.5) for j in range(m)]),
        np.array([diff[j](0.5) for j in range(m)]),
    ),
    (
        2.0,
        np.random.random(m),
        np.random.random(m),
    ),
])


class interpolation_test(unittest.TestCase):
    def test_anchors(self):
        for s in range(len(spline) - 1):
            t = spline[s][0]
            anchors = (spline[s], spline[s + 1])