U
    »mœ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ç{®Gáz„?c              	   C   s$   t |ƒ}|j}t | ||||||¡S )N)r	   Zstandard_normalÚ	constructÚrandom)ÚmÚnÚdensityÚformatÚdtypeÚrandom_stateZdata_rvs© r   úZ/home/sam/Atlas/atlas_env/lib/python3.8/site-packages/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rÎt tj||d
� ¡ tj|||d
�ƒ qÎqÀq¸d 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
||ƒ qØqÐd 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ÚequalÚ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¡ ƒƒ qÌd
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*   gš™™™™™¹¿r   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   ÚanyÚ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*   gþÔxé&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__Ú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   
