
    e!h                        d Z g ZddlmZ ddlZddlZddlZddlm	Z	m
Z
 ddlmZmZmZmZ ddlmZmZ ddlmZ dd	lmZmZmZmZmZmZmZmZ dd
lmZ ddlm Z m!Z!m"Z"m#Z#m$Z$m%Z%m&Z&m'Z'm(Z(m)Z)m*Z*m+Z+m,Z,m-Z-  G d deee      Z.ddZ/d Z0y)z>Base class for sparse matrix formats using compressed storage.    )warnN)_prune_arraycopy_if_needed   )_spbaseissparsesparraySparseEfficiencyWarning)_data_matrix_minmax_mixin)_sparsetools)get_csr_submatrixcsr_sample_offsetscsr_todensecsr_sample_valuescsr_row_indexcsr_row_slicecsr_column_index1csr_column_index2)
IndexMixin)upcastupcast_char	to_nativeisdenseisshapegetdtypeisscalarlike	isintlikedowncast_intp_indexget_sum_dtypecheck_shapeget_index_dtypebroadcast_shapesis_pydata_spmatrixc                      e Zd ZdZd@dddZdAdZej
                  j                  e_        dAdZej                  j                  e_        dBdZd Z	d	 Z
d
 Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd ZdCdZej0                  j                  e_        d Zd Zej4                  j                  e_        d Zej6                  j                  e_        dDdZej8                  j                  e_        dAdZd Zd Zd Z d  Z!d! Z"dEd"Z#d# Z$dEd$Z%d@d%Z&d& Z'd' Z(d( Z)d) Z*d* Z+d+ Z,d, Z-d- Z.dBd.Z/ej^                  j                  e/_        dFd/Z0ej`                  j                  e0_        d0 Z1e2d1e3fd2       Z4e4jj                  d3e3fd4       Z4d5 Z6e2d1e3fd6       Z7e7jj                  d3e3fd7       Z7d8 Z8d9 Z9d: Z:d; Z;ejv                  j                  e;_        dBd<Z<d= Z=d> Z>dEd?Z?y)G
_cs_matrixzY
    base array/matrix class for compressed row- and column-oriented arrays/matrices
    Nmaxprintc                ~	   t        j                  | ||       t        |      r|j                  | j                  k(  r|r|j	                         }n|j                  | j                        }|j                  |j                  |j                  |j                  f\  | _        | _        | _        | _	        nt        |t              rt        || j                        rt        || j                        | _	        | j                  | j                         \  }}| j#                  t%        | j&                              }t)        j*                  dt-        |t.                    | _        t)        j*                  d|      | _        t)        j*                  |dz   |      | _        nt1        |      dk(  r\| j3                  |||	      }	|	j5                  | j                        }
|
\  | _        | _        | _        | _	        | j7                          nt1        |      d
k(  r|\  }}}d }|d|vrt%        |      }| j#                  ||f|d      }|st8        }t)        j:                  |||      | _        t)        j:                  |||      | _        t)        j:                  |||      | _        nt=        d| j>                  j@                   d|       	 t)        jB                  |      }t        | tF              r7|jH                  dk7  r(| j                  dk(  rt=        d|jH                   d      |jH                  dkD  rt=        d|jH                   d      | j3                  ||      }	|	j5                  | j                        }
|
\  | _        | _        | _        | _	        |t        || j                        | _	        no| j&                  c	 t1        | j                        dz
  }| j                  j%                         dz   }t        | j                  ||f      | j                        | _	        |-t-        |      }| j                  jK                  |d      | _        | jM                  d       y # tD        $ r+}t=        d| j>                  j@                   d|       |d }~ww xY w# tD        $ r}t=        d      |d }~ww xY w)Nr'   allow_ndmaxvalr   )defaultr   dtype   shaper0      T)r-   check_contentscopyr0   zunrecognized z constructor input: csczCSC arrays don't support zD input. Use 2DzCSR arrays don't yet support zD.z!unable to infer matrix dimensionsFr7   
