
    -i                     H    S r SSKrSSKJrJrJrJrJr  SSK	J
r
Jr  S rS rg)a   
This module provides some Powell-style linear algebra procedures.

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   )isminorplanerotmatprodinprodhypot)	DEBUGGINGEPSc           
      d   UR                   S   nUn[        X5      n[        [        U 5      [        U5      5      n[        R                  " [        Xg5       VV	s/ s H  u  p[        X5      (       a  SOUPM     sn	n5      n[        US-
  US-
  S5       Hd  n
[        XjS-      5      S:  d  M  [        XjU
S-    5      n[        USS2XS-   /4   UR                  5      USS2XS-   /4'   [        XjU
S-    6 Xj'   Mf     X4:  a2  [        Xc   5      [        S-  :  a  [        Xc   Xs   5      (       d  US-  nUS-
  S:  a  US-
  U:  a  XcS-
     X#S-
  '   [        (       aL  XSs=::  a  [        US-   U5      ::  d   e   eU[        U5      s=::  a  U::  d   e   eUR                   XD4:X  d   eXU4$ s  sn	nf )aH  
This function updates the QR factorization of an MxN matrix A of full column rank, attempting to
add a new column C to this matrix as the LAST column while maintaining the full-rankness.
Case 1. If C is not in range(A) (theoretically, it implies N < M), then the new matrix is np.hstack([A, C])
Case 2. If C is in range(A), then the new matrix is np.hstack([A[:, :n-1], C])
N.B.:
0. Instead of R, this subroutine updates Rdiag, which is np.diag(R), with a size at most M and at
least min(m, n+1). The number is min(m, n+1) rather than min(m, n) as n may be augmented by 1 in
the function.
1. With the two cases specified as above, this function does not need A as an input.
2. The function changes only Q[:, nsave:m] (nsave is the original value of n) and
R[:, n-1] (n takes the updated value)
3. Indeed, when C is in range(A), Powell wrote in comments that "set iOUT to the index of the
constraint (here, column of A --- Zaikun) to be deleted, but branch if no suitable index can be
found". The idea is to replace a column of A by C so that the new matrix still has full rank
(such a column must exist unless C = 0). But his code essentially sets iout=n always. Maybe he
found this worked well enough in practice. Meanwhile, Powell's code includes a snippet that can
never be reached, which was probably intended to deal with the case that IOUT != n
r   r      N)shaper   absnparrayzipr   ranger   Tr   r
   r	   minlen)cQRdiagnmnsavecqcqacqicqaikGs               S/var/www/html/venv/lib/python3.13/site-packages/scipy/_lib/pyprima/common/powalg.pyqradd_Rdiagr$      s   ( 	

AE 
B
#a&#a&
!C 
S\R\	**13\R	SB
 1Q3!R rA#w<! ac#A$Qq1c({^QSS9Aa!qSkN2!9%BE ! 	uru:Qwrucf'='=FA 	1uza!eaia%y!ey.SA......CJ#!#####ww1&   Q;9 Ss    F,
c                    U R                   u  pEUS:  a  XT::  d   eUS:  a  X5:  d   e[        U5      U:X  d   eUR                   S   U:X  a&  UR                   S   U:  a  UR                   S   U::  d   eUS:  d  X5:  a  X4$ [        X5S-
  5       H]  n[        X&S-      [	        USS2U4   U SS2US-   4   5      /5      n[        USS2US-   U/4   UR                  5      USS2XfS-   /4'   M_     [        X5S-
  5       Vs/ s H   n[	        USS2U4   U SS2US-   4   5      PM"     snX#US-
  & [	        USS2US-
  4   U SS2U4   5      X%S-
  '   X4$ s  snf )a/  
This function updates the QR factorization for an MxN matrix A=Q@R so that the updated Q and
R form a QR factorization of [A_0, ..., A_{I-1}, A_{I+1}, ..., A_{N-1}, A_I] which is the matrix
obtained by rearranging columns [I, I+1, ... N-1] of A to [I+1, ..., N-1, I]. Here A is ASSUMED TO
BE OF FULL COLUMN RANK, Q is a matrix whose columns are orthogonal, and R, which is not present,
is an upper triangular matrix whose diagonal entries are nonzero. Q and R need not be square.
N.B.:
0. Instead of R, this function updates Rdiag, which is np.diag(R), the size being n.
1. With L = Q.shape[1] = R.shape[0], we have M >= L >= N. Most often L = M or N.
2. This function changes only Q[:, i:] and Rdiag[i:]
3. (NDB 20230919) In Python, i is either icon or nact - 2, whereas in FORTRAN it is either icon or nact - 1.
r   r   N)r   r   r   r   r   r   r   )Ar   r   ir   r   r!   r"   s           r#   qrexc_Rdiagr(   L   sn    77DA 6af6aeu:??771:?qwwqzQ1771:?BB
 	1ux 1c]eaCj&1a4!AqsF)"<=> 1qsAh;!##7!a1X+  9>a1F1F1QT7Aa1fI.FEAaCL!QqS&	1QT7+EA#J8O Gs   'E)__doc__numpyr   linalgr   r   r   r   r   constsr	   r
   r$   r(        r#   <module>r/      s#     = = "9x7r.   