o
    \i^R                     @   s  d Z ddlmZ ddlmZ ddl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 ddlmZ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 ej!j"dd Z#dd Z$dd Z%ej&ej'ej(ej)fZ*ej+ej,ej-fZ.ej/ej0ej1fZ2e.e2 Z3e*e3 Z4ej!5de4dd Z6d d! Z7d"d# Z8d$d% Z9d&d' Z:d(d) Z;d*d+ Z<ej!j=ed,d-d.d/ Z>dS )0z7Test functions for the sparse.linalg._interface module
    )partial)productN)raiseswarns)assert_assert_equal)matrix)assert_deallocatedIS_PYPYc                   @   $   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   /var/www/www-root/data/www/176.119.141.140/sports-predictor/venv/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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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 sw   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   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oT|jd  u oT|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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                    r0   r-   r1   r#   r%   r   r   r&   )   r2   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         )
   r7                     @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&      rA   2   q   G   \   c                      s     S r-   r   r   r   r   r   r   r&      rA   c                      rB   rC   r   r   r%   r   r   r&      rA      %      ) r   	interfaceLinearOperatorr   argsr   r*   r   r   r    r   r+   Hadjointr5   r4   r(   r)   len
isinstancendarray_ScaledLinearOperator_SumLinearOperatordtype
complex128assert_raises
ValueErrorr   r   _ProductLinearOperatorr   _PowerLinearOperator)r   r6   matvecsXzresultr   msgmatvecsAmatvecsBAtimesBmatvecsCr   rH   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|| ||   tt|	dd|j
|	dd|j
  tt|	dd| |	dd|   tt||j
||j
  tt|| ||   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    r3   )y      ?      ?r   r   r   )      	   r   r   r   r   )r   r"   rL   rM   r   r   r   operatormatmulr!   rO   rP   rX   rY   )r   Dr   r   br   r   r   test_matmul   s2   




&*zTestLinearOperator.test_matmulN)__name__
__module____qualname__r   re   rn   r   r   r   r   r      s
    	 "r   c                   @   r   )TestAsLinearOperatorc                 C   sb  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  _ |  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  _ |  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rV   c                    s(     | }t| jdkr|dd}|S )Nr   ri   r   )r    rQ   r"   r!   )r$   rV   yoriginalr   r   mv   s   
zATestAsLinearOperator.setup_method.<locals>.make_cases.<locals>.mvc                    s    j  | S r-   r(   r)   r    )r$   rV   ru   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   rV   r"   )r   rV   ru   r   r   __init__   s   zSTestAsLinearOperator.setup_method.<locals>.make_cases.<locals>.BaseMatlike.__init__c                        || j S r-   rs   r   r$   )rw   r   r   _matvec      zRTestAsLinearOperator.setup_method.<locals>.make_cases.<locals>.BaseMatlike._matvecN)ro   rp   rq   rN   r{   r~   r   )rw   rv   r   r   BaseMatlike   s    r   c                       s   e Zd ZdZ fddZdS )zITestAsLinearOperator.setup_method.<locals>.make_cases.<locals>.HasRmatvecr   c                    r|   r-   rs   r}   ry   r   r   _rmatvec   r   zRTestAsLinearOperator.setup_method.<locals>.make_cases.<locals>.HasRmatvec._rmatvecN)ro   rp   rq   rN   r   r   r   r   r   
HasRmatvec   s    r   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   rs   )r*   r+   rV   r"   )r"   r   rV   rL   rM   )r   r"   r*   r+   rw   ry   r   r   _adjoint  s   zRTestAsLinearOperator.setup_method.<locals>.make_cases.<locals>.HasAdjoint._adjointN)ro   rp   rq   rN   r   r   r   r   r   
HasAdjoint   s    r   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    r}   ru   r   r   _matmat  s   
zQTestAsLinearOperator.setup_method.<locals>.make_cases.<locals>.HasRmatmat._matmatc                    s    j  |S r-   rx   r}   ru   r   r   _rmatmat  rz   zRTestAsLinearOperator.setup_method.<locals>.make_cases.<locals>.HasRmatmat._rmatmatN)ro   rp   rq   r   r   r   ru   r   r   
HasRmatmat      r   )appendr   r   r   sparse	csr_arrayrL   rM   )rv   rV   casesr   r   r   r   r   )rw   rv   ry   r   
make_cases   s   
z5TestAsLinearOperator.setup_method.<locals>.make_casesr   r   c                 S   "   g | ]\}}t |j|jfqS r   rL   aslinearoperatorr(   .0Mr   r   r   r   
<listcomp>      z5TestAsLinearOperator.setup_method.<locals>.<listcomp>c                 S   &   g | ]\}}t |j|j fqS r   rL   r   rO   r(   r)   r   r   r   r   r         c                 S   (   g | ]\}}t | |j fqS r   rL   r   rP   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   c                 S   r   r   r   r   r   r   r   r   (  r   )r   r   r   int32float32float64r(   rW   )r   r   rv   r   r   r   r      s4   :




