U
    O8c/                    @   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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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+ d
d Z,e&Z-dddZ&dd Z.dd Z/dd Z0ddddddhZ1G dd dZ2dd Z3e	j45d  g Z6e6e3de2d!e
d"d#gd$d%gged&e
d#d"ged&e2d'e
d"d#gd$d%gged&e
d#d"ged&e2d(e
d"d#gd$d%gged&e
d#d"d%gd$d%d)gged&e2d*e
d+d,gd-d.gged&e
d/d+ged&e2d0e
d+d,gd-d.gged&e
d/d+ged&e2d1e
d+d,gd-d.gged&e
d/d+d2gd3d4d5gged&e2d6e	j7d7ed&e	j7d8ed&dhd9e2d:e	j48d;d;e	j48d;e2d<e	j48d=d=e	j48d=e2d>d=d?gd@dAggd?d=gg
7 Z6e6e3de2dBe
d"d#d$gd$d%d)gged&e
d#d"ged&e2dCe
d"d#gd$d%gdDd)gged&e
d#d"d$ged&e2dEe
d"d#d$gd$d%d)gged&e
d#d"ged&e2dFe
d"d#gd$d%gdDd)gged&e
d#d"d$ged&e2dGe
dHdIdJgdKdLdMgged&e
d/d+ged&e2dNe
dHdIgdJdOgdPdQgged&e
d/d+dJged&e2dRe
dHdIdJgdKdLdMgged&e
d/d+ged&e2dSe
dHdIgdJdOgdPdQgged&e
d/d+dJged&e2dTe
dHdIdJgdKdLdMgged&e
d/d+gdUdVgged&e2dWe
dHdIgdJdOgdPdQgged&e
d/d+gdUdVgdUdVgged&e2dXe	j48d;dYe	j48d;e2dZe	j48d=d[e	j48d=e2d\e	j48d[d=e	j48d[e2d]e	j48ddAe	j48ddhd9e2d^e	j48dAde	j48dAdhd9g7 Z6e6e3de2d_e
d"d#gd#d"gged&de2d`e
d"d#gd#d"gged&de2dae
d"d,gdbd=gged&de2dce
d"d,gdbd=gged&de2dde	j7d7ed&ddhd9e2ded=d?gd?d=ggde2dfe
d"d#gd#d"ggde2dge	j48d=d=dg7 Z6dhdi Z9e6e9 7 Z6djdk Z:dldm Z;e6e; 7 Z6G dndo doZ<G dpdq dqe<Z=G drds dse<Z>G dtdu due<Z?G dvdw dwe<Z@G dxdy dye<ZAG dzd{ d{e<ZBd|d} ZCd~d ZDG dd de=e@ZEG dd deEZFG dd de=e@ZGG dd deGZHG dd de=e@ZIG dd deIZJG dd de=e@ZKG dd deKZLG dd dZMG dd de=e@ZNG dd deNeMZOG dd de?eBZPG dd dePeMZQG dd de=e@ZRG dd deRZSG dd de=e>e@eAZTG dd deTZUG dd de?eBZVG dd deVZWG dd de=e@ZXG dd deXZYG dd de=e>ZZG dd deZZ[ej\]ddd dD G dd dZ^G dd de?eBZ_G dd dZ`G dd de?eBZaG dd dZbG dd dZcG dd decZdG dd decZeG ddÄ deeedZfG ddń dŃZgG ddǄ decZhG ddɄ decZiG dd˄ decZjG dd̈́ defehZkG ddτ defeiZlG ddф defejZmG ddӄ dӃZnddՄ ZoG ddׄ d׃ZpG ddل dكZqddۄ Zrej\jse+ddݍdd߄ Ztdd Zuej\jse+ddݍej\jvdd ZwG dd dZxG dd dZyG dd dZzdd Z{ej\j|ddݍdd Z}ej\j~e* ddݍdd ZdS )z# Test functions for linalg module

    N)arraysingledoublecsinglecdoubledotidentitymatmul)swapaxes)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 |ntjkS N)type
isinstancenpndarray)outZin_ r&   B/tmp/pip-unpacked-wheel-fd_gsd75/numpy/linalg/tests/test_linalg.pyconsistent_subclass   s    r(         c                 K   s8   t | jjttfk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_dtype,   s       r4   c                 C   s   t ttttttti|  S r    )r   r   r   r   r3   r&   r&   r'   get_complex_dtype1   s       r5   c                 C   s   | t tfkrdS dS d S )Ngh㈵>gdy=)r   r   r3   r&   r&   r'   get_rtol6   s    r6   square	nonsquare	hermitiangeneralizedsize-0stridedc                   @   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.   r/   	frozensettags)selfr?   r.   r/   rA   r&   r&   r'   __init__F   s
    zLinalgCase.__init__c                 C   s   || j | j| jd dS )zN
        Run the function `do` on this test case, expanding arguments
        rA   N)r.   r/   rA   )rB   dor&   r&   r'   checkQ   s    zLinalgCase.checkc                 C   s   d| j  dS )Nz<LinalgCase: >)r?   rB   r&   r&   r'   __repr__W   s    zLinalgCase.__repr__N)__name__
__module____qualname__setrC   rF   rI   r&   r&   r&   r'   r=   E   s   r=   c                 C   s,   | t kst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_tagsAssertionErrorrA   )tagZcasescaser&   r&   r'   	apply_tag[   s    rR     r         ?       @      @      @r3   r   Zdouble_2      @r   y      ?       @y       @      @y      @      @y      @      @y       @      ?r   Z	cdouble_2y      ?      @y      ?       y      ?      y      ?      Z0x0r   r   r   rD   Z8x8   Z1x1   Znonarray         Zsingle_nsq_1Zsingle_nsq_2g      @Zdouble_nsq_1Zdouble_nsq_2Zcsingle_nsq_1y      ?      ?y       @       @y      @      y      @      y      @      "@y      @       @Zcsingle_nsq_2y      @      "y      @      y      @       @Zcdouble_nsq_1Zcdouble_nsq_2Zcdouble_nsq_1_2y      ?      y       @       Zcdouble_nsq_2_2Z8x11   Z1x5   Z5x1Z0x4Z4x0ZhsingleZhdoubleZhcsingley       @      ZhcdoubleZhemptyZ	hnonarrayZmatrix_b_onlyZhmatrix_1x1c                  C   s  g } t D ]}t|jtjsqt|jd|j d|j g}|jd krLd }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 krd }n$t|jgd d d|jj }t|jd	 |||j	dhB d}| 
| q| S )
Nr]   r^      r)   Z_tile3r:   rD   r^   r]   Z_tile213)CASESr"   r.   r#   r$   r   r/   r=   r?   rA   appendreshapeshape)	new_casesrQ   r.   r/   new_caser&   r&   r'   _make_generalized_cases   s*    



