U
    HvfVh                     @   s  d dl Z d dlZd dlmZmZmZmZmZmZ d dl	m
Z
mZmZmZ d dl mZ d dl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mZm Z m!Z!m"Z"m#Z#m$Z$m%Z%m&Z&m'Z'm(Z(m)Z) d dl*m+Z+ d	d
 Z,G dd dZ-G dd dZ.G dd dZ/G dd dZ0G dd dZ1G dd dZ2G dd dZ3G dd dZ4G dd dZ5G dd dZ6G dd  d Z7G d!d" d"Z8G d#d$ d$Z9G d%d& d&Z:G d'd( d(Z;d)d* Z<d+d, Z=d-d. Z>d/d0 Z?G d1d2 d2Z@dS )3    N)arangeaddarrayeyecopysqrt)assert_equalassert_array_equalassert_array_almost_equalassert_allclose)raises)fft)comb)toeplitzhankel	circulanthadamardlesliedft	companiontritriutrilkron
block_diaghelmerthilbert
invhilbertpascal	invpascalfiedlerfiedler_companioneigvalsconvolution_matrix)condc                 C   s   t | }t||}|S N)r   r   outer)ndata r)   L/tmp/pip-unpacked-wheel-96ln3f52/scipy/linalg/tests/test_special_matrices.pyget_mat   s    r+   c                   @   s,   e Zd Zdd Zdd Zdd Zdd Zd	S )
TestTric              
   C   s~   t tdtddddgddddgddddgddddgg t tdddtddddgddddgddddgddddggd d S )N      r   fdtyper   r   r   selfr)   r)   r*   
test_basic   s    





zTestTri.test_basicc              
   C   s   t tdddtddddgddddgddddgddddgg t tdddtddddgddddgddddgddddgg d S )Nr-   r.   kr   r2   r3   r)   r)   r*   	test_diag$   s    





zTestTri.test_diagc              	   C   sj   t tddtdddgdddgdddgdddgg t tddtddddgddddgddddgg d S )Nr-      r.   r   r2   r3   r)   r)   r*   test_2d.   s    

zTestTri.test_2dc              	   C   sr   t tddddtddddgddddgddddgg t tddddtdddgdddgdddgdddgg d S )Nr:   r-      r6   r.   r   r2   r3   r)   r)   r*   test_diag2d7   s    

zTestTri.test_diag2dN)__name__
__module____qualname__r5   r9   r;   r>   r)   r)   r)   r*   r,      s   

	r,   c                   @   s   e Zd Zdd Zdd ZdS )TestTrilc                 C   sZ   dt d d}| }tdD ]$}t|d dD ]}d|||f< q4q"tt|| d S Nd      lr.   r   )r+   astyper   ranger   r   r4   abr7   rF   r)   r)   r*   r5   B   s    zTestTril.test_basicc                 C   s   dt d d}| }tdD ]$}t|d dD ]}d|||f< q4q"tt|dd| | }tdD ],}tt|d dfdD ]}d|||f< qqjtt|d	d| d S )
NrD   rE   r/   r:   r   r<   r6   r.   r=   )r+   rG   r   rH   r   r   maxrI   r)   r)   r*   r9   J   s    zTestTril.test_diagNr?   r@   rA   r5   r9   r)   r)   r)   r*   rB   A   s   rB   c                   @   s   e Zd Zdd Zdd ZdS )TestTriuc                 C   sZ   dt d d}| }tdD ]$}t|d dD ]}d|||f< q4q"tt|| d S rC   )r+   rG   r   rH   r   r   rI   r)   r)   r*   r5   Y   s    zTestTriu.test_basicc                 C   s   dt d d}| }tdD ],}tt|d dfdD ]}d|||f< q<q"tt|dd| | }tdD ]$}t|d dD ]}d|||f< qqrtt|d	d| d S )
NrD   rE   r/   r.   r   r<   r6   r:   r=   )r+   rG   r   rH   rL   r   r   rI   r)   r)   r*   r9   a   s    zTestTriu.test_diagNrM   r)   r)   r)   r*   rN   X   s   rN   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S )TestToeplitzc                 C   sl   t dddg}t|dddgdddgdddgg t dddgdddg}t|dddgdddgdddgg d S Nr.   r<   r:   r-   rE   r   r	   r4   yr)   r)   r*   r5   q   s    "zTestToeplitz.test_basicc                 C   sj   dt d d }t|}t|}t|| |d d df }t|| t|ddd f |dd    d S )N      ?      @y      ?      ?r   r.   )r   r   r   r	   Zconj)r4   r(   xtZcol0r)   r)   r*   test_complex_01w   s    

