ó
    EñiPj  ã                   óÞ   • S r SSKJr  SSKJr  SSKr/ SQr " S S\5      r	S	 r
    SS
 jr   SS jr\r   SS jr    SS jrS rS rS rSS jrSS jrSS jr SS jr  SS jrg)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                   ó   • \ rS rSrSrg)r   é   © N)Ú__name__Ú
__module__Ú__qualname__Ú__firstlineno__Ú__static_attributes__r   ó    ÚW/home/mande/repo/quber/.venv/lib/python3.13/site-packages/scipy/optimize/_linesearch.pyr   r      s   † Úr   r   c                 óF   • SU s=:  a  Us=:  a  S:  d  O  [        S5      eg )Nr   r   z.'c1' and 'c2' do not satisfy'0 < c1 < c2 < 1'.)Ú
ValueError)Úc1Úc2s     r   Ú_check_c1_c2r      s(   € Ø��O�R�O˜!�OÜð .ó /ð 	/ð r   c                 óÚ   ^ ^^^^^^^• Uc	  T" T/TQ76 nU/mS/mS/mUU UUU4S jnUUUUUU4S jn[         R                  " UT5      n[        XÞXVUX‰X«US9
u  nnnUTS   TS   UUTS   4$ )aÁ  
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``.

r   c                 ó<   >• TS==   S-  ss'   T" TU T-  -   /TQ76 $ ©Nr   r   r   )ÚsÚargsÚfÚfcÚpkÚxks    €€€€€r   ÚphiÚline_search_wolfe1.<locals>.phiR   s(   ø€ Ø
ˆ1‹�‰
‹Ù��a˜‘d‘Ð"˜TÒ"Ð"r   c                 óv   >• T" TU T-  -   /TQ76 TS'   TS==   S-  ss'   [         R                  " TS   T5      $ r   ©ÚnpÚdot)r   r   ÚfprimeÚgcÚgvalr"   r#   s    €€€€€€r   ÚderphiÚ"line_search_wolfe1.<locals>.derphiV   s@   ø€ Ù˜˜a ™d™Ð* TÒ*ˆˆQ‰Ø
ˆ1‹�‰
‹Ü�vŠ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 �{Ù�RÐ˜$Òˆàˆ5€DØ
ˆ€BØ
ˆ€B÷#ñ #÷#ò #ô
 �fŠf�S˜"‹o€Gä.Ø˜°Ø˜t°Tñ;Ñ€Cˆˆxð ��1‘�r˜!‘u˜d H¨d°1©gÐ5Ð5r   c
           	      óÐ   • [        XV5        Uc  U " S5      nUc  U" S5      nUb#  US:w  a  [        SSX#-
  -  U-  5      n
U
S:  a  Sn
OSn
Sn[        XXVX˜U5      nU" X¢XKS9u  pÞp/XÞU4$ )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.

ç        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 �Ôà�|Ù�2‹wˆØ�Ù˜“*ˆàÑ ¨1£Ü�S˜& $¡/Ñ2°7Ñ:Ó;ˆØ�A‹:ØˆFøàˆà€Gä�C ¨°TÓ:€FÙ"Ø 7ñÑ€Cˆtð �dˆ?Ðr   c                 ó:  ^ ^^^^^^^^^^• S/mS/mS/mS/mUU UUU4S jnUmUUUUUUU4S jmUc	  T" T/TQ76 n[         R                  " UT5      nTb  UUUUUU4S jnOSn[        UTXVXèXšXüS9
u  nnnnUc  [        S[        SS	9  OTS   nUTS   TS   UUU4$ )
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                 ó<   >• TS==   S-  ss'   T" TU T-  -   /TQ76 $ r   r   )Úalphar   r    r!   r"   r#   s    €€€€€r   r$   Úline_search_wolfe2.<locals>.phi  s(   ø€ Ø
ˆ1‹�‰
‹Ù��e˜b‘j‘Ð( 4Ò(Ð(r   c                 ó€   >• TS==   S-  ss'   T" TU T-  -   /TQ76 TS'   U TS'   [         R                  " TS   T5      $ r   r'   )rG   r   r*   r+   r,   Ú
gval_alphar"   r#   s    €€€€€€€r   r-   Ú"line_search_wolfe2.<locals>.derphi#  sI   ø€ Ø
ˆ1‹�‰
‹Ù˜˜e b™j™Ð0¨4Ò0ˆˆQ‰Øˆ
�1‰Ü�vŠv�d˜1‘g˜rÓ"Ð"r   c                 óP   >• TS   U :w  a  T" U 5        TU T-  -   nT" XUTS   5      $ )Nr   r   )	rG   r$   Úxr-   Úextra_conditionr,   rJ   r"   r#   s	      €€€€€€r   Úextra_condition2Ú,line_search_wolfe2.<locals>.extra_condition20  s6   ø€ Ø˜!‰} Ó%Ù�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/   rN   r>   r$   r5   rO   Ú
alpha_starÚphi_starÚderphi_starr-   r!   r*   r+   r,   rJ   s   ` ``   `   `       @@@@@@r   r   r   º   sÚ   ÿú€ ð| ˆ€BØ
ˆ€BØˆ6€DØ�€J÷)ñ )ð €F÷#ó #ð �{Ù�RÐ˜$ÒˆÜ�fŠf�S˜"‹o€GàÑ"÷	;ó 	;ð  Ðä2FØ�˜°¸bØñ3/Ñ/€J�˜( Kð ÑÜÐ9Ü¨1ó	.ð ˜1‘gˆà�r˜!‘u˜b ™e X¨x¸ÐDÐDr   c
                 óð  • [        XV5        Uc  U " S5      nUc  U" S5      nSn
Ub  US:w  a  [        SSX#-
  -  U-  5      nOSnUS:  a  SnUb  [        X·5      nU " U5      nUnUnUc  S n[        U	5       Hâ  nUS:X  d  Ub/  X§:”  a*  SnUnUnSnUS:X  a  SnOSS	U 3-   n[        U[        S
S9    OÀUS:„  nXÂX[-  U-  -   :”  d  XÍ:¼  a  U(       a  [        X«UXÎXX$XVU5      u  nnn  O‹U" U5      n[        U5      U* U-  ::  a  U" X¼5      (       a  UnUnUn  O[US:¼  a  [        XºUUUXX$XVU5      u  nnn  O=S
U-  nUb  [        UU5      nUn
UnUnU " U5      nUnMä     UnUnSn[        S[        S
S9  UUUU4$ )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                 ó   • g)NTr   )rG   r$   s     r   rN   Ú-scalar_search_wolfe2.<locals>.extra_conditionœ  s   € Ør   z7Rounding errors prevent the line search from convergingz4The line search algorithm could not find a solution zless than or equal to amax: rR   rS   rQ   )r   r?   Úranger   r   Ú_zoomÚabs)r$   r-   r=   r@   r5   r   r   r/   rN   r>   Úalpha0rA   Úphi_a1Úphi_a0Ú	derphi_a0ÚirV   rW   rX   ÚmsgÚnot_first_iterationÚ	derphi_a1Úalpha2s                          r   r
   r
   I  s  € ôp �Ôà�|Ù�2‹wˆà�Ù˜“*ˆà€FØÑ ¨1£Ü�S˜& $¡/Ñ2°7Ñ:Ó;‰àˆà�ƒzØˆàÑÜ�VÓ"ˆá�‹[€Fð €FØ€IàÑò	ô �7Ž^ˆØ�Q‹;˜4Ñ+°³ð ˆJØˆHØˆDØˆKà˜‹{ØO‘àLØ4°T°FÐ;ñ<�ô �Ô'°AÒ6Ùà !™eÐØ˜B™K¨'Ñ1Ñ1Ó1ØÓÖ#6ä˜f¨fØ$°Ø"¨R°_óFñ .ˆJ˜ +ñ á˜6“Nˆ	Ü�	‹N˜r˜c '™kÓ)Ù˜v×.Ñ.Ø#�
Ø!�Ø'�Ùà˜‹Nä˜f¨fØ$ i°Ø"¨R°_óFñ .ˆJ˜ +ñ à�V‘ˆØÑÜ˜ Ó&ˆFØˆØˆØˆÙ�V“ˆØŠ	ñc ðj ˆ
ØˆØˆÜÐ9Ü¨1ò	.ð �x  {Ð2Ð2r   c           
      ór  • [         R                  " SSSS9    UnX0-
  nXP-
  n	X‰-  S-  X‰-
  -  n
[         R                  " S5      nU	S-  US'   US-  * US'   U	S-  * US'   US-  US	'   [         R                  " U[         R                  " XA-
  Xx-  -
  Xa-
  Xy-  -
  /5      R                  5       5      u  pÍXÊ-  nXÚ-  nXÝ-  SU-  U-  -
  nX* [         R                  " U5      -   SU-  -  -   n S
S
S
5        [         R                  " W5      (       d  g
U$ ! [         a     S
S
S
5        g
f = f! , (       d  f       ND= f)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ÚinvalidrR   )rR   rR   )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…   Ý  sN  € ô 
�Š˜G¨'¸7Ó	Cð	ØˆAØ‘ˆBØ‘ˆBØ‘W ‘N b¡gÑ.ˆEÜ—’˜&Ó!ˆBØ˜Q‘wˆBˆt‰HØ˜a™�xˆBˆt‰HØ˜a™�xˆBˆt‰HØ˜Q‘wˆBˆt‰HÜ—V’V˜B¤§
¢
¨B©G°a±fÑ,<Ø,.©G°a±fÑ,<ð,>ó !?ß?F¹w»yóJ‰FˆQà‰JˆAØ‰JˆAØ‘e˜a !™e a™iÑ'ˆGØ˜œRŸWšW WÓ-Ñ-°!°a±%Ñ8Ñ8‰D÷! 
Dô& �;Š;�t×ÑØØ€Køô	 ó 	Ø÷% 
DÐ	Cð"	ú÷# 
DÕ	Cús)   —D(™CDÄ
D%ÄD(Ä$D%Ä%D(Ä(
D6c                 ó  • [         R                  " SSSS9    UnUnX0S-  -
  nXE-
  Xg-  -
  Xw-  -  nXSU-  -  -
  n	 SSS5        [         R                  " W	5      (       d  gU	$ ! [         a     SSS5        gf = f! , (       d  f       ND= f)zz
Finds the minimizer for a quadratic polynomial that goes through
the points (a,fa), (b,fb) with derivative at a of fpa.

ri   rj   r:   ç       @N)r(   ro   rt   ru   )
rv   rw   rx   ry   rz   ÚDr|   r}   r‚   r„   s
             r   Ú_quadminr‰   ÿ  s˜   € ô 
�Š˜G¨'¸7Ó	Cð	ØˆAØˆAØ˜‘W‘ˆBØ‘˜!™&‘ R¡WÑ-ˆAØ˜C !™G‘}Ñ$‰D÷ 
Dô �;Š;�t×ÑØØ€Køô	 ó 	Ø÷ 
DÐ	Cð	ú÷ 
DÕ	Cús(   —A;™#A#Á#
A8Á-A;Á7A8Á8A;Á;
B	c           	      ó  • SnSnSnSnUnSn X-
  nUS:  a  XnnOXnnUS:”  a  UU-  n[        XXAUUU5      nUS:X  d  Wb  UUW-
  :”  d	  UUU-   :  a/  UU-  n[        XXAU5      nUb  UUU-
  :”  d	  UUU-   :  a  U SU-  -   nU" U5      nUXyU-  U-  -   :”  d  UU:¼  a	  UnUnUnUnONU" U5      n[        U5      U
* U-  ::  a  U" UU5      (       a  UnUnUnO1UX-
  -  S:¼  a	  UnUnU nUnOUnU nUn UnUnUS-  nXÜ:”  a  SnSnSnOMû  UUU4$ )zõ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.

é
   r   gš™™™™™É?çš™™™™™¹?Nç      à?r   )r…   r‰   r^   )Úa_loÚa_hiÚphi_loÚphi_hiÚ	derphi_lor$   r-   r=   r5   r   r   rN   r>   rc   Údelta1Údelta2Úphi_recÚa_recÚdalpharv   ry   ÚcchkÚa_jÚqchkÚphi_ajÚ	derphi_ajÚa_starÚval_starÚvalprime_stars                                r   r]   r]     s­  € ð €GØ	€AØ€FØ€FØ€GØ€EØ
ð ‘ˆØ�A‹:ØˆqˆAˆqàˆqˆAð �‹EØ˜F‘?ˆDÜ˜D¨)¸6Ø! 7ó,ˆCà�‹F˜™¨¨q°4©x«¸SÀ1ÀtÁ8»^Ø˜F‘?ˆDÜ˜4¨¸&ÓAˆCØ‘  q¨¡v£°3¸¸4¹³<Ø˜S ™ZÑ'�ñ �S“ˆØ�T˜s™F 7™NÑ*Ó*°¸&Ó0@ØˆGØˆEØˆDØ‰Fá˜s›ˆIÜ�9‹~ "  W¡Ó,±ÀÀf×1MÑ1MØ�Ø!�Ø )�ØØ˜$™+Ñ&¨!Ó+Ø �Ø�Ø�Ø‘à �Ø�ØˆDØˆFØ!ˆIØ	ˆQ‰ˆØ‹KàˆFØˆHØ ˆMØñA ðB �8˜]Ð*Ð*r   c                 óÆ   ^ ^^^^• [         R                  " T5      mS/mUU UUU4S jnUc	  U" S5      n	OUn	[         R                  " UT5      n
[        X‰X¦US9u  p¼UTS   U4$ )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                 ó<   >• TS==   S-  ss'   T" TU T-  -   /TQ76 $ r   r   )rA   r   r    r!   r"   r#   s    €€€€€r   r$   Úline_search_armijo.<locals>.phi”  s(   ø€ Ø
ˆ1‹�‰
‹Ù��f˜R‘i‘Ð' $Ò'Ð'r   r9   )r   r_   )r(   Ú
atleast_1dr)   Úscalar_search_armijo)r    r#   r"   r2   r3   r   r   r_   r$   r=   r5   rG   rC   r!   s   ```  `       @r   r   r   o  sn   ü€ ôD 
�Š�rÓ	€BØ
ˆ€B÷(ñ (ð ÑÙ�2‹w‰àˆä�fŠf�S˜"‹o€GÜ& s°'Ø.4ñ6�K€Eà�"�Q‘%˜ÐÐr   c                 ó:   • [        XX#XEUUS9nUS   US   SUS   4$ )z0
Compatibility wrapper for `line_search_armijo`
)r   r   r_   r   r   rR   )r   )	r    r#   r"   r2   r3   r   r   r_   Úrs	            r   Úline_search_BFGSr§   £  s4   € ô 	˜1 "¨8À2Ø"(ñ	*€AàˆQ‰4��1‘�q˜!˜A™$ÐÐr   c                 ó<  • U " U5      nXaX4-  U-  -   ::  a  XF4$ U* US-  -  S-  Xa-
  X$-  -
  -  nU " U5      nX�X7-  U-  -   ::  a  Xx4$ Xu:”  aÎ  US-  US-  -  Xt-
  -  n	US-  X�-
  X'-  -
  -  US-  Xa-
  X$-  -
  -  -
  n
X©-  n
US-  * X�-
  X'-  -
  -  US-  Xa-
  X$-  -
  -  -   nX¹-  nU* [         R                  " [        US-  SU
-  U-  -
  5      5      -   SU
-  -  nU " U5      nXÑX<-  U-  -   ::  a  XÍ4$ X|-
  US-  :”  d  SXÇ-  -
  S:  a  US-  nUnUnUnUnXu:”  a  MÎ  SU4$ )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

rR   r‡   rn   g      @r   g¸…ëQ¸î?N)r(   rs   r^   )r$   r=   r5   r   r_   r0   ra   rA   r`   Úfactorrv   ry   rg   Úphi_a2s                 r   r¤   r¤   ¬  s¥  € ñ �‹[€FØ˜™	 'Ñ)Ñ)Ó)Øˆ~Ðð ˆZ˜& !™)Ñ# cÑ)¨V©]¸WÑ=MÑ-MÑN€FÙ�‹[€Fà˜™ 7Ñ*Ñ*Ó*Øˆ~Ðð ‹-Ø˜‘˜V Q™YÑ&¨&©-Ñ8ˆØ�A‰I˜™¨©Ñ7Ñ8Ø�A‰I˜™¨©Ñ7Ñ8ñ9ˆà‰JˆØ�Q‰YˆJ˜&™-¨'©.Ñ8Ñ9Ø�A‰I˜™¨©Ñ7Ñ8ñ9ˆà‰Jˆà�"”r—w’wœs 1 a¡4¨!¨a©%°'©/Ñ#9Ó:Ó;Ñ;ÀÀAÁÑFˆÙ�V“ˆà˜R™Y wÑ.Ñ.Ó.Ø�>Ð!à‰O˜v¨™|Ó+°°F±MÑ0AÀTÓ/IØ˜c‘\ˆFàˆØˆØˆØˆð+ �-ð0 �ˆ<Ðr   c                 ó¦  • US   n[        U5      n	Sn
SnSn XU-  -   nU " U5      u  pïXéU-   XZS-  -  U-  -
  ::  a  U
nO�U
S-  U-  USU
-  S-
  U-  -   -  nXU-  -
  nU " U5      u  pïXéU-   X[S-  -  U-  -
  ::  a  U* nOQUS-  U-  USU-  S-
  U-  -   -  n[        R                  " UXj-  Xz-  5      n
[        R                  " UXk-  X{-  5      nM·  XÍXï4$ )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   rR   )Úmaxr(   Úclip)r    Úx_kÚdÚprev_fsÚetaÚgammaÚtau_minÚtau_maxÚf_kÚf_barÚalpha_pÚalpha_mrG   ÚxpÚfpÚFpÚalpha_tpÚalpha_tms                     r   Ú_nonmonotone_line_search_cruzr¿   ê  s+  € ðP �"‰+€CÜ�‹L€Eà€GØ€GØ€Eà
Ø˜Q‘;ÑˆÙ�2“‰ˆà˜‘˜u°¡zÑ1°CÑ7Ñ7Ó7ØˆEØà˜A‘: Ñ# r¨Q¨w©Y¸©]¸CÑ,?Ñ'?Ñ@ˆà˜Q‘;ÑˆÙ�2“‰ˆà˜‘˜u°¡zÑ1°CÑ7Ñ7Ó7Ø�HˆEØà˜A‘: Ñ# r¨Q¨w©Y¸©]¸CÑ,?Ñ'?Ñ@ˆä—'’'˜( GÑ$5°wÑ7HÓIˆÜ—'’'˜( GÑ$5°wÑ7HÓIˆñ) ð, �bÐÐr   c                 ó¾  • SnSnSn XU-  -   nU " U5      u  nnXôU-   X{S-  -  U-  -
  ::  a  UnO‘US-  U-  USU-  S-
  U-  -   -  nXU-  -
  nU " U5      u  nnXôU-   X|S-  -  U-  -
  ::  a  U* nOQUS-  U-  USU-  S-
  U-  -   -  n[         R                  " UX‹-  X›-  5      n[         R                  " UXŒ-  Xœ-  5      nM¹  X¥-  S-   nX¥-  XF-   -  U-   U-  nUnXÞUUXE4$ )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   rR   )r(   r®   )r    r¯   r°   r¶   r|   ÚQr²   r³   r´   rµ   Únur¸   r¹   rG   rº   r»   r¼   r½   r¾   ÚQ_nexts                       r   Ú_nonmonotone_line_search_chengrÄ   2  sI  € ð^ €GØ€GØ€Eà
Ø˜Q‘;ÑˆÙ�2“‰ˆˆBà�S‘˜5¨A¡:Ñ-°Ñ3Ñ3Ó3ØˆEØà˜A‘: Ñ# r¨Q¨w©Y¸©]¸CÑ,?Ñ'?Ñ@ˆà˜Q‘;ÑˆÙ�2“‰ˆˆBà�S‘˜5¨A¡:Ñ-°Ñ3Ñ3Ó3Ø�HˆEØà˜A‘: Ñ# r¨Q¨w©Y¸©]¸CÑ,?Ñ'?Ñ@ˆä—'’'˜( GÑ$5°wÑ7HÓIˆÜ—'’'˜( GÑ$5°wÑ7HÓIˆñ) ð. ‰V�a‰Z€FØ	‰�1‘7Ñ	˜bÑ	  FÑ*€AØ€Aà�b˜"˜aÐ"Ð"r   )	NNNr   ç-Cëâ6?çÍÌÌÌÌÌì?é2   ç:Œ0âŽyE>ç›+¡†›„=)NNNrÅ   rÆ   rÇ   rÈ   rÉ   )	NNNr   rÅ   rÆ   NNr‹   )NNNrÅ   rÆ   NNr‹   )r   rÅ   r   )rÅ   r   r   )rÅ   rŒ   r�   )rÅ   rŒ   r�   g333333ë?)Ú__doc__Úwarningsr   Ú_dcsrchr   Únumpyr(   Ú__all__ÚRuntimeWarningr   r   r   r	   Úline_searchr   r
   r…   r‰   r]   r   r§   r¤   r¿   rÄ   r   r   r   Ú<module>rÑ      sÇ   ðñõ å Û ò!€ô	˜ô 	ò/ð /3Ø37Ø?CØ!ô<6ð~ IMØ%(Ø27ôJðZ !€ð @DØIMØ57ôLEð^ ,0Ø04Ø/3Ø79ôQ3òhòDò*T+ôv1ôhô7ð~ DGôEðR EHØ&*õN#r   