
    -ipb                         S r SSKrSSKJr  SSKJrJrJ	r	J
r
  SSKJrJr  SSKJrJrJrJrJr  S rS\R*                  S	\4S
 jrS rg)a  
This module provides subroutines concerning the trust-region calculations of COBYLA.

Translated from Zaikun Zhang's modern-Fortran reference implementation in PRIMA.

Dedicated to late Professor M. J. D. Powell FRS (1936--2015).

Python translation by Nickolai Belakovski.
    N   )	DEBUGGINGREALMINREALMAXEPS)qradd_Rdiagqrexc_Rdiag)isminormatprodinprodlsqrprimasumc                 L   U R                   S   nU R                   S   n[        (       aP  US:  d   eUS:  d   e[        R                  " U5      U:X  d   e[        R                  " U5      U:X  d   eUS:  d   e[        R                  " US-   5      n[        R                  " US-   [
        S9nSn[        R                  " U5      n	[        R                  " XU45      n
[        R                  " XR                  US45      /5      n[        R                  " US/5      n[        US-   5       HY  n[        [        USS2U4   5      5      =nS:  d  M&  [        S[        -  SU-  5      nUSS2U4==   U-  ss'   X==   U-  ss'   M[     [        USU USUSS2SU24   USU X)USU U
5	      u  USU& pUSU& n
[        XxSXX)Xj5	      u  pxpn
[        (       aN  [        [        R                  " U	5      5      (       d   e[        R                  R!                  U	5      SU-  ::  d   eU	$ )ab  
This function calculated an n-component vector d by the following two stages. In the first
stage, d is set to the shortest vector that minimizes the greatest violation of the constraints
    A.T @ D <= B,  K = 1, 2, 3, ..., M,
subject to the Euclidean length of d being at most delta. If its length is strictly less than
delta, then the second stage uses the resultant freedom in d to minimize the objective function
    G.T @ D
subject to no increase in any greatest constraint violation.

It is possible but rare that a degeneracy may prevent d from attaining the target length delta.

cviol is the largest constraint violation of the current d: max(max(A.T@D - b), 0)
icon is the index of a most violated constraint if cviol is positive.

nact is the number of constraints in the active set and iact[0], ..., iact[nact-1] are their indices,
while the remainder of the iact contains a permutation of the remaining constraint indicies.
N.B.: nact <= min(num_constraints, num_vars). Obviously nact <= num_constraints. In addition, the constraints
in iact[0, ..., nact-1] have linearly independent gradients (see the comments above the instruction
that delete a constraint from the active set to make room for the new active constraint with index iact[icon]);
it can also be seen from the update of nact: starting from 0, nact is incremented only if nact < n.

Further, Z is an orthogonal matrix whose first nact columns can be regarded as the result of
Gram-Schmidt applied to the active constraint gradients. For j = 0, 1, ..., nact-1, the number
zdota[j] is the scalar product of the jth column of Z with the gradient of the jth active
constraint. d is the current vector of variables and here the residuals of the active constraints
should be zero. Further, the active constraints have nonnegative Lagrange multipliers that are
held at the beginning of vmultc. The remainder of this vector holds the residuals of the inactive
constraints at d, the ordering of the components of vmultc being in agreement with the permutation
of the indices of the constraints that is in iact. All these residuals are nonnegative, which is
achieved by the shift cviol that makes the least residual zero.

N.B.:
0. In Powell's implementation, the constraints are A.T @ D >= B. In other words, the A and B in
our implementation are the negative of those in Powell's implementation.
1. The algorithm was NOT documented in the COBYLA paper. A note should be written to introduce it!
2. As a major part of the algorithm (see trstlp_sub), the code maintains and updates the QR
factorization of A[iact[:nact]], i.e. the gradients of all the active (linear) constraints. The
matrix Z is indeed Q, and the vector zdota is the diagonal of R. The factorization is updated by
Givens rotations when an index is added in or removed from iact.
3. There are probably better algorithms available for the trust-region linear programming problem.
   r   dtypeNg   mBr   )shaper   npsizezerosinthstackreshaperangemaxabsr   
trstlp_suballisfinitelinalgnorm)Abdeltagnum_constraintsnum_varsvmultciactnactdzA_augb_augimaxvalmodscals                   X/var/www/html/venv/lib/python3.13/site-packages/scipy/_lib/pyprima/cobyla/trustregion.pytrstlpr3      sh   X ggajOwwqzH y1}}!###wwqzX%%%wwqz_,,,qyy XXo)*F88Oa's3DD
A
(%&A IIq))XqM234EIIq!fE
 ?1$%E!Q$K())FT1!G)QvX.G!Q$K7"KHH	 & DNdScTcNdfjlmotuv  yI  zI  yI  vI  pJ  LQ  Rb  Sb  Lc  ej  ou  vF  wF  oG  IJ  DK@D	/DV,<_%=q  *$auQW[D1 y2;;q>""""yy~~a AI---H    r)   r*   c	                 4   [         R                  " UR                  S   5      n	[         R                  " [         R                  " U5      5      n
[         R                  " [         R                  " US5      5      n[         R                  " US5      n[         R                  " US5      n[        (       Gah  US:  d   eUS:X  d  US:X  d   eUS:  a  US:X  d  US:  a  US:X  d   e[         R                  " U5      U:X  d   e[         R                  " U 5      U:X  d   e[         R                  " U5      U:X  d   e[         R                  " U5      U:X  d   e[         R                  " US5      U:X  a  [         R                  " US5      U:X  d   eUS:  d   eUS:X  a  [        [         R                  " U5      5      (       a&  [         R                  R                  U5      SU-  ::  d   eUS:  a  U[         R                  " X5      ::  d   e[        USU 5      S:  d   eUS:X  a  [         R                  " SUS-
  U[        S9n Sn[         R                  " U5      n[         R                  " [         R                  " SU* 5      5      nX-   n[         R                  " U5      nUS:X  d  US::  a  XXgU4$ [        [         R                  " U5      5      (       a  XXgU4$ [         R                   " U* 5      nUn[         R                  " [#        U5      5      nOt[%        Xf5      XU-  :  a  XXgU4$ US-
  XS-
  '   SX|S-
  '   US-
  nUS-
  n[         R                  " [         R                  " S['        XcSS2SU24   5      USU -
  5      5      n[)        U5       Vs/ s H   n[%        USS2U4   USS2U U   4   5      PM"     snUSU& [*        nUnSn[         R                  " SS[        UU5      -  5      n[)        U5       GHm  n[        (       a  [        US:  5      (       d   eUS:X  a  UnO[%        XcSS2US-
  4   5      nUU:  d  UU:  a  UnSnOUS-  n[         R                  " UU5      nUS:X  a    GOX:  Ga  USU U	SU& Un[-        USS2X   4   XU5      u  pnUUS-   :X  a3  XS-   :w  a"  XqS-
     S4XUS-
  /'   XS-
  U/   XUS-
  /'   GOZSXqS-
  '   GOQ[/        USS2U SU 4   USS2X   4   USS2SU24   U	SU 5      U
SU& [1        [         R2                  " U
SU S:  U SU U:*  5      5      (       d    GO1S	XU& [)        U5       Vs/ s H&  nU
U   S:  a  U U   U::  a  UU   U
U   -  O[*        PM(     nn[5        USU 5      n[         R6                  " [         R                  " [#        USU 5      5      USU UU
SU -  -
  5      USU& [         R                  " XS-
     5      (       d  [9        XS-
     5      [:        S-  ::  a    GO_SU4XUS-
  /'   XS-
  U/   XUS-
  /'   US:X  ae  XS-
     US-
  :w  aW  US::  a    GO'[=        USS2U SU 4   XSU US-
  5      u  oSU& XS-
  US-
  /   XS-
  US-
  /'   XqS-
  US-
  /   XqS-
  US-
  /'   [         R                  " XS-
     5      (       d  [9        XS-
     5      [:        S-  ::  a    GOUS:X  a4  W[%        UUSS2XS-
     4   5      S-   XS-
     -  USS2US-
  4   -  -  nOS	XS-
     -  USS2US-
  4   -  nO[=        USS2U SU 4   XSU U5      u  oSU& / XS-   U QX   PXU& / XS-   U QX   PXU& US-  nUS:X  a	  US:  a    GOUS:  a>  [         R                  " XS-
     5      (       d  [9        XS-
     5      [:        S-  ::  a    GOUS:X  a!  W[%        UUSS2U4   5      USS2U4   -  -  nOS	XS-
     -  USS2US-
  4   -  nXU-  [%        Xf5      -
  n[%        UU5      n[%        UU5      nUS::  d+  U[:        U-  U-  ::  d  [         R                  " U5      (       a    GO[        [         R>                  " UU-  UU-  -   5      [9        U5      [         R>                  " UU-  5      5      n US:  a	  UU U-   -  n!OU U-
  U-  n!U!S::  d  [         R                  " U!5      (       d    GOUS:X  a   [A        UU!5      (       a    GO{[5        U!U5      n!UU!U-  -   n"US:X  aG  [         R                  " [         R                  " S['        U"USS2U SU 4   5      X@SU    -
  5      5      n[/        USS2U SU 4   U"USS2SU24   USU 5      * U
SU& US:X  a  [        SXS-
     5      XS-
  '   U['        U"USS2U 4   5      X@   -
  -
  n#['        [9        U"5      [9        USS2U 4   5      5      [9        X@   5      -   U-   n$SU#[A        U#U$5      '   U#X XU& [)        [#        U
5      5       Vs/ s H#  nU
U   S:  a  UU   UU   U
U   -
  -  O[*        PM%     nn[         RB                  " [         R                  " SU5      5      S-
  n[5        [         R                  " SU5      5      nUn%SU-
  U-  UU"-  -   n[         R6                  " SSU-
  U-  UU
-  -   5      n[         R                  " [E        [9        U5      5      5      (       a-  [         R                  " [E        [9        U5      5      5      (       d  U%n  OOUS:X  a7  [         R                  " [         R                  " S['        Xc5      U-
  5      5      nUS:  d  X:  d  GMn    O   [        (       Ga  [         R                  " U 5      U:X  d   e[         R                  " U5      U:X  d   e[        US:  5      (       d   e[         R                  " U5      U:X  d   e[        [         R                  " U5      5      (       d   e[         R                  R                  U5      SU-  ::  d   e[         R                  " US5      U:X  a  [         R                  " US5      U:X  d   eUS:  a  U[         R                  " X5      ::  d   eXXgU4$ s  snf s  snf s  snf )
a!  
This subroutine does the real calculations for trstlp, both stage 1 and stage 2.
Major differences between stage 1 and stage 2:
1. Initialization. Stage 2 inherits the values of some variables from stage 1, so they are
initialized in stage 1 but not in stage 2.
2. cviol. cviol is updated after at iteration in stage 1, while it remains a constant in stage2.
3. sdirn. See the definition of sdirn in the code for details.
4. optnew. The two stages have different objectives, so optnew is updated differently.
5. step. step <= cviol in stage 1.
r   r   r   Nr   i'  d      )#r   r   r   r   r   r   r   r    r!   minimumlinspacer   r   appendeyeisnan	nanargmaxlenr   r   r   r   r   r   anylogical_andminmaximumr   r   r	   sqrtr
   argminr   )&r)   r*   stager"   r#   r$   r+   r(   r,   zdasavvmultdzdotamconr'   cvioliconr&   sdirnkoptoldnactoldnfailmaxiteriteroptnewnactsavr/   fracmultfracddsssdsqrtdstepdnewcvshiftcvsabsdolds&                                         r2   r   r   t   s    XXaggaj!FXXbggfo&FHHRWWQ]#E 771a=Dwwq!}H y1}}zUaZ''	eqjdaiEQJGGwwqzT!!!wwt}$$$wwv$&&&wwqzX%%%wwq!}(RWWQ]h-FFFqyyA:r{{1~&&299>>!+<E	+III19D)C!CCCvet}%***
 z{{1d1fd#6HHXryyQB'(FF819
q!++rxx{q!++<<#DQ !<5;&q!++Av!VAv(ax ryyGA4D_4D1D/E$FK[OI\$\]^<A$KHKqF1QT7AaajM2KHE%4L FGE jjC$B BCGg9v{####A:FADF|,F F?dWnGEQJEFF+A: <!%4LF5DMG(1dj=)91TJNAd w{" 1H%-3F^Q->F$q&>*+/a+?DQ(%&F6N !%Qq$u+~%6!TZ-8H!AuPTuH+W]^c_cWd eu2>>&$-!*;T%4[O=[\]]  %'D! w|  }A  wB  C  wBqr6!9q=T!WP_E_F1IfQi/ell  wB  C8ET?+ "

288Cu4F+GPUQUY]^dejfj^kYkIk lu 88E(O,,E(O0DQ0N+,d7dQh'()-ax.>)?D1H%& zd!8n:19"-a4;.?%4LRVYZRZ"[$< *.AvtAv.>)?1fd1f%&+16462B+CQQ'( xx1f&&#eFm*<Q*F
 z6%1d6l?);<q@%Q-OQRSTVZ[\V\S\Q]]]5a=(1d1f95 *!AtET{N*;QetdSOAUd| ?!VD 1>4:>DdO D&a"5 Dv| DFAID zdQhax88Eq&M**c%Q-.@CF.J zqDz2Qq$wZ?? 5a=(1QQY<7 [6!<'E5!E17bC%K--"BGGBrEBrEM*CGRWWR"W5EF6$DBJ"$D19BKK--0 A:ud##tU#D 4%<A:FF299Qa4;6G(H1RWSW[>(YZ[E a4;/qETE{E%4LQQuA: F6N3F6N741d74qw>?TC!T'
O4s17|CeK,-()#D.D ^ccfgmcn]op]oXYQF1Ivay6!945GS]opyy1h/014299Q)* XqL4$;&AD&04;>?HSV,--"++hs6{>S2T2TAA: FF299Q(9:;E!8t| E R ywwt}$$$wwv$&&&6Q;wwqzX%%%2;;q>""""yy~~a AI---wwq!}(RWWQ]h-FFFqyTRZZ%????q!##C IJ Ch qs   'r	-r*rc                 ^   [         (       a`  Xs=:  a  S:  d   e   eSUs=::  a  Us=::  a  S:  d   e   eSUs=:  a  Ss=:  a  U:  d   e   e[        R                  " U5      (       a   eXb::  a  XA-  nO$Xc::  a  [        X@-  U5      nO[        X@-  XQ-  5      n[         (       a  US:  d   eU$ )zM
This function updates the trust region radius according to RATIO and DNORM.
r   r   )r   r   r=   r   )delta_indnormeta1eta2gamma1gamma2ratior$   s           r2   trradri     s     y$1$$$$$D$D$1$$$$$6&A&&&&&& 88E??"" } 
F%u-F%v~6( yqyyLr4   )__doc__numpyr   numpy.typingtypingnptcommon.constsr   r   r   r   common.powalgr   r	   common.linalgr
   r   r   r   r   r3   NDArrayr   r   ri    r4   r2   <module>rt      sH      < < 4 D D`DE$S[[ E$ E$P
0r4   