def _array_newton(func, x0, fprime, args, tol, maxiter, fprime2,<EOL>converged=False):
try:<EOL><INDENT>p = np.asarray(x0, dtype=float)<EOL><DEDENT>except TypeError:  <EOL><INDENT>p = np.asarray(x0)<EOL><DEDENT>failures = np.ones_like(p, dtype=bool)  <EOL>nz_der = np.copy(failures)<EOL>if fprime is not None:<EOL><INDENT>for iteration in range(maxiter):<EOL><INDENT>fval = np.asarray(func(p, *args))<EOL>if not fval.any():<EOL><INDENT>failures = fval.astype(bool)<EOL>break<EOL><DEDENT>fder = np.asarray(fprime(p, *args))<EOL>nz_der = (fder != <NUM_LIT:0>)<EOL>if not nz_der.any():<EOL><INDENT>break<EOL><DEDENT>dp = fval[nz_der] / fder[nz_der]<EOL>if fprime2 is not None:<EOL><INDENT>fder2 = np.asarray(fprime2(p, *args))<EOL>dp = dp / (<NUM_LIT:1.0> - <NUM_LIT:0.5> * dp * fder2[nz_der] / fder[nz_der])<EOL><DEDENT>p[nz_der] -= dp<EOL>failures[nz_der] = np.abs(dp) >= tol  <EOL>if not failures[nz_der].any():<EOL><INDENT>break<EOL><DEDENT><DEDENT><DEDENT>else:<EOL><INDENT>dx = np.finfo(float).eps**<NUM_LIT><EOL>p1 = p * (<NUM_LIT:1> + dx) + np.where(p >= <NUM_LIT:0>, dx, -dx)<EOL>q0 = np.asarray(func(p, *args))<EOL>q1 = np.asarray(func(p1, *args))<EOL>active = np.ones_like(p, dtype=bool)<EOL>for iteration in range(maxiter):<EOL><INDENT>nz_der = (q1 != q0)<EOL>if not nz_der.any():<EOL><INDENT>p = (p1 + p) / <NUM_LIT><EOL>break<EOL><DEDENT>dp = (q1 * (p1 - p))[nz_der] / (q1 - q0)[nz_der]<EOL>p[nz_der] = p1[nz_der] - dp<EOL>active_zero_der = ~nz_der & active<EOL>p[active_zero_der] = (p1 + p)[active_zero_der] / <NUM_LIT><EOL>active &= nz_der  <EOL>failures[nz_der] = np.abs(dp) >= tol  <EOL>if not failures[nz_der].any():<EOL><INDENT>break<EOL><DEDENT>p1, p = p, p1<EOL>q0 = q1<EOL>q1 = np.asarray(func(p1, *args))<EOL><DEDENT><DEDENT>zero_der = ~nz_der & failures  <EOL>if zero_der.any():<EOL><INDENT>if fprime is None:<EOL><INDENT>nonzero_dp = (p1 != p)<EOL>zero_der_nz_dp = (zero_der & nonzero_dp)<EOL>if zero_der_nz_dp.any():<EOL><INDENT>rms = np.sqrt(<EOL>sum((p1[zero_der_nz_dp] - p[zero_der_nz_dp]) ** <NUM_LIT:2>)<EOL>)<EOL>warnings.warn('<STR_LIT>'.format(rms),<EOL>RuntimeWarning)<EOL><DEDENT><DEDENT>else:<EOL><INDENT>all_or_some = '<STR_LIT:all>' if zero_der.all() else '<STR_LIT>'<EOL>msg = '<STR_LIT>'.format(all_or_some)<EOL>warnings.warn(msg, RuntimeWarning)<EOL><DEDENT><DEDENT>elif failures.any():<EOL><INDENT>all_or_some = '<STR_LIT:all>' if failures.all() else '<STR_LIT>'<EOL>msg = '<STR_LIT>'.format(<EOL>all_or_some, maxiter<EOL>)<EOL>if failures.all():<EOL><INDENT>raise RuntimeError(msg)<EOL><DEDENT>warnings.warn(msg, RuntimeWarning)<EOL><DEDENT>if converged:<EOL><INDENT>result = namedtuple('<STR_LIT:result>', ('<STR_LIT:root>', '<STR_LIT>', '<STR_LIT>'))<EOL>p = result(p, ~failures, zero_der)<EOL><DEDENT>return p<EOL>
A vectorized version of Newton, Halley, and secant methods for arrays. Do
not use this method directly. This method is called from :func:`newton`
when ``np.isscalar(x0)`` is true. For docstring, see :func:`newton`.