o
    Yi                  	   @   s  d Z ddlZddlmZmZmZmZ ddlZddlm	Z	m
Z
mZ ddlmZ ddlmZmZmZ ddlmZ ddlmZ dd	lmZmZ ee
g d
g dg dg dg dg dgZe
g dZe Ze Zdd Zdd Zeeej ej!dZ"dd Z#G dd dZ$dS )z,Tests for the linalg._isolve.gcrotmk module
    N)assert_assert_allcloseassert_equalsuppress_warnings)zerosarrayallclose)norm)	csr_array	eye_arrayrandom_array)LinearOperator)splu)gcrotmkgmres)   r   r   r   	   )r   r   r   r      r   )r   r   r   r   r   r   )r   r   r   r   r   r   )r      r   r   r   r   )r   r   r   r   r   r   )r      r      r      c                 C   s,   t tds	dgt_tjd  d7  < t|  S )Ncr   r   )hasattrcountr   Amv r   U/tmp/pip-target-1s0edx8b/lib/python/scipy/sparse/linalg/_isolve/tests/test_gcrotmk.pymatvec   s   
r!   c                 C   s(   t tds	dgt_tjd  d7  < d S )Nnr   r   )r   niterr"   r   r   r   r    cb%   s   
r$   r!   shapedtypec                  K   s   t tds	dgt_t tdsdgt_dtjd< t !}|td tt	t
ftt	jd dd| \}}W d    n1 s>w   Y  tjd }ttt	| t
dddtt	| t
  ||fS )	Nr"   r   r   .*called without specifying.*+=)x0rtolg-q=)r+   atol)r   r#   r"   r   r   r   filterDeprecationWarningr   Abr   r&   r   r   r	   )kwsupr*   flagcount_0r   r   r    do_solve.   s   


(
&r5   c                   @   sD   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S )TestGCROTMKc                 C   s   t t }t|jtjtjd}t \}}dt	j
d< t|td\}}t|d ||d k s/J t||dds8J t	j
d dk sAJ d S )Nr%   r   )Mcallbackr   r   r)   )r+   )r   r   tocscr   solver/   r&   r'   r5   r#   r"   r$   r   r   )selfpcr7   r*   r4   x1count_1r   r   r    test_preconditioner?   s   


zTestGCROTMK.test_preconditionerc           	   	   C   s   t jd}tdtdd|d }|d}t /}|td t||t	|j
d dddd	\}}t||t	|j
d ddd
\}}W d    n1 sKw   Y  t|d t|d t j||| dksiJ t|| d S )Nr     )r@   r@   gMb@?)densityrngr(   r   
   )r*   mkmaxiter)r*   restartrF   -C6?)nprandomdefault_rngr   r   r   r-   r.   r   r   r&   r   r   linalgr	   dotr   )	r;   rB   r/   r0   r2   r*   flag0r=   flag1r   r   r    test_arnoldiM   s   
""

zTestGCROTMK.test_arnoldic              	   C   s>  t jd dD ]}dt| }t }|td t |}t||dd\}}t	|d t
||| ddd	 t||ddd
\}}|dkrSt
||| ddd	 t j|}t||dd\}}t	|d t
||| ddd	 t||ddd
\}}|dkrt
||| ddd	 W d    n1 sw   Y  qd S )N  )r   r   rC   d   r   r(   rC   )rF   r   r)   r,   r+   rF   )rI   rJ   seedr   r   r-   r.   onesr   r   r   rM   rand)r;   r"   r/   r2   r0   xinfor   r   r    test_cornercase_   s.   


zTestGCROTMK.test_cornercasec                 C   sv   t ddd}tj|d< td}t }|td t||ddd\}}t|d	 W d    d S 1 s4w   Y  d S )
Nr   lil)format)r   r   r(   r   rC   rT   r   )	r   rI   nanrV   r   r-   r.   r   r   )r;   r/   r0   r2   rX   rY   r   r   r    	test_nans}   s   

"zTestGCROTMK.test_nansc                 C   s   t jd t jddt d }t jd}dD ]9}t }|td t||dd|ddd\}}W d    n1 s=w   Y  t	|d	 t
||| d	d
d qd S )NrQ      )oldestsmallestr(   rC   rH      )rD   rE   truncater+   rF   r   gMbP?rS   )rI   rJ   rU   rW   eyer   r-   r.   r   r   r   rM   )r;   r/   r0   rc   r2   rX   rY   r   r   r    test_truncate   s   
zTestGCROTMK.test_truncatec           	      C   s   dD ]S}g }t ||d\}}tt|dk tt|dk |r-|D ]
\}}t|d u  q"t ||d\}}|rAt|dt|  nt|d t||d k t||dd qd S )	N)TF)CU	discard_Cr   r   r   r   r)   rS   )r5   r   lenr   r   )	r;   rg   rf   r*   r4   r   ur=   r>   r   r   r    test_CU   s   
zTestGCROTMK.test_CUc                 C   s   t jddgddggtd}|dt dd 9 }t ddg}t }|td t||\}}W d    n1 s9w   Y  |dkrLt|	|| d S d S )	Nr   r   r   r   )r'   rR   r   r(   )
rI   r   float	nextafterr   r-   r.   r   r   rM   )r;   r/   r0   r2   xprY   r   r   r    test_denormals   s   zTestGCROTMK.test_denormalsN)
__name__
__module____qualname__r?   rP   rZ   r^   re   rj   rn   r   r   r   r    r6   >   s    
r6   )%__doc__	threadingnumpy.testingr   r   r   r   numpyrI   r   r   r   scipy.linalgr	   scipy.sparser
   r   r   scipy.sparse.linalg._interfacer   scipy.sparse.linalgr   scipy.sparse.linalg._isolver   r   r   r0   localr   r#   r!   r$   r&   r'   r/   r5   r6   r   r   r   r    <module>   s2   
