<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def _newton_refine(s, nodes1, t, nodes2):
 r"""Apply one step of 2D Newton's method. .. note:: There is also a Fortran implementation of this function, which will be used if it can be built. We want to use Newton's method on the function .. math:: F(s, t) = B_1(s) - B_2(t) to refine :math:`\left(s_{\ast}, t_{\ast}\right)`. Using this, and the Jacobian :math:`DF`, we "solve" .. math:: \left[\begin{array}{c} 0 \\ 0 \end{array}\right] \approx F\left(s_{\ast} + \Delta s, t_{\ast} + \Delta t\right) \approx F\left(s_{\ast}, t_{\ast}\right) + \left[\begin{array}{c c} B_1'\left(s_{\ast}\right) & - B_2'\left(t_{\ast}\right) \end{array}\right] \left[\begin{array}{c} \Delta s \\ \Delta t \end{array}\right] and refine with the component updates :math:`\Delta s` and :math:`\Delta t`. .. note:: This implementation assumes the curves live in :math:`\mathbf{R}^2`. For example, the curves .. math:: \begin{align*} B_1(s) &= \left[\begin{array}{c} 0 \\ 0 \end{array}\right] (1 - s)^2 + \left[\begin{array}{c} 2 \\ 4 \end{array}\right] 2s(1 - s) + \left[\begin{array}{c} 4 \\ 0 \end{array}\right] s^2 \\ B_2(t) &= \left[\begin{array}{c} 2 \\ 0 \end{array}\right] (1 - t) + \left[\begin{array}{c} 0 \\ 3 \end{array}\right] t \end{align*} intersect at the point :math:`B_1\left(\frac{1}{4}\right) = B_2\left(\frac{1}{2}\right) = \frac{1}{2} \left[\begin{array}{c} 2 \\ 3 \end{array}\right]`. However, starting from the wrong point we have .. math:: \begin{align*} F\left(\frac{3}{8}, \frac{1}{4}\right) &= \frac{1}{8} \left[\begin{array}{c} 0 \\ 9 \end{array}\right] \\ DF\left(\frac{3}{8}, \frac{1}{4}\right) &= \left[\begin{array}{c c} 4 & 2 \\ 2 & -3 \end{array}\right] \\ \Longrightarrow \left[\begin{array}{c} \Delta s \\ \Delta t \end{array}\right] &= \frac{9}{64} \left[\begin{array}{c} -1 \\ 2 \end{array}\right]. \end{align*} .. image:: ../images/newton_refine1.png :align: center .. testsetup:: newton-refine1, newton-refine2, newton-refine3 import numpy as np import bezier from bezier._intersection_helpers import newton_refine machine_eps = np.finfo(np.float64).eps def cuberoot(value):
 return np.cbrt(value) .. doctest:: newton-refine1 -9.0 18.0 .. testcleanup:: newton-refine1 import make_images curve1 = bezier.Curve(nodes1, degree=2) curve2 = bezier.Curve(nodes2, degree=1) make_images.newton_refine1(s, new_s, curve1, t, new_t, curve2) For "typical" curves, we converge to a solution quadratically. This means that the number of correct digits doubles every iteration (until machine precision is reached). .. image:: ../images/newton_refine2.png :align: center .. doctest:: newton-refine2 True .. testcleanup:: newton-refine2 import make_images curve1 = bezier.Curve(nodes1, degree=4) curve2 = bezier.Curve(nodes2, degree=4) make_images.newton_refine2(s_vals, curve1, curve2) However, when the intersection occurs at a point of tangency, the convergence becomes linear. This means that the number of correct digits added each iteration is roughly constant. .. image:: ../images/newton_refine3.png :align: center .. doctest:: newton-refine3 -3.0 -4.0 -5.0 -6.0 -7.0 -8.0 .. testcleanup:: newton-refine3 import make_images curve1 = bezier.Curve(nodes1, degree=2) curve2 = bezier.Curve(nodes2, degree=1) make_images.newton_refine3(s_vals, curve1, curve2) Unfortunately, the process terminates with an error that is not close to machine precision :math:`\varepsilon` when :math:`\Delta s = \Delta t = 0`. .. testsetup:: newton-refine3-continued import numpy as np import bezier from bezier._intersection_helpers import newton_refine nodes1 = np.asfortranarray([ [0.0, 0.5, 1.0], [0.0, 1.0, 0.0], ]) nodes2 = np.asfortranarray([ [0.0, 1.0], [0.5, 0.5], ]) .. doctest:: newton-refine3-continued -27.0 True -28.0 True Due to round-off near the point of tangency, the final error resembles :math:`\sqrt{\varepsilon}` rather than machine precision as expected. .. note:: The following is not implemented in this function. It's just an exploration on how the shortcomings might be addressed. However, this can be overcome. At the point of tangency, we want :math:`B_1'(s) \parallel B_2'(t)`. This can be checked numerically via .. math:: B_1'(s) \times B_2'(t) = 0. For the last example (the one that converges linearly), this is .. math:: 0 = \left[\begin{array}{c} 1 \\ 2 - 4s \end{array}\right] \times \left[\begin{array}{c} 1 \\ 0 \end{array}\right] = 4 s - 2. With this, we can modify Newton's method to find a zero of the over-determined system .. math:: G(s, t) = \left[\begin{array}{c} B_0(s) - B_1(t) \\ B_1'(s) \times B_2'(t) \end{array}\right] = \left[\begin{array}{c} s - t \\ 2 s (1 - s) - \frac{1}{2} \\ 4 s - 2\end{array}\right]. Since :math:`DG` is :math:`3 \times 2`, we can't invert it. However, we can find a least-squares solution: .. math:: \left(DG^T DG\right) \left[\begin{array}{c} \Delta s \\ \Delta t \end{array}\right] = -DG^T G. This only works if :math:`DG` has full rank. In this case, it does since the submatrix containing the first and last rows has rank two: .. math:: DG = \left[\begin{array}{c c} 1 & -1 \\ 2 - 4 s & 0 \\ 4 & 0 \end{array}\right]. Though this avoids a singular system, the normal equations have a condition number that is the square of the condition number of the matrix. Starting from :math:`s = t = \frac{3}{8}` as above: .. testsetup:: newton-refine4 import numpy as np from bezier import _helpers def modified_update(s, t):
 minus_G = np.asfortranarray([ [t - s], [0.5 - 2.0 * s * (1.0 - s)], [2.0 - 4.0 * s], ]) DG = np.asfortranarray([ [1.0, -1.0], [2.0 - 4.0 * s, 0.0], [4.0, 0.0], ]) DG_t = np.asfortranarray(DG.T) LHS = _helpers.matrix_product(DG_t, DG) RHS = _helpers.matrix_product(DG_t, minus_G) delta_params = np.linalg.solve(LHS, RHS) delta_s, delta_t = delta_params.flatten() return s + delta_s, t + delta_t .. doctest:: newton-refine4 -3.0 True 519.0 True True Args: s (float):
 Parameter of a near-intersection along the first curve. nodes1 (numpy.ndarray):
 Nodes of first curve forming intersection. t (float):
 Parameter of a near-intersection along the second curve. nodes2 (numpy.ndarray):
 Nodes of second curve forming intersection. Returns: Tuple[float, float]: The refined parameters from a single Newton step. Raises: ValueError: If the Jacobian is singular at ``(s, t)``. """

    # NOTE: We form -F(s, t) since we want to solve -DF^{-1} F(s, t).
    func_val = _curve_helpers.evaluate_multi(
        nodes2, np.asfortranarray([t])
    ) - _curve_helpers.evaluate_multi(nodes1, np.asfortranarray([s]))
    if np.all(func_val == 0.0):
        # No refinement is needed.
        return s, t

    # NOTE: This assumes the curves are 2D.
    jac_mat = np.empty((2, 2), order="F")
    jac_mat[:, :1] = _curve_helpers.evaluate_hodograph(s, nodes1)
    jac_mat[:, 1:] = -_curve_helpers.evaluate_hodograph(t, nodes2)
    # Solve the system.
    singular, delta_s, delta_t = _helpers.solve2x2(jac_mat, func_val[:, 0])
    if singular:
        raise ValueError("Jacobian is singular.")

    else:
        return s + delta_s, t + delta_t