_tstutils.py 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676
  1. r"""
  2. Parameters used in test and benchmark methods.
  3. Collections of test cases suitable for testing 1-D root-finders
  4. 'original': The original benchmarking functions.
  5. Real-valued functions of real-valued inputs on an interval
  6. with a zero.
  7. f1, .., f3 are continuous and infinitely differentiable
  8. f4 has a left- and right- discontinuity at the root
  9. f5 has a root at 1 replacing a 1st order pole
  10. f6 is randomly positive on one side of the root,
  11. randomly negative on the other.
  12. f4 - f6 are not continuous at the root.
  13. 'aps': The test problems in the 1995 paper
  14. TOMS "Algorithm 748: Enclosing Zeros of Continuous Functions"
  15. by Alefeld, Potra and Shi. Real-valued functions of
  16. real-valued inputs on an interval with a zero.
  17. Suitable for methods which start with an enclosing interval, and
  18. derivatives up to 2nd order.
  19. 'complex': Some complex-valued functions of complex-valued inputs.
  20. No enclosing bracket is provided.
  21. Suitable for methods which use one or more starting values, and
  22. derivatives up to 2nd order.
  23. The test cases are provided as a list of dictionaries. The dictionary
  24. keys will be a subset of:
  25. ["f", "fprime", "fprime2", "args", "bracket", "smoothness",
  26. "a", "b", "x0", "x1", "root", "ID"]
  27. """
  28. # Sources:
  29. # [1] Alefeld, G. E. and Potra, F. A. and Shi, Yixun,
  30. # "Algorithm 748: Enclosing Zeros of Continuous Functions",
  31. # ACM Trans. Math. Softw. Volume 221(1995)
  32. # doi = {10.1145/210089.210111},
  33. from random import random
  34. import numpy as np
  35. from scipy.optimize import _zeros_py as cc
  36. # "description" refers to the original functions
  37. description = """
  38. f2 is a symmetric parabola, x**2 - 1
  39. f3 is a quartic polynomial with large hump in interval
  40. f4 is step function with a discontinuity at 1
  41. f5 is a hyperbola with vertical asymptote at 1
  42. f6 has random values positive to left of 1, negative to right
  43. Of course, these are not real problems. They just test how the
  44. 'good' solvers behave in bad circumstances where bisection is
  45. really the best. A good solver should not be much worse than
  46. bisection in such circumstance, while being faster for smooth
  47. monotone sorts of functions.
  48. """
  49. def f1(x):
  50. r"""f1 is a quadratic with roots at 0 and 1"""
  51. return x * (x - 1.)
  52. def f1_fp(x):
  53. return 2 * x - 1
  54. def f1_fpp(x):
  55. return 2
  56. def f2(x):
  57. r"""f2 is a symmetric parabola, x**2 - 1"""
  58. return x**2 - 1
  59. def f2_fp(x):
  60. return 2 * x
  61. def f2_fpp(x):
  62. return 2
  63. def f3(x):
  64. r"""A quartic with roots at 0, 1, 2 and 3"""
  65. return x * (x - 1.) * (x - 2.) * (x - 3.) # x**4 - 6x**3 + 11x**2 - 6x
  66. def f3_fp(x):
  67. return 4 * x**3 - 18 * x**2 + 22 * x - 6
  68. def f3_fpp(x):
  69. return 12 * x**2 - 36 * x + 22
  70. def f4(x):
  71. r"""Piecewise linear, left- and right- discontinuous at x=1, the root."""
  72. if x > 1:
  73. return 1.0 + .1 * x
  74. if x < 1:
  75. return -1.0 + .1 * x
  76. return 0
  77. def f5(x):
  78. r"""Hyperbola with a pole at x=1, but pole replaced with 0. Not continuous at root."""
  79. if x != 1:
  80. return 1.0 / (1. - x)
  81. return 0
  82. # f6(x) returns random value. Without memoization, calling twice with the
  83. # same x returns different values, hence a "random value", not a
  84. # "function with random values"
  85. _f6_cache = {}
  86. def f6(x):
  87. v = _f6_cache.get(x, None)
  88. if v is None:
  89. if x > 1:
  90. v = random()
  91. elif x < 1:
  92. v = -random()
  93. else:
  94. v = 0
  95. _f6_cache[x] = v
  96. return v
  97. # Each Original test case has
  98. # - a function and its two derivatives,
  99. # - additional arguments,
  100. # - a bracket enclosing a root,
  101. # - the order of differentiability (smoothness) on this interval
  102. # - a starting value for methods which don't require a bracket
  103. # - the root (inside the bracket)
  104. # - an Identifier of the test case
  105. _ORIGINAL_TESTS_KEYS = ["f", "fprime", "fprime2", "args", "bracket", "smoothness", "x0", "root", "ID"]
  106. _ORIGINAL_TESTS = [
  107. [f1, f1_fp, f1_fpp, (), [0.5, np.sqrt(3)], np.inf, 0.6, 1.0, "original.01.00"],
  108. [f2, f2_fp, f2_fpp, (), [0.5, np.sqrt(3)], np.inf, 0.6, 1.0, "original.02.00"],
  109. [f3, f3_fp, f3_fpp, (), [0.5, np.sqrt(3)], np.inf, 0.6, 1.0, "original.03.00"],
  110. [f4, None, None, (), [0.5, np.sqrt(3)], -1, 0.6, 1.0, "original.04.00"],
  111. [f5, None, None, (), [0.5, np.sqrt(3)], -1, 0.6, 1.0, "original.05.00"],
  112. [f6, None, None, (), [0.5, np.sqrt(3)], -np.inf, 0.6, 1.0, "original.05.00"]
  113. ]
  114. _ORIGINAL_TESTS_DICTS = [dict(zip(_ORIGINAL_TESTS_KEYS, testcase)) for testcase in _ORIGINAL_TESTS]
  115. # ##################
  116. # "APS" test cases
  117. # Functions and test cases that appear in [1]
  118. def aps01_f(x):
  119. r"""Straightforward sum of trigonometric function and polynomial"""
  120. return np.sin(x) - x / 2
  121. def aps01_fp(x):
  122. return np.cos(x) - 1.0 / 2
  123. def aps01_fpp(x):
  124. return -np.sin(x)
  125. def aps02_f(x):
  126. r"""poles at x=n**2, 1st and 2nd derivatives at root are also close to 0"""
  127. ii = np.arange(1, 21)
  128. return -2 * np.sum((2 * ii - 5)**2 / (x - ii**2)**3)
  129. def aps02_fp(x):
  130. ii = np.arange(1, 21)
  131. return 6 * np.sum((2 * ii - 5)**2 / (x - ii**2)**4)
  132. def aps02_fpp(x):
  133. ii = np.arange(1, 21)
  134. return 24 * np.sum((2 * ii - 5)**2 / (x - ii**2)**5)
  135. def aps03_f(x, a, b):
  136. r"""Rapidly changing at the root"""
  137. return a * x * np.exp(b * x)
  138. def aps03_fp(x, a, b):
  139. return a * (b * x + 1) * np.exp(b * x)
  140. def aps03_fpp(x, a, b):
  141. return a * (b * (b * x + 1) + b) * np.exp(b * x)
  142. def aps04_f(x, n, a):
  143. r"""Medium-degree polynomial"""
  144. return x**n - a
  145. def aps04_fp(x, n, a):
  146. return n * x**(n - 1)
  147. def aps04_fpp(x, n, a):
  148. return n * (n - 1) * x**(n - 2)
  149. def aps05_f(x):
  150. r"""Simple Trigonometric function"""
  151. return np.sin(x) - 1.0 / 2
  152. def aps05_fp(x):
  153. return np.cos(x)
  154. def aps05_fpp(x):
  155. return -np.sin(x)
  156. def aps06_f(x, n):
  157. r"""Exponential rapidly changing from -1 to 1 at x=0"""
  158. return 2 * x * np.exp(-n) - 2 * np.exp(-n * x) + 1
  159. def aps06_fp(x, n):
  160. return 2 * np.exp(-n) + 2 * n * np.exp(-n * x)
  161. def aps06_fpp(x, n):
  162. return -2 * n * n * np.exp(-n * x)
  163. def aps07_f(x, n):
  164. r"""Upside down parabola with parametrizable height"""
  165. return (1 + (1 - n)**2) * x - (1 - n * x)**2
  166. def aps07_fp(x, n):
  167. return (1 + (1 - n)**2) + 2 * n * (1 - n * x)
  168. def aps07_fpp(x, n):
  169. return -2 * n * n
  170. def aps08_f(x, n):
  171. r"""Degree n polynomial"""
  172. return x * x - (1 - x)**n
  173. def aps08_fp(x, n):
  174. return 2 * x + n * (1 - x)**(n - 1)
  175. def aps08_fpp(x, n):
  176. return 2 - n * (n - 1) * (1 - x)**(n - 2)
  177. def aps09_f(x, n):
  178. r"""Upside down quartic with parametrizable height"""
  179. return (1 + (1 - n)**4) * x - (1 - n * x)**4
  180. def aps09_fp(x, n):
  181. return (1 + (1 - n)**4) + 4 * n * (1 - n * x)**3
  182. def aps09_fpp(x, n):
  183. return -12 * n * (1 - n * x)**2
  184. def aps10_f(x, n):
  185. r"""Exponential plus a polynomial"""
  186. return np.exp(-n * x) * (x - 1) + x**n
  187. def aps10_fp(x, n):
  188. return np.exp(-n * x) * (-n * (x - 1) + 1) + n * x**(n - 1)
  189. def aps10_fpp(x, n):
  190. return np.exp(-n * x) * (-n * (-n * (x - 1) + 1) + -n * x) + n * (n - 1) * x**(n - 2)
  191. def aps11_f(x, n):
  192. r"""Rational function with a zero at x=1/n and a pole at x=0"""
  193. return (n * x - 1) / ((n - 1) * x)
  194. def aps11_fp(x, n):
  195. return 1 / (n - 1) / x**2
  196. def aps11_fpp(x, n):
  197. return -2 / (n - 1) / x**3
  198. def aps12_f(x, n):
  199. r"""nth root of x, with a zero at x=n"""
  200. return np.power(x, 1.0 / n) - np.power(n, 1.0 / n)
  201. def aps12_fp(x, n):
  202. return np.power(x, (1.0 - n) / n) / n
  203. def aps12_fpp(x, n):
  204. return np.power(x, (1.0 - 2 * n) / n) * (1.0 / n) * (1.0 - n) / n
  205. _MAX_EXPABLE = np.log(np.finfo(float).max)
  206. def aps13_f(x):
  207. r"""Function with *all* derivatives 0 at the root"""
  208. if x == 0:
  209. return 0
  210. # x2 = 1.0/x**2
  211. # if x2 > 708:
  212. # return 0
  213. y = 1 / x**2
  214. if y > _MAX_EXPABLE:
  215. return 0
  216. return x / np.exp(y)
  217. def aps13_fp(x):
  218. if x == 0:
  219. return 0
  220. y = 1 / x**2
  221. if y > _MAX_EXPABLE:
  222. return 0
  223. return (1 + 2 / x**2) / np.exp(y)
  224. def aps13_fpp(x):
  225. if x == 0:
  226. return 0
  227. y = 1 / x**2
  228. if y > _MAX_EXPABLE:
  229. return 0
  230. return 2 * (2 - x**2) / x**5 / np.exp(y)
  231. def aps14_f(x, n):
  232. r"""0 for negative x-values, trigonometric+linear for x positive"""
  233. if x <= 0:
  234. return -n / 20.0
  235. return n / 20.0 * (x / 1.5 + np.sin(x) - 1)
  236. def aps14_fp(x, n):
  237. if x <= 0:
  238. return 0
  239. return n / 20.0 * (1.0 / 1.5 + np.cos(x))
  240. def aps14_fpp(x, n):
  241. if x <= 0:
  242. return 0
  243. return -n / 20.0 * (np.sin(x))
  244. def aps15_f(x, n):
  245. r"""piecewise linear, constant outside of [0, 0.002/(1+n)]"""
  246. if x < 0:
  247. return -0.859
  248. if x > 2 * 1e-3 / (1 + n):
  249. return np.e - 1.859
  250. return np.exp((n + 1) * x / 2 * 1000) - 1.859
  251. def aps15_fp(x, n):
  252. if not 0 <= x <= 2 * 1e-3 / (1 + n):
  253. return np.e - 1.859
  254. return np.exp((n + 1) * x / 2 * 1000) * (n + 1) / 2 * 1000
  255. def aps15_fpp(x, n):
  256. if not 0 <= x <= 2 * 1e-3 / (1 + n):
  257. return np.e - 1.859
  258. return np.exp((n + 1) * x / 2 * 1000) * (n + 1) / 2 * 1000 * (n + 1) / 2 * 1000
  259. # Each APS test case has
  260. # - a function and its two derivatives,
  261. # - additional arguments,
  262. # - a bracket enclosing a root,
  263. # - the order of differentiability of the function on this interval
  264. # - a starting value for methods which don't require a bracket
  265. # - the root (inside the bracket)
  266. # - an Identifier of the test case
  267. #
  268. # Algorithm 748 is a bracketing algorithm so a bracketing interval was provided
  269. # in [1] for each test case. Newton and Halley methods need a single
  270. # starting point x0, which was chosen to be near the middle of the interval,
  271. # unless that would have made the problem too easy.
  272. _APS_TESTS_KEYS = ["f", "fprime", "fprime2", "args", "bracket", "smoothness", "x0", "root", "ID"]
  273. _APS_TESTS = [
  274. [aps01_f, aps01_fp, aps01_fpp, (), [np.pi / 2, np.pi], np.inf, 3, 1.89549426703398094e+00, "aps.01.00"],
  275. [aps02_f, aps02_fp, aps02_fpp, (), [1 + 1e-9, 4 - 1e-9], np.inf, 2, 3.02291534727305677e+00, "aps.02.00"],
  276. [aps02_f, aps02_fp, aps02_fpp, (), [4 + 1e-9, 9 - 1e-9], np.inf, 5, 6.68375356080807848e+00, "aps.02.01"],
  277. [aps02_f, aps02_fp, aps02_fpp, (), [9 + 1e-9, 16 - 1e-9], np.inf, 10, 1.12387016550022114e+01, "aps.02.02"],
  278. [aps02_f, aps02_fp, aps02_fpp, (), [16 + 1e-9, 25 - 1e-9], np.inf, 17, 1.96760000806234103e+01, "aps.02.03"],
  279. [aps02_f, aps02_fp, aps02_fpp, (), [25 + 1e-9, 36 - 1e-9], np.inf, 26, 2.98282273265047557e+01, "aps.02.04"],
  280. [aps02_f, aps02_fp, aps02_fpp, (), [36 + 1e-9, 49 - 1e-9], np.inf, 37, 4.19061161952894139e+01, "aps.02.05"],
  281. [aps02_f, aps02_fp, aps02_fpp, (), [49 + 1e-9, 64 - 1e-9], np.inf, 50, 5.59535958001430913e+01, "aps.02.06"],
  282. [aps02_f, aps02_fp, aps02_fpp, (), [64 + 1e-9, 81 - 1e-9], np.inf, 65, 7.19856655865877997e+01, "aps.02.07"],
  283. [aps02_f, aps02_fp, aps02_fpp, (), [81 + 1e-9, 100 - 1e-9], np.inf, 82, 9.00088685391666701e+01, "aps.02.08"],
  284. [aps02_f, aps02_fp, aps02_fpp, (), [100 + 1e-9, 121 - 1e-9], np.inf, 101, 1.10026532748330197e+02, "aps.02.09"],
  285. [aps03_f, aps03_fp, aps03_fpp, (-40, -1), [-9, 31], np.inf, -2, 0, "aps.03.00"],
  286. [aps03_f, aps03_fp, aps03_fpp, (-100, -2), [-9, 31], np.inf, -2, 0, "aps.03.01"],
  287. [aps03_f, aps03_fp, aps03_fpp, (-200, -3), [-9, 31], np.inf, -2, 0, "aps.03.02"],
  288. [aps04_f, aps04_fp, aps04_fpp, (4, 0.2), [0, 5], np.inf, 2.5, 6.68740304976422006e-01, "aps.04.00"],
  289. [aps04_f, aps04_fp, aps04_fpp, (6, 0.2), [0, 5], np.inf, 2.5, 7.64724491331730039e-01, "aps.04.01"],
  290. [aps04_f, aps04_fp, aps04_fpp, (8, 0.2), [0, 5], np.inf, 2.5, 8.17765433957942545e-01, "aps.04.02"],
  291. [aps04_f, aps04_fp, aps04_fpp, (10, 0.2), [0, 5], np.inf, 2.5, 8.51339922520784609e-01, "aps.04.03"],
  292. [aps04_f, aps04_fp, aps04_fpp, (12, 0.2), [0, 5], np.inf, 2.5, 8.74485272221167897e-01, "aps.04.04"],
  293. [aps04_f, aps04_fp, aps04_fpp, (4, 1), [0, 5], np.inf, 2.5, 1, "aps.04.05"],
  294. [aps04_f, aps04_fp, aps04_fpp, (6, 1), [0, 5], np.inf, 2.5, 1, "aps.04.06"],
  295. [aps04_f, aps04_fp, aps04_fpp, (8, 1), [0, 5], np.inf, 2.5, 1, "aps.04.07"],
  296. [aps04_f, aps04_fp, aps04_fpp, (10, 1), [0, 5], np.inf, 2.5, 1, "aps.04.08"],
  297. [aps04_f, aps04_fp, aps04_fpp, (12, 1), [0, 5], np.inf, 2.5, 1, "aps.04.09"],
  298. [aps04_f, aps04_fp, aps04_fpp, (8, 1), [-0.95, 4.05], np.inf, 1.5, 1, "aps.04.10"],
  299. [aps04_f, aps04_fp, aps04_fpp, (10, 1), [-0.95, 4.05], np.inf, 1.5, 1, "aps.04.11"],
  300. [aps04_f, aps04_fp, aps04_fpp, (12, 1), [-0.95, 4.05], np.inf, 1.5, 1, "aps.04.12"],
  301. [aps04_f, aps04_fp, aps04_fpp, (14, 1), [-0.95, 4.05], np.inf, 1.5, 1, "aps.04.13"],
  302. [aps05_f, aps05_fp, aps05_fpp, (), [0, 1.5], np.inf, 1.3, np.pi / 6, "aps.05.00"],
  303. [aps06_f, aps06_fp, aps06_fpp, (1,), [0, 1], np.inf, 0.5, 4.22477709641236709e-01, "aps.06.00"],
  304. [aps06_f, aps06_fp, aps06_fpp, (2,), [0, 1], np.inf, 0.5, 3.06699410483203705e-01, "aps.06.01"],
  305. [aps06_f, aps06_fp, aps06_fpp, (3,), [0, 1], np.inf, 0.5, 2.23705457654662959e-01, "aps.06.02"],
  306. [aps06_f, aps06_fp, aps06_fpp, (4,), [0, 1], np.inf, 0.5, 1.71719147519508369e-01, "aps.06.03"],
  307. [aps06_f, aps06_fp, aps06_fpp, (5,), [0, 1], np.inf, 0.4, 1.38257155056824066e-01, "aps.06.04"],
  308. [aps06_f, aps06_fp, aps06_fpp, (20,), [0, 1], np.inf, 0.1, 3.46573590208538521e-02, "aps.06.05"],
  309. [aps06_f, aps06_fp, aps06_fpp, (40,), [0, 1], np.inf, 5e-02, 1.73286795139986315e-02, "aps.06.06"],
  310. [aps06_f, aps06_fp, aps06_fpp, (60,), [0, 1], np.inf, 1.0 / 30, 1.15524530093324210e-02, "aps.06.07"],
  311. [aps06_f, aps06_fp, aps06_fpp, (80,), [0, 1], np.inf, 2.5e-02, 8.66433975699931573e-03, "aps.06.08"],
  312. [aps06_f, aps06_fp, aps06_fpp, (100,), [0, 1], np.inf, 2e-02, 6.93147180559945415e-03, "aps.06.09"],
  313. [aps07_f, aps07_fp, aps07_fpp, (5,), [0, 1], np.inf, 0.4, 3.84025518406218985e-02, "aps.07.00"],
  314. [aps07_f, aps07_fp, aps07_fpp, (10,), [0, 1], np.inf, 0.4, 9.90000999800049949e-03, "aps.07.01"],
  315. [aps07_f, aps07_fp, aps07_fpp, (20,), [0, 1], np.inf, 0.4, 2.49375003906201174e-03, "aps.07.02"],
  316. [aps08_f, aps08_fp, aps08_fpp, (2,), [0, 1], np.inf, 0.9, 0.5, "aps.08.00"],
  317. [aps08_f, aps08_fp, aps08_fpp, (5,), [0, 1], np.inf, 0.9, 3.45954815848242059e-01, "aps.08.01"],
  318. [aps08_f, aps08_fp, aps08_fpp, (10,), [0, 1], np.inf, 0.9, 2.45122333753307220e-01, "aps.08.02"],
  319. [aps08_f, aps08_fp, aps08_fpp, (15,), [0, 1], np.inf, 0.9, 1.95547623536565629e-01, "aps.08.03"],
  320. [aps08_f, aps08_fp, aps08_fpp, (20,), [0, 1], np.inf, 0.9, 1.64920957276440960e-01, "aps.08.04"],
  321. [aps09_f, aps09_fp, aps09_fpp, (1,), [0, 1], np.inf, 0.5, 2.75508040999484394e-01, "aps.09.00"],
  322. [aps09_f, aps09_fp, aps09_fpp, (2,), [0, 1], np.inf, 0.5, 1.37754020499742197e-01, "aps.09.01"],
  323. [aps09_f, aps09_fp, aps09_fpp, (4,), [0, 1], np.inf, 0.5, 1.03052837781564422e-02, "aps.09.02"],
  324. [aps09_f, aps09_fp, aps09_fpp, (5,), [0, 1], np.inf, 0.5, 3.61710817890406339e-03, "aps.09.03"],
  325. [aps09_f, aps09_fp, aps09_fpp, (8,), [0, 1], np.inf, 0.5, 4.10872918496395375e-04, "aps.09.04"],
  326. [aps09_f, aps09_fp, aps09_fpp, (15,), [0, 1], np.inf, 0.5, 2.59895758929076292e-05, "aps.09.05"],
  327. [aps09_f, aps09_fp, aps09_fpp, (20,), [0, 1], np.inf, 0.5, 7.66859512218533719e-06, "aps.09.06"],
  328. [aps10_f, aps10_fp, aps10_fpp, (1,), [0, 1], np.inf, 0.9, 4.01058137541547011e-01, "aps.10.00"],
  329. [aps10_f, aps10_fp, aps10_fpp, (5,), [0, 1], np.inf, 0.9, 5.16153518757933583e-01, "aps.10.01"],
  330. [aps10_f, aps10_fp, aps10_fpp, (10,), [0, 1], np.inf, 0.9, 5.39522226908415781e-01, "aps.10.02"],
  331. [aps10_f, aps10_fp, aps10_fpp, (15,), [0, 1], np.inf, 0.9, 5.48182294340655241e-01, "aps.10.03"],
  332. [aps10_f, aps10_fp, aps10_fpp, (20,), [0, 1], np.inf, 0.9, 5.52704666678487833e-01, "aps.10.04"],
  333. [aps11_f, aps11_fp, aps11_fpp, (2,), [0.01, 1], np.inf, 1e-02, 1.0 / 2, "aps.11.00"],
  334. [aps11_f, aps11_fp, aps11_fpp, (5,), [0.01, 1], np.inf, 1e-02, 1.0 / 5, "aps.11.01"],
  335. [aps11_f, aps11_fp, aps11_fpp, (15,), [0.01, 1], np.inf, 1e-02, 1.0 / 15, "aps.11.02"],
  336. [aps11_f, aps11_fp, aps11_fpp, (20,), [0.01, 1], np.inf, 1e-02, 1.0 / 20, "aps.11.03"],
  337. [aps12_f, aps12_fp, aps12_fpp, (2,), [1, 100], np.inf, 1.1, 2, "aps.12.00"],
  338. [aps12_f, aps12_fp, aps12_fpp, (3,), [1, 100], np.inf, 1.1, 3, "aps.12.01"],
  339. [aps12_f, aps12_fp, aps12_fpp, (4,), [1, 100], np.inf, 1.1, 4, "aps.12.02"],
  340. [aps12_f, aps12_fp, aps12_fpp, (5,), [1, 100], np.inf, 1.1, 5, "aps.12.03"],
  341. [aps12_f, aps12_fp, aps12_fpp, (6,), [1, 100], np.inf, 1.1, 6, "aps.12.04"],
  342. [aps12_f, aps12_fp, aps12_fpp, (7,), [1, 100], np.inf, 1.1, 7, "aps.12.05"],
  343. [aps12_f, aps12_fp, aps12_fpp, (9,), [1, 100], np.inf, 1.1, 9, "aps.12.06"],
  344. [aps12_f, aps12_fp, aps12_fpp, (11,), [1, 100], np.inf, 1.1, 11, "aps.12.07"],
  345. [aps12_f, aps12_fp, aps12_fpp, (13,), [1, 100], np.inf, 1.1, 13, "aps.12.08"],
  346. [aps12_f, aps12_fp, aps12_fpp, (15,), [1, 100], np.inf, 1.1, 15, "aps.12.09"],
  347. [aps12_f, aps12_fp, aps12_fpp, (17,), [1, 100], np.inf, 1.1, 17, "aps.12.10"],
  348. [aps12_f, aps12_fp, aps12_fpp, (19,), [1, 100], np.inf, 1.1, 19, "aps.12.11"],
  349. [aps12_f, aps12_fp, aps12_fpp, (21,), [1, 100], np.inf, 1.1, 21, "aps.12.12"],
  350. [aps12_f, aps12_fp, aps12_fpp, (23,), [1, 100], np.inf, 1.1, 23, "aps.12.13"],
  351. [aps12_f, aps12_fp, aps12_fpp, (25,), [1, 100], np.inf, 1.1, 25, "aps.12.14"],
  352. [aps12_f, aps12_fp, aps12_fpp, (27,), [1, 100], np.inf, 1.1, 27, "aps.12.15"],
  353. [aps12_f, aps12_fp, aps12_fpp, (29,), [1, 100], np.inf, 1.1, 29, "aps.12.16"],
  354. [aps12_f, aps12_fp, aps12_fpp, (31,), [1, 100], np.inf, 1.1, 31, "aps.12.17"],
  355. [aps12_f, aps12_fp, aps12_fpp, (33,), [1, 100], np.inf, 1.1, 33, "aps.12.18"],
  356. [aps13_f, aps13_fp, aps13_fpp, (), [-1, 4], np.inf, 1.5, 1.54720911915117165e-02, "aps.13.00"],
  357. [aps14_f, aps14_fp, aps14_fpp, (1,), [-1000, np.pi / 2], 0, 1, 6.23806518961612433e-01, "aps.14.00"],
  358. [aps14_f, aps14_fp, aps14_fpp, (2,), [-1000, np.pi / 2], 0, 1, 6.23806518961612433e-01, "aps.14.01"],
  359. [aps14_f, aps14_fp, aps14_fpp, (3,), [-1000, np.pi / 2], 0, 1, 6.23806518961612433e-01, "aps.14.02"],
  360. [aps14_f, aps14_fp, aps14_fpp, (4,), [-1000, np.pi / 2], 0, 1, 6.23806518961612433e-01, "aps.14.03"],
  361. [aps14_f, aps14_fp, aps14_fpp, (5,), [-1000, np.pi / 2], 0, 1, 6.23806518961612433e-01, "aps.14.04"],
  362. [aps14_f, aps14_fp, aps14_fpp, (6,), [-1000, np.pi / 2], 0, 1, 6.23806518961612433e-01, "aps.14.05"],
  363. [aps14_f, aps14_fp, aps14_fpp, (7,), [-1000, np.pi / 2], 0, 1, 6.23806518961612433e-01, "aps.14.06"],
  364. [aps14_f, aps14_fp, aps14_fpp, (8,), [-1000, np.pi / 2], 0, 1, 6.23806518961612433e-01, "aps.14.07"],
  365. [aps14_f, aps14_fp, aps14_fpp, (9,), [-1000, np.pi / 2], 0, 1, 6.23806518961612433e-01, "aps.14.08"],
  366. [aps14_f, aps14_fp, aps14_fpp, (10,), [-1000, np.pi / 2], 0, 1, 6.23806518961612433e-01, "aps.14.09"],
  367. [aps14_f, aps14_fp, aps14_fpp, (11,), [-1000, np.pi / 2], 0, 1, 6.23806518961612433e-01, "aps.14.10"],
  368. [aps14_f, aps14_fp, aps14_fpp, (12,), [-1000, np.pi / 2], 0, 1, 6.23806518961612433e-01, "aps.14.11"],
  369. [aps14_f, aps14_fp, aps14_fpp, (13,), [-1000, np.pi / 2], 0, 1, 6.23806518961612433e-01, "aps.14.12"],
  370. [aps14_f, aps14_fp, aps14_fpp, (14,), [-1000, np.pi / 2], 0, 1, 6.23806518961612433e-01, "aps.14.13"],
  371. [aps14_f, aps14_fp, aps14_fpp, (15,), [-1000, np.pi / 2], 0, 1, 6.23806518961612433e-01, "aps.14.14"],
  372. [aps14_f, aps14_fp, aps14_fpp, (16,), [-1000, np.pi / 2], 0, 1, 6.23806518961612433e-01, "aps.14.15"],
  373. [aps14_f, aps14_fp, aps14_fpp, (17,), [-1000, np.pi / 2], 0, 1, 6.23806518961612433e-01, "aps.14.16"],
  374. [aps14_f, aps14_fp, aps14_fpp, (18,), [-1000, np.pi / 2], 0, 1, 6.23806518961612433e-01, "aps.14.17"],
  375. [aps14_f, aps14_fp, aps14_fpp, (19,), [-1000, np.pi / 2], 0, 1, 6.23806518961612433e-01, "aps.14.18"],
  376. [aps14_f, aps14_fp, aps14_fpp, (20,), [-1000, np.pi / 2], 0, 1, 6.23806518961612433e-01, "aps.14.19"],
  377. [aps14_f, aps14_fp, aps14_fpp, (21,), [-1000, np.pi / 2], 0, 1, 6.23806518961612433e-01, "aps.14.20"],
  378. [aps14_f, aps14_fp, aps14_fpp, (22,), [-1000, np.pi / 2], 0, 1, 6.23806518961612433e-01, "aps.14.21"],
  379. [aps14_f, aps14_fp, aps14_fpp, (23,), [-1000, np.pi / 2], 0, 1, 6.23806518961612433e-01, "aps.14.22"],
  380. [aps14_f, aps14_fp, aps14_fpp, (24,), [-1000, np.pi / 2], 0, 1, 6.23806518961612433e-01, "aps.14.23"],
  381. [aps14_f, aps14_fp, aps14_fpp, (25,), [-1000, np.pi / 2], 0, 1, 6.23806518961612433e-01, "aps.14.24"],
  382. [aps14_f, aps14_fp, aps14_fpp, (26,), [-1000, np.pi / 2], 0, 1, 6.23806518961612433e-01, "aps.14.25"],
  383. [aps14_f, aps14_fp, aps14_fpp, (27,), [-1000, np.pi / 2], 0, 1, 6.23806518961612433e-01, "aps.14.26"],
  384. [aps14_f, aps14_fp, aps14_fpp, (28,), [-1000, np.pi / 2], 0, 1, 6.23806518961612433e-01, "aps.14.27"],
  385. [aps14_f, aps14_fp, aps14_fpp, (29,), [-1000, np.pi / 2], 0, 1, 6.23806518961612433e-01, "aps.14.28"],
  386. [aps14_f, aps14_fp, aps14_fpp, (30,), [-1000, np.pi / 2], 0, 1, 6.23806518961612433e-01, "aps.14.29"],
  387. [aps14_f, aps14_fp, aps14_fpp, (31,), [-1000, np.pi / 2], 0, 1, 6.23806518961612433e-01, "aps.14.30"],
  388. [aps14_f, aps14_fp, aps14_fpp, (32,), [-1000, np.pi / 2], 0, 1, 6.23806518961612433e-01, "aps.14.31"],
  389. [aps14_f, aps14_fp, aps14_fpp, (33,), [-1000, np.pi / 2], 0, 1, 6.23806518961612433e-01, "aps.14.32"],
  390. [aps14_f, aps14_fp, aps14_fpp, (34,), [-1000, np.pi / 2], 0, 1, 6.23806518961612433e-01, "aps.14.33"],
  391. [aps14_f, aps14_fp, aps14_fpp, (35,), [-1000, np.pi / 2], 0, 1, 6.23806518961612433e-01, "aps.14.34"],
  392. [aps14_f, aps14_fp, aps14_fpp, (36,), [-1000, np.pi / 2], 0, 1, 6.23806518961612433e-01, "aps.14.35"],
  393. [aps14_f, aps14_fp, aps14_fpp, (37,), [-1000, np.pi / 2], 0, 1, 6.23806518961612433e-01, "aps.14.36"],
  394. [aps14_f, aps14_fp, aps14_fpp, (38,), [-1000, np.pi / 2], 0, 1, 6.23806518961612433e-01, "aps.14.37"],
  395. [aps14_f, aps14_fp, aps14_fpp, (39,), [-1000, np.pi / 2], 0, 1, 6.23806518961612433e-01, "aps.14.38"],
  396. [aps14_f, aps14_fp, aps14_fpp, (40,), [-1000, np.pi / 2], 0, 1, 6.23806518961612433e-01, "aps.14.39"],
  397. [aps15_f, aps15_fp, aps15_fpp, (20,), [-1000, 1e-4], 0, -2, 5.90513055942197166e-05, "aps.15.00"],
  398. [aps15_f, aps15_fp, aps15_fpp, (21,), [-1000, 1e-4], 0, -2, 5.63671553399369967e-05, "aps.15.01"],
  399. [aps15_f, aps15_fp, aps15_fpp, (22,), [-1000, 1e-4], 0, -2, 5.39164094555919196e-05, "aps.15.02"],
  400. [aps15_f, aps15_fp, aps15_fpp, (23,), [-1000, 1e-4], 0, -2, 5.16698923949422470e-05, "aps.15.03"],
  401. [aps15_f, aps15_fp, aps15_fpp, (24,), [-1000, 1e-4], 0, -2, 4.96030966991445609e-05, "aps.15.04"],
  402. [aps15_f, aps15_fp, aps15_fpp, (25,), [-1000, 1e-4], 0, -2, 4.76952852876389951e-05, "aps.15.05"],
  403. [aps15_f, aps15_fp, aps15_fpp, (26,), [-1000, 1e-4], 0, -2, 4.59287932399486662e-05, "aps.15.06"],
  404. [aps15_f, aps15_fp, aps15_fpp, (27,), [-1000, 1e-4], 0, -2, 4.42884791956647841e-05, "aps.15.07"],
  405. [aps15_f, aps15_fp, aps15_fpp, (28,), [-1000, 1e-4], 0, -2, 4.27612902578832391e-05, "aps.15.08"],
  406. [aps15_f, aps15_fp, aps15_fpp, (29,), [-1000, 1e-4], 0, -2, 4.13359139159538030e-05, "aps.15.09"],
  407. [aps15_f, aps15_fp, aps15_fpp, (30,), [-1000, 1e-4], 0, -2, 4.00024973380198076e-05, "aps.15.10"],
  408. [aps15_f, aps15_fp, aps15_fpp, (31,), [-1000, 1e-4], 0, -2, 3.87524192962066869e-05, "aps.15.11"],
  409. [aps15_f, aps15_fp, aps15_fpp, (32,), [-1000, 1e-4], 0, -2, 3.75781035599579910e-05, "aps.15.12"],
  410. [aps15_f, aps15_fp, aps15_fpp, (33,), [-1000, 1e-4], 0, -2, 3.64728652199592355e-05, "aps.15.13"],
  411. [aps15_f, aps15_fp, aps15_fpp, (34,), [-1000, 1e-4], 0, -2, 3.54307833565318273e-05, "aps.15.14"],
  412. [aps15_f, aps15_fp, aps15_fpp, (35,), [-1000, 1e-4], 0, -2, 3.44465949299614980e-05, "aps.15.15"],
  413. [aps15_f, aps15_fp, aps15_fpp, (36,), [-1000, 1e-4], 0, -2, 3.35156058778003705e-05, "aps.15.16"],
  414. [aps15_f, aps15_fp, aps15_fpp, (37,), [-1000, 1e-4], 0, -2, 3.26336162494372125e-05, "aps.15.17"],
  415. [aps15_f, aps15_fp, aps15_fpp, (38,), [-1000, 1e-4], 0, -2, 3.17968568584260013e-05, "aps.15.18"],
  416. [aps15_f, aps15_fp, aps15_fpp, (39,), [-1000, 1e-4], 0, -2, 3.10019354369653455e-05, "aps.15.19"],
  417. [aps15_f, aps15_fp, aps15_fpp, (40,), [-1000, 1e-4], 0, -2, 3.02457906702100968e-05, "aps.15.20"],
  418. [aps15_f, aps15_fp, aps15_fpp, (100,), [-1000, 1e-4], 0, -2, 1.22779942324615231e-05, "aps.15.21"],
  419. [aps15_f, aps15_fp, aps15_fpp, (200,), [-1000, 1e-4], 0, -2, 6.16953939044086617e-06, "aps.15.22"],
  420. [aps15_f, aps15_fp, aps15_fpp, (300,), [-1000, 1e-4], 0, -2, 4.11985852982928163e-06, "aps.15.23"],
  421. [aps15_f, aps15_fp, aps15_fpp, (400,), [-1000, 1e-4], 0, -2, 3.09246238772721682e-06, "aps.15.24"],
  422. [aps15_f, aps15_fp, aps15_fpp, (500,), [-1000, 1e-4], 0, -2, 2.47520442610501789e-06, "aps.15.25"],
  423. [aps15_f, aps15_fp, aps15_fpp, (600,), [-1000, 1e-4], 0, -2, 2.06335676785127107e-06, "aps.15.26"],
  424. [aps15_f, aps15_fp, aps15_fpp, (700,), [-1000, 1e-4], 0, -2, 1.76901200781542651e-06, "aps.15.27"],
  425. [aps15_f, aps15_fp, aps15_fpp, (800,), [-1000, 1e-4], 0, -2, 1.54816156988591016e-06, "aps.15.28"],
  426. [aps15_f, aps15_fp, aps15_fpp, (900,), [-1000, 1e-4], 0, -2, 1.37633453660223511e-06, "aps.15.29"],
  427. [aps15_f, aps15_fp, aps15_fpp, (1000,), [-1000, 1e-4], 0, -2, 1.23883857889971403e-06, "aps.15.30"]
  428. ]
  429. _APS_TESTS_DICTS = [dict(zip(_APS_TESTS_KEYS, testcase)) for testcase in _APS_TESTS]
  430. # ##################
  431. # "complex" test cases
  432. # A few simple, complex-valued, functions, defined on the complex plane.
  433. def cplx01_f(z, n, a):
  434. r"""z**n-a: Use to find the nth root of a"""
  435. return z**n - a
  436. def cplx01_fp(z, n, a):
  437. return n * z**(n - 1)
  438. def cplx01_fpp(z, n, a):
  439. return n * (n - 1) * z**(n - 2)
  440. def cplx02_f(z, a):
  441. r"""e**z - a: Use to find the log of a"""
  442. return np.exp(z) - a
  443. def cplx02_fp(z, a):
  444. return np.exp(z)
  445. def cplx02_fpp(z, a):
  446. return np.exp(z)
  447. # Each "complex" test case has
  448. # - a function and its two derivatives,
  449. # - additional arguments,
  450. # - the order of differentiability of the function on this interval
  451. # - two starting values x0 and x1
  452. # - the root
  453. # - an Identifier of the test case
  454. #
  455. # Algorithm 748 is a bracketing algorithm so a bracketing interval was provided
  456. # in [1] for each test case. Newton and Halley need a single starting point
  457. # x0, which was chosen to be near the middle of the interval, unless that
  458. # would make the problem too easy.
  459. _COMPLEX_TESTS_KEYS = ["f", "fprime", "fprime2", "args", "smoothness", "x0", "x1", "root", "ID"]
  460. _COMPLEX_TESTS = [
  461. [cplx01_f, cplx01_fp, cplx01_fpp, (2, -1), np.inf, (1 + 1j), (0.5 + 0.5j), 1j, "complex.01.00"],
  462. [cplx01_f, cplx01_fp, cplx01_fpp, (3, 1), np.inf, (-1 + 1j), (-0.5 + 2.0j), (-0.5 + np.sqrt(3) / 2 * 1.0j),
  463. "complex.01.01"],
  464. [cplx01_f, cplx01_fp, cplx01_fpp, (3, -1), np.inf, 1j, (0.5 + 0.5j), (0.5 + np.sqrt(3) / 2 * 1.0j),
  465. "complex.01.02"],
  466. [cplx01_f, cplx01_fp, cplx01_fpp, (3, 8), np.inf, 5, 4, 2, "complex.01.03"],
  467. [cplx02_f, cplx02_fp, cplx02_fpp, (-1,), np.inf, (1 + 2j), (0.5 + 0.5j), np.pi * 1.0j, "complex.02.00"],
  468. [cplx02_f, cplx02_fp, cplx02_fpp, (1j,), np.inf, (1 + 2j), (0.5 + 0.5j), np.pi * 0.5j, "complex.02.01"],
  469. ]
  470. _COMPLEX_TESTS_DICTS = [dict(zip(_COMPLEX_TESTS_KEYS, testcase)) for testcase in _COMPLEX_TESTS]
  471. def _add_a_b(tests):
  472. r"""Add "a" and "b" keys to each test from the "bracket" value"""
  473. for d in tests:
  474. for k, v in zip(['a', 'b'], d.get('bracket', [])):
  475. d[k] = v
  476. _add_a_b(_ORIGINAL_TESTS_DICTS)
  477. _add_a_b(_APS_TESTS_DICTS)
  478. _add_a_b(_COMPLEX_TESTS_DICTS)
  479. def get_tests(collection='original', smoothness=None):
  480. r"""Return the requested collection of test cases, as an array of dicts with subset-specific keys
  481. Allowed values of collection:
  482. 'original': The original benchmarking functions.
  483. Real-valued functions of real-valued inputs on an interval with a zero.
  484. f1, .., f3 are continuous and infinitely differentiable
  485. f4 has a single discontinuity at the root
  486. f5 has a root at 1 replacing a 1st order pole
  487. f6 is randomly positive on one side of the root, randomly negative on the other
  488. 'aps': The test problems in the TOMS "Algorithm 748: Enclosing Zeros of Continuous Functions"
  489. paper by Alefeld, Potra and Shi. Real-valued functions of
  490. real-valued inputs on an interval with a zero.
  491. Suitable for methods which start with an enclosing interval, and
  492. derivatives up to 2nd order.
  493. 'complex': Some complex-valued functions of complex-valued inputs.
  494. No enclosing bracket is provided.
  495. Suitable for methods which use one or more starting values, and
  496. derivatives up to 2nd order.
  497. The dictionary keys will be a subset of
  498. ["f", "fprime", "fprime2", "args", "bracket", "a", b", "smoothness", "x0", "x1", "root", "ID"]
  499. """
  500. collection = collection or "original"
  501. subsets = {"aps": _APS_TESTS_DICTS,
  502. "complex": _COMPLEX_TESTS_DICTS,
  503. "original": _ORIGINAL_TESTS_DICTS}
  504. tests = subsets.get(collection, [])
  505. if smoothness is not None:
  506. tests = [tc for tc in tests if tc['smoothness'] >= smoothness]
  507. return tests
  508. # Backwards compatibility
  509. methods = [cc.bisect, cc.ridder, cc.brenth, cc.brentq]
  510. mstrings = ['cc.bisect', 'cc.ridder', 'cc.brenth', 'cc.brentq']
  511. functions = [f2, f3, f4, f5, f6]
  512. fstrings = ['f2', 'f3', 'f4', 'f5', 'f6']