3
Odq%                 @   s   d Z ddlZddlmZmZmZmZ ddlm	Z	 ddl
ZddlmZmZmZmZmZmZ dd ZG dd	 d	eZG d
d deZdS )z;Test functions for the sparse.linalg._expm_multiply module
    N)assert_allcloseassert_assert_equalsuppress_warnings)SparseEfficiencyWarning)_theta_compute_p_max_onenormest_matrix_powerexpm_multiply_expm_multiply_simple_expm_multiply_intervalc             C   s   t j| |p| |k S )N)npZallclose)ab r   `/var/www/html/virt/lib64/python3.6/site-packages/scipy/sparse/linalg/tests/test_expm_multiply.pyless_than_or_close   s    r   c               @   s`   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d Zdd Z	dd Z
dd Zdd Zdd ZdS )TestExpmActionSimplezR
    These tests do not consider the case of multiple time steps in one call.
    c             C   sL   t tj }x:t|d d |dd  D ]\\}}\}}t||k  q(W d S )N   )sortedr   itemszipr   )selfpairsZm_aZtheta_aZm_bZtheta_br   r   r   test_theta_monotonicity   s    ,z,TestExpmActionSimple.test_theta_monotonicityc             C   s   d}d}t |}t|| d S )N7      )r   r   )r   m_maxZexpected_p_maxZobserved_p_maxr   r   r   test_p_max_default   s    z'TestExpmActionSimple.test_p_max_defaultc             C   sX   xRt ddD ]D}t|}t||d  |d k |d }t||d  |d k qW d S )Nr   r   8   )ranger   r   )r   r   Zp_maxZ	p_too_bigr   r   r   test_p_max_range"   s
    z%TestExpmActionSimple.test_p_max_rangec       	      C   s   t jjd d}d}xt|D ]}tjjt jj||}xdtdD ]X}|sVt j|}nt j	||}t
||}t jj|d}tt|| tt|d|  qBW qW d S )Ni  (   
      r      )r   randomseedr!   scipylinalginvrandnidentitydotr	   Znormr   r   )	r   nnsamplesiApMZ	estimatedexactr   r   r   test_onenormest_matrix_power)   s    
z1TestExpmActionSimple.test_onenormest_matrix_powerc       	      C   sz   t jjd d}d}d}x\t|D ]P}tjjt jj||}t jj||}t||}t j	tjj
||}t|| q"W d S )Ni  r#   r&   r$   )r   r'   r(   r!   r)   r*   r+   r,   r
   r.   expmr   )	r   r/   kr0   r1   r2   Bobservedexpectedr   r   r   test_expm_multiply9   s    
z'TestExpmActionSimple.test_expm_multiplyc             C   st   t jjd d}d}xZt|D ]N}tjjt jj||}t jj|}t||}t j	tjj
||}t|| qW d S )Ni  r#   r$   )r   r'   r(   r!   r)   r*   r+   r,   r
   r.   r7   r   )r   r/   r0   r1   r2   vr:   r;   r   r   r   test_matrix_vector_multiplyE   s    
z0TestExpmActionSimple.test_matrix_vector_multiplyc       
      C   s   t jjd d}d}d}xt|D ]~}xxdD ]p}t jdd	Z tjjt jj||}t jj||}t	|||d
}t j
tjj|| |}	t||	 W d Q R X q,W q"W d S )Ni  r#   r&   r$   皙?      ?      ?ignore)invalid)t)r?   r@   rA   )r   r'   r(   r!   Zerrstater)   r*   r+   r,   r   r.   r7   r   )
r   r/   r8   r0   r1   rD   r2   r9   r:   r;   r   r   r   test_scaled_expm_multiplyP   s    
z.TestExpmActionSimple.test_scaled_expm_multiplyc             C   sf   t jjd d}d}d}t jj||}t jj||}t|||d}tjj|| j|}t	|| d S )Ni  g?      )rD   )
r   r'   r(   r,   r   r)   r*   r7   r.   r   )r   rD   r/   r8   r2   r9   r:   r;   r   r   r   *test_scaled_expm_multiply_single_timepoint^   s    z?TestExpmActionSimple.test_scaled_expm_multiply_single_timepointc       
      C   s   t jjd d}d}d}xt|D ]t}tjj||dd}t jj||}t||}t	 0}|j
td |j
td tjj|j|}	W d Q R X t||	 q"W d S )	Ni  r#   r&   r$   g?)densityzsplu requires CSC matrix formatzCspsolve is more efficient when sparse b is in the CSC matrix format)r   r'   r(   r!   r)   sparserandr,   r
   r   filterr   r*   r7   r.   r   )
r   r/   r8   r0   r1   r2   r9   r:   supr;   r   r   r   test_sparse_expm_multiplyi   s    
z.TestExpmActionSimple.test_sparse_expm_multiplyc             C   s   t jddgddggtd}t jddg}t||}t jdt jd ddt jd t jd   dt jd gtd}t|| d S )Ny              ?r   )dtyper   )r   arraycomplexr
   expcossinr   )r   r2   r9   r:   r;   r   r   r   test_complexz   s    
