
    ^Mh1*                         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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_call_callback_maybe_halt)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     [/var/www/html/test/jupyter/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__5   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.fun8   '     7?ii((DGwr   c                 \    | j         |                     | j                  | _         | j         S )z=Value of Jacobian of objective function at current iteration.)r   r%   r   r4   s    r   r*   zBaseQuadraticSubproblem.jac?   r5   r   c                 \    | j         |                     | j                  | _         | j         S )z<Value of Hessian of objective function at current iteration.)r    r&   r   r4   s    r   r+   zBaseQuadraticSubproblem.hessF   s'     7?jj))DGwr   c                 z    | j         |                      | j        |          S t          j        | j        |          S r   )r'   r   r   r/   r+   r0   s     r   r,   zBaseQuadraticSubproblem.hesspM   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*   r4   s    r   jac_magzBaseQuadraticSubproblem.jac_magS   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_intersectionsZ   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(   rH   s     r   solvezBaseQuadraticSubproblem.solveq   s    ! #4 5 5 	5r   )NN)__name__
__module____qualname____doc__r-   r2   propertyr)   r*   r+   r,   r=   rP   rS   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        |                     |dz  }t/          ||j                  } t1          ||           rn#|j        |
k     rd}n||k    rd}n|j        |
k    %t2          d         t2          d         ddf}!|r|dk    rt5          |!|                    n"t7          j        |!|         t:          d           t5          d|j        d           t5          d|z             t5          d#j        z             t5          d#j        z             t5          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   r1   r   sfs      r   r,   z%_minimize_trust_region.<locals>.hessp   s    771::>>!$$$r         r?   g      ?r   )r   r)   successmaxiterz:A bad approximation caused failure to predict improvement.z3A linalg error occurred, such as a non-psd Hessian.)
stacklevelz!         Current function value: fz         Iterations: %dz!         Function evaluations: %dz!         Gradient evaluations: %dz          Hessian evaluations: %d)
r   rb   statusr)   r*   nfevnjevnhevnitmessager+   allvecs)"r   
ValueError	Exceptionr   asarrayflattenr   r)   gradcallabler+   r
   
isinstancer	   r   lenr=   rS   r;   LinAlgErrorminappendr   r   r   r   printwarningswarnRuntimeWarningrg   ngevri   r*   )$r)   x0r   r*   r+   r,   
subprobleminitial_trust_radiusmax_trust_radiusetagtolrc   disp
return_allcallbackinexactunknown_optionsnhesspwarnflagrH   r   rl   mkr1   hits_boundarypredicted_value
x_proposed
m_proposedactual_reductionpredicted_reductionrhointermediate_resultstatus_messagesresultr_   s$                                      @r   _minimize_trust_regionr   v   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::&&&	Q,qae<<<$X/BCC 	 9tH <<Ho )t

v I&I&HA	O  	Jq==/(+,,,,M/(3^PQRRRR;!%;;;<<<'!+,,,1BG;<<<1BG;<<<0BGfQi4GHIIIa(a- !15rwRW!#6!9!4!$3H$=? ? ?F
 v $#yMs   F0 0G	G	)r   NNNNrY   rZ   r[   r\   NFFNT)rW   rB   ry   numpyr   scipy.linalgr:   	_optimizer   r   r   r   r   'scipy.optimize._hessian_update_strategyr	   (scipy.optimize._differentiable_functionsr
   __all__r   r   r   r   r   r   <module>r      s                3 3 3 3 3 3 3 3 3 3 3 3 3 3 J 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	z z z z z zr   