full_check)'r   __init__r   formatr7   asformatindptrindicesdata_shape
isinstancetupler   	_allow_ndr!   _swap_shape_as_2d_get_index_dtypemaxr3   npzerosr   floatlen_coo_container_coo_to_compressedsum_duplicatesr   array
ValueError	__class____name__asarray	Exceptionr	   ndimastypecheck_format)selfarg1r3   r0   r7   r(   MN	idx_dtypecooarraysrA   r@   r?   r-   enewdtypes                    `/var/www/html/diagnosisapp-backend/venv/lib/python3.12/site-packages/scipy/sparse/_compressed.pyr<   z_cs_matrix.__init__   s   dD8<D>{{dkk)dyy{}}T[[1T\\499dkkA=DKty$+ e$tdnn5 *$Hzz$"3"341 !11TZZ1I	HHQ(FG	!xx95 hhq1uI>t9>--d%u-MC 33DJJ?FHNEDKty$+'')Y!^.2+T7F "F(Qe^!$U $ 5 5w6G7=?C !6 !EI  -#%88G$i#PDL"$((6I"NDK "D FDI$}T^^5L5L4M N;;?&&B C C
Fzz$' $(TYY!^u@T #<TYYK!WXXyy1} #@2!NOO%%d%%8C++DJJ7F@F=DKty$+ %ednnEDKZZM$q(LL$$&* &djj!Q&84>>RDKH		(((>DIU+;  F =1H1H0I J77;f"> ?DEFF(  M !DE1LMs0   Q+ 5R" +	R4&RR"	R<+R77R<c                    |t        | j                  d         S | j                  dk(  r'|dv rt        | j                  d         S t        d      |dk  r|dz  }| j	                  |d|z
  f      \  }}| j	                  | j
                        \  }}|dk(  r*t        j                  t        | j                        |      S |dk(  rt        j                  | j                        S t        d      )Nr   r   re   axis out of boundsr   r1   	minlength)intr?   rW   rR   rF   r3   rJ   bincountr   r@   diff)rZ   axis_r]   s       rc   _getnnzz_cs_matrix._getnnzu   s    <t{{2''YY!^w4;;r?++122ax	jj$D!12GD!::djj)DAqqy{{#6t||#DPQRRwwt{{++122    c                    | j                          |t        j                  | j                        S | j                  dk(  r.|dvrt        d      t        j                  | j                        S |dk  r|dz  }| j                  |d|z
  f      \  }}|dk(  rx| j                  | j                        \  }}| j                  dk7  }|j                         r| j                  n| j                  |   }t        j                  t        |      |      S |dk(  r| j                  j                         rt        j                  | j                        S t        j                  | j                        }t        j                   |D cg c]'  \  }}t        j                  | j                  ||       ) c}}      S t        d      c c}}w )Nr   rf   rg   r   r1   rh   )rP   rJ   count_nonzerorA   rW   rR   rF   r3   allr@   rk   r   rl   r?   	itertoolspairwiserQ   )	rZ   rm   rn   r]   maskidxpairsijs	            rc   rr   z_cs_matrix.count_nonzero   sd   <##DII..99>7" !566##DII..!8AID**dAH-.a19::djj)DAq99>D"&((*$,,$,,t2DC;;2371EEQYyy}}wwt{{++&&t{{3E88%P$!QR--dii!n=PQQ122 Qs    ,F?
c                 :   | j                   j                  j                  dk7  r/t        d| j                   j                  j                   dd       | j
                  j                  j                  dk7  r/t        d| j
                  j                  j                   dd       | j                  j                  | j
                  j                  | j                   j                  fD ]  }|dk7  s	t        d       | j                  | j                        \  }}t        | j                         |dz   k7  r't        d	t        | j                          d
|dz          | j                   d   dk7  rt        d      t        | j
                        t        | j                        k7  rt        d      | j                   d   t        | j
                        kD  rt        d      | j                          |r+| j                  dkD  r| j
                  j                         |k\  rt        d|       | j
                  j                         dk  rt        d      t!        j"                  | j                         j                         dk  rt        d      | j%                  | j                   | j
                  f      }t!        j&                  | j                   |      | _         t!        j&                  | j
                  |      | _        t)        | j                        | _        yy)a  Check whether the array/matrix respects the CSR or CSC format.

        Parameters
        ----------
        full_check : bool, optional
            If `True`, run rigorous check, scanning arrays for valid values.
            Note that activating those check might copy arrays for casting,
            modifying indices and index pointers' inplace.
            If `False`, run basic checks on attributes. O(1) operations.
            Default is `True`.
        ry   z$indptr array has non-integer dtype ()r4   
stacklevelz%indices array has non-integer dtype (r   z'data, indices, and indptr should be 1-Dzindex pointer size z should be r   z!index pointer should start with 0z*indices and data should have the same sizere   zQLast value of index pointer should be less than the size of index and data arrayszindices must be < zindices must be >= 0z(indptr must be a non-decreasing sequencer/   N)r?   r0   kindr   namer@   rA   rW   rR   rF   rG   rM   prunennzrI   minrJ   rl   rH   rU   r   )rZ   r;   xr\   r]   r^   s         rc   rY   z_cs_matrix.check_format   s    ;;!!S(78I8I8N8N7OqQ<<""c)89K9K9P9P8QQRS ))..$,,"3"3T[[5E5EF 	LAAv !JKK	L
 zz$++,1A%23t{{3C2DKPQTUPUwWXXKKNa@AA TYY/IJJKKOc$,,// A B B 	

xx!|<<##%*$'9!%=>><<##%)$%;<<774;;'++-1$%OPP--t{{DLL.IJI**T[[	BDK::dll)DDL!$)),DI rp   c                     | j                          | j                   || j                  |      d      }|j                          |S )zScalar version of self._binopt, for cases in which no new nonzeros
        are added. Produces a new sparse array in canonical form.
        Tr9   )rP   
_with_datarA   eliminate_zeros)rZ   otheropress       rc   _scalar_binoptz_cs_matrix._scalar_binopt   s>     	oobE2o>
rp   c                    t        |      rt        j                  |      r+| j                  | j                  t        j
                        S |dk(  rut        dt        d       | j                  t        j                  | j                  t        j
                              }| j                  |t        j                        }||z
  S | j                  |t        j                        S t        |      r| j                         |k(  S t        |      rt         S t#        |      rt        dt        d       | j                  |j                  k7  ry| j$                  |j$                  k7  r|j'                  | j$                        }| j)                  |d      }| j                  t        j                  | j                  t        j
                              }||z
  S t         S )	Nr/   r   zOComparing a sparse matrix with 0 using == is inefficient, try using != instead.r4   r}   zHComparing sparse matrices using == is inefficient, try using != instead.F_ne_)r   rJ   isnanrS   r3   bool_r   r
   onesr   operatorneeqr   todenser$   NotImplementedr   r=   r>   _binopt)rZ   r   all_trueinvr   s        rc   __eq__z_cs_matrix.__eq__   s\   xx~~djj~AAz /0G !#  >>"''$**BHH*MN))%=#~%**5(++>>U^<<>U**&!!e_  !8QH zzU[[(,t{{3,,uf-C~~bggdjj&IJHc>!!!rp   c                 \   t        |      rt        j                  |      rRt        dt        d       | j                  t        j                  | j                  t        j                              }|S |dk7  rut        dt        d       | j                  t        j                  | j                        t        j                        }| j                  |t        j                        }||z
  S | j                  |t        j                        S t        |      r| j                         |k7  S t        |      rt         S t#        |      r`| j                  |j                  k7  ry| j$                  |j$                  k7  r|j'                  | j$                        }| j)                  |d      S t         S )	Nz:Comparing a sparse matrix with nan using != is inefficientr4   r}   r/   r   z^Comparing a sparse matrix with a nonzero scalar using != is inefficient, try using == instead.Tr   )r   rJ   r   r   r
   rS   r   r3   r   r   r   r   r   r   r   r$   r   r   r=   r>   r   )rZ   r   r   r   s       rc   __ne__z_cs_matrix.__ne__  s<   xx $%<L>>"''$**BHH*MN! >,<  >>"''$***=RXX>N))%=#~%**5(++>>U^<<>U**&!!e_zzU[[(,t{{3<<v..!!rp   c                    t        |      rd|k(  r|dv rt        d       |d|      rzt        |t        d       t	        j
                  | j                  t	        j                  |            }|j                  |       | j                  |      }| j                  ||      S | j                  ||      S t        |      r || j                         |      S t        |      r| j                  |j                  k7  rt        d      | j                   |j                   k7  r|j#                  | j                         }|dvr| j                  ||      S t        d	t        d       | j                  t	        j$                  | j                  t        j&                              }| j                  ||d
k(  rdnd      }||z
  S t(        S )Nr   )_le__ge_z >= and <= don't work with 0.r4   r}   r/   inconsistent shapes)r   r   zUComparing sparse matrices using >= and <= is inefficient, using <, >, or !=, instead.r   _gt__lt_)r   NotImplementedErrorr   r
   rJ   emptyr3   result_typefillrS   r   r   r   r   r   rR   r=   r>   r   r   r   )rZ   r   r   op_namebad_scalar_msg	other_arrr   r   s           rc   _inequalityz_cs_matrix._inequality7  sm   Ezg)99)*IJJAu^%<KHHTZZr~~e7LM	u% NN95	||Iw77**5"55U^dllne,,e_zzU[[( !677,t{{3..||E733 /(Q8 ~~bggdjj&IJH,,u60AfvNCc>!!!rp   c                 F    | j                  |t        j                  dd      S )Nr   zgComparing a sparse matrix with a scalar greater than zero using < is inefficient, try using >= instead.)r   r   ltrZ   r   s     rc   __lt__z_cs_matrix.__lt__Z  $    x{{F!89 	9rp   c                 F    | j                  |t        j                  dd      S )Nr   zdComparing a sparse matrix with a scalar less than zero using > is inefficient, try using <= instead.)r   r   gtr   s     rc   __gt__z_cs_matrix.__gt__`  r   rp   c                 F    | j                  |t        j                  dd      S )Nr   zgComparing a sparse matrix with a scalar greater than zero using <= is inefficient, try using > instead.)r   r   ler   s     rc   __le__z_cs_matrix.__le__f  $    x{{F!78 	8rp   c                 F    | j                  |t        j                  dd      S )Nr   zdComparing a sparse matrix with a scalar less than zero using >= is inefficient, try using < instead.)r   r   ger   s     rc   __ge__z_cs_matrix.__ge__l  r   rp   c                 H   |j                   | j                   k7  r&t        d| j                    d|j                    d      t        | j                  j                  |j                  j                        }| j                  d      d   }t        j                  |||d      }|j                  j                  r|n|j                  }| j                  | j                        \  }}t        ||| j                  | j                  | j                  |       | j!                  |d	      S )
