§
    fŠtj�  ã                   óJ   — d Z ddlZddlmZmZmZmZmZ ddl	m
Z
mZ d„ Zd„ ZdS )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                 ó¦  — |j         d         }|}t          | |¦  «        }t          t          | ¦  «        t          |¦  «        ¦  «        }t          j        d„ t          ||¦  «        D ¦   «         ¦  «        }t          |dz
  |dz
  d¦  «        D ]}t          ||dz            ¦  «        dk    rat          |||dz   …         ¦  «        }	t          |dd…||dz   gf         |	j        ¦  «        |dd…||dz   gf<   t          |||dz   …         Ž ||<   Œ€||k     rBt          ||         ¦  «        t          dz  k    r!t          ||         ||         ¦  «        s|dz  }|dz
  dk    r|dz
  |k     r||dz
           ||dz
  <   t          rO||cxk    rt          |dz   |¦  «        k    sn J ‚|t          |¦  «        cxk    r|k    sn J ‚|j         ||fk    sJ ‚|||fS )a�  
    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   c                 ó<   — g | ]\  }}t          ||¦  «        rd n|‘ŒS )r   )r   )Ú.0ÚcqiÚcqais      ú^/var/www/html/CA-Chatbot/venv/lib/python3.11/site-packages/scipy/_lib/pyprima/common/powalg.pyú
<listcomp>zqradd_Rdiag.<locals>.<listcomp>-   s/   € ÐRÐRÐR¹	¸¸T�  TÑ*Ô*Ð3�1�1°ÐRÐRÐRó    é   éÿÿÿÿr   N)Úshaper   ÚabsÚnpÚarrayÚzipÚranger   ÚTr   r
   r   r	   ÚminÚlen)
ÚcÚQÚRdiagÚnÚmÚnsaveÚcqÚcqaÚkÚGs
             r   Úqradd_Rdiagr(      s  € ð( 	
Œ�Œ
€AØ€Eõ 
��A‰Œ€BÝ
•#�a‘&”&�#˜a™&œ&Ñ
!Ô
!€Cõ 
ŒÐRÐRÅSÈÈSÁ\Ä\ÐRÑRÔRÑ	SÔ	S€Bõ
 �1�Q‘3˜˜!™˜RÑ Ô ð &ð &ˆÝˆr�!�A‘#Œw‰<Œ<˜!ÒÐõ ˜˜A˜a ™c˜EœÑ#Ô#ˆAÝ$ Q q q q¨1¨a°©c¨( {¤^°Q´SÑ9Ô9ˆAˆaˆaˆa�!�Q�q‘S�ˆk‰NÝ˜2˜a  !¡˜eœ9Ð%ˆBˆq‰Eøð 	ˆ1‚u€uåˆr�!Œu‰:Œ:�˜Q™ÒÐ¥w¨r°!¬u°c¸!´fÑ'=Ô'=ÐØ�‰FˆAð 	ˆ1�u�‚z€z�a˜!‘e˜a’i�iØ˜!˜a™%”yˆˆa�!‰e‰åð !Ø˜Ð.Ð.Ò.Ð.�S ¨¡¨AÑ.Ô.Ò.Ð.Ð.Ð.Ð.Ð.Ø•C˜‘J”JÐ#Ð#Ò#Ð# !Ò#Ð#Ð#Ð#Ð#Ð#ØŒw˜1˜a˜&Ò Ð Ð Ð àˆe�Qˆ;Ðr   c                 óÂ  ‡ ‡— ‰ j         \  }}|dk    r||k    sJ ‚|dk    r||k     sJ ‚t          |¦  «        |k    sJ ‚‰j         d         |k    r"‰j         d         |k    r‰j         d         |k    sJ ‚|dk     s||k    r‰|fS t          ||dz
  ¦  «        D ]q}t          ||dz            t	          ‰dd…|f         ‰ dd…|dz   f         ¦  «        g¦  «        }t          ‰dd…|dz   |gf         |j        ¦  «        ‰dd…||dz   gf<   Œrˆ ˆfd„t          ||dz
  ¦  «        D ¦   «         |||dz
  …<   t	          ‰dd…|dz
  f         ‰ dd…|f         ¦  «        ||dz
  <   ‰|fS )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   Nc           	      ó^   •— g | ])}t          ‰d d …|f         ‰d d …|dz   f         ¦  «        ‘Œ*S )Nr   )r   )r   r&   ÚAr   s     €€r   r   zqrexc_Rdiag.<locals>.<listcomp>€   s?   ø€ ÐFÐFÐF°1•F˜1˜Q˜Q˜Q ˜Tœ7 A a a a¨¨1© f¤IÑ.Ô.ÐFÐFÐFr   )r   r   r   r   r   r   r   )r+   r   r    Úir"   r!   r&   r'   s   ``      r   Úqrexc_Rdiagr-   L   s©  øø€ ð Œ7�D€A€qð �Š6ˆ6�a˜1’f�f�fÐØ�Š6ˆ6�a˜!’e�e�eÐÝˆu‰:Œ:˜Š?ˆ?ˆ?ˆ?ØŒ7�1Œ:˜Š?ˆ?˜qœw qœz¨Qš˜°1´7¸1´:À²?°?°?ÐBð
 	ˆ1‚u€u��Q’�Ø�%ˆxˆõ �1�a˜‘c‰]Œ]ð 8ð 8ˆÝ�e˜A˜a™C”j¥&¨¨1¨1¨1¨a¨4¬°!°A°A°A°q¸±s°F´)Ñ"<Ô"<Ð=Ñ>Ô>ˆÝ   1 1 1 q¨¡s¨A h ;¤°!´#Ñ7Ô7ˆˆ!ˆ!ˆ!ˆa��1‘ˆXˆ+‰ˆð GÐFÐFÐFÐF½¸aÀÀ1Á¹¼ÐFÑFÔF€Eˆ!ˆAˆa‰Cˆ%�LÝ˜˜!˜!˜!˜Q˜q™S˜&œ	 1 Q Q Q¨ T¤7Ñ+Ô+€Eˆ!ˆA‰#�Jàˆeˆ8€Or   )Ú__doc__Únumpyr   Úlinalgr   r   r   r   r   Úconstsr	   r
   r(   r-   © r   r   ú<module>r3      sˆ   ððð ð Ð Ð Ð Ø =Ð =Ð =Ð =Ð =Ð =Ð =Ð =Ð =Ð =Ð =Ð =Ð =Ð =Ø "Ð "Ð "Ð "Ð "Ð "Ð "Ð "ð9ð 9ð 9ðx7ð 7ð 7ð 7ð 7r   