§
    fŠtjPj  ã                   óÔ   — d Z ddlmZ ddlmZ ddlZg d¢Z G d„ de¦  «        Z	d	„ Z
	 	 	 	 d d„Z	 	 	 d!d„ZeZ	 	 	 d"d„Z	 	 	 	 d#d„Zd„ Zd„ Zd„ Zd$d„Zd$d„Zd%d„Z	 d&d„Z	 	 d'd„ZdS )(z·
Functions
---------
.. autosummary::
   :toctree: generated/

    line_search_armijo
    line_search_wolfe1
    line_search_wolfe2
    scalar_search_wolfe1
    scalar_search_wolfe2

é    )Úwarné   )ÚDCSRCHN)ÚLineSearchWarningÚline_search_wolfe1Úline_search_wolfe2Úscalar_search_wolfe1Úscalar_search_wolfe2Úline_search_armijoc                   ó   — e Zd ZdS )r   N)Ú__name__Ú
__module__Ú__qualname__© ó    úX/var/www/html/CA-Chatbot/venv/lib/python3.11/site-packages/scipy/optimize/_linesearch.pyr   r      s   € € € € € Ø€Dr   r   c                 óP   — d| cxk     r|cxk     rdk     sn t          d¦  «        ‚d S )Nr   r   z.'c1' and 'c2' do not satisfy'0 < c1 < c2 < 1'.)Ú
ValueError)Úc1Úc2s     r   Ú_check_c1_c2r      sA   € Ø�ˆOˆOŠOˆO�RˆOˆOŠOˆO˜!ŠOˆOˆOˆOÝð .ñ /ô /ð 	/ð ˆOr   r   ç-Cëâ6?çÍÌÌÌÌÌì?é2   ç:Œ0âŽyE>ç›+¡†›„=c                 óø   ‡ ‡‡‡‡‡‡‡— |€	 ‰‰g‰¢R Ž }|gŠdgŠdgŠˆˆ ˆˆˆfd„}ˆˆˆˆˆˆfd„}t          j        |‰¦  «        }t          |||||||	|
||¬¦
  «
        \  }}}|‰d         ‰d         ||‰d         fS )a1  
    As `scalar_search_wolfe1` but do a line search to direction `pk`

    Parameters
    ----------
    f : callable
        Function `f(x)`
    fprime : callable
        Gradient of `f`
    xk : array_like
        Current point
    pk : array_like
        Search direction
    gfk : array_like, optional
        Gradient of `f` at point `xk`
    old_fval : float, optional
        Value of `f` at point `xk`
    old_old_fval : float, optional
        Value of `f` at point preceding `xk`

    The rest of the parameters are the same as for `scalar_search_wolfe1`.

    Returns
    -------
    stp, f_count, g_count, fval, old_fval
        As in `line_search_wolfe1`
    gval : array
        Gradient of `f` at the final point

    Notes
    -----
    Parameters `c1` and `c2` must satisfy ``0 < c1 < c2 < 1``.

    Nr   c                 óB   •— ‰dxx         dz  cc<    ‰‰| ‰z  z   g‰¢R Ž S ©Nr   r   r   )ÚsÚargsÚfÚfcÚpkÚxks    €€€€€r   Úphizline_search_wolfe1.<locals>.phiR   s8   ø€ Ø
ˆ1ˆˆŒ�‰
ˆˆ‰Øˆq��a˜‘d‘Ð"˜TÐ"Ð"Ð"Ð"r   c                 ó~   •—  ‰‰| ‰z  z   g‰¢R Ž ‰d<   ‰dxx         dz  cc<   t          j        ‰d         ‰¦  «        S r   ©ÚnpÚdot)r    r!   ÚfprimeÚgcÚgvalr$   r%   s    €€€€€€r   Úderphiz"line_search_wolfe1.<locals>.derphiV   sQ   ø€ Ø�&˜˜a ™d™Ð* TÐ*Ð*Ð*ˆˆQ‰Ø
ˆ1ˆˆŒ�‰
ˆˆ‰ÝŒv�d˜1”g˜rÑ"Ô"Ð"r   )r   r   ÚamaxÚaminÚxtol)r)   r*   r	   )r"   r+   r%   r$   ÚgfkÚold_fvalÚold_old_fvalr!   r   r   r/   r0   r1   r&   r.   Úderphi0ÚstpÚfvalr#   r,   r-   s   ````   `          @@@r   r   r   %   s   øøøøøøøø€ ðL €{Øˆf�RÐ˜$ÐÐÐˆàˆ5€DØ
ˆ€BØ
ˆ€Bð#ð #ð #ð #ð #ð #ð #ð #ð #ð#ð #ð #ð #ð #ð #ð #ð #ð #ð #õ
 Œf�S˜"‰oŒo€Gå.Ø�˜ <°Ø�b˜t¨$°Tð;ñ ;ô ;Ñ€Cˆˆxð ��1”�r˜!”u˜d H¨d°1¬gÐ5Ð5r   c
           	      ó  — t          ||¦  «         |€ | d¦  «        }|€ |d¦  «        }|�(|dk    r"t          dd||z
  z  |z  ¦  «        }
