o
    ZZhsE                    @   s  d Z ddlZddlZddlZddlZddlZddlZddlZddlZddl	Z
ddl	mZmZmZmZmZmZmZmZ ddlmZ ddlmZ ddl	mZmZmZmZ ddl	mZ ddlmZmZmZm Z m!Z! dd	l"m#Z# dd
l$m%Z%m&Z&m'Z'm(Z(m)Z)m*Z*m+Z+m,Z,m-Z-m.Z. zddl/Z	W n	 e0y   Y nw dd Z1e)Z2dddZ)dd Z3dd Z4dd Z5h dZ6G dd dZ7dd Z8e
j9:d g Z;e;e8de7dedd gd!d"gged#ed dged#e7d$edd gd!d"gged#ed dged#e7d%edd gd!d"gged#eg d&g d'ged#e7d(ed)d*gd+d,gged#ed-d)ged#e7d.ed)d*gd+d,gged#ed-d)ged#e7d/ed)d*gd+d,gged#eg d0g d1ged#e7d2e
j<d3ed#e
j<d4ed#d5hd6e7d7e
j9=d8d8e
j9=d8e7d9e
j9=d:d:e
j9=d:e7d;d:d<gd=d>ggd<d:gg
7 Z;e;e8d?e7d@eg dAg d'ged#ed dged#e7dBedd gd!d"gdCdDgged#eg dEed#e7dFeg dAg d'ged#ed dged#e7dGedd gd!d"gdCdDgged#eg dEed#e7dHeg dIg dJged#ed-d)ged#e7dKedLdMgdNdOgdPdQgged#eg dRed#e7dSeg dIg dJged#ed-d)ged#e7dTedLdMgdNdOgdPdQgged#eg dRed#e7dUeg dIg dJged#ed-d)gdVdWgged#e7dXedLdMgdNdOgdPdQgged#ed-d)gdVdWgdVdWgged#e7dYe
j9=d8dZe
j9=d8e7d[e
j9=d:d\e
j9=d:e7d]e
j9=d\d:e
j9=d\e7d^e
j9=dd>e
j9=dd5hd6e7d_e
j9=d>de
j9=d>d5hd6g7 Z;e;e8d`e7daedd gd dgged#de7dbedd gd dgged#de7dcedd*gddd:gged#de7deedd*gddd:gged#de7dfe
j<d3ed#dd5hd6e7dgd:d<gd<d:ggde7dhedd gd dggde7die
j9=d:d:dg7 Z;djdk Z>e;e> 7 Z;dldm Z?dndo Z@e;e@ 7 Z;G dpdq dqZAG drds dseAZBG dtdu dueAZCG dvdw dweAZDG dxdy dyeAZEG dzd{ d{eAZFG d|d} d}eAZGd~d ZHG dd deBeEZIG dd deIZJG dd deBeEZKG dd deKZLG dd deBeEZMG dd deMZNG dd deBeEZOG dd deOZPG dd dZQG dd deBeEZRG dd deReQZSG dd deDeGZTG dd deTeQZUG dd deBeEZVG dd deVZWG dd deBeCeEeFZXG dd deXZYG dd deDeGZZG dd deZZ[dd Z\G dd deBeEZ]G dd de]Z^G dd deBeCZ_G dd de_Z`ejabddd dD G dd dZcG dd deDeGZdG dd dZeG dd deDeGZfG dd dZgG dd dZhG dd dehZiG ddÄ dehZjG ddń dejeiZkG ddǄ dǃZlG ddɄ dehZmG dd˄ dehZnG dd̈́ dehZoG ddτ dekemZpG ddф dekenZqG ddӄ dekeoZrG ddՄ dՃZsddׄ ZtG ddل dكZuG ddۄ dۃZvG dd݄ d݃Zwdd߄ Zxejajye.dddd Zzejajye{ d:kdddd Z|ejajye.ddejaj}dd Z~G dd dZG dd dZG dd dZdd Zejajdddd Zejaje- dddd Zdd Zdd Zdd Zdd Zd d Zdd Zdd Zdd ZdS (	  z# Test functions for linalg module

    N)arraysingledoublecsinglecdoubledotidentitymatmul)swapaxes)	AxisError)multiply
atleast_2dinfasarray)linalg)matrix_powernormmatrix_rank	multi_dotLinAlgError)_multi_dot_matrix_chain_order)
assert_assert_equalassert_raisesassert_array_equalassert_almost_equalassert_allclosesuppress_warningsassert_raises_regexHAS_LAPACK64IS_WASMc                 C   s$   t | t|tjrt |u S tju S N)type
isinstancenpndarray)outZin_ r'   M/var/www/auris/lib/python3.10/site-packages/numpy/linalg/tests/test_linalg.pyconsistent_subclass#   s   r)         c                 K   s8   t | jjttfv r|}n|}t| |fd|i| d S )Ndecimal)r   dtyper"   r   r   old_assert_almost_equal)absingle_decimaldouble_decimalkwr,   r'   r'   r(   r   -   s   r   c                 C   s   t t tttt tti|  S r!   )r   r   r   r   r-   r'   r'   r(   get_real_dtype5   
   r5   c                 C   s   t ttttttti|  S r!   )r   r   r   r   r4   r'   r'   r(   get_complex_dtype:   r6   r7   c                 C   s   | t tfv rdS dS )Ngh㈵>gdy=)r   r   r4   r'   r'   r(   get_rtol?   s   r8   >   size-0generalizedstrided	hermitiansquare	nonsquarec                   @   s*   e Zd Ze fddZdd Zdd ZdS )
LinalgCasec                 C   s.   t t|t || _|| _|| _t|| _dS )z
        A bundle of arguments to be passed to a test case, with an identifying
        name, the operands a and b, and a set of tags to filter the tests
        N)r   r#   strnamer/   r0   	frozensettags)selfrA   r/   r0   rC   r'   r'   r(   __init__O   s
   zLinalgCase.__init__c                 C   s   || j | j| jd dS )zN
        Run the function `do` on this test case, expanding arguments
        rC   N)r/   r0   rC   )rD   dor'   r'   r(   checkZ   s   zLinalgCase.checkc                 C   s   d| j  dS )Nz<LinalgCase: >)rA   rD   r'   r'   r(   __repr__`   s   zLinalgCase.__repr__N)__name__
__module____qualname__setrE   rH   rK   r'   r'   r'   r(   r?   N   s    r?   c                 C   s,   | t v sJ d|D ]	}|j| hB |_q
|S )z^
    Add the given tag (a string) to each of the cases (a list of LinalgCase
    objects)
    zInvalid tag)all_tagsrC   )tagcasescaser'   r'   r(   	apply_tagd   s   rT     r=   r         ?       @      @      @r4   r   Zdouble_2)rW   rV   rY   )rX   rY         @r         ?       @y       @      @y      @      @y      @      @       @      ?r   Z	cdouble_2)r\   r[   y      ?      @)y      ?       y      ?      y      ?      Z0x0r   r   r   r9   rF   Z8x8   Z1x1   Znonarray         r>   Zsingle_nsq_1)rV   rW   rX   Zsingle_nsq_2g      @rZ   )rW   rV   rX   Zdouble_nsq_1Zdouble_nsq_2Zcsingle_nsq_1)      ?      ?       @       @      @      )y      @      y      @      "@y      @       @Zcsingle_nsq_2rd   re   rf   y      @      "y      @      y      @       @)r\   r[   rf   Zcdouble_nsq_1Zcdouble_nsq_2Zcdouble_nsq_1_2y      ?      y       @       Zcdouble_nsq_2_2Z8x11   Z1x5   Z5x1Z0x4Z4x0r<   ZhsingleZhdoubleZhcsingley       @      ZhcdoubleZhemptyZ	hnonarrayZmatrix_b_onlyZhmatrix_1x1c                  C   sn  g } t D ]}t|jtjsqt|jd|j d|j g}|jd u r%d }n|jjdkr/|j}nt|jd|j d|j g}t|j	d |||j
dhB d}| | t|jgd d d	|jj }|jd u rld }n4|jjdkrt|jgd d |jd
  d	|jjdd   }nt|jgd d d	|jj }t|j	d |||j
dhB d}| | q| S )Nra   rb   r`      r*   Z_tile3r:   rF   rb   ra   Z_tile213)CASESr#   r/   r$   r%   r   r0   ndimr?   rA   rC   appendreshapeshape)	new_casesrS   r/   r0   new_caser'   r'   r(   _make_generalized_cases   s6   


$
$
rt   c                 c   s   t | tjs| dfV  dS dg| j }d|d< | jdkr!d|d< | jdkr*d	|d
< tjt| D ]}dd t| j|D }tdd |D }tj	|| j
d}|tjd || }| |d< || j}tt|| k |dddd |D  fV  | jdkr| jd dkrt| j}d|d< tjjj| |d}|dfV  | jdkr| jd dkrt| j}d|d< tjjj| |d}|dfV  | jdkr| jdd dkrt| j}d|d< d|d< tjjj| |d}|dfV  q1dS )z<
    Generate cartesian product of strides for all axes
    ZnopNr`   )r`   rb   rk   r`   rl   ra   )r`   rv   c                 S   s   g | ]
