+
    /i7                         R t ^ RIt^RIHtHtHtHt ^RIHt Rt	R t
R tR tR tR	 tR
 tR tR tR tRR ltRR ltR tR tRR ltR tR tR tRR ltRR ltR tR# )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                     \         '       g   \        P                  ! W4      # ^ p\        \	        V 4      4       F  pW V,          W,          ,          ,          pK!  	  V#     )USE_NAIVE_MATHnpdotrangelenxyresultis   &&  ^/var/www/html/photoedit/myenv/lib/python3.14/site-packages/scipy/_lib/pyprima/common/linalg.pyinprodr      sC    >vva|F3q6]A$+ M    c                     \         P                  ! VP                  ^,          4      p\        VP                  ^,          4       F  p\	        WRV3,          4      W#&   K  	  V# )   NNN)r   zerosshaper   r   r   s   &&  r   	matprod12r   %   sH    XXaggaj!F1771:11g&	 Mr   c                     \         P                  ! V P                  ^ ,          4      p\        V P                  ^,          4       F!  pW RV3,          W,          ,          ,          pK#  	  V# r	   r   r   r   r   r   r   s   &&  r   	matprod21r    ,   sJ    XXaggaj!F1771:AqD'AD.  Mr   c           	      d   \         P                  ! V P                  ^ ,          VP                  ^,          34      p\        VP                  ^,          4       FU  p\        V P                  ^,          4       F2  pVRV3;;,          V RV3,          WV3,          ,          ,          uu&   K4  	  KW  	  V# r   r   )r   r   r   r   js   &&   r   	matprod22r#   3   s    XXqwwqz1771:./F1771:qwwqz"A1a4LAadGa1g--L #  Mr   c                 l   \         '       g	   W,          # \        V P                  4      ^8X  d&   \        VP                  4      ^8X  d   \        W4      # \        V P                  4      ^8X  d&   \        VP                  4      ^8X  d   \	        W4      # \        V P                  4      ^8X  d&   \        VP                  4      ^8X  d   \        W4      # \        V P                  4      ^8X  d&   \        VP                  4      ^8X  d   \        W4      # \        RV P                   RVP                   24      h)r   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                     \         '       g   \        P                  ! W4      # \        P                  ! \	        V 4      \	        V4      34      p\        \	        V 4      4       F  pWV,          ,          VR V3&   K  	  V# )r   )r
   r   outerr   r   r   r   s   &&  r   outprodr)   J   sZ    >xx~XXs1vs1v&'F3q6]t8F1a4L Mr   c           	         \         '       g)   \        P                  P                  WR R7      ^ ,          # V P                  ^ ,          pV P                  ^,          p\        WE4      p\        P                  ! V4      pVP                  4       p\        V^,
          RR4       F  p	\        WRV	3,          4      p