|
dk     rd}
nd}
d}t          | ||||	||¦  «        } ||
|||¬¦  «        \  }}}}|||fS )a   
    Scalar function search for alpha that satisfies strong Wolfe conditions

    alpha > 0 is assumed to be a descent direction.

    Parameters
    ----------
    phi : callable phi(alpha)
        Function at point `alpha`
    derphi : callable phi'(alpha)
        Objective function derivative. Returns a scalar.
    phi0 : float, optional
        Value of phi at 0
    old_phi0 : float, optional
        Value of phi at previous point
    derphi0 : float, optional
        Value derphi at 0
    c1 : float, optional
        Parameter for Armijo condition rule.
    c2 : float, optional
        Parameter for curvature condition rule.
    amax, amin : float, optional
        Maximum and minimum step size
    xtol : float, optional
        Relative tolerance for an acceptable step.

    Returns
    -------
    alpha : float
        Step size, or None if no suitable step was found
    phi : float
        Value of `phi` at the new point `alpha`
    phi0 : float
        Value of `phi` at `alpha=0`

    Notes
    -----
    Uses routine DCSRCH from MINPACK.
    
    Parameters `c1` and `c2` must satisfy ``0 < c1 < c2 < 1`` as described in [1]_.

    References
    ----------
    
    .. [1] Nocedal, J., & Wright, S. J. (2006). Numerical optimization.
       In Springer Series in Operations Research and Financial Engineering.
       (Springer Series in Operations Research and Financial Engineering).
       Springer Nature.

    Nç        r   ç      ð?ç)\�Âõ( @éd   )Úphi0r5   Úmaxiter)r   Úminr   )r&   r.   r=   Úold_phi0r5   r   r   r/   r0   r1   Úalpha1r>   Údcsrchr6   Úphi1Útasks                   r   r	   r	   d   sÌ   € õj ��RÑÔÐà€|Øˆs�2‰wŒwˆØ€Ø�&˜‘*”*ˆàÐ ¨1¢ Ý�S˜& $¨¡/Ñ2°7Ñ:Ñ;Ô;ˆØ�AŠ:ˆ:ØˆFøàˆà€Gå�C˜  R¨¨t°TÑ:Ô:€FØ"˜FØ�T 7°Gðñ ô Ñ€Cˆˆt�Tð ��dˆ?Ðr   é
   c                 ób  ‡ ‡‡‡‡‡‡‡‡‡‡— dgŠdgŠdgŠdgŠˆˆ ˆˆˆfd„}|Šˆˆˆˆˆˆˆfd„Š|€	 ‰‰g‰¢R Ž }t          j        |‰¦  «        }‰�ˆˆˆˆˆˆfd„}nd}t          |‰|||||	|
||¬¦
  «
        \  }}}}|€t          dt          d¬	¦  «         n‰d         }|‰d         ‰d         |||fS )
