__init__.py 12 KB

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