
    (ph                     @   S r Sr/ SQrSSKrSSKJr  SSKrSSKJ	r	  SS	K
Jr  SS
KJrJrJrJrJrJrJr  SSKJrJrJrJr  SSKJrJr  SSKJrJrJrJ r J!r!J"r"J#r#J$r$J%r%J&r&J'r'  SSK(r( " S S\\5      r)S r*S r+S r,S r-SS jr.S r/ " S S\)\5      r0 " S S\\)5      r1g)z1A sparse matrix in COOrdinate or 'triplet' formatzrestructuredtext en)	coo_array
coo_matrixisspmatrix_coo    N)warn   )copy_if_needed   )spmatrix)	coo_tocsrcoo_todensecoo_todense_nd
coo_matveccoo_matvec_ndcoo_matmat_densecoo_matmat_dense_nd)issparseSparseEfficiencyWarning_spbasesparray)_data_matrix_minmax_mixin)upcast_char	to_nativeisshapegetdtypegetdatadowncast_intp_indexget_index_dtypecheck_shapecheck_reshape_kwargsisscalarlikeisdensec                      \ rS rSrSr\" SS5      rS/SS.S jjr\S 5       r	\	R                  S	 5       r	\S
 5       r\R                  S 5       rS r\R                  R                  \l        S0S jr\R                  R                  \l        S0S jr\R                   R                  \l        S rS1S jr\R$                  R                  \l        S2S jr\R&                  R                  \l        S3S jr\R(                  R                  \l        S4S jrS4S jrS4S jrS4S jr\R0                  R                  \l        S4S jr\R2                  R                  \l        S4S jr\R4                  R                  \l        S5S jr\R6                  R                  \l        S rS6S jrS2S jrS r S r!S r"S  r#S! r$S" r%S# r&S$ r'S% r(S& r)S' r*S( r+S7S) jr,S* r-S+ r.S, r/S4S- jr0S.r1g)8	_coo_base   coor	   A   Nmaxprintc                  ^^^ [         R                  " XUS9  T(       d  [        m[        U[        5      (       Gae  [        XR                  S9(       a  [        XR                  S9U l        U R                  [        U R                  5      S9m[        U[        S9n[	        U4S j[        [        U R                  5      5       5       5      U l        [         R"                  " / US9U l        SU l        GO Uu  pxUc4  [-        S	 U 5       5      (       a  [+        S
5      e[	        S U 5       5      n[        X R                  S9U l        U R                  U[        U R.                  5      SS9m[	        UU4S jU 5       5      U l        [1        UTUS9U l        SU l        GOc[3        U5      (       Ga  UR4                  U R4                  :X  a  T(       a  [	        S UR                   5       5      U l        UR$                  R7                  [        X15      5      U l        [        UR.                  U R                  S9U l        UR&                  U l        GOUR9                  5       n
[	        U
R                  5      U l        U
R$                  R7                  [        X:5      SS9U l        [        U
R.                  U R                  S9U l        SU l        GO3[         R:                  " U5      n[        U [<        5      (       d?  [         R>                  " U5      nUR@                  S:w  a  [)        SUR@                   S35      e[        UR.                  U R                  S9U l        Ub>  [        X R                  S9U R                  :w  a  SU SU R                   3n[+        U5      eU R                  [        U R                  5      S9mURC                  5       n[	        U4S jU 5       5      U l        [1        X   TUS9U l        SU l        [        U R                  5      S:  a!  [	        S U R                   5       5      U l        U RE                  5         g ! [(        [*        4 a  n	[)        S5      U	eS n	A	ff = f)Nr(   allow_ndmaxval)defaultc              3   N   >#    U  H  n[         R                  " / TS 9v   M     g7fdtypeNnparray).0_	idx_dtypes     D/var/www/html/venv/lib/python3.13/site-packages/scipy/sparse/_coo.py	<genexpr>%_coo_base.__init__.<locals>.<genexpr>(   s$      $G.E %'HHRy$A.E   "%r2   Tzinvalid input formatc              3   >   #    U  H  n[        U5      S :H  v   M     g7f)r   Nlenr7   idxs     r:   r;   r<   3   s     ;FS3s8q=F   z4cannot infer dimensions from zero sized index arraysc              3   |   #    U  H2  n[         R                  " [        R                  " U5      5      S -   v   M4     g7fr	   N)operatorindexr5   maxrA   s     r:   r;   r<   6   s-      "5-3c #+.."="A-3s   :<)r.   check_contentsc              3   P   >#    U  H  n[         R                  " UTTS 9v   M     g7f)copyr3   Nr4   )r7   rB   rL   r9   s     r:   r;   r<   <   s%      $806 %'HHSt9$M06s   #&rK   Fc              3   @   #    U  H  oR                  5       v   M     g 7fNrL   rA   s     r:   r;   r<   C   s     'Jks

k   rO   r   z!expected 2D array or matrix, not Dzinconsistent shapes: z != c              3   D   >#    U  H  nUR                  TS S9v   M     g7fFrO   N)astype)r7   rB   index_dtypes     r:   r;   r<   ]   s%      $806 %(JJ{J$G06s    c              3   \   #    U  H"  oR                  [        R                  S S9v   M$     g7frS   )rT   r5   int64rA   s     r:   r;   r<   c   s     XKS

288%
 @Ks   *,)#r   __init__r   
isinstancetupler   	_allow_ndr   _shape_get_index_dtyperH   r   floatranger@   coordsr5   r6   datahas_canonical_format	TypeError
ValueErroranyshaper   r   formatrT   tocooasarrayr   
atleast_2dndimnonzero_check)selfarg1rf   r3   rL   r)   
data_dtypeobjr`   er&   Mmessager9   rU   s       `        @@r:   rX   _coo_base.__init__   s   d8<!DdE""tnn5)$H 11T[[9I1J	%eU;
# $G.3C4D.E$G GHHRz:	,0)C"&KC =;F;;;( *> ? ?! "5-3"5 5E)%..I 11&9<TZZAE 2 G	 $ $806$8 8#Cd%@	,1)~~;;$++-$"''Jdkk'J"JDK $		 0 0%1F GDI"-djj4>>"RDK040I0ID-**,C"'

"3DK #0D5 QDI"-cii$.."QDK05D- JJt$!$00a(Avv{'*KAFF8ST(UVV)!''DNNK$"5>>BdkkQ$9%T[[M"R(11"333t{{;K3L# $806$8 8#AIDF	,0)t{{aXDKKXXDKm ":. C#$:;BCs   6Q Q0Q++Q0c                     U R                   S:  a  U R                  S   $ [        R                  " U R                  5      nUR                  SS9  U$ )Nr	   F)write)rk   r`   r5   
zeros_likecolsetflags)rn   results     r:   row_coo_base.rowg   s@    99q=;;r?"txx(e$    c                     U R                   S:  a  [        S5      e[        R                  " XR                  S   R
                  S9nU R                  S S U4-   U R                  SS  -   U l        g )Nr   z8cannot set row attribute of a 1-dimensional sparse arrayrw   r2   )rk   rd   r5   ri   r`   r3   )rn   new_rows     r:   r}   r~   p   s^    99q=WXX**WKKO,A,ABkk#2&'3dkk"#6FFr   c                      U R                   S   $ Nr   r`   )rn   s    r:   rz   _coo_base.colw   s    {{2r   c                     [         R                  " XR                  S   R                  S9nU R                  S S U4-   U l        g )Nr   r2   )r5   ri   r`   r3   )rn   new_cols     r:   rz   r   {   s7    **WKKO,A,ABkk#2&'3r   c                 t  ^	 [        XR                  U R                  S9n[        U5      u  pEX0R                  :X  a  U(       a  U R	                  5       $ U $ [        U R                  U R                  US9n[        U5      S:X  a*  US:X  a  [        XcS   5      nO*[        XcS   5      S S S2   nO[        R                  " XcUS9nU R                  U R                  [        U5      S9m	[        U	4S	 jU 5       5      nU(       a  U R                  R	                  5       nOU R                  nU R                  X4US
S9$ )Nr+   orderr   Cr	   r   r   r-   c              3   N   >#    U  H  n[         R                  " UTS 9v   M     g7fr1   r5   ri   )r7   cor9   s     r:   r;   $_coo_base.reshape.<locals>.<genexpr>   s     PZr2::b	:Zr=   Frf   rL   )r   rf   r[   r    rL   _ravel_coordsr`   r@   divmodr5   unravel_indexr]   rH   rZ   ra   	__class__)
rn   argskwargsrf   r   rL   flat_coords
new_coordsnew_datar9   s
            @r:   reshape_coo_base.reshape   s   D**t~~F*62 JJyy{"
 $DKK5Iu:?|#Kq:
#Kq:4R4@
))+EJJ))$++c%j)I	PZPP
 yy~~'HyyH~~x4E~NNr   c                 8  ^ Ub  US:X  a  U R                   S:X  a  [        U R                  5      m[        U4S jU R                   5       5      (       a  [        S5      eU R                  R                   S:w  d!  [        S U R                   5       5      (       a  [        S5      e[        T5      $ US:  a  XR                   -  nXR                   :  a  [        S5      e[        R                  " [        U R                  SU-
     5      U R                  SU-
     S9$ )	Nr   r	   c              3   @   >#    U  H  n[        U5      T:g  v   M     g 7frN   r?   )r7   rB   nnzs     r:   r;   $_coo_base._getnnz.<locals>.<genexpr>   s     :ks3s8s?ks   z3all index and data arrays must have the same lengthc              3   >   #    U  H  oR                   S :g  v   M     g7frE   )rk   rA   s     r:   r;   r      s     )O;C((a-;rC   z'coordinates and data arrays must be 1-Daxis out of bounds	minlength)rk   r@   ra   re   r`   rd   intr5   bincountr   rf   )rn   axisr   s     @r:   _getnnz_coo_base._getnnz   s    <DAI$))q.dii.C:dkk:::  "/ 0 0 yy~~"c)O4;;)O&O&O !JKKs8O!8IID99122{{.t{{1t8/DE%)ZZD%9; 	;r   c                 t   U R                  5         Uc   [        R                  " U R                  5      $ US:  a  XR                  -  nUS:  d  XR                  :  a  [        S5      eU R                  S:g  nU R                  SU-
     U   n[        R                  " [        U5      U R                  SU-
     S9$ )Nr   r   r	   r   )
sum_duplicatesr5   count_nonzerora   rk   rd   r`   r   r   rf   )rn   r   maskcoords       r:   r   _coo_base.count_nonzero   s    <##DII..!8IID!8tyy(122yyA~AH%d+{{.u5APTHAUVVr   c           
        ^ U R                   [        U R                  5      :w  a.  [        S[        U R                  5       SU R                    35      e[	        U R                  5       HF  u  pUR
                  R                  S:w  d  M!  [        SU SUR
                  R                   S3SS9  MH     U R                  U R                  [        U R                  5      S	9m[        U4S
 jU R                   5       5      U l        [        U R                  5      U l        U R                  S:  a  [	        U R                  5       H  u  pUR                  5       U R                  U   :  a/  [        SU SUR                  5        SU R                  U    35      eUR!                  5       S:  d  Mk  [        SU SUR!                  5        35      e   gg)z&Checks data structure for consistency z2mismatching number of index arrays for shape; got z, expected izindex array z has non-integer dtype ()   
stacklevelr-   c              3   N   >#    U  H  n[         R                  " UTS 9v   M     g7fr1   r   )r7   rB   r9   s     r:   r;   #_coo_base._check.<locals>.<genexpr>   s#      5(3 JJs)<(3r=   r   zaxis z index z exceeds matrix dimension znegative axis z index: N)rk   r@   r`   rd   	enumerater3   kindr   namer]   rH   rf   rZ   r   ra   r   min)rn   r   rB   r9   s      @r:   rm   _coo_base._check   s   99DKK(( $$'$4#5[M N N  ,FAyy~~$|A3&>syy~~>NaP !# -
 ))$++c$**o)N	 5(,5 5dii(	88a<#DKK0779

1-$uQCwswwyk B99=A&I J J779q=$~aS%LMM 1 r   c                   ^  Uc  [        T R                  5      S S S2   nO[        T [        5      (       ac  [	        US5      (       a  [        U5      T R                  :w  a  [        S5      e[        [        U5      5      T R                  :w  a  [        S5      eOUS:w  a  [        S5      e[        U 4S jU 5       5      n[        U 4S jU 5       5      nT R                  T R                  U4X2S	9$ )
Nr   __len__z"axes don't match matrix dimensionszrepeated axis in transpose)r	   r   zoSparse matrices do not support an 'axes' parameter because swapping dimensions is the only logical permutation.c              3   B   >#    U  H  nTR                   U   v   M     g 7frN   )r\   r7   r   rn   s     r:   r;   &_coo_base.transpose.<locals>.<genexpr>   s     <t!t{{1~t   c              3   B   >#    U  H  nTR                   U   v   M     g 7frN   r   r   s     r:   r;   r      s     =1Ar   r   )r_   rk   rY   r   hasattrr@   rd   setrZ   r   ra   )rn   axesrL   permuted_shapepermuted_coordss   `    r:   	transpose_coo_base.transpose   s    <#DbD)Dg&&4++s4yDII/E !EFF3t9~* !=>> +V^ 9 : : <t<<===~~tyy/:$2  ? 	?r   c                   ^	 [        XR                  S9nU R                  S:  a  [        S5      e[	        U5      S:  a  [        S5      e[	        U5      U R                  :  ap  [        U R                  U R                  5      n[        R                  " U5      n[        R                  " US U U5      U l        U R                  S U U l        Xl        g [	        U5      U R                  :  a  U R                  S [	        U5      S-
   S-   SU R                  [	        U5      -
  -  -   nU R                  U5      nUR                  S [	        U5       U l        UR                  S [	        U5       U l        [        S [!        U R                  U5       5       5      nU(       a  [        R"                  R%                  [!        U R                  U5       VVs/ s H	  u  pxXx:  PM     snn5      m	T	R'                  5       (       d8  [)        U	4S	 jU R                   5       5      U l        U R                  T	   U l        Xl        g s  snnf )
Nr+   r   zonly 1-D or 2-D input acceptedz!shape argument must be 1-D or 2-Dr	   )r   r	   c              3   .   #    U  H  u  pX:  v   M     g 7frN    )r7   oldnews      r:   r;   #_coo_base.resize.<locals>.<genexpr>  s     M6L(#CI6Ls   c              3   ,   >#    U  H	  oT   v   M     g 7frN   r   r7   rB   r   s     r:   r;   r     s     #E#I   )r   r[   rk   rd   r@   r   r`   rf   mathprodr5   r   ra   r\   r   re   ziplogical_andreduceallrZ   )
rn   rf   r   max_size	tmp_shapetmpis_truncatingrB   sizer   s
            @r:   resize_coo_base.resize   s   ENN;99q==>>u:>@AAu:		!'TZZ@Kyy'H**;y+A5IDK		)8,DIK u:		!OSZ!^,$))c%j012 
 ,,y)C**[c%j1DK))KSZ0DK Mc$**e6LMM>>((,/U,C*,Cys
,C* D 88::##E#EE IIdO	*s   H<
c                 d   U R                  X5      n[        UR                  R                  5      nU(       d&  UR                  R                  (       d  [        S5      eU R                  S:X  ab  [        [        R                  " S/5      U R                  U R                  U R                  S   U R                  UR                  S5      U5        GOGU R                  S:X  aW  U R                  u  pV[        XVU R                  U R                   U R"                  U R                  UR                  S5      U5        OU(       a9  [        R$                  " S[        R&                  " U R                  S S 5      5      nOD[        R$                  " [        R&                  " U R                  SS  S S S2   5      S S S2   S5      n[        R(                  " U R                  5      n[        XpR                  U R                  XR                  UR                  S5      U5        UR+                  U R                  5      $ )Nz&Output array must be C or F contiguousr	   r   Ar   r   )_process_toarray_argsr   flagsf_contiguousc_contiguousrd   rk   r   r5   r6   r   r`   ra   ravelrf   r   r}   rz   appendcumprodconcatenater   )	rn   r   outBfortranrs   Nstridesr`   s	            r:   toarray_coo_base.toarray  s}   &&u2agg**+qww33EFF 99>288QC=$((DII;;q>499aggclGMYY!^::DAdhh$((DIIg/ ))Arzz$**Sb/'BC))BJJtzz!"~dd/C$DTrT$JAN^^DKK0F7HHdii!99aggclGE yy$$r   c                 n   U R                   S:w  a  [        SU R                    S35      eU R                  S:X  a$  U R                  U R                  U R
                  S9$ SSKJn  U R                  UR                  5      u  p4pVU R                  XTU4US9nU R                  (       d  UR                  5         U$ )	a  Convert this array/matrix to Compressed Sparse Column format

Duplicate entries will be summed together.

Examples
--------
>>> from numpy import array
>>> from scipy.sparse import coo_array
>>> row  = array([0, 0, 1, 3, 1, 0, 0])
>>> col  = array([0, 2, 1, 3, 1, 0, 0])
>>> data = array([1, 1, 1, 1, 1, 1, 1])
>>> A = coo_array((data, (row, col)), shape=(4, 4)).tocsc()
>>> A.toarray()
array([[3, 0, 1, 0],
       [0, 2, 0, 0],
       [0, 0, 0, 0],
       [0, 0, 0, 1]])

r   z+Cannot convert. CSC format must be 2D. Got rQ   r   r2   r	   )	csc_arrayrf   )rk   rd   r   _csc_containerrf   r3   _cscr   _coo_to_compressed_swaprb   r   )rn   rL   r   indptrindicesra   rf   xs           r:   tocsc_coo_base.tocsc:  s    ( 99>J499+UVWXX88q=&&tzz&DD'+/+B+B9??+S(FT##TF$;5#IA,,  "Hr   c                    U R                   S:  a  [        SU R                    S35      eU R                  S:X  a$  U R                  U R                  U R
                  S9$ SSKJn  U R                  UR                  US9nUu  pEpgU R                  XeU4U R                  S	9nU R                  (       d  UR                  5         U$ )
a  Convert this array/matrix to Compressed Sparse Row format

Duplicate entries will be summed together.

Examples
--------
>>> from numpy import array
>>> from scipy.sparse import coo_array
>>> row  = array([0, 0, 1, 3, 1, 0, 0])
>>> col  = array([0, 2, 1, 3, 1, 0, 0])
>>> data = array([1, 1, 1, 1, 1, 1, 1])
>>> A = coo_array((data, (row, col)), shape=(4, 4)).tocsr()
>>> A.toarray()
array([[3, 0, 1, 0],
       [0, 2, 0, 0],
       [0, 0, 0, 0],
       [0, 0, 0, 1]])

r   z*Cannot convert. CSR must be 1D or 2D. Got rQ   r   r2   r	   )	csr_arrayrO   r   )rk   rd   r   _csr_containerrf   r3   _csrr   r   r   rb   r   )	rn   rL   r   arraysr   r   ra   rf   r   s	            r:   tocsr_coo_base.tocsr[  s    ( 99q=I$))TUVWW88q=&&tzz&DD',,Y__4,HF+1(FT##TF$;4::#NA,,  "Hr   c                 <   U" U R                   5      u  p4U R                  U R                  [        U R                  U5      S9nU R
                  S:X  a  U(       a  U R                  S   R                  5       OU R                  S   n[        U5      n[        R                  " SU/US9nU(       a  U R                  R                  5       OU R                  n	XXR                  4$ U" U R                  5      u  p[        U
5      nU
R                  USS9n
UR                  USS9n[        R                  " US-   US9n[        R                  " XS9n[        R                  " U R                  U R                  S9n	[!        X4XzXR                  XU	5	        XXR                  4$ )z?convert (shape, coords, data) to (indptr, indices, data, shape)r-   r	   r   r2   FrO   )_shape_as_2dr]   r`   rH   r   rk   rL   r@   r5   r6   ra   rf   rT   empty
empty_liker3   r   )rn   swaprL   rs   r   r9   r   r   r   ra   majorminors               r:   r   _coo_base._coo_to_compressed}  sG   D%%& ))$++c$((A>N)O	99>/3dkk!n))+QGg,CXXq#hi8F'+499>>#DD**44 DKK(%jYU3YU3!a%y1--7}}TYYdjj9!E99ftLjj00r   c                 4    U(       a  U R                  5       $ U $ rN   rO   )rn   rL   s     r:   rh   _coo_base.tocoo  s    99;Kr   c                    U R                   S:w  a  [        SU R                    S35      eU R                  5         U R                  U R                  -
  n[
        R                  " USS9u  p4[        U5      S:  a  [        S[        U5       S3[        SS	9  U R                  R                  S
:X  a   [
        R                  " SU R                  S9nO_[
        R                  " [        U5      U R                  R                  5       S-   4U R                  S9nU R                  XTU R                  4'   U R                  XS4U R                   S9$ )Nr   z+Cannot convert. DIA format must be 2D. Got rQ   T)return_inversed   zConstructing a DIA matrix with z diagonals is inefficientr   r   )r   r   r2   r	   r   )rk   rd   r   rz   r}   r5   uniquer@   r   r   ra   r   zerosr3   rH   _dia_containerrf   )rn   rL   ksdiagsdiag_idxra   s         r:   todia_coo_base.todia  s   99>J499+UVWXXXX ))Bt<u:23u:, ?" "(Q8
 99>>Q88F$**5D88SZ)9:$**MD'+yyD488#$""D=

"CCr   c                 j   U R                   S:  a  [        SU R                    S35      eU R                  5         U R                  U R                  U R
                  S9nU R                   S:X  a  U R                  S   nO[        U R                  6 n[        [        X0R                  5      5      Ul
        U$ )Nr   z*Cannot convert. DOK must be 1D or 2D. Got rQ   r2   r	   r   )rk   rd   r   _dok_containerrf   r3   r`   r   dictra   _dict)rn   rL   dokr`   s       r:   todok_coo_base.todok  s    99q=I$))TUVWW!!$**DJJ!?99>[[^F$++&FVYY/0	
r   c           
        ^	 U R                   S:w  a  [        S5      eU R                  u  p#X* ::  d  X:  a)  [        R                  " SU R
                  R                  S9$ [        R                  " [        U[        US5      -   U[        US5      -
  5      U R                  S9nU R                  U-   U R                  :H  m	U R                  (       a  U R                  T	   nU R
                  T	   nOB[        U	4S jU R                   5       5      nU R                  XpR
                  T	   5      u  u  pXnXdU[        US5      -   '   U$ )Nr   z diagonal requires two dimensionsr   r2   c              3   ,   >#    U  H	  oT   v   M     g 7frN   r   )r7   rB   	diag_masks     r:   r;   %_coo_base.diagonal.<locals>.<genexpr>  s     ?;CY;r   )rk   rd   rf   r5   r  ra   r3   r  r   rH   r}   rz   rb   rZ   r`   _sum_duplicates)
rn   krowscolsdiagr}   ra   indsr8   r$  s
            @r:   diagonal_coo_base.diagonal  s   99>?@@ZZ
:88ATYY__55xxD3q!9,dSAY.>?"jj*XX\dhh.	$$((9%C99Y'D?4;;??D!11$		)8LMNHSd $S3q!9_r   c                    U R                   S:w  a  [        S5      eU R                  u  p4UR                   (       a  [        U5      (       d  g U R                  R
                  nU R                  U R                  -
  U:g  nUS:  a  [        X2-   U5      nUR                   (       a  [        U[        U5      5      n[        R                  " X`R                  U:  5      n[        R                  " U* U* U-   US9n	[        R                  " XuS9n
O[        X4U-
  5      nUR                   (       a  [        U[        U5      5      n[        R                  " X`R                  U:  5      n[        R                  " XuS9n	[        R                  " X"U-   US9n
UR                   (       a  US U nO"[        R                  " XpR
                  S9nXS S & [        R                  " U R                  U   U	45      [        R                  " U R                  U   U
45      4U l        [        R                  " U R                  U   U45      U l        SU l        g )Nr   z*setting a diagonal requires two dimensionsr   r2   F)rk   rd   rf   r@   r}   r3   rz   r   r5   
logical_oraranger  r   r`   ra   rb   )rn   valuesr'  rs   r   r9   	full_keep	max_indexkeepr   r   r   s               r:   _setdiag_coo_base._setdiag  s   99>IJJzz;;s6{{HHNN	 HHtxx'1,	q5ACI{{	3v;7	==HH	,ABDiiQBN)DGii	;GAsI{{	3v;7	==HH	,ABDii	;Giiy=	BG ;;jy)Hxx	<H QK ~~txx~w&?@~~txx~w&?@BNNDIIdOX#>?	$)!r   c                     U(       a  [        S U R                   5       5      nOU R                  nU R                  X4U R                  UR                  S9$ )z|Returns a matrix with the same sparsity structure as self,
but with different data. By default the index arrays are copied.
c              3   @   #    U  H  oR                  5       v   M     g 7frN   rO   rA   s     r:   r;   '_coo_base._with_data.<locals>.<genexpr>
  s     =#88::rP   )rf   r3   )rZ   r`   r   rf   r3   )rn   ra   rL   r`   s       r:   
_with_data_coo_base._with_data  sC     ===F[[F~~tnDJJdjj~QQr   c                     U R                   (       a  gU R                  U R                  U R                  5      nUu  U l        U l        SU l         g)zUEliminate duplicate entries by adding them together

This is an *in place* operation
NT)rb   r&  r`   ra   )rn   summeds     r:   r   _coo_base.sum_duplicates  s@    
 $$%%dkk499=!'TY$(!r   c           	        ^^ [        U5      S:X  a  X4$ [        R                  " US S S2   5      m[        U4S jU 5       5      nUT   n[        R                  R                  U Vs/ s H  o3SS  US S :g  PM     sn5      m[        R                  " ST5      m[        U4S jU 5       5      n[        R                  " T5      u  n[        R                  R                  U[        U5      U R                  S9nX4$ s  snf )Nr   r   c              3   ,   >#    U  H	  oT   v   M     g 7frN   r   )r7   rB   r   s     r:   r;   ,_coo_base._sum_duplicates.<locals>.<genexpr>"  s     4Vc5zVr   r	   Tc              3   ,   >#    U  H	  oT   v   M     g 7frN   r   )r7   rB   unique_masks     r:   r;   rA  (  s     :6C;'6r   r2   )r@   r5   lexsortrZ   r/  r   r   rl   addreduceatr   r3   )rn   r`   ra   rB   unique_indsr   rC  s        @@r:   r&  _coo_base._sum_duplicates  s    t9>< 

6$B$<(4V44E{mm**+1,
+1CGs3Bx6,
  iik2:6::zz+.vvt%8%ETZZX|,
s   (C<c                    ^ U R                   S:g  mU R                   T   U l         [        U4S jU R                   5       5      U l        g)zKRemove zero entries from the array/matrix

This is an *in place* operation
r   c              3   ,   >#    U  H	  oT   v   M     g 7frN   r   r   s     r:   r;   ,_coo_base.eliminate_zeros.<locals>.<genexpr>4  s     =#Ir   N)ra   rZ   r`   )rn   r   s    @r:   eliminate_zeros_coo_base.eliminate_zeros-  s7    
 yyA~IIdO	===r   c                    UR                   U R                   :w  a&  [        SU R                    SUR                    S35      e[        U R                  R                  UR                  R                  5      n[
        R                  " XSS9n[        UR                  R                  5      nU R                  S:X  ab  [        [
        R                  " S/5      U R                  U R                  U R                  S   U R                  UR                  S5      U5        GOGU R                  S	:X  aW  U R                   u  pV[#        XVU R                  U R$                  U R&                  U R                  UR                  S5      U5        OU(       a9  [
        R(                  " S[
        R*                  " U R                   S S
 5      5      nOD[
        R(                  " [
        R*                  " U R                   SS  S S S
2   5      S S S
2   S5      n[
        R,                  " U R                  5      n[        XpR                  U R                  XR                  UR                  S5      U5        U R/                  USS9$ )NIncompatible shapes ( and r   T)r3   rL   r	   r   r   r   r   FrO   )rf   rd   r   r3   charr5   r6   r   r   r   rk   r   r   r`   ra   r   r  r   r}   rz   r   r   r   
_container)	rn   otherr3   r|   r   rs   r   r   r`   s	            r:   
_add_dense_coo_base._add_dense:  s   ;;$**$4TZZLekk]RSTUUDJJOOU[[-=-=>%48fll//099>288QC=$((DII;;q>499fll36GRYY!^$$DAdhh$((DIIS)74 ))Arzz$**Sb/'BC))BJJtzz!"~dd/C$DTrT$JAN^^DKK0F7HHdii!99fll3.?JvE22r   c                    U R                   S:  a  U R                  5       R                  U5      $ UR                  U R                  :w  a&  [	        SU R                   SUR                   S35      eU R                  U5      n[        R                  " U R                  UR                  45      n[        [        R                  " U R                  UR                  4SS95      nU R                  X#4U R                  S9nU$ Nr   rO  rP  r   r	   )r   r   )rk   r  _add_sparserf   rd   r   r5   r   ra   rZ   r`   rn   rS  r   r   r   s        r:   rX  _coo_base._add_sparseR  s    99q=::<++E22;;$**$4TZZLekk]RSTUUu%>>499ejj"9:2>>4;;*EANO
NNH1NDr   c                    U R                   S:  a  U R                  5       R                  U5      $ UR                  U R                  :w  a&  [	        SU R                   SUR                   S35      eU R                  U5      n[        R                  " U R                  UR                  * 45      n[        [        R                  " U R                  UR                  4SS95      n[        X#4U R                  S9nU$ rW  )rk   r  _sub_sparserf   rd   r   r5   r   ra   rZ   r`   r   rY  s        r:   r\  _coo_base._sub_sparse_  s    99q=::<++E22;;$**$4TZZLekk]RSTUUu%>>499uzzk":;2>>4;;*EANO
x,DJJ?r   c           	         U R                   S:  Ga;  [        R                  " [        R                  " U R
                  S S 5      [        U R                  R                  UR                  R                  5      S9n[        R                  " U R
                  5      n[        R                  " [        R                  " US S S S S2   5      S S S2   SS  S5      n[        R                  " U R                  5      n[        U R                  [!        U R
                  5      XEU R"                  X5        UR%                  U R
                  S S 5      nU$ U R                   S:  a  U R
                  S   OSn[        R                  " U[        U R                  R                  UR                  R                  5      S9nU R                   S:X  a  U R&                  nU R(                  nONU R                   S:X  a&  U R                  S   n[        R*                  " U5      nO[-        SU R                    35      e[/        U R                  XU R"                  X5        [1        U [2        5      (       a  US:X  a  US   $ U$ )Nr   r   r2   r	   r   z$coo_matvec not implemented for ndim=)rk   r5   r  r   r   rf   r   r3   rQ  r6   r   r   r   r`   r   r   r@   ra   r   rz   r}   ry   NotImplementedErrorr   rY   r   )	rn   rS  r|   rf   r   r`   result_shaperz   r}   s	            r:   _matmul_vector_coo_base._matmul_vectorl  s   99q=XXdii

3B8$/

AQAQ$RTFHHTZZ(Eii

5":dd+; <TrT B12 FJG^^DKK0F$((C

OWdii) ^^DJJsO4FM )-		Atzz!}1, +DJJOOU[[=M=M NP99>((C((CYY!^++a.C--$C%6tyykBD D 	488Styy%@dG$$):!9r   c                 (   [        U5      (       a  U R                  U5      $  UR                  n[        [        U5      S S 5      [        [        U5      SS  S S S2   5      -   nUR                  U5      nU R                  n[        [        U5      S S 5      [        [        U5      SS  S S S2   5      -   nU R                  U5      R                  U5      nU[        L a  [        $ US:X  d  US:X  a  [        UR                  5      nOI[        [        UR                  5      S S 5      [        [        UR                  5      SS  S S S2   5      -   nUR                  U5      $ ! [         a&    [        R
                  " U5      nUR                  n GNaf = f)Nrw   r   r	   )r!   _mul_scalarrk   AttributeErrorr5   ri   rZ   r_   r   _matmul_dispatchNotImplemented)rn   rS  o_ndimpermtrs_ndimrets          r:   _rmatmul_dispatch_coo_base._rmatmul_dispatch  sa   ##E**$ vs+,uU6]235G"5M/NND&BYYFvs+,uU6]235G"5M/NND..&77;Cn$%%{fkSXXU388_Sb12U5?23;OPTRTPT;U5VV==&&! " $

5)$s   E! !,FFc                    [        U5      (       a  U R                  U5      $ [        U5      (       dg  [        U5      (       dW  [        R
                  " U5      nUR                  S:X  a$  UR                  [        R                  :X  a  [        $  UR                    U R                  S:  a&  UR                  S:  a  [        R                  " X5      $ U R                  S   nSnUR                  [        R                  L Ga&  UR                  U4:X  a  U R!                  U5      $ UR                  US4:X  a@  U R!                  UR#                  5       5      nUR$                  " / U R                  S S QSP76 $ UR                  S:X  a#  U SU SUR                  S    S3n['        U5      eUR                  S	   U:X  aK  U R                  S S	 nUR                  S S	 nXx:w  a   [        R(                  " Xx5        U R+                  U5      $ ['        U SU SUR                  S	    S35      e[        U5      (       a  U R-                  U5      $ [        U5      (       Gas  U R                  S:H  n	UR                  S:H  n
U	(       a  U R%                  U R.                  5      n U
(       a   UR%                  UR                  S   S45      nX1R                  S	   :w  a!  ['        U SU SUR                  S	    S35      eU R                  S:  d  UR                  S:  a:  U R                  S S	 nUR                  S S	 nXx:w  a   [        R(                  " Xx5        U R1                  U5      nU	(       a@  UR%                  [3        UR                  S S	 5      [3        UR                  SS  5      -   5      nU
(       a  UR%                  UR                  S S 5      nU$ g ! [         a    Un GN@f = f! [&         a    ['        S
5      ef = f! [&         a    ['        S
5      ef = f)Nr   r   r   z)matmul: dimension mismatch with signaturer	   z (n,k=z),(k=z,)->(n,)rw   z&Batch dimensions are not broadcastablez	 (n,..,k=z,..,m)->(n,..,m)r   )r!   multiplyr   r"   r5   
asanyarrayrk   r3   object_rg  rf   re  r   rf  r   ndarrayra  r   r   rd   broadcast_shapes_matmul_multivectorrd  r  _matmul_sparserZ   )rn   rS  other_ar   
err_prefixr|   msgbatch_shape_Abatch_shape_B
self_is_1dother_is_1ds              r:   rf  _coo_base._matmul_dispatch  s   ==''75>>mmE*G||q W]]bjj%@ &%  99q=UZZ!^++D88JJrN@
??bjj({{qd"**511{{q!f$,,U[[];~~:tzz#2:::zzQ#F1#U5;;q>2B(K o%{{2!# $

3B %CR 0 1S++MI //66 !l)A3eEKKO3DDTU 
 ##E**E??aJ**/K ||D$5$56u{{1~q&9: KKO# !l)A3eEKKO3DDTU  yy1}

Q $

3B %CR 0 1S++MI ((/F fll3B.?(@(-fll23.?(@)A BSb(9:MM O "   2 & S()QRRSP & S()QRRSs*   O
 O 4O6 
OOO36Pc                 6   [        U R                  R                  UR                  R                  5      nU R                  S:  d  UR                  S:  Ga  U R                  S:X  an  U R	                  SU R
                  S   5      R                  U5      nUR	                  [        UR
                  S S 5      [        UR
                  SS  5      -   5      $ [        R                  " U R
                  S S UR
                  S S 5      nX@R
                  SS  -   nXAR
                  SS  -   nU R                  U5      n [        R                  " X5      nX@R
                  SS -   UR
                  SS  -   n[        R                  " XrS9n[        U R                  [        U R
                  5      UR
                  S   [        R                   " U5      [        R                   " U5      [        R"                  " U R$                  5      U R&                  UR)                  S5      U5	        U$ U R                  S:X  a7  U R
                  S   UR
                  S   4nU R*                  nU R,                  n	OEU R                  S:X  a5  UR
                  S   4nU R$                  S   n[        R.                  " U5      n	[        R                  " WUS9n[1        U R                  UR
                  S   W	WU R&                  UR)                  S5      U5        UR3                  [5        U5      S	9$ )
Nr   r	   r   rw   r   r2   r   r   )type)r   r3   rQ  rk   r   rf   ru  rZ   r5   rt  _broadcast_tobroadcast_tor  r   r   r@   r6   r   r`   ra   r   rz   r}   ry   r   viewr  )
rn   rS  result_dtyper|   broadcast_shape
self_shapeother_shaper`  rz   r}   s
             r:   ru  _coo_base._matmul_multivector  s\   "4::??EKK4D4DE99>UZZ1_yyA~aA7KKER~~eEKK,<&=ekkRTRUFV@W&WXX 11$**Sb/5;;sPRCSTO(::bc?:J)KK,<<K%%j1DOOE7E*ZZ2->>RSAQQLXXl?F#djj/5;;r? " 5rxx7M "t{{ ; $		5;;s+;VE M99> JJqM5;;q>:L((C((CYY!^!KKN,L++a.C--$C,l;5;;r?CEKK$4f	>{{U{,,r   c                 |   [        U5      (       d  [        U5      (       dy  [        U5      (       di  [        R                  " U5      nUR
                  S:X  a6  UR                  [        R                  :X  a  [        S[        U5       S35      e UR                    U R
                  S:  aA  [        R                  " U5      (       d  UR
                  S:  a  [        R                  " X5      $ [        R                  " U5      (       a  X-  $ [        U5      (       a  U R                  U5      $ UR                   S:w  a  [        S5      eU R
                  S:X  aZ  UR
                  S:X  aJ  U R                  S   UR                  S   :w  a&  [#        SU R                   S	UR                   S
35      eX-  $ U R
                  S:X  aZ  UR
                  S:X  aJ  U R                  S   UR                  S   :w  a&  [#        SU R                   S	UR                   S35      eX-  $ U R%                  U5      $ ! [         a    Un GNf = f)ah  Return the dot product of two arrays.

Strictly speaking a dot product involves two vectors.
But in the sense that an array with ndim >= 1 is a collection
of vectors, the function computes the collection of dot products
between each vector in the first array with each vector in the
second array. The axis upon which the sum of products is performed
is the last axis of the first array and the second to last axis of
the second array. If the second array is 1-D, the last axis is used.

Thus, if both arrays are 1-D, the inner product is returned.
If both are 2-D, we have matrix multiplication. If `other` is 1-D,
the sum product is taken along the last axis of each array. If
`other` is N-D for N>=2, the sum product is over the last axis of
the first array and the second-to-last axis of the second array.

Parameters
----------
other : array_like (dense or sparse)
    Second array

Returns
-------
output : array (sparse or dense)
    The dot product of this array with `other`.
    It will be dense/sparse if `other` is dense/sparse.

Examples
--------

>>> import numpy as np
>>> from scipy.sparse import coo_array
>>> A = coo_array([[1, 2, 0], [0, 0, 3], [4, 0, 5]])
>>> v = np.array([1, 0, -1])
>>> A.dot(v)
array([ 1, -3, -1], dtype=int64)

For 2-D arrays it is the matrix product:

>>> A = coo_array([[1, 0], [0, 1]])
>>> B = coo_array([[4, 1], [2, 2]])
>>> A.dot(B).toarray()
array([[4, 1],
       [2, 2]])

For 3-D arrays the shape extends unused axes by other unused axes.

>>> A = coo_array(np.arange(3*4*5*6)).reshape((3,4,5,6))
>>> B = coo_array(np.arange(3*4*5*6)).reshape((5,4,6,3))
>>> A.dot(B).shape
(3, 4, 5, 5, 4, 3)
r   z"dot argument not supported type: ''r   r&   z input must be a COO matrix/arrayr	   shapes rP  z" are not aligned for inner productr   z are not aligned for matmul)r   r"   r!   r5   rq  rk   r3   rr  rc   r  rf   re  isscalarr   dot
_dense_dotrg   rd   _sparse_dot)rn   rS  o_arrays      r:   r  _coo_base.dot+  s   j 75>>\%5H5HmmE*G||q W]]bjj%@"DT%[MQR STT  99q=bkk%00EJJqL;;t++;;u<5>>??5))\\U">??YY!^

azz!}A. 74::,eEKK=!E"F G G<YY!^

azz!}A. 74::,eEKK=!>"? @ @<##E**3 "   s   H+ +H;:H;c                    U R                   S:H  nUR                   S:H  nU(       a  U R                  U R                  5      n U(       a   UR                  UR                  S   S45      nU R                  S   UR                  S   :w  a&  [	        SU R                   SUR                   S35      e[        U R                  U R                  U R                   S-
  /5      n[        UR                  UR                  UR                   S-
  /5      nU R                  S S nUR                  S S UR                  SS  -   n[        R                  " U5      U R                  S   4nUR                  S   [        R                  " U5      4n	X@R                  S   4n
UR                  S   U4n[        U R                  U
4U5      n[        UR                  U4U	5      nX-  R                  5       nXg-   nXg4n/ n[        UR                  U5       H,  u  nnUR                  [        R                  " UU5      5        M.     [        UR                  U4U5      nU(       a  UR                  USS  5      nU(       a  UR                  US S 5      nU$ )	Nr	   r   r   rw   r  rP   are not aligned for n-D dotr   )rk   r   r  rf   rd   _ravel_non_reduced_axesr`   r   r   r   ra   rh   r   extendr5   r   )rn   rS  r|  r}  self_raveled_coordsother_raveled_coordsself_nonreduced_shapeother_nonreduced_shaperavel_coords_shape_selfravel_coords_shape_otherself_2d_coordsother_2d_coordsself_2dother_2dr   combined_shapeshapesprod_coordscsprod_arrs                        r:   r  _coo_base._sparse_dot  sT   YY!^
jjAo << 1 12DMM5;;q>1"56E::b>U[[_, 74::,eEKK=!?"@ A A
 6dkk6:jj499Q;-Q6u||7<{{UZZPQ\N T !%

3B!&Sb!1EKK4D!D $(99-B#CTZZPR^"T$)KKOTYY?U5V#W -{{2? <<+-ABTYY79PQejj/:<TU"))+ /G (@V,DAqr//156 - dii5~F ''qr(:;H''s(;<Hr   c                 X   U R                   S:H  nUR                   S:H  nU(       a  U R                  U R                  5      n U(       a   UR                  UR                  S   S45      nU R                  S   UR                  S   :w  a&  [	        SU R                   SUR                   S35      eU R                  S S S[        UR                  5      S-
  -  -   U R                  SS  -   nS[        U R                  5      S-
  -  UR                  -   nU R                  S S UR                  S S -   UR                  SS  -   nU R                  U5      UR                  U5      -  nUR                  U5      nU(       a  UR                  USS  5      nU(       a  UR                  US S 5      nU$ )	Nr	   r   r   rw   r  rP  r  r   )rk   r   r  rf   rd   r@   )	rn   rS  r|  r}  new_shape_selfnew_shape_otherr`  r|   r  s	            r:   r  _coo_base._dense_dot  s   YY!^
jjAo << 1 12DMM5;;q>1"56E::b>U[[_, 74::,eEKK=!?"@ A A JJsOdc%++&6&:;;djjoM 	 #djj/A"56Dzz#2Sb)99EKK<LLn-o0NN>>,/ ''QR(89H''Sb(9:Hr   c                 H  ^ ^ [        T5      (       dy  [        T5      (       di  [        R                  " T5      nUR                  S:X  a6  UR
                  [        R                  :X  a  [        S[        T5       S35      e TR                    [        T R                  TR                  U5      u  pE[        UU 4S j[        XE5       5       5      (       a  [        S5      e[        T5      (       a  T R                  TXE5      $ T R!                  TXE5      $ ! [         a    Um Nf = f)a4
  Return the tensordot product with another array along the given axes.

The tensordot differs from dot and matmul in that any axis can be
chosen for each of the first and second array and the sum of the
products is computed just like for matrix multiplication, only not
just for the rows of the first times the columns of the second. It
takes the dot product of the collection of vectors along the specified
axes.  Here we can even take the sum of the products along two or even
more axes if desired. So, tensordot is a dot product computation
applied to arrays of any dimension >= 1. It is like matmul but over
arbitrary axes for each matrix.

Given two tensors, `a` and `b`, and the desired axes specified as a
2-tuple/list/array containing two sequences of axis numbers,
``(a_axes, b_axes)``, sum the products of `a`'s and `b`'s elements
(components) over the axes specified by ``a_axes`` and ``b_axes``.
The `axes` input can be a single non-negative integer, ``N``;
if it is, then the last ``N`` dimensions of `a` and the first
``N`` dimensions of `b` are summed over.

Parameters
----------
a, b : array_like
    Tensors to "dot".

axes : int or (2,) array_like
    * integer_like
      If an int N, sum over the last N axes of `a` and the first N axes
      of `b` in order. The sizes of the corresponding axes must match.
    * (2,) array_like
      A 2-tuple of sequences of axes to be summed over, the first applying
      to `a`, the second to `b`. The sequences must be the same length.
      The shape of the corresponding axes must match between `a` and `b`.

Returns
-------
output : coo_array
    The tensor dot product of this array with `other`.
    It will be dense/sparse if `other` is dense/sparse.

See Also
--------
dot

Examples
--------
>>> import numpy as np
>>> import scipy.sparse
>>> A = scipy.sparse.coo_array([[[2, 3], [0, 0]], [[0, 1], [0, 5]]])
>>> A.shape
(2, 2, 2)

Integer axes N are shorthand for (range(-N, 0), range(0, N)):

>>> A.tensordot(A, axes=1).toarray()
array([[[[ 4,  9],
         [ 0, 15]],
<BLANKLINE>
        [[ 0,  0],
         [ 0,  0]]],
<BLANKLINE>
<BLANKLINE>
       [[[ 0,  1],
         [ 0,  5]],
<BLANKLINE>
        [[ 0,  5],
         [ 0, 25]]]])
>>> A.tensordot(A, axes=2).toarray()
array([[ 4,  6],
       [ 0, 25]])
>>> A.tensordot(A, axes=3)
array(39)

Using tuple for axes:

>>> a = scipy.sparse.coo_array(np.arange(60).reshape(3,4,5))
>>> b = np.arange(24).reshape(4,3,2)
>>> c = a.tensordot(b, axes=([1,0],[0,1]))
>>> c.shape
(5, 2)
>>> c
array([[4400, 4730],
       [4532, 4874],
       [4664, 5018],
       [4796, 5162],
       [4928, 5306]])

r   z#tensordot arg not supported type: 'r  c              3   f   >#    U  H&  u  pTR                   U   TR                   U   :g  v   M(     g 7frN   r   )r7   axbxrS  rn   s      r:   r;   &_coo_base.tensordot.<locals>.<genexpr>C  s/      9762 zz"~R07s   .1z*sizes of the corresponding axes must match)r"   r   r5   rq  rk   r3   rr  rc   r  rf   re  _process_axesre   r   rd   _dense_tensordot_sparse_tensordot)rn   rS  r   other_array	axes_self
axes_others   ``    r:   	tensordot_coo_base.tensordot  s    r u~~huoo--.K1$):):bjj)H"Ed5k]RS TUU$ !.diiT J	  9 79 9 9IJJ5>>((	FF))%GG " $#$s    D D! D!c                   ^ ^^^ [        T R                  5      n[        TR                  5      n[        T R                  T R                  T5      n[        R
                  " T Vs/ s H  nT R                  U   PM     snT Vs/ s H  nT R                  U   PM     sn5      n[        TR                  TR                  T5      n	[        R
                  " T V
s/ s H  n
TR                  U
   PM     sn
T V
s/ s H  n
TR                  U
   PM     sn
5      n[        UU 4S j[        U5       5       5      n[        UU4S j[        U5       5       5      n[        R                  " U5      [        R                  " T Vs/ s H  nT R                  U   PM     sn5      4n[        R                  " T Vs/ s H  nTR                  U   PM     sn5      [        R                  " U5      4nXh4nX4n[        T R                  U4U5      n[        TR                  U4U5      nUU-  R                  5       nX-   n/ n[        UR                  X45       H5  u  nnU(       d  M  UR                  [        R                  " UU5      5        M7     U/ :X  a  [!        UR                  5      $ [        UR                  U4US9$ s  snf s  snf s  sn
f s  sn
f s  snf s  snf )Nc              3   R   >#    U  H  nUT;  d  M  TR                   U   v   M     g 7frN   r   )r7   r  r  rn   s     r:   r;   ._coo_base._sparse_tensordot.<locals>.<genexpr>]  s*      &6?O!#9!4 '5djjn?O   
''c              3   R   >#    U  H  nUT;  d  M  TR                   U   v   M     g 7frN   r   )r7   r  r  rS  s     r:   r;   r  _  s*      '8AR2"$J"6 (7u{{2ARr  r   )r@   rf   r  r`   r5   ravel_multi_indexrZ   r_   r   r   r   ra   rh   r   r  r   sum)rn   rS  r  r  	ndim_self
ndim_otherself_non_red_coordsr  self_reduced_coordsother_non_red_coordsaother_reduced_coordsr  r  r  r  r  r  r  r  r   r  r`   r  r  s   ````                     r:   r  _coo_base._sparse_tensordotM  sv   

O	%
 6dkk4::6?A 22'01yT[[_y1Y3WYrDJJrNY3WY6u||U[[7A C!33&01jU\\!_j1J3WJqEKKNJ3W 
 !& &6uY?O &6 !6!& '8zAR '8 "8 $(99-B#C $		I*NIb4::b>I*N O#Q$(II.T2u{{2.T$U$(II.D$E$G  .C/FTYY79PQejj/:<TU ("))+ /G &;%TUDAqqb..q!45 V R<tyy>! $))V,NCCQ 23W 23W +O.Ts$   $J9J>
K7K
>K
3Kc                 V   [        U R                  5      n[        UR                  5      n[        U5       Vs/ s H  ofU;  d  M
  UPM     nnU Vs/ s H  oR                  U   PM     n	nU Vs/ s H  oR                  U   PM     n
n[        U5       Vs/ s H  nXc;  d  M
  UPM     nnU Vs/ s H  oR                  U   PM     nnU Vs/ s H  oR                  U   PM     nnXr-   nUS S U-   USS  -   nU R                  U5      n [        R                  " X5      n/ U
Q[
        R                  " U	5      P7n/ US S Q[
        R                  " U5      PUSS  Q7nU R                  U5      R                  UR                  U5      5      $ s  snf s  snf s  snf s  snf s  snf s  snf r   )	r@   rf   r_   r   r5   r   r   r   r  )rn   rS  r  r  r  r  r  non_reduced_axes_selfr  reduced_shape_selfnon_reduced_shape_selfnon_reduced_axes_otherreduced_shape_othernon_reduced_shape_otherpermute_selfpermute_otherreshape_selfreshape_others                     r:   r  _coo_base._dense_tensordot  s   

O	%
.3I.> V.>IBU.> V5>?YjjmY?9N!O9NA**Q-9N!O/4Z/@ ";/@%'%9 #%/@ ";7ABz!{{1~zB;Q"R;Qa;;q>;Q"R,8"3B'*47Mbc7RR 	 ~~l+U2O/O;M1NO71#26 7		BU8V 7057 ||L)--emmM.JKK) !W?!O";B"Rs/   	FFF/F	F#F/F!F&c                    U R                   S:  a&  UR                   S:  a  [        R                  " X5      $ U R                  nUR                  n[        R
                  " USS USS 5      n[        U5      USS -   n[        U5      USS -   nU R                  U5      nUR                  U5      n[        U5      n	[        U5      n
X-  R                  5       n[        U/ UQU R                  S   PUR                  S   P7S9$ )a  
Perform sparse-sparse matrix multiplication for two n-D COO arrays.
The method converts input n-D arrays to 2-D block array format,
uses csr_matmat to multiply them, and then converts the
result back to n-D COO array.

Parameters:
self (COO): The first n-D sparse array in COO format.
other (COO): The second n-D sparse array in COO format.

Returns:
prod (COO): The resulting n-D sparse array after multiplication.
r   Nrw   r   r   )rk   r   rv  rf   r5   rt  rZ   r  _block_diagrh   _extract_block_diag)rn   rS  r  r  r  self_new_shapeother_new_shapeself_broadcastedother_broadcastedself_block_diagother_block_diagprod_block_diags               r:   rv  _coo_base._matmul_sparse  s    99q=UZZ!^))$66 ZZ
kk --j"o{3B?OP/*RS/A0;rs3CC--n=!//@ &&67&'89 +=DDF #EOETZZ^EU[[_E
 	
r   c           	      h   U R                   U:X  a  U(       a  U R                  5       $ U $ U R                   n[        U5      [        U5      :  a  [        S5      eS[        U5      [        U5      -
  -  [	        U5      -   n[        S [        XA5       5       5      (       a  [        SU S35      eU R                  U5      n [        U R                  [        U5      S9nU R                  nU R                  nUSS  nSn	US   US   :w  aV  US   n
X-  n	[        R                  " Xz5      n[        R                  " [        R                  " S	XS
9U R                   5      nU4n[#        S[        U5      S-   * S5       H  nXL   X   :w  a  X   n
X-  n	[        U5      n[        R                  " Xz5      n[	        [        R                  " XS-   S  U
5      5      n[        R                  " [        R                  " S	XS
9U5      nU4U-   nM  [        R                  " Xl   U	5      nU4U-   nM     [%        Xx4U5      $ )NzDNew shape must have at least as many dimensions as the current shaper   c              3   F   #    U  H  u  pUS :g  =(       a    X:g  v   M     g7frE   r   )r7   ons      r:   r;   *_coo_base._broadcast_to.<locals>.<genexpr>  s"     E/DtqQ!16!/Ds   !zcurrent shape z- cannot be broadcast to new shape {new_shape}r-   r   r	   r   r2   rw   )rf   rL   r@   rd   rZ   re   r   r   r   r`   rH   ra   r5   tilerepeatr0  r   r_   r   )rn   	new_shaperL   	old_shaperf   r9   r`   r   r   
cum_repeatrepeat_countnew_dimr   r   s                 r:   r  _coo_base._broadcast_to  s   ::""&499;0D0JJ	 y>C	N* 5 6 6 IY785;KK Es5/DEEE~i[ 9B B C C ||E"#DKKIG	99BC[

9	"%$R=L&Jwwx6Hii		!\ KTXXVG!JrSZ\?B/Ax9<'(|*
(m 778:"277:cd+;\#JK
 ))BIIa$OQTU%Z*4
 ''&)Z8%Z*4
! 0$ (/;;r   )r\   r`   ra   rb   )NNFrN   )NF)returnN)NN)F)r   )T)r   )2__name__
__module____qualname____firstlineno___formatr_   r[   rX   propertyr}   setterrz   r   r   __doc__r   r   rm   r   r   r   r   r  r   rh   r  r   r,  r   r5  r:  r   r&  rL  rT  rX  r\  ra  rm  rf  ru  r  r  r  r  r  r  rv  r  __static_attributes__r   r   r:   r$   r$      s   GaIGT GR   	ZZG G   	ZZ4 4!OF oo--GO;( oo--GOW $1199MN2?$  ))11I$L ^^++FN%2 oo--GOB D16 MM))EMD, MM))EM MM))EM( $,,44H$*NR	)&>30

D'4\~!-HV+r7r>nHb1DhL6(
V5<r   r$   c                    U R                   S:  a  [        S5      e[        R                  " U R                  SS 5      nU R                  S   nU R                  S   nU R                  XU45      nUR                  S   UR                  S   UR                  S   -  -   UR                  S   UR                  S   UR                  S   -  -   4nX-  X-  4n[        U R                  [        U5      4US9$ )	z
Converts an N-D COO array into a 2-D COO array in block diagonal form.

Parameters:
self (coo_array): An N-Dimensional COO sparse array.

Returns:
coo_array: A 2-Dimensional COO sparse array in block diagonal form.
r   zarray must have atleast dim=2Nrw   r   r	   r   r   )
rk   rd   r   r   rf   r   r`   r   ra   rZ   )rn   
num_blocksn_coln_rowres_arrr   r  s          r:   r  r    s     yy{8994::cr?+JJJrNEJJrNEllJu56GqGNN1-a0@@@qGNN1-a0@@@J
 #Z%78Idiiz!239EEr   c                 `   US   US   p2U R                   nU R                  U R                  pe[        R                  " [        U5      U R                  4[        S9nXR-  US'   Xc-  US'   XR-  n[        [        U5      S-
  SS5       H  n	X   n
X-  Xy'   X-  nM     [        U[        U5      4US9$ )Nrw   r   r2   r   r   )ra   r}   rz   r5   r  r@   r   r   r_   r   rZ   )rn   rf   r  r  ra   r}   rz   r   temp_block_idxr   r   s              r:   r  r    s    9eBi5 99Dxx 3u:txx0<J [JrN[JrN \N3u:>2r*x&-
'/ + dE*-.e<<r   c                   ^ ^ [        U[        5      (       aN  US:  d  U[        T T5      :  a  [        S5      e[	        [        T U-
  T 5      5      n[	        [        U5      5      nO[        U[        [        45      (       a  [        U5      S:w  a  [        S5      eUu  p4[        U5      [        U5      :w  a  [        S5      e[        U 4S jU 5       5      (       d  [        U4S jU 5       5      (       a  [        S5      eO[        S	5      eU Vs/ s H  oUS
:  a  UT -   OUPM     nnU Vs/ s H  oUS
:  a  UT-   OUPM     nnX44$ s  snf s  snf )Nr	   z.axes integer is out of bounds for input arraysr   z%axes must be a tuple/list of length 2z,axes lists/tuples must be of the same lengthc              3   F   >#    U  H  oT:  =(       d    UT* :  v   M     g 7frN   r   )r7   r  ndim_as     r:   r;    _process_axes.<locals>.<genexpr>=  !     =fV|+rVG|+f   !c              3   F   >#    U  H  oT:  =(       d    UT* :  v   M     g 7frN   r   )r7   r  ndim_bs     r:   r;   r  >  r   r  z/axes indices are out of bounds for input arraysz3axes must be an integer or a tuple/list of integersr   )
rY   r   r   rd   listr_   rZ   r@   re   rc   )r  r  r   axes_aaxes_br   s   ``    r:   r  r  1  s3   $!8tc&&11MNNeFTM623eDk"	D5$-	(	(t9>DEEv;#f+%KLL=f====f===NOO > MNN>DEfdaxdVmT1fFE>DEfdaxdVmT1fFE> FEs   E-Ec                 0  ^  [        U5      n[        U5       Vs/ s H  oDU;  d  M
  UPM     nnU(       d  [        R                  " T S   5      $ U Vs/ s H  oAU   PM	     nn[	        U 4S jU 5       5      n[        R
                  " Xv5      $ s  snf s  snf )Nr   c              3   .   >#    U  H
  nTU   v   M     g 7frN   r   )r7   rB   r`   s     r:   r;   *_ravel_non_reduced_axes.<locals>.<genexpr>T  s     G6Fsvc{6Fs   )r@   r_   r5   ry   rZ   r  )r`   rf   r   rk   r  non_reduced_axesnon_reduced_shapenon_reduced_coordss   `       r:   r  r  H  s    u:D%*4[C[rdN[C}}VAY'' .>>-=rr-=> G6FGG  2FF D ?s   	BBBc                    [        U 5      S:X  a  U S   $ [        U 5      S:X  a  Uu  p4U u  pVUS:X  aC  U[        SUS-
  5      -  [        SUS-
  5      -   n[        US9n[        R                  " XEUS9U-   $ US:X  aC  U[        SUS-
  5      -  [        SUS-
  5      -   n[        US9n[        R                  " X6US9U-   $ [        S5      e[        R                  " XUS	9$ )
z;Like np.ravel_multi_index, but avoids some overflow issues.r	   r   r   r   r-   r2   Fz'order' must be 'C' or 'F'r   )r@   rH   r   r5   rp  rd   r  )	r`   rf   r   nrowsncolsr}   rz   r.   r9   s	            r:   r   r   Z  s    
6{aay
6{aC<c!UQY//#a2CCF'v6I;;u;cAAc\c!UQY//#a2CCF'v6I;;u;cAA9::U;;r   c                 "    [        U [        5      $ )a  Is `x` of coo_matrix type?

Parameters
----------
x
    object to check for being a coo matrix

Returns
-------
bool
    True if `x` is a coo matrix, False otherwise

Examples
--------
>>> from scipy.sparse import coo_array, coo_matrix, csr_matrix, isspmatrix_coo
>>> isspmatrix_coo(coo_matrix([[5]]))
True
>>> isspmatrix_coo(coo_array([[5]]))
False
>>> isspmatrix_coo(csr_matrix([[5]]))
False
)rY   r   )r   s    r:   r   r   o  s    . a$$r   c                       \ rS rSrSrSrg)r   i  a  
A sparse array in COOrdinate format.

Also known as the 'ijv' or 'triplet' format.

This can be instantiated in several ways:
    coo_array(D)
        where D is an ndarray

    coo_array(S)
        with another sparse array or matrix S (equivalent to S.tocoo())

    coo_array(shape, [dtype])
        to construct an empty sparse array with shape `shape`
        dtype is optional, defaulting to dtype='d'.

    coo_array((data, coords), [shape])
        to construct from existing data and index arrays:
            1. data[:]       the entries of the sparse array, in any order
            2. coords[i][:]  the axis-i coordinates of the data entries

        Where ``A[coords] = data``, and coords is a tuple of index arrays.
        When shape is not specified, it is inferred from the index arrays.

Attributes
----------
dtype : dtype
    Data type of the sparse array
shape : tuple of integers
    Shape of the sparse array
ndim : int
    Number of dimensions of the sparse array
nnz
size
data
    COO format data array of the sparse array
coords
    COO format tuple of index arrays
has_canonical_format : bool
    Whether the matrix has sorted coordinates and no duplicates
format
T

Notes
-----

Sparse arrays can be used in arithmetic operations: they support
addition, subtraction, multiplication, division, and matrix power.

Advantages of the COO format
    - facilitates fast conversion among sparse formats
    - permits duplicate entries (see example)
    - very fast conversion to and from CSR/CSC formats

Disadvantages of the COO format
    - does not directly support:
        + arithmetic operations
        + slicing

Intended Usage
    - COO is a fast format for constructing sparse arrays
    - Once a COO array has been constructed, convert to CSR or
      CSC format for fast arithmetic and matrix vector operations
    - By default when converting to CSR or CSC format, duplicate (i,j)
      entries will be summed together.  This facilitates efficient
      construction of finite element matrices and the like. (see example)

Canonical format
    - Entries and coordinates sorted by row, then column.
    - There are no duplicate entries (i.e. duplicate (i,j) locations)
    - Data arrays MAY have explicit zeros.

Examples
--------

>>> # Constructing an empty sparse array
>>> import numpy as np
>>> from scipy.sparse import coo_array
>>> coo_array((3, 4), dtype=np.int8).toarray()
array([[0, 0, 0, 0],
       [0, 0, 0, 0],
       [0, 0, 0, 0]], dtype=int8)

>>> # Constructing a sparse array using ijv format
>>> row  = np.array([0, 3, 1, 0])
>>> col  = np.array([0, 3, 1, 2])
>>> data = np.array([4, 5, 7, 9])
>>> coo_array((data, (row, col)), shape=(4, 4)).toarray()
array([[4, 0, 9, 0],
       [0, 7, 0, 0],
       [0, 0, 0, 0],
       [0, 0, 0, 5]])

>>> # Constructing a sparse array with duplicate coordinates
>>> row  = np.array([0, 0, 1, 3, 1, 0, 0])
>>> col  = np.array([0, 2, 1, 3, 1, 0, 0])
>>> data = np.array([1, 1, 1, 1, 1, 1, 1])
>>> coo = coo_array((data, (row, col)), shape=(4, 4))
>>> # Duplicate coordinates are maintained until implicitly or explicitly summed
>>> np.max(coo.data)
1
>>> coo.toarray()
array([[3, 0, 1, 0],
       [0, 2, 0, 0],
       [0, 0, 0, 0],
       [0, 0, 0, 1]])

r   N)r  r  r  r  r  r  r   r   r:   r   r     s    kr   r   c                       \ rS rSrSrS rSrg)r   i  a  
A sparse matrix in COOrdinate format.

Also known as the 'ijv' or 'triplet' format.

This can be instantiated in several ways:
    coo_matrix(D)
        where D is a 2-D ndarray

    coo_matrix(S)
        with another sparse array or matrix S (equivalent to S.tocoo())

    coo_matrix((M, N), [dtype])
        to construct an empty matrix with shape (M, N)
        dtype is optional, defaulting to dtype='d'.

    coo_matrix((data, (i, j)), [shape=(M, N)])
        to construct from three arrays:
            1. data[:]   the entries of the matrix, in any order
            2. i[:]      the row indices of the matrix entries
            3. j[:]      the column indices of the matrix entries

        Where ``A[i[k], j[k]] = data[k]``.  When shape is not
        specified, it is inferred from the index arrays

Attributes
----------
dtype : dtype
    Data type of the matrix
shape : 2-tuple
    Shape of the matrix
ndim : int
    Number of dimensions (this is always 2)
nnz
size
data
    COO format data array of the matrix
row
    COO format row index array of the matrix
col
    COO format column index array of the matrix
has_canonical_format : bool
    Whether the matrix has sorted indices and no duplicates
format
T

Notes
-----

Sparse matrices can be used in arithmetic operations: they support
addition, subtraction, multiplication, division, and matrix power.

Advantages of the COO format
    - facilitates fast conversion among sparse formats
    - permits duplicate entries (see example)
    - very fast conversion to and from CSR/CSC formats

Disadvantages of the COO format
    - does not directly support:
        + arithmetic operations
        + slicing

Intended Usage
    - COO is a fast format for constructing sparse matrices
    - Once a COO matrix has been constructed, convert to CSR or
      CSC format for fast arithmetic and matrix vector operations
    - By default when converting to CSR or CSC format, duplicate (i,j)
      entries will be summed together.  This facilitates efficient
      construction of finite element matrices and the like. (see example)

Canonical format
    - Entries and coordinates sorted by row, then column.
    - There are no duplicate entries (i.e. duplicate (i,j) locations)
    - Data arrays MAY have explicit zeros.

Examples
--------

>>> # Constructing an empty matrix
>>> import numpy as np
>>> from scipy.sparse import coo_matrix
>>> coo_matrix((3, 4), dtype=np.int8).toarray()
array([[0, 0, 0, 0],
       [0, 0, 0, 0],
       [0, 0, 0, 0]], dtype=int8)

>>> # Constructing a matrix using ijv format
>>> row  = np.array([0, 3, 1, 0])
>>> col  = np.array([0, 3, 1, 2])
>>> data = np.array([4, 5, 7, 9])
>>> coo_matrix((data, (row, col)), shape=(4, 4)).toarray()
array([[4, 0, 9, 0],
       [0, 7, 0, 0],
       [0, 0, 0, 0],
       [0, 0, 0, 5]])

>>> # Constructing a matrix with duplicate coordinates
>>> row  = np.array([0, 0, 1, 3, 1, 0, 0])
>>> col  = np.array([0, 2, 1, 3, 1, 0, 0])
>>> data = np.array([1, 1, 1, 1, 1, 1, 1])
>>> coo = coo_matrix((data, (row, col)), shape=(4, 4))
>>> # Duplicate coordinates are maintained until implicitly or explicitly summed
>>> np.max(coo.data)
1
>>> coo.toarray()
array([[3, 0, 1, 0],
       [0, 2, 0, 0],
       [0, 0, 0, 0],
       [0, 0, 0, 1]])

c                     SU;  a%  UR                  S5      UR                  S5      4US'   U R                  R                  U5        g )Nr`   r}   rz   )pop__dict__update)rn   states     r:   __setstate__coo_matrix.__setstate__j  s>    5   %yy/51ABE(OU#r   r   N)r  r  r  r  r  r  r  r   r   r:   r   r     s    n`$r   r   )r   )2r  __docformat____all__r   warningsr   numpyr5   
_lib._utilr   _matrixr
   _sparsetoolsr   r   r   r   r   r   r   _baser   r   r   r   _datar   r   _sputilsr   r   r   r   r   r   r   r   r    r!   r"   rF   r$   r  r  r  r  r   r   r   r   r   r   r:   <module>r%     s    8%
7    ' 0 0 0 G F .Q Q Q Q b<m b<J'F2=2.G$<*%6l	7 l^v$9 v$r   