NzIncompatible shapes (z and r|   CFr   T)r0   orderr7   Fr9   )r3   rR   r   r0   charrF   rJ   rQ   flagsc_contiguousTrG   r   r?   r@   rA   
_container)rZ   r   r0   r   resultyr\   r]   s           rc   
_add_densez_cs_matrix._add_densev  s    ;;$**$4TZZLekk]RSTUUDJJOOU[[-=-=>

4 #%uEEll//FVXXzz$++,1Aq$++t||TYYBvE22rp   c                 &    | j                  |d      S )N_plus_r   r   s     rc   _add_sparsez_cs_matrix._add_sparse  s    ||E8,,rp   c                 &    | j                  |d      S )N_minus_r   r   s     rc   _sub_sparsez_cs_matrix._sub_sparse  s    ||E9--rp   c                    t        |      r| j                  |      S t        |      r| j                  |j                  k(  r#| j	                  |      }| j                  |d      S |j                  dk(  rS| j                  |j                         d         }| j                  dk(  r |j                  d| j                  d   f      S |S |j                  dk(  r"| j                  |j                         d         S | j                  dv r)|j                  | j                  j                               S | j                  \  }}|j                  \  }}|dk(  r4|dk(  r/|j                  | j                  ||      j                               S |dk(  r4|dk(  r/| j                  |j                  ||      j                               S t        | t              }|dk(  rb||k(  r]t!        |j                         j#                         |      }| j                  |      }| j                  dk(  r|S |j                  d|f      S |dk(  r>||k(  r9t!        | j                         j#                         |      }	|j                  |	      S |dk(  r>||k(  r9t!        |j                         j#                         |      }|j                  |       S |dk(  r>||k(  r9t!        | j                         j#                         |      }
|
j                  |      S t%        d	      t'        j(                  |      }|j                  dkD  r$t'        j*                  | j                         |      S |j,                  dk(  rp|j.                  t&        j0                  k(  rt2        S t5        | j                  |j                        }| j                  |j6                  d         j                  |      S | j                  dv r]t5        | j                  |j                        }t'        j*                  | j                  j                         |      j                  |      S | j9                         }| j                  |j                  k(  rat'        j*                  |j                  ||j:                           }|j=                  t&        j>                        j#                         |_	        |S t'        j@                  |      }| j                  d   dk(  s| j                  dk(  r|j                  d   dk(  r!t'        j*                  |j                  |      }n\|j                  d   | j                  d
   k(  r2t'        j*                  |j                  |dd|jB                  f         }nt%        d	      | jE                  |jB                  |jF                  |j                  d   z        }t'        jH                  t'        jJ                  |j                  d   |      |jF                        }t'        jL                  |jB                  jO                  |d      |j                  d         }| jQ                  |j=                  t&        j>                        j#                         ||ff|j                  d   | j                  d
   fd      S | j                  d   dk(  r|j                  d   dk(  r(t'        j*                  |j                  dddf   |      }n_|j                  d   | j                  d   k(  r5t'        j*                  |j                  dddf   ||jR                           }nt%        d	      | jE                  |jR                  |jF                  |j                  d   z        }t'        jH                  |jR                  jO                  |d      |j                  d         }t'        jL                  t'        jJ                  |j                  d   |      |jF                        }| jQ                  |j=                  t&        j>                        j#                         ||ff| j                  d   |j                  d   fd      S |j                  d   dk(  r_| j                  d   |j                  d   k(  r@t'        j*                  |j                  |dd|jB                  f   j#                               }nx|j                  d   dk(  r[| j                  d   |j                  d   k(  r<t'        j*                  |j                  ||jR                     j#                               }nt%        d	      |j=                  t&        j>                        j#                         |_	        |S )z=Point-wise multiplication by array/matrix, vector, or scalar._elmul_r   r   )r   r   r   r   r   )r   r   r1   r   re   Nr,   r/   Fr9   r3   r7   )*r   _mul_scalarr   r3   rS   r   toarrayrW   reshaperA   sumrG   _matmul_sparsetocscrC   r	   _make_diagonal_csrravelrR   rJ   
asanyarraymultiplysizer0   object_r   r#   flattocoocoordsviewndarray
atleast_2dcolrH   r   repeatarangetilerX   rN   row)rZ   r   r   sMsNoMoNis_array	new_otherr7   new_selfbshaperetrA   other2dr^   r   r   s                     rc   r   z_cs_matrix.multiply  sh    ##E**E?zzU[[(u-||E955{{f$))%--/$*?@99>!>>1djjm*<=={{d"''(:;;zz^+((99 &&FB''FBQw27++DLLR,@,F,F,HIIQw27**5==R+@+F+F+HII!$0HQw28.u}}/D/D/FQ	,,Y7!%avLV^^QG5LLQw28)$,,.*>*>*@(K++D11 Qw28.u}}/D/D/FQ	 //55Qw28-dlln.B.B.DhO..u55233 e$::>;;t||~u55::?{{bjj(%%%djj%++>F##EJJqM2::6BB::'%djj%++>F;;tyy}}6>>vFFjjl::$;;sxxszz):;Dyy,224CHJ --&::a=Aa}}Q1${{388W5q!TZZ^3{{388WQZ-@A !677--cgg58WWw}}Q?O5O . QI))BIIgmmA&6iH#''RC''#''...?qAQRC&&2::&,,.c
;}}Q'B8 '   ::a=A}}Q1${{388AtG#4g>q!TZZ]2{{388AtG#4gcgg6FG !677--cgg58WWw}}Q?O5O . QI))CGGNN95NA7==QRCSTC''"))GMM!$4IFPC&&2::&,,.c
;zz!}gmmA&67 '   ==q TZZ]gmmA6F%F;;sxxCGG)<)B)B)DED]]1"tzz!}a8H'H;;sxx)9)?)?)ABD23399RZZ(..0
rp   c           	      p   | j                   \  }}t        j                  |t        | j                  j
                  |j                  j
                              }t        t        | j                  dz         } |||| j                  | j                  | j                  ||       | j                  dk(  r|d   S |S )Nr/   _matvecr   r   )rG   rJ   rK   r   r0   r   getattrr   r=   r?   r@   rA   rW   )rZ   r   r\   r]   r   fns         rc   _matmul_vectorz_cs_matrix._matmul_vector  s      1 !;tzz@P@P#QR \4;;#:;
1adllDIIufE IINvay66rp   c                    | j                   \  }}|j                  d   }t        j                  ||ft	        | j
                  j                  |j
                  j                              }t        t        | j                  dz         } ||||| j                  | j                  | j                  |j                         |j                                | j                  dk(  r|j                  |f      S |S )Nre   r/   _matvecsr   )rG   r3   rJ   rK   r   r0   r   r   r   r=   r?   r@   rA   r   rW   r   )rZ   r   r\   r]   n_vecsr   r   s          rc   _matmul_multivectorz_cs_matrix._matmul_multivector  s      1R1f+ +DJJOOU[[=M=M NP \4;;#;<
1adllDII;;=&,,.	* 99>>>6),,rp   c                     | j                   \  }}|j                  }|dk(  r*|j                  d|j                  d   f      j                  }|j                  dk(  r|j
                  n|j                  d   df\  }}d}| j                  dk(  r||fz  }|dk(  r||fz  }| j                  dk(  r|nd|dk(  r|ndf}| j                  ||f      d   }	| j                  |      }| j                  | j                  | j                  |j                  |j                  f      }
