
     h)                         d Z ddlZddlZddlZddlZddlmZm	Z	m
Z
mZ ddlmZ ddlmZ g Zd Z G d d	          Z	 	 	 	 	 ddZdS )zTrust-region optimization.    N   )_check_unknown_options_status_messageOptimizeResult_prepare_scalar_function)HessianUpdateStrategy)
FD_METHODSc                 0     dg d fS  fd}|fS )Nr   c                 `    dxx         dz  cc<    t          j        |           g|z   R  S )Nr   r   )npcopy)xwrapper_argsargsfunctionncallss     W/var/www/html/Sam_Eipo/venv/lib/python3.11/site-packages/scipy/optimize/_trustregion.pyfunction_wrapperz(_wrap_function.<locals>.function_wrapper   s=    q			Q			x

;lT&9;;;;     )r   r   r   r   s   `` @r   _wrap_functionr      sO     SFt|< < < < < < <
 ###r   c                       e Zd ZdZddZd Zed             Zed             Zed             Z	d Z
ed	             Zd
 Zd ZdS )BaseQuadraticSubproblemaQ  
    Base/abstract class defining the quadratic model for trust-region
    minimization. Child classes must implement the ``solve`` method.

    Values of the objective function, Jacobian and Hessian (if provided) at
    the current iterate ``x`` are evaluated on demand and then stored as
    attributes ``fun``, ``jac``, ``hess``.
    Nc                     || _         d | _        d | _        d | _        d | _        d | _        d | _        || _        || _        || _	        || _
        d S N)_x_f_g_h_g_mag_cauchy_point_newton_point_fun_jac_hess_hessp)selfr   funjachesshessps         r   __init__z BaseQuadraticSubproblem.__init__'   sU    !!		
r   c                     | j         t          j        | j        |          z   dt          j        ||                     |                    z  z   S )Ng      ?)r(   r   dotr)   r+   r'   ps     r   __call__z BaseQuadraticSubproblem.__call__4   s=    x"&1---bfQ

16N6N0NNNr   c                 \    | j         |                     | j                  | _         | j         S )z1Value of objective function at current iteration.)r   r#   r   r'   s    r   r(   zBaseQuadraticSubproblem.fun7   '     7?ii((DGwr   c                 \    | j         |                     | j                  | _         | j         S )z=Value of Jacobian of objective function at current iteration.)r   r$   r   r3   s    r   r)   zBaseQuadraticSubproblem.jac>   r4   r   c                 \    | j         |                     | j                  | _         | j         S )z<Value of Hessian of objective function at current iteration.)r   r%   r   r3   s    r   r*   zBaseQuadraticSubproblem.hessE   s'     7?jj))DGwr   c                 z    | j         |                      | j        |          S t          j        | j        |          S r   )r&   r   r   r.   r*   r/   s     r   r+   zBaseQuadraticSubproblem.hesspL   s4    ;";;tw***6$)Q'''r   c                 p    | j         )t          j                            | j                  | _         | j         S )zAMagnitude of jacobian of objective function at current iteration.)r    scipylinalgnormr)   r3   s    r   jac_magzBaseQuadraticSubproblem.jac_magR   s-     ;,++DH55DK{r   c                 F   t          j        ||          }dt          j        ||          z  }t          j        ||          |dz  z
  }t          j        ||z  d|z  |z  z
            }|t          j        ||          z   }| d|z  z  }	d|z  |z  }
t          |	|
g          S )z
        Solve the scalar quadratic equation ||z + t d|| == trust_radius.
        This is like a line-sphere intersection.
        Return the two values of t, sorted from low to high.
              )r   r.   mathsqrtcopysignsorted)r'   zdtrust_radiusabcsqrt_discriminantauxtatbs              r   get_boundaries_intersectionsz4BaseQuadraticSubproblem.get_boundaries_intersectionsY   s     F1aLLq!F1aLL<?* IacAaCEk22 $- 11555TQqS\TCZr2hr   c                      t          d          )Nz9The solve method should be implemented by the child class)NotImplementedError)r'   rG   s     r   solvezBaseQuadraticSubproblem.solvep   s    ! #4 5 5 	5r   )NN)__name__
__module____qualname____doc__r,   r1   propertyr(   r)   r*   r+   r<   rO   rR   r   r   r   r   r      s            O O O   X   X   X( ( (   X     .5 5 5 5 5r   r   r         ?     @@333333?-C6?FTc                   " t          |           |t          d          ||t          d          |t          d          d|	cxk    rdk     sn t          d          |dk    rt          d          |dk    rt          d	          ||k    rt          d
          t          j        |                                          }t          | ||||          ""j        } "j        }t          |          r"j
        }nEt          |          rn5|t          v st          |t                    rd}"fd}nt          d          t          ||          \  }}|t          |          dz  }d}|}|}|r|g} ||| |||          }d}|j        |
k    r	 |                    |          \  }}n# t          j        j        $ r d}Y nw xY w ||          }||z   } ||| |||          }|j        |j        z
  }|j        |z
  }|dk    rd}n||z  }|dk     r|dz  }n|dk    r|rt)          d|z  |          }||	k    r|}|}|r'|                    t          j        |                     | |t          j        |                     |dz  }|j        |
k     rd}n||k    rd}n|j        |
k    t.          d         t.          d         ddf} |r|dk    rt1          | |                    n!t3          j        | |         t6          d           t1          d|j        z             t1          d|z             t1          d"j        z             t1          d"j        z             t1          d"j        |d         z   z             t?          ||dk    ||j        |j         "j        "j        "j        |d         z   || |         
  
        }!|
|j
        |!d<   |r||!d<   |!S )a  
    Minimization of scalar function of one or more variables using a
    trust-region algorithm.

    Options for the trust-region algorithm are:
        initial_trust_radius : float
            Initial trust radius.
        max_trust_radius : float
            Never propose steps that are longer than this value.
        eta : float
            Trust region related acceptance stringency for proposed steps.
        gtol : float
            Gradient norm must be less than `gtol`
            before successful termination.
        maxiter : int
            Maximum number of iterations to perform.
        disp : bool
            If True, print convergence message.
        inexact : bool
            Accuracy to solve subproblems. If True requires less nonlinear
            iterations, but more vector products. Only effective for method
            trust-krylov.

    This function is called by the `minimize` function.
    It is not supposed to be called directly.
    Nz7Jacobian is currently required for trust-region methodsz_Either the Hessian or the Hessian-vector product is currently required for trust-region methodszBA subproblem solving strategy is required for trust-region methodsr   g      ?zinvalid acceptance stringencyz%the max trust radius must be positivez)the initial trust radius must be positivez?the initial trust radius must be less than the max trust radius)r)   r*   r   c                 T                         |                               |          S r   )r*   r.   )r   r0   r   sfs      r   r+   z%_minimize_trust_region.<locals>.hessp   s    771::>>!$$$r         r>   g      ?r   successmaxiterz:A bad approximation caused failure to predict improvement.z3A linalg error occurred, such as a non-psd Hessian.z#         Current function value: %fz         Iterations: %dz!         Function evaluations: %dz!         Gradient evaluations: %dz          Hessian evaluations: %d)
r   ra   statusr(   r)   nfevnjevnhevnitmessager*   allvecs)!r   
ValueError	Exceptionr   asarrayflattenr   r(   gradcallabler*   r	   
isinstancer   r   lenr<   rR   r:   LinAlgErrorminappendr   r   printwarningswarnRuntimeWarningrd   ngevrf   r   r)   )#r(   x0r   r)   r*   r+   
subprobleminitial_trust_radiusmax_trust_radiusetagtolrb   disp
return_allcallbackinexactunknown_optionsnhesspwarnflagrG   r   ri   mkr0   hits_boundarypredicted_value
x_proposed
m_proposedactual_reductionpredicted_reductionrhostatus_messagesresultr^   s#                                     @r   _minimize_trust_regionr   u   s    > ?+++
{ # $ $ 	$| J K K 	K 0 1 1 	1OOOOtOOOO78881?@@@q  DEEE/// , - - 	- 
B			!	!B 
"#rsD	I	I	IB
&C
'C~~ Kw	% K 	
*


41F G G
 	% 	% 	% 	% 	% 	%  J K K 	K #5$//MFE b''#+ H (L
A #
1c3e,,A	A )t

	 ww|44A}}y$ 	 	 	HE	
 !A$$ U
Z
CdEBB
 5:>1eo5!##H!44 ::D LL4ZZMZq~/?@@L 99AA  	'NN271::&&&HRWQZZ   	Q 9tH <<Hk )t

r I&I&HA	O  	Jq==/(+,,,,M/(3^QGGG3ae;<<<'!+,,,1BG;<<<1BG;<<<0BGfQi4GHIIIa(a- !15rwRW!#6!9!4!$3H$=? ? ?F
 v $#yMs   F0 0G	G	)r   NNNNrX   rY   rZ   r[   NFFNT)rV   rA   rv   numpyr   scipy.linalgr9   	_optimizer   r   r   r   'scipy.optimize._hessian_update_strategyr   (scipy.optimize._differentiable_functionsr	   __all__r   r   r   r   r   r   <module>r      s                A A A A A A A A A A A A I I I I I I ? ? ? ? ? ?
$ $ $U5 U5 U5 U5 U5 U5 U5 U5p IMADCG@E26	x x x x x xr   