o
    ñh$$  ã                   @   s~   d Z ddlZddlmZmZmZ ddlZddlZ	ddl
Z	ddlmZmZ G dd„ de	jjjƒZG dd„ dƒZG d	d
„ d
ƒZdS )z8Test functions for the sparse.linalg._onenormest module
é    N)Úassert_allcloseÚassert_equalÚassert_)Ú_onenormest_coreÚ_algorithm_2_2c                   @   s<   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zed
d„ ƒZ	dS )ÚMatrixProductOperatorz0
    This is purely for onenormest testing.
    c                 C   sd   |j dks
|j dkrtdƒ‚|jd |jd krtdƒ‚|| _|| _d| _ |jd |jd f| _d S )Né   z'expected ndarrays representing matricesé   r   zincompatible shapes)ÚndimÚ
ValueErrorÚshapeÚAÚB)Úselfr   r   © r   ú]/var/www/vscode/kcb/lib/python3.10/site-packages/scipy/sparse/linalg/tests/test_onenormest.pyÚ__init__   s   zMatrixProductOperator.__init__c                 C   ó   t  | jt  | j|¡¡S ©N©ÚnpÚdotr   r   ©r   Úxr   r   r   Ú_matvec   ó   zMatrixProductOperator._matvecc                 C   s   t  t  || j¡| j¡S r   r   r   r   r   r   Ú_rmatvec   r   zMatrixProductOperator._rmatvecc                 C   r   r   r   )r   ÚXr   r   r   Ú_matmat!   r   zMatrixProductOperator._matmatc                 C   s   t | jj| jjƒS r   )r   r   ÚTr   )r   r   r   r   r   $   s   zMatrixProductOperator.TN)
Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   Úpropertyr   r   r   r   r   r      s    
r   c                   @   sl   e Zd Zejjdd„ ƒZejjdd„ ƒZdd„ Zejjdd„ ƒZ	d	d
„ Z
dd„ Zejjdd„ ƒZdd„ ZdS )ÚTestOnenormestc                 C   sˆ  t j d¡ d}d}d}d}g }g }g }g }t|ƒD ]3}	tj t j ||¡¡}
t|
|
j	||ƒ\}}}}}| 
|¡ | 
tj |
d¡¡ | 
|¡ | 
|¡ qt j|td}t j|td}t  || ¡| }|| }tdt  |¡  k ovd	k n  ƒ tt  |¡dƒ td
t  |¡  k odk n  ƒ t  |dk ¡}|t|ƒ }td|  k oªdk n  ƒ tdt  |¡  k o¾dk ƒ d S   ƒ d S )NéÒ  r   éd   é   éˆ  r	   ©Údtypeç®Gáz®ï?g      ð?çš™™™™™©?gš™™™™™É?ç›+¡†›„=çÍÌÌÌÌÌì?gffffffî?ç      @ç      @)r   ÚrandomÚseedÚrangeÚscipyÚlinalgÚinvÚrandnr   r   ÚappendÚnormÚarrayÚfloatÚabsr   Úmeanr   ÚmaxÚcount_nonzero©r   ÚtÚnÚitmaxÚnsamplesÚobservedÚexpectedÚ
nmult_listÚnresample_listÚir   ÚestÚvÚwÚnmultsÚ
nresamplesÚrelative_errorsÚunderestimation_ratioÚnexactÚproportion_exactr   r   r   Útest_onenormest_table_3_t_2+   s6   

"",z*TestOnenormest.test_onenormest_table_3_t_2c                 C   sf  t j d¡ d}d}d}d}g }g }g }g }t|ƒD ]3}	t jjdd||fd}
t|
|
j||ƒ\}}}}}| |¡ | tj	 
|
d	¡¡ | |¡ | |¡ qt j|td
}t j|td
}t  || ¡| }|| }tdt  |¡  k ovdk n  ƒ tt  |¡dƒ t  |dk ¡}|t|ƒ }td|  k o™dk n  ƒ tdt  |¡  k o­dk ƒ d S   ƒ d S )Nr&   é   r'   r(   r)   éÿÿÿÿr   )Úsizer	   r*   r/   r,   r   r.   g333333Ã?g      Ð?r0   r1   )r   r2   r3   r4   Úrandintr   r   r9   r5   r6   r:   r;   r<   r=   r   r>   r   r?   r@   rA   r   r   r   Útest_onenormest_table_4_t_7S   s4   

