def scalar_search_wolfe2(phi, derphi=None, phi0=None,<EOL>old_phi0=None, derphi0=None,<EOL>c1=<NUM_LIT>, c2=<NUM_LIT>, amax=<NUM_LIT:50>):
if phi0 is None:<EOL><INDENT>phi0 = phi(<NUM_LIT:0.>)<EOL><DEDENT>if derphi0 is None and derphi is not None:<EOL><INDENT>derphi0 = derphi(<NUM_LIT:0.>)<EOL><DEDENT>alpha0 = <NUM_LIT:0><EOL>if old_phi0 is not None and derphi0 != <NUM_LIT:0>:<EOL><INDENT>alpha1 = min(<NUM_LIT:1.0>, <NUM_LIT>*<NUM_LIT:2>*(phi0 - old_phi0)/derphi0)<EOL><DEDENT>else:<EOL><INDENT>alpha1 = <NUM_LIT:1.0><EOL><DEDENT>if alpha1 < <NUM_LIT:0>:<EOL><INDENT>alpha1 = <NUM_LIT:1.0><EOL><DEDENT>if alpha1 == <NUM_LIT:0>:<EOL><INDENT>alpha_star = None<EOL>phi_star = phi0<EOL>phi0 = old_phi0<EOL>derphi_star = None<EOL><DEDENT>phi_a1 = phi(alpha1)<EOL>phi_a0 = phi0<EOL>derphi_a0 = derphi0<EOL>i = <NUM_LIT:1><EOL>maxiter = <NUM_LIT:10><EOL>for i in xrange(maxiter):<EOL><INDENT>if alpha1 == <NUM_LIT:0>:<EOL><INDENT>break<EOL><DEDENT>if (phi_a1 > phi0 + c1 * alpha1 * derphi0) or((phi_a1 >= phi_a0) and (i > <NUM_LIT:1>)):<EOL><INDENT>alpha_star, phi_star, derphi_star =_zoom(alpha0, alpha1, phi_a0,<EOL>phi_a1, derphi_a0, phi, derphi,<EOL>phi0, derphi0, c1, c2)<EOL>break<EOL><DEDENT>derphi_a1 = derphi(alpha1)<EOL>if (abs(derphi_a1) <= -c2*derphi0):<EOL><INDENT>alpha_star = alpha1<EOL>phi_star = phi_a1<EOL>derphi_star = derphi_a1<EOL>break<EOL><DEDENT>if (derphi_a1 >= <NUM_LIT:0>):<EOL><INDENT>alpha_star, phi_star, derphi_star =_zoom(alpha1, alpha0, phi_a1,<EOL>phi_a0, derphi_a1, phi, derphi,<EOL>phi0, derphi0, c1, c2)<EOL>break<EOL><DEDENT>alpha2 = <NUM_LIT:2> * alpha1   <EOL>i = i + <NUM_LIT:1><EOL>alpha0 = alpha1<EOL>alpha1 = alpha2<EOL>phi_a0 = phi_a1<EOL>phi_a1 = phi(alpha1)<EOL>derphi_a0 = derphi_a1<EOL><DEDENT>else:<EOL><INDENT>alpha_star = alpha1<EOL>phi_star = phi_a1<EOL>derphi_star = None<EOL><DEDENT>return alpha_star, phi_star, phi0, derphi_star<EOL>
Find alpha that satisfies strong Wolfe conditions.

    alpha > 0 is assumed to be a descent direction.

    Parameters
    ----------
    phi : callable f(x,*args)
        Objective scalar function.

    derphi : callable f'(x,*args), optional
        Objective function derivative (can be None)
    phi0 : float, optional
        Value of phi at s=0
    old_phi0 : float, optional
        Value of phi at previous point
    derphi0 : float, optional
        Value of derphi at s=0
    args : tuple
        Additional arguments passed to objective function.
    c1 : float
        Parameter for Armijo condition rule.
    c2 : float
        Parameter for curvature condition rule.

    Returns
    -------
    alpha_star : float
        Best alpha
    phi_star
        phi at alpha_star
    phi0
        phi at 0
    derphi_star
        derphi at alpha_star

    Notes
    -----
    Uses the line search algorithm to enforce strong Wolfe
    conditions.  See Wright and Nocedal, 'Numerical Optimization',
    1999, pg. 59-60.

    For the zoom phase it uses an algorithm by [...].