o
    &?eF                     @   s   d Z ddlmZ ddlmZ ddlZddlmZm	Z	 ddl
mZmZ ddlZddlmZ ddlm  m  mZ ddlmZ G dd	 d	ZG d
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d Z"d d! Z#d"d# Z$dS )$z7Test functions for the sparse.linalg._interface module
    )partial)productN)raiseswarns)assert_assert_equal)matrixc                   @   $   e Zd Zdd Zdd Zdd ZdS )TestLinearOperatorc                 C   sR   t g dg dg| _t ddgddgddgg| _t ddgddgg| _d S )	N                  r   r   r   r   r   r   )nparrayABCself r   i/home/www/facesmatcher.com/pyenv/lib/python3.10/site-packages/scipy/sparse/linalg/tests/test_interface.pysetup_method   s   


zTestLinearOperator.setup_methodc              
      s^  dd }|| j D ]?}tjdi | t jdk t tg dddg t tdgdgd	ggdgdgg t tg d ddg t tdgdgd	gg dgdgg t 	tg dddg t 	tdgdgd	ggdgdgg t t
dgdgd	ggdgdgg t t
dgdgd	gg dgdgg t 	t
dgdgd	ggdgdgg td  g d
 ddg td  ddgg d td  jddgg d td  dgdgdgg dgdgg td  dgdgdggdgdgg t d g d
 ddg t d dgdgdgg dgdgg td  g d
 ddg t   g d
 ddg t   ddgg d t   jddgg d t   dgdgdgg dgdgg t   dgdgdggdgdgg t  g d
 ddg t  dgdgdgg dgdgg t   g d
 ddg t   dgdgdgg dgdgg tddgd	dgg}td  |t	d| j  j| t d |t	| j d j| td  |t	d| j  j | t d |t	| j d j | t   |t	| j | j  j| t d   |t	| j d| j   j | t  |t	| j  j| t   |t	| j | j  j| td  d| t	d| j  j d|     }tt|jdko|jd  u o|jd  u  d  }tt|jdko|jd  u o|jd dk tt g dtj tt tdgdgd	ggtj tt tg d tj tt tdgdgd	gg tj tt 	tg dtj tt 	tdgdgd	ggtj tt t
dgdgd	ggtj tt t
dgdgd	gg tj tt 	t
dgdgd	ggtj ttd  tj ttd  tj tt   tj tt  tj tt   tj tt d tj tt d tj t d	 d	 jd  u   tg d } d	  d }t tg d | td  jtjk d}tt|d  tddg  W d    n	1 sw   Y  tt jtddg tt jtg d tt jtdgdgg tt jtdgdgd	gdgg tt fdd tt fdd q	t|| j || jD ]\}}	tjdi | tjdi |	| j 	| j}
tddgd	dgg}t  |t	|
j| td   |t	d|
 j | t  ddg ddg t  dgdgg dgdgg t  dgdggdgdgg t  ddgddg t  jddgddg tt  tj tt fd d tt fd!d   }tt|jdko$|jd  u o$|jd u  qU|| jD ]}tjdi |}tddgd	dgg}t||t	| jj| t|d |t	t	| j| jj| t|d ddg d"d#g t|d ddgd$dg t|d jddgd$dg t|d dgdggd"gd#gg tt|d tj q-d S )%Nc                    sP    j  fdd fddd j  fdd fdd fdd fddd	gS )
Nc                    s   t  |  jd S Nr   )r   dotreshapeshapexr   r   r   <lambda>   s    zETestLinearOperator.test_matvec.<locals>.get_matvecs.<locals>.<lambda>c                    s   t  j |  jd S Nr   )r   r   Tconjr   r    r!   r#   r   r   r$       s    )r    matvecrmatvecc                       t  | S Nr   r   r!   r#   r   r   r$   %       c                       t  j | S r+   r   r   r&   r'   r!   r#   r   r   r$   &       c                    r.   r+   r/   r!   r#   r   r   r$   '   r0   c                    r*   r+   r,   r!   r#   r   r   r$   (   r-   r    r(   r)   rmatmatmatmat)r    r#   r   r#   r   get_matvecs   s   





