
    uki\                    @   d Z ddlm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ZddlZddlZddlmZ ddlmZ dd	lmZ dd
lmZ ddlmZmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlm Z  ddl!m"Z" ddl#m$Z$ ddl%m&Z&m'Z'm(Z( ddl)mZ* e	Z+e,e-df   Z. G d de
      Z/ej`                   G d de             Z1 ejd                  d      Z3dGdZ4dHdZ5d Z6e3jn                  d        Z8 ejr                  e6d      Z:d  Z;d! Z<d" Z= ej|                  e3e<dd       e=ej~                  e3<    ej                  e3e:        ej                  e3        ej                  e3 ee;d#$      d%&        ej                  e3 ee;d'$      d(&        ejd                  d)      ZBd*eB_C        de*j                  d+dId,ZEe*j                  d-dJd.ZFd/ ZGeBjn                  d0        ZH ejr                  eGd*      ZId1 ZJd2 ZKd3 ZLeKej                  eB<   eLej~                  eB<    ej                  eBeI        ej                  eB        ej                  eB eeJd#$      d%&        ej                  eB eeJd'$      d(&        ejd                  d4      ZNdKdLd5ZOdd6dMd7ZPd8 ZQeNjn                  d9        ZR ejr                  eQd      ZSd: ZTd; ZUd< ZVd= ZW ej|                  eNeUddeV       eWej~                  eN<    ej                  eNeS        ej                  eN        ej                  eN eeTd#$      d%&        ej                  eN eeTd'$      d(&        ejd                  d>      ZXdd6dNd?ZYdd6dOd@ZZdA Z[eXjn                  dB        Z\ ejr                  e[d      Z]dC Z^dD Z_dE Z`dF Za ej|                  eXe_dde`       eaej~                  eX<    ej                  eXe]        ej                  eX        ej                  eX ee^d#$      d%&        ej                  eX ee^d'$      d(&       y)Pz@COO (coordinate format) matrix object and associated primitives.    )annotations)Sequence)partialN)Any
NamedTuple)lax)mlir)
_lowerings)	JAXSparse)_coo_extractCuSparseEfficiencyWarning)	tree_util)core)dispatch)ad)_const)hlo)promote_dtypes)Array	ArrayLike	DTypeLike.c                  4    e Zd ZU ded<   dZded<   dZded<   y)COOInfoShapeshapeFboolrows_sortedcols_sortedN)__name__
__module____qualname____annotations__r   r        V/home/cdr/jupyterlab/.venv/lib/python3.12/site-packages/jax/experimental/sparse/coo.pyr   r   .   s    	,+t+tr$   r   c                  `    e Zd ZU dZded<   ded<   ded<   ded<    ed       Z ed	       Z ed
       Z ed       Z	ded<   ded<   ddd	 	 	 d fdZ
edej                  dd d       Zd!dZeddd	 	 	 d"d       Zeddd	 	 	 d#d       Zd$dZd%d&dZd'dZed        Zd(dZ xZS ))COOa  Experimental COO matrix implemented in JAX.

  Note: this class has minimal compatibility with JAX transforms such as
  grad and autodiff, and offers very little functionality. In general you
  should prefer :class:`jax.experimental.sparse.BCOO`.

  Additionally, there are known failures in the case that `nse` is larger
  than the true number of nonzeros in the represented matrix. This situation
  is better handled in BCOO.
  z	jax.Arraydatarowcolztuple[int, int]r   c                .    | j                   j                  S N)r(   sizeselfs    r%   <lambda>zCOO.<lambda>D   s    diinn r$   c                .    | j                   j                  S r,   )r(   dtyper.   s    r%   r0   zCOO.<lambda>E   s    		 r$   c                Z    t        | j                  | j                  | j                        S )Nr   r   r   )r   r   _rows_sorted_cols_sortedr.   s    r%   r0   zCOO.<lambda>F   s#    JJD$5$5##!% r$   c                H    | j                   | j                  | j                  fS r,   )r(   r)   r*   r.   s    r%   r0   zCOO.<lambda>I   s    DHHdhh ? r$   r   r5   r6   F)r   r   c                   t        t        j                  |      \  | _        | _        | _        || _        || _        t        | %  ||       y )Nr   )