(z!TestExpmActionSimple.test_complexN)__name__
__module____qualname____doc__r   r   r"   r6   r<   r>   rE   rH   rN   rU   r   r   r   r   r      s   r   c               @   sL   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd Zdd Z	dd Z
dS )TestExpmActionIntervalc             C   s   t jjd d}d}d}d}d}xdD ]}tjj||d
d}t jj||}t jj|}	x||	fD ]}
t||
||||d}t j||||d}t	 R}|j
td |j
td x2t||D ]$\}}t|tjj|| j|
 qW W d Q R X q`W q&W d S )Ni  g?g	@r#   r&   T      rG   g?)rI   )startstopnumendpointzsplu requires CSC matrix formatzCspsolve is more efficient when sparse b is in the CSC matrix format)r[   r\   rG   )r   r'   r(   r)   rJ   rK   r,   r
   linspacer   rL   r   r   r   r*   r7   r.   )r   r]   r^   r/   r8   r`   r_   r2   r9   r=   targetXsamplesrM   solutionrD   r   r   r   "test_sparse_expm_multiply_interval   s.    

z9TestExpmActionInterval.test_sparse_expm_multiply_intervalc          
   C   s   t jjd d}d}d}xdD ]}xdD ]}tjjt jj||}t jj|}t||||||d}t j||||d}	x2t	||	D ]$\}
}t
|
tjj|| j| qW q(W qW d S )Ni  g?g	@Tr[   r\   rG   r   rF      r#   )r]   r^   r_   r`   )r[   r\   rG   )r   rG   rF   rg   r#   )r   r'   r(   r)   r*   r+   r,   r
   ra   r   r   r7   r.   )r   r]   r^   r`   r_   r/   r2   r=   rc   rd   re   rD   r   r   r   "test_expm_multiply_interval_vector   s    


z9TestExpmActionInterval.test_expm_multiply_interval_vectorc             C   s   t jjd d}d}d}xdD ]}xdD ]}xdD ]}tjjt jj||}t jj||}t||||||d}	t j||||d}
x2t	|	|
D ]$\}}t
|tjj|| j| qW q2W q(W qW d S )Ni  g?g	@Tr[   r\   rG   r   rF   rg   r#   )r]   r^   r_   r`   )r[   r\   rG   )r   rG   rF   rg   r#   )r   rG   )r   r'   r(   r)   r*   r+   r,   r
   ra   r   r   r7   r.   )r   r]   r^   r`   r_   r/   r8   r2   r9   rc   rd   re   rD   r   r   r   "test_expm_multiply_interval_matrix   s    



z9TestExpmActionInterval.test_expm_multiply_interval_matrixc             C   s.  t jjtjddtd}tjdtd}t jjtjtjddd}tt	||ddd	 |j
| t jjd
tjd dtd}tjdtd}t jjtjdtjd dd}tt	||ddd |j
| t jjtjddtd}tjddtd}t jjtjtjddd}tt	||ddd |j
| d S )NrF   Zcsr)formatrO   )rO   )rj   r   r   y              ?r   y             y             r   r   )r)   rJ   Zdiagsr   ZarangeintZonesrR   r   r
   r.   rQ   full)r   r2   r9   ZAexpmr   r   r   )test_sparse_expm_multiply_interval_dtypes   s     z@TestExpmActionInterval.test_sparse_expm_multiply_interval_dtypesc             C   s   | j d d S )Nr   )(_help_test_specific_expm_interval_status)r   r   r   r   $test_expm_multiply_interval_status_0   s    z;TestExpmActionInterval.test_expm_multiply_interval_status_0c             C   s   | j d d S )Nr   )rn   )r   r   r   r   $test_expm_multiply_interval_status_1   s    z;TestExpmActionInterval.test_expm_multiply_interval_status_1c             C   s   | j d d S )NrG   )rn   )r   r   r   r   $test_expm_multiply_interval_status_2   s    z;TestExpmActionInterval.test_expm_multiply_interval_status_2c          
   C   s  t jjd d}d}d}d}d}d}d}d	}	xd
ddg| D ]}t jj||}
t jj||}t|
|||||dd}||kr<t|
|||||dd\}}t|j|||f t j||||d}x2t||D ]$\}}t	|t
jj||
 j| qW |	d7 }	q<W |	sdt| d }t|d S )Ni  g?g	@r\   TrF   rG   r$   r   r[   )r]   r^   r_   r`   Zstatus_onlyF)r]   r^   r_   r`   r   zfailed to find a status-z	 interval)r   r'   r(   r,   r   r   shapera   r   r   r)   r*   r7   r.   str	Exception)r   Ztarget_statusr]   r^   r_   r`   r/   r8   ZnrepeatsZ
nsuccessesr2   r9   statusrc   rd   re   rD   msgr   r   r   rn      s8    
 z?TestExpmActionInterval._help_test_specific_expm_interval_statusN)rV   rW   rX   rf   rh   ri   rm   ro   rp   rq   rn   r   r   r   r   rZ      s   rZ   )rY   Znumpyr   Znumpy.testingr   r   r   r   Zscipy.sparser   Zscipy.linalgr)   Z"scipy.sparse.linalg._expm_multiplyr   r   r	   r
   r   r   r   objectr   rZ   r   r   r   r   <module>   s    t