
    (ph                         S r SSKrSSKrSSKJr  SSKJr  SSKJ	r	J
r
   " S S\5      r   SS	 jrSS
 jrSS jrS rS rg)z,
Spectral Algorithm for Nonlinear Equations
    N)OptimizeResult)_check_unknown_options   )_nonmonotone_line_search_cruz_nonmonotone_line_search_chengc                       \ rS rSrSrg)_NoConvergence    N)__name__
__module____qualname____firstlineno____static_attributes__r       K/var/www/html/venv/lib/python3.13/site-packages/scipy/optimize/_spectral.pyr	   r	      s    r   r	   c           
        ^*^+^, [        U5        US;  a  [        SU< S35      eSm,U
c  U*4S jn
Uc  U+U,4S jnU,4S jnS/n[        XUUXR5      u  nnnm+nnSnT+m*UnU" U5      n[        R                  " T+/U	5      nS	nT*nS
nSn U" U5      nU(       a  [        SUUU4-  5        Ub	  U" UU5        UUU-  U-   :  a  SnSnO[        U5      SU-  :  a  SU-  [        R                  " U5      -  nO[        U5      U:  a  UnU* U-  nU
" UUU5      n  US:X  a  [        UUUUU S9u  n!n"n#n$OUS:X  a  [        UUUT+UUU S9u  n!n"n#n$nnW"U-
  n%W$U-
  n&[        R                  " U%U%5      [        R                  " U%U&5      -  nU"nU$nW#m+US:X  a  UR                  U#5        US-  nGM!  [        UUUS9n'[        UU5      n([        U'UUU(US   USS9n)U)$ ! [         a     M8  f = f)a  
Solve nonlinear equation with the DF-SANE method

Options
-------
ftol : float, optional
    Relative norm tolerance.
fatol : float, optional
    Absolute norm tolerance.
    Algorithm terminates when ``||func(x)|| < fatol + ftol ||func(x_0)||``.
fnorm : callable, optional
    Norm to use in the convergence check. If None, 2-norm is used.
maxfev : int, optional
    Maximum number of function evaluations.
disp : bool, optional
    Whether to print convergence process to stdout.
eta_strategy : callable, optional
    Choice of the ``eta_k`` parameter, which gives slack for growth
    of ``||F||**2``.  Called as ``eta_k = eta_strategy(k, x, F)`` with
    `k` the iteration number, `x` the current iterate and `F` the current
    residual. Should satisfy ``eta_k > 0`` and ``sum(eta, k=0..inf) < inf``.
    Default: ``||F||**2 / (1 + k)**2``.
sigma_eps : float, optional
    The spectral coefficient is constrained to ``sigma_eps < sigma < 1/sigma_eps``.
    Default: 1e-10
sigma_0 : float, optional
    Initial spectral coefficient.
    Default: 1.0
M : int, optional
    Number of iterates to include in the nonmonotonic line search.
    Default: 10
line_search : {'cruz', 'cheng'}
    Type of line search to employ. 'cruz' is the original one defined in
    [Martinez & Raydan. Math. Comp. 75, 1429 (2006)], 'cheng' is
    a modified search defined in [Cheng & Li. IMA J. Numer. Anal. 29, 814 (2009)].
    Default: 'cruz'

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).
.. [2] W. La Cruz, Opt. Meth. Software, 29, 24 (2014).
.. [3] W. Cheng, D.-H. Li. IMA J. Numer. Anal. **29**, 814 (2009).

)chengcruzzInvalid value z for 'line_search'   c                    > TSU -   S-  -  $ )Nr   r   r   )kxFf_0s      r   eta_strategy#_root_df_sane.<locals>.eta_strategyJ   s    !a%!##r   c                    > TST-  -  $ )N      ?r   )r   f_knexps    r   fnorm_root_df_sane.<locals>.fnormO   s    T?"r   c                 H   > [         R                  R                  U 5      T-  $ N)nplinalgnorm)r   r!   s    r   fmerit_root_df_sane.<locals>.fmeritS   s    yy~~a $&&r   r   r   Fz&too many function evaluations requiredTziter %d: ||F|| = %g, sigma = %gzsuccessful convergencer   r   )etar   )shapezdf-sane)r   successmessagefunnfevnitmethod)r   
ValueError
_wrap_funccollectionsdequeprintabsr&   signr   r   r	   vdotappend_wrap_resultr   )-funcx0argsftolfatolmaxfevr"   callbackdispMr   	sigma_epssigma_0line_searchunknown_optionsr)   r0   fx_kx_shapeF_k
is_complexr   sigma_kF_0_normprev_fsQC	convergedr.   F_k_normdr+   alphaxpfpFps_ky_kr   r   resultr   r    r!   s-                                             @@@r   _root_df_saner^      s   b ?+++>+8JKLLD	$ }	#' 3D,6t7;V-K)AsGS#z 	
A
CGSzH q)G 	AAI6G
:3q(G6LLMS#dXo--.GI w<!I+%	kBGGG$44G\I%G HsN 1c3'	f$$A!S!WFI%K!r2r'*HCQRTWIJASV+X'r2r1a 3h3h''#s#bggc3&77  & NN2	Qe h 	S*G4AS*%Aa$+ !QQyJF M5  		s   G .G 
G! G!c                   ^ ^^^^^	 [         R                  " U5      nUR                  m	[         R                  " T " U/TQ76 5      R                  5       n[         R                  " U5      =(       d    [         R                  " U5      nUR                  5       nSTS'   U(       a"  UUU UUU	4S jn[        U5      n[        U5      nOUUU UUU	4S jnXT	T" U5      Xg4$ )a   
Wrap a function and an initial value so that (i) complex values
are wrapped to reals, and (ii) value for a merit function
fmerit(x, f) is computed at the same time, (iii) iteration count
is maintained and an exception is raised if it is exceeded.

Parameters
----------
func : callable
    Function to wrap
x0 : ndarray
    Initial value
fmerit : callable
    Merit function fmerit(f) for computing merit value from residual.
nfev_list : list
    List to store number of evaluations in. Should be [0] in the beginning.
maxfev : int
    Maximum number of evaluations before _NoConvergence is raised.
args : tuple
    Extra arguments to func

Returns
-------
wrap_func : callable
    Wrapped function, to be called as
    ``F, fp = wrap_func(x0)``
x0_wrap : ndarray of float
    Wrapped initial value; raveled to 1-D and complex
    values mapped to reals.
x0_shape : tuple
    Shape of the initial value array
f : float
    Merit function at F
F : ndarray of float
    Residual at x0_wrap
is_complex : bool
    Whether complex values were mapped to reals

r   r   c                    > T	S   T:  a
  [        5       eT	S==   S-  ss'   [        U 5      R                  T
5      n[        R                  " T" U/TQ76 5      R                  5       n[        U5      nT" U5      nXC4$ Nr   r   )r	   _real2complexreshaper&   asarrayravel_complex2real)r   zvr   rJ   r?   r)   r=   rB   	nfev_listx0_shapes        r   	wrap_func_wrap_func.<locals>.wrap_func   st    |v%$&&aLALa ((2A

4>D>*002Aa Aq	A4Kr   c                    > TS   T:  a
  [        5       eTS==   S-  ss'   U R                  T5      n [        R                  " T" U /TQ76 5      R	                  5       nT" U5      nX!4$ ra   )r	   rc   r&   rd   re   )	r   r   rJ   r?   r)   r=   rB   ri   rj   s	      r   rk   rl      sc    |v%$&&aLAL		(#A

4>D>*002Aq	A4Kr   )r&   rd   r,   re   iscomplexobjrf   )
r=   r>   r)   ri   rB   r?   r   rN   rk   rj   s
   ` ````   @r   r4   r4      s    P 
BBxxH


4?T?#))+A$:(:J	BIaL	 	 2!	 	 (F1Iq<<r   c                 X    U(       a  [        U 5      nOU nUb  UR                  U5      nU$ )z9
Convert from real to complex and reshape result arrays.
)rb   rc   )r]   rN   r,   rg   s       r   r<   r<      s/     &!IIeHr   c                 n    [         R                  " U [        S9R                  [         R                  5      $ N)dtype)r&   ascontiguousarrayfloatview
complex128)r   s    r   rb   rb      s$    /44R]]CCr   c                 n    [         R                  " U [        S9R                  [         R                  5      $ rq   )r&   rs   complexru   float64)rg   s    r   rf   rf     s$    166rzzBBr   )r   g:0yE>gYni  NNF
   Ng|=r   r   )r   r%   )__doc__r5   numpyr&   scipy.optimizer   scipy.optimize._optimizer   _linesearchr   r   	Exceptionr	   r^   r4   r<   rb   rf   r   r   r   <module>r      sS      ) ; V	Y 	 FJLP<BVrG=T
DCr   