$
$
rj   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rRd	|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rF| jd dkrFt| 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r`t| j}d|d< d|d< tjjj| |d}|dfV  q`dS )z<
    Generate cartesian product of strides for all axes
    ZnopNr\   )r\   r^   r\   r]   )r\   rl   c                 S   s   g | ]\}}t || qS r&   )abs).0r.   r/   r&   r&   r'   
<listcomp>  s     z%_stride_comb_iter.<locals>.<listcomp>c                 S   s   g | ]}t d d |qS r    )slice)rq   repeatr&   r&   r'   rr     s     r3   l   >[= .Zstride__c                 S   s   g | ]}d | qS )z%+dr&   )rq   jr&   r&   r'   rr   (  s     r   )stridesZstride_xxx_0Zstride_xxx_0_xr\   r\   Zstride_xxx_0_0)r"   r#   r$   ndim	itertoolsproducttupleziprg   emptyr,   viewZuint32fill	__class__r   alljoinlistrw   libZstride_tricksZ
as_strided)xZ
stride_setZrepeats	new_shapeZslicesxisr&   r&   r'   _stride_comb_iter  sF    







r   c               	   C   sn   g } t D ]`}t|jD ]P\}}t|jD ]<\}}t|jd | d | |||jdhB d}| | q(qq| S )Nru   r<   rD   )rd   r   r.   r/   r=   r?   rA   re   )rh   rQ   r.   Za_labelr/   Zb_labelri   r&   r&   r'   _make_strided_cases=  s    
r   c                   @   s"   e Zd ZeZe e fddZdS )LinalgTestCasec                 C   s   | j D ]v}|j|@ |krq|j|@ r&qz|| j W q tk
rz } z&d|d}|t 7 }t||W 5 d}~X Y qX q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_CASESrA   rF   rE   	Exception	traceback
format_excrO   )rB   requireexcluderQ   emsgr&   r&   r'   check_casesQ  s    

zLinalgTestCase.check_casesN)rJ   rK   rL   rd   r   rM   r   r&   r&   r&   r'   r   N  s   r   c                   @   s   e Zd Zdd Zdd ZdS )LinalgSquareTestCasec                 C   s   | j dhddhd d S )Nr7   r:   r;   r   r   r   rH   r&   r&   r'   test_sq_casesg  s    z"LinalgSquareTestCase.test_sq_casesc                 C   s   | j ddhdhd d S )Nr7   r;   r:   r   r   rH   r&   r&   r'   test_empty_sq_casesk  s    
z(LinalgSquareTestCase.test_empty_sq_casesN)rJ   rK   rL   r   r   r&   r&   r&   r'   r   e  s   r   c                   @   s   e Zd Zdd Zdd ZdS )LinalgNonsquareTestCasec                 C   s   | j dhddhd d S )Nr8   r:   r;   r   r   rH   r&   r&   r'   test_nonsq_casesr  s    z(LinalgNonsquareTestCase.test_nonsq_casesc                 C   s   | j ddhdhd d S )Nr8   r;   r:   r   r   rH   r&   r&   r'   test_empty_nonsq_casesv  s    
z.LinalgNonsquareTestCase.test_empty_nonsq_casesN)rJ   rK   rL   r   r   r&   r&   r&   r'   r   p  s   r   c                   @   s   e Zd Zdd Zdd ZdS )HermitianTestCasec                 C   s   | j dhddhd d S )Nr9   r:   r;   r   r   rH   r&   r&   r'   test_herm_cases}  s    z!HermitianTestCase.test_herm_casesc                 C   s   | j ddhdhd d S )Nr9   r;   r:   r   r   rH   r&   r&   r'   test_empty_herm_cases  s    
z'HermitianTestCase.test_empty_herm_casesN)rJ   rK   rL   r   r   r&   r&   r&   r'   r   {  s   r   c                   @   s,   e Zd Zejjdd Zejjdd ZdS )LinalgGeneralizedSquareTestCasec                 C   s   | j ddhdhd d S )Nr:   r7   r;   r   r   rH   r&   r&   r'   test_generalized_sq_cases  s    
z9LinalgGeneralizedSquareTestCase.test_generalized_sq_casesc                 C   s   | j dddhd d S )Nr:   r7   r;   r   r   rH   r&   r&   r'   test_generalized_empty_sq_cases  s    z?LinalgGeneralizedSquareTestCase.test_generalized_empty_sq_casesN)rJ   rK   rL   pytestmarkslowr   r   r&   r&   r&   r'   r     s   
r   c                   @   s,   e Zd Zejjdd Zejjdd ZdS )"LinalgGeneralizedNonsquareTestCasec                 C   s   | j ddhdhd d S )Nr:   r8   r;   r   r   rH   r&   r&   r'   test_generalized_nonsq_cases  s    
z?LinalgGeneralizedNonsquareTestCase.test_generalized_nonsq_casesc                 C   s   | j dddhd d S )Nr:   r8   r;   r   r   rH   r&   r&   r'   "test_generalized_empty_nonsq_cases  s    zELinalgGeneralizedNonsquareTestCase.test_generalized_empty_nonsq_casesN)rJ   rK   rL   r   r   r   r   r   r&   r&   r&   r'   r     s   
r   c                   @   s,   e Zd Zejjdd Zejjdd ZdS )HermitianGeneralizedTestCasec                 C   s   | j ddhdhd d S )Nr:   r9   r;   r   r   rH   r&   r&   r'   test_generalized_herm_cases  s    
z8HermitianGeneralizedTestCase.test_generalized_herm_casesc                 C   s   | j dddhdhd d S )Nr:   r9   r;   noner   r   rH   r&   r&   r'   !test_generalized_empty_herm_cases  s    z>HermitianGeneralizedTestCase.test_generalized_empty_herm_casesN)rJ   rK   rL   r   r   r   r   r   r&   r&   r&   r'   r     s   
r   c                 C   s   t | } | jdkr| j|jkr<| jd d |jdd   }n(| j|jd kr\| jd d }ntdtj|t| |d}tjt	t
| jd d  D ]}t| | || ||< q|S t| |S d S )Nr^   rm   r\   zNot implemented...r3   rn   )r   ry   rg   
ValueErrorr#   r~   Zcommon_typerz   r{   mapranger   )r.   r/   r   rcr&   r&   r'   dot_generalized  s    
r   c                 C   sL   t | } | jdkr:tj| j| jd}t| jd |d< |S t| jd S d S )Nr^   r3   rn   .r   )r   ry   r#   r~   rg   r,   r   )r.   r   r&   r&   r'   identity_like_generalized  s    
r   c                   @   s   e Zd Zdd ZdS )
SolveCasesc                 C   s.   t ||}t|t|| tt|| d S r    )r   solver   r   r   r(   )rB   r.   r/   rA   r   r&   r&   r'   rE     s    zSolveCases.doNrJ   rK   rL   rE   r&   r&   r&   r'   r     s   r   c                   @   s:   e Zd Zejdeeee	g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\         ?r3   )r#   r   r   r   r   r,   rB   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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dd|}t||d d ddf }t|d d ddddf |d d ddf }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                   @   s   e Zd ZdS )z,TestSolve.test_0_size.<locals>.ArraySubclassNrJ   rK   rL   r&   r&   r&   r'   ArraySubclass  s   r   r[   r]   r)   r\   r^   r   )r#   r$   arangerf   r   r   r   r   r   r"   r   r   r   rB   r   r.   r/   expectedresultr&   r&   r'   test_0_size  s*    "8
(& 2
 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                   @   s   e Zd ZdS )z.TestSolve.test_0_size_k.<locals>.ArraySubclassNr   r&   r&   r&   r'   r     s   r   r_   r\   r]   r)   r^   r   )
r#   r$   r   rf   r   r   r   r   r   r"   r   r&   r&   r'   test_0_size_k  s    ""
"8
zTestSolve.test_0_size_kN)rJ   rK   rL   r   r   parametrizer   r   r   r   r   r   r   r&   r&   r&   r'   r     s   
!r   c                   @   s   e Zd Zdd ZdS )InvCasesc                 C   s0   t |}tt||t| tt|| d S r    )r   invr   r   r   r   r(   )rB   r.   r/   rA   Za_invr&   r&   r'   rE     s
    

zInvCases.doNr   r&   r&   r&   r'   r   
  s   r   c                   @   s2   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
k t|j|j tt|| t jdt jd|}t|}t|jj	t jk t|j|j tt|| d S )Nc                   @   s   e Zd ZdS )z*TestInv.test_0_size.<locals>.ArraySubclassNr   r&   r&   r&   r'   r     s   r   r   r\   r\   r3   rY   )r#   r$   zerosint_r   r   r   r   r,   r!   float64r   rg   r"   	complex64rB   r   r.   resr&   r&   r'   r     s    

zTestInv.test_0_sizeNrJ   rK   rL   r   r   r   r   r   r   r   r   r   r&   r&   r&   r'   r     s   
r   c                   @   s   e Zd Zdd ZdS )EigvalsCasesc                 C   s&   t |}t |\}}t|| d S r    )r   eigvalseigr   )rB   r.   r/   rA   evevaluesevectorsr&   r&   r'   rE   ,  s    
zEigvalsCases.doNr   r&   r&   r&   r'   r   *  s   r   c                   @   s2   e Zd Zejdeeee	gdd Z
dd ZdS )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   r3   rm   )r#   r   r   r   r   r,   r5   r   r&   r&   r'   r   3  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
k td|j tt|t j t jdt jd|}t|}t|jj	t jk td|j tt|t j d S )Nc                   @   s   e Zd ZdS )z.TestEigvals.test_0_size.<locals>.ArraySubclassNr   r&   r&   r&   r'   r   <  s   r   r   r3   r   r\   rY   rZ   )r#   r$   r   r   r   r   r   r   r,   r!   r   r   rg   r"   r   r   r&   r&   r'   r   :  s    

zTestEigvals.test_0_sizeNr   r&   r&   r&   r'   r   2  s   
r   c                   @   s   e Zd Zdd ZdS )EigCasesc                 C   sX   t |\}}tt||t|t|dd d d f  t|jd tt	|| d S )N.rtol)
r   r   r   r   r#   r   r6   r,   r   r(   )rB   r.   r/   rA   r   r   r&   r&   r'   rE   O  s    
 zEigCases.doNr   r&   r&   r&   r'   r   M  s   r   c                   @   s2   e Zd Zejdeeee	gdd Z
dd ZdS )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,   r5   rB   r,   r   wvr&   r&   r'   r   X  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
k t|jj	t j
k t|j|j td|j tt|t j t jdt jd|}t|\}}t|jj	t jk t|jj	t jk t|j|j td|j tt|t j d S )Nc                   @   s   e Zd ZdS )z*TestEig.test_0_size.<locals>.ArraySubclassNr   r&   r&   r&   r'   r   f  s   r   r   r3   r   rY   rZ   )r#   r$   r   r   r   r   r   r   r,   r!   r   r   rg   r"   r   rB   r   r.   r   Zres_vr&   r&   r'   r   d  s    zTestEig.test_0_sizeNr   r&   r&   r&   r'   r   W  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   sx   t jddgddgg|d}t|\}}}t|j| t|jt| t|j| tj|d| jd}t|jt| d S )Nr\   r   r3   F
compute_uvr9   )r#   r   r   svdr   r,   r4   r9   )rB   r,   r   ur   vhr&   r&   r'   r   ~  s    zSVDBaseTests.test_typesN)rJ   rK   rL   r9   r   r   r   r   r   r   r   r   r&   r&   r&   r'   r   {  s   r   c                   @   s   e Zd Zdd ZdS )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   r6   r,   r   r(   )rB   r.   r/   rA   r   r   vtr&   r&   r'   rE     s    &zSVDCases.doNr   r&   r&   r&   r'   r     s   r   c                   @   s   e Zd Zdd ZdS )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 )r_   r   Tr   r_   r_   rY   r_   )r   r_   N)r#   r~   r   r   r9   r   rg   eye)rB   r   r   r   r   r&   r&   r'   test_empty_identity  s    

zTestSVD.test_empty_identityN)rJ   rK   rL   r   r&   r&   r&   r'   r     s   r   c                   @   s   e Zd Zdd ZdS )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r9   .r   c                 S   s<   t t| j}|d |d  |d< |d< ttj| |dS )Nrn   rm   axes)r   r   ry   r#   conj	transpose)matr   r&   r&   r'   r9     s    z'SVDHermitianCases.do.<locals>.hermitianrm   )r   r   r   r   r#   r   r6   r,   r   r	   broadcast_tor   rg   r   sortr   r(   )rB   r.   r/   rA   r   r   r   r9   r&   r&   r'   rE     s    &,,zSVDHermitianCases.doNr   r&   r&   r&   r'   r     s   r   c                   @   s   e Zd ZdZdS )TestSVDHermitianTN)rJ   rK   rL   r9   r&   r&   r&   r'   r     s   r   c                   @   s   e Zd Zdd ZdS )	CondCasesc                 C   s  t |}d|k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 )Nr;   F)r   ).r   ).rm   ra   r`   r0   r1   r]   rn   r\   rm   fro)r   r   r   r   condr   r   r#   r   rp   summaxminr   sqrt)rB   r.   r/   rA   r   r   Zcinvr&   r&   r'   rE     sp      
  
  
& 
& & & 
 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 dddgdddgdddgg}tt|td tt|t d tt|dd tt|dd	 tt|d