\        \        P                  ! V4      \        P                  ! VRV	3,          4      4      p\        W4      '       d   ^ Wy&   Kk  WV	,          ,          Wy&   WV	,          V RV	3,          ,          ,
          pK  	  V# )N)rcondr   )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                 R   \         '       g   \        P                  ! W4      # \        P                  ! V 4      '       g   \	        V 4      pV# \        P                  ! V4      '       g   \	        V4      pV# \	        \        P
                  ! W.4      4      p\        P
                  ! \        V4      \        V4      .4      pV^ ,          \        P                  ! \        4      8  dU   V^,          \        P                  ! \        R,          4      8  d(   \        P                  ! \        W3,          4      4      pV# V^,          ^ 8  d_   V^,          \        P                  ! V^ ,          V^,          ,          V^ ,          V^,          ,          ,          ^,           4      ,          pV# ^ pV# )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           	          \         '       g    \        P                  P                  V 4      # \        P                  ! \        V  Uu. uF  qV,          NK  	  up4      4      pV# u upi N)r
   r   r-   normrC   rD   )r   xir   s   &  r   rJ   rJ   }   sL    >yy~~a   WWS!,!BR%%!,-.FM -s   A#c                 x    \        \        V 4      \        P                  ! \        V 4      4      ,
          4      V8*  # rI   )primasumr1   r   trilr3   tols   &&r   istrilrQ      '    CFRWWSV_,-44r   c                 x    \        \        V 4      \        P                  ! \        V 4      4      ,
          4      V8*  # rI   )rM   r1   r   triurO   s   &&r   istriurU      rR   r   c                    \         '       g    \        P                  P                  V 4      # V P	                  4       p V P
                  ^ ,          p\        V 4      '       d   V P                  p\        P                  ! W34      p\        V4       FS  p^W$V3,          ,          W4V3&   \        VRV1RV13,          VRV1V3,          4      ) W$V3,          ,          VRV1V3&   KU  	  VP                  # \        V 4      '       d|   \        P                  ! W34      p\        V4       FS  p^WV3,          ,          W4V3&   \        VRV1RV13,          V RV1V3,          4      ) WV3,          ,          VRV1V3&   KU  	  V# \        V 4      w  rRpVP                  p\        P                  ! W34      p\        V^,
          RR4       FV  pVRV3,          \        VRV^,           V13,          W$^,           V1V3,          4      ,
          W$V3,          ,          VRV3&   KX  	  \        P                  ! V\        R7      p\        P                  ! ^ V^,
          V4      Wv&   VRV3,          P                  pV# )r	   Nr   dtyper,   )r
   r   r-   invr0   r   rQ   Tr   r   r&   rU   qrintlinspace)r3   r8   RBr   r5   PInvPs   &       r   rY   rY      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                    V P                   ^ ,          pV P                   ^,          p\        P                  ! V4      pV P                  p\        P                  ! ^ V^,
          V\
        R7      p\        V4       EFX  p\        P                  ! \        \        WFV^,           1Wa^,           13,          4      ^R7      ^ R7      pV^ 8  dD   WrV,
          ^,
          8:  d0   Wv,          pWW,          WV,          uWV&   WW&   WGV.R3,          WFV.R3&   \        V^,
          VR4       F  p\        WFWh.3,          4      P                  p	\        P                  ! \        WFV3,          WFV3,          4      ^ 4      WFWh.3&   \        WF^,           V^,           1Wh.3,          V	4      WF^,           V^,           1Wh.3&   \        VRWh.3,          V	4      VRWh.3&   K  	  EK[  	  VP                  p
W:V3# )r	   rW   axisr   r,   )r   r   eyerZ   r]   r\   r   argmaxrM   	primapow2planerotappendr?   r&   )r3   r7   r8   r5   rZ   r`   r"   kr   Gr^   s   &          r   r[   r[      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                Z   \         '       g   \        P                  ! WR7      # Vf3   V P                  ^8X  d   \        \	        V ^ R7      4      # \        V 4      # V^ 8X  dc   \        P
                  ! V P                  ^,          4      p\        V P                  ^,          4       F  p\        V RV3,          4      W#&   K  	  V# V^8X  db   \        P
                  ! V P                  ^ ,          4      p\        V P                  ^ ,          4       F  p\        WR3,          4      W#&   K  	  V# R# )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.
rc   Nr   )r
   r   rD   ndimrM   r   r   r   )r   rd   r   r   s   &&  r   rM   rM      s     >vva##|66Q;x*++q6M	!''!*%qwwqz"AAadGFI #	!''!*%qwwqz"AAdGFI #	 
r   c                    W ,          # )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   rg   rg      s     3Jr   c           
        \         '       d   \        V 4      ^8X  g   Q R4       h\        \        P                  ! V 4      4      '       d   ^p^ pEMf\        \        P                  ! V 4      4      '       d   ^\        P                  ! ^4      ,          \        P                  ! V ^ ,          4      ,          p^\        P                  ! ^4      ,          \        P                  ! V ^,          4      ,          pEM\        V ^ ,          4      ^ 8:  d   \        V ^,          4      ^ 8:  d   ^p^ pEM\        V ^,          4      \        \        V ^ ,          4      ,          8:  d"   \        P                  ! V ^ ,          4      p^ pEM:\        V ^ ,          4      \        \        V ^,          4      ,          8:  d"   ^ p\        P                  ! V ^,          4      pEM\        \        P                  ! \        P                  ! \        4      \        P                  ! V 4      8  \        P                  ! V 4      \        P                  ! \        R,          4      8  4      4      '       d.   \        V 4      pV ^ ,          V,          pV ^,          V,          pEM/\        V ^ ,          4      \        V ^,          4      8  d   V ^,          V ^ ,          ,          p\        ^\        V4      \        P                  ! ^WD,          ,           4      4      pV\        P                  ! V ^ ,          4      ,          p^V,          pWE,          pMV ^ ,          V ^,          ,          p\        ^\        V4      \        P                  ! ^WD,          ,           4      .4      pV\        P                  ! V ^,          4      ,          pWE,          p^V,          p\        P                   ! W.V) V..4      p\         '       Ed   VP"                  R8X  g   Q h\        P
                  ! \        P$                  ! V4      4      '       g   Q h\        VR,          VR	,          ,
          4      \        VR
