U
    ?h                     @   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krt d|dkr(|dk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   Q/var/www/html/venv/lib/python3.8/site-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   sp  t | j}tdt| j| j }| j|k r:d}||fS |}| j}| }| |}t ||}	|	dkr| 	|||\}
}||
|  }|||  }| || |k r|}n|}d}||fS t ||}||	 }|||  }t
j||kr| 	|||\}
}|||  }d}||fS |||  }t ||}t||k rHd}||fS || }| ||  }|}|}|}qJdS )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      ?Fr   TN)npZ
zeros_liker   minmath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_squaredalphaZz_nextZr_nextZr_next_squaredZ	beta_nextZd_nextr   r   r   solve,   sJ    

zCGSteihaugSubproblem.solveN)__name__
__module____qualname____doc__r   r   r   r   r   r   *   s   r   )r   NNN)r    r   numpyr   Zscipy.linalgr   Z_trustregionr   r   __all__r   r   r   r   r   r   <module>   s   