mapjnpasarrayr(   r)   r*   r5   r6   super__init__)r/   argsr   r   r   	__class__s        r%   r>   zCOO.__init__M   sB    $'T$:!DItx#D#D	GT'r$   Nnseindex_dtypec                   t        |||      S NrA   )coo_fromdense)clsmatrB   rC   s       r%   	fromdensezCOO.fromdenseT   s    #;??r$   c                    | j                   r| S t        j                  | j                  | j                  | j
                  fd      \  }}}| j                  |||f| j                  d      S )zReturn a copy of the COO matrix with sorted indices.

    The matrix is sorted by row indices and column indices per row.
    If self._rows_sorted is True, this returns ``self`` without a copy.
       )num_keysTr   r   )r5   r   sortr)   r*   r(   r@   r   )r/   r)   r*   r(   s       r%   _sort_indiceszCOO._sort_indicesX   sc     kXXtxx499=JNCd>>4c*$**&*  , ,r$   int32r2   rC   c                   t        |      }t        |      dk7  rt        d|      t        j                  d|      }t        j                  d|      x}} | |||f|dd      S )z>Create an empty COO instance. Public method is sparse.empty().rK   z COO must have ndim=2; got shape=r   Tr4   )tuplelen
ValueErrorr;   empty)rG   r   r2   rC   r(   r)   r*   s          r%   _emptyz
COO._emptye   sm     %LE
5zQ:E8<==99QD		![))C#c3u$! !r$   c          	        dkD  rt        ||z
        }nt        |z   |      }|dk  r| j                  ||f||      S t        j                  ||      }t        j                  ||      }t        |d      t        |      t        j                  |t        j                  dk\  fdfd            }	t        j                  |t        j                  dk  fdfd            }
 | ||	|
