Exemplo n.º 1
0
def make_interp_knots(
    x: np.ndarray, k: int = 3, bc_type: BCType | None = None, check_finite: bool = True
) -> np.ndarray:
    """Compute the knots of the B-spline.

    .. note::
       This is a temporary implementation that should be moved to the main
       scipy library - see `<https://github.com/scipy/scipy/issues/8810>`_.

    Parameters
    ----------
    x : array_like, shape (n,)
        Abscissas.
    k : int, optional
        B-spline degree. Default is cubic, k=3.
    bc_type : 2-tuple or None
        Boundary conditions.
        Default is None, which means choosing the boundary conditions
        automatically. Otherwise, it must be a length-two tuple where the first
        element sets the boundary conditions at ``x[0]`` and the second
        element sets the boundary conditions at ``x[-1]``. Each of these must
        be an iterable of pairs ``(order, value)`` which gives the values of
        derivatives of specified orders at the given edge of the interpolation
        interval.
    check_finite : bool, optional
        Whether to check that the input arrays contain only finite numbers.
        Disabling may give a performance gain, but may result in problems
        (crashes, non-termination) if the inputs do contain infinities or NaNs.
        Default is True.

    Returns
    -------
    numpy array with size = x.size + k + 1, representing the B-spline knots.
    """
    if k < 2 and bc_type is not None:
        raise ValueError("Too much info for k<2: bc_type can only be None.")

    x = np.array(x)
    if x.ndim != 1 or np.any(x[1:] <= x[:-1]):
        raise ValueError("Expect x to be a 1-D sorted array-like.")

    if k == 0:
        t = np.r_[x, x[-1]]
    elif k == 1:
        t = np.r_[x[0], x, x[-1]]
    elif bc_type is None:
        if k == 2:
            # OK, it's a bit ad hoc: Greville sites + omit
            # 2nd and 2nd-to-last points, a la not-a-knot
            t = (x[1:] + x[:-1]) / 2.0
            t = np.r_[(x[0],) * (k + 1), t[1:-1], (x[-1],) * (k + 1)]
        else:
            t = _not_a_knot(x, k)
    else:
        t = _augknt(x, k)

    return _as_float_array(t, check_finite)
Exemplo n.º 2
0
    def test_full_matrix(self):
        np.random.seed(1234)
        k, n = 3, 7
        x = np.sort(np.random.random(size=n))
        y = np.random.random(size=n)
        t = _not_a_knot(x, k)

        b = make_interp_spline(x, y, k, t)
        cf = make_interp_full_matr(x, y, t, k)
        assert_allclose(b.c, cf, atol=1e-14, rtol=1e-14)
Exemplo n.º 3
0
    def test_full_matrix(self):
        np.random.seed(1234)
        k, n = 3, 7
        x = np.sort(np.random.random(size=n))
        y = np.random.random(size=n)
        t = _not_a_knot(x, k)

        b = make_interp_spline(x, y, k, t)
        cf = make_interp_full_matr(x, y, t, k)
        assert_allclose(b.c, cf, atol=1e-14, rtol=1e-14)