,          VR,          ,           4      ,           ^ 8:  g   Q h\        P&                  ! R\        P(                  ! RR\        ,          4      4      p\+        Wg4      '       g   Q h\        \        P                  ! \        P$                  ! V 4      \        P                  ! V 4      \        P                  ! \        R,          4      8  4      4      '       d_   \        P,                  P                  V 4      p\        \        W`,          V^ .,
          4      4      \        WwV,          4      8:  g   Q R4       hV# )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.
zx must be a 2-vectorr>   g|=皙?g    .AzG @ X = [||X||, 0])   rr   )r	   r	   )r   r   )r	   r   )r   r	   )r   r   anyr   isnanallisinfrC   signr1   r   logical_andr   r   rJ   rB   rA   r   r@   maximumminimumisorthr-   )r   csrG   turk   rP   s   &       r   rh   rh      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                    Rp\        V4      V\        V 4      ,          ,           p\        V4      ^V,          \        V 4      ,          ,           p\        P                  ! \        V4      V8  W48  4      # )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
rq   )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                x   \         P                  ! V ^ 4      p\        '       d   \         P                  ! V ^ 4      \         P                  ! V ^4      8X  g   Q h\         P                  ! V^ 4      \         P                  ! V^4      8X  g   Q h\         P                  ! V ^ 4      \         P                  ! V^ 4      8X  g   Q h\        V4      '       d
   V^ 8  g   Q h\        V4      '       d   TMg\         P                  ! RR\
        ,          \         P                  ! \         P                  ! V ^ 4      \         P                  ! V ^4      4      ,          4      p\         P                  ! W"\         P                  ! \        V 4      4      ,          V\         P                  ! \        V4      4      ,          .4      p\        \        W4      4      \         P                  ! V4      ,
          V8*  P                  4       ;'       gA    \        \        W4      \         P                  ! V4      ,
          4      V8*  P                  4       pV# )zB
This procedure tests whether A = B^{-1} up to the tolerance TOL.
gMbP?g      Y@)r   sizer   r   rz   r   ry   rB   r1   r&   re   ru   )r3   r_   rP   r8   is_invs   &&&  r   isinvr   [  sw    	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o#gamVXV\V\]^V_F_B`ehAh@m@m@oF
 Mr   c           
        \         '       d   \        V4      '       d
   V^ 8  g   Q h\        P                  ! V ^4      pV\        P                  ! V ^ 4      8  d   RpV# \        P                  ! \        \        V 4      4      4      '       d   RpV# \        V4      '       d   \        \        V P                  V 4      \        P                  ! V4      ,
          4      \        P                  ! W\        P                  ! \        V 4      4      ,          4      8*  P                  4       pV# \        \        V P                  V 4      \        P                  ! V4      ,
          4      ^ 8*  P                  4       pV# )z[
This function tests whether the matrix A has orthonormal columns up to the tolerance TOL.
F)r   r   r   r   rt   rM   r1   r&   rZ   re   ry   rB   ru   )r3   rP   num_varsis_orths   &&  r   r{   r{   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                     \        V 4      ^ 8X  d   \        R4      h\        R V  4       4      p\        R V  4       4      pR\        ,          \        VR4      ,          V,          # )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.
z$At least one array must be provided.c              3   8   "   T F  qP                   x  K  	  R # 5irI   )r   .0rA   s   & r   	<genexpr>!get_arrays_tol.<locals>.<genexpr>  s     .vezzvs   c           	   3      "   T FL  p\         P                  ! \         P                  ! V\         P                  ! V4      ,          4      R R7      x  KN  	  R# 5i)      ?)initialN)r   rB   r1   r@   r   s   & r   r   r     s<      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   rI   )__doc__numpyr   constsr   r   r   r   r   r
   r   r   r    r#   r&   r)   r<   r?   rJ   rQ   rU   rY   r[   rM   rg   rh   r2   r   r{   r   ro   r   r   <module>r      s     4 4  Q.&55>24Xv9,<@0r   