\}}t || qS r'   )abs).0r/   r0   r'   r'   r(   
<listcomp>,  s    z%_stride_comb_iter.<locals>.<listcomp>c                 S   s   g | ]}t d d |qS r!   )slice)ry   repeatr'   r'   r(   rz   -  s    r4   l   >[= .Zstride__c                 S   s   g | ]}d | qS )z%+dr'   )ry   jr'   r'   r(   rz   6  s    r   )stridesZstride_xxx_0Zstride_xxx_0_xr`   r`   Zstride_xxx_0_0)r#   r$   r%   rn   	itertoolsproducttupleziprq   emptyr-   viewZuint32fill	__class__r   alljoinlistr   libZstride_tricksZ
as_strided)xZ
stride_setZrepeatsZ	new_shapeZslicesxisr'   r'   r(   _stride_comb_iter  sL   








r   c               	   C   sn   g } t D ]0}t|jD ](\}}t|jD ]\}}t|jd | d | |||jdhB d}| | qqq| S )Nr}   r;   rF   )rm   r   r/   r0   r?   rA   rC   ro   )rr   rS   r/   Za_labelr0   Zb_labelrs   r'   r'   r(   _make_strided_casesK  s   
r   c                   @   s"   e Zd ZeZe e fddZdS )LinalgTestCasec                 C   sv   | j D ]5}|j|@ |krq|j|@ rqz|| j W q ty8 } zd|d}|t 7 }t||d}~ww dS )zx
        Run func on each of the cases with all of the tags in require, and none
        of the tags in exclude
        zIn test case: z

N)
TEST_CASESrC   rH   rG   	Exception	traceback
format_excAssertionError)rD   requireexcluderS   emsgr'   r'   r(   check_cases_  s   


zLinalgTestCase.check_casesN)rL   rM   rN   rm   r   rO   r   r'   r'   r'   r(   r   \  s    r   c                   @      e Zd Zdd Zdd ZdS )LinalgSquareTestCasec                 C      | j dhddhd d S )Nr=   r:   r9   r   r   r   rJ   r'   r'   r(   test_sq_casesu     
z"LinalgSquareTestCase.test_sq_casesc                 C      | j ddhdhd d S )Nr=   r9   r:   r   r   rJ   r'   r'   r(   test_empty_sq_casesy     

z(LinalgSquareTestCase.test_empty_sq_casesN)rL   rM   rN   r   r   r'   r'   r'   r(   r   s      r   c                   @   r   )LinalgNonsquareTestCasec                 C   r   )Nr>   r:   r9   r   r   rJ   r'   r'   r(   test_nonsq_cases  r   z(LinalgNonsquareTestCase.test_nonsq_casesc                 C   r   )Nr>   r9   r:   r   r   rJ   r'   r'   r(   test_empty_nonsq_cases  r   z.LinalgNonsquareTestCase.test_empty_nonsq_casesN)rL   rM   rN   r   r   r'   r'   r'   r(   r   ~  r   r   c                   @   r   )HermitianTestCasec                 C   r   )Nr<   r:   r9   r   r   rJ   r'   r'   r(   test_herm_cases  r   z!HermitianTestCase.test_herm_casesc                 C   r   )Nr<   r9   r:   r   r   rJ   r'   r'   r(   test_empty_herm_cases  r   z'HermitianTestCase.test_empty_herm_casesN)rL   rM   rN   r   r   r'   r'   r'   r(   r     r   r   c                   @   ,   e Zd Zejjdd Zejjdd ZdS )LinalgGeneralizedSquareTestCasec                 C   r   )Nr:   r=   r9   r   r   rJ   r'   r'   r(   test_generalized_sq_cases     

z9LinalgGeneralizedSquareTestCase.test_generalized_sq_casesc                 C      | j h dd d S )N>   r=   r:   r9   r   r   rJ   r'   r'   r(   test_generalized_empty_sq_cases     z?LinalgGeneralizedSquareTestCase.test_generalized_empty_sq_casesN)rL   rM   rN   pytestmarkslowr   r   r'   r'   r'   r(   r     
    
r   c                   @   r   )"LinalgGeneralizedNonsquareTestCasec                 C   r   )Nr:   r>   r9   r   r   rJ   r'   r'   r(   test_generalized_nonsq_cases  r   z?LinalgGeneralizedNonsquareTestCase.test_generalized_nonsq_casesc                 C   r   )N>   r9   r>   r:   r   r   rJ   r'   r'   r(   "test_generalized_empty_nonsq_cases  r   zELinalgGeneralizedNonsquareTestCase.test_generalized_empty_nonsq_casesN)rL   rM   rN   r   r   r   r   r   r'   r'   r'   r(   r     r   r   c                   @   r   )HermitianGeneralizedTestCasec                 C   r   )Nr:   r<   r9   r   r   rJ   r'   r'   r(   test_generalized_herm_cases  r   z8HermitianGeneralizedTestCase.test_generalized_herm_casesc                 C   s   | j h ddhd d S )N>   r9   r<   r:   noner   r   rJ   r'   r'   r(   !test_generalized_empty_herm_cases  r   z>HermitianGeneralizedTestCase.test_generalized_empty_herm_casesN)rL   rM   rN   r   r   r   r   r   r'   r'   r'   r(   r     r   r   c                 C   sH   t | } | jdkrtj| j| jd}t| jd |d< |S t| jd S )Nrb   r4   rl   .r   )r   rn   r$   r   rq   r-   r   )r/   rr'   r'   r(   identity_like_generalized  s   
r   c                   @      e Zd Zdd ZdS )
SolveCasesc                 C   sj   t ||}t|jdkr"t||d d }tt||j| n
t||}t|| t	t
|| d S )Nr`   ).N.r   )r   solver$   r   rn   r	   r   broadcast_torq   r   r)   )rD   r/   r0   rC   r   Zadotxr'   r'   r(   rG     s   

zSolveCases.doNrL   rM   rN   rG   r'   r'   r'   r(   r         r   c                   @   B   e Zd Zejdeeee	gdd Z
dd Zdd Zdd	 Zd
S )	TestSolver-   c                 C   s2   t jddgddgg|d}tt||j| d S Nr`         ?r4   )r$   r   r   r   r   r-   rD   r-   r   r'   r'   r(   
test_types  s   zTestSolve.test_typesc                 C   s   G dd dt j}t dddd}t d|}t||}|jdks(J t ddd|}t||}|jdksAJ t ddd|}tt	tj|| d S )	Nc                   @      e Zd ZdS )z)TestSolve.test_1_d.<locals>.ArraySubclassNrL   rM   rN   r'   r'   r'   r(   ArraySubclass      r   r_   ra   ra   ra   rc   )ra   ra   ra   r`   )
r$   r%   arangerp   r   r   r   rq   r   
ValueError)rD   r   r/   r0   resultr'   r'   r(   test_1_d  s   zTestSolve.test_1_dc              	   C   s  G dd dt j}t dddd}t dddd|}t||d d ddd d f }t|d d ddddf |d d ddd d f }t|| tt	|| t
tjtj|d d ddddf | t
ttj||d d ddd d f  t dddd}t
ttj|| t
ttj|dd |dd  t d|}t||d d ddf }t|d d ddddf |dd }t|| tt	|| t ddd}t
ttj|| t
ttj|dd |dd  t
ttj|d d ddddf | d S )	Nc                   @   r   )z,TestSolve.test_0_size.<locals>.ArraySubclassNr   r'   r'   r'   r(   r     r   r   r_   ra   r*   r`   rb   r   )r$   r%   r   rp   r   r   r   r   r   r#   r   r   r   rD   r   r/   r0   expectedr   r'   r'   r(   test_0_size  s*   "8
(& *
 *zTestSolve.test_0_sizec                 C   s  G dd dt j}t dddd}t dddd|}t||d d d d ddf }t||d d d d ddf }t|| tt	|| t||d d ddddf }t|d d ddddf |d d ddddf }t|| tt	|| d S )	Nc                   @   r   )z.TestSolve.test_0_size_k.<locals>.ArraySubclassNr   r'   r'   r'   r(   r     r   r   rc   r`   ra   r*   rb   r   )
r$   r%   r   rp   r   r   r   r   r   r#   r   r'   r'   r(   test_0_size_k
  s   ""
"8
zTestSolve.test_0_size_kN)rL   rM   rN   r   r   parametrizer   r   r   r   r   r   r   r   r'   r'   r'   r(   r     s    
!r   c                   @   r   )InvCasesc                 C   s0   t |}tt||t| tt|| d S r!   )r   invr   r	   r   r   r)   )rD   r/   r0   rC   Za_invr'   r'   r(   rG     s
   

zInvCases.doNr   r'   r'   r'   r(   r     r   r   c                   @   2   e Zd Zejdeeee	gdd Z
dd ZdS )TestInvr-   c                 C   s0   t jddgddgg|d}tt|j| d S r   )r$   r   r   r   r   r-   r   r'   r'   r(   r   '  s   zTestInv.test_typesc                 C   s   G dd dt j}t jdt jd|}t|}t|jj	t j
u  t|j|j tt|| t jdt jd|}t|}t|jj	t ju  t|j|j tt|| d S )Nc                   @   r   )z*TestInv.test_0_size.<locals>.ArraySubclassNr   r'   r'   r'   r(   r   .  r   r   r   r`   r`   r4   r]   )r$   r%   zerosint_r   r   r   r   r-   r"   float64r   rq   r#   	complex64rD   r   r/   resr'   r'   r(   r   ,  s   