t        t        | j                  dz         } |||t        j                  | j                  |
      t        j                  | j                  |
      t        j                  |j                  |
      t        j                  |j                  |
            }|dk(  rk|dk(  r5t        j                   dt#        | j$                  |j$                              S | j                  |t#        | j$                  |j$                              S | j                  | j                  | j                  |j                  |j                  f|      }
t        j&                  |	dz   |
      }t        j&                  ||
      }t        j&                  |t#        | j$                  |j$                              }t        t        | j                  dz         } |||t        j                  | j                  |
      t        j                  | j                  |
      | j(                  t        j                  |j                  |
      t        j                  |j                  |
      |j(                  |||       |dk(  rt        j                   |d         S | j                  |||f|	      }||k7  r0|j                  d
k7  r|j+                         }|j                  |      }|S )Nr   r   r1    _matmat_maxnnzr/   r,   _matmatr3   csr)rG   rW   r   r3   r   rB   rF   rS   rH   r?   r@   r   r   r=   rJ   rU   rQ   r   r0   r   rA   tocsr)rZ   r   r\   K1o_ndimK2r]   	new_shape
faux_shape	major_dimr^   r   r   r?   r@   rA   r   s                    rc   r   z_cs_matrix._matmul_sparse"  s,   !!2Q;MM1ekk!n"5688E %

aekk!na5HA 	99>!IQ;!I99>aqv{!J
JJ1v&q)	u%))4;;%*\\5==+B C	 \4;;1A#ABADKKy9DLL	:ELL	:EMM;	=
 !8Bxx

EKK)HII>>)6$**ekk3R>SS))4;;%*\\5==+B+. * 0	 )a-y9((3i0xx6$**ekk#BC\4;;#:;
1aDKKy9::dll)499::ell)4::emm95::7D	" ?88DG$$nndGV4JnG"zzU"iik++i(C
rp   c           
         | j                   \  }}|| k  s||k\  r+t        j                  d| j                  j                        S t        t        | j                  dz         }t        j                  t        |t        |d      z   |t        |d      z
        t        | j                              } ||| j                   d   | j                   d   | j                  | j                  | j                  |       |S )Nr   r/   	_diagonalr   )r3   rJ   r   rA   r0   r   r   r=   r   rI   r   r?   r@   )rZ   krowscolsr   r   s         rc   diagonalz_cs_matrix.diagonal^  s    ZZ
d:d88ATYY__55\4;;#<=HHSAq	)4#a)+;<!$**-/
1djjmTZZ]DKK99a	rp   c                    t        |      r ||      rt        dt        d       t        j                  | j
                  t        j                  |      j                        }|j                  |       | j                  |      }| j                  ||      S | j                           || j                  t        j                  |            }| j                  || j                  | j                  f|j                  | j
                        }|S t        |      r || j!                         |      S t#        |      r| j                  ||      S t%        d      )NzITaking maximum (minimum) with > 0 (< 0) number results to a dense matrix.r4   r}   r/   )r0   r3   zOperands not compatible.)r   r   r
   rJ   r   r3   rU   r0   r   rS   r   rP   rA   r@   r?   r   r   r   rR   )rZ   r   npopr   dense_checkr   new_datamats           rc   _maximum_minimumz_cs_matrix._maximum_minimumo  s   5! +,C !# HHTZZrzz%7H7N7NO	u% NN95	||Iw77##%		2::e+<=nnhdkk%J+3>> % M
U^..e_<<w//788rp   c                 H    | j                  |t        j                  dd       S )N	_maximum_c                 2    t        j                  |       dkD  S Nr   rJ   rU   r   s    rc   <lambda>z$_cs_matrix.maximum.<locals>.<lambda>      BJJqMA<M rp   )r  rJ   maximumr   s     rc   r  z_cs_matrix.maximum  %    $$UBJJ%02MO 	Orp   c                 H    | j                  |t        j                  dd       S )N	_minimum_c                 2    t        j                  |       dk  S r  r  r  s    rc   r  z$_cs_matrix.minimum.<locals>.<lambda>  r  rp   )r  rJ   minimumr   s     rc   r   z_cs_matrix.minimum  r  rp   c                    | j                   dk(  rt        | d      s|| j                  d      d   v rt        | j                        }t        j                  t        | j                        dz
  |      }| j                  t
        j                        \  }}|||<   | j                  |      }|dz  dk(  r|j                  }|j                  d||      S t        j                  | |||      S )	zSum the array/matrix over the given axis.  If the axis is None, sum
        over both rows and columns, returning a scalar.
        r1   	blocksize))r   re   )r   r   r   r/   r   )rm   r0   out)rW   hasattrrF   r    r0   rJ   rK   rM   r?   _minor_reduceadd_ascontainerr   r   r   )rZ   rm   r0   r$  	res_dtyper   major_indexvalues           rc   r   z_cs_matrix.sum  s     IIN74#=

#56q99%djj1I((3t{{+a/yAC!%!3!3BFF!;K$C##C(Cax1}ee77%S799 ;;t$eEErp   c                     || j                   }t        j                  t        j                  | j                              }|j                  |t        | j                  |               }||fS )a  Reduce nonzeros with a ufunc over the minor axis when non-empty

        Can be applied to a function of self.data by supplying data parameter.

        Warning: this does not call sum_duplicates()

        Returns
        -------
        major_index : array of ints
            Major indices where nonzero

        value : array of self.dtype
            Reduce result for nonzeros in each major_index
        )rA   rJ   flatnonzerorl   r?   reduceatr   )rZ   ufuncrA   r*  r+  s        rc   r&  z_cs_matrix._minor_reduce  sZ     <99DnnRWWT[[%9:t24;;{3KLNE!!rp   c                    | j                  | j                        \  }}| j                  ||f      \  }}t        ||| j                  | j                  | j
                  ||dz   ||dz   	      \  }}}	|	j                  | j                        S )Nr   r/   )rF   r3   r   r?   r@   rA   r   r0   )
rZ   r   r   r\   r]   majorminorr?   r@   rA   s
             rc   _get_intXintz_cs_matrix._get_intXint  s~    zz$**%1zz3*-u 1q$++t||TYY519eUQY!0 xxdjjx))rp   c                     | j                  ||f      \  }}|j                  dv r"|j                  dv r| j                  ||d      S | j                  |      j	                  |      S )Nr   NTr9   )rF   step_get_submatrix_major_slice_minor_slicerZ   r   r   r1  r2  s        rc   _get_sliceXslicez_cs_matrix._get_sliceXslice  sc    zz3*-u::"uzzY'>&&ue$&??  '44U;;rp   c                    | j                   j                  }| j                  | j                        \  }}| j                  ||f      \  }}t	        j
                  ||      }t	        j
                  ||      }t	        j                  |j                  | j                        }t        ||| j                  | j                   | j                  |j                  |j                         |j                         |	       |j                  dk(  r| j                  |      S | j                  |j                  |j                              S )Nr/   r   )r@   r0   rF   r3   rJ   rU   r   r   r   r?   rA   r   rW   r(  rS   r   )	rZ   r   r   r^   r\   r]   r1  r2  vals	            rc   _get_arrayXarrayz_cs_matrix._get_arrayXarray  s    LL&&	zz$**%1zz3*-u

