U
    /d;a                     @   s   d Z ddlZddlmZ ddlmZmZmZmZ ddl	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 dd	lmZ dd
lmZ dddddddgZdddZG dd dZdS )z)test sparse matrix construction functions    N)array)assert_equalassert_assert_array_equalassert_array_almost_equal_nulp)raises)check_free_memory)check_random_state)
csr_matrix
coo_matrix
_construct)rand)matrixcsrcsccooZbsrdialilZdok{Gz?c              	   C   s$   t |}|j}t| ||||||S )N)r	   Zstandard_normal	constructrandom)mndensityformatdtyperandom_stateZdata_rvs r   E/tmp/pip-unpacked-wheel-9gxwnfpp/scipy/sparse/tests/test_construct.py_sprandn   s     r   c                   @   s   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d Zdd Zdd Zdd Zdd  Zd!d" Zd#d$ Zejjejd%d&d' Zd(d) Zd*d+ Zd,d- Zd.d/ Zd0d1 Zd2d3 Zd4d5 Z d6d7 Z!d8d9 Z"d:S );TestConstructUtilsc           
      C   s  t dddddgg}t dddddgdddd	d
gg}t dddddgdddd	d
gdddddgg}g }||ddddggf ||dgdddggf ||dgdddgdggf ||dgddddggf ||dgddddggf ||dgddddggf ||dgddddgddggf ||dgddddgddggf ||dgddddgddggf ||dgddddddgddddgddddggf ||dgddddddgddddgddddggf ||dgdddddddgdddddgdddddggf ||ddgdddddgdddgdddggf ||ddgddddddgddddgddddggf ||ddgdd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f ||dddgdd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f ||dddgdd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dddddggf ||dddgt|d t|d 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f |D ]x\}}}}}	t|d |kr\||kr\tt|| |	 tt|||| |	 tt||||f |	 qd S )N                        	   
                  r   )r   appendlenr   r   Zspdiagstoarray)