zTestInv.test_0_sizeNrL   rM   rN   r   r   r   r   r   r   r   r   r   r'   r'   r'   r(   r   &  s    
r   c                   @   r   )EigvalsCasesc                 C   s&   t |}t |\}}t|| d S r!   )r   eigvalseigr   )rD   r/   r0   rC   evevaluesevectorsr'   r'   r(   rG   ?  s   
zEigvalsCases.doNr   r'   r'   r'   r(   r   =  r   r   c                   @   r   )TestEigvalsr-   c                 C   s`   t jddgddgg|d}tt|j| t jddgddgg|d}tt|jt| d S Nr`   r   r4   rk   )r$   r   r   r   r   r-   r7   r   r'   r'   r(   r   F  s   zTestEigvals.test_typesc                 C   s   G dd dt j}t jdt jd|}t|}t|jj	t j
u  td|j tt|t j t jdt jd|}t|}t|jj	t ju  td|j tt|t j d S )Nc                   @   r   )z.TestEigvals.test_0_size.<locals>.ArraySubclassNr   r'   r'   r'   r(   r   O  r   r   r   r4   r   r`   r]   r^   )r$   r%   r   r   r   r   r   r   r-   r"   r   r   rq   r#   r   r   r'   r'   r(   r   M     

zTestEigvals.test_0_sizeNr   r'   r'   r'   r(   r   E  s    
r   c                   @   r   )EigCasesc                 C   sb   t |}|j|j}}tt||t|t|dd d d f  t|j	d t
t|| d S )N.rtol)r   r   eigenvalueseigenvectorsr   r	   r$   r   r8   r-   r   r)   )rD   r/   r0   rC   r   r   r   r'   r'   r(   rG   b  s   

 zEigCases.doNr   r'   r'   r'   r(   r   `  r   r   c                   @   r   )TestEigr-   c                 C   s   t jddgddgg|d}t j|\}}t|j| t|j| t jddgddgg|d}t j|\}}t|jt| t|jt| d S r   )r$   r   r   r   r   r-   r7   rD   r-   r   wvr'   r'   r(   r   l  s   zTestEig.test_typesc                 C   s   G dd dt j}t jdt jd|}t|\}}t|jj	t j
u  t|jj	t j
u  t|j|j td|j tt|t j t jdt jd|}t|\}}t|jj	t ju  t|jj	t ju  t|j|j td|j tt|t j d S )Nc                   @   r   )z*TestEig.test_0_size.<locals>.ArraySubclassNr   r'   r'   r'   r(   r   z  r   r   r   r4   r   r]   r^   )r$   r%   r   r   r   r   r   r   r-   r"   r   r   rq   r#   r   rD   r   r/   r   Zres_vr'   r'   r(   r   x     zTestEig.test_0_sizeNr   r'   r'   r'   r(   r   k  s    
r   c                   @   s.   e Zd ZdZejdeee	e
gdd ZdS )SVDBaseTestsFr-   c                 C   s   t jddgddgg|d}t|}|j|j|j}}}t|j| t|jt	| t|j| tj|d| j
d}t|jt	| d S )Nr`   r   r4   F
compute_uvr<   )r$   r   r   svdUSVhr   r-   r5   r<   )rD   r-   r   r   r  r  r  r   r'   r'   r(   r     s   
zSVDBaseTests.test_typesN)rL   rM   rN   r<   r   r   r   r   r   r   r   r   r'   r'   r'   r(   r     s    r   c                   @   r   )SVDCasesc              	   C   sp   t |d\}}}t|tt|t|dd d d f  t|t|jd tt	|| tt	|| d S )NF.r   )
r   r  r   r	   r$   r   r8   r-   r   r)   )rD   r/   r0   rC   ur   vtr'   r'   r(   rG     s   &zSVDCases.doNr   r'   r'   r'   r(   r    r   r  c                   @   r   )TestSVDc                 C   s   t d}tj|d| jd\}}}t|jd t|jd t|t d t d}tj|d| jd\}}}t|jd t|jd t|t d dS )	z6 Empty input should put an identity matrix in u or vh )rc   r   Tr   rc   rc   r]   rc   )r   rc   N)r$   r   r   r  r<   r   rq   eye)rD   r   r  r   Zvhr'   r'   r(   test_empty_identity  s   

zTestSVD.test_empty_identityc                 C   s@   t ddgddgg}tj|d| jd}t|}t|| d S )Nr`   r   Fr   )r$   r   r   r  r<   Zsvdvalsr   )rD   r   Z
s_from_svdZs_from_svdvalsr'   r'   r(   test_svdvals  s   
zTestSVD.test_svdvalsN)rL   rM   rN   r  r  r'   r'   r'   r(   r	    s    r	  c                   @   r   )SVDHermitianCasesc              	   C   s   t j|ddd\}}}t|tt|t|dd d d f  t|t|jd dd }tt|||t	t
|jd |j tt|||t	t
|jd |j tt|dd d df | tt|| tt|| d S )	NFTr<   .r   c                 S   s<   t t| j}|d |d |d< |d< ttj| |dS )Nrl   rk   axes)r   rangern   r$   conj	transpose)matr  r'   r'   r(   r<     s   z'SVDHermitianCases.do.<locals>.hermitianrk   )r   r  r   r	   r$   r   r8   r-   r   r   r  rq   r   sortr   r)   )rD   r/   r0   rC   r  r   r  r<   r'   r'   r(   rG     s   &,,zSVDHermitianCases.doNr   r'   r'   r'   r(   r    r   r  c                   @   s   e Zd ZdZdS )TestSVDHermitianTN)rL   rM   rN   r<   r'   r'   r'   r(   r    s    r  c                   @   r   )	CondCasesc                 C   s  t |}d|v rtttj| d S tj|dd}tt||d |d  ddd tt|d	|d |d  ddd tt|d
|d |d  ddd tj|}tt|dt	|
d
dt	|
d
d ddd tt|dt	|
d
dt	|
d
d ddd tt|tjt	|
ddt	|
dd ddd tt|tj t	|
ddt	|
dd ddd tt|dtt	|d	 
d
dt	|d	 
d
d ddd d S )Nr9   F)r  r   ).rk   rh   rg   r1   r2   ra   rl   r`   rk   fro)r   r   r   r   condr  r   r$   r   rx   summaxminr   sqrt)rD   r/   r0   rC   cr   Zcinvr'   r'   r(   rG     sZ   
&
&&&

zCondCases.doNr   r'   r'   r'   r(   r    s    r  c                   @   s>   e Zd Zdd Zdd Zejjddddd	d
 Zdd Z	dS )TestCondc                 C   s   t g dg dg dg}tt|td tt|t d tt|dd tt|dd tt|d	td
 d S )N)rV   r   r`   )r   g       r   )r   r   rX   rc   gUUUUUU?r`   rk   r   r  gUUUUU6@)r   r   r   r  r   r$   r  rD   Ar'   r'   r(   test_basic_nonsvd  s   zTestCond.test_basic_nonsvdc                 C   st   t dt dg}g d}ddg}t||D ]\}}tt||dk qt||D ]
\}}t|| q-d S )Nr   Nr`   ra   r  rk   rl     4&kC)r$   r   onesr   r   r   r   r  )rD   ZAsp_posZp_negr#  pr'   r'   r(   test_singular  s   zTestCond.test_singularTFz/Platform/LAPACK-dependent failure, see gh-18914)runreasonc                 C   s   g d}g d}t d}t j|d< |D ]}t||}tt|t j tt | qt d}t j|d< |D ]:}t||}tt |d  ||v r^t|d d	k t|d
 d	k q8tt |d   tt |d
   q8d S )N)Nr`   rk   ra   rl   r  r%  r   r   )rb   ra   ra   r`   r   r`   r`   r   r&  ra   )	r$   r'  nanr   r  r   r#   r   isnan)rD   Zpsr(  r#  r)  r   r'   r'   r(   test_nan  s&   



zTestCond.test_nanc                 C   s   t jd t jdddd}d|d< d|d< dD ]*}t||}t|d t j t|d t j tt 	|d  tt 	|d  qd S )	NrU   ra   r   r]   r   )Nr`   ra   r  rk   rl   r   r`   r   )
r$   randomseedrandr   r  r   r   r   isfinite)rD   r#  r)  r   r'   r'   r(   test_stacked_singular7  s   zTestCond.test_stacked_singularN)
rL   rM   rN   r$  r*  r   r   xfailr0  r6  r'   r'   r'   r(   r!    s    	