zTestToeplitz.test_complex_01c                 C   s2   t d}t|dgg t dd}t|dgg dS )z*Scalar arguments still produce a 2D array.
      NrQ   )r4   rW   r)   r)   r*   test_scalar_00   s    
zTestToeplitz.test_scalar_00c                 C   s2   t dddg}t|d}t|dgdgdgg d S Nr.   r<   r:   r   r   r	   r4   crW   r)   r)   r*   test_scalar_01   s    
zTestToeplitz.test_scalar_01c                 C   s6   t dddg}t|t d}t|dgdgdgg d S r\   r]   r^   r)   r)   r*   test_scalar_02   s    zTestToeplitz.test_scalar_02c                 C   s8   t dddg}t|t dg}t|dgdgdgg d S r\   r]   r^   r)   r)   r*   test_scalar_03   s    zTestToeplitz.test_scalar_03c                 C   s.   t dddg}td|}t|dddgg d S )NrY   r<   r:   r.   r]   )r4   rrW   r)   r)   r*   test_scalar_04   s    
zTestToeplitz.test_scalar_04N)
r?   r@   rA   r5   rX   r[   r`   ra   rb   rd   r)   r)   r)   r*   rO   o   s   rO   c                   @   s   e Zd Zdd ZdS )
TestHankelc                 C   sl   t dddg}t|dddgdddgdddgg t dddgdddg}t|dddgdddgdddgg d S )Nr.   r<   r:   r   r-   rE   )r   r	   rR   r)   r)   r*   r5      s    "zTestHankel.test_basicNr?   r@   rA   r5   r)   r)   r)   r*   re      s   re   c                   @   s   e Zd Zdd ZdS )TestCirculantc                 C   s4   t dddg}t|dddgdddgdddgg d S r\   )r   r	   rR   r)   r)   r*   r5      s    zTestCirculant.test_basicNrf   r)   r)   r)   r*   rg      s   rg   c                   @   s   e Zd Zdd ZdS )TestHadamardc              	   C   s   t d}t|dgg t dtd}t|ddgddgg t d}t|ddddgddddgddddgddddgg ttt d ttt d	 d S )
Nr.   r<   r0   rT         r-   r8   r   rE   )r   r	   floatassert_raises
ValueErrorrR   r)   r)   r*   r5      s    


zTestHadamard.test_basicNrf   r)   r)   r)   r*   rh      s   rh   c                   @   s   e Zd Zdd Zdd ZdS )
TestLesliec                 C   sj   t ttddgddggdddg t ttdddgddgddgg t ttddgddg t ttdgg  d S rP   )rk   rl   r   r3   r)   r)   r*   test_bad_shapes   s      zTestLeslie.test_bad_shapesc                 C   sB   t dddgddg}tdddgdd	d	gd	dd	gg}t|| d S )
Nr.   r<   r:         ?      ?rT          @rU           )r   r   r	   )r4   rJ   expectedr)   r)   r*   r5      s    
zTestLeslie.test_basicNr?   r@   rA   rn   r5   r)   r)   r)   r*   rm      s   rm   c                   @   s   e Zd Zdd Zdd ZdS )TestCompanionc                 C   sH   t ttddgddgg t ttdddg t ttdg t ttg  d S )Nr.   r<   r   r-   rE   )rk   rl   r   r3   r)   r)   r*   rn      s    zTestCompanion.test_bad_shapesc                 C   s\   t dddg}tddgddgg}t|| t dd	d
g}tdd	gddgg}t|| d S )Nr.   r<   r:          g      rT   rr   rq         @g      $      )r   r   r	   )r4   r_   rs   r)   r)   r*   r5      s    
zTestCompanion.test_basicNrt   r)   r)   r)   r*   ru      s   ru   c                   @   sL   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S )TestBlockDiagc                 C   s   t tdddgddgddggdddgg}t|dddddddgdddddddgdddddddgdddddddgdddddddgdddddddgg d S )Nr<   r.   r:   r-   rE      r   )r   r   r	   r4   rV   r)   r)   r*   r5      s    *zTestBlockDiag.test_basicc                 C   s4   t dgg}t|jt t dgg}t|jt d S )Ng      ?T)r   r   r1   rj   boolr{   r)   r)   r*   
test_dtype   s    zTestBlockDiag.test_dtypec                 C   s6   t dggdgg}tddgddgg}t|| d S )Nr.                 ?r   )r   npr   r	   )r4   actualZdesiredr)   r)   r*   test_mixed_dtypes   s    zTestBlockDiag.test_mixed_dtypesc                 C   sN   t d}t|jd t|dgg t ddgd}t|dddgdddgg d S )Nr.   r.   r.   r<   r:   r-   r   )r   r   shaper	   r4   rJ   r)   r)   r*   test_scalar_and_1d_args   s
    z%TestBlockDiag.test_scalar_and_1d_argsc                 C   s   t ttdggg d S )Nr.   )rk   rl   r   r3   r)   r)   r*   test_bad_arg  s    zTestBlockDiag.test_bad_argc                 C   s"   t  }t|jd t|jd d S )Nr<   r   )r   r   ndimnbytesr   r)   r)   r*   test_no_args  s    zTestBlockDiag.test_no_argsc                 C   st   t ddgddggg ddgddgddgg}t|ddddgddddgddddgddddgddddgddddgg d S )	Nr.   r   r<   r:   r-   rE   rz      )r   r	   r   r)   r)   r*   test_empty_matrix_arg  s    




z#TestBlockDiag.test_empty_matrix_argc                 C   s   t ddgddggg gddgddgddggtjddgd	d
}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 d S )Nr.   r   r<   r:   r-   rE   rz   r   Zint32r0   )r   r   zerosr	   r   r)   r)   r*   test_zerosized_matrix_arg  s    z'TestBlockDiag.test_zerosized_matrix_argN)r?   r@   rA   r5   r}   r   r   r   r   r   r   r)   r)   r)   r*   ry      s   	ry   c                   @   s   e Zd Zdd ZdS )TestKronc              
   C   s   t tddgddggtdddgg}t|tddddddgddddddgg tddgddgg}tdgdgg}t ||}tddgddgd	d
gddgg}t|| d S )Nr.   r<   r:   r-   rY      rZ         (   !   ,   )r   r   r	   )r4   rJ   m1m2rs   r)   r)   r*   r5   ,  s    &
zTestKron.test_basicNrf   r)   r)   r)   r*   r   *  s   r   c                   @   s   e Zd Zdd Zdd ZdS )TestHelmertc                 C   sV   t ddD ]F}t|dd}t|}t||j|dd t|j||dd q
d S )Nr.   r   Tfull-q=atol)rH   r   r   r   r   dotT)r4   r'   HZIdr)   r)   r*   test_orthogonality>  s
    
zTestHelmert.test_orthogonalityc                 C   s   t ddD ]}t|dd}t|}|dd d d f j|jfD ]T}t|t||fd|  }t||j| t|j|t|d dd q>q
d S )Nr<   r   Tr   r.   r   r   )rH   r   r   r   r   r   r   r   )r4   r'   ZH_fullZ	H_partialUCr)   r)   r*   test_subspaceE  s     zTestHelmert.test_subspaceN)r?   r@   rA   r   r   r)   r)   r)   r*   r   <  s   r   c                   @   s   e Zd Zdd ZdS )TestHilbertc                 C   sX   t dddgdddgdddgg}ttd| ttddgg td}t|jd	 d S )
NrT   rp   gUUUUUU?ro   g?r:   r.   r   )r   r   )r   r
   r   r	   r   r   )r4   h3Zh0r)   r)   r*   r5   Q  s    
zTestHilbert.test_basicNrf   r)   r)   r)   r*   r   O  s   r   c                   @   s   e Zd Zdd Zdd ZdS )TestInvHilbertc              "   C   s  t dgg}ttddd| ttd| t ddgddgg}ttddd| ttd| t dd	d
gd	ddgd
ddgg}ttddd| ttd| t ddddgddddgddddgddddgg}ttddd| ttd| t dddddgdddd d!gddd"d#d$gdd d#d%d&gdd!d$d&d'gg}ttd(dd| ttd(| t d)d*d+d,d-d.d/d0d1d2d3d4d5d6d7d8d9gd*d:d;d<d=d>d?d@dAdBdCdDdEdFdGdHdIgd+d;dJdKdLdMdNdOdPdQdRdSdTdUdVdWdXgd,d<dKdYdZd[d\d]d^d_d`dadbdcdddedfgd-d=dLdZdgdhdidjdkdldmdndodpdqdrdsgd.d>dMd[dhdtdudvdwdxdydzd{d|d}d~dgd/d?dNd\didudddddddddddgd0d@dOd]djdvdddddddddddgd1dAdPd^dkdwdddddddddddgd2dBdQd_dldxdddddddddddgd3dCdRd`dmdydddddddddddgd4dDdSdadndzdddddddddddgd5dEdTdbdod{dddddddddddgd6dFdUdcdpd|dddddddddddgd7dGdVdddqd}dddddddddddgd8dHdWdedrd~dddddddddddgd9dIdXdfdsddddddddddddgg}ttddd| ttd|tddč d S )Nr.   Texactr-   i   r<   	   ir      iL   r:      i   iti  iti  iP  ii
     ii  iiv  i  i,i i  ii6 i4i|  i  ixiD  rE   i!  ip]i) i2@il%lPAy l   g
 lP= l   /w$l:R
l   xDulm7l   O@l>/,	l   t:l`j! l   < i y ǐl    r	 l@u\ l    Dbl`Hhl    Pz6 l }% l    l&W lc( l    hx l4z l    d~I
 l8wl8Z l    @u8! l`6al   @iLU l RKl   8H,!l`g`36 l   g0 l *?*^B l   ;t$ l}|l   @bskl C{'"il   `>9&%lmq>l   3CM@;l 86`T l   Pm;,
 l    pM6=l 0	 l    nC l YJVrl     <_l L^Vl    R8l uxbl    eH  lEIBQxl    HvYPl x(6$l     7	l >u1l   Dcf$l l@QM l   8ItlHv0"Qsl   N
:v l@ s/V| l   #& lp-N$v  l   5  l@,@VK-
 l   8_DdYI l B _, l   qkl    O9x&lY)lx l    pd lgsxmU l    D0FZD lK|y#l_y l    xbr l&V% l    lny?Cce l@M=p- l    @@|%6 lh\bC l   @/.L l BAyks'9 l   | l
LjYl   iYn\"%l #5Eyl    X(l@T'3?l   @,3l Hx+/S l   pO3^#
 l    @+aF}lcJ{
;_l    P8Xy8Qo&l BIhNl    `@
oc#ldLq,Ol    pFYRal Ii9Ml     R/$Ll@<8zG 0 l   $9@t)l4l   07$$Tv0l l7>?l   "`-:M=l (^@b)l   6a;:BMlb-Kx?|#l   Z$M~ll l    l>cTAl@8HV-:vl    h{d*U lzaC<C l    dYwMfl RyUC#h.l    @)]loz+%l   @e4jT l GU
 l   `i
t,@ l Y,ch8 l   E@3I3Tl X=&@El   ~Sil    p}v l`Xfz~A`si l    86Jpsbv l $JY'ipl     tk(0lu1@<fl   w2T.g] l %\n l    PYHull ZR@
Rl   LwJl    L#(S.,# l2F=EgJl    @}ll`L\kl   @i7Qt0"l 8B3\f	l   p1&S,l     @Y?l }xl]R l   /"}+
    r   )rtol)r   r	   r   r
   r   rG   rj   )r4   Zinvh1Zinvh2Zinvh3Zinvh4Zinvh5Zinvh17r)   r)   r*   r5   _  sT   



        
        
                                                                                                                zzTestInvHilbert.test_basicc                 C   sN   t ddD ]>}t|}t|}t|}t||t|d| d| d q
