Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

scipy.optimize (partial) #46

Merged
merged 11 commits into from
Oct 2, 2024
2 changes: 2 additions & 0 deletions scipy-stubs/_typing.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ __all__ = [
"AnyScalar",
"AnyShape",
"Array0D",
"Casting",
"CorrelateMode",
"NanPolicy",
"Seed",
Expand Down Expand Up @@ -54,6 +55,7 @@ AnyShape: TypeAlias = op.CanIndex | Sequence[op.CanIndex]
# numpy literals
RNG: TypeAlias = np.random.Generator | np.random.RandomState
Seed: TypeAlias = int | RNG
Casting: TypeAlias = Literal["no", "equiv", "safe", "same_kind", "unsafe"]
CorrelateMode: TypeAlias = Literal["valid", "same", "full"]

# scipy literals
Expand Down
56 changes: 26 additions & 30 deletions scipy-stubs/optimize/_constraints.pyi
Original file line number Diff line number Diff line change
@@ -1,19 +1,7 @@
from scipy._typing import Untyped

__all__ = (
"Bounds",
"LinearConstraint",
"NonlinearConstraint",
"PreparedConstraint",
"new_bounds_to_old",
"new_constraint_to_old",
"old_bound_to_new",
"old_constraint_to_new",
"strict_bounds",
)
from scipy._typing import Untyped, UntypedCallable

class NonlinearConstraint:
fun: Untyped
fun: UntypedCallable
lb: Untyped
ub: Untyped
finite_diff_rel_step: Untyped
Expand All @@ -23,40 +11,48 @@ class NonlinearConstraint:
keep_feasible: Untyped
def __init__(
self,
fun,
lb,
ub,
/,
fun: UntypedCallable,
lb: Untyped,
ub: Untyped,
jac: str = "2-point",
hess: Untyped | None = None,
hess: Untyped = ...,
keep_feasible: bool = False,
finite_diff_rel_step: Untyped | None = None,
finite_diff_jac_sparsity: Untyped | None = None,
): ...
) -> None: ...

class LinearConstraint:
A: Untyped
lb: Untyped
ub: Untyped
keep_feasible: Untyped
def __init__(self, A, lb=..., ub=..., keep_feasible: bool = False): ...
def residual(self, x) -> Untyped: ...
def __init__(self, /, A: Untyped, lb: Untyped = ..., ub: Untyped = ..., keep_feasible: bool = False) -> None: ...
def residual(self, /, x: Untyped) -> Untyped: ...

class Bounds:
lb: Untyped
ub: Untyped
keep_feasible: Untyped
def __init__(self, lb=..., ub=..., keep_feasible: bool = False): ...
def residual(self, x) -> Untyped: ...
def __init__(self, /, lb: Untyped = ..., ub: Untyped = ..., keep_feasible: bool = False) -> None: ...
def residual(self, /, x: Untyped) -> Untyped: ...

class PreparedConstraint:
fun: Untyped
bounds: Untyped
keep_feasible: Untyped
def __init__(self, constraint, x0, sparse_jacobian: Untyped | None = None, finite_diff_bounds=...): ...
def violation(self, x) -> Untyped: ...
def __init__(
self,
/,
constraint: Untyped,
x0: Untyped,
sparse_jacobian: Untyped | None = None,
finite_diff_bounds: Untyped = ...,
) -> None: ...
def violation(self, x: Untyped) -> Untyped: ...

def new_bounds_to_old(lb, ub, n) -> Untyped: ...
def old_bound_to_new(bounds) -> Untyped: ...
def strict_bounds(lb, ub, keep_feasible, n_vars) -> Untyped: ...
def new_constraint_to_old(con, x0) -> Untyped: ...
def old_constraint_to_new(ic, con) -> Untyped: ...
def new_bounds_to_old(lb: Untyped, ub: Untyped, n: Untyped) -> Untyped: ...
def old_bound_to_new(bounds: Untyped) -> Untyped: ...
def strict_bounds(lb: Untyped, ub: Untyped, keep_feasible: Untyped, n_vars: Untyped) -> Untyped: ...
def new_constraint_to_old(con: Untyped, x0: Untyped) -> Untyped: ...
def old_constraint_to_new(ic: Untyped, con: Untyped) -> Untyped: ...
31 changes: 0 additions & 31 deletions scipy-stubs/optimize/_elementwise.pyi

This file was deleted.

9 changes: 7 additions & 2 deletions scipy-stubs/optimize/_group_columns.pyi
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
from typing import Final
from typing_extensions import LiteralString

from scipy._typing import Untyped

def group_dense(m, n, A) -> Untyped: ...
def group_sparse(m, n, indices, indptr) -> Untyped: ...
__pythran__: Final[tuple[LiteralString, LiteralString]]