z3TestLinearOperator.test_matvec.<locals>.get_matvecsr   r          r   r   r   )r   r   r         )
   r5                     @y              (@y              >@iir   r   r   z.Can only divide a linear operator by a scalar.matchr   r   r   r   c                      s      S r+   r   r   r#   r   r   r$          z0TestLinearOperator.test_matvec.<locals>.<lambda>c                          d S Nr   r   r   r#   r   r   r$      r?   2   q   G   \   c                      s     S r+   r   r   r   r   r   r   r$      r?   c                      r@   rA   r   r   r#   r   r   r$      r?      %      )r   	interfaceLinearOperatorr   argsr   r(   r   r   r   r   r)   Hr3   r2   r&   r'   len
isinstanceZndarrayZ_ScaledLinearOperatorZ_SumLinearOperatordtypecomplex_assert_raises
ValueErrorr   r   Z_ProductLinearOperatorr   Z_PowerLinearOperator)r   r4   ZmatvecsXzresultr   msgZmatvecsAZmatvecsBZAtimesBZmatvecsCr   rF   r   test_matvec   s   *(*(&(&(&&($&$$ 22(& (&$&$
"$6$zTestLinearOperator.test_matvecc                    s<   j j fdd fdd fdd fddd}tjdi |}tg dg dg d	g}|d
 }tt||||  tt||	dd||	dd  tt||||  tt||j
||j
  tt|	dd|j
|	dd|j
  tt||j
||j
  tttj|d tttjd| d S )Nc                    s   t  j|  jjd S r   )r   r   r   r   r    r!   r   r   r   r$      s    z0TestLinearOperator.test_matmul.<locals>.<lambda>c                    s"   t  jj |  jjd S r%   )r   r   r   r&   r'   r   r    r!   r   r   r   r$      s    c                    s   t  jj | S r+   )r   r   r   r&   r'   r!   r   r   r   r$      s    c                    s   t  j| S r+   )r   r   r   r!   r   r   r   r$      s    r1   )y      ?      ?r   r   r   )      	   r   r   r   r   )r   r    rJ   rK   r   r   r   operatormatmulr   rM   rR   rS   )r   Dr   r   br   r   r   test_matmul   s(   




&*zTestLinearOperator.test_matmulN)__name__
__module____qualname__r   rX   ra   r   r   r   r   r
      s
    	 r
   c                   @   r	   )TestAsLinearOperatorc                 C   s  g | _ dd }tg dg dg}|  j ||tj7  _ |  j ||tj7  _ |  j ||tj7  _ |  j dd ||jtjD 7  _ |  j dd ||jtjD 7  _ tg dg d	g}|  j ||tj7  _ |  j d
d ||jtjD 7  _ |  j dd ||jtjD 7  _ d S )Nc                    s   g }| t|df | tj|df | tj|df fdd fddG  fdddtj}G fdd	d	|}G  fd
dd|}G fddd|}| ||f | ||f | ||f |S )NrP   c                    s(     | }t| jdkr|dd}|S )Nr   r\   r   )r   rN   r    r   )r"   rP   yoriginalr   r   mv   s   
zATestAsLinearOperator.setup_method.<locals>.make_cases.<locals>.mvc                    s    j  | S r+   r&   r'   r   )r"   rP   rh   r   r   rmv      zBTestAsLinearOperator.setup_method.<locals>.make_cases.<locals>.rmvc                       s(   e Zd ZdZfddZ fddZdS )zJTestAsLinearOperator.setup_method.<locals>.make_cases.<locals>.BaseMatliker   c                    s   t || _ j| _d S r+   )r   rP   r    )r   rP   rh   r   r   __init__   s   zSTestAsLinearOperator.setup_method.<locals>.make_cases.<locals>.BaseMatlike.__init__c                        || j S r+   rf   r   r"   )rj   r   r   _matvec      zRTestAsLinearOperator.setup_method.<locals>.make_cases.<locals>.BaseMatlike._matvecN)rb   rc   rd   rL   rn   rq   r   )rj   ri   r   r   BaseMatlike   s    rs   c                       s   e Zd ZdZ fddZdS )zITestAsLinearOperator.setup_method.<locals>.make_cases.<locals>.HasRmatvecr   c                    ro   r+   rf   rp   rl   r   r   _rmatvec   rr   zRTestAsLinearOperator.setup_method.<locals>.make_cases.<locals>.HasRmatvec._rmatvecN)rb   rc   rd   rL   ru   r   rt   r   r   
HasRmatvec   s    rv   c                       s   e Zd ZdZ fddZdS )zITestAsLinearOperator.setup_method.<locals>.make_cases.<locals>.HasAdjointr   c                    sD   | j d | j d f}t| jd}t | jd}tj||| j|dS )Nr   r   rf   )r(   r)   rP   r    )r    r   rP   rJ   rK   )r   r    r(   r)   rj   rl   r   r   _adjoint   s   zRTestAsLinearOperator.setup_method.<locals>.make_cases.<locals>.HasAdjoint._adjointN)rb   rc   rd   rL   rx   r   rw   r   r   
HasAdjoint   s    ry   c                       s$   e Zd Z fddZ fddZdS )zITestAsLinearOperator.setup_method.<locals>.make_cases.<locals>.HasRmatmatc                    s
     |S r+   )r   rp   rh   r   r   _matmat  s   