td d S )NrT   r   r\   g       rV   r_   gUUUUUU?rm   r   r   gUUUUU6@)r   r   r   r   r   r#   r   rB   Ar&   r&   r'   test_basic_nonsvd  s     zTestCond.test_basic_nonsvdc                 C   sx   t dt dg}d dddg}ddg}t||D ]\}}tt||dk q4t||D ]\}}t|| q^d S )Nr]   r]   r\   r]   r   rm   rn     4&kC)r#   r   onesrz   r{   r   r   r   )rB   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   d dddddg}d dddg}t d}t j|d< |D ].}t||}tt|t j tt | q4t d}t j|d	< |D ]t}t||}tt |d  ||krt|d
 dk t|d dk q|tt |d
   tt |d   q|d S )Nr\   rm   r]   rn   r   r  r   )r^   r]   r]   )r\   r   r\   r   r  )	r#   r  nanr   r   r   r"   Zfloat_isnan)rB   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||}t|d t j t|d t j tt 	|d  tt 	|d  q2d S )	NrS   r]   r   rY   rx   )Nr\   r]   r   rm   rn   r   r\   r   )
r#   randomseedrandr   r   r   r   r   isfinite)rB   r  r	  r   r&   r&   r'   test_stacked_singular  s    zTestCond.test_stacked_singularN)
rJ   rK   rL   r  r
  r   r   xfailr  r  r&   r&   r&   r'   r    s   	