def group_dense(m: Untyped, n: Untyped, A: Untyped) -> Untyped: ...
def group_sparse(m: Untyped, n: Untyped, indices: Untyped, indptr: Untyped) -> Untyped: ...
42 changes: 25 additions & 17 deletions scipy-stubs/optimize/_hessian_update_strategy.pyi
Original file line number Diff line number Diff line change
@@ -1,32 +1,40 @@
from scipy._typing import Untyped
from scipy.linalg import get_blas_funcs as get_blas_funcs, issymmetric as issymmetric
from typing import Final, Literal

from scipy._typing import Untyped, UntypedArray

__all__ = ["BFGS", "SR1", "HessianUpdateStrategy"]

class HessianUpdateStrategy:
def initialize(self, n, approx_type): ...
def update(self, delta_x, delta_grad): ...
def dot(self, p): ...
def get_matrix(self): ...
def initialize(self, /, n: int, approx_type: Literal["hess", "inv_hess"]) -> None: ...
def update(self, /, delta_x: UntypedArray, delta_grad: UntypedArray) -> None: ...
def dot(self, /, p: Untyped) -> UntypedArray: ...
def get_matrix(self, /) -> UntypedArray: ...

class FullHessianUpdateStrategy(HessianUpdateStrategy):
init_scale: Untyped
first_iteration: Untyped
approx_type: Untyped
B: Untyped
H: Untyped
def __init__(self, init_scale: str = "auto"): ...
n: Untyped
def initialize(self, n, approx_type): ...
def update(self, delta_x, delta_grad): ...
def dot(self, p) -> Untyped: ...
def get_matrix(self) -> Untyped: ...
def __init__(self, /, init_scale: Literal["auto"] | float | UntypedArray = "auto") -> None: ...

class BFGS(FullHessianUpdateStrategy):
min_curvature: Untyped
exception_strategy: Untyped
min_curvature: Final[float]
exception_strategy: Final[Literal["skip_update", "damp_update"]]
def __init__(
self, exception_strategy: str = "skip_update", min_curvature: Untyped | None = None, init_scale: str = "auto"
): ...
self,
/,
exception_strategy: Literal["skip_update", "damp_update"] = "skip_update",
min_curvature: float | None = None,
init_scale: Literal["auto"] | float | UntypedArray = "auto",
) -> None: ...

class SR1(FullHessianUpdateStrategy):
min_denominator: Untyped
def __init__(self, min_denominator: float = 1e-08, init_scale: str = "auto"): ...
min_denominator: Final[float]
def __init__(
self,
/,
min_denominator: float = 1e-08,
init_scale: Literal["auto"] | float | UntypedArray = "auto",
) -> None: ...
Empty file.
Empty file.
Empty file.
4 changes: 1 addition & 3 deletions scipy-stubs/optimize/_lbfgsb_py.pyi
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
from scipy._typing import Untyped
from scipy.sparse.linalg import LinearOperator as LinearOperator
from ._constraints import old_bound_to_new as old_bound_to_new
from ._optimize import MemoizeJac as MemoizeJac, OptimizeResult as OptimizeResult
from scipy.sparse.linalg import LinearOperator

