
    -i
0                         S r SSKrSSKrSSKrSSKJrJr  SSKJ	r	   " S S\
5      r SS jrSS	 jr       SS
 jrSS jrg)a  
Our own implementation of the Newton algorithm

Unlike the scipy.optimize version, this version of the Newton conjugate
gradient solver uses only one function call to retrieve the
func value, the gradient value and a callable for the Hessian matvec
product. If the function call is very expensive (e.g. for logistic
regression with large design matrix), this approach gives very
significant speedups.
    N)line_search_wolfe1line_search_wolfe2   )ConvergenceWarningc                       \ rS rSrSrg)_LineSearchError    N)__name__
__module____qualname____firstlineno____static_attributes__r
       I/var/www/html/venv/lib/python3.13/site-packages/sklearn/utils/optimize.pyr   r      s    r   r   c           	      .   US:  n	S[         R                  " [         R                  " U5      R                  5      R                  -  n
U	(       a$  [        S5        [        SU
 35        [        S5        [        XX#XEU40 UD6nU	(       a  US   c  SOSn[        S	U-   S
-   5        US   Gc  UR                  S[        5       5      nU " X#-   /UQ76 n[         R                  " XZ-  5      nX-
  n[         R                  " U5      U:*  nU	(       a(  [        S[         R                  " U5       SU SU 35        U(       a  [        R                  R                  USS9nU" X#-   /UQ76 n[        R                  R                  USS9nUU:  nU	(       a  [        SU SU SU 35        U(       a  SUS   S-   US   S-   UUU4nUS   cC  U	(       a  [        S5        [        XX#XEU40 UD6nU	(       a  US   c  SOSn[        SU-   S
-   5        US   c
  [        5       eU$ )z
Same as line_search_wolfe1, but fall back to line_search_wolfe2 if
suitable step length is not found, and raise an exception if a
suitable step length is not found.

Raises
------
_LineSearchError
    If no suitable step size is found.

r      z  Line Searchz    eps=16 * finfo.eps=z    try line search wolfe1r   znot  z    wolfe1 line search was 
successfulargsz2    check loss |improvement| <= eps * |loss_old|:  <=     )ordz1    check sum(|gradient|) < sum(|gradient_old|): z <       ?z'    last resort: try line search wolfe2z    wolfe2 line search was )npfinfoasarraydtypeepsprintr   gettupleabsscipylinalgnormr   r   )ffprimexkpkgfkold_fvalold_old_fvalverbosekwargs
is_verboser    ret_not_r   fval	tiny_lossloss_improvementchecksum_abs_grad_oldgradsum_abs_grads                        r   _line_search_wolfe12r;      s:    AJ
rxx

8,22377
7Co'u-.*+
Q|
Vv
VCa&.b+e3lBC
1v~ zz&%'* 4 FF8>*	?'(I5FF+,-T)AeWF $||00!0<"')D)D <<,,Tq,9L #33EG#nC(8'95'C FQJFQJ 1v~
 ;< rsl
>D
 !!fnF"E/%7,FG
1v~  Jr   c                 t   S[         R                  " [         R                  5      R                  -  n[         R                  " [        U5      UR                  S9n[         R                  " U5      nU* nSn	[         R                  " Xw5      n
U
nUS:  nX::  GaX  [         R                  " [         R                  " U5      5      U::  aD  U(       a<  [        SU	 S[         R                  " [         R                  " U5      5       SU 35        OU " U5      n[         R                  " X5      nSUs=::  a  X[-  ::  a'  O  O$U(       a  [        SU	 SU S	U S
U SX[-   3
5        OUS:  a<  U	S:  a  U(       a  [        SU	 SU 35        OmXjU-  U-  -  nU(       a  [        S5        OPX-  nXoU-  -  nXU-  -  n[         R                  " Xw5      nUU
-  nU* UU-  -   nUUS-  U-  -   nU	S-   n	Un
X::  a  GMX  U(       aA  X:  a<  [        SU	S-
   S[         R                  " [         R                  " U5      5       35        U$ )a  
Solve iteratively the linear system 'fhess_p . xsupi = fgrad'
with a conjugate gradient descent.

Parameters
----------
fhess_p : callable
    Function that takes the gradient as a parameter and returns the
    matrix product of the Hessian and gradient.

fgrad : ndarray of shape (n_features,) or (n_features + 1,)
    Gradient vector.

maxiter : int
    Number of CG iterations.

tol : float
    Stopping criterion.

Returns
-------
xsupi : ndarray of shape (n_features,) or (n_features + 1,)
    Estimated solution.
r   )r   r   r   z  Inner CG solver iteration z+ stopped with
    sum(|residuals|) <= tol: r   z2 stopped with
    tiny_|p| = eps * ||p||^2, eps = z, squared L2 norm ||p||^2 = z
    curvature <= tiny_|p|: z. stopped with negative curvature, curvature = z;  Inner CG solver iteration 0 fell back to steepest descentr   z+  Inner CG solver stopped reaching maxiter=z with sum(|residuals|) = )r   r   float64r    zeroslenr   copydotsumr$   r!   )fhess_pfgradmaxitertolr/   r    xsupiripsupiidri0psupi_norm2r1   Apcurvalphaidri1betais                     r   _cgrR   q   sA   2 rxx

#''
'CHHSZu{{3E	BCE	A66">DKAJ
,66"&&*$21# 6446FF266":4F3GtC5R U^vve )))21# 6;;>% @11< >226tC<M;NP AX1u6qc :2269  u,,WX%
rkvvb~teem#UAX33E[ ,\ ak9!a% A""$&&"4!57	
 Lr   c                    [         R                  " U5      R                  5       n[         R                  " U5      nSnU(       a  U" U/UQ76 nSnOSnU
S:  nX:  a  U " U/UQ76 u  nn[         R                  " U5      n[         R
                  " U5      nUU:*  nU(       a-  [        SU 35        [        S5        [        SU SU SU 35        U(       a  O~[         R                  " U5      n[        S[         R                  " U5      /5      nUU-  n[        UUUUU
S	9nS
nU(       a   [        UUUUUUWU
US9	u  nnnpnUUU-  -  nUS-  nX:  a  M  U	(       a'  X:  a"  [        R                  " SU S3[        5        X4$ U(       a  [        SU S35        X4$ ! [         a    [        R                  " S5         Mj  f = f)a  
Minimization of scalar function of one or more variables using the
Newton-CG algorithm.

Parameters
----------
grad_hess : callable
    Should return the gradient and a callable returning the matvec product
    of the Hessian.

func : callable
    Should return the value of the function.

grad : callable
    Should return the function value and the gradient. This is used
    by the linesearch functions.

x0 : array of float
    Initial guess.

args : tuple, default=()
    Arguments passed to func_grad_hess, func and grad.

tol : float, default=1e-4
    Stopping criterion. The iteration will stop when
    ``max{|g_i | i = 1, ..., n} <= tol``
    where ``g_i`` is the i-th component of the gradient.

maxiter : int, default=100
    Number of Newton iterations.

maxinner : int, default=200
    Number of CG iterations.

line_search : bool, default=True
    Whether to use a line search or not.

warn : bool, default=True
    Whether to warn when didn't converge.

Returns
-------
xk : ndarray of float
    Estimated minimum.
r   NzNewton-CG iter = z  Check Convergencez    max |gradient| <= tol: r   r   g      ?)rE   rF   r/   r   )r/   r   zLine Search failedr   z'newton-cg failed to converge at loss = z$. Increase the number of iterations.z   Solver did converge at loss = .)r   r   flattenr@   r$   maxr!   rB   minsqrtrR   r;   r   warningswarnr   )	grad_hessfuncr9   x0r   rF   rE   maxinnerline_searchrZ   r/   r*   kr-   r.   r1   rD   rC   absgradmax_absgradr7   maggradetatermcondrG   alphakfcgcgfkp1s                                r   
_newton_cgrj      s   t 
B			!B	B	A?T?1J + #2--w&&-ffWos"%aS)*'(/}DQugNO&&/3()*= GUH(GT@T #
A=B 	fun	QW +Z 9( D) ) 	
 5L 
0
!<=5L# $ 23s   (F  F>=F>c                 D   U S:X  a  Ub  [        UR                  U5      nOUR                  nUR                  S:w  a]  U  SU SUR                   SUR                   S3nUb  XB:X  a	  USU S3-  nUS	-  nUb  USU-   -  n[        R
                  " U[        S
S9  U$ [        e)a  Check the OptimizeResult for successful convergence

Parameters
----------
solver : str
   Solver name. Currently only `lbfgs` is supported.

result : OptimizeResult
   Result of the scipy.optimize.minimize function.

max_iter : int, default=None
   Expected maximum number of iterations.

extra_warning_msg : str, default=None
    Extra warning message.

Returns
-------
n_iter : int
   Number of iterations.
lbfgsr   z failed to converge after z iteration(s) (status=z):

zH
Increase the number of iterations to improve the convergence (max_iter=z).zr
You might also want to scale the data as shown in:
    https://scikit-learn.org/stable/modules/preprocessing.htmlr   )
stacklevel)rW   nitstatusmessagerY   rZ   r   NotImplementedError)solverresultmax_iterextra_warning_msgn_iter_iwarning_msgs         r   _check_optimize_resultry   K  s    .  6::x0HzzH==A(4XJ ?!==/>>""&  #(<--5Jb: %K
 !,t&777MM+'9aH
 O "!r   )r   )r
   g-C6?d      TTr   )NN)__doc__rY   numpyr   r%   scipy.optimize._linesearchr   r   
exceptionsr   RuntimeErrorr   r;   rR   rj   ry   r
   r   r   <module>r      s]   	"    M +	| 	
 =>OdV| 
	~B:r   