zQTestAsLinearOperator.setup_method.<locals>.make_cases.<locals>.HasRmatmat._matmatc                    s    j  |S r+   rk   rp   rh   r   r   _rmatmat  rm   zRTestAsLinearOperator.setup_method.<locals>.make_cases.<locals>.HasRmatmat._rmatmatN)rb   rc   rd   rz   r{   r   rh   r   r   
HasRmatmat      r|   )appendr   r   r   sparseZ
csr_matrixrJ   rK   )ri   rP   casesrs   rv   ry   r|   r   )rj   ri   rl   r   
make_cases   s   
z5TestAsLinearOperator.setup_method.<locals>.make_casesr   r   c                 S   "   g | ]\}}t |j|jfqS r   rJ   aslinearoperatorr&   .0Mr   r   r   r   
<listcomp>      z5TestAsLinearOperator.setup_method.<locals>.<listcomp>c                 S   &   g | ]\}}t |j|j fqS r   rJ   r   rM   r&   r'   r   r   r   r   r         r   r;                 @)y              @y              @r   c                 S   r   r   r   r   r   r   r   r     r   c                 S   r   r   r   r   r   r   r   r     r   )r   r   r   Zint32Zfloat32float64r&   rQ   )r   r   ri   r   r   r   r      s(   :


