o
    Ge"                     @   s   d dl m  mZ d dlZd dlmZmZm	Z	 d dl
mZ d dl mZ d dlZd dlmZmZmZmZ d dlZd dlmZ d dlZejdk Ze dd	 Zejejejgd
dd Ze dd Ze dd ZG dd dZ dS )    N)hilbertsvdvalsnormaslinearoperator)interp_decomp)assert_assert_allcloseassert_equalassert_array_equal)raisesl        c                   c   s    dV  d S )N-q= r   r   r   o/var/www/html/SPIRAL.web.tool/spiral_venv/lib/python3.10/site-packages/scipy/linalg/tests/test_interpolative.pyeps,   s   
r   )paramsc                 c   s    d}t || jV  d S )Ni,  )r   astypeparam)requestnr   r   r   A1   s   r   c                 c   s    t | V  d S )Nr   )r   r   r   r   L9   s   r   c                 C   sL   t jj| dd}zt ||k d d }W |S  ty%   | jd }Y |S w )NF)
compute_uvr   )nplinalgsvdnonzero
IndexErrorshape)r   r   Srankr   r   r   r    >   s   r    c                   @   s  e Zd Zejdg ddd Zejdg ddd Zejddgdd	 Zejdg dd
d Z	ejdg d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d ejejgejd!d"d#gejd$d%d&gd'd( Zd)S )*TestInterpolativeDecompositionzrand,lin_op)FF)TF)TTc                 C   sv   t r|jtjkr|rtd |s|n|}tj|||d\}}}	t|d d |d | f ||	}
t	||
|dd d S Nbug in external fortran coderand:0yE>rtolatol
	_IS_32BITdtyper   complex_pytestxfail
pymatrixidr   reconstruct_matrix_from_idr	   )selfr   r   r   r&   lin_opA_or_LkidxprojBr   r   r   test_real_id_fixed_precisionJ   s   
"z;TestInterpolativeDecomposition.test_real_id_fixed_precisionc                 C   sx   t r|jtjkr|rtd |}|s|n|}tj|||d\}	}
t|d d |	d | f |	|
}t	|||dd d S r#   r+   )r3   r   r   r   r    r&   r4   r6   r5   r7   r8   r9   r   r   r   test_real_id_fixed_rankW   s   
"z6TestInterpolativeDecomposition.test_real_id_fixed_rankr"   c                 C   sz   |}|s|n|}t j|||d\}	}
t |	|
}t |||	}t||d d |	d | f |dd t|| ||dd d S )Nr%   r'   r(   )r1   r   reconstruct_interp_matrixreconstruct_skel_matrixr	   )r3   r   r   r   r    r&   r4   r6   r5   r7   r8   Pr9   r   r   r   %test_real_id_skel_and_interp_matricesd   s   $zDTestInterpolativeDecomposition.test_real_id_skel_and_interp_matricesc                 C   sf   t r|jtjkr|rtd |s|n|}tj|||d\}}}	|| |	j	  }
t
||
|dd d S r#   r,   r-   r   r.   r/   r0   r1   r   Tconjr	   )r3   r   r   r   r&   r4   r5   Ur   Vr9   r   r   r   test_svd_fixed_precisonp   s   
z6TestInterpolativeDecomposition.test_svd_fixed_precisonc                 C   sj   t r|jtjkr|rtd |}|s|n|}tj|||d\}	}
}|	|
 |j	  }t
|||dd d S r#   r@   )r3   r   r   r   r    r&   r4   r6   r5   rC   r   rD   r9   r   r   r   test_svd_fixed_rank|   s   
z2TestInterpolativeDecomposition.test_svd_fixed_rankc                 C   sf   |}t j||dd\}}t |d d |d | f ||\}}}	|| |	j  }
t||
|dd d S )NFr%   r'   r(   )r1   r   	id_to_svdrA   rB   r	   )r3   r   r   r    r6   r7   r8   rC   r   rD   r9   r   r   r   test_id_to_svd   s
   (z-TestInterpolativeDecomposition.test_id_to_svdc                 C   s,   t |}t|}tt||d d d S )Nr   ư>)r   r1   estimate_spectral_normr   r   allclose)r3   r   s