r!  c                   @   r   )	PinvCasesc                 C   s<   t |}t}t||||||ddd tt|| d S )Nrh   rg   r  r   pinvr	   r   r   r)   rD   r/   r0   rC   Za_ginvr   r'   r'   r(   rG   L  s   
zPinvCases.doNr   r'   r'   r'   r(   r8  G  s    r8  c                   @   r   )TestPinvNr   r'   r'   r'   r(   r<  T  r   r<  c                   @   r   )PinvHermitianCasesc                 C   s@   t j|dd}t}t||||||ddd tt|| d S )NTr  rh   rg   r  r9  r;  r'   r'   r(   rG   Z  s   zPinvHermitianCases.doNr   r'   r'   r'   r(   r=  X  r   r=  c                   @   r   )TestPinvHermitianNr   r'   r'   r'   r(   r>  b  r   r>  c                  C   s   t g dg dg dg} tt jj| ddt jj| dd tjtdd t jj| ddd	 W d    d S 1 s;w   Y  d S )
Nr`   ra   rb   )rc   r`   r`   )ra   rb   r`   r   rcondr   z%`rtol` and `rcond` can't be both set.)match)rA  r   )r$   r   r   r   r:  r   Zraisesr   )r/   r'   r'   r(   test_pinv_rtol_argf  s   "rC  c                   @   r   )DetCasesc                 C   s   t |}t |}|j|j}}t|jjtt	fv r#t|
t	}nt|
t}t |}	t|tj|	dd t|t| tj|	dd t|}t|}|dk}
tt||
 d t||
  t  d S )Nrk   axisr   r`   )r   detslogdetsignZ	logabsdetr   r-   r"   r   r   astyper   r   r   r   reducer$   expZ
atleast_1drx   r   r   )rD   r/   r0   rC   dr   r   ldadr   mr'   r'   r(   rG   v  s   




zDetCases.doNr   r'   r'   r'   r(   rD  t  r   rD  c                   @   s:   e Zd Zdd Zejdeee	e
gdd Zdd ZdS )	TestDetc                 C   s   t tdggd t ttdggt t tdggd t ttdggt t tdggdt f t ttdggd t t ttdggd t t tdggdt f t ttdggd t t ttdggd t d S )N        y                r   r`   )r   r   rG  r"   r   r   rH  r   rJ   r'   r'   r(   	test_zero  s    zTestDet.test_zeror-   c                 C   s^   t jddgddgg|d}tt j|j| t j|\}}t|jt| t|j| d S r   )r$   r   r   r   rG  r-   rH  r5   )rD   r-   r   phr   r'   r'   r(   r     s
   zTestDet.test_typesc                 C   s   t jdt jd}t|}t|d t|jjt ju  t	|}t|d t|d jjt ju  t|d jjt j
u  t jdt jd}t|}t|d t|jjt ju  t	|}t|d t|d jjt ju  t|d jjt ju  d S )Nr]   r4   rV   r1  r   r`   )r$   r   r   r   rG  r   r   r-   r"   rH  float32r   )rD   r/   r   r'   r'   r(   r     s    







zTestDet.test_0_sizeN)rL   rM   rN   rS  r   r   r   r   r   r   r   r   r   r'   r'   r'   r(   rQ    s
    
rQ  c                   @   r   )
LstsqCasesc                 C   sL  t |}|j\}}t|d\}}}	tj||dd\}
}}}|dkr+t|
dk  ||kr=t|t	||
 t
|| nt
|| t||| ||kr}||kr}t tt 	||
| d jdd}t |}t |jdkr|d|_t
|j|j n
t g t|
}t|| tt |jt j tt|
| tt|| d S )	NFrk   r@  r   ra   rE  r`   ru   )r$   r   rq   r   r  lstsqr   r   r   r   r   Z__array_wrap__rx   r  rn   r   r   r"   
issubdtyper-   floatingr)   )rD   r/   r0   rC   arrrP  nr  r   r  r   	residualsranksvZexpect_residsr'   r'   r(   rG     s0   


 

zLstsqCases.doNr   r'   r'   r'   r(   rV    r   rV  c                   @   s:   e Zd Zdd Zejg dg ddd Zdd Zd	S )
	TestLstsqc                 C   s   t g dg dg dg dgj}t g d}tj||dd\}}}}t|dk t||\}}}}t|d	k tj||d d\}}}}t|d	k d S )
N)rR  rV   rR  rV   rW   rR  )rR  rW   rR  rR  rV   rR  )rV   rR  rV   rR  rR  rY   )rR  rR  rR  rW   rX   rR  )r`   r   r   r   r   r   rk   r@  rc   rb   )r$   r   Tr   rW  r   )rD   r/   r0   r   r\  r]  r   r'   r'   r(   
test_rcond  s   
zTestLstsq.test_rcond)rP  r[  n_rhs))rc   ra   ra   )r   rc   r`   )r   rc   ra   )rc   r   r`   )rc   r   ra   )rc   ra   r   r   r   r   c                 C   s   t || ||}t ||f}tj||d d\}}}}	|dkr*t|dk  t|j	||f t|j	||kr<|fnd ||krZ|dkrZ|t 
|| }
t||
|
 jdd t|t|| t|	j	t||f d S )Nr@  r   r^   rl   rE  )r$   r   rp   r'  r   rW  r   r   r   rq   r   r   r  r  )rD   rP  r[  rb  r/   r0   r   r\  r]  r   r   r'   r'   r(   test_empty_a_b  s   
zTestLstsq.test_empty_a_bc                 C   sv   t g d}t g d}t |t t|gj}ttd tj	||d d W d    d S 1 s4w   Y  d S )N)r   r`   ra   rb   )rk   g?g?g @gffffff
@zIncompatible dimensionsr@  )
r$   r   Zvstackr'  lenr`  r   r   r   rW  )rD   r   yr#  r'   r'   r(   test_incompatible_dims  s   "z TestLstsq.test_incompatible_dimsN)	rL   rM   rN   ra  r   r   r   rd  rg  r'   r'   r'   r(   r_    s
    
	r_  dtc                 C   s   g | ]}t |qS r'   )r$   r-   )ry   r   r'   r'   r(   rz     s    rz   z?bBhHiIqQefdgFDGOc                   @   s   e Zd ZedZeg d Zeg d Zeg d ZeeeegZ	e
