o
    5ήc$                     @   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   P/tmp/pip-target-vg8gfxp4/lib/python/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 odk n   tdt |  k odk  d S    d S )N  r   d        r	   dtypeGz?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 odk n   tdt |  k od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 od	k n   t |}t|d
 t |dk }|t| }td|  k odk n   t |}td|  k odk  d S    d S )Nr&   r	   r'   r(   r)   y              ?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_1   s<    





&z*TestOnenormest.test_onenormest_table_6_t_1c                 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   sv   t jd d}d}t j||}t j||}| ||}| ||}t||  ko0d| kn  d||f  d S )Nr&   ip     zfast: %g
exact:%g)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   slowrz   r   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	   rm   
   )   )r   r2   r3   r4   rX   r5   r6   r7   r8   r   r   )r   rC   rE   rJ   rB   r   g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   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