Skip to content

Commit

Permalink
Merge pull request #182 from jorenham/fix-177
Browse files Browse the repository at this point in the history
complete `scipy.interpolate._cubic`
  • Loading branch information
jorenham authored Nov 18, 2024
2 parents acce8d8 + ea28b0c commit ebb1788
Show file tree
Hide file tree
Showing 2 changed files with 191 additions and 62 deletions.
103 changes: 83 additions & 20 deletions scipy-stubs/interpolate/_cubic.pyi
Original file line number Diff line number Diff line change
@@ -1,39 +1,102 @@
from typing import Literal
from typing_extensions import override
from collections.abc import Iterable
from typing import Generic, Literal, NoReturn, TypeAlias, overload
from typing_extensions import TypeVar, override

from scipy._typing import Untyped
import numpy as np
import numpy.typing as npt
from numpy._typing import _ArrayLikeFloat_co, _ArrayLikeNumber_co
from scipy._typing import AnyInt
from ._interpolate import PPoly

_T = TypeVar("_T")
_CT = TypeVar("_CT", bound=np.float64 | np.complex128)
_CT_co = TypeVar("_CT_co", bound=np.float64 | np.complex128, default=np.float64 | np.complex128, covariant=True)

_Tuple2: TypeAlias = tuple[_T, _T]

_Extrapolate: TypeAlias = Literal["periodic"] | bool
_CubicBCName: TypeAlias = Literal["not-a-knot", "clamped", "natural"]
_CubicBCOrder: TypeAlias = Literal[1, 2]
_CubicBCType: TypeAlias = Literal[_CubicBCName, "periodic"] | _Tuple2[_CubicBCName | tuple[_CubicBCOrder, _ArrayLikeNumber_co]]

###

__all__ = ["Akima1DInterpolator", "CubicHermiteSpline", "CubicSpline", "PchipInterpolator", "pchip_interpolate"]

class CubicHermiteSpline(PPoly):
def __init__(self, x: Untyped, y: Untyped, dydx: Untyped, axis: int = 0, extrapolate: Untyped | None = None) -> None: ...
class CubicHermiteSpline(PPoly[_CT_co]):
@overload
def __init__(
self: CubicHermiteSpline[np.float64],
x: _ArrayLikeFloat_co,
y: _ArrayLikeFloat_co,
dydx: _ArrayLikeFloat_co,
axis: int = 0,
extrapolate: _Extrapolate | None = None,
) -> None: ...
@overload
def __init__(
self: CubicHermiteSpline[np.float64 | np.complex128],
x: _ArrayLikeFloat_co,
y: _ArrayLikeNumber_co,
dydx: _ArrayLikeNumber_co,
axis: int = 0,
extrapolate: _Extrapolate | None = None,
) -> None: ...

class PchipInterpolator(CubicHermiteSpline):
def __init__(self, x: Untyped, y: Untyped, axis: int = 0, extrapolate: Untyped | None = None) -> None: ...
class PchipInterpolator(CubicHermiteSpline[np.float64]):
def __init__(self, x: _ArrayLikeFloat_co, y: _ArrayLikeFloat_co, axis: int = 0, extrapolate: bool | None = None) -> None: ...

class Akima1DInterpolator(CubicHermiteSpline):
class Akima1DInterpolator(CubicHermiteSpline[np.float64]):
def __init__(
self,
x: Untyped,
y: Untyped,
x: _ArrayLikeFloat_co,
y: _ArrayLikeFloat_co,
axis: int = 0,
*,
method: Literal["akima", "makima"] = "akima",
extrapolate: bool | None = None,
) -> None: ...
@override
def extend(self, c: Untyped, x: Untyped, right: bool = True) -> None: ...
def extend(self, c: object, x: object, right: object = True) -> NoReturn: ... # not implemented
@classmethod
@override
def from_spline(cls, tck: object, extrapolate: object = ...) -> NoReturn: ... # not implemented
@classmethod
@override
def from_bernstein_basis(cls, bp: object, extrapolate: object = ...) -> NoReturn: ... # not implemented

class CubicSpline(CubicHermiteSpline):
class CubicSpline(CubicHermiteSpline[_CT_co], Generic[_CT_co]):
@overload
def __init__(
self,
x: Untyped,
y: Untyped,
axis: int = 0,
bc_type: str = "not-a-knot",
extrapolate: Untyped | None = None,
self: CubicSpline[np.float64],
x: _ArrayLikeFloat_co,
y: _ArrayLikeFloat_co,
axis: AnyInt = 0,
bc_type: _CubicBCType = "not-a-knot",
extrapolate: _Extrapolate | None = None,
) -> None: ...
@overload
def __init__(
self: CubicSpline[np.float64 | np.complex128],
x: _ArrayLikeFloat_co,
y: _ArrayLikeNumber_co,
axis: AnyInt = 0,
bc_type: _CubicBCType = "not-a-knot",
extrapolate: _Extrapolate | None = None,
) -> None: ...