ddgddggZeegggd Zeeded	ed
gZdd Zdd Zdd Zdd Zdd Zdd Zdd Zejjedddd ZdS )TestMatrixPowerrc   )rb   r   r`   ra   )ra   rb   r   r`   )r`   ra   rb   r   r`   r   ra   r   gGc                 C   s   | j |}tt|dd d d d | j tt|dd d d d | j  tt|dd d d d | j tt|dd d d d | j d S )Nra   d   i       r   r`   rb   )rshft_1rJ  r   r   rshft_0rshft_2rshft_3)rD   rh  Zrshftr'   r'   r(   test_large_power  s   z TestMatrixPower.test_large_powerc                 C   >   dd }| j D ]}||| |tkr|| j| qd S )Nc                 S   s*   t | d}t|t|  t|j| j d S )Nr   )r   r   r   r-   )Mmzr'   r'   r(   tz  s   
z.TestMatrixPower.test_power_is_zero.<locals>.tz	rshft_allrJ  objectstackedrD   rh  rv  r  r'   r'   r(   test_power_is_zero     
z"TestMatrixPower.test_power_is_zeroc                 C   rs  )Nc                 S   s&   t | d}t||  t|j| j d S )Nr`   )r   r   r-   )r  ru  r'   r'   r(   rv  &  s   

z-TestMatrixPower.test_power_is_one.<locals>.tzrw  r{  r'   r'   r(   test_power_is_one%  r}  z!TestMatrixPower.test_power_is_onec                 C   rs  )Nc                 S   s>   t | d}| jtkrtnt}t||| |  t|j| j d S )Nra   )r   r-   ry  r	   r   r   )r  ru  mmulr'   r'   r(   rv  1  s   
z-TestMatrixPower.test_power_is_two.<locals>.tzrw  r{  r'   r'   r(   test_power_is_two0  s   
z!TestMatrixPower.test_power_is_twoc                 C   s0   dd }| j D ]}|| jvr||| qd S )Nc                 S   s4   t | d}| jtkrtnt}t||| t|  d S Nrk   )r   r-   ry  r	   r   r   r   )r  Zinvmatr  r'   r'   r(   rv  =  s
   
z3TestMatrixPower.test_power_is_minus_one.<locals>.tz)rx  dtnoinvrJ  r{  r'   r'   r(   test_power_is_minus_one<  s   

z'TestMatrixPower.test_power_is_minus_onec                 C   s.   | j |}ttt|d ttt|dg d S )Ng      ?r`   )ro  rJ  r   	TypeErrorr   rD   rh  r  r'   r'   r(   test_exceptions_bad_powerG  s   z)TestMatrixPower.test_exceptions_bad_powerc                 C   sP   t tttdg|d t tttdgdgg|d t tttd|d d S )Nr`   ra   )rc   rb   ra   )r   r   r   r$   r   r'  )rD   rh  r'   r'   r(   test_exceptions_non_squareL  s   z*TestMatrixPower.test_exceptions_non_squarefp errors don't work in wasmr,  c                 C   s,   || j v rd S | j|}ttt|d d S r  )r  noninvrJ  r   r   r   r  r'   r'   r(   test_exceptions_not_invertibleQ  s   
z.TestMatrixPower.test_exceptions_not_invertibleN)rL   rM   rN   r$   r  ro  rn  rp  rq  rx  r   r  blockrz  ry  r-   r  rr  r|  r~  r  r  r  r  r   r   skipifr    r  r'   r'   r'   r(   ri    s$    
ri  c                   @   r   )TestEigvalshCasesc                 C   s^   t |d}t |\}}|jdd t||t|jd t |d}t||t|jd d S )NLrk   rE  r   r  )r   eigvalshr   r  r   r8   r-   )rD   r/   r0   rC   r   r   r   ev2r'   r'   r(   rG   [  s   zTestEigvalshCases.doNr   r'   r'   r'   r(   r  Y  r   r  c                   @   r   )TestEigvalshr-   c                 C   s:   t jddgddgg|d}t j|}t|jt| d S r   )r$   r   r   r  r   r-   r5   )rD   r-   r   r   r'   r'   r(   r   h  s   zTestEigvalsh.test_typesc                 C   X   t jddgddggt jd}ttt jj|dd ttt jj|d ttt jj|d d S Nr`   r   r4   ZlrongZUPLOlowerupper)r$   r   rU  r   r   r   r  rD   r   r'   r'   r(   test_invalidn     zTestEigvalsh.test_invalidc                 C   s   t jddgddggt jd}t jddgddggt jd}t jddgt jd}tt j}t j|}t|||d t jj|dd}t|||d t jj|dd}t|||d t jj|d	d}t|||d t jj|d
d}t|||d d S Nr   r`   r4   rk   r   r  r  lr  r  )r$   r   r   r8   r   r  r   )rD   KloKuptgtr   r   r'   r'   r(   	test_UPLOt  s   
zTestEigvalsh.test_UPLOc                 C   s   G dd dt j}t jdt jd|}t|}t|jj	t j
u  td|j tt|t j t jdt jd|}t|}t|jj	t ju  td|j tt|t j d S )Nc                   @   r   )z/TestEigvalsh.test_0_size.<locals>.ArraySubclassNr   r'   r'   r'   r(   r     r   r   r   r4   r   r]   r^   )r$   r%   r   r   r   r   r  r   r-   r"   r   r   rq   r#   r   rU  r   r'   r'   r(   r     r   zTestEigvalsh.test_0_sizeNrL   rM   rN   r   r   r   r   r   r   r   r   r  r  r   r'   r'   r'   r(   r  g  s    
r  c                   @   r   )TestEighCasesc                 C   s   t |}|j|j}}t |\}}|jdd t|| tt||t	
|dd d d f t	
| t|jd t |d\}	}
t|	| tt||
t	
|	dd d d f t	
|
 t|jt|d d S )Nrk   rE  .r   r  )r   err_msg)r   eighr   r   r   r  r   r   r	   r$   r   r8   r-   repr)rD   r/   r0   rC   r   r   Zevcr   r   r  Zevc2r'   r'   r(   rG     s   


 

 
zTestEighCases.doNr   r'   r'   r'   r(   r    r   r  c                   @   r   )TestEighr-   c                 C   sJ   t jddgddgg|d}t j|\}}t|jt| t|j| d S r   )r$   r   r   r  r   r-   r5   r   r'   r'   r(   r     s   zTestEigh.test_typesc                 C   r  r  )r$   r   rU  r   r   r   r  r  r'   r'   r(   r    r  zTestEigh.test_invalidc                 C   s   t jddgddggt jd}t jddgddggt jd}t jddgt jd}tt j}t j|\}}t|||d t jj|dd\}}t|||d t jj|dd\}}t|||d t jj|d	d\}}t|||d t jj|d
d\}}t|||d d S r  )r$   r   r   r8   r   r  r   )rD   r  r  r  r   r   r   r'   r'   r(   r    s   
zTestEigh.test_UPLOc                 C   s   G dd dt j}t jdt jd|}t|\}}t|jj	t j
u  t|jj	t j
u  t|j|j td|j tt|t j t jdt jd|}t|\}}t|jj	t ju  t|jj	t ju  t|j|j td|j tt|t j d S )Nc                   @   r   )z+TestEigh.test_0_size.<locals>.ArraySubclassNr   r'   r'   r'   r(   r     r   r   r   r4   r   r]   r^   )r$   r%   r   r   r   r   r  r   r-   r"   r   r   rq   r#   r   rU  r   r'   r'   r(   r     r   zTestEigh.test_0_sizeNr  r'   r'   r'   r(   r    s    
r  c                   @   s    e Zd ZdZdZedd ZdS )_TestNormBaseNc                 C   s<   t | jjtjrt|j| jj d S tt |jjtj d S r!   )	
issubclassr-   r"   r$   Zinexactr   realr   rY  )r   r   r'   r'   r(   check_dtype  s   z_TestNormBase.check_dtype)rL   rM   rN   rh  decstaticmethodr  r'   r'   r'   r(   r    s
    r  c                   @   s4   e Zd Zdd Zdd Zdd Zdd Zd	d
 ZdS )_TestNormGeneralc                 C   sF   t tg d t ttg | jdd t tttg | jdd d S )NrR  r4   )r   r   r   rh  r   rJ   r'   r'   r(   
test_empty  s    z_TestNormGeneral.test_emptyc           	   	   C   s  t g d}t jd }t jd }|| }|D ]}||}t|t j }| || t|d t }|	t
d t|d}| || t|d W d    n1 sTw   Y  t|d}| || t|d t|d	}| || t|d
 t|d}| || t||jd
|jd  t|d}| || t||jd
|jd  t|t j}| || t|d qd S )Nr-  
AllIntegerZAllFloatrR  divide by zero encounteredrk   r   ra   r`   rW   r   rc   g      ?rV   )r$   r   	typecodesrJ  r   r   r  r   r   filterRuntimeWarningr-   r"   	rD   r/   Zexact_typesZinexact_typesZ	all_typesZ	each_typeatZansupr'   r'   r(   test_vector_return_type  s>   










z(_TestNormGeneral.test_vector_return_typec                    sv   g d}g d}g d} fdd}|||fD ]}|| qt | jdt | jdt | jdfD ]}|| q2d S )N)r`   ra   rb   rc   )rk   rl   rw   rv   )rk   ra   rw   rc   c                    s   t jjt| d jd t jjt| td jd t jjt| t d jd t jjt| dd jd t jjt| dd jd t jjt| d	d jd t jjt| d
d jd t jjt| dd jd d S )Ng6ҭ@r,   rY   rV   r`         $@rk   gQ?ra   rl   gsc?r   rc   )r$   testingr   r   r  r   )r   rJ   r'   r(   _test1  s0   
z+_TestNormGeneral.test_vector.<locals>._testr4   )r   rh  )rD   r/   r0   r   r  r   r'   rJ   r(   test_vector,  s   

z_TestNormGeneral.test_vectorc           	   	      s  t g dg dg| jd d dddddtjtj fD ]4 fd	d
t jd D }tt dd|  fdd
t jd D }tt dd| qtjdd| jd	dddj
}d ddddtjtj dfD ]htt| |dD ][}|\}}|dk r||7 }|dk r||7 }||krttt|d qyt|d}|||  ||k rfdd
tj D }nfdd
tj D }t|| qyqmd S )Nr?  rc   rh   r*   r4   rk   r   r`   ra   rb   c                    s$   g | ]}t  d d |f dqS Nordr   ry   kr#  orderr'   r(   rz   P     $ z._TestNormGeneral.test_axis.<locals>.<listcomp>r  rF  c                    s$   g | ]}t  |d d f dqS r  r  r  r  r'   r(   rz   R  r     rc   rl   r  c                    s*   g | ]}t  d d  j|ddqS NrE  r  )r   taker  Bk_indexr  r'   r(   rz   h  s    "c                    s,   g | ]}t  d d  j|djdqS r  )r   r  r`  r  r  r'   r(   rz   k  s    $)r   rh  r$   r   r  rq   r   r   r   rp   rn   r   combinationsr   r   )	rD   Z	expected0Z	expected1ndrF  Zrow_axisZcol_axisr[  r   r'   )r#  r  r  r  r(   	test_axisJ  s:   z_TestNormGeneral.test_axisc           	   
   C   s  t jdd| jdddd}d}d}t|d d d	}t|d d d
d}tt |||d d d d}t|j	|k||j	|d d  d dddddt j
t j
 fD ]C}t|jD ];}t|||d	}t|||d
d}tt |||||d t|j	}d||< t|}t|j	|k||j	||| qTqMd ddddt j
t j
 ddf	D ]O}tt|jdD ]C}t|||d	}t|||d
