
    >qj              
          d Z ddlZddlmZ ddlmZ ddl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mZmZmZmZmZmZmZmZmZ dd
lm Z! ddl"m#Z# ddl$m#c m%c m&Z' ddl(m)Z)m*Z* ddl+m,Z, ddl-m.Z.  ee)dfe)dfe)dfe)dfe)dfe)dfe)dfe*      Z/ddddddZ0 G d d      Z1ejd                  jg                  dd       G d d             Z4 G d  d!      Z5d" Z6d# Z7d$ Z8d% Z9ejd                  jg                  d&d'      d(        Z:d) Z;d* Z<d+ Z=ej|                  ej~                  ej                  ej                  fZBej                  ej                  ej                  fZFej                  ej                  ej                  fZJeFeJz   ZKeBeKz   ZLejd                  j                  d,eL      d-        ZNd. ZOd/ ZPd0 ZQd1 ZRejd                  jg                  dd      ejd                  jg                  d&d2      d3               ZSejd                  jg                  d45      d6        ZTejd                  jg                  d7d8      d9        ZUejd                  j                  d:d;d4g      ejd                  j                  d<g d=      ejd                  j                  d>g d?      ejd                  j                  d@g dA      ejd                  j                  dBej                  ej                  g      ejd                  jg                  dd      ejd                  jg                  d&d2      dC                                                  ZVy)Dz7Test functions for the sparse.linalg._interface module
    N)partial)product)
ModuleType)
NamedTuple)raiseswarns)assert_assert_equal)
SCIPY_ARRAY_APISCIPY_DEVICEis_torchxp_assert_closeis_lazy_arrayxp_assert_equalxp_ravelis_numpymake_xp_pytest_marksis_jax_array)array_api_extra)LinearOperatoraslinearoperator)matrix)assert_deallocated__init__matvecmatmatdotrdotrmatvecrmatmat   
   ndimminmaxrngc                   t         j                  j                        t        j                  ||      }t        j                  ||      }t	        fdt        ||      D              }t        j                  |g| d      }|j                  D ]  }d|j                  d|       d   t        j                  |d            }t        t        |            D ]n  }||   }	t        j                  |	dk7        rt        j                  |	dk7        d   d   n|}
j                         dk  sT|	j                  |
dz         d  ||<    n t        j                  | |k(  sJ |D 	cg c]  }	t	        d |	D               c}	S c c}	w )Nc              3   N   K   | ]  \  }}j                  ||d z           yw)   N)integers).0min_max_r'   s      g/opt/rentech/trading_bot/.venv/lib/python3.12/site-packages/scipy/sparse/linalg/tests/test_interface.py	<genexpr>z0generate_broadcastable_shapes.<locals>.<genexpr>+   s$     SztTT462Ss   "%r   axisr*   g      ?c              3   2   K   | ]  }t        |        y wN)int)r,   els     r/   r0   z0generate_broadcastable_shapes.<locals>.<genexpr><   s     *b#b'*s   )nprandomdefault_rngbroadcast_totupleziprepeatTr+   listpermutedrangelenanywherebroadcast_shapes)nshapesr$   r%   r&   r'   batch_shapeshapescolumnishapejs       `      r/   generate_broadcastable_shapesrM   '   s^   
))


$C
//#t
$C
//#t
$CSSc]SSKYY}gA6F (( .,-s||Aw'(). #,,vA,./F3v; q	*,&&!*<BHHUaZ #A&$::<$cll1Q3/01F1I ';6668>?uE*E**???s   &Fc                   d    e Zd Zd Zd Zej                  j                  dd      d        Zd Z	y)	TestLinearOperatorc                     t        j                  g dg dg      | _        t        j                  ddgddgddgg      | _        t        j                  ddgddgg      | _        y )	Nr*   r!               r*   r!   rR   rT   rU   rV   )r7   arrayABC)selfs    r/   setup_methodzTestLinearOperator.setup_method@   si    7"$ %Aa5a5a5" # Aa5a5" #    c                 "   fd}t        j                  j                        } | || j                              D ]  }t	        j
                  di |dij                  dk(  sJ t        j                   |g d             |ddg             t        j                  t        d	      5  t        j                   |d
gdgdgg             |dgdgg             d d d        t         |g d      z   |ddg             t         |d
gdgdgg      z   |dgdgg             t        j                   |g d             |ddg             t        j                   |d
gdgdgg             |dgdgg             t        st        j                  t        d
gdgdgg             |dgdgg             t        t        d
gdgdgg      z   |dgdgg             t        j                  t        d
gdgdgg             |dgdgg             t        dz   |g d      z   |ddg             t        dz  j                   |d
d
g             |g d             t        dz  j                   j                   |d
d
g             |g d             t        dz  j#                         j                   |d
d
g             |g d             t        dz   |d
gd
gd
gg      z   |dgdgg             t        dz  j%                   |d
gd
gd
gg             |dgdgg             t        dz   |g d      z   |ddg             t        dz   |d
gd
gd
gg      z   |dgdgg             t        dz   |g d      z   |ddg             t        z    |g d      z   |ddg             t        z   j                   |d
d
g             |g d             t        z   j                   j                   |d
d
g             |g d             t        z   j#                         j                   |d
d
g             |g d             t        z    |d
gd
gd
gg      z   |dgdgg             t        z   j%                   |d
gd
gd
gg             |dgdgg             t          |g d      z   |ddg             t          |d
gd
gd
gg      z   |dgdgg             t        z
   |g d      z   |ddg             t        z
   |d
gd
gd
gg      z   |dgdgg             j                  | j                  j                        }j                  d
dgddggj                        }t        dz  j'                  |      d|z  j(                  |z         t        dz  j'                  |      |dz  j(                  |z         t        dz  j'                  |      j+                  d|z  j(                        |z         t        dz  j'                  |      j+                  |dz  j(                        |z         t        z   j'                  |      ||z   j(                  |z         t        dz  z   j'                  |      j+                  |d|z  z   j(                        |z         t         j'                  |      | j(                  |z         t        z
  j'                  |      ||z
  j(                  |z         t        dz  j'                  d|z        j+                  d|z  j(                        d|z  z         z   }t-        |j                        dk(  r"|j                  d   u r|j                  d
   u sJ dz  }t-        |j                        dk(  sJ t/        j1                  d            s%|j                  d   u r|j                  d
   dk(  sJ t3        j1                  d            }t5        j                   |g d            |      sJ t7        j1                  d            st        j                  t        d	      nt9        j:                         }	|	5  t5        j                   |d
gdgdgg            |      sJ 	 d d d        t5         |g d      z  |      sJ t5         |d
gdgdgg      z  |      sJ t5        j                   |g d            |      sJ t5        j                   |d
gdgdgg            |      sJ t        sxt5        j                  t        d
gdgdgg            |      sJ t5        t        d
gdgdgg      z  |      sJ t5        j                  t        d
gdgdgg            |      sJ t5        dz  t        j<                        sJ t5        dz  t        j<                        sJ t5        z   t        j>                        sJ t5         t        j<                        sJ t5        z
  t        j>                        sJ t5        dz  t        j<                        sJ t5        dz  t        j<                        sJ t/        j1                  d            sdz  dz  j                  d   u sJ  |g d      z  }
dz  dz  }t         |g d      z  |
       dz  j@                  j                  k(  sJ d}tC        tD        |	      5  j                  d
dg      z  }d d d        tC        tD        j                  j                  d
dg             tC        tD        j                  j                  g d             tC        tD        j                  j                  d
gdgg             tC        tD        j                  j                  d
gdgdgdgg             tC        tD        fd       tC        tD        fd        tG         | || j                               | || jH                                    D ]  \  }}t	        j
                  di |dit	        j
                  di |di || j                  | jH                  z        } |d