def prepare_input(x: Untyped, y: Untyped, axis: Untyped, dydx: Untyped | None = None) -> Untyped: ... # undocumented
def pchip_interpolate(xi: Untyped, yi: Untyped, x: Untyped, der: int = 0, axis: int = 0) -> Untyped: ...
def pchip_interpolate(
xi: _ArrayLikeFloat_co,
yi: _ArrayLikeFloat_co,
x: _ArrayLikeFloat_co,
der: int | Iterable[int] = 0,
axis: int = 0,
) -> np.float64 | npt.NDArray[np.float64]: ...

# undocumented
def prepare_input(
x: _ArrayLikeFloat_co,
y: _ArrayLikeNumber_co,
axis: int,
dydx: _ArrayLikeNumber_co | None = None,
) -> tuple[npt.NDArray[np.float64], npt.NDArray[np.float64], npt.NDArray[_CT], int, npt.NDArray[_CT]]: ...
150 changes: 108 additions & 42 deletions scipy-stubs/interpolate/_interpolate.pyi
Original file line number Diff line number Diff line change
@@ -1,10 +1,25 @@
from scipy._typing import Untyped
from typing import Final, Generic, Literal, NoReturn, TypeAlias
from typing_extensions import Self, TypeVar, deprecated

import numpy as np
import numpy.typing as npt
import optype as op
import optype.numpy as onpt
from numpy._typing import _ArrayLikeFloat_co, _ArrayLikeInt, _ArrayLikeNumber_co
from scipy._typing import AnyReal, Untyped
from ._polyint import _Interpolator1D

_CT_co = TypeVar("_CT_co", bound=np.float64 | np.complex128, default=np.float64 | np.complex128, covariant=True)

_Extrapolate: TypeAlias = bool | Literal["periodic"]

###

__all__ = ["BPoly", "NdPPoly", "PPoly", "interp1d", "interp2d", "lagrange"]

err_mesg: str # undocumented

@deprecated("removed in 1.14.0")
class interp2d:
def __init__(
self,
Expand All @@ -15,15 +30,17 @@ class interp2d:
copy: bool = True,
bounds_error: bool = False,
fill_value: Untyped | None = None,
) -> None: ...
) -> NoReturn: ...

@deprecated("legacy")
class interp1d(_Interpolator1D):
bounds_error: Untyped
copy: Untyped
axis: Untyped
y: Untyped
x: Untyped
x_bds: Untyped