d}tt |||||d t|j	}d||d < d||d < t|}t|j	|k||j	||| qqd S )Nr`   r  r4   ra   rb   rc   zorder {0}, axis = {1}z;Shape mismatch found {0}, expected {1}, order={2}, axis={3}r  T)r  rF  keepdims)r  )r`   r`   r`   rk   r   rl   r  nuc)r$   r   rh  rp   r   r   Zsqueezeformatr   rq   r   r  rn   r   r   r   permutations)	rD   r#  Zallclose_errZ	shape_errr   foundZexpected_shaper  r  r'   r'   r(   test_keepdimso  sT   




 


z_TestNormGeneral.test_keepdimsN)rL   rM   rN   r  r  r  r  r  r'   r'   r'   r(   r    s    )%r  c                   @   s:   e Zd ZejZdd Zdd Zdd Zdd Zd	d
 Z	dS )_TestNorm2Dc                 C   s    t t| jg g| jdd d S )Nr4   rR  )r   r   r   rh  rJ   r'   r'   r(   test_matrix_empty  s    z_TestNorm2D.test_matrix_emptyc           	   	   C   s  |  g dg dg}tjd }d}|| }|D ]}||}t|tj }| || t|d t }|	t
d t|d}| || t|d W d    n1 sUw   Y  t|d	}| || t|d t|d
}| || t|d t|d}| || t|d t|tj}| || t|d t|d}| || t|d t|d}| || tjj|ddd qd S )Nr-  r   r  ZfdFDrW   r  rk   rV   r`   ra   gLXz?rl   r  r  gU&,t=@r*   r  )r   r$   r  rJ  r   r   r  r   r   r  r  r  r  r'   r'   r(   test_matrix_return_type  sD   













z#_TestNorm2D.test_matrix_return_typec                 C   s   | j ddgddgg| jd}tt|d tt|dd tt|dd	 tt|td
 tt|t d tt|dd	 tt|dd tt|dd tt|dd ttt|d ttt|d ttt|d d S )Nr`   rb   rh   ri   r4   gT"@r  r  r  g      (@rY   rk   rZ   ra   gW?"@rl   gїʄ?Znofrorw   r   )r   rh  r   r   r   r   r   r"  r'   r'   r(   test_matrix_2x2  s   z_TestNorm2D.test_matrix_2x2c                 C   s   d| j g dg dg dg| jd }tt|d tt|dd tt|dd	 tt|td
 tt|t d tt|dd tt|dd tt|dd tt|dd d S )Ng?r?  )r*   r   rh   )rb   ra   r`   r4   grj=Q0?r  r  gB+pc?g?g333333?r`   rV   rk   g?ra   g&.d?rl   gܸՠ?)r   rh  r   r   r   r"  r'   r'   r(   test_matrix_3x3  s    z_TestNorm2D.test_matrix_3x3c                 C   s  | j g dg dg| jd}tjdd| jdddd}ttt|d	d
 ttt|dd
 tttddgd	d  tttddgdd  tttddgdd  dD ]}ttt||d  ttt||d ttt||d qLttt|d d ttt|d d ttt|d d d S )Nr?  r  r4   r`   r  ra   rb   rc   r  r   r  testr   rb   r   )r`   ra   ra   rb   r   r`   ra   )	r   rh  r$   r   rp   r   r   r   r   )rD   r#  r  r  r'   r'   r(   test_bad_args  s   z_TestNorm2D.test_bad_argsN)
rL   rM   rN   r$   r   r  r  r  r  r  r'   r'   r'   r(   r    s    3r  c                   @   r   )	_TestNormNr   r'   r'   r'   r(   r    r   r  c                   @   s$   e Zd Zdd Zdd Zdd ZdS )TestNorm_NonSystematicc                 C   s*   t jdt jd}tt|ddddd d S )N
   r4   rb   r  gL)@ra   r  )r$   r   Z
longdoubler.   r   r  r'   r'   r(   test_longdouble_norm  s   z+TestNorm_NonSystematic.test_longdouble_normc                 C   s,   t jdgt jd}tt|ddddd d S )Ni   r4   rb   r  l        rh   r  )r$   r   Zint32r.   r   r  r'   r'   r(   test_intmin  s   z"TestNorm_NonSystematic.test_intminc                 C   s   t jdt jd}d|d< d|d< d}tt jj|dd	|d
d |t j}tt jj|dd	|dd |t j}tt jj|dd	|dd d S )Nra   r4   y      @      @r   y            @r`   gV;'@rb   r  r  r  	   rh   )	r$   r   Zclongdoubler.   r   r   rJ  
complex128r   )rD   rM  r   r'   r'   r(   test_complex_high_ord$  s   z,TestNorm_NonSystematic.test_complex_high_ordN)rL   rM   rN   r  r  r  r'   r'   r'   r(   r    s    r  c                   @      e Zd ZejZdZdS )_TestNormDoubleBaser+   N)rL   rM   rN   r$   r   rh  r  r'   r'   r'   r(   r  2      r  c                   @   r  )_TestNormSingleBaser*   N)rL   rM   rN   r$   rU  rh  r  r'   r'   r'   r(   r  7  r  r  c                   @   r  )_TestNormInt64Baser+   N)rL   rM   rN   r$   Zint64rh  r  r'   r'   r'   r(   r  <  r  r  c                   @   r   )TestNormDoubleNr   r'   r'   r'   r(   r  A  r   r  c                   @   r   )TestNormSingleNr   r'   r'   r'   r(   r  E  r   r  c                   @   r   )TestNormInt64Nr   r'   r'   r'   r(   r  I  r   r  c                   @   r   )TestMatrixRankc                 C   s   t dttd td}d|d< t t|d t ttdd t tg dd t ttd	d t tdgd t|tdtdg}t t|tg d
 t tdd ttd t|ddd W d    d S 1 sxw   Y  d S )Nrc   rR  rk   rk   rb   r
  r   )r`   r   r   r   r`   )rc   )rb   rc   r   z#`tol` and `rtol` can't be both set.g{Gz?)tolr   )r   r   r$   r  r   r   r   r   )rD   Imsr'   r'   r(   test_matrix_rankO  s    
"zTestMatrixRank.test_matrix_rankc                 C   s   t dttddd t dttddd t dttddd td}d|d< t d	t|dd d
|d< t dt|ddd t d	t|ddd d S )Nrc   Tr  r`   r
  r   rR  r  rb   g:0yE>g|"BE>)r<   r  g;䡈E>)r   r   r$   r  r'  r   )rD   r  r'   r'   r(   test_symmetric_rankh  s   
z"TestMatrixRank.test_symmetric_rankN)rL   rM   rN   r  r  r'   r'   r'   r(   r  M  s    r  c                  C   s   t jd} tdD ]B}| jdd}|d d df |d d df  |d d df< tt|d |d d d	f |d d d
f  |d d df< tt|d q
d S )Ni3rl  )(   r  sizer`   ra   r   r  rc   rh   rb   r_   )r$   r2  ZRandomStater  normalr   r   )rngiXr'   r'   r(   test_reduced_rankv  s   ,,r   c                	   @   s   e Zd ZejZdd Zejddgg ddd Z	dd	 Z
d
d Zdd Zejdg dejdg dejdejejejejgdd ZdS )TestQRc                 C   s  t |}|j}|j\}}t||}tj|dd}|j|j}}	t|j|k t|	j|k tt	|| tt	|	| t|j||fk t|	j||fk t
t||	| t
t|j |t| t
t|	|	 tj|dd\}
}t|
j|k t|j|k tt	|
| tt	|| t|
j||fk t|j||fk t
t|
|| t
t|
j |
t| t
t|| tj|dd}t|j|k tt	|| t
|| d S )Ncompletemodereducedr   )r"   r-   rq   r  r   qrQRr   r#   r   r   r`  r  r$   r  triu)rD   r/   a_typea_dtyperP  r[  r  r   r  r  q1r1r2r'   r'   r(   check_qr  s:   

zTestQR.check_qrrP  r[  )rb   r   r  r]   c                 C   st   t ||}t||f}| | tjj|dd\}}t|jtj t|jtj t|j	||f t|j	|f d S )Nrawr  )
r  r$   r   r  r   r  r   r-   r   rq   )rD   rP  r[  r  r/   htaur'   r'   r(   test_qr_empty  s   

zTestQR.test_qr_emptyc                 C   s   | j ddgddgddggtjd}tj|dd	\}}t|jtjk t|jtjk t|jd
k t|jdk tj|jdd	\}}t|jtjk t|jtjk t|jdk t|jdk d S )Nr`   ra   rb   rc   rh   r*   r4   r  r  r  r  rj   )	r   r$   r   r   r  r   r-   rq   r`  )rD   r/   r  r  r'   r'   r(   test_mode_raw  s   "zTestQR.test_mode_rawc                 C   s   |  ddgddgg}|  ddgddgddgg}dD ]}||}||}| | | | | |j qdD ]$}dd||  }dd||  }| | | | | |j q:d S )	Nr`   ra   rb   rc   rh   r*   fd              ?)r   rJ  r  r`  )rD   r/   r0   rh  m1m2r'   r'   r(   test_mode_all_but_economic  s   





z!TestQR.test_mode_all_but_economicc                 C   sf  t |}|j}|jdd  \}}t||}tj|dd\}}t|j|k t|j|k tt|| tt|| t|jdd  ||fk t|jdd  ||fk tt	||| t
|jd }	t
|	|jd d |jd fd  }
tt	t|dd ||
 tt
