
    Џkh7                         d Z ddlZddlmZmZmZmZ ddlmZ dZ	d Z
d Zd	 Zd
 Zd Zd Zd Zd Zd ZddZddZd Zd ZddZd Zd Zd ZddZddZd Zy)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                     t         st        j                  | |      S d}t        t	        |             D ]  }|| |   ||   z  z  } |S )Nr   )USE_NAIVE_MATHnpdotrangelenxyresultis       \/var/www/teggl/fontify/venv/lib/python3.12/site-packages/scipy/_lib/pyprima/common/linalg.pyinprodr      sL    vva|F3q6] !A$1+M    c                     t        j                  |j                  d         }t        |j                  d         D ]  }t	        | |d d |f         ||<    |S )Nr   )r   zerosshaper   r   r   s       r   	matprod12r   %   sQ    XXaggaj!F1771: '1a1g&q	'Mr   c                     t        j                  | j                  d         }t        | j                  d         D ]  }|| d d |f   ||   z  z  } |S Nr   r   r   r   r   r   r   s       r   	matprod21r   ,   sS    XXaggaj!F1771: !!AqD'AaD. !Mr   c           	         t        j                  | j                  d   |j                  d   f      }t        |j                  d         D ]?  }t        | j                  d         D ]"  }|d d |fxx   | d d |f   |||f   z  z  cc<   $ A |S r   r   )r   r   r   r   js        r   	matprod22r!   3   s    XXqwwqz1771:./F1771: .qwwqz" 	.A1a4LAadGa1g--L	.. Mr   c                 B   t         s| |z  S t        | j                        dk(  r$t        |j                        dk(  rt        | |      S t        | j                        dk(  r$t        |j                        dk(  rt	        | |      S t        | j                        dk(  r$t        |j                        dk(  rt        | |      S t        | j                        dk(  r$t        |j                        dk(  rt        | |      S t        d| j                   d|j                         )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A	QWW	s177|q0A	QWW	s177|q0A7yaggYOPPr   c                     t         st        j                  | |      S t        j                  t	        |       t	        |      f      }t        t	        |             D ]  }| ||   z  |d d |f<    |S N)r
   r   outerr   r   r   r   s       r   outprodr)   J   sa    xx1~XXs1vs1v&'F3q6] $qt8F1a4L$Mr   c           	         t         s%t        j                  j                  | |d       d   S | j                  d   }| j                  d   }t        ||      }t        j                  |      }|j                         }t        |dz
  dd      D ]}  }	t        ||d d |	f         }
t        t        j                  |      t        j                  |d d |	f               }t        |
|      rd||	<   a|
||	   z  ||	<   |||	   | d d |	f   z  z
  } |S )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q!4033	
A	
Aq!9D
A	A4!8R$ #AqAwRVVAYqAw02sAaDa=AaDAaD1QT7N"A# Hr   c                    t         st        j                  | |      S t        j                  |       st	        |       }|S t        j                  |      st	        |      }|S t	        t        j
                  | |g            }t        j
                  t        |      t        |      g      }|d   t        j                  t              kD  rE|d   t        j                  t        dz        k  r#t        j                  t        ||z              }|S |d   dkD  r5|d   t        j                  |d   |d   z  |d   |d   z  z  dz         z  }|S d}|S )Nr   r    @)r
   r   hypotisfiniter1   arrayr/   maxsqrtr   r   sum)x1x2rr   s       r   r?   r?   j   s%   xxB;;r?G H [[_G H "b"#HHc!fc!f%&Q4"'''""qtbgggck.B'BAaC!A
 H	 qTAX!rww!QqT	AaD1I6:;;A H AHr   c           	          t         st        j                  j                  |       S t        j                  t        | D cg c]  }||z  	 c}            }|S c c}w r'   )r
   r   r-   normrC   rD   )r   xir   s      r   rI   rI   }   sF    yy~~a   WWS!,B"R%,-.FM -s   Ac                 n    t        t        |       t        j                  t        |             z
        |k  S r'   )primasumr1   r   trilr3   tols     r   istrilrP      '    CFRWWSV_,-44r   c                 n    t        t        |       t        j                  t        |             z
        |k  S r'   )rL   r1   r   triurN   s     r   istriurT      rQ   r   c                    t         st        j                  j                  |       S | j	                         } | j
                  d   }t        |       rz| j                  }t        j                  ||f      }t        |      D ]=  }d|||f   z  |||f<   t        |d |d |f   |d ||f          |||f   z  |d ||f<   ? |j                  S t        |       rdt        j                  ||f      }t        |      D ]=  }d| ||f   z  |||f<   t        |d |d |f   | d ||f          | ||f   z  |d ||f<   ? |S t        |       \  }}}|j                  }t        j                  ||f      }t        |dz
  dd      D ]=  }|d d |f   t        |d d |dz   |f   ||dz   ||f         z
  |||f   z  |d d |f<   ? t        j                  |t              }t        j                  d|dz
  |      ||<   |d d |f   j                  }|S )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      s5   yy}}Q	A	
AayCCHHaVq 	?A!AqD'kAadG"1"bqb&	1RaRU844qAw>Abqb!eH	? ss
	HHaVq 	?A!AqD'kAadG"1"bqb&	1RaRU844qAw>Abqb!eH	? H Q%1aCCHHaVq1ub"% 	RAAw1a!eAg:!a%'1*!FF!AqD'QAadG	Rxx%++a1a(QagJLLHr   c                 B   | j                   d   }| j                   d   }t        j                  |      }| j                  }t        j                  d|dz
  |t
              }t        |      D ]!  }t        j                  t        t        |||dz   ||dz   f         d      d      }|dkD  r5|||z
  dz
  k  r*||z  }||   ||   c||<   ||<   |||gd d f   |||gd d f<   t        |dz
  |d      D ]  }t        ||||gf         j                  }	t        j                  t        |||f   |||f         d      ||||gf<   t        ||dz   |dz   ||gf   |	      ||dz   |dz   ||gf<   t        |d d ||gf   |	      |d d ||gf<    $ |j                  }