def __init__(
self,
x: Untyped,
Expand All @@ -38,53 +55,102 @@ class interp1d(_Interpolator1D):
@property
def fill_value(self) -> Untyped: ...
@fill_value.setter
def fill_value(self, fill_value: Untyped) -> None: ...
def fill_value(self, fill_value: Untyped, /) -> None: ...

class _PPolyBase(Generic[_CT_co]):
c: onpt.Array[onpt.AtLeast2D, _CT_co]
x: onpt.Array[tuple[int], np.float64]
extrapolate: Final[_Extrapolate]
axis: Final[int]

class _PPolyBase:
c: Untyped
x: Untyped
extrapolate: Untyped
axis: Untyped
def __init__(self, c: Untyped, x: Untyped, extrapolate: Untyped | None = None, axis: int = 0) -> None: ...
@classmethod
def construct_fast(cls, c: Untyped, x: Untyped, extrapolate: Untyped | None = None, axis: int = 0) -> Untyped: ...
def extend(self, c: Untyped, x: Untyped) -> None: ...
def __call__(self, x: Untyped, nu: int = 0, extrapolate: Untyped | None = None) -> Untyped: ...

class PPoly(_PPolyBase):
def derivative(self, nu: int = 1) -> Untyped: ...
def antiderivative(self, nu: int = 1) -> Untyped: ...
def integrate(self, a: Untyped, b: Untyped, extrapolate: Untyped | None = None) -> Untyped: ...
def solve(self, y: float = 0.0, discontinuity: bool = True, extrapolate: Untyped | None = None) -> Untyped: ...
def roots(self, discontinuity: bool = True, extrapolate: Untyped | None = None) -> Untyped: ...
def construct_fast(
cls,
c: _ArrayLikeNumber_co,
x: _ArrayLikeFloat_co,
extrapolate: _Extrapolate | None = None,
axis: int = 0,
) -> Self: ...
def __init__(
self,
/,
c: _ArrayLikeNumber_co,
x: _ArrayLikeFloat_co,
extrapolate: _Extrapolate | None = None,
axis: int = 0,
) -> None: ...
def __call__(
self,
/,
x: _ArrayLikeFloat_co,
nu: int = 0,
extrapolate: _Extrapolate | None = None,
) -> onpt.Array[onpt.AtLeast2D, _CT_co]: ...
def extend(self, /, c: _ArrayLikeNumber_co, x: _ArrayLikeFloat_co) -> None: ...

class PPoly(_PPolyBase[_CT_co], Generic[_CT_co]):
@classmethod
def from_spline(cls, tck: Untyped, extrapolate: Untyped | None = None) -> Untyped: ...
def from_spline(cls, tck: Untyped, extrapolate: _Extrapolate | None = None) -> Self: ...
@classmethod
def from_bernstein_basis(cls, bp: Untyped, extrapolate: Untyped | None = None) -> Untyped: ...
def from_bernstein_basis(cls, bp: BPoly[_CT_co], extrapolate: _Extrapolate | None = None) -> Self: ...
def derivative(self, nu: int = 1) -> Self: ...
def antiderivative(self, nu: int = 1) -> Self: ...
def integrate(self, a: AnyReal, b: AnyReal, extrapolate: _Extrapolate | None = None) -> npt.NDArray[_CT_co]: ...
def solve(
self,
y: AnyReal = 0.0,
discontinuity: bool = True,
extrapolate: _Extrapolate | None = None,
) -> _CT_co | npt.NDArray[_CT_co]: ...
def roots(self, discontinuity: bool = True, extrapolate: _Extrapolate | None = None) -> _CT_co | npt.NDArray[_CT_co]: ...

class BPoly(_PPolyBase):
def derivative(self, nu: int = 1) -> Untyped: ...
def antiderivative(self, nu: int = 1) -> Untyped: ...
def integrate(self, a: Untyped, b: Untyped, extrapolate: Untyped | None = None) -> Untyped: ...
class BPoly(_PPolyBase[_CT_co], Generic[_CT_co]):
@classmethod
def from_power_basis(cls, pp: Untyped, extrapolate: Untyped | None = None) -> Untyped: ...
def from_power_basis(cls, pp: PPoly[_CT_co], extrapolate: _Extrapolate | None = None) -> Self: ...
@classmethod
def from_derivatives(
cls,
xi: Untyped,
yi: Untyped,
orders: Untyped | None = None,
extrapolate: Untyped | None = None,
) -> Untyped: ...

class NdPPoly:
def __init__(self, c: Untyped, x: Untyped, extrapolate: Untyped | None = None) -> None: ...
xi: _ArrayLikeNumber_co,
yi: _ArrayLikeNumber_co,
orders: _ArrayLikeInt | None = None,
extrapolate: _Extrapolate | None = None,
) -> Self: ...
def derivative(self, nu: int = 1) -> Self: ...
def antiderivative(self, nu: int = 1) -> Self: ...
def integrate(self, a: AnyReal, b: AnyReal, extrapolate: _Extrapolate | None = None) -> npt.NDArray[_CT_co]: ...

class NdPPoly(Generic[_CT_co]):
c: onpt.Array[onpt.AtLeast2D, _CT_co]
x: tuple[onpt.Array[tuple[int], np.float64], ...]

@classmethod
def construct_fast(cls, c: Untyped, x: Untyped, extrapolate: Untyped | None = None) -> Untyped: ...
def __call__(self, x: Untyped, nu: Untyped | None = None, extrapolate: Untyped | None = None) -> Untyped: ...
def derivative(self, nu: Untyped) -> Untyped: ...
def antiderivative(self, nu: Untyped) -> Untyped: ...
def integrate_1d(self, a: Untyped, b: Untyped, axis: Untyped, extrapolate: Untyped | None = None) -> Untyped: ...
def integrate(self, ranges: Untyped, extrapolate: Untyped | None = None) -> Untyped: ...

def lagrange(x: Untyped, w: Untyped) -> Untyped: ...
def construct_fast(
cls,
c: _ArrayLikeNumber_co,
x: tuple[_ArrayLikeFloat_co, ...],
extrapolate: bool | None = None,
) -> Self: ...
def __init__(
self,
c: _ArrayLikeNumber_co,
x: tuple[_ArrayLikeFloat_co, ...],
extrapolate: bool | None = None,
) -> None: ...
def __call__(
self,
x: _ArrayLikeFloat_co,
nu: tuple[int, ...] | None = None,
extrapolate: bool | None = None,
) -> npt.NDArray[_CT_co]: ...
def derivative(self, nu: tuple[int, ...]) -> Self: ...
def antiderivative(self, nu: tuple[int, ...]) -> Self: ...
def integrate_1d(
self,
a: AnyReal,
b: AnyReal,
axis: op.CanIndex,
extrapolate: bool | None = None,
) -> Self | npt.NDArray[_CT_co]: ...
def integrate(self, ranges: tuple[tuple[AnyReal, AnyReal]], extrapolate: bool | None = None) -> npt.NDArray[_CT_co]: ...

def lagrange(x: _ArrayLikeNumber_co, w: _ArrayLikeNumber_co) -> np.poly1d: ...

0 comments on commit ebb1788

Please sign in to comment.