o
    èEbä  ã                   @   sN   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d„Z
G dd	„ d	eƒZdS )z$Newton-CG trust-region optimization.é    Né   )Ú_minimize_trust_regionÚBaseQuadraticSubproblem© c                 K   sF   |du rt dƒ‚|du r|du r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.

    Nz<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ÚhesspZ
subproblem)Ú
ValueErrorr   ÚCGSteihaugSubproblem)ZfunZx0r   r   r   r	   Ztrust_region_optionsr   r   úA/usr/lib/python3/dist-packages/scipy/optimize/_trustregion_ncg.pyÚ_minimize_trust_ncg   s   ÿþr   c                   @   s   e Zd ZdZdd„ ZdS )r   z:Quadratic subproblem solved by a conjugate gradient methodc                 C   sj  t  | j¡}tdt | j¡ƒ| j }| j|k rd}||fS |}| j}| }	 |  |¡}t  ||¡}	|	dkr]|  	|||¡\}
}||
|  }|||  }| |ƒ| |ƒk rU|}n|}d}||fS t  ||¡}||	 }|||  }t
j |¡|krŠ|  	|||¡\}
}|||  }d}||fS |||  }t  ||¡}t |¡|k r£d}||fS || }| ||  }|}|}|}q&)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   )ÚnpZ
zeros_liker   ÚminÚmathZsqrtZjac_magr	   ÚdotZget_boundaries_intersectionsÚscipyZlinalgZnorm)ÚselfZtrust_radiusZp_originZ	toleranceZhits_boundaryÚzÚrÚdZBdZdBdÚtaÚtbÚpaZpbZ
p_boundaryZ	r_squaredZalphaZz_nextZr_nextZr_next_squaredZ	beta_nextZd_nextr   r   r   Úsolve,   sN   

ØzCGSteihaugSubproblem.solveN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   r   r   *   s    r   )r   NNN)r   r   Znumpyr   Zscipy.linalgr   Z_trustregionr   r   Ú__all__r   r   r   r   r   r   Ú<module>   s    