|dd d d d f | tj|dd\}}t|j|k t|j|k tt|| tt|| t|jdd  ||fk t|jdd  ||fk tt	||| t
|jd }	t
|	|jd d |jd fd  }
tt	t|dd ||
 tt
|dd d d d f | tj|dd}t|j|k tt|| t|| d S )	Nrl   r  r  rk   ra   .r  r   )r"   r-   rq   r  r   r  r   r#   r   r	   r$   r   r   r
   r  r	  )rD   r/   r
  r  rP  r[  r  qr   ZI_matZstack_I_matr  r  r  r'   r'   r(   check_qr_stacked  sL   
""zTestQR.check_qr_stackedr  ))rb   rc   )rc   rb   r
  r  r  
outer_size)r   r  )ra   rb   rc   rh  c                 C   sX   t jd}|j|| d|}|j|| d|}| | | |d|   d S )N{   r  r  )r$   r2  Zdefault_rngr  rJ  r  )rD   r  r  rh  r  r#  r  r'   r'   r(   test_stacked_inputs  s
   

zTestQR.test_stacked_inputsN)rL   rM   rN   r$   r   r  r   r   r   r  r  r  r  r   r   r   r   r  r'   r'   r'   r(   r    s    (
-r  c                   @   sf   e Zd Zejdg dejdejejej	ej
fejdddgdd Zd	d
 Zdd ZdS )TestCholeskyrq   )r   r   rb   rb   )2   r"  )rb   r  r  r-   r  FTc           
      C   s6  t jd t jj| }t |t jr|dt jj|   }ttt|}d|dd < t 	|
| |}t j||d}t jj||d}|rSt 	|
| |}nt 	||
| }d|jd  t |j }t|||| d	| d
| d
| d t j|ddd}	tt t |	 tt |	dk d S )Nr`   r  )rk   rl   rl   r4   r  i  r    
)atolr  rk   )Zaxis1Zaxis2)r$   r2  r3  randnrX  Zcomplexfloatingr   r  re  r	   r  r  r   r   choleskyrq   ZfinfoZepsr   diagonalr   r   Zisreal)
rD   rq   r-   r  r/   tr   r0   r&  rM  r'   r'   r(   test_basic_property'  s"   	&z TestCholesky.test_basic_propertyc                 C   s   G dd dt j}t jdt jd|}t|}t|j|j t	|j
jt ju  t	t|t j t jdt jd|}t|}t|j|j t	|j
jt ju  t	t|t j d S )Nc                   @   r   )z/TestCholesky.test_0_size.<locals>.ArraySubclassNr   r'   r'   r'   r(   r   L  r   r   r   r4   )r`   r   r   )r$   r%   r   r   r   r   r(  r   rq   r   r-   r"   r   r#   r   r   r'   r'   r(   r   K  s   

zTestCholesky.test_0_sizec                 C   sT   t ddgddgg}tt|tj|dd ttj|ddt|j  d S )Ny      ?        y               y               @y      @        Fr#  T)r$   r   r   r   r(  r`  r  )rD   r/   r'   r'   r(   test_upper_lower_arg[  s   z!TestCholesky.test_upper_lower_argN)rL   rM   rN   r   r   r   r$   rU  r   r   r  r+  r   r,  r'   r'   r'   r(   r   %  s    r   c                   @   s   e Zd ZedZedZeg dg dg dgZe	ej
eee eed ej
eddejf e W d   dS 1 sEw   Y  dS )	TestOuterrb   rc  r  )r   ra   rc   z$Input arrays must be one-dimensionalN)rL   rM   rN   r$   r   Zarr1Zarr2r   r   r   r   outerr   r   Znewaxisr'   r'   r'   r(   r-  g  s    

"r-  c                  C   s   t jdkrd} nd} tjtjfD ]A}tjd|d}||j| }||jd	 }t
|jjd tjtjtjfD ]}||}t||| t||| q<qd S )Nlittle<rI   rc   r4   r  =)sys	byteorderr$   rU  r   r  r   r-   Znewbyteorderbyteswapr   r   r   rG  r:  r   )nativeZdttrZ  Zn_arrZsw_arrZroutiner   r'   r'   r(   test_byteorder_checkx  s   
r6  r  r  c                  C   sl   t ddgddgg} t ddgddgg}t g ddd d }| |d< ||d< tt jjt jj| d S )Nr`   ra   rb   rc   )rc   rc   ra   ra   .r]   )r$   r   r   r   r   r   r   )Z
invertibleZnon_invertibler   r'   r'   r(    test_generalized_raise_multiloop  s   r7  z?skipping test that uses fork because there are multiple threadsc                  C   sL  d} zt  }W n ttfy   td Y nw |dkrt d t d dd l}||j	d zt
jj  W n tyC   Y n tyQ   t t j Y nw zt
dgg}t
jjddd|d||dd	 W n ty } zdt|v r}t |  W Y d }~nd }~ww t t j d S t  \}}t || krtd d S d S )	N   zNot POSIX or fork failed.r   r`   r]   rV   zDORGQR parameter number 5zNumpy xerbla not linked in.)osforkOSErrorAttributeErrorr   skipcloseresourceZ	setrlimitZRLIMIT_COREr$   r   lapack_liteZxerblar   r   _exit	EX_CONFIGr   Zdorgqrr@   waitWEXITSTATUS)Z	XERBLA_OKpidr?  r/   r   statusr'   r'   r(   test_xerbla_override  sF   


rG  zCannot start subprocessc                  C   sd   ddg} t d}| D ]$}|jdd|d}ttjd|g |jdd|d}ttjd|g qd S )	NzPyQt5.QtWidgetsZIPythonz
    import sys
    {before}
    try:
        import {bad_lib}
    except ImportError:
        sys.exit(0)
    {after}
    x = np.ones(2, dtype=np.float32)
    sys.exit(0 if np.allclose(x.dot(x), 2.0) else 1)
    zimport numpy as np )beforeafterbad_libz-c)rJ  rI  rK  )textwrapdedentr  
subprocess
check_callr2  
executable)Zbad_libstemplaterK  coder'   r'   r(   test_sdot_bug_8577  s   
rS  c                   @   sd   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd Zdd Z	dd Z
dd Zdd Zdd ZdS )TestMultiDotc              	   C   sl   t jd}t jd}t jd}tt|||g||| tt|||gt |t || d S Nr*   ra   ra   r*   r$   r2  r   r   r   )rD   r#  r  Cr'   r'   r(   (test_basic_function_with_three_arguments  s
    (z5TestMultiDot.test_basic_function_with_three_argumentsc                 C   sN   t jd}t jd}tt||g|| tt||gt || d S rU  rX  )rD   r#  r  r'   r'   r(   &test_basic_function_with_two_arguments  s   z3TestMultiDot.test_basic_function_with_two_argumentsc                 C   s\   t jd}t jd}t jd}t jd}tt||||g|||| d S )NrV  rW  ra   r`   rX  )rD   r#  r  rY  Dr'   r'   r(   9test_basic_function_with_dynamic_programming_optimization  s
   ,zFTestMultiDot.test_basic_function_with_dynamic_programming_optimizationc                 C   sL   t jd}t jd}t jd}t jd}tt||||gjd d S )Nra   rW  rV  r   r  r$   r2  r   r   rq   )rD   A1dr  rY  r]  r'   r'   r(   test_vector_as_first_argument  
   z*TestMultiDot.test_vector_as_first_argumentc                 C   sL   t jd}t jd}t jd}t jd}tt||||gjd d S )NrV  rW  ra   )r*   r_  )rD   r#  r  rY  D1dr'   r'   r(   test_vector_as_last_argument  rb  z)TestMultiDot.test_vector_as_last_argumentc                 C   sL   t jd}t jd}t jd}t jd}tt||||gjd d S )Nra   rW  rV  r'   r_  )rD   r`  r  rY  rc  r'   r'   r(   &test_vector_as_first_and_last_argument$  rb  z3TestMultiDot.test_vector_as_first_and_last_argumentc              	   C   s   t jd}t jd}t jd}t d}t|||g|d}||u s&J t|||| t|t |t || d S )NrV  rW  r&   r$   r2  r   r   r   r   )rD   r#  r  rY  r&   retr'   r'   r(   test_three_arguments_and_out.  s   
z)TestMultiDot.test_three_arguments_and_outc                 C   sd   t jd}t jd}t d}t||g|d}||u sJ t||| t|t || d S )NrV  rW  )r*   r*   rf  rg  )rD   r#  r  r&   rh  r'   r'   r(   test_two_arguments_and_out;  s   
z'TestMultiDot.test_two_arguments_and_outc                 C   sz   t jd}t jd}t jd}t jd}t d}t||||g|d}||u s-J t||||| d S )NrV  rW  r\  )r*   r`   rf  rg  )rD   r#  r  rY  r]  r&   rh  r'   r'   r(   -test_dynamic_programming_optimization_and_outE  s   
 z:TestMultiDot.test_dynamic_programming_optimization_and_outc              	   C   s  t jdt jdt jdt jdt jdt jdg}t g dg dg d	g d