a  Find alpha that satisfies strong Wolfe conditions.

    Parameters
    ----------
    f : callable f(x,*args)
        Objective function.
    myfprime : callable f'(x,*args)
        Objective function gradient.
    xk : ndarray
        Starting point.
    pk : ndarray
        Search direction. The search direction must be a descent direction
        for the algorithm to converge.
    gfk : ndarray, optional
        Gradient value for x=xk (xk being the current parameter
        estimate). Will be recomputed if omitted.
    old_fval : float, optional
        Function value for x=xk. Will be recomputed if omitted.
    old_old_fval : float, optional
        Function value for the point preceding x=xk.
    args : tuple, optional
        Additional arguments passed to objective function.
    c1 : float, optional
        Parameter for Armijo condition rule.
    c2 : float, optional
        Parameter for curvature condition rule.
    amax : float, optional
        Maximum step size
    extra_condition : callable, optional
        A callable of the form ``extra_condition(alpha, x, f, g)``
        returning a boolean. Arguments are the proposed step ``alpha``
        and the corresponding ``x``, ``f`` and ``g`` values. The line search
        accepts the value of ``alpha`` only if this
        callable returns ``True``. If the callable returns ``False``
        for the step length, the algorithm will continue with
        new iterates. The callable is only called for iterates
        satisfying the strong Wolfe conditions.
    maxiter : int, optional
        Maximum number of iterations to perform.

    Returns
    -------
    alpha : float or None
        Alpha for which ``x_new = x0 + alpha * pk``,
        or None if the line search algorithm did not converge.
    fc : int
        Number of function evaluations made.
    gc : int
        Number of gradient evaluations made.
    new_fval : float or None
        New function value ``f(x_new)=f(x0+alpha*pk)``,
        or None if the line search algorithm did not converge.
    old_fval : float
        Old function value ``f(x0)``.
    new_slope : float or None
        The local slope along the search direction at the
        new value ``<myfprime(x_new), pk>``,
        or None if the line search algorithm did not converge.


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

    The search direction `pk` must be a descent direction (e.g.
    ``-myfprime(xk)``) to find a step length that satisfies the strong Wolfe
    conditions. If the search direction is not a descent direction (e.g.
    ``myfprime(xk)``), then `alpha`, `new_fval`, and `new_slope` will be None.

    Examples
    --------
    >>> import numpy as np
    >>> from scipy.optimize import line_search

    An objective function and its gradient are defined.

    >>> def obj_func(x):
    ...     return (x[0])**2+(x[1])**2
    >>> def obj_grad(x):
    ...     return [2*x[0], 2*x[1]]

    We can find alpha that satisfies strong Wolfe conditions.

    >>> start_point = np.array([1.8, 1.7])
    >>> search_gradient = np.array([-1.0, -1.0])
    >>> line_search(obj_func, obj_grad, start_point, search_gradient)
    (1.0, 2, 1, 1.1300000000000001, 6.13, [1.6, 1.4])

    r   Nc                 óB   •— ‰dxx         dz  cc<    ‰‰| ‰z  z   g‰¢R Ž S r   r   )Úalphar!   r"   r#   r$   r%   s    €€€€€r   r&   zline_search_wolfe2.<locals>.phi  s8   ø€ Ø
ˆ1ˆˆŒ�‰
ˆˆ‰Øˆq��e˜b‘j‘Ð( 4Ð(Ð(Ð(Ð(r   c                 óˆ   •— ‰dxx         dz  cc<    ‰‰| ‰z  z   g‰¢R Ž ‰d<   | ‰d<   t          j        ‰d         ‰¦  «        S r   r(   )rH   r!   r+   r,   r-   Ú
gval_alphar$   r%   s    €€€€€€€r   r.   z"line_search_wolfe2.<locals>.derphi#  sZ   ø€ Ø
ˆ1ˆˆŒ�‰
ˆˆ‰Ø�&˜˜e b™j™Ð0¨4Ð0Ð0Ð0ˆˆQ‰Øˆ
�1‰ÝŒv�d˜1”g˜rÑ"Ô"Ð"r   c                 ój   •— ‰d         | k    r ‰| ¦  «         ‰| ‰z  z   } ‰| ||‰d         ¦  «        S )Nr   r   )	rH   r&   Úxr.   Úextra_conditionr-   rJ   r$   r%   s	      €€€€€€r   Úextra_condition2z,line_search_wolfe2.<locals>.extra_condition20  sF   ø€ Ø˜!Œ} Ò%Ð%Ø��u‘”�Ø�U˜R‘Z‘ˆAØ"�? 5¨!¨S°$°q´'Ñ:Ô:Ð:r   )r>   ú*The line search algorithm did not convergeé   ©Ú
stacklevel)r)   r*   r
   r   r   )r"   Úmyfprimer%   r$   r2   r3   r4   r!   r   r   r/   rM   r>   r&   r5   rN   Ú
alpha_starÚphi_starÚderphi_starr.   r#   r+   r,   r-   rJ   s   ` ``   `   `       @@@@@@r   r   r   º   sŽ  øøøøøøøøøøø€ ð| ˆ€BØ
ˆ€BØˆ6€DØ�€Jð)ð )ð )ð )ð )ð )ð )ð )ð )ð €Fð#ð #ð #ð #ð #ð #ð #ð #ð #ð #ð #ð €{Øˆf�RÐ˜$ÐÐÐˆÝŒf�S˜"‰oŒo€GàÐ"ð	;ð 	;ð 	;ð 	;ð 	;ð 	;ð 	;ð 	;ð 	;ð 	;ð 	;ð  Ðå2FØ�˜ <°¸"¸bÀ$Ø gð3/ñ 3/ô 3/Ñ/€J�˜( Kð ÐÝÐ9Ý¨1ð	.ñ 	.ô 	.ð 	.ð 	.ð ˜1”gˆà�r˜!”u˜b œe X¨x¸ÐDÐDr   c
                 ór  — t          ||¦  «         |€ | d¦  «        }|€ |d¦  «        }d}
|� |dk    rt          dd||z
  z  |z  ¦  «        }nd}|dk     rd}|�t          ||¦  «        } | |¦  «        }|}|}|€d„ }t          |	¦  «        D �]}|dk    s|�8|