r  c                   @   s   e Zd Zdd ZdS )	PinvCasesc                 C   s<   t |}t}t||||||ddd tt|| d S )Nra   r`   r   r   pinvr   r   r   r(   rB   r.   r/   rA   Za_ginvr   r&   r&   r'   rE   1  s    
zPinvCases.doNr   r&   r&   r&   r'   r  ,  s   r  c                   @   s   e Zd ZdS )TestPinvNr   r&   r&   r&   r'   r  9  s   r  c                   @   s   e Zd Zdd ZdS )PinvHermitianCasesc                 C   s@   t j|dd}t}t||||||ddd tt|| d S )NTr   ra   r`   r   r  r  r&   r&   r'   rE   ?  s    zPinvHermitianCases.doNr   r&   r&   r&   r'   r  =  s   r  c                   @   s   e Zd ZdS )TestPinvHermitianNr   r&   r&   r&   r'   r  G  s   r  c                   @   s   e Zd Zdd ZdS )DetCasesc           
      C   s   t |}t |\}}t|jjttfk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 )Nrm   axisr   r\   )r   detslogdetr   r,   r!   r   r   astyper   r   r   r   reducer#   expZ
atleast_1drp   r   r   )
rB   r.   r/   rA   dr   Zldadr   mr&   r&   r'   rE   M  s    



zDetCases.doNr   r&   r&   r&   r'   r  K  s   r  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   r!  r!   r   r   r"  r   rH   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   r!  r,   r"  r4   )rB   r,   r   phr   r&   r&   r'   r   m  s
    zTestDet.test_typesc                 C   s   t jdt jd}t|}t|d t|jjt jk t	|}t|d t|d jjt jk t|d jjt j
k t jdt jd}t|}t|d t|jjt jk t	|}t|d t|d jjt jk t|d jjt jk d S )NrY   r3   rT   r  r   r\   )r#   r   r   r   r!  r   r   r,   r!   r"  float32r   )rB   r.   r   r&   r&   r'   r   u  s     







zTestDet.test_0_sizeN)rJ   rK   rL   r+  r   r   r   r   r   r   r   r   r   r&   r&   r&   r'   r)  _  s   
r)  c                   @   s   e Zd Zdd ZdS )
LstsqCasesc                 C   sL  t |}|j\}}t|d\}}}	tj||dd\}
}}}|dkrVt|
dk  ||krzt|t	||
 t
|| n
t
|| t||| ||kr||krt tt 	||
| d jdd}t |}t |jdkrd|_t
|j|j nt g t|
}t|| tt |jt j tt|
| tt|| d S )	NFrm   Zrcondr   r]   r  r\   rk   )r#   r   rg   r   r   lstsqr   r   r   r   r   Z__array_wrap__rp   r   ry   r   r   r!   
issubdtyper,   floatingr(   )rB   r.   r/   rA   arrr(  nr   r   r   r   	residualsranksvZexpect_residsr&   r&   r'   rE     s2    




zLstsqCases.doNr   r&   r&   r&   r'   r.    s   r.  c                
   @   sF   e Zd Zdd Zejdddgdddd	d
ddgdd Zdd ZdS )	TestLstsqc           	      C   s   t ddddddgddddddgddddddgddddddggj}t ddddddg}t }|td}t||\}}}}t|d	k tj||d
d\}}}}t|d	k tj||d d\}}}}t|dk tt	|dk W 5 Q R X d S )Nr*  rT   rU   rW   rV   r\   r   z`rcond` parameter will changer_   rm   r/  r^   )
r#   r   Tr   recordFutureWarningr   r0  r   len)	rB   r.   r/   supr   r   r5  r6  r   r&   r&   r'   test_future_rcond  s    zTestLstsq.test_future_rcondr(  r4  n_rhs)r_   r]   r]   )r   r_   r\   )r   r_   r]   )r_   r   r\   )r_   r   r]   )r_   r]   r   )r   r   r   c                 C   s   t || ||}t ||f}tj||d d\}}}}	|dkrTt|dk  t|j	||f t|j	||krx|fnd ||kr|dkr|t 
|| }
t||
|
 jdd t|t|| t|	j	t||f d S )Nr/  r   rZ   rn   r  )r#   r   rf   r  r   r0  r   r   r   rg   r   r   r   r   )rB   r(  r4  r?  r.   r/   r   r5  r6  r   r   r&   r&   r'   test_empty_a_b  s    
