3
Od$                 @   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eZG d	d
 d
e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/html/virt/lib64/python3.6/site-packages/scipy/sparse/linalg/tests/test_onenormest.py__init__   s    zMatrixProductOperator.__init__c             C   s   t j| jt j| j|S )N)npdotr   r   )r   xr   r   r   _matvec   s    zMatrixProductOperator._matvecc             C   s   t jt j|| j| jS )N)r   r   r   r   )r   r   r   r   r   _rmatvec   s    zMatrixProductOperator._rmatvecc             C   s   t j| jt j| j|S )N)r   r   r   r   )r   Xr   r   r   _matmat!   s    zMatrixProductOperator._matmatc             C   s   t | jj| jjS )N)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jd d}d}d}d}g }g }g }g }xrt|D ]f}	tjjt jj||}
t|
|
j	||\}}}}}|j
| |j
tjj|
d |j
| |j
| q6W t j|td}t j|td}t j|| | }|| }tdt j|  k od	k n   tt j|d td
t j|  k o$dk n   t j|dk }|t| }td|  k o\dk n   tdt j|  k odk n   d S )Ni  r   d      i  r	   )dtypegGz?g      ?g?g?g+=g?gffffff?g      @g      @)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   sh  t jjd d}d}d}d}g }g }g }g }xrt|D ]f}	t jjdd||fd}
t|
|
j||\}}}}}|j| |jtj	j
|
d |j| |j| q6W t j|td	}t j|td	}t j|| | }|| }td
t j|  k odk n   tt j|d t j|dk }|t| }td|  k o8dk n   tdt j|  k o\dk n   d S )Ni     r!   r"   i  r	   r   )size)r#   g?gGz?r   g+=g333333?g      ?g      @g      @)r   r$   r%   r&   randintr   r   r+   r'   r(   r,   r-   r.   r/   r   r0   r   r1   r2   )r   r3   r4   r5   r6   r7   r8   r9   r:   r;   r   r<   r=   r>   r?   r@   rA   rB   rC   rD   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jtj| tj|dd   }tjdgdg|d   }tj fddt|D }t jj|| }t	|| t
||j||\}}	}
}}t j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   ).0r;   )alphar   r   
<listcomp>   s    z>TestOnenormest.test_onenormest_table_5_t_1.<locals>.<listcomp>g?g-C6?)rtol   )r3   r5   g!?)r'   r(   r)   r   identityZeyer-   r&   Ztoeplitzr   r   r   r,   r   sparse
onenormest)r   r3   r4   r5   r   Z	first_colZ	first_rowr   r<   r=   r>   r?   r@   exact_valueZunderest_ratioZ	est_plainr   )rM   r   test_onenormest_table_5_t_1z   s"    &


z*TestOnenormest.test_onenormest_table_5_t_1c             C   s  t jjd d}d}d}d}g }g }g }g }xt|D ]|}	t jj||dt jj||  }
tjj|
}t||j	||\}}}}}|j
| |j
tjj|d |j
| |j
| q6W t j|td}t j|td}t j|| | }|| }t j|}td|  k od	k n   t j|}t|d
 t j|dk }|t| }td|  k oXdk n   t j|}td|  k odk n   d S )Ni  r	   r!   r"   i  y              ?)r#   g?gGz?r   g+=gffffff?g?   )r   r$   r%   r&   randr'   r(   r)   r   r   r+   r,   r-   r.   r/   r0   r   r1   r   r2   )r   r3   r4   r5   r6   r7   r8   r9   r:   r;   ZA_invr   r<   r=   r>   r?   r@   rA   rB   Zunderestimation_ratio_meanZmax_nresamplesrC   rD   Z
mean_nmultr   r   r   test_onenormest_table_6_t_1   s<     





z*TestOnenormest.test_onenormest_table_6_t_1c             C   s   t j||}tjj|dS )Nr	   )r   r   r'   r(   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   r3   r5   Dr<   r=   r>   r?   r@   r   r   r   _help_product_norm_fast   s
    
z&TestOnenormest._help_product_norm_fastc             C   sv   t jjd d}d}t jj||}t jj||}| j||}| j||}t||  ko`d| kn  d||f  d S )Ni  ip     zfast: %g
exact:%g)r   r$   r%   r*   r\   rZ   r   )r   r4   rK   r   r   Zfast_estimaterT   r   r   r   test_onenormest_linear_operator   s    z.TestOnenormest.test_onenormest_linear_operatorc       
      C   s   t jjd tjjddd}tjj|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j|j|d|t jj|d d	d
 t
|j||d	d
 d S )Ni  2   g?r	   T)	compute_v)	compute_w)ra   r`   g&.>)rO   )r   r$   r%   r'   rR   rW   r(   r,   ZtodenserS   r   r   )
r   r   s0s1r=   s2r>   Zs3v2Zw2r   r   r   test_returns   s    ,zTestOnenormest.test_returnsN)r   r   r   pytestmarkZxslowrE   rJ   rU   rX   rZ   r\   Zslowr^   rf   r   r   r   r   r    )   s   ('.r    c               @   s   e Zd Zdd ZdS )TestAlgorithm_2_2c             C   sn   t jjd d}d}xTt|D ]H}t jjdd}t jjdd}tjjt jj||}t	||j
|\}}qW d S )Ni     r!   r	   rV   
   )   )r   r$   r%   r&   rI   r'   r(   r)   r*   r   r   )r   r4   r6   r;   r3   r   gindr   r   r   test_randn_inv   s    z TestAlgorithm_2_2.test_randn_invN)r   r   r   ro   r   r   r   r   ri      s   ri   )r   Znumpyr   Znumpy.testingr   r   r   rg   Zscipy.linalgr'   Zscipy.sparse.linalgZscipy.sparse.linalg._onenormestr   r   rR   r(   ZLinearOperatorr   objectr    ri   r   r   r   r   <module>   s    A