5	2

5	2hhuzz4!QT\\499**ekkmU[[]C	I::?$$S))~~ckk%++677rp   c                 n    | j                  ||f      \  }}| j                  |      j                  |      S N)rF   _major_index_fancy_minor_index_fancyr:  s        rc   _get_columnXarrayz_cs_matrix._get_columnXarray  s4    zz3*-u&&u-@@GGrp   c           	         | j                  | j                  | j                  f      }t        j                  ||      j                         }| j                  | j                        d   }t        |      }| j                  dk(  r| j                  ||f      n|f}|dk(  r| j                  || j                        S | j                  |dz      | j                  |   z
  j                  |      }t        j                  |dz   |      }t        j                  ||dd        |d   }	t        j                  |	|      }
t        j                  |	| j                        }t!        ||| j                  j                  |d	      | j                  j                  |d	      | j"                  |
|       | j                  ||
|f|d
      S )zBIndex along the major axis where idx is an array of ints.
        r/   r   r1   r   Nr$  re   Fr9   r   )rH   r?   r@   rJ   rU   r   rF   rG   rM   rW   rS   r0   rX   rK   cumsumr   r   rA   )rZ   rw   r^   r@   r]   r\   r  row_nnz
res_indptrr   res_indicesres_datas               rc   rA  z_cs_matrix._major_index_fancy  s    ))4;;*EF	**S	288:JJt(()!,L*.))q.DJJ1v&qd	6>>)4::>>>;;w{+dkk'.BBJJ9UXXa!e95

		'z!"~.nhhs)488Ctzz2KKyu5LL	6II	
 ~~xjA$-E  ; 	;rp   c           
      h   |t        d      k(  r|r| j                         S | S | j                  | j                        \  }}|j	                  |      \  }}}t        t        |||            }| j                  dk(  r| j                  ||f      n|f}|dk(  r| j                  || j                        S ||}
}	|dk(  r|dk\  rd}
|dz   |dz   }}| j                  |||   | j                  |	|
|   z
  }| j                  j                  }t        j                  |dz   |      }t        j                  ||dd        |dk(  rot        | j                  |   | j                  |         }t        j                  | j                  |   |      }t        j                  | j                  |   |      }nm|d   }t        j                   ||      }t        j                   || j                        }t#        |||| j                  | j                  | j                  ||       | j                  |||f|d	
      S )z@Index along the major axis where idx is a slice object.
        Nr1   r   r/   re   r   rE  r9   Fr   )slicer7   rF   rG   r@   rM   rangerW   rS   r0   r?   rJ   rK   rF  rQ   rA   r   r   )rZ   rw   r7   r\   r]   startstopr6  r  start0stop0start1stop1rG  r^   rH  all_idxrI  rJ  r   s                       rc   r8  z_cs_matrix._major_slice  s    %+"&499;0D0zz$++,1KKNtTeT4()*.))q.DJJ1v&qd	6>>)4::>>> t2:%1*E	4!8++fU4/0KKuT)*+LL&&	XXac3

		'z!"~.19DKK.D0ABG((4<<#8tDKxx		' 2>HR.C((3i8Kxx4::6H%tT[[$,,))[(< ~~xjA$-E  ; 	;rp   c           
         | j                  | j                  | j                  f      }| j                  j                  |d      }| j                  j                  |d      }t	        j
                  ||      j                         }| j                  | j                        \  }}t        |      }| j                  dk(  r| j                  ||f      n|f}|dk(  r| j                  || j                        S t	        j                  ||      }	t	        j                  | j                  |      }
t        |||||||	|
       t	        j                   |      j                  |d      }|
d   }t	        j"                  ||      }t	        j"                  || j                        }t%        ||	t        | j                        || j&                  ||       | j                  |||
f|d      S )zBIndex along the minor axis where idx is an array of ints.
        Fr9   r/   r1   r   re   r   )rH   r@   r?   rX   rJ   rU   r   rF   rG   rM   rW   rS   r0   rK   
empty_liker   argsortr   r   rA   )rZ   rw   r^   r@   r?   r\   r]   r	  r  col_offsetsrH  	col_orderr   rI  rJ  s                  rc   rB  z_cs_matrix._minor_index_fancy3  s    ))4<<*EF	,,%%ie%<##IE#:jjI.446zz$++,1H*.))q.DJJ1v&qd	6>>)4::>>> hhq	2]]4;;i@
		
 JJsO**95*A	nhhs)488Ctzz2)[#dll2C!499k8	E~~xjA$-E  ; 	;rp   c                    |t        d      k(  r|r| j                         S | S | j                  | j                        \  }}|j	                  |      \  }}}t        t        |||            }|dk(  r.| j                  | j                  ||f      | j                        S |dk(  r| j                  ||      S | j                  t        j                  |||            S )z@Index along the minor axis where idx is a slice object.
        Nr   r/   r   )r2  r7   )rL  r7   rF   rG   r@   rM   rM  rS   r0   r7  rB  rJ   r   )rZ   rw   r7   r\   r]   rN  rO  r6  s           rc   r9  z_cs_matrix._minor_sliceZ  s     %+"&499;0D0zz$++,1KKNtTeT4()6>>$**aV"4DJJ>GG19&&St&<<&&ryyd'CDDrp   c                    | j                  | j                        \  }}t        ||      \  }}t        ||      \  }}	|dk(  r#|dk(  r||k(  r|	|k(  r|r| j                         S | S t	        ||| j
                  | j                  | j                  ||||		      \  }
}}| j                  ||z
  |	|z
  f      }| j                  dk(  r|d   f}| j                  |||
f|| j                  d      S )zbReturn a submatrix of this matrix.

        major, minor: None, int, or slice with step 1
        r   r   F)r3   r0   r7   )rF   rG   _process_slicer7   r   r?   r@   rA   rW   rS   r0   )rZ   r1  r2  r7   r\   r]   i0i1j0j1r?   r@   rA   r3   s                 rc   r7  z_cs_matrix._get_submatrixj  s    
 zz$++,1q)Bq)B7rQw27rQw"&499;0D0 1q$++t||TYYBB!H 