zTestLstsq.test_empty_a_bc              	   C   sj   t ddddg}t ddddd	g}t |t t|gj}ttd
 tj	||d d W 5 Q R X d S )Nr   r\   r]   r^   rm   g?g?g @gffffff
@zIncompatible dimensionsr/  )
r#   r   Zvstackr  r<  r9  r   r   r   r0  )rB   r   yr  r&   r&   r'   test_incompatible_dims  s
    z TestLstsq.test_incompatible_dimsN)	rJ   rK   rL   r>  r   r   r   r@  rB  r&   r&   r&   r'   r8    s   	
r8  dtc                 C   s   g | ]}t |qS r&   )r#   r,   )rq   r   r&   r&   r'   rr     s     rr   z?bBhHiIqQefdgFDGOc                   @   s   e Zd ZedZeddddg Zeddddg Zeddddg 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 )TestMatrixPowerr_   r^   r   r\   r]   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 )Nr]   d   i       r   r\   r^   )rshft_1r#  r   r   rshft_0rshft_2rshft_3)rB   rC  Zrshftr&   r&   r'   test_large_power  s"        z TestMatrixPower.test_large_powerc                 C   s>   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_allr#  objectstackedrB   rC  rP  r   r&   r&   r'   test_power_is_zero  s
    
z"TestMatrixPower.test_power_is_zeroc                 C   s>   dd }| j D ]*}||| |tkr|| j| qd S )Nc                 S   s&   t | d}t||  t|j| j d S )Nr\   )r   r   r,   )r   rO  r&   r&   r'   rP    s    

z-TestMatrixPower.test_power_is_one.<locals>.tzrQ  rU  r&   r&   r'   test_power_is_one  s
    
z!TestMatrixPower.test_power_is_onec                 C   s>   dd }| j D ]*}||| |tkr|| j| qd S )Nc                 S   s>   t | d}| jtkrtnt}t||| |  t|j| j d S )Nr]   )r   r,   rS  r	   r   r   )r   rO  mmulr&   r&   r'   rP  
  s    
z-TestMatrixPower.test_power_is_two.<locals>.tzrQ  rU  r&   r&   r'   test_power_is_two	  s
    
z!TestMatrixPower.test_power_is_twoc                 C   s0   dd }| j D ]}|| jkr||| qd S )Nc                 S   s4   t | d}| jtkrtnt}t||| t|  d S Nrm   )r   r,   rS  r	   r   r   r   )r   ZinvmatrX  r&   r&   r'   rP    s    
 z3TestMatrixPower.test_power_is_minus_one.<locals>.tz)rR  dtnoinvr#  rU  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\   )rJ  r#  r   	TypeErrorr   rB   rC  r   r&   r&   r'   test_exceptions_bad_power   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\   r]   )r_   r^   r]   )r   r   r   r#   r   r  )rB   rC  r&   r&   r'   test_exceptions_non_square%  s    z*TestMatrixPower.test_exceptions_non_squarefp errors don't work in wasmr  c                 C   s,   || j krd S | j|}ttt|d d S rZ  )r[  noninvr#  r   r   r   r^  r&   r&   r'   test_exceptions_not_invertible*  s    
z.TestMatrixPower.test_exceptions_not_invertibleN)rJ   rK   rL   r#   r   rJ  rI  rK  rL  rR  r   rc  blockrT  rS  r,   r[  rM  rV  rW  rY  r\  r_  r`  r   r   skipifr   rd  r&   r&   r&   r'   rD    s"   
rD  c                   @   s   e Zd Zdd ZdS )TestEigvalshCasesc                 C   s^   t |d}t |\}}|jdd t||t|jd t |d}t||t|jd d S )NLrm   r  r   U)r   eigvalshr   r   r   r6   r,   )rB   r.   r/   rA   r   r   r   ev2r&   r&   r'   rE   4  s    zTestEigvalshCases.doNr   r&   r&   r&   r'   rg  2  s   rg  c                   @   sB   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 )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   rj  r   r,   r4   )rB   r,   r   r   r&   r&   r'   r   A  s    zTestEigvalsh.test_typesc                 C   sX   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   r3   ZlrongZUPLOlowerupper)r#   r   r-  r   r   r   rj  rB   r   r&   r&   r'   test_invalidG  s    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\   r3   rm   r   rh  rn  lri  r   )r#   r   r   r6   r   rj  r   )rB   KloKuptgtr   r   r&   r&   r'   	test_UPLOM  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
k td|j tt|t j t jdt jd|}t|}t|jj	t jk td|j tt|t j d S )Nc                   @   s   e Zd ZdS )z/TestEigvalsh.test_0_size.<locals>.ArraySubclassNr   r&   r&   r&   r'   r   e  s   r   r   r3   r   rY   rZ   )r#   r$   r   r   r   r   rj  r   r,   r!   r   r   rg   r"   r   r-  r   r&   r&   r'   r   c  s    

zTestEigvalsh.test_0_sizeNrJ   rK   rL   r   r   r   r   r   r   r   r   rr  rx  r   r&   r&   r&   r'   rl  @  s
   
rl  c                   @   s   e Zd Zdd ZdS )TestEighCasesc           
      C   s   t |\}}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 )Nrm   r  .r   ri  )r   err_msg)r   eighr   r   r   r   r   r#   r   r6   r,   repr)
rB   r.   r/   rA   r   Zevcr   r   rk  Zevc2r&   r&   r'   rE   x  s    

 

  zTestEighCases.doNr   r&   r&   r&   r'   rz  v  s   rz  c                   @   sB   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 )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,   r4   r   r&   r&   r'   r     s    zTestEigh.test_typesc                 C   sX   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 rm  )r#   r   r-  r   r   r   r|  rq  r&   r&   r'   rr    s    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 rs  )r#   r   r   r6   r   r|  r   )rB   ru  rv  rw  r   r   r   r&   r&   r'   rx    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
