U
    Hvf"                     @   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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   I/tmp/pip-unpacked-wheel-96ln3f52/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   A0   s    r   c                 c   s   t | V  d S )Nr   )r   r   r   r   L8   s    r   c                 C   sN   t jj| dd}zt ||k d d }W n tk
rH   | jd }Y nX |S )NF)Z
compute_uvr   )nplinalgsvdZnonzero
IndexErrorshape)r   r   Srankr   r   r   r   =   s    r   c                   @   s  e Zd Zejddddgdd Zejddddgdd Zejddgd	d
 Zejddddgdd Z	ejddddg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>ZrtolZatol
	_IS_32BITdtyper   complex_pytestxfail
pymatrixidr   Zreconstruct_matrix_from_idr	   )selfr   r   r   r#   lin_opA_or_LkidxprojBr   r   r   test_real_id_fixed_precisionI   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&   )r-   r   r   r   r   r#   r.   r0   r/   r1   r2   r3   r   r   r   test_real_id_fixed_rankV   s    
"z6TestInterpolativeDecomposition.test_real_id_fixed_rankc                 C   sz   |}|s|n|}t j|||d\}	}
t |	|
}t |||	}t||d d |	d | f |dd t|| ||dd d S )Nr"   r$   r%   )r,   r   reconstruct_interp_matrixreconstruct_skel_matrixr	   )r-   r   r   r   r   r#   r.   r0   r/   r1   r2   Pr3   r   r   r   %test_real_id_skel_and_interp_matricesc   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*   r+   r,   r   Tconjr	   )r-   r   r   r   r#   r.   r/   Ur   Vr3   r   r   r   test_svd_fixed_precisono   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:   )r-   r   r   r   r   r#   r.   r0   r/   r=   r   r>   r3   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%   )r,   r   Z	id_to_svdr;   r<   r	   )r-   r   r   r   r0   r1   r2   r=   r   r>   r3   r   r   r   test_id_to_svd   s
    (z-TestInterpolativeDecomposition.test_id_to_svdc                 C   s*   t |}t|}t||d ddd d S )Nr   ư>r$   r%   )r   r,   Zestimate_spectral_normr	   )r-   r   s
norm_2_estr   r   r   test_estimate_spectral_norm   s    
z:TestInterpolativeDecomposition.test_estimate_spectral_normc                 C   sP   |  }|d d df  d9  < t|| }t||}t||d ddd d S )Nr   g333333?rB   r$   r%   )copyr   r,   Zestimate_spectral_norm_diffr	   )r-   r   r3   rC   rD   r   r   r    test_estimate_spectral_norm_diff   s
    z?TestInterpolativeDecomposition.test_estimate_spectral_norm_diffc                 C   s~   t jdddgdddgdddgg|jd}||fD ]H}d}t j|t|d| }t||}t||k t||d k q0d S )N   r   r(   &.>   
   )	r   arrayr(   r   matrix_rankr   r,   estimate_rankr   )r-   r   r3   Mrank_tolrank_nprank_estr   r   r   test_rank_estimates_array   s    (z8TestInterpolativeDecomposition.test_rank_estimates_arrayc                 C   s   t jdddgdddgdddgg|jd}||fD ]T}t|}d}t j|t|d| }t||}t	||d k t	||d k q0d S )NrH   r   rI   rJ   rK      )
r   rM   r(   r   r   rN   r   r,   rO   r   )r-   r   r3   rP   ZMLrQ   rR   rS   r   r   r   test_rank_estimates_lin_op   s    (z9TestInterpolativeDecomposition.test_rank_estimates_lin_opc                 C   s   t d tt dddgddd t d t d}t|d	d
gddd tjd t   t d}tjd t tjd t d}t|| t|| d S )NdefaultrK   g#$?g<P?g-C6?r$   r%   i  gdbR?g2@吱?7   )r,   seedr	   r#   r   random)r-   x1Zx2Zx3r   r   r   	test_rand   s     
 




z(TestInterpolativeDecomposition.test_randc              	   C   s8   t dtj}tt tj|ddd W 5 Q R X d S )N   rB   Fr"   )r   r   r   Zfloat32assert_raises
ValueErrorr,   r   )r-   r   r   r   r   test_badcall   s    
z+TestInterpolativeDecomposition.test_badcallc              	   C   s.   t d}tt t|d W 5 Q R X d S )N)rU      rU   )r   Zonesr^   r_   r,   r   )r-   ar   r   r   test_rank_too_large   s    

z2TestInterpolativeDecomposition.test_rank_too_largec                 C   s   d}t jdd}t||\}}}t||jd  t||}t|||}t	|||  t||\}}t||}t|||}t	|||  d S )Nr         rH   )
r   rZ   r#   r,   r   r
   r   r6   r7   r	   )r-   r   r   r0   r1   r2   r8   r3   r   r   r   test_full_rank   s    z-TestInterpolativeDecomposition.test_full_rankr(   r#   TFr   rH   g?c                 C   s   t r|tjkr|rtd tj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d}| }t|j||d t	|| d S )Nr!   r   rH   C)r(   orderr"   )
r'   r   r)   r*   r+   rM   rF   r   r;   r   )r-   r(   r#   r   r   r3   r   r   r   test_bug_9793   s    
 z,TestInterpolativeDecomposition.test_bug_9793N)__name__
__module____qualname__r*   markZparametrizer4   r5   r9   r?   r@   rA   rE   rG   rT   rV   r\   r`   rc   rf   r   Zfloat_r)   rj   r   r   r   r   r   G   sF   





	


r   ) Zscipy.linalg.interpolativer   Zinterpolativer,   Znumpyr   Zscipy.linalgr   r   r   Zscipy.sparse.linalgr   r   Znumpy.testingr   r	   r
   r   r*   r   r^   sysmaxsizer'   Zfixturer   Zfloat64Z
complex128r   r   r   r   r   r   r   r   <module>   s$   




	