|k    r2d}|}|}d}|dk    rd}ndd	|› �z   }t          |t          d
¬¦  «          nâ|dk    }||||z  |z  z   k    s||k    r"|r t          |
||||| ||||||¦  «        \  }}} n¥ ||¦  «        }t          |¦  «        | |z  k    r |||¦  «        r|}|}|} no|dk    r t          ||
|||| ||||||¦  «        \  }}} nId
|z  }|�t          ||¦  «        }|}
|}|} | |¦  «        }|}�Œ|}|}d}t          dt          d
¬¦  «         ||||fS )a­  Find alpha that satisfies strong Wolfe conditions.

    alpha > 0 is assumed to be a descent direction.

    Parameters
    ----------
    phi : callable phi(alpha)
        Objective scalar function.
    derphi : callable phi'(alpha)
        Objective function derivative. Returns a scalar.
    phi0 : float, optional
        Value of phi at 0.
    old_phi0 : float, optional
        Value of phi at previous point.
    derphi0 : float, optional
        Value of derphi at 0
    c1 : float, optional
        Parameter for Armijo condition rule.
    c2 : float, optional
        Parameter for curvature condition rule.
    amax : float, optional
        Maximum step size.
    extra_condition : callable, optional
        A callable of the form ``extra_condition(alpha, phi_value)``
        returning a boolean. The line search accepts the value
        of ``alpha`` only if this callable returns ``True``.
        If the callable returns ``False`` for the step length,
        the algorithm will continue with new iterates.
        The callable is only called for iterates satisfying
        the strong Wolfe conditions.
    maxiter : int, optional
        Maximum number of iterations to perform.

    Returns
    -------
    alpha_star : float or None
        Best alpha, or None if the line search algorithm did not converge.
    phi_star : float
        phi at alpha_star.
    phi0 : float
        phi at 0.
    derphi_star : float or None
        derphi at alpha_star, or None if the line search algorithm
        did not converge.

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

    Nr9   r   r:   r;   c                 ó   — dS )NTr   )rH   r&   s     r   rM   z-scalar_search_wolfe2.<locals>.extra_conditionœ  s   € Ø�4r   z7Rounding errors prevent the line search from convergingz4The line search algorithm could not find a solution zless than or equal to amax: rP   rQ   rO   )r   r?   Úranger   r   Ú_zoomÚabs)r&   r.   r=   r@   r5   r   r   r/   rM   r>   Úalpha0rA   Úphi_a1Úphi_a0Ú	derphi_a0ÚirT   rU   rV   ÚmsgÚnot_first_iterationÚ	derphi_a1Úalpha2s                          r   r
   r
   I  sÇ  € õp ��RÑÔÐà€|Øˆs�2‰wŒwˆà€Ø�&˜‘*”*ˆà€FØÐ ¨1¢ Ý�S˜& $¨¡/Ñ2°7Ñ:Ñ;Ô;ˆˆàˆà�‚z€zØˆàÐÝ�V˜TÑ"Ô"ˆàˆS�‰[Œ[€Fð €FØ€IàÐð	ð 	ð 	õ �7‰^Œ^ð 9.ñ 9.ˆØ�QŠ;ˆ;˜4Ð+°¸²°ð ˆJØˆHØˆDØˆKà˜Š{ˆ{ØO��àLØ;°TÐ;Ð;ñ<�õ �Õ'°AÐ6Ñ6Ô6Ð6ØˆEà !šeÐØ�T˜B ™K¨'Ñ1Ñ1Ò1Ð1Ø�vÒÐÐ#6Ðå˜f f¨fØ$ i°°fØ" G¨R°°_ñFô Fñ .ˆJ˜ +ð ˆEà�F˜6‘N”Nˆ	Ý�	‰NŒN˜r˜c '™kÒ)Ð)Øˆ˜v vÑ.Ô.ð Ø#�
Ø!�Ø'�Ø�à˜ŠNˆNå˜f f¨fØ$ i°°fØ" G¨R°°_ñFô Fñ .ˆJ˜ +ð ˆEà�V‘ˆØÐÝ˜ Ñ&Ô&ˆFØˆØˆØˆØ��V‘”ˆØˆ	‰	ð ˆ
ØˆØˆÝÐ9Ý¨1ð	.ñ 	.ô 	.ð 	.ð �x  {Ð2Ð2r   c           
      ó€  — t          j        ddd¬¦  «        5  	 |}|| z
  }|| z
  }	||	z  dz  ||	z
  z  }
t          j        d¦  «        }|	dz  |d<   |dz   |d<   |	dz   |d<   |dz  |d	<   t          j        |t          j        ||z
  ||z  z
  ||z
  ||	z  z
  g¦  «                             ¦   «         ¦  «        \  }}||
z  }||
z  }||z  d|z  |z  z
  }| | t          j        |¦  «        z   d|z  z  z   }n# t          $ r Y d