z!TestAsLinearOperator.setup_methodc           
      C   sp  | j D ]1\}}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 
|	 t| 	|	|j 
|	 q|D ]B}	|	jdk rqt||	|j 
|	 t|j|	|j
|	 t|j|	|j 
|	 t| |	|j 
|	 qt|dr.t|j|j tt|d qd S )Nr   r   r   r   r   r=   r   r   r   r   rV   rN   )r   rL   r   r"   r   r   rV   rW   r   r*   r    r5   r+   r(   r)   rO   rP   ndimr4   hasattrr   )
r   r   A_arrayr   Nxsysx2r$   rt   r   r   r   
test_basic+  sD   

 $"
"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   rL   r   r"   r   r   r   r    )r   r   r   r   r   x0x1r   r   r   r   test_dotV  s   

zTestAsLinearOperator.test_dotN)ro   rp   rq   r   r   r   r   r   r   r   rr      s    Q+rr   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&   f      ztest_repr.<locals>.<lambda>r"   r*   zunspecified dtype)rL   rM   reprr   )r   repr_Ar   r   r   	test_repre  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   rh   r   r@   )rL   IdentityOperatorr   r    r   aranger!   ravelrX   rY   r*   )identr   r   r   test_identityk  s   
(r   c               
   C   s   t tddd} dd }t jd|d}| || | | | | j|  | |  || | d g	}|D ]}tt	|d tt	|d tt	|d	 q1d S )
N   r   c                 S   s,   t | } t| jdkp| jdk t dS )N)r   )r   r   r   )r   asarrayr   r"   onesr#   r   r   r   always_four_onesv  s   

z)test_attributes.<locals>.always_four_ones)r   r   r   rV   r"   r~   )
rL   r   r   r   r!   rM   rO   rP   r   r   )r   r   r   opsopr   r   r   test_attributess  s   0r   c                 C   s
   t dS )z> Needed for test_pickle as local functions are not pickleable r   )r   zerosr#   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_PROTOCOLrL   rM   r*   dumpsloads__dict__r   getattr)r   r   r   sr   kr   r   r   test_pickle  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)ro   rp   rq   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)rV   r"   )superr{   )r   n	__class__r   r   r{     s   z+test_inheritance.<locals>.Identity.__init__c                 S   s   |S r-   r   r}   r   r   r   r~     s   z*test_inheritance.<locals>.Identity._matvec)ro   rp   rq   r{   r~   __classcell__r   r   r   r   Identity  r   r   r   r   r   c                       r   )z$test_inheritance.<locals>.MatmatOnlyc                       t  |j|j || _d S r-   r   r{   rV   r"   r   r   r   r   r   r   r{        
