o
    h9a                     @   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 g d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coobsrdialildok{Gz?r   c              	   C   s$   t |}|j}t| ||||||S )N)r	   standard_normal	constructrandom)mndensityformatdtyperandom_statedata_rvs r!   \/var/www/html/scripts/venv/lib/python3.10/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 g dg}t g dg dg}t g dg dg d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	g d
g dg dgf ||dgdd	g dg dg dgf ||dgddg dg dg dgf ||ddgddg dg dg dgf ||ddgdd	g dg dg dgf ||ddgddg dg dg dg dg dg dgf ||g d ddg d!g d"g d#g d$g d%g dgf ||g d&ddg d'g d(g d)g d*g d+g d,gf ||g d-t|d t|d g d.g d/g d0g d1g dgf |D ]<\}}}}}	t|d |kr||krtt|| |	 tt|||| |	 tt||||f |	 qd S )2N                        	   
                  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   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   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   r7   r   r&   )r,   r3   r   r   r   r   )r&   r-   r4   r   r   r   )r   r'   r.   r5   r   r   )r   r   r(   r/   r6   r   )r   r   r   r)   r0   r   r'   r7   )r   r   r.   r   r   )r2   r   r   r/   r   )r   r3   r   r   r0   r   r   r4   r   r   r&   r   r   r5   r   r   r'   r   r   r6   )r7   r&   r'   )r   r-   r4   r   r   )r&   r   r.   r5   r   )r   r'   r   r/   r6   )r   r   r(   r   r0   )r   appendlenr   r   spdiagstoarray)
selfdiags1diags2diags3casesdor   r   resultr!   r!   r"   test_spdiags   st   """(((**  zTestConstructUtils.test_spdiagsc           
      C   s4  t g d}t g d}t g d}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g dg dg dgf ||d d gdgdg dg dg dgf ||d d gdgdg dg dg dgf ||d d	 gdgdg dg dg dgf ||d d gdgdg dg dg dgf ||d d gdgdg dg dg dgf ||d d gd	gdg dg dg dgf ||d d	 gdgdg d g d!g dgf ||d d gd"gdg d#g dg dgf ||d d gd$gd%g d&g d&g d&g d&g d'gf ||d d	 gd(gd%g d&g d&g d&g d'g d)gf ||d d gdgd%g d&g d&g d'g d)g d*gf ||d d gdgd%g d&g d'g d)g d*g d&gf ||d d gdgd%g d'g d)g d*g d&g d&gf ||d d	 gdgd%g d+g d,g d&g d&g d&gf ||d d gd	gd%g d-g d&g d&g d&g d&gf ||d d |d d gdd	gd.g d/g d)g d*gf ||d d	 |d d gddgdg d0g d1g d2gf ||d d" |d d gd	d(gd3g d4g d5g d6g d7g d8g d9gf ||d d" ||d d" gg d:d;g d<g d=g d>g d?g d@gf ||d d	 |d d |gg dAdBg dCg dDg dEg dFg dGg dHgf ||gdgddggf ||d d |gdd	gd.g d/g d)g d*gf |t g dIg dJgddgd.g d'g dKg dLgf |g dMg dNd.g dOg dMg dPgf |D ]L\}}}}|dQ|dQ|dQ|}	ttj|||dR ||	dS |d |d krt|d dTrt|d t	|krtt|| ||	dS qKd S )UNr%   r+   r1   r&   r   r&   r&   r'   r&   r&   r'   r'   r'   r'   r7   r(   )r(   r)   r8   r9   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&   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   )r   r   r   r   r'   r)   )r   r   r   r   r&   rF   )r*   r(   )r   r   r   r&   r   r   rA   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   )r   r   r   r   r(   r   )r,   r   r   r   r   r)   rB   rC   rD   r*   r*   )r,   r2   r   r   r   )r&   r-   r3   r   r   )r   r'   r.   r4   r   )r   r   r(   r/   r5   )r   r   r   r)   r0   rE   r,   r*   )r   r   r,   r   r   )r2   r   r   r-   r   )r   r3   r   r   r.   rG   rH   rI   r&   r'   r(   )r)   r*   r,   )r)   r'   r   )r   r*   r(   )r&   r[   r&   )r&   r   r7   )r[   r&   r   )r   r&   r[    shape)err_msg__len__)
