
    Џkh                     R    d Z ddlZddlZddlZddlmZmZ g Z	ddZ
 G d de      Zy)	z$Newton-CG trust-region optimization.    N   )_minimize_trust_regionBaseQuadraticSubproblemc           	      h    |t        d      ||t        d      t        | |f||||t        d|S )a2  
    Minimization of scalar function of one or more variables using
    the Newton conjugate gradient trust-region algorithm.

    Options
    -------
    initial_trust_radius : float
        Initial trust-region radius.
    max_trust_radius : float
        Maximum value of the trust-region radius. No steps that are longer
        than this value will be proposed.
    eta : float
        Trust region related acceptance stringency for proposed steps.
    gtol : float
        Gradient norm must be less than `gtol` before successful
        termination.

    z<Jacobian is required for Newton-CG trust-region minimizationzdEither the Hessian or the Hessian-vector product is required for Newton-CG trust-region minimization)argsjachesshessp
subproblem)
ValueErrorr   CGSteihaugSubproblem)funx0r   r   r	   r
   trust_region_optionss          [/var/www/teggl/fontify/venv/lib/python3.12/site-packages/scipy/optimize/_trustregion_ncg.py_minimize_trust_ncgr      sc    ( { ( ) 	)| O P 	P!#r :#D(-:N:$8: :    c                       e Zd ZdZd Zy)r   z:Quadratic subproblem solved by a conjugate gradient methodc                 L   t        j                  | j                        }t        dt	        j
                  | j                              | j                  z  }| j                  |k  rd}||fS |}| j                  }| }	 | j                  |      }t        j                  ||      }	|	dk  rB| j                  |||      \  }
}||
|z  z   }|||z  z   } | |       | |      k  r|}n|}d}||fS t        j                  ||      }||	z  }|||z  z   }t        j                  j                  |      |k\  r$| j                  |||      \  }
}|||z  z   }d}||fS |||z  z   }t        j                  ||      }t	        j
                  |      |k  rd}||fS ||z  }| ||z  z   }|}|}|}))a  
        Solve the subproblem using a conjugate gradient method.

        Parameters
        ----------
        trust_radius : float
            We are allowed to wander only this far away from the origin.

        Returns
        -------
        p : ndarray
            The proposed step.
        hits_boundary : bool
            True if the proposed step is on the boundary of the trust region.

        Notes
        -----
        This is algorithm (7.2) of Nocedal and Wright 2nd edition.
        Only the function that computes the Hessian-vector product is required.
        The Hessian itself is not required, and the Hessian does
        not need to be positive semidefinite.
        g      ?FTr   )np
zeros_liker   minmathsqrtjac_magr
   dotget_boundaries_intersectionsscipylinalgnorm)selftrust_radiusp_origin	tolerancehits_boundaryzrdBddBdtatbpapb
p_boundary	r_squaredalphaz_nextr_nextr_next_squared	beta_nextd_nexts                         r   solvezCGSteihaugSubproblem.solve,   s   2 ==* TYYt||45D	 <<)#!M]** HHB  AB&&B-Cax
 ::1aNBaZaZ8d2h&!#J!#J $!=00q!IOE]F||  (L8 ::1aNBaZ
 $!=00^FVVFF3Nyy(94 %},,&2IWy1},F AAAQ r   N)__name__
__module____qualname____doc__r7    r   r   r   r   *   s    DRr   r   )r<   NNN)r;   r   numpyr   scipy.linalgr   _trustregionr   r   __all__r   r   r<   r   r   <module>rA      s-    *    K
:>T2 Tr   