
    ^Mh                         d Z ddlZddlZddlmZ ddlmZ ddlm	Z	m
Z
  G d de          Z	 	 	 ddZddZddZd Zd ZdS )z,
Spectral Algorithm for Nonlinear Equations
    N)OptimizeResult)_check_unknown_options   )_nonmonotone_line_search_cruz_nonmonotone_line_search_chengc                       e Zd ZdS )_NoConvergenceN)__name__
__module____qualname__     X/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/scipy/optimize/_spectral.pyr	   r	      s        Dr   r	   r   :0yE>Yn  F
   绽|=      ?cruzc           	      6  *+, t          |           |dvrt          d|d          d,|
*fd}
|+,fd},fd}d	g}t          | |||||          \  }}}+}}d	}+*|} ||          }t          j        +g|	          }d
}*}d}d}	  ||          }|rt          d|||fz             | |||           |||z  |z   k     rd}d}nt          |          d|z  k    rd|z  t          j        |          z  }nt          |          |k     r|}| |z  } |
|||          } 	 |dk    rt          |||||           \  }!}"}#}$n#|dk    rt          |||+|||           \  }!}"}#}$}}n# t          $ r Y naw xY w|"|z
  }%|$|z
  }&t          j        |%|%          t          j        |%|&          z  }|"}|$}|#+|dk    r|                    |#           |dz  }Mt          |||          }'t          ||          }(t          |'|||(|d	         |d          })|)S )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).

    )chengr   zInvalid value z for 'line_search'   Nc                     d| z   dz  z  S )Nr   r   r   )kxFf_0s      r   eta_strategyz#_root_df_sane.<locals>.eta_strategyJ   s    !a%!##r   c                     dz  z  S )Nr   r   )r   f_knexps    r   fnormz_root_df_sane.<locals>.fnormO   s    T?"r   c                 H    t           j                            |           z  S N)nplinalgnorm)r   r"   s    r   fmeritz_root_df_sane.<locals>.fmeritS   s    y~~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)   r/   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 ?++++++K+KKKLLLD	$ 	$ 	$ 	$ 	$ }	# 	# 	# 	# 	# 	#' ' ' ' ' 3D,6tR7;VT-K -K)AsGS#z 	
A
CGuSzzH q))G 	AAI6G25:: 	N3q(G6LLMMMHS#dXo---.GI w<<!I+%%	kBGG$4$44GG\\I%%G HsN l1c3''	f$$$A!S!WFI%K %K %K!r2rr''*HCQRTWIJASV+X +X +X'r2r1a 	 	 	E	 3h3h'#s##bgc3&7&77  &  NN2	Qe2h 	S*G444AS*%%Aa$+ !QQyJ J JF Ms   *AE. .
E;:E;c                    	 t          j        |          }|j        	t          j          |gR                                            }t          j        |          pt          j        |          }|                                }dd<   |r) 	fd}t          |          }t          |          }n
 	fd}||	 |          ||fS )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                 2   	d         k    rt                      	dxx         dz  cc<   t          |                               
          }t          j         |gR                                            }t          |          } |          }||fS Nr   r   )r	   _real2complexreshaper&   asarrayravel_complex2real)r   zvr   rI   r>   r)   r<   rA   	nfev_listx0_shapes        r   	wrap_funcz_wrap_func.<locals>.wrap_func   s    |v%%$&&&aLLLALLLa  ((22A
44>D>>>**0022Aa  Aq		Aa4Kr   c                     d         k    rt                      dxx         dz  cc<   |                               } t          j         | gR                                            } |          }||fS r`   )r	   rb   r&   rc   rd   )	r   r   rI   r>   r)   r<   rA   rh   ri   s	      r   rj   z_wrap_func.<locals>.wrap_func   s    |v%%$&&&aLLLALLL		(##A
44>D>>>**0022Aq		Aa4Kr   )r&   rc   r+   rd   iscomplexobjre   )
r<   r=   r)   rh   rA   r>   r   rM   rj   ri   s
   ` ````   @r   r3   r3      s#   P 
BBxH

44?T???##))++A$$:(:(:J	BIaL 	 	 	 	 	 	 	 	 	 	 2!	 	 	 	 	 	 	 	 	 	 b(FF1IIq*<<r   c                 \    |rt          |           }n| }||                    |          }|S )zA
    Convert from real to complex and reshape result arrays.
    )ra   rb   )r\   rM   r+   rf   s       r   r;   r;      s<      &!!IIeHr   c                 r    t          j        | t                                        t           j                  S N)dtype)r&   ascontiguousarrayfloatview
complex128)r   s    r   ra   ra      s(    ///44R]CCCr   c                 r    t          j        | t                                        t           j                  S ro   )r&   rq   complexrs   float64)rf   s    r   re   re     s(    11166rzBBBr   )r   r   r   r   NNFr   Nr   r   r   )r   r%   )__doc__r4   numpyr&   scipy.optimizer   scipy.optimize._optimizer   _linesearchr   r   	Exceptionr	   r]   r3   r;   ra   re   r   r   r   <module>r~      s            ) ) ) ) ) ) ; ; ; ; ; ; V V V V V V V V	 	 	 	 	Y 	 	 	 FJLP<BV V V VrG= G= G= G=T
 
 
 
D D DC C C C Cr   