k t|jj	t j
k t|j|j td|j tt|t j t jdt jd|}t|\}}t|jj	t jk t|jj	t jk t|j|j td|j tt|t j d S )Nc                   @   s   e Zd ZdS )z+TestEigh.test_0_size.<locals>.ArraySubclassNr   r&   r&   r&   r'   r     s   r   r   r3   r   rY   rZ   )r#   r$   r   r   r   r   r|  r   r,   r!   r   r   rg   r"   r   r-  r   r&   r&   r'   r     s    zTestEigh.test_0_sizeNry  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 ntt |jjtj d S r    )	
issubclassr,   r!   r#   Zinexactr   realr   r2  )r   r   r&   r&   r'   check_dtype  s    z_TestNormBase.check_dtype)rJ   rK   rL   rC  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 )Nr*  r3   )r   r   r   rC  r   rH   r&   r&   r'   
test_empty  s    z_TestNormGeneral.test_emptyc           	   
   C   sr  t dddg}t jd }t jd }|| }|D ]:}||}t|t j }| || t|d t 2}|	t
d t|d}| || t|d W 5 Q R X 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 q0d S )Nr\   r   
AllIntegerZAllFloatr*  divide by zero encounteredrm   r]   rU   r   r_   g      ?rT   )r#   r   	typecodesr#  r   r   r  r   r   filterRuntimeWarningr,   r!   	rB   r.   Zexact_typesZinexact_typesZ	all_typesZ	each_typeatZanr=  r&   r&   r'   test_vector_return_type  s:    











z(_TestNormGeneral.test_vector_return_typec                    s   ddddg}ddddg}ddddg} fd	d
}|||fD ]}|| q:t | jdt | jdt | jdfD ]}|| qpd S )Nr\   r]   r^   r_   rm   rn   ro   rl   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+   rW   rT   r\         $@rm   gQ?r]   rn   gsc?r   r_   )r#   testingr   r   r  r   )r   rH   r&   r'   _test	  s0    z+_TestNormGeneral.test_vector.<locals>._testr3   )r   rC  )rB   r.   r/   r   r  r   r&   rH   r'   test_vector  s    
z_TestNormGeneral.test_vectorc           	   	      s  t dddgdddgg| jd d dd	dddtjtj fD ]h fd
dt jd D }tt d	d|  fddt jd	 D }tt dd| q8tjdd| jd	dddj
}d ddddtjtj dfD ]؉tt| |dD ]}|\}}|d	k r||7 }|d	k r&||7 }||krDttt|d qt|d}|||  ||k rfddtj D }n fddtj D }t|| qqd S )Nr\   r]   r^   r_   ra   r)   r3   rm   r   c                    s$   g | ]}t  d d |f dqS Nordr   rq   kr  orderr&   r'   rr   (  s     z._TestNormGeneral.test_axis.<locals>.<listcomp>r  r   c                    s$   g | ]}t  |d d f dqS r  r  r  r  r&   r'   rr   *  s        rn   r   c                    s*   g | ]"}t  d d  j|ddqS Nr  r  )r   taker  Bk_indexr  r&   r'   rr   @  s   c                    s,   g | ]$}t  d d  j|djdqS r  )r   r  r9  r  r  r&   r'   rr   C  s   )r   rC  r#   Infr   rg   r   r   r   rf   ry   rz   combinationsr   r   )	rB   Z	expected0Z	expected1Zndr   Zrow_axisZcol_axisr4  r   r&   )r  r  r  r  r'   	test_axis"  s6    



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 ]}t|jD ]v}t|||d	}t|||d
d}tt |||||d t|j	}d||< t|}t|j	|k||j	||| qqd ddddt j
t j
 ddf	D ]}tt|jdD ]}t|||d	}t|||d
d}tt |||||d t|j	}d||d < d||d < t|}t|j	|k||j	||| qTq>d S )Nr\   r  r3   r]   r^   r_   zorder {0}, axis = {1}z;Shape mismatch found {0}, expected {1}, order={2}, axis={3}r  T)r  r   Zkeepdims)r{  )r\   r\   r\   rm   r   rn   r   nuc)r#   r   rC  rf   r   r   Zsqueezeformatr   rg   r  r   ry   r   r|   rz   permutations)	rB   r  Zallclose_errZ	shape_errr   foundZexpected_shaper  r  r&   r&   r'   test_keepdimsG  sN    




 


z_TestNormGeneral.test_keepdimsN)rJ   rK   rL   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 )Nr3   r*  )r   r   r   rC  rH   r&   r&   r'   test_matrix_emptyw  s    z_TestNorm2D.test_matrix_emptyc           	   
   C   sv  |  dddgdddgg}tjd }d}|| }|D ]:}||}t|tj }| || t|d t 2}|	t
d t|d}| || t|d W 5 Q R X 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 q4d S )Nr\   r   r  ZfdFDrU   r  rm   rT   r]   gLXz?rn   r   r  gU&,t=@r)   r  )r   r#   r  r#  r   r   r  r   r   r  r  r  r  r&   r&   r'   test_matrix_return_typez  s@    














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\   r^   ra   rb   r3   gT"@r   r  r  g      (@rW   rm   rX   r]   gW?"@rn   gїʄ?Znofroro   r   )r   rC  r   r   r   r   r   r  r&   r&   r'   test_matrix_2x2  s    z_TestNorm2D.test_matrix_2x2c                 C   s   d| j dddgdddgd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 d S )Ng?r\   r]   r^   r)   r   ra   r3   grj=Q0?r   r  gB+pc?g?g333333?rT   rm   g?g&.d?rn   gܸՠ?)r   rC  r   r   r   r  r&   r&   r'   test_matrix_3x3  s    &z_TestNorm2D.test_matrix_3x3c                 C   s
  | j dddgdddg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 ]4}ttt||d  ttt||d ttt||d qttjt|d d ttjt|d d ttt|d d d S )Nr\   r]   r^   r_   ra   r)   r3   r  r   r   r  testr   r^   r   )r\   r]   r]   r^   )r   r\   r]   )	r   rC  r#   r   rf   r   r   r   Z	AxisError)rB   r  r  r  r&   r&   r'   test_bad_args  s     z_TestNorm2D.test_bad_argsN)
rJ   rK   rL   r#   r   r  r  r  r  r  r&   r&   r&   r'   r  r  s   3r  c                   @   s   e Zd ZdS )	_TestNormNr   r&   r&   r&   r'   r    s   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
   r3   r^   r  gL)@r]   r  )r#   r   Z
