Repository: scipy/scipy
File: scipy/interpolate/tests/test_bsplines.py (Python)

Code before:
        k = 2
        t = [-1, -1, 0, 1, 1, 3, 4, 6, 6, 6, 7, 7]
        rng = np.random.RandomState(1234)
        c = np.r_[0, 0, rng.random(5), 0, 0]
        b = BSpline(t, c, k)

        # b is continuous at x != 6 (triple knot)
        x = np.asarray([1, 3, 4, 6])
        xp_assert_close(b(x[x != 6] - 1e-10),
                        b(x[x != 6] + 1e-10))
        assert not np.allclose(b(6.-1e-10), b(6+1e-10))

        # 1st derivative jumps at double knots, 1 & 6:
        x0 = np.asarray([3, 4])
        xp_assert_close(b(x0 - 1e-10, nu=1),
                        b(x0 + 1e-10, nu=1))
        x1 = np.asarray([1, 6])
        assert not np.allclose(b(x1 - 1e-10, nu=1), b(x1 + 1e-10, nu=1))

        # 2nd derivative is not guaranteed to be continuous either
        assert not np.allclose(b(x - 1e-10, nu=2), b(x + 1e-10, nu=2))

    def test_basis_element_invalid_too_short(self):
        # There should be atleast 2 knots
        assert_raises(ValueError, BSpline.basis_element, **dict(t=[0]))
        assert_raises(ValueError, BSpline.basis_element, **dict(t=[]))

    def test_basis_element_quadratic(self, xp):
        xx = xp.linspace(-1, 4, 20)
        b = BSpline.basis_element(t=xp.asarray([0, 1, 2, 3]))

        xx_np, t_np, c_np = map(np.asarray, (xx, b.t, b.c))
        splev_result = xp.asarray(splev(xx_np, (t_np, c_np, b.k)))
        xp_assert_close(b(xx), splev_result, atol=1e-14)

        atol=1e-14 if xp_default_dtype(xp) == xp.float64 else 1e-7
        xp_assert_close(b(xx), xp.asarray(B_0123(xx), dtype=xp.float64), atol=atol)

        b = BSpline.basis_element(t=xp.asarray([0, 1, 1, 2]))
        xx = xp.linspace(0, 2, 10, dtype=xp.float64)
        xp_assert_close(b(xx),
                xp.where(xx < 1, xx*xx, (2.-xx)**2), atol=1e-14)

    def test_basis_element_rndm(self):
        b = _make_random_spline()
        t, c, k = b.tck
        xx = np.linspace(t[k], t[-k-1], 20)
        xp_assert_close(b(xx), _sum_basis_elements(xx, t, c, k), atol=1e-14)

    def test_cmplx(self):
        b = _make_random_spline()

Review comment (suggestion):
```suggestion
    def test_basis_element_invalid_too_short(self, xp):
        # There should be atleast 2 knots
        assert_raises(ValueError, BSpline.basis_element, **dict(t=xp.asarray([0])))
        assert_raises(ValueError, BSpline.basis_element, **dict(t=xp.asarray[])))
```

Code after:
        k = 2
        t = [-1, -1, 0, 1, 1, 3, 4, 6, 6, 6, 7, 7]
        rng = np.random.RandomState(1234)
        c = np.r_[0, 0, rng.random(5), 0, 0]
        b = BSpline(t, c, k)

        # b is continuous at x != 6 (triple knot)
        x = np.asarray([1, 3, 4, 6])
        xp_assert_close(b(x[x != 6] - 1e-10),
                        b(x[x != 6] + 1e-10))
        assert not np.allclose(b(6.-1e-10), b(6+1e-10))

        # 1st derivative jumps at double knots, 1 & 6:
        x0 = np.asarray([3, 4])
        xp_assert_close(b(x0 - 1e-10, nu=1),
                        b(x0 + 1e-10, nu=1))
        x1 = np.asarray([1, 6])
        assert not np.allclose(b(x1 - 1e-10, nu=1), b(x1 + 1e-10, nu=1))

        # 2nd derivative is not guaranteed to be continuous either
        assert not np.allclose(b(x - 1e-10, nu=2), b(x + 1e-10, nu=2))

    def test_basis_element_invalid_too_short(self, xp):
        # There should be at least 2 knots
        assert_raises(ValueError, BSpline.basis_element, **dict(t=xp.asarray([0])))
        assert_raises(ValueError, BSpline.basis_element, **dict(t=xp.asarray([])))

    def test_basis_element_quadratic(self, xp):
        xx = xp.linspace(-1, 4, 20)
        b = BSpline.basis_element(t=xp.asarray([0, 1, 2, 3]))

        xx_np, t_np, c_np = map(np.asarray, (xx, b.t, b.c))
        splev_result = xp.asarray(splev(xx_np, (t_np, c_np, b.k)))
        xp_assert_close(b(xx), splev_result, atol=1e-14)

        atol=1e-14 if xp_default_dtype(xp) == xp.float64 else 1e-7
        xp_assert_close(b(xx), xp.asarray(B_0123(xx), dtype=xp.float64), atol=atol)

        b = BSpline.basis_element(t=xp.asarray([0, 1, 1, 2]))
        xx = xp.linspace(0, 2, 10, dtype=xp.float64)
        xp_assert_close(b(xx),
                xp.where(xx < 1, xx*xx, (2.-xx)**2), atol=1e-14)

    def test_basis_element_rndm(self):
        b = _make_random_spline()
        t, c, k = b.tck
        xx = np.linspace(t[k], t[-k-1], 20)
        xp_assert_close(b(xx), _sum_basis_elements(xx, t, c, k), atol=1e-14)

    def test_cmplx(self):
        b = _make_random_spline()