d S )Nr.   rY   gV瞯<)r   r   )rH   r   r   r$   r   r   r   )r4   r'   rJ   rK   r_   r)   r)   r*   test_inverse  s
    zTestInvHilbert.test_inverseN)r?   r@   rA   r5   r   r)   r)   r)   r*   r   ]  s    r   c                   @   s  e Zd ZdedggedggfdeddgddggeddgddggfdedddgdddgdddggedddgdddgdddggfdeddddgddddgddddgddddggeddddgddddgddddgddddggfgZd	d
 Zdd Zdd Zdd ZdS )
TestPascalr.   r<   r   r:   rz   r-   rY   rZ   c                 C   st   t t|| t t|dd| t t|dd|j tt|dd| tt|ddd| tt|ddd|j d S )Nlower)kindupperFr   )r   r   )r	   r   r   r
   r4   r'   symlowr)   r)   r*   
check_case  s    zTestPascal.check_casec                 C   s$   | j D ]\}}}| ||| qd S r%   )casesr   r   r)   r)   r*   
test_cases%  s    zTestPascal.test_casesc                 C   s&   t d}|d tddddks"td S )N2   )r8   r8   b   1   Tr   )r   r   AssertionErrorr4   pr)   r)   r*   test_big)  s    zTestPascal.test_bigc                 C   s\   t d}td|dd |dddd t d}td|dd d	|dd d
d d S )N"   r<   r8   r=   zn = 34err_msg#   rq   rT   zn = 35)r   r   itemr   r)   r)   r*   test_threshold-  s    "zTestPascal.test_thresholdN)	r?   r@   rA   r   r   r   r   r   r   r)   r)   r)   r*   r     sB   