longdoubler-   r   rq  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   r3   r^   r  l        ra   r  )r#   r   Zint32r-   r   rq  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r]   r3   y      @      @r   y            @r\   gV;'@r^   r  r  r  	   ra   )	r#   r~   Zclongdoubler-   r   r   r#  
complex128r   )rB   r&  r   r&   r&   r'   test_complex_high_ord  s    z,TestNorm_NonSystematic.test_complex_high_ordN)rJ   rK   rL   r  r  r  r&   r&   r&   r'   r    s   r  c                   @   s   e Zd ZejZdZdS )_TestNormDoubleBaser*   N)rJ   rK   rL   r#   r   rC  r  r&   r&   r&   r'   r  
  s   r  c                   @   s   e Zd ZejZdZdS )_TestNormSingleBaser)   N)rJ   rK   rL   r#   r-  rC  r  r&   r&   r&   r'   r    s   r  c                   @   s   e Zd ZejZdZdS )_TestNormInt64Baser*   N)rJ   rK   rL   r#   Zint64rC  r  r&   r&   r&   r'   r    s   r  c                   @   s   e Zd ZdS )TestNormDoubleNr   r&   r&   r&   r'   r    s   r  c                   @   s   e Zd ZdS )TestNormSingleNr   r&   r&   r&   r'   r    s   r  c                   @   s   e Zd ZdS )TestNormInt64Nr   r&   r&   r&   r'   r  !  s   r  c                   @   s   e Zd Zdd Zdd ZdS )TestMatrixRankc                 C   s   t dttd td}d|d< t t|d t ttdd t tddddgd t ttdd t tdgd t|tdtdg}t t|tdddg t tdd d S )	Nr_   r*  rm   rm   r^   r   r   r\   )r_   )r   r   r#   r   r   r   )rB   Imsr&   r&   r'   test_matrix_rank'  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 )Nr_   Tr   r\   r   r   r*  r  r^   g:0yE>g|"BE>)r9   Ztolg;䡈E>)r   r   r#   r   r  r   )rB   r  r&   r&   r'   test_symmetric_rank;  s    
z"TestMatrixRank.test_symmetric_rankN)rJ   rK   rL   r  r  r&   r&   r&   r'   r  %  s   r  c                  C   s   t jd} tdD ]}| 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rG  )(   r  sizer\   r]   r   r  r_   ra   r^   r[   )r#   r  ZRandomStater   normalr   r   )rngiXr&   r&   r'   test_reduced_rankI  s    ,,r  c                	   @   s   e Zd ZejZdd Zejddgdddgdd	 Z	d
d Z
dd Zdd Zejddddddgejddddg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\}}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,   rg   r   r   qrr   r"   r   r   r9  r   r#   r   triu)rB   r.   a_typea_dtyper(  r4  r  qr   q1r1r2r&   r&   r'   check_qrZ  s8    

zTestQR.check_qrr(  r4  )r^   r   r  rY   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   rg   )rB   r(  r4  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\   r]   r^   r_   ra   r)   r3   r  r  r  r  rc   )	r   r#   r   r   r  r   r,   rg   r9  )rB   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 ]8}||}||}| | | | | |j q6dD ]H}dd||  }dd||  }| | | | | |j qtd S )	Nr\   r]   r^   r_   ra   r)   fd              ?)r   r#  r  r9  )rB   r.   r/   rC  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 )	Nrn   r  r  rm   r]   .r  r   )r!   r,   rg   r   r   r  r   r"   r   r	   r#   r   r   r
   r   r  )rB   r.   r  r  r(  r4  r  r  r   ZI_matZstack_I_matr  r  r  r&   r&   r'   check_qr_stacked  sL    
""zTestQR.check_qr_stackedr  )r^   r_   )r_   r^   r   
outer_sizer  r  )r]   r^   r_   rC  c                 C   sP   t jj|| d|}t jj|| d|}| | | |d|   d S )Nr  r  )r#   r  r  r#  r  )rB   r  r  rC  r  r  r&   r&   r'   test_stacked_inputs  s    

zTestQR.test_stacked_inputsN)rJ   rK   rL   r#   r   r  r   r   r   r  r  r  r  r   r   r   r   r  r&   r&   r&   r'   r  V  s<   '
-       r  c                   @   sR   e Zd Zejddddddgejdejejej	ej
fdd	 Zd
d ZdS )TestCholeskyrg   rx   r  r^   r^   )2   r  )r^   r  r  r,   c                 C   s   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|}t 	||
| }t    d|jd  t |j }W 5 Q R X t|||| d| d	| d	| d
 d S )Nr\   r  )rm   rn   rn   r3   i  r    
)atolr{  )r#   r  r  randnr1  Zcomplexfloatingr   r   r<  r	   r   r   r   r   choleskyZ_no_nep50_warningrg   ZfinfoZepsr   )rB   rg   r,   r.   tr   r/   r  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k t	t|t j t jdt jd|}t|}t|j|j t	|j
jt jk t	t|t j d S )Nc                   @   s   e Zd ZdS )z/TestCholesky.test_0_size.<locals>.ArraySubclassNr   r&   r&   r&   r'   r     s   r   r   r3   )r\   r   r   )r#   r$   r   r   r   r   r  r   rg   r   r,   r!   r   r"   r   r   r&   r&   r'   r     s    

zTestCholesky.test_0_sizeN)rJ   rK   rL   r   r   r   r#   r-  r   r   r  r  r   r&   r&   r&   r'   r    s     r  c                  C   s   t jdkrd} nd} tjtjfD ]r}tjd|d}|| }|d }t|j	jd t
jt
jt
jfD ](}||}t||| t||| qhq d S )Nlittle<rG   r_   r3   S=)sys	byteorderr#   r-  r   r   ZnewbyteorderZbyteswapr   r,   r   r   r!  r  r   )ZnativeZdttr3  Zn_arrZsw_arrZroutiner   r&   r&   r'   test_byteorder_check$  s    

r  ra  rb  c                  C   sp   t ddgddgg} t ddgddgg}t ddddgdd d }| |d< ||d< tt jjt jj| d S )Nr\   r]   r^   r_   .rY   )r#   r   r   r   r   r   r   )Z
invertibleZnon_invertibler   r&   r&   r'    test_generalized_raise_multiloop9  s    r  c                  C   sR  d} zt  }W n" ttfk
r2   td Y nX |dkr(t d t d dd l}||j	d zt
jj  W n2 tk
r   Y n  tk
r   t t j Y nX z0t
dgg}t
jjddd|d||dd	 W n< tk
r } zdt|krt |  W 5 d }~X Y nX t t j n&t  \}}t || krNtd d S )	N   zNot POSIX or fork failed.r   r\   rY   rT   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_overrideH  sH    


      r  zCannot start subprocessc                  C   sd   ddg} t d}| D ]H}|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)r  r  r  )textwrapdedentr  
