o
    `^h:                     @   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   c/home/air/shanriGPT/back/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)
compute_uvr   )r   linalgsvd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'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>rtolatol
pymatrixidr   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-   )r0   r   r   r   r$   r(   r1   r   r3   r2   r4   r5   r6   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	   )r0   r   r   r   r$   r(   r1   r   r3   r2   r4   r5   Pr6   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	   )r0   r   r   r   r(   r1   r   r2   Ur#   Vr6   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'   r=   )r0   r   r   r   r$   r(   r1   r   r3   r2   r@   r#   rA   r6   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   	id_to_svdr>   r?   r	   )r0   r   r   r$   r3   r4   r5   r@   r#   rA   r6   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.   estimate_spectral_normr	   )r0   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   rG   r)   r*   )copyr   r.   estimate_spectral_norm_diffr	   )r0   r   r   r6   rI   rJ   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   rP   r   r   r   rP   dtype&.>   r   
   )	r   arrayrS   r   matrix_rankr   r.   estimate_rankr   )r0   r   r   r6   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 )NrO   rQ   rR   rT   rU   r      )
r   rW   rS   r   r   rX   r   r.   rY   r   )	r0   r   r   r6   rZ   MLr[   r\   r]   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   rG   FrD   )r   r   r   float32assert_raises
ValueErrorr.   r   )r0   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)r_      r_   )r   onesrd   re   r.   r   )r0   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         rP   )
r   r   r(   r.   r   r
   r"   r9   r:   r	   )r0   r   r   r3   r4   r5   r;   r6   r   r   r   test_full_rank   s   z-TestInterpolativeDecomposition.test_full_rankrS   r(   TFr   rP   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)rn   rn   r   r   r   )r   r   r   rP   rP   rP   )rP   r   r   rP   r   r   )r   rP   r   r   rP   r   )r   r   rP   r   r   rP   C)rS   orderrD   )r   rW   rL   r   r>   r   )r0   rS   r(   r   r   r6   r   r   r   test_bug_9793   s   
z,TestInterpolativeDecomposition.test_bug_9793N)__name__
__module____qualname__pytestmarkparametrizer7   r8   r<   rB   rC   rF   rK   rN   r^   ra   rf   rj   rm   r   float64
complex128rq   r   r   r   r   r%   K   sF    





r%   )scipy.linalg.interpolativer   interpolativer.   numpyr   scipy.linalgr   r   r   scipy.sparse.linalgr   r   numpy.testingr   r	   r
   r   ru   r   rd   fixturer   r   rx   ry   r   r   r$   r%   r   r   r   r   <module>   s&   




	