||
|fS )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 
4IIhy1QqS5!AaC%<9BKq5Q!a%!)^FA1qtJAaD!A$aVQY<Aq!faiLqsAr" 	4A1q!f9&((A99U1QT7AadG%<a@Aa!QiL%,Qq1uQU{QF/B-CQ%GAa!eAEkAq6!""1QAY<3Aa!QiL		4
4 	
Aa7Nr   c                    t         st        j                  | |      S |0| j                  dk(  rt        t	        | d            S t        |       S |dk(  rVt        j
                  | j                  d         }t        | j                  d         D ]  }t        | dd|f         ||<    |S |dk(  rVt        j
                  | j                  d         }t        | j                  d         D ]  }t        | |ddf         ||<    |S y)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     vvad##|66Q;x*++q6M	!''!*%qwwqz" 	%AAadGF1I	%	!''!*%qwwqz" 	%AAadGF1I	%	 
r   c                     | | z  S )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     Q3Jr   c           
         t         rt        |       dk(  sJ d       t        t        j                  |             rd}d}nt        t        j                  |             rddt        j                  d      z  t        j                  | d         z  }dt        j                  d      z  t        j                  | d         z  }nt        | d         dk  rt        | d         dk  rd}d}nt        | d         t        t        | d         z  k  rt        j                  | d         }d}nt        | d         t        t        | d         z  k  rd}t        j                  | d         }nut        t        j                  t        j                  t              t        j                  |       k  t        j                  |       t        j                  t        dz        k              rt        |       }| d   |z  }| d   |z  }nt        | d         t        | d         kD  r`| d   | d   z  }t        dt        |      t        j                  d||z  z               }|t        j                  | d         z  }d|z  }||z  }n`| d   | d   z  }t        dt        |      t        j                  d||z  z         g      }|t        j                  | d         z  }||z  }d|z  }t        j                   ||g| |gg      }t         r\|j"                  dk(  sJ t        j
                  t        j$                  |            sJ t        |d   |d   z
        t        |d	   |d
   z         z   dk  sJ t        j&                  dt        j(                  ddt        z              }t+        ||      sJ t        t        j                  t        j$                  |       t        j                  |       t        j                  t        dz        k              rRt        j,                  j                  |       }t        t        || z  |dgz
              t        |||z        k  sJ d       |S )aP  
    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    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QA!qA!qA!A$i#ad)#!qtAAs1vrwwq1Q3w/0A1AAAAA!qtAQAAaC 012A1AAAAA
1a&A2q'"#A ww%vvbkk!n%%%1T7QtW$%AdGag,=(>>!CCCjj"**VUS["ABa~~r~~bkk!nbffQi"'''C-:P.PQR		q!As1Q3!Q<()ScAg->>T@TT>Hr   c                     d}t        |      |t        |       z  z   }t        |      d|z  t        |       z  z   }t        j                  t        |      |k\  ||k\        S )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  sY      Ks8kCF**Ds8a+oA..D==ST)44<88r   c                    t        j                  | d      }t        rt        j                  | d      t        j                  | d      k(  sJ t        j                  |d      t        j                  |d      k(  sJ t        j                  | d      t        j                  |d      k(  sJ t        |      r|dk\  sJ t        |      r|n[t        j                  ddt
        z  t        j                  t        j                  | d      t        j                  | d            z        }t        j                  ||t        j                  t        |             z  |t        j                  t        |            z  g      }t        t        | |            t        j                  |      z
  |k  j                         xs< t        t        ||       t        j                  |      z
        |k  j                         }|S )zJ
    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   [  su    	1A wwq!}1---wwq!}1---wwq!}1---3<!8O8 #2::dC#I

277STVW=Z\ZaZabcefZg@h4h#iC
&&#sRVVCF^+S266#a&>-AB
CC71a=!BFF1I-#5::<o#gaQRmVXV\V\]^V_F_B`ehAh@m@m@oF
 Mr   c           
         t         rt        |      r|dk\  sJ t        j                  | d      }|t        j                  | d      kD  rd}|S t        j                  t        t        |                   rd}|S t        |      r{t        t        | j                  |       t        j                  |      z
        t        j                  ||t        j                  t        |             z        k  j                         }|S t        t        | j                  |       t        j                  |      z
        dk  j                         }|S )zc
    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    3<!8O8 wwq!}H"''!Q- N ((8CF#
$ N 3<7133?RVVH-==>"**SRUXZX^X^_bcd_eXfRfBggllnG N 7133?RVVH-==>!CHHJG
 Nr   c                      t        |       dk(  rt        d      t        d | D              }t        d | D              }dt        z  t        |d      z  |z  S )aS  
    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   4   K   | ]  }|j                     y wr'   )r   .0rA   s     r   	<genexpr>z!get_arrays_tol.<locals>.<genexpr>  s     .euzz.s   c           	   3      K   | ]D  }t        j                  t        j                  |t        j                  |               d        F yw)      ?)initialN)r   rB   r1   r@   r   s     r   r   z!get_arrays_tol.<locals>.<genexpr>  s=       	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   