a
    R¹§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r8t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/sunrise/env/lib/python3.9/site-packages/scipy/sparse/linalg/tests/test_onenormest.pyÚ__init__   s    zMatrixProductOperator.__init__c                 C   s   t  | jt  | j|¡¡S ©N©ÚnpÚdotr   r   ©r   Úxr   r   r   Ú_matvec   s    zMatrixProductOperator._matvecc                 C   s   t  t  || j¡| j¡S r   r   r   r   r   r   Ú_rmatvec   s    zMatrixProductOperator._rmatvecc                 C   s   t  | jt  | j|¡¡S r   r   )r   ÚXr   r   r   Ú_matmat!   s    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 ]f}	tj t j ||¡¡}
t|
|
j	||ƒ\}}}}}| 
|¡ | 
tj |
d¡¡ | 
|¡ | 
|¡ q4t j|td}t j|td}t  || ¡| }|| }tdt  |¡  k oìd	k n  ƒ tt  |¡dƒ td
t  |¡  k o dk n  ƒ t  |dk ¡}|t|ƒ }td|  k oXdk n  ƒ tdt  |¡  k o|dk n  ƒ 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   sd  t j d¡ d}d}d}d}g }g }g }g }t|ƒD ]f}	t jjdd||fd}
t|
|
j||ƒ\}}}}}| |¡ | tj	 
|
d	¡¡ | |¡ | |¡ q4t j|td
}t j|td
}t  || ¡| }|| }tdt  |¡  k oìdk n  ƒ tt  |¡dƒ t  |dk ¡}|t|ƒ }td|  k o4dk n  ƒ tdt  |¡  k oXdk n  ƒ d S )Nr$   é   r%   r&   r'   éÿÿÿÿr   )Úsizer	   r(   r,   r)   r   r+   g333333Ã?g      Ð?r-   r.   )r   r/   r0   r1   Úrandintr   r   r6   r2   r3   r7   r8   r9   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   )Ú.0rG   ©Úalphar   r   Ú
<listcomp>‚   ó    z>TestOnenormest.test_onenormest_table_5_t_1.<locals>.<listcomp>r*   g-Cëâ6?©Zrtolé   )r?   rA   )r2   r3   r4   r   ÚidentityZeyer8   r1   Ztoeplitzr   r   r   r7   r   ÚsparseÚ
onenormest)r   r?   r@   rA   r   Z	first_colZ	first_rowr   rH   rI   rJ   rK   rL   Úexact_valueZunderest_ratioZ	est_plainr   rY   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¡¡ | 
|¡ | 
|¡ q4t 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 oTdk n  ƒ t  |¡}td|  k o|dk n  ƒ d S )Nr$   r	   r%   r&   r'   y              ð?r(   r,   r)   r   r+   gffffffæ?gš™™™™™é?é   )r   r/   r0   r1   Úrandr2   r3   r4   r   r   r6   r7   r8   r9   r:   r;   r   r<   r   r=   )r   r?   r@   rA   rB   rC   rD   rE   rF   rG   ZA_invr   rH   rI   rJ   rK   rL   rM   rN   Zunderestimation_ratio_meanZmax_nresamplesrO   rP   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   r2   r3   r7   )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   r?   rA   ÚDrH   rI   rJ   rK   rL   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o`d| kn  d|d›d|d›ƒ d S )Nr$   ip  é   zfast: Úgz
exact:)r   r/   r0   r5   rj   rh   r   )r   r@   rW   r   r   Zfast_estimaterb   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)rp   ro   g•Ö&è.>r]   )r   r/   r0   r2   r`   re   r3   r7   Ztoarrayra   r   r   )
r   r   Ús0Ús1rI   Ús2rJ   Zs3Zv2Zw2r   r   r   Útest_returnsÛ   s    ,zTestOnenormest.test_returnsN)r   r   r    ÚpytestÚmarkZxslowrQ   rV   rc   rf   rh   rj   Zslowrm   rt   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 ]H}t j dd¡}t j dd¡}tj t j ||¡¡}t	||j
|ƒ\}}qd S )Nr$   é   r%   r	   rd   é
   é)   )r   r/   r0   r1   rU   r2   r3   r4   r5   r   r   )r   r@   rB   rG   r?   r   rl   Úindr   r   r   Útest_randn_invë   s    z TestAlgorithm_2_2.test_randn_invN)r   r   r    r|   r   r   r   r   rw   é   s   rw   )r!   Únumpyr   Znumpy.testingr   r   r   ru   Zscipy.linalgr2   Zscipy.sparse.linalgZscipy.sparse.linalg._onenormestr   r   r`   r3   ZLinearOperatorr   r#   rw   r   r   r   r   Ú<module>   s    A