techprotrade commited on
Commit
d151d5f
·
verified ·
1 Parent(s): 8fc5980

Add loan-advisor-agent (part 5)

Browse files
This view is limited to 50 files because it contains too many changes.   See raw diff
Files changed (50) hide show
  1. .gitattributes +22 -0
  2. loan-advisor-agent/.venv/Lib/site-packages/scipy/optimize/_tstutils.py +972 -0
  3. loan-advisor-agent/.venv/Lib/site-packages/scipy/optimize/_zeros.cp312-win_amd64.dll.a +0 -0
  4. loan-advisor-agent/.venv/Lib/site-packages/scipy/optimize/_zeros.cp312-win_amd64.pyd +0 -0
  5. loan-advisor-agent/.venv/Lib/site-packages/scipy/optimize/_zeros_py.py +1475 -0
  6. loan-advisor-agent/.venv/Lib/site-packages/scipy/optimize/cobyla.py +19 -0
  7. loan-advisor-agent/.venv/Lib/site-packages/scipy/optimize/cython_optimize.pxd +11 -0
  8. loan-advisor-agent/.venv/Lib/site-packages/scipy/optimize/cython_optimize/__init__.py +133 -0
  9. loan-advisor-agent/.venv/Lib/site-packages/scipy/optimize/cython_optimize/_zeros.cp312-win_amd64.dll.a +0 -0
  10. loan-advisor-agent/.venv/Lib/site-packages/scipy/optimize/cython_optimize/_zeros.cp312-win_amd64.pyd +0 -0
  11. loan-advisor-agent/.venv/Lib/site-packages/scipy/optimize/cython_optimize/_zeros.pxd +33 -0
  12. loan-advisor-agent/.venv/Lib/site-packages/scipy/optimize/cython_optimize/c_zeros.pxd +26 -0
  13. loan-advisor-agent/.venv/Lib/site-packages/scipy/optimize/elementwise.py +38 -0
  14. loan-advisor-agent/.venv/Lib/site-packages/scipy/optimize/lbfgsb.py +23 -0
  15. loan-advisor-agent/.venv/Lib/site-packages/scipy/optimize/linesearch.py +18 -0
  16. loan-advisor-agent/.venv/Lib/site-packages/scipy/optimize/minpack.py +27 -0
  17. loan-advisor-agent/.venv/Lib/site-packages/scipy/optimize/minpack2.py +17 -0
  18. loan-advisor-agent/.venv/Lib/site-packages/scipy/optimize/moduleTNC.py +19 -0
  19. loan-advisor-agent/.venv/Lib/site-packages/scipy/optimize/nonlin.py +29 -0
  20. loan-advisor-agent/.venv/Lib/site-packages/scipy/optimize/optimize.py +40 -0
  21. loan-advisor-agent/.venv/Lib/site-packages/scipy/optimize/slsqp.py +22 -0
  22. loan-advisor-agent/.venv/Lib/site-packages/scipy/optimize/tests/__init__.py +0 -0
  23. loan-advisor-agent/.venv/Lib/site-packages/scipy/optimize/tests/_cython_examples/extending.pyx +43 -0
  24. loan-advisor-agent/.venv/Lib/site-packages/scipy/optimize/tests/_cython_examples/meson.build +32 -0
  25. loan-advisor-agent/.venv/Lib/site-packages/scipy/optimize/tests/test__basinhopping.py +537 -0
  26. loan-advisor-agent/.venv/Lib/site-packages/scipy/optimize/tests/test__differential_evolution.py +1758 -0
  27. loan-advisor-agent/.venv/Lib/site-packages/scipy/optimize/tests/test__dual_annealing.py +415 -0
  28. loan-advisor-agent/.venv/Lib/site-packages/scipy/optimize/tests/test__linprog_clean_inputs.py +320 -0
  29. loan-advisor-agent/.venv/Lib/site-packages/scipy/optimize/tests/test__numdiff.py +885 -0
  30. loan-advisor-agent/.venv/Lib/site-packages/scipy/optimize/tests/test__remove_redundancy.py +227 -0
  31. loan-advisor-agent/.venv/Lib/site-packages/scipy/optimize/tests/test__root.py +123 -0
  32. loan-advisor-agent/.venv/Lib/site-packages/scipy/optimize/tests/test__shgo.py +1228 -0
  33. loan-advisor-agent/.venv/Lib/site-packages/scipy/optimize/tests/test__spectral.py +225 -0
  34. loan-advisor-agent/.venv/Lib/site-packages/scipy/optimize/tests/test_bracket.py +888 -0
  35. loan-advisor-agent/.venv/Lib/site-packages/scipy/optimize/tests/test_chandrupatla.py +976 -0
  36. loan-advisor-agent/.venv/Lib/site-packages/scipy/optimize/tests/test_cobyla.py +195 -0
  37. loan-advisor-agent/.venv/Lib/site-packages/scipy/optimize/tests/test_cobyqa.py +252 -0
  38. loan-advisor-agent/.venv/Lib/site-packages/scipy/optimize/tests/test_constraint_conversion.py +284 -0
  39. loan-advisor-agent/.venv/Lib/site-packages/scipy/optimize/tests/test_constraints.py +255 -0
  40. loan-advisor-agent/.venv/Lib/site-packages/scipy/optimize/tests/test_cython_optimize.py +92 -0
  41. loan-advisor-agent/.venv/Lib/site-packages/scipy/optimize/tests/test_differentiable_functions.py +1023 -0
  42. loan-advisor-agent/.venv/Lib/site-packages/scipy/optimize/tests/test_direct.py +321 -0
  43. loan-advisor-agent/.venv/Lib/site-packages/scipy/optimize/tests/test_extending.py +28 -0
  44. loan-advisor-agent/.venv/Lib/site-packages/scipy/optimize/tests/test_hessian_update_strategy.py +300 -0
  45. loan-advisor-agent/.venv/Lib/site-packages/scipy/optimize/tests/test_isotonic_regression.py +167 -0
  46. loan-advisor-agent/.venv/Lib/site-packages/scipy/optimize/tests/test_lbfgsb_hessinv.py +65 -0
  47. loan-advisor-agent/.venv/Lib/site-packages/scipy/optimize/tests/test_lbfgsb_setulb.py +122 -0
  48. loan-advisor-agent/.venv/Lib/site-packages/scipy/optimize/tests/test_least_squares.py +999 -0
  49. loan-advisor-agent/.venv/Lib/site-packages/scipy/optimize/tests/test_linear_assignment.py +116 -0
  50. loan-advisor-agent/.venv/Lib/site-packages/scipy/optimize/tests/test_linesearch.py +334 -0
.gitattributes CHANGED
@@ -611,3 +611,25 @@ loan-advisor-agent/.venv/Lib/site-packages/scipy/optimize/_moduleTNC.cp312-win_a
611
  loan-advisor-agent/.venv/Lib/site-packages/scipy/optimize/_pava_pybind.cp312-win_amd64.pyd filter=lfs diff=lfs merge=lfs -text
612
  loan-advisor-agent/.venv/Lib/site-packages/scipy/optimize/_slsqplib.cp312-win_amd64.pyd filter=lfs diff=lfs merge=lfs -text
613
  loan-advisor-agent/.venv/Lib/site-packages/scipy/optimize/_trlib/_trlib.cp312-win_amd64.pyd filter=lfs diff=lfs merge=lfs -text
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
611
  loan-advisor-agent/.venv/Lib/site-packages/scipy/optimize/_pava_pybind.cp312-win_amd64.pyd filter=lfs diff=lfs merge=lfs -text
612
  loan-advisor-agent/.venv/Lib/site-packages/scipy/optimize/_slsqplib.cp312-win_amd64.pyd filter=lfs diff=lfs merge=lfs -text
613
  loan-advisor-agent/.venv/Lib/site-packages/scipy/optimize/_trlib/_trlib.cp312-win_amd64.pyd filter=lfs diff=lfs merge=lfs -text
614
+ loan-advisor-agent/.venv/Lib/site-packages/scipy/signal/_max_len_seq_inner.cp312-win_amd64.pyd filter=lfs diff=lfs merge=lfs -text
615
+ loan-advisor-agent/.venv/Lib/site-packages/scipy/signal/_peak_finding_utils.cp312-win_amd64.pyd filter=lfs diff=lfs merge=lfs -text
616
+ loan-advisor-agent/.venv/Lib/site-packages/scipy/signal/_sigtools.cp312-win_amd64.pyd filter=lfs diff=lfs merge=lfs -text
617
+ loan-advisor-agent/.venv/Lib/site-packages/scipy/signal/_sosfilt.cp312-win_amd64.pyd filter=lfs diff=lfs merge=lfs -text
618
+ loan-advisor-agent/.venv/Lib/site-packages/scipy/signal/_upfirdn_apply.cp312-win_amd64.pyd filter=lfs diff=lfs merge=lfs -text
619
+ loan-advisor-agent/.venv/Lib/site-packages/scipy/sparse/_csparsetools.cp312-win_amd64.pyd filter=lfs diff=lfs merge=lfs -text
620
+ loan-advisor-agent/.venv/Lib/site-packages/scipy/sparse/_sparsetools.cp312-win_amd64.pyd filter=lfs diff=lfs merge=lfs -text
621
+ loan-advisor-agent/.venv/Lib/site-packages/scipy/sparse/csgraph/_flow.cp312-win_amd64.pyd filter=lfs diff=lfs merge=lfs -text
622
+ loan-advisor-agent/.venv/Lib/site-packages/scipy/sparse/csgraph/_matching.cp312-win_amd64.pyd filter=lfs diff=lfs merge=lfs -text
623
+ loan-advisor-agent/.venv/Lib/site-packages/scipy/sparse/csgraph/_min_spanning_tree.cp312-win_amd64.pyd filter=lfs diff=lfs merge=lfs -text
624
+ loan-advisor-agent/.venv/Lib/site-packages/scipy/sparse/csgraph/_reordering.cp312-win_amd64.pyd filter=lfs diff=lfs merge=lfs -text
625
+ loan-advisor-agent/.venv/Lib/site-packages/scipy/sparse/csgraph/_shortest_path.cp312-win_amd64.pyd filter=lfs diff=lfs merge=lfs -text
626
+ loan-advisor-agent/.venv/Lib/site-packages/scipy/sparse/csgraph/_tools.cp312-win_amd64.pyd filter=lfs diff=lfs merge=lfs -text
627
+ loan-advisor-agent/.venv/Lib/site-packages/scipy/sparse/csgraph/_traversal.cp312-win_amd64.pyd filter=lfs diff=lfs merge=lfs -text
628
+ loan-advisor-agent/.venv/Lib/site-packages/scipy/sparse/linalg/_dsolve/_superlu.cp312-win_amd64.pyd filter=lfs diff=lfs merge=lfs -text
629
+ loan-advisor-agent/.venv/Lib/site-packages/scipy/sparse/linalg/_eigen/arpack/_arpacklib.cp312-win_amd64.pyd filter=lfs diff=lfs merge=lfs -text
630
+ loan-advisor-agent/.venv/Lib/site-packages/scipy/sparse/linalg/_propack.cp312-win_amd64.pyd filter=lfs diff=lfs merge=lfs -text
631
+ loan-advisor-agent/.venv/Lib/site-packages/scipy/spatial/__pycache__/distance.cpython-312.pyc filter=lfs diff=lfs merge=lfs -text
632
+ loan-advisor-agent/.venv/Lib/site-packages/scipy/spatial/_ckdtree.cp312-win_amd64.pyd filter=lfs diff=lfs merge=lfs -text
633
+ loan-advisor-agent/.venv/Lib/site-packages/scipy/spatial/_distance_pybind.cp312-win_amd64.pyd filter=lfs diff=lfs merge=lfs -text
634
+ loan-advisor-agent/.venv/Lib/site-packages/scipy/spatial/_distance_wrap.cp312-win_amd64.pyd filter=lfs diff=lfs merge=lfs -text
635
+ loan-advisor-agent/.venv/Lib/site-packages/scipy/spatial/_qhull.cp312-win_amd64.pyd filter=lfs diff=lfs merge=lfs -text
loan-advisor-agent/.venv/Lib/site-packages/scipy/optimize/_tstutils.py ADDED
@@ -0,0 +1,972 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ r"""
2
+ Parameters used in test and benchmark methods.
3
+
4
+ Collections of test cases suitable for testing 1-D root-finders
5
+ 'original': The original benchmarking functions.
6
+ Real-valued functions of real-valued inputs on an interval
7
+ with a zero.
8
+ f1, .., f3 are continuous and infinitely differentiable
9
+ f4 has a left- and right- discontinuity at the root
10
+ f5 has a root at 1 replacing a 1st order pole
11
+ f6 is randomly positive on one side of the root,
12
+ randomly negative on the other.
13
+ f4 - f6 are not continuous at the root.
14
+
15
+ 'aps': The test problems in the 1995 paper
16
+ TOMS "Algorithm 748: Enclosing Zeros of Continuous Functions"
17
+ by Alefeld, Potra and Shi. Real-valued functions of
18
+ real-valued inputs on an interval with a zero.
19
+ Suitable for methods which start with an enclosing interval, and
20
+ derivatives up to 2nd order.
21
+
22
+ 'complex': Some complex-valued functions of complex-valued inputs.
23
+ No enclosing bracket is provided.
24
+ Suitable for methods which use one or more starting values, and
25
+ derivatives up to 2nd order.
26
+
27
+ The test cases are provided as a list of dictionaries. The dictionary
28
+ keys will be a subset of:
29
+ ["f", "fprime", "fprime2", "args", "bracket", "smoothness",
30
+ "a", "b", "x0", "x1", "root", "ID"]
31
+ """
32
+
33
+ # Sources:
34
+ # [1] Alefeld, G. E. and Potra, F. A. and Shi, Yixun,
35
+ # "Algorithm 748: Enclosing Zeros of Continuous Functions",
36
+ # ACM Trans. Math. Softw. Volume 221(1995)
37
+ # doi = {10.1145/210089.210111},
38
+ # [2] Chandrupatla, Tirupathi R. "A new hybrid quadratic/bisection algorithm
39
+ # for finding the zero of a nonlinear function without using derivatives."
40
+ # Advances in Engineering Software 28.3 (1997): 145-149.
41
+
42
+ from random import random
43
+
44
+ import numpy as np
45
+
46
+ from scipy.optimize import _zeros_py as cc
47
+ from scipy._lib._array_api import array_namespace
48
+
49
+ # "description" refers to the original functions
50
+ description = """
51
+ f2 is a symmetric parabola, x**2 - 1
52
+ f3 is a quartic polynomial with large hump in interval
53
+ f4 is step function with a discontinuity at 1
54
+ f5 is a hyperbola with vertical asymptote at 1
55
+ f6 has random values positive to left of 1, negative to right
56
+
57
+ Of course, these are not real problems. They just test how the
58
+ 'good' solvers behave in bad circumstances where bisection is
59
+ really the best. A good solver should not be much worse than
60
+ bisection in such circumstance, while being faster for smooth
61
+ monotone sorts of functions.
62
+ """
63
+
64
+
65
+ def f1(x):
66
+ r"""f1 is a quadratic with roots at 0 and 1"""
67
+ return x * (x - 1.)
68
+
69
+
70
+ def f1_fp(x):
71
+ return 2 * x - 1
72
+
73
+
74
+ def f1_fpp(x):
75
+ return 2
76
+
77
+
78
+ def f2(x):
79
+ r"""f2 is a symmetric parabola, x**2 - 1"""
80
+ return x**2 - 1
81
+
82
+
83
+ def f2_fp(x):
84
+ return 2 * x
85
+
86
+
87
+ def f2_fpp(x):
88
+ return 2
89
+
90
+
91
+ def f3(x):
92
+ r"""A quartic with roots at 0, 1, 2 and 3"""
93
+ return x * (x - 1.) * (x - 2.) * (x - 3.) # x**4 - 6x**3 + 11x**2 - 6x
94
+
95
+
96
+ def f3_fp(x):
97
+ return 4 * x**3 - 18 * x**2 + 22 * x - 6
98
+
99
+
100
+ def f3_fpp(x):
101
+ return 12 * x**2 - 36 * x + 22
102
+
103
+
104
+ def f4(x):
105
+ r"""Piecewise linear, left- and right- discontinuous at x=1, the root."""
106
+ if x > 1:
107
+ return 1.0 + .1 * x
108
+ if x < 1:
109
+ return -1.0 + .1 * x
110
+ return 0
111
+
112
+
113
+ def f5(x):
114
+ r"""
115
+ Hyperbola with a pole at x=1, but pole replaced with 0. Not continuous at root.
116
+ """
117
+ if x != 1:
118
+ return 1.0 / (1. - x)
119
+ return 0
120
+
121
+
122
+ # f6(x) returns random value. Without memoization, calling twice with the
123
+ # same x returns different values, hence a "random value", not a
124
+ # "function with random values"
125
+ _f6_cache = {}
126
+ def f6(x):
127
+ v = _f6_cache.get(x, None)
128
+ if v is None:
129
+ if x > 1:
130
+ v = random()
131
+ elif x < 1:
132
+ v = -random()
133
+ else:
134
+ v = 0
135
+ _f6_cache[x] = v
136
+ return v
137
+
138
+
139
+ # Each Original test case has
140
+ # - a function and its two derivatives,
141
+ # - additional arguments,
142
+ # - a bracket enclosing a root,
143
+ # - the order of differentiability (smoothness) on this interval
144
+ # - a starting value for methods which don't require a bracket
145
+ # - the root (inside the bracket)
146
+ # - an Identifier of the test case
147
+
148
+ _ORIGINAL_TESTS_KEYS = [
149
+ "f", "fprime", "fprime2", "args", "bracket", "smoothness", "x0", "root", "ID"
150
+ ]
151
+ _ORIGINAL_TESTS = [
152
+ [f1, f1_fp, f1_fpp, (), [0.5, np.sqrt(3)], np.inf, 0.6, 1.0, "original.01.00"],
153
+ [f2, f2_fp, f2_fpp, (), [0.5, np.sqrt(3)], np.inf, 0.6, 1.0, "original.02.00"],
154
+ [f3, f3_fp, f3_fpp, (), [0.5, np.sqrt(3)], np.inf, 0.6, 1.0, "original.03.00"],
155
+ [f4, None, None, (), [0.5, np.sqrt(3)], -1, 0.6, 1.0, "original.04.00"],
156
+ [f5, None, None, (), [0.5, np.sqrt(3)], -1, 0.6, 1.0, "original.05.00"],
157
+ [f6, None, None, (), [0.5, np.sqrt(3)], -np.inf, 0.6, 1.0, "original.05.00"]
158
+ ]
159
+
160
+ _ORIGINAL_TESTS_DICTS = [
161
+ dict(zip(_ORIGINAL_TESTS_KEYS, testcase)) for testcase in _ORIGINAL_TESTS
162
+ ]
163
+
164
+ # ##################
165
+ # "APS" test cases
166
+ # Functions and test cases that appear in [1]
167
+
168
+
169
+ def aps01_f(x):
170
+ r"""Straightforward sum of trigonometric function and polynomial"""
171
+ return np.sin(x) - x / 2
172
+
173
+
174
+ def aps01_fp(x):
175
+ return np.cos(x) - 1.0 / 2
176
+
177
+
178
+ def aps01_fpp(x):
179
+ return -np.sin(x)
180
+
181
+
182
+ def aps02_f(x):
183
+ r"""poles at x=n**2, 1st and 2nd derivatives at root are also close to 0"""
184
+ ii = np.arange(1, 21)
185
+ return -2 * np.sum((2 * ii - 5)**2 / (x - ii**2)**3)
186
+
187
+
188
+ def aps02_fp(x):
189
+ ii = np.arange(1, 21)
190
+ return 6 * np.sum((2 * ii - 5)**2 / (x - ii**2)**4)
191
+
192
+
193
+ def aps02_fpp(x):
194
+ ii = np.arange(1, 21)
195
+ return 24 * np.sum((2 * ii - 5)**2 / (x - ii**2)**5)
196
+
197
+
198
+ def aps03_f(x, a, b):
199
+ r"""Rapidly changing at the root"""
200
+ return a * x * np.exp(b * x)
201
+
202
+
203
+ def aps03_fp(x, a, b):
204
+ return a * (b * x + 1) * np.exp(b * x)
205
+
206
+
207
+ def aps03_fpp(x, a, b):
208
+ return a * (b * (b * x + 1) + b) * np.exp(b * x)
209
+
210
+
211
+ def aps04_f(x, n, a):
212
+ r"""Medium-degree polynomial"""
213
+ return x**n - a
214
+
215
+
216
+ def aps04_fp(x, n, a):
217
+ return n * x**(n - 1)
218
+
219
+
220
+ def aps04_fpp(x, n, a):
221
+ return n * (n - 1) * x**(n - 2)
222
+
223
+
224
+ def aps05_f(x):
225
+ r"""Simple Trigonometric function"""
226
+ return np.sin(x) - 1.0 / 2
227
+
228
+
229
+ def aps05_fp(x):
230
+ return np.cos(x)
231
+
232
+
233
+ def aps05_fpp(x):
234
+ return -np.sin(x)
235
+
236
+
237
+ def aps06_f(x, n):
238
+ r"""Exponential rapidly changing from -1 to 1 at x=0"""
239
+ return 2 * x * np.exp(-n) - 2 * np.exp(-n * x) + 1
240
+
241
+
242
+ def aps06_fp(x, n):
243
+ return 2 * np.exp(-n) + 2 * n * np.exp(-n * x)
244
+
245
+
246
+ def aps06_fpp(x, n):
247
+ return -2 * n * n * np.exp(-n * x)
248
+
249
+
250
+ def aps07_f(x, n):
251
+ r"""Upside down parabola with parametrizable height"""
252
+ return (1 + (1 - n)**2) * x - (1 - n * x)**2
253
+
254
+
255
+ def aps07_fp(x, n):
256
+ return (1 + (1 - n)**2) + 2 * n * (1 - n * x)
257
+
258
+
259
+ def aps07_fpp(x, n):
260
+ return -2 * n * n
261
+
262
+
263
+ def aps08_f(x, n):
264
+ r"""Degree n polynomial"""
265
+ return x * x - (1 - x)**n
266
+
267
+
268
+ def aps08_fp(x, n):
269
+ return 2 * x + n * (1 - x)**(n - 1)
270
+
271
+
272
+ def aps08_fpp(x, n):
273
+ return 2 - n * (n - 1) * (1 - x)**(n - 2)
274
+
275
+
276
+ def aps09_f(x, n):
277
+ r"""Upside down quartic with parametrizable height"""
278
+ return (1 + (1 - n)**4) * x - (1 - n * x)**4
279
+
280
+
281
+ def aps09_fp(x, n):
282
+ return (1 + (1 - n)**4) + 4 * n * (1 - n * x)**3
283
+
284
+
285
+ def aps09_fpp(x, n):
286
+ return -12 * n * (1 - n * x)**2
287
+
288
+
289
+ def aps10_f(x, n):
290
+ r"""Exponential plus a polynomial"""
291
+ return np.exp(-n * x) * (x - 1) + x**n
292
+
293
+
294
+ def aps10_fp(x, n):
295
+ return np.exp(-n * x) * (-n * (x - 1) + 1) + n * x**(n - 1)
296
+
297
+
298
+ def aps10_fpp(x, n):
299
+ return (np.exp(-n * x) * (-n * (-n * (x - 1) + 1) + -n * x)
300
+ + n * (n - 1) * x**(n - 2))
301
+
302
+
303
+ def aps11_f(x, n):
304
+ r"""Rational function with a zero at x=1/n and a pole at x=0"""
305
+ return (n * x - 1) / ((n - 1) * x)
306
+
307
+
308
+ def aps11_fp(x, n):
309
+ return 1 / (n - 1) / x**2
310
+
311
+
312
+ def aps11_fpp(x, n):
313
+ return -2 / (n - 1) / x**3
314
+
315
+
316
+ def aps12_f(x, n):
317
+ r"""nth root of x, with a zero at x=n"""
318
+ return np.power(x, 1.0 / n) - np.power(n, 1.0 / n)
319
+
320
+
321
+ def aps12_fp(x, n):
322
+ return np.power(x, (1.0 - n) / n) / n
323
+
324
+
325
+ def aps12_fpp(x, n):
326
+ return np.power(x, (1.0 - 2 * n) / n) * (1.0 / n) * (1.0 - n) / n
327
+
328
+
329
+ _MAX_EXPABLE = np.log(np.finfo(float).max)
330
+
331
+
332
+ def aps13_f(x):
333
+ r"""Function with *all* derivatives 0 at the root"""
334
+ if x == 0:
335
+ return 0
336
+ # x2 = 1.0/x**2
337
+ # if x2 > 708:
338
+ # return 0
339
+ y = 1 / x**2
340
+ if y > _MAX_EXPABLE:
341
+ return 0
342
+ return x / np.exp(y)
343
+
344
+
345
+ def aps13_fp(x):
346
+ if x == 0:
347
+ return 0
348
+ y = 1 / x**2
349
+ if y > _MAX_EXPABLE:
350
+ return 0
351
+ return (1 + 2 / x**2) / np.exp(y)
352
+
353
+
354
+ def aps13_fpp(x):
355
+ if x == 0:
356
+ return 0
357
+ y = 1 / x**2
358
+ if y > _MAX_EXPABLE:
359
+ return 0
360
+ return 2 * (2 - x**2) / x**5 / np.exp(y)
361
+
362
+
363
+ def aps14_f(x, n):
364
+ r"""0 for negative x-values, trigonometric+linear for x positive"""
365
+ if x <= 0:
366
+ return -n / 20.0
367
+ return n / 20.0 * (x / 1.5 + np.sin(x) - 1)
368
+
369
+
370
+ def aps14_fp(x, n):
371
+ if x <= 0:
372
+ return 0
373
+ return n / 20.0 * (1.0 / 1.5 + np.cos(x))
374
+
375
+
376
+ def aps14_fpp(x, n):
377
+ if x <= 0:
378
+ return 0
379
+ return -n / 20.0 * (np.sin(x))
380
+
381
+
382
+ def aps15_f(x, n):
383
+ r"""piecewise linear, constant outside of [0, 0.002/(1+n)]"""
384
+ if x < 0:
385
+ return -0.859
386
+ if x > 2 * 1e-3 / (1 + n):
387
+ return np.e - 1.859
388
+ return np.exp((n + 1) * x / 2 * 1000) - 1.859
389
+
390
+
391
+ def aps15_fp(x, n):
392
+ if not 0 <= x <= 2 * 1e-3 / (1 + n):
393
+ return np.e - 1.859
394
+ return np.exp((n + 1) * x / 2 * 1000) * (n + 1) / 2 * 1000
395
+
396
+
397
+ def aps15_fpp(x, n):
398
+ if not 0 <= x <= 2 * 1e-3 / (1 + n):
399
+ return np.e - 1.859
400
+ return np.exp((n + 1) * x / 2 * 1000) * (n + 1) / 2 * 1000 * (n + 1) / 2 * 1000
401
+
402
+
403
+ # Each APS test case has
404
+ # - a function and its two derivatives,
405
+ # - additional arguments,
406
+ # - a bracket enclosing a root,
407
+ # - the order of differentiability of the function on this interval
408
+ # - a starting value for methods which don't require a bracket
409
+ # - the root (inside the bracket)
410
+ # - an Identifier of the test case
411
+ #
412
+ # Algorithm 748 is a bracketing algorithm so a bracketing interval was provided
413
+ # in [1] for each test case. Newton and Halley methods need a single
414
+ # starting point x0, which was chosen to be near the middle of the interval,
415
+ # unless that would have made the problem too easy.
416
+
417
+ _APS_TESTS_KEYS = [
418
+ "f", "fprime", "fprime2", "args", "bracket", "smoothness", "x0", "root", "ID"
419
+ ]
420
+ _APS_TESTS = [
421
+ [aps01_f, aps01_fp, aps01_fpp, (), [np.pi / 2, np.pi], np.inf,
422
+ 3, 1.89549426703398094e+00, "aps.01.00"],
423
+ [aps02_f, aps02_fp, aps02_fpp, (), [1 + 1e-9, 4 - 1e-9], np.inf,
424
+ 2, 3.02291534727305677e+00, "aps.02.00"],
425
+ [aps02_f, aps02_fp, aps02_fpp, (), [4 + 1e-9, 9 - 1e-9], np.inf,
426
+ 5, 6.68375356080807848e+00, "aps.02.01"],
427
+ [aps02_f, aps02_fp, aps02_fpp, (), [9 + 1e-9, 16 - 1e-9], np.inf,
428
+ 10, 1.12387016550022114e+01, "aps.02.02"],
429
+ [aps02_f, aps02_fp, aps02_fpp, (), [16 + 1e-9, 25 - 1e-9], np.inf,
430
+ 17, 1.96760000806234103e+01, "aps.02.03"],
431
+ [aps02_f, aps02_fp, aps02_fpp, (), [25 + 1e-9, 36 - 1e-9], np.inf,
432
+ 26, 2.98282273265047557e+01, "aps.02.04"],
433
+ [aps02_f, aps02_fp, aps02_fpp, (), [36 + 1e-9, 49 - 1e-9], np.inf,
434
+ 37, 4.19061161952894139e+01, "aps.02.05"],
435
+ [aps02_f, aps02_fp, aps02_fpp, (), [49 + 1e-9, 64 - 1e-9], np.inf,
436
+ 50, 5.59535958001430913e+01, "aps.02.06"],
437
+ [aps02_f, aps02_fp, aps02_fpp, (), [64 + 1e-9, 81 - 1e-9], np.inf,
438
+ 65, 7.19856655865877997e+01, "aps.02.07"],
439
+ [aps02_f, aps02_fp, aps02_fpp, (), [81 + 1e-9, 100 - 1e-9], np.inf,
440
+ 82, 9.00088685391666701e+01, "aps.02.08"],
441
+ [aps02_f, aps02_fp, aps02_fpp, (), [100 + 1e-9, 121 - 1e-9], np.inf,
442
+ 101, 1.10026532748330197e+02, "aps.02.09"],
443
+ [aps03_f, aps03_fp, aps03_fpp, (-40, -1), [-9, 31], np.inf,
444
+ -2, 0, "aps.03.00"],
445
+ [aps03_f, aps03_fp, aps03_fpp, (-100, -2), [-9, 31], np.inf,
446
+ -2, 0, "aps.03.01"],
447
+ [aps03_f, aps03_fp, aps03_fpp, (-200, -3), [-9, 31], np.inf,
448
+ -2, 0, "aps.03.02"],
449
+ [aps04_f, aps04_fp, aps04_fpp, (4, 0.2), [0, 5], np.inf,
450
+ 2.5, 6.68740304976422006e-01, "aps.04.00"],
451
+ [aps04_f, aps04_fp, aps04_fpp, (6, 0.2), [0, 5], np.inf,
452
+ 2.5, 7.64724491331730039e-01, "aps.04.01"],
453
+ [aps04_f, aps04_fp, aps04_fpp, (8, 0.2), [0, 5], np.inf,
454
+ 2.5, 8.17765433957942545e-01, "aps.04.02"],
455
+ [aps04_f, aps04_fp, aps04_fpp, (10, 0.2), [0, 5], np.inf,
456
+ 2.5, 8.51339922520784609e-01, "aps.04.03"],
457
+ [aps04_f, aps04_fp, aps04_fpp, (12, 0.2), [0, 5], np.inf,
458
+ 2.5, 8.74485272221167897e-01, "aps.04.04"],
459
+ [aps04_f, aps04_fp, aps04_fpp, (4, 1), [0, 5], np.inf,
460
+ 2.5, 1, "aps.04.05"],
461
+ [aps04_f, aps04_fp, aps04_fpp, (6, 1), [0, 5], np.inf,
462
+ 2.5, 1, "aps.04.06"],
463
+ [aps04_f, aps04_fp, aps04_fpp, (8, 1), [0, 5], np.inf,
464
+ 2.5, 1, "aps.04.07"],
465
+ [aps04_f, aps04_fp, aps04_fpp, (10, 1), [0, 5], np.inf,
466
+ 2.5, 1, "aps.04.08"],
467
+ [aps04_f, aps04_fp, aps04_fpp, (12, 1), [0, 5], np.inf,
468
+ 2.5, 1, "aps.04.09"],
469
+ [aps04_f, aps04_fp, aps04_fpp, (8, 1), [-0.95, 4.05], np.inf,
470
+ 1.5, 1, "aps.04.10"],
471
+ [aps04_f, aps04_fp, aps04_fpp, (10, 1), [-0.95, 4.05], np.inf,
472
+ 1.5, 1, "aps.04.11"],
473
+ [aps04_f, aps04_fp, aps04_fpp, (12, 1), [-0.95, 4.05], np.inf,
474
+ 1.5, 1, "aps.04.12"],
475
+ [aps04_f, aps04_fp, aps04_fpp, (14, 1), [-0.95, 4.05], np.inf,
476
+ 1.5, 1, "aps.04.13"],
477
+ [aps05_f, aps05_fp, aps05_fpp, (), [0, 1.5], np.inf,
478
+ 1.3, np.pi / 6, "aps.05.00"],
479
+ [aps06_f, aps06_fp, aps06_fpp, (1,), [0, 1], np.inf,
480
+ 0.5, 4.22477709641236709e-01, "aps.06.00"],
481
+ [aps06_f, aps06_fp, aps06_fpp, (2,), [0, 1], np.inf,
482
+ 0.5, 3.06699410483203705e-01, "aps.06.01"],
483
+ [aps06_f, aps06_fp, aps06_fpp, (3,), [0, 1], np.inf,
484
+ 0.5, 2.23705457654662959e-01, "aps.06.02"],
485
+ [aps06_f, aps06_fp, aps06_fpp, (4,), [0, 1], np.inf,
486
+ 0.5, 1.71719147519508369e-01, "aps.06.03"],
487
+ [aps06_f, aps06_fp, aps06_fpp, (5,), [0, 1], np.inf,
488
+ 0.4, 1.38257155056824066e-01, "aps.06.04"],
489
+ [aps06_f, aps06_fp, aps06_fpp, (20,), [0, 1], np.inf,
490
+ 0.1, 3.46573590208538521e-02, "aps.06.05"],
491
+ [aps06_f, aps06_fp, aps06_fpp, (40,), [0, 1], np.inf,
492
+ 5e-02, 1.73286795139986315e-02, "aps.06.06"],
493
+ [aps06_f, aps06_fp, aps06_fpp, (60,), [0, 1], np.inf,
494
+ 1.0 / 30, 1.15524530093324210e-02, "aps.06.07"],
495
+ [aps06_f, aps06_fp, aps06_fpp, (80,), [0, 1], np.inf,
496
+ 2.5e-02, 8.66433975699931573e-03, "aps.06.08"],
497
+ [aps06_f, aps06_fp, aps06_fpp, (100,), [0, 1], np.inf,
498
+ 2e-02, 6.93147180559945415e-03, "aps.06.09"],
499
+ [aps07_f, aps07_fp, aps07_fpp, (5,), [0, 1], np.inf,
500
+ 0.4, 3.84025518406218985e-02, "aps.07.00"],
501
+ [aps07_f, aps07_fp, aps07_fpp, (10,), [0, 1], np.inf,
502
+ 0.4, 9.90000999800049949e-03, "aps.07.01"],
503
+ [aps07_f, aps07_fp, aps07_fpp, (20,), [0, 1], np.inf,
504
+ 0.4, 2.49375003906201174e-03, "aps.07.02"],
505
+ [aps08_f, aps08_fp, aps08_fpp, (2,), [0, 1], np.inf,
506
+ 0.9, 0.5, "aps.08.00"],
507
+ [aps08_f, aps08_fp, aps08_fpp, (5,), [0, 1], np.inf,
508
+ 0.9, 3.45954815848242059e-01, "aps.08.01"],
509
+ [aps08_f, aps08_fp, aps08_fpp, (10,), [0, 1], np.inf,
510
+ 0.9, 2.45122333753307220e-01, "aps.08.02"],
511
+ [aps08_f, aps08_fp, aps08_fpp, (15,), [0, 1], np.inf,
512
+ 0.9, 1.95547623536565629e-01, "aps.08.03"],
513
+ [aps08_f, aps08_fp, aps08_fpp, (20,), [0, 1], np.inf,
514
+ 0.9, 1.64920957276440960e-01, "aps.08.04"],
515
+ [aps09_f, aps09_fp, aps09_fpp, (1,), [0, 1], np.inf,
516
+ 0.5, 2.75508040999484394e-01, "aps.09.00"],
517
+ [aps09_f, aps09_fp, aps09_fpp, (2,), [0, 1], np.inf,
518
+ 0.5, 1.37754020499742197e-01, "aps.09.01"],
519
+ [aps09_f, aps09_fp, aps09_fpp, (4,), [0, 1], np.inf,
520
+ 0.5, 1.03052837781564422e-02, "aps.09.02"],
521
+ [aps09_f, aps09_fp, aps09_fpp, (5,), [0, 1], np.inf,
522
+ 0.5, 3.61710817890406339e-03, "aps.09.03"],
523
+ [aps09_f, aps09_fp, aps09_fpp, (8,), [0, 1], np.inf,
524
+ 0.5, 4.10872918496395375e-04, "aps.09.04"],
525
+ [aps09_f, aps09_fp, aps09_fpp, (15,), [0, 1], np.inf,
526
+ 0.5, 2.59895758929076292e-05, "aps.09.05"],
527
+ [aps09_f, aps09_fp, aps09_fpp, (20,), [0, 1], np.inf,
528
+ 0.5, 7.66859512218533719e-06, "aps.09.06"],
529
+ [aps10_f, aps10_fp, aps10_fpp, (1,), [0, 1], np.inf,
530
+ 0.9, 4.01058137541547011e-01, "aps.10.00"],
531
+ [aps10_f, aps10_fp, aps10_fpp, (5,), [0, 1], np.inf,
532
+ 0.9, 5.16153518757933583e-01, "aps.10.01"],
533
+ [aps10_f, aps10_fp, aps10_fpp, (10,), [0, 1], np.inf,
534
+ 0.9, 5.39522226908415781e-01, "aps.10.02"],
535
+ [aps10_f, aps10_fp, aps10_fpp, (15,), [0, 1], np.inf,
536
+ 0.9, 5.48182294340655241e-01, "aps.10.03"],
537
+ [aps10_f, aps10_fp, aps10_fpp, (20,), [0, 1], np.inf,
538
+ 0.9, 5.52704666678487833e-01, "aps.10.04"],
539
+ [aps11_f, aps11_fp, aps11_fpp, (2,), [0.01, 1], np.inf,
540
+ 1e-02, 1.0 / 2, "aps.11.00"],
541
+ [aps11_f, aps11_fp, aps11_fpp, (5,), [0.01, 1], np.inf,
542
+ 1e-02, 1.0 / 5, "aps.11.01"],
543
+ [aps11_f, aps11_fp, aps11_fpp, (15,), [0.01, 1], np.inf,
544
+ 1e-02, 1.0 / 15, "aps.11.02"],
545
+ [aps11_f, aps11_fp, aps11_fpp, (20,), [0.01, 1], np.inf,
546
+ 1e-02, 1.0 / 20, "aps.11.03"],
547
+ [aps12_f, aps12_fp, aps12_fpp, (2,), [1, 100], np.inf,
548
+ 1.1, 2, "aps.12.00"],
549
+ [aps12_f, aps12_fp, aps12_fpp, (3,), [1, 100], np.inf,
550
+ 1.1, 3, "aps.12.01"],
551
+ [aps12_f, aps12_fp, aps12_fpp, (4,), [1, 100], np.inf,
552
+ 1.1, 4, "aps.12.02"],
553
+ [aps12_f, aps12_fp, aps12_fpp, (5,), [1, 100], np.inf,
554
+ 1.1, 5, "aps.12.03"],
555
+ [aps12_f, aps12_fp, aps12_fpp, (6,), [1, 100], np.inf,
556
+ 1.1, 6, "aps.12.04"],
557
+ [aps12_f, aps12_fp, aps12_fpp, (7,), [1, 100], np.inf,
558
+ 1.1, 7, "aps.12.05"],
559
+ [aps12_f, aps12_fp, aps12_fpp, (9,), [1, 100], np.inf,
560
+ 1.1, 9, "aps.12.06"],
561
+ [aps12_f, aps12_fp, aps12_fpp, (11,), [1, 100], np.inf,
562
+ 1.1, 11, "aps.12.07"],
563
+ [aps12_f, aps12_fp, aps12_fpp, (13,), [1, 100], np.inf,
564
+ 1.1, 13, "aps.12.08"],
565
+ [aps12_f, aps12_fp, aps12_fpp, (15,), [1, 100], np.inf,
566
+ 1.1, 15, "aps.12.09"],
567
+ [aps12_f, aps12_fp, aps12_fpp, (17,), [1, 100], np.inf,
568
+ 1.1, 17, "aps.12.10"],
569
+ [aps12_f, aps12_fp, aps12_fpp, (19,), [1, 100], np.inf,
570
+ 1.1, 19, "aps.12.11"],
571
+ [aps12_f, aps12_fp, aps12_fpp, (21,), [1, 100], np.inf,
572
+ 1.1, 21, "aps.12.12"],
573
+ [aps12_f, aps12_fp, aps12_fpp, (23,), [1, 100], np.inf,
574
+ 1.1, 23, "aps.12.13"],
575
+ [aps12_f, aps12_fp, aps12_fpp, (25,), [1, 100], np.inf,
576
+ 1.1, 25, "aps.12.14"],
577
+ [aps12_f, aps12_fp, aps12_fpp, (27,), [1, 100], np.inf,
578
+ 1.1, 27, "aps.12.15"],
579
+ [aps12_f, aps12_fp, aps12_fpp, (29,), [1, 100], np.inf,
580
+ 1.1, 29, "aps.12.16"],
581
+ [aps12_f, aps12_fp, aps12_fpp, (31,), [1, 100], np.inf,
582
+ 1.1, 31, "aps.12.17"],
583
+ [aps12_f, aps12_fp, aps12_fpp, (33,), [1, 100], np.inf,
584
+ 1.1, 33, "aps.12.18"],
585
+ [aps13_f, aps13_fp, aps13_fpp, (), [-1, 4], np.inf,
586
+ 1.5, 0, "aps.13.00"],
587
+ [aps14_f, aps14_fp, aps14_fpp, (1,), [-1000, np.pi / 2], 0,
588
+ 1, 6.23806518961612433e-01, "aps.14.00"],
589
+ [aps14_f, aps14_fp, aps14_fpp, (2,), [-1000, np.pi / 2], 0,
590
+ 1, 6.23806518961612433e-01, "aps.14.01"],
591
+ [aps14_f, aps14_fp, aps14_fpp, (3,), [-1000, np.pi / 2], 0,
592
+ 1, 6.23806518961612433e-01, "aps.14.02"],
593
+ [aps14_f, aps14_fp, aps14_fpp, (4,), [-1000, np.pi / 2], 0,
594
+ 1, 6.23806518961612433e-01, "aps.14.03"],
595
+ [aps14_f, aps14_fp, aps14_fpp, (5,), [-1000, np.pi / 2], 0,
596
+ 1, 6.23806518961612433e-01, "aps.14.04"],
597
+ [aps14_f, aps14_fp, aps14_fpp, (6,), [-1000, np.pi / 2], 0,
598
+ 1, 6.23806518961612433e-01, "aps.14.05"],
599
+ [aps14_f, aps14_fp, aps14_fpp, (7,), [-1000, np.pi / 2], 0,
600
+ 1, 6.23806518961612433e-01, "aps.14.06"],
601
+ [aps14_f, aps14_fp, aps14_fpp, (8,), [-1000, np.pi / 2], 0,
602
+ 1, 6.23806518961612433e-01, "aps.14.07"],
603
+ [aps14_f, aps14_fp, aps14_fpp, (9,), [-1000, np.pi / 2], 0,
604
+ 1, 6.23806518961612433e-01, "aps.14.08"],
605
+ [aps14_f, aps14_fp, aps14_fpp, (10,), [-1000, np.pi / 2], 0,
606
+ 1, 6.23806518961612433e-01, "aps.14.09"],
607
+ [aps14_f, aps14_fp, aps14_fpp, (11,), [-1000, np.pi / 2], 0,
608
+ 1, 6.23806518961612433e-01, "aps.14.10"],
609
+ [aps14_f, aps14_fp, aps14_fpp, (12,), [-1000, np.pi / 2], 0,
610
+ 1, 6.23806518961612433e-01, "aps.14.11"],
611
+ [aps14_f, aps14_fp, aps14_fpp, (13,), [-1000, np.pi / 2], 0,
612
+ 1, 6.23806518961612433e-01, "aps.14.12"],
613
+ [aps14_f, aps14_fp, aps14_fpp, (14,), [-1000, np.pi / 2], 0,
614
+ 1, 6.23806518961612433e-01, "aps.14.13"],
615
+ [aps14_f, aps14_fp, aps14_fpp, (15,), [-1000, np.pi / 2], 0,
616
+ 1, 6.23806518961612433e-01, "aps.14.14"],
617
+ [aps14_f, aps14_fp, aps14_fpp, (16,), [-1000, np.pi / 2], 0,
618
+ 1, 6.23806518961612433e-01, "aps.14.15"],
619
+ [aps14_f, aps14_fp, aps14_fpp, (17,), [-1000, np.pi / 2], 0,
620
+ 1, 6.23806518961612433e-01, "aps.14.16"],
621
+ [aps14_f, aps14_fp, aps14_fpp, (18,), [-1000, np.pi / 2], 0,
622
+ 1, 6.23806518961612433e-01, "aps.14.17"],
623
+ [aps14_f, aps14_fp, aps14_fpp, (19,), [-1000, np.pi / 2], 0,
624
+ 1, 6.23806518961612433e-01, "aps.14.18"],
625
+ [aps14_f, aps14_fp, aps14_fpp, (20,), [-1000, np.pi / 2], 0,
626
+ 1, 6.23806518961612433e-01, "aps.14.19"],
627
+ [aps14_f, aps14_fp, aps14_fpp, (21,), [-1000, np.pi / 2], 0,
628
+ 1, 6.23806518961612433e-01, "aps.14.20"],
629
+ [aps14_f, aps14_fp, aps14_fpp, (22,), [-1000, np.pi / 2], 0,
630
+ 1, 6.23806518961612433e-01, "aps.14.21"],
631
+ [aps14_f, aps14_fp, aps14_fpp, (23,), [-1000, np.pi / 2], 0,
632
+ 1, 6.23806518961612433e-01, "aps.14.22"],
633
+ [aps14_f, aps14_fp, aps14_fpp, (24,), [-1000, np.pi / 2], 0,
634
+ 1, 6.23806518961612433e-01, "aps.14.23"],
635
+ [aps14_f, aps14_fp, aps14_fpp, (25,), [-1000, np.pi / 2], 0,
636
+ 1, 6.23806518961612433e-01, "aps.14.24"],
637
+ [aps14_f, aps14_fp, aps14_fpp, (26,), [-1000, np.pi / 2], 0,
638
+ 1, 6.23806518961612433e-01, "aps.14.25"],
639
+ [aps14_f, aps14_fp, aps14_fpp, (27,), [-1000, np.pi / 2], 0,
640
+ 1, 6.23806518961612433e-01, "aps.14.26"],
641
+ [aps14_f, aps14_fp, aps14_fpp, (28,), [-1000, np.pi / 2], 0,
642
+ 1, 6.23806518961612433e-01, "aps.14.27"],
643
+ [aps14_f, aps14_fp, aps14_fpp, (29,), [-1000, np.pi / 2], 0,
644
+ 1, 6.23806518961612433e-01, "aps.14.28"],
645
+ [aps14_f, aps14_fp, aps14_fpp, (30,), [-1000, np.pi / 2], 0,
646
+ 1, 6.23806518961612433e-01, "aps.14.29"],
647
+ [aps14_f, aps14_fp, aps14_fpp, (31,), [-1000, np.pi / 2], 0,
648
+ 1, 6.23806518961612433e-01, "aps.14.30"],
649
+ [aps14_f, aps14_fp, aps14_fpp, (32,), [-1000, np.pi / 2], 0,
650
+ 1, 6.23806518961612433e-01, "aps.14.31"],
651
+ [aps14_f, aps14_fp, aps14_fpp, (33,), [-1000, np.pi / 2], 0,
652
+ 1, 6.23806518961612433e-01, "aps.14.32"],
653
+ [aps14_f, aps14_fp, aps14_fpp, (34,), [-1000, np.pi / 2], 0,
654
+ 1, 6.23806518961612433e-01, "aps.14.33"],
655
+ [aps14_f, aps14_fp, aps14_fpp, (35,), [-1000, np.pi / 2], 0,
656
+ 1, 6.23806518961612433e-01, "aps.14.34"],
657
+ [aps14_f, aps14_fp, aps14_fpp, (36,), [-1000, np.pi / 2], 0,
658
+ 1, 6.23806518961612433e-01, "aps.14.35"],
659
+ [aps14_f, aps14_fp, aps14_fpp, (37,), [-1000, np.pi / 2], 0,
660
+ 1, 6.23806518961612433e-01, "aps.14.36"],
661
+ [aps14_f, aps14_fp, aps14_fpp, (38,), [-1000, np.pi / 2], 0,
662
+ 1, 6.23806518961612433e-01, "aps.14.37"],
663
+ [aps14_f, aps14_fp, aps14_fpp, (39,), [-1000, np.pi / 2], 0,
664
+ 1, 6.23806518961612433e-01, "aps.14.38"],
665
+ [aps14_f, aps14_fp, aps14_fpp, (40,), [-1000, np.pi / 2], 0,
666
+ 1, 6.23806518961612433e-01, "aps.14.39"],
667
+ [aps15_f, aps15_fp, aps15_fpp, (20,), [-1000, 1e-4], 0,
668
+ -2, 5.90513055942197166e-05, "aps.15.00"],
669
+ [aps15_f, aps15_fp, aps15_fpp, (21,), [-1000, 1e-4], 0,
670
+ -2, 5.63671553399369967e-05, "aps.15.01"],
671
+ [aps15_f, aps15_fp, aps15_fpp, (22,), [-1000, 1e-4], 0,
672
+ -2, 5.39164094555919196e-05, "aps.15.02"],
673
+ [aps15_f, aps15_fp, aps15_fpp, (23,), [-1000, 1e-4], 0,
674
+ -2, 5.16698923949422470e-05, "aps.15.03"],
675
+ [aps15_f, aps15_fp, aps15_fpp, (24,), [-1000, 1e-4], 0,
676
+ -2, 4.96030966991445609e-05, "aps.15.04"],
677
+ [aps15_f, aps15_fp, aps15_fpp, (25,), [-1000, 1e-4], 0,
678
+ -2, 4.76952852876389951e-05, "aps.15.05"],
679
+ [aps15_f, aps15_fp, aps15_fpp, (26,), [-1000, 1e-4], 0,
680
+ -2, 4.59287932399486662e-05, "aps.15.06"],
681
+ [aps15_f, aps15_fp, aps15_fpp, (27,), [-1000, 1e-4], 0,
682
+ -2, 4.42884791956647841e-05, "aps.15.07"],
683
+ [aps15_f, aps15_fp, aps15_fpp, (28,), [-1000, 1e-4], 0,
684
+ -2, 4.27612902578832391e-05, "aps.15.08"],
685
+ [aps15_f, aps15_fp, aps15_fpp, (29,), [-1000, 1e-4], 0,
686
+ -2, 4.13359139159538030e-05, "aps.15.09"],
687
+ [aps15_f, aps15_fp, aps15_fpp, (30,), [-1000, 1e-4], 0,
688
+ -2, 4.00024973380198076e-05, "aps.15.10"],
689
+ [aps15_f, aps15_fp, aps15_fpp, (31,), [-1000, 1e-4], 0,
690
+ -2, 3.87524192962066869e-05, "aps.15.11"],
691
+ [aps15_f, aps15_fp, aps15_fpp, (32,), [-1000, 1e-4], 0,
692
+ -2, 3.75781035599579910e-05, "aps.15.12"],
693
+ [aps15_f, aps15_fp, aps15_fpp, (33,), [-1000, 1e-4], 0,
694
+ -2, 3.64728652199592355e-05, "aps.15.13"],
695
+ [aps15_f, aps15_fp, aps15_fpp, (34,), [-1000, 1e-4], 0,
696
+ -2, 3.54307833565318273e-05, "aps.15.14"],
697
+ [aps15_f, aps15_fp, aps15_fpp, (35,), [-1000, 1e-4], 0,
698
+ -2, 3.44465949299614980e-05, "aps.15.15"],
699
+ [aps15_f, aps15_fp, aps15_fpp, (36,), [-1000, 1e-4], 0,
700
+ -2, 3.35156058778003705e-05, "aps.15.16"],
701
+ [aps15_f, aps15_fp, aps15_fpp, (37,), [-1000, 1e-4], 0,
702
+ -2, 3.26336162494372125e-05, "aps.15.17"],
703
+ [aps15_f, aps15_fp, aps15_fpp, (38,), [-1000, 1e-4], 0,
704
+ -2, 3.17968568584260013e-05, "aps.15.18"],
705
+ [aps15_f, aps15_fp, aps15_fpp, (39,), [-1000, 1e-4], 0,
706
+ -2, 3.10019354369653455e-05, "aps.15.19"],
707
+ [aps15_f, aps15_fp, aps15_fpp, (40,), [-1000, 1e-4], 0,
708
+ -2, 3.02457906702100968e-05, "aps.15.20"],
709
+ [aps15_f, aps15_fp, aps15_fpp, (100,), [-1000, 1e-4], 0,
710
+ -2, 1.22779942324615231e-05, "aps.15.21"],
711
+ [aps15_f, aps15_fp, aps15_fpp, (200,), [-1000, 1e-4], 0,
712
+ -2, 6.16953939044086617e-06, "aps.15.22"],
713
+ [aps15_f, aps15_fp, aps15_fpp, (300,), [-1000, 1e-4], 0,
714
+ -2, 4.11985852982928163e-06, "aps.15.23"],
715
+ [aps15_f, aps15_fp, aps15_fpp, (400,), [-1000, 1e-4], 0,
716
+ -2, 3.09246238772721682e-06, "aps.15.24"],
717
+ [aps15_f, aps15_fp, aps15_fpp, (500,), [-1000, 1e-4], 0,
718
+ -2, 2.47520442610501789e-06, "aps.15.25"],
719
+ [aps15_f, aps15_fp, aps15_fpp, (600,), [-1000, 1e-4], 0,
720
+ -2, 2.06335676785127107e-06, "aps.15.26"],
721
+ [aps15_f, aps15_fp, aps15_fpp, (700,), [-1000, 1e-4], 0,
722
+ -2, 1.76901200781542651e-06, "aps.15.27"],
723
+ [aps15_f, aps15_fp, aps15_fpp, (800,), [-1000, 1e-4], 0,
724
+ -2, 1.54816156988591016e-06, "aps.15.28"],
725
+ [aps15_f, aps15_fp, aps15_fpp, (900,), [-1000, 1e-4], 0,
726
+ -2, 1.37633453660223511e-06, "aps.15.29"],
727
+ [aps15_f, aps15_fp, aps15_fpp, (1000,), [-1000, 1e-4], 0,
728
+ -2, 1.23883857889971403e-06, "aps.15.30"]
729
+ ]
730
+
731
+ _APS_TESTS_DICTS = [dict(zip(_APS_TESTS_KEYS, testcase)) for testcase in _APS_TESTS]
732
+
733
+
734
+ # ##################
735
+ # "complex" test cases
736
+ # A few simple, complex-valued, functions, defined on the complex plane.
737
+
738
+
739
+ def cplx01_f(z, n, a):
740
+ r"""z**n-a: Use to find the nth root of a"""
741
+ return z**n - a
742
+
743
+
744
+ def cplx01_fp(z, n, a):
745
+ return n * z**(n - 1)
746
+
747
+
748
+ def cplx01_fpp(z, n, a):
749
+ return n * (n - 1) * z**(n - 2)
750
+
751
+
752
+ def cplx02_f(z, a):
753
+ r"""e**z - a: Use to find the log of a"""
754
+ return np.exp(z) - a
755
+
756
+
757
+ def cplx02_fp(z, a):
758
+ return np.exp(z)
759
+
760
+
761
+ def cplx02_fpp(z, a):
762
+ return np.exp(z)
763
+
764
+
765
+ # Each "complex" test case has
766
+ # - a function and its two derivatives,
767
+ # - additional arguments,
768
+ # - the order of differentiability of the function on this interval
769
+ # - two starting values x0 and x1
770
+ # - the root
771
+ # - an Identifier of the test case
772
+ #
773
+ # Algorithm 748 is a bracketing algorithm so a bracketing interval was provided
774
+ # in [1] for each test case. Newton and Halley need a single starting point
775
+ # x0, which was chosen to be near the middle of the interval, unless that
776
+ # would make the problem too easy.
777
+
778
+
779
+ _COMPLEX_TESTS_KEYS = [
780
+ "f", "fprime", "fprime2", "args", "smoothness", "x0", "x1", "root", "ID"
781
+ ]
782
+ _COMPLEX_TESTS = [
783
+ [cplx01_f, cplx01_fp, cplx01_fpp, (2, -1), np.inf,
784
+ (1 + 1j), (0.5 + 0.5j), 1j, "complex.01.00"],
785
+ [cplx01_f, cplx01_fp, cplx01_fpp, (3, 1), np.inf,
786
+ (-1 + 1j), (-0.5 + 2.0j), (-0.5 + np.sqrt(3) / 2 * 1.0j),
787
+ "complex.01.01"],
788
+ [cplx01_f, cplx01_fp, cplx01_fpp, (3, -1), np.inf,
789
+ 1j, (0.5 + 0.5j), (0.5 + np.sqrt(3) / 2 * 1.0j),
790
+ "complex.01.02"],
791
+ [cplx01_f, cplx01_fp, cplx01_fpp, (3, 8), np.inf,
792
+ 5, 4, 2, "complex.01.03"],
793
+ [cplx02_f, cplx02_fp, cplx02_fpp, (-1,), np.inf,
794
+ (1 + 2j), (0.5 + 0.5j), np.pi * 1.0j, "complex.02.00"],
795
+ [cplx02_f, cplx02_fp, cplx02_fpp, (1j,), np.inf,
796
+ (1 + 2j), (0.5 + 0.5j), np.pi * 0.5j, "complex.02.01"],
797
+ ]
798
+
799
+ _COMPLEX_TESTS_DICTS = [
800
+ dict(zip(_COMPLEX_TESTS_KEYS, testcase)) for testcase in _COMPLEX_TESTS
801
+ ]
802
+
803
+
804
+ def _add_a_b(tests):
805
+ r"""Add "a" and "b" keys to each test from the "bracket" value"""
806
+ for d in tests:
807
+ for k, v in zip(['a', 'b'], d.get('bracket', [])):
808
+ d[k] = v
809
+
810
+
811
+ _add_a_b(_ORIGINAL_TESTS_DICTS)
812
+ _add_a_b(_APS_TESTS_DICTS)
813
+ _add_a_b(_COMPLEX_TESTS_DICTS)
814
+
815
+
816
+ def get_tests(collection='original', smoothness=None):
817
+ r"""Return the requested collection of test cases, as an array of dicts with subset-specific keys
818
+
819
+ Allowed values of collection:
820
+ 'original': The original benchmarking functions.
821
+ Real-valued functions of real-valued inputs on an interval with a zero.
822
+ f1, .., f3 are continuous and infinitely differentiable
823
+ f4 has a single discontinuity at the root
824
+ f5 has a root at 1 replacing a 1st order pole
825
+ f6 is randomly positive on one side of the root, randomly negative on the other
826
+ 'aps': The test problems in the TOMS "Algorithm 748: Enclosing Zeros of Continuous Functions"
827
+ paper by Alefeld, Potra and Shi. Real-valued functions of
828
+ real-valued inputs on an interval with a zero.
829
+ Suitable for methods which start with an enclosing interval, and
830
+ derivatives up to 2nd order.
831
+ 'complex': Some complex-valued functions of complex-valued inputs.
832
+ No enclosing bracket is provided.
833
+ Suitable for methods which use one or more starting values, and
834
+ derivatives up to 2nd order.
835
+
836
+ The dictionary keys will be a subset of
837
+ ["f", "fprime", "fprime2", "args", "bracket", "a", b", "smoothness", "x0", "x1", "root", "ID"]
838
+ """ # noqa: E501
839
+ collection = collection or "original"
840
+ subsets = {"aps": _APS_TESTS_DICTS,
841
+ "complex": _COMPLEX_TESTS_DICTS,
842
+ "original": _ORIGINAL_TESTS_DICTS,
843
+ "chandrupatla": _CHANDRUPATLA_TESTS_DICTS}
844
+ tests = subsets.get(collection, [])
845
+ if smoothness is not None:
846
+ tests = [tc for tc in tests if tc['smoothness'] >= smoothness]
847
+ return tests
848
+
849
+
850
+ # Backwards compatibility
851
+ methods = [cc.bisect, cc.ridder, cc.brenth, cc.brentq]
852
+ mstrings = ['cc.bisect', 'cc.ridder', 'cc.brenth', 'cc.brentq']
853
+ functions = [f2, f3, f4, f5, f6]
854
+ fstrings = ['f2', 'f3', 'f4', 'f5', 'f6']
855
+
856
+ # ##################
857
+ # "Chandrupatla" test cases
858
+ # Functions and test cases that appear in [2]
859
+
860
+ def fun1(x):
861
+ return x**3 - 2*x - 5
862
+ fun1.root = 2.0945514815423265 # additional precision using mpmath.findroot
863
+
864
+
865
+ def fun2(x):
866
+ return 1 - 1/x**2
867
+ fun2.root = 1
868
+
869
+
870
+ def fun3(x):
871
+ return (x-3)**3
872
+ fun3.root = 3
873
+
874
+
875
+ def fun4(x):
876
+ return 6*(x-2)**5
877
+ fun4.root = 2
878
+
879
+
880
+ def fun5(x):
881
+ return x**9
882
+ fun5.root = 0
883
+
884
+
885
+ def fun6(x):
886
+ return x**19
887
+ fun6.root = 0
888
+
889
+
890
+ def fun7(x):
891
+ xp = array_namespace(x)
892
+ return 0 if xp.abs(x) < 3.8e-4 else x*xp.exp(-x**(-2))
893
+ fun7.root = 0
894
+
895
+
896
+ def fun8(x):
897
+ xp = array_namespace(x)
898
+ xi = 0.61489
899
+ return -(3062*(1-xi)*xp.exp(-x))/(xi + (1-xi)*xp.exp(-x)) - 1013 + 1628/x
900
+ fun8.root = 1.0375360332870405
901
+
902
+
903
+ def fun9(x):
904
+ xp = array_namespace(x)
905
+ return xp.exp(x) - 2 - 0.01/x**2 + .000002/x**3
906
+ fun9.root = 0.7032048403631358
907
+
908
+ # Each "chandropatla" test case has
909
+ # - a function,
910
+ # - two starting values x0 and x1
911
+ # - the root
912
+ # - the number of function evaluations required by Chandrupatla's algorithm
913
+ # - an Identifier of the test case
914
+ #
915
+ # Chandrupatla's is a bracketing algorithm, so a bracketing interval was
916
+ # provided in [2] for each test case. No special support for testing with
917
+ # secant/Newton/Halley is provided.
918
+
919
+ _CHANDRUPATLA_TESTS_KEYS = ["f", "bracket", "root", "nfeval", "ID"]
920
+ _CHANDRUPATLA_TESTS = [
921
+ [fun1, [2, 3], fun1.root, 7],
922
+ [fun1, [1, 10], fun1.root, 11],
923
+ [fun1, [1, 100], fun1.root, 14],
924
+ [fun1, [-1e4, 1e4], fun1.root, 23],
925
+ [fun1, [-1e10, 1e10], fun1.root, 43],
926
+ [fun2, [0.5, 1.51], fun2.root, 8],
927
+ [fun2, [1e-4, 1e4], fun2.root, 22],
928
+ [fun2, [1e-6, 1e6], fun2.root, 28],
929
+ [fun2, [1e-10, 1e10], fun2.root, 41],
930
+ [fun2, [1e-12, 1e12], fun2.root, 48],
931
+ [fun3, [0, 5], fun3.root, 21],
932
+ [fun3, [-10, 10], fun3.root, 23],
933
+ [fun3, [-1e4, 1e4], fun3.root, 36],
934
+ [fun3, [-1e6, 1e6], fun3.root, 45],
935
+ [fun3, [-1e10, 1e10], fun3.root, 55],
936
+ [fun4, [0, 5], fun4.root, 21],
937
+ [fun4, [-10, 10], fun4.root, 23],
938
+ [fun4, [-1e4, 1e4], fun4.root, 33],
939
+ [fun4, [-1e6, 1e6], fun4.root, 43],
940
+ [fun4, [-1e10, 1e10], fun4.root, 54],
941
+ [fun5, [-1, 4], fun5.root, 21],
942
+ [fun5, [-2, 5], fun5.root, 22],
943
+ [fun5, [-1, 10], fun5.root, 23],
944
+ [fun5, [-5, 50], fun5.root, 25],
945
+ [fun5, [-10, 100], fun5.root, 26],
946
+ [fun6, [-1., 4.], fun6.root, 21],
947
+ [fun6, [-2., 5.], fun6.root, 22],
948
+ [fun6, [-1., 10.], fun6.root, 23],
949
+ [fun6, [-5., 50.], fun6.root, 25],
950
+ [fun6, [-10., 100.], fun6.root, 26],
951
+ [fun7, [-1, 4], fun7.root, 8],
952
+ [fun7, [-2, 5], fun7.root, 8],
953
+ [fun7, [-1, 10], fun7.root, 11],
954
+ [fun7, [-5, 50], fun7.root, 18],
955
+ [fun7, [-10, 100], fun7.root, 19],
956
+ [fun8, [2e-4, 2], fun8.root, 9],
957
+ [fun8, [2e-4, 3], fun8.root, 10],
958
+ [fun8, [2e-4, 9], fun8.root, 11],
959
+ [fun8, [2e-4, 27], fun8.root, 12],
960
+ [fun8, [2e-4, 81], fun8.root, 14],
961
+ [fun9, [2e-4, 1], fun9.root, 7],
962
+ [fun9, [2e-4, 3], fun9.root, 8],
963
+ [fun9, [2e-4, 9], fun9.root, 10],
964
+ [fun9, [2e-4, 27], fun9.root, 11],
965
+ [fun9, [2e-4, 81], fun9.root, 13],
966
+ ]
967
+ _CHANDRUPATLA_TESTS = [test + [f'{test[0].__name__}.{i%5+1}']
968
+ for i, test in enumerate(_CHANDRUPATLA_TESTS)]
969
+
970
+ _CHANDRUPATLA_TESTS_DICTS = [dict(zip(_CHANDRUPATLA_TESTS_KEYS, testcase))
971
+ for testcase in _CHANDRUPATLA_TESTS]
972
+ _add_a_b(_CHANDRUPATLA_TESTS_DICTS)
loan-advisor-agent/.venv/Lib/site-packages/scipy/optimize/_zeros.cp312-win_amd64.dll.a ADDED
Binary file (1.53 kB). View file
 
loan-advisor-agent/.venv/Lib/site-packages/scipy/optimize/_zeros.cp312-win_amd64.pyd ADDED
Binary file (23 kB). View file
 
loan-advisor-agent/.venv/Lib/site-packages/scipy/optimize/_zeros_py.py ADDED
@@ -0,0 +1,1475 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import warnings
2
+ from collections import namedtuple
3
+ import operator
4
+ from . import _zeros
5
+ from ._optimize import OptimizeResult
6
+ import numpy as np
7
+
8
+
9
+ _iter = 100
10
+ _xtol = 2e-12
11
+ _rtol = 4 * np.finfo(float).eps
12
+
13
+ __all__ = ['newton', 'bisect', 'ridder', 'brentq', 'brenth', 'toms748',
14
+ 'RootResults']
15
+
16
+ # Must agree with CONVERGED, SIGNERR, CONVERR, ... in zeros.h
17
+ _ECONVERGED = 0
18
+ _ESIGNERR = -1 # used in _chandrupatla
19
+ _ECONVERR = -2
20
+ _EVALUEERR = -3
21
+ _ECALLBACK = -4
22
+ _EINPROGRESS = 1
23
+
24
+ CONVERGED = 'converged'
25
+ SIGNERR = 'sign error'
26
+ CONVERR = 'convergence error'
27
+ VALUEERR = 'value error'
28
+ INPROGRESS = 'No error'
29
+
30
+
31
+ flag_map = {_ECONVERGED: CONVERGED, _ESIGNERR: SIGNERR, _ECONVERR: CONVERR,
32
+ _EVALUEERR: VALUEERR, _EINPROGRESS: INPROGRESS}
33
+
34
+
35
+ class RootResults(OptimizeResult):
36
+ """Represents the root finding result.
37
+
38
+ Attributes
39
+ ----------
40
+ root : float
41
+ Estimated root location.
42
+ iterations : int
43
+ Number of iterations needed to find the root.
44
+ function_calls : int
45
+ Number of times the function was called.
46
+ converged : bool
47
+ True if the routine converged.
48
+ flag : str
49
+ Description of the cause of termination.
50
+ method : str
51
+ Root finding method used.
52
+
53
+ """
54
+
55
+ def __init__(self, root, iterations, function_calls, flag, method):
56
+ self.root = root
57
+ self.iterations = iterations
58
+ self.function_calls = function_calls
59
+ self.converged = flag == _ECONVERGED
60
+ if flag in flag_map:
61
+ self.flag = flag_map[flag]
62
+ else:
63
+ self.flag = flag
64
+ self.method = method
65
+
66
+
67
+ def results_c(full_output, r, method):
68
+ if full_output:
69
+ x, funcalls, iterations, flag = r
70
+ results = RootResults(root=x,
71
+ iterations=iterations,
72
+ function_calls=funcalls,
73
+ flag=flag, method=method)
74
+ return x, results
75
+ else:
76
+ return r
77
+
78
+
79
+ def _results_select(full_output, r, method):
80
+ """Select from a tuple of (root, funccalls, iterations, flag)"""
81
+ x, funcalls, iterations, flag = r
82
+ if full_output:
83
+ results = RootResults(root=x,
84
+ iterations=iterations,
85
+ function_calls=funcalls,
86
+ flag=flag, method=method)
87
+ return x, results
88
+ return x
89
+
90
+
91
+ def _wrap_nan_raise(f):
92
+
93
+ def f_raise(x, *args):
94
+ fx = f(x, *args)
95
+ f_raise._function_calls += 1
96
+ if np.isnan(fx):
97
+ msg = (f'The function value at x={x} is NaN; '
98
+ 'solver cannot continue.')
99
+ err = ValueError(msg)
100
+ err._x = x
101
+ err._function_calls = f_raise._function_calls
102
+ raise err
103
+ return fx
104
+
105
+ f_raise._function_calls = 0
106
+ return f_raise
107
+
108
+
109
+ def newton(func, x0, fprime=None, args=(), tol=1.48e-8, maxiter=50,
110
+ fprime2=None, x1=None, rtol=0.0,
111
+ full_output=False, disp=True):
112
+ """
113
+ Find a root of a real or complex function using the Newton-Raphson
114
+ (or secant or Halley's) method.
115
+
116
+ Find a root of the scalar-valued function `func` given a nearby scalar
117
+ starting point `x0`.
118
+ The Newton-Raphson method is used if the derivative `fprime` of `func`
119
+ is provided, otherwise the secant method is used. If the second order
120
+ derivative `fprime2` of `func` is also provided, then Halley's method is
121
+ used.
122
+
123
+ If `x0` is a sequence with more than one item, `newton` returns an array:
124
+ the roots of the function from each (scalar) starting point in `x0`.
125
+ In this case, `func` must be vectorized to return a sequence or array of
126
+ the same shape as its first argument. If `fprime` (`fprime2`) is given,
127
+ then its return must also have the same shape: each element is the first
128
+ (second) derivative of `func` with respect to its only variable evaluated
129
+ at each element of its first argument.
130
+
131
+ `newton` is for finding roots of a scalar-valued functions of a single
132
+ variable. For problems involving several variables, see `root`.
133
+
134
+ Parameters
135
+ ----------
136
+ func : callable
137
+ The function whose root is wanted. It must be a function of a
138
+ single variable of the form ``f(x,a,b,c,...)``, where ``a,b,c,...``
139
+ are extra arguments that can be passed in the `args` parameter.
140
+ x0 : float, sequence, or ndarray
141
+ An initial estimate of the root that should be somewhere near the
142
+ actual root. If not scalar, then `func` must be vectorized and return
143
+ a sequence or array of the same shape as its first argument.
144
+ fprime : callable, optional
145
+ The derivative of the function when available and convenient. If it
146
+ is None (default), then the secant method is used.
147
+ args : tuple, optional
148
+ Extra arguments to be used in the function call.
149
+ tol : float, optional
150
+ The allowable error of the root's value. If `func` is complex-valued,
151
+ a larger `tol` is recommended as both the real and imaginary parts
152
+ of `x` contribute to ``|x - x0|``.
153
+ maxiter : int, optional
154
+ Maximum number of iterations.
155
+ fprime2 : callable, optional
156
+ The second order derivative of the function when available and
157
+ convenient. If it is None (default), then the normal Newton-Raphson
158
+ or the secant method is used. If it is not None, then Halley's method
159
+ is used.
160
+ x1 : float, optional
161
+ Another estimate of the root that should be somewhere near the
162
+ actual root. Used if `fprime` is not provided.
163
+ rtol : float, optional
164
+ Tolerance (relative) for termination.
165
+ full_output : bool, optional
166
+ If `full_output` is False (default), the root is returned.
167
+ If True and `x0` is scalar, the return value is ``(x, r)``, where ``x``
168
+ is the root and ``r`` is a `RootResults` object.
169
+ If True and `x0` is non-scalar, the return value is ``(x, converged,
170
+ zero_der)`` (see Returns section for details).
171
+ disp : bool, optional
172
+ If True, raise a RuntimeError if the algorithm didn't converge, with
173
+ the error message containing the number of iterations and current
174
+ function value. Otherwise, the convergence status is recorded in a
175
+ `RootResults` return object.
176
+ Ignored if `x0` is not scalar.
177
+ *Note: this has little to do with displaying, however,
178
+ the `disp` keyword cannot be renamed for backwards compatibility.*
179
+
180
+ Returns
181
+ -------
182
+ root : float, sequence, or ndarray
183
+ Estimated location where function is zero.
184
+ r : `RootResults`, optional
185
+ Present if ``full_output=True`` and `x0` is scalar.
186
+ Object containing information about the convergence. In particular,
187
+ ``r.converged`` is True if the routine converged.
188
+ converged : ndarray of bool, optional
189
+ Present if ``full_output=True`` and `x0` is non-scalar.
190
+ For vector functions, indicates which elements converged successfully.
191
+ zero_der : ndarray of bool, optional
192
+ Present if ``full_output=True`` and `x0` is non-scalar.
193
+ For vector functions, indicates which elements had a zero derivative.
194
+
195
+ See Also
196
+ --------
197
+ root_scalar : interface to root solvers for scalar functions
198
+ root : interface to root solvers for multi-input, multi-output functions
199
+
200
+ Notes
201
+ -----
202
+ The convergence rate of the Newton-Raphson method is quadratic,
203
+ the Halley method is cubic, and the secant method is
204
+ sub-quadratic. This means that if the function is well-behaved
205
+ the actual error in the estimated root after the nth iteration
206
+ is approximately the square (cube for Halley) of the error
207
+ after the (n-1)th step. However, the stopping criterion used
208
+ here is the step size and there is no guarantee that a root
209
+ has been found. Consequently, the result should be verified.
210
+ Safer algorithms are brentq, brenth, ridder, and bisect,
211
+ but they all require that the root first be bracketed in an
212
+ interval where the function changes sign. The brentq algorithm
213
+ is recommended for general use in one dimensional problems
214
+ when such an interval has been found.
215
+
216
+ When `newton` is used with arrays, it is best suited for the following
217
+ types of problems:
218
+
219
+ * The initial guesses, `x0`, are all relatively the same distance from
220
+ the roots.
221
+ * Some or all of the extra arguments, `args`, are also arrays so that a
222
+ class of similar problems can be solved together.
223
+ * The size of the initial guesses, `x0`, is larger than O(100) elements.
224
+ Otherwise, a naive loop may perform as well or better than a vector.
225
+
226
+ Examples
227
+ --------
228
+ >>> import numpy as np
229
+ >>> import matplotlib.pyplot as plt
230
+ >>> from scipy import optimize
231
+
232
+ >>> def f(x):
233
+ ... return (x**3 - 1) # only one real root at x = 1
234
+
235
+ ``fprime`` is not provided, use the secant method:
236
+
237
+ >>> root = optimize.newton(f, 1.5)
238
+ >>> root
239
+ 1.0000000000000016
240
+ >>> root = optimize.newton(f, 1.5, fprime2=lambda x: 6 * x)
241
+ >>> root
242
+ 1.0000000000000016
243
+
244
+ Only ``fprime`` is provided, use the Newton-Raphson method:
245
+
246
+ >>> root = optimize.newton(f, 1.5, fprime=lambda x: 3 * x**2)
247
+ >>> root
248
+ 1.0
249
+
250
+ Both ``fprime2`` and ``fprime`` are provided, use Halley's method:
251
+
252
+ >>> root = optimize.newton(f, 1.5, fprime=lambda x: 3 * x**2,
253
+ ... fprime2=lambda x: 6 * x)
254
+ >>> root
255
+ 1.0
256
+
257
+ When we want to find roots for a set of related starting values and/or
258
+ function parameters, we can provide both of those as an array of inputs:
259
+
260
+ >>> f = lambda x, a: x**3 - a
261
+ >>> fder = lambda x, a: 3 * x**2
262
+ >>> rng = np.random.default_rng()
263
+ >>> x = rng.standard_normal(100)
264
+ >>> a = np.arange(-50, 50)
265
+ >>> vec_res = optimize.newton(f, x, fprime=fder, args=(a, ), maxiter=200)
266
+
267
+ The above is the equivalent of solving for each value in ``(x, a)``
268
+ separately in a for-loop, just faster:
269
+
270
+ >>> loop_res = [optimize.newton(f, x0, fprime=fder, args=(a0,),
271
+ ... maxiter=200)
272
+ ... for x0, a0 in zip(x, a)]
273
+ >>> np.allclose(vec_res, loop_res)
274
+ True
275
+
276
+ Plot the results found for all values of ``a``:
277
+
278
+ >>> analytical_result = np.sign(a) * np.abs(a)**(1/3)
279
+ >>> fig, ax = plt.subplots()
280
+ >>> ax.plot(a, analytical_result, 'o')
281
+ >>> ax.plot(a, vec_res, '.')
282
+ >>> ax.set_xlabel('$a$')
283
+ >>> ax.set_ylabel('$x$ where $f(x, a)=0$')
284
+ >>> plt.show()
285
+
286
+ """
287
+ if tol <= 0:
288
+ raise ValueError(f"tol too small ({tol:g} <= 0)")
289
+ maxiter = operator.index(maxiter)
290
+ if maxiter < 1:
291
+ raise ValueError("maxiter must be greater than 0")
292
+ if np.size(x0) > 1:
293
+ return _array_newton(func, x0, fprime, args, tol, maxiter, fprime2,
294
+ full_output)
295
+
296
+ # Convert to float (don't use float(x0); this works also for complex x0)
297
+ # Use np.asarray because we want x0 to be a numpy object, not a Python
298
+ # object. e.g. np.complex(1+1j) > 0 is possible, but (1 + 1j) > 0 raises
299
+ # a TypeError
300
+ x0 = np.asarray(x0)[()] * 1.0
301
+ p0 = x0
302
+ funcalls = 0
303
+ if fprime is not None:
304
+ # Newton-Raphson method
305
+ method = "newton"
306
+ for itr in range(maxiter):
307
+ # first evaluate fval
308
+ fval = func(p0, *args)
309
+ funcalls += 1
310
+ # If fval is 0, a root has been found, then terminate
311
+ if fval == 0:
312
+ return _results_select(
313
+ full_output, (p0, funcalls, itr, _ECONVERGED), method)
314
+ fder = fprime(p0, *args)
315
+ funcalls += 1
316
+ if fder == 0:
317
+ msg = "Derivative was zero."
318
+ if disp:
319
+ msg += (
320
+ f" Failed to converge after {itr + 1} iterations,"
321
+ f" value is {p0}."
322
+ )
323
+ raise RuntimeError(msg)
324
+ warnings.warn(msg, RuntimeWarning, stacklevel=2)
325
+ return _results_select(
326
+ full_output, (p0, funcalls, itr + 1, _ECONVERR), method)
327
+ newton_step = fval / fder
328
+ if fprime2:
329
+ fder2 = fprime2(p0, *args)
330
+ funcalls += 1
331
+ method = "halley"
332
+ # Halley's method:
333
+ # newton_step /= (1.0 - 0.5 * newton_step * fder2 / fder)
334
+ # Only do it if denominator stays close enough to 1
335
+ # Rationale: If 1-adj < 0, then Halley sends x in the
336
+ # opposite direction to Newton. Doesn't happen if x is close
337
+ # enough to root.
338
+ adj = newton_step * fder2 / fder / 2
339
+ if np.abs(adj) < 1:
340
+ newton_step /= 1.0 - adj
341
+ p = p0 - newton_step
342
+ if np.isclose(p, p0, rtol=rtol, atol=tol):
343
+ return _results_select(
344
+ full_output, (p, funcalls, itr + 1, _ECONVERGED), method)
345
+ p0 = p
346
+ else:
347
+ # Secant method
348
+ method = "secant"
349
+ if x1 is not None:
350
+ if x1 == x0:
351
+ raise ValueError("x1 and x0 must be different")
352
+ p1 = x1
353
+ else:
354
+ eps = 1e-4
355
+ p1 = x0 * (1 + eps)
356
+ p1 += (eps if p1 >= 0 else -eps)
357
+ q0 = func(p0, *args)
358
+ funcalls += 1
359
+ q1 = func(p1, *args)
360
+ funcalls += 1
361
+ if abs(q1) < abs(q0):
362
+ p0, p1, q0, q1 = p1, p0, q1, q0
363
+ for itr in range(maxiter):
364
+ if q1 == q0:
365
+ if p1 != p0:
366
+ msg = f"Tolerance of {p1 - p0} reached."
367
+ if disp:
368
+ msg += (
369
+ f" Failed to converge after {itr + 1} iterations,"
370
+ f" value is {p1}."
371
+ )
372
+ raise RuntimeError(msg)
373
+ warnings.warn(msg, RuntimeWarning, stacklevel=2)
374
+ p = (p1 + p0) / 2.0
375
+ return _results_select(
376
+ full_output, (p, funcalls, itr + 1, _ECONVERR), method)
377
+ else:
378
+ if abs(q1) > abs(q0):
379
+ p = (-q0 / q1 * p1 + p0) / (1 - q0 / q1)
380
+ else:
381
+ p = (-q1 / q0 * p0 + p1) / (1 - q1 / q0)
382
+ if np.isclose(p, p1, rtol=rtol, atol=tol):
383
+ return _results_select(
384
+ full_output, (p, funcalls, itr + 1, _ECONVERGED), method)
385
+ p0, q0 = p1, q1
386
+ p1 = p
387
+ q1 = func(p1, *args)
388
+ funcalls += 1
389
+
390
+ if disp:
391
+ msg = f"Failed to converge after {itr + 1} iterations, value is {p}."
392
+ raise RuntimeError(msg)
393
+
394
+ return _results_select(full_output, (p, funcalls, itr + 1, _ECONVERR), method)
395
+
396
+
397
+ def _array_newton(func, x0, fprime, args, tol, maxiter, fprime2, full_output):
398
+ """
399
+ A vectorized version of Newton, Halley, and secant methods for arrays.
400
+
401
+ Do not use this method directly. This method is called from `newton`
402
+ when ``np.size(x0) > 1`` is ``True``. For docstring, see `newton`.
403
+ """
404
+ # Explicitly copy `x0` as `p` will be modified inplace, but the
405
+ # user's array should not be altered.
406
+ p = np.array(x0, copy=True)
407
+
408
+ failures = np.ones_like(p, dtype=bool)
409
+ nz_der = np.ones_like(failures)
410
+ if fprime is not None:
411
+ # Newton-Raphson method
412
+ for iteration in range(maxiter):
413
+ # first evaluate fval
414
+ fval = np.asarray(func(p, *args))
415
+ # If all fval are 0, all roots have been found, then terminate
416
+ if not fval.any():
417
+ failures = fval.astype(bool)
418
+ break
419
+ fder = np.asarray(fprime(p, *args))
420
+ nz_der = (fder != 0)
421
+ # stop iterating if all derivatives are zero
422
+ if not nz_der.any():
423
+ break
424
+ # Newton step
425
+ dp = fval[nz_der] / fder[nz_der]
426
+ if fprime2 is not None:
427
+ fder2 = np.asarray(fprime2(p, *args))
428
+ dp = dp / (1.0 - 0.5 * dp * fder2[nz_der] / fder[nz_der])
429
+ # only update nonzero derivatives
430
+ p = np.asarray(p, dtype=np.result_type(p, dp, np.float64))
431
+ p[nz_der] -= dp
432
+ failures[nz_der] = np.abs(dp) >= tol # items not yet converged
433
+ # stop iterating if there aren't any failures, not incl zero der
434
+ if not failures[nz_der].any():
435
+ break
436
+ else:
437
+ # Secant method
438
+ dx = np.finfo(float).eps**0.33
439
+ p1 = p * (1 + dx) + np.where(p >= 0, dx, -dx)
440
+ q0 = np.asarray(func(p, *args))
441
+ q1 = np.asarray(func(p1, *args))
442
+ active = np.ones_like(p, dtype=bool)
443
+ for iteration in range(maxiter):
444
+ nz_der = (q1 != q0)
445
+ # stop iterating if all derivatives are zero
446
+ if not nz_der.any():
447
+ p = (p1 + p) / 2.0
448
+ break
449
+ # Secant Step
450
+ dp = (q1 * (p1 - p))[nz_der] / (q1 - q0)[nz_der]
451
+ # only update nonzero derivatives
452
+ p = np.asarray(p, dtype=np.result_type(p, p1, dp, np.float64))
453
+ p[nz_der] = p1[nz_der] - dp
454
+ active_zero_der = ~nz_der & active
455
+ p[active_zero_der] = (p1 + p)[active_zero_der] / 2.0
456
+ active &= nz_der # don't assign zero derivatives again
457
+ failures[nz_der] = np.abs(dp) >= tol # not yet converged
458
+ # stop iterating if there aren't any failures, not incl zero der
459
+ if not failures[nz_der].any():
460
+ break
461
+ p1, p = p, p1
462
+ q0 = q1
463
+ q1 = np.asarray(func(p1, *args))
464
+
465
+ zero_der = ~nz_der & failures # don't include converged with zero-ders
466
+ if zero_der.any():
467
+ # Secant warnings
468
+ if fprime is None:
469
+ nonzero_dp = (p1 != p)
470
+ # non-zero dp, but infinite newton step
471
+ zero_der_nz_dp = (zero_der & nonzero_dp)
472
+ if zero_der_nz_dp.any():
473
+ rms = np.sqrt(
474
+ sum((p1[zero_der_nz_dp] - p[zero_der_nz_dp]) ** 2)
475
+ )
476
+ warnings.warn(f'RMS of {rms:g} reached', RuntimeWarning, stacklevel=3)
477
+ # Newton or Halley warnings
478
+ else:
479
+ all_or_some = 'all' if zero_der.all() else 'some'
480
+ msg = f'{all_or_some:s} derivatives were zero'
481
+ warnings.warn(msg, RuntimeWarning, stacklevel=3)
482
+ elif failures.any():
483
+ all_or_some = 'all' if failures.all() else 'some'
484
+ msg = f'{all_or_some:s} failed to converge after {maxiter:d} iterations'
485
+ if failures.all():
486
+ raise RuntimeError(msg)
487
+ warnings.warn(msg, RuntimeWarning, stacklevel=3)
488
+
489
+ if full_output:
490
+ result = namedtuple('result', ('root', 'converged', 'zero_der'))
491
+ p = result(p, ~failures, zero_der)
492
+
493
+ return p
494
+
495
+
496
+ def bisect(f, a, b, args=(),
497
+ xtol=_xtol, rtol=_rtol, maxiter=_iter,
498
+ full_output=False, disp=True):
499
+ """
500
+ Find root of a function within an interval using bisection.
501
+
502
+ Basic bisection routine to find a root of the function `f` between the
503
+ arguments `a` and `b`. `f(a)` and `f(b)` cannot have the same signs.
504
+ Slow but sure.
505
+
506
+ Parameters
507
+ ----------
508
+ f : function
509
+ Python function returning a number. `f` must be continuous, and
510
+ f(a) and f(b) must have opposite signs.
511
+ a : scalar
512
+ One end of the bracketing interval [a,b].
513
+ b : scalar
514
+ The other end of the bracketing interval [a,b].
515
+ xtol : number, optional
516
+ The computed root ``x0`` will satisfy ``np.isclose(x, x0,
517
+ atol=xtol, rtol=rtol)``, where ``x`` is the exact root. The
518
+ parameter must be positive.
519
+ rtol : number, optional
520
+ The computed root ``x0`` will satisfy ``np.isclose(x, x0,
521
+ atol=xtol, rtol=rtol)``, where ``x`` is the exact root. The
522
+ parameter cannot be smaller than its default value of
523
+ ``4*np.finfo(float).eps``.
524
+ maxiter : int, optional
525
+ If convergence is not achieved in `maxiter` iterations, an error is
526
+ raised. Must be >= 0.
527
+ args : tuple, optional
528
+ Containing extra arguments for the function `f`.
529
+ `f` is called by ``apply(f, (x)+args)``.
530
+ full_output : bool, optional
531
+ If `full_output` is False, the root is returned. If `full_output` is
532
+ True, the return value is ``(x, r)``, where x is the root, and r is
533
+ a `RootResults` object.
534
+ disp : bool, optional
535
+ If True, raise RuntimeError if the algorithm didn't converge.
536
+ Otherwise, the convergence status is recorded in a `RootResults`
537
+ return object.
538
+
539
+ Returns
540
+ -------
541
+ root : float
542
+ Root of `f` between `a` and `b`.
543
+ r : `RootResults` (present if ``full_output = True``)
544
+ Object containing information about the convergence. In particular,
545
+ ``r.converged`` is True if the routine converged.
546
+
547
+ Notes
548
+ -----
549
+ As mentioned in the parameter documentation, the computed root ``x0`` will
550
+ satisfy ``np.isclose(x, x0, atol=xtol, rtol=rtol)``, where ``x`` is the
551
+ exact root. In equation form, this terminating condition is ``abs(x - x0)
552
+ <= xtol + rtol * abs(x0)``.
553
+
554
+ The default value ``xtol=2e-12`` may lead to surprising behavior if one
555
+ expects `bisect` to always compute roots with relative error near machine
556
+ precision. Care should be taken to select `xtol` for the use case at hand.
557
+ Setting ``xtol=5e-324``, the smallest subnormal number, will ensure the
558
+ highest level of accuracy. Larger values of `xtol` may be useful for saving
559
+ function evaluations when a root is at or near zero in applications where
560
+ the tiny absolute differences available between floating point numbers near
561
+ zero are not meaningful.
562
+
563
+ Examples
564
+ --------
565
+
566
+ >>> def f(x):
567
+ ... return (x**2 - 1)
568
+
569
+ >>> from scipy import optimize
570
+
571
+ >>> root = optimize.bisect(f, 0, 2)
572
+ >>> root
573
+ 1.0
574
+
575
+ >>> root = optimize.bisect(f, -2, 0)
576
+ >>> root
577
+ -1.0
578
+
579
+ See Also
580
+ --------
581
+ brentq, brenth, bisect, newton
582
+ fixed_point : scalar fixed-point finder
583
+ fsolve : n-dimensional root-finding
584
+ elementwise.find_root : efficient elementwise 1-D root-finder
585
+
586
+ """
587
+ if not isinstance(args, tuple):
588
+ args = (args,)
589
+ maxiter = operator.index(maxiter)
590
+ if xtol <= 0:
591
+ raise ValueError(f"xtol too small ({xtol:g} <= 0)")
592
+ if rtol < _rtol:
593
+ raise ValueError(f"rtol too small ({rtol:g} < {_rtol:g})")
594
+ f = _wrap_nan_raise(f)
595
+ r = _zeros._bisect(f, a, b, xtol, rtol, maxiter, args, full_output, disp)
596
+ return results_c(full_output, r, "bisect")
597
+
598
+
599
+ def ridder(f, a, b, args=(),
600
+ xtol=_xtol, rtol=_rtol, maxiter=_iter,
601
+ full_output=False, disp=True):
602
+ """
603
+ Find a root of a function in an interval using Ridder's method.
604
+
605
+ Parameters
606
+ ----------
607
+ f : function
608
+ Python function returning a number. f must be continuous, and f(a) and
609
+ f(b) must have opposite signs.
610
+ a : scalar
611
+ One end of the bracketing interval [a,b].
612
+ b : scalar
613
+ The other end of the bracketing interval [a,b].
614
+ xtol : number, optional
615
+ The computed root ``x0`` will satisfy ``np.isclose(x, x0,
616
+ atol=xtol, rtol=rtol)``, where ``x`` is the exact root. The
617
+ parameter must be positive.
618
+ rtol : number, optional
619
+ The computed root ``x0`` will satisfy ``np.isclose(x, x0,
620
+ atol=xtol, rtol=rtol)``, where ``x`` is the exact root. The
621
+ parameter cannot be smaller than its default value of
622
+ ``4*np.finfo(float).eps``.
623
+ maxiter : int, optional
624
+ If convergence is not achieved in `maxiter` iterations, an error is
625
+ raised. Must be >= 0.
626
+ args : tuple, optional
627
+ Containing extra arguments for the function `f`.
628
+ `f` is called by ``apply(f, (x)+args)``.
629
+ full_output : bool, optional
630
+ If `full_output` is False, the root is returned. If `full_output` is
631
+ True, the return value is ``(x, r)``, where `x` is the root, and `r` is
632
+ a `RootResults` object.
633
+ disp : bool, optional
634
+ If True, raise RuntimeError if the algorithm didn't converge.
635
+ Otherwise, the convergence status is recorded in any `RootResults`
636
+ return object.
637
+
638
+ Returns
639
+ -------
640
+ root : float
641
+ Root of `f` between `a` and `b`.
642
+ r : `RootResults` (present if ``full_output = True``)
643
+ Object containing information about the convergence.
644
+ In particular, ``r.converged`` is True if the routine converged.
645
+
646
+ See Also
647
+ --------
648
+ brentq, brenth, bisect, newton : 1-D root-finding
649
+ fixed_point : scalar fixed-point finder
650
+ elementwise.find_root : efficient elementwise 1-D root-finder
651
+
652
+ Notes
653
+ -----
654
+ Uses [Ridders1979]_ method to find a root of the function `f` between the
655
+ arguments `a` and `b`. Ridders' method is faster than bisection, but not
656
+ generally as fast as the Brent routines. [Ridders1979]_ provides the
657
+ classic description and source of the algorithm. A description can also be
658
+ found in any recent edition of Numerical Recipes.
659
+
660
+ The routine used here diverges slightly from standard presentations in
661
+ order to be a bit more careful of tolerance.
662
+
663
+ As mentioned in the parameter documentation, the computed root ``x0`` will
664
+ satisfy ``np.isclose(x, x0, atol=xtol, rtol=rtol)``, where ``x`` is the
665
+ exact root. In equation form, this terminating condition is ``abs(x - x0)
666
+ <= xtol + rtol * abs(x0)``.
667
+
668
+ The default value ``xtol=2e-12`` may lead to surprising behavior if one
669
+ expects `ridder` to always compute roots with relative error near machine
670
+ precision. Care should be taken to select `xtol` for the use case at hand.
671
+ Setting ``xtol=5e-324``, the smallest subnormal number, will ensure the
672
+ highest level of accuracy. Larger values of `xtol` may be useful for saving
673
+ function evaluations when a root is at or near zero in applications where
674
+ the tiny absolute differences available between floating point numbers near
675
+ zero are not meaningful.
676
+
677
+ References
678
+ ----------
679
+ .. [Ridders1979]
680
+ Ridders, C. F. J. "A New Algorithm for Computing a
681
+ Single Root of a Real Continuous Function."
682
+ IEEE Trans. Circuits Systems 26, 979-980, 1979.
683
+
684
+ Examples
685
+ --------
686
+
687
+ >>> def f(x):
688
+ ... return (x**2 - 1)
689
+
690
+ >>> from scipy import optimize
691
+
692
+ >>> root = optimize.ridder(f, 0, 2)
693
+ >>> root
694
+ 1.0
695
+
696
+ >>> root = optimize.ridder(f, -2, 0)
697
+ >>> root
698
+ -1.0
699
+ """
700
+ if not isinstance(args, tuple):
701
+ args = (args,)
702
+ maxiter = operator.index(maxiter)
703
+ if xtol <= 0:
704
+ raise ValueError(f"xtol too small ({xtol:g} <= 0)")
705
+ if rtol < _rtol:
706
+ raise ValueError(f"rtol too small ({rtol:g} < {_rtol:g})")
707
+ f = _wrap_nan_raise(f)
708
+ r = _zeros._ridder(f, a, b, xtol, rtol, maxiter, args, full_output, disp)
709
+ return results_c(full_output, r, "ridder")
710
+
711
+
712
+ def brentq(f, a, b, args=(),
713
+ xtol=_xtol, rtol=_rtol, maxiter=_iter,
714
+ full_output=False, disp=True):
715
+ """
716
+ Find a root of a function in a bracketing interval using Brent's method.
717
+
718
+ Uses the classic Brent's method to find a root of the function `f` on
719
+ the sign changing interval [a , b]. Generally considered the best of the
720
+ rootfinding routines here. It is a safe version of the secant method that
721
+ uses inverse quadratic extrapolation. Brent's method combines root
722
+ bracketing, interval bisection, and inverse quadratic interpolation. It is
723
+ sometimes known as the van Wijngaarden-Dekker-Brent method. Brent (1973)
724
+ claims convergence is guaranteed for functions computable within [a,b].
725
+
726
+ [Brent1973]_ provides the classic description of the algorithm. Another
727
+ description can be found in a recent edition of Numerical Recipes, including
728
+ [PressEtal1992]_. A third description is at
729
+ http://mathworld.wolfram.com/BrentsMethod.html. It should be easy to
730
+ understand the algorithm just by reading our code. Our code diverges a bit
731
+ from standard presentations: we choose a different formula for the
732
+ extrapolation step.
733
+
734
+ Parameters
735
+ ----------
736
+ f : function
737
+ Python function returning a number. The function :math:`f`
738
+ must be continuous, and :math:`f(a)` and :math:`f(b)` must
739
+ have opposite signs.
740
+ a : scalar
741
+ One end of the bracketing interval :math:`[a, b]`.
742
+ b : scalar
743
+ The other end of the bracketing interval :math:`[a, b]`.
744
+ xtol : number, optional
745
+ The computed root ``x0`` will satisfy ``np.isclose(x, x0,
746
+ atol=xtol, rtol=rtol)``, where ``x`` is the exact root. The
747
+ parameter must be positive. For nice functions, Brent's
748
+ method will often satisfy the above condition with ``xtol/2``
749
+ and ``rtol/2``. [Brent1973]_
750
+ rtol : number, optional
751
+ The computed root ``x0`` will satisfy ``np.isclose(x, x0,
752
+ atol=xtol, rtol=rtol)``, where ``x`` is the exact root. The
753
+ parameter cannot be smaller than its default value of
754
+ ``4*np.finfo(float).eps``. For nice functions, Brent's
755
+ method will often satisfy the above condition with ``xtol/2``
756
+ and ``rtol/2``. [Brent1973]_
757
+ maxiter : int, optional
758
+ If convergence is not achieved in `maxiter` iterations, an error is
759
+ raised. Must be >= 0.
760
+ args : tuple, optional
761
+ Containing extra arguments for the function `f`.
762
+ `f` is called by ``apply(f, (x)+args)``.
763
+ full_output : bool, optional
764
+ If `full_output` is False, the root is returned. If `full_output` is
765
+ True, the return value is ``(x, r)``, where `x` is the root, and `r` is
766
+ a `RootResults` object.
767
+ disp : bool, optional
768
+ If True, raise RuntimeError if the algorithm didn't converge.
769
+ Otherwise, the convergence status is recorded in any `RootResults`
770
+ return object.
771
+
772
+ Returns
773
+ -------
774
+ root : float
775
+ Root of `f` between `a` and `b`.
776
+ r : `RootResults` (present if ``full_output = True``)
777
+ Object containing information about the convergence. In particular,
778
+ ``r.converged`` is True if the routine converged.
779
+
780
+ See Also
781
+ --------
782
+ fmin, fmin_powell, fmin_cg, fmin_bfgs, fmin_ncg : multivariate local optimizers
783
+ leastsq : nonlinear least squares minimizer
784
+ fmin_l_bfgs_b, fmin_tnc, fmin_cobyla : constrained multivariate optimizers
785
+ basinhopping, differential_evolution, brute : global optimizers
786
+ fminbound, brent, golden, bracket : local scalar minimizers
787
+ fsolve : N-D root-finding
788
+ brenth, ridder, bisect, newton : 1-D root-finding
789
+ fixed_point : scalar fixed-point finder
790
+ elementwise.find_root : efficient elementwise 1-D root-finder
791
+
792
+ Notes
793
+ -----
794
+ `f` must be continuous. f(a) and f(b) must have opposite signs.
795
+
796
+ As mentioned in the parameter documentation, the computed root ``x0`` will
797
+ satisfy ``np.isclose(x, x0, atol=xtol, rtol=rtol)``, where ``x`` is the
798
+ exact root. In equation form, this terminating condition is ``abs(x - x0)
799
+ <= xtol + rtol * abs(x0)``.
800
+
801
+ The default value ``xtol=2e-12`` may lead to surprising behavior if one
802
+ expects `brentq` to always compute roots with relative error near machine
803
+ precision. Care should be taken to select `xtol` for the use case at hand.
804
+ Setting ``xtol=5e-324``, the smallest subnormal number, will ensure the
805
+ highest level of accuracy. Larger values of `xtol` may be useful for saving
806
+ function evaluations when a root is at or near zero in applications where
807
+ the tiny absolute differences available between floating point numbers near
808
+ zero are not meaningful.
809
+
810
+ References
811
+ ----------
812
+ .. [Brent1973]
813
+ Brent, R. P.,
814
+ *Algorithms for Minimization Without Derivatives*.
815
+ Englewood Cliffs, NJ: Prentice-Hall, 1973. Ch. 3-4.
816
+
817
+ .. [PressEtal1992]
818
+ Press, W. H.; Flannery, B. P.; Teukolsky, S. A.; and Vetterling, W. T.
819
+ *Numerical Recipes in FORTRAN: The Art of Scientific Computing*, 2nd ed.
820
+ Cambridge, England: Cambridge University Press, pp. 352-355, 1992.
821
+ Section 9.3: "Van Wijngaarden-Dekker-Brent Method."
822
+
823
+ Examples
824
+ --------
825
+ >>> def f(x):
826
+ ... return (x**2 - 1)
827
+
828
+ >>> from scipy import optimize
829
+
830
+ >>> root = optimize.brentq(f, -2, 0)
831
+ >>> root
832
+ -1.0
833
+
834
+ >>> root = optimize.brentq(f, 0, 2)
835
+ >>> root
836
+ 1.0
837
+ """
838
+ if not isinstance(args, tuple):
839
+ args = (args,)
840
+ maxiter = operator.index(maxiter)
841
+ if xtol <= 0:
842
+ raise ValueError(f"xtol too small ({xtol:g} <= 0)")
843
+ if rtol < _rtol:
844
+ raise ValueError(f"rtol too small ({rtol:g} < {_rtol:g})")
845
+ f = _wrap_nan_raise(f)
846
+ r = _zeros._brentq(f, a, b, xtol, rtol, maxiter, args, full_output, disp)
847
+ return results_c(full_output, r, "brentq")
848
+
849
+
850
+ def brenth(f, a, b, args=(),
851
+ xtol=_xtol, rtol=_rtol, maxiter=_iter,
852
+ full_output=False, disp=True):
853
+ """Find a root of a function in a bracketing interval using Brent's
854
+ method with hyperbolic extrapolation.
855
+
856
+ A variation on the classic Brent routine to find a root of the function f
857
+ between the arguments a and b that uses hyperbolic extrapolation instead of
858
+ inverse quadratic extrapolation. Bus & Dekker (1975) guarantee convergence
859
+ for this method, claiming that the upper bound of function evaluations here
860
+ is 4 or 5 times that of bisection.
861
+ f(a) and f(b) cannot have the same signs. Generally, on a par with the
862
+ brent routine, but not as heavily tested. It is a safe version of the
863
+ secant method that uses hyperbolic extrapolation.
864
+ The version here is by Chuck Harris, and implements Algorithm M of
865
+ [BusAndDekker1975]_, where further details (convergence properties,
866
+ additional remarks and such) can be found
867
+
868
+ Parameters
869
+ ----------
870
+ f : function
871
+ Python function returning a number. f must be continuous, and f(a) and
872
+ f(b) must have opposite signs.
873
+ a : scalar
874
+ One end of the bracketing interval [a,b].
875
+ b : scalar
876
+ The other end of the bracketing interval [a,b].
877
+ xtol : number, optional
878
+ The computed root ``x0`` will satisfy ``np.isclose(x, x0,
879
+ atol=xtol, rtol=rtol)``, where ``x`` is the exact root. The
880
+ parameter must be positive. As with `brentq`, for nice
881
+ functions the method will often satisfy the above condition
882
+ with ``xtol/2`` and ``rtol/2``.
883
+ rtol : number, optional
884
+ The computed root ``x0`` will satisfy ``np.isclose(x, x0,
885
+ atol=xtol, rtol=rtol)``, where ``x`` is the exact root. The
886
+ parameter cannot be smaller than its default value of
887
+ ``4*np.finfo(float).eps``. As with `brentq`, for nice functions
888
+ the method will often satisfy the above condition with
889
+ ``xtol/2`` and ``rtol/2``.
890
+ maxiter : int, optional
891
+ If convergence is not achieved in `maxiter` iterations, an error is
892
+ raised. Must be >= 0.
893
+ args : tuple, optional
894
+ Containing extra arguments for the function `f`.
895
+ `f` is called by ``apply(f, (x)+args)``.
896
+ full_output : bool, optional
897
+ If `full_output` is False, the root is returned. If `full_output` is
898
+ True, the return value is ``(x, r)``, where `x` is the root, and `r` is
899
+ a `RootResults` object.
900
+ disp : bool, optional
901
+ If True, raise RuntimeError if the algorithm didn't converge.
902
+ Otherwise, the convergence status is recorded in any `RootResults`
903
+ return object.
904
+
905
+ Returns
906
+ -------
907
+ root : float
908
+ Root of `f` between `a` and `b`.
909
+ r : `RootResults` (present if ``full_output = True``)
910
+ Object containing information about the convergence. In particular,
911
+ ``r.converged`` is True if the routine converged.
912
+
913
+ See Also
914
+ --------
915
+ fmin, fmin_powell, fmin_cg, fmin_bfgs, fmin_ncg : multivariate local optimizers
916
+ leastsq : nonlinear least squares minimizer
917
+ fmin_l_bfgs_b, fmin_tnc, fmin_cobyla : constrained multivariate optimizers
918
+ basinhopping, differential_evolution, brute : global optimizers
919
+ fminbound, brent, golden, bracket : local scalar minimizers
920
+ fsolve : N-D root-finding
921
+ brentq, ridder, bisect, newton : 1-D root-finding
922
+ fixed_point : scalar fixed-point finder
923
+ elementwise.find_root : efficient elementwise 1-D root-finder
924
+
925
+ Notes
926
+ -----
927
+ As mentioned in the parameter documentation, the computed root ``x0`` will
928
+ satisfy ``np.isclose(x, x0, atol=xtol, rtol=rtol)``, where ``x`` is the
929
+ exact root. In equation form, this terminating condition is ``abs(x - x0)
930
+ <= xtol + rtol * abs(x0)``.
931
+
932
+ The default value ``xtol=2e-12`` may lead to surprising behavior if one
933
+ expects `brenth` to always compute roots with relative error near machine
934
+ precision. Care should be taken to select `xtol` for the use case at hand.
935
+ Setting ``xtol=5e-324``, the smallest subnormal number, will ensure the
936
+ highest level of accuracy. Larger values of `xtol` may be useful for saving
937
+ function evaluations when a root is at or near zero in applications where
938
+ the tiny absolute differences available between floating point numbers near
939
+ zero are not meaningful.
940
+
941
+ References
942
+ ----------
943
+ .. [BusAndDekker1975]
944
+ Bus, J. C. P., Dekker, T. J.,
945
+ "Two Efficient Algorithms with Guaranteed Convergence for Finding a Zero
946
+ of a Function", ACM Transactions on Mathematical Software, Vol. 1, Issue
947
+ 4, Dec. 1975, pp. 330-345. Section 3: "Algorithm M".
948
+ :doi:`10.1145/355656.355659`
949
+
950
+ Examples
951
+ --------
952
+ >>> def f(x):
953
+ ... return (x**2 - 1)
954
+
955
+ >>> from scipy import optimize
956
+
957
+ >>> root = optimize.brenth(f, -2, 0)
958
+ >>> root
959
+ -1.0
960
+
961
+ >>> root = optimize.brenth(f, 0, 2)
962
+ >>> root
963
+ 1.0
964
+
965
+ """
966
+ if not isinstance(args, tuple):
967
+ args = (args,)
968
+ maxiter = operator.index(maxiter)
969
+ if xtol <= 0:
970
+ raise ValueError(f"xtol too small ({xtol:g} <= 0)")
971
+ if rtol < _rtol:
972
+ raise ValueError(f"rtol too small ({rtol:g} < {_rtol:g})")
973
+ f = _wrap_nan_raise(f)
974
+ r = _zeros._brenth(f, a, b, xtol, rtol, maxiter, args, full_output, disp)
975
+ return results_c(full_output, r, "brenth")
976
+
977
+
978
+ ################################
979
+ # TOMS "Algorithm 748: Enclosing Zeros of Continuous Functions", by
980
+ # Alefeld, G. E. and Potra, F. A. and Shi, Yixun,
981
+ # See [1]
982
+
983
+
984
+ def _notclose(fs, rtol=_rtol, atol=_xtol):
985
+ # Ensure not None, not 0, all finite, and not very close to each other
986
+ notclosefvals = (
987
+ all(fs) and all(np.isfinite(fs)) and
988
+ not any(any(np.isclose(_f, fs[i + 1:], rtol=rtol, atol=atol))
989
+ for i, _f in enumerate(fs[:-1])))
990
+ return notclosefvals
991
+
992
+
993
+ def _secant(xvals, fvals):
994
+ """Perform a secant step, taking a little care"""
995
+ # Secant has many "mathematically" equivalent formulations
996
+ # x2 = x0 - (x1 - x0)/(f1 - f0) * f0
997
+ # = x1 - (x1 - x0)/(f1 - f0) * f1
998
+ # = (-x1 * f0 + x0 * f1) / (f1 - f0)
999
+ # = (-f0 / f1 * x1 + x0) / (1 - f0 / f1)
1000
+ # = (-f1 / f0 * x0 + x1) / (1 - f1 / f0)
1001
+ x0, x1 = xvals[:2]
1002
+ f0, f1 = fvals[:2]
1003
+ if f0 == f1:
1004
+ return np.nan
1005
+ if np.abs(f1) > np.abs(f0):
1006
+ x2 = (-f0 / f1 * x1 + x0) / (1 - f0 / f1)
1007
+ else:
1008
+ x2 = (-f1 / f0 * x0 + x1) / (1 - f1 / f0)
1009
+ return x2
1010
+
1011
+
1012
+ def _update_bracket(ab, fab, c, fc):
1013
+ """Update a bracket given (c, fc), return the discarded endpoints."""
1014
+ fa, fb = fab
1015
+ idx = (0 if np.sign(fa) * np.sign(fc) > 0 else 1)
1016
+ rx, rfx = ab[idx], fab[idx]
1017
+ fab[idx] = fc
1018
+ ab[idx] = c
1019
+ return rx, rfx
1020
+
1021
+
1022
+ def _compute_divided_differences(xvals, fvals, N=None, full=True,
1023
+ forward=True):
1024
+ """Return a matrix of divided differences for the xvals, fvals pairs
1025
+
1026
+ DD[i, j] = f[x_{i-j}, ..., x_i] for 0 <= j <= i
1027
+
1028
+ If full is False, just return the main diagonal(or last row):
1029
+ f[a], f[a, b] and f[a, b, c].
1030
+ If forward is False, return f[c], f[b, c], f[a, b, c]."""
1031
+ if full:
1032
+ if forward:
1033
+ xvals = np.asarray(xvals)
1034
+ else:
1035
+ xvals = np.array(xvals)[::-1]
1036
+ M = len(xvals)
1037
+ N = M if N is None else min(N, M)
1038
+ DD = np.zeros([M, N])
1039
+ DD[:, 0] = fvals[:]
1040
+ for i in range(1, N):
1041
+ DD[i:, i] = (np.diff(DD[i - 1:, i - 1]) /
1042
+ (xvals[i:] - xvals[:M - i]))
1043
+ return DD
1044
+
1045
+ xvals = np.asarray(xvals)
1046
+ dd = np.array(fvals)
1047
+ row = np.array(fvals)
1048
+ idx2Use = (0 if forward else -1)
1049
+ dd[0] = fvals[idx2Use]
1050
+ for i in range(1, len(xvals)):
1051
+ denom = xvals[i:i + len(row) - 1] - xvals[:len(row) - 1]
1052
+ row = np.diff(row)[:] / denom
1053
+ dd[i] = row[idx2Use]
1054
+ return dd
1055
+
1056
+
1057
+ def _interpolated_poly(xvals, fvals, x):
1058
+ """Compute p(x) for the polynomial passing through the specified locations.
1059
+
1060
+ Use Neville's algorithm to compute p(x) where p is the minimal degree
1061
+ polynomial passing through the points xvals, fvals"""
1062
+ xvals = np.asarray(xvals)
1063
+ N = len(xvals)
1064
+ Q = np.zeros([N, N])
1065
+ D = np.zeros([N, N])
1066
+ Q[:, 0] = fvals[:]
1067
+ D[:, 0] = fvals[:]
1068
+ for k in range(1, N):
1069
+ alpha = D[k:, k - 1] - Q[k - 1:N - 1, k - 1]
1070
+ diffik = xvals[0:N - k] - xvals[k:N]
1071
+ Q[k:, k] = (xvals[k:] - x) / diffik * alpha
1072
+ D[k:, k] = (xvals[:N - k] - x) / diffik * alpha
1073
+ # Expect Q[-1, 1:] to be small relative to Q[-1, 0] as x approaches a root
1074
+ return np.sum(Q[-1, 1:]) + Q[-1, 0]
1075
+
1076
+
1077
+ def _inverse_poly_zero(a, b, c, d, fa, fb, fc, fd):
1078
+ """Inverse cubic interpolation f-values -> x-values
1079
+
1080
+ Given four points (fa, a), (fb, b), (fc, c), (fd, d) with
1081
+ fa, fb, fc, fd all distinct, find poly IP(y) through the 4 points
1082
+ and compute x=IP(0).
1083
+ """
1084
+ return _interpolated_poly([fa, fb, fc, fd], [a, b, c, d], 0)
1085
+
1086
+
1087
+ def _newton_quadratic(ab, fab, d, fd, k):
1088
+ """Apply Newton-Raphson like steps, using divided differences to approximate f'
1089
+
1090
+ ab is a real interval [a, b] containing a root,
1091
+ fab holds the real values of f(a), f(b)
1092
+ d is a real number outside [ab, b]
1093
+ k is the number of steps to apply
1094
+ """
1095
+ a, b = ab
1096
+ fa, fb = fab
1097
+ _, B, A = _compute_divided_differences([a, b, d], [fa, fb, fd],
1098
+ forward=True, full=False)
1099
+
1100
+ # _P is the quadratic polynomial through the 3 points
1101
+ def _P(x):
1102
+ # Horner evaluation of fa + B * (x - a) + A * (x - a) * (x - b)
1103
+ return (A * (x - b) + B) * (x - a) + fa
1104
+
1105
+ if A == 0:
1106
+ r = a - fa / B
1107
+ else:
1108
+ r = (a if np.sign(A) * np.sign(fa) > 0 else b)
1109
+ # Apply k Newton-Raphson steps to _P(x), starting from x=r
1110
+ for i in range(k):
1111
+ r1 = r - _P(r) / (B + A * (2 * r - a - b))
1112
+ if not (ab[0] < r1 < ab[1]):
1113
+ if (ab[0] < r < ab[1]):
1114
+ return r
1115
+ r = sum(ab) / 2.0
1116
+ break
1117
+ r = r1
1118
+
1119
+ return r
1120
+
1121
+
1122
+ class TOMS748Solver:
1123
+ """Solve f(x, *args) == 0 using Algorithm748 of Alefeld, Potro & Shi.
1124
+ """
1125
+ _MU = 0.5
1126
+ _K_MIN = 1
1127
+ _K_MAX = 100 # A very high value for real usage. Expect 1, 2, maybe 3.
1128
+
1129
+ def __init__(self):
1130
+ self.f = None
1131
+ self.args = None
1132
+ self.function_calls = 0
1133
+ self.iterations = 0
1134
+ self.k = 2
1135
+ # ab=[a,b] is a global interval containing a root
1136
+ self.ab = [np.nan, np.nan]
1137
+ # fab is function values at a, b
1138
+ self.fab = [np.nan, np.nan]
1139
+ self.d = None
1140
+ self.fd = None
1141
+ self.e = None
1142
+ self.fe = None
1143
+ self.disp = False
1144
+ self.xtol = _xtol
1145
+ self.rtol = _rtol
1146
+ self.maxiter = _iter
1147
+
1148
+ def configure(self, xtol, rtol, maxiter, disp, k):
1149
+ self.disp = disp
1150
+ self.xtol = xtol
1151
+ self.rtol = rtol
1152
+ self.maxiter = maxiter
1153
+ # Silently replace a low value of k with 1
1154
+ self.k = max(k, self._K_MIN)
1155
+ # Noisily replace a high value of k with self._K_MAX
1156
+ if self.k > self._K_MAX:
1157
+ msg = f"toms748: Overriding k: ->{self._K_MAX}"
1158
+ warnings.warn(msg, RuntimeWarning, stacklevel=3)
1159
+ self.k = self._K_MAX
1160
+
1161
+ def _callf(self, x, error=True):
1162
+ """Call the user-supplied function, update book-keeping"""
1163
+ fx = self.f(x, *self.args)
1164
+ self.function_calls += 1
1165
+ if not np.isfinite(fx) and error:
1166
+ raise ValueError(f"Invalid function value: f({x:f}) -> {fx} ")
1167
+ return fx
1168
+
1169
+ def get_result(self, x, flag=_ECONVERGED):
1170
+ r"""Package the result and statistics into a tuple."""
1171
+ return (x, self.function_calls, self.iterations, flag)
1172
+
1173
+ def _update_bracket(self, c, fc):
1174
+ return _update_bracket(self.ab, self.fab, c, fc)
1175
+
1176
+ def start(self, f, a, b, args=()):
1177
+ r"""Prepare for the iterations."""
1178
+ self.function_calls = 0
1179
+ self.iterations = 0
1180
+
1181
+ self.f = f
1182
+ self.args = args
1183
+ self.ab[:] = [a, b]
1184
+ if not np.isfinite(a) or np.imag(a) != 0:
1185
+ raise ValueError(f"Invalid x value: {a} ")
1186
+ if not np.isfinite(b) or np.imag(b) != 0:
1187
+ raise ValueError(f"Invalid x value: {b} ")
1188
+
1189
+ fa = self._callf(a)
1190
+ if not np.isfinite(fa) or np.imag(fa) != 0:
1191
+ raise ValueError(f"Invalid function value: f({a:f}) -> {fa} ")
1192
+ if fa == 0:
1193
+ return _ECONVERGED, a
1194
+ fb = self._callf(b)
1195
+ if not np.isfinite(fb) or np.imag(fb) != 0:
1196
+ raise ValueError(f"Invalid function value: f({b:f}) -> {fb} ")
1197
+ if fb == 0:
1198
+ return _ECONVERGED, b
1199
+
1200
+ if np.sign(fb) * np.sign(fa) > 0:
1201
+ raise ValueError("f(a) and f(b) must have different signs, but "
1202
+ f"f({a:e})={fa:e}, f({b:e})={fb:e} ")
1203
+ self.fab[:] = [fa, fb]
1204
+
1205
+ return _EINPROGRESS, sum(self.ab) / 2.0
1206
+
1207
+ def get_status(self):
1208
+ """Determine the current status."""
1209
+ a, b = self.ab[:2]
1210
+ if np.isclose(a, b, rtol=self.rtol, atol=self.xtol):
1211
+ return _ECONVERGED, sum(self.ab) / 2.0
1212
+ if self.iterations >= self.maxiter:
1213
+ return _ECONVERR, sum(self.ab) / 2.0
1214
+ return _EINPROGRESS, sum(self.ab) / 2.0
1215
+
1216
+ def iterate(self):
1217
+ """Perform one step in the algorithm.
1218
+
1219
+ Implements Algorithm 4.1(k=1) or 4.2(k=2) in [APS1995]
1220
+ """
1221
+ self.iterations += 1
1222
+ eps = np.finfo(float).eps
1223
+ d, fd, e, fe = self.d, self.fd, self.e, self.fe
1224
+ ab_width = self.ab[1] - self.ab[0] # Need the start width below
1225
+ c = None
1226
+
1227
+ for nsteps in range(2, self.k+2):
1228
+ # If the f-values are sufficiently separated, perform an inverse
1229
+ # polynomial interpolation step. Otherwise, nsteps repeats of
1230
+ # an approximate Newton-Raphson step.
1231
+ if _notclose(self.fab + [fd, fe], rtol=0, atol=32*eps):
1232
+ c0 = _inverse_poly_zero(self.ab[0], self.ab[1], d, e,
1233
+ self.fab[0], self.fab[1], fd, fe)
1234
+ if self.ab[0] < c0 < self.ab[1]:
1235
+ c = c0
1236
+ if c is None:
1237
+ c = _newton_quadratic(self.ab, self.fab, d, fd, nsteps)
1238
+
1239
+ fc = self._callf(c)
1240
+ if fc == 0:
1241
+ return _ECONVERGED, c
1242
+
1243
+ # re-bracket
1244
+ e, fe = d, fd
1245
+ d, fd = self._update_bracket(c, fc)
1246
+
1247
+ # u is the endpoint with the smallest f-value
1248
+ uix = (0 if np.abs(self.fab[0]) < np.abs(self.fab[1]) else 1)
1249
+ u, fu = self.ab[uix], self.fab[uix]
1250
+
1251
+ _, A = _compute_divided_differences(self.ab, self.fab,
1252
+ forward=(uix == 0), full=False)
1253
+ c = u - 2 * fu / A
1254
+ if np.abs(c - u) > 0.5 * (self.ab[1] - self.ab[0]):
1255
+ c = sum(self.ab) / 2.0
1256
+ else:
1257
+ if np.isclose(c, u, rtol=eps, atol=0):
1258
+ # c didn't change (much).
1259
+ # Either because the f-values at the endpoints have vastly
1260
+ # differing magnitudes, or because the root is very close to
1261
+ # that endpoint
1262
+ frs = np.frexp(self.fab)[1]
1263
+ if frs[uix] < frs[1 - uix] - 50: # Differ by more than 2**50
1264
+ c = (31 * self.ab[uix] + self.ab[1 - uix]) / 32
1265
+ else:
1266
+ # Make a bigger adjustment, about the
1267
+ # size of the requested tolerance.
1268
+ mm = (1 if uix == 0 else -1)
1269
+ adj = mm * np.abs(c) * self.rtol + mm * self.xtol
1270
+ c = u + adj
1271
+ if not self.ab[0] < c < self.ab[1]:
1272
+ c = sum(self.ab) / 2.0
1273
+
1274
+ fc = self._callf(c)
1275
+ if fc == 0:
1276
+ return _ECONVERGED, c
1277
+
1278
+ e, fe = d, fd
1279
+ d, fd = self._update_bracket(c, fc)
1280
+
1281
+ # If the width of the new interval did not decrease enough, bisect
1282
+ if self.ab[1] - self.ab[0] > self._MU * ab_width:
1283
+ e, fe = d, fd
1284
+ z = sum(self.ab) / 2.0
1285
+ fz = self._callf(z)
1286
+ if fz == 0:
1287
+ return _ECONVERGED, z
1288
+ d, fd = self._update_bracket(z, fz)
1289
+
1290
+ # Record d and e for next iteration
1291
+ self.d, self.fd = d, fd
1292
+ self.e, self.fe = e, fe
1293
+
1294
+ status, xn = self.get_status()
1295
+ return status, xn
1296
+
1297
+ def solve(self, f, a, b, args=(),
1298
+ xtol=_xtol, rtol=_rtol, k=2, maxiter=_iter, disp=True):
1299
+ r"""Solve f(x) = 0 given an interval containing a root."""
1300
+ self.configure(xtol=xtol, rtol=rtol, maxiter=maxiter, disp=disp, k=k)
1301
+ status, xn = self.start(f, a, b, args)
1302
+ if status == _ECONVERGED:
1303
+ return self.get_result(xn)
1304
+
1305
+ # The first step only has two x-values.
1306
+ c = _secant(self.ab, self.fab)
1307
+ if not self.ab[0] < c < self.ab[1]:
1308
+ c = sum(self.ab) / 2.0
1309
+ fc = self._callf(c)
1310
+ if fc == 0:
1311
+ return self.get_result(c)
1312
+
1313
+ self.d, self.fd = self._update_bracket(c, fc)
1314
+ self.e, self.fe = None, None
1315
+ self.iterations += 1
1316
+
1317
+ while True:
1318
+ status, xn = self.iterate()
1319
+ if status == _ECONVERGED:
1320
+ return self.get_result(xn)
1321
+ if status == _ECONVERR:
1322
+ fmt = "Failed to converge after %d iterations, bracket is %s"
1323
+ if disp:
1324
+ msg = fmt % (self.iterations + 1, self.ab)
1325
+ raise RuntimeError(msg)
1326
+ return self.get_result(xn, _ECONVERR)
1327
+
1328
+
1329
+ def toms748(f, a, b, args=(), k=1,
1330
+ xtol=_xtol, rtol=_rtol, maxiter=_iter,
1331
+ full_output=False, disp=True):
1332
+ """
1333
+ Find a root using TOMS Algorithm 748 method.
1334
+
1335
+ Implements the Algorithm 748 method of Alefeld, Potro and Shi to find a
1336
+ root of the function `f` on the interval ``[a , b]``, where `f(a)` and
1337
+ `f(b)` must have opposite signs.
1338
+
1339
+ It uses a mixture of inverse cubic interpolation and
1340
+ "Newton-quadratic" steps. [APS1995].
1341
+
1342
+ Parameters
1343
+ ----------
1344
+ f : function
1345
+ Python function returning a scalar. The function :math:`f`
1346
+ must be continuous, and :math:`f(a)` and :math:`f(b)`
1347
+ have opposite signs.
1348
+ a : scalar,
1349
+ lower boundary of the search interval
1350
+ b : scalar,
1351
+ upper boundary of the search interval
1352
+ args : tuple, optional
1353
+ containing extra arguments for the function `f`.
1354
+ `f` is called by ``f(x, *args)``.
1355
+ k : int, optional
1356
+ The number of Newton quadratic steps to perform each
1357
+ iteration. ``k>=1``.
1358
+ xtol : scalar, optional
1359
+ The computed root ``x0`` will satisfy ``np.isclose(x, x0,
1360
+ atol=xtol, rtol=rtol)``, where ``x`` is the exact root. The
1361
+ parameter must be positive.
1362
+ rtol : scalar, optional
1363
+ The computed root ``x0`` will satisfy ``np.isclose(x, x0,
1364
+ atol=xtol, rtol=rtol)``, where ``x`` is the exact root.
1365
+ maxiter : int, optional
1366
+ If convergence is not achieved in `maxiter` iterations, an error is
1367
+ raised. Must be >= 0.
1368
+ full_output : bool, optional
1369
+ If `full_output` is False, the root is returned. If `full_output` is
1370
+ True, the return value is ``(x, r)``, where `x` is the root, and `r` is
1371
+ a `RootResults` object.
1372
+ disp : bool, optional
1373
+ If True, raise RuntimeError if the algorithm didn't converge.
1374
+ Otherwise, the convergence status is recorded in the `RootResults`
1375
+ return object.
1376
+
1377
+ Returns
1378
+ -------
1379
+ root : float
1380
+ Approximate root of `f`
1381
+ r : `RootResults` (present if ``full_output = True``)
1382
+ Object containing information about the convergence. In particular,
1383
+ ``r.converged`` is True if the routine converged.
1384
+
1385
+ See Also
1386
+ --------
1387
+ brentq, brenth, ridder, bisect, newton
1388
+ fsolve : find roots in N dimensions.
1389
+ elementwise.find_root : efficient elementwise 1-D root-finder
1390
+
1391
+ Notes
1392
+ -----
1393
+ `f` must be continuous.
1394
+ Algorithm 748 with ``k=2`` is asymptotically the most efficient
1395
+ algorithm known for finding roots of a four times continuously
1396
+ differentiable function.
1397
+ In contrast with Brent's algorithm, which may only decrease the length of
1398
+ the enclosing bracket on the last step, Algorithm 748 decreases it each
1399
+ iteration with the same asymptotic efficiency as it finds the root.
1400
+
1401
+ For easy statement of efficiency indices, assume that `f` has 4
1402
+ continuous deriviatives.
1403
+ For ``k=1``, the convergence order is at least 2.7, and with about
1404
+ asymptotically 2 function evaluations per iteration, the efficiency
1405
+ index is approximately 1.65.
1406
+ For ``k=2``, the order is about 4.6 with asymptotically 3 function
1407
+ evaluations per iteration, and the efficiency index 1.66.
1408
+ For higher values of `k`, the efficiency index approaches
1409
+ the kth root of ``(3k-2)``, hence ``k=1`` or ``k=2`` are
1410
+ usually appropriate.
1411
+
1412
+ As mentioned in the parameter documentation, the computed root ``x0`` will
1413
+ satisfy ``np.isclose(x, x0, atol=xtol, rtol=rtol)``, where ``x`` is the
1414
+ exact root. In equation form, this terminating condition is ``abs(x - x0)
1415
+ <= xtol + rtol * abs(x0)``.
1416
+
1417
+ The default value ``xtol=2e-12`` may lead to surprising behavior if one
1418
+ expects `toms748` to always compute roots with relative error near machine
1419
+ precision. Care should be taken to select `xtol` for the use case at hand.
1420
+ Setting ``xtol=5e-324``, the smallest subnormal number, will ensure the
1421
+ highest level of accuracy. Larger values of `xtol` may be useful for saving
1422
+ function evaluations when a root is at or near zero in applications where
1423
+ the tiny absolute differences available between floating point numbers near
1424
+ zero are not meaningful.
1425
+
1426
+ References
1427
+ ----------
1428
+ .. [APS1995]
1429
+ Alefeld, G. E. and Potra, F. A. and Shi, Yixun,
1430
+ *Algorithm 748: Enclosing Zeros of Continuous Functions*,
1431
+ ACM Trans. Math. Softw. Volume 221(1995)
1432
+ doi = {10.1145/210089.210111}
1433
+
1434
+ Examples
1435
+ --------
1436
+ >>> def f(x):
1437
+ ... return (x**3 - 1) # only one real root at x = 1
1438
+
1439
+ >>> from scipy import optimize
1440
+ >>> root, results = optimize.toms748(f, 0, 2, full_output=True)
1441
+ >>> root
1442
+ 1.0
1443
+ >>> results
1444
+ converged: True
1445
+ flag: converged
1446
+ function_calls: 11
1447
+ iterations: 5
1448
+ root: 1.0
1449
+ method: toms748
1450
+ """
1451
+ if xtol <= 0:
1452
+ raise ValueError(f"xtol too small ({xtol:g} <= 0)")
1453
+ if rtol < _rtol / 4:
1454
+ raise ValueError(f"rtol too small ({rtol:g} < {_rtol/4:g})")
1455
+ maxiter = operator.index(maxiter)
1456
+ if maxiter < 1:
1457
+ raise ValueError("maxiter must be greater than 0")
1458
+ if not np.isfinite(a):
1459
+ raise ValueError(f"a is not finite {a}")
1460
+ if not np.isfinite(b):
1461
+ raise ValueError(f"b is not finite {b}")
1462
+ if a >= b:
1463
+ raise ValueError(f"a and b are not an interval [{a}, {b}]")
1464
+ if not k >= 1:
1465
+ raise ValueError(f"k too small ({k} < 1)")
1466
+
1467
+ if not isinstance(args, tuple):
1468
+ args = (args,)
1469
+ f = _wrap_nan_raise(f)
1470
+ solver = TOMS748Solver()
1471
+ result = solver.solve(f, a, b, args=args, k=k, xtol=xtol, rtol=rtol,
1472
+ maxiter=maxiter, disp=disp)
1473
+ x, function_calls, iterations, flag = result
1474
+ return _results_select(full_output, (x, function_calls, iterations, flag),
1475
+ "toms748")
loan-advisor-agent/.venv/Lib/site-packages/scipy/optimize/cobyla.py ADDED
@@ -0,0 +1,19 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # This file is not meant for public use and will be removed in SciPy v2.0.0.
2
+ # Use the `scipy.optimize` namespace for importing the functions
3
+ # included below.
4
+
5
+ from scipy._lib.deprecation import _sub_module_deprecation
6
+
7
+
8
+ __all__ = [ # noqa: F822
9
+ 'OptimizeResult',
10
+ 'fmin_cobyla',
11
+ ]
12
+
13
+ def __dir__():
14
+ return __all__
15
+
16
+ def __getattr__(name):
17
+ return _sub_module_deprecation(sub_package="optimize", module="cobyla",
18
+ private_modules=["_cobyla_py"], all=__all__,
19
+ attribute=name)
loan-advisor-agent/.venv/Lib/site-packages/scipy/optimize/cython_optimize.pxd ADDED
@@ -0,0 +1,11 @@
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Public Cython API declarations
2
+ #
3
+ # See doc/source/dev/contributor/public_cython_api.rst for guidelines
4
+
5
+
6
+ # The following cimport statement provides legacy ABI
7
+ # support. Changing it causes an ABI forward-compatibility break
8
+ # (gh-11793), so we currently leave it as is (no further cimport
9
+ # statements should be used in this file).
10
+ from scipy.optimize.cython_optimize._zeros cimport (
11
+ brentq, brenth, ridder, bisect, zeros_full_output)
loan-advisor-agent/.venv/Lib/site-packages/scipy/optimize/cython_optimize/__init__.py ADDED
@@ -0,0 +1,133 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Cython optimize root finding API
3
+ ================================
4
+ The underlying C functions for the following root finders can be accessed
5
+ directly using Cython:
6
+
7
+ - `~scipy.optimize.bisect`
8
+ - `~scipy.optimize.ridder`
9
+ - `~scipy.optimize.brenth`
10
+ - `~scipy.optimize.brentq`
11
+
12
+ The Cython API for the root finding functions is similar except there is no
13
+ ``disp`` argument. Import the root finding functions using ``cimport`` from
14
+ `scipy.optimize.cython_optimize`. ::
15
+
16
+ from scipy.optimize.cython_optimize cimport bisect, ridder, brentq, brenth
17
+
18
+
19
+ Callback signature
20
+ ------------------
21
+ The zeros functions in `~scipy.optimize.cython_optimize` expect a callback that
22
+ takes a double for the scalar independent variable as the 1st argument and a
23
+ user defined ``struct`` with any extra parameters as the 2nd argument. ::
24
+
25
+ double (*callback_type)(double, void*) noexcept
26
+
27
+
28
+ Examples
29
+ --------
30
+ Usage of `~scipy.optimize.cython_optimize` requires Cython to write callbacks
31
+ that are compiled into C. For more information on compiling Cython, see the
32
+ `Cython Documentation <http://docs.cython.org/en/latest/index.html>`_.
33
+
34
+ These are the basic steps:
35
+
36
+ 1. Create a Cython ``.pyx`` file, for example: ``myexample.pyx``.
37
+ 2. Import the desired root finder from `~scipy.optimize.cython_optimize`.
38
+ 3. Write the callback function, and call the selected root finding function
39
+ passing the callback, any extra arguments, and the other solver
40
+ parameters. ::
41
+
42
+ from scipy.optimize.cython_optimize cimport brentq
43
+
44
+ # import math from Cython
45
+ from libc cimport math
46
+
47
+ myargs = {'C0': 1.0, 'C1': 0.7} # a dictionary of extra arguments
48
+ XLO, XHI = 0.5, 1.0 # lower and upper search boundaries
49
+ XTOL, RTOL, MITR = 1e-3, 1e-3, 10 # other solver parameters
50
+
51
+ # user-defined struct for extra parameters
52
+ ctypedef struct test_params:
53
+ double C0
54
+ double C1
55
+
56
+
57
+ # user-defined callback
58
+ cdef double f(double x, void *args) noexcept:
59
+ cdef test_params *myargs = <test_params *> args
60
+ return myargs.C0 - math.exp(-(x - myargs.C1))
61
+
62
+
63
+ # Cython wrapper function
64
+ cdef double brentq_wrapper_example(dict args, double xa, double xb,
65
+ double xtol, double rtol, int mitr):
66
+ # Cython automatically casts dictionary to struct
67
+ cdef test_params myargs = args
68
+ return brentq(
69
+ f, xa, xb, <test_params *> &myargs, xtol, rtol, mitr, NULL)
70
+
71
+
72
+ # Python function
73
+ def brentq_example(args=myargs, xa=XLO, xb=XHI, xtol=XTOL, rtol=RTOL,
74
+ mitr=MITR):
75
+ '''Calls Cython wrapper from Python.'''
76
+ return brentq_wrapper_example(args, xa, xb, xtol, rtol, mitr)
77
+
78
+ 4. If you want to call your function from Python, create a Cython wrapper, and
79
+ a Python function that calls the wrapper, or use ``cpdef``. Then, in Python,
80
+ you can import and run the example. ::
81
+
82
+ from myexample import brentq_example
83
+
84
+ x = brentq_example()
85
+ # 0.6999942848231314
86
+
87
+ 5. Create a Cython ``.pxd`` file if you need to export any Cython functions.
88
+
89
+
90
+ Full output
91
+ -----------
92
+ The functions in `~scipy.optimize.cython_optimize` can also copy the full
93
+ output from the solver to a C ``struct`` that is passed as its last argument.
94
+ If you don't want the full output, just pass ``NULL``. The full output
95
+ ``struct`` must be type ``zeros_full_output``, which is defined in
96
+ `scipy.optimize.cython_optimize` with the following fields:
97
+
98
+ - ``int funcalls``: number of function calls
99
+ - ``int iterations``: number of iterations
100
+ - ``int error_num``: error number
101
+ - ``double root``: root of function
102
+
103
+ The root is copied by `~scipy.optimize.cython_optimize` to the full output
104
+ ``struct``. An error number of -1 means a sign error, -2 means a convergence
105
+ error, and 0 means the solver converged. Continuing from the previous example::
106
+
107
+ from scipy.optimize.cython_optimize cimport zeros_full_output
108
+
109
+
110
+ # cython brentq solver with full output
111
+ cdef zeros_full_output brentq_full_output_wrapper_example(
112
+ dict args, double xa, double xb, double xtol, double rtol,
113
+ int mitr):
114
+ cdef test_params myargs = args
115
+ cdef zeros_full_output my_full_output
116
+ # use my_full_output instead of NULL
117
+ brentq(f, xa, xb, &myargs, xtol, rtol, mitr, &my_full_output)
118
+ return my_full_output
119
+
120
+
121
+ # Python function
122
+ def brent_full_output_example(args=myargs, xa=XLO, xb=XHI, xtol=XTOL,
123
+ rtol=RTOL, mitr=MITR):
124
+ '''Returns full output'''
125
+ return brentq_full_output_wrapper_example(args, xa, xb, xtol, rtol,
126
+ mitr)
127
+
128
+ result = brent_full_output_example()
129
+ # {'error_num': 0,
130
+ # 'funcalls': 6,
131
+ # 'iterations': 5,
132
+ # 'root': 0.6999942848231314}
133
+ """
loan-advisor-agent/.venv/Lib/site-packages/scipy/optimize/cython_optimize/_zeros.cp312-win_amd64.dll.a ADDED
Binary file (1.53 kB). View file
 
loan-advisor-agent/.venv/Lib/site-packages/scipy/optimize/cython_optimize/_zeros.cp312-win_amd64.pyd ADDED
Binary file (78.8 kB). View file
 
loan-advisor-agent/.venv/Lib/site-packages/scipy/optimize/cython_optimize/_zeros.pxd ADDED
@@ -0,0 +1,33 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Legacy public Cython API declarations
2
+ #
3
+ # NOTE: due to the way Cython ABI compatibility works, **no changes
4
+ # should be made to this file** --- any API additions/changes should be
5
+ # done in `cython_optimize.pxd` (see gh-11793).
6
+
7
+ ctypedef double (*callback_type)(double, void*) noexcept
8
+
9
+ ctypedef struct zeros_parameters:
10
+ callback_type function
11
+ void* args
12
+
13
+ ctypedef struct zeros_full_output:
14
+ int funcalls
15
+ int iterations
16
+ int error_num
17
+ double root
18
+
19
+ cdef double bisect(callback_type f, double xa, double xb, void* args,
20
+ double xtol, double rtol, int iter,
21
+ zeros_full_output *full_output) noexcept nogil
22
+
23
+ cdef double ridder(callback_type f, double xa, double xb, void* args,
24
+ double xtol, double rtol, int iter,
25
+ zeros_full_output *full_output) noexcept nogil
26
+
27
+ cdef double brenth(callback_type f, double xa, double xb, void* args,
28
+ double xtol, double rtol, int iter,
29
+ zeros_full_output *full_output) noexcept nogil
30
+
31
+ cdef double brentq(callback_type f, double xa, double xb, void* args,
32
+ double xtol, double rtol, int iter,
33
+ zeros_full_output *full_output) noexcept nogil
loan-advisor-agent/.venv/Lib/site-packages/scipy/optimize/cython_optimize/c_zeros.pxd ADDED
@@ -0,0 +1,26 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ cdef extern from "../Zeros/zeros.h":
2
+ ctypedef double (*callback_type)(double, void*) noexcept
3
+ ctypedef struct scipy_zeros_info:
4
+ int funcalls
5
+ int iterations
6
+ int error_num
7
+
8
+ cdef extern from "../Zeros/bisect.c" nogil:
9
+ double bisect(callback_type f, double xa, double xb, double xtol,
10
+ double rtol, int iter, void *func_data_param,
11
+ scipy_zeros_info *solver_stats)
12
+
13
+ cdef extern from "../Zeros/ridder.c" nogil:
14
+ double ridder(callback_type f, double xa, double xb, double xtol,
15
+ double rtol, int iter, void *func_data_param,
16
+ scipy_zeros_info *solver_stats)
17
+
18
+ cdef extern from "../Zeros/brenth.c" nogil:
19
+ double brenth(callback_type f, double xa, double xb, double xtol,
20
+ double rtol, int iter, void *func_data_param,
21
+ scipy_zeros_info *solver_stats)
22
+
23
+ cdef extern from "../Zeros/brentq.c" nogil:
24
+ double brentq(callback_type f, double xa, double xb, double xtol,
25
+ double rtol, int iter, void *func_data_param,
26
+ scipy_zeros_info *solver_stats)
loan-advisor-agent/.venv/Lib/site-packages/scipy/optimize/elementwise.py ADDED
@@ -0,0 +1,38 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ ===================================================================
3
+ Elementwise Scalar Optimization (:mod:`scipy.optimize.elementwise`)
4
+ ===================================================================
5
+
6
+ .. currentmodule:: scipy.optimize.elementwise
7
+
8
+ This module provides a collection of functions for root finding and
9
+ minimization of scalar, real-valued functions of one variable. Unlike their
10
+ counterparts in the base :mod:`scipy.optimize` namespace, these functions work
11
+ elementwise, enabling the solution of many related problems in an efficient,
12
+ vectorized call. Furthermore, when environment variable ``SCIPY_ARRAY_API=1``,
13
+ these functions can accept non-NumPy, array API standard compatible arrays and
14
+ perform all calculations using the corresponding array library (e.g. PyTorch,
15
+ JAX, CuPy).
16
+
17
+ Root finding
18
+ ============
19
+
20
+ .. autosummary::
21
+ :toctree: generated/
22
+
23
+ find_root
24
+ bracket_root
25
+
26
+ Minimization
27
+ ============
28
+
29
+ .. autosummary::
30
+ :toctree: generated/
31
+
32
+ find_minimum
33
+ bracket_minimum
34
+
35
+ """
36
+ from ._elementwise import find_root, find_minimum, bracket_root, bracket_minimum # noqa: F401, E501
37
+
38
+ __all__ = ["find_root", "find_minimum", "bracket_root", "bracket_minimum"]
loan-advisor-agent/.venv/Lib/site-packages/scipy/optimize/lbfgsb.py ADDED
@@ -0,0 +1,23 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # This file is not meant for public use and will be removed in SciPy v2.0.0.
2
+ # Use the `scipy.optimize` namespace for importing the functions
3
+ # included below.
4
+
5
+ from scipy._lib.deprecation import _sub_module_deprecation
6
+
7
+
8
+ __all__ = [ # noqa: F822
9
+ 'LbfgsInvHessProduct',
10
+ 'OptimizeResult',
11
+ 'fmin_l_bfgs_b',
12
+ 'zeros',
13
+ ]
14
+
15
+
16
+ def __dir__():
17
+ return __all__
18
+
19
+
20
+ def __getattr__(name):
21
+ return _sub_module_deprecation(sub_package="optimize", module="lbfgsb",
22
+ private_modules=["_lbfgsb_py"], all=__all__,
23
+ attribute=name)
loan-advisor-agent/.venv/Lib/site-packages/scipy/optimize/linesearch.py ADDED
@@ -0,0 +1,18 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # This file is not meant for public use and will be removed in SciPy v2.0.0.
2
+ # Use the `scipy.optimize` namespace for importing the functions
3
+ # included below.
4
+
5
+ from scipy._lib.deprecation import _sub_module_deprecation
6
+
7
+
8
+ __all__ = ["line_search"] # noqa: F822
9
+
10
+
11
+ def __dir__():
12
+ return __all__
13
+
14
+
15
+ def __getattr__(name):
16
+ return _sub_module_deprecation(sub_package="optimize", module="linesearch",
17
+ private_modules=["_linesearch"], all=__all__,
18
+ attribute=name)
loan-advisor-agent/.venv/Lib/site-packages/scipy/optimize/minpack.py ADDED
@@ -0,0 +1,27 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # This file is not meant for public use and will be removed in SciPy v2.0.0.
2
+ # Use the `scipy.optimize` namespace for importing the functions
3
+ # included below.
4
+
5
+ from scipy._lib.deprecation import _sub_module_deprecation
6
+
7
+
8
+ __all__ = [ # noqa: F822
9
+ 'OptimizeResult',
10
+ 'OptimizeWarning',
11
+ 'curve_fit',
12
+ 'fixed_point',
13
+ 'fsolve',
14
+ 'least_squares',
15
+ 'leastsq',
16
+ 'zeros',
17
+ ]
18
+
19
+
20
+ def __dir__():
21
+ return __all__
22
+
23
+
24
+ def __getattr__(name):
25
+ return _sub_module_deprecation(sub_package="optimize", module="minpack",
26
+ private_modules=["_minpack_py"], all=__all__,
27
+ attribute=name)
loan-advisor-agent/.venv/Lib/site-packages/scipy/optimize/minpack2.py ADDED
@@ -0,0 +1,17 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # This file is not meant for public use and will be removed in SciPy v2.0.0.
2
+ # Use the `scipy.optimize` namespace for importing the functions
3
+ # included below.
4
+
5
+ from scipy._lib.deprecation import _sub_module_deprecation
6
+
7
+ __all__: list[str] = []
8
+
9
+
10
+ def __dir__():
11
+ return __all__
12
+
13
+
14
+ def __getattr__(name):
15
+ return _sub_module_deprecation(sub_package="optimize", module="minpack2",
16
+ private_modules=["_minpack2"], all=__all__,
17
+ attribute=name)
loan-advisor-agent/.venv/Lib/site-packages/scipy/optimize/moduleTNC.py ADDED
@@ -0,0 +1,19 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # This file is not meant for public use and will be removed in SciPy v2.0.0.
2
+ # Use the `scipy.optimize` namespace for importing the functions
3
+ # included below.
4
+
5
+
6
+ from scipy._lib.deprecation import _sub_module_deprecation
7
+
8
+
9
+ __all__ = []
10
+
11
+
12
+ def __dir__():
13
+ return __all__
14
+
15
+
16
+ def __getattr__(name):
17
+ return _sub_module_deprecation(sub_package="optimize", module="moduleTNC",
18
+ private_modules=["_moduleTNC"], all=__all__,
19
+ attribute=name)
loan-advisor-agent/.venv/Lib/site-packages/scipy/optimize/nonlin.py ADDED
@@ -0,0 +1,29 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # This file is not meant for public use and will be removed in SciPy v2.0.0.
2
+ # Use the `scipy.optimize` namespace for importing the functions
3
+ # included below.
4
+
5
+ from scipy._lib.deprecation import _sub_module_deprecation
6
+
7
+
8
+ __all__ = [ # noqa: F822
9
+ 'BroydenFirst',
10
+ 'InverseJacobian',
11
+ 'KrylovJacobian',
12
+ 'anderson',
13
+ 'broyden1',
14
+ 'broyden2',
15
+ 'diagbroyden',
16
+ 'excitingmixing',
17
+ 'linearmixing',
18
+ 'newton_krylov',
19
+ ]
20
+
21
+
22
+ def __dir__():
23
+ return __all__
24
+
25
+
26
+ def __getattr__(name):
27
+ return _sub_module_deprecation(sub_package="optimize", module="nonlin",
28
+ private_modules=["_nonlin"], all=__all__,
29
+ attribute=name)
loan-advisor-agent/.venv/Lib/site-packages/scipy/optimize/optimize.py ADDED
@@ -0,0 +1,40 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # This file is not meant for public use and will be removed in SciPy v2.0.0.
2
+ # Use the `scipy.optimize` namespace for importing the functions
3
+ # included below.
4
+
5
+ from scipy._lib.deprecation import _sub_module_deprecation
6
+
7
+
8
+ __all__ = [ # noqa: F822
9
+ 'OptimizeResult',
10
+ 'OptimizeWarning',
11
+ 'approx_fprime',
12
+ 'bracket',
13
+ 'brent',
14
+ 'brute',
15
+ 'check_grad',
16
+ 'fmin',
17
+ 'fmin_bfgs',
18
+ 'fmin_cg',
19
+ 'fmin_ncg',
20
+ 'fmin_powell',
21
+ 'fminbound',
22
+ 'golden',
23
+ 'line_search',
24
+ 'rosen',
25
+ 'rosen_der',
26
+ 'rosen_hess',
27
+ 'rosen_hess_prod',
28
+ 'show_options',
29
+ 'zeros',
30
+ ]
31
+
32
+
33
+ def __dir__():
34
+ return __all__
35
+
36
+
37
+ def __getattr__(name):
38
+ return _sub_module_deprecation(sub_package="optimize", module="optimize",
39
+ private_modules=["_optimize"], all=__all__,
40
+ attribute=name)
loan-advisor-agent/.venv/Lib/site-packages/scipy/optimize/slsqp.py ADDED
@@ -0,0 +1,22 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # This file is not meant for public use and will be removed in SciPy v2.0.0.
2
+ # Use the `scipy.optimize` namespace for importing the functions
3
+ # included below.
4
+
5
+ from scipy._lib.deprecation import _sub_module_deprecation
6
+
7
+
8
+ __all__ = [ # noqa: F822
9
+ 'OptimizeResult',
10
+ 'fmin_slsqp',
11
+ 'slsqp',
12
+ ]
13
+
14
+
15
+ def __dir__():
16
+ return __all__
17
+
18
+
19
+ def __getattr__(name):
20
+ return _sub_module_deprecation(sub_package="optimize", module="slsqp",
21
+ private_modules=["_slsqp_py"], all=__all__,
22
+ attribute=name)
loan-advisor-agent/.venv/Lib/site-packages/scipy/optimize/tests/__init__.py ADDED
File without changes
loan-advisor-agent/.venv/Lib/site-packages/scipy/optimize/tests/_cython_examples/extending.pyx ADDED
@@ -0,0 +1,43 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python3
2
+ #cython: language_level=3
3
+ #cython: boundscheck=False
4
+ #cython: wraparound=False
5
+ """
6
+ Taken from docstring for scipy.optimize.cython_optimize module.
7
+ """
8
+
9
+ from scipy.optimize.cython_optimize cimport brentq
10
+
11
+ # import math from Cython
12
+ from libc cimport math
13
+
14
+ myargs = {'C0': 1.0, 'C1': 0.7} # a dictionary of extra arguments
15
+ XLO, XHI = 0.5, 1.0 # lower and upper search boundaries
16
+ XTOL, RTOL, MITR = 1e-3, 1e-3, 10 # other solver parameters
17
+
18
+ # user-defined struct for extra parameters
19
+ ctypedef struct test_params:
20
+ double C0
21
+ double C1
22
+
23
+
24
+ # user-defined callback
25
+ cdef double f(double x, void *args) noexcept:
26
+ cdef test_params *myargs = <test_params *> args
27
+ return myargs.C0 - math.exp(-(x - myargs.C1))
28
+
29
+
30
+ # Cython wrapper function
31
+ cdef double brentq_wrapper_example(dict args, double xa, double xb,
32
+ double xtol, double rtol, int mitr):
33
+ # Cython automatically casts dictionary to struct
34
+ cdef test_params myargs = args
35
+ return brentq(
36
+ f, xa, xb, <test_params *> &myargs, xtol, rtol, mitr, NULL)
37
+
38
+
39
+ # Python function
40
+ def brentq_example(args=myargs, xa=XLO, xb=XHI, xtol=XTOL, rtol=RTOL,
41
+ mitr=MITR):
42
+ '''Calls Cython wrapper from Python.'''
43
+ return brentq_wrapper_example(args, xa, xb, xtol, rtol, mitr)
loan-advisor-agent/.venv/Lib/site-packages/scipy/optimize/tests/_cython_examples/meson.build ADDED
@@ -0,0 +1,32 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ project('random-build-examples', 'c', 'cpp', 'cython')
2
+
3
+ fs = import('fs')
4
+
5
+ py3 = import('python').find_installation(pure: false)
6
+
7
+ cy = meson.get_compiler('cython')
8
+
9
+ if not cy.version().version_compare('>=3.0.8')
10
+ error('tests requires Cython >= 3.0.8')
11
+ endif
12
+
13
+ cython_args = []
14
+ if cy.version().version_compare('>=3.1.0')
15
+ cython_args += ['-Xfreethreading_compatible=True']
16
+ endif
17
+
18
+ py3.extension_module(
19
+ 'extending',
20
+ 'extending.pyx',
21
+ cython_args: cython_args,
22
+ install: false,
23
+ )
24
+
25
+ extending_cpp = fs.copyfile('extending.pyx', 'extending_cpp.pyx')
26
+ py3.extension_module(
27
+ 'extending_cpp',
28
+ extending_cpp,
29
+ cython_args: cython_args,
30
+ install: false,
31
+ override_options : ['cython_language=cpp']
32
+ )
loan-advisor-agent/.venv/Lib/site-packages/scipy/optimize/tests/test__basinhopping.py ADDED
@@ -0,0 +1,537 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Unit tests for the basin hopping global minimization algorithm.
3
+ """
4
+ import copy
5
+
6
+ from numpy.testing import (assert_almost_equal, assert_equal, assert_,
7
+ assert_allclose)
8
+ import pytest
9
+ from pytest import raises as assert_raises
10
+ import numpy as np
11
+ from numpy import cos, sin
12
+
13
+ from scipy.optimize import basinhopping, OptimizeResult
14
+ from scipy.optimize._basinhopping import (
15
+ Storage, RandomDisplacement, Metropolis, AdaptiveStepsize)
16
+
17
+
18
+ def func1d(x):
19
+ f = cos(14.5 * x - 0.3) + (x + 0.2) * x
20
+ df = np.array(-14.5 * sin(14.5 * x - 0.3) + 2. * x + 0.2)
21
+ return f, df
22
+
23
+
24
+ def func2d_nograd(x):
25
+ f = cos(14.5 * x[0] - 0.3) + (x[1] + 0.2) * x[1] + (x[0] + 0.2) * x[0]
26
+ return f
27
+
28
+
29
+ def func2d(x):
30
+ f = cos(14.5 * x[0] - 0.3) + (x[1] + 0.2) * x[1] + (x[0] + 0.2) * x[0]
31
+ df = np.zeros(2)
32
+ df[0] = -14.5 * sin(14.5 * x[0] - 0.3) + 2. * x[0] + 0.2
33
+ df[1] = 2. * x[1] + 0.2
34
+ return f, df
35
+
36
+
37
+ def func2d_easyderiv(x):
38
+ f = 2.0*x[0]**2 + 2.0*x[0]*x[1] + 2.0*x[1]**2 - 6.0*x[0]
39
+ df = np.zeros(2)
40
+ df[0] = 4.0*x[0] + 2.0*x[1] - 6.0
41
+ df[1] = 2.0*x[0] + 4.0*x[1]
42
+
43
+ return f, df
44
+
45
+
46
+ class MyTakeStep1(RandomDisplacement):
47
+ """use a copy of displace, but have it set a special parameter to
48
+ make sure it's actually being used."""
49
+ def __init__(self):
50
+ self.been_called = False
51
+ super().__init__()
52
+
53
+ def __call__(self, x):
54
+ self.been_called = True
55
+ return super().__call__(x)
56
+
57
+
58
+ def myTakeStep2(x):
59
+ """redo RandomDisplacement in function form without the attribute stepsize
60
+ to make sure everything still works ok
61
+ """
62
+ s = 0.5
63
+ x += np.random.uniform(-s, s, np.shape(x))
64
+ return x
65
+
66
+
67
+ class MyAcceptTest:
68
+ """pass a custom accept test
69
+
70
+ This does nothing but make sure it's being used and ensure all the
71
+ possible return values are accepted
72
+ """
73
+ def __init__(self):
74
+ self.been_called = False
75
+ self.ncalls = 0
76
+ self.testres = [False, 'force accept', True, np.bool_(True),
77
+ np.bool_(False), [], {}, 0, 1]
78
+
79
+ def __call__(self, **kwargs):
80
+ self.been_called = True
81
+ self.ncalls += 1
82
+ if self.ncalls - 1 < len(self.testres):
83
+ return self.testres[self.ncalls - 1]
84
+ else:
85
+ return True
86
+
87
+
88
+ class MyCallBack:
89
+ """pass a custom callback function
90
+
91
+ This makes sure it's being used. It also returns True after 10
92
+ steps to ensure that it's stopping early.
93
+
94
+ """
95
+ def __init__(self):
96
+ self.been_called = False
97
+ self.ncalls = 0
98
+
99
+ def __call__(self, x, f, accepted):
100
+ self.been_called = True
101
+ self.ncalls += 1
102
+ if self.ncalls == 10:
103
+ return True
104
+
105
+
106
+ class TestBasinHopping:
107
+
108
+ def setup_method(self):
109
+ """ Tests setup.
110
+
111
+ Run tests based on the 1-D and 2-D functions described above.
112
+ """
113
+ self.x0 = (1.0, [1.0, 1.0])
114
+ self.sol = (-0.195, np.array([-0.195, -0.1]))
115
+
116
+ self.tol = 3 # number of decimal places
117
+
118
+ self.niter = 100
119
+ self.disp = False
120
+
121
+ self.kwargs = {"method": "L-BFGS-B", "jac": True}
122
+ self.kwargs_nograd = {"method": "L-BFGS-B"}
123
+
124
+ def test_TypeError(self):
125
+ # test the TypeErrors are raised on bad input
126
+ i = 1
127
+ # if take_step is passed, it must be callable
128
+ assert_raises(TypeError, basinhopping, func2d, self.x0[i],
129
+ take_step=1)
130
+ # if accept_test is passed, it must be callable
131
+ assert_raises(TypeError, basinhopping, func2d, self.x0[i],
132
+ accept_test=1)
133
+
134
+ def test_input_validation(self):
135
+ msg = 'target_accept_rate has to be in range \\(0, 1\\)'
136
+ with assert_raises(ValueError, match=msg):
137
+ basinhopping(func1d, self.x0[0], target_accept_rate=0.)
138
+ with assert_raises(ValueError, match=msg):
139
+ basinhopping(func1d, self.x0[0], target_accept_rate=1.)
140
+
141
+ msg = 'stepwise_factor has to be in range \\(0, 1\\)'
142
+ with assert_raises(ValueError, match=msg):
143
+ basinhopping(func1d, self.x0[0], stepwise_factor=0.)
144
+ with assert_raises(ValueError, match=msg):
145
+ basinhopping(func1d, self.x0[0], stepwise_factor=1.)
146
+
147
+ def test_1d_grad(self):
148
+ # test 1-D minimizations with gradient
149
+ i = 0
150
+ res = basinhopping(func1d, self.x0[i], minimizer_kwargs=self.kwargs,
151
+ niter=self.niter, disp=self.disp)
152
+ assert_almost_equal(res.x, self.sol[i], self.tol)
153
+
154
+ def test_2d(self):
155
+ # test 2d minimizations with gradient
156
+ i = 1
157
+ res = basinhopping(func2d, self.x0[i], minimizer_kwargs=self.kwargs,
158
+ niter=self.niter, disp=self.disp)
159
+ assert_almost_equal(res.x, self.sol[i], self.tol)
160
+ assert_(res.nfev > 0)
161
+
162
+ def test_njev(self):
163
+ # test njev is returned correctly
164
+ i = 1
165
+ minimizer_kwargs = self.kwargs.copy()
166
+ # L-BFGS-B doesn't use njev, but BFGS does
167
+ minimizer_kwargs["method"] = "BFGS"
168
+ res = basinhopping(func2d, self.x0[i],
169
+ minimizer_kwargs=minimizer_kwargs, niter=self.niter,
170
+ disp=self.disp)
171
+ assert_(res.nfev > 0)
172
+ assert_equal(res.nfev, res.njev)
173
+
174
+ def test_jac(self):
175
+ # test Jacobian returned
176
+ minimizer_kwargs = self.kwargs.copy()
177
+ # BFGS returns a Jacobian
178
+ minimizer_kwargs["method"] = "BFGS"
179
+
180
+ res = basinhopping(func2d_easyderiv, [0.0, 0.0],
181
+ minimizer_kwargs=minimizer_kwargs, niter=self.niter,
182
+ disp=self.disp)
183
+
184
+ assert_(hasattr(res.lowest_optimization_result, "jac"))
185
+
186
+ # in this case, the Jacobian is just [df/dx, df/dy]
187
+ _, jacobian = func2d_easyderiv(res.x)
188
+ assert_almost_equal(res.lowest_optimization_result.jac, jacobian,
189
+ self.tol)
190
+
191
+ def test_2d_nograd(self):
192
+ # test 2-D minimizations without gradient
193
+ i = 1
194
+ res = basinhopping(func2d_nograd, self.x0[i],
195
+ minimizer_kwargs=self.kwargs_nograd,
196
+ niter=self.niter, disp=self.disp)
197
+ assert_almost_equal(res.x, self.sol[i], self.tol)
198
+
199
+ @pytest.mark.fail_slow(10)
200
+ def test_all_minimizers(self):
201
+ # Test 2-D minimizations with gradient. Nelder-Mead, Powell, COBYLA, and
202
+ # COBYQA don't accept jac=True, so aren't included here.
203
+ i = 1
204
+ methods = ['CG', 'BFGS', 'Newton-CG', 'L-BFGS-B', 'TNC', 'SLSQP']
205
+ minimizer_kwargs = copy.copy(self.kwargs)
206
+ for method in methods:
207
+ minimizer_kwargs["method"] = method
208
+ res = basinhopping(func2d, self.x0[i],
209
+ minimizer_kwargs=minimizer_kwargs,
210
+ niter=self.niter, disp=self.disp)
211
+ assert_almost_equal(res.x, self.sol[i], self.tol)
212
+
213
+ @pytest.mark.fail_slow(40)
214
+ @pytest.mark.parametrize("method", [
215
+ 'CG', 'BFGS', 'L-BFGS-B', 'TNC', 'SLSQP',
216
+ 'Nelder-Mead', 'Powell', 'COBYLA', 'COBYQA'])
217
+ def test_all_nograd_minimizers(self, method):
218
+ # Test 2-D minimizations without gradient. Newton-CG requires jac=True,
219
+ # so not included here.
220
+ i = 1
221
+ minimizer_kwargs = self.kwargs_nograd.copy()
222
+ minimizer_kwargs["method"] = method
223
+ # These methods take extensive amount of time on this problem
224
+ niter = 10 if method in ('COBYLA', 'COBYQA') else self.niter
225
+
226
+ res = basinhopping(func2d_nograd, self.x0[i],
227
+ minimizer_kwargs=minimizer_kwargs,
228
+ niter=niter, disp=self.disp, seed=1234)
229
+
230
+ tol = 2 if method == 'COBYLA' else self.tol
231
+ assert_almost_equal(res.x, self.sol[i], decimal=tol)
232
+
233
+ def test_pass_takestep(self):
234
+ # test that passing a custom takestep works
235
+ # also test that the stepsize is being adjusted
236
+ takestep = MyTakeStep1()
237
+ initial_step_size = takestep.stepsize
238
+ i = 1
239
+ res = basinhopping(func2d, self.x0[i], minimizer_kwargs=self.kwargs,
240
+ niter=self.niter, disp=self.disp,
241
+ take_step=takestep)
242
+ assert_almost_equal(res.x, self.sol[i], self.tol)
243
+ assert_(takestep.been_called)
244
+ # make sure that the build in adaptive step size has been used
245
+ assert_(initial_step_size != takestep.stepsize)
246
+
247
+ def test_pass_simple_takestep(self):
248
+ # test that passing a custom takestep without attribute stepsize
249
+ takestep = myTakeStep2
250
+ i = 1
251
+ res = basinhopping(func2d_nograd, self.x0[i],
252
+ minimizer_kwargs=self.kwargs_nograd,
253
+ niter=self.niter, disp=self.disp,
254
+ take_step=takestep)
255
+ assert_almost_equal(res.x, self.sol[i], self.tol)
256
+
257
+ def test_pass_accept_test(self):
258
+ # test passing a custom accept test
259
+ # makes sure it's being used and ensures all the possible return values
260
+ # are accepted.
261
+ accept_test = MyAcceptTest()
262
+ i = 1
263
+ # there's no point in running it more than a few steps.
264
+ basinhopping(func2d, self.x0[i], minimizer_kwargs=self.kwargs,
265
+ niter=10, disp=self.disp, accept_test=accept_test)
266
+ assert_(accept_test.been_called)
267
+
268
+ def test_pass_callback(self):
269
+ # test passing a custom callback function
270
+ # This makes sure it's being used. It also returns True after 10 steps
271
+ # to ensure that it's stopping early.
272
+ callback = MyCallBack()
273
+ i = 1
274
+ # there's no point in running it more than a few steps.
275
+ res = basinhopping(func2d, self.x0[i], minimizer_kwargs=self.kwargs,
276
+ niter=30, disp=self.disp, callback=callback)
277
+ assert_(callback.been_called)
278
+ assert_("callback" in res.message[0])
279
+ # One of the calls of MyCallBack is during BasinHoppingRunner
280
+ # construction, so there are only 9 remaining before MyCallBack stops
281
+ # the minimization.
282
+ assert_equal(res.nit, 9)
283
+
284
+ def test_minimizer_fail(self):
285
+ # test if a minimizer fails
286
+ i = 1
287
+ self.kwargs["options"] = dict(maxiter=0)
288
+ niter = 10
289
+ res = basinhopping(func2d, self.x0[i], minimizer_kwargs=self.kwargs,
290
+ niter=niter, disp=self.disp)
291
+ # the number of failed minimizations should be the number of
292
+ # iterations + 1
293
+ assert_equal(res.nit + 1, res.minimization_failures)
294
+
295
+ def test_niter_zero(self):
296
+ # gh5915, what happens if you call basinhopping with niter=0
297
+ i = 0
298
+ basinhopping(func1d, self.x0[i], minimizer_kwargs=self.kwargs,
299
+ niter=0, disp=self.disp)
300
+
301
+ def test_rng_reproducibility(self):
302
+ # rng should ensure reproducibility between runs
303
+ minimizer_kwargs = {"method": "L-BFGS-B", "jac": True}
304
+
305
+ f_1 = []
306
+
307
+ def callback(x, f, accepted):
308
+ f_1.append(f)
309
+
310
+ basinhopping(func2d, [1.0, 1.0], minimizer_kwargs=minimizer_kwargs,
311
+ niter=10, callback=callback, rng=10)
312
+
313
+ f_2 = []
314
+
315
+ def callback2(x, f, accepted):
316
+ f_2.append(f)
317
+
318
+ basinhopping(func2d, [1.0, 1.0], minimizer_kwargs=minimizer_kwargs,
319
+ niter=10, callback=callback2, rng=10)
320
+ assert_equal(np.array(f_1), np.array(f_2))
321
+
322
+ def test_random_gen(self):
323
+ # check that np.random.Generator can be used (numpy >= 1.17)
324
+ rng = np.random.default_rng(1)
325
+
326
+ minimizer_kwargs = {"method": "L-BFGS-B", "jac": True}
327
+
328
+ res1 = basinhopping(func2d, [1.0, 1.0],
329
+ minimizer_kwargs=minimizer_kwargs,
330
+ niter=10, rng=rng)
331
+
332
+ rng = np.random.default_rng(1)
333
+ res2 = basinhopping(func2d, [1.0, 1.0],
334
+ minimizer_kwargs=minimizer_kwargs,
335
+ niter=10, rng=rng)
336
+ assert_equal(res1.x, res2.x)
337
+
338
+ def test_monotonic_basin_hopping(self):
339
+ # test 1-D minimizations with gradient and T=0
340
+ i = 0
341
+
342
+ res = basinhopping(func1d, self.x0[i], minimizer_kwargs=self.kwargs,
343
+ niter=self.niter, disp=self.disp, T=0)
344
+ assert_almost_equal(res.x, self.sol[i], self.tol)
345
+
346
+
347
+ class Test_Storage:
348
+ def create_storage(self):
349
+ self.x0 = np.array(1)
350
+ self.f0 = 0
351
+
352
+ minres = OptimizeResult(success=True)
353
+ minres.x = self.x0
354
+ minres.fun = self.f0
355
+
356
+ return Storage(minres)
357
+
358
+ def test_higher_f_rejected(self):
359
+ storage = self.create_storage()
360
+ new_minres = OptimizeResult(success=True)
361
+ new_minres.x = self.x0 + 1
362
+ new_minres.fun = self.f0 + 1
363
+
364
+ ret = storage.update(new_minres)
365
+ minres = storage.get_lowest()
366
+ assert_equal(self.x0, minres.x)
367
+ assert_equal(self.f0, minres.fun)
368
+ assert_(not ret)
369
+
370
+ @pytest.mark.parametrize('success', [True, False])
371
+ def test_lower_f_accepted(self, success):
372
+ storage = self.create_storage()
373
+ new_minres = OptimizeResult(success=success)
374
+ new_minres.x = self.x0 + 1
375
+ new_minres.fun = self.f0 - 1
376
+
377
+ ret = storage.update(new_minres)
378
+ minres = storage.get_lowest()
379
+ assert (self.x0 != minres.x) == success # can't use `is`
380
+ assert (self.f0 != minres.fun) == success # left side is NumPy bool
381
+ assert ret is success
382
+
383
+
384
+ class Test_RandomDisplacement:
385
+ def setup_method(self):
386
+ self.stepsize = 1.0
387
+ self.N = 300000
388
+
389
+ def test_random(self):
390
+ # the mean should be 0
391
+ # the variance should be (2*stepsize)**2 / 12
392
+ # note these tests are random, they will fail from time to time
393
+ rng = np.random.RandomState(0)
394
+ x0 = np.zeros([self.N])
395
+ displace = RandomDisplacement(stepsize=self.stepsize, rng=rng)
396
+ x = displace(x0)
397
+ v = (2. * self.stepsize) ** 2 / 12
398
+ assert_almost_equal(np.mean(x), 0., 1)
399
+ assert_almost_equal(np.var(x), v, 1)
400
+
401
+
402
+ class Test_Metropolis:
403
+ def setup_method(self):
404
+ self.T = 2.
405
+ self.met = Metropolis(self.T)
406
+ self.res_new = OptimizeResult(success=True, fun=0.)
407
+ self.res_old = OptimizeResult(success=True, fun=1.)
408
+
409
+ def test_boolean_return(self):
410
+ # the return must be a bool, else an error will be raised in
411
+ # basinhopping
412
+ ret = self.met(res_new=self.res_new, res_old=self.res_old)
413
+ assert isinstance(ret, bool)
414
+
415
+ def test_lower_f_accepted(self):
416
+ assert_(self.met(res_new=self.res_new, res_old=self.res_old))
417
+
418
+ def test_accept(self):
419
+ # test that steps are randomly accepted for f_new > f_old
420
+ one_accept = False
421
+ one_reject = False
422
+ for i in range(1000):
423
+ if one_accept and one_reject:
424
+ break
425
+ res_new = OptimizeResult(success=True, fun=1.)
426
+ res_old = OptimizeResult(success=True, fun=0.5)
427
+ ret = self.met(res_new=res_new, res_old=res_old)
428
+ if ret:
429
+ one_accept = True
430
+ else:
431
+ one_reject = True
432
+ assert_(one_accept)
433
+ assert_(one_reject)
434
+
435
+ def test_GH7495(self):
436
+ # an overflow in exp was producing a RuntimeWarning
437
+ # create own object here in case someone changes self.T
438
+ met = Metropolis(2)
439
+ res_new = OptimizeResult(success=True, fun=0.)
440
+ res_old = OptimizeResult(success=True, fun=2000)
441
+ with np.errstate(over='raise'):
442
+ met.accept_reject(res_new=res_new, res_old=res_old)
443
+
444
+ def test_gh7799(self):
445
+ # gh-7799 reported a problem in which local search was successful but
446
+ # basinhopping returned an invalid solution. Show that this is fixed.
447
+ def func(x):
448
+ return (x**2-8)**2+(x+2)**2
449
+
450
+ x0 = -4
451
+ limit = 50 # Constrain to func value >= 50
452
+ con = {'type': 'ineq', 'fun': lambda x: func(x) - limit},
453
+ res = basinhopping(
454
+ func,
455
+ x0,
456
+ 30,
457
+ seed=np.random.RandomState(1234),
458
+ minimizer_kwargs={'constraints': con}
459
+ )
460
+ assert res.success
461
+ assert_allclose(res.fun, limit, rtol=1e-6)
462
+
463
+ def test_accept_gh7799(self):
464
+ # Metropolis should not accept the result of an unsuccessful new local
465
+ # search if the old local search was successful
466
+
467
+ met = Metropolis(0) # monotonic basin hopping
468
+ res_new = OptimizeResult(success=True, fun=0.)
469
+ res_old = OptimizeResult(success=True, fun=1.)
470
+
471
+ # if new local search was successful and energy is lower, accept
472
+ assert met(res_new=res_new, res_old=res_old)
473
+ # if new res is unsuccessful, don't accept - even if energy is lower
474
+ res_new.success = False
475
+ assert not met(res_new=res_new, res_old=res_old)
476
+ # ...unless the old res was unsuccessful, too. In that case, why not?
477
+ res_old.success = False
478
+ assert met(res_new=res_new, res_old=res_old)
479
+
480
+ def test_reject_all_gh7799(self):
481
+ # Test the behavior when there is no feasible solution
482
+ def fun(x):
483
+ return x@x
484
+
485
+ def constraint(x):
486
+ return x + 1
487
+
488
+ kwargs = {'constraints': {'type': 'eq', 'fun': constraint},
489
+ 'bounds': [(0, 1), (0, 1)], 'method': 'slsqp'}
490
+ res = basinhopping(fun, x0=[2, 3], niter=10, minimizer_kwargs=kwargs)
491
+ assert not res.success
492
+
493
+
494
+ @pytest.mark.thread_unsafe(reason="shared state")
495
+ class Test_AdaptiveStepsize:
496
+ def setup_method(self):
497
+ self.stepsize = 1.
498
+ self.ts = RandomDisplacement(stepsize=self.stepsize)
499
+ self.target_accept_rate = 0.5
500
+ self.takestep = AdaptiveStepsize(takestep=self.ts, verbose=False,
501
+ accept_rate=self.target_accept_rate)
502
+
503
+ def test_adaptive_increase(self):
504
+ # if few steps are rejected, the stepsize should increase
505
+ x = 0.
506
+ self.takestep(x)
507
+ self.takestep.report(False)
508
+ for i in range(self.takestep.interval):
509
+ self.takestep(x)
510
+ self.takestep.report(True)
511
+ assert_(self.ts.stepsize > self.stepsize)
512
+
513
+ def test_adaptive_decrease(self):
514
+ # if few steps are rejected, the stepsize should increase
515
+ x = 0.
516
+ self.takestep(x)
517
+ self.takestep.report(True)
518
+ for i in range(self.takestep.interval):
519
+ self.takestep(x)
520
+ self.takestep.report(False)
521
+ assert_(self.ts.stepsize < self.stepsize)
522
+
523
+ def test_all_accepted(self):
524
+ # test that everything works OK if all steps were accepted
525
+ x = 0.
526
+ for i in range(self.takestep.interval + 1):
527
+ self.takestep(x)
528
+ self.takestep.report(True)
529
+ assert_(self.ts.stepsize > self.stepsize)
530
+
531
+ def test_all_rejected(self):
532
+ # test that everything works OK if all steps were rejected
533
+ x = 0.
534
+ for i in range(self.takestep.interval + 1):
535
+ self.takestep(x)
536
+ self.takestep.report(False)
537
+ assert_(self.ts.stepsize < self.stepsize)
loan-advisor-agent/.venv/Lib/site-packages/scipy/optimize/tests/test__differential_evolution.py ADDED
@@ -0,0 +1,1758 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Unit tests for the differential global minimization algorithm.
3
+ """
4
+ from multiprocessing.dummy import Pool as ThreadPool
5
+ import platform
6
+ import warnings
7
+
8
+ from scipy._lib._gcutils import assert_deallocated
9
+ from scipy.optimize._differentialevolution import (DifferentialEvolutionSolver,
10
+ _ConstraintWrapper)
11
+ from scipy.optimize import differential_evolution, OptimizeResult
12
+ from scipy.optimize._constraints import (Bounds, NonlinearConstraint,
13
+ LinearConstraint)
14
+ from scipy.optimize import rosen, minimize, rosen_der
15
+ from scipy.sparse import csr_array
16
+ from scipy import stats
17
+
18
+ import numpy as np
19
+ from numpy.testing import (assert_equal, assert_allclose, assert_almost_equal,
20
+ assert_string_equal, assert_)
21
+ from pytest import raises as assert_raises, warns
22
+ import pytest
23
+
24
+
25
+ class TestDifferentialEvolutionSolver:
26
+
27
+ def setup_method(self):
28
+ self.old_seterr = np.seterr(invalid='raise')
29
+ self.limits = np.array([[0., 0.],
30
+ [2., 2.]])
31
+ self.bounds = [(0., 2.), (0., 2.)]
32
+
33
+ self.dummy_solver = DifferentialEvolutionSolver(self.quadratic,
34
+ [(0, 100)])
35
+
36
+ # dummy_solver2 will be used to test mutation strategies
37
+ self.dummy_solver2 = DifferentialEvolutionSolver(self.quadratic,
38
+ [(0, 1)],
39
+ popsize=7,
40
+ mutation=0.5)
41
+ # create a population that's only 7 members long
42
+ # [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7]
43
+ population = np.atleast_2d(np.arange(0.1, 0.8, 0.1)).T
44
+ self.dummy_solver2.population = population
45
+
46
+ def teardown_method(self):
47
+ np.seterr(**self.old_seterr)
48
+
49
+ def quadratic(self, x):
50
+ return x[0]**2
51
+
52
+ def test__strategy_resolves(self):
53
+ # test that the correct mutation function is resolved by
54
+ # different requested strategy arguments
55
+ solver = DifferentialEvolutionSolver(rosen,
56
+ self.bounds,
57
+ strategy='best1exp')
58
+ assert_equal(solver.strategy, 'best1exp')
59
+ assert_equal(solver.mutation_func.__name__, '_best1')
60
+
61
+ solver = DifferentialEvolutionSolver(rosen,
62
+ self.bounds,
63
+ strategy='best1bin')
64
+ assert_equal(solver.strategy, 'best1bin')
65
+ assert_equal(solver.mutation_func.__name__, '_best1')
66
+
67
+ solver = DifferentialEvolutionSolver(rosen,
68
+ self.bounds,
69
+ strategy='rand1bin')
70
+ assert_equal(solver.strategy, 'rand1bin')
71
+ assert_equal(solver.mutation_func.__name__, '_rand1')
72
+
73
+ solver = DifferentialEvolutionSolver(rosen,
74
+ self.bounds,
75
+ strategy='rand1exp')
76
+ assert_equal(solver.strategy, 'rand1exp')
77
+ assert_equal(solver.mutation_func.__name__, '_rand1')
78
+
79
+ solver = DifferentialEvolutionSolver(rosen,
80
+ self.bounds,
81
+ strategy='rand2exp')
82
+ assert_equal(solver.strategy, 'rand2exp')
83
+ assert_equal(solver.mutation_func.__name__, '_rand2')
84
+
85
+ solver = DifferentialEvolutionSolver(rosen,
86
+ self.bounds,
87
+ strategy='best2bin')
88
+ assert_equal(solver.strategy, 'best2bin')
89
+ assert_equal(solver.mutation_func.__name__, '_best2')
90
+
91
+ solver = DifferentialEvolutionSolver(rosen,
92
+ self.bounds,
93
+ strategy='rand2bin')
94
+ assert_equal(solver.strategy, 'rand2bin')
95
+ assert_equal(solver.mutation_func.__name__, '_rand2')
96
+
97
+ solver = DifferentialEvolutionSolver(rosen,
98
+ self.bounds,
99
+ strategy='rand2exp')
100
+ assert_equal(solver.strategy, 'rand2exp')
101
+ assert_equal(solver.mutation_func.__name__, '_rand2')
102
+
103
+ solver = DifferentialEvolutionSolver(rosen,
104
+ self.bounds,
105
+ strategy='randtobest1bin')
106
+ assert_equal(solver.strategy, 'randtobest1bin')
107
+ assert_equal(solver.mutation_func.__name__, '_randtobest1')
108
+
109
+ solver = DifferentialEvolutionSolver(rosen,
110
+ self.bounds,
111
+ strategy='randtobest1exp')
112
+ assert_equal(solver.strategy, 'randtobest1exp')
113
+ assert_equal(solver.mutation_func.__name__, '_randtobest1')
114
+
115
+ solver = DifferentialEvolutionSolver(rosen,
116
+ self.bounds,
117
+ strategy='currenttobest1bin')
118
+ assert_equal(solver.strategy, 'currenttobest1bin')
119
+ assert_equal(solver.mutation_func.__name__, '_currenttobest1')
120
+
121
+ solver = DifferentialEvolutionSolver(rosen,
122
+ self.bounds,
123
+ strategy='currenttobest1exp')
124
+ assert_equal(solver.strategy, 'currenttobest1exp')
125
+ assert_equal(solver.mutation_func.__name__, '_currenttobest1')
126
+
127
+ def test__mutate1(self):
128
+ # strategies */1/*, i.e. rand/1/bin, best/1/exp, etc.
129
+ result = np.array([0.05])
130
+ trial = self.dummy_solver2._best1(np.array([2, 3, 4, 5, 6]))
131
+ assert_allclose(trial, result)
132
+
133
+ result = np.array([0.25])
134
+ trial = self.dummy_solver2._rand1(np.array([2, 3, 4, 5, 6]))
135
+ assert_allclose(trial, result)
136
+
137
+ def test__mutate2(self):
138
+ # strategies */2/*, i.e. rand/2/bin, best/2/exp, etc.
139
+ # [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7]
140
+
141
+ result = np.array([-0.1])
142
+ trial = self.dummy_solver2._best2(np.array([2, 3, 4, 5, 6]))
143
+ assert_allclose(trial, result)
144
+
145
+ result = np.array([0.1])
146
+ trial = self.dummy_solver2._rand2(np.array([2, 3, 4, 5, 6]))
147
+ assert_allclose(trial, result)
148
+
149
+ def test__randtobest1(self):
150
+ # strategies randtobest/1/*
151
+ result = np.array([0.15])
152
+ trial = self.dummy_solver2._randtobest1(np.array([2, 3, 4, 5, 6]))
153
+ assert_allclose(trial, result)
154
+
155
+ def test__currenttobest1(self):
156
+ # strategies currenttobest/1/*
157
+ result = np.array([0.1])
158
+ trial = self.dummy_solver2._currenttobest1(
159
+ 1,
160
+ np.array([2, 3, 4, 5, 6])
161
+ )
162
+ assert_allclose(trial, result)
163
+
164
+ def test_can_init_with_dithering(self):
165
+ mutation = (0.5, 1)
166
+ solver = DifferentialEvolutionSolver(self.quadratic,
167
+ self.bounds,
168
+ mutation=mutation)
169
+
170
+ assert_equal(solver.dither, list(mutation))
171
+
172
+ def test_invalid_mutation_values_arent_accepted(self):
173
+ func = rosen
174
+ mutation = (0.5, 3)
175
+ assert_raises(ValueError,
176
+ DifferentialEvolutionSolver,
177
+ func,
178
+ self.bounds,
179
+ mutation=mutation)
180
+
181
+ mutation = (-1, 1)
182
+ assert_raises(ValueError,
183
+ DifferentialEvolutionSolver,
184
+ func,
185
+ self.bounds,
186
+ mutation=mutation)
187
+
188
+ mutation = (0.1, np.nan)
189
+ assert_raises(ValueError,
190
+ DifferentialEvolutionSolver,
191
+ func,
192
+ self.bounds,
193
+ mutation=mutation)
194
+
195
+ mutation = 0.5
196
+ solver = DifferentialEvolutionSolver(func,
197
+ self.bounds,
198
+ mutation=mutation)
199
+ assert_equal(0.5, solver.scale)
200
+ assert_equal(None, solver.dither)
201
+
202
+ def test_invalid_functional(self):
203
+ def func(x):
204
+ return np.array([np.sum(x ** 2), np.sum(x)])
205
+
206
+ with assert_raises(
207
+ RuntimeError,
208
+ match=r"func\(x, \*args\) must return a scalar value"):
209
+ differential_evolution(func, [(-2, 2), (-2, 2)])
210
+
211
+ def test__scale_parameters(self):
212
+ trial = np.array([0.3])
213
+ assert_equal(30, self.dummy_solver._scale_parameters(trial))
214
+
215
+ # it should also work with the limits reversed
216
+ self.dummy_solver.limits = np.array([[100], [0.]])
217
+ assert_equal(30, self.dummy_solver._scale_parameters(trial))
218
+
219
+ def test__unscale_parameters(self):
220
+ trial = np.array([30])
221
+ assert_equal(0.3, self.dummy_solver._unscale_parameters(trial))
222
+
223
+ # it should also work with the limits reversed
224
+ self.dummy_solver.limits = np.array([[100], [0.]])
225
+ assert_equal(0.3, self.dummy_solver._unscale_parameters(trial))
226
+
227
+ def test_equal_bounds(self):
228
+ with np.errstate(invalid='raise'):
229
+ solver = DifferentialEvolutionSolver(
230
+ self.quadratic,
231
+ bounds=[(2.0, 2.0), (1.0, 3.0)]
232
+ )
233
+ v = solver._unscale_parameters([2.0, 2.0])
234
+ assert_allclose(v, 0.5)
235
+
236
+ res = differential_evolution(self.quadratic, [(2.0, 2.0), (3.0, 3.0)])
237
+ assert_equal(res.x, [2.0, 3.0])
238
+
239
+ def test__ensure_constraint(self):
240
+ trial = np.array([1.1, -100, 0.9, 2., 300., -0.00001])
241
+ self.dummy_solver._ensure_constraint(trial)
242
+
243
+ assert_equal(trial[2], 0.9)
244
+ assert_(np.logical_and(trial >= 0, trial <= 1).all())
245
+
246
+ def test_differential_evolution(self):
247
+ # test that the Jmin of DifferentialEvolutionSolver
248
+ # is the same as the function evaluation
249
+ solver = DifferentialEvolutionSolver(
250
+ self.quadratic, [(-2, 2)], maxiter=1, polish=False
251
+ )
252
+ result = solver.solve()
253
+ assert_equal(result.fun, self.quadratic(result.x))
254
+
255
+ solver = DifferentialEvolutionSolver(
256
+ self.quadratic, [(-2, 2)], maxiter=1, polish=True
257
+ )
258
+ result = solver.solve()
259
+ assert_equal(result.fun, self.quadratic(result.x))
260
+
261
+ def test_best_solution_retrieval(self):
262
+ # test that the getter property method for the best solution works.
263
+ solver = DifferentialEvolutionSolver(self.quadratic, [(-2, 2)])
264
+ result = solver.solve()
265
+ assert_allclose(result.x, solver.x, atol=1e-15, rtol=0)
266
+
267
+ def test_intermediate_result(self):
268
+ # Check that intermediate result object passed into the callback
269
+ # function contains the expected information and that raising
270
+ # `StopIteration` causes the expected behavior.
271
+ maxiter = 10
272
+
273
+ def func(x):
274
+ val = rosen(x)
275
+ if val < func.val:
276
+ func.x = x
277
+ func.val = val
278
+ return val
279
+ func.x = None
280
+ func.val = np.inf
281
+
282
+ def callback(intermediate_result):
283
+ callback.nit += 1
284
+ callback.intermediate_result = intermediate_result
285
+ assert intermediate_result.population.ndim == 2
286
+ assert intermediate_result.population.shape[1] == 2
287
+ assert intermediate_result.nit == callback.nit
288
+
289
+ # Check that `x` and `fun` attributes are the best found so far
290
+ assert_equal(intermediate_result.x, callback.func.x)
291
+ assert_equal(intermediate_result.fun, callback.func.val)
292
+
293
+ # Check for consistency between `fun`, `population_energies`,
294
+ # `x`, and `population`
295
+ assert_equal(intermediate_result.fun, rosen(intermediate_result.x))
296
+ for i in range(len(intermediate_result.population_energies)):
297
+ res = intermediate_result.population_energies[i]
298
+ ref = rosen(intermediate_result.population[i])
299
+ assert_equal(res, ref)
300
+ assert_equal(intermediate_result.x,
301
+ intermediate_result.population[0])
302
+ assert_equal(intermediate_result.fun,
303
+ intermediate_result.population_energies[0])
304
+
305
+ assert intermediate_result.message == 'in progress'
306
+ assert intermediate_result.success is True
307
+ assert isinstance(intermediate_result, OptimizeResult)
308
+ if callback.nit == maxiter:
309
+ raise StopIteration
310
+ callback.nit = 0
311
+ callback.intermediate_result = None
312
+ callback.func = func
313
+
314
+ bounds = [(0, 2), (0, 2)]
315
+ kwargs = dict(func=func, bounds=bounds, rng=838245, polish=False)
316
+ res = differential_evolution(**kwargs, callback=callback)
317
+ ref = differential_evolution(**kwargs, maxiter=maxiter)
318
+
319
+ # Check that final `intermediate_result` is equivalent to returned
320
+ # result object and that terminating with callback `StopIteration`
321
+ # after `maxiter` iterations is equivalent to terminating with
322
+ # `maxiter` parameter.
323
+ assert res.success is ref.success is False
324
+ assert callback.nit == res.nit == maxiter
325
+ assert res.message == 'callback function requested stop early'
326
+ assert ref.message == 'Maximum number of iterations has been exceeded.'
327
+ for field, val in ref.items():
328
+ if field in {'message', 'success'}: # checked separately
329
+ continue
330
+ assert_equal(callback.intermediate_result[field], val)
331
+ assert_equal(res[field], val)
332
+
333
+ # Check that polish occurs after `StopIteration` as advertised
334
+ callback.nit = 0
335
+ func.val = np.inf
336
+ kwargs['polish'] = True
337
+ res = differential_evolution(**kwargs, callback=callback)
338
+ assert res.fun < ref.fun
339
+
340
+ def test_callback_terminates(self):
341
+ # test that if the callback returns true, then the minimization halts
342
+ bounds = [(0, 2), (0, 2)]
343
+ expected_msg = 'callback function requested stop early'
344
+ def callback_python_true(param, convergence=0.):
345
+ return True
346
+
347
+ result = differential_evolution(
348
+ rosen, bounds, callback=callback_python_true
349
+ )
350
+ assert_string_equal(result.message, expected_msg)
351
+
352
+ # if callback raises StopIteration then solve should be interrupted
353
+ def callback_stop(intermediate_result):
354
+ raise StopIteration
355
+
356
+ result = differential_evolution(rosen, bounds, callback=callback_stop)
357
+ assert not result.success
358
+
359
+ def callback_evaluates_true(param, convergence=0.):
360
+ # DE should stop if bool(self.callback) is True
361
+ return [10]
362
+
363
+ result = differential_evolution(rosen, bounds, callback=callback_evaluates_true)
364
+ assert_string_equal(result.message, expected_msg)
365
+ assert not result.success
366
+
367
+ def callback_evaluates_false(param, convergence=0.):
368
+ return []
369
+
370
+ result = differential_evolution(rosen, bounds,
371
+ callback=callback_evaluates_false)
372
+ assert result.success
373
+
374
+ def test_args_tuple_is_passed(self):
375
+ # test that the args tuple is passed to the cost function properly.
376
+ bounds = [(-10, 10)]
377
+ args = (1., 2., 3.)
378
+
379
+ def quadratic(x, *args):
380
+ if not isinstance(args, tuple):
381
+ raise ValueError('args should be a tuple')
382
+ return args[0] + args[1] * x + args[2] * x**2.
383
+
384
+ result = differential_evolution(quadratic,
385
+ bounds,
386
+ args=args,
387
+ polish=True)
388
+ assert_almost_equal(result.fun, 2 / 3.)
389
+
390
+ def test_init_with_invalid_strategy(self):
391
+ # test that passing an invalid strategy raises ValueError
392
+ func = rosen
393
+ bounds = [(-3, 3)]
394
+ assert_raises(ValueError,
395
+ differential_evolution,
396
+ func,
397
+ bounds,
398
+ strategy='abc')
399
+
400
+ def test_bounds_checking(self):
401
+ # test that the bounds checking works
402
+ func = rosen
403
+ bounds = [(-3)]
404
+ assert_raises(ValueError,
405
+ differential_evolution,
406
+ func,
407
+ bounds)
408
+ bounds = [(-3, 3), (3, 4, 5)]
409
+ assert_raises(ValueError,
410
+ differential_evolution,
411
+ func,
412
+ bounds)
413
+
414
+ # test that we can use a new-type Bounds object
415
+ result = differential_evolution(rosen, Bounds([0, 0], [2, 2]))
416
+ assert_almost_equal(result.x, (1., 1.))
417
+
418
+ def test_select_samples(self):
419
+ # select_samples should return 5 separate random numbers.
420
+ limits = np.arange(12., dtype='float64').reshape(2, 6)
421
+ bounds = list(zip(limits[0, :], limits[1, :]))
422
+ solver = DifferentialEvolutionSolver(None, bounds, popsize=1)
423
+ candidate = 0
424
+ r1, r2, r3, r4, r5 = solver._select_samples(candidate, 5)
425
+ assert_equal(
426
+ len(np.unique(np.array([candidate, r1, r2, r3, r4, r5]))), 6)
427
+
428
+ def test_maxiter_stops_solve(self):
429
+ # test that if the maximum number of iterations is exceeded
430
+ # the solver stops.
431
+ solver = DifferentialEvolutionSolver(rosen, self.bounds, maxiter=1)
432
+ result = solver.solve()
433
+ assert_equal(result.success, False)
434
+ assert_equal(result.message,
435
+ 'Maximum number of iterations has been exceeded.')
436
+
437
+ def test_maxfun_stops_solve(self):
438
+ # test that if the maximum number of function evaluations is exceeded
439
+ # during initialisation the solver stops
440
+ solver = DifferentialEvolutionSolver(rosen, self.bounds, maxfun=1,
441
+ polish=False)
442
+ result = solver.solve()
443
+
444
+ assert_equal(result.nfev, 2)
445
+ assert_equal(result.success, False)
446
+ assert_equal(result.message,
447
+ 'Maximum number of function evaluations has '
448
+ 'been exceeded.')
449
+
450
+ # test that if the maximum number of function evaluations is exceeded
451
+ # during the actual minimisation, then the solver stops.
452
+ # Have to turn polishing off, as this will still occur even if maxfun
453
+ # is reached. For popsize=5 and len(bounds)=2, then there are only 10
454
+ # function evaluations during initialisation.
455
+ solver = DifferentialEvolutionSolver(rosen,
456
+ self.bounds,
457
+ popsize=5,
458
+ polish=False,
459
+ maxfun=40)
460
+ result = solver.solve()
461
+
462
+ assert_equal(result.nfev, 41)
463
+ assert_equal(result.success, False)
464
+ assert_equal(result.message,
465
+ 'Maximum number of function evaluations has '
466
+ 'been exceeded.')
467
+
468
+ # now repeat for updating='deferred version
469
+ # 47 function evaluations is not a multiple of the population size,
470
+ # so maxfun is reached partway through a population evaluation.
471
+ solver = DifferentialEvolutionSolver(rosen,
472
+ self.bounds,
473
+ popsize=5,
474
+ polish=False,
475
+ maxfun=47,
476
+ updating='deferred')
477
+ result = solver.solve()
478
+
479
+ assert_equal(result.nfev, 47)
480
+ assert_equal(result.success, False)
481
+ assert_equal(result.message,
482
+ 'Maximum number of function evaluations has '
483
+ 'been reached.')
484
+
485
+ def test_quadratic(self):
486
+ # test the quadratic function from object
487
+ solver = DifferentialEvolutionSolver(self.quadratic,
488
+ [(-100, 100)],
489
+ tol=0.02)
490
+ solver.solve()
491
+ assert_equal(np.argmin(solver.population_energies), 0)
492
+
493
+ def test_quadratic_from_diff_ev(self):
494
+ # test the quadratic function from differential_evolution function
495
+ differential_evolution(self.quadratic,
496
+ [(-100, 100)],
497
+ tol=0.02,
498
+ seed=1)
499
+
500
+ def test_rng_gives_repeatability(self):
501
+ result = differential_evolution(self.quadratic,
502
+ [(-100, 100)],
503
+ polish=False,
504
+ rng=1,
505
+ tol=0.5)
506
+ result2 = differential_evolution(self.quadratic,
507
+ [(-100, 100)],
508
+ polish=False,
509
+ rng=1,
510
+ tol=0.5)
511
+ assert_equal(result.x, result2.x)
512
+ assert_equal(result.nfev, result2.nfev)
513
+
514
+ def test_random_generator(self):
515
+ # check that np.random.Generator can be used (numpy >= 1.17)
516
+ # obtain a np.random.Generator object
517
+ rng = np.random.default_rng()
518
+
519
+ inits = ['random', 'latinhypercube', 'sobol', 'halton']
520
+ for init in inits:
521
+ differential_evolution(self.quadratic,
522
+ [(-100, 100)],
523
+ polish=False,
524
+ rng=rng,
525
+ tol=0.5,
526
+ init=init)
527
+
528
+ def test_exp_runs(self):
529
+ # test whether exponential mutation loop runs
530
+ solver = DifferentialEvolutionSolver(rosen,
531
+ self.bounds,
532
+ strategy='best1exp',
533
+ maxiter=1)
534
+
535
+ solver.solve()
536
+
537
+ def test_gh_4511_regression(self):
538
+ # This modification of the differential evolution docstring example
539
+ # uses a custom popsize that had triggered an off-by-one error.
540
+ # Because we do not care about solving the optimization problem in
541
+ # this test, we use maxiter=1 to reduce the testing time.
542
+ bounds = [(-5, 5), (-5, 5)]
543
+ # result = differential_evolution(rosen, bounds, popsize=1815,
544
+ # maxiter=1)
545
+
546
+ # the original issue arose because of rounding error in arange, with
547
+ # linspace being a much better solution. 1815 is quite a large popsize
548
+ # to use and results in a long test time (~13s). I used the original
549
+ # issue to figure out the lowest number of samples that would cause
550
+ # this rounding error to occur, 49.
551
+ differential_evolution(rosen, bounds, popsize=49, maxiter=1)
552
+
553
+ def test_calculate_population_energies(self):
554
+ # if popsize is 3, then the overall generation has size (6,)
555
+ solver = DifferentialEvolutionSolver(rosen, self.bounds, popsize=3)
556
+ solver._calculate_population_energies(solver.population)
557
+ solver._promote_lowest_energy()
558
+ assert_equal(np.argmin(solver.population_energies), 0)
559
+
560
+ # initial calculation of the energies should require 6 nfev.
561
+ assert_equal(solver._nfev, 6)
562
+
563
+ def test_iteration(self):
564
+ # test that DifferentialEvolutionSolver is iterable
565
+ # if popsize is 3, then the overall generation has size (6,)
566
+ solver = DifferentialEvolutionSolver(rosen, self.bounds, popsize=3,
567
+ maxfun=12)
568
+ x, fun = next(solver)
569
+ assert_equal(np.size(x, 0), 2)
570
+
571
+ # 6 nfev are required for initial calculation of energies, 6 nfev are
572
+ # required for the evolution of the 6 population members.
573
+ assert_equal(solver._nfev, 12)
574
+
575
+ # the next generation should halt because it exceeds maxfun
576
+ assert_raises(StopIteration, next, solver)
577
+
578
+ # check a proper minimisation can be done by an iterable solver
579
+ solver = DifferentialEvolutionSolver(rosen, self.bounds)
580
+ _, fun_prev = next(solver)
581
+ for i, soln in enumerate(solver):
582
+ x_current, fun_current = soln
583
+ assert fun_prev >= fun_current
584
+ _, fun_prev = x_current, fun_current
585
+ # need to have this otherwise the solver would never stop.
586
+ if i == 50:
587
+ break
588
+
589
+ def test_convergence(self):
590
+ solver = DifferentialEvolutionSolver(rosen, self.bounds, tol=0.2,
591
+ polish=False)
592
+ solver.solve()
593
+ assert_(solver.convergence < 0.2)
594
+
595
+ def test_maxiter_none_GH5731(self):
596
+ # Pre 0.17 the previous default for maxiter and maxfun was None.
597
+ # the numerical defaults are now 1000 and np.inf. However, some scripts
598
+ # will still supply None for both of those, this will raise a TypeError
599
+ # in the solve method.
600
+ solver = DifferentialEvolutionSolver(rosen, self.bounds, maxiter=None,
601
+ maxfun=None)
602
+ solver.solve()
603
+
604
+ def test_population_initiation(self):
605
+ # test the different modes of population initiation
606
+
607
+ # init must be either 'latinhypercube' or 'random'
608
+ # raising ValueError is something else is passed in
609
+ assert_raises(ValueError,
610
+ DifferentialEvolutionSolver,
611
+ *(rosen, self.bounds),
612
+ **{'init': 'rubbish'})
613
+
614
+ solver = DifferentialEvolutionSolver(rosen, self.bounds)
615
+
616
+ # check that population initiation:
617
+ # 1) resets _nfev to 0
618
+ # 2) all population energies are np.inf
619
+ solver.init_population_random()
620
+ assert_equal(solver._nfev, 0)
621
+ assert_(np.all(np.isinf(solver.population_energies)))
622
+
623
+ solver.init_population_lhs()
624
+ assert_equal(solver._nfev, 0)
625
+ assert_(np.all(np.isinf(solver.population_energies)))
626
+
627
+ solver.init_population_qmc(qmc_engine='halton')
628
+ assert_equal(solver._nfev, 0)
629
+ assert_(np.all(np.isinf(solver.population_energies)))
630
+
631
+ solver = DifferentialEvolutionSolver(rosen, self.bounds, init='sobol')
632
+ solver.init_population_qmc(qmc_engine='sobol')
633
+ assert_equal(solver._nfev, 0)
634
+ assert_(np.all(np.isinf(solver.population_energies)))
635
+
636
+ # we should be able to initialize with our own array
637
+ population = np.linspace(-1, 3, 10).reshape(5, 2)
638
+ solver = DifferentialEvolutionSolver(rosen, self.bounds,
639
+ init=population,
640
+ strategy='best2bin',
641
+ atol=0.01, rng=1, popsize=5)
642
+
643
+ assert_equal(solver._nfev, 0)
644
+ assert_(np.all(np.isinf(solver.population_energies)))
645
+ assert_(solver.num_population_members == 5)
646
+ assert_(solver.population_shape == (5, 2))
647
+
648
+ # check that the population was initialized correctly
649
+ unscaled_population = np.clip(solver._unscale_parameters(population),
650
+ 0, 1)
651
+ assert_almost_equal(solver.population[:5], unscaled_population)
652
+
653
+ # population values need to be clipped to bounds
654
+ assert_almost_equal(np.min(solver.population[:5]), 0)
655
+ assert_almost_equal(np.max(solver.population[:5]), 1)
656
+
657
+ # shouldn't be able to initialize with an array if it's the wrong shape
658
+ # this would have too many parameters
659
+ population = np.linspace(-1, 3, 15).reshape(5, 3)
660
+ assert_raises(ValueError,
661
+ DifferentialEvolutionSolver,
662
+ *(rosen, self.bounds),
663
+ **{'init': population})
664
+
665
+ # provide an initial solution
666
+ # bounds are [(0, 2), (0, 2)]
667
+ x0 = np.random.uniform(low=0.0, high=2.0, size=2)
668
+ solver = DifferentialEvolutionSolver(
669
+ rosen, self.bounds, x0=x0
670
+ )
671
+ # parameters are scaled to unit interval
672
+ assert_allclose(solver.population[0], x0 / 2.0)
673
+
674
+ def test_x0(self):
675
+ # smoke test that checks that x0 is usable.
676
+ res = differential_evolution(rosen, self.bounds, x0=[0.2, 0.8])
677
+ assert res.success
678
+
679
+ # check what happens if some of the x0 lay outside the bounds
680
+ with assert_raises(ValueError):
681
+ differential_evolution(rosen, self.bounds, x0=[0.2, 2.1])
682
+
683
+ def test_infinite_objective_function(self):
684
+ # Test that there are no problems if the objective function
685
+ # returns inf on some runs
686
+ def sometimes_inf(x):
687
+ if x[0] < .5:
688
+ return np.inf
689
+ return x[1]
690
+ bounds = [(0, 1), (0, 1)]
691
+ differential_evolution(sometimes_inf, bounds=bounds, disp=False)
692
+
693
+ def test_deferred_updating(self):
694
+ # check setting of deferred updating, with default workers
695
+ bounds = [(0., 2.), (0., 2.)]
696
+ solver = DifferentialEvolutionSolver(rosen, bounds, updating='deferred')
697
+ assert_(solver._updating == 'deferred')
698
+ assert_(solver._mapwrapper._mapfunc is map)
699
+ res = solver.solve()
700
+ assert res.success
701
+
702
+ # check that deferred updating works with an exponential crossover
703
+ res = differential_evolution(
704
+ rosen, bounds, updating='deferred', strategy='best1exp'
705
+ )
706
+ assert res.success
707
+
708
+ def test_immediate_updating(self):
709
+ # check setting of immediate updating, with default workers
710
+ bounds = [(0., 2.), (0., 2.)]
711
+ solver = DifferentialEvolutionSolver(rosen, bounds)
712
+ assert_(solver._updating == 'immediate')
713
+
714
+ # should raise a UserWarning because the updating='immediate'
715
+ # is being overridden by the workers keyword
716
+ with warns(UserWarning):
717
+ with DifferentialEvolutionSolver(rosen, bounds, workers=2) as s:
718
+ solver.solve()
719
+ assert s._updating == 'deferred'
720
+
721
+ def test_parallel_threads(self):
722
+ # smoke test for parallelization with deferred updating
723
+ bounds = [(0., 2.), (0., 2.)]
724
+ # use threads instead of Process to speed things up for this simple example
725
+ with ThreadPool(2) as p, DifferentialEvolutionSolver(
726
+ rosen, bounds, updating='deferred', workers=p.map, tol=0.1, popsize=3
727
+ ) as solver:
728
+ assert solver._mapwrapper.pool is not None
729
+ assert solver._updating == 'deferred'
730
+ solver.solve()
731
+
732
+ @pytest.mark.fail_slow(10)
733
+ def test_parallel_processes(self):
734
+ bounds = [(0., 2.), (0., 2.)]
735
+ with DifferentialEvolutionSolver(
736
+ rosen, bounds, updating='deferred', workers=2, popsize=3, tol=0.1
737
+ ) as solver:
738
+ assert solver._mapwrapper.pool is not None
739
+ assert solver._updating == 'deferred'
740
+ solver.solve()
741
+
742
+ def test_converged(self):
743
+ solver = DifferentialEvolutionSolver(rosen, [(0, 2), (0, 2)])
744
+ solver.solve()
745
+ assert_(solver.converged())
746
+
747
+ def test_constraint_violation_fn(self):
748
+ def constr_f(x):
749
+ return [x[0] + x[1]]
750
+
751
+ def constr_f2(x):
752
+ return np.array([x[0]**2 + x[1], x[0] - x[1]])
753
+
754
+ nlc = NonlinearConstraint(constr_f, -np.inf, 1.9)
755
+
756
+ solver = DifferentialEvolutionSolver(rosen, [(0, 2), (0, 2)],
757
+ constraints=(nlc,))
758
+
759
+ cv = solver._constraint_violation_fn(np.array([1.0, 1.0]))
760
+ assert_almost_equal(cv, 0.1)
761
+
762
+ nlc2 = NonlinearConstraint(constr_f2, -np.inf, 1.8)
763
+ solver = DifferentialEvolutionSolver(rosen, [(0, 2), (0, 2)],
764
+ constraints=(nlc, nlc2))
765
+
766
+ # for multiple constraints the constraint violations should
767
+ # be concatenated.
768
+ xs = [(1.2, 1), (2.0, 2.0), (0.5, 0.5)]
769
+ vs = [(0.3, 0.64, 0.0), (2.1, 4.2, 0.0), (0, 0, 0)]
770
+
771
+ for x, v in zip(xs, vs):
772
+ cv = solver._constraint_violation_fn(np.array(x))
773
+ assert_allclose(cv, np.atleast_2d(v))
774
+
775
+ # vectorized calculation of a series of solutions
776
+ assert_allclose(
777
+ solver._constraint_violation_fn(np.array(xs)), np.array(vs)
778
+ )
779
+
780
+ # the following line is used in _calculate_population_feasibilities.
781
+ # _constraint_violation_fn returns an (1, M) array when
782
+ # x.shape == (N,), i.e. a single solution. Therefore this list
783
+ # comprehension should generate (S, 1, M) array.
784
+ constraint_violation = np.array([solver._constraint_violation_fn(x)
785
+ for x in np.array(xs)])
786
+ assert constraint_violation.shape == (3, 1, 3)
787
+
788
+ # we need reasonable error messages if the constraint function doesn't
789
+ # return the right thing
790
+ def constr_f3(x):
791
+ # returns (S, M), rather than (M, S)
792
+ return constr_f2(x).T
793
+
794
+ nlc2 = NonlinearConstraint(constr_f3, -np.inf, 1.8)
795
+ solver = DifferentialEvolutionSolver(rosen, [(0, 2), (0, 2)],
796
+ constraints=(nlc, nlc2),
797
+ vectorized=False)
798
+ solver.vectorized = True
799
+ with pytest.raises(
800
+ RuntimeError, match="An array returned from a Constraint"
801
+ ):
802
+ solver._constraint_violation_fn(np.array(xs))
803
+
804
+ def test_constraint_population_feasibilities(self):
805
+ def constr_f(x):
806
+ return [x[0] + x[1]]
807
+
808
+ def constr_f2(x):
809
+ return [x[0]**2 + x[1], x[0] - x[1]]
810
+
811
+ nlc = NonlinearConstraint(constr_f, -np.inf, 1.9)
812
+
813
+ solver = DifferentialEvolutionSolver(rosen, [(0, 2), (0, 2)],
814
+ constraints=(nlc,))
815
+
816
+ # are population feasibilities correct
817
+ # [0.5, 0.5] corresponds to scaled values of [1., 1.]
818
+ feas, cv = solver._calculate_population_feasibilities(
819
+ np.array([[0.5, 0.5], [1., 1.]]))
820
+ assert_equal(feas, [False, False])
821
+ assert_almost_equal(cv, np.array([[0.1], [2.1]]))
822
+ assert cv.shape == (2, 1)
823
+
824
+ nlc2 = NonlinearConstraint(constr_f2, -np.inf, 1.8)
825
+
826
+ for vectorize in [False, True]:
827
+ solver = DifferentialEvolutionSolver(rosen, [(0, 2), (0, 2)],
828
+ constraints=(nlc, nlc2),
829
+ vectorized=vectorize,
830
+ updating='deferred')
831
+
832
+ feas, cv = solver._calculate_population_feasibilities(
833
+ np.array([[0.5, 0.5], [0.6, 0.5]]))
834
+ assert_equal(feas, [False, False])
835
+ assert_almost_equal(cv, np.array([[0.1, 0.2, 0], [0.3, 0.64, 0]]))
836
+
837
+ feas, cv = solver._calculate_population_feasibilities(
838
+ np.array([[0.5, 0.5], [1., 1.]]))
839
+ assert_equal(feas, [False, False])
840
+ assert_almost_equal(cv, np.array([[0.1, 0.2, 0], [2.1, 4.2, 0]]))
841
+ assert cv.shape == (2, 3)
842
+
843
+ feas, cv = solver._calculate_population_feasibilities(
844
+ np.array([[0.25, 0.25], [1., 1.]]))
845
+ assert_equal(feas, [True, False])
846
+ assert_almost_equal(cv, np.array([[0.0, 0.0, 0.], [2.1, 4.2, 0]]))
847
+ assert cv.shape == (2, 3)
848
+
849
+ def test_constraint_solve(self):
850
+ def constr_f(x):
851
+ return np.array([x[0] + x[1]])
852
+
853
+ nlc = NonlinearConstraint(constr_f, -np.inf, 1.9)
854
+
855
+ solver = DifferentialEvolutionSolver(rosen, [(0, 2), (0, 2)],
856
+ constraints=(nlc,))
857
+
858
+ # trust-constr warns if the constraint function is linear
859
+ with warns(UserWarning):
860
+ res = solver.solve()
861
+
862
+ assert constr_f(res.x) <= 1.9
863
+ assert res.success
864
+
865
+ @pytest.mark.fail_slow(10)
866
+ def test_impossible_constraint(self):
867
+ def constr_f(x):
868
+ return np.array([x[0] + x[1]])
869
+
870
+ nlc = NonlinearConstraint(constr_f, -np.inf, -1)
871
+
872
+ solver = DifferentialEvolutionSolver(
873
+ rosen, [(0, 2), (0, 2)], constraints=(nlc,), popsize=1, rng=1, maxiter=100
874
+ )
875
+
876
+ # a UserWarning is issued because the 'trust-constr' polishing is
877
+ # attempted on the least infeasible solution found.
878
+ with warns(UserWarning):
879
+ res = solver.solve()
880
+
881
+ assert res.maxcv > 0
882
+ assert not res.success
883
+
884
+ # test _promote_lowest_energy works when none of the population is
885
+ # feasible. In this case, the solution with the lowest constraint
886
+ # violation should be promoted.
887
+ solver = DifferentialEvolutionSolver(
888
+ rosen, [(0, 2), (0, 2)], constraints=(nlc,), polish=False)
889
+ next(solver)
890
+ assert not solver.feasible.all()
891
+ assert not np.isfinite(solver.population_energies).all()
892
+
893
+ # now swap two of the entries in the population
894
+ l = 20
895
+ cv = solver.constraint_violation[0]
896
+
897
+ solver.population_energies[[0, l]] = solver.population_energies[[l, 0]]
898
+ solver.population[[0, l], :] = solver.population[[l, 0], :]
899
+ solver.constraint_violation[[0, l], :] = (
900
+ solver.constraint_violation[[l, 0], :])
901
+
902
+ solver._promote_lowest_energy()
903
+ assert_equal(solver.constraint_violation[0], cv)
904
+
905
+ def test_accept_trial(self):
906
+ # _accept_trial(self, energy_trial, feasible_trial, cv_trial,
907
+ # energy_orig, feasible_orig, cv_orig)
908
+ def constr_f(x):
909
+ return [x[0] + x[1]]
910
+ nlc = NonlinearConstraint(constr_f, -np.inf, 1.9)
911
+ solver = DifferentialEvolutionSolver(rosen, [(0, 2), (0, 2)],
912
+ constraints=(nlc,))
913
+ fn = solver._accept_trial
914
+ # both solutions are feasible, select lower energy
915
+ assert fn(0.1, True, np.array([0.]), 1.0, True, np.array([0.]))
916
+ assert (fn(1.0, True, np.array([0.0]), 0.1, True, np.array([0.0])) is False)
917
+ assert fn(0.1, True, np.array([0.]), 0.1, True, np.array([0.]))
918
+
919
+ # trial is feasible, original is not
920
+ assert fn(9.9, True, np.array([0.]), 1.0, False, np.array([1.]))
921
+
922
+ # trial and original are infeasible
923
+ # cv_trial have to be <= cv_original to be better
924
+ assert (fn(0.1, False, np.array([0.5, 0.5]),
925
+ 1.0, False, np.array([1., 1.0])))
926
+ assert (fn(0.1, False, np.array([0.5, 0.5]),
927
+ 1.0, False, np.array([1., 0.50])))
928
+ assert not (fn(1.0, False, np.array([0.5, 0.5]),
929
+ 1.0, False, np.array([1.0, 0.4])))
930
+
931
+ def test_constraint_wrapper(self):
932
+ lb = np.array([0, 20, 30])
933
+ ub = np.array([0.5, np.inf, 70])
934
+ x0 = np.array([1, 2, 3])
935
+ pc = _ConstraintWrapper(Bounds(lb, ub), x0)
936
+ assert (pc.violation(x0) > 0).any()
937
+ assert (pc.violation([0.25, 21, 31]) == 0).all()
938
+
939
+ # check vectorized Bounds constraint
940
+ xs = np.arange(1, 16).reshape(5, 3)
941
+ violations = []
942
+ for x in xs:
943
+ violations.append(pc.violation(x))
944
+ np.testing.assert_allclose(pc.violation(xs.T), np.array(violations).T)
945
+
946
+ x0 = np.array([1, 2, 3, 4])
947
+ A = np.array([[1, 2, 3, 4], [5, 0, 0, 6], [7, 0, 8, 0]])
948
+ pc = _ConstraintWrapper(LinearConstraint(A, -np.inf, 0), x0)
949
+ assert (pc.violation(x0) > 0).any()
950
+ assert (pc.violation([-10, 2, -10, 4]) == 0).all()
951
+
952
+ # check vectorized LinearConstraint, for 7 lots of parameter vectors
953
+ # with each parameter vector being 4 long, with 3 constraints
954
+ # xs is the same shape as stored in the differential evolution
955
+ # population, but it's sent to the violation function as (len(x), M)
956
+ xs = np.arange(1, 29).reshape(7, 4)
957
+ violations = []
958
+ for x in xs:
959
+ violations.append(pc.violation(x))
960
+ np.testing.assert_allclose(pc.violation(xs.T), np.array(violations).T)
961
+
962
+ pc = _ConstraintWrapper(LinearConstraint(csr_array(A), -np.inf, 0),
963
+ x0)
964
+ assert (pc.violation(x0) > 0).any()
965
+ assert (pc.violation([-10, 2, -10, 4]) == 0).all()
966
+
967
+ def fun(x):
968
+ return A.dot(x)
969
+
970
+ nonlinear = NonlinearConstraint(fun, -np.inf, 0)
971
+ pc = _ConstraintWrapper(nonlinear, [-10, 2, -10, 4])
972
+ assert (pc.violation(x0) > 0).any()
973
+ assert (pc.violation([-10, 2, -10, 4]) == 0).all()
974
+
975
+ def test_constraint_wrapper_violation(self):
976
+ def cons_f(x):
977
+ # written in vectorised form to accept an array of (N, S)
978
+ # returning (M, S)
979
+ # where N is the number of parameters,
980
+ # S is the number of solution vectors to be examined,
981
+ # and M is the number of constraint components
982
+ return np.array([x[0] ** 2 + x[1],
983
+ x[0] ** 2 - x[1]])
984
+
985
+ nlc = NonlinearConstraint(cons_f, [-1, -0.8500], [2, 2])
986
+ pc = _ConstraintWrapper(nlc, [0.5, 1])
987
+ assert np.size(pc.bounds[0]) == 2
988
+
989
+ xs = [(0.5, 1), (0.5, 1.2), (1.2, 1.2), (0.1, -1.2), (0.1, 2.0)]
990
+ vs = [(0, 0), (0, 0.1), (0.64, 0), (0.19, 0), (0.01, 1.14)]
991
+
992
+ for x, v in zip(xs, vs):
993
+ assert_allclose(pc.violation(x), v)
994
+
995
+ # now check that we can vectorize the constraint wrapper
996
+ assert_allclose(pc.violation(np.array(xs).T),
997
+ np.array(vs).T)
998
+ assert pc.fun(np.array(xs).T).shape == (2, len(xs))
999
+ assert pc.violation(np.array(xs).T).shape == (2, len(xs))
1000
+ assert pc.num_constr == 2
1001
+ assert pc.parameter_count == 2
1002
+
1003
+ def test_matrix_linear_constraint(self):
1004
+ # gh20041 supplying an np.matrix to construct a LinearConstraint caused
1005
+ # _ConstraintWrapper to start returning constraint violations of the
1006
+ # wrong shape.
1007
+ with warnings.catch_warnings():
1008
+ warnings.simplefilter("ignore", PendingDeprecationWarning)
1009
+ matrix = np.matrix([[1, 1, 1, 1.],
1010
+ [2, 2, 2, 2.]])
1011
+ lc = LinearConstraint(matrix, 0, 1)
1012
+ x0 = np.ones(4)
1013
+ cw = _ConstraintWrapper(lc, x0)
1014
+ # the shape of the constraint violation should be the same as the number
1015
+ # of constraints applied.
1016
+ assert cw.violation(x0).shape == (2,)
1017
+
1018
+ # let's try a vectorised violation call.
1019
+ xtrial = np.arange(4 * 5).reshape(4, 5)
1020
+ assert cw.violation(xtrial).shape == (2, 5)
1021
+
1022
+ @pytest.mark.fail_slow(20)
1023
+ def test_L1(self):
1024
+ # Lampinen ([5]) test problem 1
1025
+
1026
+ def f(x):
1027
+ x = np.hstack(([0], x)) # 1-indexed to match reference
1028
+ fun = np.sum(5*x[1:5]) - 5*x[1:5]@x[1:5] - np.sum(x[5:])
1029
+ return fun
1030
+
1031
+ A = np.zeros((10, 14)) # 1-indexed to match reference
1032
+ A[1, [1, 2, 10, 11]] = 2, 2, 1, 1
1033
+ A[2, [1, 10]] = -8, 1
1034
+ A[3, [4, 5, 10]] = -2, -1, 1
1035
+ A[4, [1, 3, 10, 11]] = 2, 2, 1, 1
1036
+ A[5, [2, 11]] = -8, 1
1037
+ A[6, [6, 7, 11]] = -2, -1, 1
1038
+ A[7, [2, 3, 11, 12]] = 2, 2, 1, 1
1039
+ A[8, [3, 12]] = -8, 1
1040
+ A[9, [8, 9, 12]] = -2, -1, 1
1041
+ A = A[1:, 1:]
1042
+
1043
+ b = np.array([10, 0, 0, 10, 0, 0, 10, 0, 0])
1044
+
1045
+ L = LinearConstraint(A, -np.inf, b)
1046
+
1047
+ bounds = [(0, 1)]*9 + [(0, 100)]*3 + [(0, 1)]
1048
+
1049
+ # using a lower popsize to speed the test up
1050
+ res = differential_evolution(
1051
+ f, bounds, strategy='best1bin', rng=12345, constraints=(L,),
1052
+ popsize=5, tol=0.01
1053
+ )
1054
+
1055
+ x_opt = (1, 1, 1, 1, 1, 1, 1, 1, 1, 3, 3, 3, 1)
1056
+ f_opt = -15
1057
+
1058
+ assert_allclose(f(x_opt), f_opt, atol=6e-4)
1059
+ assert res.success
1060
+ assert_allclose(res.x, x_opt, atol=6e-4)
1061
+ assert_allclose(res.fun, f_opt, atol=5e-3)
1062
+ assert_(np.all(A@res.x <= b))
1063
+ assert_(np.all(res.x >= np.array(bounds)[:, 0]))
1064
+ assert_(np.all(res.x <= np.array(bounds)[:, 1]))
1065
+
1066
+ # now repeat the same solve, using the same overall constraints,
1067
+ # but using a sparse array for the LinearConstraint instead of an
1068
+ # array
1069
+
1070
+ L = LinearConstraint(csr_array(A), -np.inf, b)
1071
+
1072
+ # using a lower popsize to speed the test up
1073
+ res = differential_evolution(
1074
+ f, bounds, strategy='best1bin', rng=1211134, constraints=(L,),
1075
+ popsize=2, tol=0.05
1076
+ )
1077
+
1078
+ assert_allclose(f(x_opt), f_opt)
1079
+ assert res.success
1080
+ assert_allclose(res.x, x_opt, atol=5e-4)
1081
+ assert_allclose(res.fun, f_opt, atol=5e-3)
1082
+ assert_(np.all(A@res.x <= b))
1083
+ assert_(np.all(res.x >= np.array(bounds)[:, 0]))
1084
+ assert_(np.all(res.x <= np.array(bounds)[:, 1]))
1085
+
1086
+ # now repeat the same solve, using the same overall constraints,
1087
+ # but specify half the constraints in terms of LinearConstraint,
1088
+ # and the other half by NonlinearConstraint
1089
+ def c1(x):
1090
+ x = np.hstack(([0], x))
1091
+ return [2*x[2] + 2*x[3] + x[11] + x[12],
1092
+ -8*x[3] + x[12]]
1093
+
1094
+ def c2(x):
1095
+ x = np.hstack(([0], x))
1096
+ return -2*x[8] - x[9] + x[12]
1097
+
1098
+ L = LinearConstraint(A[:5, :], -np.inf, b[:5])
1099
+ L2 = LinearConstraint(A[5:6, :], -np.inf, b[5:6])
1100
+ N = NonlinearConstraint(c1, -np.inf, b[6:8])
1101
+ N2 = NonlinearConstraint(c2, -np.inf, b[8:9])
1102
+ constraints = (L, N, L2, N2)
1103
+
1104
+ with warnings.catch_warnings():
1105
+ warnings.simplefilter("ignore", UserWarning)
1106
+ res = differential_evolution(
1107
+ f, bounds, strategy='best1bin', rng=1211134,
1108
+ constraints=constraints, popsize=2, tol=0.05
1109
+ )
1110
+
1111
+ assert_allclose(res.x, x_opt, atol=6e-4)
1112
+ assert_allclose(res.fun, f_opt, atol=5e-3)
1113
+ assert_(np.all(A@res.x <= b))
1114
+ assert_(np.all(res.x >= np.array(bounds)[:, 0]))
1115
+ assert_(np.all(res.x <= np.array(bounds)[:, 1]))
1116
+
1117
+ @pytest.mark.fail_slow(10)
1118
+ def test_L2(self):
1119
+ # Lampinen ([5]) test problem 2
1120
+
1121
+ def f(x):
1122
+ x = np.hstack(([0], x)) # 1-indexed to match reference
1123
+ fun = ((x[1]-10)**2 + 5*(x[2]-12)**2 + x[3]**4 + 3*(x[4]-11)**2 +
1124
+ 10*x[5]**6 + 7*x[6]**2 + x[7]**4 - 4*x[6]*x[7] - 10*x[6] -
1125
+ 8*x[7])
1126
+ return fun
1127
+
1128
+ def c1(x):
1129
+ x = np.hstack(([0], x)) # 1-indexed to match reference
1130
+ return [127 - 2*x[1]**2 - 3*x[2]**4 - x[3] - 4*x[4]**2 - 5*x[5],
1131
+ 196 - 23*x[1] - x[2]**2 - 6*x[6]**2 + 8*x[7],
1132
+ 282 - 7*x[1] - 3*x[2] - 10*x[3]**2 - x[4] + x[5],
1133
+ -4*x[1]**2 - x[2]**2 + 3*x[1]*x[2] - 2*x[3]**2 -
1134
+ 5*x[6] + 11*x[7]]
1135
+
1136
+ N = NonlinearConstraint(c1, 0, np.inf)
1137
+ bounds = [(-10, 10)]*7
1138
+ constraints = (N)
1139
+
1140
+ with warnings.catch_warnings():
1141
+ warnings.simplefilter("ignore", UserWarning)
1142
+ res = differential_evolution(f, bounds, strategy='best1bin',
1143
+ rng=1234, constraints=constraints)
1144
+
1145
+ f_opt = 680.6300599487869
1146
+ x_opt = (2.330499, 1.951372, -0.4775414, 4.365726,
1147
+ -0.6244870, 1.038131, 1.594227)
1148
+
1149
+ assert_allclose(f(x_opt), f_opt)
1150
+ assert_allclose(res.fun, f_opt)
1151
+ assert_allclose(res.x, x_opt, atol=1e-5)
1152
+ assert res.success
1153
+ assert_(np.all(np.array(c1(res.x)) >= 0))
1154
+ assert_(np.all(res.x >= np.array(bounds)[:, 0]))
1155
+ assert_(np.all(res.x <= np.array(bounds)[:, 1]))
1156
+
1157
+ @pytest.mark.fail_slow(10)
1158
+ def test_L3(self):
1159
+ # Lampinen ([5]) test problem 3
1160
+
1161
+ def f(x):
1162
+ x = np.hstack(([0], x)) # 1-indexed to match reference
1163
+ fun = (x[1]**2 + x[2]**2 + x[1]*x[2] - 14*x[1] - 16*x[2] +
1164
+ (x[3]-10)**2 + 4*(x[4]-5)**2 + (x[5]-3)**2 + 2*(x[6]-1)**2 +
1165
+ 5*x[7]**2 + 7*(x[8]-11)**2 + 2*(x[9]-10)**2 +
1166
+ (x[10] - 7)**2 + 45
1167
+ )
1168
+ return fun # maximize
1169
+
1170
+ A = np.zeros((4, 11))
1171
+ A[1, [1, 2, 7, 8]] = -4, -5, 3, -9
1172
+ A[2, [1, 2, 7, 8]] = -10, 8, 17, -2
1173
+ A[3, [1, 2, 9, 10]] = 8, -2, -5, 2
1174
+ A = A[1:, 1:]
1175
+ b = np.array([-105, 0, -12])
1176
+
1177
+ def c1(x):
1178
+ x = np.hstack(([0], x)) # 1-indexed to match reference
1179
+ return [3*x[1] - 6*x[2] - 12*(x[9]-8)**2 + 7*x[10],
1180
+ -3*(x[1]-2)**2 - 4*(x[2]-3)**2 - 2*x[3]**2 + 7*x[4] + 120,
1181
+ -x[1]**2 - 2*(x[2]-2)**2 + 2*x[1]*x[2] - 14*x[5] + 6*x[6],
1182
+ -5*x[1]**2 - 8*x[2] - (x[3]-6)**2 + 2*x[4] + 40,
1183
+ -0.5*(x[1]-8)**2 - 2*(x[2]-4)**2 - 3*x[5]**2 + x[6] + 30]
1184
+
1185
+ L = LinearConstraint(A, b, np.inf)
1186
+ N = NonlinearConstraint(c1, 0, np.inf)
1187
+ bounds = [(-10, 10)]*10
1188
+ constraints = (L, N)
1189
+
1190
+ with warnings.catch_warnings():
1191
+ warnings.simplefilter("ignore", UserWarning)
1192
+ res = differential_evolution(f, bounds, rng=1234,
1193
+ constraints=constraints, popsize=3)
1194
+
1195
+ x_opt = (2.171996, 2.363683, 8.773926, 5.095984, 0.9906548,
1196
+ 1.430574, 1.321644, 9.828726, 8.280092, 8.375927)
1197
+ f_opt = 24.3062091
1198
+
1199
+ assert_allclose(f(x_opt), f_opt, atol=1e-5)
1200
+ assert_allclose(res.x, x_opt, atol=1e-6)
1201
+ assert_allclose(res.fun, f_opt, atol=1e-5)
1202
+ assert res.success
1203
+ assert_(np.all(A @ res.x >= b))
1204
+ assert_(np.all(np.array(c1(res.x)) >= 0))
1205
+ assert_(np.all(res.x >= np.array(bounds)[:, 0]))
1206
+ assert_(np.all(res.x <= np.array(bounds)[:, 1]))
1207
+
1208
+ @pytest.mark.fail_slow(10)
1209
+ def test_L4(self):
1210
+ # Lampinen ([5]) test problem 4
1211
+ def f(x):
1212
+ return np.sum(x[:3])
1213
+
1214
+ A = np.zeros((4, 9))
1215
+ A[1, [4, 6]] = 0.0025, 0.0025
1216
+ A[2, [5, 7, 4]] = 0.0025, 0.0025, -0.0025
1217
+ A[3, [8, 5]] = 0.01, -0.01
1218
+ A = A[1:, 1:]
1219
+ b = np.array([1, 1, 1])
1220
+
1221
+ def c1(x):
1222
+ x = np.hstack(([0], x)) # 1-indexed to match reference
1223
+ return [x[1]*x[6] - 833.33252*x[4] - 100*x[1] + 83333.333,
1224
+ x[2]*x[7] - 1250*x[5] - x[2]*x[4] + 1250*x[4],
1225
+ x[3]*x[8] - 1250000 - x[3]*x[5] + 2500*x[5]]
1226
+
1227
+ L = LinearConstraint(A, -np.inf, 1)
1228
+ N = NonlinearConstraint(c1, 0, np.inf)
1229
+
1230
+ bounds = [(100, 10000)] + [(1000, 10000)]*2 + [(10, 1000)]*5
1231
+ constraints = (L, N)
1232
+
1233
+ with warnings.catch_warnings():
1234
+ warnings.simplefilter("ignore", UserWarning)
1235
+ res = differential_evolution(
1236
+ f, bounds, strategy='best1bin', rng=1234,
1237
+ constraints=constraints, popsize=3, tol=0.05
1238
+ )
1239
+
1240
+ f_opt = 7049.248
1241
+
1242
+ x_opt = [579.306692, 1359.97063, 5109.9707, 182.0177, 295.601172,
1243
+ 217.9823, 286.416528, 395.601172]
1244
+
1245
+ assert_allclose(f(x_opt), f_opt, atol=0.001)
1246
+ assert_allclose(res.fun, f_opt, atol=0.001)
1247
+
1248
+ # use higher tol here for 32-bit Windows, see gh-11693
1249
+ if (platform.system() == 'Windows' and np.dtype(np.intp).itemsize < 8):
1250
+ assert_allclose(res.x, x_opt, rtol=2.4e-6, atol=0.0035)
1251
+ else:
1252
+ # tolerance determined from macOS + MKL failure, see gh-12701
1253
+ assert_allclose(res.x, x_opt, rtol=5e-6, atol=0.0024)
1254
+
1255
+ assert res.success
1256
+ assert_(np.all(A @ res.x <= b))
1257
+ assert_(np.all(np.array(c1(res.x)) >= 0))
1258
+ assert_(np.all(res.x >= np.array(bounds)[:, 0]))
1259
+ assert_(np.all(res.x <= np.array(bounds)[:, 1]))
1260
+
1261
+ @pytest.mark.fail_slow(10)
1262
+ def test_L5(self):
1263
+ # Lampinen ([5]) test problem 5
1264
+
1265
+ def f(x):
1266
+ x = np.hstack(([0], x)) # 1-indexed to match reference
1267
+ fun = (np.sin(2*np.pi*x[1])**3*np.sin(2*np.pi*x[2]) /
1268
+ (x[1]**3*(x[1]+x[2])))
1269
+ return -fun # maximize
1270
+
1271
+ def c1(x):
1272
+ x = np.hstack(([0], x)) # 1-indexed to match reference
1273
+ return [x[1]**2 - x[2] + 1,
1274
+ 1 - x[1] + (x[2]-4)**2]
1275
+
1276
+ N = NonlinearConstraint(c1, -np.inf, 0)
1277
+ bounds = [(0, 10)]*2
1278
+ constraints = (N)
1279
+
1280
+ res = differential_evolution(f, bounds, strategy='rand1bin', rng=1234,
1281
+ constraints=constraints)
1282
+
1283
+ x_opt = (1.22797135, 4.24537337)
1284
+ f_opt = -0.095825
1285
+ assert_allclose(f(x_opt), f_opt, atol=2e-5)
1286
+ assert_allclose(res.fun, f_opt, atol=1e-4)
1287
+ assert res.success
1288
+ assert_(np.all(np.array(c1(res.x)) <= 0))
1289
+ assert_(np.all(res.x >= np.array(bounds)[:, 0]))
1290
+ assert_(np.all(res.x <= np.array(bounds)[:, 1]))
1291
+
1292
+ @pytest.mark.fail_slow(10)
1293
+ def test_L6(self):
1294
+ # Lampinen ([5]) test problem 6
1295
+ def f(x):
1296
+ x = np.hstack(([0], x)) # 1-indexed to match reference
1297
+ fun = (x[1]-10)**3 + (x[2] - 20)**3
1298
+ return fun
1299
+
1300
+ def c1(x):
1301
+ x = np.hstack(([0], x)) # 1-indexed to match reference
1302
+ return [(x[1]-5)**2 + (x[2] - 5)**2 - 100,
1303
+ -(x[1]-6)**2 - (x[2] - 5)**2 + 82.81]
1304
+
1305
+ N = NonlinearConstraint(c1, 0, np.inf)
1306
+ bounds = [(13, 100), (0, 100)]
1307
+ constraints = (N)
1308
+ res = differential_evolution(f, bounds, strategy='rand1bin', rng=1234,
1309
+ constraints=constraints, tol=1e-7)
1310
+ x_opt = (14.095, 0.84296)
1311
+ f_opt = -6961.814744
1312
+
1313
+ assert_allclose(f(x_opt), f_opt, atol=1e-6)
1314
+ assert_allclose(res.fun, f_opt, atol=0.001)
1315
+ assert_allclose(res.x, x_opt, atol=1e-4)
1316
+ assert res.success
1317
+ assert_(np.all(np.array(c1(res.x)) >= 0))
1318
+ assert_(np.all(res.x >= np.array(bounds)[:, 0]))
1319
+ assert_(np.all(res.x <= np.array(bounds)[:, 1]))
1320
+
1321
+ def test_L7(self):
1322
+ # Lampinen ([5]) test problem 7
1323
+ def f(x):
1324
+ x = np.hstack(([0], x)) # 1-indexed to match reference
1325
+ fun = (5.3578547*x[3]**2 + 0.8356891*x[1]*x[5] +
1326
+ 37.293239*x[1] - 40792.141)
1327
+ return fun
1328
+
1329
+ def c1(x):
1330
+ x = np.hstack(([0], x)) # 1-indexed to match reference
1331
+ return [
1332
+ 85.334407 + 0.0056858*x[2]*x[5] + 0.0006262*x[1]*x[4] -
1333
+ 0.0022053*x[3]*x[5],
1334
+
1335
+ 80.51249 + 0.0071317*x[2]*x[5] + 0.0029955*x[1]*x[2] +
1336
+ 0.0021813*x[3]**2,
1337
+
1338
+ 9.300961 + 0.0047026*x[3]*x[5] + 0.0012547*x[1]*x[3] +
1339
+ 0.0019085*x[3]*x[4]
1340
+ ]
1341
+
1342
+ N = NonlinearConstraint(c1, [0, 90, 20], [92, 110, 25])
1343
+
1344
+ bounds = [(78, 102), (33, 45)] + [(27, 45)]*3
1345
+ constraints = (N)
1346
+
1347
+ res = differential_evolution(f, bounds, strategy='rand1bin', rng=1234,
1348
+ constraints=constraints)
1349
+
1350
+ # using our best solution, rather than Lampinen/Koziel. Koziel solution
1351
+ # doesn't satisfy constraints, Lampinen f_opt just plain wrong.
1352
+ x_opt = [78.00000686, 33.00000362, 29.99526064, 44.99999971,
1353
+ 36.77579979]
1354
+
1355
+ f_opt = -30665.537578
1356
+
1357
+ assert_allclose(f(x_opt), f_opt)
1358
+ assert_allclose(res.x, x_opt, atol=1e-3)
1359
+ assert_allclose(res.fun, f_opt, atol=1e-3)
1360
+
1361
+ assert res.success
1362
+ assert_(np.all(np.array(c1(res.x)) >= np.array([0, 90, 20])))
1363
+ assert_(np.all(np.array(c1(res.x)) <= np.array([92, 110, 25])))
1364
+ assert_(np.all(res.x >= np.array(bounds)[:, 0]))
1365
+ assert_(np.all(res.x <= np.array(bounds)[:, 1]))
1366
+
1367
+ @pytest.mark.xslow
1368
+ @pytest.mark.xfail(platform.machine() == 'ppc64le',
1369
+ reason="fails on ppc64le")
1370
+ def test_L8(self):
1371
+ def f(x):
1372
+ x = np.hstack(([0], x)) # 1-indexed to match reference
1373
+ fun = 3*x[1] + 0.000001*x[1]**3 + 2*x[2] + 0.000002/3*x[2]**3
1374
+ return fun
1375
+
1376
+ A = np.zeros((3, 5))
1377
+ A[1, [4, 3]] = 1, -1
1378
+ A[2, [3, 4]] = 1, -1
1379
+ A = A[1:, 1:]
1380
+ b = np.array([-.55, -.55])
1381
+
1382
+ def c1(x):
1383
+ x = np.hstack(([0], x)) # 1-indexed to match reference
1384
+ return [
1385
+ 1000*np.sin(-x[3]-0.25) + 1000*np.sin(-x[4]-0.25) +
1386
+ 894.8 - x[1],
1387
+ 1000*np.sin(x[3]-0.25) + 1000*np.sin(x[3]-x[4]-0.25) +
1388
+ 894.8 - x[2],
1389
+ 1000*np.sin(x[4]-0.25) + 1000*np.sin(x[4]-x[3]-0.25) +
1390
+ 1294.8
1391
+ ]
1392
+ L = LinearConstraint(A, b, np.inf)
1393
+ N = NonlinearConstraint(c1, np.full(3, -0.001), np.full(3, 0.001))
1394
+
1395
+ bounds = [(0, 1200)]*2+[(-.55, .55)]*2
1396
+ constraints = (L, N)
1397
+
1398
+ with warnings.catch_warnings():
1399
+ warnings.simplefilter("ignore", UserWarning)
1400
+ # original Lampinen test was with rand1bin, but that takes a
1401
+ # huge amount of CPU time. Changing strategy to best1bin speeds
1402
+ # things up a lot
1403
+ res = differential_evolution(f, bounds, strategy='best1bin',
1404
+ rng=1234, constraints=constraints,
1405
+ maxiter=5000)
1406
+
1407
+ x_opt = (679.9453, 1026.067, 0.1188764, -0.3962336)
1408
+ f_opt = 5126.4981
1409
+
1410
+ assert_allclose(f(x_opt), f_opt, atol=1e-3)
1411
+ assert_allclose(res.x[:2], x_opt[:2], atol=2e-3)
1412
+ assert_allclose(res.x[2:], x_opt[2:], atol=2e-3)
1413
+ assert_allclose(res.fun, f_opt, atol=2e-2)
1414
+ assert res.success
1415
+ assert_(np.all(A@res.x >= b))
1416
+ assert_(np.all(np.array(c1(res.x)) >= -0.001))
1417
+ assert_(np.all(np.array(c1(res.x)) <= 0.001))
1418
+ assert_(np.all(res.x >= np.array(bounds)[:, 0]))
1419
+ assert_(np.all(res.x <= np.array(bounds)[:, 1]))
1420
+
1421
+ @pytest.mark.fail_slow(5)
1422
+ def test_L9(self):
1423
+ # Lampinen ([5]) test problem 9
1424
+
1425
+ def f(x):
1426
+ x = np.hstack(([0], x)) # 1-indexed to match reference
1427
+ return x[1]**2 + (x[2]-1)**2
1428
+
1429
+ def c1(x):
1430
+ x = np.hstack(([0], x)) # 1-indexed to match reference
1431
+ return [x[2] - x[1]**2]
1432
+
1433
+ N = NonlinearConstraint(c1, [-.001], [0.001])
1434
+
1435
+ bounds = [(-1, 1)]*2
1436
+ constraints = (N)
1437
+ res = differential_evolution(f, bounds, strategy='rand1bin', rng=1234,
1438
+ constraints=constraints)
1439
+
1440
+ x_opt = [np.sqrt(2)/2, 0.5]
1441
+ f_opt = 0.75
1442
+
1443
+ assert_allclose(f(x_opt), f_opt)
1444
+ assert_allclose(np.abs(res.x), x_opt, atol=1e-3)
1445
+ assert_allclose(res.fun, f_opt, atol=1e-3)
1446
+ assert res.success
1447
+ assert_(np.all(np.array(c1(res.x)) >= -0.001))
1448
+ assert_(np.all(np.array(c1(res.x)) <= 0.001))
1449
+ assert_(np.all(res.x >= np.array(bounds)[:, 0]))
1450
+ assert_(np.all(res.x <= np.array(bounds)[:, 1]))
1451
+
1452
+ @pytest.mark.fail_slow(10)
1453
+ def test_integrality(self):
1454
+ # test fitting discrete distribution to data
1455
+ rng = np.random.default_rng(6519843218105)
1456
+ dist = stats.nbinom
1457
+ shapes = (5, 0.5)
1458
+ x = dist.rvs(*shapes, size=10000, random_state=rng)
1459
+
1460
+ def func(p, *args):
1461
+ dist, x = args
1462
+ # negative log-likelihood function
1463
+ ll = -np.log(dist.pmf(x, *p)).sum(axis=-1)
1464
+ if np.isnan(ll): # occurs when x is outside of support
1465
+ ll = np.inf # we don't want that
1466
+ return ll
1467
+
1468
+ integrality = [True, False]
1469
+ bounds = [(1, 18), (0, 0.95)]
1470
+
1471
+ res = differential_evolution(func, bounds, args=(dist, x),
1472
+ integrality=integrality, polish=False,
1473
+ rng=rng)
1474
+ # tolerance has to be fairly relaxed for the second parameter
1475
+ # because we're fitting a distribution to random variates.
1476
+ assert res.x[0] == 5
1477
+ assert_allclose(res.x, shapes, rtol=0.025)
1478
+
1479
+ # check that we can still use integrality constraints with polishing
1480
+ res2 = differential_evolution(func, bounds, args=(dist, x),
1481
+ integrality=integrality, polish=True,
1482
+ rng=rng)
1483
+
1484
+ def func2(p, *args):
1485
+ n, dist, x = args
1486
+ return func(np.array([n, p[0]]), dist, x)
1487
+
1488
+ # compare the DE derived solution to an LBFGSB solution (that doesn't
1489
+ # have to find the integral values). Note we're setting x0 to be the
1490
+ # output from the first DE result, thereby making the polishing step
1491
+ # and this minimisation pretty much equivalent.
1492
+ LBFGSB = minimize(func2, res2.x[1], args=(5, dist, x),
1493
+ bounds=[(0, 0.95)])
1494
+ assert_allclose(res2.x[1], LBFGSB.x)
1495
+ assert res2.fun <= res.fun
1496
+
1497
+ def test_integrality_limits(self):
1498
+ def f(x):
1499
+ return x
1500
+
1501
+ integrality = [True, False, True]
1502
+ bounds = [(0.2, 1.1), (0.9, 2.2), (3.3, 4.9)]
1503
+
1504
+ # no integrality constraints
1505
+ solver = DifferentialEvolutionSolver(f, bounds=bounds, polish=False,
1506
+ integrality=False)
1507
+ assert_allclose(solver.limits[0], [0.2, 0.9, 3.3])
1508
+ assert_allclose(solver.limits[1], [1.1, 2.2, 4.9])
1509
+
1510
+ # with integrality constraints
1511
+ solver = DifferentialEvolutionSolver(f, bounds=bounds, polish=False,
1512
+ integrality=integrality)
1513
+ assert_allclose(solver.limits[0], [0.5, 0.9, 3.5])
1514
+ assert_allclose(solver.limits[1], [1.5, 2.2, 4.5])
1515
+ assert_equal(solver.integrality, [True, False, True])
1516
+ assert solver.polish is False
1517
+
1518
+ bounds = [(-1.2, -0.9), (0.9, 2.2), (-10.3, 4.1)]
1519
+ solver = DifferentialEvolutionSolver(f, bounds=bounds, polish=False,
1520
+ integrality=integrality)
1521
+ assert_allclose(solver.limits[0], [-1.5, 0.9, -10.5])
1522
+ assert_allclose(solver.limits[1], [-0.5, 2.2, 4.5])
1523
+
1524
+ # A lower bound of -1.2 is converted to
1525
+ # np.nextafter(np.ceil(-1.2) - 0.5, np.inf)
1526
+ # with a similar process to the upper bound. Check that the
1527
+ # conversions work
1528
+ assert_allclose(np.round(solver.limits[0]), [-1.0, 1.0, -10.0])
1529
+ assert_allclose(np.round(solver.limits[1]), [-1.0, 2.0, 4.0])
1530
+
1531
+ bounds = [(-10.2, -8.1), (0.9, 2.2), (-10.9, -9.9999)]
1532
+ solver = DifferentialEvolutionSolver(f, bounds=bounds, polish=False,
1533
+ integrality=integrality)
1534
+ assert_allclose(solver.limits[0], [-10.5, 0.9, -10.5])
1535
+ assert_allclose(solver.limits[1], [-8.5, 2.2, -9.5])
1536
+
1537
+ bounds = [(-10.2, -10.1), (0.9, 2.2), (-10.9, -9.9999)]
1538
+ with pytest.raises(ValueError, match='One of the integrality'):
1539
+ DifferentialEvolutionSolver(f, bounds=bounds, polish=False,
1540
+ integrality=integrality)
1541
+
1542
+ @pytest.mark.fail_slow(10)
1543
+ def test_vectorized(self):
1544
+ def quadratic(x):
1545
+ return np.sum(x**2)
1546
+
1547
+ def quadratic_vec(x):
1548
+ return np.sum(x**2, axis=0)
1549
+
1550
+ # A vectorized function needs to accept (len(x), S) and return (S,)
1551
+ with pytest.raises(RuntimeError, match='The vectorized function'):
1552
+ differential_evolution(quadratic, self.bounds,
1553
+ vectorized=True, updating='deferred')
1554
+
1555
+ # vectorized overrides the updating keyword, check for warning
1556
+ with warns(UserWarning, match="differential_evolution: the 'vector"):
1557
+ differential_evolution(quadratic_vec, self.bounds,
1558
+ vectorized=True)
1559
+
1560
+ # vectorized defers to the workers keyword, check for warning
1561
+ with warns(UserWarning, match="differential_evolution: the 'workers"):
1562
+ differential_evolution(quadratic_vec, self.bounds,
1563
+ vectorized=True, workers=map,
1564
+ updating='deferred')
1565
+
1566
+ ncalls = [0]
1567
+
1568
+ def rosen_vec(x):
1569
+ ncalls[0] += 1
1570
+ return rosen(x)
1571
+
1572
+ bounds = [(0, 10), (0, 10)]
1573
+ res1 = differential_evolution(rosen, bounds, updating='deferred',
1574
+ rng=1)
1575
+ res2 = differential_evolution(rosen_vec, bounds, vectorized=True,
1576
+ updating='deferred', rng=1)
1577
+
1578
+ # the two minimisation runs should be functionally equivalent
1579
+ assert_allclose(res1.x, res2.x)
1580
+ assert ncalls[0] == res2.nfev
1581
+ assert res1.nit == res2.nit
1582
+
1583
+ def test_vectorized_constraints(self):
1584
+ def constr_f(x):
1585
+ return np.array([x[0] + x[1]])
1586
+
1587
+ def constr_f2(x):
1588
+ return np.array([x[0]**2 + x[1], x[0] - x[1]])
1589
+
1590
+ nlc1 = NonlinearConstraint(constr_f, -np.inf, 1.9)
1591
+ nlc2 = NonlinearConstraint(constr_f2, (0.9, 0.5), (2.0, 2.0))
1592
+
1593
+ def rosen_vec(x):
1594
+ # accept an (len(x0), S) array, returning a (S,) array
1595
+ v = 100 * (x[1:] - x[:-1]**2.0)**2.0
1596
+ v += (1 - x[:-1])**2.0
1597
+ return np.squeeze(v)
1598
+
1599
+ bounds = [(0, 10), (0, 10)]
1600
+
1601
+ res1 = differential_evolution(rosen, bounds, updating='deferred',
1602
+ rng=1, constraints=[nlc1, nlc2],
1603
+ polish=False)
1604
+ res2 = differential_evolution(rosen_vec, bounds, vectorized=True,
1605
+ updating='deferred', rng=1,
1606
+ constraints=[nlc1, nlc2],
1607
+ polish=False)
1608
+ # the two minimisation runs should be functionally equivalent
1609
+ assert_allclose(res1.x, res2.x)
1610
+
1611
+ def test_polish_function(self):
1612
+ # the polishing may be done by a callable
1613
+ N = len(self.bounds)
1614
+
1615
+ def pf(fun, x, **kwds):
1616
+ pf.res = minimize(fun, x, jac=rosen_der, method='trust-constr', **kwds)
1617
+ return pf.res
1618
+ pf.res = None
1619
+
1620
+ res = differential_evolution(rosen, self.bounds, polish=pf, maxiter=1, rng=0)
1621
+ ref = differential_evolution(rosen, self.bounds, polish=True, maxiter=1, rng=0)
1622
+
1623
+ # res.success will be False because of the small number of iterations
1624
+ # The solution produced by DE would be bad after only one iteration.
1625
+ # However, we still expect a good answer if the polishing worked
1626
+ assert res.jac is not None
1627
+ assert_allclose(res.x, np.ones(N), atol=1e-6)
1628
+ # test that the `pf` callable is really used; it's not just truthy
1629
+ assert res.fun == pf.res.fun
1630
+ assert ref.fun != res.fun
1631
+
1632
+ def dummy_pf(func, x, **kwds):
1633
+ assert "bounds" in kwds
1634
+ assert isinstance(kwds["bounds"], Bounds)
1635
+ assert "constraints" in kwds
1636
+ return np.ones(N)
1637
+
1638
+ with assert_raises(
1639
+ ValueError,
1640
+ match="The result from a user defined polishing"
1641
+ ):
1642
+ differential_evolution(
1643
+ rosen,
1644
+ self.bounds,
1645
+ polish=dummy_pf,
1646
+ maxiter=1
1647
+ )
1648
+
1649
+ # check that output of polish==False followed by a manual polish
1650
+ # is the same as a callable(polish). Limit maxiter on DE so polisher
1651
+ # has to do all the work.
1652
+ def pf(func, x, **kwds):
1653
+ return minimize(func, x, method='L-BFGS-B', **kwds)
1654
+
1655
+ rng = np.random.default_rng(110980928209)
1656
+ res = differential_evolution(
1657
+ rosen, self.bounds, polish=False, rng=rng, maxiter=1
1658
+ )
1659
+ res = minimize(rosen, res.x, bounds=self.bounds, method='L-BFGS-B')
1660
+ rng = np.random.default_rng(110980928209)
1661
+ res2 = differential_evolution(
1662
+ rosen, self.bounds, polish=pf, rng=rng, maxiter=1
1663
+ )
1664
+ # could possibly do assert_allequal, but not sure about bitwise exactness
1665
+ # for repeated runs from same starting point
1666
+ assert_allclose(res.x, res2.x)
1667
+ assert_allclose(res.fun, res2.fun)
1668
+
1669
+ def test_constraint_violation_error_message(self):
1670
+
1671
+ def func(x):
1672
+ return np.cos(x[0]) + np.sin(x[1])
1673
+
1674
+ # Intentionally infeasible constraints.
1675
+ c0 = NonlinearConstraint(lambda x: x[1] - (x[0]-1)**2, 0, np.inf)
1676
+ c1 = NonlinearConstraint(lambda x: x[1] + x[0]**2, -np.inf, 0)
1677
+
1678
+ result = differential_evolution(func,
1679
+ bounds=[(-1, 2), (-1, 1)],
1680
+ constraints=[c0, c1],
1681
+ maxiter=10,
1682
+ polish=False,
1683
+ rng=864197532)
1684
+ assert result.success is False
1685
+ # The numerical value in the error message might be sensitive to
1686
+ # changes in the implementation. It can be updated if the code is
1687
+ # changed. The essential part of the test is that there is a number
1688
+ # after the '=', so if necessary, the text could be reduced to, say,
1689
+ # "MAXCV = 0.".
1690
+ assert "MAXCV = 0." in result.message
1691
+
1692
+ @pytest.mark.fail_slow(20) # fail-slow exception by request - see gh-20806
1693
+ def test_strategy_fn(self):
1694
+ # examines ability to customize strategy by mimicking one of the
1695
+ # in-built strategies
1696
+ parameter_count = 4
1697
+ popsize = 10
1698
+ bounds = [(0, 10.)] * parameter_count
1699
+ total_popsize = parameter_count * popsize
1700
+ mutation = 0.8
1701
+ recombination = 0.7
1702
+
1703
+ calls = [0]
1704
+ def custom_strategy_fn(candidate, population, rng=None):
1705
+ calls[0] += 1
1706
+ trial = np.copy(population[candidate])
1707
+ fill_point = rng.choice(parameter_count)
1708
+
1709
+ pool = np.arange(total_popsize)
1710
+ rng.shuffle(pool)
1711
+ idxs = pool[:2 + 1]
1712
+ idxs = idxs[idxs != candidate][:2]
1713
+
1714
+ r0, r1 = idxs[:2]
1715
+
1716
+ bprime = (population[0] + mutation *
1717
+ (population[r0] - population[r1]))
1718
+
1719
+ crossovers = rng.uniform(size=parameter_count)
1720
+ crossovers = crossovers < recombination
1721
+ crossovers[fill_point] = True
1722
+ trial = np.where(crossovers, bprime, trial)
1723
+ return trial
1724
+
1725
+ solver = DifferentialEvolutionSolver(
1726
+ rosen,
1727
+ bounds,
1728
+ popsize=popsize,
1729
+ recombination=recombination,
1730
+ mutation=mutation,
1731
+ maxiter=2,
1732
+ strategy=custom_strategy_fn,
1733
+ rng=10,
1734
+ polish=False
1735
+ )
1736
+ assert solver.strategy is custom_strategy_fn
1737
+ solver.solve()
1738
+ assert calls[0] > 0
1739
+
1740
+ # check custom strategy works with updating='deferred'
1741
+ res = differential_evolution(
1742
+ rosen, bounds, strategy=custom_strategy_fn, updating='deferred'
1743
+ )
1744
+ assert res.success
1745
+
1746
+ def custom_strategy_fn(candidate, population, rng=None):
1747
+ return np.array([1.0, 2.0])
1748
+
1749
+ with pytest.raises(RuntimeError, match="strategy*"):
1750
+ differential_evolution(
1751
+ rosen,
1752
+ bounds,
1753
+ strategy=custom_strategy_fn
1754
+ )
1755
+
1756
+ def test_reference_cycles(self):
1757
+ with assert_deallocated(DifferentialEvolutionSolver, rosen, [(0, 10)]*2):
1758
+ pass
loan-advisor-agent/.venv/Lib/site-packages/scipy/optimize/tests/test__dual_annealing.py ADDED
@@ -0,0 +1,415 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Dual annealing unit tests implementation.
2
+ # Copyright (c) 2018 Sylvain Gubian <sylvain.gubian@pmi.com>,
3
+ # Yang Xiang <yang.xiang@pmi.com>
4
+ # Author: Sylvain Gubian, PMP S.A.
5
+ """
6
+ Unit tests for the dual annealing global optimizer
7
+ """
8
+ from scipy.optimize import dual_annealing, Bounds
9
+
10
+ from scipy.optimize._dual_annealing import EnergyState
11
+ from scipy.optimize._dual_annealing import LocalSearchWrapper
12
+ from scipy.optimize._dual_annealing import ObjectiveFunWrapper
13
+ from scipy.optimize._dual_annealing import StrategyChain
14
+ from scipy.optimize._dual_annealing import VisitingDistribution
15
+ from scipy.optimize import rosen, rosen_der
16
+ import pytest
17
+ import numpy as np
18
+ from numpy.testing import assert_equal, assert_allclose, assert_array_less
19
+ from pytest import raises as assert_raises
20
+ from scipy._lib._util import check_random_state
21
+
22
+ import threading
23
+
24
+
25
+ class TestDualAnnealing:
26
+
27
+ def setup_method(self):
28
+ # A function that returns always infinity for initialization tests
29
+ self.weirdfunc = lambda x: np.inf
30
+ # 2-D bounds for testing function
31
+ self.ld_bounds = [(-5.12, 5.12)] * 2
32
+ # 4-D bounds for testing function
33
+ self.hd_bounds = self.ld_bounds * 4
34
+ # Number of values to be generated for testing visit function
35
+ self.nbtestvalues = 5000
36
+ self.high_temperature = 5230
37
+ self.low_temperature = 0.1
38
+ self.qv = 2.62
39
+ self.seed = 1234
40
+ self.nb_fun_call = threading.local()
41
+ self.ngev = threading.local()
42
+
43
+ def callback(self, x, f, context):
44
+ # For testing callback mechanism. Should stop for e <= 1 as
45
+ # the callback function returns True
46
+ if f <= 1.0:
47
+ return True
48
+
49
+ def func(self, x, args=()):
50
+ # Using Rastrigin function for performing tests
51
+ if args:
52
+ shift = args
53
+ else:
54
+ shift = 0
55
+ y = np.sum((x - shift) ** 2 - 10 * np.cos(2 * np.pi * (
56
+ x - shift))) + 10 * np.size(x) + shift
57
+ if not hasattr(self.nb_fun_call, 'c'):
58
+ self.nb_fun_call.c = 0
59
+ self.nb_fun_call.c += 1
60
+ return y
61
+
62
+ def rosen_der_wrapper(self, x, args=()):
63
+ if not hasattr(self.ngev, 'c'):
64
+ self.ngev.c = 0
65
+ self.ngev.c += 1
66
+ return rosen_der(x, *args)
67
+
68
+ # FIXME: there are some discontinuities in behaviour as a function of `qv`,
69
+ # this needs investigating - see gh-12384
70
+ @pytest.mark.parametrize('qv', [1.1, 1.41, 2, 2.62, 2.9])
71
+ def test_visiting_stepping(self, qv):
72
+ rng = np.random.default_rng(1234)
73
+ lu = list(zip(*self.ld_bounds))
74
+ lower = np.array(lu[0])
75
+ upper = np.array(lu[1])
76
+ dim = lower.size
77
+ vd = VisitingDistribution(lower, upper, qv, rng)
78
+ values = np.zeros(dim)
79
+ x_step_low = vd.visiting(values, 0, self.high_temperature)
80
+ # Make sure that only the first component is changed
81
+ assert_equal(np.not_equal(x_step_low, 0), True)
82
+ values = np.zeros(dim)
83
+ x_step_high = vd.visiting(values, dim, self.high_temperature)
84
+ # Make sure that component other than at dim has changed
85
+ assert_equal(np.not_equal(x_step_high[0], 0), True)
86
+
87
+ @pytest.mark.parametrize('qv', [2.25, 2.62, 2.9])
88
+ def test_visiting_dist_high_temperature(self, qv):
89
+ rng = np.random.default_rng(1234)
90
+ lu = list(zip(*self.ld_bounds))
91
+ lower = np.array(lu[0])
92
+ upper = np.array(lu[1])
93
+ vd = VisitingDistribution(lower, upper, qv, rng)
94
+ # values = np.zeros(self.nbtestvalues)
95
+ # for i in np.arange(self.nbtestvalues):
96
+ # values[i] = vd.visit_fn(self.high_temperature)
97
+ values = vd.visit_fn(self.high_temperature, self.nbtestvalues)
98
+
99
+ # Visiting distribution is a distorted version of Cauchy-Lorentz
100
+ # distribution, and as no 1st and higher moments (no mean defined,
101
+ # no variance defined).
102
+ # Check that big tails values are generated
103
+ assert_array_less(np.min(values), 1e-10)
104
+ assert_array_less(1e+10, np.max(values))
105
+
106
+ def test_reset(self):
107
+ owf = ObjectiveFunWrapper(self.weirdfunc)
108
+ lu = list(zip(*self.ld_bounds))
109
+ lower = np.array(lu[0])
110
+ upper = np.array(lu[1])
111
+ es = EnergyState(lower, upper)
112
+ assert_raises(ValueError, es.reset, owf, check_random_state(None))
113
+
114
+ def test_low_dim(self):
115
+ ret = dual_annealing(
116
+ self.func, self.ld_bounds, rng=self.seed)
117
+ assert_allclose(ret.fun, 0., atol=1e-12)
118
+ assert ret.success
119
+
120
+ @pytest.mark.fail_slow(10)
121
+ def test_high_dim(self):
122
+ ret = dual_annealing(self.func, self.hd_bounds, rng=self.seed)
123
+ assert_allclose(ret.fun, 0., atol=1e-12)
124
+ assert ret.success
125
+
126
+ def test_low_dim_no_ls(self):
127
+ ret = dual_annealing(self.func, self.ld_bounds,
128
+ no_local_search=True, seed=self.seed)
129
+ assert_allclose(ret.fun, 0., atol=1e-4)
130
+
131
+ @pytest.mark.fail_slow(10)
132
+ def test_high_dim_no_ls(self):
133
+ ret = dual_annealing(self.func, self.hd_bounds,
134
+ no_local_search=True, rng=self.seed)
135
+ assert_allclose(ret.fun, 0., atol=1.2e-4)
136
+
137
+ def test_nb_fun_call(self):
138
+ self.nb_fun_call.c = 0
139
+ ret = dual_annealing(self.func, self.ld_bounds, rng=self.seed)
140
+ assert_equal(self.nb_fun_call.c, ret.nfev)
141
+
142
+ def test_nb_fun_call_no_ls(self):
143
+ self.nb_fun_call.c = 0
144
+ ret = dual_annealing(self.func, self.ld_bounds,
145
+ no_local_search=True, rng=self.seed)
146
+ assert_equal(self.nb_fun_call.c, ret.nfev)
147
+
148
+ def test_max_reinit(self):
149
+ assert_raises(ValueError, dual_annealing, self.weirdfunc,
150
+ self.ld_bounds)
151
+
152
+ @pytest.mark.fail_slow(10)
153
+ def test_reproduce(self):
154
+ res1 = dual_annealing(self.func, self.ld_bounds, rng=self.seed)
155
+ res2 = dual_annealing(self.func, self.ld_bounds, rng=self.seed)
156
+ res3 = dual_annealing(self.func, self.ld_bounds, rng=self.seed)
157
+ # If we have reproducible results, x components found has to
158
+ # be exactly the same, which is not the case with no seeding
159
+ assert_equal(res1.x, res2.x)
160
+ assert_equal(res1.x, res3.x)
161
+
162
+ def test_rand_gen(self):
163
+ # check that np.random.Generator can be used (numpy >= 1.17)
164
+ # obtain a np.random.Generator object
165
+ rng = np.random.default_rng(1)
166
+
167
+ res1 = dual_annealing(self.func, self.ld_bounds, rng=rng)
168
+ # seed again
169
+ rng = np.random.default_rng(1)
170
+ res2 = dual_annealing(self.func, self.ld_bounds, rng=rng)
171
+ # If we have reproducible results, x components found has to
172
+ # be exactly the same, which is not the case with no seeding
173
+ assert_equal(res1.x, res2.x)
174
+
175
+ def test_bounds_integrity(self):
176
+ wrong_bounds = [(-5.12, 5.12), (1, 0), (5.12, 5.12)]
177
+ assert_raises(ValueError, dual_annealing, self.func,
178
+ wrong_bounds)
179
+
180
+ def test_bound_validity(self):
181
+ invalid_bounds = [(-5, 5), (-np.inf, 0), (-5, 5)]
182
+ assert_raises(ValueError, dual_annealing, self.func,
183
+ invalid_bounds)
184
+ invalid_bounds = [(-5, 5), (0, np.inf), (-5, 5)]
185
+ assert_raises(ValueError, dual_annealing, self.func,
186
+ invalid_bounds)
187
+ invalid_bounds = [(-5, 5), (0, np.nan), (-5, 5)]
188
+ assert_raises(ValueError, dual_annealing, self.func,
189
+ invalid_bounds)
190
+
191
+ def test_deprecated_local_search_options_bounds(self):
192
+ def func(x):
193
+ return np.sum((x - 5) * (x - 1))
194
+ bounds = list(zip([-6, -5], [6, 5]))
195
+ # Test bounds can be passed (see gh-10831)
196
+
197
+ with pytest.warns(RuntimeWarning, match=r"Method CG cannot handle "):
198
+ dual_annealing(
199
+ func,
200
+ bounds=bounds,
201
+ minimizer_kwargs={"method": "CG", "bounds": bounds})
202
+
203
+ def test_minimizer_kwargs_bounds(self):
204
+ def func(x):
205
+ return np.sum((x - 5) * (x - 1))
206
+ bounds = list(zip([-6, -5], [6, 5]))
207
+ # Test bounds can be passed (see gh-10831)
208
+ dual_annealing(
209
+ func,
210
+ bounds=bounds,
211
+ minimizer_kwargs={"method": "SLSQP", "bounds": bounds})
212
+
213
+ with pytest.warns(RuntimeWarning, match=r"Method CG cannot handle "):
214
+ dual_annealing(
215
+ func,
216
+ bounds=bounds,
217
+ minimizer_kwargs={"method": "CG", "bounds": bounds})
218
+
219
+ def test_max_fun_ls(self):
220
+ ret = dual_annealing(self.func, self.ld_bounds, maxfun=100,
221
+ rng=self.seed)
222
+
223
+ ls_max_iter = min(max(
224
+ len(self.ld_bounds) * LocalSearchWrapper.LS_MAXITER_RATIO,
225
+ LocalSearchWrapper.LS_MAXITER_MIN),
226
+ LocalSearchWrapper.LS_MAXITER_MAX)
227
+ assert ret.nfev <= 100 + ls_max_iter
228
+ assert not ret.success
229
+
230
+ def test_max_fun_no_ls(self):
231
+ ret = dual_annealing(self.func, self.ld_bounds,
232
+ no_local_search=True, maxfun=500, rng=self.seed)
233
+ assert ret.nfev <= 500
234
+ assert not ret.success
235
+
236
+ def test_maxiter(self):
237
+ ret = dual_annealing(self.func, self.ld_bounds, maxiter=700,
238
+ rng=self.seed)
239
+ assert ret.nit <= 700
240
+
241
+ # Testing that args are passed correctly for dual_annealing
242
+ def test_fun_args_ls(self):
243
+ ret = dual_annealing(self.func, self.ld_bounds,
244
+ args=((3.14159,)), rng=self.seed)
245
+ assert_allclose(ret.fun, 3.14159, atol=1e-6)
246
+
247
+ # Testing that args are passed correctly for pure simulated annealing
248
+ def test_fun_args_no_ls(self):
249
+ ret = dual_annealing(self.func, self.ld_bounds,
250
+ args=((3.14159, )), no_local_search=True,
251
+ rng=self.seed)
252
+ assert_allclose(ret.fun, 3.14159, atol=1e-4)
253
+
254
+ def test_callback_stop(self):
255
+ # Testing that callback make the algorithm stop for
256
+ # fun value <= 1.0 (see callback method)
257
+ ret = dual_annealing(self.func, self.ld_bounds,
258
+ callback=self.callback, rng=self.seed)
259
+ assert ret.fun <= 1.0
260
+ assert 'stop early' in ret.message[0]
261
+ assert not ret.success
262
+
263
+ @pytest.mark.parametrize('method, atol', [
264
+ ('Nelder-Mead', 2e-5),
265
+ ('COBYLA', 1e-5),
266
+ ('COBYQA', 1e-8),
267
+ ('Powell', 1e-8),
268
+ ('CG', 1e-8),
269
+ ('BFGS', 1e-8),
270
+ ('TNC', 1e-8),
271
+ ('SLSQP', 2e-7),
272
+ ])
273
+ def test_multi_ls_minimizer(self, method, atol):
274
+ ret = dual_annealing(self.func, self.ld_bounds,
275
+ minimizer_kwargs=dict(method=method),
276
+ rng=self.seed)
277
+ assert_allclose(ret.fun, 0., atol=atol)
278
+
279
+ def test_wrong_restart_temp(self):
280
+ assert_raises(ValueError, dual_annealing, self.func,
281
+ self.ld_bounds, restart_temp_ratio=1)
282
+ assert_raises(ValueError, dual_annealing, self.func,
283
+ self.ld_bounds, restart_temp_ratio=0)
284
+
285
+ def test_gradient_gnev(self):
286
+ minimizer_opts = {
287
+ 'jac': self.rosen_der_wrapper,
288
+ }
289
+ ret = dual_annealing(rosen, self.ld_bounds,
290
+ minimizer_kwargs=minimizer_opts,
291
+ rng=self.seed)
292
+ assert ret.njev == self.ngev.c
293
+
294
+ @pytest.mark.fail_slow(10)
295
+ def test_from_docstring(self):
296
+ def func(x):
297
+ return np.sum(x * x - 10 * np.cos(2 * np.pi * x)) + 10 * np.size(x)
298
+ lw = [-5.12] * 10
299
+ up = [5.12] * 10
300
+ ret = dual_annealing(func, bounds=list(zip(lw, up)), rng=1234)
301
+ assert_allclose(ret.x,
302
+ [-4.26437714e-09, -3.91699361e-09, -1.86149218e-09,
303
+ -3.97165720e-09, -6.29151648e-09, -6.53145322e-09,
304
+ -3.93616815e-09, -6.55623025e-09, -6.05775280e-09,
305
+ -5.00668935e-09], atol=4e-8)
306
+ assert_allclose(ret.fun, 0.000000, atol=5e-13)
307
+
308
+ @pytest.mark.parametrize('new_e, temp_step, accepted, accept_rate', [
309
+ (0, 100, 1000, 1.0097587941791923),
310
+ (0, 2, 1000, 1.2599210498948732),
311
+ (10, 100, 878, 0.8786035869128718),
312
+ (10, 60, 695, 0.6812920690579612),
313
+ (2, 100, 990, 0.9897404249173424),
314
+ ])
315
+ def test_accept_reject_probabilistic(
316
+ self, new_e, temp_step, accepted, accept_rate):
317
+ # Test accepts unconditionally with e < current_energy and
318
+ # probabilistically with e > current_energy
319
+
320
+ rs = check_random_state(123)
321
+
322
+ count_accepted = 0
323
+ iterations = 1000
324
+
325
+ accept_param = -5
326
+ current_energy = 1
327
+ for _ in range(iterations):
328
+ energy_state = EnergyState(lower=None, upper=None)
329
+ # Set energy state with current_energy, any location.
330
+ energy_state.update_current(current_energy, [0])
331
+
332
+ chain = StrategyChain(
333
+ accept_param, None, None, None, rs, energy_state)
334
+ # Normally this is set in run()
335
+ chain.temperature_step = temp_step
336
+
337
+ # Check if update is accepted.
338
+ chain.accept_reject(j=1, e=new_e, x_visit=[2])
339
+ if energy_state.current_energy == new_e:
340
+ count_accepted += 1
341
+
342
+ assert count_accepted == accepted
343
+
344
+ # Check accept rate
345
+ pqv = 1 - (1 - accept_param) * (new_e - current_energy) / temp_step
346
+ rate = 0 if pqv <= 0 else np.exp(np.log(pqv) / (1 - accept_param))
347
+
348
+ assert_allclose(rate, accept_rate)
349
+
350
+ @pytest.mark.fail_slow(10)
351
+ def test_bounds_class(self):
352
+ # test that result does not depend on the bounds type
353
+ def func(x):
354
+ f = np.sum(x * x - 10 * np.cos(2 * np.pi * x)) + 10 * np.size(x)
355
+ return f
356
+ lw = [-5.12] * 5
357
+ up = [5.12] * 5
358
+
359
+ # Unbounded global minimum is all zeros. Most bounds below will force
360
+ # a DV away from unbounded minimum and be active at solution.
361
+ up[0] = -2.0
362
+ up[1] = -1.0
363
+ lw[3] = 1.0
364
+ lw[4] = 2.0
365
+
366
+ # run optimizations
367
+ bounds = Bounds(lw, up)
368
+ ret_bounds_class = dual_annealing(func, bounds=bounds, rng=1234)
369
+
370
+ bounds_old = list(zip(lw, up))
371
+ ret_bounds_list = dual_annealing(func, bounds=bounds_old, rng=1234)
372
+
373
+ # test that found minima, function evaluations and iterations match
374
+ assert_allclose(ret_bounds_class.x, ret_bounds_list.x, atol=1e-8)
375
+ assert_allclose(ret_bounds_class.x, np.arange(-2, 3), atol=1e-7)
376
+ assert_allclose(ret_bounds_list.fun, ret_bounds_class.fun, atol=1e-9)
377
+ assert ret_bounds_list.nfev == ret_bounds_class.nfev
378
+
379
+ @pytest.mark.fail_slow(10)
380
+ def test_callable_jac_hess_with_args_gh11052(self):
381
+ # dual_annealing used to fail when `jac` was callable and `args` were
382
+ # used; check that this is resolved. Example is from gh-11052.
383
+
384
+ # extended to hess as part of closing gh20614
385
+ rng = np.random.default_rng(94253637693657847462)
386
+ def f(x, power):
387
+ return np.sum(np.exp(x ** power))
388
+
389
+ def jac(x, power):
390
+ return np.exp(x ** power) * power * x ** (power - 1)
391
+
392
+ def hess(x, power):
393
+ # calculated using WolframAlpha as d^2/dx^2 e^(x^p)
394
+ return np.diag(
395
+ power * np.exp(x ** power) * x ** (power - 2) *
396
+ (power * x ** power + power - 1)
397
+ )
398
+
399
+ def hessp(x, p, power):
400
+ return hess(x, power) @ p
401
+
402
+ res1 = dual_annealing(f, args=(2, ), bounds=[[0, 1], [0, 1]], rng=rng,
403
+ minimizer_kwargs=dict(method='L-BFGS-B'))
404
+ res2 = dual_annealing(f, args=(2, ), bounds=[[0, 1], [0, 1]], rng=rng,
405
+ minimizer_kwargs=dict(method='L-BFGS-B',
406
+ jac=jac))
407
+ res3 = dual_annealing(f, args=(2, ), bounds=[[0, 1], [0, 1]], rng=rng,
408
+ minimizer_kwargs=dict(method='newton-cg',
409
+ jac=jac, hess=hess))
410
+ res4 = dual_annealing(f, args=(2, ), bounds=[[0, 1], [0, 1]], rng=rng,
411
+ minimizer_kwargs=dict(method='newton-cg',
412
+ jac=jac, hessp=hessp))
413
+ assert_allclose(res1.fun, res2.fun, rtol=1e-6)
414
+ assert_allclose(res3.fun, res2.fun, rtol=1e-6)
415
+ assert_allclose(res4.fun, res2.fun, rtol=1e-6)
loan-advisor-agent/.venv/Lib/site-packages/scipy/optimize/tests/test__linprog_clean_inputs.py ADDED
@@ -0,0 +1,320 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Unit test for Linear Programming via Simplex Algorithm.
3
+ """
4
+ import warnings
5
+
6
+ from copy import deepcopy
7
+ from datetime import date
8
+
9
+ import numpy as np
10
+ from numpy.testing import assert_, assert_allclose, assert_equal
11
+ from numpy.exceptions import VisibleDeprecationWarning
12
+ from pytest import raises as assert_raises
13
+
14
+ from scipy.optimize._linprog_util import _clean_inputs, _LPProblem
15
+
16
+
17
+ def test_aliasing():
18
+ """
19
+ Test for ensuring that no objects referred to by `lp` attributes,
20
+ `c`, `A_ub`, `b_ub`, `A_eq`, `b_eq`, `bounds`, have been modified
21
+ by `_clean_inputs` as a side effect.
22
+ """
23
+ lp = _LPProblem(
24
+ c=1,
25
+ A_ub=[[1]],
26
+ b_ub=[1],
27
+ A_eq=[[1]],
28
+ b_eq=[1],
29
+ bounds=(-np.inf, np.inf)
30
+ )
31
+ lp_copy = deepcopy(lp)
32
+
33
+ _clean_inputs(lp)
34
+
35
+ assert_(lp.c == lp_copy.c, "c modified by _clean_inputs")
36
+ assert_(lp.A_ub == lp_copy.A_ub, "A_ub modified by _clean_inputs")
37
+ assert_(lp.b_ub == lp_copy.b_ub, "b_ub modified by _clean_inputs")
38
+ assert_(lp.A_eq == lp_copy.A_eq, "A_eq modified by _clean_inputs")
39
+ assert_(lp.b_eq == lp_copy.b_eq, "b_eq modified by _clean_inputs")
40
+ assert_(lp.bounds == lp_copy.bounds, "bounds modified by _clean_inputs")
41
+
42
+
43
+ def test_aliasing2():
44
+ """
45
+ Similar purpose as `test_aliasing` above.
46
+ """
47
+ lp = _LPProblem(
48
+ c=np.array([1, 1]),
49
+ A_ub=np.array([[1, 1], [2, 2]]),
50
+ b_ub=np.array([[1], [1]]),
51
+ A_eq=np.array([[1, 1]]),
52
+ b_eq=np.array([1]),
53
+ bounds=[(-np.inf, np.inf), (None, 1)]
54
+ )
55
+ lp_copy = deepcopy(lp)
56
+
57
+ _clean_inputs(lp)
58
+
59
+ assert_allclose(lp.c, lp_copy.c, err_msg="c modified by _clean_inputs")
60
+ assert_allclose(lp.A_ub, lp_copy.A_ub, err_msg="A_ub modified by _clean_inputs")
61
+ assert_allclose(lp.b_ub, lp_copy.b_ub, err_msg="b_ub modified by _clean_inputs")
62
+ assert_allclose(lp.A_eq, lp_copy.A_eq, err_msg="A_eq modified by _clean_inputs")
63
+ assert_allclose(lp.b_eq, lp_copy.b_eq, err_msg="b_eq modified by _clean_inputs")
64
+ assert_(lp.bounds == lp_copy.bounds, "bounds modified by _clean_inputs")
65
+
66
+
67
+ def test_missing_inputs():
68
+ c = [1, 2]
69
+ A_ub = np.array([[1, 1], [2, 2]])
70
+ b_ub = np.array([1, 1])
71
+ A_eq = np.array([[1, 1], [2, 2]])
72
+ b_eq = np.array([1, 1])
73
+
74
+ assert_raises(TypeError, _clean_inputs)
75
+ assert_raises(TypeError, _clean_inputs, _LPProblem(c=None))
76
+ assert_raises(ValueError, _clean_inputs, _LPProblem(c=c, A_ub=A_ub))
77
+ assert_raises(ValueError, _clean_inputs, _LPProblem(c=c, A_ub=A_ub, b_ub=None))
78
+ assert_raises(ValueError, _clean_inputs, _LPProblem(c=c, b_ub=b_ub))
79
+ assert_raises(ValueError, _clean_inputs, _LPProblem(c=c, A_ub=None, b_ub=b_ub))
80
+ assert_raises(ValueError, _clean_inputs, _LPProblem(c=c, A_eq=A_eq))
81
+ assert_raises(ValueError, _clean_inputs, _LPProblem(c=c, A_eq=A_eq, b_eq=None))
82
+ assert_raises(ValueError, _clean_inputs, _LPProblem(c=c, b_eq=b_eq))
83
+ assert_raises(ValueError, _clean_inputs, _LPProblem(c=c, A_eq=None, b_eq=b_eq))
84
+
85
+
86
+ def test_too_many_dimensions():
87
+ cb = [1, 2, 3, 4]
88
+ rng = np.random.default_rng(1234)
89
+ A = rng.random((4, 4))
90
+ bad2D = [[1, 2], [3, 4]]
91
+ bad3D = rng.random((4, 4, 4))
92
+ assert_raises(ValueError, _clean_inputs, _LPProblem(c=bad2D, A_ub=A, b_ub=cb))
93
+ assert_raises(ValueError, _clean_inputs, _LPProblem(c=cb, A_ub=bad3D, b_ub=cb))
94
+ assert_raises(ValueError, _clean_inputs, _LPProblem(c=cb, A_ub=A, b_ub=bad2D))
95
+ assert_raises(ValueError, _clean_inputs, _LPProblem(c=cb, A_eq=bad3D, b_eq=cb))
96
+ assert_raises(ValueError, _clean_inputs, _LPProblem(c=cb, A_eq=A, b_eq=bad2D))
97
+
98
+
99
+ def test_too_few_dimensions():
100
+ rng = np.random.default_rng(1234)
101
+ bad = rng.random((4, 4)).ravel()
102
+ cb = rng.random(4)
103
+ assert_raises(ValueError, _clean_inputs, _LPProblem(c=cb, A_ub=bad, b_ub=cb))
104
+ assert_raises(ValueError, _clean_inputs, _LPProblem(c=cb, A_eq=bad, b_eq=cb))
105
+
106
+
107
+ def test_inconsistent_dimensions():
108
+ m = 2
109
+ n = 4
110
+ c = [1, 2, 3, 4]
111
+
112
+ rng = np.random.default_rng(122390)
113
+ Agood = rng.random((m, n))
114
+ Abad = rng.random((m, n + 1))
115
+ bgood = rng.random(m)
116
+ bbad = rng.random(m + 1)
117
+ boundsbad = [(0, 1)] * (n + 1)
118
+ assert_raises(ValueError, _clean_inputs, _LPProblem(c=c, A_ub=Abad, b_ub=bgood))
119
+ assert_raises(ValueError, _clean_inputs, _LPProblem(c=c, A_ub=Agood, b_ub=bbad))
120
+ assert_raises(ValueError, _clean_inputs, _LPProblem(c=c, A_eq=Abad, b_eq=bgood))
121
+ assert_raises(ValueError, _clean_inputs, _LPProblem(c=c, A_eq=Agood, b_eq=bbad))
122
+ assert_raises(ValueError, _clean_inputs, _LPProblem(c=c, bounds=boundsbad))
123
+ with warnings.catch_warnings():
124
+ warnings.filterwarnings(
125
+ "ignore", "Creating an ndarray from ragged", VisibleDeprecationWarning)
126
+ assert_raises(ValueError, _clean_inputs,
127
+ _LPProblem(c=c, bounds=[[1, 2], [2, 3], [3, 4], [4, 5, 6]]))
128
+
129
+
130
+ def test_type_errors():
131
+ lp = _LPProblem(
132
+ c=[1, 2],
133
+ A_ub=np.array([[1, 1], [2, 2]]),
134
+ b_ub=np.array([1, 1]),
135
+ A_eq=np.array([[1, 1], [2, 2]]),
136
+ b_eq=np.array([1, 1]),
137
+ bounds=[(0, 1)]
138
+ )
139
+ bad = "hello"
140
+
141
+ assert_raises(TypeError, _clean_inputs, lp._replace(c=bad))
142
+ assert_raises(TypeError, _clean_inputs, lp._replace(A_ub=bad))
143
+ assert_raises(TypeError, _clean_inputs, lp._replace(b_ub=bad))
144
+ assert_raises(TypeError, _clean_inputs, lp._replace(A_eq=bad))
145
+ assert_raises(TypeError, _clean_inputs, lp._replace(b_eq=bad))
146
+
147
+ assert_raises(ValueError, _clean_inputs, lp._replace(bounds=bad))
148
+ assert_raises(ValueError, _clean_inputs, lp._replace(bounds="hi"))
149
+ assert_raises(ValueError, _clean_inputs, lp._replace(bounds=["hi"]))
150
+ assert_raises(ValueError, _clean_inputs, lp._replace(bounds=[("hi")]))
151
+ assert_raises(ValueError, _clean_inputs, lp._replace(bounds=[(1, "")]))
152
+ assert_raises(ValueError, _clean_inputs, lp._replace(bounds=[(1, 2), (1, "")]))
153
+ assert_raises(TypeError, _clean_inputs,
154
+ lp._replace(bounds=[(1, date(2020, 2, 29))]))
155
+ assert_raises(ValueError, _clean_inputs, lp._replace(bounds=[[[1, 2]]]))
156
+
157
+
158
+ def test_non_finite_errors():
159
+ lp = _LPProblem(
160
+ c=[1, 2],
161
+ A_ub=np.array([[1, 1], [2, 2]]),
162
+ b_ub=np.array([1, 1]),
163
+ A_eq=np.array([[1, 1], [2, 2]]),
164
+ b_eq=np.array([1, 1]),
165
+ bounds=[(0, 1)]
166
+ )
167
+ assert_raises(ValueError, _clean_inputs, lp._replace(c=[0, None]))
168
+ assert_raises(ValueError, _clean_inputs, lp._replace(c=[np.inf, 0]))
169
+ assert_raises(ValueError, _clean_inputs, lp._replace(c=[0, -np.inf]))
170
+ assert_raises(ValueError, _clean_inputs, lp._replace(c=[np.nan, 0]))
171
+
172
+ assert_raises(ValueError, _clean_inputs, lp._replace(A_ub=[[1, 2], [None, 1]]))
173
+ assert_raises(ValueError, _clean_inputs, lp._replace(b_ub=[np.inf, 1]))
174
+ assert_raises(ValueError, _clean_inputs, lp._replace(A_eq=[[1, 2], [1, -np.inf]]))
175
+ assert_raises(ValueError, _clean_inputs, lp._replace(b_eq=[1, np.nan]))
176
+
177
+
178
+ def test__clean_inputs1():
179
+ lp = _LPProblem(
180
+ c=[1, 2],
181
+ A_ub=[[1, 1], [2, 2]],
182
+ b_ub=[1, 1],
183
+ A_eq=[[1, 1], [2, 2]],
184
+ b_eq=[1, 1],
185
+ bounds=None
186
+ )
187
+
188
+ lp_cleaned = _clean_inputs(lp)
189
+
190
+ assert_allclose(lp_cleaned.c, np.array(lp.c))
191
+ assert_allclose(lp_cleaned.A_ub, np.array(lp.A_ub))
192
+ assert_allclose(lp_cleaned.b_ub, np.array(lp.b_ub))
193
+ assert_allclose(lp_cleaned.A_eq, np.array(lp.A_eq))
194
+ assert_allclose(lp_cleaned.b_eq, np.array(lp.b_eq))
195
+ assert_equal(lp_cleaned.bounds, [(0, np.inf)] * 2)
196
+
197
+ assert_(lp_cleaned.c.shape == (2,), "")
198
+ assert_(lp_cleaned.A_ub.shape == (2, 2), "")
199
+ assert_(lp_cleaned.b_ub.shape == (2,), "")
200
+ assert_(lp_cleaned.A_eq.shape == (2, 2), "")
201
+ assert_(lp_cleaned.b_eq.shape == (2,), "")
202
+
203
+
204
+ def test__clean_inputs2():
205
+ lp = _LPProblem(
206
+ c=1,
207
+ A_ub=[[1]],
208
+ b_ub=1,
209
+ A_eq=[[1]],
210
+ b_eq=1,
211
+ bounds=(0, 1)
212
+ )
213
+
214
+ lp_cleaned = _clean_inputs(lp)
215
+
216
+ assert_allclose(lp_cleaned.c, np.array(lp.c))
217
+ assert_allclose(lp_cleaned.A_ub, np.array(lp.A_ub))
218
+ assert_allclose(lp_cleaned.b_ub, np.array(lp.b_ub))
219
+ assert_allclose(lp_cleaned.A_eq, np.array(lp.A_eq))
220
+ assert_allclose(lp_cleaned.b_eq, np.array(lp.b_eq))
221
+ assert_equal(lp_cleaned.bounds, [(0, 1)])
222
+
223
+ assert_(lp_cleaned.c.shape == (1,), "")
224
+ assert_(lp_cleaned.A_ub.shape == (1, 1), "")
225
+ assert_(lp_cleaned.b_ub.shape == (1,), "")
226
+ assert_(lp_cleaned.A_eq.shape == (1, 1), "")
227
+ assert_(lp_cleaned.b_eq.shape == (1,), "")
228
+
229
+
230
+ def test__clean_inputs3():
231
+ rng = np.random.default_rng(1890908)
232
+ lp = _LPProblem(
233
+ c=[[1, 2]],
234
+ A_ub=rng.random((2, 2)),
235
+ b_ub=[[1], [2]],
236
+ A_eq=rng.random((2, 2)),
237
+ b_eq=[[1], [2]],
238
+ bounds=[(0, 1)]
239
+ )
240
+
241
+ lp_cleaned = _clean_inputs(lp)
242
+
243
+ assert_allclose(lp_cleaned.c, np.array([1, 2]))
244
+ assert_allclose(lp_cleaned.b_ub, np.array([1, 2]))
245
+ assert_allclose(lp_cleaned.b_eq, np.array([1, 2]))
246
+ assert_equal(lp_cleaned.bounds, [(0, 1)] * 2)
247
+
248
+ assert_(lp_cleaned.c.shape == (2,), "")
249
+ assert_(lp_cleaned.b_ub.shape == (2,), "")
250
+ assert_(lp_cleaned.b_eq.shape == (2,), "")
251
+
252
+
253
+ def test_bad_bounds():
254
+ lp = _LPProblem(c=[1, 2])
255
+
256
+ assert_raises(ValueError, _clean_inputs, lp._replace(bounds=(1, 2, 2)))
257
+ assert_raises(ValueError, _clean_inputs, lp._replace(bounds=[(1, 2, 2)]))
258
+ with warnings.catch_warnings():
259
+ warnings.filterwarnings(
260
+ "ignore", "Creating an ndarray from ragged", VisibleDeprecationWarning)
261
+ assert_raises(ValueError, _clean_inputs,
262
+ lp._replace(bounds=[(1, 2), (1, 2, 2)]))
263
+ assert_raises(ValueError, _clean_inputs,
264
+ lp._replace(bounds=[(1, 2), (1, 2), (1, 2)]))
265
+
266
+ lp = _LPProblem(c=[1, 2, 3, 4])
267
+
268
+ assert_raises(ValueError, _clean_inputs,
269
+ lp._replace(bounds=[(1, 2, 3, 4), (1, 2, 3, 4)]))
270
+
271
+
272
+ def test_good_bounds():
273
+ lp = _LPProblem(c=[1, 2])
274
+
275
+ lp_cleaned = _clean_inputs(lp) # lp.bounds is None by default
276
+ assert_equal(lp_cleaned.bounds, [(0, np.inf)] * 2)
277
+
278
+ lp_cleaned = _clean_inputs(lp._replace(bounds=[]))
279
+ assert_equal(lp_cleaned.bounds, [(0, np.inf)] * 2)
280
+
281
+ lp_cleaned = _clean_inputs(lp._replace(bounds=[[]]))
282
+ assert_equal(lp_cleaned.bounds, [(0, np.inf)] * 2)
283
+
284
+ lp_cleaned = _clean_inputs(lp._replace(bounds=(1, 2)))
285
+ assert_equal(lp_cleaned.bounds, [(1, 2)] * 2)
286
+
287
+ lp_cleaned = _clean_inputs(lp._replace(bounds=[(1, 2)]))
288
+ assert_equal(lp_cleaned.bounds, [(1, 2)] * 2)
289
+
290
+ lp_cleaned = _clean_inputs(lp._replace(bounds=[(1, None)]))
291
+ assert_equal(lp_cleaned.bounds, [(1, np.inf)] * 2)
292
+
293
+ lp_cleaned = _clean_inputs(lp._replace(bounds=[(None, 1)]))
294
+ assert_equal(lp_cleaned.bounds, [(-np.inf, 1)] * 2)
295
+
296
+ lp_cleaned = _clean_inputs(lp._replace(bounds=[(None, None), (-np.inf, None)]))
297
+ assert_equal(lp_cleaned.bounds, [(-np.inf, np.inf)] * 2)
298
+
299
+ lp = _LPProblem(c=[1, 2, 3, 4])
300
+
301
+ lp_cleaned = _clean_inputs(lp) # lp.bounds is None by default
302
+ assert_equal(lp_cleaned.bounds, [(0, np.inf)] * 4)
303
+
304
+ lp_cleaned = _clean_inputs(lp._replace(bounds=(1, 2)))
305
+ assert_equal(lp_cleaned.bounds, [(1, 2)] * 4)
306
+
307
+ lp_cleaned = _clean_inputs(lp._replace(bounds=[(1, 2)]))
308
+ assert_equal(lp_cleaned.bounds, [(1, 2)] * 4)
309
+
310
+ lp_cleaned = _clean_inputs(lp._replace(bounds=[(1, None)]))
311
+ assert_equal(lp_cleaned.bounds, [(1, np.inf)] * 4)
312
+
313
+ lp_cleaned = _clean_inputs(lp._replace(bounds=[(None, 1)]))
314
+ assert_equal(lp_cleaned.bounds, [(-np.inf, 1)] * 4)
315
+
316
+ lp_cleaned = _clean_inputs(lp._replace(bounds=[(None, None),
317
+ (-np.inf, None),
318
+ (None, np.inf),
319
+ (-np.inf, np.inf)]))
320
+ assert_equal(lp_cleaned.bounds, [(-np.inf, np.inf)] * 4)
loan-advisor-agent/.venv/Lib/site-packages/scipy/optimize/tests/test__numdiff.py ADDED
@@ -0,0 +1,885 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import math
2
+ from itertools import product
3
+
4
+ import numpy as np
5
+ from numpy.testing import assert_allclose, assert_equal, assert_
6
+ import pytest
7
+ from pytest import raises as assert_raises
8
+
9
+ from scipy._lib._util import MapWrapper, _ScalarFunctionWrapper
10
+ from scipy.sparse import csr_array, csc_array, lil_array
11
+
12
+ from scipy.optimize._numdiff import (
13
+ _adjust_scheme_to_bounds, approx_derivative, check_derivative,
14
+ group_columns, _eps_for_method, _compute_absolute_step)
15
+ from scipy.optimize import rosen
16
+
17
+
18
+ def test_group_columns():
19
+ structure = [
20
+ [1, 1, 0, 0, 0, 0],
21
+ [1, 1, 1, 0, 0, 0],
22
+ [0, 1, 1, 1, 0, 0],
23
+ [0, 0, 1, 1, 1, 0],
24
+ [0, 0, 0, 1, 1, 1],
25
+ [0, 0, 0, 0, 1, 1],
26
+ [0, 0, 0, 0, 0, 0]
27
+ ]
28
+ for transform in [np.asarray, csr_array, csc_array, lil_array]:
29
+ A = transform(structure)
30
+ order = np.arange(6)
31
+ groups_true = np.array([0, 1, 2, 0, 1, 2])
32
+ groups = group_columns(A, order)
33
+ assert_equal(groups, groups_true)
34
+
35
+ order = [1, 2, 4, 3, 5, 0]
36
+ groups_true = np.array([2, 0, 1, 2, 0, 1])
37
+ groups = group_columns(A, order)
38
+ assert_equal(groups, groups_true)
39
+
40
+ # Test repeatability.
41
+ groups_1 = group_columns(A)
42
+ groups_2 = group_columns(A)
43
+ assert_equal(groups_1, groups_2)
44
+
45
+
46
+ def test_correct_fp_eps():
47
+ # check that relative step size is correct for FP size
48
+ EPS = np.finfo(np.float64).eps
49
+ relative_step = {"2-point": EPS**0.5,
50
+ "3-point": EPS**(1/3),
51
+ "cs": EPS**0.5}
52
+ for method in ['2-point', '3-point', 'cs']:
53
+ assert_allclose(
54
+ _eps_for_method(np.float64, np.float64, method),
55
+ relative_step[method])
56
+ assert_allclose(
57
+ _eps_for_method(np.complex128, np.complex128, method),
58
+ relative_step[method]
59
+ )
60
+
61
+ # check another FP size
62
+ EPS = np.finfo(np.float32).eps
63
+ relative_step = {"2-point": EPS**0.5,
64
+ "3-point": EPS**(1/3),
65
+ "cs": EPS**0.5}
66
+
67
+ for method in ['2-point', '3-point', 'cs']:
68
+ assert_allclose(
69
+ _eps_for_method(np.float64, np.float32, method),
70
+ relative_step[method]
71
+ )
72
+ assert_allclose(
73
+ _eps_for_method(np.float32, np.float64, method),
74
+ relative_step[method]
75
+ )
76
+ assert_allclose(
77
+ _eps_for_method(np.float32, np.float32, method),
78
+ relative_step[method]
79
+ )
80
+
81
+
82
+ class TestAdjustSchemeToBounds:
83
+ def test_no_bounds(self):
84
+ x0 = np.zeros(3)
85
+ h = np.full(3, 1e-2)
86
+ inf_lower = np.empty_like(x0)
87
+ inf_upper = np.empty_like(x0)
88
+ inf_lower.fill(-np.inf)
89
+ inf_upper.fill(np.inf)
90
+
91
+ h_adjusted, one_sided = _adjust_scheme_to_bounds(
92
+ x0, h, 1, '1-sided', inf_lower, inf_upper)
93
+ assert_allclose(h_adjusted, h)
94
+ assert_(np.all(one_sided))
95
+
96
+ h_adjusted, one_sided = _adjust_scheme_to_bounds(
97
+ x0, h, 2, '1-sided', inf_lower, inf_upper)
98
+ assert_allclose(h_adjusted, h)
99
+ assert_(np.all(one_sided))
100
+
101
+ h_adjusted, one_sided = _adjust_scheme_to_bounds(
102
+ x0, h, 1, '2-sided', inf_lower, inf_upper)
103
+ assert_allclose(h_adjusted, h)
104
+ assert_(np.all(~one_sided))
105
+
106
+ h_adjusted, one_sided = _adjust_scheme_to_bounds(
107
+ x0, h, 2, '2-sided', inf_lower, inf_upper)
108
+ assert_allclose(h_adjusted, h)
109
+ assert_(np.all(~one_sided))
110
+
111
+ def test_with_bound(self):
112
+ x0 = np.array([0.0, 0.85, -0.85])
113
+ lb = -np.ones(3)
114
+ ub = np.ones(3)
115
+ h = np.array([1, 1, -1]) * 1e-1
116
+
117
+ h_adjusted, _ = _adjust_scheme_to_bounds(x0, h, 1, '1-sided', lb, ub)
118
+ assert_allclose(h_adjusted, h)
119
+
120
+ h_adjusted, _ = _adjust_scheme_to_bounds(x0, h, 2, '1-sided', lb, ub)
121
+ assert_allclose(h_adjusted, np.array([1, -1, 1]) * 1e-1)
122
+
123
+ h_adjusted, one_sided = _adjust_scheme_to_bounds(
124
+ x0, h, 1, '2-sided', lb, ub)
125
+ assert_allclose(h_adjusted, np.abs(h))
126
+ assert_(np.all(~one_sided))
127
+
128
+ h_adjusted, one_sided = _adjust_scheme_to_bounds(
129
+ x0, h, 2, '2-sided', lb, ub)
130
+ assert_allclose(h_adjusted, np.array([1, -1, 1]) * 1e-1)
131
+ assert_equal(one_sided, np.array([False, True, True]))
132
+
133
+ def test_tight_bounds(self):
134
+ lb = np.array([-0.03, -0.03])
135
+ ub = np.array([0.05, 0.05])
136
+ x0 = np.array([0.0, 0.03])
137
+ h = np.array([-0.1, -0.1])
138
+
139
+ h_adjusted, _ = _adjust_scheme_to_bounds(x0, h, 1, '1-sided', lb, ub)
140
+ assert_allclose(h_adjusted, np.array([0.05, -0.06]))
141
+
142
+ h_adjusted, _ = _adjust_scheme_to_bounds(x0, h, 2, '1-sided', lb, ub)
143
+ assert_allclose(h_adjusted, np.array([0.025, -0.03]))
144
+
145
+ h_adjusted, one_sided = _adjust_scheme_to_bounds(
146
+ x0, h, 1, '2-sided', lb, ub)
147
+ assert_allclose(h_adjusted, np.array([0.03, -0.03]))
148
+ assert_equal(one_sided, np.array([False, True]))
149
+
150
+ h_adjusted, one_sided = _adjust_scheme_to_bounds(
151
+ x0, h, 2, '2-sided', lb, ub)
152
+ assert_allclose(h_adjusted, np.array([0.015, -0.015]))
153
+ assert_equal(one_sided, np.array([False, True]))
154
+
155
+
156
+ class TestApproxDerivativesDense:
157
+ def fun_scalar_scalar(self, x):
158
+ return np.sinh(x)
159
+
160
+ def jac_scalar_scalar(self, x):
161
+ return np.cosh(x)
162
+
163
+ def fun_scalar_vector(self, x):
164
+ return np.array([x[0]**2, np.tan(x[0]), np.exp(x[0])])
165
+
166
+ def jac_scalar_vector(self, x):
167
+ return np.array(
168
+ [2 * x[0], np.cos(x[0]) ** -2, np.exp(x[0])]).reshape(-1, 1)
169
+
170
+ def fun_vector_scalar(self, x):
171
+ return np.sin(x[0] * x[1]) * np.log(x[0])
172
+
173
+ def wrong_dimensions_fun(self, x):
174
+ return np.array([x**2, np.tan(x), np.exp(x)])
175
+
176
+ def jac_vector_scalar(self, x):
177
+ return np.array([
178
+ x[1] * np.cos(x[0] * x[1]) * np.log(x[0]) +
179
+ np.sin(x[0] * x[1]) / x[0],
180
+ x[0] * np.cos(x[0] * x[1]) * np.log(x[0])
181
+ ])
182
+
183
+ def fun_vector_vector(self, x):
184
+ return np.array([
185
+ x[0] * np.sin(x[1]),
186
+ x[1] * np.cos(x[0]),
187
+ x[0] ** 3 * x[1] ** -0.5
188
+ ])
189
+
190
+ def fun_vector_vector_with_arg(self, x, arg):
191
+ """Used to test passing custom arguments with check_derivative()"""
192
+ assert arg == 42
193
+ return np.array([
194
+ x[0] * np.sin(x[1]),
195
+ x[1] * np.cos(x[0]),
196
+ x[0] ** 3 * x[1] ** -0.5
197
+ ])
198
+
199
+ def jac_vector_vector(self, x):
200
+ return np.array([
201
+ [np.sin(x[1]), x[0] * np.cos(x[1])],
202
+ [-x[1] * np.sin(x[0]), np.cos(x[0])],
203
+ [3 * x[0] ** 2 * x[1] ** -0.5, -0.5 * x[0] ** 3 * x[1] ** -1.5]
204
+ ])
205
+
206
+ def jac_vector_vector_with_arg(self, x, arg):
207
+ """Used to test passing custom arguments with check_derivative()"""
208
+ assert arg == 42
209
+ return np.array([
210
+ [np.sin(x[1]), x[0] * np.cos(x[1])],
211
+ [-x[1] * np.sin(x[0]), np.cos(x[0])],
212
+ [3 * x[0] ** 2 * x[1] ** -0.5, -0.5 * x[0] ** 3 * x[1] ** -1.5]
213
+ ])
214
+
215
+ def fun_parametrized(self, x, c0, c1=1.0):
216
+ return np.array([np.exp(c0 * x[0]), np.exp(c1 * x[1])])
217
+
218
+ def jac_parametrized(self, x, c0, c1=0.1):
219
+ return np.array([
220
+ [c0 * np.exp(c0 * x[0]), 0],
221
+ [0, c1 * np.exp(c1 * x[1])]
222
+ ])
223
+
224
+ def fun_with_nan(self, x):
225
+ return x if np.abs(x) <= 1e-8 else np.nan
226
+
227
+ def jac_with_nan(self, x):
228
+ return 1.0 if np.abs(x) <= 1e-8 else np.nan
229
+
230
+ def fun_zero_jacobian(self, x):
231
+ return np.array([x[0] * x[1], np.cos(x[0] * x[1])])
232
+
233
+ def jac_zero_jacobian(self, x):
234
+ return np.array([
235
+ [x[1], x[0]],
236
+ [-x[1] * np.sin(x[0] * x[1]), -x[0] * np.sin(x[0] * x[1])]
237
+ ])
238
+
239
+ def jac_non_numpy(self, x):
240
+ # x can be a scalar or an array [val].
241
+ # Cast to true scalar before handing over to math.exp
242
+ xp = np.asarray(x).item()
243
+ return math.exp(xp)
244
+
245
+ def test_scalar_scalar(self):
246
+ x0 = 1.0
247
+ jac_diff_2 = approx_derivative(self.fun_scalar_scalar, x0,
248
+ method='2-point')
249
+ jac_diff_3 = approx_derivative(self.fun_scalar_scalar, x0)
250
+ jac_diff_4 = approx_derivative(self.fun_scalar_scalar, x0,
251
+ method='cs')
252
+ jac_true = self.jac_scalar_scalar(x0)
253
+ assert_allclose(jac_diff_2, jac_true, rtol=1e-6)
254
+ assert_allclose(jac_diff_3, jac_true, rtol=1e-9)
255
+ assert_allclose(jac_diff_4, jac_true, rtol=1e-12)
256
+
257
+ def test_scalar_scalar_abs_step(self):
258
+ # can approx_derivative use abs_step?
259
+ x0 = 1.0
260
+ jac_diff_2 = approx_derivative(self.fun_scalar_scalar, x0,
261
+ method='2-point', abs_step=1.49e-8)
262
+ jac_diff_3 = approx_derivative(self.fun_scalar_scalar, x0,
263
+ abs_step=1.49e-8)
264
+ jac_diff_4 = approx_derivative(self.fun_scalar_scalar, x0,
265
+ method='cs', abs_step=1.49e-8)
266
+ jac_true = self.jac_scalar_scalar(x0)
267
+ assert_allclose(jac_diff_2, jac_true, rtol=1e-6)
268
+ assert_allclose(jac_diff_3, jac_true, rtol=1e-9)
269
+ assert_allclose(jac_diff_4, jac_true, rtol=1e-12)
270
+
271
+ def test_scalar_vector(self):
272
+ x0 = 0.5
273
+ with MapWrapper(2) as mapper:
274
+ jac_diff_2 = approx_derivative(self.fun_scalar_vector, x0,
275
+ method='2-point', workers=mapper)
276
+ jac_diff_3 = approx_derivative(self.fun_scalar_vector, x0, workers=map)
277
+ jac_diff_4 = approx_derivative(self.fun_scalar_vector, x0,
278
+ method='cs', workers=None)
279
+ jac_true = self.jac_scalar_vector(np.atleast_1d(x0))
280
+ assert_allclose(jac_diff_2, jac_true, rtol=1e-6)
281
+ assert_allclose(jac_diff_3, jac_true, rtol=1e-9)
282
+ assert_allclose(jac_diff_4, jac_true, rtol=1e-12)
283
+
284
+ @pytest.mark.fail_slow(5.0)
285
+ def test_workers_evaluations_and_nfev(self):
286
+ # check that nfev consumed by approx_derivative is tracked properly
287
+ # and that parallel evaluation is same as series
288
+ x0 = [0.5, 1.5, 2.0]
289
+ with MapWrapper(2) as mapper:
290
+ md2, mdct2 = approx_derivative(rosen, x0,
291
+ method='2-point', workers=mapper,
292
+ full_output=True)
293
+ md3, mdct3 = approx_derivative(rosen, x0,
294
+ workers=mapper, full_output=True)
295
+ # supply a number for workers. This is not normally recommended
296
+ # for upstream workers as setting up processes incurs a large overhead
297
+ md4, mdct4 = approx_derivative(rosen, x0,
298
+ method='cs', workers=2,
299
+ full_output=True)
300
+
301
+ sfr = _ScalarFunctionWrapper(rosen)
302
+ d2, dct2 = approx_derivative(sfr, x0, method='2-point', full_output=True)
303
+ assert_equal(dct2['nfev'], sfr.nfev)
304
+ sfr.nfev = 0
305
+ d3, dct3 = approx_derivative(sfr, x0, full_output=True)
306
+ assert_equal(dct3['nfev'], sfr.nfev)
307
+ sfr.nfev = 0
308
+ d4, dct4 = approx_derivative(sfr, x0, method='cs', full_output=True)
309
+ assert_equal(dct4['nfev'], sfr.nfev)
310
+
311
+ assert_equal(mdct2['nfev'], dct2['nfev'])
312
+ assert_equal(mdct3['nfev'], dct3['nfev'])
313
+ assert_equal(mdct4['nfev'], dct4['nfev'])
314
+ # also check that gradients are equivalent
315
+ assert_equal(md2, d2)
316
+ assert_equal(md3, d3)
317
+ assert_equal(md4, d4)
318
+
319
+ def test_vector_scalar(self):
320
+ x0 = np.array([100.0, -0.5])
321
+ jac_diff_2 = approx_derivative(self.fun_vector_scalar, x0,
322
+ method='2-point')
323
+ jac_diff_3 = approx_derivative(self.fun_vector_scalar, x0)
324
+ jac_diff_4 = approx_derivative(self.fun_vector_scalar, x0,
325
+ method='cs')
326
+ jac_true = self.jac_vector_scalar(x0)
327
+ assert_allclose(jac_diff_2, jac_true, rtol=1e-6)
328
+ assert_allclose(jac_diff_3, jac_true, rtol=1e-7)
329
+ assert_allclose(jac_diff_4, jac_true, rtol=1e-12)
330
+
331
+ def test_vector_scalar_abs_step(self):
332
+ # can approx_derivative use abs_step?
333
+ x0 = np.array([100.0, -0.5])
334
+ jac_diff_2 = approx_derivative(self.fun_vector_scalar, x0,
335
+ method='2-point', abs_step=1.49e-8)
336
+ jac_diff_3 = approx_derivative(self.fun_vector_scalar, x0,
337
+ abs_step=1.49e-8, rel_step=np.inf)
338
+ jac_diff_4 = approx_derivative(self.fun_vector_scalar, x0,
339
+ method='cs', abs_step=1.49e-8)
340
+ jac_true = self.jac_vector_scalar(x0)
341
+ assert_allclose(jac_diff_2, jac_true, rtol=1e-6)
342
+ assert_allclose(jac_diff_3, jac_true, rtol=3e-9)
343
+ assert_allclose(jac_diff_4, jac_true, rtol=1e-12)
344
+
345
+ def test_vector_vector(self):
346
+ x0 = np.array([-100.0, 0.2])
347
+ jac_diff_2 = approx_derivative(self.fun_vector_vector, x0,
348
+ method='2-point')
349
+ jac_diff_3 = approx_derivative(self.fun_vector_vector, x0)
350
+ with MapWrapper(2) as mapper:
351
+ jac_diff_4 = approx_derivative(self.fun_vector_vector, x0,
352
+ method='cs', workers=mapper)
353
+ jac_true = self.jac_vector_vector(x0)
354
+ assert_allclose(jac_diff_2, jac_true, rtol=1e-5)
355
+ assert_allclose(jac_diff_3, jac_true, rtol=1e-6)
356
+ assert_allclose(jac_diff_4, jac_true, rtol=1e-12)
357
+
358
+ def test_wrong_dimensions(self):
359
+ x0 = 1.0
360
+ assert_raises(RuntimeError, approx_derivative,
361
+ self.wrong_dimensions_fun, x0)
362
+ f0 = self.wrong_dimensions_fun(np.atleast_1d(x0))
363
+ assert_raises(ValueError, approx_derivative,
364
+ self.wrong_dimensions_fun, x0, f0=f0)
365
+
366
+ def test_custom_rel_step(self):
367
+ x0 = np.array([-0.1, 0.1])
368
+ jac_diff_2 = approx_derivative(self.fun_vector_vector, x0,
369
+ method='2-point', rel_step=1e-4)
370
+ jac_diff_3 = approx_derivative(self.fun_vector_vector, x0,
371
+ rel_step=1e-4)
372
+ jac_true = self.jac_vector_vector(x0)
373
+ assert_allclose(jac_diff_2, jac_true, rtol=1e-2)
374
+ assert_allclose(jac_diff_3, jac_true, rtol=1e-4)
375
+
376
+ def test_options(self):
377
+ x0 = np.array([1.0, 1.0])
378
+ c0 = -1.0
379
+ c1 = 1.0
380
+ lb = 0.0
381
+ ub = 2.0
382
+ f0 = self.fun_parametrized(x0, c0, c1=c1)
383
+ rel_step = np.array([-1e-6, 1e-7])
384
+ jac_true = self.jac_parametrized(x0, c0, c1)
385
+ jac_diff_2 = approx_derivative(
386
+ self.fun_parametrized, x0, method='2-point', rel_step=rel_step,
387
+ f0=f0, args=(c0,), kwargs=dict(c1=c1), bounds=(lb, ub))
388
+ jac_diff_3 = approx_derivative(
389
+ self.fun_parametrized, x0, rel_step=rel_step,
390
+ f0=f0, args=(c0,), kwargs=dict(c1=c1), bounds=(lb, ub))
391
+ assert_allclose(jac_diff_2, jac_true, rtol=1e-6)
392
+ assert_allclose(jac_diff_3, jac_true, rtol=1e-9)
393
+
394
+ def test_with_bounds_2_point(self):
395
+ lb = -np.ones(2)
396
+ ub = np.ones(2)
397
+
398
+ x0 = np.array([-2.0, 0.2])
399
+ assert_raises(ValueError, approx_derivative,
400
+ self.fun_vector_vector, x0, bounds=(lb, ub))
401
+
402
+ x0 = np.array([-1.0, 1.0])
403
+ jac_diff = approx_derivative(self.fun_vector_vector, x0,
404
+ method='2-point', bounds=(lb, ub))
405
+ jac_true = self.jac_vector_vector(x0)
406
+ assert_allclose(jac_diff, jac_true, rtol=1e-6)
407
+
408
+ def test_with_bounds_3_point(self):
409
+ lb = np.array([1.0, 1.0])
410
+ ub = np.array([2.0, 2.0])
411
+
412
+ x0 = np.array([1.0, 2.0])
413
+ jac_true = self.jac_vector_vector(x0)
414
+
415
+ jac_diff = approx_derivative(self.fun_vector_vector, x0)
416
+ assert_allclose(jac_diff, jac_true, rtol=1e-9)
417
+
418
+ jac_diff = approx_derivative(self.fun_vector_vector, x0,
419
+ bounds=(lb, np.inf))
420
+ assert_allclose(jac_diff, jac_true, rtol=1e-9)
421
+
422
+ jac_diff = approx_derivative(self.fun_vector_vector, x0,
423
+ bounds=(-np.inf, ub))
424
+ assert_allclose(jac_diff, jac_true, rtol=1e-9)
425
+
426
+ jac_diff = approx_derivative(self.fun_vector_vector, x0,
427
+ bounds=(lb, ub))
428
+ assert_allclose(jac_diff, jac_true, rtol=1e-9)
429
+
430
+ def test_tight_bounds(self):
431
+ x0 = np.array([10.0, 10.0])
432
+ lb = x0 - 3e-9
433
+ ub = x0 + 2e-9
434
+ jac_true = self.jac_vector_vector(x0)
435
+ jac_diff = approx_derivative(
436
+ self.fun_vector_vector, x0, method='2-point', bounds=(lb, ub))
437
+ assert_allclose(jac_diff, jac_true, rtol=1e-6)
438
+ jac_diff = approx_derivative(
439
+ self.fun_vector_vector, x0, method='2-point',
440
+ rel_step=1e-6, bounds=(lb, ub))
441
+ assert_allclose(jac_diff, jac_true, rtol=1e-6)
442
+
443
+ jac_diff = approx_derivative(
444
+ self.fun_vector_vector, x0, bounds=(lb, ub))
445
+ assert_allclose(jac_diff, jac_true, rtol=1e-6)
446
+ jac_diff = approx_derivative(
447
+ self.fun_vector_vector, x0, rel_step=1e-6, bounds=(lb, ub))
448
+ assert_allclose(jac_true, jac_diff, rtol=1e-6)
449
+
450
+ def test_bound_switches(self):
451
+ lb = -1e-8
452
+ ub = 1e-8
453
+ x0 = 0.0
454
+ jac_true = self.jac_with_nan(x0)
455
+ jac_diff_2 = approx_derivative(
456
+ self.fun_with_nan, x0, method='2-point', rel_step=1e-6,
457
+ bounds=(lb, ub))
458
+ jac_diff_3 = approx_derivative(
459
+ self.fun_with_nan, x0, rel_step=1e-6, bounds=(lb, ub))
460
+ assert_allclose(jac_diff_2, jac_true, rtol=1e-6)
461
+ assert_allclose(jac_diff_3, jac_true, rtol=1e-9)
462
+
463
+ x0 = 1e-8
464
+ jac_true = self.jac_with_nan(x0)
465
+ jac_diff_2 = approx_derivative(
466
+ self.fun_with_nan, x0, method='2-point', rel_step=1e-6,
467
+ bounds=(lb, ub))
468
+ jac_diff_3 = approx_derivative(
469
+ self.fun_with_nan, x0, rel_step=1e-6, bounds=(lb, ub))
470
+ assert_allclose(jac_diff_2, jac_true, rtol=1e-6)
471
+ assert_allclose(jac_diff_3, jac_true, rtol=1e-9)
472
+
473
+ def test_non_numpy(self):
474
+ x0 = 1.0
475
+ jac_true = self.jac_non_numpy(x0)
476
+ jac_diff_2 = approx_derivative(self.jac_non_numpy, x0,
477
+ method='2-point')
478
+ jac_diff_3 = approx_derivative(self.jac_non_numpy, x0)
479
+ assert_allclose(jac_diff_2, jac_true, rtol=1e-6)
480
+ assert_allclose(jac_diff_3, jac_true, rtol=1e-8)
481
+
482
+ # math.exp cannot handle complex arguments, hence this raises
483
+ assert_raises(TypeError, approx_derivative, self.jac_non_numpy, x0,
484
+ **dict(method='cs'))
485
+
486
+ def test_fp(self):
487
+ # checks that approx_derivative works for FP size other than 64.
488
+ # Example is derived from the minimal working example in gh12991.
489
+ rng = np.random.default_rng(1)
490
+
491
+ def func(p, x):
492
+ return p[0] + p[1] * x
493
+
494
+ def err(p, x, y):
495
+ return func(p, x) - y
496
+
497
+ x = np.linspace(0, 1, 100, dtype=np.float64)
498
+ y = rng.random(size=100, dtype=np.float64)
499
+ p0 = np.array([-1.0, -1.0])
500
+
501
+ jac_fp64 = approx_derivative(err, p0, method='2-point', args=(x, y))
502
+
503
+ # parameter vector is float32, func output is float64
504
+ jac_fp = approx_derivative(err, p0.astype(np.float32),
505
+ method='2-point', args=(x, y))
506
+ assert err(p0, x, y).dtype == np.float64
507
+ assert_allclose(jac_fp, jac_fp64, atol=1e-3)
508
+
509
+ # parameter vector is float64, func output is float32
510
+ def err_fp32(p):
511
+ assert p.dtype == np.float32
512
+ return err(p, x, y).astype(np.float32)
513
+
514
+ jac_fp = approx_derivative(err_fp32, p0.astype(np.float32),
515
+ method='2-point')
516
+ assert_allclose(jac_fp, jac_fp64, atol=1e-3)
517
+
518
+ # check upper bound of error on the derivative for 2-point
519
+ def f(x):
520
+ return np.sin(x)
521
+ def g(x):
522
+ return np.cos(x)
523
+ def hess(x):
524
+ return -np.sin(x)
525
+
526
+ def calc_atol(h, x0, f, hess, EPS):
527
+ # truncation error
528
+ t0 = h / 2 * max(np.abs(hess(x0)), np.abs(hess(x0 + h)))
529
+ # roundoff error. There may be a divisor (>1) missing from
530
+ # the following line, so this contribution is possibly
531
+ # overestimated
532
+ t1 = EPS / h * max(np.abs(f(x0)), np.abs(f(x0 + h)))
533
+ return t0 + t1
534
+
535
+ for dtype in [np.float16, np.float32, np.float64]:
536
+ EPS = np.finfo(dtype).eps
537
+ x0 = np.array(1.0).astype(dtype)
538
+ h = _compute_absolute_step(None, x0, f(x0), '2-point')
539
+ atol = calc_atol(h, x0, f, hess, EPS)
540
+ err = approx_derivative(f, x0, method='2-point',
541
+ abs_step=h) - g(x0)
542
+ assert abs(err) < atol
543
+
544
+ def test_check_derivative(self):
545
+ x0 = np.array([-10.0, 10])
546
+ accuracy = check_derivative(self.fun_vector_vector,
547
+ self.jac_vector_vector, x0)
548
+ assert_(accuracy < 1e-9)
549
+ accuracy = check_derivative(self.fun_vector_vector,
550
+ self.jac_vector_vector, x0)
551
+ assert_(accuracy < 1e-6)
552
+
553
+ x0 = np.array([0.0, 0.0])
554
+ accuracy = check_derivative(self.fun_zero_jacobian,
555
+ self.jac_zero_jacobian, x0)
556
+ assert_(accuracy == 0)
557
+ accuracy = check_derivative(self.fun_zero_jacobian,
558
+ self.jac_zero_jacobian, x0)
559
+ assert_(accuracy == 0)
560
+
561
+ def test_check_derivative_with_kwargs(self):
562
+ x0 = np.array([-10.0, 10])
563
+ accuracy = check_derivative(self.fun_vector_vector_with_arg,
564
+ self.jac_vector_vector_with_arg,
565
+ x0,
566
+ kwargs={'arg': 42})
567
+ assert_(accuracy < 1e-9)
568
+
569
+
570
+ class TestApproxDerivativeSparse:
571
+ # Example from Numerical Optimization 2nd edition, p. 198.
572
+ def setup_method(self):
573
+ self.rng = np.random.default_rng(121091202)
574
+ self.n = 50
575
+ self.lb = -0.1 * (1 + np.arange(self.n))
576
+ self.ub = 0.1 * (1 + np.arange(self.n))
577
+ self.x0 = np.empty(self.n)
578
+ self.x0[::2] = (1 - 1e-7) * self.lb[::2]
579
+ self.x0[1::2] = (1 - 1e-7) * self.ub[1::2]
580
+
581
+ self.J_true = self.jac(self.x0)
582
+
583
+ def fun(self, x):
584
+ e = x[1:]**3 - x[:-1]**2
585
+ return np.hstack((0, 3 * e)) + np.hstack((2 * e, 0))
586
+
587
+ def jac(self, x):
588
+ n = x.size
589
+ J = np.zeros((n, n))
590
+ J[0, 0] = -4 * x[0]
591
+ J[0, 1] = 6 * x[1]**2
592
+ for i in range(1, n - 1):
593
+ J[i, i - 1] = -6 * x[i-1]
594
+ J[i, i] = 9 * x[i]**2 - 4 * x[i]
595
+ J[i, i + 1] = 6 * x[i+1]**2
596
+ J[-1, -1] = 9 * x[-1]**2
597
+ J[-1, -2] = -6 * x[-2]
598
+
599
+ return J
600
+
601
+ def structure(self, n):
602
+ A = np.zeros((n, n), dtype=int)
603
+ A[0, 0] = 1
604
+ A[0, 1] = 1
605
+ for i in range(1, n - 1):
606
+ A[i, i - 1: i + 2] = 1
607
+ A[-1, -1] = 1
608
+ A[-1, -2] = 1
609
+
610
+ return A
611
+
612
+ @pytest.mark.fail_slow(5)
613
+ def test_all(self):
614
+ A = self.structure(self.n)
615
+ order = np.arange(self.n)
616
+ groups_1 = group_columns(A, order)
617
+ self.rng.shuffle(order)
618
+ groups_2 = group_columns(A, order)
619
+
620
+ with MapWrapper(2) as mapper:
621
+ for method, groups, l, u, mf in product(
622
+ ['2-point', '3-point', 'cs'], [groups_1, groups_2],
623
+ [-np.inf, self.lb], [np.inf, self.ub], [map, mapper]):
624
+ J = approx_derivative(self.fun, self.x0, method=method,
625
+ bounds=(l, u), sparsity=(A, groups),
626
+ workers=mf)
627
+ assert_(isinstance(J, csr_array))
628
+ assert_allclose(J.toarray(), self.J_true, rtol=1e-6)
629
+
630
+ rel_step = np.full_like(self.x0, 1e-8)
631
+ rel_step[::2] *= -1
632
+ J = approx_derivative(self.fun, self.x0, method=method,
633
+ rel_step=rel_step, sparsity=(A, groups),
634
+ workers=mf)
635
+ assert_allclose(J.toarray(), self.J_true, rtol=1e-5)
636
+
637
+ def test_no_precomputed_groups(self):
638
+ A = self.structure(self.n)
639
+ J = approx_derivative(self.fun, self.x0, sparsity=A)
640
+ assert_allclose(J.toarray(), self.J_true, rtol=1e-6)
641
+
642
+ def test_equivalence(self):
643
+ structure = np.ones((self.n, self.n), dtype=int)
644
+ groups = np.arange(self.n)
645
+ for method in ['2-point', '3-point', 'cs']:
646
+ J_dense = approx_derivative(self.fun, self.x0, method=method)
647
+ J_sparse = approx_derivative(
648
+ self.fun, self.x0, sparsity=(structure, groups), method=method)
649
+ assert_allclose(J_dense, J_sparse.toarray(),
650
+ rtol=5e-16, atol=7e-15)
651
+
652
+ def test_check_derivative(self):
653
+ def jac(x):
654
+ return csr_array(self.jac(x))
655
+
656
+ accuracy = check_derivative(self.fun, jac, self.x0,
657
+ bounds=(self.lb, self.ub))
658
+ assert_(accuracy < 1e-9)
659
+
660
+ accuracy = check_derivative(self.fun, jac, self.x0,
661
+ bounds=(self.lb, self.ub))
662
+ assert_(accuracy < 1e-9)
663
+
664
+
665
+ class TestApproxDerivativeLinearOperator:
666
+
667
+ def fun_scalar_scalar(self, x):
668
+ return np.sinh(x)
669
+
670
+ def jac_scalar_scalar(self, x):
671
+ return np.cosh(x)
672
+
673
+ def fun_scalar_vector(self, x):
674
+ return np.array([x[0]**2, np.tan(x[0]), np.exp(x[0])])
675
+
676
+ def jac_scalar_vector(self, x):
677
+ return np.array(
678
+ [2 * x[0], np.cos(x[0]) ** -2, np.exp(x[0])]).reshape(-1, 1)
679
+
680
+ def fun_vector_scalar(self, x):
681
+ return np.sin(x[0] * x[1]) * np.log(x[0])
682
+
683
+ def jac_vector_scalar(self, x):
684
+ return np.array([
685
+ x[1] * np.cos(x[0] * x[1]) * np.log(x[0]) +
686
+ np.sin(x[0] * x[1]) / x[0],
687
+ x[0] * np.cos(x[0] * x[1]) * np.log(x[0])
688
+ ])
689
+
690
+ def fun_vector_vector(self, x):
691
+ return np.array([
692
+ x[0] * np.sin(x[1]),
693
+ x[1] * np.cos(x[0]),
694
+ x[0] ** 3 * x[1] ** -0.5
695
+ ])
696
+
697
+ def jac_vector_vector(self, x):
698
+ return np.array([
699
+ [np.sin(x[1]), x[0] * np.cos(x[1])],
700
+ [-x[1] * np.sin(x[0]), np.cos(x[0])],
701
+ [3 * x[0] ** 2 * x[1] ** -0.5, -0.5 * x[0] ** 3 * x[1] ** -1.5]
702
+ ])
703
+
704
+ def test_scalar_scalar(self):
705
+ x0 = 1.0
706
+ jac_diff_2 = approx_derivative(self.fun_scalar_scalar, x0,
707
+ method='2-point',
708
+ as_linear_operator=True)
709
+ jac_diff_3 = approx_derivative(self.fun_scalar_scalar, x0,
710
+ as_linear_operator=True)
711
+ jac_diff_4 = approx_derivative(self.fun_scalar_scalar, x0,
712
+ method='cs',
713
+ as_linear_operator=True)
714
+ jac_true = self.jac_scalar_scalar(x0)
715
+ rng = np.random.default_rng(11290049580398)
716
+ for i in range(10):
717
+ p = rng.uniform(-10, 10, size=(1,))
718
+ assert_allclose(jac_diff_2.dot(p), jac_true*p,
719
+ rtol=1e-5)
720
+ assert_allclose(jac_diff_3.dot(p), jac_true*p,
721
+ rtol=5e-6)
722
+ assert_allclose(jac_diff_4.dot(p), jac_true*p,
723
+ rtol=5e-6)
724
+
725
+ def test_scalar_vector(self):
726
+ x0 = 0.5
727
+ jac_diff_2 = approx_derivative(self.fun_scalar_vector, x0,
728
+ method='2-point',
729
+ as_linear_operator=True)
730
+ jac_diff_3 = approx_derivative(self.fun_scalar_vector, x0,
731
+ as_linear_operator=True)
732
+ jac_diff_4 = approx_derivative(self.fun_scalar_vector, x0,
733
+ method='cs',
734
+ as_linear_operator=True)
735
+ jac_true = self.jac_scalar_vector(np.atleast_1d(x0))
736
+ rng = np.random.default_rng(11290049580398)
737
+ for i in range(10):
738
+ p = rng.uniform(-10, 10, size=(1,))
739
+ assert_allclose(jac_diff_2.dot(p), jac_true.dot(p),
740
+ rtol=1e-5)
741
+ assert_allclose(jac_diff_3.dot(p), jac_true.dot(p),
742
+ rtol=5e-6)
743
+ assert_allclose(jac_diff_4.dot(p), jac_true.dot(p),
744
+ rtol=5e-6)
745
+
746
+ def test_vector_scalar(self):
747
+ x0 = np.array([100.0, -0.5])
748
+ jac_diff_2 = approx_derivative(self.fun_vector_scalar, x0,
749
+ method='2-point',
750
+ as_linear_operator=True)
751
+ jac_diff_3 = approx_derivative(self.fun_vector_scalar, x0,
752
+ as_linear_operator=True)
753
+ jac_diff_4 = approx_derivative(self.fun_vector_scalar, x0,
754
+ method='cs',
755
+ as_linear_operator=True)
756
+ jac_true = self.jac_vector_scalar(x0)
757
+ rng = np.random.default_rng(11290049580398)
758
+ for i in range(10):
759
+ p = rng.uniform(-10, 10, size=x0.shape)
760
+ assert_allclose(jac_diff_2.dot(p), np.atleast_1d(jac_true.dot(p)),
761
+ rtol=1e-5)
762
+ assert_allclose(jac_diff_3.dot(p), np.atleast_1d(jac_true.dot(p)),
763
+ rtol=5e-6)
764
+ assert_allclose(jac_diff_4.dot(p), np.atleast_1d(jac_true.dot(p)),
765
+ rtol=1e-7)
766
+
767
+ def test_vector_vector(self):
768
+ x0 = np.array([-100.0, 0.2])
769
+ jac_diff_2 = approx_derivative(self.fun_vector_vector, x0,
770
+ method='2-point',
771
+ as_linear_operator=True)
772
+ jac_diff_3 = approx_derivative(self.fun_vector_vector, x0,
773
+ as_linear_operator=True)
774
+ jac_diff_4 = approx_derivative(self.fun_vector_vector, x0,
775
+ method='cs',
776
+ as_linear_operator=True)
777
+ jac_true = self.jac_vector_vector(x0)
778
+ rng = np.random.default_rng(11290049580398)
779
+ for i in range(10):
780
+ p = rng.uniform(-10, 10, size=x0.shape)
781
+ assert_allclose(jac_diff_2.dot(p), jac_true.dot(p), rtol=1e-5)
782
+ assert_allclose(jac_diff_3.dot(p), jac_true.dot(p), rtol=1e-6)
783
+ assert_allclose(jac_diff_4.dot(p), jac_true.dot(p), rtol=1e-7)
784
+
785
+ def test_exception(self):
786
+ x0 = np.array([-100.0, 0.2])
787
+ assert_raises(ValueError, approx_derivative,
788
+ self.fun_vector_vector, x0,
789
+ method='2-point', bounds=(1, np.inf))
790
+
791
+
792
+ def test_absolute_step_sign():
793
+ # test for gh12487
794
+ # if an absolute step is specified for 2-point differences make sure that
795
+ # the side corresponds to the step. i.e. if step is positive then forward
796
+ # differences should be used, if step is negative then backwards
797
+ # differences should be used.
798
+
799
+ # function has double discontinuity at x = [-1, -1]
800
+ # first component is \/, second component is /\
801
+ def f(x):
802
+ return -np.abs(x[0] + 1) + np.abs(x[1] + 1)
803
+
804
+ # check that the forward difference is used
805
+ grad = approx_derivative(f, [-1, -1], method='2-point', abs_step=1e-8)
806
+ assert_allclose(grad, [-1.0, 1.0])
807
+
808
+ # check that the backwards difference is used
809
+ grad = approx_derivative(f, [-1, -1], method='2-point', abs_step=-1e-8)
810
+ assert_allclose(grad, [1.0, -1.0])
811
+
812
+ # check that the forwards difference is used with a step for both
813
+ # parameters
814
+ grad = approx_derivative(
815
+ f, [-1, -1], method='2-point', abs_step=[1e-8, 1e-8]
816
+ )
817
+ assert_allclose(grad, [-1.0, 1.0])
818
+
819
+ # check that we can mix forward/backwards steps.
820
+ grad = approx_derivative(
821
+ f, [-1, -1], method='2-point', abs_step=[1e-8, -1e-8]
822
+ )
823
+ assert_allclose(grad, [-1.0, -1.0])
824
+ grad = approx_derivative(
825
+ f, [-1, -1], method='2-point', abs_step=[-1e-8, 1e-8]
826
+ )
827
+ assert_allclose(grad, [1.0, 1.0])
828
+
829
+ # the forward step should reverse to a backwards step if it runs into a
830
+ # bound
831
+ # This is kind of tested in TestAdjustSchemeToBounds, but only for a lower level
832
+ # function.
833
+ grad = approx_derivative(
834
+ f, [-1, -1], method='2-point', abs_step=1e-8,
835
+ bounds=(-np.inf, -1)
836
+ )
837
+ assert_allclose(grad, [1.0, -1.0])
838
+
839
+ grad = approx_derivative(
840
+ f, [-1, -1], method='2-point', abs_step=-1e-8, bounds=(-1, np.inf)
841
+ )
842
+ assert_allclose(grad, [-1.0, 1.0])
843
+
844
+
845
+ def test__compute_absolute_step():
846
+ # tests calculation of absolute step from rel_step
847
+ methods = ['2-point', '3-point', 'cs']
848
+
849
+ x0 = np.array([1e-5, 0, 1, 1e5])
850
+
851
+ EPS = np.finfo(np.float64).eps
852
+ relative_step = {
853
+ "2-point": EPS**0.5,
854
+ "3-point": EPS**(1/3),
855
+ "cs": EPS**0.5
856
+ }
857
+ f0 = np.array(1.0)
858
+
859
+ for method in methods:
860
+ rel_step = relative_step[method]
861
+ correct_step = np.array([rel_step,
862
+ rel_step * 1.,
863
+ rel_step * 1.,
864
+ rel_step * np.abs(x0[3])])
865
+
866
+ abs_step = _compute_absolute_step(None, x0, f0, method)
867
+ assert_allclose(abs_step, correct_step)
868
+
869
+ sign_x0 = (-x0 >= 0).astype(float) * 2 - 1
870
+ abs_step = _compute_absolute_step(None, -x0, f0, method)
871
+ assert_allclose(abs_step, sign_x0 * correct_step)
872
+
873
+ # if a relative step is provided it should be used
874
+ rel_step = np.array([0.1, 1, 10, 100])
875
+ correct_step = np.array([rel_step[0] * x0[0],
876
+ relative_step['2-point'],
877
+ rel_step[2] * 1.,
878
+ rel_step[3] * np.abs(x0[3])])
879
+
880
+ abs_step = _compute_absolute_step(rel_step, x0, f0, '2-point')
881
+ assert_allclose(abs_step, correct_step)
882
+
883
+ sign_x0 = (-x0 >= 0).astype(float) * 2 - 1
884
+ abs_step = _compute_absolute_step(rel_step, -x0, f0, '2-point')
885
+ assert_allclose(abs_step, sign_x0 * correct_step)
loan-advisor-agent/.venv/Lib/site-packages/scipy/optimize/tests/test__remove_redundancy.py ADDED
@@ -0,0 +1,227 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Unit test for Linear Programming via Simplex Algorithm.
3
+ """
4
+
5
+ # TODO: add tests for:
6
+ # https://github.com/scipy/scipy/issues/5400
7
+ # https://github.com/scipy/scipy/issues/6690
8
+
9
+ import numpy as np
10
+ from numpy.testing import (
11
+ assert_,
12
+ assert_allclose,
13
+ assert_equal)
14
+
15
+ from .test_linprog import magic_square
16
+ from scipy.optimize._remove_redundancy import _remove_redundancy_svd
17
+ from scipy.optimize._remove_redundancy import _remove_redundancy_pivot_dense
18
+ from scipy.optimize._remove_redundancy import _remove_redundancy_pivot_sparse
19
+ from scipy.optimize._remove_redundancy import _remove_redundancy_id
20
+
21
+ from scipy.sparse import csc_array
22
+
23
+
24
+ def redundancy_removed(A, B):
25
+ """Checks whether a matrix contains only independent rows of another"""
26
+ for rowA in A:
27
+ # `rowA in B` is not a reliable check
28
+ for rowB in B:
29
+ if np.all(rowA == rowB):
30
+ break
31
+ else:
32
+ return False
33
+ return A.shape[0] == np.linalg.matrix_rank(A) == np.linalg.matrix_rank(B)
34
+
35
+
36
+ class RRCommonTests:
37
+ def setup_method(self):
38
+ self.rng = np.random.default_rng(2017)
39
+
40
+ def test_no_redundancy(self):
41
+ m, n = 10, 10
42
+ A0 = self.rng.random((m, n))
43
+ b0 = self.rng.random(m)
44
+ A1, b1, status, message = self.rr(A0, b0)
45
+ assert_allclose(A0, A1)
46
+ assert_allclose(b0, b1)
47
+ assert_equal(status, 0)
48
+
49
+ def test_infeasible_zero_row(self):
50
+ A = np.eye(3)
51
+ A[1, :] = 0
52
+ b = self.rng.random(3)
53
+ A1, b1, status, message = self.rr(A, b)
54
+ assert_equal(status, 2)
55
+
56
+ def test_remove_zero_row(self):
57
+ A = np.eye(3)
58
+ A[1, :] = 0
59
+ b = self.rng.random(3)
60
+ b[1] = 0
61
+ A1, b1, status, message = self.rr(A, b)
62
+ assert_equal(status, 0)
63
+ assert_allclose(A1, A[[0, 2], :])
64
+ assert_allclose(b1, b[[0, 2]])
65
+
66
+ def test_infeasible_m_gt_n(self):
67
+ m, n = 20, 10
68
+ A0 = self.rng.random((m, n))
69
+ b0 = self.rng.random(m)
70
+ A1, b1, status, message = self.rr(A0, b0)
71
+ assert_equal(status, 2)
72
+
73
+ def test_infeasible_m_eq_n(self):
74
+ m, n = 10, 10
75
+ A0 = self.rng.random((m, n))
76
+ b0 = self.rng.random(m)
77
+ A0[-1, :] = 2 * A0[-2, :]
78
+ A1, b1, status, message = self.rr(A0, b0)
79
+ assert_equal(status, 2)
80
+
81
+ def test_infeasible_m_lt_n(self):
82
+ m, n = 9, 10
83
+ A0 = self.rng.random((m, n))
84
+ b0 = self.rng.random(m)
85
+ A0[-1, :] = np.arange(m - 1).dot(A0[:-1])
86
+ A1, b1, status, message = self.rr(A0, b0)
87
+ assert_equal(status, 2)
88
+
89
+ def test_m_gt_n(self):
90
+ rng = np.random.default_rng(2032)
91
+ m, n = 20, 10
92
+ A0 = rng.random((m, n))
93
+ b0 = rng.random(m)
94
+ x = np.linalg.solve(A0[:n, :], b0[:n])
95
+ b0[n:] = A0[n:, :].dot(x)
96
+ A1, b1, status, message = self.rr(A0, b0)
97
+ assert_equal(status, 0)
98
+ assert_equal(A1.shape[0], n)
99
+ assert_equal(np.linalg.matrix_rank(A1), n)
100
+
101
+ def test_m_gt_n_rank_deficient(self):
102
+ m, n = 20, 10
103
+ A0 = np.zeros((m, n))
104
+ A0[:, 0] = 1
105
+ b0 = np.ones(m)
106
+ A1, b1, status, message = self.rr(A0, b0)
107
+ assert_equal(status, 0)
108
+ assert_allclose(A1, A0[0:1, :])
109
+ assert_allclose(b1, b0[0])
110
+
111
+ def test_m_lt_n_rank_deficient(self):
112
+ m, n = 9, 10
113
+ A0 = self.rng.random((m, n))
114
+ b0 = self.rng.random(m)
115
+ A0[-1, :] = np.arange(m - 1).dot(A0[:-1])
116
+ b0[-1] = np.arange(m - 1).dot(b0[:-1])
117
+ A1, b1, status, message = self.rr(A0, b0)
118
+ assert_equal(status, 0)
119
+ assert_equal(A1.shape[0], 8)
120
+ assert_equal(np.linalg.matrix_rank(A1), 8)
121
+
122
+ def test_dense1(self):
123
+ A = np.ones((6, 6))
124
+ A[0, :3] = 0
125
+ A[1, 3:] = 0
126
+ A[3:, ::2] = -1
127
+ A[3, :2] = 0
128
+ A[4, 2:] = 0
129
+ b = np.zeros(A.shape[0])
130
+
131
+ A1, b1, status, message = self.rr(A, b)
132
+ assert_(redundancy_removed(A1, A))
133
+ assert_equal(status, 0)
134
+
135
+ def test_dense2(self):
136
+ A = np.eye(6)
137
+ A[-2, -1] = 1
138
+ A[-1, :] = 1
139
+ b = np.zeros(A.shape[0])
140
+ A1, b1, status, message = self.rr(A, b)
141
+ assert_(redundancy_removed(A1, A))
142
+ assert_equal(status, 0)
143
+
144
+ def test_dense3(self):
145
+ A = np.eye(6)
146
+ A[-2, -1] = 1
147
+ A[-1, :] = 1
148
+ b = self.rng.random(A.shape[0])
149
+ b[-1] = np.sum(b[:-1])
150
+ A1, b1, status, message = self.rr(A, b)
151
+ assert_(redundancy_removed(A1, A))
152
+ assert_equal(status, 0)
153
+
154
+ def test_m_gt_n_sparse(self):
155
+ rng = np.random.default_rng(2013)
156
+ m, n = 20, 5
157
+ p = 0.1
158
+ A = rng.random((m, n))
159
+ A[rng.random((m, n)) > p] = 0
160
+ rank = np.linalg.matrix_rank(A)
161
+ b = np.zeros(A.shape[0])
162
+ A1, b1, status, message = self.rr(A, b)
163
+ assert_equal(status, 0)
164
+ assert_equal(A1.shape[0], rank)
165
+ assert_equal(np.linalg.matrix_rank(A1), rank)
166
+
167
+ def test_m_lt_n_sparse(self):
168
+ rng = np.random.default_rng(2017)
169
+ m, n = 20, 50
170
+ p = 0.05
171
+ A = rng.random((m, n))
172
+ A[rng.random((m, n)) > p] = 0
173
+ rank = np.linalg.matrix_rank(A)
174
+ b = np.zeros(A.shape[0])
175
+ A1, b1, status, message = self.rr(A, b)
176
+ assert_equal(status, 0)
177
+ assert_equal(A1.shape[0], rank)
178
+ assert_equal(np.linalg.matrix_rank(A1), rank)
179
+
180
+ def test_m_eq_n_sparse(self):
181
+ rng = np.random.default_rng(2017)
182
+ m, n = 100, 100
183
+ p = 0.01
184
+ A = rng.random((m, n))
185
+ A[rng.random((m, n)) > p] = 0
186
+ rank = np.linalg.matrix_rank(A)
187
+ b = np.zeros(A.shape[0])
188
+ A1, b1, status, message = self.rr(A, b)
189
+ assert_equal(status, 0)
190
+ assert_equal(A1.shape[0], rank)
191
+ assert_equal(np.linalg.matrix_rank(A1), rank)
192
+
193
+ def test_magic_square(self):
194
+ A, b, c, numbers, _ = magic_square(3)
195
+ A1, b1, status, message = self.rr(A, b)
196
+ assert_equal(status, 0)
197
+ assert_equal(A1.shape[0], 23)
198
+ assert_equal(np.linalg.matrix_rank(A1), 23)
199
+
200
+ def test_magic_square2(self):
201
+ A, b, c, numbers, _ = magic_square(4)
202
+ A1, b1, status, message = self.rr(A, b)
203
+ assert_equal(status, 0)
204
+ assert_equal(A1.shape[0], 39)
205
+ assert_equal(np.linalg.matrix_rank(A1), 39)
206
+
207
+
208
+ class TestRRSVD(RRCommonTests):
209
+ def rr(self, A, b):
210
+ return _remove_redundancy_svd(A, b)
211
+
212
+
213
+ class TestRRPivotDense(RRCommonTests):
214
+ def rr(self, A, b):
215
+ return _remove_redundancy_pivot_dense(A, b)
216
+
217
+
218
+ class TestRRID(RRCommonTests):
219
+ def rr(self, A, b):
220
+ return _remove_redundancy_id(A, b)
221
+
222
+
223
+ class TestRRPivotSparse(RRCommonTests):
224
+ def rr(self, A, b):
225
+ rr_res = _remove_redundancy_pivot_sparse(csc_array(A), b)
226
+ A1, b1, status, message = rr_res
227
+ return A1.toarray(), b1, status, message
loan-advisor-agent/.venv/Lib/site-packages/scipy/optimize/tests/test__root.py ADDED
@@ -0,0 +1,123 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Unit tests for optimization routines from _root.py.
3
+ """
4
+ from numpy.testing import assert_, assert_equal
5
+ import pytest
6
+ from pytest import raises as assert_raises, warns as assert_warns
7
+ import numpy as np
8
+
9
+ from scipy.optimize import root
10
+
11
+
12
+ class TestRoot:
13
+ def test_tol_parameter(self):
14
+ # Check that the minimize() tol= argument does something
15
+ def func(z):
16
+ x, y = z
17
+ return np.array([x**3 - 1, y**3 - 1])
18
+
19
+ def dfunc(z):
20
+ x, y = z
21
+ return np.array([[3*x**2, 0], [0, 3*y**2]])
22
+
23
+ for method in ['hybr', 'lm', 'broyden1', 'broyden2', 'anderson',
24
+ 'diagbroyden', 'krylov']:
25
+ if method in ('linearmixing', 'excitingmixing'):
26
+ # doesn't converge
27
+ continue
28
+
29
+ if method in ('hybr', 'lm'):
30
+ jac = dfunc
31
+ else:
32
+ jac = None
33
+
34
+ sol1 = root(func, [1.1,1.1], jac=jac, tol=1e-4, method=method)
35
+ sol2 = root(func, [1.1,1.1], jac=jac, tol=0.5, method=method)
36
+ msg = f"{method}: {func(sol1.x)} vs. {func(sol2.x)}"
37
+ assert_(sol1.success, msg)
38
+ assert_(sol2.success, msg)
39
+ assert_(abs(func(sol1.x)).max() < abs(func(sol2.x)).max(),
40
+ msg)
41
+
42
+ def test_tol_norm(self):
43
+
44
+ def norm(x):
45
+ return abs(x[0])
46
+
47
+ for method in ['excitingmixing',
48
+ 'diagbroyden',
49
+ 'linearmixing',
50
+ 'anderson',
51
+ 'broyden1',
52
+ 'broyden2',
53
+ 'krylov']:
54
+
55
+ root(np.zeros_like, np.zeros(2), method=method,
56
+ options={"tol_norm": norm})
57
+
58
+ def test_minimize_scalar_coerce_args_param(self):
59
+ # GitHub issue #3503
60
+ def func(z, f=1):
61
+ x, y = z
62
+ return np.array([x**3 - 1, y**3 - f])
63
+ root(func, [1.1, 1.1], args=1.5)
64
+
65
+ def test_f_size(self):
66
+ # gh8320
67
+ # check that decreasing the size of the returned array raises an error
68
+ # and doesn't segfault
69
+ class fun:
70
+ def __init__(self):
71
+ self.count = 0
72
+
73
+ def __call__(self, x):
74
+ self.count += 1
75
+
76
+ if not (self.count % 5):
77
+ ret = x[0] + 0.5 * (x[0] - x[1]) ** 3 - 1.0
78
+ else:
79
+ ret = ([x[0] + 0.5 * (x[0] - x[1]) ** 3 - 1.0,
80
+ 0.5 * (x[1] - x[0]) ** 3 + x[1]])
81
+
82
+ return ret
83
+
84
+ F = fun()
85
+ with assert_raises(ValueError):
86
+ root(F, [0.1, 0.0], method='lm')
87
+
88
+ def test_gh_10370(self):
89
+ # gh-10370 reported that passing both `args` and `jac` to `root` with
90
+ # `method='krylov'` caused a failure. Ensure that this is fixed whether
91
+ # the gradient is passed via `jac` or as a second output of `fun`.
92
+ def fun(x, ignored):
93
+ return [3*x[0] - 0.25*x[1]**2 + 10, 0.1*x[0]**2 + 5*x[1] - 2]
94
+
95
+ def grad(x, ignored):
96
+ return [[3, 0.5 * x[1]], [0.2 * x[0], 5]]
97
+
98
+ def fun_grad(x, ignored):
99
+ return fun(x, ignored), grad(x, ignored)
100
+
101
+ x0 = np.zeros(2)
102
+
103
+ ref = root(fun, x0, args=(1,), method='krylov')
104
+ message = 'Method krylov does not use the jacobian'
105
+ with assert_warns(RuntimeWarning, match=message):
106
+ res1 = root(fun, x0, args=(1,), method='krylov', jac=grad)
107
+ with assert_warns(RuntimeWarning, match=message):
108
+ res2 = root(fun_grad, x0, args=(1,), method='krylov', jac=True)
109
+
110
+ assert_equal(res1.x, ref.x)
111
+ assert_equal(res2.x, ref.x)
112
+ assert res1.success is res2.success is ref.success is True
113
+
114
+ @pytest.mark.parametrize("method", ["hybr", "lm", "broyden1", "broyden2",
115
+ "anderson", "linearmixing",
116
+ "diagbroyden", "excitingmixing",
117
+ "krylov", "df-sane"])
118
+ def test_method_in_result(self, method):
119
+ def func(x):
120
+ return x - 1
121
+
122
+ res = root(func, x0=[1], method=method)
123
+ assert res.method == method
loan-advisor-agent/.venv/Lib/site-packages/scipy/optimize/tests/test__shgo.py ADDED
@@ -0,0 +1,1228 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import logging
2
+ import sys
3
+ import warnings
4
+
5
+ import numpy as np
6
+ import time
7
+ from multiprocessing import Pool
8
+ from numpy.testing import assert_allclose, IS_PYPY
9
+ import pytest
10
+ from pytest import raises as assert_raises, warns
11
+ from scipy.optimize import (shgo, Bounds, minimize_scalar, minimize, rosen,
12
+ rosen_der, rosen_hess, NonlinearConstraint, OptimizeWarning)
13
+ from scipy.optimize._constraints import new_constraint_to_old
14
+ from scipy.optimize._shgo import SHGO
15
+ from scipy.optimize.tests.test_minimize_constrained import MaratosTestArgs
16
+
17
+
18
+ class StructTestFunction:
19
+ def __init__(self, bounds, expected_x, expected_fun=None,
20
+ expected_xl=None, expected_funl=None):
21
+ self.bounds = bounds
22
+ self.expected_x = expected_x
23
+ self.expected_fun = expected_fun
24
+ self.expected_xl = expected_xl
25
+ self.expected_funl = expected_funl
26
+
27
+
28
+ def wrap_constraints(g):
29
+ cons = []
30
+ if g is not None:
31
+ if not isinstance(g, tuple | list):
32
+ g = (g,)
33
+ else:
34
+ pass
35
+ for g in g:
36
+ cons.append({'type': 'ineq',
37
+ 'fun': g})
38
+ cons = tuple(cons)
39
+ else:
40
+ cons = None
41
+ return cons
42
+
43
+
44
+ class StructTest1(StructTestFunction):
45
+ def f(self, x):
46
+ return x[0] ** 2 + x[1] ** 2
47
+
48
+ def g(x):
49
+ return -(np.sum(x, axis=0) - 6.0)
50
+
51
+ cons = wrap_constraints(g)
52
+
53
+
54
+ test1_1 = StructTest1(bounds=[(-1, 6), (-1, 6)],
55
+ expected_x=[0, 0])
56
+ test1_2 = StructTest1(bounds=[(0, 1), (0, 1)],
57
+ expected_x=[0, 0])
58
+ test1_3 = StructTest1(bounds=[(None, None), (None, None)],
59
+ expected_x=[0, 0])
60
+
61
+
62
+ class StructTest2(StructTestFunction):
63
+ """
64
+ Scalar function with several minima to test all minimiser retrievals
65
+ """
66
+
67
+ def f(self, x):
68
+ return (x - 30) * np.sin(x)
69
+
70
+ def g(x):
71
+ return 58 - np.sum(x, axis=0)
72
+
73
+ cons = wrap_constraints(g)
74
+
75
+
76
+ test2_1 = StructTest2(bounds=[(0, 60)],
77
+ expected_x=[1.53567906],
78
+ expected_fun=-28.44677132,
79
+ # Important: test that funl return is in the correct
80
+ # order
81
+ expected_xl=np.array([[1.53567906],
82
+ [55.01782167],
83
+ [7.80894889],
84
+ [48.74797493],
85
+ [14.07445705],
86
+ [42.4913859],
87
+ [20.31743841],
88
+ [36.28607535],
89
+ [26.43039605],
90
+ [30.76371366]]),
91
+
92
+ expected_funl=np.array([-28.44677132, -24.99785984,
93
+ -22.16855376, -18.72136195,
94
+ -15.89423937, -12.45154942,
95
+ -9.63133158, -6.20801301,
96
+ -3.43727232, -0.46353338])
97
+ )
98
+
99
+ test2_2 = StructTest2(bounds=[(0, 4.5)],
100
+ expected_x=[1.53567906],
101
+ expected_fun=[-28.44677132],
102
+ expected_xl=np.array([[1.53567906]]),
103
+ expected_funl=np.array([-28.44677132])
104
+ )
105
+
106
+
107
+ class StructTest3(StructTestFunction):
108
+ """
109
+ Hock and Schittkowski 18 problem (HS18). Hoch and Schittkowski (1981)
110
+ http://www.ai7.uni-bayreuth.de/test_problem_coll.pdf
111
+ Minimize: f = 0.01 * (x_1)**2 + (x_2)**2
112
+
113
+ Subject to: x_1 * x_2 - 25.0 >= 0,
114
+ (x_1)**2 + (x_2)**2 - 25.0 >= 0,
115
+ 2 <= x_1 <= 50,
116
+ 0 <= x_2 <= 50.
117
+
118
+ Approx. Answer:
119
+ f([(250)**0.5 , (2.5)**0.5]) = 5.0
120
+
121
+
122
+ """
123
+
124
+ # amended to test vectorisation of constraints
125
+ def f(self, x):
126
+ return 0.01 * (x[0]) ** 2 + (x[1]) ** 2
127
+
128
+ def g1(x):
129
+ return x[0] * x[1] - 25.0
130
+
131
+ def g2(x):
132
+ return x[0] ** 2 + x[1] ** 2 - 25.0
133
+
134
+ # g = (g1, g2)
135
+ # cons = wrap_constraints(g)
136
+
137
+ def g(x):
138
+ return x[0] * x[1] - 25.0, x[0] ** 2 + x[1] ** 2 - 25.0
139
+
140
+ # this checks that shgo can be sent new-style constraints
141
+ __nlc = NonlinearConstraint(g, 0, np.inf)
142
+ cons = (__nlc,)
143
+
144
+ test3_1 = StructTest3(bounds=[(2, 50), (0, 50)],
145
+ expected_x=[250 ** 0.5, 2.5 ** 0.5],
146
+ expected_fun=5.0
147
+ )
148
+
149
+
150
+ class StructTest4(StructTestFunction):
151
+ """
152
+ Hock and Schittkowski 11 problem (HS11). Hoch and Schittkowski (1981)
153
+
154
+ NOTE: Did not find in original reference to HS collection, refer to
155
+ Henderson (2015) problem 7 instead. 02.03.2016
156
+ """
157
+
158
+ def f(self, x):
159
+ return ((x[0] - 10) ** 2 + 5 * (x[1] - 12) ** 2 + x[2] ** 4
160
+ + 3 * (x[3] - 11) ** 2 + 10 * x[4] ** 6 + 7 * x[5] ** 2 + x[
161
+ 6] ** 4
162
+ - 4 * x[5] * x[6] - 10 * x[5] - 8 * x[6]
163
+ )
164
+
165
+ def g1(x):
166
+ return -(2 * x[0] ** 2 + 3 * x[1] ** 4 + x[2] + 4 * x[3] ** 2
167
+ + 5 * x[4] - 127)
168
+
169
+ def g2(x):
170
+ return -(7 * x[0] + 3 * x[1] + 10 * x[2] ** 2 + x[3] - x[4] - 282.0)
171
+
172
+ def g3(x):
173
+ return -(23 * x[0] + x[1] ** 2 + 6 * x[5] ** 2 - 8 * x[6] - 196)
174
+
175
+ def g4(x):
176
+ return -(4 * x[0] ** 2 + x[1] ** 2 - 3 * x[0] * x[1] + 2 * x[2] ** 2
177
+ + 5 * x[5] - 11 * x[6])
178
+
179
+ g = (g1, g2, g3, g4)
180
+
181
+ cons = wrap_constraints(g)
182
+
183
+
184
+ test4_1 = StructTest4(bounds=[(-10, 10), ] * 7,
185
+ expected_x=[2.330499, 1.951372, -0.4775414,
186
+ 4.365726, -0.6244870, 1.038131, 1.594227],
187
+ expected_fun=680.6300573
188
+ )
189
+
190
+
191
+ class StructTest5(StructTestFunction):
192
+ def f(self, x):
193
+ return (
194
+ -(x[1] + 47.0)*np.sin(np.sqrt(abs(x[0]/2.0 + (x[1] + 47.0))))
195
+ - x[0]*np.sin(np.sqrt(abs(x[0] - (x[1] + 47.0))))
196
+ )
197
+
198
+ g = None
199
+ cons = wrap_constraints(g)
200
+
201
+
202
+ test5_1 = StructTest5(bounds=[(-512, 512), (-512, 512)],
203
+ expected_fun=[-959.64066272085051],
204
+ expected_x=[512., 404.23180542])
205
+
206
+
207
+ class StructTestLJ(StructTestFunction):
208
+ """
209
+ LennardJones objective function. Used to test symmetry constraints
210
+ settings.
211
+ """
212
+
213
+ def f(self, x, *args):
214
+ print(f'x = {x}')
215
+ self.N = args[0]
216
+ k = int(self.N / 3)
217
+ s = 0.0
218
+
219
+ for i in range(k - 1):
220
+ for j in range(i + 1, k):
221
+ a = 3 * i
222
+ b = 3 * j
223
+ xd = x[a] - x[b]
224
+ yd = x[a + 1] - x[b + 1]
225
+ zd = x[a + 2] - x[b + 2]
226
+ ed = xd * xd + yd * yd + zd * zd
227
+ ud = ed * ed * ed
228
+ if ed > 0.0:
229
+ s += (1.0 / ud - 2.0) / ud
230
+
231
+ return s
232
+
233
+ g = None
234
+ cons = wrap_constraints(g)
235
+
236
+
237
+ N = 6
238
+ boundsLJ = list(zip([-4.0] * 6, [4.0] * 6))
239
+
240
+ testLJ = StructTestLJ(bounds=boundsLJ,
241
+ expected_fun=[-1.0],
242
+ expected_x=None,
243
+ # expected_x=[-2.71247337e-08,
244
+ # -2.71247337e-08,
245
+ # -2.50000222e+00,
246
+ # -2.71247337e-08,
247
+ # -2.71247337e-08,
248
+ # -1.50000222e+00]
249
+ )
250
+
251
+
252
+ class StructTestS(StructTestFunction):
253
+ def f(self, x):
254
+ return ((x[0] - 0.5) ** 2 + (x[1] - 0.5) ** 2
255
+ + (x[2] - 0.5) ** 2 + (x[3] - 0.5) ** 2)
256
+
257
+ g = None
258
+ cons = wrap_constraints(g)
259
+
260
+
261
+ test_s = StructTestS(bounds=[(0, 2.0), ] * 4,
262
+ expected_fun=0.0,
263
+ expected_x=np.ones(4) - 0.5
264
+ )
265
+
266
+
267
+ class StructTestTable(StructTestFunction):
268
+ def f(self, x):
269
+ if x[0] == 3.0 and x[1] == 3.0:
270
+ return 50
271
+ else:
272
+ return 100
273
+
274
+ g = None
275
+ cons = wrap_constraints(g)
276
+
277
+
278
+ test_table = StructTestTable(bounds=[(-10, 10), (-10, 10)],
279
+ expected_fun=[50],
280
+ expected_x=[3.0, 3.0])
281
+
282
+
283
+ class StructTestInfeasible(StructTestFunction):
284
+ """
285
+ Test function with no feasible domain.
286
+ """
287
+
288
+ def f(self, x, *args):
289
+ return x[0] ** 2 + x[1] ** 2
290
+
291
+ def g1(x):
292
+ return x[0] + x[1] - 1
293
+
294
+ def g2(x):
295
+ return -(x[0] + x[1] - 1)
296
+
297
+ def g3(x):
298
+ return -x[0] + x[1] - 1
299
+
300
+ def g4(x):
301
+ return -(-x[0] + x[1] - 1)
302
+
303
+ g = (g1, g2, g3, g4)
304
+ cons = wrap_constraints(g)
305
+
306
+
307
+ test_infeasible = StructTestInfeasible(bounds=[(2, 50), (-1, 1)],
308
+ expected_fun=None,
309
+ expected_x=None
310
+ )
311
+
312
+
313
+ @pytest.mark.skip("Not a test")
314
+ def run_test(test, args=(), test_atol=1e-5, n=100, iters=None,
315
+ callback=None, minimizer_kwargs=None, options=None,
316
+ sampling_method='sobol', workers=1):
317
+ res = shgo(test.f, test.bounds, args=args, constraints=test.cons,
318
+ n=n, iters=iters, callback=callback,
319
+ minimizer_kwargs=minimizer_kwargs, options=options,
320
+ sampling_method=sampling_method, workers=workers)
321
+
322
+ print(f'res = {res}')
323
+ logging.info(f'res = {res}')
324
+ if test.expected_x is not None:
325
+ np.testing.assert_allclose(res.x, test.expected_x,
326
+ rtol=test_atol,
327
+ atol=test_atol)
328
+
329
+ # (Optional tests)
330
+ if test.expected_fun is not None:
331
+ np.testing.assert_allclose(res.fun,
332
+ test.expected_fun,
333
+ atol=test_atol)
334
+
335
+ if test.expected_xl is not None:
336
+ np.testing.assert_allclose(res.xl,
337
+ test.expected_xl,
338
+ atol=test_atol)
339
+
340
+ if test.expected_funl is not None:
341
+ np.testing.assert_allclose(res.funl,
342
+ test.expected_funl,
343
+ atol=test_atol)
344
+ return
345
+
346
+
347
+ # Base test functions:
348
+ class TestShgoSobolTestFunctions:
349
+ """
350
+ Global optimisation tests with Sobol sampling:
351
+ """
352
+
353
+ # Sobol algorithm
354
+ def test_f1_1_sobol(self):
355
+ """Multivariate test function 1:
356
+ x[0]**2 + x[1]**2 with bounds=[(-1, 6), (-1, 6)]"""
357
+ run_test(test1_1)
358
+
359
+ def test_f1_2_sobol(self):
360
+ """Multivariate test function 1:
361
+ x[0]**2 + x[1]**2 with bounds=[(0, 1), (0, 1)]"""
362
+ run_test(test1_2)
363
+
364
+ def test_f1_3_sobol(self):
365
+ """Multivariate test function 1:
366
+ x[0]**2 + x[1]**2 with bounds=[(None, None),(None, None)]"""
367
+ options = {'disp': True}
368
+ run_test(test1_3, options=options)
369
+
370
+ def test_f2_1_sobol(self):
371
+ """Univariate test function on
372
+ f(x) = (x - 30) * sin(x) with bounds=[(0, 60)]"""
373
+ run_test(test2_1)
374
+
375
+ def test_f2_2_sobol(self):
376
+ """Univariate test function on
377
+ f(x) = (x - 30) * sin(x) bounds=[(0, 4.5)]"""
378
+ run_test(test2_2)
379
+
380
+ def test_f3_sobol(self):
381
+ """NLP: Hock and Schittkowski problem 18"""
382
+ run_test(test3_1)
383
+
384
+ @pytest.mark.slow
385
+ def test_f4_sobol(self):
386
+ """NLP: (High dimensional) Hock and Schittkowski 11 problem (HS11)"""
387
+ options = {'infty_constraints': False}
388
+ # run_test(test4_1, n=990, options=options)
389
+ run_test(test4_1, n=990 * 2, options=options)
390
+
391
+ def test_f5_1_sobol(self):
392
+ """NLP: Eggholder, multimodal"""
393
+ # run_test(test5_1, n=30)
394
+ run_test(test5_1, n=60)
395
+
396
+ def test_f5_2_sobol(self):
397
+ """NLP: Eggholder, multimodal"""
398
+ # run_test(test5_1, n=60, iters=5)
399
+ run_test(test5_1, n=60, iters=5)
400
+
401
+ # def test_t911(self):
402
+ # """1D tabletop function"""
403
+ # run_test(test11_1)
404
+
405
+
406
+ class TestShgoSimplicialTestFunctions:
407
+ """
408
+ Global optimisation tests with Simplicial sampling:
409
+ """
410
+
411
+ def test_f1_1_simplicial(self):
412
+ """Multivariate test function 1:
413
+ x[0]**2 + x[1]**2 with bounds=[(-1, 6), (-1, 6)]"""
414
+ run_test(test1_1, n=1, sampling_method='simplicial')
415
+
416
+ def test_f1_2_simplicial(self):
417
+ """Multivariate test function 1:
418
+ x[0]**2 + x[1]**2 with bounds=[(0, 1), (0, 1)]"""
419
+ run_test(test1_2, n=1, sampling_method='simplicial')
420
+
421
+ def test_f1_3_simplicial(self):
422
+ """Multivariate test function 1: x[0]**2 + x[1]**2
423
+ with bounds=[(None, None),(None, None)]"""
424
+ run_test(test1_3, n=5, sampling_method='simplicial')
425
+
426
+ def test_f2_1_simplicial(self):
427
+ """Univariate test function on
428
+ f(x) = (x - 30) * sin(x) with bounds=[(0, 60)]"""
429
+ options = {'minimize_every_iter': False}
430
+ run_test(test2_1, n=200, iters=7, options=options,
431
+ sampling_method='simplicial')
432
+
433
+ def test_f2_2_simplicial(self):
434
+ """Univariate test function on
435
+ f(x) = (x - 30) * sin(x) bounds=[(0, 4.5)]"""
436
+ run_test(test2_2, n=1, sampling_method='simplicial')
437
+
438
+ def test_f3_simplicial(self):
439
+ """NLP: Hock and Schittkowski problem 18"""
440
+ run_test(test3_1, n=1, sampling_method='simplicial')
441
+
442
+ @pytest.mark.slow
443
+ def test_f4_simplicial(self):
444
+ """NLP: (High dimensional) Hock and Schittkowski 11 problem (HS11)"""
445
+ run_test(test4_1, n=1, sampling_method='simplicial')
446
+
447
+ def test_lj_symmetry_old(self):
448
+ """LJ: Symmetry-constrained test function"""
449
+ options = {'symmetry': True,
450
+ 'disp': True}
451
+ args = (6,) # Number of atoms
452
+ run_test(testLJ, args=args, n=300,
453
+ options=options, iters=1,
454
+ sampling_method='simplicial')
455
+
456
+ def test_f5_1_lj_symmetry(self):
457
+ """LJ: Symmetry constrained test function"""
458
+ options = {'symmetry': [0, ] * 6,
459
+ 'disp': True}
460
+ args = (6,) # No. of atoms
461
+
462
+ run_test(testLJ, args=args, n=300,
463
+ options=options, iters=1,
464
+ sampling_method='simplicial')
465
+
466
+ def test_f5_2_cons_symmetry(self):
467
+ """Symmetry constrained test function"""
468
+ options = {'symmetry': [0, 0],
469
+ 'disp': True}
470
+
471
+ run_test(test1_1, n=200,
472
+ options=options, iters=1,
473
+ sampling_method='simplicial')
474
+
475
+ @pytest.mark.fail_slow(10)
476
+ def test_f5_3_cons_symmetry(self):
477
+ """Asymmetrically constrained test function"""
478
+ options = {'symmetry': [0, 0, 0, 3],
479
+ 'disp': True}
480
+
481
+ run_test(test_s, n=10000,
482
+ options=options,
483
+ iters=1,
484
+ sampling_method='simplicial')
485
+
486
+ @pytest.mark.skip("Not a test")
487
+ def test_f0_min_variance(self):
488
+ """Return a minimum on a perfectly symmetric problem, based on
489
+ gh10429"""
490
+ avg = 0.5 # Given average value of x
491
+ cons = {'type': 'eq', 'fun': lambda x: np.mean(x) - avg}
492
+
493
+ # Minimize the variance of x under the given constraint
494
+ res = shgo(np.var, bounds=6 * [(0, 1)], constraints=cons)
495
+ assert res.success
496
+ assert_allclose(res.fun, 0, atol=1e-15)
497
+ assert_allclose(res.x, 0.5)
498
+
499
+ @pytest.mark.skip("Not a test")
500
+ def test_f0_min_variance_1D(self):
501
+ """Return a minimum on a perfectly symmetric 1D problem, based on
502
+ gh10538"""
503
+
504
+ def fun(x):
505
+ return x * (x - 1.0) * (x - 0.5)
506
+
507
+ bounds = [(0, 1)]
508
+ res = shgo(fun, bounds=bounds)
509
+ ref = minimize_scalar(fun, bounds=bounds[0])
510
+ assert res.success
511
+ assert_allclose(res.fun, ref.fun)
512
+ assert_allclose(res.x, ref.x, rtol=1e-6)
513
+
514
+ # Argument test functions
515
+ class TestShgoArguments:
516
+ def test_1_1_simpl_iter(self):
517
+ """Iterative simplicial sampling on TestFunction 1 (multivariate)"""
518
+ run_test(test1_2, n=None, iters=2, sampling_method='simplicial')
519
+
520
+ def test_1_2_simpl_iter(self):
521
+ """Iterative simplicial on TestFunction 2 (univariate)"""
522
+ options = {'minimize_every_iter': False}
523
+ run_test(test2_1, n=None, iters=9, options=options,
524
+ sampling_method='simplicial')
525
+
526
+ def test_2_1_sobol_iter(self):
527
+ """Iterative Sobol sampling on TestFunction 1 (multivariate)"""
528
+ run_test(test1_2, n=None, iters=1, sampling_method='sobol')
529
+
530
+ def test_2_2_sobol_iter(self):
531
+ """Iterative Sobol sampling on TestFunction 2 (univariate)"""
532
+ res = shgo(test2_1.f, test2_1.bounds, constraints=test2_1.cons,
533
+ n=None, iters=1, sampling_method='sobol')
534
+
535
+ np.testing.assert_allclose(res.x, test2_1.expected_x, rtol=1e-5, atol=1e-5)
536
+ np.testing.assert_allclose(res.fun, test2_1.expected_fun, atol=1e-5)
537
+
538
+ def test_3_1_disp_simplicial(self):
539
+ """Iterative sampling on TestFunction 1 and 2 (multi and univariate)
540
+ """
541
+
542
+ def callback_func(x):
543
+ print("Local minimization callback test")
544
+
545
+ for test in [test1_1, test2_1]:
546
+ shgo(test.f, test.bounds, iters=1,
547
+ sampling_method='simplicial',
548
+ callback=callback_func, options={'disp': True})
549
+ shgo(test.f, test.bounds, n=1, sampling_method='simplicial',
550
+ callback=callback_func, options={'disp': True})
551
+
552
+ def test_3_2_disp_sobol(self):
553
+ """Iterative sampling on TestFunction 1 and 2 (multi and univariate)"""
554
+
555
+ def callback_func(x):
556
+ print("Local minimization callback test")
557
+
558
+ for test in [test1_1, test2_1]:
559
+ shgo(test.f, test.bounds, iters=1, sampling_method='sobol',
560
+ callback=callback_func, options={'disp': True})
561
+
562
+ shgo(test.f, test.bounds, n=1, sampling_method='simplicial',
563
+ callback=callback_func, options={'disp': True})
564
+
565
+ def test_args_gh14589(self):
566
+ """Using `args` used to cause `shgo` to fail; see #14589, #15986,
567
+ #16506"""
568
+ res = shgo(func=lambda x, y, z: x * z + y, bounds=[(0, 3)], args=(1, 2)
569
+ )
570
+ ref = shgo(func=lambda x: 2 * x + 1, bounds=[(0, 3)])
571
+ assert_allclose(res.fun, ref.fun)
572
+ assert_allclose(res.x, ref.x)
573
+
574
+ def test_args_gh23517(self):
575
+ """
576
+ Checks that using `args` for func, jac and hess works
577
+ """
578
+ obj = MaratosTestArgs("a", 234)
579
+ obj.bounds = Bounds([-5, -5], [5, 5])
580
+ res2 = minimize(
581
+ obj.fun,
582
+ [4.0, 4.0],
583
+ constraints=obj.constr,
584
+ bounds=obj.bounds,
585
+ method='trust-constr',
586
+ args=("a", 234),
587
+ jac=obj.grad
588
+ )
589
+ assert_allclose(res2.x, obj.x_opt, atol=1e-6)
590
+
591
+ obj = MaratosTestArgs("a", 234)
592
+ obj.bounds = Bounds([-10., -10.], [10., 10.])
593
+ with warnings.catch_warnings():
594
+ # warnings are from initialization of NonlinearConstraint and
595
+ # poor initialization of the constraint by shgo
596
+ warnings.simplefilter(
597
+ "ignore",
598
+ (OptimizeWarning, RuntimeWarning)
599
+ )
600
+ res = shgo(
601
+ func=obj.fun,
602
+ bounds=obj.bounds,
603
+ args=("a", 234),
604
+ minimizer_kwargs={
605
+ "method": 'trust-constr',
606
+ "constraints": obj.constr,
607
+ "bounds": obj.bounds,
608
+ "jac": obj.grad,
609
+ "args": ("a", 234),
610
+ },
611
+ constraints=obj.constr,
612
+ sampling_method='sobol',
613
+ )
614
+ assert_allclose(res.x, obj.x_opt, atol=1e-6)
615
+
616
+ res = shgo(
617
+ func=obj.fun,
618
+ bounds=obj.bounds,
619
+ args=("a", 234),
620
+ minimizer_kwargs={
621
+ "method": 'trust-constr',
622
+ "constraints": obj.constr,
623
+ "bounds": obj.bounds,
624
+ "jac": obj.grad,
625
+ "hess": obj.hess,
626
+ },
627
+ constraints=obj.constr,
628
+ sampling_method='sobol',
629
+ )
630
+ assert_allclose(res.x, obj.x_opt, atol=1e-6)
631
+
632
+ @pytest.mark.slow
633
+ def test_4_1_known_f_min(self):
634
+ """Test known function minima stopping criteria"""
635
+ # Specify known function value
636
+ options = {'f_min': test4_1.expected_fun,
637
+ 'f_tol': 1e-6,
638
+ 'minimize_every_iter': True}
639
+ # TODO: Make default n higher for faster tests
640
+ run_test(test4_1, n=None, test_atol=1e-5, options=options,
641
+ sampling_method='simplicial')
642
+
643
+ @pytest.mark.slow
644
+ def test_4_2_known_f_min(self):
645
+ """Test Global mode limiting local evaluations"""
646
+ options = { # Specify known function value
647
+ 'f_min': test4_1.expected_fun,
648
+ 'f_tol': 1e-6,
649
+ # Specify number of local iterations to perform
650
+ 'minimize_every_iter': True,
651
+ 'local_iter': 1}
652
+
653
+ run_test(test4_1, n=None, test_atol=1e-5, options=options,
654
+ sampling_method='simplicial')
655
+
656
+ def test_4_4_known_f_min(self):
657
+ """Test Global mode limiting local evaluations for 1D funcs"""
658
+ options = { # Specify known function value
659
+ 'f_min': test2_1.expected_fun,
660
+ 'f_tol': 1e-6,
661
+ # Specify number of local iterations to perform+
662
+ 'minimize_every_iter': True,
663
+ 'local_iter': 1,
664
+ 'infty_constraints': False}
665
+
666
+ res = shgo(test2_1.f, test2_1.bounds, constraints=test2_1.cons,
667
+ n=None, iters=None, options=options,
668
+ sampling_method='sobol')
669
+ np.testing.assert_allclose(res.x, test2_1.expected_x, rtol=1e-5, atol=1e-5)
670
+
671
+ def test_5_1_simplicial_argless(self):
672
+ """Test Default simplicial sampling settings on TestFunction 1"""
673
+ res = shgo(test1_1.f, test1_1.bounds, constraints=test1_1.cons)
674
+ np.testing.assert_allclose(res.x, test1_1.expected_x, rtol=1e-5, atol=1e-5)
675
+
676
+ def test_5_2_sobol_argless(self):
677
+ """Test Default sobol sampling settings on TestFunction 1"""
678
+ res = shgo(test1_1.f, test1_1.bounds, constraints=test1_1.cons,
679
+ sampling_method='sobol')
680
+ np.testing.assert_allclose(res.x, test1_1.expected_x, rtol=1e-5, atol=1e-5)
681
+
682
+ def test_6_1_simplicial_max_iter(self):
683
+ """Test that maximum iteration option works on TestFunction 3"""
684
+ options = {'max_iter': 2}
685
+ res = shgo(test3_1.f, test3_1.bounds, constraints=test3_1.cons,
686
+ options=options, sampling_method='simplicial')
687
+ np.testing.assert_allclose(res.x, test3_1.expected_x, rtol=1e-5, atol=1e-5)
688
+ np.testing.assert_allclose(res.fun, test3_1.expected_fun, atol=1e-5)
689
+
690
+ def test_6_2_simplicial_min_iter(self):
691
+ """Test that maximum iteration option works on TestFunction 3"""
692
+ options = {'min_iter': 2}
693
+ res = shgo(test3_1.f, test3_1.bounds, constraints=test3_1.cons,
694
+ options=options, sampling_method='simplicial')
695
+ np.testing.assert_allclose(res.x, test3_1.expected_x, rtol=1e-5, atol=1e-5)
696
+ np.testing.assert_allclose(res.fun, test3_1.expected_fun, atol=1e-5)
697
+
698
+ def test_7_1_minkwargs(self):
699
+ """Test the minimizer_kwargs arguments for solvers with constraints"""
700
+ # Test solvers
701
+ for solver in ['COBYLA', 'COBYQA', 'SLSQP']:
702
+ # Note that passing global constraints to SLSQP is tested in other
703
+ # unittests which run test4_1 normally
704
+ minimizer_kwargs = {'method': solver,
705
+ 'constraints': test3_1.cons}
706
+ run_test(test3_1, n=100, test_atol=1e-3,
707
+ minimizer_kwargs=minimizer_kwargs,
708
+ sampling_method='sobol')
709
+
710
+ def test_7_2_minkwargs(self):
711
+ """Test the minimizer_kwargs default inits"""
712
+ minimizer_kwargs = {'ftol': 1e-5}
713
+ options = {'disp': True} # For coverage purposes
714
+ SHGO(test3_1.f, test3_1.bounds, constraints=test3_1.cons[0],
715
+ minimizer_kwargs=minimizer_kwargs, options=options)
716
+
717
+ def test_7_3_minkwargs(self):
718
+ """Test minimizer_kwargs arguments for solvers without constraints"""
719
+ for solver in ['Nelder-Mead', 'Powell', 'CG', 'BFGS', 'Newton-CG',
720
+ 'L-BFGS-B', 'TNC', 'dogleg', 'trust-ncg', 'trust-exact',
721
+ 'trust-krylov']:
722
+ def jac(x):
723
+ return np.array([2 * x[0], 2 * x[1]]).T
724
+
725
+ def hess(x):
726
+ return np.array([[2, 0], [0, 2]])
727
+
728
+ minimizer_kwargs = {'method': solver,
729
+ 'jac': jac,
730
+ 'hess': hess}
731
+ logging.info(f"Solver = {solver}")
732
+ logging.info("=" * 100)
733
+ run_test(test1_1, n=100, test_atol=1e-3,
734
+ minimizer_kwargs=minimizer_kwargs,
735
+ sampling_method='sobol')
736
+
737
+ def test_8_homology_group_diff(self):
738
+ options = {'minhgrd': 1,
739
+ 'minimize_every_iter': True}
740
+
741
+ run_test(test1_1, n=None, iters=None, options=options,
742
+ sampling_method='simplicial')
743
+
744
+ def test_9_cons_g(self):
745
+ """Test single function constraint passing"""
746
+ SHGO(test3_1.f, test3_1.bounds, constraints=test3_1.cons[0])
747
+
748
+ @pytest.mark.xfail(IS_PYPY and sys.platform == 'win32',
749
+ reason="Failing and fix in PyPy not planned (see gh-18632)")
750
+ def test_10_finite_time(self):
751
+ """Test single function constraint passing"""
752
+ options = {'maxtime': 1e-15}
753
+
754
+ def f(x):
755
+ time.sleep(1e-14)
756
+ return 0.0
757
+
758
+ res = shgo(f, test1_1.bounds, iters=5, options=options)
759
+ # Assert that only 1 rather than 5 requested iterations ran:
760
+ assert res.nit == 1
761
+
762
+ def test_11_f_min_0(self):
763
+ """Test to cover the case where f_lowest == 0"""
764
+ options = {'f_min': 0.0,
765
+ 'disp': True}
766
+ res = shgo(test1_2.f, test1_2.bounds, n=10, iters=None,
767
+ options=options, sampling_method='sobol')
768
+ np.testing.assert_equal(0, res.x[0])
769
+ np.testing.assert_equal(0, res.x[1])
770
+
771
+ # @nottest
772
+ @pytest.mark.skip(reason="no way of currently testing this")
773
+ def test_12_sobol_inf_cons(self):
774
+ """Test to cover the case where f_lowest == 0"""
775
+ # TODO: This test doesn't cover anything new, it is unknown what the
776
+ # original test was intended for as it was never complete. Delete or
777
+ # replace in the future.
778
+ options = {'maxtime': 1e-15,
779
+ 'f_min': 0.0}
780
+ res = shgo(test1_2.f, test1_2.bounds, n=1, iters=None,
781
+ options=options, sampling_method='sobol')
782
+ np.testing.assert_equal(0.0, res.fun)
783
+
784
+ def test_13_high_sobol(self):
785
+ """Test init of high-dimensional sobol sequences"""
786
+
787
+ def f(x):
788
+ return 0
789
+
790
+ bounds = [(None, None), ] * 41
791
+ SHGOc = SHGO(f, bounds, sampling_method='sobol')
792
+ # SHGOc.sobol_points(2, 50)
793
+ SHGOc.sampling_function(2, 50)
794
+
795
+ def test_14_local_iter(self):
796
+ """Test limited local iterations for a pseudo-global mode"""
797
+ options = {'local_iter': 4}
798
+ run_test(test5_1, n=60, options=options)
799
+
800
+ def test_15_min_every_iter(self):
801
+ """Test minimize every iter options and cover function cache"""
802
+ options = {'minimize_every_iter': True}
803
+ run_test(test1_1, n=1, iters=7, options=options,
804
+ sampling_method='sobol')
805
+
806
+ def test_16_disp_bounds_minimizer(self, capsys):
807
+ """Test disp=True with minimizers that do not support bounds """
808
+ options = {'disp': True}
809
+ minimizer_kwargs = {'method': 'nelder-mead'}
810
+ run_test(test1_2, sampling_method='simplicial',
811
+ options=options, minimizer_kwargs=minimizer_kwargs)
812
+
813
+ def test_17_custom_sampling(self):
814
+ """Test the functionality to add custom sampling methods to shgo"""
815
+
816
+ def sample(n, d):
817
+ return np.random.uniform(size=(n, d))
818
+
819
+ run_test(test1_1, n=30, sampling_method=sample)
820
+
821
+ def test_18_bounds_class(self):
822
+ # test that new and old bounds yield same result
823
+ def f(x):
824
+ return np.square(x).sum()
825
+
826
+ lb = [-6., 1., -5.]
827
+ ub = [-1., 3., 5.]
828
+ bounds_old = list(zip(lb, ub))
829
+ bounds_new = Bounds(lb, ub)
830
+
831
+ res_old_bounds = shgo(f, bounds_old)
832
+ res_new_bounds = shgo(f, bounds_new)
833
+
834
+ assert res_new_bounds.nfev == res_old_bounds.nfev
835
+ assert res_new_bounds.message == res_old_bounds.message
836
+ assert res_new_bounds.success == res_old_bounds.success
837
+ x_opt = np.array([-1., 1., 0.])
838
+ np.testing.assert_allclose(res_new_bounds.x, x_opt)
839
+ np.testing.assert_allclose(res_new_bounds.x, res_old_bounds.x)
840
+
841
+ @pytest.mark.fail_slow(10)
842
+ def test_19_parallelization(self):
843
+ """Test the functionality to add custom sampling methods to shgo"""
844
+
845
+ with Pool(2) as p:
846
+ run_test(test1_1, n=30, workers=p.map) # Constrained
847
+ run_test(test1_1, n=30, workers=map) # Constrained
848
+ with Pool(2) as p:
849
+ run_test(test_s, n=30, workers=p.map) # Unconstrained
850
+ run_test(test_s, n=30, workers=map) # Unconstrained
851
+
852
+ def test_20_constrained_args(self):
853
+ """Test that constraints can be passed to arguments"""
854
+
855
+ def eggholder(x):
856
+ return (
857
+ -(x[1] + 47.0)*np.sin(np.sqrt(abs(x[0] / 2.0 + (x[1] + 47.0))))
858
+ - x[0]*np.sin(np.sqrt(abs(x[0] - (x[1] + 47.0))))
859
+ )
860
+
861
+ def f(x): # (cattle-feed)
862
+ return 24.55 * x[0] + 26.75 * x[1] + 39 * x[2] + 40.50 * x[3]
863
+
864
+ bounds = [(0, 1.0), ] * 4
865
+
866
+ def g1_modified(x, i):
867
+ return i * 2.3 * x[0] + i * 5.6 * x[1] + 11.1 * x[2] + 1.3 * x[
868
+ 3] - 5 # >=0
869
+
870
+ def g2(x):
871
+ return (
872
+ 12*x[0] + 11.9*x[1] + 41.8*x[2] + 52.1*x[3] - 21
873
+ - 1.645*np.sqrt(
874
+ 0.28*x[0]**2 + 0.19*x[1]**2 + 20.5*x[2]**2 + 0.62*x[3]**2
875
+ )
876
+ ) # >=0
877
+
878
+ def h1(x):
879
+ return x[0] + x[1] + x[2] + x[3] - 1 # == 0
880
+
881
+ cons = ({'type': 'ineq', 'fun': g1_modified, "args": (0,)},
882
+ {'type': 'ineq', 'fun': g2},
883
+ {'type': 'eq', 'fun': h1})
884
+
885
+ shgo(f, bounds, n=300, iters=1, constraints=cons)
886
+ # using constrain with arguments AND sampling method sobol
887
+ shgo(f, bounds, n=300, iters=1, constraints=cons,
888
+ sampling_method='sobol')
889
+
890
+ def test_21_1_jac_true(self):
891
+ """Test that shgo can handle objective functions that return the
892
+ gradient alongside the objective value. Fixes gh-13547"""
893
+ # previous
894
+ def func(x):
895
+ return np.sum(np.power(x, 2)), 2 * x
896
+
897
+ min_kwds = {"method": "SLSQP", "jac": True}
898
+ opt_kwds = {"jac": True}
899
+ shgo(
900
+ func,
901
+ bounds=[[-1, 1], [1, 2]],
902
+ n=100, iters=5,
903
+ sampling_method="sobol",
904
+ minimizer_kwargs=min_kwds,
905
+ options=opt_kwds
906
+ )
907
+ assert min_kwds['jac'] is True
908
+ assert "jac" in opt_kwds
909
+
910
+ # new
911
+ def func(x):
912
+ return np.sum(x ** 2), 2 * x
913
+
914
+ bounds = [[-1, 1], [1, 2], [-1, 1], [1, 2], [0, 3]]
915
+
916
+ res = shgo(func, bounds=bounds, sampling_method="sobol",
917
+ minimizer_kwargs={'method': 'SLSQP', 'jac': True})
918
+ ref = minimize(func, x0=[1, 1, 1, 1, 1], bounds=bounds,
919
+ jac=True)
920
+ assert res.success
921
+ assert_allclose(res.fun, ref.fun)
922
+ assert_allclose(res.x, ref.x, atol=1e-15)
923
+
924
+ # Testing the passing of jac via options dict
925
+ res = shgo(func, bounds=bounds, sampling_method="sobol",
926
+ minimizer_kwargs={'method': 'SLSQP'},
927
+ options={'jac': True})
928
+ assert res.success
929
+ assert_allclose(res.fun, ref.fun)
930
+ assert_allclose(res.x, ref.x, atol=1e-15)
931
+
932
+ @pytest.mark.parametrize('derivative', ['jac', 'hess', 'hessp'])
933
+ def test_21_2_derivative_options(self, derivative):
934
+ """shgo used to raise an error when passing `options` with 'jac'
935
+ # see gh-12963. check that this is resolved
936
+ """
937
+
938
+ def objective(x):
939
+ return 3 * x[0] * x[0] + 2 * x[0] + 5
940
+
941
+ def gradient(x):
942
+ return 6 * x[0] + 2
943
+
944
+ def hess(x):
945
+ return 6
946
+
947
+ def hessp(x, p):
948
+ return 6 * p
949
+
950
+ derivative_funcs = {'jac': gradient, 'hess': hess, 'hessp': hessp}
951
+ options = {derivative: derivative_funcs[derivative]}
952
+ minimizer_kwargs = {'method': 'trust-constr'}
953
+
954
+ bounds = [(-100, 100)]
955
+ res = shgo(objective, bounds, minimizer_kwargs=minimizer_kwargs,
956
+ options=options)
957
+ ref = minimize(objective, x0=[0], bounds=bounds, **minimizer_kwargs,
958
+ **options)
959
+
960
+ assert res.success
961
+ np.testing.assert_allclose(res.fun, ref.fun)
962
+ np.testing.assert_allclose(res.x, ref.x)
963
+
964
+ def test_21_3_hess_options_rosen(self):
965
+ """Ensure the Hessian gets passed correctly to the local minimizer
966
+ routine. Previous report gh-14533.
967
+ """
968
+ bounds = [(0, 1.6), (0, 1.6), (0, 1.4), (0, 1.4), (0, 1.4)]
969
+ options = {'jac': rosen_der, 'hess': rosen_hess}
970
+ minimizer_kwargs = {'method': 'Newton-CG'}
971
+ res = shgo(rosen, bounds, minimizer_kwargs=minimizer_kwargs,
972
+ options=options)
973
+ ref = minimize(rosen, np.zeros(5), method='Newton-CG',
974
+ **options)
975
+ assert res.success
976
+ assert_allclose(res.fun, ref.fun)
977
+ assert_allclose(res.x, ref.x, atol=1e-15)
978
+
979
+ def test_21_arg_tuple_sobol(self):
980
+ """shgo used to raise an error when passing `args` with Sobol sampling
981
+ # see gh-12114. check that this is resolved"""
982
+
983
+ def fun(x, k):
984
+ return x[0] ** k
985
+
986
+ constraints = ({'type': 'ineq', 'fun': lambda x: x[0] - 1})
987
+
988
+ bounds = [(0, 10)]
989
+ res = shgo(fun, bounds, args=(1,), constraints=constraints,
990
+ sampling_method='sobol')
991
+ ref = minimize(fun, np.zeros(1), bounds=bounds, args=(1,),
992
+ constraints=constraints)
993
+ assert res.success
994
+ assert_allclose(res.fun, ref.fun)
995
+ assert_allclose(res.x, ref.x)
996
+
997
+
998
+ # Failure test functions
999
+ class TestShgoFailures:
1000
+ def test_1_maxiter(self):
1001
+ """Test failure on insufficient iterations"""
1002
+ options = {'maxiter': 2}
1003
+ res = shgo(test4_1.f, test4_1.bounds, n=2, iters=None,
1004
+ options=options, sampling_method='sobol')
1005
+
1006
+ np.testing.assert_equal(False, res.success)
1007
+ # np.testing.assert_equal(4, res.nfev)
1008
+ np.testing.assert_equal(4, res.tnev)
1009
+
1010
+ def test_2_sampling(self):
1011
+ """Rejection of unknown sampling method"""
1012
+ assert_raises(ValueError, shgo, test1_1.f, test1_1.bounds,
1013
+ sampling_method='not_Sobol')
1014
+
1015
+ def test_3_1_no_min_pool_sobol(self):
1016
+ """Check that the routine stops when no minimiser is found
1017
+ after maximum specified function evaluations"""
1018
+ options = {'maxfev': 10,
1019
+ # 'maxev': 10,
1020
+ 'disp': True}
1021
+ res = shgo(test_table.f, test_table.bounds, n=3, options=options,
1022
+ sampling_method='sobol')
1023
+ np.testing.assert_equal(False, res.success)
1024
+ # np.testing.assert_equal(9, res.nfev)
1025
+ np.testing.assert_equal(12, res.nfev)
1026
+
1027
+ def test_3_2_no_min_pool_simplicial(self):
1028
+ """Check that the routine stops when no minimiser is found
1029
+ after maximum specified sampling evaluations"""
1030
+ options = {'maxev': 10,
1031
+ 'disp': True}
1032
+ res = shgo(test_table.f, test_table.bounds, n=3, options=options,
1033
+ sampling_method='simplicial')
1034
+ np.testing.assert_equal(False, res.success)
1035
+
1036
+ def test_4_1_bound_err(self):
1037
+ """Specified bounds ub > lb"""
1038
+ bounds = [(6, 3), (3, 5)]
1039
+ assert_raises(ValueError, shgo, test1_1.f, bounds)
1040
+
1041
+ def test_4_2_bound_err(self):
1042
+ """Specified bounds are of the form (lb, ub)"""
1043
+ bounds = [(3, 5, 5), (3, 5)]
1044
+ assert_raises(ValueError, shgo, test1_1.f, bounds)
1045
+
1046
+ def test_5_1_1_infeasible_sobol(self):
1047
+ """Ensures the algorithm terminates on infeasible problems
1048
+ after maxev is exceeded. Use infty constraints option"""
1049
+ options = {'maxev': 100,
1050
+ 'disp': True}
1051
+
1052
+ res = shgo(test_infeasible.f, test_infeasible.bounds,
1053
+ constraints=test_infeasible.cons, n=100, options=options,
1054
+ sampling_method='sobol')
1055
+
1056
+ np.testing.assert_equal(False, res.success)
1057
+
1058
+ def test_5_1_2_infeasible_sobol(self):
1059
+ """Ensures the algorithm terminates on infeasible problems
1060
+ after maxev is exceeded. Do not use infty constraints option"""
1061
+ options = {'maxev': 100,
1062
+ 'disp': True,
1063
+ 'infty_constraints': False}
1064
+
1065
+ res = shgo(test_infeasible.f, test_infeasible.bounds,
1066
+ constraints=test_infeasible.cons, n=100, options=options,
1067
+ sampling_method='sobol')
1068
+
1069
+ np.testing.assert_equal(False, res.success)
1070
+
1071
+ def test_5_2_infeasible_simplicial(self):
1072
+ """Ensures the algorithm terminates on infeasible problems
1073
+ after maxev is exceeded."""
1074
+ options = {'maxev': 1000,
1075
+ 'disp': False}
1076
+
1077
+ res = shgo(test_infeasible.f, test_infeasible.bounds,
1078
+ constraints=test_infeasible.cons, n=100, options=options,
1079
+ sampling_method='simplicial')
1080
+
1081
+ np.testing.assert_equal(False, res.success)
1082
+
1083
+ def test_6_1_lower_known_f_min(self):
1084
+ """Test Global mode limiting local evaluations with f* too high"""
1085
+ options = { # Specify known function value
1086
+ 'f_min': test2_1.expected_fun + 2.0,
1087
+ 'f_tol': 1e-6,
1088
+ # Specify number of local iterations to perform+
1089
+ 'minimize_every_iter': True,
1090
+ 'local_iter': 1,
1091
+ 'infty_constraints': False}
1092
+ args = (test2_1.f, test2_1.bounds)
1093
+ kwargs = {'constraints': test2_1.cons,
1094
+ 'n': None,
1095
+ 'iters': None,
1096
+ 'options': options,
1097
+ 'sampling_method': 'sobol'
1098
+ }
1099
+ warns(UserWarning, shgo, *args, **kwargs)
1100
+
1101
+ def test(self):
1102
+ from scipy.optimize import rosen, shgo
1103
+ bounds = [(0, 2), (0, 2), (0, 2), (0, 2), (0, 2)]
1104
+
1105
+ def fun(x):
1106
+ fun.nfev += 1
1107
+ return rosen(x)
1108
+
1109
+ fun.nfev = 0
1110
+
1111
+ result = shgo(fun, bounds)
1112
+ print(result.x, result.fun, fun.nfev) # 50
1113
+
1114
+
1115
+ # Returns
1116
+ class TestShgoReturns:
1117
+ def test_1_nfev_simplicial(self):
1118
+ bounds = [(0, 2), (0, 2), (0, 2), (0, 2), (0, 2)]
1119
+
1120
+ def fun(x):
1121
+ fun.nfev += 1
1122
+ return rosen(x)
1123
+
1124
+ fun.nfev = 0
1125
+
1126
+ result = shgo(fun, bounds)
1127
+ np.testing.assert_equal(fun.nfev, result.nfev)
1128
+
1129
+ def test_1_nfev_sobol(self):
1130
+ bounds = [(0, 2), (0, 2), (0, 2), (0, 2), (0, 2)]
1131
+
1132
+ def fun(x):
1133
+ fun.nfev += 1
1134
+ return rosen(x)
1135
+
1136
+ fun.nfev = 0
1137
+
1138
+ result = shgo(fun, bounds, sampling_method='sobol')
1139
+ np.testing.assert_equal(fun.nfev, result.nfev)
1140
+
1141
+
1142
+ def test_vector_constraint():
1143
+ # gh15514
1144
+ def quad(x):
1145
+ x = np.asarray(x)
1146
+ return [np.sum(x ** 2)]
1147
+
1148
+ nlc = NonlinearConstraint(quad, [2.2], [3])
1149
+ oldc = new_constraint_to_old(nlc, np.array([1.0, 1.0]))
1150
+
1151
+ res = shgo(rosen, [(0, 10), (0, 10)], constraints=oldc, sampling_method='sobol')
1152
+ assert np.all(np.sum((res.x)**2) >= 2.2)
1153
+ assert np.all(np.sum((res.x) ** 2) <= 3.0)
1154
+ assert res.success
1155
+
1156
+
1157
+ @pytest.mark.filterwarnings("ignore:delta_grad")
1158
+ def test_trust_constr():
1159
+ def quad(x):
1160
+ x = np.asarray(x)
1161
+ return [np.sum(x ** 2)]
1162
+
1163
+ nlc = NonlinearConstraint(quad, [2.6], [3])
1164
+ minimizer_kwargs = {'method': 'trust-constr'}
1165
+ # note that we don't supply the constraints in minimizer_kwargs,
1166
+ # so if the final result obeys the constraints we know that shgo
1167
+ # passed them on to 'trust-constr'
1168
+ res = shgo(
1169
+ rosen,
1170
+ [(0, 10), (0, 10)],
1171
+ constraints=nlc,
1172
+ sampling_method='sobol',
1173
+ minimizer_kwargs=minimizer_kwargs
1174
+ )
1175
+ assert np.all(np.sum((res.x)**2) >= 2.6)
1176
+ assert np.all(np.sum((res.x) ** 2) <= 3.0)
1177
+ assert res.success
1178
+
1179
+
1180
+ def test_equality_constraints():
1181
+ # gh16260
1182
+ bounds = [(0.9, 4.0)] * 2 # Constrain probabilities to 0 and 1.
1183
+
1184
+ def faulty(x):
1185
+ return x[0] + x[1]
1186
+
1187
+ nlc = NonlinearConstraint(faulty, 3.9, 3.9)
1188
+ res = shgo(rosen, bounds=bounds, constraints=nlc)
1189
+ assert_allclose(np.sum(res.x), 3.9)
1190
+
1191
+ def faulty(x):
1192
+ return x[0] + x[1] - 3.9
1193
+
1194
+ constraints = {'type': 'eq', 'fun': faulty}
1195
+ res = shgo(rosen, bounds=bounds, constraints=constraints)
1196
+ assert_allclose(np.sum(res.x), 3.9)
1197
+
1198
+ bounds = [(0, 1.0)] * 4
1199
+ # sum of variable should equal 1.
1200
+ def faulty(x):
1201
+ return x[0] + x[1] + x[2] + x[3] - 1
1202
+
1203
+ # options = {'minimize_every_iter': True, 'local_iter':10}
1204
+ constraints = {'type': 'eq', 'fun': faulty}
1205
+ res = shgo(
1206
+ lambda x: - np.prod(x),
1207
+ bounds=bounds,
1208
+ constraints=constraints,
1209
+ sampling_method='sobol'
1210
+ )
1211
+ assert_allclose(np.sum(res.x), 1.0)
1212
+
1213
+ def test_gh16971():
1214
+ def cons(x):
1215
+ return np.sum(x**2) - 0
1216
+
1217
+ c = {'fun': cons, 'type': 'ineq'}
1218
+ minimizer_kwargs = {
1219
+ 'method': 'COBYLA',
1220
+ 'options': {'rhobeg': 5, 'tol': 5e-1, 'catol': 0.05}
1221
+ }
1222
+
1223
+ s = SHGO(
1224
+ rosen, [(0, 10)]*2, constraints=c, minimizer_kwargs=minimizer_kwargs
1225
+ )
1226
+
1227
+ assert s.minimizer_kwargs['method'].lower() == 'cobyla'
1228
+ assert s.minimizer_kwargs['options']['catol'] == 0.05
loan-advisor-agent/.venv/Lib/site-packages/scipy/optimize/tests/test__spectral.py ADDED
@@ -0,0 +1,225 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import itertools
2
+
3
+ import numpy as np
4
+ from numpy import exp
5
+ from numpy.testing import assert_, assert_equal
6
+
7
+ from scipy.optimize import root
8
+
9
+
10
+ def test_performance():
11
+ # Compare performance results to those listed in
12
+ # [Cheng & Li, IMA J. Num. An. 29, 814 (2008)]
13
+ # and
14
+ # [W. La Cruz, J.M. Martinez, M. Raydan, Math. Comp. 75, 1429 (2006)].
15
+ # and those produced by dfsane.f from M. Raydan's website.
16
+ #
17
+ # Where the results disagree, the largest limits are taken.
18
+
19
+ e_a = 1e-5
20
+ e_r = 1e-4
21
+
22
+ table_1 = [
23
+ dict(F=F_1, x0=x0_1, n=1000, nit=5, nfev=5),
24
+ dict(F=F_1, x0=x0_1, n=10000, nit=2, nfev=2),
25
+ dict(F=F_2, x0=x0_2, n=500, nit=11, nfev=11),
26
+ dict(F=F_2, x0=x0_2, n=2000, nit=11, nfev=11),
27
+ # dict(F=F_4, x0=x0_4, n=999, nit=243, nfev=1188) removed:
28
+ # too sensitive to rounding errors
29
+ # Results from dfsane.f; papers list nit=3, nfev=3
30
+ dict(F=F_6, x0=x0_6, n=100, nit=6, nfev=6),
31
+ # Must have n%3==0, typo in papers?
32
+ dict(F=F_7, x0=x0_7, n=99, nit=23, nfev=29),
33
+ # Must have n%3==0, typo in papers?
34
+ dict(F=F_7, x0=x0_7, n=999, nit=23, nfev=29),
35
+ # Results from dfsane.f; papers list nit=nfev=6?
36
+ dict(F=F_9, x0=x0_9, n=100, nit=12, nfev=18),
37
+ dict(F=F_9, x0=x0_9, n=1000, nit=12, nfev=18),
38
+ # Results from dfsane.f; papers list nit=2, nfev=12
39
+ dict(F=F_10, x0=x0_10, n=1000, nit=5, nfev=5),
40
+ ]
41
+
42
+ # Check also scaling invariance
43
+ for xscale, yscale, line_search in itertools.product(
44
+ [1.0, 1e-10, 1e10], [1.0, 1e-10, 1e10], ['cruz', 'cheng']
45
+ ):
46
+ for problem in table_1:
47
+ n = problem['n']
48
+ def func(x, n):
49
+ return yscale * problem['F'](x / xscale, n)
50
+ args = (n,)
51
+ x0 = problem['x0'](n) * xscale
52
+
53
+ fatol = np.sqrt(n) * e_a * yscale + e_r * np.linalg.norm(func(x0, n))
54
+
55
+ sigma_eps = 1e-10 * min(yscale/xscale, xscale/yscale)
56
+ sigma_0 = xscale/yscale
57
+
58
+ with np.errstate(over='ignore'):
59
+ sol = root(func, x0, args=args,
60
+ options=dict(ftol=0, fatol=fatol, maxfev=problem['nfev'] + 1,
61
+ sigma_0=sigma_0, sigma_eps=sigma_eps,
62
+ line_search=line_search),
63
+ method='DF-SANE')
64
+
65
+ err_msg = repr(
66
+ [xscale, yscale, line_search, problem, np.linalg.norm(func(sol.x, n)),
67
+ fatol, sol.success, sol.nit, sol.nfev]
68
+ )
69
+ assert sol.success, err_msg
70
+ # nfev+1: dfsane.f doesn't count first eval
71
+ assert sol.nfev <= problem['nfev'] + 1, err_msg
72
+ assert sol.nit <= problem['nit'], err_msg
73
+ assert np.linalg.norm(func(sol.x, n)) <= fatol, err_msg
74
+
75
+
76
+ def test_complex():
77
+ def func(z):
78
+ return z**2 - 1 + 2j
79
+ x0 = 2.0j
80
+
81
+ ftol = 1e-4
82
+ sol = root(func, x0, tol=ftol, method='DF-SANE')
83
+
84
+ assert_(sol.success)
85
+
86
+ f0 = np.linalg.norm(func(x0))
87
+ fx = np.linalg.norm(func(sol.x))
88
+ assert_(fx <= ftol*f0)
89
+
90
+
91
+ def test_linear_definite():
92
+ # The DF-SANE paper proves convergence for "strongly isolated"
93
+ # solutions.
94
+ #
95
+ # For linear systems F(x) = A x - b = 0, with A positive or
96
+ # negative definite, the solution is strongly isolated.
97
+
98
+ def check_solvability(A, b, line_search='cruz'):
99
+ def func(x):
100
+ return A.dot(x) - b
101
+ xp = np.linalg.solve(A, b)
102
+ eps = np.linalg.norm(func(xp)) * 1e3
103
+ sol = root(
104
+ func, b,
105
+ options=dict(fatol=eps, ftol=0, maxfev=17523, line_search=line_search),
106
+ method='DF-SANE',
107
+ )
108
+ assert_(sol.success)
109
+ assert_(np.linalg.norm(func(sol.x)) <= eps)
110
+
111
+ n = 90
112
+
113
+ # Test linear pos.def. system
114
+ A = np.arange(n*n).reshape(n, n)
115
+ A = A + n*n * np.diag(1 + np.arange(n))
116
+ assert_(np.linalg.eigvals(A).min() > 0)
117
+ b = np.arange(n) * 1.0
118
+ check_solvability(A, b, 'cruz')
119
+ check_solvability(A, b, 'cheng')
120
+
121
+ # Test linear neg.def. system
122
+ check_solvability(-A, b, 'cruz')
123
+ check_solvability(-A, b, 'cheng')
124
+
125
+
126
+ def test_shape():
127
+ def f(x, arg):
128
+ return x - arg
129
+
130
+ for dt in [float, complex]:
131
+ x = np.zeros([2,2])
132
+ arg = np.ones([2,2], dtype=dt)
133
+
134
+ sol = root(f, x, args=(arg,), method='DF-SANE')
135
+ assert_(sol.success)
136
+ assert_equal(sol.x.shape, x.shape)
137
+
138
+
139
+ # Some of the test functions and initial guesses listed in
140
+ # [W. La Cruz, M. Raydan. Optimization Methods and Software, 18, 583 (2003)]
141
+
142
+ def F_1(x, n):
143
+ g = np.zeros([n])
144
+ i = np.arange(2, n+1)
145
+ g[0] = exp(x[0] - 1) - 1
146
+ g[1:] = i*(exp(x[1:] - 1) - x[1:])
147
+ return g
148
+
149
+ def x0_1(n):
150
+ x0 = np.empty([n])
151
+ x0.fill(n/(n-1))
152
+ return x0
153
+
154
+ def F_2(x, n):
155
+ g = np.zeros([n])
156
+ i = np.arange(2, n+1)
157
+ g[0] = exp(x[0]) - 1
158
+ g[1:] = 0.1*i*(exp(x[1:]) + x[:-1] - 1)
159
+ return g
160
+
161
+ def x0_2(n):
162
+ x0 = np.empty([n])
163
+ x0.fill(1/n**2)
164
+ return x0
165
+
166
+
167
+ def F_4(x, n): # skip name check
168
+ assert_equal(n % 3, 0)
169
+ g = np.zeros([n])
170
+ # Note: the first line is typoed in some of the references;
171
+ # correct in original [Gasparo, Optimization Meth. 13, 79 (2000)]
172
+ g[::3] = 0.6 * x[::3] + 1.6 * x[1::3]**3 - 7.2 * x[1::3]**2 + 9.6 * x[1::3] - 4.8
173
+ g[1::3] = (0.48 * x[::3] - 0.72 * x[1::3]**3 + 3.24 * x[1::3]**2 - 4.32 * x[1::3]
174
+ - x[2::3] + 0.2 * x[2::3]**3 + 2.16)
175
+ g[2::3] = 1.25 * x[2::3] - 0.25*x[2::3]**3
176
+ return g
177
+
178
+
179
+ def x0_4(n): # skip name check
180
+ assert_equal(n % 3, 0)
181
+ x0 = np.array([-1, 1/2, -1] * (n//3))
182
+ return x0
183
+
184
+ def F_6(x, n):
185
+ c = 0.9
186
+ mu = (np.arange(1, n+1) - 0.5)/n
187
+ return x - 1/(1 - c/(2*n) * (mu[:,None]*x / (mu[:,None] + mu)).sum(axis=1))
188
+
189
+ def x0_6(n):
190
+ return np.ones([n])
191
+
192
+ def F_7(x, n):
193
+ assert_equal(n % 3, 0)
194
+
195
+ def phi(t):
196
+ v = 0.5*t - 2
197
+ v[t > -1] = ((-592*t**3 + 888*t**2 + 4551*t - 1924)/1998)[t > -1]
198
+ v[t >= 2] = (0.5*t + 2)[t >= 2]
199
+ return v
200
+ g = np.zeros([n])
201
+ g[::3] = 1e4 * x[1::3]**2 - 1
202
+ g[1::3] = exp(-x[::3]) + exp(-x[1::3]) - 1.0001
203
+ g[2::3] = phi(x[2::3])
204
+ return g
205
+
206
+ def x0_7(n):
207
+ assert_equal(n % 3, 0)
208
+ return np.array([1e-3, 18, 1] * (n//3))
209
+
210
+ def F_9(x, n):
211
+ g = np.zeros([n])
212
+ i = np.arange(2, n)
213
+ g[0] = x[0]**3/3 + x[1]**2/2
214
+ g[1:-1] = -x[1:-1]**2/2 + i*x[1:-1]**3/3 + x[2:]**2/2
215
+ g[-1] = -x[-1]**2/2 + n*x[-1]**3/3
216
+ return g
217
+
218
+ def x0_9(n):
219
+ return np.ones([n])
220
+
221
+ def F_10(x, n):
222
+ return np.log(1 + x) - x/n
223
+
224
+ def x0_10(n):
225
+ return np.ones([n])
loan-advisor-agent/.venv/Lib/site-packages/scipy/optimize/tests/test_bracket.py ADDED
@@ -0,0 +1,888 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import pytest
2
+
3
+ import numpy as np
4
+
5
+ from scipy.optimize._bracket import _ELIMITS
6
+ from scipy.optimize.elementwise import bracket_root, bracket_minimum
7
+ import scipy._lib._elementwise_iterative_method as eim
8
+ from scipy import stats
9
+ from scipy._lib._array_api_no_0d import (xp_assert_close, xp_assert_equal,
10
+ xp_assert_less)
11
+ from scipy._lib._array_api import xp_ravel, make_xp_test_case
12
+
13
+
14
+ # These tests were originally written for the private `optimize._bracket`
15
+ # interfaces, but now we want the tests to check the behavior of the public
16
+ # `optimize.elementwise` interfaces. Therefore, rather than importing
17
+ # `_bracket_root`/`_bracket_minimum` from `_bracket.py`, we import
18
+ # `bracket_root`/`bracket_minimum` from `optimize.elementwise` and wrap those
19
+ # functions to conform to the private interface. This may look a little strange,
20
+ # since it effectively just inverts the interface transformation done within the
21
+ # `bracket_root`/`bracket_minimum` functions, but it allows us to run the original,
22
+ # unmodified tests on the public interfaces, simplifying the PR that adds
23
+ # the public interfaces. We'll refactor this when we want to @parametrize the
24
+ # tests over multiple `method`s.
25
+ def _bracket_root(*args, **kwargs):
26
+ res = bracket_root(*args, **kwargs)
27
+ res.xl, res.xr = res.bracket
28
+ res.fl, res.fr = res.f_bracket
29
+ del res.bracket
30
+ del res.f_bracket
31
+ return res
32
+
33
+
34
+ def _bracket_minimum(*args, **kwargs):
35
+ res = bracket_minimum(*args, **kwargs)
36
+ res.xl, res.xm, res.xr = res.bracket
37
+ res.fl, res.fm, res.fr = res.f_bracket
38
+ del res.bracket
39
+ del res.f_bracket
40
+ return res
41
+
42
+
43
+ @make_xp_test_case(bracket_root)
44
+ class TestBracketRoot:
45
+ @pytest.mark.parametrize("seed", (615655101, 3141866013, 238075752))
46
+ @pytest.mark.parametrize("use_xmin", (False, True))
47
+ @pytest.mark.parametrize("other_side", (False, True))
48
+ @pytest.mark.parametrize("fix_one_side", (False, True))
49
+ def test_nfev_expected(self, seed, use_xmin, other_side, fix_one_side, xp):
50
+ # Property-based test to confirm that _bracket_root is behaving as
51
+ # expected. The basic case is when root < a < b.
52
+ # The number of times bracket expands (per side) can be found by
53
+ # setting the expression for the left endpoint of the bracket to the
54
+ # root of f (x=0), solving for i, and rounding up. The corresponding
55
+ # lower and upper ends of the bracket are found by plugging this back
56
+ # into the expression for the ends of the bracket.
57
+ # `other_side=True` is the case that a < b < root
58
+ # Special cases like a < root < b are tested separately
59
+ rng = np.random.default_rng(seed)
60
+ xl0, d, factor = xp.asarray(rng.random(size=3) * [1e5, 10, 5])
61
+ factor = 1 + factor # factor must be greater than 1
62
+ xr0 = xl0 + d # xr0 must be greater than a in basic case
63
+
64
+ def f(x):
65
+ f.count += 1
66
+ return x # root is 0
67
+
68
+ if use_xmin:
69
+ xmin = xp.asarray(-rng.random())
70
+ n = xp.ceil(xp.log(-(xl0 - xmin) / xmin) / xp.log(factor))
71
+ l, u = xmin + (xl0 - xmin)*factor**-n, xmin + (xl0 - xmin)*factor**-(n - 1)
72
+ kwargs = dict(xl0=xl0, xr0=xr0, factor=factor, xmin=xmin)
73
+ else:
74
+ n = xp.ceil(xp.log(xr0/d) / xp.log(factor))
75
+ l, u = xr0 - d*factor**n, xr0 - d*factor**(n-1)
76
+ kwargs = dict(xl0=xl0, xr0=xr0, factor=factor)
77
+
78
+ if other_side:
79
+ kwargs['xl0'], kwargs['xr0'] = -kwargs['xr0'], -kwargs['xl0']
80
+ l, u = -u, -l
81
+ if 'xmin' in kwargs:
82
+ kwargs['xmax'] = -kwargs.pop('xmin')
83
+
84
+ if fix_one_side:
85
+ if other_side:
86
+ kwargs['xmin'] = -xr0
87
+ else:
88
+ kwargs['xmax'] = xr0
89
+
90
+ f.count = 0
91
+ res = _bracket_root(f, **kwargs)
92
+
93
+ # Compare reported number of function evaluations `nfev` against
94
+ # reported `nit`, actual function call count `f.count`, and theoretical
95
+ # number of expansions `n`.
96
+ # When both sides are free, these get multiplied by 2 because function
97
+ # is evaluated on the left and the right each iteration.
98
+ # When one side is fixed, however, we add one: on the right side, the
99
+ # function gets evaluated once at b.
100
+ # Add 1 to `n` and `res.nit` because function evaluations occur at
101
+ # iterations *0*, 1, ..., `n`. Subtract 1 from `f.count` because
102
+ # function is called separately for left and right in iteration 0.
103
+ if not fix_one_side:
104
+ assert res.nfev == 2*(res.nit+1) == 2*(f.count-1) == 2*(n + 1)
105
+ else:
106
+ assert res.nfev == (res.nit+1)+1 == (f.count-1)+1 == (n+1)+1
107
+
108
+ # Compare reported bracket to theoretical bracket and reported function
109
+ # values to function evaluated at bracket.
110
+ bracket = xp.asarray([res.xl, res.xr])
111
+ xp_assert_close(bracket, xp.asarray([l, u]))
112
+ f_bracket = xp.asarray([res.fl, res.fr])
113
+ xp_assert_close(f_bracket, f(bracket))
114
+
115
+ # Check that bracket is valid and that status and success are correct
116
+ assert res.xr > res.xl
117
+ signs = xp.sign(f_bracket)
118
+ assert signs[0] == -signs[1]
119
+ assert res.status == 0
120
+ assert res.success
121
+
122
+ def f(self, q, p):
123
+ return stats._stats_py._SimpleNormal().cdf(q) - p
124
+
125
+ @pytest.mark.parametrize('p', [0.6, np.linspace(0.05, 0.95, 10)])
126
+ @pytest.mark.parametrize('xmin', [-5, None])
127
+ @pytest.mark.parametrize('xmax', [5, None])
128
+ @pytest.mark.parametrize('factor', [1.2, 2])
129
+ def test_basic(self, p, xmin, xmax, factor, xp):
130
+ # Test basic functionality to bracket root (distribution PPF)
131
+ res = _bracket_root(self.f, xp.asarray(-0.01), 0.01, xmin=xmin, xmax=xmax,
132
+ factor=factor, args=(xp.asarray(p),))
133
+ xp_assert_equal(-xp.sign(res.fl), xp.sign(res.fr))
134
+
135
+ @pytest.mark.parametrize('shape', [tuple(), (12,), (3, 4), (3, 2, 2)])
136
+ def test_vectorization(self, shape, xp):
137
+ # Test for correct functionality, output shapes, and dtypes for various
138
+ # input shapes.
139
+ p = np.linspace(-0.05, 1.05, 12).reshape(shape) if shape else np.float64(0.6)
140
+ args = (p,)
141
+ maxiter = 10
142
+
143
+ @np.vectorize
144
+ def bracket_root_single(xl0, xr0, xmin, xmax, factor, p):
145
+ return _bracket_root(self.f, xl0, xr0, xmin=xmin, xmax=xmax,
146
+ factor=factor, args=(p,),
147
+ maxiter=maxiter)
148
+
149
+ def f(*args, **kwargs):
150
+ f.f_evals += 1
151
+ return self.f(*args, **kwargs)
152
+ f.f_evals = 0
153
+
154
+ rng = np.random.default_rng(2348234)
155
+ xl0 = -rng.random(size=shape)
156
+ xr0 = rng.random(size=shape)
157
+ xmin, xmax = 1e3*xl0, 1e3*xr0
158
+ if shape: # make some elements un
159
+ i = rng.random(size=shape) > 0.5
160
+ xmin[i], xmax[i] = -np.inf, np.inf
161
+ factor = rng.random(size=shape) + 1.5
162
+ refs = bracket_root_single(xl0, xr0, xmin, xmax, factor, p).ravel()
163
+ xl0, xr0, xmin, xmax, factor = (xp.asarray(xl0), xp.asarray(xr0),
164
+ xp.asarray(xmin), xp.asarray(xmax),
165
+ xp.asarray(factor))
166
+ args = tuple(map(xp.asarray, args))
167
+ res = _bracket_root(f, xl0, xr0, xmin=xmin, xmax=xmax, factor=factor,
168
+ args=args, maxiter=maxiter)
169
+
170
+ attrs = ['xl', 'xr', 'fl', 'fr', 'success', 'nfev', 'nit']
171
+ for attr in attrs:
172
+ ref_attr = [xp.asarray(getattr(ref, attr)) for ref in refs]
173
+ res_attr = getattr(res, attr)
174
+ xp_assert_close(xp_ravel(res_attr, xp=xp), xp.stack(ref_attr))
175
+ assert res_attr.shape == shape
176
+
177
+ assert res.success.dtype == xp.bool
178
+ if shape:
179
+ assert xp.all(res.success[1:-1])
180
+ assert res.status.dtype == xp.int32
181
+ assert res.nfev.dtype == xp.int32
182
+ assert res.nit.dtype == xp.int32
183
+ assert xp.max(res.nit) == f.f_evals - 2
184
+ xp_assert_less(res.xl, res.xr)
185
+ xp_assert_close(res.fl, xp.asarray(self.f(res.xl, *args)))
186
+ xp_assert_close(res.fr, xp.asarray(self.f(res.xr, *args)))
187
+
188
+ def test_flags(self, xp):
189
+ # Test cases that should produce different status flags; show that all
190
+ # can be produced simultaneously.
191
+ def f(xs, js):
192
+ funcs = [lambda x: x - 1.5,
193
+ lambda x: x - 1000,
194
+ lambda x: x - 1000,
195
+ lambda x: x * xp.nan,
196
+ lambda x: x]
197
+
198
+ return [funcs[int(j)](x) for x, j in zip(xs, js)]
199
+
200
+ args = (xp.arange(5, dtype=xp.int64),)
201
+ res = _bracket_root(f,
202
+ xl0=xp.asarray([-1., -1., -1., -1., 4.]),
203
+ xr0=xp.asarray([1, 1, 1, 1, -4]),
204
+ xmin=xp.asarray([-xp.inf, -1, -xp.inf, -xp.inf, 6]),
205
+ xmax=xp.asarray([xp.inf, 1, xp.inf, xp.inf, 2]),
206
+ args=args, maxiter=3)
207
+
208
+ ref_flags = xp.asarray([eim._ECONVERGED,
209
+ _ELIMITS,
210
+ eim._ECONVERR,
211
+ eim._EVALUEERR,
212
+ eim._EINPUTERR],
213
+ dtype=xp.int32)
214
+
215
+ xp_assert_equal(res.status, ref_flags)
216
+
217
+ @pytest.mark.parametrize("root", (0.622, [0.622, 0.623]))
218
+ @pytest.mark.parametrize('xmin', [-5, None])
219
+ @pytest.mark.parametrize('xmax', [5, None])
220
+ @pytest.mark.parametrize("dtype", ("float16", "float32", "float64"))
221
+ def test_dtype(self, root, xmin, xmax, dtype, xp):
222
+ # Test that dtypes are preserved
223
+ dtype = getattr(xp, dtype)
224
+ xmin = xmin if xmin is None else xp.asarray(xmin, dtype=dtype)
225
+ xmax = xmax if xmax is None else xp.asarray(xmax, dtype=dtype)
226
+ root = xp.asarray(root, dtype=dtype)
227
+ def f(x, root):
228
+ return xp.astype((x - root) ** 3, dtype)
229
+
230
+ bracket = xp.asarray([-0.01, 0.01], dtype=dtype)
231
+ res = _bracket_root(f, *bracket, xmin=xmin, xmax=xmax, args=(root,))
232
+ assert xp.all(res.success)
233
+ assert res.xl.dtype == res.xr.dtype == dtype
234
+ assert res.fl.dtype == res.fr.dtype == dtype
235
+
236
+ def test_input_validation(self, xp):
237
+ # Test input validation for appropriate error messages
238
+
239
+ message = '`func` must be callable.'
240
+ with pytest.raises(ValueError, match=message):
241
+ _bracket_root(None, -4, 4)
242
+
243
+ message = '...must be numeric and real.'
244
+ with pytest.raises(ValueError, match=message):
245
+ _bracket_root(lambda x: x, -4+1j, 4)
246
+ with pytest.raises(ValueError, match=message):
247
+ _bracket_root(lambda x: x, -4, 4+1j)
248
+ with pytest.raises(ValueError, match=message):
249
+ _bracket_root(lambda x: x, -4, 4, xmin=4+1j)
250
+ with pytest.raises(ValueError, match=message):
251
+ _bracket_root(lambda x: x, -4, 4, xmax=4+1j)
252
+ with pytest.raises(ValueError, match=message):
253
+ _bracket_root(lambda x: x, -4, 4, factor=4+1j)
254
+
255
+ message = "All elements of `factor` must be greater than 1."
256
+ with pytest.raises(ValueError, match=message):
257
+ _bracket_root(lambda x: x, -4, 4, factor=0.5)
258
+
259
+ message = "broadcast"
260
+ # raised by `xp.broadcast, but the traceback is readable IMO
261
+ with pytest.raises(Exception, match=message):
262
+ _bracket_root(lambda x: x, xp.asarray([-2, -3]), xp.asarray([3, 4, 5]))
263
+ # Consider making this give a more readable error message
264
+ # with pytest.raises(ValueError, match=message):
265
+ # _bracket_root(lambda x: [x[0], x[1], x[1]], [-3, -3], [5, 5])
266
+
267
+ message = '`maxiter` must be a non-negative integer.'
268
+ with pytest.raises(ValueError, match=message):
269
+ _bracket_root(lambda x: x, -4, 4, maxiter=1.5)
270
+ with pytest.raises(ValueError, match=message):
271
+ _bracket_root(lambda x: x, -4, 4, maxiter=-1)
272
+ with pytest.raises(ValueError, match=message):
273
+ _bracket_root(lambda x: x, -4, 4, maxiter="shrubbery")
274
+
275
+ def test_special_cases(self, xp):
276
+ # Test edge cases and other special cases
277
+ # Test that integers are not passed to `f`
278
+ # (otherwise this would overflow)
279
+ def f(x):
280
+ assert xp.isdtype(x.dtype, "real floating")
281
+ return x ** 99 - 1
282
+
283
+ res = _bracket_root(f, xp.asarray(-7.), xp.asarray(5.))
284
+ assert res.success
285
+
286
+ # Test maxiter = 0. Should do nothing to bracket.
287
+ def f(x):
288
+ return x - 10
289
+
290
+ bracket = (xp.asarray(-3.), xp.asarray(5.))
291
+ res = _bracket_root(f, *bracket, maxiter=0)
292
+ assert res.xl, res.xr == bracket
293
+ assert res.nit == 0
294
+ assert res.nfev == 2
295
+ assert res.status == -2
296
+
297
+ # Test scalar `args` (not in tuple)
298
+ def f(x, c):
299
+ return c*x - 1
300
+
301
+ res = _bracket_root(f, xp.asarray(-1.), xp.asarray(1.),
302
+ args=xp.asarray(3.))
303
+ assert res.success
304
+ xp_assert_close(res.fl, f(res.xl, 3))
305
+
306
+ # Test other edge cases
307
+
308
+ def f(x):
309
+ f.count += 1
310
+ return x
311
+
312
+ # 1. root lies within guess of bracket
313
+ f.count = 0
314
+ _bracket_root(f, xp.asarray(-10), xp.asarray(20))
315
+ assert f.count == 2
316
+
317
+ # 2. bracket endpoint hits root exactly
318
+ f.count = 0
319
+ res = _bracket_root(f, xp.asarray(5.), xp.asarray(10.),
320
+ factor=2)
321
+
322
+ assert res.nfev == 4
323
+ xp_assert_close(res.xl, xp.asarray(0.), atol=1e-15)
324
+ xp_assert_close(res.xr, xp.asarray(5.), atol=1e-15)
325
+
326
+ # 3. bracket limit hits root exactly
327
+ with np.errstate(over='ignore'):
328
+ res = _bracket_root(f, xp.asarray(5.), xp.asarray(10.),
329
+ xmin=0)
330
+ xp_assert_close(res.xl, xp.asarray(0.), atol=1e-15)
331
+
332
+ with np.errstate(over='ignore'):
333
+ res = _bracket_root(f, xp.asarray(-10.), xp.asarray(-5.),
334
+ xmax=0)
335
+ xp_assert_close(res.xr, xp.asarray(0.), atol=1e-15)
336
+
337
+ # 4. bracket not within min, max
338
+ with np.errstate(over='ignore'):
339
+ res = _bracket_root(f, xp.asarray(5.), xp.asarray(10.),
340
+ xmin=1)
341
+ assert not res.success
342
+
343
+ def test_bug_fixes(self):
344
+ # 1. Bug in double sided bracket search.
345
+ # Happened in some cases where there are terminations on one side
346
+ # after corresponding searches on other side failed due to reaching the
347
+ # boundary.
348
+
349
+ # https://github.com/scipy/scipy/pull/22560#discussion_r1962853839
350
+ def f(x, p):
351
+ return np.exp(x) - p
352
+
353
+ p = np.asarray([0.29, 0.35])
354
+ res = _bracket_root(f, xl0=-1, xmin=-np.inf, xmax=0, args=(p, ))
355
+
356
+ # https://github.com/scipy/scipy/pull/22560/files#r1962952517
357
+ def f(x, p, c):
358
+ return np.exp(x*c) - p
359
+
360
+ p = [0.32061201, 0.39175242, 0.40047535, 0.50527218, 0.55654373,
361
+ 0.11911647, 0.37507896, 0.66554191]
362
+ c = [1., -1., 1., 1., -1., 1., 1., 1.]
363
+ xl0 = [-7.63108551, 3.27840947, -8.36968526, -1.78124372,
364
+ 0.92201295, -2.48930123, -0.66733533, -0.44606749]
365
+ xr0 = [-6.63108551, 4.27840947, -7.36968526, -0.78124372,
366
+ 1.92201295, -1.48930123, 0., 0.]
367
+ xmin = [-np.inf, 0., -np.inf, -np.inf, 0., -np.inf, -np.inf,
368
+ -np.inf]
369
+ xmax = [0., np.inf, 0., 0., np.inf, 0., 0., 0.]
370
+
371
+ res = _bracket_root(f, xl0=xl0, xr0=xr0, xmin=xmin, xmax=xmax, args=(p, c))
372
+
373
+ # 2. Default xl0 + 1 for xr0 exceeds xmax.
374
+ # https://github.com/scipy/scipy/pull/22560#discussion_r1962947434
375
+ res = _bracket_root(lambda x: x + 0.25, xl0=-0.5, xmin=-np.inf, xmax=0)
376
+ assert res.success
377
+
378
+
379
+ @make_xp_test_case(bracket_minimum)
380
+ class TestBracketMinimum:
381
+ def init_f(self):
382
+ def f(x, a, b):
383
+ f.count += 1
384
+ return (x - a)**2 + b
385
+ f.count = 0
386
+ return f
387
+
388
+ def assert_valid_bracket(self, result, xp):
389
+ assert xp.all(
390
+ (result.xl < result.xm) & (result.xm < result.xr)
391
+ )
392
+ assert xp.all(
393
+ (result.fl >= result.fm) & (result.fr > result.fm)
394
+ | (result.fl > result.fm) & (result.fr > result.fm)
395
+ )
396
+
397
+ def get_kwargs(
398
+ self, *, xl0=None, xr0=None, factor=None, xmin=None, xmax=None, args=None
399
+ ):
400
+ names = ("xl0", "xr0", "xmin", "xmax", "factor", "args")
401
+ return {
402
+ name: val for name, val in zip(names, (xl0, xr0, xmin, xmax, factor, args))
403
+ if val is not None
404
+ }
405
+
406
+ @pytest.mark.parametrize(
407
+ "seed",
408
+ (
409
+ 307448016549685229886351382450158984917,
410
+ 11650702770735516532954347931959000479,
411
+ 113767103358505514764278732330028568336,
412
+ )
413
+ )
414
+ @pytest.mark.parametrize("use_xmin", (False, True))
415
+ @pytest.mark.parametrize("other_side", (False, True))
416
+ def test_nfev_expected(self, seed, use_xmin, other_side, xp):
417
+ rng = np.random.default_rng(seed)
418
+ args = (xp.asarray(0.), xp.asarray(0.)) # f(x) = x^2 with minimum at 0
419
+ # xl0, xm0, xr0 are chosen such that the initial bracket is to
420
+ # the right of the minimum, and the bracket will expand
421
+ # downhill towards zero.
422
+ xl0, d1, d2, factor = xp.asarray(rng.random(size=4) * [1e5, 10, 10, 5])
423
+ xm0 = xl0 + d1
424
+ xr0 = xm0 + d2
425
+ # Factor should be greater than one.
426
+ factor += 1
427
+
428
+ if use_xmin:
429
+ xmin = xp.asarray(-rng.random() * 5, dtype=xp.float64)
430
+ n = int(xp.ceil(xp.log(-(xl0 - xmin) / xmin) / xp.log(factor)))
431
+ lower = xmin + (xl0 - xmin)*factor**-n
432
+ middle = xmin + (xl0 - xmin)*factor**-(n-1)
433
+ upper = xmin + (xl0 - xmin)*factor**-(n-2) if n > 1 else xm0
434
+ # It may be the case the lower is below the minimum, but we still
435
+ # don't have a valid bracket.
436
+ if middle**2 > lower**2:
437
+ n += 1
438
+ lower, middle, upper = (
439
+ xmin + (xl0 - xmin)*factor**-n, lower, middle
440
+ )
441
+ else:
442
+ xmin = None
443
+ n = int(xp.ceil(xp.log(xl0 / d1) / xp.log(factor)))
444
+ lower = xl0 - d1*factor**n
445
+ middle = xl0 - d1*factor**(n-1) if n > 1 else xl0
446
+ upper = xl0 - d1*factor**(n-2) if n > 1 else xm0
447
+ # It may be the case the lower is below the minimum, but we still
448
+ # don't have a valid bracket.
449
+ if middle**2 > lower**2:
450
+ n += 1
451
+ lower, middle, upper = (
452
+ xl0 - d1*factor**n, lower, middle
453
+ )
454
+ f = self.init_f()
455
+
456
+ xmax = None
457
+ if other_side:
458
+ xl0, xm0, xr0 = -xr0, -xm0, -xl0
459
+ xmin, xmax = None, -xmin if xmin is not None else None
460
+ lower, middle, upper = -upper, -middle, -lower
461
+
462
+ kwargs = self.get_kwargs(
463
+ xl0=xl0, xr0=xr0, xmin=xmin, xmax=xmax, factor=factor, args=args
464
+ )
465
+ result = _bracket_minimum(f, xp.asarray(xm0), **kwargs)
466
+
467
+ # Check that `nfev` and `nit` have the correct relationship
468
+ assert result.nfev == result.nit + 3
469
+ # Check that `nfev` reports the correct number of function evaluations.
470
+ assert result.nfev == f.count
471
+ # Check that the number of iterations matches the theoretical value.
472
+ assert result.nit == n
473
+
474
+ # Compare reported bracket to theoretical bracket and reported function
475
+ # values to function evaluated at bracket.
476
+ xp_assert_close(result.xl, lower)
477
+ xp_assert_close(result.xm, middle)
478
+ xp_assert_close(result.xr, upper)
479
+ xp_assert_close(result.fl, f(lower, *args))
480
+ xp_assert_close(result.fm, f(middle, *args))
481
+ xp_assert_close(result.fr, f(upper, *args))
482
+
483
+ self.assert_valid_bracket(result, xp)
484
+ assert result.status == 0
485
+ assert result.success
486
+
487
+ def test_flags(self, xp):
488
+ # Test cases that should produce different status flags; show that all
489
+ # can be produced simultaneously
490
+ def f(xs, js):
491
+ funcs = [lambda x: (x - 1.5)**2,
492
+ lambda x: x,
493
+ lambda x: x,
494
+ lambda x: xp.asarray(xp.nan),
495
+ lambda x: x**2]
496
+
497
+ return [funcs[int(j)](x) for x, j in zip(xs, js)]
498
+
499
+ args = (xp.arange(5, dtype=xp.int64),)
500
+ xl0 = xp.asarray([-1.0, -1.0, -1.0, -1.0, 6.0])
501
+ xm0 = xp.asarray([0.0, 0.0, 0.0, 0.0, 4.0])
502
+ xr0 = xp.asarray([1.0, 1.0, 1.0, 1.0, 2.0])
503
+ xmin = xp.asarray([-xp.inf, -1.0, -xp.inf, -xp.inf, 8.0])
504
+
505
+ result = _bracket_minimum(f, xm0, xl0=xl0, xr0=xr0, xmin=xmin,
506
+ args=args, maxiter=3)
507
+
508
+ reference_flags = xp.asarray([eim._ECONVERGED, _ELIMITS,
509
+ eim._ECONVERR, eim._EVALUEERR,
510
+ eim._EINPUTERR], dtype=xp.int32)
511
+ xp_assert_equal(result.status, reference_flags)
512
+
513
+ @pytest.mark.parametrize("minimum", (0.622, [0.622, 0.623]))
514
+ @pytest.mark.parametrize("dtype", ("float16", "float32", "float64"))
515
+ @pytest.mark.parametrize("xmin", [-5, None])
516
+ @pytest.mark.parametrize("xmax", [5, None])
517
+ def test_dtypes(self, minimum, xmin, xmax, dtype, xp):
518
+ dtype = getattr(xp, dtype)
519
+ xmin = xmin if xmin is None else xp.asarray(xmin, dtype=dtype)
520
+ xmax = xmax if xmax is None else xp.asarray(xmax, dtype=dtype)
521
+ minimum = xp.asarray(minimum, dtype=dtype)
522
+
523
+ def f(x, minimum):
524
+ return xp.astype((x - minimum)**2, dtype)
525
+
526
+ xl0, xm0, xr0 = [-0.01, 0.0, 0.01]
527
+ result = _bracket_minimum(
528
+ f, xp.asarray(xm0, dtype=dtype), xl0=xp.asarray(xl0, dtype=dtype),
529
+ xr0=xp.asarray(xr0, dtype=dtype), xmin=xmin, xmax=xmax, args=(minimum, )
530
+ )
531
+ assert xp.all(result.success)
532
+ assert result.xl.dtype == result.xm.dtype == result.xr.dtype == dtype
533
+ assert result.fl.dtype == result.fm.dtype == result.fr.dtype == dtype
534
+
535
+ @pytest.mark.skip_xp_backends(np_only=True, reason="str/object arrays")
536
+ def test_input_validation(self, xp):
537
+ # Test input validation for appropriate error messages
538
+
539
+ message = '`func` must be callable.'
540
+ with pytest.raises(ValueError, match=message):
541
+ _bracket_minimum(None, -4, xl0=4)
542
+
543
+ message = '...must be numeric and real.'
544
+ with pytest.raises(ValueError, match=message):
545
+ _bracket_minimum(lambda x: x**2, xp.asarray(4+1j))
546
+ with pytest.raises(ValueError, match=message):
547
+ _bracket_minimum(lambda x: x**2, xp.asarray(-4), xl0=4+1j)
548
+ with pytest.raises(ValueError, match=message):
549
+ _bracket_minimum(lambda x: x**2, xp.asarray(-4), xr0=4+1j)
550
+ with pytest.raises(ValueError, match=message):
551
+ _bracket_minimum(lambda x: x**2, xp.asarray(-4), xmin=4+1j)
552
+ with pytest.raises(ValueError, match=message):
553
+ _bracket_minimum(lambda x: x**2, xp.asarray(-4), xmax=4+1j)
554
+ with pytest.raises(ValueError, match=message):
555
+ _bracket_minimum(lambda x: x**2, xp.asarray(-4), factor=4+1j)
556
+
557
+ message = "All elements of `factor` must be greater than 1."
558
+ with pytest.raises(ValueError, match=message):
559
+ _bracket_minimum(lambda x: x, xp.asarray(-4), factor=0.5)
560
+
561
+ message = "Array shapes are incompatible for broadcasting."
562
+ with pytest.raises(ValueError, match=message):
563
+ _bracket_minimum(lambda x: x**2, xp.asarray([-2, -3]), xl0=[-3, -4, -5])
564
+
565
+ message = '`maxiter` must be a non-negative integer.'
566
+ with pytest.raises(ValueError, match=message):
567
+ _bracket_minimum(lambda x: x**2, xp.asarray(-4), xr0=4, maxiter=1.5)
568
+ with pytest.raises(ValueError, match=message):
569
+ _bracket_minimum(lambda x: x**2, xp.asarray(-4), xr0=4, maxiter=-1)
570
+ with pytest.raises(ValueError, match=message):
571
+ _bracket_minimum(lambda x: x**2, xp.asarray(-4), xr0=4, maxiter="ekki")
572
+
573
+ @pytest.mark.parametrize("xl0", [0.0, None])
574
+ @pytest.mark.parametrize("xm0", (0.05, 0.1, 0.15))
575
+ @pytest.mark.parametrize("xr0", (0.2, 0.4, 0.6, None))
576
+ # Minimum is ``a`` for each tuple ``(a, b)`` below. Tests cases where minimum
577
+ # is within, or at varying distances to the left or right of the initial
578
+ # bracket.
579
+ @pytest.mark.parametrize(
580
+ "args",
581
+ (
582
+ (1.2, 0), (-0.5, 0), (0.1, 0), (0.2, 0), (3.6, 0), (21.4, 0),
583
+ (121.6, 0), (5764.1, 0), (-6.4, 0), (-12.9, 0), (-146.2, 0)
584
+ )
585
+ )
586
+ def test_scalar_no_limits(self, xl0, xm0, xr0, args, xp):
587
+ f = self.init_f()
588
+ kwargs = self.get_kwargs(xl0=xl0, xr0=xr0, args=tuple(map(xp.asarray, args)))
589
+ result = _bracket_minimum(f, xp.asarray(xm0, dtype=xp.float64), **kwargs)
590
+ self.assert_valid_bracket(result, xp)
591
+ assert result.status == 0
592
+ assert result.success
593
+ assert result.nfev == f.count
594
+
595
+ @pytest.mark.parametrize(
596
+ # xmin is set at 0.0 in all cases.
597
+ "xl0,xm0,xr0,xmin",
598
+ (
599
+ # Initial bracket at varying distances from the xmin.
600
+ (0.5, 0.75, 1.0, 0.0),
601
+ (1.0, 2.5, 4.0, 0.0),
602
+ (2.0, 4.0, 6.0, 0.0),
603
+ (12.0, 16.0, 20.0, 0.0),
604
+ # Test default initial left endpoint selection. It should not
605
+ # be below xmin.
606
+ (None, 0.75, 1.0, 0.0),
607
+ (None, 2.5, 4.0, 0.0),
608
+ (None, 4.0, 6.0, 0.0),
609
+ (None, 16.0, 20.0, 0.0),
610
+ )
611
+ )
612
+ @pytest.mark.parametrize(
613
+ "args", (
614
+ (0.0, 0.0), # Minimum is directly at xmin.
615
+ (1e-300, 0.0), # Minimum is extremely close to xmin.
616
+ (1e-20, 0.0), # Minimum is very close to xmin.
617
+ # Minimum at varying distances from xmin.
618
+ (0.1, 0.0),
619
+ (0.2, 0.0),
620
+ (0.4, 0.0)
621
+ )
622
+ )
623
+ def test_scalar_with_limit_left(self, xl0, xm0, xr0, xmin, args, xp):
624
+ f = self.init_f()
625
+ kwargs = self.get_kwargs(xl0=xl0, xr0=xr0, xmin=xmin,
626
+ args=tuple(map(xp.asarray, args)))
627
+ result = _bracket_minimum(f, xp.asarray(xm0), **kwargs)
628
+ self.assert_valid_bracket(result, xp)
629
+ assert result.status == 0
630
+ assert result.success
631
+ assert result.nfev == f.count
632
+
633
+ @pytest.mark.parametrize(
634
+ #xmax is set to 1.0 in all cases.
635
+ "xl0,xm0,xr0,xmax",
636
+ (
637
+ # Bracket at varying distances from xmax.
638
+ (0.2, 0.3, 0.4, 1.0),
639
+ (0.05, 0.075, 0.1, 1.0),
640
+ (-0.2, -0.1, 0.0, 1.0),
641
+ (-21.2, -17.7, -14.2, 1.0),
642
+ # Test default right endpoint selection. It should not exceed xmax.
643
+ (0.2, 0.3, None, 1.0),
644
+ (0.05, 0.075, None, 1.0),
645
+ (-0.2, -0.1, None, 1.0),
646
+ (-21.2, -17.7, None, 1.0),
647
+ )
648
+ )
649
+ @pytest.mark.parametrize(
650
+ "args", (
651
+ (0.9999999999999999, 0.0), # Minimum very close to xmax.
652
+ # Minimum at varying distances from xmax.
653
+ (0.9, 0.0),
654
+ (0.7, 0.0),
655
+ (0.5, 0.0)
656
+ )
657
+ )
658
+ def test_scalar_with_limit_right(self, xl0, xm0, xr0, xmax, args, xp):
659
+ f = self.init_f()
660
+ args = tuple(xp.asarray(arg, dtype=xp.float64) for arg in args)
661
+ kwargs = self.get_kwargs(xl0=xl0, xr0=xr0, xmax=xmax, args=args)
662
+ result = _bracket_minimum(f, xp.asarray(xm0, dtype=xp.float64), **kwargs)
663
+ self.assert_valid_bracket(result, xp)
664
+ assert result.status == 0
665
+ assert result.success
666
+ assert result.nfev == f.count
667
+
668
+ @pytest.mark.parametrize(
669
+ "xl0,xm0,xr0,xmin,xmax,args",
670
+ (
671
+ ( # Case 1:
672
+ # Initial bracket.
673
+ 0.2,
674
+ 0.3,
675
+ 0.4,
676
+ # Function slopes down to the right from the bracket to a minimum
677
+ # at 1.0. xmax is also at 1.0
678
+ None,
679
+ 1.0,
680
+ (1.0, 0.0)
681
+ ),
682
+ ( # Case 2:
683
+ # Initial bracket.
684
+ 1.4,
685
+ 1.95,
686
+ 2.5,
687
+ # Function slopes down to the left from the bracket to a minimum at
688
+ # 0.3 with xmin set to 0.3.
689
+ 0.3,
690
+ None,
691
+ (0.3, 0.0)
692
+ ),
693
+ (
694
+ # Case 3:
695
+ # Initial bracket.
696
+ 2.6,
697
+ 3.25,
698
+ 3.9,
699
+ # Function slopes down and to the right to a minimum at 99.4 with xmax
700
+ # at 99.4. Tests case where minimum is at xmax relatively further from
701
+ # the bracket.
702
+ None,
703
+ 99.4,
704
+ (99.4, 0)
705
+ ),
706
+ (
707
+ # Case 4:
708
+ # Initial bracket.
709
+ 4,
710
+ 4.5,
711
+ 5,
712
+ # Function slopes down and to the left away from the bracket with a
713
+ # minimum at -26.3 with xmin set to -26.3. Tests case where minimum is
714
+ # at xmin relatively far from the bracket.
715
+ -26.3,
716
+ None,
717
+ (-26.3, 0)
718
+ ),
719
+ (
720
+ # Case 5:
721
+ # Similar to Case 1 above, but tests default values of xl0 and xr0.
722
+ None,
723
+ 0.3,
724
+ None,
725
+ None,
726
+ 1.0,
727
+ (1.0, 0.0)
728
+ ),
729
+ ( # Case 6:
730
+ # Similar to Case 2 above, but tests default values of xl0 and xr0.
731
+ None,
732
+ 1.95,
733
+ None,
734
+ 0.3,
735
+ None,
736
+ (0.3, 0.0)
737
+ ),
738
+ (
739
+ # Case 7:
740
+ # Similar to Case 3 above, but tests default values of xl0 and xr0.
741
+ None,
742
+ 3.25,
743
+ None,
744
+ None,
745
+ 99.4,
746
+ (99.4, 0)
747
+ ),
748
+ (
749
+ # Case 8:
750
+ # Similar to Case 4 above, but tests default values of xl0 and xr0.
751
+ None,
752
+ 4.5,
753
+ None,
754
+ -26.3,
755
+ None,
756
+ (-26.3, 0)
757
+ ),
758
+ )
759
+ )
760
+ def test_minimum_at_boundary_point(self, xl0, xm0, xr0, xmin, xmax, args, xp):
761
+ f = self.init_f()
762
+ kwargs = self.get_kwargs(xr0=xr0, xmin=xmin, xmax=xmax,
763
+ args=tuple(map(xp.asarray, args)))
764
+ result = _bracket_minimum(f, xp.asarray(xm0), **kwargs)
765
+ assert result.status == -1
766
+ assert args[0] in (result.xl, result.xr)
767
+ assert result.nfev == f.count
768
+
769
+ @pytest.mark.parametrize('shape', [tuple(), (12, ), (3, 4), (3, 2, 2)])
770
+ def test_vectorization(self, shape, xp):
771
+ # Test for correct functionality, output shapes, and dtypes for
772
+ # various input shapes.
773
+ a = np.linspace(-0.05, 1.05, 12).reshape(shape) if shape else 0.6
774
+ args = (a, 0.)
775
+ maxiter = 10
776
+
777
+ @np.vectorize
778
+ def bracket_minimum_single(xm0, xl0, xr0, xmin, xmax, factor, a):
779
+ return _bracket_minimum(self.init_f(), xm0, xl0=xl0, xr0=xr0, xmin=xmin,
780
+ xmax=xmax, factor=factor, maxiter=maxiter,
781
+ args=(a, 0.0))
782
+
783
+ f = self.init_f()
784
+
785
+ rng = np.random.default_rng(2348234)
786
+ xl0 = -rng.random(size=shape)
787
+ xr0 = rng.random(size=shape)
788
+ xm0 = xl0 + rng.random(size=shape) * (xr0 - xl0)
789
+ xmin, xmax = 1e3*xl0, 1e3*xr0
790
+ if shape: # make some elements un
791
+ i = rng.random(size=shape) > 0.5
792
+ xmin[i], xmax[i] = -np.inf, np.inf
793
+ factor = rng.random(size=shape) + 1.5
794
+ refs = bracket_minimum_single(xm0, xl0, xr0, xmin, xmax, factor, a).ravel()
795
+ args = tuple(xp.asarray(arg, dtype=xp.float64) for arg in args)
796
+ res = _bracket_minimum(f, xp.asarray(xm0), xl0=xp.asarray(xl0),
797
+ xr0=xp.asarray(xr0), xmin=xp.asarray(xmin),
798
+ xmax=xp.asarray(xmax), factor=xp.asarray(factor),
799
+ args=args, maxiter=maxiter)
800
+
801
+ attrs = ['xl', 'xm', 'xr', 'fl', 'fm', 'fr', 'success', 'nfev', 'nit']
802
+ for attr in attrs:
803
+ ref_attr = [xp.asarray(getattr(ref, attr)) for ref in refs]
804
+ res_attr = getattr(res, attr)
805
+ xp_assert_close(xp_ravel(res_attr, xp=xp), xp.stack(ref_attr))
806
+ assert res_attr.shape == shape
807
+
808
+ assert res.success.dtype == xp.bool
809
+ if shape:
810
+ assert xp.all(res.success[1:-1])
811
+ assert res.status.dtype == xp.int32
812
+ assert res.nfev.dtype == xp.int32
813
+ assert res.nit.dtype == xp.int32
814
+ assert xp.max(res.nit) == f.count - 3
815
+ self.assert_valid_bracket(res, xp)
816
+ xp_assert_close(res.fl, f(res.xl, *args))
817
+ xp_assert_close(res.fm, f(res.xm, *args))
818
+ xp_assert_close(res.fr, f(res.xr, *args))
819
+
820
+ def test_special_cases(self, xp):
821
+ # Test edge cases and other special cases.
822
+
823
+ # Test that integers are not passed to `f`
824
+ # (otherwise this would overflow)
825
+ def f(x):
826
+ assert xp.isdtype(x.dtype, "numeric")
827
+ return x ** 98 - 1
828
+
829
+ result = _bracket_minimum(f, xp.asarray(-7., dtype=xp.float64), xr0=5)
830
+ assert result.success
831
+
832
+ # Test maxiter = 0. Should do nothing to bracket.
833
+ def f(x):
834
+ return x**2 - 10
835
+
836
+ xl0, xm0, xr0 = xp.asarray(-3.), xp.asarray(-1.), xp.asarray(2.)
837
+ result = _bracket_minimum(f, xm0, xl0=xl0, xr0=xr0, maxiter=0)
838
+ xp_assert_equal(result.xl, xl0)
839
+ xp_assert_equal(result.xm, xm0)
840
+ xp_assert_equal(result.xr, xr0)
841
+
842
+ # Test scalar `args` (not in tuple)
843
+ def f(x, c):
844
+ return c*x**2 - 1
845
+
846
+ result = _bracket_minimum(f, xp.asarray(-1.), args=xp.asarray(3.))
847
+ assert result.success
848
+ xp_assert_close(result.fl, f(result.xl, 3))
849
+
850
+ # Initial bracket is valid.
851
+ f = self.init_f()
852
+ xl0, xm0, xr0 = xp.asarray(-1.0), xp.asarray(-0.2), xp.asarray(1.0)
853
+ args = (xp.asarray(0.), xp.asarray(0.))
854
+ result = _bracket_minimum(f, xm0, xl0=xl0, xr0=xr0, args=args)
855
+ assert f.count == 3
856
+
857
+ xp_assert_equal(result.xl, xl0)
858
+ xp_assert_equal(result.xm , xm0)
859
+ xp_assert_equal(result.xr, xr0)
860
+ xp_assert_equal(result.fl, f(xl0, *args))
861
+ xp_assert_equal(result.fm, f(xm0, *args))
862
+ xp_assert_equal(result.fr, f(xr0, *args))
863
+
864
+ def test_gh_20562_left(self, xp):
865
+ # Regression test for https://github.com/scipy/scipy/issues/20562
866
+ # minimum of f in [xmin, xmax] is at xmin.
867
+ xmin, xmax = xp.asarray(0.21933608), xp.asarray(1.39713606)
868
+
869
+ def f(x):
870
+ log_a, log_b = xp.log(xmin), xp.log(xmax)
871
+ return -((log_b - log_a)*x)**-1
872
+
873
+ result = _bracket_minimum(f, xp.asarray(0.5535723499480897), xmin=xmin,
874
+ xmax=xmax)
875
+ xp_assert_close(result.xl, xmin)
876
+
877
+ def test_gh_20562_right(self, xp):
878
+ # Regression test for https://github.com/scipy/scipy/issues/20562
879
+ # minimum of f in [xmin, xmax] is at xmax.
880
+ xmin, xmax = xp.asarray(-1.39713606), xp.asarray(-0.21933608)
881
+
882
+ def f(x):
883
+ log_a, log_b = xp.log(-xmax), xp.log(-xmin)
884
+ return ((log_b - log_a)*x)**-1
885
+
886
+ result = _bracket_minimum(f, xp.asarray(-0.5535723499480897),
887
+ xmin=xmin, xmax=xmax)
888
+ xp_assert_close(result.xr, xmax)
loan-advisor-agent/.venv/Lib/site-packages/scipy/optimize/tests/test_chandrupatla.py ADDED
@@ -0,0 +1,976 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import math
2
+ import pytest
3
+ import numpy as np
4
+ from copy import deepcopy
5
+
6
+ from scipy import stats, special
7
+ import scipy._lib._elementwise_iterative_method as eim
8
+ import scipy._lib.array_api_extra as xpx
9
+ from scipy._lib._array_api import (array_namespace, is_cupy, is_numpy, xp_ravel,
10
+ xp_size, make_xp_test_case)
11
+ from scipy._lib._array_api_no_0d import (xp_assert_close, xp_assert_equal,
12
+ xp_assert_less)
13
+
14
+ from scipy.optimize.elementwise import find_minimum, find_root
15
+ from scipy.optimize._tstutils import _CHANDRUPATLA_TESTS
16
+
17
+ from itertools import permutations
18
+
19
+
20
+ def _vectorize(xp):
21
+ # xp-compatible version of np.vectorize
22
+ # assumes arguments are all arrays of the same shape
23
+ def decorator(f):
24
+ def wrapped(*arg_arrays):
25
+ shape = arg_arrays[0].shape
26
+ arg_arrays = [xp_ravel(arg_array, xp=xp) for arg_array in arg_arrays]
27
+ res = []
28
+ for i in range(math.prod(shape)):
29
+ arg_scalars = [arg_array[i] for arg_array in arg_arrays]
30
+ res.append(f(*arg_scalars))
31
+ return res
32
+
33
+ return wrapped
34
+
35
+ return decorator
36
+
37
+
38
+ # These tests were originally written for the private `optimize._chandrupatla`
39
+ # interfaces, but now we want the tests to check the behavior of the public
40
+ # `optimize.elementwise` interfaces. Therefore, rather than importing
41
+ # `_chandrupatla`/`_chandrupatla_minimize` from `_chandrupatla.py`, we import
42
+ # `find_root`/`find_minimum` from `optimize.elementwise` and wrap those
43
+ # functions to conform to the private interface. This may look a little strange,
44
+ # since it effectively just inverts the interface transformation done within the
45
+ # `find_root`/`find_minimum` functions, but it allows us to run the original,
46
+ # unmodified tests on the public interfaces, simplifying the PR that adds
47
+ # the public interfaces. We'll refactor this when we want to @parametrize the
48
+ # tests over multiple `method`s.
49
+ def _wrap_chandrupatla(func):
50
+ def _chandrupatla_wrapper(f, *bracket, **kwargs):
51
+ # avoid passing arguments to `find_minimum` to this function
52
+ tol_keys = {'xatol', 'xrtol', 'fatol', 'frtol'}
53
+ tolerances = {key: kwargs.pop(key) for key in tol_keys if key in kwargs}
54
+ _callback = kwargs.pop('callback', None)
55
+ if callable(_callback):
56
+ def callback(res):
57
+ if func == find_root:
58
+ res.xl, res.xr = res.bracket
59
+ res.fl, res.fr = res.f_bracket
60
+ else:
61
+ res.xl, res.xm, res.xr = res.bracket
62
+ res.fl, res.fm, res.fr = res.f_bracket
63
+ res.fun = res.f_x
64
+ del res.bracket
65
+ del res.f_bracket
66
+ del res.f_x
67
+ return _callback(res)
68
+ else:
69
+ callback = _callback
70
+
71
+ res = func(f, bracket, tolerances=tolerances, callback=callback, **kwargs)
72
+ if func == find_root:
73
+ res.xl, res.xr = res.bracket
74
+ res.fl, res.fr = res.f_bracket
75
+ else:
76
+ res.xl, res.xm, res.xr = res.bracket
77
+ res.fl, res.fm, res.fr = res.f_bracket
78
+ res.fun = res.f_x
79
+ del res.bracket
80
+ del res.f_bracket
81
+ del res.f_x
82
+ return res
83
+ return _chandrupatla_wrapper
84
+
85
+
86
+ _chandrupatla_minimize = _wrap_chandrupatla(find_minimum)
87
+
88
+
89
+ def f1(x):
90
+ return 100*(1 - x**3.)**2 + (1-x**2.) + 2*(1-x)**2.
91
+
92
+
93
+ def f2(x):
94
+ return 5 + (x - 2.)**6
95
+
96
+
97
+ def f3(x):
98
+ xp = array_namespace(x)
99
+ return xp.exp(x) - 5*x
100
+
101
+
102
+ def f4(x):
103
+ return x**5. - 5*x**3. - 20.*x + 5.
104
+
105
+
106
+ def f5(x):
107
+ return 8*x**3 - 2*x**2 - 7*x + 3
108
+
109
+
110
+ def _bracket_minimum(func, x1, x2):
111
+ phi = 1.61803398875
112
+ maxiter = 100
113
+ f1 = func(x1)
114
+ f2 = func(x2)
115
+ step = x2 - x1
116
+ x1, x2, f1, f2, step = ((x2, x1, f2, f1, -step) if f2 > f1
117
+ else (x1, x2, f1, f2, step))
118
+
119
+ for i in range(maxiter):
120
+ step *= phi
121
+ x3 = x2 + step
122
+ f3 = func(x3)
123
+ if f3 < f2:
124
+ x1, x2, f1, f2 = x2, x3, f2, f3
125
+ else:
126
+ break
127
+ return x1, x2, x3, f1, f2, f3
128
+
129
+
130
+ cases = [
131
+ (f1, -1, 11),
132
+ (f1, -2, 13),
133
+ (f1, -4, 13),
134
+ (f1, -8, 15),
135
+ (f1, -16, 16),
136
+ (f1, -32, 19),
137
+ (f1, -64, 20),
138
+ (f1, -128, 21),
139
+ (f1, -256, 21),
140
+ (f1, -512, 19),
141
+ (f1, -1024, 24),
142
+ (f2, -1, 8),
143
+ (f2, -2, 6),
144
+ (f2, -4, 6),
145
+ (f2, -8, 7),
146
+ (f2, -16, 8),
147
+ (f2, -32, 8),
148
+ (f2, -64, 9),
149
+ (f2, -128, 11),
150
+ (f2, -256, 13),
151
+ (f2, -512, 12),
152
+ (f2, -1024, 13),
153
+ (f3, -1, 11),
154
+ (f3, -2, 11),
155
+ (f3, -4, 11),
156
+ (f3, -8, 10),
157
+ (f3, -16, 14),
158
+ (f3, -32, 12),
159
+ (f3, -64, 15),
160
+ (f3, -128, 18),
161
+ (f3, -256, 18),
162
+ (f3, -512, 19),
163
+ (f3, -1024, 19),
164
+ (f4, -0.05, 9),
165
+ (f4, -0.10, 11),
166
+ (f4, -0.15, 11),
167
+ (f4, -0.20, 11),
168
+ (f4, -0.25, 11),
169
+ (f4, -0.30, 9),
170
+ (f4, -0.35, 9),
171
+ (f4, -0.40, 9),
172
+ (f4, -0.45, 10),
173
+ (f4, -0.50, 10),
174
+ (f4, -0.55, 10),
175
+ (f5, -0.05, 6),
176
+ (f5, -0.10, 7),
177
+ (f5, -0.15, 8),
178
+ (f5, -0.20, 10),
179
+ (f5, -0.25, 9),
180
+ (f5, -0.30, 8),
181
+ (f5, -0.35, 7),
182
+ (f5, -0.40, 7),
183
+ (f5, -0.45, 9),
184
+ (f5, -0.50, 9),
185
+ (f5, -0.55, 8)
186
+ ]
187
+
188
+
189
+ @make_xp_test_case(find_minimum)
190
+ class TestChandrupatlaMinimize:
191
+
192
+ def f(self, x, loc):
193
+ xp = array_namespace(x, loc)
194
+ res = -xp.exp(-1/2 * (x-loc)**2) / (2*xp.pi)**0.5
195
+ return xp.asarray(res, dtype=x.dtype)[()]
196
+
197
+ @pytest.mark.parametrize('dtype', ('float32', 'float64'))
198
+ @pytest.mark.parametrize('loc', [0.6, np.linspace(-1.05, 1.05, 10)])
199
+ def test_basic(self, loc, xp, dtype):
200
+ # Find mode of normal distribution. Compare mode against location
201
+ # parameter and value of pdf at mode against expected pdf.
202
+ rtol = {'float32': 5e-3, 'float64': 5e-7}[dtype]
203
+ dtype = getattr(xp, dtype)
204
+ bracket = (xp.asarray(xi, dtype=dtype) for xi in (-5, 0, 5))
205
+ loc = xp.asarray(loc, dtype=dtype)
206
+ fun = xp.broadcast_to(xp.asarray(-stats.norm.pdf(0), dtype=dtype), loc.shape)
207
+
208
+ res = _chandrupatla_minimize(self.f, *bracket, args=(loc,))
209
+ xp_assert_close(res.x, loc, rtol=rtol)
210
+ xp_assert_equal(res.fun, fun)
211
+
212
+ @pytest.mark.parametrize('shape', [tuple(), (12,), (3, 4), (3, 2, 2)])
213
+ def test_vectorization(self, shape, xp):
214
+ # Test for correct functionality, output shapes, and dtypes for various
215
+ # input shapes.
216
+ loc = xp.linspace(-0.05, 1.05, 12).reshape(shape) if shape else xp.asarray(0.6)
217
+ args = (loc,)
218
+ bracket = xp.asarray(-5.), xp.asarray(0.), xp.asarray(5.)
219
+
220
+ @_vectorize(xp)
221
+ def chandrupatla_single(loc_single):
222
+ return _chandrupatla_minimize(self.f, *bracket, args=(loc_single,))
223
+
224
+ def f(*args, **kwargs):
225
+ f.f_evals += 1
226
+ return self.f(*args, **kwargs)
227
+ f.f_evals = 0
228
+
229
+ res = _chandrupatla_minimize(f, *bracket, args=args)
230
+ refs = chandrupatla_single(loc)
231
+
232
+ attrs = ['x', 'fun', 'success', 'status', 'nfev', 'nit',
233
+ 'xl', 'xm', 'xr', 'fl', 'fm', 'fr']
234
+ for attr in attrs:
235
+ ref_attr = xp.stack([getattr(ref, attr) for ref in refs])
236
+ res_attr = xp_ravel(getattr(res, attr))
237
+ xp_assert_equal(res_attr, ref_attr)
238
+ assert getattr(res, attr).shape == shape
239
+
240
+ xp_assert_equal(res.fun, self.f(res.x, *args))
241
+ xp_assert_equal(res.fl, self.f(res.xl, *args))
242
+ xp_assert_equal(res.fm, self.f(res.xm, *args))
243
+ xp_assert_equal(res.fr, self.f(res.xr, *args))
244
+ assert xp.max(res.nfev) == f.f_evals
245
+ assert xp.max(res.nit) == f.f_evals - 3
246
+
247
+ assert xp.isdtype(res.success.dtype, 'bool')
248
+ assert xp.isdtype(res.status.dtype, 'integral')
249
+ assert xp.isdtype(res.nfev.dtype, 'integral')
250
+ assert xp.isdtype(res.nit.dtype, 'integral')
251
+
252
+
253
+ def test_flags(self, xp):
254
+ # Test cases that should produce different status flags; show that all
255
+ # can be produced simultaneously.
256
+ def f(xs, js):
257
+ funcs = [lambda x: (x - 2.5) ** 2,
258
+ lambda x: x - 10,
259
+ lambda x: (x - 2.5) ** 4,
260
+ lambda x: xp.full_like(x, xp.asarray(xp.nan))]
261
+ res = []
262
+ for i in range(xp_size(js)):
263
+ x = xs[i, ...]
264
+ j = int(xp_ravel(js)[i])
265
+ res.append(funcs[j](x))
266
+ return xp.stack(res)
267
+
268
+ args = (xp.arange(4, dtype=xp.int64),)
269
+ bracket = (xp.asarray([0]*4, dtype=xp.float64),
270
+ xp.asarray([2]*4, dtype=xp.float64),
271
+ xp.asarray([np.pi]*4, dtype=xp.float64))
272
+ res = _chandrupatla_minimize(f, *bracket, args=args, maxiter=10)
273
+
274
+ ref_flags = xp.asarray([eim._ECONVERGED, eim._ESIGNERR, eim._ECONVERR,
275
+ eim._EVALUEERR], dtype=xp.int32)
276
+ xp_assert_equal(res.status, ref_flags)
277
+
278
+ def test_convergence(self, xp):
279
+ # Test that the convergence tolerances behave as expected
280
+ rng = np.random.default_rng(2585255913088665241)
281
+ p = xp.asarray(rng.random(size=3))
282
+ bracket = (xp.asarray(-5, dtype=xp.float64), xp.asarray(0), xp.asarray(5))
283
+ args = (p,)
284
+ kwargs0 = dict(args=args, xatol=0, xrtol=0, fatol=0, frtol=0)
285
+
286
+ kwargs = kwargs0.copy()
287
+ kwargs['xatol'] = 1e-3
288
+ res1 = _chandrupatla_minimize(self.f, *bracket, **kwargs)
289
+ j1 = xp.abs(res1.xr - res1.xl)
290
+ tol = xp.asarray(4*kwargs['xatol'], dtype=p.dtype)
291
+ xp_assert_less(j1, xp.full((3,), tol, dtype=p.dtype))
292
+ kwargs['xatol'] = 1e-6
293
+ res2 = _chandrupatla_minimize(self.f, *bracket, **kwargs)
294
+ j2 = xp.abs(res2.xr - res2.xl)
295
+ tol = xp.asarray(4*kwargs['xatol'], dtype=p.dtype)
296
+ xp_assert_less(j2, xp.full((3,), tol, dtype=p.dtype))
297
+ xp_assert_less(j2, j1)
298
+
299
+ kwargs = kwargs0.copy()
300
+ kwargs['xrtol'] = 1e-3
301
+ res1 = _chandrupatla_minimize(self.f, *bracket, **kwargs)
302
+ j1 = xp.abs(res1.xr - res1.xl)
303
+ tol = xp.asarray(4*kwargs['xrtol']*xp.abs(res1.x), dtype=p.dtype)
304
+ xp_assert_less(j1, tol)
305
+ kwargs['xrtol'] = 1e-6
306
+ res2 = _chandrupatla_minimize(self.f, *bracket, **kwargs)
307
+ j2 = xp.abs(res2.xr - res2.xl)
308
+ tol = xp.asarray(4*kwargs['xrtol']*xp.abs(res2.x), dtype=p.dtype)
309
+ xp_assert_less(j2, tol)
310
+ xp_assert_less(j2, j1)
311
+
312
+ kwargs = kwargs0.copy()
313
+ kwargs['fatol'] = 1e-3
314
+ res1 = _chandrupatla_minimize(self.f, *bracket, **kwargs)
315
+ h1 = xp.abs(res1.fl - 2 * res1.fm + res1.fr)
316
+ tol = xp.asarray(2*kwargs['fatol'], dtype=p.dtype)
317
+ xp_assert_less(h1, xp.full((3,), tol, dtype=p.dtype))
318
+ kwargs['fatol'] = 1e-6
319
+ res2 = _chandrupatla_minimize(self.f, *bracket, **kwargs)
320
+ h2 = xp.abs(res2.fl - 2 * res2.fm + res2.fr)
321
+ tol = xp.asarray(2*kwargs['fatol'], dtype=p.dtype)
322
+ xp_assert_less(h2, xp.full((3,), tol, dtype=p.dtype))
323
+ xp_assert_less(h2, h1)
324
+
325
+ kwargs = kwargs0.copy()
326
+ kwargs['frtol'] = 1e-3
327
+ res1 = _chandrupatla_minimize(self.f, *bracket, **kwargs)
328
+ h1 = xp.abs(res1.fl - 2 * res1.fm + res1.fr)
329
+ tol = xp.asarray(2*kwargs['frtol']*xp.abs(res1.fun), dtype=p.dtype)
330
+ xp_assert_less(h1, tol)
331
+ kwargs['frtol'] = 1e-6
332
+ res2 = _chandrupatla_minimize(self.f, *bracket, **kwargs)
333
+ h2 = xp.abs(res2.fl - 2 * res2.fm + res2.fr)
334
+ tol = xp.asarray(2*kwargs['frtol']*abs(res2.fun), dtype=p.dtype)
335
+ xp_assert_less(h2, tol)
336
+ xp_assert_less(h2, h1)
337
+
338
+ def test_maxiter_callback(self, xp):
339
+ # Test behavior of `maxiter` parameter and `callback` interface
340
+ loc = xp.asarray(0.612814)
341
+ bracket = (xp.asarray(-5), xp.asarray(0), xp.asarray(5))
342
+ maxiter = 5
343
+
344
+ res = _chandrupatla_minimize(self.f, *bracket, args=(loc,),
345
+ maxiter=maxiter)
346
+ assert not xp.any(res.success)
347
+ assert xp.all(res.nfev == maxiter+3)
348
+ assert xp.all(res.nit == maxiter)
349
+
350
+ def callback(res):
351
+ callback.iter += 1
352
+ callback.res = res
353
+ assert hasattr(res, 'x')
354
+ if callback.iter == 0:
355
+ # callback is called once with initial bracket
356
+ assert (res.xl, res.xm, res.xr) == bracket
357
+ else:
358
+ changed_xr = (res.xl == callback.xl) & (res.xr != callback.xr)
359
+ changed_xl = (res.xl != callback.xl) & (res.xr == callback.xr)
360
+ assert xp.all(changed_xr | changed_xl)
361
+
362
+ callback.xl = res.xl
363
+ callback.xr = res.xr
364
+ assert res.status == eim._EINPROGRESS
365
+ xp_assert_equal(self.f(res.xl, loc), res.fl)
366
+ xp_assert_equal(self.f(res.xm, loc), res.fm)
367
+ xp_assert_equal(self.f(res.xr, loc), res.fr)
368
+ xp_assert_equal(self.f(res.x, loc), res.fun)
369
+ if callback.iter == maxiter:
370
+ raise StopIteration
371
+
372
+ callback.xl = xp.nan
373
+ callback.xr = xp.nan
374
+ callback.iter = -1 # callback called once before first iteration
375
+ callback.res = None
376
+
377
+ res2 = _chandrupatla_minimize(self.f, *bracket, args=(loc,),
378
+ callback=callback)
379
+
380
+ # terminating with callback is identical to terminating due to maxiter
381
+ # (except for `status`)
382
+ for key in res.keys():
383
+ if key == 'status':
384
+ assert res[key] == eim._ECONVERR
385
+ # assert callback.res[key] == eim._EINPROGRESS
386
+ assert res2[key] == eim._ECALLBACK
387
+ else:
388
+ assert res2[key] == callback.res[key] == res[key]
389
+
390
+ @pytest.mark.parametrize('case', cases)
391
+ def test_nit_expected(self, case, xp):
392
+ # Test that `_chandrupatla` implements Chandrupatla's algorithm:
393
+ # in all 55 test cases, the number of iterations performed
394
+ # matches the number reported in the original paper.
395
+ func, x1, nit = case
396
+
397
+ # Find bracket using the algorithm in the paper
398
+ step = 0.2
399
+ x2 = x1 + step
400
+ x1, x2, x3, f1, f2, f3 = _bracket_minimum(func, x1, x2)
401
+
402
+ # Use tolerances from original paper
403
+ xatol = 0.0001
404
+ fatol = 0.000001
405
+ xrtol = 1e-16
406
+ frtol = 1e-16
407
+
408
+ bracket = xp.asarray(x1), xp.asarray(x2), xp.asarray(x3, dtype=xp.float64)
409
+ res = _chandrupatla_minimize(func, *bracket, xatol=xatol,
410
+ fatol=fatol, xrtol=xrtol, frtol=frtol)
411
+ xp_assert_equal(res.nit, xp.asarray(nit, dtype=xp.int32))
412
+
413
+ @pytest.mark.parametrize("loc", (0.65, [0.65, 0.7]))
414
+ @pytest.mark.parametrize("dtype", ('float16', 'float32', 'float64'))
415
+ def test_dtype(self, loc, dtype, xp):
416
+ # Test that dtypes are preserved
417
+ dtype = getattr(xp, dtype)
418
+
419
+ loc = xp.asarray(loc, dtype=dtype)
420
+ bracket = (xp.asarray(-3, dtype=dtype),
421
+ xp.asarray(1, dtype=dtype),
422
+ xp.asarray(5, dtype=dtype))
423
+
424
+ def f(x, loc):
425
+ assert x.dtype == dtype
426
+ return xp.astype((x - loc)**2, dtype)
427
+
428
+ res = _chandrupatla_minimize(f, *bracket, args=(loc,))
429
+ assert res.x.dtype == dtype
430
+ xp_assert_close(res.x, loc, rtol=math.sqrt(xp.finfo(dtype).eps))
431
+
432
+ def test_input_validation(self, xp):
433
+ # Test input validation for appropriate error messages
434
+
435
+ message = '`func` must be callable.'
436
+ bracket = xp.asarray(-4), xp.asarray(0), xp.asarray(4)
437
+ with pytest.raises(ValueError, match=message):
438
+ _chandrupatla_minimize(None, *bracket)
439
+
440
+ message = 'Abscissae and function output must be real numbers.'
441
+ bracket = xp.asarray(-4 + 1j), xp.asarray(0), xp.asarray(4)
442
+ with pytest.raises(ValueError, match=message):
443
+ _chandrupatla_minimize(lambda x: x, *bracket)
444
+
445
+ message = "...be broadcast..."
446
+ bracket = xp.asarray([-2, -3]), xp.asarray([0, 0]), xp.asarray([3, 4, 5])
447
+ # raised by `np.broadcast, but the traceback is readable IMO
448
+ with pytest.raises((ValueError, RuntimeError), match=message):
449
+ _chandrupatla_minimize(lambda x: x, *bracket)
450
+
451
+ message = "The shape of the array returned by `func` must be the same"
452
+ bracket = xp.asarray([-3, -3]), xp.asarray([0, 0]), xp.asarray([5, 5])
453
+ with pytest.raises(ValueError, match=message):
454
+ _chandrupatla_minimize(lambda x: [x[0, ...], x[1, ...], x[1, ...]],
455
+ *bracket)
456
+
457
+ message = 'Tolerances must be non-negative scalars.'
458
+ bracket = xp.asarray(-4), xp.asarray(0), xp.asarray(4)
459
+ with pytest.raises(ValueError, match=message):
460
+ _chandrupatla_minimize(lambda x: x, *bracket, xatol=-1)
461
+ with pytest.raises(ValueError, match=message):
462
+ _chandrupatla_minimize(lambda x: x, *bracket, xrtol=xp.nan)
463
+ with pytest.raises(ValueError, match=message):
464
+ _chandrupatla_minimize(lambda x: x, *bracket, fatol='ekki')
465
+ with pytest.raises(ValueError, match=message):
466
+ _chandrupatla_minimize(lambda x: x, *bracket, frtol=xp.nan)
467
+
468
+ message = '`maxiter` must be a non-negative integer.'
469
+ with pytest.raises(ValueError, match=message):
470
+ _chandrupatla_minimize(lambda x: x, *bracket, maxiter=1.5)
471
+ with pytest.raises(ValueError, match=message):
472
+ _chandrupatla_minimize(lambda x: x, *bracket, maxiter=-1)
473
+
474
+ message = '`callback` must be callable.'
475
+ with pytest.raises(ValueError, match=message):
476
+ _chandrupatla_minimize(lambda x: x, *bracket, callback='shrubbery')
477
+
478
+ def test_bracket_order(self, xp):
479
+ # Confirm that order of points in bracket doesn't
480
+ loc = xp.linspace(-1, 1, 6)[:, xp.newaxis]
481
+ brackets = xp.asarray(list(permutations([-5, 0, 5]))).T
482
+ res = _chandrupatla_minimize(self.f, *brackets, args=(loc,))
483
+ assert xp.all(xpx.isclose(res.x, loc) | (res.fun == self.f(loc, loc)))
484
+ ref = res.x[:, 0] # all columns should be the same
485
+ xp_assert_close(*xp.broadcast_arrays(res.x.T, ref), rtol=1e-15)
486
+
487
+ def test_special_cases(self, xp):
488
+ # Test edge cases and other special cases
489
+
490
+ # Test that integers are not passed to `f`
491
+ def f(x):
492
+ assert xp.isdtype(x.dtype, "real floating")
493
+ return (x - 1)**2
494
+
495
+ bracket = xp.asarray(-7), xp.asarray(0), xp.asarray(8)
496
+ with np.errstate(invalid='ignore'):
497
+ res = _chandrupatla_minimize(f, *bracket, fatol=0, frtol=0)
498
+ assert res.success
499
+ xp_assert_close(res.x, xp.asarray(1.), rtol=1e-3)
500
+ xp_assert_close(res.fun, xp.asarray(0.), atol=1e-200)
501
+
502
+ # Test that if all elements of bracket equal minimizer, algorithm
503
+ # reports convergence
504
+ def f(x):
505
+ return (x-1)**2
506
+
507
+ bracket = xp.asarray(1), xp.asarray(1), xp.asarray(1)
508
+ res = _chandrupatla_minimize(f, *bracket)
509
+ assert res.success
510
+ xp_assert_equal(res.x, xp.asarray(1.))
511
+
512
+ # Test maxiter = 0. Should do nothing to bracket.
513
+ def f(x):
514
+ return (x-1)**2
515
+
516
+ bracket = xp.asarray(-3), xp.asarray(1.1), xp.asarray(5)
517
+ res = _chandrupatla_minimize(f, *bracket, maxiter=0)
518
+ assert res.xl, res.xr == bracket
519
+ assert res.nit == 0
520
+ assert res.nfev == 3
521
+ assert res.status == -2
522
+ assert res.x == 1.1 # best so far
523
+
524
+ # Test scalar `args` (not in tuple)
525
+ def f(x, c):
526
+ return (x-c)**2 - 1
527
+
528
+ bracket = xp.asarray(-1), xp.asarray(0), xp.asarray(1)
529
+ c = xp.asarray(1/3)
530
+ res = _chandrupatla_minimize(f, *bracket, args=(c,))
531
+ xp_assert_close(res.x, c)
532
+
533
+ # Test zero tolerances
534
+ def f(x):
535
+ return -xp.sin(x)
536
+
537
+ bracket = xp.asarray(0), xp.asarray(1), xp.asarray(xp.pi)
538
+ res = _chandrupatla_minimize(f, *bracket, xatol=0, xrtol=0, fatol=0, frtol=0)
539
+ assert res.success
540
+ # found a minimum exactly (according to floating point arithmetic)
541
+ assert res.xl < res.xm < res.xr
542
+ assert f(res.xl) == f(res.xm) == f(res.xr)
543
+
544
+
545
+ @make_xp_test_case(find_root)
546
+ class TestFindRoot:
547
+
548
+ def f(self, q, p):
549
+ return special.ndtr(q) - p
550
+
551
+ @pytest.mark.parametrize('p', [0.6, np.linspace(-0.05, 1.05, 10)])
552
+ def test_basic(self, p, xp):
553
+ # Invert distribution CDF and compare against distribution `ppf`
554
+ a, b = xp.asarray(-5.), xp.asarray(5.)
555
+ res = find_root(self.f, (a, b), args=(xp.asarray(p),))
556
+ ref = xp.asarray(stats.norm().ppf(p), dtype=xp.asarray(p).dtype)
557
+ xp_assert_close(res.x, ref)
558
+
559
+ @pytest.mark.parametrize('shape', [tuple(), (12,), (3, 4), (3, 2, 2)])
560
+ def test_vectorization(self, shape, xp):
561
+ # Test for correct functionality, output shapes, and dtypes for various
562
+ # input shapes.
563
+ p = (np.linspace(-0.05, 1.05, 12).reshape(shape) if shape
564
+ else np.float64(0.6))
565
+ p_xp = xp.asarray(p)
566
+ args_xp = (p_xp,)
567
+ dtype = p_xp.dtype
568
+
569
+ @np.vectorize
570
+ def find_root_single(p):
571
+ return find_root(self.f, (-5, 5), args=(p,))
572
+
573
+ def f(*args, **kwargs):
574
+ f.f_evals += 1
575
+ return self.f(*args, **kwargs)
576
+ f.f_evals = 0
577
+
578
+ bracket = xp.asarray(-5., dtype=xp.float64), xp.asarray(5., dtype=xp.float64)
579
+ res = find_root(f, bracket, args=args_xp)
580
+ refs = find_root_single(p).ravel()
581
+
582
+ ref_x = [ref.x for ref in refs]
583
+ ref_x = xp.reshape(xp.asarray(ref_x, dtype=dtype), shape)
584
+ xp_assert_close(res.x, ref_x)
585
+
586
+ ref_f = [ref.f_x for ref in refs]
587
+ ref_f = xp.reshape(xp.asarray(ref_f, dtype=dtype), shape)
588
+ xp_assert_close(res.f_x, ref_f, atol=1e-15)
589
+ xp_assert_equal(res.f_x, self.f(res.x, *args_xp))
590
+
591
+ ref_success = [bool(ref.success) for ref in refs]
592
+ ref_success = xp.reshape(xp.asarray(ref_success, dtype=xp.bool), shape)
593
+ xp_assert_equal(res.success, ref_success)
594
+
595
+ ref_status = [ref.status for ref in refs]
596
+ ref_status = xp.reshape(xp.asarray(ref_status, dtype=xp.int32), shape)
597
+ xp_assert_equal(res.status, ref_status)
598
+
599
+ ref_nfev = [ref.nfev for ref in refs]
600
+ ref_nfev = xp.reshape(xp.asarray(ref_nfev, dtype=xp.int32), shape)
601
+ if is_numpy(xp):
602
+ xp_assert_equal(res.nfev, ref_nfev)
603
+ assert xp.max(res.nfev) == f.f_evals
604
+ else: # different backend may lead to different nfev
605
+ assert res.nfev.shape == shape
606
+ assert res.nfev.dtype == xp.int32
607
+
608
+ ref_nit = [ref.nit for ref in refs]
609
+ ref_nit = xp.reshape(xp.asarray(ref_nit, dtype=xp.int32), shape)
610
+ if is_numpy(xp):
611
+ xp_assert_equal(res.nit, ref_nit)
612
+ assert xp.max(res.nit) == f.f_evals-2
613
+ else:
614
+ assert res.nit.shape == shape
615
+ assert res.nit.dtype == xp.int32
616
+
617
+ ref_xl = [ref.bracket[0] for ref in refs]
618
+ ref_xl = xp.reshape(xp.asarray(ref_xl, dtype=dtype), shape)
619
+ xp_assert_close(res.bracket[0], ref_xl)
620
+
621
+ ref_xr = [ref.bracket[1] for ref in refs]
622
+ ref_xr = xp.reshape(xp.asarray(ref_xr, dtype=dtype), shape)
623
+ xp_assert_close(res.bracket[1], ref_xr)
624
+
625
+ xp_assert_less(res.bracket[0], res.bracket[1])
626
+ finite = xp.isfinite(res.x)
627
+ assert xp.all((res.x[finite] == res.bracket[0][finite])
628
+ | (res.x[finite] == res.bracket[1][finite]))
629
+
630
+ # PyTorch and CuPy don't solve to the same accuracy as NumPy - that's OK.
631
+ atol = 1e-15 if is_numpy(xp) else 1e-9
632
+
633
+ ref_fl = [ref.f_bracket[0] for ref in refs]
634
+ ref_fl = xp.reshape(xp.asarray(ref_fl, dtype=dtype), shape)
635
+ xp_assert_close(res.f_bracket[0], ref_fl, atol=atol)
636
+ xp_assert_equal(res.f_bracket[0], self.f(res.bracket[0], *args_xp))
637
+
638
+ ref_fr = [ref.f_bracket[1] for ref in refs]
639
+ ref_fr = xp.reshape(xp.asarray(ref_fr, dtype=dtype), shape)
640
+ xp_assert_close(res.f_bracket[1], ref_fr, atol=atol)
641
+ xp_assert_equal(res.f_bracket[1], self.f(res.bracket[1], *args_xp))
642
+
643
+ assert xp.all(xp.abs(res.f_x[finite]) ==
644
+ xp.minimum(xp.abs(res.f_bracket[0][finite]),
645
+ xp.abs(res.f_bracket[1][finite])))
646
+
647
+ def test_flags(self, xp):
648
+ # Test cases that should produce different status flags; show that all
649
+ # can be produced simultaneously.
650
+ def f(xs, js):
651
+ # Note that full_like and int(j) shouldn't really be required. CuPy
652
+ # is just really picky here, so I'm making it a special case to
653
+ # make sure the other backends work when the user is less careful.
654
+ assert js.dtype == xp.int64
655
+ if is_cupy(xp):
656
+ funcs = [lambda x: x - 2.5,
657
+ lambda x: x - 10,
658
+ lambda x: (x - 0.1)**3,
659
+ lambda x: xp.full_like(x, xp.asarray(xp.nan))]
660
+ return [funcs[int(j)](x) for x, j in zip(xs, js)]
661
+
662
+ funcs = [lambda x: x - 2.5,
663
+ lambda x: x - 10,
664
+ lambda x: (x - 0.1) ** 3,
665
+ lambda x: xp.nan]
666
+ return [funcs[j](x) for x, j in zip(xs, js)]
667
+
668
+ args = (xp.arange(4, dtype=xp.int64),)
669
+ a, b = xp.asarray([0.]*4), xp.asarray([xp.pi]*4)
670
+ res = find_root(f, (a, b), args=args, maxiter=2)
671
+
672
+ ref_flags = xp.asarray([eim._ECONVERGED,
673
+ eim._ESIGNERR,
674
+ eim._ECONVERR,
675
+ eim._EVALUEERR], dtype=xp.int32)
676
+ xp_assert_equal(res.status, ref_flags)
677
+
678
+ def test_convergence(self, xp):
679
+ # Test that the convergence tolerances behave as expected
680
+ rng = np.random.default_rng(2585255913088665241)
681
+ p = xp.asarray(rng.random(size=3))
682
+ bracket = (-xp.asarray(5.), xp.asarray(5.))
683
+ args = (p,)
684
+ kwargs0 = dict(args=args, tolerances=dict(xatol=0, xrtol=0, fatol=0, frtol=0))
685
+
686
+ kwargs = deepcopy(kwargs0)
687
+ kwargs['tolerances']['xatol'] = 1e-3
688
+ res1 = find_root(self.f, bracket, **kwargs)
689
+ xp_assert_less(res1.bracket[1] - res1.bracket[0],
690
+ xp.full_like(p, xp.asarray(1e-3)))
691
+ kwargs['tolerances']['xatol'] = 1e-6
692
+ res2 = find_root(self.f, bracket, **kwargs)
693
+ xp_assert_less(res2.bracket[1] - res2.bracket[0],
694
+ xp.full_like(p, xp.asarray(1e-6)))
695
+ xp_assert_less(res2.bracket[1] - res2.bracket[0],
696
+ res1.bracket[1] - res1.bracket[0])
697
+
698
+ kwargs = deepcopy(kwargs0)
699
+ kwargs['tolerances']['xrtol'] = 1e-3
700
+ res1 = find_root(self.f, bracket, **kwargs)
701
+ xp_assert_less(res1.bracket[1] - res1.bracket[0], 1e-3 * xp.abs(res1.x))
702
+ kwargs['tolerances']['xrtol'] = 1e-6
703
+ res2 = find_root(self.f, bracket, **kwargs)
704
+ xp_assert_less(res2.bracket[1] - res2.bracket[0],
705
+ 1e-6 * xp.abs(res2.x))
706
+ xp_assert_less(res2.bracket[1] - res2.bracket[0],
707
+ res1.bracket[1] - res1.bracket[0])
708
+
709
+ kwargs = deepcopy(kwargs0)
710
+ kwargs['tolerances']['fatol'] = 1e-3
711
+ res1 = find_root(self.f, bracket, **kwargs)
712
+ xp_assert_less(xp.abs(res1.f_x), xp.full_like(p, xp.asarray(1e-3)))
713
+ kwargs['tolerances']['fatol'] = 1e-6
714
+ res2 = find_root(self.f, bracket, **kwargs)
715
+ xp_assert_less(xp.abs(res2.f_x), xp.full_like(p, xp.asarray(1e-6)))
716
+ xp_assert_less(xp.abs(res2.f_x), xp.abs(res1.f_x))
717
+
718
+ kwargs = deepcopy(kwargs0)
719
+ kwargs['tolerances']['frtol'] = 1e-3
720
+ x1, x2 = bracket
721
+ f0 = xp.minimum(xp.abs(self.f(x1, *args)), xp.abs(self.f(x2, *args)))
722
+ res1 = find_root(self.f, bracket, **kwargs)
723
+ xp_assert_less(xp.abs(res1.f_x), 1e-3*f0)
724
+ kwargs['tolerances']['frtol'] = 1e-6
725
+ res2 = find_root(self.f, bracket, **kwargs)
726
+ xp_assert_less(xp.abs(res2.f_x), 1e-6*f0)
727
+ xp_assert_less(xp.abs(res2.f_x), xp.abs(res1.f_x))
728
+
729
+ def test_maxiter_callback(self, xp):
730
+ # Test behavior of `maxiter` parameter and `callback` interface
731
+ p = xp.asarray(0.612814)
732
+ bracket = (xp.asarray(-5.), xp.asarray(5.))
733
+ maxiter = 5
734
+
735
+ def f(q, p):
736
+ res = special.ndtr(q) - p
737
+ f.x = q
738
+ f.f_x = res
739
+ return res
740
+ f.x = None
741
+ f.f_x = None
742
+
743
+ res = find_root(f, bracket, args=(p,), maxiter=maxiter)
744
+ assert not xp.any(res.success)
745
+ assert xp.all(res.nfev == maxiter+2)
746
+ assert xp.all(res.nit == maxiter)
747
+
748
+ def callback(res):
749
+ callback.iter += 1
750
+ callback.res = res
751
+ assert hasattr(res, 'x')
752
+ if callback.iter == 0:
753
+ # callback is called once with initial bracket
754
+ assert (res.bracket[0], res.bracket[1]) == bracket
755
+ else:
756
+ changed = (((res.bracket[0] == callback.bracket[0])
757
+ & (res.bracket[1] != callback.bracket[1]))
758
+ | ((res.bracket[0] != callback.bracket[0])
759
+ & (res.bracket[1] == callback.bracket[1])))
760
+ assert xp.all(changed)
761
+
762
+ callback.bracket[0] = res.bracket[0]
763
+ callback.bracket[1] = res.bracket[1]
764
+ assert res.status == eim._EINPROGRESS
765
+ xp_assert_equal(self.f(res.bracket[0], p), res.f_bracket[0])
766
+ xp_assert_equal(self.f(res.bracket[1], p), res.f_bracket[1])
767
+ xp_assert_equal(self.f(res.x, p), res.f_x)
768
+ if callback.iter == maxiter:
769
+ raise StopIteration
770
+ callback.iter = -1 # callback called once before first iteration
771
+ callback.res = None
772
+ callback.bracket = [None, None]
773
+
774
+ res2 = find_root(f, bracket, args=(p,), callback=callback)
775
+
776
+ # terminating with callback is identical to terminating due to maxiter
777
+ # (except for `status`)
778
+ for key in res.keys():
779
+ if key == 'status':
780
+ xp_assert_equal(res[key], xp.asarray(eim._ECONVERR, dtype=xp.int32))
781
+ xp_assert_equal(res2[key], xp.asarray(eim._ECALLBACK, dtype=xp.int32))
782
+ elif key in {'bracket', 'f_bracket'}:
783
+ xp_assert_equal(res2[key][0], res[key][0])
784
+ xp_assert_equal(res2[key][1], res[key][1])
785
+ elif key.startswith('_'):
786
+ continue
787
+ else:
788
+ xp_assert_equal(res2[key], res[key])
789
+
790
+ @pytest.mark.parametrize('case', _CHANDRUPATLA_TESTS)
791
+ def test_nit_expected(self, case, xp):
792
+ # Test that `_chandrupatla` implements Chandrupatla's algorithm:
793
+ # in all 40 test cases, the number of iterations performed
794
+ # matches the number reported in the original paper.
795
+ f, bracket, root, nfeval, id = case
796
+ # Chandrupatla's criterion is equivalent to
797
+ # abs(x2-x1) < 4*abs(xmin)*xrtol + xatol, but we use the more standard
798
+ # abs(x2-x1) < abs(xmin)*xrtol + xatol. Therefore, set xrtol to 4x
799
+ # that used by Chandrupatla in tests.
800
+ bracket = (xp.asarray(bracket[0], dtype=xp.float64),
801
+ xp.asarray(bracket[1], dtype=xp.float64))
802
+ root = xp.asarray(root, dtype=xp.float64)
803
+
804
+ res = find_root(f, bracket, tolerances=dict(xrtol=4e-10, xatol=1e-5))
805
+ xp_assert_close(res.f_x, xp.asarray(f(root), dtype=xp.float64),
806
+ rtol=1e-8, atol=2e-3)
807
+ xp_assert_equal(res.nfev, xp.asarray(nfeval, dtype=xp.int32))
808
+
809
+ @pytest.mark.parametrize("root", (0.622, [0.622, 0.623]))
810
+ @pytest.mark.parametrize("dtype", ('float16', 'float32', 'float64'))
811
+ def test_dtype(self, root, dtype, xp):
812
+ # Test that dtypes are preserved
813
+ not_numpy = not is_numpy(xp)
814
+ if not_numpy and dtype == 'float16':
815
+ pytest.skip("`float16` dtype only supported for NumPy arrays.")
816
+
817
+ dtype = getattr(xp, dtype, None)
818
+ if dtype is None:
819
+ pytest.skip(f"{xp} does not support {dtype}")
820
+
821
+ def f(x, root):
822
+ res = (x - root) ** 3.
823
+ if is_numpy(xp): # NumPy does not preserve dtype
824
+ return xp.asarray(res, dtype=dtype)
825
+ return res
826
+
827
+ a, b = xp.asarray(-3, dtype=dtype), xp.asarray(3, dtype=dtype)
828
+ root = xp.asarray(root, dtype=dtype)
829
+ res = find_root(f, (a, b), args=(root,), tolerances={'xatol': 1e-3})
830
+ try:
831
+ xp_assert_close(res.x, root, atol=1e-3)
832
+ except AssertionError:
833
+ assert res.x.dtype == dtype
834
+ xp.all(res.f_x == 0)
835
+
836
+ def test_input_validation(self, xp):
837
+ # Test input validation for appropriate error messages
838
+
839
+ def func(x):
840
+ return x
841
+
842
+ message = '`func` must be callable.'
843
+ with pytest.raises(ValueError, match=message):
844
+ bracket = xp.asarray(-4), xp.asarray(4)
845
+ find_root(None, bracket)
846
+
847
+ message = 'Abscissae and function output must be real numbers.'
848
+ with pytest.raises(ValueError, match=message):
849
+ bracket = xp.asarray(-4+1j), xp.asarray(4)
850
+ find_root(func, bracket)
851
+
852
+ # raised by `np.broadcast, but the traceback is readable IMO
853
+ # all messages include this part
854
+ message = "(not be broadcast|Attempting to broadcast a dimension of length)"
855
+ with pytest.raises((ValueError, RuntimeError), match=message):
856
+ bracket = xp.asarray([-2, -3]), xp.asarray([3, 4, 5])
857
+ find_root(func, bracket)
858
+
859
+ message = "The shape of the array returned by `func`..."
860
+ with pytest.raises(ValueError, match=message):
861
+ bracket = xp.asarray([-3, -3]), xp.asarray([5, 5])
862
+ find_root(lambda x: [x[0], x[1], x[1]], bracket)
863
+
864
+ message = 'Tolerances must be non-negative scalars.'
865
+ bracket = xp.asarray(-4), xp.asarray(4)
866
+ with pytest.raises(ValueError, match=message):
867
+ find_root(func, bracket, tolerances=dict(xatol=-1))
868
+ with pytest.raises(ValueError, match=message):
869
+ find_root(func, bracket, tolerances=dict(xrtol=xp.nan))
870
+ with pytest.raises(ValueError, match=message):
871
+ find_root(func, bracket, tolerances=dict(fatol='ekki'))
872
+ with pytest.raises(ValueError, match=message):
873
+ find_root(func, bracket, tolerances=dict(frtol=xp.nan))
874
+
875
+ message = '`maxiter` must be a non-negative integer.'
876
+ with pytest.raises(ValueError, match=message):
877
+ find_root(func, bracket, maxiter=1.5)
878
+ with pytest.raises(ValueError, match=message):
879
+ find_root(func, bracket, maxiter=-1)
880
+
881
+ message = '`callback` must be callable.'
882
+ with pytest.raises(ValueError, match=message):
883
+ find_root(func, bracket, callback='shrubbery')
884
+
885
+ def test_special_cases(self, xp):
886
+ # Test edge cases and other special cases
887
+
888
+ # Test infinite function values
889
+ def f(x):
890
+ return 1 / x + 1 - 1 / (-x + 1)
891
+
892
+ a, b = xp.asarray([0.1, 0., 0., 0.1]), xp.asarray([0.9, 1.0, 0.9, 1.0])
893
+
894
+ with np.errstate(divide='ignore', invalid='ignore'):
895
+ res = find_root(f, (a, b))
896
+
897
+ assert xp.all(res.success)
898
+ xp_assert_close(res.x[1:], xp.full((3,), res.x[0]))
899
+
900
+ # Test that integers are not passed to `f`
901
+ # (otherwise this would overflow)
902
+ def f(x):
903
+ assert xp.isdtype(x.dtype, "real floating")
904
+ # this would overflow if x were an xp integer dtype
905
+ return x ** 31 - 1
906
+
907
+ # note that all inputs are integer type; result is automatically default float
908
+ res = find_root(f, (xp.asarray(-7), xp.asarray(5)))
909
+ assert res.success
910
+ xp_assert_close(res.x, xp.asarray(1.))
911
+
912
+ # Test that if both ends of bracket equal root, algorithm reports
913
+ # convergence.
914
+ def f(x, root):
915
+ return x**2 - root
916
+
917
+ root = xp.asarray([0, 1])
918
+ res = find_root(f, (xp.asarray(1), xp.asarray(1)), args=(root,))
919
+ xp_assert_equal(res.success, xp.asarray([False, True]))
920
+ xp_assert_equal(res.x, xp.asarray([xp.nan, 1.]))
921
+
922
+ def f(x):
923
+ return 1/x
924
+
925
+ with np.errstate(invalid='ignore'):
926
+ inf = xp.asarray(xp.inf)
927
+ res = find_root(f, (inf, inf))
928
+ assert res.success
929
+ xp_assert_equal(res.x, xp.asarray(xp.inf))
930
+
931
+ # Test maxiter = 0. Should do nothing to bracket.
932
+ def f(x):
933
+ return x**3 - 1
934
+
935
+ a, b = xp.asarray(-3.), xp.asarray(5.)
936
+ res = find_root(f, (a, b), maxiter=0)
937
+ xp_assert_equal(res.success, xp.asarray(False))
938
+ xp_assert_equal(res.status, xp.asarray(-2, dtype=xp.int32))
939
+ xp_assert_equal(res.nit, xp.asarray(0, dtype=xp.int32))
940
+ xp_assert_equal(res.nfev, xp.asarray(2, dtype=xp.int32))
941
+ xp_assert_equal(res.bracket[0], a)
942
+ xp_assert_equal(res.bracket[1], b)
943
+ # The `x` attribute is the one with the smaller function value
944
+ xp_assert_equal(res.x, a)
945
+ # Reverse bracket; check that this is still true
946
+ res = find_root(f, (-b, -a), maxiter=0)
947
+ xp_assert_equal(res.x, -a)
948
+
949
+ # Test maxiter = 1
950
+ res = find_root(f, (a, b), maxiter=1)
951
+ xp_assert_equal(res.success, xp.asarray(True))
952
+ xp_assert_equal(res.status, xp.asarray(0, dtype=xp.int32))
953
+ xp_assert_equal(res.nit, xp.asarray(1, dtype=xp.int32))
954
+ xp_assert_equal(res.nfev, xp.asarray(3, dtype=xp.int32))
955
+ xp_assert_close(res.x, xp.asarray(1.))
956
+
957
+ # Test scalar `args` (not in tuple)
958
+ def f(x, c):
959
+ return c*x - 1
960
+
961
+ res = find_root(f, (xp.asarray(-1), xp.asarray(1)), args=xp.asarray(3))
962
+ xp_assert_close(res.x, xp.asarray(1/3))
963
+
964
+ # # TODO: Test zero tolerance
965
+ # # ~~What's going on here - why are iterations repeated?~~
966
+ # # tl goes to zero when xatol=xrtol=0. When function is nearly linear,
967
+ # # this causes convergence issues.
968
+ # def f(x):
969
+ # return np.cos(x)
970
+ #
971
+ # res = _chandrupatla_root(f, 0, np.pi, xatol=0, xrtol=0)
972
+ # assert res.nit < 100
973
+ # xp = np.nextafter(res.x, np.inf)
974
+ # xm = np.nextafter(res.x, -np.inf)
975
+ # assert np.abs(res.fun) < np.abs(f(xp))
976
+ # assert np.abs(res.fun) < np.abs(f(xm))
loan-advisor-agent/.venv/Lib/site-packages/scipy/optimize/tests/test_cobyla.py ADDED
@@ -0,0 +1,195 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import math
2
+
3
+ import numpy as np
4
+ from numpy.testing import assert_allclose, assert_array_almost_equal
5
+
6
+ from scipy.optimize import (
7
+ fmin_cobyla, minimize, Bounds, NonlinearConstraint, LinearConstraint,
8
+ OptimizeResult
9
+ )
10
+
11
+
12
+ class TestCobyla:
13
+ def setup_method(self):
14
+ # The algorithm is very fragile on 32 bit, so unfortunately we need to start
15
+ # very near the solution in order for the test to pass.
16
+ self.x0 = [np.sqrt(25 - (2.0/3)**2), 2.0/3 + 1e-4]
17
+ self.solution = [math.sqrt(25 - (2.0/3)**2), 2.0/3]
18
+ self.opts = {'disp': 0, 'rhobeg': 1, 'tol': 1e-6,
19
+ 'maxiter': 100}
20
+
21
+ def fun(self, x):
22
+ return x[0]**2 + abs(x[1])**3
23
+
24
+ def con1(self, x):
25
+ return x[0]**2 + x[1]**2 - 25
26
+
27
+ def con2(self, x):
28
+ return -self.con1(x)
29
+
30
+ def test_simple(self):
31
+ # use disp=True as smoke test for gh-8118
32
+ x = fmin_cobyla(self.fun, self.x0, [self.con1, self.con2], rhobeg=1,
33
+ rhoend=1e-5, maxfun=100, disp=1)
34
+ assert_allclose(x, self.solution, atol=1e-4)
35
+
36
+ def test_minimize_simple(self):
37
+ class Callback:
38
+ def __init__(self):
39
+ self.n_calls = 0
40
+ self.last_x = None
41
+
42
+ def __call__(self, x):
43
+ self.n_calls += 1
44
+ self.last_x = x
45
+
46
+ class CallbackNewSyntax:
47
+ def __init__(self):
48
+ self.n_calls = 0
49
+
50
+ def __call__(self, intermediate_result):
51
+ assert isinstance(intermediate_result, OptimizeResult)
52
+ self.n_calls += 1
53
+
54
+ callback = Callback()
55
+ callback_new_syntax = CallbackNewSyntax()
56
+
57
+ # Minimize with method='COBYLA'
58
+ cons = (NonlinearConstraint(self.con1, 0, np.inf),
59
+ {'type': 'ineq', 'fun': self.con2})
60
+ sol = minimize(self.fun, self.x0, method='cobyla', constraints=cons,
61
+ callback=callback, options=self.opts)
62
+ sol_new = minimize(self.fun, self.x0, method='cobyla', constraints=cons,
63
+ callback=callback_new_syntax, options=self.opts)
64
+ assert_allclose(sol.x, self.solution, atol=1e-4)
65
+ assert sol.success, sol.message
66
+ assert sol.maxcv < 1e-5, sol
67
+ assert sol.nfev < 70, sol
68
+ assert sol.fun < self.fun(self.solution) + 1e-3, sol
69
+ assert_array_almost_equal(
70
+ sol.x,
71
+ callback.last_x,
72
+ decimal=5,
73
+ err_msg="Last design vector sent to the callback is not equal to"
74
+ " returned value.",
75
+ )
76
+ assert sol_new.success, sol_new.message
77
+ assert sol.fun == sol_new.fun
78
+ assert sol.maxcv == sol_new.maxcv
79
+ assert sol.nfev == sol_new.nfev
80
+ assert callback.n_calls == callback_new_syntax.n_calls, \
81
+ "Callback is not called the same amount of times for old and new syntax."
82
+
83
+ def test_minimize_constraint_violation(self):
84
+ # We set up conflicting constraints so that the algorithm will be
85
+ # guaranteed to end up with maxcv > 0.
86
+ cons = ({'type': 'ineq', 'fun': lambda x: 4 - x},
87
+ {'type': 'ineq', 'fun': lambda x: x - 5})
88
+ sol = minimize(lambda x: x, [0], method='cobyla', constraints=cons,
89
+ options={'catol': 0.6})
90
+ assert sol.maxcv > 0.1
91
+ assert sol.success
92
+ sol = minimize(lambda x: x, [0], method='cobyla', constraints=cons,
93
+ options={'catol': 0.4})
94
+ assert sol.maxcv > 0.1
95
+ assert not sol.success
96
+
97
+ def test_f_target(self):
98
+ f_target = 250
99
+ sol = minimize(lambda x: x**2, [500], method='cobyla',
100
+ options={'f_target': f_target})
101
+ assert sol.status == 1
102
+ assert sol.success
103
+ assert sol.fun <= f_target
104
+
105
+ def test_minimize_linear_constraints(self):
106
+ constraints = LinearConstraint([1.0, 1.0], 1.0, 1.0)
107
+ sol = minimize(
108
+ self.fun,
109
+ self.x0,
110
+ method='cobyla',
111
+ constraints=constraints,
112
+ options=self.opts,
113
+ )
114
+ solution = [(4 - np.sqrt(7)) / 3, (np.sqrt(7) - 1) / 3]
115
+ assert_allclose(sol.x, solution, atol=1e-4)
116
+ assert sol.success, sol.message
117
+ assert sol.maxcv < 1e-8, sol
118
+ assert sol.nfev <= 100, sol
119
+ assert sol.fun < self.fun(solution) + 1e-3, sol
120
+
121
+
122
+ def test_vector_constraints():
123
+ # test that fmin_cobyla and minimize can take a combination
124
+ # of constraints, some returning a number and others an array
125
+ def fun(x):
126
+ return (x[0] - 1)**2 + (x[1] - 2.5)**2
127
+
128
+ def fmin(x):
129
+ return fun(x) - 1
130
+
131
+ def cons1(x):
132
+ a = np.array([[1, -2, 2], [-1, -2, 6], [-1, 2, 2]])
133
+ return np.array([a[i, 0] * x[0] + a[i, 1] * x[1] +
134
+ a[i, 2] for i in range(len(a))])
135
+
136
+ def cons2(x):
137
+ return x # identity, acts as bounds x > 0
138
+
139
+ x0 = np.array([2, 0])
140
+ cons_list = [fun, cons1, cons2]
141
+
142
+ xsol = [1.4, 1.7]
143
+ fsol = 0.8
144
+
145
+ # testing fmin_cobyla
146
+ sol = fmin_cobyla(fun, x0, cons_list, rhoend=1e-5)
147
+ assert_allclose(sol, xsol, atol=1e-4)
148
+
149
+ sol = fmin_cobyla(fun, x0, fmin, rhoend=1e-5)
150
+ assert_allclose(fun(sol), 1, atol=1e-4)
151
+
152
+ # testing minimize
153
+ constraints = [{'type': 'ineq', 'fun': cons} for cons in cons_list]
154
+ sol = minimize(fun, x0, constraints=constraints, tol=1e-5)
155
+ assert_allclose(sol.x, xsol, atol=1e-4)
156
+ assert sol.success, sol.message
157
+ assert_allclose(sol.fun, fsol, atol=1e-4)
158
+
159
+ constraints = {'type': 'ineq', 'fun': fmin}
160
+ sol = minimize(fun, x0, constraints=constraints, tol=1e-5)
161
+ assert_allclose(sol.fun, 1, atol=1e-4)
162
+
163
+
164
+ class TestBounds:
165
+ # Test cobyla support for bounds (only when used via `minimize`)
166
+ # Invalid bounds is tested in
167
+ # test_optimize.TestOptimizeSimple.test_minimize_invalid_bounds
168
+
169
+ def test_basic(self):
170
+ def f(x):
171
+ return np.sum(x**2)
172
+
173
+ lb = [-1, None, 1, None, -0.5]
174
+ ub = [-0.5, -0.5, None, None, -0.5]
175
+ bounds = [(a, b) for a, b in zip(lb, ub)]
176
+ # these are converted to Bounds internally
177
+
178
+ res = minimize(f, x0=[1, 2, 3, 4, 5], method='cobyla', bounds=bounds)
179
+ ref = [-0.5, -0.5, 1, 0, -0.5]
180
+ assert res.success
181
+ assert_allclose(res.x, ref, atol=1e-3)
182
+
183
+ def test_unbounded(self):
184
+ def f(x):
185
+ return np.sum(x**2)
186
+
187
+ bounds = Bounds([-np.inf, -np.inf], [np.inf, np.inf])
188
+ res = minimize(f, x0=[1, 2], method='cobyla', bounds=bounds)
189
+ assert res.success
190
+ assert_allclose(res.x, 0, atol=1e-3)
191
+
192
+ bounds = Bounds([1, -np.inf], [np.inf, np.inf])
193
+ res = minimize(f, x0=[1, 2], method='cobyla', bounds=bounds)
194
+ assert res.success
195
+ assert_allclose(res.x, [1, 0], atol=1e-3)
loan-advisor-agent/.venv/Lib/site-packages/scipy/optimize/tests/test_cobyqa.py ADDED
@@ -0,0 +1,252 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import numpy as np
2
+ import pytest
3
+ import threading
4
+ from numpy.testing import assert_allclose, assert_equal
5
+
6
+ from scipy.optimize import (
7
+ Bounds,
8
+ LinearConstraint,
9
+ NonlinearConstraint,
10
+ OptimizeResult,
11
+ minimize,
12
+ )
13
+
14
+
15
+ class TestCOBYQA:
16
+
17
+ def setup_method(self):
18
+ self.x0 = [4.95, 0.66]
19
+ self.options = {'maxfev': 100}
20
+
21
+ @staticmethod
22
+ def fun(x, c=1.0):
23
+ return x[0]**2 + c * abs(x[1])**3
24
+
25
+ @staticmethod
26
+ def con(x):
27
+ return x[0]**2 + x[1]**2 - 25.0
28
+
29
+ def test_minimize_simple(self):
30
+ class Callback:
31
+ def __init__(self):
32
+ self.lock = threading.Lock()
33
+ self.n_calls = 0
34
+
35
+ def __call__(self, x):
36
+ assert isinstance(x, np.ndarray)
37
+ with self.lock:
38
+ self.n_calls += 1
39
+
40
+ class CallbackNewSyntax:
41
+ def __init__(self):
42
+ self.lock = threading.Lock()
43
+ self.n_calls = 0
44
+
45
+ def __call__(self, intermediate_result):
46
+ assert isinstance(intermediate_result, OptimizeResult)
47
+ with self.lock:
48
+ self.n_calls += 1
49
+
50
+ x0 = [4.95, 0.66]
51
+ callback = Callback()
52
+ callback_new_syntax = CallbackNewSyntax()
53
+
54
+ # Minimize with method='cobyqa'.
55
+ constraints = NonlinearConstraint(self.con, 0.0, 0.0)
56
+ sol = minimize(
57
+ self.fun,
58
+ x0,
59
+ method='cobyqa',
60
+ constraints=constraints,
61
+ callback=callback,
62
+ options=self.options,
63
+ )
64
+ sol_new = minimize(
65
+ self.fun,
66
+ x0,
67
+ method='cobyqa',
68
+ constraints=constraints,
69
+ callback=callback_new_syntax,
70
+ options=self.options,
71
+ )
72
+ solution = [np.sqrt(25.0 - 4.0 / 9.0), 2.0 / 3.0]
73
+ assert_allclose(sol.x, solution, atol=1e-4)
74
+ assert sol.success, sol.message
75
+ assert sol.maxcv < 1e-8, sol
76
+ assert sol.nfev <= 100, sol
77
+ assert sol.fun < self.fun(solution) + 1e-3, sol
78
+ assert sol.nfev == callback.n_calls, \
79
+ "Callback is not called exactly once for every function eval."
80
+ assert_equal(sol.x, sol_new.x)
81
+ assert sol_new.success, sol_new.message
82
+ assert sol.fun == sol_new.fun
83
+ assert sol.maxcv == sol_new.maxcv
84
+ assert sol.nfev == sol_new.nfev
85
+ assert sol.nit == sol_new.nit
86
+ assert sol_new.nfev == callback_new_syntax.n_calls, \
87
+ "Callback is not called exactly once for every function eval."
88
+
89
+ def test_minimize_bounds(self):
90
+ def fun_check_bounds(x):
91
+ assert np.all(bounds.lb <= x) and np.all(x <= bounds.ub)
92
+ return self.fun(x)
93
+
94
+ # Case where the bounds are not active at the solution.
95
+ bounds = Bounds([4.5, 0.6], [5.0, 0.7])
96
+ constraints = NonlinearConstraint(self.con, 0.0, 0.0)
97
+ sol = minimize(
98
+ fun_check_bounds,
99
+ self.x0,
100
+ method='cobyqa',
101
+ bounds=bounds,
102
+ constraints=constraints,
103
+ options=self.options,
104
+ )
105
+ solution = [np.sqrt(25.0 - 4.0 / 9.0), 2.0 / 3.0]
106
+ assert_allclose(sol.x, solution, atol=1e-4)
107
+ assert sol.success, sol.message
108
+ assert sol.maxcv < 1e-8, sol
109
+ assert np.all(bounds.lb <= sol.x) and np.all(sol.x <= bounds.ub), sol
110
+ assert sol.nfev <= 100, sol
111
+ assert sol.fun < self.fun(solution) + 1e-3, sol
112
+
113
+ # Case where the bounds are active at the solution.
114
+ bounds = Bounds([5.0, 0.6], [5.5, 0.65])
115
+ sol = minimize(
116
+ fun_check_bounds,
117
+ self.x0,
118
+ method='cobyqa',
119
+ bounds=bounds,
120
+ constraints=constraints,
121
+ options=self.options,
122
+ )
123
+ assert not sol.success, sol.message
124
+ assert sol.maxcv > 0.35, sol
125
+ assert np.all(bounds.lb <= sol.x) and np.all(sol.x <= bounds.ub), sol
126
+ assert sol.nfev <= 100, sol
127
+
128
+ def test_minimize_linear_constraints(self):
129
+ constraints = LinearConstraint([1.0, 1.0], 1.0, 1.0)
130
+ sol = minimize(
131
+ self.fun,
132
+ self.x0,
133
+ method='cobyqa',
134
+ constraints=constraints,
135
+ options=self.options,
136
+ )
137
+ solution = [(4 - np.sqrt(7)) / 3, (np.sqrt(7) - 1) / 3]
138
+ assert_allclose(sol.x, solution, atol=1e-4)
139
+ assert sol.success, sol.message
140
+ assert sol.maxcv < 1e-8, sol
141
+ assert sol.nfev <= 100, sol
142
+ assert sol.fun < self.fun(solution) + 1e-3, sol
143
+
144
+ def test_minimize_args(self):
145
+ constraints = NonlinearConstraint(self.con, 0.0, 0.0)
146
+ sol = minimize(
147
+ self.fun,
148
+ self.x0,
149
+ args=(2.0,),
150
+ method='cobyqa',
151
+ constraints=constraints,
152
+ options=self.options,
153
+ )
154
+ solution = [np.sqrt(25.0 - 4.0 / 36.0), 2.0 / 6.0]
155
+ assert_allclose(sol.x, solution, atol=1e-4)
156
+ assert sol.success, sol.message
157
+ assert sol.maxcv < 1e-8, sol
158
+ assert sol.nfev <= 100, sol
159
+ assert sol.fun < self.fun(solution, 2.0) + 1e-3, sol
160
+
161
+ def test_minimize_array(self):
162
+ def fun_array(x, dim):
163
+ f = np.array(self.fun(x))
164
+ return np.reshape(f, (1,) * dim)
165
+
166
+ # The argument fun can return an array with a single element.
167
+ bounds = Bounds([4.5, 0.6], [5.0, 0.7])
168
+ constraints = NonlinearConstraint(self.con, 0.0, 0.0)
169
+ sol = minimize(
170
+ self.fun,
171
+ self.x0,
172
+ method='cobyqa',
173
+ bounds=bounds,
174
+ constraints=constraints,
175
+ options=self.options,
176
+ )
177
+ for dim in [0, 1, 2]:
178
+ sol_array = minimize(
179
+ fun_array,
180
+ self.x0,
181
+ args=(dim,),
182
+ method='cobyqa',
183
+ bounds=bounds,
184
+ constraints=constraints,
185
+ options=self.options,
186
+ )
187
+ assert_equal(sol.x, sol_array.x)
188
+ assert sol_array.success, sol_array.message
189
+ assert sol.fun == sol_array.fun
190
+ assert sol.maxcv == sol_array.maxcv
191
+ assert sol.nfev == sol_array.nfev
192
+ assert sol.nit == sol_array.nit
193
+
194
+ # The argument fun cannot return an array with more than one element.
195
+ with pytest.raises(TypeError):
196
+ minimize(
197
+ lambda x: np.array([self.fun(x), self.fun(x)]),
198
+ self.x0,
199
+ method='cobyqa',
200
+ bounds=bounds,
201
+ constraints=constraints,
202
+ options=self.options,
203
+ )
204
+
205
+ def test_minimize_maxfev(self):
206
+ constraints = NonlinearConstraint(self.con, 0.0, 0.0)
207
+ options = {'maxfev': 2}
208
+ sol = minimize(
209
+ self.fun,
210
+ self.x0,
211
+ method='cobyqa',
212
+ constraints=constraints,
213
+ options=options,
214
+ )
215
+ assert not sol.success, sol.message
216
+ assert sol.nfev <= 2, sol
217
+
218
+ def test_minimize_maxiter(self):
219
+ constraints = NonlinearConstraint(self.con, 0.0, 0.0)
220
+ options = {'maxiter': 2}
221
+ sol = minimize(
222
+ self.fun,
223
+ self.x0,
224
+ method='cobyqa',
225
+ constraints=constraints,
226
+ options=options,
227
+ )
228
+ assert not sol.success, sol.message
229
+ assert sol.nit <= 2, sol
230
+
231
+ def test_minimize_f_target(self):
232
+ constraints = NonlinearConstraint(self.con, 0.0, 0.0)
233
+ sol_ref = minimize(
234
+ self.fun,
235
+ self.x0,
236
+ method='cobyqa',
237
+ constraints=constraints,
238
+ options=self.options,
239
+ )
240
+ options = dict(self.options)
241
+ options['f_target'] = sol_ref.fun
242
+ sol = minimize(
243
+ self.fun,
244
+ self.x0,
245
+ method='cobyqa',
246
+ constraints=constraints,
247
+ options=options,
248
+ )
249
+ assert sol.success, sol.message
250
+ assert sol.maxcv < 1e-8, sol
251
+ assert sol.nfev <= sol_ref.nfev, sol
252
+ assert sol.fun <= sol_ref.fun, sol
loan-advisor-agent/.venv/Lib/site-packages/scipy/optimize/tests/test_constraint_conversion.py ADDED
@@ -0,0 +1,284 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Unit test for constraint conversion
3
+ """
4
+ import warnings
5
+
6
+ import numpy as np
7
+ from numpy.testing import (assert_array_almost_equal,
8
+ assert_allclose)
9
+ import pytest
10
+ from scipy.optimize import (NonlinearConstraint, LinearConstraint,
11
+ OptimizeWarning, minimize, BFGS)
12
+ from .test_minimize_constrained import (Maratos, HyperbolicIneq, Rosenbrock,
13
+ IneqRosenbrock, EqIneqRosenbrock,
14
+ BoundedRosenbrock, Elec)
15
+
16
+
17
+ class TestOldToNew:
18
+ x0 = (2, 0)
19
+ bnds = ((0, None), (0, None))
20
+ method = "trust-constr"
21
+
22
+ def test_constraint_dictionary_1(self):
23
+ def fun(x):
24
+ return (x[0] - 1) ** 2 + (x[1] - 2.5) ** 2
25
+ cons = ({'type': 'ineq', 'fun': lambda x: x[0] - 2 * x[1] + 2},
26
+ {'type': 'ineq', 'fun': lambda x: -x[0] - 2 * x[1] + 6},
27
+ {'type': 'ineq', 'fun': lambda x: -x[0] + 2 * x[1] + 2})
28
+
29
+ with warnings.catch_warnings():
30
+ warnings.filterwarnings("ignore", "delta_grad == 0.0", UserWarning)
31
+ res = minimize(fun, self.x0, method=self.method,
32
+ bounds=self.bnds, constraints=cons)
33
+ assert_allclose(res.x, [1.4, 1.7], rtol=1e-4)
34
+ assert_allclose(res.fun, 0.8, rtol=1e-4)
35
+
36
+ def test_constraint_dictionary_2(self):
37
+ def fun(x):
38
+ return (x[0] - 1) ** 2 + (x[1] - 2.5) ** 2
39
+ cons = {'type': 'eq',
40
+ 'fun': lambda x, p1, p2: p1*x[0] - p2*x[1],
41
+ 'args': (1, 1.1),
42
+ 'jac': lambda x, p1, p2: np.array([[p1, -p2]])}
43
+ with warnings.catch_warnings():
44
+ warnings.filterwarnings("ignore", "delta_grad == 0.0", UserWarning)
45
+ res = minimize(fun, self.x0, method=self.method,
46
+ bounds=self.bnds, constraints=cons)
47
+ assert_allclose(res.x, [1.7918552, 1.62895927])
48
+ assert_allclose(res.fun, 1.3857466063348418)
49
+
50
+ def test_constraint_dictionary_3(self):
51
+ def fun(x):
52
+ return (x[0] - 1) ** 2 + (x[1] - 2.5) ** 2
53
+ cons = [{'type': 'ineq', 'fun': lambda x: x[0] - 2 * x[1] + 2},
54
+ NonlinearConstraint(lambda x: x[0] - x[1], 0, 0)]
55
+
56
+ with warnings.catch_warnings():
57
+ warnings.filterwarnings("ignore", "delta_grad == 0.0", UserWarning)
58
+ res = minimize(fun, self.x0, method=self.method,
59
+ bounds=self.bnds, constraints=cons)
60
+ assert_allclose(res.x, [1.75, 1.75], rtol=1e-4)
61
+ assert_allclose(res.fun, 1.125, rtol=1e-4)
62
+
63
+
64
+ class TestNewToOld:
65
+ @pytest.mark.fail_slow(2)
66
+ def test_multiple_constraint_objects(self):
67
+ def fun(x):
68
+ return (x[0] - 1) ** 2 + (x[1] - 2.5) ** 2 + (x[2] - 0.75) ** 2
69
+ x0 = [2, 0, 1]
70
+ coni = [] # only inequality constraints (can use cobyla)
71
+ methods = ["slsqp", "cobyla", "cobyqa", "trust-constr"]
72
+
73
+ # mixed old and new
74
+ coni.append([{'type': 'ineq', 'fun': lambda x: x[0] - 2 * x[1] + 2},
75
+ NonlinearConstraint(lambda x: x[0] - x[1], -1, 1)])
76
+
77
+ coni.append([LinearConstraint([1, -2, 0], -2, np.inf),
78
+ NonlinearConstraint(lambda x: x[0] - x[1], -1, 1)])
79
+
80
+ coni.append([NonlinearConstraint(lambda x: x[0] - 2 * x[1] + 2, 0, np.inf),
81
+ NonlinearConstraint(lambda x: x[0] - x[1], -1, 1)])
82
+
83
+ for con in coni:
84
+ funs = {}
85
+ for method in methods:
86
+ with warnings.catch_warnings():
87
+ warnings.simplefilter("ignore", UserWarning)
88
+ result = minimize(fun, x0, method=method, constraints=con)
89
+ funs[method] = result.fun
90
+ assert_allclose(funs['slsqp'], funs['trust-constr'], rtol=1e-4)
91
+ assert_allclose(funs['cobyla'], funs['trust-constr'], rtol=1e-4)
92
+ assert_allclose(funs['cobyqa'], funs['trust-constr'],
93
+ rtol=1e-4)
94
+
95
+ @pytest.mark.fail_slow(20)
96
+ def test_individual_constraint_objects(self):
97
+ def fun(x):
98
+ return (x[0] - 1) ** 2 + (x[1] - 2.5) ** 2 + (x[2] - 0.75) ** 2
99
+ x0 = [2, 0, 1]
100
+
101
+ cone = [] # with equality constraints (can't use cobyla)
102
+ coni = [] # only inequality constraints (can use cobyla)
103
+ methods = ["slsqp", "cobyla", "cobyqa", "trust-constr"]
104
+
105
+ # nonstandard data types for constraint equality bounds
106
+ cone.append(NonlinearConstraint(lambda x: x[0] - x[1], 1, 1))
107
+ cone.append(NonlinearConstraint(lambda x: x[0] - x[1], [1.21], [1.21]))
108
+ cone.append(NonlinearConstraint(lambda x: x[0] - x[1],
109
+ 1.21, np.array([1.21])))
110
+
111
+ # multiple equalities
112
+ cone.append(NonlinearConstraint(
113
+ lambda x: [x[0] - x[1], x[1] - x[2]],
114
+ 1.21, 1.21)) # two same equalities
115
+ cone.append(NonlinearConstraint(
116
+ lambda x: [x[0] - x[1], x[1] - x[2]],
117
+ [1.21, 1.4], [1.21, 1.4])) # two different equalities
118
+ cone.append(NonlinearConstraint(
119
+ lambda x: [x[0] - x[1], x[1] - x[2]],
120
+ [1.21, 1.21], 1.21)) # equality specified two ways
121
+ cone.append(NonlinearConstraint(
122
+ lambda x: [x[0] - x[1], x[1] - x[2]],
123
+ [1.21, -np.inf], [1.21, np.inf])) # equality + unbounded
124
+
125
+ # nonstandard data types for constraint inequality bounds
126
+ coni.append(NonlinearConstraint(lambda x: x[0] - x[1], 1.21, np.inf))
127
+ coni.append(NonlinearConstraint(lambda x: x[0] - x[1], [1.21], np.inf))
128
+ coni.append(NonlinearConstraint(lambda x: x[0] - x[1],
129
+ 1.21, np.array([np.inf])))
130
+ coni.append(NonlinearConstraint(lambda x: x[0] - x[1], -np.inf, -3))
131
+ coni.append(NonlinearConstraint(lambda x: x[0] - x[1],
132
+ np.array(-np.inf), -3))
133
+
134
+ # multiple inequalities/equalities
135
+ coni.append(NonlinearConstraint(
136
+ lambda x: [x[0] - x[1], x[1] - x[2]],
137
+ 1.21, np.inf)) # two same inequalities
138
+ cone.append(NonlinearConstraint(
139
+ lambda x: [x[0] - x[1], x[1] - x[2]],
140
+ [1.21, -np.inf], [1.21, 1.4])) # mixed equality/inequality
141
+ coni.append(NonlinearConstraint(
142
+ lambda x: [x[0] - x[1], x[1] - x[2]],
143
+ [1.1, .8], [1.2, 1.4])) # bounded above and below
144
+ coni.append(NonlinearConstraint(
145
+ lambda x: [x[0] - x[1], x[1] - x[2]],
146
+ [-1.2, -1.4], [-1.1, -.8])) # - bounded above and below
147
+
148
+ # quick check of LinearConstraint class (very little new code to test)
149
+ cone.append(LinearConstraint([1, -1, 0], 1.21, 1.21))
150
+ cone.append(LinearConstraint([[1, -1, 0], [0, 1, -1]], 1.21, 1.21))
151
+ cone.append(LinearConstraint([[1, -1, 0], [0, 1, -1]],
152
+ [1.21, -np.inf], [1.21, 1.4]))
153
+
154
+ for con in coni:
155
+ funs = {}
156
+ for method in methods:
157
+ with warnings.catch_warnings():
158
+ warnings.simplefilter("ignore", UserWarning)
159
+ result = minimize(fun, x0, method=method, constraints=con)
160
+ funs[method] = result.fun
161
+ assert_allclose(funs['slsqp'], funs['trust-constr'], rtol=1e-3)
162
+ assert_allclose(funs['cobyla'], funs['trust-constr'], rtol=1e-3)
163
+ assert_allclose(funs['cobyqa'], funs['trust-constr'],
164
+ rtol=1e-3)
165
+
166
+ for con in cone:
167
+ funs = {}
168
+ for method in [method for method in methods if method != 'cobyla']:
169
+ with warnings.catch_warnings():
170
+ warnings.simplefilter("ignore", UserWarning)
171
+ result = minimize(fun, x0, method=method, constraints=con)
172
+ funs[method] = result.fun
173
+ assert_allclose(funs['slsqp'], funs['trust-constr'], rtol=1e-3)
174
+ assert_allclose(funs['cobyqa'], funs['trust-constr'],
175
+ rtol=1e-3)
176
+
177
+
178
+ class TestNewToOldSLSQP:
179
+ method = 'slsqp'
180
+ elec = Elec(n_electrons=2)
181
+ elec.x_opt = np.array([-0.58438468, 0.58438466, 0.73597047,
182
+ -0.73597044, 0.34180668, -0.34180667])
183
+ brock = BoundedRosenbrock()
184
+ brock.x_opt = [0, 0]
185
+ list_of_problems = [Maratos(),
186
+ HyperbolicIneq(),
187
+ Rosenbrock(),
188
+ IneqRosenbrock(),
189
+ EqIneqRosenbrock(),
190
+ elec,
191
+ brock
192
+ ]
193
+
194
+ def test_list_of_problems(self):
195
+
196
+ for prob in self.list_of_problems:
197
+
198
+ with warnings.catch_warnings():
199
+ warnings.simplefilter("ignore", UserWarning)
200
+ result = minimize(prob.fun, prob.x0,
201
+ method=self.method,
202
+ bounds=prob.bounds,
203
+ constraints=prob.constr)
204
+
205
+ assert_array_almost_equal(result.x, prob.x_opt, decimal=3)
206
+
207
+ def test_warn_mixed_constraints(self):
208
+ # warns about inefficiency of mixed equality/inequality constraints
209
+ def fun(x):
210
+ return (x[0] - 1) ** 2 + (x[1] - 2.5) ** 2 + (x[2] - 0.75) ** 2
211
+ cons = NonlinearConstraint(lambda x: [x[0]**2 - x[1], x[1] - x[2]],
212
+ [1.1, .8], [1.1, 1.4])
213
+ bnds = ((0, None), (0, None), (0, None))
214
+ with warnings.catch_warnings():
215
+ warnings.filterwarnings("ignore", "delta_grad == 0.0", UserWarning)
216
+ with pytest.warns(OptimizeWarning):
217
+ minimize(fun, (2, 0, 1),
218
+ method=self.method, bounds=bnds, constraints=cons)
219
+
220
+ def test_warn_ignored_options(self):
221
+ # warns about constraint options being ignored
222
+ def fun(x):
223
+ return (x[0] - 1) ** 2 + (x[1] - 2.5) ** 2 + (x[2] - 0.75) ** 2
224
+ x0 = (2, 0, 1)
225
+
226
+ if self.method == "slsqp":
227
+ bnds = ((0, None), (0, None), (0, None))
228
+ else:
229
+ bnds = None
230
+
231
+ cons = NonlinearConstraint(lambda x: x[0], 2, np.inf)
232
+ res = minimize(fun, x0, method=self.method,
233
+ bounds=bnds, constraints=cons)
234
+ # no warnings without constraint options
235
+ assert_allclose(res.fun, 1)
236
+
237
+ cons = LinearConstraint([1, 0, 0], 2, np.inf)
238
+ res = minimize(fun, x0, method=self.method,
239
+ bounds=bnds, constraints=cons)
240
+ # no warnings without constraint options
241
+ assert_allclose(res.fun, 1)
242
+
243
+ cons = []
244
+ cons.append(NonlinearConstraint(lambda x: x[0]**2, 2, np.inf,
245
+ keep_feasible=True))
246
+ cons.append(NonlinearConstraint(lambda x: x[0]**2, 2, np.inf,
247
+ hess=BFGS()))
248
+ cons.append(NonlinearConstraint(lambda x: x[0]**2, 2, np.inf,
249
+ finite_diff_jac_sparsity=42))
250
+ cons.append(NonlinearConstraint(lambda x: x[0]**2, 2, np.inf,
251
+ finite_diff_rel_step=42))
252
+ cons.append(LinearConstraint([1, 0, 0], 2, np.inf,
253
+ keep_feasible=True))
254
+ for con in cons:
255
+ with pytest.warns(OptimizeWarning):
256
+ minimize(fun, x0,
257
+ method=self.method, bounds=bnds, constraints=cons)
258
+
259
+
260
+ class TestNewToOldCobyla:
261
+ method = 'cobyla'
262
+
263
+ list_of_problems = [
264
+ Elec(n_electrons=2),
265
+ Elec(n_electrons=4),
266
+ ]
267
+
268
+ @pytest.mark.slow
269
+ def test_list_of_problems(self):
270
+
271
+ for prob in self.list_of_problems:
272
+
273
+ with warnings.catch_warnings():
274
+ warnings.simplefilter("ignore", UserWarning)
275
+ truth = minimize(prob.fun, prob.x0,
276
+ method='trust-constr',
277
+ bounds=prob.bounds,
278
+ constraints=prob.constr)
279
+ result = minimize(prob.fun, prob.x0,
280
+ method=self.method,
281
+ bounds=prob.bounds,
282
+ constraints=prob.constr)
283
+
284
+ assert_allclose(result.fun, truth.fun, rtol=1e-3)
loan-advisor-agent/.venv/Lib/site-packages/scipy/optimize/tests/test_constraints.py ADDED
@@ -0,0 +1,255 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import pytest
2
+ import numpy as np
3
+ from numpy.testing import TestCase, assert_array_equal
4
+ import scipy.sparse as sps
5
+ from scipy.optimize._constraints import (
6
+ Bounds, LinearConstraint, NonlinearConstraint, PreparedConstraint,
7
+ new_bounds_to_old, old_bound_to_new, strict_bounds)
8
+
9
+
10
+ class TestStrictBounds(TestCase):
11
+ def test_scalarvalue_unique_enforce_feasibility(self):
12
+ m = 3
13
+ lb = 2
14
+ ub = 4
15
+ enforce_feasibility = False
16
+ strict_lb, strict_ub = strict_bounds(lb, ub,
17
+ enforce_feasibility,
18
+ m)
19
+ assert_array_equal(strict_lb, [-np.inf, -np.inf, -np.inf])
20
+ assert_array_equal(strict_ub, [np.inf, np.inf, np.inf])
21
+
22
+ enforce_feasibility = True
23
+ strict_lb, strict_ub = strict_bounds(lb, ub,
24
+ enforce_feasibility,
25
+ m)
26
+ assert_array_equal(strict_lb, [2, 2, 2])
27
+ assert_array_equal(strict_ub, [4, 4, 4])
28
+
29
+ def test_vectorvalue_unique_enforce_feasibility(self):
30
+ m = 3
31
+ lb = [1, 2, 3]
32
+ ub = [4, 5, 6]
33
+ enforce_feasibility = False
34
+ strict_lb, strict_ub = strict_bounds(lb, ub,
35
+ enforce_feasibility,
36
+ m)
37
+ assert_array_equal(strict_lb, [-np.inf, -np.inf, -np.inf])
38
+ assert_array_equal(strict_ub, [np.inf, np.inf, np.inf])
39
+
40
+ enforce_feasibility = True
41
+ strict_lb, strict_ub = strict_bounds(lb, ub,
42
+ enforce_feasibility,
43
+ m)
44
+ assert_array_equal(strict_lb, [1, 2, 3])
45
+ assert_array_equal(strict_ub, [4, 5, 6])
46
+
47
+ def test_scalarvalue_vector_enforce_feasibility(self):
48
+ m = 3
49
+ lb = 2
50
+ ub = 4
51
+ enforce_feasibility = [False, True, False]
52
+ strict_lb, strict_ub = strict_bounds(lb, ub,
53
+ enforce_feasibility,
54
+ m)
55
+ assert_array_equal(strict_lb, [-np.inf, 2, -np.inf])
56
+ assert_array_equal(strict_ub, [np.inf, 4, np.inf])
57
+
58
+ def test_vectorvalue_vector_enforce_feasibility(self):
59
+ m = 3
60
+ lb = [1, 2, 3]
61
+ ub = [4, 6, np.inf]
62
+ enforce_feasibility = [True, False, True]
63
+ strict_lb, strict_ub = strict_bounds(lb, ub,
64
+ enforce_feasibility,
65
+ m)
66
+ assert_array_equal(strict_lb, [1, -np.inf, 3])
67
+ assert_array_equal(strict_ub, [4, np.inf, np.inf])
68
+
69
+
70
+ def test_prepare_constraint_infeasible_x0():
71
+ lb = np.array([0, 20, 30])
72
+ ub = np.array([0.5, np.inf, 70])
73
+ x0 = np.array([1, 2, 3])
74
+ enforce_feasibility = np.array([False, True, True], dtype=bool)
75
+ bounds = Bounds(lb, ub, enforce_feasibility)
76
+ pytest.raises(ValueError, PreparedConstraint, bounds, x0)
77
+
78
+ pc = PreparedConstraint(Bounds(lb, ub), [1, 2, 3])
79
+ assert (pc.violation([1, 2, 3]) > 0).any()
80
+ assert (pc.violation([0.25, 21, 31]) == 0).all()
81
+
82
+ x0 = np.array([1, 2, 3, 4])
83
+ A = np.array([[1, 2, 3, 4], [5, 0, 0, 6], [7, 0, 8, 0]])
84
+ enforce_feasibility = np.array([True, True, True], dtype=bool)
85
+ linear = LinearConstraint(A, -np.inf, 0, enforce_feasibility)
86
+ pytest.raises(ValueError, PreparedConstraint, linear, x0)
87
+
88
+ pc = PreparedConstraint(LinearConstraint(A, -np.inf, 0),
89
+ [1, 2, 3, 4])
90
+ assert (pc.violation([1, 2, 3, 4]) > 0).any()
91
+ assert (pc.violation([-10, 2, -10, 4]) == 0).all()
92
+
93
+ def fun(x):
94
+ return A.dot(x)
95
+
96
+ def jac(x):
97
+ return A
98
+
99
+ def hess(x, v):
100
+ return sps.csr_array((4, 4))
101
+
102
+ nonlinear = NonlinearConstraint(fun, -np.inf, 0, jac, hess,
103
+ enforce_feasibility)
104
+ pytest.raises(ValueError, PreparedConstraint, nonlinear, x0)
105
+
106
+ pc = PreparedConstraint(nonlinear, [-10, 2, -10, 4])
107
+ assert (pc.violation([1, 2, 3, 4]) > 0).any()
108
+ assert (pc.violation([-10, 2, -10, 4]) == 0).all()
109
+
110
+
111
+ def test_violation():
112
+ def cons_f(x):
113
+ return np.array([x[0] ** 2 + x[1], x[0] ** 2 - x[1]])
114
+
115
+ nlc = NonlinearConstraint(cons_f, [-1, -0.8500], [2, 2])
116
+ pc = PreparedConstraint(nlc, [0.5, 1])
117
+
118
+ assert_array_equal(pc.violation([0.5, 1]), [0., 0.])
119
+
120
+ np.testing.assert_almost_equal(pc.violation([0.5, 1.2]), [0., 0.1])
121
+
122
+ np.testing.assert_almost_equal(pc.violation([1.2, 1.2]), [0.64, 0])
123
+
124
+ np.testing.assert_almost_equal(pc.violation([0.1, -1.2]), [0.19, 0])
125
+
126
+ np.testing.assert_almost_equal(pc.violation([0.1, 2]), [0.01, 1.14])
127
+
128
+
129
+ def test_new_bounds_to_old():
130
+ lb = np.array([-np.inf, 2, 3])
131
+ ub = np.array([3, np.inf, 10])
132
+
133
+ bounds = [(None, 3), (2, None), (3, 10)]
134
+ assert_array_equal(new_bounds_to_old(lb, ub, 3), bounds)
135
+
136
+ bounds_single_lb = [(-1, 3), (-1, None), (-1, 10)]
137
+ assert_array_equal(new_bounds_to_old(-1, ub, 3), bounds_single_lb)
138
+
139
+ bounds_no_lb = [(None, 3), (None, None), (None, 10)]
140
+ assert_array_equal(new_bounds_to_old(-np.inf, ub, 3), bounds_no_lb)
141
+
142
+ bounds_single_ub = [(None, 20), (2, 20), (3, 20)]
143
+ assert_array_equal(new_bounds_to_old(lb, 20, 3), bounds_single_ub)
144
+
145
+ bounds_no_ub = [(None, None), (2, None), (3, None)]
146
+ assert_array_equal(new_bounds_to_old(lb, np.inf, 3), bounds_no_ub)
147
+
148
+ bounds_single_both = [(1, 2), (1, 2), (1, 2)]
149
+ assert_array_equal(new_bounds_to_old(1, 2, 3), bounds_single_both)
150
+
151
+ bounds_no_both = [(None, None), (None, None), (None, None)]
152
+ assert_array_equal(new_bounds_to_old(-np.inf, np.inf, 3), bounds_no_both)
153
+
154
+
155
+ def test_old_bounds_to_new():
156
+ bounds = ([1, 2], (None, 3), (-1, None))
157
+ lb_true = np.array([1, -np.inf, -1])
158
+ ub_true = np.array([2, 3, np.inf])
159
+
160
+ lb, ub = old_bound_to_new(bounds)
161
+ assert_array_equal(lb, lb_true)
162
+ assert_array_equal(ub, ub_true)
163
+
164
+ bounds = [(-np.inf, np.inf), (np.array([1]), np.array([1]))]
165
+ lb, ub = old_bound_to_new(bounds)
166
+
167
+ assert_array_equal(lb, [-np.inf, 1])
168
+ assert_array_equal(ub, [np.inf, 1])
169
+
170
+
171
+ class TestBounds:
172
+ def test_repr(self):
173
+ # so that eval works
174
+ from numpy import array, inf # noqa: F401
175
+ for args in (
176
+ (-1.0, 5.0),
177
+ (-1.0, np.inf, True),
178
+ (np.array([1.0, -np.inf]), np.array([2.0, np.inf])),
179
+ (np.array([1.0, -np.inf]), np.array([2.0, np.inf]),
180
+ np.array([True, False])),
181
+ ):
182
+ bounds = Bounds(*args)
183
+ bounds2 = eval(repr(Bounds(*args)))
184
+ assert_array_equal(bounds.lb, bounds2.lb)
185
+ assert_array_equal(bounds.ub, bounds2.ub)
186
+ assert_array_equal(bounds.keep_feasible, bounds2.keep_feasible)
187
+
188
+ def test_array(self):
189
+ # gh13501
190
+ b = Bounds(lb=[0.0, 0.0], ub=[1.0, 1.0])
191
+ assert isinstance(b.lb, np.ndarray)
192
+ assert isinstance(b.ub, np.ndarray)
193
+
194
+ def test_defaults(self):
195
+ b1 = Bounds()
196
+ b2 = Bounds(np.asarray(-np.inf), np.asarray(np.inf))
197
+ assert b1.lb == b2.lb
198
+ assert b1.ub == b2.ub
199
+
200
+ def test_input_validation(self):
201
+ message = "Lower and upper bounds must be dense arrays."
202
+ with pytest.raises(ValueError, match=message):
203
+ Bounds(sps.coo_array([1, 2]), [1, 2])
204
+ with pytest.raises(ValueError, match=message):
205
+ Bounds([1, 2], sps.coo_array([1, 2]))
206
+
207
+ message = "`keep_feasible` must be a dense array."
208
+ with pytest.raises(ValueError, match=message):
209
+ Bounds([1, 2], [1, 2], keep_feasible=sps.coo_array([True, True]))
210
+
211
+ message = "`lb`, `ub`, and `keep_feasible` must be broadcastable."
212
+ with pytest.raises(ValueError, match=message):
213
+ Bounds([1, 2], [1, 2, 3])
214
+
215
+ def test_residual(self):
216
+ bounds = Bounds(-2, 4)
217
+ x0 = [-1, 2]
218
+ np.testing.assert_allclose(bounds.residual(x0), ([1, 4], [5, 2]))
219
+
220
+
221
+ class TestLinearConstraint:
222
+ def test_defaults(self):
223
+ A = np.eye(4)
224
+ lc = LinearConstraint(A)
225
+ lc2 = LinearConstraint(A, -np.inf, np.inf)
226
+ assert_array_equal(lc.lb, lc2.lb)
227
+ assert_array_equal(lc.ub, lc2.ub)
228
+
229
+ def test_input_validation(self):
230
+ A = np.eye(4)
231
+ message = "`lb`, `ub`, and `keep_feasible` must be broadcastable"
232
+ with pytest.raises(ValueError, match=message):
233
+ LinearConstraint(A, [1, 2], [1, 2, 3])
234
+
235
+ message = "Constraint limits must be dense arrays"
236
+ with pytest.raises(ValueError, match=message):
237
+ LinearConstraint(A, sps.coo_array([1, 2]), [2, 3])
238
+ with pytest.raises(ValueError, match=message):
239
+ LinearConstraint(A, [1, 2], sps.coo_array([2, 3]))
240
+
241
+ message = "`keep_feasible` must be a dense array"
242
+ with pytest.raises(ValueError, match=message):
243
+ keep_feasible = sps.coo_array([True, True])
244
+ LinearConstraint(A, [1, 2], [2, 3], keep_feasible=keep_feasible)
245
+
246
+ A = np.empty((4, 3, 5))
247
+ message = "`A` must have exactly two dimensions."
248
+ with pytest.raises(ValueError, match=message):
249
+ LinearConstraint(A)
250
+
251
+ def test_residual(self):
252
+ A = np.eye(2)
253
+ lc = LinearConstraint(A, -2, 4)
254
+ x0 = [-1, 2]
255
+ np.testing.assert_allclose(lc.residual(x0), ([1, 4], [5, 2]))
loan-advisor-agent/.venv/Lib/site-packages/scipy/optimize/tests/test_cython_optimize.py ADDED
@@ -0,0 +1,92 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Test Cython optimize zeros API functions: ``bisect``, ``ridder``, ``brenth``,
3
+ and ``brentq`` in `scipy.optimize.cython_optimize`, by finding the roots of a
4
+ 3rd order polynomial given a sequence of constant terms, ``a0``, and fixed 1st,
5
+ 2nd, and 3rd order terms in ``args``.
6
+
7
+ .. math::
8
+
9
+ f(x, a0, args) = ((args[2]*x + args[1])*x + args[0])*x + a0
10
+
11
+ The 3rd order polynomial function is written in Cython and called in a Python
12
+ wrapper named after the zero function. See the private ``_zeros`` Cython module
13
+ in `scipy.optimize.cython_optimze` for more information.
14
+ """
15
+
16
+ import numpy.testing as npt
17
+ from scipy.optimize.cython_optimize import _zeros
18
+
19
+ # CONSTANTS
20
+ # Solve x**3 - A0 = 0 for A0 = [2.0, 2.1, ..., 2.9].
21
+ # The ARGS have 3 elements just to show how this could be done for any cubic
22
+ # polynomial.
23
+ A0 = tuple(-2.0 - x/10.0 for x in range(10)) # constant term
24
+ ARGS = (0.0, 0.0, 1.0) # 1st, 2nd, and 3rd order terms
25
+ XLO, XHI = 0.0, 2.0 # first and second bounds of zeros functions
26
+ # absolute and relative tolerances and max iterations for zeros functions
27
+ XTOL, RTOL, MITR = 0.001, 0.001, 10
28
+ EXPECTED = [(-a0) ** (1.0/3.0) for a0 in A0]
29
+ # = [1.2599210498948732,
30
+ # 1.2805791649874942,
31
+ # 1.300591446851387,
32
+ # 1.3200061217959123,
33
+ # 1.338865900164339,
34
+ # 1.3572088082974532,
35
+ # 1.375068867074141,
36
+ # 1.3924766500838337,
37
+ # 1.4094597464129783,
38
+ # 1.4260431471424087]
39
+
40
+
41
+ # test bisect
42
+ def test_bisect():
43
+ npt.assert_allclose(
44
+ EXPECTED,
45
+ list(
46
+ _zeros.loop_example('bisect', A0, ARGS, XLO, XHI, XTOL, RTOL, MITR)
47
+ ),
48
+ rtol=RTOL, atol=XTOL
49
+ )
50
+
51
+
52
+ # test ridder
53
+ def test_ridder():
54
+ npt.assert_allclose(
55
+ EXPECTED,
56
+ list(
57
+ _zeros.loop_example('ridder', A0, ARGS, XLO, XHI, XTOL, RTOL, MITR)
58
+ ),
59
+ rtol=RTOL, atol=XTOL
60
+ )
61
+
62
+
63
+ # test brenth
64
+ def test_brenth():
65
+ npt.assert_allclose(
66
+ EXPECTED,
67
+ list(
68
+ _zeros.loop_example('brenth', A0, ARGS, XLO, XHI, XTOL, RTOL, MITR)
69
+ ),
70
+ rtol=RTOL, atol=XTOL
71
+ )
72
+
73
+
74
+ # test brentq
75
+ def test_brentq():
76
+ npt.assert_allclose(
77
+ EXPECTED,
78
+ list(
79
+ _zeros.loop_example('brentq', A0, ARGS, XLO, XHI, XTOL, RTOL, MITR)
80
+ ),
81
+ rtol=RTOL, atol=XTOL
82
+ )
83
+
84
+
85
+ # test brentq with full output
86
+ def test_brentq_full_output():
87
+ output = _zeros.full_output_example(
88
+ (A0[0],) + ARGS, XLO, XHI, XTOL, RTOL, MITR)
89
+ npt.assert_allclose(EXPECTED[0], output['root'], rtol=RTOL, atol=XTOL)
90
+ npt.assert_equal(6, output['iterations'])
91
+ npt.assert_equal(7, output['funcalls'])
92
+ npt.assert_equal(0, output['error_num'])
loan-advisor-agent/.venv/Lib/site-packages/scipy/optimize/tests/test_differentiable_functions.py ADDED
@@ -0,0 +1,1023 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import pytest
2
+ import platform
3
+ import numpy as np
4
+ from numpy.testing import (TestCase, assert_array_almost_equal,
5
+ assert_array_equal, assert_, assert_allclose,
6
+ assert_equal)
7
+ from scipy._lib._gcutils import assert_deallocated
8
+ from scipy._lib._util import MapWrapper
9
+ from scipy.sparse import csr_array
10
+ from scipy.sparse.linalg import LinearOperator
11
+ from scipy.optimize._differentiable_functions import (ScalarFunction,
12
+ VectorFunction,
13
+ LinearVectorFunction,
14
+ IdentityVectorFunction)
15
+ from scipy.optimize import rosen, rosen_der, rosen_hess
16
+ from scipy.optimize._hessian_update_strategy import BFGS
17
+
18
+
19
+ class ExScalarFunction:
20
+
21
+ def __init__(self):
22
+ self.nfev = 0
23
+ self.ngev = 0
24
+ self.nhev = 0
25
+
26
+ def fun(self, x):
27
+ self.nfev += 1
28
+ return 2*(x[0]**2 + x[1]**2 - 1) - x[0]
29
+
30
+ def grad(self, x):
31
+ self.ngev += 1
32
+ return np.array([4*x[0]-1, 4*x[1]])
33
+
34
+ def hess(self, x):
35
+ self.nhev += 1
36
+ return 4*np.eye(2)
37
+
38
+
39
+ class TestScalarFunction(TestCase):
40
+
41
+ def test_finite_difference_grad(self):
42
+ ex = ExScalarFunction()
43
+ nfev = 0
44
+ ngev = 0
45
+
46
+ x0 = [1.0, 0.0]
47
+ analit = ScalarFunction(ex.fun, x0, (), ex.grad,
48
+ ex.hess, None, (-np.inf, np.inf))
49
+ nfev += 1
50
+ ngev += 1
51
+ assert_array_equal(ex.nfev, nfev)
52
+ assert_array_equal(analit.nfev, nfev)
53
+ assert_array_equal(ex.ngev, ngev)
54
+ assert_array_equal(analit.ngev, nfev)
55
+ approx = ScalarFunction(ex.fun, x0, (), '2-point',
56
+ ex.hess, None, (-np.inf, np.inf))
57
+ nfev += 3
58
+ ngev += 1
59
+ assert_array_equal(ex.nfev, nfev)
60
+ assert_array_equal(analit.nfev+approx.nfev, nfev)
61
+ assert_array_equal(analit.ngev+approx.ngev, ngev)
62
+ assert_array_equal(analit.f, approx.f)
63
+ assert_array_almost_equal(analit.g, approx.g)
64
+
65
+ x = [10, 0.3]
66
+ f_analit = analit.fun(x)
67
+ g_analit = analit.grad(x)
68
+ nfev += 1
69
+ ngev += 1
70
+ assert_array_equal(ex.nfev, nfev)
71
+ assert_array_equal(analit.nfev+approx.nfev, nfev)
72
+ assert_array_equal(analit.ngev+approx.ngev, ngev)
73
+ f_approx = approx.fun(x)
74
+ g_approx = approx.grad(x)
75
+ nfev += 3
76
+ ngev += 1
77
+ assert_array_equal(ex.nfev, nfev)
78
+ assert_array_equal(analit.nfev+approx.nfev, nfev)
79
+ assert_array_equal(analit.ngev+approx.ngev, ngev)
80
+ assert_array_almost_equal(f_analit, f_approx)
81
+ assert_array_almost_equal(g_analit, g_approx)
82
+
83
+ x = [2.0, 1.0]
84
+ g_analit = analit.grad(x)
85
+ ngev += 1
86
+ assert_array_equal(ex.nfev, nfev)
87
+ assert_array_equal(analit.nfev+approx.nfev, nfev)
88
+ assert_array_equal(analit.ngev+approx.ngev, ngev)
89
+
90
+ g_approx = approx.grad(x)
91
+ nfev += 3
92
+ ngev += 1
93
+ assert_array_equal(ex.nfev, nfev)
94
+ assert_array_equal(analit.nfev+approx.nfev, nfev)
95
+ assert_array_equal(analit.ngev+approx.ngev, ngev)
96
+ assert_array_almost_equal(g_analit, g_approx)
97
+
98
+ x = [2.5, 0.3]
99
+ f_analit = analit.fun(x)
100
+ g_analit = analit.grad(x)
101
+ nfev += 1
102
+ ngev += 1
103
+ assert_array_equal(ex.nfev, nfev)
104
+ assert_array_equal(analit.nfev+approx.nfev, nfev)
105
+ assert_array_equal(analit.ngev+approx.ngev, ngev)
106
+ f_approx = approx.fun(x)
107
+ g_approx = approx.grad(x)
108
+ nfev += 3
109
+ ngev += 1
110
+ assert_array_equal(ex.nfev, nfev)
111
+ assert_array_equal(analit.nfev+approx.nfev, nfev)
112
+ assert_array_equal(analit.ngev+approx.ngev, ngev)
113
+ assert_array_almost_equal(f_analit, f_approx)
114
+ assert_array_almost_equal(g_analit, g_approx)
115
+
116
+ x = [2, 0.3]
117
+ f_analit = analit.fun(x)
118
+ g_analit = analit.grad(x)
119
+ nfev += 1
120
+ ngev += 1
121
+ assert_array_equal(ex.nfev, nfev)
122
+ assert_array_equal(analit.nfev+approx.nfev, nfev)
123
+ assert_array_equal(analit.ngev+approx.ngev, ngev)
124
+ f_approx = approx.fun(x)
125
+ g_approx = approx.grad(x)
126
+ nfev += 3
127
+ ngev += 1
128
+ assert_array_equal(ex.nfev, nfev)
129
+ assert_array_equal(analit.nfev+approx.nfev, nfev)
130
+ assert_array_equal(analit.ngev+approx.ngev, ngev)
131
+ assert_array_almost_equal(f_analit, f_approx)
132
+ assert_array_almost_equal(g_analit, g_approx)
133
+
134
+ @pytest.mark.fail_slow(5.0)
135
+ def test_workers(self):
136
+ x0 = np.array([2.0, 0.3])
137
+ ex = ExScalarFunction()
138
+ ex2 = ExScalarFunction()
139
+ with MapWrapper(2) as mapper:
140
+ approx = ScalarFunction(ex.fun, x0, (), '2-point',
141
+ ex.hess, None, (-np.inf, np.inf),
142
+ workers=mapper)
143
+ approx_series = ScalarFunction(ex2.fun, x0, (), '2-point',
144
+ ex2.hess, None, (-np.inf, np.inf),
145
+ )
146
+ assert_allclose(approx.grad(x0), ex.grad(x0))
147
+ assert_allclose(approx_series.grad(x0), ex.grad(x0))
148
+ assert_allclose(approx_series.hess(x0), ex.hess(x0))
149
+ assert_allclose(approx.hess(x0), ex.hess(x0))
150
+ assert_equal(approx.nfev, approx_series.nfev)
151
+ assert_equal(approx_series.nfev, ex2.nfev)
152
+ assert_equal(approx.ngev, approx_series.ngev)
153
+ assert_equal(approx.nhev, approx_series.nhev)
154
+ assert_equal(approx_series.nhev, ex2.nhev)
155
+
156
+ ex = ExScalarFunction()
157
+ ex2 = ExScalarFunction()
158
+ approx = ScalarFunction(ex.fun, x0, (), '3-point',
159
+ ex.hess, None, (-np.inf, np.inf),
160
+ workers=mapper)
161
+ approx_series = ScalarFunction(ex2.fun, x0, (), '3-point',
162
+ ex2.hess, None, (-np.inf, np.inf),
163
+ )
164
+ assert_allclose(approx.grad(x0), ex.grad(x0))
165
+ assert_allclose(approx_series.grad(x0), ex.grad(x0))
166
+ assert_allclose(approx_series.hess(x0), ex.hess(x0))
167
+ assert_allclose(approx.hess(x0), ex.hess(x0))
168
+ assert_equal(approx.nfev, approx_series.nfev)
169
+ assert_equal(approx_series.nfev, ex2.nfev)
170
+ assert_equal(approx.ngev, approx_series.ngev)
171
+ assert_equal(approx.nhev, approx_series.nhev)
172
+ assert_equal(approx_series.nhev, ex2.nhev)
173
+
174
+ ex = ExScalarFunction()
175
+ ex2 = ExScalarFunction()
176
+ x1 = np.array([3.0, 4.0])
177
+
178
+ approx = ScalarFunction(ex.fun, x0, (), ex.grad,
179
+ '3-point', None, (-np.inf, np.inf),
180
+ workers=mapper)
181
+ approx_series = ScalarFunction(ex2.fun, x0, (), ex2.grad,
182
+ '3-point', None, (-np.inf, np.inf),
183
+ )
184
+ assert_allclose(approx.grad(x1), ex.grad(x1))
185
+ assert_allclose(approx_series.grad(x1), ex.grad(x1))
186
+ approx_series.hess(x1)
187
+ approx.hess(x1)
188
+ assert_equal(approx.nfev, approx_series.nfev)
189
+ assert_equal(approx_series.nfev, ex2.nfev)
190
+ assert_equal(approx.ngev, approx_series.ngev)
191
+ assert_equal(approx_series.ngev, ex2.ngev)
192
+ assert_equal(approx.nhev, approx_series.nhev)
193
+ assert_equal(approx_series.nhev, ex2.nhev)
194
+
195
+ def test_fun_and_grad(self):
196
+ ex = ExScalarFunction()
197
+
198
+ def fg_allclose(x, y):
199
+ assert_allclose(x[0], y[0])
200
+ assert_allclose(x[1], y[1])
201
+
202
+ # with analytic gradient
203
+ x0 = [2.0, 0.3]
204
+ analit = ScalarFunction(ex.fun, x0, (), ex.grad,
205
+ ex.hess, None, (-np.inf, np.inf))
206
+
207
+ fg = ex.fun(x0), ex.grad(x0)
208
+ fg_allclose(analit.fun_and_grad(x0), fg)
209
+ assert analit.ngev == 1
210
+
211
+ x0[1] = 1.
212
+ fg = ex.fun(x0), ex.grad(x0)
213
+ fg_allclose(analit.fun_and_grad(x0), fg)
214
+
215
+ # with finite difference gradient
216
+ x0 = [2.0, 0.3]
217
+ sf = ScalarFunction(ex.fun, x0, (), '3-point',
218
+ ex.hess, None, (-np.inf, np.inf))
219
+ assert sf.ngev == 1
220
+ fg = ex.fun(x0), ex.grad(x0)
221
+ fg_allclose(sf.fun_and_grad(x0), fg)
222
+ assert sf.ngev == 1
223
+
224
+ x0[1] = 1.
225
+ fg = ex.fun(x0), ex.grad(x0)
226
+ fg_allclose(sf.fun_and_grad(x0), fg)
227
+
228
+ def test_finite_difference_hess_linear_operator(self):
229
+ ex = ExScalarFunction()
230
+ nfev = 0
231
+ ngev = 0
232
+ nhev = 0
233
+
234
+ x0 = [1.0, 0.0]
235
+ analit = ScalarFunction(ex.fun, x0, (), ex.grad,
236
+ ex.hess, None, (-np.inf, np.inf))
237
+ nfev += 1
238
+ ngev += 1
239
+ nhev += 1
240
+ assert_array_equal(ex.nfev, nfev)
241
+ assert_array_equal(analit.nfev, nfev)
242
+ assert_array_equal(ex.ngev, ngev)
243
+ assert_array_equal(analit.ngev, ngev)
244
+ assert_array_equal(ex.nhev, nhev)
245
+ assert_array_equal(analit.nhev, nhev)
246
+ approx = ScalarFunction(ex.fun, x0, (), ex.grad,
247
+ '2-point', None, (-np.inf, np.inf))
248
+ assert_(isinstance(approx.H, LinearOperator))
249
+ for v in ([1.0, 2.0], [3.0, 4.0], [5.0, 2.0]):
250
+ assert_array_equal(analit.f, approx.f)
251
+ assert_array_almost_equal(analit.g, approx.g)
252
+ assert_array_almost_equal(analit.H.dot(v), approx.H.dot(v))
253
+ nfev += 1
254
+ ngev += 4
255
+ assert_array_equal(ex.nfev, nfev)
256
+ assert_array_equal(analit.nfev+approx.nfev, nfev)
257
+ assert_array_equal(ex.ngev, ngev)
258
+ assert_array_equal(analit.ngev+approx.ngev, ngev)
259
+ assert_array_equal(ex.nhev, nhev)
260
+ assert_array_equal(analit.nhev+approx.nhev, nhev)
261
+
262
+ x = [2.0, 1.0]
263
+ H_analit = analit.hess(x)
264
+ nhev += 1
265
+ assert_array_equal(ex.nfev, nfev)
266
+ assert_array_equal(analit.nfev+approx.nfev, nfev)
267
+ assert_array_equal(ex.ngev, ngev)
268
+ assert_array_equal(analit.ngev+approx.ngev, ngev)
269
+ assert_array_equal(ex.nhev, nhev)
270
+ assert_array_equal(analit.nhev+approx.nhev, nhev)
271
+ H_approx = approx.hess(x)
272
+ assert_(isinstance(H_approx, LinearOperator))
273
+ for v in ([1.0, 2.0], [3.0, 4.0], [5.0, 2.0]):
274
+ assert_array_almost_equal(H_analit.dot(v), H_approx.dot(v))
275
+ ngev += 4
276
+ assert_array_equal(ex.nfev, nfev)
277
+ assert_array_equal(analit.nfev+approx.nfev, nfev)
278
+ assert_array_equal(ex.ngev, ngev)
279
+ assert_array_equal(analit.ngev+approx.ngev, ngev)
280
+ assert_array_equal(ex.nhev, nhev)
281
+ assert_array_equal(analit.nhev+approx.nhev, nhev)
282
+
283
+ x = [2.1, 1.2]
284
+ H_analit = analit.hess(x)
285
+ nhev += 1
286
+ assert_array_equal(ex.nfev, nfev)
287
+ assert_array_equal(analit.nfev+approx.nfev, nfev)
288
+ assert_array_equal(ex.ngev, ngev)
289
+ assert_array_equal(analit.ngev+approx.ngev, ngev)
290
+ assert_array_equal(ex.nhev, nhev)
291
+ assert_array_equal(analit.nhev+approx.nhev, nhev)
292
+ H_approx = approx.hess(x)
293
+ assert_(isinstance(H_approx, LinearOperator))
294
+ for v in ([1.0, 2.0], [3.0, 4.0], [5.0, 2.0]):
295
+ assert_array_almost_equal(H_analit.dot(v), H_approx.dot(v))
296
+ ngev += 4
297
+ assert_array_equal(ex.nfev, nfev)
298
+ assert_array_equal(analit.nfev+approx.nfev, nfev)
299
+ assert_array_equal(ex.ngev, ngev)
300
+ assert_array_equal(analit.ngev+approx.ngev, ngev)
301
+ assert_array_equal(ex.nhev, nhev)
302
+ assert_array_equal(analit.nhev+approx.nhev, nhev)
303
+
304
+ x = [2.5, 0.3]
305
+ _ = analit.grad(x)
306
+ H_analit = analit.hess(x)
307
+ ngev += 1
308
+ nhev += 1
309
+ assert_array_equal(ex.nfev, nfev)
310
+ assert_array_equal(analit.nfev+approx.nfev, nfev)
311
+ assert_array_equal(ex.ngev, ngev)
312
+ assert_array_equal(analit.ngev+approx.ngev, ngev)
313
+ assert_array_equal(ex.nhev, nhev)
314
+ assert_array_equal(analit.nhev+approx.nhev, nhev)
315
+ _ = approx.grad(x)
316
+ H_approx = approx.hess(x)
317
+ assert_(isinstance(H_approx, LinearOperator))
318
+ for v in ([1.0, 2.0], [3.0, 4.0], [5.0, 2.0]):
319
+ assert_array_almost_equal(H_analit.dot(v), H_approx.dot(v))
320
+ ngev += 4
321
+ assert_array_equal(ex.nfev, nfev)
322
+ assert_array_equal(analit.nfev+approx.nfev, nfev)
323
+ assert_array_equal(ex.ngev, ngev)
324
+ assert_array_equal(analit.ngev+approx.ngev, ngev)
325
+ assert_array_equal(ex.nhev, nhev)
326
+ assert_array_equal(analit.nhev+approx.nhev, nhev)
327
+
328
+ x = [5.2, 2.3]
329
+ _ = analit.grad(x)
330
+ H_analit = analit.hess(x)
331
+ ngev += 1
332
+ nhev += 1
333
+ assert_array_equal(ex.nfev, nfev)
334
+ assert_array_equal(analit.nfev+approx.nfev, nfev)
335
+ assert_array_equal(ex.ngev, ngev)
336
+ assert_array_equal(analit.ngev+approx.ngev, ngev)
337
+ assert_array_equal(ex.nhev, nhev)
338
+ assert_array_equal(analit.nhev+approx.nhev, nhev)
339
+ _ = approx.grad(x)
340
+ H_approx = approx.hess(x)
341
+ assert_(isinstance(H_approx, LinearOperator))
342
+ for v in ([1.0, 2.0], [3.0, 4.0], [5.0, 2.0]):
343
+ assert_array_almost_equal(H_analit.dot(v), H_approx.dot(v))
344
+ ngev += 4
345
+ assert_array_equal(ex.nfev, nfev)
346
+ assert_array_equal(analit.nfev+approx.nfev, nfev)
347
+ assert_array_equal(ex.ngev, ngev)
348
+ assert_array_equal(analit.ngev+approx.ngev, ngev)
349
+ assert_array_equal(ex.nhev, nhev)
350
+ assert_array_equal(analit.nhev+approx.nhev, nhev)
351
+
352
+ def test_x_storage_overlap(self):
353
+ # Scalar_Function should not store references to arrays, it should
354
+ # store copies - this checks that updating an array in-place causes
355
+ # Scalar_Function.x to be updated.
356
+
357
+ def f(x):
358
+ return np.sum(np.asarray(x) ** 2)
359
+
360
+ x = np.array([1., 2., 3.])
361
+ sf = ScalarFunction(f, x, (), '3-point', lambda x: x, None, (-np.inf, np.inf))
362
+
363
+ assert x is not sf.x
364
+ assert_equal(sf.fun(x), 14.0)
365
+ assert x is not sf.x
366
+
367
+ x[0] = 0.
368
+ f1 = sf.fun(x)
369
+ assert_equal(f1, 13.0)
370
+
371
+ x[0] = 1
372
+ f2 = sf.fun(x)
373
+ assert_equal(f2, 14.0)
374
+ assert x is not sf.x
375
+
376
+ # now test with a HessianUpdate strategy specified
377
+ hess = BFGS()
378
+ x = np.array([1., 2., 3.])
379
+ sf = ScalarFunction(f, x, (), '3-point', hess, None, (-np.inf, np.inf))
380
+
381
+ assert x is not sf.x
382
+ assert_equal(sf.fun(x), 14.0)
383
+ assert x is not sf.x
384
+
385
+ x[0] = 0.
386
+ f1 = sf.fun(x)
387
+ assert_equal(f1, 13.0)
388
+
389
+ x[0] = 1
390
+ f2 = sf.fun(x)
391
+ assert_equal(f2, 14.0)
392
+ assert x is not sf.x
393
+
394
+ # gh13740 x is changed in user function
395
+ def ff(x):
396
+ x *= x # overwrite x
397
+ return np.sum(x)
398
+
399
+ x = np.array([1., 2., 3.])
400
+ sf = ScalarFunction(
401
+ ff, x, (), '3-point', lambda x: x, None, (-np.inf, np.inf)
402
+ )
403
+ assert x is not sf.x
404
+ assert_equal(sf.fun(x), 14.0)
405
+ assert_equal(sf.x, np.array([1., 2., 3.]))
406
+ assert x is not sf.x
407
+
408
+ def test_lowest_x(self):
409
+ # ScalarFunction should remember the lowest func(x) visited.
410
+ x0 = np.array([2, 3, 4])
411
+ sf = ScalarFunction(rosen, x0, (), rosen_der, rosen_hess,
412
+ None, None)
413
+ sf.fun([1, 1, 1])
414
+ sf.fun(x0)
415
+ sf.fun([1.01, 1, 1.0])
416
+ sf.grad([1.01, 1, 1.0])
417
+ assert_equal(sf._lowest_f, 0.0)
418
+ assert_equal(sf._lowest_x, [1.0, 1.0, 1.0])
419
+
420
+ sf = ScalarFunction(rosen, x0, (), '2-point', rosen_hess,
421
+ None, (-np.inf, np.inf))
422
+ sf.fun([1, 1, 1])
423
+ sf.fun(x0)
424
+ sf.fun([1.01, 1, 1.0])
425
+ sf.grad([1.01, 1, 1.0])
426
+ assert_equal(sf._lowest_f, 0.0)
427
+ assert_equal(sf._lowest_x, [1.0, 1.0, 1.0])
428
+
429
+ def test_float_size(self):
430
+ x0 = np.array([2, 3, 4]).astype(np.float32)
431
+
432
+ # check that ScalarFunction/approx_derivative always send the correct
433
+ # float width
434
+ def rosen_(x):
435
+ assert x.dtype == np.float32
436
+ return rosen(x)
437
+
438
+ sf = ScalarFunction(rosen_, x0, (), '2-point', rosen_hess,
439
+ None, (-np.inf, np.inf))
440
+ res = sf.fun(x0)
441
+ assert res.dtype == np.float32
442
+
443
+
444
+ class ExVectorialFunction:
445
+
446
+ def __init__(self):
447
+ self.nfev = 0
448
+ self.njev = 0
449
+ self.nhev = 0
450
+
451
+ def fun(self, x):
452
+ self.nfev += 1
453
+ return np.array([2*(x[0]**2 + x[1]**2 - 1) - x[0],
454
+ 4*(x[0]**3 + x[1]**2 - 4) - 3*x[0]], dtype=x.dtype)
455
+
456
+ def jac(self, x):
457
+ self.njev += 1
458
+ return np.array([[4*x[0]-1, 4*x[1]],
459
+ [12*x[0]**2-3, 8*x[1]]], dtype=x.dtype)
460
+
461
+ def hess(self, x, v):
462
+ self.nhev += 1
463
+ return v[0]*4*np.eye(2) + v[1]*np.array([[24*x[0], 0],
464
+ [0, 8]])
465
+
466
+
467
+ class TestVectorialFunction(TestCase):
468
+
469
+ def test_finite_difference_jac(self):
470
+ ex = ExVectorialFunction()
471
+ nfev = 0
472
+ njev = 0
473
+
474
+ x0 = [1.0, 0.0]
475
+ analit = VectorFunction(ex.fun, x0, ex.jac, ex.hess, None, None,
476
+ (-np.inf, np.inf), None)
477
+ nfev += 1
478
+ njev += 1
479
+ assert_array_equal(ex.nfev, nfev)
480
+ assert_array_equal(analit.nfev, nfev)
481
+ assert_array_equal(ex.njev, njev)
482
+ assert_array_equal(analit.njev, njev)
483
+ # create with defaults for the keyword arguments, to
484
+ # ensure that the defaults work
485
+ approx = VectorFunction(ex.fun, x0, '2-point', ex.hess)
486
+ nfev += 3
487
+ assert_array_equal(ex.nfev, nfev)
488
+ assert_array_equal(analit.nfev+approx.nfev, nfev)
489
+ assert_array_equal(ex.njev, njev)
490
+ assert_array_equal(analit.njev+approx.njev, njev)
491
+ assert_array_equal(analit.f, approx.f)
492
+ assert_array_almost_equal(analit.J, approx.J)
493
+
494
+ x = [10, 0.3]
495
+ f_analit = analit.fun(x)
496
+ J_analit = analit.jac(x)
497
+ nfev += 1
498
+ njev += 1
499
+ assert_array_equal(ex.nfev, nfev)
500
+ assert_array_equal(analit.nfev+approx.nfev, nfev)
501
+ assert_array_equal(ex.njev, njev)
502
+ assert_array_equal(analit.njev+approx.njev, njev)
503
+ f_approx = approx.fun(x)
504
+ J_approx = approx.jac(x)
505
+ nfev += 3
506
+ assert_array_equal(ex.nfev, nfev)
507
+ assert_array_equal(analit.nfev+approx.nfev, nfev)
508
+ assert_array_equal(ex.njev, njev)
509
+ assert_array_equal(analit.njev+approx.njev, njev)
510
+ assert_array_almost_equal(f_analit, f_approx)
511
+ assert_array_almost_equal(J_analit, J_approx, decimal=4)
512
+
513
+ x = [2.0, 1.0]
514
+ J_analit = analit.jac(x)
515
+ njev += 1
516
+ assert_array_equal(ex.nfev, nfev)
517
+ assert_array_equal(analit.nfev+approx.nfev, nfev)
518
+ assert_array_equal(ex.njev, njev)
519
+ assert_array_equal(analit.njev+approx.njev, njev)
520
+ J_approx = approx.jac(x)
521
+ nfev += 3
522
+ assert_array_equal(ex.nfev, nfev)
523
+ assert_array_equal(analit.nfev+approx.nfev, nfev)
524
+ assert_array_equal(ex.njev, njev)
525
+ assert_array_equal(analit.njev+approx.njev, njev)
526
+ assert_array_almost_equal(J_analit, J_approx)
527
+
528
+ x = [2.5, 0.3]
529
+ f_analit = analit.fun(x)
530
+ J_analit = analit.jac(x)
531
+ nfev += 1
532
+ njev += 1
533
+ assert_array_equal(ex.nfev, nfev)
534
+ assert_array_equal(analit.nfev+approx.nfev, nfev)
535
+ assert_array_equal(ex.njev, njev)
536
+ assert_array_equal(analit.njev+approx.njev, njev)
537
+ f_approx = approx.fun(x)
538
+ J_approx = approx.jac(x)
539
+ nfev += 3
540
+ assert_array_equal(ex.nfev, nfev)
541
+ assert_array_equal(analit.nfev+approx.nfev, nfev)
542
+ assert_array_equal(ex.njev, njev)
543
+ assert_array_equal(analit.njev+approx.njev, njev)
544
+ assert_array_almost_equal(f_analit, f_approx)
545
+ assert_array_almost_equal(J_analit, J_approx)
546
+
547
+ x = [2, 0.3]
548
+ f_analit = analit.fun(x)
549
+ J_analit = analit.jac(x)
550
+ nfev += 1
551
+ njev += 1
552
+ assert_array_equal(ex.nfev, nfev)
553
+ assert_array_equal(analit.nfev+approx.nfev, nfev)
554
+ assert_array_equal(ex.njev, njev)
555
+ assert_array_equal(analit.njev+approx.njev, njev)
556
+ f_approx = approx.fun(x)
557
+ J_approx = approx.jac(x)
558
+ nfev += 3
559
+ assert_array_equal(ex.nfev, nfev)
560
+ assert_array_equal(analit.nfev+approx.nfev, nfev)
561
+ assert_array_equal(ex.njev, njev)
562
+ assert_array_equal(analit.njev+approx.njev, njev)
563
+ assert_array_almost_equal(f_analit, f_approx)
564
+ assert_array_almost_equal(J_analit, J_approx)
565
+
566
+ def test_updating_on_initial_setup(self):
567
+ # Check that memoisation works with the freshly created VectorFunction
568
+ # On initialization vf.f_updated attribute wasn't being set correctly.
569
+ x0 = np.array([2.5, 3.0])
570
+ ex = ExVectorialFunction()
571
+ vf = VectorFunction(ex.fun, x0, ex.jac, ex.hess)
572
+ assert vf.f_updated
573
+ assert vf.nfev == 1
574
+ assert vf.njev == 1
575
+ assert ex.nfev == 1
576
+ assert ex.njev == 1
577
+ vf.fun(x0)
578
+ vf.jac(x0)
579
+ assert vf.nfev == 1
580
+ assert vf.njev == 1
581
+ assert ex.nfev == 1
582
+ assert ex.njev == 1
583
+
584
+ @pytest.mark.fail_slow(5.0)
585
+ def test_workers(self):
586
+ x0 = np.array([2.5, 3.0])
587
+ ex = ExVectorialFunction()
588
+ ex2 = ExVectorialFunction()
589
+ v = np.array([1.0, 2.0])
590
+
591
+ with MapWrapper(2) as mapper:
592
+ approx = VectorFunction(ex.fun, x0, '2-point',
593
+ ex.hess, None, None, (-np.inf, np.inf),
594
+ False, workers=mapper)
595
+ approx_series = VectorFunction(ex2.fun, x0, '2-point',
596
+ ex2.hess, None, None, (-np.inf, np.inf),
597
+ False)
598
+
599
+ assert_allclose(approx.jac(x0), ex.jac(x0))
600
+ assert_allclose(approx_series.jac(x0), ex.jac(x0))
601
+ assert_allclose(approx_series.hess(x0, v), ex.hess(x0, v))
602
+ assert_allclose(approx.hess(x0, v), ex.hess(x0, v))
603
+ assert_equal(approx.nfev, approx_series.nfev)
604
+ assert_equal(approx_series.nfev, ex2.nfev)
605
+ assert_equal(approx.njev, approx_series.njev)
606
+ assert_equal(approx.nhev, approx_series.nhev)
607
+ assert_equal(approx_series.nhev, ex2.nhev)
608
+
609
+ ex.nfev = ex.njev = ex.nhev = 0
610
+ ex2.nfev = ex2.njev = ex2.nhev = 0
611
+ approx = VectorFunction(ex.fun, x0, '3-point',
612
+ ex.hess, None, None, (-np.inf, np.inf),
613
+ False, workers=mapper)
614
+ approx_series = VectorFunction(ex2.fun, x0, '3-point',
615
+ ex2.hess, None, None, (-np.inf, np.inf),
616
+ False)
617
+ assert_allclose(approx.jac(x0), ex.jac(x0))
618
+ assert_allclose(approx_series.jac(x0), ex.jac(x0))
619
+ assert_allclose(approx_series.hess(x0, v), ex.hess(x0, v))
620
+ assert_allclose(approx.hess(x0, v), ex.hess(x0, v))
621
+
622
+ assert_equal(approx.nfev, approx_series.nfev)
623
+ assert_equal(approx_series.nfev, ex2.nfev)
624
+ assert_equal(approx.njev, approx_series.njev)
625
+ assert_equal(approx.nhev, approx_series.nhev)
626
+ assert_equal(approx_series.nhev, ex2.nhev)
627
+
628
+ # The following tests are somewhat redundant because the LinearOperator
629
+ # produced by VectorFunction.hess does not use any parallelisation.
630
+ # The tests are left for completeness, in case that situation changes.
631
+ ex.nfev = ex.njev = ex.nhev = 0
632
+ ex2.nfev = ex2.njev = ex2.nhev = 0
633
+ approx = VectorFunction(ex.fun, x0, ex.jac,
634
+ '2-point', None, None, (-np.inf, np.inf),
635
+ False, workers=mapper)
636
+ approx_series = VectorFunction(ex2.fun, x0, ex2.jac,
637
+ '2-point', None, None, (-np.inf, np.inf),
638
+ False)
639
+ assert_allclose(approx.jac(x0), ex.jac(x0))
640
+ assert_allclose(approx_series.jac(x0), ex.jac(x0))
641
+
642
+ H_analit = ex2.hess(x0, v)
643
+ H_approx_series = approx_series.hess(x0, v)
644
+ H_approx = approx.hess(x0, v)
645
+ x = [5, 2.0]
646
+ assert_allclose(H_approx.dot(x), H_analit.dot(x))
647
+ assert_allclose(H_approx_series.dot(x), H_analit.dot(x))
648
+
649
+ assert_equal(approx.nfev, approx_series.nfev)
650
+ assert_equal(approx_series.nfev, ex2.nfev)
651
+ assert_equal(approx.njev, approx_series.njev)
652
+ assert_equal(approx.nhev, approx_series.nhev)
653
+
654
+ def test_finite_difference_hess_linear_operator(self):
655
+ ex = ExVectorialFunction()
656
+ nfev = 0
657
+ njev = 0
658
+ nhev = 0
659
+
660
+ x0 = [1.0, 0.0]
661
+ v0 = [1.0, 2.0]
662
+ analit = VectorFunction(ex.fun, x0, ex.jac, ex.hess, None, None,
663
+ (-np.inf, np.inf), None)
664
+ nfev += 1
665
+ njev += 1
666
+ nhev += 1
667
+ assert_array_equal(ex.nfev, nfev)
668
+ assert_array_equal(analit.nfev, nfev)
669
+ assert_array_equal(ex.njev, njev)
670
+ assert_array_equal(analit.njev, njev)
671
+ assert_array_equal(ex.nhev, nhev)
672
+ assert_array_equal(analit.nhev, nhev)
673
+ approx = VectorFunction(ex.fun, x0, ex.jac, '2-point', None, None,
674
+ (-np.inf, np.inf), None)
675
+ assert_(isinstance(approx.H, LinearOperator))
676
+ for p in ([1.0, 2.0], [3.0, 4.0], [5.0, 2.0]):
677
+ assert_array_equal(analit.f, approx.f)
678
+ assert_array_almost_equal(analit.J, approx.J)
679
+ assert_array_almost_equal(analit.H.dot(p), approx.H.dot(p))
680
+ nfev += 1
681
+ njev += 4
682
+ assert_array_equal(ex.nfev, nfev)
683
+ assert_array_equal(analit.nfev+approx.nfev, nfev)
684
+ assert_array_equal(ex.njev, njev)
685
+ assert_array_equal(analit.njev+approx.njev, njev)
686
+ assert_array_equal(ex.nhev, nhev)
687
+ assert_array_equal(analit.nhev+approx.nhev, nhev)
688
+
689
+ x = [2.0, 1.0]
690
+ H_analit = analit.hess(x, v0)
691
+ nhev += 1
692
+ assert_array_equal(ex.nfev, nfev)
693
+ assert_array_equal(analit.nfev+approx.nfev, nfev)
694
+ assert_array_equal(ex.njev, njev)
695
+ assert_array_equal(analit.njev+approx.njev, njev)
696
+ assert_array_equal(ex.nhev, nhev)
697
+ assert_array_equal(analit.nhev+approx.nhev, nhev)
698
+ H_approx = approx.hess(x, v0)
699
+ assert_(isinstance(H_approx, LinearOperator))
700
+ for p in ([1.0, 2.0], [3.0, 4.0], [5.0, 2.0]):
701
+ assert_array_almost_equal(H_analit.dot(p), H_approx.dot(p),
702
+ decimal=5)
703
+ njev += 4
704
+ assert_array_equal(ex.nfev, nfev)
705
+ assert_array_equal(analit.nfev+approx.nfev, nfev)
706
+ assert_array_equal(ex.njev, njev)
707
+ assert_array_equal(analit.njev+approx.njev, njev)
708
+ assert_array_equal(ex.nhev, nhev)
709
+ assert_array_equal(analit.nhev+approx.nhev, nhev)
710
+
711
+ x = [2.1, 1.2]
712
+ v = [1.0, 1.0]
713
+ H_analit = analit.hess(x, v)
714
+ nhev += 1
715
+ assert_array_equal(ex.nfev, nfev)
716
+ assert_array_equal(analit.nfev+approx.nfev, nfev)
717
+ assert_array_equal(ex.njev, njev)
718
+ assert_array_equal(analit.njev+approx.njev, njev)
719
+ assert_array_equal(ex.nhev, nhev)
720
+ assert_array_equal(analit.nhev+approx.nhev, nhev)
721
+ H_approx = approx.hess(x, v)
722
+ assert_(isinstance(H_approx, LinearOperator))
723
+ for v in ([1.0, 2.0], [3.0, 4.0], [5.0, 2.0]):
724
+ assert_array_almost_equal(H_analit.dot(v), H_approx.dot(v))
725
+ njev += 4
726
+ assert_array_equal(ex.nfev, nfev)
727
+ assert_array_equal(analit.nfev+approx.nfev, nfev)
728
+ assert_array_equal(ex.njev, njev)
729
+ assert_array_equal(analit.njev+approx.njev, njev)
730
+ assert_array_equal(ex.nhev, nhev)
731
+ assert_array_equal(analit.nhev+approx.nhev, nhev)
732
+
733
+ x = [2.5, 0.3]
734
+ _ = analit.jac(x)
735
+ H_analit = analit.hess(x, v0)
736
+ njev += 1
737
+ nhev += 1
738
+ assert_array_equal(ex.nfev, nfev)
739
+ assert_array_equal(analit.nfev+approx.nfev, nfev)
740
+ assert_array_equal(ex.njev, njev)
741
+ assert_array_equal(analit.njev+approx.njev, njev)
742
+ assert_array_equal(ex.nhev, nhev)
743
+ assert_array_equal(analit.nhev+approx.nhev, nhev)
744
+ _ = approx.jac(x)
745
+ H_approx = approx.hess(x, v0)
746
+ assert_(isinstance(H_approx, LinearOperator))
747
+ for v in ([1.0, 2.0], [3.0, 4.0], [5.0, 2.0]):
748
+ assert_array_almost_equal(H_analit.dot(v), H_approx.dot(v), decimal=4)
749
+ njev += 4
750
+ assert_array_equal(ex.nfev, nfev)
751
+ assert_array_equal(analit.nfev+approx.nfev, nfev)
752
+ assert_array_equal(ex.njev, njev)
753
+ assert_array_equal(analit.njev+approx.njev, njev)
754
+ assert_array_equal(ex.nhev, nhev)
755
+ assert_array_equal(analit.nhev+approx.nhev, nhev)
756
+
757
+ x = [5.2, 2.3]
758
+ v = [2.3, 5.2]
759
+ _ = analit.jac(x)
760
+ H_analit = analit.hess(x, v)
761
+ njev += 1
762
+ nhev += 1
763
+ assert_array_equal(ex.nfev, nfev)
764
+ assert_array_equal(analit.nfev+approx.nfev, nfev)
765
+ assert_array_equal(ex.njev, njev)
766
+ assert_array_equal(analit.njev+approx.njev, njev)
767
+ assert_array_equal(ex.nhev, nhev)
768
+ assert_array_equal(analit.nhev+approx.nhev, nhev)
769
+ _ = approx.jac(x)
770
+ H_approx = approx.hess(x, v)
771
+ assert_(isinstance(H_approx, LinearOperator))
772
+ for v in ([1.0, 2.0], [3.0, 4.0], [5.0, 2.0]):
773
+ assert_array_almost_equal(H_analit.dot(v), H_approx.dot(v), decimal=4)
774
+ njev += 4
775
+ assert_array_equal(ex.nfev, nfev)
776
+ assert_array_equal(analit.nfev+approx.nfev, nfev)
777
+ assert_array_equal(ex.njev, njev)
778
+ assert_array_equal(analit.njev+approx.njev, njev)
779
+ assert_array_equal(ex.nhev, nhev)
780
+ assert_array_equal(analit.nhev+approx.nhev, nhev)
781
+
782
+ # Test VectorFunction.hess_wrapped with J0=None
783
+ x = np.array([1.5, 0.5])
784
+ v = np.array([1.0, 2.0])
785
+ njev_before = approx.hess_wrapped.njev
786
+ H = approx.hess_wrapped(x, v, J0=None)
787
+ assert isinstance(H, LinearOperator)
788
+ # The njev counter should be incremented by exactly 1
789
+ assert approx.hess_wrapped.njev == njev_before + 1
790
+
791
+ def test_fgh_overlap(self):
792
+ # VectorFunction.fun/jac should return copies to internal attributes
793
+ ex = ExVectorialFunction()
794
+ x0 = np.array([1.0, 0.0])
795
+
796
+ vf = VectorFunction(ex.fun, x0, '3-point', ex.hess, None, None,
797
+ (-np.inf, np.inf), None)
798
+ f = vf.fun(np.array([1.1, 0.1]))
799
+ J = vf.jac([1.1, 0.1])
800
+ assert vf.f is not f
801
+ assert vf.J is not J
802
+ assert_equal(f, vf.f)
803
+ assert_equal(J, vf.J)
804
+
805
+ vf = VectorFunction(ex.fun, x0, ex.jac, ex.hess, None, None,
806
+ (-np.inf, np.inf), None)
807
+ f = vf.fun(np.array([1.1, 0.1]))
808
+ J = vf.jac([1.1, 0.1])
809
+ assert vf.f is not f
810
+ assert vf.J is not J
811
+ assert_equal(f, vf.f)
812
+ assert_equal(J, vf.J)
813
+
814
+ def test_x_storage_overlap(self):
815
+ # VectorFunction should not store references to arrays, it should
816
+ # store copies - this checks that updating an array in-place causes
817
+ # Scalar_Function.x to be updated.
818
+ ex = ExVectorialFunction()
819
+ x0 = np.array([1.0, 0.0])
820
+
821
+ vf = VectorFunction(ex.fun, x0, '3-point', ex.hess, None, None,
822
+ (-np.inf, np.inf), None)
823
+
824
+ assert x0 is not vf.x
825
+ assert_equal(vf.fun(x0), ex.fun(x0))
826
+ assert x0 is not vf.x
827
+
828
+ x0[0] = 2.
829
+ assert_equal(vf.fun(x0), ex.fun(x0))
830
+ assert x0 is not vf.x
831
+
832
+ x0[0] = 1.
833
+ assert_equal(vf.fun(x0), ex.fun(x0))
834
+ assert x0 is not vf.x
835
+
836
+ # now test with a HessianUpdate strategy specified
837
+ hess = BFGS()
838
+ x0 = np.array([1.0, 0.0])
839
+ vf = VectorFunction(ex.fun, x0, '3-point', hess, None, None,
840
+ (-np.inf, np.inf), None)
841
+
842
+ with pytest.warns(UserWarning):
843
+ # filter UserWarning because ExVectorialFunction is linear and
844
+ # a quasi-Newton approximation is used for the Hessian.
845
+ assert x0 is not vf.x
846
+ assert_equal(vf.fun(x0), ex.fun(x0))
847
+ assert x0 is not vf.x
848
+
849
+ x0[0] = 2.
850
+ assert_equal(vf.fun(x0), ex.fun(x0))
851
+ assert x0 is not vf.x
852
+
853
+ x0[0] = 1.
854
+ assert_equal(vf.fun(x0), ex.fun(x0))
855
+ assert x0 is not vf.x
856
+
857
+ def test_float_size(self):
858
+ ex = ExVectorialFunction()
859
+ x0 = np.array([1.0, 0.0]).astype(np.float32)
860
+
861
+ vf = VectorFunction(ex.fun, x0, ex.jac, ex.hess, None, None,
862
+ (-np.inf, np.inf), None)
863
+
864
+ res = vf.fun(x0)
865
+ assert res.dtype == np.float32
866
+
867
+ res = vf.jac(x0)
868
+ assert res.dtype == np.float32
869
+
870
+ def test_sparse_analytic_jac(self):
871
+ ex = ExVectorialFunction()
872
+ x0 = np.array([1.0, 0.0])
873
+ def sparse_adapter(func):
874
+ def inner(x):
875
+ f_x = func(x)
876
+ return csr_array(f_x)
877
+ return inner
878
+
879
+ # jac(x) returns dense jacobian
880
+ vf1 = VectorFunction(ex.fun, x0, ex.jac, ex.hess, None, None,
881
+ (-np.inf, np.inf), sparse_jacobian=None)
882
+ # jac(x) returns sparse jacobian, but sparse_jacobian=False requests dense
883
+ vf2 = VectorFunction(ex.fun, x0, sparse_adapter(ex.jac), ex.hess, None, None,
884
+ (-np.inf, np.inf), sparse_jacobian=False)
885
+
886
+ res1 = vf1.jac(x0 + 1)
887
+ res2 = vf2.jac(x0 + 1)
888
+ assert_equal(res1, res2)
889
+
890
+ def test_sparse_numerical_jac(self):
891
+ ex = ExVectorialFunction()
892
+ x0 = np.array([1.0, 0.0])
893
+ N = len(x0)
894
+
895
+ # normal dense numerical difference
896
+ vf1 = VectorFunction(ex.fun, x0, '2-point', ex.hess, None, None,
897
+ (-np.inf, np.inf), sparse_jacobian=None)
898
+ # use sparse numerical difference, but ask it to be converted to dense
899
+ finite_diff_jac_sparsity = csr_array(np.ones((N, N)))
900
+ vf2 = VectorFunction(ex.fun, x0, '2-point', ex.hess, None,
901
+ finite_diff_jac_sparsity, (-np.inf, np.inf),
902
+ sparse_jacobian=False)
903
+
904
+ res1 = vf1.jac(x0 + 1)
905
+ res2 = vf2.jac(x0 + 1)
906
+ assert_equal(res1, res2)
907
+
908
+
909
+ def test_LinearVectorFunction():
910
+ A_dense = np.array([
911
+ [-1, 2, 0],
912
+ [0, 4, 2]
913
+ ])
914
+ x0 = np.zeros(3)
915
+ A_sparse = csr_array(A_dense)
916
+ x = np.array([1, -1, 0])
917
+ v = np.array([-1, 1])
918
+ Ax = np.array([-3, -4])
919
+
920
+ f1 = LinearVectorFunction(A_dense, x0, None)
921
+ assert_(not f1.sparse_jacobian)
922
+
923
+ f2 = LinearVectorFunction(A_dense, x0, True)
924
+ assert_(f2.sparse_jacobian)
925
+
926
+ f3 = LinearVectorFunction(A_dense, x0, False)
927
+ assert_(not f3.sparse_jacobian)
928
+
929
+ f4 = LinearVectorFunction(A_sparse, x0, None)
930
+ assert_(f4.sparse_jacobian)
931
+
932
+ f5 = LinearVectorFunction(A_sparse, x0, True)
933
+ assert_(f5.sparse_jacobian)
934
+
935
+ f6 = LinearVectorFunction(A_sparse, x0, False)
936
+ assert_(not f6.sparse_jacobian)
937
+
938
+ assert_array_equal(f1.fun(x), Ax)
939
+ assert_array_equal(f2.fun(x), Ax)
940
+ assert_array_equal(f1.jac(x), A_dense)
941
+ assert_array_equal(f2.jac(x).toarray(), A_sparse.toarray())
942
+ assert_array_equal(f1.hess(x, v).toarray(), np.zeros((3, 3)))
943
+
944
+
945
+ def test_LinearVectorFunction_memoization():
946
+ A = np.array([[-1, 2, 0], [0, 4, 2]])
947
+ x0 = np.array([1, 2, -1])
948
+ fun = LinearVectorFunction(A, x0, False)
949
+
950
+ assert_array_equal(x0, fun.x)
951
+ assert_array_equal(A.dot(x0), fun.f)
952
+
953
+ x1 = np.array([-1, 3, 10])
954
+ assert_array_equal(A, fun.jac(x1))
955
+ assert_array_equal(x1, fun.x)
956
+ assert_array_equal(A.dot(x0), fun.f)
957
+ assert_array_equal(A.dot(x1), fun.fun(x1))
958
+ assert_array_equal(A.dot(x1), fun.f)
959
+
960
+
961
+ def test_IdentityVectorFunction():
962
+ x0 = np.zeros(3)
963
+
964
+ f1 = IdentityVectorFunction(x0, None)
965
+ f2 = IdentityVectorFunction(x0, False)
966
+ f3 = IdentityVectorFunction(x0, True)
967
+
968
+ assert_(f1.sparse_jacobian)
969
+ assert_(not f2.sparse_jacobian)
970
+ assert_(f3.sparse_jacobian)
971
+
972
+ x = np.array([-1, 2, 1])
973
+ v = np.array([-2, 3, 0])
974
+
975
+ assert_array_equal(f1.fun(x), x)
976
+ assert_array_equal(f2.fun(x), x)
977
+
978
+ assert_array_equal(f1.jac(x).toarray(), np.eye(3))
979
+ assert_array_equal(f2.jac(x), np.eye(3))
980
+
981
+ assert_array_equal(f1.hess(x, v).toarray(), np.zeros((3, 3)))
982
+
983
+
984
+ @pytest.mark.skipif(
985
+ platform.python_implementation() == "PyPy",
986
+ reason="assert_deallocate not available on PyPy"
987
+ )
988
+ def test_ScalarFunctionNoReferenceCycle():
989
+ """Regression test for gh-20768."""
990
+ ex = ExScalarFunction()
991
+ x0 = np.zeros(3)
992
+ with assert_deallocated(lambda: ScalarFunction(ex.fun, x0, (), ex.grad,
993
+ ex.hess, None, (-np.inf, np.inf))):
994
+ pass
995
+
996
+
997
+ @pytest.mark.skipif(
998
+ platform.python_implementation() == "PyPy",
999
+ reason="assert_deallocate not available on PyPy"
1000
+ )
1001
+ def test_VectorFunctionNoReferenceCycle():
1002
+ """Regression test for gh-20768."""
1003
+ ex = ExVectorialFunction()
1004
+ x0 = [1.0, 0.0]
1005
+ with assert_deallocated(lambda: VectorFunction(ex.fun, x0, ex.jac,
1006
+ ex.hess, None, None, (-np.inf, np.inf), None)):
1007
+ pass
1008
+
1009
+
1010
+ @pytest.mark.skipif(
1011
+ platform.python_implementation() == "PyPy",
1012
+ reason="assert_deallocate not available on PyPy"
1013
+ )
1014
+ def test_LinearVectorFunctionNoReferenceCycle():
1015
+ """Regression test for gh-20768."""
1016
+ A_dense = np.array([
1017
+ [-1, 2, 0],
1018
+ [0, 4, 2]
1019
+ ])
1020
+ x0 = np.zeros(3)
1021
+ A_sparse = csr_array(A_dense)
1022
+ with assert_deallocated(lambda: LinearVectorFunction(A_sparse, x0, None)):
1023
+ pass
loan-advisor-agent/.venv/Lib/site-packages/scipy/optimize/tests/test_direct.py ADDED
@@ -0,0 +1,321 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Unit test for DIRECT optimization algorithm.
3
+ """
4
+ from numpy.testing import (assert_allclose,
5
+ assert_array_less)
6
+ import pytest
7
+ import numpy as np
8
+ from scipy.optimize import direct, Bounds
9
+ import threading
10
+
11
+
12
+ class TestDIRECT:
13
+
14
+ def setup_method(self):
15
+ self.fun_calls = threading.local()
16
+ self.bounds_sphere = 4*[(-2, 3)]
17
+ self.optimum_sphere_pos = np.zeros((4, ))
18
+ self.optimum_sphere = 0.0
19
+ self.bounds_stylinski_tang = Bounds([-4., -4.], [4., 4.])
20
+ self.maxiter = 1000
21
+
22
+ # test functions
23
+ def sphere(self, x):
24
+ if not hasattr(self.fun_calls, 'c'):
25
+ self.fun_calls.c = 0
26
+ self.fun_calls.c += 1
27
+ return np.square(x).sum()
28
+
29
+ def inv(self, x):
30
+ if np.sum(x) == 0:
31
+ raise ZeroDivisionError()
32
+ return 1/np.sum(x)
33
+
34
+ def nan_fun(self, x):
35
+ return np.nan
36
+
37
+ def inf_fun(self, x):
38
+ return np.inf
39
+
40
+ def styblinski_tang(self, pos):
41
+ x, y = pos
42
+ return 0.5 * (x**4 - 16 * x**2 + 5 * x + y**4 - 16 * y**2 + 5 * y)
43
+
44
+ @pytest.mark.parametrize("locally_biased", [True, False])
45
+ def test_direct(self, locally_biased):
46
+ res = direct(self.sphere, self.bounds_sphere,
47
+ locally_biased=locally_biased)
48
+
49
+ # test accuracy
50
+ assert_allclose(res.x, self.optimum_sphere_pos,
51
+ rtol=1e-3, atol=1e-3)
52
+ assert_allclose(res.fun, self.optimum_sphere, atol=1e-5, rtol=1e-5)
53
+
54
+ # test that result lies within bounds
55
+ _bounds = np.asarray(self.bounds_sphere)
56
+ assert_array_less(_bounds[:, 0], res.x)
57
+ assert_array_less(res.x, _bounds[:, 1])
58
+
59
+ # test number of function evaluations. Original DIRECT overshoots by
60
+ # up to 500 evaluations in last iteration
61
+ assert res.nfev <= 1000 * (len(self.bounds_sphere) + 1)
62
+ # test that number of function evaluations is correct
63
+ assert res.nfev == self.fun_calls.c
64
+
65
+ # test that number of iterations is below supplied maximum
66
+ assert res.nit <= self.maxiter
67
+
68
+ @pytest.mark.parametrize("locally_biased", [True, False])
69
+ def test_direct_callback(self, locally_biased):
70
+ # test that callback does not change the result
71
+ res = direct(self.sphere, self.bounds_sphere,
72
+ locally_biased=locally_biased)
73
+
74
+ def callback(x):
75
+ x = 2*x
76
+ dummy = np.square(x)
77
+ print("DIRECT minimization algorithm callback test")
78
+ return dummy
79
+
80
+ res_callback = direct(self.sphere, self.bounds_sphere,
81
+ locally_biased=locally_biased,
82
+ callback=callback)
83
+
84
+ assert_allclose(res.x, res_callback.x)
85
+
86
+ assert res.nit == res_callback.nit
87
+ assert res.nfev == res_callback.nfev
88
+ assert res.status == res_callback.status
89
+ assert res.success == res_callback.success
90
+ assert res.fun == res_callback.fun
91
+ assert_allclose(res.x, res_callback.x)
92
+ assert res.message == res_callback.message
93
+
94
+ # test accuracy
95
+ assert_allclose(res_callback.x, self.optimum_sphere_pos,
96
+ rtol=1e-3, atol=1e-3)
97
+ assert_allclose(res_callback.fun, self.optimum_sphere,
98
+ atol=1e-5, rtol=1e-5)
99
+
100
+ @pytest.mark.parametrize("locally_biased", [True, False])
101
+ def test_exception(self, locally_biased):
102
+ bounds = 4*[(-10, 10)]
103
+ with pytest.raises(ZeroDivisionError):
104
+ direct(self.inv, bounds=bounds,
105
+ locally_biased=locally_biased)
106
+
107
+ @pytest.mark.parametrize("locally_biased", [True, False])
108
+ def test_nan(self, locally_biased):
109
+ bounds = 4*[(-10, 10)]
110
+ direct(self.nan_fun, bounds=bounds,
111
+ locally_biased=locally_biased)
112
+
113
+ @pytest.mark.parametrize("len_tol", [1e-3, 1e-4])
114
+ @pytest.mark.parametrize("locally_biased", [True, False])
115
+ def test_len_tol(self, len_tol, locally_biased):
116
+ bounds = 4*[(-10., 10.)]
117
+ res = direct(self.sphere, bounds=bounds, len_tol=len_tol,
118
+ vol_tol=1e-30, locally_biased=locally_biased)
119
+ assert res.status == 5
120
+ assert res.success
121
+ assert_allclose(res.x, np.zeros((4, )))
122
+ message = ("The side length measure of the hyperrectangle containing "
123
+ "the lowest function value found is below "
124
+ f"len_tol={len_tol}")
125
+ assert res.message == message
126
+
127
+ @pytest.mark.parametrize("vol_tol", [1e-6, 1e-8])
128
+ @pytest.mark.parametrize("locally_biased", [True, False])
129
+ def test_vol_tol(self, vol_tol, locally_biased):
130
+ bounds = 4*[(-10., 10.)]
131
+ res = direct(self.sphere, bounds=bounds, vol_tol=vol_tol,
132
+ len_tol=0., locally_biased=locally_biased)
133
+ assert res.status == 4
134
+ assert res.success
135
+ assert_allclose(res.x, np.zeros((4, )))
136
+ message = ("The volume of the hyperrectangle containing the lowest "
137
+ f"function value found is below vol_tol={vol_tol}")
138
+ assert res.message == message
139
+
140
+ @pytest.mark.parametrize("f_min_rtol", [1e-3, 1e-5, 1e-7])
141
+ @pytest.mark.parametrize("locally_biased", [True, False])
142
+ def test_f_min(self, f_min_rtol, locally_biased):
143
+ # test that desired function value is reached within
144
+ # relative tolerance of f_min_rtol
145
+ f_min = 1.
146
+ bounds = 4*[(-2., 10.)]
147
+ res = direct(self.sphere, bounds=bounds, f_min=f_min,
148
+ f_min_rtol=f_min_rtol,
149
+ locally_biased=locally_biased)
150
+ assert res.status == 3
151
+ assert res.success
152
+ assert res.fun < f_min * (1. + f_min_rtol)
153
+ message = ("The best function value found is within a relative "
154
+ f"error={f_min_rtol} of the (known) global optimum f_min")
155
+ assert res.message == message
156
+
157
+ def circle_with_args(self, x, a, b):
158
+ return np.square(x[0] - a) + np.square(x[1] - b).sum()
159
+
160
+ @pytest.mark.parametrize("locally_biased", [True, False])
161
+ def test_f_circle_with_args(self, locally_biased):
162
+ bounds = 2*[(-2.0, 2.0)]
163
+
164
+ res = direct(self.circle_with_args, bounds, args=(1, 1), maxfun=1250,
165
+ locally_biased=locally_biased)
166
+ assert_allclose(res.x, np.array([1., 1.]), rtol=1e-5)
167
+
168
+ @pytest.mark.parametrize("locally_biased", [True, False])
169
+ def test_failure_maxfun(self, locally_biased):
170
+ # test that if optimization runs for the maximal number of
171
+ # evaluations, success = False is returned
172
+
173
+ maxfun = 100
174
+ result = direct(self.styblinski_tang, self.bounds_stylinski_tang,
175
+ maxfun=maxfun, locally_biased=locally_biased)
176
+ assert result.success is False
177
+ assert result.status == 1
178
+ assert result.nfev >= maxfun
179
+ message = ("Number of function evaluations done is "
180
+ f"larger than maxfun={maxfun}")
181
+ assert result.message == message
182
+
183
+ @pytest.mark.parametrize("locally_biased", [True, False])
184
+ def test_failure_maxiter(self, locally_biased):
185
+ # test that if optimization runs for the maximal number of
186
+ # iterations, success = False is returned
187
+
188
+ maxiter = 10
189
+ result = direct(self.styblinski_tang, self.bounds_stylinski_tang,
190
+ maxiter=maxiter, locally_biased=locally_biased)
191
+ assert result.success is False
192
+ assert result.status == 2
193
+ assert result.nit >= maxiter
194
+ message = f"Number of iterations is larger than maxiter={maxiter}"
195
+ assert result.message == message
196
+
197
+ @pytest.mark.parametrize("locally_biased", [True, False])
198
+ def test_bounds_variants(self, locally_biased):
199
+ # test that new and old bounds yield same result
200
+
201
+ lb = [-6., 1., -5.]
202
+ ub = [-1., 3., 5.]
203
+ x_opt = np.array([-1., 1., 0.])
204
+ bounds_old = list(zip(lb, ub))
205
+ bounds_new = Bounds(lb, ub)
206
+
207
+ res_old_bounds = direct(self.sphere, bounds_old,
208
+ locally_biased=locally_biased)
209
+ res_new_bounds = direct(self.sphere, bounds_new,
210
+ locally_biased=locally_biased)
211
+
212
+ assert res_new_bounds.nfev == res_old_bounds.nfev
213
+ assert res_new_bounds.message == res_old_bounds.message
214
+ assert res_new_bounds.success == res_old_bounds.success
215
+ assert res_new_bounds.nit == res_old_bounds.nit
216
+ assert_allclose(res_new_bounds.x, res_old_bounds.x)
217
+ assert_allclose(res_new_bounds.x, x_opt, rtol=1e-2)
218
+
219
+ @pytest.mark.parametrize("locally_biased", [True, False])
220
+ @pytest.mark.parametrize("eps", [1e-5, 1e-4, 1e-3])
221
+ def test_epsilon(self, eps, locally_biased):
222
+ result = direct(self.styblinski_tang, self.bounds_stylinski_tang,
223
+ eps=eps, vol_tol=1e-6,
224
+ locally_biased=locally_biased)
225
+ assert result.status == 4
226
+ assert result.success
227
+
228
+ @pytest.mark.xslow
229
+ @pytest.mark.parametrize("locally_biased", [True, False])
230
+ def test_no_segmentation_fault(self, locally_biased):
231
+ # test that an excessive number of function evaluations
232
+ # does not result in segmentation fault
233
+ bounds = [(-5., 20.)] * 100
234
+ result = direct(self.sphere, bounds, maxfun=10000000,
235
+ maxiter=1000000, locally_biased=locally_biased)
236
+ assert result is not None
237
+
238
+ @pytest.mark.parametrize("locally_biased", [True, False])
239
+ def test_inf_fun(self, locally_biased):
240
+ # test that an objective value of infinity does not crash DIRECT
241
+ bounds = [(-5., 5.)] * 2
242
+ result = direct(self.inf_fun, bounds,
243
+ locally_biased=locally_biased)
244
+ assert result is not None
245
+
246
+ @pytest.mark.parametrize("len_tol", [-1, 2])
247
+ def test_len_tol_validation(self, len_tol):
248
+ error_msg = "len_tol must be between 0 and 1."
249
+ with pytest.raises(ValueError, match=error_msg):
250
+ direct(self.styblinski_tang, self.bounds_stylinski_tang,
251
+ len_tol=len_tol)
252
+
253
+ @pytest.mark.parametrize("vol_tol", [-1, 2])
254
+ def test_vol_tol_validation(self, vol_tol):
255
+ error_msg = "vol_tol must be between 0 and 1."
256
+ with pytest.raises(ValueError, match=error_msg):
257
+ direct(self.styblinski_tang, self.bounds_stylinski_tang,
258
+ vol_tol=vol_tol)
259
+
260
+ @pytest.mark.parametrize("f_min_rtol", [-1, 2])
261
+ def test_fmin_rtol_validation(self, f_min_rtol):
262
+ error_msg = "f_min_rtol must be between 0 and 1."
263
+ with pytest.raises(ValueError, match=error_msg):
264
+ direct(self.styblinski_tang, self.bounds_stylinski_tang,
265
+ f_min_rtol=f_min_rtol, f_min=0.)
266
+
267
+ @pytest.mark.parametrize("maxfun", [1.5, "string", (1, 2)])
268
+ def test_maxfun_wrong_type(self, maxfun):
269
+ error_msg = "maxfun must be of type int."
270
+ with pytest.raises(ValueError, match=error_msg):
271
+ direct(self.styblinski_tang, self.bounds_stylinski_tang,
272
+ maxfun=maxfun)
273
+
274
+ @pytest.mark.parametrize("maxiter", [1.5, "string", (1, 2)])
275
+ def test_maxiter_wrong_type(self, maxiter):
276
+ error_msg = "maxiter must be of type int."
277
+ with pytest.raises(ValueError, match=error_msg):
278
+ direct(self.styblinski_tang, self.bounds_stylinski_tang,
279
+ maxiter=maxiter)
280
+
281
+ def test_negative_maxiter(self):
282
+ error_msg = "maxiter must be > 0."
283
+ with pytest.raises(ValueError, match=error_msg):
284
+ direct(self.styblinski_tang, self.bounds_stylinski_tang,
285
+ maxiter=-1)
286
+
287
+ def test_negative_maxfun(self):
288
+ error_msg = "maxfun must be > 0."
289
+ with pytest.raises(ValueError, match=error_msg):
290
+ direct(self.styblinski_tang, self.bounds_stylinski_tang,
291
+ maxfun=-1)
292
+
293
+ @pytest.mark.parametrize("bounds", ["bounds", 2., 0])
294
+ def test_invalid_bounds_type(self, bounds):
295
+ error_msg = ("bounds must be a sequence or "
296
+ "instance of Bounds class")
297
+ with pytest.raises(ValueError, match=error_msg):
298
+ direct(self.styblinski_tang, bounds)
299
+
300
+ @pytest.mark.parametrize("bounds",
301
+ [Bounds([-1., -1], [-2, 1]),
302
+ Bounds([-np.nan, -1], [-2, np.nan]),
303
+ ]
304
+ )
305
+ def test_incorrect_bounds(self, bounds):
306
+ error_msg = 'Bounds are not consistent min < max'
307
+ with pytest.raises(ValueError, match=error_msg):
308
+ direct(self.styblinski_tang, bounds)
309
+
310
+ def test_inf_bounds(self):
311
+ error_msg = 'Bounds must not be inf.'
312
+ bounds = Bounds([-np.inf, -1], [-2, np.inf])
313
+ with pytest.raises(ValueError, match=error_msg):
314
+ direct(self.styblinski_tang, bounds)
315
+
316
+ @pytest.mark.parametrize("locally_biased", ["bias", [0, 0], 2.])
317
+ def test_locally_biased_validation(self, locally_biased):
318
+ error_msg = 'locally_biased must be True or False.'
319
+ with pytest.raises(ValueError, match=error_msg):
320
+ direct(self.styblinski_tang, self.bounds_stylinski_tang,
321
+ locally_biased=locally_biased)
loan-advisor-agent/.venv/Lib/site-packages/scipy/optimize/tests/test_extending.py ADDED
@@ -0,0 +1,28 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import platform
3
+ import sysconfig
4
+
5
+ import pytest
6
+
7
+ from scipy._lib._testutils import IS_EDITABLE, _test_cython_extension, cython
8
+
9
+
10
+ @pytest.mark.fail_slow(40)
11
+ # essential per https://github.com/scipy/scipy/pull/20487#discussion_r1567057247
12
+ @pytest.mark.skipif(IS_EDITABLE,
13
+ reason='Editable install cannot find .pxd headers.')
14
+ @pytest.mark.skipif((platform.system() == 'Windows' and
15
+ sysconfig.get_config_var('Py_GIL_DISABLED')),
16
+ reason='gh-22039')
17
+ @pytest.mark.skipif(platform.machine() in ["wasm32", "wasm64"],
18
+ reason="Can't start subprocess")
19
+ @pytest.mark.skipif(cython is None, reason="requires cython")
20
+ def test_cython(tmp_path):
21
+ srcdir = os.path.dirname(os.path.dirname(__file__))
22
+ extensions, extensions_cpp = _test_cython_extension(tmp_path, srcdir)
23
+ # actually test the cython c-extensions
24
+ # From docstring for scipy.optimize.cython_optimize module
25
+ x = extensions.brentq_example()
26
+ assert x == 0.6999942848231314
27
+ x = extensions_cpp.brentq_example()
28
+ assert x == 0.6999942848231314
loan-advisor-agent/.venv/Lib/site-packages/scipy/optimize/tests/test_hessian_update_strategy.py ADDED
@@ -0,0 +1,300 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import re
2
+ from copy import deepcopy
3
+
4
+ import numpy as np
5
+ import pytest
6
+ from numpy.linalg import norm
7
+ from numpy.testing import (TestCase, assert_array_almost_equal,
8
+ assert_array_equal, assert_array_less)
9
+ from scipy.optimize import (BFGS, SR1)
10
+
11
+
12
+ class Rosenbrock:
13
+ """Rosenbrock function.
14
+
15
+ The following optimization problem:
16
+ minimize sum(100.0*(x[1:] - x[:-1]**2.0)**2.0 + (1 - x[:-1])**2.0)
17
+ """
18
+
19
+ def __init__(self, n=2, random_state=0):
20
+ rng = np.random.RandomState(random_state)
21
+ self.x0 = rng.uniform(-1, 1, n)
22
+ self.x_opt = np.ones(n)
23
+
24
+ def fun(self, x):
25
+ x = np.asarray(x)
26
+ r = np.sum(100.0 * (x[1:] - x[:-1]**2.0)**2.0 + (1 - x[:-1])**2.0,
27
+ axis=0)
28
+ return r
29
+
30
+ def grad(self, x):
31
+ x = np.asarray(x)
32
+ xm = x[1:-1]
33
+ xm_m1 = x[:-2]
34
+ xm_p1 = x[2:]
35
+ der = np.zeros_like(x)
36
+ der[1:-1] = (200 * (xm - xm_m1**2) -
37
+ 400 * (xm_p1 - xm**2) * xm - 2 * (1 - xm))
38
+ der[0] = -400 * x[0] * (x[1] - x[0]**2) - 2 * (1 - x[0])
39
+ der[-1] = 200 * (x[-1] - x[-2]**2)
40
+ return der
41
+
42
+ def hess(self, x):
43
+ x = np.atleast_1d(x)
44
+ H = np.diag(-400 * x[:-1], 1) - np.diag(400 * x[:-1], -1)
45
+ diagonal = np.zeros(len(x), dtype=x.dtype)
46
+ diagonal[0] = 1200 * x[0]**2 - 400 * x[1] + 2
47
+ diagonal[-1] = 200
48
+ diagonal[1:-1] = 202 + 1200 * x[1:-1]**2 - 400 * x[2:]
49
+ H = H + np.diag(diagonal)
50
+ return H
51
+
52
+
53
+ class TestHessianUpdateStrategy(TestCase):
54
+
55
+
56
+ def test_hessian_initialization(self):
57
+
58
+ ndims = 5
59
+ symmetric_matrix = np.array([[43, 24, 33, 34, 49],
60
+ [24, 36, 44, 15, 44],
61
+ [33, 44, 37, 1, 30],
62
+ [34, 15, 1, 5, 46],
63
+ [49, 44, 30, 46, 22]])
64
+ init_scales = (
65
+ ('auto', np.eye(ndims)),
66
+ (2, np.eye(ndims) * 2),
67
+ (np.arange(1, ndims + 1) * np.eye(ndims),
68
+ np.arange(1, ndims + 1) * np.eye(ndims)),
69
+ (symmetric_matrix, symmetric_matrix),)
70
+ for approx_type in ['hess', 'inv_hess']:
71
+ for init_scale, true_matrix in init_scales:
72
+ # large min_{denominator,curvatur} makes them skip an update,
73
+ # so we can have our initial matrix
74
+ quasi_newton = (BFGS(init_scale=init_scale,
75
+ min_curvature=1e50,
76
+ exception_strategy='skip_update'),
77
+ SR1(init_scale=init_scale,
78
+ min_denominator=1e50))
79
+
80
+ for qn in quasi_newton:
81
+ qn.initialize(ndims, approx_type)
82
+ B = qn.get_matrix()
83
+
84
+ assert_array_equal(B, np.eye(ndims))
85
+ # don't test the auto init scale
86
+ if isinstance(init_scale, str) and init_scale == 'auto':
87
+ continue
88
+
89
+ qn.update(np.ones(ndims) * 1e-5, np.arange(ndims) + 0.2)
90
+ B = qn.get_matrix()
91
+ assert_array_equal(B, true_matrix)
92
+
93
+ # For this list of points, it is known
94
+ # that no exception occur during the
95
+ # Hessian update. Hence no update is
96
+ # skiped or damped.
97
+
98
+
99
+ def test_initialize_catch_illegal(self):
100
+ ndims = 3
101
+ # no complex allowed
102
+ inits_msg_errtype = ((complex(3.14),
103
+ r"float\(\) argument must be a string or a "
104
+ r"(real )?number, not 'complex'",
105
+ TypeError),
106
+
107
+ (np.array([3.2, 2.3, 1.2]).astype(np.complex128),
108
+ "init_scale contains complex elements, "
109
+ "must be real.",
110
+ TypeError),
111
+
112
+ (np.array([[43, 24, 33],
113
+ [24, 36, 44, ],
114
+ [33, 44, 37, ]]).astype(np.complex128),
115
+ "init_scale contains complex elements, "
116
+ "must be real.",
117
+ TypeError),
118
+
119
+ # not square
120
+ (np.array([[43, 55, 66]]),
121
+ re.escape(
122
+ "If init_scale is an array, it must have the "
123
+ "dimensions of the hess/inv_hess: (3, 3)."
124
+ " Got (1, 3)."),
125
+ ValueError),
126
+
127
+ # not symmetric
128
+ (np.array([[43, 24, 33],
129
+ [24.1, 36, 44, ],
130
+ [33, 44, 37, ]]),
131
+ re.escape("If init_scale is an array, it must be"
132
+ " symmetric (passing scipy.linalg.issymmetric)"
133
+ " to be an approximation of a hess/inv_hess."),
134
+ ValueError),
135
+ )
136
+ for approx_type in ['hess', 'inv_hess']:
137
+ for init_scale, message, errortype in inits_msg_errtype:
138
+ # large min_{denominator,curvatur} makes it skip an update,
139
+ # so we can retrieve our initial matrix
140
+ quasi_newton = (BFGS(init_scale=init_scale),
141
+ SR1(init_scale=init_scale))
142
+
143
+ for qn in quasi_newton:
144
+ qn.initialize(ndims, approx_type)
145
+ with pytest.raises(errortype, match=message):
146
+ qn.update(np.ones(ndims), np.arange(ndims))
147
+
148
+ def test_rosenbrock_with_no_exception(self):
149
+ # Define auxiliary problem
150
+ prob = Rosenbrock(n=5)
151
+ # Define iteration points
152
+ x_list = [[0.0976270, 0.4303787, 0.2055267, 0.0897663, -0.15269040],
153
+ [0.1847239, 0.0505757, 0.2123832, 0.0255081, 0.00083286],
154
+ [0.2142498, -0.0188480, 0.0503822, 0.0347033, 0.03323606],
155
+ [0.2071680, -0.0185071, 0.0341337, -0.0139298, 0.02881750],
156
+ [0.1533055, -0.0322935, 0.0280418, -0.0083592, 0.01503699],
157
+ [0.1382378, -0.0276671, 0.0266161, -0.0074060, 0.02801610],
158
+ [0.1651957, -0.0049124, 0.0269665, -0.0040025, 0.02138184],
159
+ [0.2354930, 0.0443711, 0.0173959, 0.0041872, 0.00794563],
160
+ [0.4168118, 0.1433867, 0.0111714, 0.0126265, -0.00658537],
161
+ [0.4681972, 0.2153273, 0.0225249, 0.0152704, -0.00463809],
162
+ [0.6023068, 0.3346815, 0.0731108, 0.0186618, -0.00371541],
163
+ [0.6415743, 0.3985468, 0.1324422, 0.0214160, -0.00062401],
164
+ [0.7503690, 0.5447616, 0.2804541, 0.0539851, 0.00242230],
165
+ [0.7452626, 0.5644594, 0.3324679, 0.0865153, 0.00454960],
166
+ [0.8059782, 0.6586838, 0.4229577, 0.1452990, 0.00976702],
167
+ [0.8549542, 0.7226562, 0.4991309, 0.2420093, 0.02772661],
168
+ [0.8571332, 0.7285741, 0.5279076, 0.2824549, 0.06030276],
169
+ [0.8835633, 0.7727077, 0.5957984, 0.3411303, 0.09652185],
170
+ [0.9071558, 0.8299587, 0.6771400, 0.4402896, 0.17469338],
171
+ [0.9190793, 0.8486480, 0.7163332, 0.5083780, 0.26107691],
172
+ [0.9371223, 0.8762177, 0.7653702, 0.5773109, 0.32181041],
173
+ [0.9554613, 0.9119893, 0.8282687, 0.6776178, 0.43162744],
174
+ [0.9545744, 0.9099264, 0.8270244, 0.6822220, 0.45237623],
175
+ [0.9688112, 0.9351710, 0.8730961, 0.7546601, 0.56622448],
176
+ [0.9743227, 0.9491953, 0.9005150, 0.8086497, 0.64505437],
177
+ [0.9807345, 0.9638853, 0.9283012, 0.8631675, 0.73812581],
178
+ [0.9886746, 0.9777760, 0.9558950, 0.9123417, 0.82726553],
179
+ [0.9899096, 0.9803828, 0.9615592, 0.9255600, 0.85822149],
180
+ [0.9969510, 0.9935441, 0.9864657, 0.9726775, 0.94358663],
181
+ [0.9979533, 0.9960274, 0.9921724, 0.9837415, 0.96626288],
182
+ [0.9995981, 0.9989171, 0.9974178, 0.9949954, 0.99023356],
183
+ [1.0002640, 1.0005088, 1.0010594, 1.0021161, 1.00386912],
184
+ [0.9998903, 0.9998459, 0.9997795, 0.9995484, 0.99916305],
185
+ [1.0000008, 0.9999905, 0.9999481, 0.9998903, 0.99978047],
186
+ [1.0000004, 0.9999983, 1.0000001, 1.0000031, 1.00000297],
187
+ [0.9999995, 1.0000003, 1.0000005, 1.0000001, 1.00000032],
188
+ [0.9999999, 0.9999997, 0.9999994, 0.9999989, 0.99999786],
189
+ [0.9999999, 0.9999999, 0.9999999, 0.9999999, 0.99999991]]
190
+ # Get iteration points
191
+ grad_list = [prob.grad(x) for x in x_list]
192
+ delta_x = [np.array(x_list[i+1])-np.array(x_list[i])
193
+ for i in range(len(x_list)-1)]
194
+ delta_grad = [grad_list[i+1]-grad_list[i]
195
+ for i in range(len(grad_list)-1)]
196
+ # Check curvature condition
197
+ for s, y in zip(delta_x, delta_grad):
198
+ if np.dot(s, y) <= 0:
199
+ raise ArithmeticError()
200
+ # Define QuasiNewton update
201
+ for quasi_newton in (BFGS(init_scale=1, min_curvature=1e-4),
202
+ SR1(init_scale=1)):
203
+ hess = deepcopy(quasi_newton)
204
+ inv_hess = deepcopy(quasi_newton)
205
+ hess.initialize(len(x_list[0]), 'hess')
206
+ inv_hess.initialize(len(x_list[0]), 'inv_hess')
207
+ # Compare the hessian and its inverse
208
+ for s, y in zip(delta_x, delta_grad):
209
+ hess.update(s, y)
210
+ inv_hess.update(s, y)
211
+ B = hess.get_matrix()
212
+ H = inv_hess.get_matrix()
213
+ assert_array_almost_equal(np.linalg.inv(B), H, decimal=10)
214
+ B_true = prob.hess(x_list[len(delta_x)])
215
+ assert_array_less(norm(B - B_true)/norm(B_true), 0.1)
216
+
217
+ def test_SR1_skip_update(self):
218
+ # Define auxiliary problem
219
+ prob = Rosenbrock(n=5)
220
+ # Define iteration points
221
+ x_list = [[0.0976270, 0.4303787, 0.2055267, 0.0897663, -0.15269040],
222
+ [0.1847239, 0.0505757, 0.2123832, 0.0255081, 0.00083286],
223
+ [0.2142498, -0.0188480, 0.0503822, 0.0347033, 0.03323606],
224
+ [0.2071680, -0.0185071, 0.0341337, -0.0139298, 0.02881750],
225
+ [0.1533055, -0.0322935, 0.0280418, -0.0083592, 0.01503699],
226
+ [0.1382378, -0.0276671, 0.0266161, -0.0074060, 0.02801610],
227
+ [0.1651957, -0.0049124, 0.0269665, -0.0040025, 0.02138184],
228
+ [0.2354930, 0.0443711, 0.0173959, 0.0041872, 0.00794563],
229
+ [0.4168118, 0.1433867, 0.0111714, 0.0126265, -0.00658537],
230
+ [0.4681972, 0.2153273, 0.0225249, 0.0152704, -0.00463809],
231
+ [0.6023068, 0.3346815, 0.0731108, 0.0186618, -0.00371541],
232
+ [0.6415743, 0.3985468, 0.1324422, 0.0214160, -0.00062401],
233
+ [0.7503690, 0.5447616, 0.2804541, 0.0539851, 0.00242230],
234
+ [0.7452626, 0.5644594, 0.3324679, 0.0865153, 0.00454960],
235
+ [0.8059782, 0.6586838, 0.4229577, 0.1452990, 0.00976702],
236
+ [0.8549542, 0.7226562, 0.4991309, 0.2420093, 0.02772661],
237
+ [0.8571332, 0.7285741, 0.5279076, 0.2824549, 0.06030276],
238
+ [0.8835633, 0.7727077, 0.5957984, 0.3411303, 0.09652185],
239
+ [0.9071558, 0.8299587, 0.6771400, 0.4402896, 0.17469338]]
240
+ # Get iteration points
241
+ grad_list = [prob.grad(x) for x in x_list]
242
+ delta_x = [np.array(x_list[i+1])-np.array(x_list[i])
243
+ for i in range(len(x_list)-1)]
244
+ delta_grad = [grad_list[i+1]-grad_list[i]
245
+ for i in range(len(grad_list)-1)]
246
+ hess = SR1(init_scale=1, min_denominator=1e-2)
247
+ hess.initialize(len(x_list[0]), 'hess')
248
+ # Compare the Hessian and its inverse
249
+ for i in range(len(delta_x)-1):
250
+ s = delta_x[i]
251
+ y = delta_grad[i]
252
+ hess.update(s, y)
253
+ # Test skip update
254
+ B = np.copy(hess.get_matrix())
255
+ s = delta_x[17]
256
+ y = delta_grad[17]
257
+ hess.update(s, y)
258
+ B_updated = np.copy(hess.get_matrix())
259
+ assert_array_equal(B, B_updated)
260
+
261
+ def test_BFGS_skip_update(self):
262
+ # Define auxiliary problem
263
+ prob = Rosenbrock(n=5)
264
+ # Define iteration points
265
+ x_list = [[0.0976270, 0.4303787, 0.2055267, 0.0897663, -0.15269040],
266
+ [0.1847239, 0.0505757, 0.2123832, 0.0255081, 0.00083286],
267
+ [0.2142498, -0.0188480, 0.0503822, 0.0347033, 0.03323606],
268
+ [0.2071680, -0.0185071, 0.0341337, -0.0139298, 0.02881750],
269
+ [0.1533055, -0.0322935, 0.0280418, -0.0083592, 0.01503699],
270
+ [0.1382378, -0.0276671, 0.0266161, -0.0074060, 0.02801610],
271
+ [0.1651957, -0.0049124, 0.0269665, -0.0040025, 0.02138184]]
272
+ # Get iteration points
273
+ grad_list = [prob.grad(x) for x in x_list]
274
+ delta_x = [np.array(x_list[i+1])-np.array(x_list[i])
275
+ for i in range(len(x_list)-1)]
276
+ delta_grad = [grad_list[i+1]-grad_list[i]
277
+ for i in range(len(grad_list)-1)]
278
+ hess = BFGS(init_scale=1, min_curvature=10)
279
+ hess.initialize(len(x_list[0]), 'hess')
280
+ # Compare the Hessian and its inverse
281
+ for i in range(len(delta_x)-1):
282
+ s = delta_x[i]
283
+ y = delta_grad[i]
284
+ hess.update(s, y)
285
+ # Test skip update
286
+ B = np.copy(hess.get_matrix())
287
+ s = delta_x[5]
288
+ y = delta_grad[5]
289
+ hess.update(s, y)
290
+ B_updated = np.copy(hess.get_matrix())
291
+ assert_array_equal(B, B_updated)
292
+
293
+
294
+ @pytest.mark.parametrize('strategy', [BFGS, SR1])
295
+ @pytest.mark.parametrize('approx_type', ['hess', 'inv_hess'])
296
+ def test_matmul_equals_dot(strategy, approx_type):
297
+ H = strategy(init_scale=1)
298
+ H.initialize(2, approx_type)
299
+ v = np.array([1, 2])
300
+ assert_array_equal(H @ v, H.dot(v))
loan-advisor-agent/.venv/Lib/site-packages/scipy/optimize/tests/test_isotonic_regression.py ADDED
@@ -0,0 +1,167 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import numpy as np
2
+ from numpy.testing import assert_allclose, assert_equal
3
+ import pytest
4
+
5
+ from scipy.optimize._pava_pybind import pava
6
+ from scipy.optimize import isotonic_regression
7
+
8
+
9
+ class TestIsotonicRegression:
10
+ @pytest.mark.parametrize(
11
+ ("y", "w", "msg"),
12
+ [
13
+ ([[0, 1]], None,
14
+ "array has incorrect number of dimensions: 2; expected 1"),
15
+ ([0, 1], [[1, 2]],
16
+ "Input arrays y and w must have one dimension of equal length"),
17
+ ([0, 1], [1],
18
+ "Input arrays y and w must have one dimension of equal length"),
19
+ (1, [1, 2],
20
+ "Input arrays y and w must have one dimension of equal length"),
21
+ ([1, 2], 1,
22
+ "Input arrays y and w must have one dimension of equal length"),
23
+ ([0, 1], [0, 1],
24
+ "Weights w must be strictly positive"),
25
+ ]
26
+ )
27
+ def test_raise_error(self, y, w, msg):
28
+ with pytest.raises(ValueError, match=msg):
29
+ isotonic_regression(y=y, weights=w)
30
+
31
+ def test_simple_pava(self):
32
+ # Test case of Busing 2020
33
+ # https://doi.org/10.18637/jss.v102.c01
34
+ y = np.array([8, 4, 8, 2, 2, 0, 8], dtype=np.float64)
35
+ w = np.ones_like(y)
36
+ r = np.full(shape=y.shape[0] + 1, fill_value=-1, dtype=np.intp)
37
+ pava(y, w, r)
38
+ assert_allclose(y, [4, 4, 4, 4, 4, 4, 8])
39
+ # Only first 2 elements of w are changed.
40
+ assert_allclose(w, [6, 1, 1, 1, 1, 1, 1])
41
+ # Only first 3 elements of r are changed.
42
+ assert_allclose(r, [0, 6, 7, -1, -1, -1, -1, -1])
43
+
44
+ @pytest.mark.parametrize("y_dtype", [np.float64, np.float32, np.int64, np.int32])
45
+ @pytest.mark.parametrize("w_dtype", [np.float64, np.float32, np.int64, np.int32])
46
+ @pytest.mark.parametrize("w", [None, "ones"])
47
+ def test_simple_isotonic_regression(self, w, w_dtype, y_dtype):
48
+ # Test case of Busing 2020
49
+ # https://doi.org/10.18637/jss.v102.c01
50
+ y = np.array([8, 4, 8, 2, 2, 0, 8], dtype=y_dtype)
51
+ if w is not None:
52
+ w = np.ones_like(y, dtype=w_dtype)
53
+ res = isotonic_regression(y, weights=w)
54
+ assert res.x.dtype == np.float64
55
+ assert res.weights.dtype == np.float64
56
+ assert_allclose(res.x, [4, 4, 4, 4, 4, 4, 8])
57
+ assert_allclose(res.weights, [6, 1])
58
+ assert_allclose(res.blocks, [0, 6, 7])
59
+ # Assert that y was not overwritten
60
+ assert_equal(y, np.array([8, 4, 8, 2, 2, 0, 8], dtype=np.float64))
61
+
62
+ @pytest.mark.parametrize("increasing", [True, False])
63
+ def test_linspace(self, increasing):
64
+ n = 10
65
+ y = np.linspace(0, 1, n) if increasing else np.linspace(1, 0, n)
66
+ res = isotonic_regression(y, increasing=increasing)
67
+ assert_allclose(res.x, y)
68
+ assert_allclose(res.blocks, np.arange(n + 1))
69
+
70
+ def test_weights(self):
71
+ w = np.array([1, 2, 5, 0.5, 0.5, 0.5, 1, 3])
72
+ y = np.array([3, 2, 1, 10, 9, 8, 20, 10])
73
+ res = isotonic_regression(y, weights=w)
74
+ assert_allclose(res.x, [12/8, 12/8, 12/8, 9, 9, 9, 50/4, 50/4])
75
+ assert_allclose(res.weights, [8, 1.5, 4])
76
+ assert_allclose(res.blocks, [0, 3, 6, 8])
77
+
78
+ # weights are like repeated observations, we repeat the 3rd element 5
79
+ # times.
80
+ w2 = np.array([1, 2, 1, 1, 1, 1, 1, 0.5, 0.5, 0.5, 1, 3])
81
+ y2 = np.array([3, 2, 1, 1, 1, 1, 1, 10, 9, 8, 20, 10])
82
+ res2 = isotonic_regression(y2, weights=w2)
83
+ assert_allclose(np.diff(res2.x[0:7]), 0)
84
+ assert_allclose(res2.x[4:], res.x)
85
+ assert_allclose(res2.weights, res.weights)
86
+ assert_allclose(res2.blocks[1:] - 4, res.blocks[1:])
87
+
88
+ def test_against_R_monotone(self):
89
+ y = [0, 6, 8, 3, 5, 2, 1, 7, 9, 4]
90
+ res = isotonic_regression(y)
91
+ # R code
92
+ # library(monotone)
93
+ # options(digits=8)
94
+ # monotone(c(0, 6, 8, 3, 5, 2, 1, 7, 9, 4))
95
+ x_R = [
96
+ 0, 4.1666667, 4.1666667, 4.1666667, 4.1666667, 4.1666667,
97
+ 4.1666667, 6.6666667, 6.6666667, 6.6666667,
98
+ ]
99
+ assert_allclose(res.x, x_R)
100
+ assert_equal(res.blocks, [0, 1, 7, 10])
101
+
102
+ n = 100
103
+ y = np.linspace(0, 1, num=n, endpoint=False)
104
+ y = 5 * y + np.sin(10 * y)
105
+ res = isotonic_regression(y)
106
+ # R code
107
+ # library(monotone)
108
+ # n <- 100
109
+ # y <- 5 * ((1:n)-1)/n + sin(10 * ((1:n)-1)/n)
110
+ # options(digits=8)
111
+ # monotone(y)
112
+ x_R = [
113
+ 0.00000000, 0.14983342, 0.29866933, 0.44552021, 0.58941834, 0.72942554,
114
+ 0.86464247, 0.99421769, 1.11735609, 1.23332691, 1.34147098, 1.44120736,
115
+ 1.53203909, 1.57081100, 1.57081100, 1.57081100, 1.57081100, 1.57081100,
116
+ 1.57081100, 1.57081100, 1.57081100, 1.57081100, 1.57081100, 1.57081100,
117
+ 1.57081100, 1.57081100, 1.57081100, 1.57081100, 1.57081100, 1.57081100,
118
+ 1.57081100, 1.57081100, 1.57081100, 1.57081100, 1.57081100, 1.57081100,
119
+ 1.57081100, 1.57081100, 1.57081100, 1.57081100, 1.57081100, 1.57081100,
120
+ 1.57081100, 1.57081100, 1.57081100, 1.57081100, 1.57081100, 1.57081100,
121
+ 1.57081100, 1.57081100, 1.57081100, 1.62418532, 1.71654534, 1.81773256,
122
+ 1.92723551, 2.04445967, 2.16873336, 2.29931446, 2.43539782, 2.57612334,
123
+ 2.72058450, 2.86783750, 3.01691060, 3.16681390, 3.31654920, 3.46511999,
124
+ 3.61154136, 3.75484992, 3.89411335, 4.02843976, 4.15698660, 4.27896904,
125
+ 4.39366786, 4.50043662, 4.59870810, 4.68799998, 4.76791967, 4.83816823,
126
+ 4.86564130, 4.86564130, 4.86564130, 4.86564130, 4.86564130, 4.86564130,
127
+ 4.86564130, 4.86564130, 4.86564130, 4.86564130, 4.86564130, 4.86564130,
128
+ 4.86564130, 4.86564130, 4.86564130, 4.86564130, 4.86564130, 4.86564130,
129
+ 4.86564130, 4.86564130, 4.86564130, 4.86564130,
130
+ ]
131
+ assert_allclose(res.x, x_R)
132
+
133
+ # Test increasing
134
+ assert np.all(np.diff(res.x) >= 0)
135
+
136
+ # Test balance property: sum(y) == sum(x)
137
+ assert_allclose(np.sum(res.x), np.sum(y))
138
+
139
+ # Reverse order
140
+ res_inv = isotonic_regression(-y, increasing=False)
141
+ assert_allclose(-res_inv.x, res.x)
142
+ assert_equal(res_inv.blocks, res.blocks)
143
+
144
+ def test_readonly(self):
145
+ x = np.arange(3, dtype=float)
146
+ w = np.ones(3, dtype=float)
147
+
148
+ x.flags.writeable = False
149
+ w.flags.writeable = False
150
+
151
+ res = isotonic_regression(x, weights=w)
152
+ assert np.all(np.isfinite(res.x))
153
+ assert np.all(np.isfinite(res.weights))
154
+ assert np.all(np.isfinite(res.blocks))
155
+
156
+ def test_non_contiguous_arrays(self):
157
+ x = np.arange(10, dtype=float)[::3]
158
+ w = np.ones(10, dtype=float)[::3]
159
+ assert not x.flags.c_contiguous
160
+ assert not x.flags.f_contiguous
161
+ assert not w.flags.c_contiguous
162
+ assert not w.flags.f_contiguous
163
+
164
+ res = isotonic_regression(x, weights=w)
165
+ assert np.all(np.isfinite(res.x))
166
+ assert np.all(np.isfinite(res.weights))
167
+ assert np.all(np.isfinite(res.blocks))
loan-advisor-agent/.venv/Lib/site-packages/scipy/optimize/tests/test_lbfgsb_hessinv.py ADDED
@@ -0,0 +1,65 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import numpy as np
2
+ from numpy.testing import assert_allclose
3
+ import scipy.linalg
4
+ from scipy.optimize import minimize
5
+
6
+
7
+ def test_1():
8
+ def f(x):
9
+ return x**4, 4*x**3
10
+
11
+ for gtol in [1e-8, 1e-12, 1e-20]:
12
+ for maxcor in range(20, 35):
13
+ result = minimize(fun=f, jac=True, method='L-BFGS-B', x0=20,
14
+ options={'gtol': gtol, 'maxcor': maxcor})
15
+
16
+ H1 = result.hess_inv(np.array([1])).reshape(1,1)
17
+ H2 = result.hess_inv.todense()
18
+
19
+ assert_allclose(H1, H2)
20
+
21
+
22
+ def test_2():
23
+ H0 = [[3, 0], [1, 2]]
24
+
25
+ def f(x):
26
+ return np.dot(x, np.dot(scipy.linalg.inv(H0), x))
27
+
28
+ result1 = minimize(fun=f, method='L-BFGS-B', x0=[10, 20])
29
+ result2 = minimize(fun=f, method='BFGS', x0=[10, 20])
30
+
31
+ H1 = result1.hess_inv.todense()
32
+
33
+ H2 = np.vstack((
34
+ result1.hess_inv(np.array([1, 0])),
35
+ result1.hess_inv(np.array([0, 1]))))
36
+
37
+ assert_allclose(
38
+ result1.hess_inv(np.array([1, 0]).reshape(2,1)).reshape(-1),
39
+ result1.hess_inv(np.array([1, 0])))
40
+ assert_allclose(H1, H2)
41
+ assert_allclose(H1, result2.hess_inv, rtol=1e-2, atol=0.03)
42
+
43
+
44
+ def test_3():
45
+
46
+ def todense_old_impl(self):
47
+ s, y, n_corrs, rho = self.sk, self.yk, self.n_corrs, self.rho
48
+ I_arr = np.eye(*self.shape, dtype=self.dtype)
49
+ Hk = I_arr
50
+
51
+ for i in range(n_corrs):
52
+ A1 = I_arr - s[i][:, np.newaxis] * y[i][np.newaxis, :] * rho[i]
53
+ A2 = I_arr - y[i][:, np.newaxis] * s[i][np.newaxis, :] * rho[i]
54
+
55
+ Hk = np.dot(A1, np.dot(Hk, A2)) + (rho[i] * s[i][:, np.newaxis] *
56
+ s[i][np.newaxis, :])
57
+ return Hk
58
+
59
+ H0 = [[3, 0], [1, 2]]
60
+
61
+ def f(x):
62
+ return np.dot(x, np.dot(scipy.linalg.inv(H0), x))
63
+
64
+ result1 = minimize(fun=f, method='L-BFGS-B', x0=[10, 20])
65
+ assert_allclose(result1.hess_inv.todense(), todense_old_impl(result1.hess_inv))
loan-advisor-agent/.venv/Lib/site-packages/scipy/optimize/tests/test_lbfgsb_setulb.py ADDED
@@ -0,0 +1,122 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import numpy as np
2
+ from scipy.optimize import _lbfgsb, minimize
3
+
4
+
5
+ def objfun(x):
6
+ """simplified objective func to test lbfgsb bound violation"""
7
+ x0 = [0.8750000000000278,
8
+ 0.7500000000000153,
9
+ 0.9499999999999722,
10
+ 0.8214285714285992,
11
+ 0.6363636363636085]
12
+ x1 = [1.0, 0.0, 1.0, 0.0, 0.0]
13
+ x2 = [1.0,
14
+ 0.0,
15
+ 0.9889733043149325,
16
+ 0.0,
17
+ 0.026353554421041155]
18
+ x3 = [1.0,
19
+ 0.0,
20
+ 0.9889917442915558,
21
+ 0.0,
22
+ 0.020341986743231205]
23
+
24
+ f0 = 5163.647901211178
25
+ f1 = 5149.8181642072905
26
+ f2 = 5149.379332309634
27
+ f3 = 5149.374490771297
28
+
29
+ g0 = np.array([-0.5934820547965749,
30
+ 1.6251549718258351,
31
+ -71.99168459202559,
32
+ 5.346636965797545,
33
+ 37.10732723092604])
34
+ g1 = np.array([-0.43295349282641515,
35
+ 1.008607936794592,
36
+ 18.223666726602975,
37
+ 31.927010036981997,
38
+ -19.667512518739386])
39
+ g2 = np.array([-0.4699874455100256,
40
+ 0.9466285353668347,
41
+ -0.016874360242016825,
42
+ 48.44999161133457,
43
+ 5.819631620590712])
44
+ g3 = np.array([-0.46970678696829116,
45
+ 0.9612719312174818,
46
+ 0.006129809488833699,
47
+ 48.43557729419473,
48
+ 6.005481418498221])
49
+
50
+ if np.allclose(x, x0):
51
+ f = f0
52
+ g = g0
53
+ elif np.allclose(x, x1):
54
+ f = f1
55
+ g = g1
56
+ elif np.allclose(x, x2):
57
+ f = f2
58
+ g = g2
59
+ elif np.allclose(x, x3):
60
+ f = f3
61
+ g = g3
62
+ else:
63
+ raise ValueError(
64
+ 'Simplified objective function not defined '
65
+ 'at requested point')
66
+ return (np.copy(f), np.copy(g))
67
+
68
+
69
+ def test_setulb_floatround():
70
+ """test if setulb() violates bounds
71
+
72
+ checks for violation due to floating point rounding error
73
+ """
74
+
75
+ n = 5
76
+ m = 10
77
+ factr = 1e7
78
+ pgtol = 1e-5
79
+ maxls = 20
80
+ nbd = np.full(shape=(n,), fill_value=2, dtype=np.int32)
81
+ low_bnd = np.zeros(n, dtype=np.float64)
82
+ upper_bnd = np.ones(n, dtype=np.float64)
83
+
84
+ x0 = np.array(
85
+ [0.8750000000000278,
86
+ 0.7500000000000153,
87
+ 0.9499999999999722,
88
+ 0.8214285714285992,
89
+ 0.6363636363636085])
90
+ x = np.copy(x0)
91
+
92
+ f = np.array(0.0, dtype=np.float64)
93
+ g = np.zeros(n, dtype=np.float64)
94
+
95
+ wa = np.zeros(2*m*n + 5*n + 11*m*m + 8*m, dtype=np.float64)
96
+ iwa = np.zeros(3*n, dtype=np.int32)
97
+ task = np.zeros(2, dtype=np.int32)
98
+ ln_task = np.zeros(2, dtype=np.int32)
99
+ lsave = np.zeros(4, dtype=np.int32)
100
+ isave = np.zeros(44, dtype=np.int32)
101
+ dsave = np.zeros(29, dtype=np.float64)
102
+
103
+ for n_iter in range(7): # 7 steps required to reproduce error
104
+ f, g = objfun(x)
105
+
106
+ _lbfgsb.setulb(m, x, low_bnd, upper_bnd, nbd, f, g, factr, pgtol, wa,
107
+ iwa, task, lsave, isave, dsave, maxls, ln_task)
108
+
109
+ assert (x <= upper_bnd).all() and (x >= low_bnd).all(), (
110
+ "_lbfgsb.setulb() stepped to a point outside of the bounds")
111
+
112
+
113
+ def test_gh_issue18730():
114
+ # issue 18730 reported that l-bfgs-b did not work with objectives
115
+ # returning single precision gradient arrays
116
+ def fun_single_precision(x):
117
+ x = x.astype(np.float32)
118
+ return np.sum(x**2), (2*x)
119
+
120
+ res = minimize(fun_single_precision, x0=np.array([1., 1.]), jac=True,
121
+ method="l-bfgs-b")
122
+ np.testing.assert_allclose(res.fun, 0., atol=1e-15)
loan-advisor-agent/.venv/Lib/site-packages/scipy/optimize/tests/test_least_squares.py ADDED
@@ -0,0 +1,999 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import warnings
2
+
3
+ from itertools import product
4
+ from multiprocessing import Pool
5
+
6
+ import numpy as np
7
+ from numpy.linalg import norm
8
+ from numpy.testing import (assert_, assert_allclose,
9
+ assert_equal)
10
+ import pytest
11
+ from pytest import raises as assert_raises
12
+ from scipy.sparse import issparse, lil_array
13
+ from scipy.sparse.linalg import aslinearoperator
14
+
15
+ from scipy.optimize import least_squares, Bounds, minimize
16
+ from scipy.optimize._lsq.least_squares import IMPLEMENTED_LOSSES
17
+ from scipy.optimize._lsq.common import EPS, make_strictly_feasible, CL_scaling_vector
18
+
19
+ from scipy.optimize import OptimizeResult
20
+
21
+ def fun_trivial(x, a=0):
22
+ return (x - a)**2 + 5.0
23
+
24
+
25
+ def jac_trivial(x, a=0.0):
26
+ return 2 * (x - a)
27
+
28
+
29
+ def fun_2d_trivial(x):
30
+ return np.array([x[0], x[1]])
31
+
32
+
33
+ def jac_2d_trivial(x):
34
+ return np.identity(2)
35
+
36
+
37
+ def fun_rosenbrock(x):
38
+ return np.array([10 * (x[1] - x[0]**2), (1 - x[0])])
39
+
40
+
41
+ class Fun_Rosenbrock:
42
+ def __init__(self):
43
+ self.nfev = 0
44
+
45
+ def __call__(self, x, a=0):
46
+ self.nfev += 1
47
+ return fun_rosenbrock(x)
48
+
49
+
50
+ def jac_rosenbrock(x):
51
+ return np.array([
52
+ [-20 * x[0], 10],
53
+ [-1, 0]
54
+ ])
55
+
56
+
57
+ def jac_rosenbrock_bad_dim(x):
58
+ return np.array([
59
+ [-20 * x[0], 10],
60
+ [-1, 0],
61
+ [0.0, 0.0]
62
+ ])
63
+
64
+
65
+ def fun_rosenbrock_cropped(x):
66
+ return fun_rosenbrock(x)[0]
67
+
68
+
69
+ def jac_rosenbrock_cropped(x):
70
+ return jac_rosenbrock(x)[0]
71
+
72
+
73
+ # When x is 1-D array, return is 2-D array.
74
+ def fun_wrong_dimensions(x):
75
+ return np.array([x, x**2, x**3])
76
+
77
+
78
+ def jac_wrong_dimensions(x, a=0.0):
79
+ return np.atleast_3d(jac_trivial(x, a=a))
80
+
81
+
82
+ def fun_bvp(x):
83
+ n = int(np.sqrt(x.shape[0]))
84
+ u = np.zeros((n + 2, n + 2))
85
+ x = x.reshape((n, n))
86
+ u[1:-1, 1:-1] = x
87
+ y = u[:-2, 1:-1] + u[2:, 1:-1] + u[1:-1, :-2] + u[1:-1, 2:] - 4 * x + x**3
88
+ return y.ravel()
89
+
90
+
91
+ class BroydenTridiagonal:
92
+ def __init__(self, n=100, mode='sparse'):
93
+ rng = np.random.default_rng(123440)
94
+
95
+ self.n = n
96
+
97
+ self.x0 = -np.ones(n)
98
+ self.lb = np.linspace(-2, -1.5, n)
99
+ self.ub = np.linspace(-0.8, 0.0, n)
100
+
101
+ self.lb += 0.1 * rng.standard_normal(n)
102
+ self.ub += 0.1 * rng.standard_normal(n)
103
+
104
+ self.x0 += 0.1 * rng.standard_normal(n)
105
+ self.x0 = make_strictly_feasible(self.x0, self.lb, self.ub)
106
+
107
+ if mode == 'sparse':
108
+ self.sparsity = lil_array((n, n), dtype=int)
109
+ i = np.arange(n)
110
+ self.sparsity[i, i] = 1
111
+ i = np.arange(1, n)
112
+ self.sparsity[i, i - 1] = 1
113
+ i = np.arange(n - 1)
114
+ self.sparsity[i, i + 1] = 1
115
+
116
+ self.jac = self._jac
117
+ elif mode == 'operator':
118
+ self.jac = lambda x: aslinearoperator(self._jac(x))
119
+ elif mode == 'dense':
120
+ self.sparsity = None
121
+ self.jac = lambda x: self._jac(x).toarray()
122
+ else:
123
+ assert_(False)
124
+
125
+ def fun(self, x):
126
+ f = (3 - x) * x + 1
127
+ f[1:] -= x[:-1]
128
+ f[:-1] -= 2 * x[1:]
129
+ return f
130
+
131
+ def _jac(self, x):
132
+ J = lil_array((self.n, self.n))
133
+ i = np.arange(self.n)
134
+ J[i, i] = 3 - 2 * x
135
+ i = np.arange(1, self.n)
136
+ J[i, i - 1] = -1
137
+ i = np.arange(self.n - 1)
138
+ J[i, i + 1] = -2
139
+ return J
140
+
141
+
142
+ class ExponentialFittingProblem:
143
+ """Provide data and function for exponential fitting in the form
144
+ y = a + exp(b * x) + noise."""
145
+
146
+ def __init__(self, a, b, noise, n_outliers=1, x_range=(-1, 1),
147
+ n_points=11, rng=None):
148
+ rng = np.random.default_rng(rng)
149
+ self.m = n_points
150
+ self.n = 2
151
+
152
+ self.p0 = np.zeros(2)
153
+ self.x = np.linspace(x_range[0], x_range[1], n_points)
154
+
155
+ self.y = a + np.exp(b * self.x)
156
+ self.y += noise * rng.standard_normal(self.m)
157
+
158
+ outliers = rng.integers(0, self.m, n_outliers)
159
+ self.y[outliers] += 50 * noise * rng.random(n_outliers)
160
+
161
+ self.p_opt = np.array([a, b])
162
+
163
+ def fun(self, p):
164
+ return p[0] + np.exp(p[1] * self.x) - self.y
165
+
166
+ def jac(self, p):
167
+ J = np.empty((self.m, self.n))
168
+ J[:, 0] = 1
169
+ J[:, 1] = self.x * np.exp(p[1] * self.x)
170
+ return J
171
+
172
+
173
+ def cubic_soft_l1(z):
174
+ rho = np.empty((3, z.size))
175
+
176
+ t = 1 + z
177
+ rho[0] = 3 * (t**(1/3) - 1)
178
+ rho[1] = t ** (-2/3)
179
+ rho[2] = -2/3 * t**(-5/3)
180
+
181
+ return rho
182
+
183
+
184
+ LOSSES = list(IMPLEMENTED_LOSSES.keys()) + [cubic_soft_l1]
185
+
186
+
187
+ class BaseMixin:
188
+ msg = "jac='(3-point|cs)' works equivalently to '2-point' for method='lm'"
189
+ def test_basic(self):
190
+ # Test that the basic calling sequence works.
191
+ res = least_squares(fun_trivial, 2., method=self.method)
192
+ assert_allclose(res.x, 0, atol=1e-4)
193
+ assert_allclose(res.fun, fun_trivial(res.x))
194
+
195
+ def test_args_kwargs(self):
196
+ # Test that args and kwargs are passed correctly to the functions.
197
+ a = 3.0
198
+ for jac in ['2-point', '3-point', 'cs', jac_trivial]:
199
+ with warnings.catch_warnings():
200
+ warnings.filterwarnings("ignore", self.msg, UserWarning)
201
+ res = least_squares(fun_trivial, 2.0, jac, args=(a,),
202
+ method=self.method)
203
+ res1 = least_squares(fun_trivial, 2.0, jac, kwargs={'a': a},
204
+ method=self.method)
205
+
206
+ assert_allclose(res.x, a, rtol=1e-4)
207
+ assert_allclose(res1.x, a, rtol=1e-4)
208
+
209
+ assert_raises(TypeError, least_squares, fun_trivial, 2.0,
210
+ args=(3, 4,), method=self.method)
211
+ assert_raises(TypeError, least_squares, fun_trivial, 2.0,
212
+ kwargs={'kaboom': 3}, method=self.method)
213
+
214
+ def test_jac_options(self):
215
+ for jac in ['2-point', '3-point', 'cs', jac_trivial]:
216
+ with warnings.catch_warnings():
217
+ warnings.filterwarnings("ignore", self.msg, UserWarning)
218
+ res = least_squares(fun_trivial, 2.0, jac, method=self.method)
219
+ assert_allclose(res.x, 0, atol=1e-4)
220
+
221
+ assert_raises(ValueError, least_squares, fun_trivial, 2.0, jac='oops',
222
+ method=self.method)
223
+
224
+ def test_nfev_options(self):
225
+ for max_nfev in [None, 20]:
226
+ res = least_squares(fun_trivial, 2.0, max_nfev=max_nfev,
227
+ method=self.method)
228
+ assert_allclose(res.x, 0, atol=1e-4)
229
+
230
+ def test_x_scale_options(self):
231
+ for x_scale in [1.0, np.array([0.5]), 'jac']:
232
+ res = least_squares(fun_trivial, 2.0, x_scale=x_scale)
233
+ assert_allclose(res.x, 0)
234
+ assert_raises(ValueError, least_squares, fun_trivial,
235
+ 2.0, x_scale='auto', method=self.method)
236
+ assert_raises(ValueError, least_squares, fun_trivial,
237
+ 2.0, x_scale=-1.0, method=self.method)
238
+ assert_raises(ValueError, least_squares, fun_trivial,
239
+ 2.0, x_scale=1.0+2.0j, method=self.method)
240
+
241
+ def test_diff_step(self):
242
+ res1 = least_squares(fun_trivial, 2.0, diff_step=1e-1,
243
+ method=self.method)
244
+ res3 = least_squares(fun_trivial, 2.0,
245
+ diff_step=None, method=self.method)
246
+ assert_allclose(res1.x, 0, atol=1e-4)
247
+ assert_allclose(res3.x, 0, atol=1e-4)
248
+
249
+
250
+ def test_incorrect_options_usage(self):
251
+ assert_raises(TypeError, least_squares, fun_trivial, 2.0,
252
+ method=self.method, options={'no_such_option': 100})
253
+ assert_raises(TypeError, least_squares, fun_trivial, 2.0,
254
+ method=self.method, options={'max_nfev': 100})
255
+
256
+ def test_full_result(self):
257
+ # MINPACK doesn't work very well with factor=100 on this problem,
258
+ # thus using low 'atol'.
259
+ res = least_squares(fun_trivial, 2.0, method=self.method)
260
+ assert_allclose(res.x, 0, atol=1e-4)
261
+ assert_allclose(res.cost, 12.5)
262
+ assert_allclose(res.fun, 5)
263
+ assert_allclose(res.jac, 0, atol=1e-4)
264
+ assert_allclose(res.grad, 0, atol=1e-2)
265
+ assert_allclose(res.optimality, 0, atol=1e-2)
266
+ assert_equal(res.active_mask, 0)
267
+ if self.method == 'lm':
268
+ assert_(res.njev is None)
269
+ else:
270
+ assert_(res.nfev < 10)
271
+ assert_(res.njev < 10)
272
+ assert_(res.status > 0)
273
+ assert_(res.success)
274
+
275
+ def test_full_result_single_fev(self):
276
+ # MINPACK checks the number of nfev after the iteration,
277
+ # so it's hard to tell what he is going to compute.
278
+ if self.method == 'lm':
279
+ return
280
+
281
+ res = least_squares(fun_trivial, 2.0, method=self.method,
282
+ max_nfev=1)
283
+ assert_equal(res.x, np.array([2]))
284
+ assert_equal(res.cost, 40.5)
285
+ assert_equal(res.fun, np.array([9]))
286
+ assert_equal(res.jac, np.array([[4]]))
287
+ assert_equal(res.grad, np.array([36]))
288
+ assert_equal(res.optimality, 36)
289
+ assert_equal(res.active_mask, np.array([0]))
290
+ assert_equal(res.nfev, 1)
291
+ assert_equal(res.njev, 1)
292
+ assert_equal(res.status, 0)
293
+ assert_equal(res.success, 0)
294
+
295
+ def test_nfev(self):
296
+ # checks that the true number of nfev are being consumed
297
+ for i in range(1, 3):
298
+ rng = np.random.default_rng(128908)
299
+ x0 = rng.uniform(size=2) * 10
300
+ ftrivial = Fun_Rosenbrock()
301
+ res = least_squares(
302
+ ftrivial, x0, jac=jac_rosenbrock, method=self.method, max_nfev=i
303
+ )
304
+ assert res.nfev == ftrivial.nfev
305
+
306
+ def test_rosenbrock(self):
307
+ x0 = [-2, 1]
308
+ x_opt = [1, 1]
309
+ for jac, x_scale, tr_solver in product(
310
+ ['2-point', '3-point', 'cs', jac_rosenbrock],
311
+ [1.0, np.array([1.0, 0.2]), 'jac'],
312
+ ['exact', 'lsmr']):
313
+ with warnings.catch_warnings():
314
+ warnings.filterwarnings("ignore", self.msg, UserWarning)
315
+ res = least_squares(fun_rosenbrock, x0, jac, x_scale=x_scale,
316
+ tr_solver=tr_solver, method=self.method)
317
+ assert_allclose(res.x, x_opt)
318
+
319
+ def test_rosenbrock_cropped(self):
320
+ x0 = [-2, 1]
321
+ if self.method == 'lm':
322
+ assert_raises(ValueError, least_squares, fun_rosenbrock_cropped,
323
+ x0, method='lm')
324
+ else:
325
+ for jac, x_scale, tr_solver in product(
326
+ ['2-point', '3-point', 'cs', jac_rosenbrock_cropped],
327
+ [1.0, np.array([1.0, 0.2]), 'jac'],
328
+ ['exact', 'lsmr']):
329
+ res = least_squares(
330
+ fun_rosenbrock_cropped, x0, jac, x_scale=x_scale,
331
+ tr_solver=tr_solver, method=self.method)
332
+ assert_allclose(res.cost, 0, atol=1e-14)
333
+
334
+ def test_fun_wrong_dimensions(self):
335
+ assert_raises(ValueError, least_squares, fun_wrong_dimensions,
336
+ 2.0, method=self.method)
337
+
338
+ def test_jac_wrong_dimensions(self):
339
+ assert_raises(ValueError, least_squares, fun_trivial,
340
+ 2.0, jac_wrong_dimensions, method=self.method)
341
+
342
+ def test_fun_and_jac_inconsistent_dimensions(self):
343
+ x0 = [1, 2]
344
+ assert_raises(ValueError, least_squares, fun_rosenbrock, x0,
345
+ jac_rosenbrock_bad_dim, method=self.method)
346
+
347
+ def test_x0_multidimensional(self):
348
+ x0 = np.ones(4).reshape(2, 2)
349
+ assert_raises(ValueError, least_squares, fun_trivial, x0,
350
+ method=self.method)
351
+
352
+ def test_x0_complex_scalar(self):
353
+ x0 = 2.0 + 0.0*1j
354
+ assert_raises(ValueError, least_squares, fun_trivial, x0,
355
+ method=self.method)
356
+
357
+ def test_x0_complex_array(self):
358
+ x0 = [1.0, 2.0 + 0.0*1j]
359
+ assert_raises(ValueError, least_squares, fun_trivial, x0,
360
+ method=self.method)
361
+
362
+ def test_bvp(self):
363
+ # This test was introduced with fix #5556. It turned out that
364
+ # dogbox solver had a bug with trust-region radius update, which
365
+ # could block its progress and create an infinite loop. And this
366
+ # discrete boundary value problem is the one which triggers it.
367
+ n = 10
368
+ x0 = np.ones(n**2)
369
+ if self.method == 'lm':
370
+ max_nfev = 5000 # To account for Jacobian estimation.
371
+ else:
372
+ max_nfev = 100
373
+ res = least_squares(fun_bvp, x0, ftol=1e-2, method=self.method,
374
+ max_nfev=max_nfev)
375
+
376
+ assert_(res.nfev < max_nfev)
377
+ assert_(res.cost < 0.5)
378
+
379
+ def test_error_raised_when_all_tolerances_below_eps(self):
380
+ # Test that all 0 tolerances are not allowed.
381
+ assert_raises(ValueError, least_squares, fun_trivial, 2.0,
382
+ method=self.method, ftol=None, xtol=None, gtol=None)
383
+
384
+ def test_convergence_with_only_one_tolerance_enabled(self):
385
+ if self.method == 'lm':
386
+ return # should not do test
387
+ x0 = [-2, 1]
388
+ x_opt = [1, 1]
389
+ for ftol, xtol, gtol in [(1e-8, None, None),
390
+ (None, 1e-8, None),
391
+ (None, None, 1e-8)]:
392
+ res = least_squares(fun_rosenbrock, x0, jac=jac_rosenbrock,
393
+ ftol=ftol, gtol=gtol, xtol=xtol,
394
+ method=self.method)
395
+ assert_allclose(res.x, x_opt)
396
+
397
+ # This test is thread safe, but it is too slow and opens
398
+ # too many file descriptors to run it in parallel.
399
+ @pytest.mark.parallel_threads_limit(1)
400
+ @pytest.mark.fail_slow(5.0)
401
+ def test_workers(self):
402
+ serial = least_squares(fun_trivial, 2.0, method=self.method)
403
+
404
+ reses = []
405
+ for workers in [None, 2]:
406
+ res = least_squares(
407
+ fun_trivial, 2.0, method=self.method, workers=workers
408
+ )
409
+ reses.append(res)
410
+ with Pool(2) as workers:
411
+ res = least_squares(
412
+ fun_trivial, 2.0, method=self.method, workers=workers.map
413
+ )
414
+ reses.append(res)
415
+ for res in reses:
416
+ assert res.success
417
+ assert_equal(res.x, serial.x)
418
+ assert_equal(res.nfev, serial.nfev)
419
+ assert_equal(res.njev, serial.njev)
420
+
421
+
422
+ class BoundsMixin:
423
+ def test_inconsistent(self):
424
+ assert_raises(ValueError, least_squares, fun_trivial, 2.0,
425
+ bounds=(10.0, 0.0), method=self.method)
426
+
427
+ def test_infeasible(self):
428
+ assert_raises(ValueError, least_squares, fun_trivial, 2.0,
429
+ bounds=(3., 4), method=self.method)
430
+
431
+ def test_wrong_number(self):
432
+ assert_raises(ValueError, least_squares, fun_trivial, 2.,
433
+ bounds=(1., 2, 3), method=self.method)
434
+
435
+ def test_inconsistent_shape(self):
436
+ assert_raises(ValueError, least_squares, fun_trivial, 2.0,
437
+ bounds=(1.0, [2.0, 3.0]), method=self.method)
438
+ # 1-D array won't be broadcast
439
+ assert_raises(ValueError, least_squares, fun_rosenbrock, [1.0, 2.0],
440
+ bounds=([0.0], [3.0, 4.0]), method=self.method)
441
+
442
+ def test_in_bounds(self):
443
+ for jac in ['2-point', '3-point', 'cs', jac_trivial]:
444
+ res = least_squares(fun_trivial, 2.0, jac=jac,
445
+ bounds=(-1.0, 3.0), method=self.method)
446
+ assert_allclose(res.x, 0.0, atol=1e-4)
447
+ assert_equal(res.active_mask, [0])
448
+ assert_(-1 <= res.x <= 3)
449
+ res = least_squares(fun_trivial, 2.0, jac=jac,
450
+ bounds=(0.5, 3.0), method=self.method)
451
+ assert_allclose(res.x, 0.5, atol=1e-4)
452
+ assert_equal(res.active_mask, [-1])
453
+ assert_(0.5 <= res.x <= 3)
454
+
455
+ def test_bounds_shape(self):
456
+ def get_bounds_direct(lb, ub):
457
+ return lb, ub
458
+
459
+ def get_bounds_instances(lb, ub):
460
+ return Bounds(lb, ub)
461
+
462
+ for jac in ['2-point', '3-point', 'cs', jac_2d_trivial]:
463
+ for bounds_func in [get_bounds_direct, get_bounds_instances]:
464
+ x0 = [1.0, 1.0]
465
+ res = least_squares(fun_2d_trivial, x0, jac=jac)
466
+ assert_allclose(res.x, [0.0, 0.0])
467
+ res = least_squares(fun_2d_trivial, x0, jac=jac,
468
+ bounds=bounds_func(0.5, [2.0, 2.0]),
469
+ method=self.method)
470
+ assert_allclose(res.x, [0.5, 0.5])
471
+ res = least_squares(fun_2d_trivial, x0, jac=jac,
472
+ bounds=bounds_func([0.3, 0.2], 3.0),
473
+ method=self.method)
474
+ assert_allclose(res.x, [0.3, 0.2])
475
+ res = least_squares(
476
+ fun_2d_trivial, x0, jac=jac,
477
+ bounds=bounds_func([-1, 0.5], [1.0, 3.0]),
478
+ method=self.method)
479
+ assert_allclose(res.x, [0.0, 0.5], atol=1e-5)
480
+
481
+ def test_bounds_instances(self):
482
+ res = least_squares(fun_trivial, 0.5, bounds=Bounds())
483
+ assert_allclose(res.x, 0.0, atol=1e-4)
484
+
485
+ res = least_squares(fun_trivial, 3.0, bounds=Bounds(lb=1.0))
486
+ assert_allclose(res.x, 1.0, atol=1e-4)
487
+
488
+ res = least_squares(fun_trivial, 0.5, bounds=Bounds(lb=-1.0, ub=1.0))
489
+ assert_allclose(res.x, 0.0, atol=1e-4)
490
+
491
+ res = least_squares(fun_trivial, -3.0, bounds=Bounds(ub=-1.0))
492
+ assert_allclose(res.x, -1.0, atol=1e-4)
493
+
494
+ res = least_squares(fun_2d_trivial, [0.5, 0.5],
495
+ bounds=Bounds(lb=[-1.0, -1.0], ub=1.0))
496
+ assert_allclose(res.x, [0.0, 0.0], atol=1e-5)
497
+
498
+ res = least_squares(fun_2d_trivial, [0.5, 0.5],
499
+ bounds=Bounds(lb=[0.1, 0.1]))
500
+ assert_allclose(res.x, [0.1, 0.1], atol=1e-5)
501
+
502
+ @pytest.mark.fail_slow(10)
503
+ def test_rosenbrock_bounds(self):
504
+ x0_1 = np.array([-2.0, 1.0])
505
+ x0_2 = np.array([2.0, 2.0])
506
+ x0_3 = np.array([-2.0, 2.0])
507
+ x0_4 = np.array([0.0, 2.0])
508
+ x0_5 = np.array([-1.2, 1.0])
509
+ problems = [
510
+ (x0_1, ([-np.inf, -1.5], np.inf)),
511
+ (x0_2, ([-np.inf, 1.5], np.inf)),
512
+ (x0_3, ([-np.inf, 1.5], np.inf)),
513
+ (x0_4, ([-np.inf, 1.5], [1.0, np.inf])),
514
+ (x0_2, ([1.0, 1.5], [3.0, 3.0])),
515
+ (x0_5, ([-50.0, 0.0], [0.5, 100]))
516
+ ]
517
+ for x0, bounds in problems:
518
+ for jac, x_scale, tr_solver in product(
519
+ ['2-point', '3-point', 'cs', jac_rosenbrock],
520
+ [1.0, [1.0, 0.5], 'jac'],
521
+ ['exact', 'lsmr']):
522
+ res = least_squares(fun_rosenbrock, x0, jac, bounds,
523
+ x_scale=x_scale, tr_solver=tr_solver,
524
+ method=self.method)
525
+ assert_allclose(res.optimality, 0.0, atol=1e-5)
526
+
527
+
528
+ class SparseMixin:
529
+ def test_exact_tr_solver(self):
530
+ p = BroydenTridiagonal()
531
+ assert_raises(ValueError, least_squares, p.fun, p.x0, p.jac,
532
+ tr_solver='exact', method=self.method)
533
+ assert_raises(ValueError, least_squares, p.fun, p.x0,
534
+ tr_solver='exact', jac_sparsity=p.sparsity,
535
+ method=self.method)
536
+
537
+ def test_equivalence(self):
538
+ sparse = BroydenTridiagonal(mode='sparse')
539
+ dense = BroydenTridiagonal(mode='dense')
540
+ res_sparse = least_squares(
541
+ sparse.fun, sparse.x0, jac=sparse.jac,
542
+ method=self.method)
543
+ res_dense = least_squares(
544
+ dense.fun, dense.x0, jac=sparse.jac,
545
+ method=self.method)
546
+ assert_equal(res_sparse.nfev, res_dense.nfev)
547
+ assert_allclose(res_sparse.x, res_dense.x, atol=1e-20)
548
+ assert_allclose(res_sparse.cost, 0, atol=1e-20)
549
+ assert_allclose(res_dense.cost, 0, atol=1e-20)
550
+
551
+ def test_tr_options(self):
552
+ p = BroydenTridiagonal()
553
+ res = least_squares(p.fun, p.x0, p.jac, method=self.method,
554
+ tr_options={'btol': 1e-10})
555
+ assert_allclose(res.cost, 0, atol=1e-20)
556
+
557
+ def test_wrong_parameters(self):
558
+ p = BroydenTridiagonal()
559
+ assert_raises(ValueError, least_squares, p.fun, p.x0, p.jac,
560
+ tr_solver='best', method=self.method)
561
+ assert_raises(TypeError, least_squares, p.fun, p.x0, p.jac,
562
+ tr_solver='lsmr', tr_options={'tol': 1e-10})
563
+
564
+ def test_solver_selection(self):
565
+ sparse = BroydenTridiagonal(mode='sparse')
566
+ dense = BroydenTridiagonal(mode='dense')
567
+ res_sparse = least_squares(sparse.fun, sparse.x0, jac=sparse.jac,
568
+ method=self.method)
569
+ res_dense = least_squares(dense.fun, dense.x0, jac=dense.jac,
570
+ method=self.method)
571
+ assert_allclose(res_sparse.cost, 0, atol=1e-20)
572
+ assert_allclose(res_dense.cost, 0, atol=1e-20)
573
+ assert_(issparse(res_sparse.jac))
574
+ assert_(isinstance(res_dense.jac, np.ndarray))
575
+
576
+ def test_numerical_jac(self):
577
+ p = BroydenTridiagonal()
578
+ for jac in ['2-point', '3-point', 'cs']:
579
+ res_dense = least_squares(p.fun, p.x0, jac, method=self.method)
580
+ res_sparse = least_squares(
581
+ p.fun, p.x0, jac,method=self.method,
582
+ jac_sparsity=p.sparsity)
583
+ assert_equal(res_dense.nfev, res_sparse.nfev)
584
+ assert_allclose(res_dense.x, res_sparse.x, atol=1e-20)
585
+ assert_allclose(res_dense.cost, 0, atol=1e-20)
586
+ assert_allclose(res_sparse.cost, 0, atol=1e-20)
587
+
588
+ @pytest.mark.fail_slow(10)
589
+ def test_with_bounds(self):
590
+ p = BroydenTridiagonal()
591
+ for jac, jac_sparsity in product(
592
+ [p.jac, '2-point', '3-point', 'cs'], [None, p.sparsity]):
593
+ res_1 = least_squares(
594
+ p.fun, p.x0, jac, bounds=(p.lb, np.inf),
595
+ method=self.method,jac_sparsity=jac_sparsity)
596
+ res_2 = least_squares(
597
+ p.fun, p.x0, jac, bounds=(-np.inf, p.ub),
598
+ method=self.method, jac_sparsity=jac_sparsity)
599
+ res_3 = least_squares(
600
+ p.fun, p.x0, jac, bounds=(p.lb, p.ub),
601
+ method=self.method, jac_sparsity=jac_sparsity)
602
+ assert_allclose(res_1.optimality, 0, atol=1e-10)
603
+ assert_allclose(res_2.optimality, 0, atol=1e-10)
604
+ assert_allclose(res_3.optimality, 0, atol=1e-10)
605
+
606
+ def test_wrong_jac_sparsity(self):
607
+ p = BroydenTridiagonal()
608
+ sparsity = p.sparsity[:-1]
609
+ assert_raises(ValueError, least_squares, p.fun, p.x0,
610
+ jac_sparsity=sparsity, method=self.method)
611
+
612
+ def test_linear_operator(self):
613
+ p = BroydenTridiagonal(mode='operator')
614
+ res = least_squares(p.fun, p.x0, p.jac, method=self.method)
615
+ assert_allclose(res.cost, 0.0, atol=1e-20)
616
+ assert_raises(ValueError, least_squares, p.fun, p.x0, p.jac,
617
+ method=self.method, tr_solver='exact')
618
+
619
+ def test_x_scale_jac_scale(self):
620
+ p = BroydenTridiagonal()
621
+ res = least_squares(p.fun, p.x0, p.jac, method=self.method,
622
+ x_scale='jac')
623
+ assert_allclose(res.cost, 0.0, atol=1e-20)
624
+
625
+ p = BroydenTridiagonal(mode='operator')
626
+ assert_raises(ValueError, least_squares, p.fun, p.x0, p.jac,
627
+ method=self.method, x_scale='jac')
628
+
629
+
630
+ class LossFunctionMixin:
631
+ def test_options(self):
632
+ for loss in LOSSES:
633
+ res = least_squares(fun_trivial, 2.0, loss=loss,
634
+ method=self.method)
635
+ assert_allclose(res.x, 0, atol=1e-15)
636
+
637
+ assert_raises(ValueError, least_squares, fun_trivial, 2.0,
638
+ loss='hinge', method=self.method)
639
+
640
+ def test_fun(self):
641
+ # Test that res.fun is actual residuals, and not modified by loss
642
+ # function stuff.
643
+ for loss in LOSSES:
644
+ res = least_squares(fun_trivial, 2.0, loss=loss,
645
+ method=self.method)
646
+ assert_equal(res.fun, fun_trivial(res.x))
647
+
648
+ def test_grad(self):
649
+ # Test that res.grad is true gradient of loss function at the
650
+ # solution. Use max_nfev = 1, to avoid reaching minimum.
651
+ x = np.array([2.0]) # res.x will be this.
652
+
653
+ res = least_squares(fun_trivial, x, jac_trivial, loss='linear',
654
+ max_nfev=1, method=self.method)
655
+ assert_equal(res.grad, 2 * x * (x**2 + 5))
656
+
657
+ res = least_squares(fun_trivial, x, jac_trivial, loss='huber',
658
+ max_nfev=1, method=self.method)
659
+ assert_equal(res.grad, 2 * x)
660
+
661
+ res = least_squares(fun_trivial, x, jac_trivial, loss='soft_l1',
662
+ max_nfev=1, method=self.method)
663
+ assert_allclose(res.grad,
664
+ 2 * x * (x**2 + 5) / (1 + (x**2 + 5)**2)**0.5)
665
+
666
+ res = least_squares(fun_trivial, x, jac_trivial, loss='cauchy',
667
+ max_nfev=1, method=self.method)
668
+ assert_allclose(res.grad, 2 * x * (x**2 + 5) / (1 + (x**2 + 5)**2))
669
+
670
+ res = least_squares(fun_trivial, x, jac_trivial, loss='arctan',
671
+ max_nfev=1, method=self.method)
672
+ assert_allclose(res.grad, 2 * x * (x**2 + 5) / (1 + (x**2 + 5)**4))
673
+
674
+ res = least_squares(fun_trivial, x, jac_trivial, loss=cubic_soft_l1,
675
+ max_nfev=1, method=self.method)
676
+ assert_allclose(res.grad,
677
+ 2 * x * (x**2 + 5) / (1 + (x**2 + 5)**2)**(2/3))
678
+
679
+ def test_jac(self):
680
+ # Test that res.jac.T.dot(res.jac) gives Gauss-Newton approximation
681
+ # of Hessian. This approximation is computed by doubly differentiating
682
+ # the cost function and dropping the part containing second derivative
683
+ # of f. For a scalar function it is computed as
684
+ # H = (rho' + 2 * rho'' * f**2) * f'**2, if the expression inside the
685
+ # brackets is less than EPS it is replaced by EPS. Here, we check
686
+ # against the root of H.
687
+
688
+ x = 2.0 # res.x will be this.
689
+ f = x**2 + 5 # res.fun will be this.
690
+
691
+ res = least_squares(fun_trivial, x, jac_trivial, loss='linear',
692
+ max_nfev=1, method=self.method)
693
+ assert_equal(res.jac, 2 * x)
694
+
695
+ # For `huber` loss the Jacobian correction is identically zero
696
+ # in outlier region, in such cases it is modified to be equal EPS**0.5.
697
+ res = least_squares(fun_trivial, x, jac_trivial, loss='huber',
698
+ max_nfev=1, method=self.method)
699
+ assert_equal(res.jac, 2 * x * EPS**0.5)
700
+
701
+ # Now, let's apply `loss_scale` to turn the residual into an inlier.
702
+ # The loss function becomes linear.
703
+ res = least_squares(fun_trivial, x, jac_trivial, loss='huber',
704
+ f_scale=10, max_nfev=1)
705
+ assert_equal(res.jac, 2 * x)
706
+
707
+ # 'soft_l1' always gives a positive scaling.
708
+ res = least_squares(fun_trivial, x, jac_trivial, loss='soft_l1',
709
+ max_nfev=1, method=self.method)
710
+ assert_allclose(res.jac, 2 * x * (1 + f**2)**-0.75)
711
+
712
+ # For 'cauchy' the correction term turns out to be negative, and it
713
+ # replaced by EPS**0.5.
714
+ res = least_squares(fun_trivial, x, jac_trivial, loss='cauchy',
715
+ max_nfev=1, method=self.method)
716
+ assert_allclose(res.jac, 2 * x * EPS**0.5)
717
+
718
+ # Now use scaling to turn the residual to inlier.
719
+ res = least_squares(fun_trivial, x, jac_trivial, loss='cauchy',
720
+ f_scale=10, max_nfev=1, method=self.method)
721
+ fs = f / 10
722
+ assert_allclose(res.jac, 2 * x * (1 - fs**2)**0.5 / (1 + fs**2))
723
+
724
+ # 'arctan' gives an outlier.
725
+ res = least_squares(fun_trivial, x, jac_trivial, loss='arctan',
726
+ max_nfev=1, method=self.method)
727
+ assert_allclose(res.jac, 2 * x * EPS**0.5)
728
+
729
+ # Turn to inlier.
730
+ res = least_squares(fun_trivial, x, jac_trivial, loss='arctan',
731
+ f_scale=20.0, max_nfev=1, method=self.method)
732
+ fs = f / 20
733
+ assert_allclose(res.jac, 2 * x * (1 - 3 * fs**4)**0.5 / (1 + fs**4))
734
+
735
+ # cubic_soft_l1 will give an outlier.
736
+ res = least_squares(fun_trivial, x, jac_trivial, loss=cubic_soft_l1,
737
+ max_nfev=1)
738
+ assert_allclose(res.jac, 2 * x * EPS**0.5)
739
+
740
+ # Turn to inlier.
741
+ res = least_squares(fun_trivial, x, jac_trivial,
742
+ loss=cubic_soft_l1, f_scale=6, max_nfev=1)
743
+ fs = f / 6
744
+ assert_allclose(res.jac,
745
+ 2 * x * (1 - fs**2 / 3)**0.5 * (1 + fs**2)**(-5/6))
746
+
747
+ def test_robustness(self):
748
+ for noise in [0.1, 1.0]:
749
+ p = ExponentialFittingProblem(1, 0.1, noise, rng=12220903)
750
+
751
+ for jac in ['2-point', '3-point', 'cs', p.jac]:
752
+ res_lsq = least_squares(p.fun, p.p0, jac=jac,
753
+ method=self.method)
754
+ assert_allclose(res_lsq.optimality, 0, atol=1e-2)
755
+ for loss in LOSSES:
756
+ if loss == 'linear':
757
+ continue
758
+ res_robust = least_squares(
759
+ p.fun, p.p0, jac=jac, loss=loss, f_scale=noise,
760
+ method=self.method)
761
+ assert_allclose(res_robust.optimality, 0, atol=1e-2)
762
+ assert_(norm(res_robust.x - p.p_opt) <
763
+ norm(res_lsq.x - p.p_opt))
764
+
765
+
766
+ class TestDogbox(BaseMixin, BoundsMixin, SparseMixin, LossFunctionMixin):
767
+ method = 'dogbox'
768
+
769
+
770
+ class TestTRF(BaseMixin, BoundsMixin, SparseMixin, LossFunctionMixin):
771
+ method = 'trf'
772
+
773
+ def test_lsmr_regularization(self):
774
+ p = BroydenTridiagonal()
775
+ for regularize in [True, False]:
776
+ res = least_squares(p.fun, p.x0, p.jac, method='trf',
777
+ tr_options={'regularize': regularize})
778
+ assert_allclose(res.cost, 0, atol=1e-20)
779
+
780
+
781
+ class TestLM(BaseMixin):
782
+ method = 'lm'
783
+
784
+ def test_bounds_not_supported(self):
785
+ assert_raises(ValueError, least_squares, fun_trivial,
786
+ 2.0, bounds=(-3.0, 3.0), method='lm')
787
+
788
+ def test_m_less_n_not_supported(self):
789
+ x0 = [-2, 1]
790
+ assert_raises(ValueError, least_squares, fun_rosenbrock_cropped, x0,
791
+ method='lm')
792
+
793
+ def test_sparse_not_supported(self):
794
+ p = BroydenTridiagonal()
795
+ assert_raises(ValueError, least_squares, p.fun, p.x0, p.jac,
796
+ method='lm')
797
+
798
+ def test_jac_sparsity_not_supported(self):
799
+ assert_raises(ValueError, least_squares, fun_trivial, 2.0,
800
+ jac_sparsity=[1], method='lm')
801
+
802
+ def test_LinearOperator_not_supported(self):
803
+ p = BroydenTridiagonal(mode="operator")
804
+ assert_raises(ValueError, least_squares, p.fun, p.x0, p.jac,
805
+ method='lm')
806
+
807
+ def test_loss(self):
808
+ res = least_squares(fun_trivial, 2.0, loss='linear', method='lm')
809
+ assert_allclose(res.x, 0.0, atol=1e-4)
810
+
811
+ assert_raises(ValueError, least_squares, fun_trivial, 2.0,
812
+ method='lm', loss='huber')
813
+
814
+ def test_callback_with_lm_method(self):
815
+ def callback(x):
816
+ assert(False) # Dummy callback function
817
+
818
+ with warnings.catch_warnings():
819
+ warnings.filterwarnings(
820
+ "ignore",
821
+ "Callback function specified, but not supported with `lm` method.",
822
+ UserWarning,
823
+ )
824
+ least_squares(fun_trivial, x0=[0], method='lm', callback=callback)
825
+
826
+
827
+ def test_basic():
828
+ # test that 'method' arg is really optional
829
+ res = least_squares(fun_trivial, 2.0)
830
+ assert_allclose(res.x, 0, atol=1e-10)
831
+
832
+
833
+ def test_callback():
834
+ # test that callback function works as expected
835
+
836
+ results = []
837
+
838
+ def my_callback_optimresult(intermediate_result: OptimizeResult):
839
+ results.append(intermediate_result)
840
+
841
+ def my_callback_x(x):
842
+ r = OptimizeResult()
843
+ r.nit = 1
844
+ r.x = x
845
+ results.append(r)
846
+ return False
847
+
848
+ def my_callback_optimresult_stop_exception(
849
+ intermediate_result: OptimizeResult):
850
+ results.append(intermediate_result)
851
+ raise StopIteration
852
+
853
+ def my_callback_x_stop_exception(x):
854
+ r = OptimizeResult()
855
+ r.nit = 1
856
+ r.x = x
857
+ results.append(r)
858
+ raise StopIteration
859
+
860
+ # Try for different function signatures and stop methods
861
+ callbacks_nostop = [my_callback_optimresult, my_callback_x]
862
+ callbacks_stop = [my_callback_optimresult_stop_exception,
863
+ my_callback_x_stop_exception]
864
+
865
+ # Try for all the implemented methods: trf, trf_bounds and dogbox
866
+ calls = [
867
+ lambda callback: least_squares(fun_trivial, 5.0, method='trf',
868
+ callback=callback),
869
+ lambda callback: least_squares(fun_trivial, 5.0, method='trf',
870
+ bounds=(-8.0, 8.0), callback=callback),
871
+ lambda callback: least_squares(fun_trivial, 5.0, method='dogbox',
872
+ callback=callback)
873
+ ]
874
+
875
+ for mycallback, call in product(callbacks_nostop, calls):
876
+ results.clear()
877
+ # Call the different implemented methods
878
+ res = call(mycallback)
879
+ # Check that callback was called
880
+ assert len(results) > 0
881
+ # Check that results data makes sense
882
+ assert results[-1].nit > 0
883
+ # Check that it didn't stop because of the callback
884
+ assert res.status != -2
885
+ # final callback x should be same as final result
886
+ assert_allclose(results[-1].x, res.x)
887
+
888
+ for mycallback, call in product(callbacks_stop, calls):
889
+ results.clear()
890
+ # Call the different implemented methods
891
+ res = call(mycallback)
892
+ # Check that callback was called
893
+ assert len(results) > 0
894
+ # Check that only one iteration was run
895
+ assert results[-1].nit == 1
896
+ # Check that it stopped because of the callback
897
+ assert res.status == -2
898
+
899
+
900
+ def test_small_tolerances_for_lm():
901
+ for ftol, xtol, gtol in [(None, 1e-13, 1e-13),
902
+ (1e-13, None, 1e-13),
903
+ (1e-13, 1e-13, None)]:
904
+ assert_raises(ValueError, least_squares, fun_trivial, 2.0, xtol=xtol,
905
+ ftol=ftol, gtol=gtol, method='lm')
906
+
907
+
908
+ def test_fp32_gh12991():
909
+ # checks that smaller FP sizes can be used in least_squares
910
+ # this is the minimum working example reported for gh12991
911
+ rng = np.random.default_rng(1978)
912
+
913
+ x = np.linspace(0, 1, 100, dtype=np.float32)
914
+ y = rng.random(size=100, dtype=np.float32)
915
+
916
+ # changed in gh21872. These functions should've been working in fp32 to force
917
+ # approx_derivative to work in fp32. One of the initial steps in least_squares
918
+ # is to force x0 (p) to be a float, meaning that the output of func and err would
919
+ # be in float64, unless forced to be in float32
920
+ def func(p, x):
921
+ return (p[0] + p[1] * x).astype(np.float32)
922
+
923
+ def err(p, x, y):
924
+ return (func(p, x) - y).astype(np.float32)
925
+
926
+ def mse(p, x, y):
927
+ return np.sum(err(p, x, y)**2)
928
+
929
+ res = least_squares(err, [-1.0, -1.0], args=(x, y))
930
+ # previously the initial jacobian calculated for this would be all 0
931
+ # and the minimize would terminate immediately, with nfev=1, would
932
+ # report a successful minimization (it shouldn't have done), but be
933
+ # unchanged from the initial solution.
934
+ # It was terminating early because the underlying approx_derivative
935
+ # used a step size for FP64 when the working space was FP32.
936
+ assert res.nfev > 2
937
+ # compare output to solver that doesn't use derivatives
938
+ res2 = minimize(
939
+ mse,
940
+ [-1.0, 1.0],
941
+ method='cobyqa',
942
+ args=(x, y),
943
+ options={'final_tr_radius': 1e-6}
944
+ )
945
+ assert_allclose(res.x, res2.x, atol=9e-5)
946
+
947
+
948
+ def test_gh_18793_and_19351():
949
+ answer = 1e-12
950
+ initial_guess = 1.1e-12
951
+
952
+ def chi2(x):
953
+ return (x-answer)**2
954
+
955
+ gtol = 1e-15
956
+ res = least_squares(chi2, x0=initial_guess, gtol=1e-15, bounds=(0, np.inf))
957
+ # Original motivation: gh-18793
958
+ # if we choose an initial condition that is close to the solution
959
+ # we shouldn't return an answer that is further away from the solution
960
+
961
+ # Update: gh-19351
962
+ # However this requirement does not go well with 'trf' algorithm logic.
963
+ # Some regressions were reported after the presumed fix.
964
+ # The returned solution is good as long as it satisfies the convergence
965
+ # conditions.
966
+ # Specifically in this case the scaled gradient will be sufficiently low.
967
+
968
+ scaling, _ = CL_scaling_vector(res.x, res.grad,
969
+ np.atleast_1d(0), np.atleast_1d(np.inf))
970
+ assert res.status == 1 # Converged by gradient
971
+ assert np.linalg.norm(res.grad * scaling, ord=np.inf) < gtol
972
+
973
+
974
+ def test_gh_19103():
975
+ # Checks that least_squares trf method selects a strictly feasible point,
976
+ # and thus succeeds instead of failing,
977
+ # when the initial guess is reported exactly at a boundary point.
978
+ # This is a reduced example from gh191303
979
+
980
+ ydata = np.array([0.] * 66 + [
981
+ 1., 0., 0., 0., 0., 0., 1., 1., 0., 0., 1.,
982
+ 1., 1., 1., 0., 0., 0., 1., 0., 0., 2., 1.,
983
+ 0., 3., 1., 6., 5., 0., 0., 2., 8., 4., 4.,
984
+ 6., 9., 7., 2., 7., 8., 2., 13., 9., 8., 11.,
985
+ 10., 13., 14., 19., 11., 15., 18., 26., 19., 32., 29.,
986
+ 28., 36., 32., 35., 36., 43., 52., 32., 58., 56., 52.,
987
+ 67., 53., 72., 88., 77., 95., 94., 84., 86., 101., 107.,
988
+ 108., 118., 96., 115., 138., 137.,
989
+ ])
990
+ xdata = np.arange(0, ydata.size) * 0.1
991
+
992
+ def exponential_wrapped(params):
993
+ A, B, x0 = params
994
+ return A * np.exp(B * (xdata - x0)) - ydata
995
+
996
+ x0 = [0.01, 1., 5.]
997
+ bounds = ((0.01, 0, 0), (np.inf, 10, 20.9))
998
+ res = least_squares(exponential_wrapped, x0, method='trf', bounds=bounds)
999
+ assert res.success
loan-advisor-agent/.venv/Lib/site-packages/scipy/optimize/tests/test_linear_assignment.py ADDED
@@ -0,0 +1,116 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Author: Brian M. Clapper, G. Varoquaux, Lars Buitinck
2
+ # License: BSD
3
+
4
+ from numpy.testing import assert_array_equal
5
+ import pytest
6
+
7
+ import numpy as np
8
+
9
+ from scipy.optimize import linear_sum_assignment
10
+ from scipy.sparse import random_array
11
+ from scipy.sparse._sputils import matrix
12
+ from scipy.sparse.csgraph import min_weight_full_bipartite_matching
13
+ from scipy.sparse.csgraph.tests.test_matching import (
14
+ linear_sum_assignment_assertions, linear_sum_assignment_test_cases
15
+ )
16
+
17
+
18
+ def test_linear_sum_assignment_input_shape():
19
+ with pytest.raises(ValueError, match="expected a matrix"):
20
+ linear_sum_assignment([1, 2, 3])
21
+
22
+
23
+ def test_linear_sum_assignment_input_object():
24
+ C = [[1, 2, 3], [4, 5, 6]]
25
+ assert_array_equal(linear_sum_assignment(C),
26
+ linear_sum_assignment(np.asarray(C)))
27
+ assert_array_equal(linear_sum_assignment(C),
28
+ linear_sum_assignment(matrix(C)))
29
+
30
+
31
+ def test_linear_sum_assignment_input_bool():
32
+ I = np.identity(3)
33
+ assert_array_equal(linear_sum_assignment(I.astype(np.bool_)),
34
+ linear_sum_assignment(I))
35
+
36
+
37
+ def test_linear_sum_assignment_input_string():
38
+ I = np.identity(3)
39
+ with pytest.raises(TypeError, match="Cannot cast array data"):
40
+ linear_sum_assignment(I.astype(str))
41
+
42
+
43
+ def test_linear_sum_assignment_input_nan():
44
+ I = np.diag([np.nan, 1, 1])
45
+ with pytest.raises(ValueError, match="contains invalid numeric entries"):
46
+ linear_sum_assignment(I)
47
+
48
+
49
+ def test_linear_sum_assignment_input_neginf():
50
+ I = np.diag([1, -np.inf, 1])
51
+ with pytest.raises(ValueError, match="contains invalid numeric entries"):
52
+ linear_sum_assignment(I)
53
+
54
+
55
+ def test_linear_sum_assignment_input_inf():
56
+ I = np.identity(3)
57
+ I[:, 0] = np.inf
58
+ with pytest.raises(ValueError, match="cost matrix is infeasible"):
59
+ linear_sum_assignment(I)
60
+
61
+
62
+ def test_constant_cost_matrix():
63
+ # Fixes #11602
64
+ n = 8
65
+ C = np.ones((n, n))
66
+ row_ind, col_ind = linear_sum_assignment(C)
67
+ assert_array_equal(row_ind, np.arange(n))
68
+ assert_array_equal(col_ind, np.arange(n))
69
+
70
+
71
+ @pytest.mark.parametrize('num_rows,num_cols', [(0, 0), (2, 0), (0, 3)])
72
+ def test_linear_sum_assignment_trivial_cost(num_rows, num_cols):
73
+ C = np.empty(shape=(num_cols, num_rows))
74
+ row_ind, col_ind = linear_sum_assignment(C)
75
+ assert len(row_ind) == 0
76
+ assert len(col_ind) == 0
77
+
78
+
79
+ @pytest.mark.parametrize('sign,test_case', linear_sum_assignment_test_cases)
80
+ def test_linear_sum_assignment_small_inputs(sign, test_case):
81
+ linear_sum_assignment_assertions(
82
+ linear_sum_assignment, np.array, sign, test_case)
83
+
84
+
85
+ # Tests that combine scipy.optimize.linear_sum_assignment and
86
+ # scipy.sparse.csgraph.min_weight_full_bipartite_matching
87
+ def test_two_methods_give_same_result_on_many_sparse_inputs():
88
+ # As opposed to the test above, here we do not spell out the expected
89
+ # output; only assert that the two methods give the same result.
90
+ # Concretely, the below tests 100 cases of size 100x100, out of which
91
+ # 36 are infeasible.
92
+ rng = np.random.default_rng(12342309)
93
+ for _ in range(100):
94
+ lsa_raises = False
95
+ mwfbm_raises = False
96
+ sparse = random_array((100, 100), density=0.06,
97
+ data_sampler=lambda size: rng.integers(1, 100, size))
98
+ # In csgraph, zeros correspond to missing edges, so we explicitly
99
+ # replace those with infinities
100
+ dense = np.full(sparse.shape, np.inf)
101
+ dense[sparse.row, sparse.col] = sparse.data
102
+ sparse = sparse.tocsr()
103
+ try:
104
+ row_ind, col_ind = linear_sum_assignment(dense)
105
+ lsa_cost = dense[row_ind, col_ind].sum()
106
+ except ValueError:
107
+ lsa_raises = True
108
+ try:
109
+ row_ind, col_ind = min_weight_full_bipartite_matching(sparse)
110
+ mwfbm_cost = sparse[row_ind, col_ind].sum()
111
+ except ValueError:
112
+ mwfbm_raises = True
113
+ # Ensure that if one method raises, so does the other one.
114
+ assert lsa_raises == mwfbm_raises
115
+ if not lsa_raises:
116
+ assert lsa_cost == mwfbm_cost
loan-advisor-agent/.venv/Lib/site-packages/scipy/optimize/tests/test_linesearch.py ADDED
@@ -0,0 +1,334 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Tests for line search routines
3
+ """
4
+ import warnings
5
+
6
+ from numpy.testing import (assert_equal, assert_array_almost_equal,
7
+ assert_array_almost_equal_nulp)
8
+ import scipy.optimize._linesearch as ls
9
+ from scipy.optimize._linesearch import LineSearchWarning
10
+ import numpy as np
11
+ import pytest
12
+ import threading
13
+
14
+
15
+ def assert_wolfe(s, phi, derphi, c1=1e-4, c2=0.9, err_msg=""):
16
+ """
17
+ Check that strong Wolfe conditions apply
18
+ """
19
+ phi1 = phi(s)
20
+ phi0 = phi(0)
21
+ derphi0 = derphi(0)
22
+ derphi1 = derphi(s)
23
+ msg = (f"s = {s}; phi(0) = {phi0}; phi(s) = {phi1}; phi'(0) = {derphi0};"
24
+ f" phi'(s) = {derphi1}; {err_msg}")
25
+
26
+ assert phi1 <= phi0 + c1*s*derphi0, "Wolfe 1 failed: " + msg
27
+ assert abs(derphi1) <= abs(c2*derphi0), "Wolfe 2 failed: " + msg
28
+
29
+
30
+ def assert_armijo(s, phi, c1=1e-4, err_msg=""):
31
+ """
32
+ Check that Armijo condition applies
33
+ """
34
+ phi1 = phi(s)
35
+ phi0 = phi(0)
36
+ msg = f"s = {s}; phi(0) = {phi0}; phi(s) = {phi1}; {err_msg}"
37
+ assert phi1 <= (1 - c1*s)*phi0, msg
38
+
39
+
40
+ def assert_line_wolfe(x, p, s, f, fprime, **kw):
41
+ assert_wolfe(s, phi=lambda sp: f(x + p*sp),
42
+ derphi=lambda sp: np.dot(fprime(x + p*sp), p), **kw)
43
+
44
+
45
+ def assert_line_armijo(x, p, s, f, **kw):
46
+ assert_armijo(s, phi=lambda sp: f(x + p*sp), **kw)
47
+
48
+
49
+ def assert_fp_equal(x, y, err_msg="", nulp=50):
50
+ """Assert two arrays are equal, up to some floating-point rounding error"""
51
+ try:
52
+ assert_array_almost_equal_nulp(x, y, nulp)
53
+ except AssertionError as e:
54
+ raise AssertionError(f"{e}\n{err_msg}") from e
55
+
56
+
57
+ class TestLineSearch:
58
+ # -- scalar functions; must have dphi(0.) < 0
59
+ def _scalar_func_1(self, s): # skip name check
60
+ if not hasattr(self.fcount, 'c'):
61
+ self.fcount.c = 0
62
+ self.fcount.c += 1
63
+ p = -s - s**3 + s**4
64
+ dp = -1 - 3*s**2 + 4*s**3
65
+ return p, dp
66
+
67
+ def _scalar_func_2(self, s): # skip name check
68
+ if not hasattr(self.fcount, 'c'):
69
+ self.fcount.c = 0
70
+ self.fcount.c += 1
71
+ p = np.exp(-4*s) + s**2
72
+ dp = -4*np.exp(-4*s) + 2*s
73
+ return p, dp
74
+
75
+ def _scalar_func_3(self, s): # skip name check
76
+ if not hasattr(self.fcount, 'c'):
77
+ self.fcount.c = 0
78
+ self.fcount.c += 1
79
+ p = -np.sin(10*s)
80
+ dp = -10*np.cos(10*s)
81
+ return p, dp
82
+
83
+ # -- n-d functions
84
+
85
+ def _line_func_1(self, x): # skip name check
86
+ if not hasattr(self.fcount, 'c'):
87
+ self.fcount.c = 0
88
+ self.fcount.c += 1
89
+ f = np.dot(x, x)
90
+ df = 2*x
91
+ return f, df
92
+
93
+ def _line_func_2(self, x): # skip name check
94
+ if not hasattr(self.fcount, 'c'):
95
+ self.fcount.c = 0
96
+ self.fcount.c += 1
97
+ f = np.dot(x, np.dot(self.A, x)) + 1
98
+ df = np.dot(self.A + self.A.T, x)
99
+ return f, df
100
+
101
+ # --
102
+
103
+ def setup_method(self):
104
+ self.scalar_funcs = []
105
+ self.line_funcs = []
106
+ self.N = 20
107
+ self.fcount = threading.local()
108
+
109
+ def bind_index(func, idx):
110
+ # Remember Python's closure semantics!
111
+ return lambda *a, **kw: func(*a, **kw)[idx]
112
+
113
+ for name in sorted(dir(self)):
114
+ if name.startswith('_scalar_func_'):
115
+ value = getattr(self, name)
116
+ self.scalar_funcs.append(
117
+ (name, bind_index(value, 0), bind_index(value, 1)))
118
+ elif name.startswith('_line_func_'):
119
+ value = getattr(self, name)
120
+ self.line_funcs.append(
121
+ (name, bind_index(value, 0), bind_index(value, 1)))
122
+
123
+ # the choice of seed affects whether the tests pass
124
+ rng = np.random.default_rng(1231892908)
125
+ self.A = rng.standard_normal((self.N, self.N))
126
+
127
+ def scalar_iter(self):
128
+ rng = np.random.default_rng(2231892908)
129
+ for name, phi, derphi in self.scalar_funcs:
130
+ for old_phi0 in rng.standard_normal(3):
131
+ yield name, phi, derphi, old_phi0
132
+
133
+ def line_iter(self):
134
+ rng = np.random.default_rng(2231892908)
135
+ for name, f, fprime in self.line_funcs:
136
+ k = 0
137
+ while k < 9:
138
+ x = rng.standard_normal(self.N)
139
+ p = rng.standard_normal(self.N)
140
+ if np.dot(p, fprime(x)) >= 0:
141
+ # always pick a descent direction
142
+ continue
143
+ k += 1
144
+ old_fv = float(rng.standard_normal())
145
+ yield name, f, fprime, x, p, old_fv
146
+
147
+ # -- Generic scalar searches
148
+
149
+ def test_scalar_search_wolfe1(self):
150
+ c = 0
151
+ for name, phi, derphi, old_phi0 in self.scalar_iter():
152
+ c += 1
153
+ s, phi1, phi0 = ls.scalar_search_wolfe1(phi, derphi, phi(0),
154
+ old_phi0, derphi(0))
155
+ assert_fp_equal(phi0, phi(0), name)
156
+ assert_fp_equal(phi1, phi(s), name)
157
+ assert_wolfe(s, phi, derphi, err_msg=name)
158
+
159
+ assert c > 3 # check that the iterator really works...
160
+
161
+ def test_scalar_search_wolfe2(self):
162
+ for name, phi, derphi, old_phi0 in self.scalar_iter():
163
+ s, phi1, phi0, derphi1 = ls.scalar_search_wolfe2(
164
+ phi, derphi, phi(0), old_phi0, derphi(0))
165
+ assert_fp_equal(phi0, phi(0), name)
166
+ assert_fp_equal(phi1, phi(s), name)
167
+ if derphi1 is not None:
168
+ assert_fp_equal(derphi1, derphi(s), name)
169
+ assert_wolfe(s, phi, derphi, err_msg=f"{name} {old_phi0:g}")
170
+
171
+ def test_scalar_search_wolfe2_with_low_amax(self):
172
+ def phi(alpha):
173
+ return (alpha - 5) ** 2
174
+
175
+ def derphi(alpha):
176
+ return 2 * (alpha - 5)
177
+
178
+ alpha_star, _, _, derphi_star = ls.scalar_search_wolfe2(phi, derphi, amax=0.001)
179
+ assert alpha_star is None # Not converged
180
+ assert derphi_star is None # Not converged
181
+
182
+ def test_scalar_search_wolfe2_regression(self):
183
+ # Regression test for gh-12157
184
+ # This phi has its minimum at alpha=4/3 ~ 1.333.
185
+ def phi(alpha):
186
+ if alpha < 1:
187
+ return - 3*np.pi/2 * (alpha - 1)
188
+ else:
189
+ return np.cos(3*np.pi/2 * alpha - np.pi)
190
+
191
+ def derphi(alpha):
192
+ if alpha < 1:
193
+ return - 3*np.pi/2
194
+ else:
195
+ return - 3*np.pi/2 * np.sin(3*np.pi/2 * alpha - np.pi)
196
+
197
+ s, _, _, _ = ls.scalar_search_wolfe2(phi, derphi)
198
+ # Without the fix in gh-13073, the scalar_search_wolfe2
199
+ # returned s=2.0 instead.
200
+ assert s < 1.5
201
+
202
+ def test_scalar_search_armijo(self):
203
+ for name, phi, derphi, old_phi0 in self.scalar_iter():
204
+ s, phi1 = ls.scalar_search_armijo(phi, phi(0), derphi(0))
205
+ assert_fp_equal(phi1, phi(s), name)
206
+ assert_armijo(s, phi, err_msg=f"{name} {old_phi0:g}")
207
+
208
+ # -- Generic line searches
209
+
210
+ def test_line_search_wolfe1(self):
211
+ c = 0
212
+ smax = 100
213
+ for name, f, fprime, x, p, old_f in self.line_iter():
214
+ f0 = f(x)
215
+ g0 = fprime(x)
216
+ self.fcount.c = 0
217
+ s, fc, gc, fv, ofv, gv = ls.line_search_wolfe1(f, fprime, x, p,
218
+ g0, f0, old_f,
219
+ amax=smax)
220
+ assert_equal(self.fcount.c, fc+gc)
221
+ assert_fp_equal(ofv, f(x))
222
+ if s is None:
223
+ continue
224
+ assert_fp_equal(fv, f(x + s*p))
225
+ assert_array_almost_equal(gv, fprime(x + s*p), decimal=14)
226
+ if s < smax:
227
+ c += 1
228
+ assert_line_wolfe(x, p, s, f, fprime, err_msg=name)
229
+
230
+ assert c > 3 # check that the iterator really works...
231
+
232
+ def test_line_search_wolfe2(self):
233
+ c = 0
234
+ smax = 512
235
+ for name, f, fprime, x, p, old_f in self.line_iter():
236
+ f0 = f(x)
237
+ g0 = fprime(x)
238
+ self.fcount.c = 0
239
+ with warnings.catch_warnings():
240
+ warnings.filterwarnings(
241
+ "ignore",
242
+ "The line search algorithm could not find a solution",
243
+ LineSearchWarning)
244
+ warnings.filterwarnings(
245
+ "ignore",
246
+ "The line search algorithm did not converge",
247
+ LineSearchWarning)
248
+ s, fc, gc, fv, ofv, gv = ls.line_search_wolfe2(f, fprime, x, p,
249
+ g0, f0, old_f,
250
+ amax=smax)
251
+ assert_equal(self.fcount.c, fc+gc)
252
+ assert_fp_equal(ofv, f(x))
253
+ assert_fp_equal(fv, f(x + s*p))
254
+ if gv is not None:
255
+ assert_array_almost_equal(gv, fprime(x + s*p), decimal=14)
256
+ if s < smax:
257
+ c += 1
258
+ assert_line_wolfe(x, p, s, f, fprime, err_msg=name)
259
+ assert c > 3 # check that the iterator really works...
260
+
261
+ def test_line_search_wolfe2_bounds(self):
262
+ # See gh-7475
263
+
264
+ # For this f and p, starting at a point on axis 0, the strong Wolfe
265
+ # condition 2 is met if and only if the step length s satisfies
266
+ # |x + s| <= c2 * |x|
267
+ def f(x):
268
+ return np.dot(x, x)
269
+ def fp(x):
270
+ return 2 * x
271
+ p = np.array([1, 0])
272
+
273
+ # Smallest s satisfying strong Wolfe conditions for these arguments is 30
274
+ x = -60 * p
275
+ c2 = 0.5
276
+
277
+ s, _, _, _, _, _ = ls.line_search_wolfe2(f, fp, x, p, amax=30, c2=c2)
278
+ assert_line_wolfe(x, p, s, f, fp)
279
+
280
+ with pytest.warns(LineSearchWarning):
281
+ s, _, _, _, _, _ = ls.line_search_wolfe2(f, fp, x, p,
282
+ amax=29, c2=c2)
283
+ assert s is None
284
+
285
+ # s=30 will only be tried on the 6th iteration, so this won't converge
286
+ with pytest.warns(LineSearchWarning):
287
+ ls.line_search_wolfe2(f, fp, x, p, c2=c2, maxiter=5)
288
+
289
+ def test_line_search_armijo(self):
290
+ c = 0
291
+ for name, f, fprime, x, p, old_f in self.line_iter():
292
+ f0 = f(x)
293
+ g0 = fprime(x)
294
+ self.fcount.c = 0
295
+ s, fc, fv = ls.line_search_armijo(f, x, p, g0, f0)
296
+ c += 1
297
+ assert_equal(self.fcount.c, fc)
298
+ assert_fp_equal(fv, f(x + s*p))
299
+ assert_line_armijo(x, p, s, f, err_msg=name)
300
+ assert c >= 9
301
+
302
+ # -- More specific tests
303
+
304
+ def test_armijo_terminate_1(self):
305
+ # Armijo should evaluate the function only once if the trial step
306
+ # is already suitable
307
+ count = [0]
308
+
309
+ def phi(s):
310
+ count[0] += 1
311
+ return -s + 0.01*s**2
312
+ s, phi1 = ls.scalar_search_armijo(phi, phi(0), -1, alpha0=1)
313
+ assert_equal(s, 1)
314
+ assert_equal(count[0], 2)
315
+ assert_armijo(s, phi)
316
+
317
+ def test_wolfe_terminate(self):
318
+ # wolfe1 and wolfe2 should also evaluate the function only a few
319
+ # times if the trial step is already suitable
320
+
321
+ def phi(s):
322
+ count[0] += 1
323
+ return -s + 0.05*s**2
324
+
325
+ def derphi(s):
326
+ count[0] += 1
327
+ return -1 + 0.05*2*s
328
+
329
+ for func in [ls.scalar_search_wolfe1, ls.scalar_search_wolfe2]:
330
+ count = [0]
331
+ r = func(phi, derphi, phi(0), None, derphi(0))
332
+ assert r[0] is not None, (r, func)
333
+ assert count[0] <= 2 + 2, (count, func)
334
+ assert_wolfe(r[0], phi, derphi, err_msg=str(func))