
    -i7                         S r SSKrSSKJrJrJrJr  SSKJr  Sr	S r
S rS	 rS
 rS rS rS rS rS rSS jrSS jrS rS rSS jrS rS rS rSS jrSS jrS rg)z
This module provides some basic 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   )	DEBUGGINGEPSREALMAXREALMIN)presentFc                     [         (       d  [        R                  " X5      $ Sn[        [	        U 5      5       H  nX U   X   -  -  nM     U$ )Nr   )USE_NAIVE_MATHnpdotrangelenxyresultis       S/var/www/html/venv/lib/python3.13/site-packages/scipy/_lib/pyprima/common/linalg.pyinprodr      sC    >vva|F3q6]A$+ M    c                     [         R                  " UR                  S   5      n[        UR                  S   5       H  n[	        XS S 2U4   5      X#'   M     U$ )Nr   )r   zerosshaper   r   r   s       r   	matprod12r   %   sH    XXaggaj!F1771:11g&	 Mr   c                     [         R                  " U R                  S   5      n[        U R                  S   5       H  nX S S 2U4   X   -  -  nM     U$ Nr   r   r   r   r   r   r   s       r   	matprod21r   ,   sJ    XXaggaj!F1771:AqD'AD.  Mr   c           	      $   [         R                  " U R                  S   UR                  S   45      n[        UR                  S   5       HA  n[        U R                  S   5       H"  nUS S 2U4==   U S S 2U4   XU4   -  -  ss'   M$     MC     U$ r   r   )r   r   r   r   js        r   	matprod22r!   3   s    XXqwwqz1771:./F1771:qwwqz"A1a4LAadGa1g--L #  Mr   c                 R   [         (       d  X-  $ [        U R                  5      S:X  a$  [        UR                  5      S:X  a  [        X5      $ [        U R                  5      S:X  a$  [        UR                  5      S:X  a  [	        X5      $ [        U R                  5      S:X  a$  [        UR                  5      S:X  a  [        X5      $ [        U R                  5      S:X  a$  [        UR                  5      S:X  a  [        X5      $ [        SU R                   SUR                   35      e)Nr      zInvalid shapes for x and y: z and )r
   r   r   r   r   r   r!   
ValueError)r   r   s     r   matprodr%   ;   s    >s

177|qS\Q.a|	QWW	s177|q0	QWW	s177|q0	QWW	s177|q07yaggYOPPr   c                     [         (       d  [        R                  " X5      $ [        R                  " [	        U 5      [	        U5      45      n[        [	        U 5      5       H  nXU   -  US S 2U4'   M     U$ N)r
   r   outerr   r   r   r   s       r   outprodr)   J   sZ    >xx~XXs1vs1v&'F3q6]t8F1a4L Mr   c           	      $   [         (       d!  [        R                  R                  XS S9S   $ U R                  S   nU R                  S   n[        XE5      n[        R                  " U5      nUR                  5       n[        US-
  SS5       H  n	[        XS S 2U	4   5      n
[        [        R                  " U5      [        R                  " US S 2U	4   5      5      n[        X5      (       a  SXy'   Mf  XU	   -  Xy'   XU	   U S S 2U	4   -  -
  nM     U$ )N)rcondr   r   )r
   r   linalglstsqr   minr   copyr   r   absisminor)AbQRdiagmnrankr   r   r   yqyqas               r   lsqrr<   S   s    >yyq4033	
A	
Aq9D
A	A4!8R$AAwRVVAYqAw02ADa=ADaD1QT7N"A % Hr   c                    [         (       d  [        R                  " X5      $ [        R                  " U 5      (       d  [	        U 5      nU$ [        R                  " U5      (       d  [	        U5      nU$ [	        [        R
                  " X/5      5      n[        R
                  " [        U5      [        U5      /5      nUS   [        R                  " [        5      :  aG  US   [        R                  " [        S-  5      :  a#  [        R                  " [        X3-  5      5      nU$ US   S:  a6  US   [        R                  " US   US   -  US   US   -  -  S-   5      -  nU$ SnU$ )Nr   r    @)r
   r   hypotisfiniter1   arrayr/   maxsqrtr   r   sum)x1x2rr   s       r   r?   r?   j   s%   >xx;;r??G H [[__G H ""#HHc!fc!f%&Q4"'''""qtbgggck.B'BAC!A
 H	 qTAX!rww!QqT	AaD1I6:;;A H AHr   c           	          [         (       d  [        R                  R                  U 5      $ [        R                  " [        U  Vs/ s H  oU-  PM	     sn5      5      nU$ s  snf r'   )r
   r   r-   normrC   rD   )r   xir   s      r   rI   rI   }   sJ    >yy~~a   WWS!,!BR%!,-.FM -s   Ac                 p    [        [        U 5      [        R                  " [        U 5      5      -
  5      U:*  $ r'   )primasumr1   r   trilr3   tols     r   istrilrP      '    CFRWWSV_,-44r   c                 p    [        [        U 5      [        R                  " [        U 5      5      -
  5      U:*  $ r'   )rL   r1   r   triurN   s     r   istriurT      rQ   r   c                 $   [         (       d  [        R                  R                  U 5      $ U R	                  5       n U R
                  S   n[        U 5      (       ay  U R                  n[        R                  " X45      n[        U5       H;  nSX$U4   -  X4U4'   [        US U2S U24   US U2U4   5      * X$U4   -  US U2U4'   M=     UR                  $ [        U 5      (       ac  [        R                  " X45      n[        U5       H;  nSXU4   -  X4U4'   [        US U2S U24   U S U2U4   5      * XU4   -  US U2U4'   M=     U$ [        U 5      u  pRnUR                  n[        R                  " X45      n[        US-
  SS5       H<  nUS S 2U4   [        US S 2US-   U24   X$S-   U2U4   5      -
  X$U4   -  US S 2U4'   M>     [        R                  " U[        S9n[        R                  " SUS-
  U5      Xv'   US S 2U4   R                  nU$ )Nr   r   r,   dtype)r
   r   r-   invr0   r   rP   Tr   r   r%   rT   qrintlinspace)r3   r8   RBr   r5   PInvPs           r   rX   rX      s   >yy}}Q	A	
AayyCCHHaVqA!qD'kAdG"1"bqb&	1RaRU844qAw>Abqb!eH  ss
	HHaVqA!qD'kAdG"1"bqb&	1RaRU844qAw>Abqb!eH  H Q%aCCHHaVq1ub"%AAw1a!eAg:a%'1*!FF!qD'QAadG &xx%++a1a(agJLLHr   c                    U R                   S   nU R                   S   n[        R                  " U5      nU R                  n[        R                  " SUS-
  U[
        S9n[        U5       GH  n[        R                  " [        [        XFUS-   2XaS-   24   5      SS9SS9nUS:  a.  XrU-
  S-
  ::  a#  Xv-  nXW   XV   sXV'   XW'   XGU/S S 24   XFU/S S 24'   [        US-
  US5       H  n[        XFXh/4   5      R                  n	[        R                  " [        XFU4   XFU4   5      S5      XFXh/4'   [        XFS-   US-   2Xh/4   U	5      XFS-   US-   2Xh/4'   [        US S 2Xh/4   U	5      US S 2Xh/4'   M     GM     UR                  n
X:U4$ )Nr   r   rV   axisr,   )r   r   eyerY   r\   r[   r   argmaxrL   	primapow2planerotappendr?   r%   )r3   r7   r8   r5   rY   r_   r    kr   Gr]   s              r   rZ   rZ      s   	
A	
A
q	A	A
AqsAS)A1XIIhyQqS5!aC%<9BKq5Qa%!)^FAqtJAD!$VQY<A!faiLqsAr"Aqf9&((A99U1T7AdG%<a@A!iL%,Q1uQU{QF/B-CQ%GA!eAEkA6!""1QY<3Aa!iL	 #  	
A7Nr   c                    [         (       d  [        R                  " XS9$ Uc.  U R                  S:X  a  [        [	        U SS95      $ [        U 5      $ US:X  aX  [        R
                  " U R                  S   5      n[        U R                  S   5       H  n[        U SS2U4   5      X#'   M     U$ US:X  aW  [        R
                  " U R                  S   5      n[        U R                  S   5       H  n[        XSS24   5      X#'   M     U$ g)z
According to its documentation, np.sum will sometimes do partial pairwise summation.
For our purposes, when comparing, we want don't want to do anything fancy, and we
just want to add things up one at a time.
rb   Nr#   r   r   )r
   r   rD   ndimrL   r   r   r   )r   rc   r   r   s       r   rL   rL      s     >vva##|66Q;x*++q6M	!''!*%qwwqz"AAadGFI #	!''!*%qwwqz"AAdGFI #	 
r   c                 
    X -  $ )a,  
Believe it or now, x**2 is not always the same as x*x in Python. In Fortran they
appear to be identical. Here's a quick one-line to find an example on your system
(well, two liner after importing numpy):
list(filter(lambda x: x[1], [(x:=np.random.random(), x**2 - x*x != 0) for _ in range(10000)]))
 )r   s    r   rf   rf      s     3Jr   c           
      F	   [         (       a  [        U 5      S:X  d   S5       e[        [        R                  " U 5      5      (       a  SnSnGO[        [        R                  " U 5      5      (       ah  S[        R                  " S5      -  [        R                  " U S   5      -  nS[        R                  " S5      -  [        R                  " U S   5      -  nGO.[        U S   5      S::  a  [        U S   5      S::  a  SnSnGO[        U S   5      [        [        U S   5      -  ::  a  [        R                  " U S   5      nSnGO[        U S   5      [        [        U S   5      -  ::  a  Sn[        R                  " U S   5      nGO[        [        R                  " [        R                  " [        5      [        R                  " U 5      :  [        R                  " U 5      [        R                  " [        S-  5      :  5      5      (       a  [        U 5      nU S   U-  nU S   U-  nO[        U S   5      [        U S   5      :  a`  U S   U S   -  n[        S[        U5      [        R                  " SXD-  -   5      5      nU[        R                  " U S   5      -  nSU-  nXE-  nO`U S   U S   -  n[        S[        U5      [        R                  " SXD-  -   5      /5      nU[        R                  " U S   5      -  nXE-  nSU-  n[        R                   " X/U* U//5      n[         (       Gas  UR"                  S:X  d   e[        R
                  " [        R$                  " U5      5      (       d   e[        US   US   -
  5      [        US	   US
   -   5      -   S::  d   e[        R&                  " S[        R(                  " SS[        -  5      5      n[+        Xg5      (       d   e[        [        R                  " [        R$                  " U 5      [        R                  " U 5      [        R                  " [        S-  5      :  5      5      (       aQ  [        R,                  R                  U 5      n[        [        X`-  US/-
  5      5      [        XwU-  5      ::  d   S5       eU$ )a(  
As in MATLAB, planerot(x) returns a 2x2 Givens matrix G for x in R2 so that Y=G@x has Y[1] = 0.
Roughly speaking, G = np.array([[x[0]/R, x[1]/R], [-x[1]/R, x[0]/R]]), where R = np.linalg.norm(x).
0. We need to take care of the possibilities of R=0, Inf, NaN, and over/underflow.
1. The G defined above is continuous with respect to X except at 0. Following this definition,
G = np.array([[np.sign(x[0]), 0], [0, np.sign(x[0])]]) if x[1] == 0,
G = np.array([[0, np.sign(x[1])], [np.sign(x[1]), 0]]) if x[0] == 0
Yet some implementations ignore the signs, leading to discontinuity and numerical instability.
2. Difference from MATLAB: if x contains NaN of consists of only Inf, MATLAB returns a NaN matrix,
but we return an identity matrix or a matrix of +/-np.sqrt(2). We intend to keep G always orthogonal.
r#   zx must be a 2-vectorr   r   r>   )r#   r#   )r   r   )r   r   )r   r   )r   r   g|=皙?g    .AzG @ X = [||X||, 0])r   r   anyr   isnanallisinfrC   signr1   r   logical_andr   r   rI   rB   rA   r   r@   maximumminimumisorthr-   )r   csrG   turj   rO   s           r   rg   rg      s    y1v{222{ 	BHHQK
bhhqk


NRWWQqT]*
NRWWQqT]*
ad)q.S1Y!^
ad)sS1Y
& GGAaDM
ad)sS1Y
& GGAaDM rwww/"&&);RVVAYQX[^Q^I_=_`aaQA!qA!qA!A$i#ad)#!qtAAs1vrwwq13w/0A1AAAA!qtAQAAC 012A1AAAA
1&A2q'"#A yww%vvbkk!n%%%%1T7QtW$%AdGag,=(>>!CCCjj"**VUS["ABa~~~r~~bkk!nbffQi"'''C-:P.PQRR		q!As13!Q<()SAg->>T@TT>Hr   c                     Sn[        U5      U[        U 5      -  -   n[        U5      SU-  [        U 5      -  -   n[        R                  " [        U5      U:  X4:  5      $ )a  
This function tests whether x is minor compared to ref. It is used by Powell, e.g., in COBYLA.
In precise arithmetic, isminor(x, ref) is true if and only if x == 0; in floating point
arithmetic, isminor(x, ref) is true if x is 0 or its nonzero value can be attributed to
computer rounding errors according to ref.
Larger sensitivity means the function is more strict/precise, the value 0.1 being due to Powell.

For example:
isminor(1e-20, 1e300) -> True, because in floating point arithmetic 1e-20 cannot be added to
1e300 without being rounded to 1e300.
isminor(1e300, 1e-20) -> False, because in floating point arithmetic adding 1e300 to 1e-20
dominates the latter number.
isminor(3, 4) -> False, because 3 can be added to 4 without being rounded off
rp   r#   )r1   r   
logical_or)r   refsensitivityrefarefbs        r   r2   r2   E  sW      Ks8kCF**Ds8a+oA..D==ST)4<88r   c                 8   [         R                  " U S5      n[        (       a  [         R                  " U S5      [         R                  " U S5      :X  d   e[         R                  " US5      [         R                  " US5      :X  d   e[         R                  " U S5      [         R                  " US5      :X  d   e[        U5      (       a  US:  d   e[        U5      (       a  UO_[         R                  " SS[
        -  [         R                  " [         R                  " U S5      [         R                  " U S5      5      -  5      n[         R                  " X"[         R                  " [        U 5      5      -  U[         R                  " [        U5      5      -  /5      n[        [        X5      5      [         R                  " U5      -
  U:*  R                  5       =(       d<    [        [        X5      [         R                  " U5      -
  5      U:*  R                  5       nU$ )zB
This procedure tests whether A = B^{-1} up to the tolerance TOL.
r   r   gMbP?g      Y@)r   sizer   r   rx   r   rw   rB   r1   r%   rd   rs   )r3   r^   rO   r8   is_invs        r   isinvr   [  st    	1A ywwq!}1---wwq!}1---wwq!}1---3<<!8O8 #2::dC#I

277STVW=Z\ZaZabcefZg@h4h#iC
&&#RVVCF^+S266#a&>-AB
CC71=!BFF1I-#5::<o#gamVXV\V\]^V_F_B`ehAh@m@m@oF
 Mr   c           
         [         (       a  [        U5      (       a  US:  d   e[        R                  " U S5      nU[        R                  " U S5      :  a  SnU$ [        R                  " [        [        U 5      5      5      (       a  SnU$ [        U5      (       a}  [        [        U R                  U 5      [        R                  " U5      -
  5      [        R                  " X[        R                  " [        U 5      5      -  5      :*  R                  5       nU$ [        [        U R                  U 5      [        R                  " U5      -
  5      S:*  R                  5       nU$ )z[
This function tests whether the matrix A has orthonormal columns up to the tolerance TOL.
r   r   F)r   r   r   r   rr   rL   r1   r%   rY   rd   rw   rB   rs   )r3   rO   num_varsis_orths       r   ry   ry   y  s    y3<<!8O8 wwq!}H"''!Q- N ((8CF#
$
$ N 3<<7133?RVVH-==>"**SXZX^X^_bcd_eXfRfBggllnG N 7133?RVVH-==>!CHHJG
 Nr   c                      [        U 5      S:X  a  [        S5      e[        S U  5       5      n[        S U  5       5      nS[        -  [        US5      -  U-  $ )a  
Get a relative tolerance for a set of arrays. Borrowed from COBYQA

Parameters
----------
*arrays: tuple
    Set of `numpy.ndarray` to get the tolerance for.

Returns
-------
float
    Relative tolerance for the set of arrays.

Raises
------
ValueError
    If no array is provided.
r   z$At least one array must be provided.c              3   8   #    U  H  oR                   v   M     g 7fr'   )r   .0rA   s     r   	<genexpr>!get_arrays_tol.<locals>.<genexpr>  s     .vezzvs   c           	   3      #    U  HE  n[         R                  " [         R                  " U[         R                  " U5         5      S S9v   MG     g7f)      ?)initialN)r   rB   r1   r@   r   s     r   r   r     s9      E 	rvveBKK./0#>s   AAg      $@r   )r   r$   rB   r   )arraysr   weights      r   get_arrays_tolr     s_    & 6{a?@@.v..D  F #:D#&//r   )r   r'   )__doc__numpyr   constsr   r   r   r   r   r
   r   r   r   r!   r%   r)   r<   r?   rI   rP   rT   rX   rZ   rL   rf   rg   r2   r   ry   r   rn   r   r   <module>r      s     4 4  Q.&55>24Xv9,<@0r   