dgddgg      }t        z  j'                  |      |j(                  |z         t        dz  z  j'                  |      j+                  d|z  j(                        |z         t        z   |d
d
g      z   |ddg             t        z   |d
gd
gg      z   |dgdgg             t        z  j%                   |d
gd
gg             |dgdgg             t        z  j                   |d
d
g             |dd g             t        z  j                   j                   |d
d
g             |dd g             t        z  j#                         j                   |d
d
g             |dd g             t5        z  t        jJ                        sJ tC        tD        fd!       tC        tD        fd"       z  }t-        |j                        dk(  sJ t/        j1                  d            rn|j                  d   u r|j                  d
   u rJ   | || jL                              D ]  }t	        j
                  di |di} |d
dgddgg      } || jL                        }t        |j'                  |      |j(                  |z         t        |dz  j'                  |      ||z  j(                  |z         t        |dz   |d
d
g      z   |d#d$g             t        |dz  j                   |d
d
g             |d%dg             t        |dz  j                   j                   |d
d
g             |d%dg             t        |dz  j#                         j                   |d
d
g             |d%dg             t        |dz  j%                   |d
gd
gg             |d#gd$gg             t5        |dz  t        jN                        rJ  y # 1 sw Y    xY w# 1 sw Y   xY w# 1 sw Y   xY w)&Nc                 ^      j                   j                   fd fd fd fddgS )Nc                     | z  S r4    xrX   s    r/   <lambda>zETestLinearOperator.test_matvec.<locals>.get_matvecs.<locals>.<lambda>N       AE r]   c                 @    j                  j                        | z  S r4   conjr>   rc   rX   xps    r/   rd   zETestLinearOperator.test_matvec.<locals>.get_matvecs.<locals>.<lambda>O       RWWQSS\A%5 r]   c                 @    j                  j                        | z  S r4   rg   ri   s    r/   rd   zETestLinearOperator.test_matvec.<locals>.get_matvecs.<locals>.<lambda>P   rk   r]   c                     | z  S r4   ra   rb   s    r/   rd   zETestLinearOperator.test_matvec.<locals>.get_matvecs.<locals>.<lambda>Q   re   r]   rK   dtyper   r   r    r   )rK   float64)rX   rj   s   `r/   get_matvecsz3TestLinearOperator.test_matvec.<locals>.get_matvecsJ   s,    )55)  r]   ro   rj   ra   rQ          column vectorsmatchr*   r!   rR   )r*   r*   r*         )r"   rs                     @y              (@y              >@iir   rT   rU   z.Can only divide a linear operator by a scalar.r*   r!   rR   rT   c                        z  S r4   ra   rX   s   r/   rd   z0TestLinearOperator.test_matvec.<locals>.<lambda>       ac r]   c                       dz  S Nr!   ra   r~   s   r/   rd   z0TestLinearOperator.test_matvec.<locals>.<lambda>       ad r]   2   q   G   \   c                       z   S r4   ra   )rX   rY   s   r/   rd   z0TestLinearOperator.test_matvec.<locals>.<lambda>   r   r]   c                       dz  S r   ra   r~   s   r/   rd   z0TestLinearOperator.test_matvec.<locals>.<lambda>   r   r]      %      )(r   asarray
complex128rX   	interfacer   argsr   r   pytestr   FutureWarningr   r   r   r   Hadjointr   r    r>   rh   rB   r   emptytype
isinstancer   
contextlibnullcontext_ScaledLinearOperator_SumLinearOperatorro   assert_raises
ValueErrorr   rY   _ProductLinearOperatorrZ   _PowerLinearOperator)r[   rj   rq   _asarraymatvecsA_Xzarray_objectctxresultrZ   msg_matvecsAmatvecsBAtimesBmatvecsCC_rX   rY   s    `                 @@r/   test_matveczTestLinearOperator.test_matvecI   sd   	 2::R]];"8DFF#34 	4G((:7:r:A66R<<AHHXg%672b'9JKm3CD HHXsA3sm45rdB4[)
 A 118RG3DEA1#qc1# 77B4+9NOAEE(7"34h2w6GHAEE(QCQC="9:HrdB4[<QR"HHVaS!aSM23rdB4[)  FQCQC=$9 98bT2$K;PQfqc1#qc]&; <hbT{>STQqSHW$55xB7HIQUOOHaV,<=x?UVQqSGGNN8QqE?;Xl=ST1&&x17& QqSHqc1#qc]$;;XtRDk=RSQx!qcA3892$& QqSHW$55xB7HIQqSHqc1#qc]$;;XtRDk=RSRTXg%66#c8KLQqSHW$55xR7IJQUOOHaV,<=x?UVQqSGGNN8QqE?;Xl=STQ!((1Q%9& QqSHqc1#qc]$;;XtbTl=ST1XsA3sm452$& aR8G#44h3x6HIaR8aS!aSM#::HrdC5\<RSQqSHW$55x1GQUhaS1#&??A3QRPS*AUVDFF"--8B

QFQF+2==
AAQUOOA.R

Q?QUOOA.a

Q?R!V,,Q/"r'1E1IJQV,,Q/"r'1E1IJQUOOA.ba@QaZ003RWWb27l=M=M5NQR5RSaRLLOrcWWq[9QUOOA.ba@R!V,,R!V4bggrBwkk6JbSTf6UV!Aqvv;!#q	Q166!9>II!Aqvv;!### !-vvayA~!&&)q.88,Lahhx	':;\JJJ $BHHQK0 ]2BC++- 
  S!!((8aS!aSM+B"C\RRRSa(7"33\BBBa(QCQC="99<HHHaeeHW$56EEEaeeHqc1#qc]$;<lKKK"!!((6A3sA3-+@"A<PPP!!fqc1#qc]&;";\JJJ!!%%QC}(=">MMMac9#B#BCCCbdI$C$CDDDac9#?#?@@@qb)"A"ABBBac9#?#?@@@ac9#B#BCCCadI$C$CDDD !-Q!))!,111 ),,FAAAAA 33V<F>>R]]222 CCz5 +

Aq6**+ *ahh

Aa50AB*ahh

90EF*ahh

QC90EF*ahh

QCQC;L0MN*k2*l3	4B #*+htvv6F*G*5htvv6F*G#I 	9Hh((;8;;A((;8;;Atvv/G1a&1a&)*AQUOOA.		A>R!VaZ003RWWb7l=M=M5NQR5RSQUh!uo5xC7IJQUhQCy&998bT3%L;QRQUNN8aS!I+>?B4QTPU,AWXQUOOHaV,<=xR?QRQUII,,Xq!f-=>"b@RSQ!((1a&)9:"b"
 ac9#C#CDDD*k2*l3!Aqvv;!### !-vvayA~!&&)q.88=	9@ $HTVV$45 	DH((;8;;A1a&1a&)*A$&&!BAIIaL2&&1*5QTNN1-R{{Q?QT8QqE?2HbW4EFQTNN8QF+;<hBx>PQQTHHOOHaV,<=xR?QRA ''!Q(89"b" QTMM(QC9*=>2$PRt@UVadI$B$BCCC#	Dw ZS SF+ +s-   62AC&7*AC3AD C&AC0	C3AC=	D AD
	array_api_strict8https://github.com/data-apis/array-api-strict/issues/188reasonc           	         t        j                  j                        } || j                        j                  j                  fdfdfdfdd}t        j                  di |di} |g dg d	g d
g      }|d   }t        t        j                  ||      ||z         t        t        j                  |j                  |d            |j                  |d      z         t        t        j                  ||      ||z         t        t        j                  ||j                        ||j                  z         t        t        j                  ||j                               ||j                         z         t        t        j                  j                  |d      |j                        j                  |d      |j                  z         t        t        j                  |j                  dd      |j                               |j                  dd      |j                         z         t        t        j                  ||j                        ||j                  z         t        t        j                  ||j                               ||j                         z         t        t        t        j                  |d       t        t        t        j                  d|       y )Nrr   c                 J    j                  | z  j                  d   f      S )Nr   )reshaperK   rc   r   rj   s    r/   rd   z0TestLinearOperator.test_matmul.<locals>.<lambda>  s     BFRXXa[N!C r]   c                 |    j                  j                  j                        | z  j                  d   f      S Nr*   )r   rh   r>   rK   r   s    r/   rd   z0TestLinearOperator.test_matmul.<locals>.<lambda>  s-    "**RWWRTT]Q->!"O r]   c                 @    j                  j                        | z  S r4   rg   r   s    r/   rd   z0TestLinearOperator.test_matmul.<locals>.<lambda>  s    "''"$$-!"3 r]   c                     | z  S r4   ra   )rc   r   s    r/   rd   z0TestLinearOperator.test_matmul.<locals>.<lambda>  s    a r]   rn   rj   )y      ?      ?r!   rR   rS   )      	   )r   .r*   )r*   r   r*   r   r!   ra   )r   r   r   rX   rK   r   r   r   operatormatmulr   r   r   r   r   )r[   rj   r   DrX   rY   br   s    `     @r/   test_matmulzTestLinearOperator.test_matmul
  s   
 2::R]];dffhhmmCO3') $$0q0R0n! " fI1-q1u5OOArzz!W56