d
d
¦  «         d
S w xY w	 d
d
d
¦  «         n# 1 swxY w Y   t          j        |¦  «        sd
S |S )z¾
    Finds the minimizer for a cubic polynomial that goes through the
    points (a,fa), (b,fb), and (c,fc) with derivative at a of fpa.

    If no minimizer can be found, return None.

    Úraise©ÚdivideÚoverÚinvalidrP   )rP   rP   )r   r   )r   r   é   )r   r   )r   r   N)	r)   ÚerrstateÚemptyr*   ÚasarrayÚflattenÚsqrtÚArithmeticErrorÚisfinite)ÚaÚfaÚfpaÚbÚfbÚcr#   ÚCÚdbÚdcÚdenomÚd1ÚAÚBÚradicalÚxmins                   r   Ú	_cubicminr‚   Ý  sô  € õ 
Œ˜G¨'¸7Ð	CÑ	CÔ	Cð ð ð	ØˆAØ�Q‘ˆBØ�Q‘ˆBØ˜"‘W ‘N b¨2¡gÑ.ˆEÝ”˜&Ñ!Ô!ˆBØ˜Q‘wˆBˆt‰HØ˜a™�xˆBˆt‰HØ˜a™�xˆBˆt‰HØ˜Q‘wˆBˆt‰HÝ”V˜B¥¤
¨B°©G°a¸"±fÑ,<Ø,.°©G°a¸"±fÑ,<ð,>ñ !?ô !?ß?Fºw¹y¼yñJô J‰FˆQ�à�‰JˆAØ�‰JˆAØ˜!‘e˜a !™e a™iÑ'ˆGØ˜˜�RœW WÑ-Ô-Ñ-°!°a±%Ñ8Ñ8ˆDˆDøÝð 	ð 	ð 	Øð%ð ð ñ ô ð ð ð ð"	øøøð ð!ð ð ñ ô ð ð ð ð ð ð øøøð ð ð ð õ& Œ;�tÑÔð ØˆtØ€Ks5   ˜DšCC4Ã3DÃ4
DÃ>DÄDÄDÄD!Ä$D!c                 ó  — t          j        ddd¬¦  «        5  	 |}|}|| dz  z
  }||z
  ||z  z
  ||z  z  }| |d|z  z  z
  }	n# t          $ r Y ddd¦  «         dS w xY w	 ddd¦  «         n# 1 swxY w Y   t          j        |	¦  «        sdS |	S )z†
    Finds the minimizer for a quadratic polynomial that goes through
    the points (a,fa), (b,fb) with derivative at a of fpa.

    rf   rg   r:   ç       @N)r)   rl   rq   rr   )
rs   rt   ru   rv   rw   ÚDry   rz   r   r�   s
             r   Ú_quadminr†   ÿ  s#  € õ 