def fmin_l_bfgs_b(
func,
Expand Down
71 changes: 52 additions & 19 deletions scipy-stubs/optimize/_linesearch.pyi
Original file line number Diff line number Diff line change
@@ -1,26 +1,34 @@
from scipy._typing import Untyped
from ._dcsrch import DCSRCH as DCSRCH
from scipy._typing import Untyped, UntypedCallable

__all__ = [
"LineSearchWarning",
"line_search_armijo",
"line_search_wolfe1",
"line_search_wolfe2",
"scalar_search_wolfe1",
"scalar_search_wolfe2",
]

class LineSearchWarning(RuntimeWarning): ...

def line_search_wolfe1(
f,
fprime,
xk,
pk,
f: UntypedCallable,
fprime: Untyped,
xk: Untyped,
pk: Untyped,
gfk: Untyped | None = None,
old_fval: Untyped | None = None,
old_old_fval: Untyped | None = None,
args=(),
args: Untyped = (),
c1: float = 0.0001,
c2: float = 0.9,
amax: int = 50,
amin: float = 1e-08,
xtol: float = 1e-14,
) -> Untyped: ...
def scalar_search_wolfe1(
phi,
derphi,
phi: Untyped,
derphi: Untyped,
phi0: Untyped | None = None,
old_phi0: Untyped | None = None,
derphi0: Untyped | None = None,
Expand All @@ -34,23 +42,23 @@ def scalar_search_wolfe1(
line_search = line_search_wolfe1

def line_search_wolfe2(
f,
myfprime,
xk,
pk,
f: UntypedCallable,
myfprime: Untyped,
xk: Untyped,
pk: Untyped,
gfk: Untyped | None = None,
old_fval: Untyped | None = None,
old_old_fval: Untyped | None = None,
args=(),
args: Untyped = (),
c1: float = 0.0001,
c2: float = 0.9,
amax: Untyped | None = None,
extra_condition: Untyped | None = None,
maxiter: int = 10,
) -> Untyped: ...
def scalar_search_wolfe2(
phi,
derphi,
phi: Untyped,
derphi: Untyped,
phi0: Untyped | None = None,
old_phi0: Untyped | None = None,
derphi0: Untyped | None = None,
Expand All @@ -60,6 +68,31 @@ def scalar_search_wolfe2(
extra_condition: Untyped | None = None,
maxiter: int = 10,
) -> Untyped: ...
def line_search_armijo(f, xk, pk, gfk, old_fval, args=(), c1: float = 0.0001, alpha0: int = 1) -> Untyped: ...
def line_search_BFGS(f, xk, pk, gfk, old_fval, args=(), c1: float = 0.0001, alpha0: int = 1) -> Untyped: ...
def scalar_search_armijo(phi, phi0, derphi0, c1: float = 0.0001, alpha0: int = 1, amin: int = 0) -> Untyped: ...
def line_search_armijo(
f: UntypedCallable,
xk: Untyped,
pk: Untyped,
gfk: Untyped,
old_fval: Untyped,
args: Untyped = (),
c1: float = 0.0001,
alpha0: int = 1,
) -> Untyped: ...
def line_search_BFGS(
f: UntypedCallable,
xk: Untyped,
pk: Untyped,
gfk: Untyped,
old_fval: Untyped,
args: Untyped = (),
c1: float = 0.0001,
alpha0: int = 1,
) -> Untyped: ...
def scalar_search_armijo(
phi: Untyped,
phi0: Untyped,
derphi0: Untyped,
c1: float = 0.0001,
alpha0: int = 1,
amin: int = 0,
) -> Untyped: ...
45 changes: 27 additions & 18 deletions scipy-stubs/optimize/_linprog.pyi
Original file line number Diff line number Diff line change
@@ -1,21 +1,30 @@
from scipy._typing import Untyped
from ._optimize import OptimizeResult as OptimizeResult, OptimizeWarning as OptimizeWarning
from collections.abc import Callable, Mapping, Sequence
from typing import Final

__docformat__: str
LINPROG_METHODS: Untyped
import numpy.typing as npt
from ._optimize import OptimizeResult
from ._typing import Bound, MethodLinprog

def linprog_verbose_callback(res) -> Untyped: ...
def linprog_terse_callback(res): ...
__all__ = ["linprog", "linprog_terse_callback", "linprog_verbose_callback"]

__docformat__: Final[str] = ...
LINPROG_METHODS: Final[Sequence[MethodLinprog]] = ...

def linprog_verbose_callback(res: OptimizeResult) -> None: ...
def linprog_terse_callback(res: OptimizeResult) -> None: ...

# TODO: Tighen these array-like types
def linprog(
c,
A_ub: Untyped | None = None,
b_ub: Untyped | None = None,
A_eq: Untyped | None = None,
b_eq: Untyped | None = None,
bounds=(0, None),
method: str = "highs",
callback: Untyped | None = None,
options: Untyped | None = None,
x0: Untyped | None = None,
integrality: Untyped | None = None,
) -> Untyped: ...
c: npt.ArrayLike,
A_ub: npt.ArrayLike | None = None,
b_ub: npt.ArrayLike | None = None,
A_eq: npt.ArrayLike | None = None,
b_eq: npt.ArrayLike | None = None,
bounds: Bound = (0, None),
method: MethodLinprog = "highs",
callback: Callable[[OptimizeResult], None] | None = None,
# TODO: `TypedDict`
options: Mapping[str, object] | None = None,
x0: npt.ArrayLike | None = None,
integrality: npt.ArrayLike | None = None,
) -> OptimizeResult: ...
1 change: 1 addition & 0 deletions scipy-stubs/optimize/_linprog_highs.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# nothing to see here
3 changes: 0 additions & 3 deletions scipy-stubs/optimize/_linprog_ip.pyi
Original file line number Diff line number Diff line change
@@ -1,5 +1,2 @@
from scipy.linalg import LinAlgError as LinAlgError
from ._optimize import OptimizeResult as OptimizeResult, OptimizeWarning as OptimizeWarning

has_umfpack: bool
has_cholmod: bool
1 change: 1 addition & 0 deletions scipy-stubs/optimize/_linprog_rs.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# nothing to see here
2 changes: 1 addition & 1 deletion scipy-stubs/optimize/_linprog_simplex.pyi
Original file line number Diff line number Diff line change
@@ -1 +1 @@
from ._optimize import OptimizeResult as OptimizeResult, OptimizeWarning as OptimizeWarning
# nothing to see here
Loading
Loading