_tstutils.py 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972
  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. # [2] Chandrupatla, Tirupathi R. "A new hybrid quadratic/bisection algorithm
  34. # for finding the zero of a nonlinear function without using derivatives."
  35. # Advances in Engineering Software 28.3 (1997): 145-149.
  36. from random import random
  37. import numpy as np
  38. from scipy.optimize import _zeros_py as cc
  39. from scipy._lib._array_api import array_namespace
  40. # "description" refers to the original functions
  41. description = """
  42. f2 is a symmetric parabola, x**2 - 1
  43. f3 is a quartic polynomial with large hump in interval
  44. f4 is step function with a discontinuity at 1
  45. f5 is a hyperbola with vertical asymptote at 1
  46. f6 has random values positive to left of 1, negative to right
  47. Of course, these are not real problems. They just test how the
  48. 'good' solvers behave in bad circumstances where bisection is
  49. really the best. A good solver should not be much worse than
  50. bisection in such circumstance, while being faster for smooth
  51. monotone sorts of functions.
  52. """
  53. def f1(x):
  54. r"""f1 is a quadratic with roots at 0 and 1"""
  55. return x * (x - 1.)
  56. def f1_fp(x):
  57. return 2 * x - 1
  58. def f1_fpp(x):
  59. return 2
  60. def f2(x):
  61. r"""f2 is a symmetric parabola, x**2 - 1"""
  62. return x**2 - 1
  63. def f2_fp(x):
  64. return 2 * x
  65. def f2_fpp(x):
  66. return 2
  67. def f3(x):
  68. r"""A quartic with roots at 0, 1, 2 and 3"""
  69. return x * (x - 1.) * (x - 2.) * (x - 3.) # x**4 - 6x**3 + 11x**2 - 6x
  70. def f3_fp(x):
  71. return 4 * x**3 - 18 * x**2 + 22 * x - 6
  72. def f3_fpp(x):
  73. return 12 * x**2 - 36 * x + 22
  74. def f4(x):
  75. r"""Piecewise linear, left- and right- discontinuous at x=1, the root."""
  76. if x > 1:
  77. return 1.0 + .1 * x
  78. if x < 1:
  79. return -1.0 + .1 * x
  80. return 0
  81. def f5(x):
  82. r"""
  83. Hyperbola with a pole at x=1, but pole replaced with 0. Not continuous at root.
  84. """
  85. if x != 1:
  86. return 1.0 / (1. - x)
  87. return 0
  88. # f6(x) returns random value. Without memoization, calling twice with the
  89. # same x returns different values, hence a "random value", not a
  90. # "function with random values"
  91. _f6_cache = {}
  92. def f6(x):
  93. v = _f6_cache.get(x, None)
  94. if v is None:
  95. if x > 1:
  96. v = random()
  97. elif x < 1:
  98. v = -random()
  99. else:
  100. v = 0
  101. _f6_cache[x] = v
  102. return v
  103. # Each Original test case has
  104. # - a function and its two derivatives,
  105. # - additional arguments,
  106. # - a bracket enclosing a root,
  107. # - the order of differentiability (smoothness) on this interval
  108. # - a starting value for methods which don't require a bracket
  109. # - the root (inside the bracket)
  110. # - an Identifier of the test case
  111. _ORIGINAL_TESTS_KEYS = [
  112. "f", "fprime", "fprime2", "args", "bracket", "smoothness", "x0", "root", "ID"
  113. ]
  114. _ORIGINAL_TESTS = [
  115. [f1, f1_fp, f1_fpp, (), [0.5, np.sqrt(3)], np.inf, 0.6, 1.0, "original.01.00"],
  116. [f2, f2_fp, f2_fpp, (), [0.5, np.sqrt(3)], np.inf, 0.6, 1.0, "original.02.00"],
  117. [f3, f3_fp, f3_fpp, (), [0.5, np.sqrt(3)], np.inf, 0.6, 1.0, "original.03.00"],
  118. [f4, None, None, (), [0.5, np.sqrt(3)], -1, 0.6, 1.0, "original.04.00"],
  119. [f5, None, None, (), [0.5, np.sqrt(3)], -1, 0.6, 1.0, "original.05.00"],
  120. [f6, None, None, (), [0.5, np.sqrt(3)], -np.inf, 0.6, 1.0, "original.05.00"]
  121. ]
  122. _ORIGINAL_TESTS_DICTS = [
  123. dict(zip(_ORIGINAL_TESTS_KEYS, testcase)) for testcase in _ORIGINAL_TESTS
  124. ]
  125. # ##################
  126. # "APS" test cases
  127. # Functions and test cases that appear in [1]
  128. def aps01_f(x):
  129. r"""Straightforward sum of trigonometric function and polynomial"""
  130. return np.sin(x) - x / 2
  131. def aps01_fp(x):
  132. return np.cos(x) - 1.0 / 2
  133. def aps01_fpp(x):
  134. return -np.sin(x)
  135. def aps02_f(x):
  136. r"""poles at x=n**2, 1st and 2nd derivatives at root are also close to 0"""
  137. ii = np.arange(1, 21)
  138. return -2 * np.sum((2 * ii - 5)**2 / (x - ii**2)**3)
  139. def aps02_fp(x):
  140. ii = np.arange(1, 21)
  141. return 6 * np.sum((2 * ii - 5)**2 / (x - ii**2)**4)
  142. def aps02_fpp(x):
  143. ii = np.arange(1, 21)
  144. return 24 * np.sum((2 * ii - 5)**2 / (x - ii**2)**5)
  145. def aps03_f(x, a, b):
  146. r"""Rapidly changing at the root"""
  147. return a * x * np.exp(b * x)
  148. def aps03_fp(x, a, b):
  149. return a * (b * x + 1) * np.exp(b * x)
  150. def aps03_fpp(x, a, b):
  151. return a * (b * (b * x + 1) + b) * np.exp(b * x)
  152. def aps04_f(x, n, a):
  153. r"""Medium-degree polynomial"""
  154. return x**n - a
  155. def aps04_fp(x, n, a):
  156. return n * x**(n - 1)
  157. def aps04_fpp(x, n, a):
  158. return n * (n - 1) * x**(n - 2)
  159. def aps05_f(x):
  160. r"""Simple Trigonometric function"""
  161. return np.sin(x) - 1.0 / 2
  162. def aps05_fp(x):
  163. return np.cos(x)
  164. def aps05_fpp(x):
  165. return -np.sin(x)
  166. def aps06_f(x, n):
  167. r"""Exponential rapidly changing from -1 to 1 at x=0"""
  168. return 2 * x * np.exp(-n) - 2 * np.exp(-n * x) + 1
  169. def aps06_fp(x, n):
  170. return 2 * np.exp(-n) + 2 * n * np.exp(-n * x)
  171. def aps06_fpp(x, n):
  172. return -2 * n * n * np.exp(-n * x)
  173. def aps07_f(x, n):
  174. r"""Upside down parabola with parametrizable height"""
  175. return (1 + (1 - n)**2) * x - (1 - n * x)**2
  176. def aps07_fp(x, n):
  177. return (1 + (1 - n)**2) + 2 * n * (1 - n * x)
  178. def aps07_fpp(x, n):
  179. return -2 * n * n
  180. def aps08_f(x, n):
  181. r"""Degree n polynomial"""
  182. return x * x - (1 - x)**n
  183. def aps08_fp(x, n):
  184. return 2 * x + n * (1 - x)**(n - 1)
  185. def aps08_fpp(x, n):
  186. return 2 - n * (n - 1) * (1 - x)**(n - 2)
  187. def aps09_f(x, n):
  188. r"""Upside down quartic with parametrizable height"""
  189. return (1 + (1 - n)**4) * x - (1 - n * x)**4
  190. def aps09_fp(x, n):
  191. return (1 + (1 - n)**4) + 4 * n * (1 - n * x)**3
  192. def aps09_fpp(x, n):
  193. return -12 * n * (1 - n * x)**2
  194. def aps10_f(x, n):
  195. r"""Exponential plus a polynomial"""
  196. return np.exp(-n * x) * (x - 1) + x**n
  197. def aps10_fp(x, n):
  198. return np.exp(-n * x) * (-n * (x - 1) + 1) + n * x**(n - 1)
  199. def aps10_fpp(x, n):
  200. return (np.exp(-n * x) * (-n * (-n * (x - 1) + 1) + -n * x)
  201. + n * (n - 1) * x**(n - 2))
  202. def aps11_f(x, n):
  203. r"""Rational function with a zero at x=1/n and a pole at x=0"""
  204. return (n * x - 1) / ((n - 1) * x)
  205. def aps11_fp(x, n):
  206. return 1 / (n - 1) / x**2
  207. def aps11_fpp(x, n):
  208. return -2 / (n - 1) / x**3
  209. def aps12_f(x, n):
  210. r"""nth root of x, with a zero at x=n"""
  211. return np.power(x, 1.0 / n) - np.power(n, 1.0 / n)
  212. def aps12_fp(x, n):
  213. return np.power(x, (1.0 - n) / n) / n
  214. def aps12_fpp(x, n):
  215. return np.power(x, (1.0 - 2 * n) / n) * (1.0 / n) * (1.0 - n) / n
  216. _MAX_EXPABLE = np.log(np.finfo(float).max)
  217. def aps13_f(x):
  218. r"""Function with *all* derivatives 0 at the root"""
  219. if x == 0:
  220. return 0
  221. # x2 = 1.0/x**2
  222. # if x2 > 708:
  223. # return 0
  224. y = 1 / x**2
  225. if y > _MAX_EXPABLE:
  226. return 0
  227. return x / np.exp(y)
  228. def aps13_fp(x):
  229. if x == 0:
  230. return 0
  231. y = 1 / x**2
  232. if y > _MAX_EXPABLE:
  233. return 0
  234. return (1 + 2 / x**2) / np.exp(y)
  235. def aps13_fpp(x):
  236. if x == 0:
  237. return 0
  238. y = 1 / x**2
  239. if y > _MAX_EXPABLE:
  240. return 0
  241. return 2 * (2 - x**2) / x**5 / np.exp(y)
  242. def aps14_f(x, n):
  243. r"""0 for negative x-values, trigonometric+linear for x positive"""
  244. if x <= 0:
  245. return -n / 20.0
  246. return n / 20.0 * (x / 1.5 + np.sin(x) - 1)
  247. def aps14_fp(x, n):
  248. if x <= 0:
  249. return 0
  250. return n / 20.0 * (1.0 / 1.5 + np.cos(x))
  251. def aps14_fpp(x, n):
  252. if x <= 0:
  253. return 0
  254. return -n / 20.0 * (np.sin(x))
  255. def aps15_f(x, n):
  256. r"""piecewise linear, constant outside of [0, 0.002/(1+n)]"""
  257. if x < 0:
  258. return -0.859
  259. if x > 2 * 1e-3 / (1 + n):
  260. return np.e - 1.859
  261. return np.exp((n + 1) * x / 2 * 1000) - 1.859
  262. def aps15_fp(x, n):
  263. if not 0 <= x <= 2 * 1e-3 / (1 + n):
  264. return np.e - 1.859
  265. return np.exp((n + 1) * x / 2 * 1000) * (n + 1) / 2 * 1000
  266. def aps15_fpp(x, n):
  267. if not 0 <= x <= 2 * 1e-3 / (1 + n):
  268. return np.e - 1.859
  269. return np.exp((n + 1) * x / 2 * 1000) * (n + 1) / 2 * 1000 * (n + 1) / 2 * 1000
  270. # Each APS test case has
  271. # - a function and its two derivatives,
  272. # - additional arguments,
  273. # - a bracket enclosing a root,
  274. # - the order of differentiability of the function on this interval
  275. # - a starting value for methods which don't require a bracket
  276. # - the root (inside the bracket)
  277. # - an Identifier of the test case
  278. #
  279. # Algorithm 748 is a bracketing algorithm so a bracketing interval was provided
  280. # in [1] for each test case. Newton and Halley methods need a single
  281. # starting point x0, which was chosen to be near the middle of the interval,
  282. # unless that would have made the problem too easy.
  283. _APS_TESTS_KEYS = [
  284. "f", "fprime", "fprime2", "args", "bracket", "smoothness", "x0", "root", "ID"
  285. ]
  286. _APS_TESTS = [
  287. [aps01_f, aps01_fp, aps01_fpp, (), [np.pi / 2, np.pi], np.inf,
  288. 3, 1.89549426703398094e+00, "aps.01.00"],
  289. [aps02_f, aps02_fp, aps02_fpp, (), [1 + 1e-9, 4 - 1e-9], np.inf,
  290. 2, 3.02291534727305677e+00, "aps.02.00"],
  291. [aps02_f, aps02_fp, aps02_fpp, (), [4 + 1e-9, 9 - 1e-9], np.inf,
  292. 5, 6.68375356080807848e+00, "aps.02.01"],
  293. [aps02_f, aps02_fp, aps02_fpp, (), [9 + 1e-9, 16 - 1e-9], np.inf,
  294. 10, 1.12387016550022114e+01, "aps.02.02"],
  295. [aps02_f, aps02_fp, aps02_fpp, (), [16 + 1e-9, 25 - 1e-9], np.inf,
  296. 17, 1.96760000806234103e+01, "aps.02.03"],
  297. [aps02_f, aps02_fp, aps02_fpp, (), [25 + 1e-9, 36 - 1e-9], np.inf,
  298. 26, 2.98282273265047557e+01, "aps.02.04"],
  299. [aps02_f, aps02_fp, aps02_fpp, (), [36 + 1e-9, 49 - 1e-9], np.inf,
  300. 37, 4.19061161952894139e+01, "aps.02.05"],
  301. [aps02_f, aps02_fp, aps02_fpp, (), [49 + 1e-9, 64 - 1e-9], np.inf,
  302. 50, 5.59535958001430913e+01, "aps.02.06"],
  303. [aps02_f, aps02_fp, aps02_fpp, (), [64 + 1e-9, 81 - 1e-9], np.inf,
  304. 65, 7.19856655865877997e+01, "aps.02.07"],
  305. [aps02_f, aps02_fp, aps02_fpp, (), [81 + 1e-9, 100 - 1e-9], np.inf,
  306. 82, 9.00088685391666701e+01, "aps.02.08"],
  307. [aps02_f, aps02_fp, aps02_fpp, (), [100 + 1e-9, 121 - 1e-9], np.inf,
  308. 101, 1.10026532748330197e+02, "aps.02.09"],
  309. [aps03_f, aps03_fp, aps03_fpp, (-40, -1), [-9, 31], np.inf,
  310. -2, 0, "aps.03.00"],
  311. [aps03_f, aps03_fp, aps03_fpp, (-100, -2), [-9, 31], np.inf,
  312. -2, 0, "aps.03.01"],
  313. [aps03_f, aps03_fp, aps03_fpp, (-200, -3), [-9, 31], np.inf,
  314. -2, 0, "aps.03.02"],
  315. [aps04_f, aps04_fp, aps04_fpp, (4, 0.2), [0, 5], np.inf,
  316. 2.5, 6.68740304976422006e-01, "aps.04.00"],
  317. [aps04_f, aps04_fp, aps04_fpp, (6, 0.2), [0, 5], np.inf,
  318. 2.5, 7.64724491331730039e-01, "aps.04.01"],
  319. [aps04_f, aps04_fp, aps04_fpp, (8, 0.2), [0, 5], np.inf,
  320. 2.5, 8.17765433957942545e-01, "aps.04.02"],
  321. [aps04_f, aps04_fp, aps04_fpp, (10, 0.2), [0, 5], np.inf,
  322. 2.5, 8.51339922520784609e-01, "aps.04.03"],
  323. [aps04_f, aps04_fp, aps04_fpp, (12, 0.2), [0, 5], np.inf,
  324. 2.5, 8.74485272221167897e-01, "aps.04.04"],
  325. [aps04_f, aps04_fp, aps04_fpp, (4, 1), [0, 5], np.inf,
  326. 2.5, 1, "aps.04.05"],
  327. [aps04_f, aps04_fp, aps04_fpp, (6, 1), [0, 5], np.inf,
  328. 2.5, 1, "aps.04.06"],
  329. [aps04_f, aps04_fp, aps04_fpp, (8, 1), [0, 5], np.inf,
  330. 2.5, 1, "aps.04.07"],
  331. [aps04_f, aps04_fp, aps04_fpp, (10, 1), [0, 5], np.inf,
  332. 2.5, 1, "aps.04.08"],
  333. [aps04_f, aps04_fp, aps04_fpp, (12, 1), [0, 5], np.inf,
  334. 2.5, 1, "aps.04.09"],
  335. [aps04_f, aps04_fp, aps04_fpp, (8, 1), [-0.95, 4.05], np.inf,
  336. 1.5, 1, "aps.04.10"],
  337. [aps04_f, aps04_fp, aps04_fpp, (10, 1), [-0.95, 4.05], np.inf,
  338. 1.5, 1, "aps.04.11"],
  339. [aps04_f, aps04_fp, aps04_fpp, (12, 1), [-0.95, 4.05], np.inf,
  340. 1.5, 1, "aps.04.12"],
  341. [aps04_f, aps04_fp, aps04_fpp, (14, 1), [-0.95, 4.05], np.inf,
  342. 1.5, 1, "aps.04.13"],
  343. [aps05_f, aps05_fp, aps05_fpp, (), [0, 1.5], np.inf,
  344. 1.3, np.pi / 6, "aps.05.00"],
  345. [aps06_f, aps06_fp, aps06_fpp, (1,), [0, 1], np.inf,
  346. 0.5, 4.22477709641236709e-01, "aps.06.00"],
  347. [aps06_f, aps06_fp, aps06_fpp, (2,), [0, 1], np.inf,
  348. 0.5, 3.06699410483203705e-01, "aps.06.01"],
  349. [aps06_f, aps06_fp, aps06_fpp, (3,), [0, 1], np.inf,
  350. 0.5, 2.23705457654662959e-01, "aps.06.02"],
  351. [aps06_f, aps06_fp, aps06_fpp, (4,), [0, 1], np.inf,
  352. 0.5, 1.71719147519508369e-01, "aps.06.03"],
  353. [aps06_f, aps06_fp, aps06_fpp, (5,), [0, 1], np.inf,
  354. 0.4, 1.38257155056824066e-01, "aps.06.04"],
  355. [aps06_f, aps06_fp, aps06_fpp, (20,), [0, 1], np.inf,
  356. 0.1, 3.46573590208538521e-02, "aps.06.05"],
  357. [aps06_f, aps06_fp, aps06_fpp, (40,), [0, 1], np.inf,
  358. 5e-02, 1.73286795139986315e-02, "aps.06.06"],
  359. [aps06_f, aps06_fp, aps06_fpp, (60,), [0, 1], np.inf,
  360. 1.0 / 30, 1.15524530093324210e-02, "aps.06.07"],
  361. [aps06_f, aps06_fp, aps06_fpp, (80,), [0, 1], np.inf,
  362. 2.5e-02, 8.66433975699931573e-03, "aps.06.08"],
  363. [aps06_f, aps06_fp, aps06_fpp, (100,), [0, 1], np.inf,
  364. 2e-02, 6.93147180559945415e-03, "aps.06.09"],
  365. [aps07_f, aps07_fp, aps07_fpp, (5,), [0, 1], np.inf,
  366. 0.4, 3.84025518406218985e-02, "aps.07.00"],
  367. [aps07_f, aps07_fp, aps07_fpp, (10,), [0, 1], np.inf,
  368. 0.4, 9.90000999800049949e-03, "aps.07.01"],
  369. [aps07_f, aps07_fp, aps07_fpp, (20,), [0, 1], np.inf,
  370. 0.4, 2.49375003906201174e-03, "aps.07.02"],
  371. [aps08_f, aps08_fp, aps08_fpp, (2,), [0, 1], np.inf,
  372. 0.9, 0.5, "aps.08.00"],
  373. [aps08_f, aps08_fp, aps08_fpp, (5,), [0, 1], np.inf,
  374. 0.9, 3.45954815848242059e-01, "aps.08.01"],
  375. [aps08_f, aps08_fp, aps08_fpp, (10,), [0, 1], np.inf,
  376. 0.9, 2.45122333753307220e-01, "aps.08.02"],
  377. [aps08_f, aps08_fp, aps08_fpp, (15,), [0, 1], np.inf,
  378. 0.9, 1.95547623536565629e-01, "aps.08.03"],
  379. [aps08_f, aps08_fp, aps08_fpp, (20,), [0, 1], np.inf,
  380. 0.9, 1.64920957276440960e-01, "aps.08.04"],
  381. [aps09_f, aps09_fp, aps09_fpp, (1,), [0, 1], np.inf,
  382. 0.5, 2.75508040999484394e-01, "aps.09.00"],
  383. [aps09_f, aps09_fp, aps09_fpp, (2,), [0, 1], np.inf,
  384. 0.5, 1.37754020499742197e-01, "aps.09.01"],
  385. [aps09_f, aps09_fp, aps09_fpp, (4,), [0, 1], np.inf,
  386. 0.5, 1.03052837781564422e-02, "aps.09.02"],
  387. [aps09_f, aps09_fp, aps09_fpp, (5,), [0, 1], np.inf,
  388. 0.5, 3.61710817890406339e-03, "aps.09.03"],
  389. [aps09_f, aps09_fp, aps09_fpp, (8,), [0, 1], np.inf,
  390. 0.5, 4.10872918496395375e-04, "aps.09.04"],
  391. [aps09_f, aps09_fp, aps09_fpp, (15,), [0, 1], np.inf,
  392. 0.5, 2.59895758929076292e-05, "aps.09.05"],
  393. [aps09_f, aps09_fp, aps09_fpp, (20,), [0, 1], np.inf,
  394. 0.5, 7.66859512218533719e-06, "aps.09.06"],
  395. [aps10_f, aps10_fp, aps10_fpp, (1,), [0, 1], np.inf,
  396. 0.9, 4.01058137541547011e-01, "aps.10.00"],
  397. [aps10_f, aps10_fp, aps10_fpp, (5,), [0, 1], np.inf,
  398. 0.9, 5.16153518757933583e-01, "aps.10.01"],
  399. [aps10_f, aps10_fp, aps10_fpp, (10,), [0, 1], np.inf,
  400. 0.9, 5.39522226908415781e-01, "aps.10.02"],
  401. [aps10_f, aps10_fp, aps10_fpp, (15,), [0, 1], np.inf,
  402. 0.9, 5.48182294340655241e-01, "aps.10.03"],
  403. [aps10_f, aps10_fp, aps10_fpp, (20,), [0, 1], np.inf,
  404. 0.9, 5.52704666678487833e-01, "aps.10.04"],
  405. [aps11_f, aps11_fp, aps11_fpp, (2,), [0.01, 1], np.inf,
  406. 1e-02, 1.0 / 2, "aps.11.00"],
  407. [aps11_f, aps11_fp, aps11_fpp, (5,), [0.01, 1], np.inf,
  408. 1e-02, 1.0 / 5, "aps.11.01"],
  409. [aps11_f, aps11_fp, aps11_fpp, (15,), [0.01, 1], np.inf,
  410. 1e-02, 1.0 / 15, "aps.11.02"],
  411. [aps11_f, aps11_fp, aps11_fpp, (20,), [0.01, 1], np.inf,
  412. 1e-02, 1.0 / 20, "aps.11.03"],
  413. [aps12_f, aps12_fp, aps12_fpp, (2,), [1, 100], np.inf,
  414. 1.1, 2, "aps.12.00"],
  415. [aps12_f, aps12_fp, aps12_fpp, (3,), [1, 100], np.inf,
  416. 1.1, 3, "aps.12.01"],
  417. [aps12_f, aps12_fp, aps12_fpp, (4,), [1, 100], np.inf,
  418. 1.1, 4, "aps.12.02"],
  419. [aps12_f, aps12_fp, aps12_fpp, (5,), [1, 100], np.inf,
  420. 1.1, 5, "aps.12.03"],
  421. [aps12_f, aps12_fp, aps12_fpp, (6,), [1, 100], np.inf,
  422. 1.1, 6, "aps.12.04"],
  423. [aps12_f, aps12_fp, aps12_fpp, (7,), [1, 100], np.inf,
  424. 1.1, 7, "aps.12.05"],
  425. [aps12_f, aps12_fp, aps12_fpp, (9,), [1, 100], np.inf,
  426. 1.1, 9, "aps.12.06"],
  427. [aps12_f, aps12_fp, aps12_fpp, (11,), [1, 100], np.inf,
  428. 1.1, 11, "aps.12.07"],
  429. [aps12_f, aps12_fp, aps12_fpp, (13,), [1, 100], np.inf,
  430. 1.1, 13, "aps.12.08"],
  431. [aps12_f, aps12_fp, aps12_fpp, (15,), [1, 100], np.inf,
  432. 1.1, 15, "aps.12.09"],
  433. [aps12_f, aps12_fp, aps12_fpp, (17,), [1, 100], np.inf,
  434. 1.1, 17, "aps.12.10"],
  435. [aps12_f, aps12_fp, aps12_fpp, (19,), [1, 100], np.inf,
  436. 1.1, 19, "aps.12.11"],
  437. [aps12_f, aps12_fp, aps12_fpp, (21,), [1, 100], np.inf,
  438. 1.1, 21, "aps.12.12"],
  439. [aps12_f, aps12_fp, aps12_fpp, (23,), [1, 100], np.inf,
  440. 1.1, 23, "aps.12.13"],
  441. [aps12_f, aps12_fp, aps12_fpp, (25,), [1, 100], np.inf,
  442. 1.1, 25, "aps.12.14"],
  443. [aps12_f, aps12_fp, aps12_fpp, (27,), [1, 100], np.inf,
  444. 1.1, 27, "aps.12.15"],
  445. [aps12_f, aps12_fp, aps12_fpp, (29,), [1, 100], np.inf,
  446. 1.1, 29, "aps.12.16"],
  447. [aps12_f, aps12_fp, aps12_fpp, (31,), [1, 100], np.inf,
  448. 1.1, 31, "aps.12.17"],
  449. [aps12_f, aps12_fp, aps12_fpp, (33,), [1, 100], np.inf,
  450. 1.1, 33, "aps.12.18"],
  451. [aps13_f, aps13_fp, aps13_fpp, (), [-1, 4], np.inf,
  452. 1.5, 0, "aps.13.00"],
  453. [aps14_f, aps14_fp, aps14_fpp, (1,), [-1000, np.pi / 2], 0,
  454. 1, 6.23806518961612433e-01, "aps.14.00"],
  455. [aps14_f, aps14_fp, aps14_fpp, (2,), [-1000, np.pi / 2], 0,
  456. 1, 6.23806518961612433e-01, "aps.14.01"],
  457. [aps14_f, aps14_fp, aps14_fpp, (3,), [-1000, np.pi / 2], 0,
  458. 1, 6.23806518961612433e-01, "aps.14.02"],
  459. [aps14_f, aps14_fp, aps14_fpp, (4,), [-1000, np.pi / 2], 0,
  460. 1, 6.23806518961612433e-01, "aps.14.03"],
  461. [aps14_f, aps14_fp, aps14_fpp, (5,), [-1000, np.pi / 2], 0,
  462. 1, 6.23806518961612433e-01, "aps.14.04"],
  463. [aps14_f, aps14_fp, aps14_fpp, (6,), [-1000, np.pi / 2], 0,
  464. 1, 6.23806518961612433e-01, "aps.14.05"],
  465. [aps14_f, aps14_fp, aps14_fpp, (7,), [-1000, np.pi / 2], 0,
  466. 1, 6.23806518961612433e-01, "aps.14.06"],
  467. [aps14_f, aps14_fp, aps14_fpp, (8,), [-1000, np.pi / 2], 0,
  468. 1, 6.23806518961612433e-01, "aps.14.07"],
  469. [aps14_f, aps14_fp, aps14_fpp, (9,), [-1000, np.pi / 2], 0,
  470. 1, 6.23806518961612433e-01, "aps.14.08"],
  471. [aps14_f, aps14_fp, aps14_fpp, (10,), [-1000, np.pi / 2], 0,
  472. 1, 6.23806518961612433e-01, "aps.14.09"],
  473. [aps14_f, aps14_fp, aps14_fpp, (11,), [-1000, np.pi / 2], 0,
  474. 1, 6.23806518961612433e-01, "aps.14.10"],
  475. [aps14_f, aps14_fp, aps14_fpp, (12,), [-1000, np.pi / 2], 0,
  476. 1, 6.23806518961612433e-01, "aps.14.11"],
  477. [aps14_f, aps14_fp, aps14_fpp, (13,), [-1000, np.pi / 2], 0,
  478. 1, 6.23806518961612433e-01, "aps.14.12"],
  479. [aps14_f, aps14_fp, aps14_fpp, (14,), [-1000, np.pi / 2], 0,
  480. 1, 6.23806518961612433e-01, "aps.14.13"],
  481. [aps14_f, aps14_fp, aps14_fpp, (15,), [-1000, np.pi / 2], 0,
  482. 1, 6.23806518961612433e-01, "aps.14.14"],
  483. [aps14_f, aps14_fp, aps14_fpp, (16,), [-1000, np.pi / 2], 0,
  484. 1, 6.23806518961612433e-01, "aps.14.15"],
  485. [aps14_f, aps14_fp, aps14_fpp, (17,), [-1000, np.pi / 2], 0,
  486. 1, 6.23806518961612433e-01, "aps.14.16"],
  487. [aps14_f, aps14_fp, aps14_fpp, (18,), [-1000, np.pi / 2], 0,
  488. 1, 6.23806518961612433e-01, "aps.14.17"],
  489. [aps14_f, aps14_fp, aps14_fpp, (19,), [-1000, np.pi / 2], 0,
  490. 1, 6.23806518961612433e-01, "aps.14.18"],
  491. [aps14_f, aps14_fp, aps14_fpp, (20,), [-1000, np.pi / 2], 0,
  492. 1, 6.23806518961612433e-01, "aps.14.19"],
  493. [aps14_f, aps14_fp, aps14_fpp, (21,), [-1000, np.pi / 2], 0,
  494. 1, 6.23806518961612433e-01, "aps.14.20"],
  495. [aps14_f, aps14_fp, aps14_fpp, (22,), [-1000, np.pi / 2], 0,
  496. 1, 6.23806518961612433e-01, "aps.14.21"],
  497. [aps14_f, aps14_fp, aps14_fpp, (23,), [-1000, np.pi / 2], 0,
  498. 1, 6.23806518961612433e-01, "aps.14.22"],
  499. [aps14_f, aps14_fp, aps14_fpp, (24,), [-1000, np.pi / 2], 0,
  500. 1, 6.23806518961612433e-01, "aps.14.23"],
  501. [aps14_f, aps14_fp, aps14_fpp, (25,), [-1000, np.pi / 2], 0,
  502. 1, 6.23806518961612433e-01, "aps.14.24"],
  503. [aps14_f, aps14_fp, aps14_fpp, (26,), [-1000, np.pi / 2], 0,
  504. 1, 6.23806518961612433e-01, "aps.14.25"],
  505. [aps14_f, aps14_fp, aps14_fpp, (27,), [-1000, np.pi / 2], 0,
  506. 1, 6.23806518961612433e-01, "aps.14.26"],
  507. [aps14_f, aps14_fp, aps14_fpp, (28,), [-1000, np.pi / 2], 0,
  508. 1, 6.23806518961612433e-01, "aps.14.27"],
  509. [aps14_f, aps14_fp, aps14_fpp, (29,), [-1000, np.pi / 2], 0,
  510. 1, 6.23806518961612433e-01, "aps.14.28"],
  511. [aps14_f, aps14_fp, aps14_fpp, (30,), [-1000, np.pi / 2], 0,
  512. 1, 6.23806518961612433e-01, "aps.14.29"],
  513. [aps14_f, aps14_fp, aps14_fpp, (31,), [-1000, np.pi / 2], 0,
  514. 1, 6.23806518961612433e-01, "aps.14.30"],
  515. [aps14_f, aps14_fp, aps14_fpp, (32,), [-1000, np.pi / 2], 0,
  516. 1, 6.23806518961612433e-01, "aps.14.31"],
  517. [aps14_f, aps14_fp, aps14_fpp, (33,), [-1000, np.pi / 2], 0,
  518. 1, 6.23806518961612433e-01, "aps.14.32"],
  519. [aps14_f, aps14_fp, aps14_fpp, (34,), [-1000, np.pi / 2], 0,
  520. 1, 6.23806518961612433e-01, "aps.14.33"],
  521. [aps14_f, aps14_fp, aps14_fpp, (35,), [-1000, np.pi / 2], 0,
  522. 1, 6.23806518961612433e-01, "aps.14.34"],
  523. [aps14_f, aps14_fp, aps14_fpp, (36,), [-1000, np.pi / 2], 0,
  524. 1, 6.23806518961612433e-01, "aps.14.35"],
  525. [aps14_f, aps14_fp, aps14_fpp, (37,), [-1000, np.pi / 2], 0,
  526. 1, 6.23806518961612433e-01, "aps.14.36"],
  527. [aps14_f, aps14_fp, aps14_fpp, (38,), [-1000, np.pi / 2], 0,
  528. 1, 6.23806518961612433e-01, "aps.14.37"],
  529. [aps14_f, aps14_fp, aps14_fpp, (39,), [-1000, np.pi / 2], 0,
  530. 1, 6.23806518961612433e-01, "aps.14.38"],
  531. [aps14_f, aps14_fp, aps14_fpp, (40,), [-1000, np.pi / 2], 0,
  532. 1, 6.23806518961612433e-01, "aps.14.39"],
  533. [aps15_f, aps15_fp, aps15_fpp, (20,), [-1000, 1e-4], 0,
  534. -2, 5.90513055942197166e-05, "aps.15.00"],
  535. [aps15_f, aps15_fp, aps15_fpp, (21,), [-1000, 1e-4], 0,
  536. -2, 5.63671553399369967e-05, "aps.15.01"],
  537. [aps15_f, aps15_fp, aps15_fpp, (22,), [-1000, 1e-4], 0,
  538. -2, 5.39164094555919196e-05, "aps.15.02"],
  539. [aps15_f, aps15_fp, aps15_fpp, (23,), [-1000, 1e-4], 0,
  540. -2, 5.16698923949422470e-05, "aps.15.03"],
  541. [aps15_f, aps15_fp, aps15_fpp, (24,), [-1000, 1e-4], 0,
  542. -2, 4.96030966991445609e-05, "aps.15.04"],
  543. [aps15_f, aps15_fp, aps15_fpp, (25,), [-1000, 1e-4], 0,
  544. -2, 4.76952852876389951e-05, "aps.15.05"],
  545. [aps15_f, aps15_fp, aps15_fpp, (26,), [-1000, 1e-4], 0,
  546. -2, 4.59287932399486662e-05, "aps.15.06"],
  547. [aps15_f, aps15_fp, aps15_fpp, (27,), [-1000, 1e-4], 0,
  548. -2, 4.42884791956647841e-05, "aps.15.07"],
  549. [aps15_f, aps15_fp, aps15_fpp, (28,), [-1000, 1e-4], 0,
  550. -2, 4.27612902578832391e-05, "aps.15.08"],
  551. [aps15_f, aps15_fp, aps15_fpp, (29,), [-1000, 1e-4], 0,
  552. -2, 4.13359139159538030e-05, "aps.15.09"],
  553. [aps15_f, aps15_fp, aps15_fpp, (30,), [-1000, 1e-4], 0,
  554. -2, 4.00024973380198076e-05, "aps.15.10"],
  555. [aps15_f, aps15_fp, aps15_fpp, (31,), [-1000, 1e-4], 0,
  556. -2, 3.87524192962066869e-05, "aps.15.11"],
  557. [aps15_f, aps15_fp, aps15_fpp, (32,), [-1000, 1e-4], 0,
  558. -2, 3.75781035599579910e-05, "aps.15.12"],
  559. [aps15_f, aps15_fp, aps15_fpp, (33,), [-1000, 1e-4], 0,
  560. -2, 3.64728652199592355e-05, "aps.15.13"],
  561. [aps15_f, aps15_fp, aps15_fpp, (34,), [-1000, 1e-4], 0,
  562. -2, 3.54307833565318273e-05, "aps.15.14"],
  563. [aps15_f, aps15_fp, aps15_fpp, (35,), [-1000, 1e-4], 0,
  564. -2, 3.44465949299614980e-05, "aps.15.15"],
  565. [aps15_f, aps15_fp, aps15_fpp, (36,), [-1000, 1e-4], 0,
  566. -2, 3.35156058778003705e-05, "aps.15.16"],
  567. [aps15_f, aps15_fp, aps15_fpp, (37,), [-1000, 1e-4], 0,
  568. -2, 3.26336162494372125e-05, "aps.15.17"],
  569. [aps15_f, aps15_fp, aps15_fpp, (38,), [-1000, 1e-4], 0,
  570. -2, 3.17968568584260013e-05, "aps.15.18"],
  571. [aps15_f, aps15_fp, aps15_fpp, (39,), [-1000, 1e-4], 0,
  572. -2, 3.10019354369653455e-05, "aps.15.19"],
  573. [aps15_f, aps15_fp, aps15_fpp, (40,), [-1000, 1e-4], 0,
  574. -2, 3.02457906702100968e-05, "aps.15.20"],
  575. [aps15_f, aps15_fp, aps15_fpp, (100,), [-1000, 1e-4], 0,
  576. -2, 1.22779942324615231e-05, "aps.15.21"],
  577. [aps15_f, aps15_fp, aps15_fpp, (200,), [-1000, 1e-4], 0,
  578. -2, 6.16953939044086617e-06, "aps.15.22"],
  579. [aps15_f, aps15_fp, aps15_fpp, (300,), [-1000, 1e-4], 0,
  580. -2, 4.11985852982928163e-06, "aps.15.23"],
  581. [aps15_f, aps15_fp, aps15_fpp, (400,), [-1000, 1e-4], 0,
  582. -2, 3.09246238772721682e-06, "aps.15.24"],
  583. [aps15_f, aps15_fp, aps15_fpp, (500,), [-1000, 1e-4], 0,
  584. -2, 2.47520442610501789e-06, "aps.15.25"],
  585. [aps15_f, aps15_fp, aps15_fpp, (600,), [-1000, 1e-4], 0,
  586. -2, 2.06335676785127107e-06, "aps.15.26"],
  587. [aps15_f, aps15_fp, aps15_fpp, (700,), [-1000, 1e-4], 0,
  588. -2, 1.76901200781542651e-06, "aps.15.27"],
  589. [aps15_f, aps15_fp, aps15_fpp, (800,), [-1000, 1e-4], 0,
  590. -2, 1.54816156988591016e-06, "aps.15.28"],
  591. [aps15_f, aps15_fp, aps15_fpp, (900,), [-1000, 1e-4], 0,
  592. -2, 1.37633453660223511e-06, "aps.15.29"],
  593. [aps15_f, aps15_fp, aps15_fpp, (1000,), [-1000, 1e-4], 0,
  594. -2, 1.23883857889971403e-06, "aps.15.30"]
  595. ]
  596. _APS_TESTS_DICTS = [dict(zip(_APS_TESTS_KEYS, testcase)) for testcase in _APS_TESTS]
  597. # ##################
  598. # "complex" test cases
  599. # A few simple, complex-valued, functions, defined on the complex plane.
  600. def cplx01_f(z, n, a):
  601. r"""z**n-a: Use to find the nth root of a"""
  602. return z**n - a
  603. def cplx01_fp(z, n, a):
  604. return n * z**(n - 1)
  605. def cplx01_fpp(z, n, a):
  606. return n * (n - 1) * z**(n - 2)
  607. def cplx02_f(z, a):
  608. r"""e**z - a: Use to find the log of a"""
  609. return np.exp(z) - a
  610. def cplx02_fp(z, a):
  611. return np.exp(z)
  612. def cplx02_fpp(z, a):
  613. return np.exp(z)
  614. # Each "complex" test case has
  615. # - a function and its two derivatives,
  616. # - additional arguments,
  617. # - the order of differentiability of the function on this interval
  618. # - two starting values x0 and x1
  619. # - the root
  620. # - an Identifier of the test case
  621. #
  622. # Algorithm 748 is a bracketing algorithm so a bracketing interval was provided
  623. # in [1] for each test case. Newton and Halley need a single starting point
  624. # x0, which was chosen to be near the middle of the interval, unless that
  625. # would make the problem too easy.
  626. _COMPLEX_TESTS_KEYS = [
  627. "f", "fprime", "fprime2", "args", "smoothness", "x0", "x1", "root", "ID"
  628. ]
  629. _COMPLEX_TESTS = [
  630. [cplx01_f, cplx01_fp, cplx01_fpp, (2, -1), np.inf,
  631. (1 + 1j), (0.5 + 0.5j), 1j, "complex.01.00"],
  632. [cplx01_f, cplx01_fp, cplx01_fpp, (3, 1), np.inf,
  633. (-1 + 1j), (-0.5 + 2.0j), (-0.5 + np.sqrt(3) / 2 * 1.0j),
  634. "complex.01.01"],
  635. [cplx01_f, cplx01_fp, cplx01_fpp, (3, -1), np.inf,
  636. 1j, (0.5 + 0.5j), (0.5 + np.sqrt(3) / 2 * 1.0j),
  637. "complex.01.02"],
  638. [cplx01_f, cplx01_fp, cplx01_fpp, (3, 8), np.inf,
  639. 5, 4, 2, "complex.01.03"],
  640. [cplx02_f, cplx02_fp, cplx02_fpp, (-1,), np.inf,
  641. (1 + 2j), (0.5 + 0.5j), np.pi * 1.0j, "complex.02.00"],
  642. [cplx02_f, cplx02_fp, cplx02_fpp, (1j,), np.inf,
  643. (1 + 2j), (0.5 + 0.5j), np.pi * 0.5j, "complex.02.01"],
  644. ]
  645. _COMPLEX_TESTS_DICTS = [
  646. dict(zip(_COMPLEX_TESTS_KEYS, testcase)) for testcase in _COMPLEX_TESTS
  647. ]
  648. def _add_a_b(tests):
  649. r"""Add "a" and "b" keys to each test from the "bracket" value"""
  650. for d in tests:
  651. for k, v in zip(['a', 'b'], d.get('bracket', [])):
  652. d[k] = v
  653. _add_a_b(_ORIGINAL_TESTS_DICTS)
  654. _add_a_b(_APS_TESTS_DICTS)
  655. _add_a_b(_COMPLEX_TESTS_DICTS)
  656. def get_tests(collection='original', smoothness=None):
  657. r"""Return the requested collection of test cases, as an array of dicts with subset-specific keys
  658. Allowed values of collection:
  659. 'original': The original benchmarking functions.
  660. Real-valued functions of real-valued inputs on an interval with a zero.
  661. f1, .., f3 are continuous and infinitely differentiable
  662. f4 has a single discontinuity at the root
  663. f5 has a root at 1 replacing a 1st order pole
  664. f6 is randomly positive on one side of the root, randomly negative on the other
  665. 'aps': The test problems in the TOMS "Algorithm 748: Enclosing Zeros of Continuous Functions"
  666. paper by Alefeld, Potra and Shi. Real-valued functions of
  667. real-valued inputs on an interval with a zero.
  668. Suitable for methods which start with an enclosing interval, and
  669. derivatives up to 2nd order.
  670. 'complex': Some complex-valued functions of complex-valued inputs.
  671. No enclosing bracket is provided.
  672. Suitable for methods which use one or more starting values, and
  673. derivatives up to 2nd order.
  674. The dictionary keys will be a subset of
  675. ["f", "fprime", "fprime2", "args", "bracket", "a", b", "smoothness", "x0", "x1", "root", "ID"]
  676. """ # noqa: E501
  677. collection = collection or "original"
  678. subsets = {"aps": _APS_TESTS_DICTS,
  679. "complex": _COMPLEX_TESTS_DICTS,
  680. "original": _ORIGINAL_TESTS_DICTS,
  681. "chandrupatla": _CHANDRUPATLA_TESTS_DICTS}
  682. tests = subsets.get(collection, [])
  683. if smoothness is not None:
  684. tests = [tc for tc in tests if tc['smoothness'] >= smoothness]
  685. return tests
  686. # Backwards compatibility
  687. methods = [cc.bisect, cc.ridder, cc.brenth, cc.brentq]
  688. mstrings = ['cc.bisect', 'cc.ridder', 'cc.brenth', 'cc.brentq']
  689. functions = [f2, f3, f4, f5, f6]
  690. fstrings = ['f2', 'f3', 'f4', 'f5', 'f6']
  691. # ##################
  692. # "Chandrupatla" test cases
  693. # Functions and test cases that appear in [2]
  694. def fun1(x):
  695. return x**3 - 2*x - 5
  696. fun1.root = 2.0945514815423265 # additional precision using mpmath.findroot
  697. def fun2(x):
  698. return 1 - 1/x**2
  699. fun2.root = 1
  700. def fun3(x):
  701. return (x-3)**3
  702. fun3.root = 3
  703. def fun4(x):
  704. return 6*(x-2)**5
  705. fun4.root = 2
  706. def fun5(x):
  707. return x**9
  708. fun5.root = 0
  709. def fun6(x):
  710. return x**19
  711. fun6.root = 0
  712. def fun7(x):
  713. xp = array_namespace(x)
  714. return 0 if xp.abs(x) < 3.8e-4 else x*xp.exp(-x**(-2))
  715. fun7.root = 0
  716. def fun8(x):
  717. xp = array_namespace(x)
  718. xi = 0.61489
  719. return -(3062*(1-xi)*xp.exp(-x))/(xi + (1-xi)*xp.exp(-x)) - 1013 + 1628/x
  720. fun8.root = 1.0375360332870405
  721. def fun9(x):
  722. xp = array_namespace(x)
  723. return xp.exp(x) - 2 - 0.01/x**2 + .000002/x**3
  724. fun9.root = 0.7032048403631358
  725. # Each "chandropatla" test case has
  726. # - a function,
  727. # - two starting values x0 and x1
  728. # - the root
  729. # - the number of function evaluations required by Chandrupatla's algorithm
  730. # - an Identifier of the test case
  731. #
  732. # Chandrupatla's is a bracketing algorithm, so a bracketing interval was
  733. # provided in [2] for each test case. No special support for testing with
  734. # secant/Newton/Halley is provided.
  735. _CHANDRUPATLA_TESTS_KEYS = ["f", "bracket", "root", "nfeval", "ID"]
  736. _CHANDRUPATLA_TESTS = [
  737. [fun1, [2, 3], fun1.root, 7],
  738. [fun1, [1, 10], fun1.root, 11],
  739. [fun1, [1, 100], fun1.root, 14],
  740. [fun1, [-1e4, 1e4], fun1.root, 23],
  741. [fun1, [-1e10, 1e10], fun1.root, 43],
  742. [fun2, [0.5, 1.51], fun2.root, 8],
  743. [fun2, [1e-4, 1e4], fun2.root, 22],
  744. [fun2, [1e-6, 1e6], fun2.root, 28],
  745. [fun2, [1e-10, 1e10], fun2.root, 41],
  746. [fun2, [1e-12, 1e12], fun2.root, 48],
  747. [fun3, [0, 5], fun3.root, 21],
  748. [fun3, [-10, 10], fun3.root, 23],
  749. [fun3, [-1e4, 1e4], fun3.root, 36],
  750. [fun3, [-1e6, 1e6], fun3.root, 45],
  751. [fun3, [-1e10, 1e10], fun3.root, 55],
  752. [fun4, [0, 5], fun4.root, 21],
  753. [fun4, [-10, 10], fun4.root, 23],
  754. [fun4, [-1e4, 1e4], fun4.root, 33],
  755. [fun4, [-1e6, 1e6], fun4.root, 43],
  756. [fun4, [-1e10, 1e10], fun4.root, 54],
  757. [fun5, [-1, 4], fun5.root, 21],
  758. [fun5, [-2, 5], fun5.root, 22],
  759. [fun5, [-1, 10], fun5.root, 23],
  760. [fun5, [-5, 50], fun5.root, 25],
  761. [fun5, [-10, 100], fun5.root, 26],
  762. [fun6, [-1., 4.], fun6.root, 21],
  763. [fun6, [-2., 5.], fun6.root, 22],
  764. [fun6, [-1., 10.], fun6.root, 23],
  765. [fun6, [-5., 50.], fun6.root, 25],
  766. [fun6, [-10., 100.], fun6.root, 26],
  767. [fun7, [-1, 4], fun7.root, 8],
  768. [fun7, [-2, 5], fun7.root, 8],
  769. [fun7, [-1, 10], fun7.root, 11],
  770. [fun7, [-5, 50], fun7.root, 18],
  771. [fun7, [-10, 100], fun7.root, 19],
  772. [fun8, [2e-4, 2], fun8.root, 9],
  773. [fun8, [2e-4, 3], fun8.root, 10],
  774. [fun8, [2e-4, 9], fun8.root, 11],
  775. [fun8, [2e-4, 27], fun8.root, 12],
  776. [fun8, [2e-4, 81], fun8.root, 14],
  777. [fun9, [2e-4, 1], fun9.root, 7],
  778. [fun9, [2e-4, 3], fun9.root, 8],
  779. [fun9, [2e-4, 9], fun9.root, 10],
  780. [fun9, [2e-4, 27], fun9.root, 11],
  781. [fun9, [2e-4, 81], fun9.root, 13],
  782. ]
  783. _CHANDRUPATLA_TESTS = [test + [f'{test[0].__name__}.{i%5+1}']
  784. for i, test in enumerate(_CHANDRUPATLA_TESTS)]
  785. _CHANDRUPATLA_TESTS_DICTS = [dict(zip(_CHANDRUPATLA_TESTS_KEYS, testcase))
  786. for testcase in _CHANDRUPATLA_TESTS]
  787. _add_a_b(_CHANDRUPATLA_TESTS_DICTS)