BGR"W-.99>1XKE~~tWf5U$(JJU  < 	<rp   c                 V    | j                  ||f      \  }}| j                  |||       y r@  rF   	_set_manyrZ   r   r   r   ry   rz   s         rc   _set_intXintz_cs_matrix._set_intXint  (    zz3*%1q!Qrp   c                 V    | j                  ||f      \  }}| j                  |||       y r@  rb  rd  s         rc   _set_arrayXarrayz_cs_matrix._set_arrayXarray  rf  rp   c                 R    | j                   | j                  ||f        |j                  \  }}|dk7  xr |j                  d   dk(  }|dk7  xr |j                  d   dk(  }|j                  |j                  }	}t        j                  |j                  | j                        }|j                  dk(  ry |r^t        j                  t        j                  |      t        |            }t        j                  |	|      }	t        j                  ||      }|r^t        j                  ||      }t        j                  t        j                  |      t        |	            }	t        j                  ||      }| j                  |||	f   |||	f   f      \  }
}| j                  |
||       y )Nr   r   r/   )
_zero_manyrF   r3   r   r   rJ   rU   rA   r0   r   r   r   rM   r   rc  )rZ   r   r   r   r\   r]   broadcast_rowbroadcast_colrcry   rz   s               rc   _set_arrayXarray_sparsez"_cs_matrix._set_arrayXarray_sparse  sF   S#J/0yy1Q21771:?Q21771:?uuaee1JJqvvTZZ066Q;		"))A,A/A1A1A		!QA		!c!f-A		!QAzz3q!t9c!Q$i011q!Qrp   c           
      N   d| j                   v ry | j                  dk(  rt        d      | j                   \  }}|j                  dk(  }|dk  r|rt        ||z   |      }nt        ||z   |t	        |            }t        j                  | ||z
  | j                  j                        }t        j                  || j                  j                        }n|rt        |||z
        }nt        |||z
  t	        |            }t        j                  || j                  j                        }t        j                  |||z   | j                  j                        }|s|d t	        |       }t        j                  t        j                  || j                              j                         }	|	j                         j                   |j                         j                   k7  r t        j                  |	|j                         }	|	j                  dk(  ry | j                  ||f      \  }}| j                  ||f      \  }}|	j                  }
t        j                   |
| j                  j                        }t#        ||| j$                  | j                  |
|||      }|dk(  r6| j'                          t#        ||| j$                  | j                  |
|||       d|vr|	| j(                  |<   y |dk\  }| j*                  |j-                         z
  | j*                  dz  k  r9|	|   | j(                  ||   <   | }||   }||   }| j/                  |||	|          y | j1                         }|j3                  ||       |j5                  | j                        }|\  | _        | _        | _        }y )Nr   r   z"diagonals cant be set in 1d arraysr/   re   gMbP?)r3   rW   r   r   rM   rJ   r   r@   r0   