z-test_inheritance.<locals>.MatmatOnly.__init__c                 S   s   | j |S r-   )r   r    r}   r   r   r   r     r   z,test_inheritance.<locals>.MatmatOnly._matmat)ro   rp   rq   r{   r   r   r   r   r   r   
MatmatOnly  s    r   r   )r   )rL   rM   r   RuntimeWarningrX   	TypeErrorr   r*   NotImplementedErrorr+   r   randomrandnr"   )r   r   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   randrL   r   r   rV   r   rW   )mat_complexmat_realcomplex_operatorreal_operatorsum_complex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
call_countr   r   r*     s   z#test_no_double_init.<locals>.matvecr   r   r*   r   )rL   rM   r   r   r   r   r   test_no_double_init  s   r   
test_dtypec                    s@   t jd| d  fdd}tjd|d}|jt | ksJ d S )Nr   rs   c                    s   t  | d  | d gS r   r   r   r   scalarr   r   rw        z/test_determine_lo_dtype_from_matvec.<locals>.mvr   r   )r   r   rL   rM   rV   )r   rw   lor   r   r   #test_determine_lo_dtype_from_matvec  s   r   c                  C   s(   dd } t jd| d}|jtv sJ d S )Nc                 S   s   t d| d  | d gS )N   r   r   r   r   r   r   r   rw     r   z+test_determine_lo_dtype_for_int.<locals>.mvr   r   )rL   rM   rV   
INT_DTYPES)rw   r   r   r   r   test_determine_lo_dtype_for_int  s   r   c                  C   s   t dgg} t| }d| }d|  }t dg}t|||| t|j||j | t|	 ||j | d S Nr   r   )
r   r   rL   r   r   r    rO   r(   r)   rP   r]   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   rL   r   r   r   )r]   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   rL   r   r   r    r(   r   r   r   r   test_transpose_noconjugate  s   
r  c                  C   s0  G dd dt j} | tddgddgg}tddg}tddgd	d
gg}|dd}tddgddgg}t|| ||  t|j| |j|  t|j| |j|  t|| ||  t|j| |j|  t|j| |j|  t|| ||  t|j| |j|  t|j| |j|  d S )Nc                       s,   e Zd Z fddZdd Zdd Z  ZS )z/test_transpose_multiplication.<locals>.MyMatrixc                    r   r-   r   r   r   r   r   r{     r   z8test_transpose_multiplication.<locals>.MyMatrix.__init__c                 S   s
   | j | S r-   r%   r   otherr   r   r   r   	  s   
 z7test_transpose_multiplication.<locals>.MyMatrix._matmatc                 S   s   | j j| S r-   )r   r(   r  r   r   r   r   
  r/   z8test_transpose_multiplication.<locals>.MyMatrix._rmatmat)ro   rp   rq   r{   r   r   r   r   r   r   r   MyMatrix  s    r  r   r   r   r   r;      r:   (   ri   )rL   rM   r   r   r!   r   r(   )r  r   r]   r   X2r   r   r   r   test_transpose_multiplication  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   )	rL   rM   r   	eye_arrayrX   r   rY   r   identity)r   r   r`   r   r   r   test_sparse_matmat_exception  s   




"r  zTest not meaningful on PyPy)reasonc                  C   sF   t d} ttj| }|  ~W d    d S 1 sw   Y  d S rC   )r   eyer	   rL   MatrixLinearOperatorrP   )r   r   r   r   r   "test_MatrixLinearOperator_refcycle*  s
   
"r  )?__doc__	functoolsr   	itertoolsr   rj   pytestr   rX   r   numpy.testingr   r   numpyr   scipy.sparser   scipy.sparse.linalg._interfacelinalg
_interfacerL   scipy.sparse._sputilsr   scipy._lib._gcutilsr	   r
   r   rr   r   r   r   r*   r   markthread_unsafer   r   r   int8int16r   int64r   r   r   
longdoubleREAL_DTYPES	complex64rW   clongdoubleCOMPLEX_DTYPESINEXACTDTYPES	ALLDTYPESparametrizer   r   r   r   r  r  r  skipifr  r   r   r   r   <module>   sP     G 

		