o
    wThOj                     @   s   d Z ddlmZ ddlmZ ddlZg dZG dd deZ	d	d
 Z
				d-ddZ			d.ddZeZ			d/ddZ				d0ddZdd Zdd Zdd Zd1d d!Zd1d"d#Zd2d$d%Z	'd3d(d)Z	'	*d4d+d,ZdS )5z
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                   @   s   e Zd ZdS )r   N)__name__
__module____qualname__ r   r   [/home/air/segue/gemini/back/venv/lib/python3.10/site-packages/scipy/optimize/_linesearch.pyr      s    r   c                 C   s2   d|   k r|  k rdk st d t dd S )Nr   r   z.'c1' and 'c2' do not satisfy'0 < c1 < c2 < 1'.)
ValueError)c1c2r   r   r   _check_c1_c2   s
   r   r   -C6??2   :0yE>+=c                    s   |du rg R  }|gdgdg fdd} fdd}t |}t|||||||	|
||d
\}}}|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                    (   d  d7  < |   g R  S Nr   r   r   sargsffcpkxkr   r   phiR      zline_search_wolfe1.<locals>.phic                    s<   |   g R  d< d  d7  < t d S r   npdotr   )r   fprimegcgvalr!   r"   r   r   derphiV   s   z"line_search_wolfe1.<locals>.derphi)r   r   amaxaminxtol)r&   r'   r   )r   r(   r"   r!   gfkold_fvalold_old_fvalr   r   r   r,   r-   r.   r#   r+   derphi0stpfvalr   )r   r   r    r(   r)   r*   r!   r"   r   r   %   s   &

r   c
                 C   s   t || |du r| d}|du r|d}|dur/|dkr/tdd||  | }
|
dk r.d}
nd}
d}t| ||||	||}||
|||d\}}}}|||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   )phi0r2   maxiter)r   minr   )r#   r+   r9   old_phi0r2   r   r   r,   r-   r.   alpha1r:   dcsrchr3   phi1taskr   r   r   r   d   s"   
5
r   
   c                    s   dgdgdgdg 	
fdd}| 	
fdd|du r0
g R  }t |	}durF	
fdd}nd}t||||||	|
||d	
\}}}}|du rftd
td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

    A 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                    r   r   r   alphar   r   r   r#     r$   zline_search_wolfe2.<locals>.phic                    sD   d  d7  < |   g R  d< | d< t d S r   r%   rB   )r   r(   r)   r*   
gval_alphar!   r"   r   r   r+   #  s   z"line_search_wolfe2.<locals>.derphic                    s2   d | kr
 |  |   }| ||d S )Nr   r   )rC   r#   x)r+   extra_conditionr*   rD   r!   r"   r   r   extra_condition20  s   z,line_search_wolfe2.<locals>.extra_condition2)r:   *The line search algorithm did not converge   
stacklevel)r&   r'   r	   r   r   )r   myfprimer"   r!   r/   r0   r1   r   r   r   r,   rF   r:   r#   r2   rG   
alpha_starphi_starderphi_starr   )r   r+   rF   r   r    r(   r)   r*   rD   r!   r"   r   r      s.   ^r   c
                 C   s  t || |du r| d}|du r|d}d}
|dur+|dkr+tdd||  | }nd}|dk r3d}|dur<t||}| |}|}|}|du rLdd }t|	D ]}|dks^|dur}|
|kr}d}|}|}d}|dkrmd}nd	d
|  }t|tdd  n|dk}|||| |  ks||kr|rt|
||||| ||||||\}}} nY||}t|| | kr|||r|}|}|} n?|dkrt||
|||| ||||||\}}} n'd| }|durt||}|}
|}|}| |}|}qP|}|}d}tdtd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.

    Nr5   r   r6   r7   c                 S   s   dS )NTr   )rC   r#   r   r   r   rF     s   z-scalar_search_wolfe2.<locals>.extra_conditionz7Rounding errors prevent the line search from convergingz4The line search algorithm could not find a solution zless than or equal to amax: rI   rJ   rH   )r   r;   ranger   r   _zoomabs)r#   r+   r9   r<   r2   r   r   r,   rF   r:   alpha0r=   phi_a1phi_a0	derphi_a0irM   rN   rO   msgnot_first_iteration	derphi_a1alpha2r   r   r   r	   I  s   
8