1g&&	
 	1-q1u5133/QSS9199;7QYY[IOOBJJq'2ACC8JJq'"QSS(	
 	OOAIIa,aiik:IIaqyy{*	
 	133/QSS9199;7QYY[Ij(//1a8j(//1a8r]   c                    d}t        j                  |j                  d            }|j                  |j                  d             t	        j
                  t        |      5  |j                  |j                  d             d d d        |j                  |j                  d             t	        j
                  t        |      5  |j                  |j                  d             d d d        |j                  |j                  d             t	        j
                  t        |      5  |j                  |j                  d             d d d        |j                  |j                  d             t	        j
                  t        |      5  |j                  |j                  d             d d d        |j                  |j                  d             t	        j
                  t        |      5  |j                  |j                  d             d d d        |j                  |j                  d             t	        j
                  t        |      5  |j                  |j                  d             d d d        y # 1 sw Y   xY w# 1 sw Y   xY w# 1 sw Y   QxY w# 1 sw Y   xY w# 1 sw Y   xY w# 1 sw Y   y xY w)	Nz(D|d)imension mismatch)rR   rT   rT   rv   rR   rT   rR   rR   rR   rT   rT   )r   r   onesr   r   r   r   r   r   r    r   r   )r[   rj   r   rX   s       r/   test_dimension_mismatchz*TestLinearOperator.test_dimension_mismatch2  s   &&&rwwv7	]]:S1 	!HHRWWQZ 	! 	
		"''!*]]:S1 	"IIbggaj!	" 	
!]]:S1 	&HHRWWV_%	& 	
		"''&/"]]:S1 	'IIbggfo&	' 	
bggfo]]:S1 	#EE"''&/"	# 	
rwwv]]:S1 	$FF2776?#	$ 	$)	! 	!	" 	"	& 	&	' 	'	# 	#	$ 	$sH   "!J !J*!J!J'2!J3!J? J
JJ$'J03J<?KN)
__name__
__module____qualname__r\   r   r   markskip_xp_backendsr   r   ra   r]   r/   rO   rO   ?   sC    #DB [[!!I " "9	"9H$r]   rO   
dask.array)https://github.com/dask/dask/issues/11711r   c                      e Zd ZU dZ G d de      Z edddd      Zeed<    edd	d
d      Zeed<    eddd
d      Z	eed<    edddd      Z
eed<    edddd      Zeed<    edddd      Zeed<   eee	gZee   ed<   ee
eegz   Zee   ed<   	 d/dedej$                  dedefdZ	 	 d0dej$                  dedededef
dZej0                  j3                  d g d!      ej0                  j3                  d"e      d"ed eed#f   fd$              Zej0                  j3                  d g d!      ej0                  j3                  d"e      d"ed eed#f   fd%              Zej0                  j3                  d g d!      ej0                  j3                  d"e      d"ed eed#f   fd&              Zej0                  j3                  d g d!      d eed#f   fd'       Zej0                  j3                  d g d!      ej0                  jA                  d(d)*      d eed#f   fd+              Z!ej0                  j3                  d g d!      ej0                  jA                  d,      d eed#f   fd-              Z"y.)1TestDotTestsz
    This class aims to help ensure correctness of the LinearOperator
    interface, by verifying correctness properties based on equivalent
    computations using 'forward' and 'adjoint' modes.
    c                   D    e Zd ZU dZeedf   ed<   eed<   eed<   eed<   y)TestDotTests.OperatorArgsz
        shape: (core) shape of the operator
        op_dtype: dtype of the operator
        data_dtype: real dtype corresponding to op_dtype for data generation
        complex: the operator has a complex dtype
        .rK   op_dtype
data_dtypecomplexN)	r   r   r   __doc__r;   r5   __annotations__strboolra   r]   r/   OperatorArgsr   V  s&    	 S#Xr]   r   )rx   rx   rp   Freal_square_args)r   r   int32float32integer_square_args)   r   	complex64Tcomplex_square_args)r      real_overdetermined_argsr   complex_overdetermined_args)rU   r   real_underdetermined_argssquare_args_listall_args_listrj   opr   complex_datac                 8   t         j                  j                  d      }t        j                  |      }|j                  ^ }}}	t        ddddd      \  }
}|	f|ffg |
||	g |||ffD ]'  \  }}|j                  ||      }|j                  ||      }|r2|d	|j                  ||      z  z   }|d	|j                  ||      z  z   }|j                  |      }|j                  |      }|j                  |      }|j                  |      }|j                  ||d
      }|j                  ||d
      }t        j                  |      j                  dk  rdnd}t        j                  |      j                  dk  rdnd}t        ||||       * y)a-  
        This check verifies the equivalence of the forward and adjoint computation,
        using `matvec` and `rmatvec` respectively, on randomised data.

        Data is generated with the real dtype `data_dtype` and operated on by the
        linear operator `op`.

        If `complex_data` is set to `True`, complex data is instead generated
        by combining randomised real and imaginary components, each generated
        with `data_dtype`.

        If `check_operators` is set to `True`, equivalence is checked between
        `matvec` and `*` and `@`,
        and between `rmatvec` and the composition of `.H` and `*`.

        If `check_dot` is set to `True`, equivalence is checked between
        `matvec` and `.dot`,
        and between `rmatvec` and the composition of `.H` and `.dot`.
        *   r!   rR   r   rU   ?   r#   rr                 ?r   r1   :0yE>-q=h㈵>V瞯<rtolatolN)r7   r8   r9   ro   rK   rM   standard_normalr   r   r   vecdotfinfoepsr   )r[   rj   r   r   r   r'   ro   rG   MNu_broadcastv_broadcastu_shapev_shapeuvop_uopH_vop_u_H_vuH_opH_vr   r   s                         r/   check_matveczTestDotTests.check_matvec|  s   . ii##B'$XXa#@A1!$
 [ TA4L,{,[,!,.M.Mk.M1.MN	!
 	FGW ##G5#9A##G5#9Ac11'1GGHc11'1GGH

1A

1A99Q<DJJqMEyyqry2HyyEy3HHHZ044t;5DHHZ044t;5DHhTE-	Fr]   check_operators	check_dotc                    t         j                  j                  d      }|j                  dd      }t        j                  |      }	|j
                  ^ }
}}|j                  g |
|||	      }|j                  g |
|||	      }|r@|d|j                  g |
|||	      z  z   }|d|j                  g |
|||	      z  z   }|j                  |      }|j                  |      }|j                  |      }|j                  |      }|rPt        |||z         t        |||z         t        ||j                  |z         t        ||j                  |z         |r@t        ||j                  |             t        ||j                  j                  |             |j                  |      j                  }|j                  |      j                  }|j                  ||      }|j                  ||      }t        j                   |      j"                  dk  rdnd}t        j                   |      j"                  dk  rd	nd}t        ||||
       y)a-  
        This check verifies the equivalence of the forward and adjoint computation,
        using `matmat` and `rmatmat` respectively, on randomised data.

        Data is generated with the real dtype `data_dtype` and operated on by the
        linear operator `op`.

        If `complex_data` is set to `True`, complex data is instead generated
        by combining randomised real and imaginary components, each generated
        with `data_dtype`.

        If `check_operators` is set to `True`, equivalence is checked between
        `matmat` and `*` and `@`,
        and between `rmatvec` and the composition of `.H` and `@`.

        If `check_dot` is set to `True`, equivalence is checked between
        `matmat` and `.dot`,
        and between `rmatmat` and the composition of `.H` and `.dot`.
        r   r!   d   )sizero   r   r   r   r   r   r   N)r7   r8   r9   r+   ro   rK   r   r   r   r    r   r   r   rh   mTr   r   r   )r[   rj   r   r   r   r	  r
  r'   kro   rG   r   r   UVop_UopH_Vop_U_HUHop_U_H_VUH_opH_Vr   r   s                          r/   check_matmatzTestDotTests.check_matmat  s!   0 ii##B'LLC $XXa %9{%9A%9q%9G%9{%9A%9q%9GR#--3G[3G!3GQ3Gu-UUVAR#--3G[3G!3GQ3Gu-UUVAJJqMJJqMyy|

1D"q&)D"q&)E244!8,E244!8,D"&&),E24488A;/!!WWQZ]]99VQ'99R',0047uT,0047uT(DAr]   rG   )ra   rR   )rR   rT   rU   r   r   .c                 6   fd}|j                   z   }t        |j                        }t        j                  ||||      }| j                  ||j                  |j                         | j                  ||j                  |j                         y)zs
        Simple identity operator on square matrices.
        Tests batches of RHS via `args.batch_shape`.
        c                     g t        j                  | j                  d d       | j                  d   }j                  | |      S )Nr   )xpxrE   rK   r:   )rc   rK   rG   rj   s     r/   identityz3TestDotTests.test_identity_square.<locals>.identity  sB    Sc**;ESqwwr{SE??1e,,r]   rK   ro   r   r   rj   r   r   N	rK   getattrr   r   r   r  r   r   r  )r[   r   rG   rj   r  rK   ro   r   s     ``    r/   test_identity_squarez!TestDotTests.test_identity_square  s    	- djj(DMM*%%uX"

 	"bT__4<<X"bT__4<<Xr]   c                 t  	
 |j                   \  	
d 	fd}
fd}|j                   z   }t        |j                        }t        j                  ||||      }| j                  ||j                  |j                         | j                  ||j                  |j                         y)z
        Identity operator with zero-padding on non-square matrices.
        Tests batches of RHS via `args.batch_shape`.
        c                     dg| j                   z  }d|| j                  d   z
  f|d<   t        j                  | |d      S )N)r   r   r   r   constant)mode)r$   rK   r  pad)rc   target_size
