o
    Ÿ&?e$  ã                   @   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   új/home/www/facesmatcher.com/pyenv/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	   ©Z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      Ð?r/   r0   )r   r1   r2   r3   Úrandintr   r   r8   r4   r5   r9   r:   r;   r<   r   r=   r   r>   r?   r@   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   )Ú.0rI   ©Úalphar   r   Ú
<listcomp>‚   s    z>TestOnenormest.test_onenormest_table_5_t_1.<locals>.<listcomp>r,   g-Cëâ6?©Zrtolé   )rA   rC   )r4   r5   r6   r   ÚidentityÚeyer:   r3   Ztoeplitzr   r   r   r9   r   ÚsparseÚ
onenormest)r   rA   rB   rC   r   Z	first_colÚ	first_rowr   rJ   rK   rL   rM   rN   Úexact_valueZunderest_ratioZ	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-   gffffffæ?gš™™™™™é?é   )r   r1   r2   r3   Úrandr4   r5   r6   r   r   r8   r9   r:   r;   r<   r=   r   r>   r   r?   )r   rA   rB   rC   rD   rE   rF   rG   rH   rI   ZA_invr   rJ   rK   rL   rM   rN   rO   rP   Zunderestimation_ratio_meanZmax_nresamplesrQ   rR   Z
mean_nmultr   r   r   Útest_onenormest_table_6_t_1   s<    





&z*TestOnenormest.test_onenormest_table_6_t_1c                 C   s   t  ||¡}tj |d¡S )Nr	   )r   r   r4   r5   r9   )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   rA   rC   ÚDrJ   rK   rL   rM   rN   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   r1   r2   r7   rm   rk   r   )r   rB   rY   r   r   Zfast_estimatere   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)rs   rr   g•Ö&è.>r^   )r   r1   r2   r4   rb   rh   r5   r9   Ztoarrayrc   r   r   )
r   r   Ús0Ús1rK   Ús2rL   Zs3Úv2Zw2r   r   r   Útest_returnsÛ   s   ,zTestOnenormest.test_returnsN)r    r!   r"   ÚpytestÚmarkZxslowrS   rX   rf   ri   rk   rm   Zslowrp   rx   r   r   r   r   r%   )   s    
'
&
-
r%   c                   @   s   e Zd Zdd„ ZdS )ÚTestAlgorithm_2_2c                 C   sj   t j d¡ d}d}t|ƒD ]$}t j dd¡}t j dd¡}tj t j ||¡¡}t	||j
|ƒ\}}qd S )Nr&   é   r'   r	   rg   é
   é)   )r   r1   r2   r3   rW   r4   r5   r6   r7   r   r   )r   rB   rD   rI   rA   r   ro   Úindr   r   r   Útest_randn_invë   s   ôz TestAlgorithm_2_2.test_randn_invN)r    r!   r"   r€   r   r   r   r   r{   é   s    r{   )r#   Únumpyr   Znumpy.testingr   r   r   ry   Zscipy.linalgr4   Zscipy.sparse.linalgZscipy.sparse.linalg._onenormestr   r   rb   r5   ZLinearOperatorr   r%   r{   r   r   r   r   Ú<module>   s     A