atleast_1drU   r   squeezebroadcast_tor   rF   r   r   r?   rP   rA   r   r   _insert_manyr   _setdiagrO   )rZ   valuesr	  r\   r]   	broadcast	max_indexry   rz   r   	n_samplesoffsetsr   rv   r_   r`   rn   s                    rc   ru  z_cs_matrix._setdiag  s   

?99>%&JKKzz1[[A%	q5AqM	Aq#f+6			1"i!m4<<3E3EFA		)4<<+=+=>A 1q5M	1q5#f+6			)4<<+=+=>A		!Q]$,,2D2DEAGSV_FMM"**V4::>?EEG99;		 1 11177+A66Q;zz1a&!1zz1a&!1FF	((9DLL,>,>? At{{DLL)!"Aw0!8!q!T[[$,,	 !W.W!"DIIg1 88dhhj 488e#33'(wDIIgdm$5D$A$AaAdG, **,CLL#++DJJ7F6<3DKty!rp   c                    | j                  | j                        \  }}d }t        j                  t        j                  || j
                  j                              j                         }t        j                  t        j                  || j
                  j                              j                         } |||        |||       ||||fS )Nc                     | j                         }||k\  rt        d| d| d      | j                         }|| k  rt        d| d| d      y )Nzindex (z) out of range (>= r|   z) out of range (< -)rI   
IndexErrorr   )r@   boundrw   s      rc   check_boundsz1_cs_matrix._prepare_indices.<locals>.check_bounds  sc    ++-Ce| 73%/B5'!KLL++-CeV| 73%/B5'!KLL rp   r/   )rF   rG   rJ   rq  rU   r@   r0   r   )rZ   ry   rz   r\   r]   r  s         rc   _prepare_indicesz_cs_matrix._prepare_indices  s    zz$++,1	M MM"**Qdll.@.@ABHHJMM"**Qdll.@.@ABHHJQQ!Qzrp   c           
         | j                  ||      \  }}}}t        j                  t        j                  || j                              j                         }|j                  }t        j                  || j                  j                        }t        ||| j                  | j                  ||||      }|dk(  r6| j                          t        ||| j                  | j                  ||||       d|vr|| j                  |<   yt        d| j                  j                   dt         d       |dkD  }	||	   | j                  ||	   <   |	 }	||	   }||d	k  xx   |z  cc<   ||	   }||d	k  xx   |z  cc<   | j#                  ||||	          y)
zSets value at each (i, j) to x

        Here (i,j) index major and minor respectively, and must not contain
        duplicate entries.
        r/   r   re   Nz%Changing the sparsity structure of a z. is expensive. lil and dok are more efficient.r4   r}   r   )r  rJ   rq  rU   r0   r   r   r   r@   r   r?   rP   rA   r   rS   rT   r
   rt  )
rZ   ry   rz   r   r\   r]   ry  rz  r   rv   s
             rc   rc  z_cs_matrix._set_many  sw    **1a0
1aMM"**Qdjj9:@@BFF	((9DLL,>,>? At{{DLL)!"Aw0!8!q!T[[$,,	 !W. W!"DIIg 89P9P8Q R? ?(Q8 R<D'(wDIIgdm$5D$Aa!eHMH$Aa!eHMHaAdG,rp   c           
         | j                  ||      \  }}}}t        |      }t        j                  || j                  j
                        }t        ||| j                  | j                  ||||      }|dk(  r6| j                          t        ||| j                  | j                  ||||       d| j                  ||dkD     <   y)zSets value at each (i, j) to zero, preserving sparsity structure.

        Here (i,j) index major and minor respectively.
        r/   r   r   re   N)
r  rM   rJ   r   r@   r0   r   r?   rP   rA   )rZ   ry   rz   r\   r]   ry  rz  r   s           rc   rj  z_cs_matrix._zero_many  s    
 **1a0
1aF	((9DLL,>,>? At{{DLL)!"Aw0!8!q!T[[$,,	 !W. ,-		''B,'(rp   c           	         t        j                  |d      }|j                  |d      }|j                  |d      }|j                  |d      }| j                  }| j	                  | j
                  | j                  f| j                  d   |j                  z         }t        j                  | j                  |      | _        t        j                  | j
                  |      | _        t        j                  ||      }t        j                  ||      }g }g }t        j                  |d	      \  }	}
t        j                  |
t        |            }
t        j                  |
      }d
}t        t        |	|
|
dd             D ]  \  }\  }}}| j                  |   }| j                  |   }|j                  | j
                  ||        |j                  | j                  ||        t        j                  ||| ddd   d	      \  }}t        |      ||z
  k(  r)|j                  |||        |j                  |||        nH|j                  ||| ddd   |          |j                  ||| ddd   |          t        |      ||<   |} | j                     }|j                  | j
                  |d        |j                  | j                  |d        t        j                   |      | _        t        j                   |      | _        t        j"                  | j                  j$                  |      } |d
      |d
<   t        j                  | j                        }||	xx   |z  cc<   ||dd t        j&                  ||      | _        |rd| _        | j)                          | j+                  d       y)a:  Inserts new nonzero at each (i, j) with value x

        Here (i,j) index major and minor respectively.
        i, j and x must be non-empty, 1d arrays.
        Inserts each major group (e.g. all entries per row) at a time.
        Maintains has_sorted_indices property.
        Modifies i, j, x in place.
        	mergesort)r   clip)modere   r,   r/   T)return_indexr   r   NrE  Fr:   )rJ   rW  takehas_sorted_indicesrH   r@   r?   r   rU   uniqueappendrM   rl   	enumerateziprA   concatenater   r3   rF  sort_indicesrY   )rZ   ry   rz   r   r   do_sortr^   indices_parts
data_partsui	ui_indptrnew_nnzsprevrn  iijsjerN  rO  uj	uj_indptrnnzsindptr_diffs                          rc   rt  z_cs_matrix._insert_many0  s<    

1;/FF5vF&FF5vF&FF5vF&)) ))4<<*E,0KKOaff,D * G	jjI>zz$,,i@JJq	*JJq	* 
		!$7IIIiQ0	779%(RIabM)JK 	OA|BKK%E;;r?D  eD!9:diid34 IIa2httn4HMB	2w"r'!$$Qr"X.!!!Br(+$$Qr"Xdd^I%>?!!!Br(4R4.";<!"gD#	( BT\\%&12$))EF+, ~~m4NN:.	xx));A,Qggdkk*B8#QRii$/&+D#U+rp   c                 8   | j                   dk(  rE| j                         }| j                  |j                  |j                  ff|j
                  |      S | j                  | j
                        \  }}| j                  }t        j                  t        |      | j                  j                        }t        j                  || j                  |       | j                  ||f      }| j                  | j                  |f| j
                  || j                        S )Nr   r9   r/   r6   )rW   r   rN   rA   r@   r3   rF   rJ   r   rM   r0   r   	expandptrr?   )rZ   r7   r   r  	minor_dimminor_indicesmajor_indicesr   s           rc   r   z_cs_matrix.tocoo|  s    99>**,C&&3;;.'A399SW&XX#zz$**5	9]!34<<;M;MNy$++}E]M:;""YY$djj # 
 	
rp   c                    ||| j                  d      d   }| j                  ||      }|j                  j                  s!|j                  j                  st        d      |j                  j                  r| j                         }|}n| j                         }|j                  }|j                  |j                        \  }}t        |||j                  |j                  |j                  |       |S )Ncfr   z&Output array must be C or F contiguous)rF   _process_toarray_argsr   r   f_contiguousrR   r   r   r   rG   r   r?   r@   rA   )rZ   r   r$  r   r   r\   r]   s          rc   r   z_cs_matrix.toarray  s    ;5=JJt$Q'E((4		&&#))*@*@EFF99!!

AA

AAwwq~~&1Aq!((AIIqvvq9
rp   c                     | j                  | j                        \  }}t        j                  ||| j                  | j
                  | j                         | j                          y)z\Remove zero entries from the array/matrix

        This is an *in place* operation.
        N)rF   rG   r   csr_eliminate_zerosr?   r@   rA   r   rZ   r\   r]   s      rc   r   z_cs_matrix.eliminate_zeros  sG    
 zz$++,1((At{{DLL$))T

rp   returnc                    t        | dd      sd| _        | j                  S t        | d      sOt        t	        j
                  t        | j                        dz
  | j                  | j                              | _	        | j                  S )aZ  Whether the array/matrix has sorted indices and no duplicates

        Returns
            - True: if the above applies
            - False: otherwise

        has_canonical_format implies has_sorted_indices, so if the latter flag
        is False, so will the former be; if the former is found True, the
        latter flag is also set.
        _has_sorted_indicesTF_has_canonical_formatr   )
r   r  r%  boolr   csr_has_canonical_formatrM   r?   r@   has_canonical_formatrZ   s    rc   r  z_cs_matrix.has_canonical_format  s{     t2D9).D& ))) 67(,55$q($++t||E)D% )))rp   r=  c                 8    t        |      | _        |rd| _        y y )NT)r  r  r  rZ   r=  s     rc   r  z_cs_matrix.has_canonical_format  s    %)#Y"&*D# rp   c                    | j                   ry| j                          | j                  | j                        \  }}t	        j
                  ||| j                  | j                  | j                         | j                          d| _         y)zfEliminate duplicate entries by adding them together

        This is an *in place* operation.
        NT)
r  r  rF   rG   r   csr_sum_duplicatesr?   r@   rA   r   r  s      rc   rP   z_cs_matrix.sum_duplicates  sg    
 $$zz$++,1''1dkk4<<S

$(!rp   c                     t        | d      sOt        t        j                  t	        | j
                        dz
  | j
                  | j                              | _        | j                  S )zWhether the indices are sorted

        Returns
            - True: if the indices of the array/matrix are in sorted order
            - False: otherwise
        r  r   )r%  r  r   csr_has_sorted_indicesrM   r?   r@   r  r  s    rc   r  z_cs_matrix.has_sorted_indices  sV     t23'+33$q($++t||E(D$ '''rp   c                 $    t        |      | _        y r@  )r  r  r  s     rc   r  z_cs_matrix.has_sorted_indices  s    #'9 rp   c                 F    | j                         }|j                          |S )z?Return a copy of this array/matrix with sorted indices
        )r7   r  )rZ   As     rc   sorted_indicesz_cs_matrix.sorted_indices  s     IIK	rp   c                     | j                   sTt        j                  t        | j                        dz
  | j                  | j
                  | j                         d| _         yy)z9Sort the indices of this array/matrix *in place*
        r   TN)r  r   csr_sort_indicesrM   r?   r@   rA   r  s    rc   r  z_cs_matrix.sort_indices  sL     &&))#dkk*:Q*>*.,,		C&*D# 'rp   c                    | j                  | j                        d   }t        | j                        |dz   k7  rt	        d      t        | j
                        | j                  k  rt	        d      t        | j                        | j                  k  rt	        d      t        | j
                  d| j                         | _        t        | j                  d| j                         | _        y)z8Remove empty space after all non-zero elements.
        r   r   z index pointer has invalid lengthz)indices array has fewer than nnz elementsz&data array has fewer than nnz elementsN)	rF   rG   rM   r?   rR   r@   r   rA   r   )rZ   r  s     rc   r   z_cs_matrix.prune  s     JJt001!4	t{{y1},?@@t||txx'HIItyy>DHH$EFF#DLL$(($;< 9DHH!56	rp   c                    t        || j                        }t        | d      rw| j                  \  }}t	        |d   |      \  }}t	        |d   |      \  }}|s|rt        d| j                   d|       | j                  d   |z  | j                  d   |z  }	}nG| j                  t        |      dkD  r|nd|d   f      \  }}| j                  | j                        \  }}	||k  rZ| j                  d | j                  |    | _
        | j                  d | j                  |    | _        | j                  d |dz    | _        n[||kD  rVt        j                  | j                  |dz         | _        | j                  |dz   d  j                  | j                  |          ||	k  r| j                  |k  }
t        j                   |
      s| j                  |
   | _
        | j                  |
   | _        | j#                  t        j$                  |
      \  }}| j                  j                  d       || j                  dd  |<   t        j&                  | j                  | j                         || _        y )Nr*   r"  r   r   zshape must be divisible into z blocks. Got rE  )r!   rE   r%  r"  divmodrR   r3   rF   rM   rG   r@   r?   rA   rJ   resizer   rs   r&  r'  rF  rB   )rZ   r3   bmbnnew_Mrmnew_Nrnr\   r]   rv   r*  r=  s                rc   r  z_cs_matrix.resize  s   EDNN;4%^^FBuQx,IE2uQx,IE2R #@@P#0"9 : :::a=B&

1(;qA::s5z!|e!U1XOLE5::d//0DAq19<<(;U);<DL		"54;;u#56DI++juqy1DKQY))DKK;DKKKA$$T[[^419<<%'D66$<#||D1 IIdO	#'#5#5bffd#C S  #/2AB,		$++4;;7rp   c                 :   |r[| j                  || j                  j                         | j                  j                         f| j                  |j
                        S | j                  || j                  | j                  f| j                  |j
                        S )zReturns a matrix with the same sparsity structure as self,
        but with different data.  By default the structure arrays
        (i.e. .indptr and .indices) are copied.
        r2   )rS   r@   r7   r?   r3   r0   )rZ   rA   r7   s      rc   r   z_cs_matrix._with_data:  s    
 >>4):):)<#';;#3#3#5#7(,

(,

 " 4 4
 >>4t{{"C(,

$** " F Frp   c                 8   | j                  |      }t        t        | j                  |z   | j                  z         }| j                  |j                  z   }| j                  | j                  | j                  |j                  |j                  f|      }t        j                  | j                  j                  |      }t        j                  ||      }g d}||v r&t        j                  |t        j                        }	n5t        j                  |t        | j                  |j                              }	| j                  \  }
} ||
|t        j                  | j                  |      t        j                  | j                  |      | j                   t        j                  |j                  |      t        j                  |j                  |      |j                   |||	       | j                  |	||f| j                        }|j#                          |S )z5apply the binary operation fn to two sparse matrices.r,   r/   )r   r   r   r   r   r   )rS   r   r   r=   r   rH   r?   r@   rJ   r   r3   r   r   r0   rG   rU   rA   r   )rZ   r   r   r   maxnnzr^   r?   r@   bool_opsrA   r\   r]   r  s                rc   r   z_cs_matrix._binoptH  s   u% \4;;#3dkk#ABEII%))4;;%*\\5==+B+1 * 3	 $++++9=((63;>88F"((3D88F&U[[*IJD  1
1a::dkk3::dll)499::ell)4::emm95::7D	" NND'62$**NE		rp   c                 .   |j                   | j                   k7  rt        d      | j                  |d      }t        j                  |j
                  t        j                        rt        j                  | j                   | j
                        }|j                  t        j                         |j                         }| j                  dk(  r|d   f}d||<   |j                         }|j                  ||j                  <   | j                  |      S |}|S )z?
        Divide this matrix by a second sparse matrix.
        r   _eldiv_r/   r   re   r   )r3   rR   r   rJ   
issubdtyper0   inexactr   r   nannonzerorW   r   rA   r   r   )rZ   r   rm  r$  r   s        rc   _divide_sparsez_cs_matrix._divide_sparsek  s     ;;$**$233LL	*=="**-
 ((4::TZZ8CHHRVV]]_FyyA~ *CK	AFFCM??3'' CJrp   c                    | j                   |k(  r|r| j                         S | S t        || j                        }t	        | j                   |      |k7  rt        d      t        | j                         dk(  rt        |      dk(  r| j                          | j                  dk(  r| j                  || j                  d      S |d   }t        j                  || j                  d         }t        j                  d|      }t        j                  d|g      }| j!                  |||f|d      S | j"                  }t        |      dk7  rt        |      }t        d	| d
      | j                  dk(  r| j                  || j                  d      S | j                          | j%                  |      \  }	}| j%                  |      \  }
}t'        d |D              rst        j                  |	|z  | j                  d         }t        j(                  t        j                  |      |	      }t        j                  dt        |      dz   |      }n|
dk(  r|||k(  rwt        j(                  | j                  |	      }t        j(                  | j*                  |	      }t        j                  dt        |      dz   t        | j                              }nu|dk(  rp|
|	k(  rkt        j,                  | j                  |      }t        j(                  t        j                  |      t        | j                              }| j.                  |z  }| j                  f|d      S )Nr*   zcannot be broadcastr   r   F)r0   r7   r   r1   z0CSR/CSC broadcast_to cannot have shape >2D. Got Dc              3   &   K   | ]	  }|d k(    ywr5  r   ).0ss     rc   	<genexpr>z+_cs_matrix._broadcast_to.<locals>.<genexpr>  s     )!qAv)s   )r3   r7   r!   rE   r#   rR   rM   rP   r   rS   r0   rJ   fullrA   r   rQ   _csr_containerrG   rF   rs   r   r@   r   r?   )rZ   r3   r7   r]   rA   r@   r?   	old_shaperW   r\   r   r   s               rc   _broadcast_toz_cs_matrix._broadcast_to  s   ::"&499;0D0ET^^=DJJ.%7233tzz?aCJ!O!xx1}~~e4::E~JJaA771diil+Dii!nGXXq!f%F&&gv'>eRW&XX %%	u:?u:DOPTvUVWXX88q=>>%tzz>FFzz% 1I&B)y))771q5$))A,/DggbiilA.GYYq#d)a-3F1Wq77499a(DggdllA.GYYq#d)a-TYY@F1Wq99TYY*DggbiilC		N;G[[1_F~~tWf5U~OOrp   )NNFr@  )T)r   )NNNF)NN)@rT   
__module____qualname____doc__r<   ro   r   rr   rY   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r  r  r   r   r&  r3  r;  r>  rC  rA  r8  rB  r9  r7  re  rh  ro  ru  r  rc  rj  rt  r   r   r   propertyr  r  setterrP   r  r  r  r   r  r   r   r  r  r   rp   rc   r&   r&      s`   V,T V,p3$ oo--GO34 $1199M7-F""H "D!"F9988	3-.y~
7 :x	 ''//H9.O oo--GOO oo--GOF. ++%%CK"4*<8H
;@&;P%;NE <*   2B=H"%-N-(F,X
 MM))EM" oo--GO *d * *,   + + !+
) (D ( ( -d - -+7!F ^^++FNF!F80Prp   r&   c                     ddl m}m} |r|n|}t        |       }t	        |      }t        j                  |dz   |      }|dd } || ||f||f      S )zbuild diagonal csc_array/csr_array => self._csr_container

    Parameter `data` should be a raveled numpy array holding the
    values on the diagonal of the resulting sparse matrix.
    r   )	csr_array
