o
    i!                     @   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 e dd 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)raisesc                   c   s    dV  d S )N-q= r   r   r   _/home/kim/smarthome/.venv/lib/python3.10/site-packages/scipy/linalg/tests/test_interpolative.pyeps)   s   
r   c                  c   s    t jd} | V  d S )Nl   ,b30k0 )nprandomdefault_rngrngr   r   r   r   .   s   
r   )paramsc                 c   s    d}t || jV  d S )Ni,  )r   astypeparam)requestnr   r   r   A4   s   r   c                 c   s    t | V  d S )Nr   )r   r   r   r   L<   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)Z
compute_uvr   )r   linalgsvdZnonzero
IndexErrorshape)r   r   Srankr   r   r   r"   A   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ejdejejgejdd d!gejd"d#d$gd%d& Zd'd( Zd)S )*TestInterpolativeDecompositionzrand,lin_op)FF)TF)TTc                 C   sZ   |s|n|}t j||||d\}}	}
t |d d |	d | f |	|
}t|||dd d S N)randr   :0yE>ZrtolZatol
pymatrixidr   Zreconstruct_matrix_from_idr	   )selfr   r   r   r&   lin_opr   A_or_LkidxprojBr   r   r   test_real_id_fixed_precisionM   s   "z;TestInterpolativeDecomposition.test_real_id_fixed_precisionc                 C   s\   |}|s|n|}	t j|	|||d\}
}t |d d |
d | f |
|}t|||dd d S r%   r)   )r+   r   r   r   r"   r&   r,   r   r.   r-   r/   r0   r1   r   r   r   test_real_id_fixed_rankX   s
   "z6TestInterpolativeDecomposition.test_real_id_fixed_rankr$   c                 C   s|   |}|s|n|}	t j|	|||d\}
}t |
|}t |||
}t||d d |
d | f |dd t|| ||dd d S r%   )r*   r   reconstruct_interp_matrixreconstruct_skel_matrixr	   )r+   r   r   r   r"   r&   r,   r   r.   r-   r/   r0   Pr1   r   r   r   %test_real_id_skel_and_interp_matricesc   s   $zDTestInterpolativeDecomposition.test_real_id_skel_and_interp_matricesc                 C   sJ   |s|n|}t j||||d\}}	}
||	 |
j  }t|||dd d S r%   r*   r   Tconjr	   )r+   r   r   r   r&   r,   r   r-   Ur!   Vr1   r   r   r   test_svd_fixed_precisiono   s   z7TestInterpolativeDecomposition.test_svd_fixed_precisionc                 C   sN   |}|s|n|}	t j|	|||d\}
}}|
| |j  }t|||dd d S r%   r8   )r+   r   r   r   r"   r&   r,   r   r.   r-   r;   r!   r<   r1   r   r   r   test_svd_fixed_ranky   s
   z2TestInterpolativeDecomposition.test_svd_fixed_rankc                 C   sf   |}t j||dd\}}t |d d |d | f ||\}}}	|| |	j  }
t||
|dd d S )NFr&   r'   r(   )r*   r   Z	id_to_svdr9   r:   r	   )r+   r   r   r"   r.   r/   r0   r;   r!   r<   r1   r   r   r   test_id_to_svd   s
   (z-TestInterpolativeDecomposition.test_id_to_svdc                 C   s.   t |}tj||d}t||d ddd d S )Nr   r   ư>r'   r(   )r   r*   Zestimate_spectral_normr	   )r+   r   r   s
norm_2_estr   r   r   test_estimate_spectral_norm   s   z:TestInterpolativeDecomposition.test_estimate_spectral_normc                 C   sT   |  }|d d df  d9  < t|| }tj|||d}t||d ddd d S )Nr   g333333?r   rA   r'   r(   )copyr   r*   Zestimate_spectral_norm_diffr	   )r+   r   r   r1   rB   rC   r   r   r    test_estimate_spectral_norm_diff   s
   z?TestInterpolativeDecomposition.test_estimate_spectral_norm_diffc                 C   s|   t jg dg dg dg|jd}||fD ]&}d}t j|t|d| }tj|||d}t||k t||d k qd S )N   rH   r   r   r   rH   dtype&.>   r   
   )	r   arrayrK   r   matrix_rankr   r*   estimate_rankr   )r+   r   r   r1   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j|||d}t	||d k t	||d k qd S )NrG   rI   rJ   rL   rM   r      )
r   rO   rK   r   r   rP   r   r*   rQ   r   )	r+   r   r   r1   rR   ZMLrS   rT   rU   r   r   r   test_rank_estimates_lin_op   s   "z9TestInterpolativeDecomposition.test_rank_estimates_lin_opc                 C   sN   t dtj}tt tj|ddd W d    d S 1 s w   Y  d S )N   rA   Fr?   )r   r   r   Zfloat32assert_raises
ValueErrorr*   r   )r+   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)rW      rW   )r   ZonesrZ   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   r   r&   r*   r   r
   r    r4   r5   r	   )r+   r   r   r.   r/   r0   r6   r1   r   r   r   test_full_rank   s   z-TestInterpolativeDecomposition.test_full_rankrK   r&   TFr   rH   g?c                 C   sT   t jg dg dg dg dg dg|dd}| }t|j||d t|| d S )	N)rc   rc   r   r   r   )r   r   r   rH   rH   rH   )rH   r   r   rH   r   r   )r   rH   r   r   rH   r   )r   r   rH   r   r   rH   C)rK   orderr?   )r   rO   rE   r   r9   r   )r+   rK   r&   r   r   r1   r   r   r   test_bug_9793   s   
z,TestInterpolativeDecomposition.test_bug_9793c                 C   s   t jd}|jddgd}t|}t|d\}}}t|jd t|jd t|jd |jd	d
gd}t|}t|d\}}}t|jd t|jd t|jd d S )Nl   CaNJ1    rY   )sizer]   )rg   r]   )r]   )rY   r]   rW   	   rM   )rW   rM   )rM   )ri   rM   )	r   r   r   uniformr   r*   r   r
   r    )r+   r   xZxlurB   vr   r   r   %test_svd_aslinearoperator_shape_check   s   zDTestInterpolativeDecomposition.test_svd_aslinearoperator_shape_checkN)__name__
__module____qualname__pytestmarkZparametrizer2   r3   r7   r=   r>   r@   rD   rF   rV   rX   r\   r_   rb   r   float64
complex128rf   rn   r   r   r   r   r#   K   sH    





r#   )Zscipy.linalg.interpolativer   Zinterpolativer*   numpyr   Zscipy.linalgr   r   r   Zscipy.sparse.linalgr   r   Znumpy.testingr   r	   r
   r   rr   r   rZ   Zfixturer   r   rt   ru   r   r   r"   r#   r   r   r   r   <module>   s&   




	