pad_widthss      r/   pad_core_dimz:TestDotTests.test_identity_nonsquare.<locals>.pad_core_dim  s@     AFF*Jqwwr{!:;JrN771jz::r]   c                 $   t        j                  | j                  d   z
        xdk(  r nxdk(  r | dd f   } ndk(  r
 |       } g t        j                  | j                  d d       | j                  d   }j                  | |      S )Nr   r   r*   .r)  r7   signrK   r  rE   r:   )rc   rK   r   rG   r+  rj   s     r/   mvz0TestDotTests.test_identity_nonsquare.<locals>.mv  s    ''!''"+/*#rr'
A$QA6ASc**;ESqwwr{SE??1e,,r]   c                 $   t        j                  | j                  d   z
        xdk(  r nxdk(  r  |       } ndk(  r	| dd f   } g t        j                  | j                  d d       | j                  d   }j                  | |      S )Nr   r   r*   r-  .r.  )rc   rK   r   rG   r+  rj   s     r/   rmvz1TestDotTests.test_identity_nonsquare.<locals>.rmv%  s    ''!aggbk/*$QA6A#rr'
ASc**;ESqwwr{SE??1e,,r]   r  r   Nr!  )r[   r   rG   rj   r0  r2  rK   ro   r   r   r   r+  s     ``     @@@r/   test_identity_nonsquarez$TestDotTests.test_identity_nonsquare	  s     zz1	;

	-
	- djj(DMM*%%uR
 	"bT__4<<X"bT__4<<Xr]   c                 B   fd}fd}|j                   z   }t        |j                        }t        j                  ||||      }| j                  ||j                  |j                         | j                  ||j                  |j                         y)z|
        Simple (complex) scaling operator on square matrices.
        Tests batches of RHS via `args.batch_shape`.
        c                     dt        d      z  }g t        j                  | j                  d d       | j                  d   }j	                  || z  |      S )Ny      @       @r   absr  rE   rK   r:   rc   scalerK   rG   rj   s      r/   r9  z/TestDotTests.test_scaling_square.<locals>.scaleA  T    s6{*ESc**;ESqwwr{SE??519e44r]   c                     dt        d      z  }g t        j                  | j                  d d       | j                  d   }j	                  || z  |      S )Ny      @       r   r6  r8  s      r/   r_scalez1TestDotTests.test_scaling_square.<locals>.r_scaleF  r:  r]   r  r   Nr!  )	r[   r   rG   rj   r9  r<  rK   ro   r   s	     ``     r/   test_scaling_squarez TestDotTests.test_scaling_square:  s    	5
	5
 djj(DMM*%%uUG
 	"bT__4<<X"bT__4<<Xr]   c                      G fddt         j                        j                  dz  }d} g ddj                  |      }| j	                  ||d       | j                  ||d       y)	z
        Simple rotation operator defined by `matmat` and `adjoint`,
        subclassing `LinearOperator`.
        Tests batches of RHS via `batch_shape`.
        c                   8     e Zd Z fdZfdZfdZ xZS )0TestDotTests.test_subclass_matmat.<locals>.RotOpc                 8    || _         t        | 	  ||       y Nrj   )_thetasuperr   )r[   ro   rK   theta	__class__rj   s       r/   r   z9TestDotTests.test_subclass_matmat.<locals>.RotOp.__init__\  s    # " 5r]   c                 x   | j                   }j                  t        j                  |      t        j                  |       gt        j                  |      t        j                  |      gg      }||z  }t        j                  |j                  d d       |j                  dd  z   }j                  ||      S )N)	rD  r   r7   cossinr  rE   rK   r:   )r[   r   rF  RrY   rK   rG   rj   s         r/   _matmatz8TestDotTests.test_subclass_matmat.<locals>.RotOp._matmat`  s    JJVVE]RVVE]N3VVE]RVVE]3   E,,[!''#2,G!''RTRU,Vq%00r]   c                 Z    | j                    } | j                  | j                  |      S r4   )rD  ro   rK   )r[   negative_thetaRotOps     r/   _adjointz9TestDotTests.test_subclass_matmat.<locals>.RotOp._adjointj  s%    "&++TZZ^DDr]   )r   r   r   r   rM  rQ  __classcell__)rG  rP  rG   rj   s   @r/   rP  r@  Z  s    61E Er]   rP  r!   rp   )rK   ro   rF  Fr   N)r   r   pirp   r  r  )r[   rG   rj   rF  r   r   rP  s    ``   @r/   test_subclass_matmatz!TestDotTests.test_subclass_matmatS  s    	E 	EI,, 	E( 	
-;--1-RZZuM"bZeL"bZeLr]   r   r   r   c                 *   t         j                  j                  d      }|j                  |j	                  g |dd      |j
                        }t        j                  |      }d}| j                  |||d       | j                  |||d       y)za
        Test operators coming from `aslinearoperator`,
        *including batched LHS*.
        r   rT   rr   rp   Tr   N)
r7   r8   r9   r   r   r   r   r   r  r  )r[   rG   rj   r'   r   r   r   s          r/   test_aslinearop_densez"TestDotTests.test_aslinearop_denseu  s     ii##B' 4+ 4q 4! 45--  
 ''/
"bZdK"bZdKr]   np_onlyc                     t        j                  g |dd|j                        }t        j                  |      }d}| j                  |||d       | j                  |||d       y)zs
        Test operators coming from `aslinearoperator` with sparse arrays,
        *including batched LHS*
        rT   rr   rp   Tr   N)sparserandom_arrayr   r   r   r  r  )r[   rG   rj   r   r   r   s         r/   test_aslinearop_sparsez#TestDotTests.test_aslinearop_sparse  sm     $$%9{%9A%9q%9O''/
"bZdK"bZdKr]   N)F)FFF)#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   parametrizer;   r5   r#  r3  r=  rT  r   rV  r\  ra   r]   r/   r   r   O  s{   

z 
 &2)Y&l  )5E)  )5+y$)  .:)Y.l  1=,	41  /;	9e/| 
 	-/B,d<(  *: "=?X= *M4%  #6F6F"+":":6FHK6F6Ft =B<B..<B<?<B<B59<B <B| [[],HI[[V%56Y Y/4S#XY 7 JY* [[],HI[[V]3-Y -Y/4S#X-Y 4 J-Y^ [[],HI[[V%56Y Y5c? Y 7 JY. [[],HIMc3h M JMB [[],HI[[!!,7b!cL c?L d JL" [[],HI[[!!$!/	L%S/ 	L 0 J	Lr]   r   c                       e Zd ZddZd Zej                  j                  d      d        Zej                  j                  d      d        Z	ej                  j                  dg d	      d
        Zy)TestAsLinearOperatorNc                   	
 g }nt         		fd
	fd G 
fddt        j                        } G fdd|      } G 
fdd|      } G 	fd	d
|      }r0|j                  f       |j                   |      f       |j                   |      f       |j                   |      f       |j                  t        j                  j
                        j
                  f       |j                  t        j                  j
                        j                  j                        f       |j                  t        j                  j
                        j                         j                        f       |S |j                  t              f       |j                  t        j                        f       |j                  t        j                        f       |j                   |      f       |j                   |      f       |j                   |      f       |S )Nc                 f    | z  }t        | j                        dk(  rj                  |d      }|S )Nr!   r   )rB   rK   r   )rc   y_xporiginals     r/   r0  z+TestAsLinearOperator.make_cases.<locals>.mv  s1    1A177|q KK7+Hr]   c                 @    j                  j                        | z  S r4   rg   )rc   rc  rd  s    r/   r2  z,TestAsLinearOperator.make_cases.<locals>.rmv  s    88HJJ'!++r]   c                   2     e Zd ZdZ fdZfdZ xZS )4TestAsLinearOperator.make_cases.<locals>.BaseMatlikera   c                 >    t         |   |j                         y N)ro   rK   rj   )rE  r   rK   )r[   ro   rG  rd  rj   s     r/   r   z=TestAsLinearOperator.make_cases.<locals>.BaseMatlike.__init__  s"     ".. ! r]   c                      |      S r4   ra   )r[   rc   r0  s     r/   _matvecz<TestAsLinearOperator.make_cases.<locals>.BaseMatlike._matvec  s    !ur]   )r   r   r   r   r   rk  rR  )rG  r0  rd  rj   s   @r/   BaseMatlikerg    s    D r]   rl  c                       e Zd ZdZ fdZy)3TestAsLinearOperator.make_cases.<locals>.HasRmatvecra   c                      |      S r4   ra   )r[   rc   r2  s     r/   _rmatvecz<TestAsLinearOperator.make_cases.<locals>.HasRmatvec._rmatvec  s    1vr]   N)r   r   r   r   rp  )r2  s   r/   
HasRmatvecrn    s    Dr]   rq  c                   "    e Zd ZdZ fdZy)3TestAsLinearOperator.make_cases.<locals>.HasAdjointra   c                     | j                   d   | j                   d   f}t              }t              }t        j                  |||      S )Nr*   r   )r   r   ro   rK   rj   )rK   r   r   r   )r[   rK   r   r   ro   r0  r2  rj   s       r/   rQ  z<TestAsLinearOperator.make_cases.<locals>.HasAdjoint._adjoint  sP    

1tzz!}4 !"+ //v8?6;6;35	8 8r]   N)r   r   r   r   rQ  )ro   r0  r2  rj   s   r/   
HasAdjointrs    s    D8 8r]   ru  c                   $    e Zd ZfdZ fdZy)3TestAsLinearOperator.make_cases.<locals>.HasRmatmatc                     |z  S r4   ra   )r[   rc   rd  s     r/   rM  z;TestAsLinearOperator.make_cases.<locals>.HasRmatmat._matmat  s    !|#r]   c                 @    j                  j                        |z  S r4   rg   )r[   rc   rc  rd  s     r/   _rmatmatz<TestAsLinearOperator.make_cases.<locals>.HasRmatmat._rmatmat  s    xx

+a//r]   Nr   r   r   rM  rz  )rc  rd  s   r/   
HasRmatmatrw    s    $0r]   r|  rr   )r7   r   r   appendr   r>   r   rh   r   r   rW   rZ  	csr_array)r[   rd  ro   rj   casesrl  rq  ru  r|  rc  r0  r2  s    ```     @@@r/   
make_caseszTestAsLinearOperator.make_cases  s    Nb		,	 	)22 		 		8 	8 	8	0 	0 LL(H-.LL*U+X67LL*U+X67LL*U+X67LL)44XZZ@BBHMNLL**8::688!  LL**8::6>>@!# 
 LfXU3X>?bhhxu5x@Af&&xu=xHIj'23j'23j'23r]   c           	         g | _         | j                  }t        j                  g dg dg      }| xj                    ||t        j                        z  c_         | xj                    ||t        j
                        z  c_         | xj                    ||t        j                        z  c_         | xj                    ||j                  t        j                        D cg c]0  \  }}t        j                  |      j                  |j                  f2 c}}z  c_         | xj                    ||j                  t        j                        D cg c]>  \  }}t        j                  |      j                  |j                  j                         f@ c}}z  c_         | xj                    ||j                  t        j                        D cg c]B  \  }}t        j                  |      j                         |j                  j                         fD c}}z  c_         t        j                  g dg dg      }| xj                    ||t        j                        z  c_         | xj                    ||j                  t        j                        D cg c]0  \  }}t        j                  |      j                  |j                  f2 c}}z  c_         | xj                    ||j                  t        j                        D cg c]>  \  }}t        j                  |      j                  |j                  j                         f@ c}}z  c_         | xj                    ||j                  t        j                        D cg c]B  \  }}t        j                  |      j                         |j                  j                         fD c}}z  c_         y c c}}w c c}}w c c}}w c c}}w c c}}w c c}}w )NrQ   rS   r*   r{                 @)y              @y              @rV   )r  r  r7   rW   r   r   rp   r>   r   r   r   rh   r   r   )r[   r  rd  r   rX   s        r/   r\   z!TestAsLinearOperator.setup_method  s   
__
88Wg./

j28844


j2::66


j2::66


#-hjj"**#EG1a "221577= G 	G


#-hjj"**#EG1a "221577D G 	G


#-hjj"**#EG1a "2215==?L G 	G
 88[+67

j2==99


#-hjj"--#HJ1a "221577= J 	J


#-hjj"--#HJ1a "221577D J 	J


#-hjj"--#HJ1a "2215==?L J 	J
GGG
JJJs(   5O
AO
AO
5O%
AO+
 AO1
TrW  c           	      ,
   | j                   D ]  \  }}t        j                  |      }t        j                  g d      t        j                  dgdgdgg      g}t        j                  ddg      t        j                  dgdgg      g}|j
                  t        j                  k(  rh|t        j                  g d      t        j                  dgdgdgg      gz  }|t        j                  ddg      t        j                  dgdgg      gz  }t        j                  ddgdd	gdd
gg      }|D ]  }|j                  d   dk(  rt        j                  t        d      nt        j                         }	|	5  t        |j                  |      |j                  |             d d d        t        ||z  |j                  |              t        |j!                  |      |j                  |             t        ||z  |j                  |             |D ]k  }
|
j                  d   dk(  rt        j                  t        d      nt        j                         }	|	5  t        |j#                  |
      |j$                  j'                         j                  |
             t        |j$                  j                  |
      |j$                  j                  |
             t        |j(                  j                  |
      |j$                  j'                         j                  |
             t        |j+                         j                  |
      |j$                  j'                         j                  |
             d d d        n |D ]/  }
|
j,                  dk  rt        |j/                  |
      |j$                  j'                         j                  |
             t        |j$                  j!                  |
      |j$                  j                  |
             t        |j(                  j!                  |
      |j$                  j'                         j                  |
             t        |j+                         j!                  |
      |j$                  j'                         j                  |
             2 t1        |d      r t        |j
                  |j
                         t3        t1        |d              y # 1 sw Y   ]xY w# 1 sw Y    xY w)NrQ   r*   r!   rR   r  r{   r  rT   rU   rV   r   ru   rv   ro   r   )r  r   r   r7   rW   ro   r   rK   r   r   r   r   r   r
   r   r   r   r   r>   rh   r   r   r$   r    hasattrr	   )r[   rj   r   A_arrayrX   xsysx2rc   r   rb  s              r/   
test_basiczTestAsLinearOperator.test_basic   s    ** 3	(JAw**1-A((9%((QC!qc?+-B((Aq6"BHHqcA3Z$89Bww"--'rxx,xx!rdRD 124 4rxxB("((QC";*?@@Aq6Aq6Aq623B 	4 wwr{a' LL6FG#//1 
  > !gkk!n=> QUGKKN3	4 "w{{27RR1 
Q wwr{a' LL6FG#//1 
  Q 1wyy~~/?/C/CA/FG A		a0@A A		0@0D0DQ0GH !3!3A!6		8H8L8LQ8OP	Q Q
Q  M66A:QYYq\799>>+;+?+?+BCQSSZZ]GIIMM!,<=QSSZZ]GIINN,<,@,@,CDQYY[//2GIINN4D4H4H4KLM q!QWWagg.GAv&'g3	((> >Q Qs   6+S<DT	<T	Tc                    | j                   D ]  \  }}t        j                  |      }t        j                  g d      }t        j                  dgdgdgg      }t        j                  ddgddgddgg      }t        |j                  |      |j                  |             t        |j                  |      |j                  |             t        |j                  |      |j                  |              y )NrQ   r*   r!   rR   rT   rU   rV   )r  r   r   r7   rW   r
   r   )r[   rj   r   r  rX   x0x1r  s           r/   test_dotzTestAsLinearOperator.test_dot8  s     ** 		5JAw**1-A)$BA3aS/*BAq6Aq6Aq623BrGKKO4rGKKO4rGKKO4		5r]   ro   )int64rp   r   c                 J   |dk(  r)t        |      rt        dk7  rt        j                  d       t	        ||      }|j                  g dg dg|      }| j                  |||      D ]  \  }}t        j                  |      }|j                  g d      |j                  dgd	gd
gg      g}|j                  dd	g      |j                  dgd	gg      g}|j                  |j                  k(  rX||j                  g d      |j                  dgdgdgg      gz  }||j                  ddg      |j                  dgdgg      gz  }|j                  ddgd	dgd
dgg|      }	|D ]&  }
|j                  |
|      }
t        ||
z  ||
z         ( t        |j                  |	      ||	z         t        ||	z  ||	z         |D ]#  }|j                  d	k  r|j                  ||      }t        |j                  |      |j!                  |j"                        |z         t        |j"                  j                  |      |j!                  |j"                        |z         t        |j$                  j                  |      |j!                  |j"                        |z         t        |j'                         j                  |      |j!                  |j"                        |z         & t)        |d      r t+        |j                  |j                         t)        |d      rJ  y )Nr  cpuz'"addmm_cuda" not implemented for 'Long'rQ   rS   rr   rC  r*   r!   rR   r  r{   r  rT   rU   rV   ro   r   )r   r   r   skipr"  r   r  r   r   ro   r   astyper   r   r$   r    rh   r>   r   r   r  r
   )r[   ro   rj   rd  r   r  rX   r  r  r  rc   rb  s               r/   test_xpzTestAsLinearOperator.test_xpF  s   G1FKKCDE"::y)4E:B//(Eb/A !	&JAw**1-A**Y'**qcA3_-/B**aV$bjj1#s&<=Bww"--'rzz+.zzA3rd"346 6rzz1b'*BJJbT{,CDDaVaVaV4EBB 4IIa'Aw{34 AHHRL'B,7AFGbL1 O66A:IIa'		!bgggii.@1.DE

1rwwwyy/AA/EF

1rwwwyy/AA/EF		 2 21 5rwwwyy7IA7MNO q'"QWWagg.1f%%%C!	&r]   r4   )r   r   r   r  r\   r   r   r   r  r  r]  r  ra   r]   r/   r_  r_    s    PdJ. [[!!$!/5( 05(n [[!!$!/5 05 [[W&HI&& J&&r]   r_  c                 h     t        j                  d fd       }t        |      }d|vsJ |       y )N)r*   r*   c                 (    j                  dg      S r   r   rc   rj   s    r/   rd   ztest_repr.<locals>.<lambda>q  s    

A3 r]   rK   r   rj   zunspecified dtype)r   r   repr)rj   rX   repr_As   `  r/   	test_reprr  p  s5      v6OTVWA!WFf,4f4,r]   c                    t        j                  d|       }t        || j                  g d      z  | j                  g d             t        t	        |j                  | j                  | j                  d      d                  | j                  d             t        t        |j                  | j                  g d             y )Nr   rC  rQ   r   r|   )r   IdentityOperatorr   r   r   r   r   aranger   r   r   )rj   idents     r/   test_identityr  v  s    &&v"5EEBJJy112::i3HIHUYYrzz"))A,'GHI299UV<X*ellBJJ|,DEr]   c           
          t        j                   j                   j                  d j                        d            } fd}t        j
                  d|       }||||z  ||z  |j                  |j                         ||z   ||z   |dz  g	}|D ],  }t        |d      sJ t        |d	      sJ t        |d
      r,J  y )N   rr   r   c                     j                  |       } | j                  dk(  s| j                  dk(  sJ j                  d      S )Nr  )rR   r*   rT   )r   rK   r   r  s    r/   always_four_onesz)test_attributes.<locals>.always_four_ones  s:    JJqMww$!''V"333wwqzr]   r   r  rT   ro   rK   rk  )	r   r   r   r  rp   r   r   r   r  )rj   rX   r  rY   opsr   s   `     r/   test_attributesr  ~  s    ""2::bii"**i.Mv#VWA
 	  v6F2NAaQAqssAIIKQAq!t
DC &r7###r7###r9%%%&r]   c                     | S )z> Needed for test_pickle as local functions are not pickleable ra   rc   s    r/   matvec_for_pickler    s    Hr]   r   z0pickle-ability is not guaranteed by the standardc                 >   dd l }t        |       rdnd}t        ||j                  dz         D ]n  }t	        j
                  dt        |       }|j                  ||      }|j                  |      }|j                  D ]  }t        ||      t        ||      k(  rJ  p y )Nr   r!   r*   r   rC  )protocol)pickler   rA   HIGHEST_PROTOCOLr   r   r  dumpsloads__dict__r"  )rj   r  protocol_minr  rX   srY   r  s           r/   test_pickler    s    
  1!L,(?(?!(CD 2$$V->2FLLXL.LLO 	2A1a=GAqM111	22r]   c                      G d dt         j                        }t        t        d      5  t	        t
        |       d d d         G  fddt         j                        } |d      }t        |j                   j                  g d             j                  g d             t	        t        |j                   j                  g d	              G  fd
dt         j                        } | j                  t        j                  j                  dd                  }|j                   j                  t        j                  j                  d                  j                  dk(  sJ y # 1 sw Y   3xY w)Nc                       e Zd Zy)test_inheritance.<locals>.EmptyN)r   r   r   ra   r]   r/   Emptyr    s    r]   r  zshould implement at leastrv   c                   (     e Zd Z fdZd Z xZS )"test_inheritance.<locals>.Identityc                 .    t         |   d ||f       y ri  )rE  r   )r[   nrG  rj   s     r/   r   z+test_inheritance.<locals>.Identity.__init__  s    G41v"=r]   c                     |S r4   ra   r[   rc   s     r/   rk  z*test_inheritance.<locals>.Identity._matvec  s    Hr]   )r   r   r   r   rk  rR  rG  rj   s   @r/   Identityr    s    	>	r]   r  rR   rQ   rS   c                   (     e Zd Z fdZd Z xZS )$test_inheritance.<locals>.MatmatOnlyc                 `    t         |   |j                  |j                         || _        y rB  rE  r   ro   rK   rX   r[   rX   rG  rj   s     r/   r   z-test_inheritance.<locals>.MatmatOnly.__init__  &    GQWWagg"5DFr]   c                      | j                   |z  S r4   r~   r  s     r/   rM  z,test_inheritance.<locals>.MatmatOnly._matmat  s    66A:r]   )r   r   r   r   rM  rR  r  s   @r/   
MatmatOnlyr    s    		r]   r  rU   rU   )r   r   r   RuntimeWarningr   	TypeErrorr   r   r   NotImplementedErrorr   r7   r8   randnrK   )rj   r  r  id3r  mms   `     r/   test_inheritancer    s   	((  
~%@	A (i'(9++  1+CCJJrzz)45rzz)7LM%s{{BJJy4IJY--  
BJJryyq!45	6B99RZZ		 234::dBBB/( (s   E..E8c                    | j                  t        j                  j                  dd      dt        j                  j                  dd      z  z         }| j                  t        j                  j                  dd            }t	        j
                  |      }t	        j
                  |      }||z   }||z   }|j                  | j                  k(  sJ |j                  | j                  k(  sJ y )Nr!   r   )	r   r7   r8   randr   r   ro   rp   r   )rj   mat_complexmat_realcomplex_operatorreal_operatorsum_complexsum_reals          r/   test_dtypes_of_operator_sumr    s     **RYY^^Aa02		q8K3KKLKzz"))..1-.H 11+>..x8M"%55K},H>>RZZ'''---r]   c                 d    dgfd}t        j                  d||        t        d   d       y )Nr   c                 "    dxx   dz  cc<   | S Nr   r*   ra   )r  
call_counts    r/   r   z#test_no_double_init.<locals>.matvec  s    1r]   r!   r!   r   rj   r*   )r   r   r
   )rj   r   r  s     @r/   test_no_double_initr    s1    J VFr:A"r]   
test_dtypec                    d| j                   v r t              st        j                  d       j	                  t        j                  d|             fd}t        j                  d|      }j                  }j                  |d      r$	 j	                  d	      j	                  d
      z    |j                  |k(  sJ y # t        $ r t        j                  d      }Y 4w xY w)N
longdoublez$longdoubles are only tested for `np`r*   rr   c                 <    j                  | d   z  | d   g      S r  )stack)r  scalarrj   s    r/   r0  z/test_determine_lo_dtype_from_matvec.<locals>.mv  s#    xx!A$!-..r]   r  r  )zreal floatingzcomplex floatingr!   g       @integral)kind)r   r   r   r  r   r7   rW   r   r   ro   isdtyper  r  default_dtype)r  rj   r0  loexpectedr  s    `   @r/   #test_determine_lo_dtype_from_matvecr    s    z***8B<:;ZZ*56F/ 
	!	!&	;B ||H	zz(AB	>JJqMBJJsO+ 88x  	>((*=H	>s   #C	 	 C,+C,c                 |      fd}t        j                  d|       } j                  |j                  d      sJ y )Nc                 <    j                  d| d   z  | d   g      S )N   r   r*   r  )r  rj   s    r/   r0  z+test_determine_lo_dtype_for_int.<locals>.mv  s#    zz31:qt,--r]   r  r  r  )r   r   r  ro   )rj   r0  r  s   `  r/   test_determine_lo_dtype_for_intr    s6    . 
	!	!&	;B::bhh
+++r]   c                 R   | j                  dgg| j                        }t        j                  |      }d|z  }d|z  }| j                  dg| j                        }t	        |j                  |      | j                  ||             t	        |j                  j                  |      | j                  | j                  |j                        |             t	        |j                         j                  |      | j                  | j                  |j                        |             y Nr   rr   r*   )r   r   r   r   r   r   r   r   rh   r>   r   rj   r   rX   rY   Yr  s         r/   test_adjoint_conjugater  	  s    


RD6
/A""1%A
QA
QA


A3bmm
,AAEE!Hbii1o.ACCGGAJ		"''!##, :;AIIKOOA&		"''!##,(BCr]   c                 v    | j                  dgg      }t        j                  |      }|j                  dk(  sJ y )Nr*   r!   )r   r   r   r$   )rj   r   rX   s      r/   	test_ndimr    s4    


QC5A""1%A66Q;;r]   c                    | j                  dgg| j                        }t        j                  |      }d|z  }d|z  }| j                  dg| j                        }t	        |j                  |      | j                  ||             t	        |j                  j                  |      | j                  |j                  |             y r  )r   r   r   r   r   r   r   r>   r  s         r/   test_transpose_noconjugater    s    


RD6
/A""1%A
QA
QA


A3bmm
,AAEE!Hbii1o.ACCGGAJ		!##q 12r]   r   c                 &    t         j                   j                        } G  fddt        j                        } | |ddgddgg            } |ddg      }|} |dd	gd
dgg      } j                  |d      } |ddgddgg      }t        ||z  ||z         t        |j                  |z  |j                  |z         t        |j                  |z  |j                  |z         t        ||z  ||z         t        ||z  ||z         t        |j                  |z  |j                  |z         t        ||z  ||z         t        |j                  |z  |j                  |z         t        |j                  |z  |j                  |z         y )Nrr   c                   .     e Zd Z fdZd Zd Z xZS )/test_transpose_multiplication.<locals>.MyMatrixc                 `    t         |   |j                  |j                         || _        y rB  r  r  s     r/   r   z8test_transpose_multiplication.<locals>.MyMatrix.__init__2  r  r]   c                      | j                   |z  S r4   r~   r[   others     r/   rM  z7test_transpose_multiplication.<locals>.MyMatrix._matmat5  s    %"7r]   c                 4    | j                   j                  |z  S r4   )rX   r  r   s     r/   rz  z8test_transpose_multiplication.<locals>.MyMatrix._rmatmat6  s    $&&))e*;#;r]   )r   r   r   r   rM  rz  rR  r  s   @r/   MyMatrixr  1  s    	 	8;r]   r  r*   r!   rR   rT   r"      ry   (   r   )	r   r   r   r   r   r   r   r>   r  )	rj   r   r  rX   r   X_TrY   X2r  s	   `        r/   test_transpose_multiplicationr  *  sd    rzz7H<9++ < 	Aq6Aq6*+,A!QA
C2r(RH%&A	Aw	B1a&1a&!"AAE1q5!ACC!GQSS1W%ACC!GQTTAX&AE1q5!C!GS1W%ACC!GQTTAX&AFAF#BEEAIruuqy)ACC"HaddRi(r]   TrW  c                     t        j                  d      } t        j                  t        j                  d            }t	        || z  t         j
                        sJ t	        | |z  t         j
                        sJ t        || z  j                         t        j                  d             t        | |z  j                         t        j                  d             t        j                  dd       }d}t        t        |      5  || z   d d d        t        t        |      5  | |z   d d d        t        j                  |       } t	        || z  t        j                        sJ t	        | |z  t        j                        sJ t        || z  j                  t        j                  d            t        j                  d             y # 1 sw Y   xY w# 1 sw Y   xY w)Nr!   r  c                     | S r4   ra   r  s    r/   rd   z.test_sparse_matmat_exception.<locals>.<lambda>T  s    ! r]   )r   z6Try wrapping the matrix with `aslinearoperator` first.rv   )rZ  	eye_arrayr   r   r   sparrayr   toarrayr7   eyer   r   r  r   r   )rY   rX   r   s      r/   test_sparse_matmat_exceptionr  J  sg   A""6#3#3A#67Aa!eV^^,,,a!eV^^,,,QUOO%rvvay1QUOO%rvvay1  <A
BC	y	, 	A	y	, 	A 	""1%Aa!eY55666a!eY55666QUNN2771:.
;  s   G&GGG!z	jax.numpyz.remaining reference (due to lazy_xp_function?)c                     | j                  d      }t        t        j                  ||       5 }|j	                          ~d d d        y # 1 sw Y   y xY wr   )r  r   r   MatrixLinearOperatorr   )rj   rX   r   s      r/   "test_MatrixLinearOperator_refcycler  a  sF     	q	A	I::Ar	B b


  s   AAleftFoperator_definition)subclass_matvecsubclass_matmat__init__matvec__init__matmatr   batch_A)ra   r  r  batch_x)ra   )rV   r*   )r   r*   ro   c                    t         j                  j                  d      }d\  }}}	|j                  g |||      |j                  g | r|n|      }
|j                  g | r|n|d      }|j                  g | r|n||	      }|t         j                  k(  rd|j                  j                        z  z   |
d|j                  |
j                        z  z   }
|d|j                  |j                        z  z   }|d|j                  |j                        z  z   }fd|
||fD        \  }
}}fdfd|dk(  rt        j                        }n|d	k(  r8t        j                  j                  j                  fd
fd      }n|dk(  rt        j                  d       n|dk(  r= G fddt
        j                        } |j                  j                        }nA|dk(  r< G fddt
        j                        } |j                  j                        }t        | rj                  |
      nj                  |
      | r	 |
      n! j                  j                        |
             rKd}t        j                   t"        |      5  | r|j                  |      n|j                  |       d d d        n{| rdnd}t        j$                  t&        |      5  t        | r|j                  |      n|j                  |      | r|z  nj                  j                        |z         d d d        t        j                   t"        d      5  | r|j                  |      n|j                  |       d d d        rdnd}t        j                   t"        |      5  | r|j)                  |
      n|j+                  |
       d d d        t        | r|j)                  |      n|j+                  |      | r|z  nj                  j                        |z         t        | r|j)                  |      n|j+                  |      | r|z  nj                  j                        |z         | rr*t        j                   t"        d      5  ||
z   d d d        nEt        ||
z  |
z         t         ||
      |
z         t        |j-                  |
      |
z         t        ||z  |z         t         ||      |z         t        |j-                  |      |z         t        ||z  |z         t         ||      |z         t        |j-                  |      |z         y d}	 t        j.                  |d d        |s2d}t        j                   t"        t0        f|      5  |
|z   d d d        n0t        |
|z  |
z         t        |j3                  |
      |
z         t        j                   t"        d      5  ||z   d d d        t        j                  |j                        |z  j                  |j                        z         t        |j3                  j                  |j                              j                  |j                        z         y # 1 sw Y   ~xY w# 1 sw Y   xY w# 1 sw Y   PxY w# 1 sw Y   xY w# 1 sw Y   xY w# t"        $ r d}Y qw xY w# 1 sw Y   xY w# 1 sw Y   xY w)Nl   w{3j)rT   rR   r!   r*   r   c              3   @   K   | ]  }j                  |        y wr4   r  )r,   rc   rj   s     r/   r0   ztest_batch.<locals>.<genexpr>  s     Przz!}Ps   c                     s|j                   dk(  sJ | |z  S |j                   dk\  sJ | |dj                  f   z  d   S )Nr*   r!   .).r   )r$   newaxis)rX   rc   r  rj   s     r/   r   ztest_batch.<locals>.matvec  sN    66Q;;q5L 66Q;;3

?+,f55r]   c                 `    s|j                   dk(  sJ | |z  S |j                   dk\  sJ | |z  S )Nr!   rR   )r$   )rX   r   r  s     r/   r   ztest_batch.<locals>.matmat  s<    66Q;;
 1u 66Q;;1ur]   r   r  c                      |       S r4   ra   )rc   r   r   s    r/   rd   ztest_batch.<locals>.<lambda>  s    VB] r]   c                 H     j                  j                        |       S r4   rh   r  )rc   r   r   rj   s    r/   rd   ztest_batch.<locals>.<lambda>  s    fRWWRUU^Q7 r]   r  r  z&should work but doesn't - see gh-24510r  c                   (    e Zd Z fdZ fdZy)$test_batch.<locals>.MyLinearOperatorc                      |      S r4   ra   )r[   rc   r   r   s     r/   rk  z,test_batch.<locals>.MyLinearOperator._matvec      b!}$r]   c                 H     j                  j                        |      S r4   r"  )r[   rc   r   r   rj   s     r/   rp  z-test_batch.<locals>.MyLinearOperator._rmatvec      bggbeena00r]   N)r   r   r   rk  rp  )r   r   rj   s   r/   MyLinearOperatorr$        %1r]   r)  )rK   ro   rj   r  c                   (    e Zd Z fdZ fdZy)r$  c                      |      S r4   ra   )r[   r   r   r   s     r/   rM  z,test_batch.<locals>.MyLinearOperator._matmat  r&  r]   c                 H     j                  j                        |      S r4   r"  )r[   r   r   r   rj   s     r/   rz  z-test_batch.<locals>.MyLinearOperator._rmatmat  r(  r]   Nr{  )r   r   rj   s   r/   r)  z$test_batch.<locals>.MyLinearOperator  r*  r]   zDimension mismatch:...rv   z'Calling `matvec` on 'column vectors'...z(Calling `rmatvec` on 'column vectors'...zExpected at least 2-d...Tr   Fz<Incompatible shapes|size of tensor|could not be broadcast...)r7   r8   r9   r   rK   r   r   r   ro   r   r  r   r   r   rh   r  r   r   r   r   r   r    r   rE   RuntimeErrorr   )r  r  r  r  ro   rj   r'   mr  r  x_rowx_colx_matrX   r)  messagebroadcastabler   r   r   r   s      ` `            @@@r/   
test_batchr5  m  s4   " ))

 1
2CGAq!	$g$q$!$	%BJJ33t!34EJJ66t!6A67EJJ66t!6A67E"szz"((+++SZZ444SZZ444SZZ444Pr5%6OPBue6 00&&r*	 0	0$$(("((*7	
 
 0	0 	<=	 1	1	1 	1y77 	1
 288288C	 1	1	1 	1y77 	1
 288288C QYYu%5!r5vbggbeene'D
 *]]:W5 	:#AHHUO5)9	: 	: AE<B 	\\-w7 	#'QYYu-="U
(>	 
z)A	B 6QYYu%56
 +2&7QG	z	1 6QYYu%56 QYYu%5U
 6
 QYYu%5U
 6 z1IJ E	  AIrEz2AeHb5j1AEE%L"u*5E	2:.%"u*-eb5j1E	2:.%"u*-eb5j1 	"
 "6 PC
L9E 	  EAIurz2AFF5M52:6]]:-EF 	AI	 	)A-rwwuxx/@2/EFrwwuxx012775883Dr3IJQ	: 	:
	 	6 66 6" .  	"!M	"
 	 	sg   9%[A[#=%[0%[=9\
\ \)3\6[ #[-0[:=\
\\&%\&)\36\?)Wr   r   	functoolsr   	itertoolsr   r   typesr   typingr   r   r   r   r   numpy.testingr	   r
   numpyr7   scipy._lib._array_apir   r   r   r   r   r   r   r   r   r   scipy._externalr   r  scipy.sparserZ  scipy.sparse.linalg._interfacelinalg
_interfacer   scipy.sparse.linalgr   r   scipy.sparse._sputilsr   scipy._lib._gcutilsr   
pytestmarkrM   rO   r   r   r   r_  r  r  r  r  r  r  r  r  int8int16r   r  
INT_DTYPESr   rp   r  REAL_DTYPESr   r   clongdoubleCOMPLEX_DTYPESINEXACTDTYPES	ALLDTYPESr]  r  r  r  r  r  r  r  r  r5  ra   r]   r/   <module>rN     sr          1 /    3  2 2 @ ( 2!Z XXUVYY	
 45! @0M$ M$` l3^_CL CL `CLL
W& W&t5F&"
 =  
2	
2C<. 
# ggrxx2884
zz2::r}}5,,r~~>n,&	 y1  2 .,D
3 l3^_E  )	 `
)6 d+< ,<, H   %/. 1E F $45$892::r}}"=>l3^_E  XK	 ` ? : 6F 0XKr]   