r   c                  C   sx   dd } dddg}dddd	g}|D ]$}|D ]}d
D ]}| ||| q2q*q"ddddg}|D ]}|D ]}| ||d q`qXd S )Nc                 S   sR   t | ||d}t| ||d}|t|t}t|t| d| ||f d d S )N)r   r   zn=%d  kind=%r exact=%rr   )r   r   rG   objectr   r	   r   )r'   r   r   ipr   er)   r)   r*   check_invpascal:  s    z'test_invpascal.<locals>.check_invpascalZ	symmetricr   r   r.   r<   rE      )TF   r   r   r   Tr)   )r   kindsnsr'   r   r   r)   r)   r*   test_invpascal8  s    
r   c               	   C   s   t d} tddgddgg}t| | t ddd} t| |d  t ddd} t| |td  tdd	dd
dddd	g}t d} | |}t|}t|| d S )Nr<   rT   ri   r'   )Zscalerq   Zsqrtnr   r.   r:   r-   rE      )r   r   r
   r   r   r   )mrs   rV   ZmxZfxr)   r)   r*   test_dftU  s    

r   c                  C   s   t g } t| jd t dg} t| tdgg t tdd} 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| | d S )
Nr   g     ^@rr   r.   r   r<   r:   r-   rE   )r    r   sizer	   r   r   r   )r/   Zdesr)   r)   r*   test_fiedlere  s    
r   c               	   C   s   t g } t| jd t dg} t| jd t ddg} t| tdgg t dddg} t| tdddg tt	 t dddg W 5 Q R X t dd	d
ddg} tt
| tddddg d S )Nr   rT   rq   rv   r   rU   r.   r<   g      0g     U@g      fg     @Z@g      @rw   )r!   r   r   r	   r   r   r
   r   rk   rl   r"   )Zfcr)   r)   r*   test_fiedler_companiont  s    