g dg dg}t jg dg dg dg dg dg dgtd}|d8 }t|dd\}}tt |d ddd f t |d ddd f  tt |t | d S )N)   #   )rm     )rn  rh   )rh   r  )r     )ro  r  )rR  g     @g     þ@g    O@g    1@g    @)rR  rR  g     @g     @g     ջ@g     @)rR  rR  rR  g     p@g     @g     @)rR  rR  rR  rR  g     @@g     X@)rR  rR  rR  rR  rR  g     @)rR  rR  rR  rR  rR  rR  )r   r`   r`   rb   rb   rb   )r   r   ra   rb   rb   rb   )r   r   r   rb   rb   rb   )r   r   r   r   rc   rh   )r   r   r   r   r   rh   )r   r   r   r   r   r   r4   r`   T)Zreturn_costsrk   )r$   r2  r   intr   r   r	  )rD   ZarraysZ
m_expectedZ
s_expectedr   rP  r'   r'   r(   test_dynamic_programming_logicQ  s:   







z+TestMultiDot.test_dynamic_programming_logicc                 C   s&   t ttg  t tttjdg d S )Nr!  )r   r   r   r$   r2  rJ   r'   r'   r(   test_too_few_input_arrayso  s   z&TestMultiDot.test_too_few_input_arraysN)rL   rM   rN   rZ  r[  r^  ra  rd  re  ri  rj  rk  rq  rr  r'   r'   r'   r(   rT    s    
	



rT  c                   @   sv   e Zd Zejdeddfeddfgdd Zejdd	d
gdd Z	ejdddgdd Z
dd ZdS )TestTensorinvzarr, indrc   r*   r_   ra   ra   rb   rb   ra   r`   c                 C   s<   t t tj||d W d    d S 1 sw   Y  d S )Nind)r   r   r   	tensorinv)rD   rZ  rw  r'   r'   r(   test_non_square_handlingv  s   
"z&TestTensorinv.test_non_square_handlingz
shape, ind)rc   r*   r_   rb   ra   )   r_   rb   r`   c                 C   sN   t d}||_tj||d}|j|d  |jd |  }|j}t|| d S )Nr|  r/   rw  )r$   r  rq   r   rx  r   )rD   rq   rw  r/   ainvr   actualr'   r'   r(   test_tensorinv_shape~  s   
z"TestTensorinv.test_tensorinv_shaperw  r   rl   c                 C   sL   t d}d|_tt tj||d W d    d S 1 sw   Y  d S )Nr|  rz  r}  )r$   r  rq   r   r   r   rx  )rD   rw  r/   r'   r'   r(   test_tensorinv_ind_limit  s
   

"z&TestTensorinv.test_tensorinv_ind_limitc                 C   sJ   t d}d|_tj|dd}t d}tt ||dt j|| d S )Nr|  r{  r`   rv  )	r$   r  rq   r   rx  r'  r   	tensordottensorsolve)rD   r/   r~  r0   r'   r'   r(   test_tensorinv_result  s
   

"z#TestTensorinv.test_tensorinv_resultN)rL   rM   rN   r   r   r   r$   r'  ry  r  r  r  r'   r'   r'   r(   rs  t  s     


rs  c                   @   sT   e Zd Zejdeddfeddfgdd Zejdg d	d
d Z	dS )TestTensorsolveza, axesrt  Nru  )r   ra   c                 C   sR   t t t|jd d }tj|||d W d    d S 1 s"w   Y  d S Nra   r  )r   r   r$   r'  rq   r   r  )rD   r/   r  r0   r'   r'   r(   ry    s   
"z(TestTensorsolve.test_non_square_handlingrq   ))ra   rb   r*   )rb   rc   rc   rb   )r   rb   rb   r   c                 C   sN   t jj| }t |jd d }t j||}tt j||t	|jd| d S r  )
r$   r2  r'  r'  rq   r   r  r   r  re  )rD   rq   r/   r0   r   r'   r'   r(   test_tensorsolve_result  s    z'TestTensorsolve.test_tensorsolve_result)
rL   rM   rN   r   r   r   r$   r'  ry  r  r'   r'   r'   r(   r    s    
r  c                  C   sT   t jddgddggdd} ttd t|  W d    d S 1 s#w   Y  d S )Nr`   rl   ra   rh   Zfloat16r4   zunsupported in linalg)r$   r   r   r  r   r(  )rZ  r'   r'   r(   test_unsupported_commontype  s   "r  z,Bad memory reports lead to OOM in ci testingc                  C   sR   d} t jd| gt jd}t jddgt jd}d|d< t ||}t|d d d S )N        r`   r4   )r   rk   )r$   r   rU  r'  r   r   )r[  r/   r0   r   r'   r'   r(   test_blas64_dot  s   r  z*Numpy not compiled with 64-bit BLAS/LAPACKc            
   	   C   s   t j} t jjj}d}d}|}t jddg| d}t jdg| d}t jdg| d}|||||||dd}t|d d t|d | t|d | t| }	t	d	|	  k oZd
k  d S    d S )Nl       r`   r4   rk   r   inforP  r[  r  l        )
r$   r   r   r@  Zdgeqrfr   r   rp  itemr   )
r-   Zlapack_routinerP  r[  Zldar/   Zworkr  resultsZlworkr'   r'   r(   !test_blas64_geqrf_lwork_smoketest  s   
&r  c                  C   sF   t dd} t j| }t g dg dg dg}t|| d S )N<   rb   rc   rh   )r   r*   r+      )ro     rm  &   )r  .   4   :   )r$   r   rp   r   r)  r   r   r   r  r   r'   r'   r(   test_diagonal  s   r  c                  C   s8   t dd} t j| }t g d}t|| d S )Nr  r  )$   t      )r$   r   rp   r   tracer   r   r  r'   r'   r(   
test_trace  s   r  c                  C   s   t dd} t j| | d }t g dg dg dg}t|| g d}g d}t j||}tg d}t|| ttd | d d dd f }t j|| W d    d S 1 s`w   Y  d S )	Nr  r!  r`   )rk   ra   rk   r?  r  )rw   r*   rw   z8input arrays must be \(arrays of\) 3-dimensional vectors)	r$   r   rp   r   crossr   r   r   r   )r   r  r   r  r   Zx_2dimr'   r'   r(   
test_cross  s(   

"r  c                  C   sH   t dd} t j| | dksJ t jj| | ddgddks"J d S )Nr*   r  7   r   r  )r$   r   rp   r   r  )r   r'   r'   r(   test_tensordot	  s   "r  c                  C   sD   t dd} t j| | j}t ddgddgg}t|| d S )Nr*   r  rh      r"  )r$   r   rp   r   r	   r`  r   r   r  r'   r'   r(   test_matmul!	  s   r  c                  C   st   t dd} t j| }| j}t|| ttd t j| d d df  W d    d S 1 s3w   Y  d S )Nr*   r  z$array must be at least 2-dimensionalr   )	r$   r   rp   r   Zmatrix_transposer`  r   r   r   r  r'   r'   r(   test_matrix_transpose+	  s   
"r  c                  C   s\   t dd} t j| }t|t ddd t jj| dd}t|t dggdd d S )Nr  r!  ^)ː,@rb   r2   Tr  )r$   r   rp   r   Zmatrix_normr   r   r   )r   r  r'   r'   r(   test_matrix_norm8	  s
   r  c                  C   s   t dd} t j| }t|t ddd t jj| dd}t|t g ddd t jj| d	d
}t jdddd}t	|j
|j
 t||dd d S )Nr  r!  r  rb   r  r   rE  )g|a2@gsh|? @gDioI#@Tr  r   r   r4   )r$   r   rp   r   Zvector_normr   r   r   fullr   rq   r  r'   r'   r(   test_vector_normC	  s   r  )r*   r+   )__doc__r9  r2  r   	threadingr   rL  rN  r   numpyr$   r   r   r   r   r   r   r   r	   Znumpy._corer
   Znumpy.exceptionsr   r   r   r   r   r   Znumpy.linalgr   r   r   r   r   Znumpy.linalg._linalgr   Znumpy.testingr   r   r   r   r   r   r   r   r   r    Znumpy.linalg.lapack_liteImportErrorr)   r.   r5   r7   r8   rP   r?   rT   r2  r3  rm   r   r4  rt   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r	  r  r  r  r!  r8  r<  r=  r>  rC  rD  rQ  rV  r_  r   r   ri  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r   r  r   r-  r6  r  r7  active_countrG  r   rS  rT  rs  r  r  r=  r  r7  r  r  r  r  r  r  r  r  r  r'   r'   r'   r(   <module>   s6   (0	

#




9 
!0

K	$.C
*1V6; x) #B


1" )

	