norm_2_estr   r   r   test_estimate_spectral_norm   s   
z:TestInterpolativeDecomposition.test_estimate_spectral_normc                 C   sR   |  }|d d df  d9  < t|| }t||}tt||d d d S )Nr   g333333?rI   )copyr   r1   estimate_spectral_norm_diffr   r   rK   )r3   r   r9   rL   rM   r   r   r    test_estimate_spectral_norm_diff   s
   z?TestInterpolativeDecomposition.test_estimate_spectral_norm_diffc                 C   sx   t jg dg dg dg|jd}||fD ]$}d}t j|t|d| }t||}t||k t||d k qd S )N   rS   r   r   r   rS   r-   &.>   
   )	r   arrayr-   r   matrix_rankr   r1   estimate_rankr   )r3   r   r9   Mrank_tolrank_nprank_estr   r   r   test_rank_estimates_array   s   "z8TestInterpolativeDecomposition.test_rank_estimates_arrayc                 C   s   t jg dg dg dg|jd}||fD ]*}t|}d}t j|t|d| }t||}t	||d k t	||d k qd S )NrR   rT   rU   rV   rW      )
r   rY   r-   r   r   rZ   r   r1   r[   r   )r3   r   r9   r\   MLr]   r^   r_   r   r   r   test_rank_estimates_lin_op   s   "z9TestInterpolativeDecomposition.test_rank_estimates_lin_opc                 C   s   t d ttt dddgd t d t d}tt|ddgd tjd t   t d}tjd t tjd	 t d}t|| t|| d S )
NdefaultrW   g#$?g<P?g-C6?i  gdbR?g2@吱?7   )r1   seedr   r   rK   r&   randomr	   )r3   x1x2x3r   r   r   	test_rand   s   





z(TestInterpolativeDecomposition.test_randc                 C   s(   t dtj}tttj|ddd d S )N   rI   Fr%   )r   r   r   float32assert_raises
ValueErrorr1   r   )r3   r   r   r   r   test_badcall   s   z+TestInterpolativeDecomposition.test_badcallc                 C   sD   t d}tt t|d W d    d S 1 sw   Y  d S )N)ra      ra   )r   onesrn   ro   r1   r   )r3   ar   r   r   test_rank_too_large   s   

"z2TestInterpolativeDecomposition.test_rank_too_largec                 C   s   d}t jdd}t||\}}}t||jd k t||}t|||}t	||
| t||\}}t||}t|||}t	||
| d S )Nr         rS   )r   rg   r&   r1   r   r   r   r<   r=   r	   dot)r3   r   r   r6   r7   r8   r>   r9   r   r   r   test_full_rank   s   z-TestInterpolativeDecomposition.test_full_rankr-   r&   TFr   rS   g?c                 C   sp   t r|tjkr|rtd tjg dg dg dg dg dg|dd}| }t|j||d	 t	|| d S )
Nr$   )ry   ry   r   r   r   )r   r   r   rS   rS   rS   )rS   r   r   rS   r   r   )r   rS   r   r   rS   r   )r   r   rS   r   r   rS   C)r-   orderr%   )
r,   r   r.   r/   r0   rY   rO   r   rA   r   )r3   r-   r&   r   r   r9   r   r   r   test_bug_9793   s   

z,TestInterpolativeDecomposition.test_bug_9793N)__name__
__module____qualname__r/   markparametrizer:   r;   r?   rE   rF   rH   rN   rQ   r`   rc   rk   rp   rt   rx   r   float_r.   r|   r   r   r   r   r!   H   sH    





	


r!   )!scipy.linalg.interpolativer   interpolativer1   numpyr   scipy.linalgr   r   r   scipy.sparse.linalgr   r   	itertoolsnumpy.testingr   r	   r
   r   r/   r   rn   sysmaxsizer,   fixturer   float64
complex128r   r   r    r!   r   r   r   r   <module>   s(   




	