selfZdiags1Zdiags2Zdiags3casesdor   r   resultr   r   r   test_spdiags   sr    """44:06 "zTestConstructUtils.test_spdiagsc           
      C   s  t dddddg}t dddd	d
g}t dddddg}g }||d d dddggf ||d d gdgddggf ||d d gdgddgdggf ||d d gdgdddggf ||d d gdgdddggf ||d d gdgdddgddggf ||d d gdgdddgddggf ||d d gdgdddddgddddgddddggf ||d d gdgdddddgddddgddddggf ||d d gdgddddddgdddddgdddddggf ||d d gdgddddddgdddddgdddddggf ||d d gdgddddddgdddddgdddddggf ||d d gdgddddddgdddddgdddddggf ||d d gdgddddddgdddddgdddddggf ||d d gdgddddddgdddddgdddddggf ||d d gdgddddddgdddddgdddddggf ||d d gdgddddgdddgdddgdddgdddggf ||d d gdgddddgdddgdddgdddgdddggf ||d d gdgddddgdddgdddgdddgdddggf ||d d gdgddddgdddgdddgdddgdddggf ||d d gdgddddgdddgdddgdddgdddggf ||d d gdgddddgdddgdddgdddgdddggf ||d d gdgddddgdddgdddgdddgdddggf ||d d |d d gddgddddgdddgdddggf ||d d |d d gddgdddddgddddgddddggf ||d d |d d gddgd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f ||d d ||d d gdddgd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f ||d d |d d |gdddgd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dddddggf ||gdgddggf ||d d |gddgddddgdddgdddggf |t dddgdddggddgddddgdddgdddggf |dddgdddgddddgdddgdddggf |D ]\}}}}d ||||f }	ttj|||d! ||	d" |d |d kr&t|d d#r&t|d t	|kr&tt|| ||	d" q&d S )$Nr!   r"   r#   r$   r%   r&   r'   r(   r)   r*   r+   r,   r-   r.   r/   r   r!   r!   r"   r!   r!   r"   r"   r"   r0   )r#   r$   r#   r%   r2   )r%   r#   r1   )r#   r#   )r&   r&   r%   r%   r&   r%   z%r %r %r %rshape)err_msg__len__)
r   r3   npr   r   diagsr5   hasattrr4   max)
r6   abcr7   r8   r9   rE   r:   rF   r   r   r   
test_diagsW   s    "&$$**<<BBBBBBBFFFFFFFBH022:D 8zTestConstructUtils.test_diagsc                 C   s0   t dddddg}tt| t| d S )Nr!   r"   r#   r$   r%   )r   r   r   rI   r5   rH   diagr6   rL   r   r   r   test_diags_default   s    z%TestConstructUtils.test_diags_defaultc                 C   s2   t dddddgdddddgg}tttj| d S )Nr!   r"   r#   r$   r%   r&   )r   assert_raises
ValueErrorr   rI   rQ   r   r   r   test_diags_default_bad   s     z)TestConstructUtils.test_diags_default_badc                 C   sR  t dddddg}t dddd	d
g}t dddddg}g }||d d gddf ||d d ||d d gdddgdf ||d d ||d d gdddgdf ||d d ||d d gdddgd f |g dddgd f |dgdgdf ||gdd f |D ]\}}}tttj||| qtttjd ggdg d S )Nr!   r"   r#   r$   r%   r&   r'   r(   r)   r*   r+   r,   r-   r.   r/   r   r<   r0   rB   r2   rC   )r$   r$   )r   r3   rS   rT   r   rI   	TypeError)r6   rL   rM   rN   r7   r8   r9   rE   r   r   r   test_diags_bad   s    ,,,z!TestConstructUtils.test_diags_badc                    s   t jd dD ]}d|d  t jdd  t   d  d }t j| |d | } fdd|D }t||}td	d t	||D }t
| | t|dkrt|d |d }t |d |d }t
| | qd S )
Ni  )r!   r"   r#   r$   r%   r*   r!   r"   r   r*   c                    s    g | ]}t j t| qS r   )rH   r   r   abs).0qr   r   r   
<listcomp>   s     z9TestConstructUtils.test_diags_vs_diag.<locals>.<listcomp>c                 S   s   g | ]\}}t ||qS r   )rH   rP   )rZ   xjr   r   r   r]      s     )rH   r   seedrandintZarangeshuffler   rI   sumzipr   r5   r4   rP   )r6   Zn_diagsoffsetsZ	diagonalsmatZ	dense_matr   r\   r   test_diags_vs_diag   s    z%TestConstructUtils.test_diags_vs_diagc                 C   s@   t jdgdgdtd}t|jt t| ddgddgg d S )Ng@r   r?   )rE   r   r"   )r   rI   intr   r   r5   r6   r^   r   r   r   test_diags_dtype   s    z#TestConstructUtils.test_diags_dtypec                 C   sF   t td}tddD ]*}tt|| t|g|g  qd S )Nr%   rV   r&   )listranger   r   rI   r5   )r6   r8   kr   r   r   test_diags_one_diagonal   s
    z*TestConstructUtils.test_diags_one_diagonalc                 C   s   t g }t|jd d S )Nr   r   )r   rI   r   rE   ri   r   r   r   test_diags_empty   s    
z#TestConstructUtils.test_diags_emptyc                 C   s   t td dgg t td ddgddgg tjdddd}t |jtd t |jd tD ]D}tjd|d}t |j| t | dddgdddgdddgg qjd S 	Nr!   r"   r   r#   Zint8r   )r   r   r   )r   r   identityr5   r   rH   r   sparse_formatsr6   Ifmtr   r   r   test_identity   s     z TestConstructUtils.test_identityc              
   C   s.  t tdd dgg t tdd dddgdddgg t tdd ddgddgddgg t tdd dddgdddgdddgg t tjddddjtd dD ]p}dD ]f}tdd	D ]V}t tj|||d
 tj|||d
 ||krt tj||d
 tj|||d
 qqqd S )Nr!   r"   r#   r   Zint16r   rA   rV   r&   )rm   )r   r   eyer5   r   rH   rl   )r6   r   r   rm   r   r   r   test_eye   s    &(.&zTestConstructUtils.test_eyec                 C   s   t td dgg t td ddgddgg tjdddd}t |jtd t |jd tD ]D}tjd|d}t |j| t | dddgdddgdddgg qjd S rq   )r   r   rz   r5   r   rH   r   rt   ru   r   r   r   test_eye_one   s     zTestConstructUtils.test_eye_onec              
   C   s  g }| tdgg | tdgg | tdgg | tdgg | tdgdgg | tddgg | tddgddgg | tddgddgg | tddd	gd
ddgg | tddgddgddgg | tdddgdddgddd
gg | tddddddd
gg | tddddgddddgg |D ]\}|D ]P}t||}tD ]8}tjt|t||d}t|j| t	|
 | qnqZqRd S )Nr   r0   r$   r*   r!   r"   r#   r%   r(   r.   r&         ?g      ?g      
@g      @rr   )r3   r   rH   kronrt   r   r
   r   r   r   r5   )r6   r7   rL   rM   expectedrw   r:   r   r   r   	test_kron  s*     &"zTestConstructUtils.test_kronc                 C   sD   d}t d||d }t |dd| }t || t || d S )Ni   r!   )r   rz   r   )r6   r   rL   rM   r   r   r   test_kron_large%  s
    z"TestConstructUtils.test_kron_largec              
   C   s2  g }| tdgg | tdgg | tdgg | tdgg | tddgddgg | tddgddgg | tddd	gd
ddgdddgg | tdddgdddgddd
gg |D ]\}|D ]R}tt|t| }ttt	||t|tt	| }t
|| qqd S )Nr   r0   r$   r*   r!   r"   r#   r%   r}   r(   r.   r@   )r3   r   r   Zkronsumr
   r5   rH   r   rz   r4   r   )r6   r7   rL   rM   r:   r   r   r   r   test_kronsum-  s&    && zTestConstructUtils.test_kronsumc                 C   sJ  t ddgddgg}t ddgg}tddgddgddgg}tt||g | ttj||gtjdjtj tt|	 |	 g | tj|	 |	 gtjd}t|jtj t|j
jtj t|jjtj tt| | g | tj| | gtjd}t|jtj t|j
jtj t|jjtj d S Nr!   r"   r#   r$   r%   r&   ry   )r   r   r   r   Zvstackr5   rH   float32r   tocsrindicesint32indptrtocsc)r6   ABr   r:   r   r   r   test_vstackA  s0    zTestConstructUtils.test_vstackc                 C   s  t ddgddgg}t dgdgg}tdddgdddgg}tt||g | ttj||gtjdjtj tt|	 |	 g | ttj|	 |	 gtjdjtj tt|
 |
 g | ttj|
 |
 gtjdjtj d S r   )r   r   r   r   Zhstackr5   rH   r   r   r   r   )r6   r   r   r   r   r   r   test_hstack[  s2    
zTestConstructUtils.test_hstackc           	   	   C   s  t ddgddgg}t dgdgg}t dgg}t d}tdddgdddgd	d	dgg}tt||gd |gg | td
tjd}tt|	 |	 g||	 gg | tt|
 |
 g|
 |
 gg | tddd	gddd	gd	d	dgg}tt|d gd |gg | tt|	 |j	 g||	 gg | tt|
 |j
 g|
 |
 gg | tdtjd}td	dgd	dgdd	gg}ttd |g|d gg | tt|j	 |	 g|	 |gg | tt|j
 |
 g|
 |
 gg | ttd}ttd d gg | ttd |g|d gg | tdgg}ttd |g|d gg | tt}t|g|gg W 5 Q R X |d tt }t|	 g|	 gg W 5 Q R X |d tt }t|
 g|
 gg W 5 Q R X |d tt}t||gg W 5 Q R X |d tt}t|	 |	 gg W 5 Q R X |d tt}t|
 |
 gg W 5 Q R X |d d S )Nr!   r"   r#   r$   r%   r&   r'   ro   r   r>   ry   r<   z.Got blocks\[1,0\]\.shape\[1\] == 1, expected 2z"incompatible dimensions for axis 1z4Mismatching dimensions along axis 1: ({1, 2}|{2, 1})z.Got blocks\[0,1\]\.shape\[0\] == 1, expected 2z4Mismatching dimensions along axis 0: ({1, 2}|{2, 1})z"incompatible dimensions for axis 0)r   r   r   r   Zbmatr5   r
   rH   r   r   r   Tr   emptyrS   rT   match)	r6   r   r   CDr   EZexcinfor   r   r   	test_bmatp  s    
 






$

$



"

"zTestConstructUtils.test_bmatz!Can't create large array for testc                 C   s|   t d d}ttj||ftd}| }t||fd}tt	t
t|j| t|jjtj t|jjtj dS )z6 test for indptr overflow when concatenating matrices i0u  i  ry   r   N)r   r
   rH   Zonesboolcopyr   Z_compressed_sparse_stackr   allequalZdiffr   r   r   r   int64)r6   r   r   r   r   r   r   r   test_concatenate_int32_overflow  s    z2TestConstructUtils.test_concatenate_int32_overflowc              	   C   s   t ddgddgg}t dgdgg}t 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g}tt|||f | d	S )
z basic test for block_diag r!   r"   r#   r$   r%   r&   r'   r   N)r   r   r   r   
block_diagr5   )r6   r   r   r   r   r   r   r   test_block_diag_basic  s    



z(TestConstructUtils.test_block_diag_basicc                 C   s0   t tddgdg dddgdddgg dS )z) block_diag with scalar and 1d arguments r"   r#   r$   r   N)r   r   r   r5   r6   r   r   r   test_block_diag_scalar_1d_args  s    z1TestConstructUtils.test_block_diag_scalar_1d_argsc                 C   s   t tddgg tddgg t tddggg tddgg t tdgdggg tdgdgg t tdg tdgg dS )z block_diag with one matrix r!   r   N)r   r   r   r5   r   r   r   r   r   test_block_diag_1  s    
z$TestConstructUtils.test_block_diag_1c              	   C   s   t dddggddt ddggddg}t|}t| tdddd	d	gd	d	d	ddgg t dgdgdggd
dt dgdggddg}t|}t| tdd	gdd	gdd	gd	dgd	dgg dS )z! block_diag with sparse matrices r!   r"   r#   )r!   r#   rD   r$   r%   r>   r   )r#   r!   r=   N)r   r   r   r   r5   r   )r6   Zsparse_col_matricesZblock_sparse_cols_matricesZsparse_row_matricesZblock_sparse_row_matricesr   r   r   test_block_diag_sparse_matrices  s    

$z2TestConstructUtils.test_block_diag_sparse_matricesc              	      sL  t tfD ]< tjtjtjtjtjtjtj	fD ]8} ddd|d}t
|j| t
|jd t
|jd q. ddddd}t
|jtj  dddtjdd}t|j|j t|j|j t|j|j dD ].} dd|d	}t
|jt|t|j  qd
D ] } dd|d}t
|j| q tt fdd tt fdd qd S )Nr%   r*   皙?)r   r   )r%   r*     )r   r   )g        r   r~   g      ?r   )r   r   r   r   rr   c                      s    dddS )Nr%   r*   g?r   r   fr   r   <lambda>      z9TestConstructUtils.test_random_sampling.<locals>.<lambda>c                      s    dddS )Nr%   r*   gr   r   r   r   r   r     r   )sprandr   rH   r   float64Z
longdoubler   r   Z	complex64Z
complex128r   r   rE   Znnzdoubler   RandomStater   datarowcolrh   prodr   rS   rT   )r6   tr^   x1Zx2r   rw   r   r   r   test_random_sampling  s6       
z'TestConstructUtils.test_random_samplingc                 C   s   d dt j g}zt j }|| W n tk
r<   Y nX |D ]H}tdddt j|d}tt 	t 
d|j tt 	t 
|jd qBd S Nr   r*      r~   )r   r   r   r   r!   )rH   r   r   default_rngr3   AttributeErrorr   r   r   r   Z
less_equalr   r6   Zrandom_statesgenr   r^   r   r   r   	test_rand  s    
zTestConstructUtils.test_randc                 C   s   d dt j g}zt j }|| W n tk
r<   Y nX |D ]H}tdddt j|d}tt 	t 
|jd tt 	t 
d|j qBd S r   )rH   r   r   r   r3   r   r   r   r   anyZlessr   r   r   r   r   
test_randn+  s    
zTestConstructUtils.test_randnc                 C   s   t jdddd d S )Nr*   r8   ry   )r   r   r   r   r   r   test_random_accept_str_dtype<  s    z/TestConstructUtils.test_random_accept_str_dtypec                 C   s"   t jdddd}t| d d S )Nr*   gx&1?r   r-   )r   r   r   Zcount_nonzero)r6   Zsparse_matrixr   r   r   Etest_random_sparse_matrix_returns_correct_number_of_non_zero_elementsA  s    zXTestConstructUtils.test_random_sparse_matrix_returns_correct_number_of_non_zero_elementsN)#__name__
__module____qualname__r;   rO   rR   rU   rX   rg   rj   rn   rp   rx   r{   r|   r   r   r   r   r   r   pytestmarkZslowZxfail_on_32bitr   r   r   r   r   r   r   r   r   r   r   r   r   r   r       s<   8GO
r    )r   r   NN)__doc__ZnumpyrH   r   Znumpy.testingr   r   r   r   r   r   rS   Zscipy._lib._testutilsr   Zscipy._lib._utilr	   Zscipy.sparser
   r   r   r   Zscipy.sparse._constructr   r   Zscipy.sparse._sputilsr   rt   r   r    r   r   r   r   <module>   s   