",z*TestOnenormest.test_onenormest_table_4_t_7c                    sú   d}d}d}d‰ t j t |¡ˆ tj|dd  ¡ }t dgdg|d   ¡}t ‡ fdd„t|ƒD ƒ¡}t j ||¡ }t	||ƒ t
||j||ƒ\}}	}
}}t j |d¡}|| }t	|d	d
d t|dƒ t|dƒ t jjj|||d}t	||ƒ d S )Nr	   r'   r(   gé!çýÿï?)Úkr   c                    s   g | ]}ˆ  | ‘qS r   r   )Ú.0rJ   ©Úalphar   r   Ú
<listcomp>‚   s    z>TestOnenormest.test_onenormest_table_5_t_1.<locals>.<listcomp>r-   g-Cëâ6?©Úrtolé   )rB   rD   )r5   r6   r7   r   ÚidentityÚeyer;   r4   Útoeplitzr   r   r   r:   r   ÚsparseÚ
onenormest)r   rB   rC   rD   r   Ú	first_colÚ	first_rowr   rK   rL   rM   rN   rO   Úexact_valueÚunderest_ratioÚ	est_plainr   r\   r   Útest_onenormest_table_5_t_1z   s"   &


z*TestOnenormest.test_onenormest_table_5_t_1c                 C   sˆ  t j d¡ d}d}d}d}g }g }g }g }t|ƒD ]>}	t j ||¡dt j ||¡  }
tj |
¡}t||j	||ƒ\}}}}}| 
|¡ | 
tj |d¡¡ | 
|¡ | 
|¡ qt j|td}t j|td}t  || ¡| }|| }t  |¡}td	|  k oƒd
k n  ƒ t  |¡}t|dƒ t  |dk ¡}|t|ƒ }td|  k o¨dk n  ƒ t  |¡}td|  k o¾dk ƒ d S   ƒ d S )Nr&   r   r'   r(   r)   y              ð?r	   r*   r/   r,   r   r.   gffffffæ?gš™™™™™é?é   )r   r2   r3   r4   Úrandr5   r6   r7   r   r   r9   r:   r;   r<   r=   r>   r   r?   r   r@   )r   rB   rC   rD   rE   rF   rG   rH   rI   rJ   ÚA_invr   rK   rL   rM   rN   rO   rP   rQ   Úunderestimation_ratio_meanÚmax_nresamplesrR   rS   Ú
mean_nmultr   r   r   Útest_onenormest_table_6_t_2   s<    





&z*TestOnenormest.test_onenormest_table_6_t_2c                 C   s   t  ||¡}tj |d¡S )Nr	   )r   r   r5   r6   r:   )r   r   r   ÚCr   r   r   Ú_help_product_norm_slow½   s   z&TestOnenormest._help_product_norm_slowc                 C   s0   d}d}t ||ƒ}t||j||ƒ\}}}}	}
|S )Nr   r(   )r   r   r   )r   r   r   rB   rD   ÚDrK   rL   rM   rN   rO   r   r   r   Ú_help_product_norm_fastÂ   s
   
z&TestOnenormest._help_product_norm_fastc                 C   s~   t j d¡ d}d}t j ||¡}t j ||¡}|  ||¡}|  ||¡}t||  ko0d| kn  d|d›d|d›ƒ d S )Nr&   ip  é   zfast: Úgz
exact:)r   r2   r3   r8   rw   ru   r   )r   rC   rZ   r   r   Úfast_estimateri   r   r   r   Útest_onenormest_linear_operatorÊ   s   ÿz.TestOnenormest.test_onenormest_linear_operatorc           
      C   sÆ   t j d¡ tj ddd¡}tj | ¡ d¡}tjjj	|dd\}}tjjj	|dd\}}tjjj	|ddd\}}}	t
||d	d
 t
t j | |¡d¡|t j |d¡ d	d
 t
| |¡|d	d
 d S )Nr&   é2   gš™™™™™¹?r	   T)Ú	compute_v)Ú	compute_w)r~   r}   g•Ö&è.>r_   )r   r2   r3   r5   re   rn   r6   r:   Útoarrayrf   r   r   )
r   r   Ús0Ús1rL   Ús2rM   Ús3Úv2Úw2r   r   r   Útest_returnsÛ   s   ,zTestOnenormest.test_returnsN)r    r!   r"   ÚpytestÚmarkÚxslowrT   rY   rl   rs   ru   rw   Úslowr{   r†   r   r   r   r   r%   )   s    
'
&
-
r%   c                   @   s   e Zd Zejjdd„ ƒZdS )ÚTestAlgorithm_2_2c           	      C   sd   t j d¡}d}d}t|ƒD ]!}| dd¡}| dd¡}tj | ||¡¡}t	||j
|ƒ\}}qd S )Nr&   é   r'   r	   rm   é
   é)   )r   r2   ÚRandomStater4   rX   r5   r6   r7   r8   r   r   )	r   ÚrngrC   rE   rJ   rB   r   ry   Úindr   r   r   Útest_randn_invë   s   ôz TestAlgorithm_2_2.test_randn_invN)r    r!   r"   r‡   rˆ   Úthread_unsafer’   r   r   r   r   r‹   é   s    r‹   )r#   Únumpyr   Únumpy.testingr   r   r   r‡   Úscipy.linalgr5   Úscipy.sparse.linalgÚscipy.sparse.linalg._onenormestr   r   re   r6   ÚLinearOperatorr   r%   r‹   r   r   r   r   Ú<module>   s     A