subprocess
check_callr  
executable)Zbad_libstemplater  coder&   r&   r'   test_sdot_bug_8577y  s    
r  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)   r]   r]   r)   r#   r  r   r   r   )rB   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 r  r  )rB   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 )Nr  r  r]   r\   r  )rB   r  r  r  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 )Nr]   r  r  r  r  r#   r  r   r   rg   )rB   A1dr  r  r#  r&   r&   r'   test_vector_as_first_argument  s
    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 )Nr  r  r]   )r)   r%  )rB   r  r  r  D1dr&   r&   r'   test_vector_as_last_argument  s
    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 )Nr]   r  r  r&   r%  )rB   r&  r  r  r(  r&   r&   r'   &test_vector_as_first_and_last_argument  s
    z3TestMultiDot.test_vector_as_first_and_last_argumentc              	   C   s   t jd}t jd}t jd}t d}t|||g|d}||ksLtt|||| t|t |t || d S )Nr  r  r%   r#   r  r   r   rO   r   r   )rB   r  r  r  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}||ks>tt||| t|t || d S )Nr  r  )r)   r)   r+  r,  )rB   r  r  r%   r-  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}||ksZtt||||| d S )Nr  r  r"  )r)   r\   r+  r,  )rB   r  r  r  r#  r%   r-  r&   r&   r'   -test_dynamic_programming_optimization_and_out  s    
z:TestMultiDot.test_dynamic_programming_optimization_and_outc                 C   sh  t jdt jdt jdt jdt jdt jdg}t ddd	d
ddgddddddgddddddgddddddgddddddgddddddgg}t jddddddgddddddgddddddgddddddgddddddgddddddg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)   #   )r2     )r3  ra   )ra   r  )r     )r4  r  r*  g     @g     þ@g    O@g    1@g    @g     @g     @g     ջ@g     @g     p@g     @g     @g     @@g     X@g     @r   r\   r^   r]   r_   ra   r3   T)Zreturn_costsrm   )r#   r  r   intr   r   r  )rB   ZarraysZ
m_expectedZ
s_expectedr   r(  r&   r&   r'   test_dynamic_programming_logic  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#   r  rH   r&   r&   r'   test_too_few_input_arrays  s    z&TestMultiDot.test_too_few_input_arraysN)rJ   rK   rL   r   r!  r$  r'  r)  r*  r.  r/  r0  r6  r7  r&   r&   r&   r'   r    s   
	



r  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r_   r)   r[   r]   r]   r^   r^   r]   r\   c              	   C   s&   t t tj||d W 5 Q R X d S )Nind)r   r   r   	tensorinv)rB   r3  r<  r&   r&   r'   test_non_square_handling   s    
z&TestTensorinv.test_non_square_handlingz
shape, ind)r_   r)   r[   r^   r]   )   r[   r^   r\   c                 C   sN   t d}||_tj||d}|j|d  |jd |  }|j}t|| d S )NrA  r.   r<  )r#   r   rg   r   r=  r   )rB   rg   r<  r.   ainvr   actualr&   r&   r'   test_tensorinv_shape(  s    
z"TestTensorinv.test_tensorinv_shaper<  r   rn   c              	   C   s6   t d}d|_tt tj||d W 5 Q R X d S )NrA  r?  rB  )r#   r   rg   r   r   r   r=  )rB   r<  r.   r&   r&   r'   test_tensorinv_ind_limit5  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 )NrA  r@  r\   r;  )	r#   r   rg   r   r=  r  r   	tensordottensorsolve)rB   r.   rC  r/   r&   r&   r'   test_tensorinv_result>  s
    

z#TestTensorinv.test_tensorinv_resultN)rJ   rK   rL   r   r   r   r#   r  r>  rE  rF  rI  r&   r&   r&   r'   r8    s    

 
r8  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
dgdd Z	dS )TestTensorsolveza, axesr9  Nr:  )r   r]   c              	   C   s<   t t* t|jd d }tj|||d W 5 Q R X d S Nr]   r   )r   r   r#   r  rg   r   rH  )rB   r.   r   r/   r&   r&   r'   r>  I  s    
z(TestTensorsolve.test_non_square_handlingrg   )r]   r^   r)   )r^   r_   r_   r^   )r   r^   r^   r   c                 C   sN   t jj| }t |jd d }t j||}tt j||t	|jd| d S rK  )
r#   r  r  r  rg   r   rH  r   rG  r<  )rB   rg   r.   r/   r   r&   r&   r'   test_tensorsolve_resultR  s    z'TestTensorsolve.test_tensorsolve_result)
rJ   rK   rL   r   r   r   r#   r  r>  rL  r&   r&   r&   r'   rJ  G  s   
rJ  c               	   C   s>   t jddgddggdd} ttd t|  W 5 Q R X d S )Nr\   rn   r]   ra   Zfloat16r3   zunsupported in linalg)r#   r   r   r]  r   r  )r3  r&   r&   r'   test_unsupported_commontype\  s    rM  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\   r3   )r   rm   )r#   r   r-  r  r   r   )r4  r.   r/   r   r&   r&   r'   test_blas64_dotg  s    rO  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 od
k n   d S )Nl       r\   r3   rm   r   infor(  r4  rN  l        )
r#   r   r   r  Zdgeqrfr   r   r5  itemr   )
r,   Zlapack_routiner(  r4  Zldar.   Zworkr  resultsZlworkr&   r&   r'   !test_blas64_geqrf_lwork_smoketestq  s    
rS  )r)   r*   )__doc__r  r  rz   r   r  r  r   Znumpyr#   r   r   r   r   r   r   r   r	   Z
numpy.corer
   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   r(   r-   r4   r5   r6   rN   r=   rR   r  r  rd   r~   r  rj   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   r  r  r  r  r  r  r)  r.  r8  r   r   rD  rg  rl  rz  r~  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  rf  r  r  r   r  r  r8  rJ  rM  r  rO  r  rS  r&   r&   r&   r'   <module>   s$  (0

    

#      




9 
0

:	
$.C
*4V6; x$ !.
1" )
	