f||fdd	      S )
Nr   rQ   )r2   c                      S r,   r#   zeros   r%   r0   zCOO._eye.<locals>.<lambda>        r$   c                      S r,   r#   ks   r%   r0   zCOO._eye.<locals>.<lambda>       a r$   c                      S r,   r#   rZ   s   r%   r0   zCOO._eye.<locals>.<lambda>   r\   r$   c                      S r,   r#   r^   s   r%   r0   zCOO._eye.<locals>.<lambda>   r`   r$   Tr4   )
minrW   r;   onesaranger   r   subcondadd)rG   NMr_   r2   rC   	diag_sizer(   idxr)   r*   r[   s      `       @r%   _eyezCOO._eyeq   s     	1uaQ-ia!eQ-iA~ZZAeZEE88IU+D
**Yk
2C#q>DsAA
''#sxxQi@
AC
''#sxxQi@
ACc31v4TRRr$   c                    t        |       S r,   )coo_todenser.   s    r%   todensezCOO.todense   s    tr$   c                    |t        d      t        | j                  | j                  | j                  f| j
                  d d d   | j                  | j                        S )Nzaxes argument to transpose()r4   )NotImplementedErrorr'   r(   r*   r)   r   r6   r5   )r/   axess     r%   	transposezCOO.transpose   sX     >??		488TXX.djj26F,,$:K:KM Mr$   c                |    | j                   | j                  | j                  f| j                  j	                         fS r,   )r(   r)   r*   _info_asdictr.   s    r%   tree_flattenzCOO.tree_flatten   s-    IItxx*DJJ,>,>,@@@r$   c                    t         j                  |       }|\  |_        |_        |_        |j                         h dk7  rt        d|      |d   |_        |d   |_        |d   |_	        |S )N>   r   r   r   z%COO.tree_unflatten: invalid aux_data=r   r   r   )
object__new__r(   r)   r*   keysrU   r   r5   r6   )rG   aux_datachildrenobjs       r%   tree_unflattenzCOO.tree_unflatten   so    
..
C!)CHcgsw}}AA?h[ABB!CI.C.CJr$   c                   t        |t              rt        d      t        j                  |      }t        | j                  |      \  }}t        || j                  | j                  ffi | j                  j                         }|j                  dk(  rt        ||      S |j                  dk(  rt        ||      S t        d|j                         )Nz"matmul between two sparse objects.   rK   zmatmul with object of shape )
isinstancer   rs   r;   r<   r   r(   r'   r)   r*   rw   rx   ndim
coo_matvec
coo_matmatr   )r/   otherr(   self_promoteds       r%   
__matmul__zCOO.__matmul__   s    %# DEEKKE E2KD%txx2Kdjj6H6H6JKMzzQu--	qu--">u{{m LMMr$   )r?   tuple[Array, Array, Array]r   r   r   r   r   r   rH   r   rB   z
int | NonerC   r   returnr'   )r   r'   )r   zSequence[int]r2   DTypeLike | NonerC   r   r   r'   )ri   intrj   r   r_   r   r2   r   rC   r   r   r'   )r   r   r,   )rt   ztuple[int, ...] | Noner   r'   )r   z1tuple[tuple[Array, Array, Array], dict[str, Any]])r   r   r   r   )r   r    r!   __doc__r"   propertyrB   r2   rw   _bufsr>   classmethodnprP   rI   rO   rW   rm   rp   ru   ry   r   r   __classcell__)r@   s   @r%   r'   r'   4   s#   	 	/..	,-#
/
0%
 % &% ?
@% $)e( (7;( 6:UWU]U] @ @, EI&-	!#	!25	! 	! EI$+S!S03S S&MA  Nr$   r'   ro   c                p    t        | j                  | j                  | j                  | j                        S )zConvert a COO-format sparse matrix to a dense matrix.

  Args:
    mat : COO matrix
  Returns:
    mat_dense: dense version of ``mat``
  spinfo)_coo_todenser(   r)   r*   rw   )rH   s    r%   ro   ro      s%     
chh	CCr$   c               4    t         j                  | |||      S )aI  Convert CSR-format sparse matrix to a dense matrix.

  Args:
    data : array of shape ``(nse,)``.
    row : array of shape ``(nse,)``
    col : array of shape ``(nse,)`` and dtype ``row.dtype``
    spinfo : COOInfo object containing matrix metadata

  Returns:
    mat : array with specified shape and dtype matching ``data``
  r   )coo_todense_pbindr(   r)   r*   r   s       r%   r   r      s     
		D#s6		::r$   c                   t        j                  |j                  | j                        j                  ||f   j                  |       S r,   )r;   zerosr   r2   atrh   r   s       r%   _coo_todense_implr      s5    	6<<	,	/	/S	9	=	=d	CCr$   c               V    t        j                  |j                  | j                        S r,   )r   ShapedArrayr   r2   r   s       r%   _coo_todense_abstract_evalr      s    			&,,

	33r$   F)multiple_resultsc               >   | j                   \  }}}|j                  }	t        j                  |	t        j                        sRt        j                  |	t        j
                        s.t        j                  d|	dt               t        | ||||      S |j                  r|j                  }
d}nO|j                  r||}}d}|j                  d d d   }
n*t        j                  dt               t        | ||||      S | }|rR| j                  \  }t        j                  |j                  d d d   |j                        }|j!                  |g	      }t#        j$                  |||||
|
      }|r,t'        j(                  |t+        j,                  ddg            gS |gS )Nz@coo_todense cusparse/hipsparse lowering not available for dtype=). Falling back to default implementation.r   FTrr   zcoo_todense GPU lowering requires matrices with sorted rows or sorted cols. To sort the rows in your matrix, use e.g. mat = mat._sort_indices(). Falling back to the default implementation.)r   r2   )	avals_out)r   target_name_prefixr   r   )avals_inr2   r   
issubdtypefloatingcomplexfloatingwarningswarnr   _coo_todense_loweringr   r   r   r   r   r   replacer
   coo_todense_gpu_loweringr   ru   r	   dense_int_array)ctxr(   r)   r*   r   r   	data_avalrow_aval_r2   r   ru   sub_ctxout_avalresults                  r%   _coo_todense_gpu_loweringr      s|   <<)Xq
//%
--r{{
+r}}UBDVDV/WMMUuh W< <=VX dCVDDLLEICCILL2EMM 89RT !dCVDD'IHhnnTrT&:(..QHoo
o3G..tS#U?QS& 
 }}VT111a&9:;""r$   c                    t        | |||      S )Nr   )r   )data_dotr(   r)   r*   r   s        r%   _coo_todense_jvpr      s    	hS	88r$   c                  t        j                  |      sJ t        j                  |      st        j                  |      rt        d      | j                  |j                  k(  sJ |j                  j
                  |j                  j
                  k(  sJ | j
                  |j                  j
                  k(  sJ t        |||       ||fS )N/Cannot transpose with respect to sparse indices)r   is_undefined_primalrU   r   avalr2   r   )ctr(   r)   r*   r   s        r%   _coo_todense_transposer      s     
			%%	%C B$:$:3$?
F
GG	V\\	!!	!	388>>	))	)	TYY__	$$	$	c3	#S#	--r$   cu)r   cuda)platformhiprocmrF   TrA   c                   |t        | dk7  j                               }t        j                  t        j
                  |d      }t        t        | ||      | j                  d      S )aE  Create a COO-format sparse matrix from a dense matrix.

  Args:
    mat : array to be converted to COO.
    nse : number of specified entries in ``mat``. If not specified,
      it will be computed from the input matrix.
    index_dtype : dtype of sparse indices

  Returns:
    mat_coo : COO representation of the matrix.
  r   zcoo_fromdense nse argumentrA   TrM   )	r   sumr   concrete_or_erroroperatorindexr'   _coo_fromdenser   )rH   rB   rC   nse_ints       r%   rF   rF     sX     	[
saxnn
C""8>>38TU'	^CW+F99$
0 0r$   )rC   c                   t        j                  |       } t        j                  t        j
                  |d      }t        j                  | ||      S )a  Create COO-format sparse matrix from a dense matrix.

  Args:
    mat : array to be converted to COO.
    nse : number of specified entries in ``mat``
    index_dtype : dtype of sparse indices

  Returns:
    data : array of shape ``(nse,)`` and dtype ``mat.dtype``
    row : array of shape ``(nse,)`` and dtype ``index_dtype``
    col : array of shape ``(nse,)`` and dtype ``index_dtype``
  znse argument of coo_fromdense()rA   )r;   r<   r   r   r   r   coo_fromdense_pr   )rH   rB   rC   s      r%   r   r   %  sC     	C#x~~s4UV#			cs		DDr$   c               V   t        j                  |       } | j                  dk(  sJ t        j                  | |      \  }}| ||f   }t        j                  |      | dk7  j                         k  }t        j                  ||d      }||j                  |      |j                  |      fS )NrK   )r-   r   )r;   r<   r   nonzerore   r   whereastype)rH   rB   rC   r)   r*   r(   true_nonzeross          r%   _coo_fromdense_implr   6  s    C#	Q[[3'(#s	S#X$**S/SAXNN$44-	=$	*$	szz+&

;(?	??r$   c                   t        j                  |f| j                        }t        j                  |f|      x}}|||fS r,   )r   r   r2   )rH   rB   rC   r(   r)   r*   s         r%   _coo_fromdense_abstract_evalr   B  s?    			3&#))	,$v{33#	sCr$   c               R   | j                   d   j                  }t        j                  |t        j                        sQt        j                  |t        j
                        s-t        j                  d|dt               t        | |||      S t        j                  | ||||      S )Nr   zBcoo_fromdense cusparse/hipsparse lowering not available for dtype=r   rA   )nnzrC   r   )r   r2   r   r   r   r   r   r   r   _coo_fromdense_loweringr
   coo_fromdense_gpu_lowering)r   rH   rB   rC   r   r2   s         r%   _coo_fromdense_gpu_loweringr   K  s    
,,q/

%
--r{{
+r}}UBDVDV/WMMWQVPX Y< <=VX"3+NN		.	.	3C[+
- -r$   c               P   | \  }|\  }t        |||      }|\  }}}	t        |      t        j                  u r t        j                  j	                  |      }
nt        ||	|      }
|
t        j                  j	                  |      t        j                  j	                  |	      f}||fS rE   )r   typer   Zerofrom_primal_valuer   )primalstangentsrB   rC   rj   Mdotprimals_outr(   r)   r*   r   tangents_outs               r%   _coo_fromdense_jvpr   U  s    "!
%$qc{C+.$S	$Z277ww((.HCd+HBGG55c:BGG<U<UVY<Z[,	l	""r$   c               |   | \  }}}t        |      |k(  sJ |j                  |j                  cxk(  r|k(  sJ  J t        |t        j                        st        |t        j                        rt        d      t        j                  |      sJ t        |||t        |j                  j                              S )Nr   r9   r   )rT   r2   r   r   r   rU   r   r   r   r   r   )r   rj   rB   rC   r(   r)   r*   s          r%   _coo_fromdense_transposer   e  s    .$S	Tc			cii	.;	..	..	.RWWC!9
F
GG				""	"	dCW166<<-H	IIr$   r   c                X    | j                   \  }}}t        ||||| j                  |      S )a  Product of COO sparse matrix and a dense vector.

  Args:
    mat : COO matrix
    v : one-dimensional array of size ``(shape[0] if transpose else shape[1],)`` and
      dtype ``mat.dtype``
    transpose : boolean specifying whether to transpose the sparse matrix
      before computing.

  Returns:
    y : array of shape ``(mat.shape[1] if transpose else mat.shape[0],)`` representing
      the matrix vector product.
  r   ru   )r   _coo_matvecrw   )rH   vru   r(   r)   r*   s         r%   r   r     ,     99.$S	T3QsyyI	NNr$   )ru   c               8    t         j                  | |||||      S )a:  Product of COO sparse matrix and a dense vector.

  Args:
    data : array of shape ``(nse,)``.
    row : array of shape ``(nse,)``
    col : array of shape ``(nse,)`` and dtype ``row.dtype``
    v : array of shape ``(shape[0] if transpose else shape[1],)`` and
      dtype ``data.dtype``
    shape : length-2 tuple representing the matrix shape
    transpose : boolean specifying whether to transpose the sparse matrix
      before computing.

  Returns:
    y : array of shape ``(shape[1] if transpose else shape[0],)`` representing
      the matrix vector product.
  r   )coo_matvec_pr   )r(   r)   r*   r   r   ru   s         r%   r   r     "    " 
		4c1Vy		QQr$   c                   t        j                  |      }|r||}}|r|j                  d   n|j                  d   }| ||   z  }t        j                  ||j                        j
                  |   j                  |      S Nr   r   r;   r<   r   r   r2   r   rh   )r(   r)   r*   r   r   ru   	out_shapedvs           r%   _coo_matvec_implr     sl    	kk!n!CC!*fll1oQ)af}"	9bhh	'	*	*3	/	3	3B	77r$   c                  | j                   |j                   cxk(  r|j                   k(  sJ  J | j                  |j                  k(  sJ |j                  |j                  k(  sJ t        |j                         dk(  sJ |j                  dk(  sJ |j                   d   |r|j                   d   n|j                   d   k(  sJ |r|j                   d   n|j                   d   }t	        j
                  |f| j                        S )NrK   r   r   )r   r2   rT   r   r   r   )r(   r)   r*   r   r   ru   r   s          r%   _coo_matvec_abstract_evalr     s    	syy	-CII	--	--	-	qww			cii			V\\	a			
1	
9Q&,,q/	JJ	J!*fll1oQ)			9,

	33r$   c          
     ,   | j                   \  }}	}
}|j                  }|t        j                  t        j                  t        j
                  t        j                  fvr0t        j                  d|dt               t        | ||||||      S |j                  r|j                  }nR|j                  r||}}| }|j                  d d d   }n,t        j                  dt               t        | ||||||      S t        j                  | |||||||      S )Nz?coo_matvec cusparse/hipsparse lowering not available for dtype=r   r   rr   zcoo_matvec GPU lowering requires matrices with sorted rows or sorted cols. To sort the rows in your matrix, use e.g. mat = mat._sort_indices(). Falling back to the default implementation.ru   r   r   )r   r2   r   float32float64	complex64
complex128r   r   r   _coo_matvec_loweringr   r   r   r
   _coo_spmv_gpu_lowering)r   r(   r)   r*   r   r   ru   r   r   r   r   x_avalr2   r   s                 r%   _coo_matvec_gpu_loweringr    s   #&<< )Xq&
//%
2::rzz2<<GGMMTeX V< <=VXT3QvQZ[[LLECCILL2EMM 89RT  T3Qv*35 5 
	*	*	4c1	+
- -r$   c               $    t        | |||||      S Nr   r   )r   r(   r)   r*   r   r   ru   s          r%   _coo_matvec_jvp_matr	        	XsC6Y	OOr$   c               $    t        |||| ||      S r  r  )v_dotr(   r)   r*   r   r   ru   s          r%   _coo_matvec_jvp_vecr        	T3U6Y	OOr$   c                   t        j                  |      rJ t        j                  |      rJ t        j                  |      r|||t        |||| ||       fS t        j                  |      }| |   ||   z  |||fS r  )r   r   r   r;   r<   )r   r(   r)   r*   r   r   ru   s          r%   _coo_matvec_transposer    s    ##C((	(##C((	(Ac;tS#r&XaTabbbAA c7QsVS#q((r$   r   c               X    | j                   \  }}}t        ||||| j                  |      S )a  Product of COO sparse matrix and a dense matrix.

  Args:
    mat : COO matrix
    B : array of shape ``(mat.shape[0] if transpose else mat.shape[1], cols)`` and
      dtype ``mat.dtype``
    transpose : boolean specifying whether to transpose the sparse matrix
      before computing.

  Returns:
    C : array of shape ``(mat.shape[1] if transpose else mat.shape[0], cols)``
      representing the matrix vector product.
  r   )r   _coo_matmatrw   )rH   Bru   r(   r)   r*   s         r%   r   r     r   r$   c               8    t         j                  | |||||      S )aD  Product of COO sparse matrix and a dense matrix.

  Args:
    data : array of shape ``(nse,)``.
    row : array of shape ``(nse,)``
    col : array of shape ``(nse,)`` and dtype ``row.dtype``
    B : array of shape ``(shape[0] if transpose else shape[1], cols)`` and
      dtype ``data.dtype``
    shape : length-2 tuple representing the matrix shape
    transpose : boolean specifying whether to transpose the sparse matrix
      before computing.

  Returns:
    C : array of shape ``(shape[1] if transpose else shape[0], cols)``
      representing the matrix vector product.
  r   )coo_matmat_pr   )r(   r)   r*   r  r   ru   s         r%   r  r    r   r$   c               ,   t        j                  |      }|r||}}|r|j                  d   n|j                  d   }| d d d f   ||   z  }t        j                  ||j                  d   f|j                        j
                  |   j                  |      S r   r   )r(   r)   r*   r  r   ru   r   dBs           r%   _coo_matmat_implr     s    	kk!n!CC!*fll1oQ)AtG}qv"	Iqwwqz*BHH	5	8	8	=	A	A"	EEr$   c                  | j                   |j                   cxk(  r|j                   k(  sJ  J | j                  |j                  k(  sJ |j                  dk(  sJ t        |j                         dk(  sJ |j                   d   |r|j                   d   n|j                   d   k(  sJ |r|j                   d   n|j                   d   }t	        j
                  ||j                   d   f| j                        S )NrK   r   r   )r   r2   r   rT   r   r   )r(   r)   r*   r  r   ru   r   s          r%   _coo_matmat_abstract_evalr  (  s    	syy	-CII	--	--	-	qww			
1	V\\	a			
9Q&,,q/	JJ	J!*fll1oQ)			9aggaj14::	>>r$   c          
     ,   | j                   \  }}	}
}|j                  }|t        j                  t        j                  t        j
                  t        j                  fvr0t        j                  d|dt               t        | ||||||      S |j                  r|j                  }nR|j                  r||}}| }|j                  d d d   }n,t        j                  dt               t        | ||||||      S t        j                  | |||||||      S )Nz>coo_matmat cusparse/hipsprse lowering not available for dtype=r   r   rr   zcoo_matmat GPU lowering requires matrices with sorted rows or sorted cols. To sort the rows in your matrix, use e.g. mat = mat._sort_indices(). Falling back to the default implementation.r   )r   r2   r   r   r   r   r  r   r   r   _coo_matmat_loweringr   r   r   r
   _coo_spmm_gpu_lowering)r   r(   r)   r*   r  r   ru   r   r   r   r   B_avalr2   r   s                 r%   _coo_matmat_gpu_loweringr  4  s   #&<< )Xq&
//%
2::rzz2<<GGMMSUH U< <=VXT3QvQZ[[LLECCILL2EMM 89RT  T3Qv*35 5 
	*	*	4c1	+
- -r$   c               $    t        | |||||      S r  r  )r   r(   r)   r*   r  r   ru   s          r%   _coo_matmat_jvp_leftr"  O  r
  r$   c               $    t        |||| ||      S r  r!  )B_dotr(   r)   r*   r  r   ru   s          r%   _coo_matmat_jvp_rightr%  R  r  r$   c                  t        j                  |      rJ t        j                  |      rJ t        j                  |      r|||t        |||| ||       fS t        j                  |      }| |   ||   z  j                  d      |||fS )Nr   r   )r   r   r  r;   r<   r   )r   r(   r)   r*   r  r   ru   s          r%   _coo_matmat_transposer'  U  s    ##C((	(##C((	(Ac;tS#r&XaTabbbAAsGaf!!!$c311r$   )rH   r'   r   r   )
r(   r   r)   r   r*   r   r   r   r   r   r   )rH   r   rB   r   rC   r   r   r   )F)rH   r'   r   r   ru   r   r   r   )r(   r   r)   r   r*   r   r   r   r   r   ru   r   r   r   )rH   r'   r  r   ru   r   r   r   )r(   r   r)   r   r*   r   r  r   r   r   ru   r   r   r   )br   
__future__r   collections.abcr   	functoolsr   r   typingr   r   r   numpyr   jaxr   jax.interpretersr	   jax.experimental.sparser
   jax.experimental.sparse._baser   jax.experimental.sparse.utilr   r   r   jax._srcr   r   jax._src.interpretersr   jax._src.lax.laxr   jax._src.lib.mlir.dialectsr   jax._src.numpy.utilr   jax._src.typingr   r   r   	jax.numpyr;   DtyperS   r   r   r   register_pytree_node_classr'   	Primitiver   ro   r   r   def_abstract_evalr   	lower_funr   r   r   r   defjvpprimitive_transposesregister_loweringsimple_implr   r   rP   rF   r   r   r   r   r   r   r   primitive_jvpsr   r   r   r   r   r  r  r	  r  r  r  r   r  r  r  r  r  r"  r%  r'  r#   r$   r%   <module>rC     s   G " $   "   
  ! . 3 P    $ # * . 7 7  	c3hj  %%rN) rN &rNn }-D;D   4 !4 '/ "B9	. 
		-)4 6)?   &   }&; <   ] #   %$?   %%@ !$..1#'  37RUR[R[ 0$ FIYY E"
@ "" #
 )$..$0 -# J &8  / "+C   (   (? @   _ %   'DA   'EB t~~l+O" fk R&8 4  4 &t~~u. -6PP
) 
		,+T49L M(=   %   |%9 :   \ "   $>   $? t~~l+8= O" fk R&F ?  ? &t~~&6O -6PP2 
		,,dD:O P(=   %   |%9 :   \ "   $>   $?r$   