r   rJ   npr   r   diagsrM   hasattrrK   max)
rN   abcrR   rS   rT   re   rU   rf   r!   r!   r"   
test_diagsW   s   "&$$**000000000<<<<<<<<<(**4:8zTestConstructUtils.test_diagsc                 C   s*   t g d}tt| t| d S )Nr%   )r   r   r   ri   rM   rh   diagrN   rl   r!   r!   r"   test_diags_default   s   z%TestConstructUtils.test_diags_defaultc                 C   s&   t g dg d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   s6  t g d}t g d}t g d}g }||d d gddf ||d d ||d d gg dd	f ||d d
 ||d d gg ddf ||d d
 ||d d gg dd f |g g d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+   r1   r   rW   r)   r(   rD   r`   r'   rE   ra   r&   )r)   r)   )r   rJ   rs   rt   r   ri   	TypeError)rN   rl   rm   rn   rR   rS   rT   re   r!   r!   r"   test_diags_bad   s   ***z!TestConstructUtils.test_diags_badc                    s   t jd dD ]i}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rqt|d |d }t |d |d }t
| | qd S )
Ni  )r&   r'   r(   r)   r*   r0   r&   r'   r   r0   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randintarangeshuffler   ri   sumzipr   rM   rK   rp   )rN   n_diagsoffsets	diagonalsmat	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   rZ   )re   r   r'   )r   ri   intr   r   rM   rN   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   rM   )rN   rS   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   r   r!   r!   r"   test_diags_empty   s   
z#TestConstructUtils.test_diags_emptyc                 C      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 ]}tjd|d}t |j| t | g d	g d
g dg q5d S Nr&   r'   r   r(   int8r   )r   r   r   r]   r^   r_   )r   r   identityrM   r   rh   r   sparse_formatsrN   Ifmtr!   r!   r"   test_identity       "z TestConstructUtils.test_identityc              
   C   s$  t tdd dgg t tdd g dg dg t tdd ddgddgddgg t tdd g dg dg dg t tjdddd	jtd d
D ]8}d
D ]3}tddD ]+}t tj|||d tj|||d ||krt tj||d tj|||d qbq[qWd S )Nr&   r'   r(   r]   r^   r   r_   int16r   r\   rv   r,   )r   )r   r   eyerM   r   rh   r   )rN   r   r   r   r!   r!   r"   test_eye   s   "((&$zTestConstructUtils.test_eyec                 C   r   r   )r   r   r   rM   r   rh   r   r   r   r!   r!   r"   test_eye_one   r   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g d	g d
g | tddgddgddgg | tg dg dg dg | tg dg | tg dg dg |D ]+}|D ]&}t||}tD ]}tjt|t||d}t|j| t	|
 | qqqd S )Nr   r7   r)   r0   r&   r'   r(   r*   r   r'   ir.   r   r5   r,   )r*   r)   r)   r]   )r,   r   r.   )r   r&   r   r'   r   r*   r.   )      ?g      ?r   g      
@)r   g      @r   r   r   )rJ   r   rh   kronr   r   r
   r   r   r   rM   )rN   rR   rl   rm   expectedr   rU   r!   r!   r"   	test_kron  s0     zTestConstructUtils.test_kronc                 C   sD   d}t d||d }t |dd| }t || t || d S )Ni   r&   )r   r   r   )rN   r   rl   rm   r!   r!   r"   test_kron_large%  s
   z"TestConstructUtils.test_kron_largec              
   C   s&  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g d	g d
g dg | tg dg dg dg |D ].}|D ])}tt|t| }ttt	||t|tt	| }t
|| qfqbd S )Nr   r7   r)   r0   r&   r'   r(   r*   r   r   )r   r(   r   r]   )r   r*   r7   )r)   r[   r.   )rJ   r   r   kronsumr
   rM   rh   r   r   rK   r   )rN   rR   rl   rm   rU   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,   r   )r   r   r   r   vstackrM   rh   float32r   tocsrindicesint32indptrtocsc)rN   ABr   rU   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g dg d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 )
Nr&   r'   r(   r)   r*   r,   r&   r'   r*   r(   r)   r,   r   )r   r   r   r   hstackrM   rh   r   r   r   r   )rN   r   r   r   r!   r!   r"   test_hstack[  s6   zTestConstructUtils.test_hstackc           	      C   sT  t ddgddgg}t dgdgg}t dgg}t d}tg d	g d
g dg}tt||gd |gg | tdtjd}tt|	 |	 g||	 gg | tt|
 |
 g|
 |
 gg | tg dg dg d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 d    n	1 sbw   Y  |d tt}t|	 g|	 gg W d    n	1 sw   Y  |d tt}t|
 g|
 gg W d    n	1 sw   Y  |d tt}t||gg W d    n	1 sw   Y  |d tt}t|	 |	 gg W d    n	1 sw   Y  |d tt}t|
 |
 gg W d    n	1 sw   Y  |d d S )Nr&   r'   r(   r)   r*   r,   r-   r   r   r   )r   r   r-   rY   r   )r&   r'   r   )r(   r)   r   rW   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   bmatrM   r
   rh   r   r   r   Tr   emptyrs   rt   match)	rN   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  r   r   N)r   r
   rh   onesboolcopyr   _compressed_sparse_stackr   allequaldiffr   r   r   r   int64)rN   r   r   r   r   r!   r!   r"   test_concatenate_int32_overflow  s   z2TestConstructUtils.test_concatenate_int32_overflowc                 C   st   t ddgddgg}t dgdgg}t dgg}tg dg d	g d
g dg dg}tt|||f | dS )z basic test for block_diag 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-   N)r   r   r   r   
block_diagrM   )rN   r   r   r   r   r!   r!   r"   test_block_diag_basic  s   z(TestConstructUtils.test_block_diag_basicc                 C   s,   t tddgdg g dg dg dS )z) block_diag with scalar and 1d arguments r'   r(   r)   )r'   r(   r   )r   r   r)   N)r   r   r   rM   rN   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   rM   r   r   r!   r!   r"   test_block_diag_1  s   
z$TestConstructUtils.test_block_diag_1c              	   C   s   t g dgddt ddggddg}t|}t| tg dg d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 rb   )r&   r(   rd   r)   r*   rY   )r&   r'   r(   r   r   )r   r   r   r)   r*   r&   r'   r(   )r(   r&   rX   r   N)r   r   r   r   rM   r   )rN   sparse_col_matricesblock_sparse_cols_matricessparse_row_matricesblock_sparse_row_matricesr!   r!   r"   test_block_diag_sparse_matrices  s   

$z2TestConstructUtils.test_block_diag_sparse_matricesc              	      sH  t tfD ] tjtjtjtjtjtjtj	fD ]} 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  qed
D ]} dd|d}t
|j| qtt fdd tt fdd qd S )Nr*   r0   皙?)r   r   )r*   r0     )r   r   )g        r   r   g      ?r   )r   r   r   r   r   c                          dddS )Nr*   r0   g?r!   r!   fr!   r"   <lambda>      z9TestConstructUtils.test_random_sampling.<locals>.<lambda>c                      r   )Nr*   r0   gr!   r!   r   r!   r"   r     r   )sprandr#   rh   r   float64
longdoubler   r   	complex64
complex128r   r   re   nnzdoubler   RandomStater   datarowcolr   prodr   rs   rt   )rN   tr~   x1x2r   r   r!   r   r"   test_random_sampling  s2   
z'TestConstructUtils.test_random_samplingc                 C   s   d dt j g}zt j }|| W n	 ty   Y nw |D ]$}tdddt j|d}tt 	t 
d|j tt 	t 
|jd q d S Nr   r0      r   )r   r   r   r   r&   )rh   r   r   default_rngrJ   AttributeErrorr   r   r   r   
less_equalr   rN   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y   Y nw |D ]$}tdddt j|d}tt 	t 
|jd tt 	t 
d|j q d S r   )rh   r   r   r   rJ   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 )Nr0   rS   r   )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 )Nr0   gx&1?r   r4   )r   r   r   count_nonzero)rN   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__rV   ro   rr   ru   rx   r   r   r   r   r   r   r   r   r   r   r   r   r   pytestmarkslow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   numpy.testingr   r   r   r   r  r   rs   scipy._lib._testutilsr   scipy._lib._utilr	   scipy.sparser
   r   r   r   scipy.sparse._constructr   r   scipy.sparse._sputilsr   r   r#   r$   r!   r!   r!   r"   <module>   s    
