test_nonlin.py 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571
  1. """ Unit tests for nonlinear solvers
  2. Author: Ondrej Certik
  3. May 2007
  4. """
  5. from numpy.testing import assert_
  6. import pytest
  7. from functools import partial
  8. from scipy.optimize import _nonlin as nonlin, root
  9. from scipy.sparse import csr_array
  10. from numpy import diag, dot
  11. from numpy.linalg import inv
  12. import numpy as np
  13. import scipy
  14. from scipy.sparse.linalg import minres
  15. from .test_minpack import pressure_network
  16. SOLVERS = {'anderson': nonlin.anderson,
  17. 'diagbroyden': nonlin.diagbroyden,
  18. 'linearmixing': nonlin.linearmixing,
  19. 'excitingmixing': nonlin.excitingmixing,
  20. 'broyden1': nonlin.broyden1,
  21. 'broyden2': nonlin.broyden2,
  22. 'krylov': nonlin.newton_krylov}
  23. MUST_WORK = {'anderson': nonlin.anderson, 'broyden1': nonlin.broyden1,
  24. 'broyden2': nonlin.broyden2, 'krylov': nonlin.newton_krylov}
  25. # ----------------------------------------------------------------------------
  26. # Test problems
  27. # ----------------------------------------------------------------------------
  28. def F(x):
  29. x = np.asarray(x).T
  30. d = diag([3, 2, 1.5, 1, 0.5])
  31. c = 0.01
  32. f = -d @ x - c * float(x.T @ x) * x
  33. return f
  34. F.xin = [1, 1, 1, 1, 1]
  35. F.KNOWN_BAD = {}
  36. F.JAC_KSP_BAD = {}
  37. F.ROOT_JAC_KSP_BAD = {}
  38. def F2(x):
  39. return x
  40. F2.xin = [1, 2, 3, 4, 5, 6]
  41. F2.KNOWN_BAD = {'linearmixing': nonlin.linearmixing,
  42. 'excitingmixing': nonlin.excitingmixing}
  43. F2.JAC_KSP_BAD = {}
  44. F2.ROOT_JAC_KSP_BAD = {}
  45. def F2_lucky(x):
  46. return x
  47. F2_lucky.xin = [0, 0, 0, 0, 0, 0]
  48. F2_lucky.KNOWN_BAD = {}
  49. F2_lucky.JAC_KSP_BAD = {}
  50. F2_lucky.ROOT_JAC_KSP_BAD = {}
  51. def F3(x):
  52. A = np.array([[-2, 1, 0.], [1, -2, 1], [0, 1, -2]])
  53. b = np.array([1, 2, 3.])
  54. return A @ x - b
  55. F3.xin = [1, 2, 3]
  56. F3.KNOWN_BAD = {}
  57. F3.JAC_KSP_BAD = {}
  58. F3.ROOT_JAC_KSP_BAD = {}
  59. def F4_powell(x):
  60. A = 1e4
  61. return [A*x[0]*x[1] - 1, np.exp(-x[0]) + np.exp(-x[1]) - (1 + 1/A)]
  62. F4_powell.xin = [-1, -2]
  63. F4_powell.KNOWN_BAD = {'linearmixing': nonlin.linearmixing,
  64. 'excitingmixing': nonlin.excitingmixing,
  65. 'diagbroyden': nonlin.diagbroyden}
  66. # In the extreme case, it does not converge for nolinear problem solved by
  67. # MINRES and root problem solved by GMRES/BiCGStab/CGS/MINRES/TFQMR when using
  68. # Krylov method to approximate Jacobian
  69. F4_powell.JAC_KSP_BAD = {'minres'}
  70. F4_powell.ROOT_JAC_KSP_BAD = {'gmres', 'bicgstab', 'cgs', 'minres', 'tfqmr'}
  71. def F5(x):
  72. return pressure_network(x, 4, np.array([.5, .5, .5, .5]))
  73. F5.xin = [2., 0, 2, 0]
  74. F5.KNOWN_BAD = {'excitingmixing': nonlin.excitingmixing,
  75. 'linearmixing': nonlin.linearmixing,
  76. 'diagbroyden': nonlin.diagbroyden}
  77. # In the extreme case, the Jacobian inversion yielded zero vector for nonlinear
  78. # problem solved by CGS/MINRES and it does not converge for root problem solved
  79. # by MINRES and when using Krylov method to approximate Jacobian
  80. F5.JAC_KSP_BAD = {'cgs', 'minres'}
  81. F5.ROOT_JAC_KSP_BAD = {'minres'}
  82. def F6(x):
  83. x1, x2 = x
  84. J0 = np.array([[-4.256, 14.7],
  85. [0.8394989, 0.59964207]])
  86. v = np.array([(x1 + 3) * (x2**5 - 7) + 3*6,
  87. np.sin(x2 * np.exp(x1) - 1)])
  88. return -np.linalg.solve(J0, v)
  89. F6.xin = [-0.5, 1.4]
  90. F6.KNOWN_BAD = {'excitingmixing': nonlin.excitingmixing,
  91. 'linearmixing': nonlin.linearmixing,
  92. 'diagbroyden': nonlin.diagbroyden}
  93. F6.JAC_KSP_BAD = {}
  94. F6.ROOT_JAC_KSP_BAD = {}
  95. # ----------------------------------------------------------------------------
  96. # Tests
  97. # ----------------------------------------------------------------------------
  98. class TestNonlin:
  99. """
  100. Check the Broyden methods for a few test problems.
  101. broyden1, broyden2, and newton_krylov must succeed for
  102. all functions. Some of the others don't -- tests in KNOWN_BAD are skipped.
  103. """
  104. def _check_nonlin_func(self, f, func, f_tol=1e-2):
  105. # Test all methods mentioned in the class `KrylovJacobian`
  106. if func == SOLVERS['krylov']:
  107. for method in ['gmres', 'bicgstab', 'cgs', 'minres', 'tfqmr']:
  108. if method in f.JAC_KSP_BAD:
  109. continue
  110. x = func(f, f.xin, method=method, line_search=None,
  111. f_tol=f_tol, maxiter=200, verbose=0)
  112. assert_(np.absolute(f(x)).max() < f_tol)
  113. x = func(f, f.xin, f_tol=f_tol, maxiter=200, verbose=0)
  114. assert_(np.absolute(f(x)).max() < f_tol)
  115. def _check_root(self, f, method, f_tol=1e-2):
  116. # Test Krylov methods
  117. if method == 'krylov':
  118. for jac_method in ['gmres', 'bicgstab', 'cgs', 'minres', 'tfqmr']:
  119. if jac_method in f.ROOT_JAC_KSP_BAD:
  120. continue
  121. res = root(f, f.xin, method=method,
  122. options={'ftol': f_tol, 'maxiter': 200,
  123. 'disp': 0,
  124. 'jac_options': {'method': jac_method}})
  125. assert_(np.absolute(res.fun).max() < f_tol)
  126. res = root(f, f.xin, method=method,
  127. options={'ftol': f_tol, 'maxiter': 200, 'disp': 0})
  128. assert_(np.absolute(res.fun).max() < f_tol)
  129. @pytest.mark.xfail
  130. def _check_func_fail(self, *a, **kw):
  131. pass
  132. @pytest.mark.filterwarnings('ignore::DeprecationWarning')
  133. def test_problem_nonlin(self):
  134. for f in [F, F2, F2_lucky, F3, F4_powell, F5, F6]:
  135. for func in SOLVERS.values():
  136. if func in f.KNOWN_BAD.values():
  137. if func in MUST_WORK.values():
  138. self._check_func_fail(f, func)
  139. continue
  140. self._check_nonlin_func(f, func)
  141. @pytest.mark.filterwarnings('ignore::DeprecationWarning')
  142. @pytest.mark.parametrize("method", ['lgmres', 'gmres', 'bicgstab', 'cgs',
  143. 'minres', 'tfqmr'])
  144. def test_tol_norm_called(self, method):
  145. # Check that supplying tol_norm keyword to nonlin_solve works
  146. self._tol_norm_used = False
  147. def local_norm_func(x):
  148. self._tol_norm_used = True
  149. return np.absolute(x).max()
  150. nonlin.newton_krylov(F, F.xin, method=method, f_tol=1e-2,
  151. maxiter=200, verbose=0,
  152. tol_norm=local_norm_func)
  153. assert_(self._tol_norm_used)
  154. @pytest.mark.filterwarnings('ignore::DeprecationWarning')
  155. def test_problem_root(self):
  156. for f in [F, F2, F2_lucky, F3, F4_powell, F5, F6]:
  157. for meth in SOLVERS:
  158. if meth in f.KNOWN_BAD:
  159. if meth in MUST_WORK:
  160. self._check_func_fail(f, meth)
  161. continue
  162. self._check_root(f, meth)
  163. def test_no_convergence(self):
  164. def wont_converge(x):
  165. return 1e3 + x
  166. with pytest.raises(scipy.optimize.NoConvergence):
  167. nonlin.newton_krylov(wont_converge, xin=[0], maxiter=1)
  168. def test_warnings_invalid_inner_param(self):
  169. """
  170. Test for ENH #21986, for behavior of `nonlin.newton_krylov`
  171. Test the following scenarios:
  172. 1. Raise warning for invalid inner param
  173. 2. No warning for valid inner param
  174. 3. No warning for user-provided callable method
  175. """
  176. # This should raise exactly one warning
  177. # (`inner_atol` is not valid for `minres`)
  178. with pytest.warns(UserWarning,
  179. match="Please check inner method documentation"):
  180. nonlin.newton_krylov(F, F.xin, method="minres", inner_atol=1e-5)
  181. # This should not raise a warning (`minres` without `inner_atol`,
  182. # but with `inner_maxiter` which is valid)
  183. nonlin.newton_krylov(F, F.xin, method="minres", inner_maxiter=100,
  184. inner_callback= lambda _ : ...)
  185. # Test newton_krylov with a user-provided callable method
  186. def user_provided_callable_method_enh_21986(op, rhs, **kwargs):
  187. """A dummy user-provided callable method for testing."""
  188. # Return a dummy result (mimicking minres)
  189. return minres(op, rhs, **kwargs)
  190. # This should not raise any warnings
  191. nonlin.newton_krylov(F, F.xin,
  192. method=user_provided_callable_method_enh_21986)
  193. def test_non_inner_prefix(self):
  194. with pytest.raises(ValueError,
  195. match="Unknown parameter"
  196. ):
  197. # Pass a parameter without 'inner_' prefix
  198. nonlin.newton_krylov(F, F.xin, method="minres", invalid_param=1e-5)
  199. class TestSecant:
  200. """Check that some Jacobian approximations satisfy the secant condition"""
  201. xs = [np.array([1., 2., 3., 4., 5.]),
  202. np.array([2., 3., 4., 5., 1.]),
  203. np.array([3., 4., 5., 1., 2.]),
  204. np.array([4., 5., 1., 2., 3.]),
  205. np.array([9., 1., 9., 1., 3.]),
  206. np.array([0., 1., 9., 1., 3.]),
  207. np.array([5., 5., 7., 1., 1.]),
  208. np.array([1., 2., 7., 5., 1.]),]
  209. fs = [x**2 - 1 for x in xs]
  210. def _check_secant(self, jac_cls, npoints=1, **kw):
  211. """
  212. Check that the given Jacobian approximation satisfies secant
  213. conditions for last `npoints` points.
  214. """
  215. jac = jac_cls(**kw)
  216. jac.setup(self.xs[0], self.fs[0], None)
  217. for j, (x, f) in enumerate(zip(self.xs[1:], self.fs[1:])):
  218. jac.update(x, f)
  219. for k in range(min(npoints, j+1)):
  220. dx = self.xs[j-k+1] - self.xs[j-k]
  221. df = self.fs[j-k+1] - self.fs[j-k]
  222. assert_(np.allclose(dx, jac.solve(df)))
  223. # Check that the `npoints` secant bound is strict
  224. if j >= npoints:
  225. dx = self.xs[j-npoints+1] - self.xs[j-npoints]
  226. df = self.fs[j-npoints+1] - self.fs[j-npoints]
  227. assert_(not np.allclose(dx, jac.solve(df)))
  228. def test_broyden1(self):
  229. self._check_secant(nonlin.BroydenFirst)
  230. def test_broyden2(self):
  231. self._check_secant(nonlin.BroydenSecond)
  232. def test_broyden1_update(self):
  233. # Check that BroydenFirst update works as for a dense matrix
  234. jac = nonlin.BroydenFirst(alpha=0.1)
  235. jac.setup(self.xs[0], self.fs[0], None)
  236. B = np.identity(5) * (-1/0.1)
  237. for last_j, (x, f) in enumerate(zip(self.xs[1:], self.fs[1:])):
  238. df = f - self.fs[last_j]
  239. dx = x - self.xs[last_j]
  240. B += (df - dot(B, dx))[:, None] * dx[None, :] / dot(dx, dx)
  241. jac.update(x, f)
  242. assert_(np.allclose(jac.todense(), B, rtol=1e-10, atol=1e-13))
  243. def test_broyden2_update(self):
  244. # Check that BroydenSecond update works as for a dense matrix
  245. jac = nonlin.BroydenSecond(alpha=0.1)
  246. jac.setup(self.xs[0], self.fs[0], None)
  247. H = np.identity(5) * (-0.1)
  248. for last_j, (x, f) in enumerate(zip(self.xs[1:], self.fs[1:])):
  249. df = f - self.fs[last_j]
  250. dx = x - self.xs[last_j]
  251. H += (dx - dot(H, df))[:, None] * df[None, :] / dot(df, df)
  252. jac.update(x, f)
  253. assert_(np.allclose(jac.todense(), inv(H), rtol=1e-10, atol=1e-13))
  254. def test_anderson(self):
  255. # Anderson mixing (with w0=0) satisfies secant conditions
  256. # for the last M iterates, see [Ey]_
  257. #
  258. # .. [Ey] V. Eyert, J. Comp. Phys., 124, 271 (1996).
  259. self._check_secant(nonlin.Anderson, M=3, w0=0, npoints=3)
  260. class TestLinear:
  261. """Solve a linear equation;
  262. some methods find the exact solution in a finite number of steps"""
  263. def _check(self, jac, N, maxiter, complex=False, **kw):
  264. rng = np.random.default_rng(123)
  265. A = rng.standard_normal((N, N))
  266. if complex:
  267. A = A + 1j*rng.standard_normal((N, N))
  268. b = rng.standard_normal(N)
  269. if complex:
  270. b = b + 1j*rng.standard_normal(N)
  271. def func(x):
  272. return dot(A, x) - b
  273. sol = nonlin.nonlin_solve(func, np.zeros(N), jac, maxiter=maxiter,
  274. f_tol=1e-6, line_search=None, verbose=0)
  275. assert_(np.allclose(dot(A, sol), b, atol=1e-6))
  276. def test_broyden1(self):
  277. # Broyden methods solve linear systems exactly in 2*N steps
  278. self._check(nonlin.BroydenFirst(alpha=1.0), 20, 41, False)
  279. self._check(nonlin.BroydenFirst(alpha=1.0), 20, 41, True)
  280. def test_broyden2(self):
  281. # Broyden methods solve linear systems exactly in 2*N steps
  282. self._check(nonlin.BroydenSecond(alpha=1.0), 20, 41, False)
  283. self._check(nonlin.BroydenSecond(alpha=1.0), 20, 41, True)
  284. def test_anderson(self):
  285. # Anderson is rather similar to Broyden, if given enough storage space
  286. self._check(nonlin.Anderson(M=50, alpha=1.0), 20, 29, False)
  287. self._check(nonlin.Anderson(M=50, alpha=1.0), 20, 29, True)
  288. def test_krylov(self):
  289. # Krylov methods solve linear systems exactly in N inner steps
  290. self._check(nonlin.KrylovJacobian, 20, 2, False, inner_m=10)
  291. self._check(nonlin.KrylovJacobian, 20, 2, True, inner_m=10)
  292. def _check_autojac(self, A, b):
  293. def func(x):
  294. return A.dot(x) - b
  295. def jac(v):
  296. return A
  297. sol = nonlin.nonlin_solve(func, np.zeros(b.shape[0]), jac, maxiter=2,
  298. f_tol=1e-6, line_search=None, verbose=0)
  299. np.testing.assert_allclose(A @ sol, b, atol=1e-6)
  300. # test jac input as array -- not a function
  301. sol = nonlin.nonlin_solve(func, np.zeros(b.shape[0]), A, maxiter=2,
  302. f_tol=1e-6, line_search=None, verbose=0)
  303. np.testing.assert_allclose(A @ sol, b, atol=1e-6)
  304. def test_jac_sparse(self):
  305. A = csr_array([[1, 2], [2, 1]])
  306. b = np.array([1, -1])
  307. self._check_autojac(A, b)
  308. self._check_autojac((1 + 2j) * A, (2 + 2j) * b)
  309. def test_jac_ndarray(self):
  310. A = np.array([[1, 2], [2, 1]])
  311. b = np.array([1, -1])
  312. self._check_autojac(A, b)
  313. self._check_autojac((1 + 2j) * A, (2 + 2j) * b)
  314. class TestJacobianDotSolve:
  315. """
  316. Check that solve/dot methods in Jacobian approximations are consistent
  317. """
  318. def _func(self, x, A=None):
  319. return x**2 - 1 + np.dot(A, x)
  320. def _check_dot(self, jac_cls, complex=False, tol=1e-6, **kw):
  321. rng = np.random.default_rng(123)
  322. N = 7
  323. def rand(*a):
  324. q = rng.random(a)
  325. if complex:
  326. q = q + 1j*rng.random(a)
  327. return q
  328. def assert_close(a, b, msg):
  329. d = abs(a - b).max()
  330. f = tol + abs(b).max()*tol
  331. if d > f:
  332. raise AssertionError(f'{msg}: err {d:g}')
  333. A = rand(N, N)
  334. # initialize
  335. x0 = rng.random(N)
  336. jac = jac_cls(**kw)
  337. jac.setup(x0, self._func(x0, A), partial(self._func, A=A))
  338. # check consistency
  339. for k in range(2*N):
  340. v = rand(N)
  341. if hasattr(jac, '__array__'):
  342. Jd = np.array(jac)
  343. if hasattr(jac, 'solve'):
  344. Gv = jac.solve(v)
  345. Gv2 = np.linalg.solve(Jd, v)
  346. assert_close(Gv, Gv2, 'solve vs array')
  347. if hasattr(jac, 'rsolve'):
  348. Gv = jac.rsolve(v)
  349. Gv2 = np.linalg.solve(Jd.T.conj(), v)
  350. assert_close(Gv, Gv2, 'rsolve vs array')
  351. if hasattr(jac, 'matvec'):
  352. Jv = jac.matvec(v)
  353. Jv2 = np.dot(Jd, v)
  354. assert_close(Jv, Jv2, 'dot vs array')
  355. if hasattr(jac, 'rmatvec'):
  356. Jv = jac.rmatvec(v)
  357. Jv2 = np.dot(Jd.T.conj(), v)
  358. assert_close(Jv, Jv2, 'rmatvec vs array')
  359. if hasattr(jac, 'matvec') and hasattr(jac, 'solve'):
  360. Jv = jac.matvec(v)
  361. Jv2 = jac.solve(jac.matvec(Jv))
  362. assert_close(Jv, Jv2, 'dot vs solve')
  363. if hasattr(jac, 'rmatvec') and hasattr(jac, 'rsolve'):
  364. Jv = jac.rmatvec(v)
  365. Jv2 = jac.rmatvec(jac.rsolve(Jv))
  366. assert_close(Jv, Jv2, 'rmatvec vs rsolve')
  367. x = rand(N)
  368. jac.update(x, self._func(x, A))
  369. def test_broyden1(self):
  370. self._check_dot(nonlin.BroydenFirst, complex=False)
  371. self._check_dot(nonlin.BroydenFirst, complex=True)
  372. def test_broyden2(self):
  373. self._check_dot(nonlin.BroydenSecond, complex=False)
  374. self._check_dot(nonlin.BroydenSecond, complex=True)
  375. def test_anderson(self):
  376. self._check_dot(nonlin.Anderson, complex=False)
  377. self._check_dot(nonlin.Anderson, complex=True)
  378. def test_diagbroyden(self):
  379. self._check_dot(nonlin.DiagBroyden, complex=False)
  380. self._check_dot(nonlin.DiagBroyden, complex=True)
  381. def test_linearmixing(self):
  382. self._check_dot(nonlin.LinearMixing, complex=False)
  383. self._check_dot(nonlin.LinearMixing, complex=True)
  384. def test_excitingmixing(self):
  385. self._check_dot(nonlin.ExcitingMixing, complex=False)
  386. self._check_dot(nonlin.ExcitingMixing, complex=True)
  387. def test_krylov(self):
  388. self._check_dot(nonlin.KrylovJacobian, complex=False, tol=1e-3)
  389. self._check_dot(nonlin.KrylovJacobian, complex=True, tol=1e-3)
  390. class TestNonlinOldTests:
  391. """ Test case for a simple constrained entropy maximization problem
  392. (the machine translation example of Berger et al in
  393. Computational Linguistics, vol 22, num 1, pp 39--72, 1996.)
  394. """
  395. def test_broyden1(self):
  396. x = nonlin.broyden1(F, F.xin, iter=12, alpha=1)
  397. assert_(nonlin.norm(x) < 1e-9)
  398. assert_(nonlin.norm(F(x)) < 1e-9)
  399. def test_broyden2(self):
  400. x = nonlin.broyden2(F, F.xin, iter=12, alpha=1)
  401. assert_(nonlin.norm(x) < 1e-9)
  402. assert_(nonlin.norm(F(x)) < 1e-9)
  403. def test_anderson(self):
  404. x = nonlin.anderson(F, F.xin, iter=12, alpha=0.03, M=5)
  405. assert_(nonlin.norm(x) < 0.33)
  406. def test_linearmixing(self):
  407. x = nonlin.linearmixing(F, F.xin, iter=60, alpha=0.5)
  408. assert_(nonlin.norm(x) < 1e-7)
  409. assert_(nonlin.norm(F(x)) < 1e-7)
  410. def test_exciting(self):
  411. x = nonlin.excitingmixing(F, F.xin, iter=20, alpha=0.5)
  412. assert_(nonlin.norm(x) < 1e-5)
  413. assert_(nonlin.norm(F(x)) < 1e-5)
  414. def test_diagbroyden(self):
  415. x = nonlin.diagbroyden(F, F.xin, iter=11, alpha=1)
  416. assert_(nonlin.norm(x) < 1e-8)
  417. assert_(nonlin.norm(F(x)) < 1e-8)
  418. def test_root_broyden1(self):
  419. res = root(F, F.xin, method='broyden1',
  420. options={'nit': 12, 'jac_options': {'alpha': 1}})
  421. assert_(nonlin.norm(res.x) < 1e-9)
  422. assert_(nonlin.norm(res.fun) < 1e-9)
  423. def test_root_broyden2(self):
  424. res = root(F, F.xin, method='broyden2',
  425. options={'nit': 12, 'jac_options': {'alpha': 1}})
  426. assert_(nonlin.norm(res.x) < 1e-9)
  427. assert_(nonlin.norm(res.fun) < 1e-9)
  428. def test_root_anderson(self):
  429. res = root(F, F.xin, method='anderson',
  430. options={'nit': 12,
  431. 'jac_options': {'alpha': 0.03, 'M': 5}})
  432. assert_(nonlin.norm(res.x) < 0.33)
  433. def test_root_linearmixing(self):
  434. res = root(F, F.xin, method='linearmixing',
  435. options={'nit': 60,
  436. 'jac_options': {'alpha': 0.5}})
  437. assert_(nonlin.norm(res.x) < 1e-7)
  438. assert_(nonlin.norm(res.fun) < 1e-7)
  439. def test_root_excitingmixing(self):
  440. res = root(F, F.xin, method='excitingmixing',
  441. options={'nit': 20,
  442. 'jac_options': {'alpha': 0.5}})
  443. assert_(nonlin.norm(res.x) < 1e-5)
  444. assert_(nonlin.norm(res.fun) < 1e-5)
  445. def test_root_diagbroyden(self):
  446. res = root(F, F.xin, method='diagbroyden',
  447. options={'nit': 11,
  448. 'jac_options': {'alpha': 1}})
  449. assert_(nonlin.norm(res.x) < 1e-8)
  450. assert_(nonlin.norm(res.fun) < 1e-8)