Œ˜G¨'¸7Ð	CÑ	CÔ	Cð ð ð	ØˆAØˆAØ�Q˜‘W‘ˆBØ�a‘˜!˜b™&‘ R¨"¡WÑ-ˆAØ�q˜C !™G‘}Ñ$ˆDˆDøÝð 	ð 	ð 	Øðð ð ñ ô ð ð ð ð	øøøð ðð ð ñ ô ð ð ð ð ð ð øøøð ð ð ð õ Œ;�tÑÔð ØˆtØ€Ks4   ˜A,š(AÁA,Á
AÁA,ÁAÁA,Á,A0Á3A0c           	      óL  — d}d}d}d}|}d}	 || z
  }|dk     r|| }}n| |}}|dk    r||z  }t          | ||||||¦  «        }|dk    s|�|||z
  k    s	|||z   k     r4||z  }t          | ||||¦  «        }|�|||z
  k    s	|||z   k     r| d|z  z   } ||¦  «        }|||	|z  |z  z   k    s||k    r	|}|}|}|}nT ||¦  «        }t          |¦  «        |
 |z  k    r |||¦  «        r|}|}|}n3||| z
  z  dk    r	|}|}| }|}n|}| }|} |}|}|dz  }||k    rd}d}d}n�Œ|||fS )	a  Zoom stage of approximate linesearch satisfying strong Wolfe conditions.

    Part of the optimization algorithm in `scalar_search_wolfe2`.

    Notes
    -----
    Implements Algorithm 3.6 (zoom) in Wright and Nocedal,
    'Numerical Optimization', 1999, pp. 61.

    rE   r   gš™™™™™É?çš™™™™™¹?TNç      à?r   )r‚   r†   r[   )Úa_loÚa_hiÚphi_loÚphi_hiÚ	derphi_lor&   r.   r=   r5   r   r   rM   r>   r`   Údelta1Údelta2Úphi_recÚa_recÚdalphars   rv   ÚcchkÚa_jÚqchkÚphi_ajÚ	derphi_ajÚa_starÚval_starÚvalprime_stars                                r   rZ   rZ     sü  € ð €GØ	€AØ€FØ€FØ€GØ€Eð@ð ˜‘ˆØ�AŠ:ˆ:Ø˜ˆqˆAˆAà˜ˆqˆAð �ŠEˆEØ˜F‘?ˆDÝ˜D &¨)°T¸6Ø! 7ñ,ô ,ˆCà�ŠFˆF˜˜¨¨q°4©xª¨¸SÀ1ÀtÁ8º^¸^Ø˜F‘?ˆDÝ˜4 ¨°D¸&ÑAÔAˆCØ�  q¨¡v¢ °3¸¸4¹²<°<Ø˜S ™ZÑ'�ð ��S‘”ˆØ�T˜B˜s™F 7™NÑ*Ò*Ð*°¸&Ò0@Ð0@ØˆGØˆEØˆDØˆFˆFà˜˜s™œˆIÝ�9‰~Œ~ "  W¡Ò,Ð,°°ÀÀfÑ1MÔ1MÐ,Ø�Ø!�Ø )�ØØ˜$ ™+Ñ&¨!Ò+Ð+Ø �Ø�Ø�Ø��à �Ø�ØˆDØˆFØ!ˆIØ	ˆQ‰ˆØ�ŠKˆKàˆFØˆHØ ˆMØñA@ðB �8˜]Ð*Ð*r   c                 óÚ   ‡ ‡‡‡‡— t          j        ‰¦  «        ŠdgŠˆˆ ˆˆˆfd„}|€ |d¦  «        }	n|}	t          j        |‰¦  «        }
t          ||	|
||¬¦  «        \  }}|‰d         |fS )a  Minimize over alpha, the function ``f(xk+alpha pk)``.

    Parameters
    ----------
    f : callable
        Function to be minimized.
    xk : array_like
        Current point.
    pk : array_like
        Search direction.
    gfk : array_like
        Gradient of `f` at point `xk`.
    old_fval : float
        Value of `f` at point `xk`.
    args : tuple, optional
        Optional arguments.
    c1 : float, optional
        Value to control stopping criterion.
    alpha0 : scalar, optional
        Value of `alpha` at start of the optimization.

    Returns
    -------
    alpha
    f_count
    f_val_at_alpha

    Notes
    -----
    Uses the interpolation algorithm (Armijo backtracking) as suggested by
    Wright and Nocedal in 'Numerical Optimization', 1999, pp. 56-57

    r   c                 óB   •— ‰dxx         dz  cc<    ‰‰| ‰z  z   g‰¢R Ž S r   r   )rA   r!   r"   r#   r$   r%   s    €€€€€r   r&   zline_search_armijo.<locals>.phi”  s8   ø€ Ø
ˆ1ˆˆŒ�‰
ˆˆ‰Øˆq��f˜R‘i‘Ð' $Ð'Ð'Ð'Ð'r   Nr9   )r   r\   )r)   Ú
atleast_1dr*   Úscalar_search_armijo)r"   r%   r$   r2   r3   r!   r   r\   r&   r=   r5   rH   rC   r#   s   ```  `       @r   r   r   o  s¬   øøøøø€ õD 
Œ�rÑ	Ô	€BØ
ˆ€Bð(ð (ð (ð (ð (ð (ð (ð (ð (ð ÐØˆs�2‰wŒwˆˆàˆåŒf�S˜"‰oŒo€GÝ& s¨D°'¸bØ.4ð6ñ 6ô 6�K€Eˆ4à�"�Q”%˜ÐÐr   c           
      ó`   — t          | |||||||¬¦  «        }|d         |d         d|d         fS )z8
    Compatibility wrapper for `line_search_armijo`
    )r!   r   r\   r   r   rP   )r   )	r"   r%   r$   r2   r3   r!   r   r\   Úrs	            r   Úline_search_BFGSr¢   £  sD   € õ 	˜1˜b " c¨8¸$À2Ø"(ð	*ñ 	*ô 	*€AàˆQŒ4��1”�q˜!˜Aœ$ÐÐr   c                 ó‚  —  | |¦  «        }||||z  |z  z   k    r||fS | |dz  z  dz  ||z
  ||z  z
  z  } | |¦  «        }||||z  |z  z   k    r||fS ||k    râ|dz  |dz  z  ||z
  z  }	|dz  ||z
  ||z  z
  z  |dz  ||z
  ||z  z
  z  z
  }
|
|	z  }
|dz   ||z
  ||z  z
  z  |dz  ||z
  ||z  z
  z  z   }||	z  }| t          j        t          |dz  d|
z  |z  z
  ¦  «        ¦  «        z   d|