r	   c              
   C   sD  t jdddd zp|}||  }||  }	||	 d ||	  }
t d}|	d |d< |d  |d< |	d  |d< |d |d	< t |t || ||  || ||	  g \}}||
 }||
 }|| d| |  }| | t | d|   }W n ty   Y W d
   d
S w W d
   n1 sw   Y  t |sd
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invalidrI   )rI   rI   )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xminr   r   r   	_cubicmin  s:   

 
rx   c           
   	   C   s   t jdddd9 z |}|}|| d  }|| ||  ||  }| |d|   }	W n ty9   Y W d   dS w W d   n1 sDw   Y  t |	sPd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.

    r\   r]   r6          @N)r&   rb   rg   rh   )
ri   rj   rk   rl   rm   Dro   rp   ru   rw   r   r   r   _quadmin  s    
	r{   c                 C   s  d}d}d}d}|}d}	 ||  }|dk r|| }}n| |}}|dkr2|| }t | ||||||}|dksF|du sF||| ksF||| k rh|| }t| ||||}|du sb||| ksb||| k rh| d|  }||}|||	| |  ksz||kr|}|}|}|}n4||}t||
 | kr|||r|}|}|}n+|||   dkr|}|}| }|}n|}| }|} |}|}|d7 }||krd}d}d}nq|||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.

    rA   r   g?皙?TN      ?r   )rx   r{   rR   )a_loa_hiphi_lophi_hi	derphi_lor#   r+   r9   r2   r   r   rF   r:   rW   delta1delta2phi_reca_recdalphari   rl   cchka_jqchkphi_aj	derphi_aja_starval_starvalprime_starr   r   r   rQ     sf   

( 
ArQ   c                    sj   t dg fdd}|du r|d}	n|}	t |}
t||	|
||d\}}|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                    r   r   r   )r=   r   r   r   r#     r$   zline_search_armijo.<locals>.phiNr5   )r   rS   )r&   
atleast_1dr'   scalar_search_armijo)r   r"   r!   r/   r0   r   r   rS   r#   r9   r2   rC   r?   r   r   r   r
   o  s   
"


r
   c           	   
   C   s0   t | |||||||d}|d |d d|d fS )z8
    Compatibility wrapper for `line_search_armijo`
    )r   r   rS   r   r   rI   )r
   )	r   r"   r!   r/   r0   r   r   rS   rr   r   r   line_search_BFGS  s   r   c                 C   s  | |}|||| |  kr||fS | |d  d || ||   }| |}|||| |  kr5||fS ||kr|d |d  ||  }	|d || ||   |d || ||    }
|
|	 }
|d  || ||   |d || ||    }||	 }| t t|d d|
 |   d|
  }| |}|||| |  kr||fS || |d ksd||  dk r|d }|}|}|}|}||ks9d|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

    rI   ry   ra   g      @r   gQ?N)r&   rf   rR   )r#   r9   r2   r   rS   r-   rU   r=   rT   factorri   rl   r[   phi_a2r   r   r   r     s:   ", r   r|   r}   c                 C   s  |d }t |}	d}
d}d}	 ||
|  }| |\}}||	| ||
d  |  kr,|
}nU|
d | |d|
 d |   }|||  }| |\}}||	| ||d  |  krZ| }n'|d | |d| d |   }t|||
 ||
 }
t||| || }q||||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   TrI   )maxr&   clip)r   x_kdprev_fsetagammatau_mintau_maxf_kf_baralpha_palpha_mrC   xpfpFpalpha_tpalpha_tmr   r   r   _nonmonotone_line_search_cruz  s,   (  r   333333?c                 C   s*  d}d}d}	 |||  }| |\}}||| ||d  |  kr$|}nU|d | |d| d |   }|||  }| |\}}||| ||d  |  krR| }n'|d | |d| d |   }t ||| |	| }t ||| |	| }q|
| d }|
| ||  | | }|}||||||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   TrI   )r&   r   )r   r   r   r   ro   Qr   r   r   r   nur   r   rC   r   r   r   r   r   Q_nextr   r   r   _nonmonotone_line_search_cheng2  s.   /  r   )	NNNr   r   r   r   r   r   )NNNr   r   r   r   r   )	NNNr   r   r   NNrA   )NNNr   r   NNrA   )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	   rx   r{   rQ   r
   r   r   r   r   r   r   r   r   <module>   sN    

?
M	
 
 "
[
4
	?
I