r   c                
   @   s   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d Ze	j
dddge	j
ddddge	j
ddddge	j
dddddgdd ZdS )TestConvolutionMatrixzL
    Test convolution_matrix vs. numpy.convolve for various parameters.
    c                 C   s,   t dd|}|r(|dt dd|  }|S )z/Make a complex or real test vector of length n.rx   g@r~   g      g@)r   Zlinspace)r4   r'   cpxrV   r)   r)   r*   create_vector  s    z#TestConvolutionMatrix.create_vectorc              	   C   s.   t jtdd tdddgd W 5 Q R X d S )Nzn must be a positive integermatchr.   r<   r:   r   pytestr   rl   r#   r3   r)   r)   r*   
test_bad_n  s    z TestConvolutionMatrix.test_bad_nc              	   C   s(   t jtdd tdd W 5 Q R X d S )Nzone-dimensionalr   r.   r-   r   r3   r)   r)   r*   test_bad_first_arg  s    z(TestConvolutionMatrix.test_bad_first_argc              	   C   s(   t jtdd tg d W 5 Q R X d S )Nzlen\(a\)r   r-   r   r3   r)   r)   r*   test_empty_first_arg  s    z*TestConvolutionMatrix.test_empty_first_argc              	   C   s,   t jtdd tdddd W 5 Q R X d S )Nzmode.*must be one ofr   r   r-   zinvalid argument)moder   r3   r)   r)   r*   test_bad_mode  s    z#TestConvolutionMatrix.test_bad_moder   FTnar.   r<   r   nvr   Nr   ZvalidZsamec           
      C   sh   |  ||}|  ||}|d kr8t||}t||}nt|||}t|||}|| }	t||	 d S r%   )r   r   Zconvolver#   r
   )
r4   r   r   r   r   rJ   vy1Ay2r)   r)   r*   test_against_numpy_convolve  s    z1TestConvolutionMatrix.test_against_numpy_convolve)r?   r@   rA   __doc__r   r   r   r   r   r   markZparametrizer   r)   r)   r)   r*   r     s   r   )Ar   Znumpyr   r   r   r   r   r   r   Znumpy.testingr   r	   r
   r   r   rk   Z	scipy.fftr   Zscipy.specialr   Zscipy.linalgr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r    r!   r"   r#   Znumpy.linalgr$   r+   r,   rB   rN   rO   re   rg   rh   rm   ru   ry   r   r   r   r   r   r   r   r   r   r   r)   r)   r)   r*   <module>   s<    \(/D )3