z  z  } | |¦  «        }||||z  |z  z   k    r||fS ||z
  |dz  k    sd||z  z
  dk     r|dz  }|}|}|}|}||k    °âd|fS )a(  Minimize over alpha, the function ``phi(alpha)``.

    Uses the interpolation algorithm (Armijo backtracking) as suggested by
    Wright and Nocedal in 'Numerical Optimization', 1999, pp. 56-57

    alpha > 0 is assumed to be a descent direction.

    Returns
    -------
    alpha
    phi1

    rP   r„   rk   g      @r   g¸…ëQ¸î?N)r)   rp   r[   )r&   r=   r5   r   r\   r0   r^   rA   r]   Úfactorrs   rv   rd   Úphi_a2s                 r   rŸ   rŸ   ¬  sü  € ð ˆS�‰[Œ[€FØ�˜˜6™	 'Ñ)Ñ)Ò)Ð)Ø�vˆ~Ðð ˆZ˜& !™)Ñ# cÑ)¨V°d©]¸WÀvÑ=MÑ-MÑN€FØˆS�‰[Œ[€Fà�$˜˜F™ 7Ñ*Ñ*Ò*Ð*Ø�vˆ~Ðð �4Š-ˆ-Ø˜‘˜V Q™YÑ&¨&°©-Ñ8ˆØ�A‰I˜ $™¨°©Ñ7Ñ8Ø�A‰I˜ $™¨°©Ñ7Ñ8ñ9ˆà�‰JˆØ�Q‰YˆJ˜& 4™-¨'°&©.Ñ8Ñ9Ø�A‰I˜ $™¨°©Ñ7Ñ8ñ9ˆà�‰Jˆà�"•r”w�s 1 a¡4¨!¨a©%°'©/Ñ#9Ñ:Ô:Ñ;Ô;Ñ;ÀÀAÁÑFˆØ��V‘”ˆà�d˜R ™Y wÑ.Ñ.Ò.Ð.Ø˜6�>Ð!à�V‰O˜v¨™|Ò+Ð+°°F¸6±MÑ0AÀTÒ/IÐ/IØ˜c‘\ˆFàˆØˆØˆØˆð+ �4Š-ˆ-ð0 �ˆ<Ðr   rˆ   r‰   c                 óÒ  — |d         }t          |¦  «        }	d}
d}d}	 ||
|z  z   } | |¦  «        \  }}||	|z   ||
dz  z  |z  z
  k    r|
}n–|
dz  |z  |d|
z  dz
  |z  z   z  }|||z  z
  } | |¦  «        \  }}||	|z   ||dz  z  |z  z
  k    r| }nP|dz  |z  |d|z  dz
  |z  z   z  }t          j        |||
z  ||
z  ¦  «        }
t          j        |||z  ||z  ¦  «        }ŒÄ||||fS )a@  
    Nonmonotone backtracking line search as described in [1]_

    Parameters
    ----------
    f : callable
        Function returning a tuple ``(f, F)`` where ``f`` is the value
        of a merit function and ``F`` the residual.
    x_k : ndarray
        Initial position.
    d : ndarray
        Search direction.
    prev_fs : float
        List of previous merit function values. Should have ``len(prev_fs) <= M``
        where ``M`` is the nonmonotonicity window parameter.
    eta : float
        Allowed merit function increase, see [1]_
    gamma, tau_min, tau_max : float, optional
        Search parameters, see [1]_

    Returns
    -------
    alpha : float
        Step length
    xp : ndarray
        Next position
    fp : float
        Merit function value at next position
    Fp : ndarray
        Residual at next position

    References
    ----------
    [1] "Spectral residual method without gradient information for solving
        large-scale nonlinear systems of equations." W. La Cruz,
        J.M. Martinez, M. Raydan. Math. Comp. **75**, 1429 (2006).

    éÿÿÿÿr   TrP   )Úmaxr)   Úclip)r"   Úx_kÚdÚprev_fsÚetaÚgammaÚtau_minÚtau_maxÚf_kÚf_barÚalpha_pÚalpha_mrH   ÚxpÚfpÚFpÚalpha_tpÚalpha_tms                     r   Ú_nonmonotone_line_search_cruzrº   ê  s]  € ðP �"Œ+€CÝ�‰LŒL€Eà€GØ€GØ€EðJØ�7˜Q‘;ÑˆØ��2‘”‰ˆˆBà�˜‘˜u w°¡zÑ1°CÑ7Ñ7Ò7Ð7ØˆEØà˜A‘: Ñ# r¨Q¨w©Y¸©]¸CÑ,?Ñ'?Ñ@ˆà�7˜Q‘;ÑˆØ��2‘”‰ˆˆBà�˜‘˜u w°¡zÑ1°CÑ7Ñ7Ò7Ð7Ø�HˆEØà˜A‘: Ñ# r¨Q¨w©Y¸©]¸CÑ,?Ñ'?Ñ@ˆå”'˜( G¨gÑ$5°wÀÑ7HÑIÔIˆÝ”'˜( G¨gÑ$5°wÀÑ7HÑIÔIˆð)Jð, �"�b˜"ÐÐr   ç333333ë?c                 óÞ  — d}d}d}	 |||z  z   } | |¦  «        \  }}|||z   ||dz  z  |z  z
  k    r|}n–|dz  |z  |d|z  dz
  |z  z   z  }|||z  z
  } | |¦  «        \  }}|||z   ||dz  z  |z  z
  k    r| }nP|dz  |z  |d|z  dz
  |z  z   z  }t          j        |||z  |	|z  ¦  «        }t          j        |||z  |	|z  ¦  «        }ŒÄ|
