def test_kink(self): # Should refuse to differentiate splines with kinks spl2 = insert(0.5, self.spl, m=2) splder(spl2, 2) # Should work assert_raises(ValueError, splder, spl2, 3) spl2 = insert(0.5, self.spl, m=3) splder(spl2, 1) # Should work assert_raises(ValueError, splder, spl2, 2) spl2 = insert(0.5, self.spl, m=4) assert_raises(ValueError, splder, spl2, 1)
def test_inverse(self): # Check that antiderivative + derivative is identity. for n in range(5): spl2 = splantider(self.spl, n) spl3 = splder(spl2, n) assert_allclose(self.spl[0], spl3[0]) assert_allclose(self.spl[1], spl3[1]) assert_equal(self.spl[2], spl3[2])
def test_multidim(self): # c can have trailing dims for n in range(3): t, c, k = self.spl c2 = np.c_[c, c, c] c2 = np.dstack((c2, c2)) spl2 = splantider((t, c2, k), n) spl3 = splder(spl2, n) assert_allclose(t, spl3[0]) assert_allclose(c2, spl3[1]) assert_equal(k, spl3[2])
def test_splder_vs_splev(self): # Check derivative vs. FITPACK for n in range(3+1): # Also extrapolation! xx = np.linspace(-1, 2, 2000) if n == 3: # ... except that FITPACK extrapolates strangely for # order 0, so let's not check that. xx = xx[(xx >= 0) & (xx <= 1)] dy = splev(xx, self.spl, n) spl2 = splder(self.spl, n) dy2 = splev(xx, spl2) if n == 1: assert_allclose(dy, dy2, rtol=2e-6) else: assert_allclose(dy, dy2)
def test_splev_der_k(): # regression test for gh-2188: splev(x, tck, der=k) gives garbage or crashes # for x outside of knot range # test case from gh-2188 tck = (np.array([0., 0., 2.5, 2.5]), np.array([-1.56679978, 2.43995873, 0., 0.]), 1) t, c, k = tck x = np.array([-3, 0, 2.5, 3]) # an explicit form of the linear spline assert_allclose(splev(x, tck), c[0] + (c[1] - c[0]) * x / t[2]) assert_allclose(splev(x, tck, 1), (c[1] - c[0]) / t[2]) # now check a random spline vs splder np.random.seed(1234) x = np.sort(np.random.random(30)) y = np.random.random(30) t, c, k = splrep(x, y) x = [t[0] - 1., t[-1] + 1.] tck2 = splder((t, c, k), k) assert_allclose(splev(x, (t, c, k), k), splev(x, tck2))