z!TestAsLinearOperator.setup_methodc           
      C   s0  | j D ]\}}t|}|j\}}tg dtdgdgdggg}tddgtdgdggg}|jtjkr`|tg dtdgdgdggg7 }|tddgtdgdggg7 }tddgdd	gdd
gg}|D ]}t|	||
| t|| |
| qpt|||
| t|| |
| |D ],}	t||	|j 
|	 t|j	|	|j
|	 t|j	|	|j 
|	 q|D ]2}	|	jdk rqt||	|j 
|	 t|j|	|j
|	 t|j|	|j 
|	 qt|drt|j|j tt|d qd S )Nr   r   r   r   r   r;   r   r   r   r   rP   rL   )r   rJ   r   r    r   r   rP   rQ   r   r(   r   r3   r)   r&   r'   rM   ndimr2   hasattrr   )
r   r   A_arrayr   NZxsZysx2r"   rg   r   r   r   
test_basic  s@   

 $ 
 zTestAsLinearOperator.test_basicc                 C   s   | j D ]O\}}t|}|j\}}tg d}tdgdgdgg}tddgddgddgg}t|||| t|||| t|||| qd S )Nr   r   r   r   r   r   r   )r   rJ   r   r    r   r   r   r   )r   r   r   r   r   Zx0x1r   r   r   r   test_dotF  s   

zTestAsLinearOperator.test_dotN)rb   rc   rd   r   r   r   r   r   r   r   re      s    M)re   c                  C   s,   t jddd d} t| }td|v| d S )N)r   r   c                 S   s   dS r%   r   r!   r   r   r   r$   V      ztest_repr.<locals>.<lambda>r    r(   zunspecified dtype)rJ   rK   reprr   )r   Zrepr_Ar   r   r   	test_reprU  s   r   c                  C   s^   t d} t| g d g d t| tddd td tt	| j
g d d S )Nr   r   r   r[   r   r>   )rJ   ZIdentityOperatorr   r   r   aranger   ZravelrR   rS   r(   )identr   r   r   test_identity[  s   
(r   c                  C   s   t tddd} dd }t jd|d}| || | | j| |  || | d fD ]}tt|d tt|d tt|d	 q)d S )
N   r   c                 S   s,   t | } t| jdkp| jdk t dS )N)r   )r   r   r   )r   Zasarrayr   r    Zonesr!   r   r   r   always_four_onesf  s   

z)test_attributes.<locals>.always_four_ones)r   r   r   rP   r    rq   )	rJ   r   r   r   r   rK   rM   r   r   )r   r   r   opr   r   r   test_attributesc  s   (r   c                 C   s
   t dS )z> Needed for test_pickle as local functions are not pickleable r   )r   Zzerosr!   r   r   r   r(   r  s   
r(   c                  C   sf   dd l } t| jd D ]%}tdt}| j||d}| |}|jD ]}t	t
||t
|| q"qd S )Nr   r   r   )protocol)picklerangeHIGHEST_PROTOCOLrJ   rK   r(   dumpsloads__dict__r   getattr)r   r   r   sr   kr   r   r   test_picklev  s   

r   c                  C   s   G dd dt j} ttdd tt|  W d    n1 sw   Y  G dd dt j}|d}t|g dg d tt|j	g d	 G d
d dt j}|t
jdd}t|t
jdjd d S )Nc                   @   s   e Zd ZdS )ztest_inheritance.<locals>.EmptyN)rb   rc   rd   r   r   r   r   Empty  s    r   zshould implement at leastr<   c                       $   e Zd Z fddZdd Z  ZS )z"test_inheritance.<locals>.Identityc                    s   t  jd ||fd d S )N)rP   r    )superrn   )r   n	__class__r   r   rn     s   z+test_inheritance.<locals>.Identity.__init__c                 S   s   |S r+   r   rp   r   r   r   rq     s   z*test_inheritance.<locals>.Identity._matvec)rb   rc   rd   rn   rq   __classcell__r   r   r   r   Identity  r}   r   r   r   r   c                       r   )z$test_inheritance.<locals>.MatmatOnlyc                    s   t  |j|j || _d S r+   )r   rn   rP   r    r   )r   r   r   r   r   rn     s   
z-test_inheritance.<locals>.MatmatOnly.__init__c                 S   s   | j |S r+   )r   r   rp   r   r   r   rz     rr   z,test_inheritance.<locals>.MatmatOnly._matmat)rb   rc   rd   rn   rz   r   r   r   r   r   
MatmatOnly  s    r   r   )r   )rJ   rK   r   RuntimeWarningrR   	TypeErrorr   r(   NotImplementedErrorr)   r   randomZrandnr    )r   r   Zid3r   mmr   r   r   test_inheritance  s   r   c                  C   sr   t jdddt jdd  } t jdd}t| }t|}|| }|| }t|jt j t|jt j d S )Nr                 ?)	r   r   ZrandrJ   r   r   rP   r   Z
complex128)Zmat_complexZmat_realZcomplex_operatorZreal_operatorZsum_complexZsum_realr   r   r   test_dtypes_of_operator_sum  s    

r   c                     s2   dg  fdd} t jd| d t d d d S )Nr   c                    s    d  d7  < | S )Nr   r   r   )vZ
call_countr   r   r(     s   z#test_no_double_init.<locals>.matvecr   r   r(   r   )rJ   rK   r   r   r   r   r   test_no_double_init  s   r   c                  C   sl   t dgg} t| }d| }d|  }t dg}t|||| t|j||j | d S Nr   r   )	r   r   rJ   r   r   r   rM   r&   r'   rT   r   r   Yr   r   r   r   test_adjoint_conjugate  s   
"r   c                  C   s(   t dgg} t| }t|jd d S )Nr   r   )r   r   rJ   r   r   r   )rT   r   r   r   r   	test_ndim  s   
r   c                  C   sh   t dgg} t| }d| }d|  }t dg}t|||| t|j||j| d S r   )r   r   rJ   r   r   r   r&   r   r   r   r   test_transpose_noconjugate  s   
r   c                  C   s   t jddd d} td}d}tt|d | |  W d    n1 s%w   Y  tt|d ||   W d    n1 s?w   Y  tt | td  W d    n1 sZw   Y  tt td|   W d    d S 1 svw   Y  d S )	Nr   c                 S   s   | S r+   r   r!   r   r   r   r$     r   z.test_sparse_matmat_exception.<locals>.<lambda>r   r   z9Unable to multiply a LinearOperator with a sparse matrix.r<   r   )rJ   rK   r   identityrR   r   rS   r   )r   r   rW   r   r   r   test_sparse_matmat_exception  s   




"r   )%__doc__	functoolsr   	itertoolsr   r]   Zpytestr   rR   r   Znumpy.testingr   r   numpyr   Zscipy.sparser   Zscipy.sparse.linalg._interfaceZlinalgZ
_interfacerJ   Zscipy.sparse._sputilsr   r
   re   r   r   r   r(   r   r   r   r   r   r   r   r   r   r   r   r   <module>   s4     ? 