|z  dz   }|
|z  ||z   z  |z   |z  }|}||||||fS )aŠ  
    Nonmonotone line search from [1]

    Parameters
    ----------
    f : callable
        Function returning a tuple ``(f, F)`` where ``f`` is the value
        of a merit function and ``F`` the residual.
    x_k : ndarray
        Initial position.
    d : ndarray
        Search direction.
    f_k : float
        Initial merit function value.
    C, Q : float
        Control parameters. On the first iteration, give values
        Q=1.0, C=f_k
    eta : float
        Allowed merit function increase, see [1]_
    nu, gamma, tau_min, tau_max : float, optional
        Search parameters, see [1]_

    Returns
    -------
    alpha : float
        Step length
    xp : ndarray
        Next position
    fp : float
        Merit function value at next position
    Fp : ndarray
        Residual at next position
    C : float
        New value for the control parameter C
    Q : float
        New value for the control parameter Q

    References
    ----------
    .. [1] W. Cheng & D.-H. Li, ''A derivative-free nonmonotone line
           search and its application to the spectral residual
           method'', IMA J. Numer. Anal. 29, 814 (2009).

    r   TrP   )r)   r©   )r"   rª   r«   r±   ry   ÚQr­   r®   r¯   r°   Únur³   r´   rH   rµ   r¶   r·   r¸   r¹   ÚQ_nexts                       r   Ú_nonmonotone_line_search_chengrÀ   2  s{  € ð^ €GØ€GØ€EðJØ�7˜Q‘;ÑˆØ��2‘”‰ˆˆBà��S‘˜5 7¨A¡:Ñ-°Ñ3Ñ3Ò3Ð3ØˆEØà˜A‘: Ñ# r¨Q¨w©Y¸©]¸CÑ,?Ñ'?Ñ@ˆà�7˜Q‘;ÑˆØ��2‘”‰ˆˆBà��S‘˜5 7¨A¡:Ñ-°Ñ3Ñ3Ò3Ð3Ø�HˆEØà˜A‘: Ñ# r¨Q¨w©Y¸©]¸CÑ,?Ñ'?Ñ@ˆå”'˜( G¨gÑ$5°wÀÑ7HÑIÔIˆÝ”'˜( G¨gÑ$5°wÀÑ7HÑIÔIˆð)Jð. �!‰V�a‰Z€FØ	ˆa‰�1�s‘7Ñ	˜bÑ	  FÑ*€AØ€Aà�"�b˜"˜a Ð"Ð"r   )	NNNr   r   r   r   r   r   )NNNr   r   r   r   r   )	NNNr   r   r   NNrE   )NNNr   r   NNrE   )r   r   r   )r   r   r   )r   rˆ   r‰   )r   rˆ   r‰   r»   )Ú__doc__Úwarningsr   Ú_dcsrchr   Únumpyr)   Ú__all__ÚRuntimeWarningr   r   r   r	   Úline_searchr   r
   r‚   r†   rZ   r   r¢   rŸ   rº   rÀ   r   r   r   ú<module>rÈ      sê  ððð ð Ð Ð Ð Ð Ð à Ð Ð Ð Ð Ð Ø Ð Ð Ð ð!ð !ð !€ð	ð 	ð 	ð 	ð 	˜ñ 	ô 	ð 	ð/ð /ð /ð /3Ø37Ø?CØ!ð<6ð <6ð <6ð <6ð~ IMØ%(Ø27ðJð Jð Jð JðZ !€ð @DØIMØ57ðLEð LEð LEð LEð^ ,0Ø04Ø/3Ø79ðQ3ð Q3ð Q3ð Q3ðhð ð ðDð ð ð*T+ð T+ð T+ðv1ð 1ð 1ð 1ðhð ð ð ð7ð 7ð 7ð 7ð~ DGðEð Eð Eð EðR EHØ&*ðN#ð N#ð N#ð N#ð N#ð N#r   