__init__.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  1. """
  2. =====================================================
  3. Optimization and root finding (:mod:`scipy.optimize`)
  4. =====================================================
  5. .. currentmodule:: scipy.optimize
  6. .. toctree::
  7. :hidden:
  8. optimize.cython_optimize
  9. SciPy ``optimize`` provides functions for minimizing (or maximizing)
  10. objective functions, possibly subject to constraints. It includes
  11. solvers for nonlinear problems (with support for both local and global
  12. optimization algorithms), linear programming, constrained
  13. and nonlinear least-squares, root finding, and curve fitting.
  14. Common functions and objects, shared across different solvers, are:
  15. .. autosummary::
  16. :toctree: generated/
  17. show_options - Show specific options optimization solvers.
  18. OptimizeResult - The optimization result returned by some optimizers.
  19. OptimizeWarning - The optimization encountered problems.
  20. Optimization
  21. ============
  22. Scalar functions optimization
  23. -----------------------------
  24. .. autosummary::
  25. :toctree: generated/
  26. minimize_scalar - Interface for minimizers of univariate functions
  27. The `minimize_scalar` function supports the following methods:
  28. .. toctree::
  29. optimize.minimize_scalar-brent
  30. optimize.minimize_scalar-bounded
  31. optimize.minimize_scalar-golden
  32. Local (multivariate) optimization
  33. ---------------------------------
  34. .. autosummary::
  35. :toctree: generated/
  36. minimize - Interface for minimizers of multivariate functions.
  37. The `minimize` function supports the following methods:
  38. .. toctree::
  39. optimize.minimize-neldermead
  40. optimize.minimize-powell
  41. optimize.minimize-cg
  42. optimize.minimize-bfgs
  43. optimize.minimize-newtoncg
  44. optimize.minimize-lbfgsb
  45. optimize.minimize-tnc
  46. optimize.minimize-cobyla
  47. optimize.minimize-cobyqa
  48. optimize.minimize-slsqp
  49. optimize.minimize-trustconstr
  50. optimize.minimize-dogleg
  51. optimize.minimize-trustncg
  52. optimize.minimize-trustkrylov
  53. optimize.minimize-trustexact
  54. Constraints are passed to `minimize` function as a single object or
  55. as a list of objects from the following classes:
  56. .. autosummary::
  57. :toctree: generated/
  58. NonlinearConstraint - Class defining general nonlinear constraints.
  59. LinearConstraint - Class defining general linear constraints.
  60. Simple bound constraints are handled separately and there is a special class
  61. for them:
  62. .. autosummary::
  63. :toctree: generated/
  64. Bounds - Bound constraints.
  65. Quasi-Newton strategies implementing `HessianUpdateStrategy`
  66. interface can be used to approximate the Hessian in `minimize`
  67. function (available only for the 'trust-constr' method). Available
  68. quasi-Newton methods implementing this interface are:
  69. .. autosummary::
  70. :toctree: generated/
  71. BFGS - Broyden-Fletcher-Goldfarb-Shanno (BFGS) Hessian update strategy.
  72. SR1 - Symmetric-rank-1 Hessian update strategy.
  73. .. _global_optimization:
  74. Global optimization
  75. -------------------
  76. .. autosummary::
  77. :toctree: generated/
  78. basinhopping - Basinhopping stochastic optimizer.
  79. brute - Brute force searching optimizer.
  80. differential_evolution - Stochastic optimizer using differential evolution.
  81. shgo - Simplicial homology global optimizer.
  82. dual_annealing - Dual annealing stochastic optimizer.
  83. direct - DIRECT (Dividing Rectangles) optimizer.
  84. Least-squares and curve fitting
  85. ===============================
  86. Nonlinear least-squares
  87. -----------------------
  88. .. autosummary::
  89. :toctree: generated/
  90. least_squares - Solve a nonlinear least-squares problem with bounds on the variables.
  91. Linear least-squares
  92. --------------------
  93. .. autosummary::
  94. :toctree: generated/
  95. nnls - Linear least-squares problem with non-negativity constraint.
  96. lsq_linear - Linear least-squares problem with bound constraints.
  97. isotonic_regression - Least squares problem of isotonic regression via PAVA.
  98. Curve fitting
  99. -------------
  100. .. autosummary::
  101. :toctree: generated/
  102. curve_fit -- Fit curve to a set of points.
  103. Root finding
  104. ============
  105. Scalar functions
  106. ----------------
  107. .. autosummary::
  108. :toctree: generated/
  109. root_scalar - Unified interface for nonlinear solvers of scalar functions.
  110. brentq - quadratic interpolation Brent method.
  111. brenth - Brent method, modified by Harris with hyperbolic extrapolation.
  112. ridder - Ridder's method.
  113. bisect - Bisection method.
  114. newton - Newton's method (also Secant and Halley's methods).
  115. toms748 - Alefeld, Potra & Shi Algorithm 748.
  116. RootResults - The root finding result returned by some root finders.
  117. The `root_scalar` function supports the following methods:
  118. .. toctree::
  119. optimize.root_scalar-brentq
  120. optimize.root_scalar-brenth
  121. optimize.root_scalar-bisect
  122. optimize.root_scalar-ridder
  123. optimize.root_scalar-newton
  124. optimize.root_scalar-toms748
  125. optimize.root_scalar-secant
  126. optimize.root_scalar-halley
  127. The table below lists situations and appropriate methods, along with
  128. *asymptotic* convergence rates per iteration (and per function evaluation)
  129. for successful convergence to a simple root(*).
  130. Bisection is the slowest of them all, adding one bit of accuracy for each
  131. function evaluation, but is guaranteed to converge.
  132. The other bracketing methods all (eventually) increase the number of accurate
  133. bits by about 50% for every function evaluation.
  134. The derivative-based methods, all built on `newton`, can converge quite quickly
  135. if the initial value is close to the root. They can also be applied to
  136. functions defined on (a subset of) the complex plane.
  137. +-------------+----------+----------+-----------+-------------+-------------+----------------+
  138. | Domain of f | Bracket? | Derivatives? | Solvers | Convergence |
  139. + + +----------+-----------+ +-------------+----------------+
  140. | | | `fprime` | `fprime2` | | Guaranteed? | Rate(s)(*) |
  141. +=============+==========+==========+===========+=============+=============+================+
  142. | `R` | Yes | N/A | N/A | - bisection | - Yes | - 1 "Linear" |
  143. | | | | | - brentq | - Yes | - >=1, <= 1.62 |
  144. | | | | | - brenth | - Yes | - >=1, <= 1.62 |
  145. | | | | | - ridder | - Yes | - 2.0 (1.41) |
  146. | | | | | - toms748 | - Yes | - 2.7 (1.65) |
  147. +-------------+----------+----------+-----------+-------------+-------------+----------------+
  148. | `R` or `C` | No | No | No | secant | No | 1.62 (1.62) |
  149. +-------------+----------+----------+-----------+-------------+-------------+----------------+
  150. | `R` or `C` | No | Yes | No | newton | No | 2.00 (1.41) |
  151. +-------------+----------+----------+-----------+-------------+-------------+----------------+
  152. | `R` or `C` | No | Yes | Yes | halley | No | 3.00 (1.44) |
  153. +-------------+----------+----------+-----------+-------------+-------------+----------------+
  154. .. seealso::
  155. `scipy.optimize.cython_optimize` -- Typed Cython versions of root finding functions
  156. Fixed point finding:
  157. .. autosummary::
  158. :toctree: generated/
  159. fixed_point - Single-variable fixed-point solver.
  160. Multidimensional
  161. ----------------
  162. .. autosummary::
  163. :toctree: generated/
  164. root - Unified interface for nonlinear solvers of multivariate functions.
  165. The `root` function supports the following methods:
  166. .. toctree::
  167. optimize.root-hybr
  168. optimize.root-lm
  169. optimize.root-broyden1
  170. optimize.root-broyden2
  171. optimize.root-anderson
  172. optimize.root-linearmixing
  173. optimize.root-diagbroyden
  174. optimize.root-excitingmixing
  175. optimize.root-krylov
  176. optimize.root-dfsane
  177. Elementwise Minimization and Root Finding
  178. =========================================
  179. .. toctree::
  180. :maxdepth: 3
  181. optimize.elementwise
  182. Linear programming / MILP
  183. =========================
  184. .. autosummary::
  185. :toctree: generated/
  186. milp -- Mixed integer linear programming.
  187. linprog -- Unified interface for minimizers of linear programming problems.
  188. The `linprog` function supports the following methods:
  189. .. toctree::
  190. optimize.linprog-simplex
  191. optimize.linprog-interior-point
  192. optimize.linprog-revised_simplex
  193. optimize.linprog-highs-ipm
  194. optimize.linprog-highs-ds
  195. optimize.linprog-highs
  196. The simplex, interior-point, and revised simplex methods support callback
  197. functions, such as:
  198. .. autosummary::
  199. :toctree: generated/
  200. linprog_verbose_callback -- Sample callback function for linprog (simplex).
  201. Assignment problems
  202. ===================
  203. .. autosummary::
  204. :toctree: generated/
  205. linear_sum_assignment -- Solves the linear-sum assignment problem.
  206. quadratic_assignment -- Solves the quadratic assignment problem.
  207. The `quadratic_assignment` function supports the following methods:
  208. .. toctree::
  209. optimize.qap-faq
  210. optimize.qap-2opt
  211. Utilities
  212. =========
  213. Finite-difference approximation
  214. -------------------------------
  215. .. autosummary::
  216. :toctree: generated/
  217. approx_fprime - Approximate the gradient of a scalar function.
  218. check_grad - Check the supplied derivative using finite differences.
  219. Line search
  220. -----------
  221. .. autosummary::
  222. :toctree: generated/
  223. bracket - Bracket a minimum, given two starting points.
  224. line_search - Return a step that satisfies the strong Wolfe conditions.
  225. Hessian approximation
  226. ---------------------
  227. .. autosummary::
  228. :toctree: generated/
  229. LbfgsInvHessProduct - Linear operator for L-BFGS approximate inverse Hessian.
  230. HessianUpdateStrategy - Interface for implementing Hessian update strategies
  231. Benchmark problems
  232. ------------------
  233. .. autosummary::
  234. :toctree: generated/
  235. rosen - The Rosenbrock function.
  236. rosen_der - The derivative of the Rosenbrock function.
  237. rosen_hess - The Hessian matrix of the Rosenbrock function.
  238. rosen_hess_prod - Product of the Rosenbrock Hessian with a vector.
  239. Legacy functions
  240. ================
  241. The functions below are not recommended for use in new scripts;
  242. all of these methods are accessible via a newer, more consistent
  243. interfaces, provided by the interfaces above.
  244. Optimization
  245. ------------
  246. General-purpose multivariate methods:
  247. .. autosummary::
  248. :toctree: generated/
  249. fmin - Nelder-Mead Simplex algorithm.
  250. fmin_powell - Powell's (modified) conjugate direction method.
  251. fmin_cg - Non-linear (Polak-Ribiere) conjugate gradient algorithm.
  252. fmin_bfgs - Quasi-Newton method (Broydon-Fletcher-Goldfarb-Shanno).
  253. fmin_ncg - Line-search Newton Conjugate Gradient.
  254. Constrained multivariate methods:
  255. .. autosummary::
  256. :toctree: generated/
  257. fmin_l_bfgs_b - Zhu, Byrd, and Nocedal's constrained optimizer.
  258. fmin_tnc - Truncated Newton code.
  259. fmin_cobyla - Constrained optimization by linear approximation.
  260. fmin_slsqp - Minimization using sequential least-squares programming.
  261. Univariate (scalar) minimization methods:
  262. .. autosummary::
  263. :toctree: generated/
  264. fminbound - Bounded minimization of a scalar function.
  265. brent - 1-D function minimization using Brent method.
  266. golden - 1-D function minimization using Golden Section method.
  267. Least-squares
  268. -------------
  269. .. autosummary::
  270. :toctree: generated/
  271. leastsq - Minimize the sum of squares of M equations in N unknowns.
  272. Root finding
  273. ------------
  274. General nonlinear solvers:
  275. .. autosummary::
  276. :toctree: generated/
  277. fsolve - Non-linear multivariable equation solver.
  278. broyden1 - Broyden's first method.
  279. broyden2 - Broyden's second method.
  280. NoConvergence - Exception raised when nonlinear solver does not converge.
  281. Large-scale nonlinear solvers:
  282. .. autosummary::
  283. :toctree: generated/
  284. newton_krylov
  285. anderson
  286. BroydenFirst
  287. InverseJacobian
  288. KrylovJacobian
  289. Simple iteration solvers:
  290. .. autosummary::
  291. :toctree: generated/
  292. excitingmixing
  293. linearmixing
  294. diagbroyden
  295. """ # noqa: E501
  296. from ._optimize import *
  297. from ._minimize import *
  298. from ._root import *
  299. from ._root_scalar import *
  300. from ._minpack_py import *
  301. from ._zeros_py import *
  302. from ._lbfgsb_py import fmin_l_bfgs_b, LbfgsInvHessProduct
  303. from ._tnc import fmin_tnc
  304. from ._cobyla_py import fmin_cobyla
  305. from ._nonlin import *
  306. from ._slsqp_py import fmin_slsqp
  307. from ._nnls import nnls
  308. from ._basinhopping import basinhopping
  309. from ._linprog import linprog, linprog_verbose_callback
  310. from ._lsap import linear_sum_assignment
  311. from ._differentialevolution import differential_evolution
  312. from ._lsq import least_squares, lsq_linear
  313. from ._isotonic import isotonic_regression
  314. from ._constraints import (NonlinearConstraint,
  315. LinearConstraint,
  316. Bounds)
  317. from ._hessian_update_strategy import HessianUpdateStrategy, BFGS, SR1
  318. from ._shgo import shgo
  319. from ._dual_annealing import dual_annealing
  320. from ._qap import quadratic_assignment
  321. from ._direct_py import direct
  322. from ._milp import milp
  323. # Deprecated namespaces, to be removed in v2.0.0
  324. from . import (
  325. cobyla, lbfgsb, linesearch, minpack, minpack2, moduleTNC, nonlin, optimize,
  326. slsqp, tnc, zeros
  327. )
  328. __all__ = [s for s in dir() if not s.startswith('_')]
  329. from scipy._lib._testutils import PytestTester
  330. test = PytestTester(__name__)
  331. del PytestTester