csr_matrixr,   r/   Nre   r   )_csrr  r  rM   r"   rJ   r   )rA   r   r  r  r]   r^   r?   r@   s           rc   r   r     sZ     ,%	:ID	Aq)IYYq1uI.FSbkGdGV,QF;;rp   c                 2   | d|}}||fS t        | t              r5| j                  |      \  }}}|dk7  rt        d      t	        ||      }||fS t        |       r3| dk  r| |z  } | | dz   }}|dk  s||kD  rt        d| d| d|       ||fS t        d      )Nr   r   z$slicing with step != 1 not supportedzindex out of bounds: 0 <= z < z <= zexpected slice or scalar)rC   rL  r@   rR   r   r   r}  	TypeError)slnumr]  r^  strides        rc   r\  r\    s    	zCB r6M 
B	CBQ;CDDR[ r6M 
26#IBR!VB6R#X9"SDNOO r6M 233rp   r  )1r  __all__warningsr   rt   r   numpyrJ   scipy._lib._utilr   r   _baser   r   r	   r
   _datar   r    r   r   r   r   r   r   r   r   r   _indexr   _sputilsr   r   r   r   r   r   r   r   r   r    r!   r"   r#   r$   r&   r   r\  r   rp   rc   <module>r     sr    D
     9 F F . A A A + + + +_P}j _PD-<"rp   