
    ^jl                         d Z dZg dZddlZddlZddlmZ ddl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  e       Z G d
 de	ee      Zd Z G d dee
      Z G d dee      Zy)zDictionary Of Keys based matrixzrestructuredtext en)	dok_array
dok_matrixisspmatrix_dok    N   )spmatrix)_spbasesparrayissparse)
IndexMixin)isdensegetdtypeisshape	isintlikeisscalarlikeupcastupcast_scalarcheck_shapec                       e Zd ZdZdZd<dddZd Zd=dZd=dZe	j                  j                  e_
        e	j                  j                  e_
        d	 Zd
 Zd=dZd Zd ZefdZd Zd Zd Zd Zd Zd Zd Zd Zd>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(d& Z)d' Z*d( Z+d) Z,d* Z-d+ Z.d, Z/d- Z0d. Z1d/ Z2d0 Z3d1 Z4d2 Z5d? fd3	Z6e	jl                  j                  e6_
        d@d4Z7e	jn                  j                  e7_
        d5 Z8e	jp                  j                  e8_
        e9dAd6       Z:dBd7Z;e	jv                  j                  e;_
        dBd8Z<e	jx                  j                  e<_
        dBd9Z=e	jz                  j                  e=_
        d: Z>e	j|                  j                  e>_
        dCd;Z?e	j~                  j                  e?_
         xZ@S )D	_dok_basedok)r      Nmaxprintc                   t        j                  | ||       t        |t              rQt	        || j
                        r:t        || j
                        | _        i | _        t        |t              | _        y t        |      r|j                  | j                  k(  r|r|j                         n|}n|j                         }||j!                  |d      }|j                  | _        t        |j"                  | j
                        | _        t        |j                        | _        y 	 t%        j&                  |      }|j,                  dkD  rt/        d|j,                   d	      |j,                  d
k(  rZ||j!                  |d      }t1        |      D ci c]  \  }}|dk7  s|| c}}| _        t        |j                        | _        nM| j3                  |||      j                         }	|	j                  | _        t        |	j                        | _        t        |j"                  | j
                        | _        y # t(        $ r}t+        d      |d }~ww xY wc c}}w )Nr   allow_nd)defaultFcopyzInvalid input format.r   zDOK arrays don't yet support zD input.r   r   shapedtype)r   __init__
isinstancetupler   	_allow_ndr   _shape_dictr   floatr"   r
   formatr   todokastyper!   npasarray	Exception	TypeErrorndim
ValueError	enumerate_coo_container)
selfarg1r!   r"   r   r   eivds
             J/opt/ringagent/.cad-venv/lib/python3.12/site-packages/scipy/sparse/_dok.pyr#   z_dok_base.__init__   s   th7dE"wtdnn'M%dT^^DDKDJ!%7DJd^{{dkk)&*tyy{zz| {{5u{5DJ%djj4>>JDK!$**-DJ@zz$' yy1} #@8!TUUyyA~$;;u5;9D/8Itq!!q&adI
%djj1
''E'GMMOWW
%agg.
%djj4>>JDK  @ 78a?@ Js$   +I I"(I"	IIIc                     t        |t              r|j                         }n|}|D ]  \  }}t        |      r|fn
t	        |      }t        |      | j                  k7  rt        d| d      t        d t        || j                        D              rmt        || j                        D ]F  \  }}t        |      st        d|       |dk  rt        d| d      ||k\  s9t        d| d	        | j                  j                  |       y
)a"  Update values from a dict, sparse dok or iterable of 2-tuples like
        ``.items()``.

        Parameters
        ----------
        val : dict, dok_array, iterable of 2-tuples
            The values to update in the dok_array. If a dict or dok_array is
            provided, the keys and values will be taken from it. If an iterable
            of 2-tuples is provided, each tuple should contain a key and a value.
            Keys of the input must be sequences of nonnegative integers less than
            the shape for each axis.
        Index ! length needs to match self.shapec              3   \   K   | ]$  \  }}t        |      xr d |cxk  xr |k  nc  & ywr   N)r   ).0idxmax_idxs      r;   	<genexpr>z#_dok_base.update.<locals>.<genexpr>U   s2       C #51#5g#55s   *,z&integer keys required for update. Got r   znegative index z not allowed in updatezindex z is too large for self.shapeN)r$   dictitemsr   r%   lenr1   
IndexErrorallzipr!   r(   update)r5   valinputskeyvalueindexrB   rC   s           r;   rK   z_dok_base.update?   s    c4 YY[FF  	UJC'nSF%*E5zTYY& 6#.O!PQQ $'tzz$: 
 %(tzz$: ULC$S>(+QRUQV)WXXQw(?3%?U)VWWg~(6#6R)STTU	U" 	

&!    c                 F    |t        d      t        | j                        S )Nz7_getnnz over an axis is not implemented for DOK format.)NotImplementedErrorrG   r(   r5   axiss     r;   _getnnzz_dok_base._getnnzd   s(    %I  4::rQ   c                     | | j                         j                  |      S t        d | j                         D              S )N)rU   c              3   &   K   | ]	  }|d k7    ywr@    )rA   xs     r;   rD   z*_dok_base.count_nonzero.<locals>.<genexpr>n   s     1a161s   )tocoocount_nonzerosumvaluesrT   s     r;   r\   z_dok_base.count_nonzerok   s9    ::<--4-8814;;=111rQ   c                 ,    t        | j                        S N)rG   r(   r5   s    r;   __len__z_dok_base.__len__s   s    4::rQ   c                     || j                   v S r`   r(   r5   rN   s     r;   __contains__z_dok_base.__contains__v   s    djj  rQ   c                :    | j                   j                  ||      S )a  Insert key with a value of default if key is not in the dictionary.

        Parameters
        ----------
        key : int or tuple of int
            The key to look for in the dok_array.
        default : optional
            The value to insert if `key` is not found. Default is None.

        Returns
        -------
        scalar
            The value for `key` if `key` is in the dictionary, else `default`.
        )r(   
setdefaultr5   rN   r   s      r;   rh   z_dok_base.setdefaulty   s     zz$$S'22rQ   c                    | j                   |= y r`   rd   re   s     r;   __delitem__z_dok_base.__delitem__   s    JJsOrQ   c                 8    | j                   j                          y)z$Remove all items from the dok_array.N)r(   clearra   s    r;   rm   z_dok_base.clear   s    

rQ   c                    |t         u r| j                  j                  |      S | j                  j                  ||      S )a"  Remove specified key and return the corresponding value.

        Parameters
        ----------
        key : tuple
            The key to remove from the dok_array.
        default : optional
            The value to return if `key` is not found. If not provided and
            `key` is not found, a KeyError is raised.

        Returns
        -------
        scalar
            The value associated with the removed key.

        Raises
        ------
        KeyError
            If the key is not found and default is not provided.
        )_NoValuer(   popri   s      r;   rp   z_dok_base.pop   s4    * h::>>#&&::>>#w//rQ   c                     t        d      )Nz*reversed is not defined for dok_array type)r0   ra   s    r;   __reversed__z_dok_base.__reversed__   s    DEErQ   c                 x    t        |       j                   dt        |      j                   }t        d|       Nz and z unsupported operand type for |: type__name__r0   r5   other
type_namess      r;   __or__z_dok_base.__or__   ;    T
++,E$u+2F2F1GH
::,GHHrQ   c                 x    t        |       j                   dt        |      j                   }t        d|       rt   ru   rx   s      r;   __ror__z_dok_base.__ror__   r|   rQ   c                 x    t        |       j                   dt        |      j                   }t        d|       rt   ru   rx   s      r;   __ior__z_dok_base.__ior__   r|   rQ   c                 6    | j                   j                         S )al  
        Remove and return a (key, value) pair as a 2-tuple, returned in LIFO (last-in,
        first-out) order.

        Returns
        -------
        key : tuple
            The key of the removed item.
        value : scalar
            The value of the removed item.

        Raises
        ------
        KeyError
            If the dict is empty.
        )r(   popitemra   s    r;   r   z_dok_base.popitem   s    " zz!!##rQ   c                 6    | j                   j                         S )z
        Return a set-like object providing a view on the array's items.

        Returns
        -------
        dict_items
            A view object displaying a list of a array's key-value tuple pairs.
        )r(   rF   ra   s    r;   rF   z_dok_base.items   s     zz!!rQ   c                 6    | j                   j                         S )z
        Return a set-like object providing a view on the dict's keys.

        Returns
        -------
        dict_keys
            A view object displaying a list of all the keys in the dictionary.
        )r(   keysra   s    r;   r   z_dok_base.keys   s     zz  rQ   c                 6    | j                   j                         S )z
        Return an object providing a view on the dict's values.

        Returns
        -------
        dict_values
            A view object displaying a list of all the values in the dictionary.
        )r(   r^   ra   s    r;   r^   z_dok_base.values   s     zz  ""rQ   c                 N   || j                   v r| j                   |   S t        |      r| j                  dk(  r|f}| j                  t        |      k7  rt	        d| d      	 |D ]  }t        |      rJ  	 t        d t        || j                        D              }t        d t        || j                        D              rt	        d      | j                  dk(  r|d	   }| j                   j                  ||      S # t
        t        t        f$ r}t	        d      |d}~ww xY w)
a  This provides dict.get method functionality with type checking.

        Parameters
        ----------
        key : int or tuple of int
            The key to look for in the dok_array.
        default : optional
            The value to return if `key` is not found. Default is 0.0.

        Returns
        -------
        value
            The value associated with `key` if it exists, otherwise `default`.
        r   r=   r>   z%Index must be or consist of integers.Nc              3   :   K   | ]  \  }}|d k  r||z   n|  ywr@   rY   rA   r8   Ms      r;   rD   z _dok_base.get.<locals>.<genexpr>  s#     KdaQUAE)K   c              3   :   K   | ]  \  }}|d k  xs ||k\    ywr@   rY   r   s      r;   rD   z _dok_base.get.<locals>.<genexpr>	  s"     @41aq1uQ@r   zIndex out of bounds.r   )r(   r   r1   rG   rH   AssertionErrorr0   r2   r%   rJ   r!   anyget)r5   rN   r   r8   r7   s        r;   r   z_dok_base.get   s    $**::c?"S>dii1n&C99C vcU*KLMM	M $ |#|$ Kc#tzz6JKK@3sDJJ+?@@34499>a&Czz~~c7++ 	:6 	MDE1L	Ms   #C? 4C? ?D$DD$c                 l    | j                   j                  || j                  j                  d            S Nr   r(   r   r"   rv   )r5   rB   s     r;   _get_intz_dok_base._get_int  s$    zz~~c4::??1#566rQ   c                 ~    t        |j                  | j                  d          }| j                  t	        |            S r   )rangeindicesr!   
_get_arraylist)r5   rB   i_ranges      r;   
_get_slicez_dok_base._get_slice  s0    TZZ]34tG}--rQ   c                 R   t        j                  |      }|j                  dk(  r_| j                  j	                  t        |      | j                  j                  d            }t        j                  || j                        S | j                  |j                  | j                        }|j                         D cg c]  }| j                  j	                  |d        }}|rt        |j                        dk(  r't        |      D ]  \  }}|s	||j                  |<    |S t        j                  t        j                  t        |            |j                        }t        |      dk(  r|d   nt!        | }t!        ||d      D ]  \  }}|s	||j                  |<    |S c c}w )Nr   )styper"   r   T)strict)r-   r.   r1   r(   r   intr"   rv   array_dok_containerr!   ravelrG   r3   unravel_indexarangerJ   )r5   rB   rL   new_dokr8   dok_valsr9   new_idxs           r;   r   z_dok_base._get_array  sT   jjo88q=**..S4::??1+=>C88Ctzz22%%ciitzz%B25))+>QDJJNN1a(>>399~"%h/ -DAq+,a(-  **299S]+CSYYO(+G(9'!*sG}$? -DAq+,a(-  ?s   =#F$c                 p    | j                   j                  ||f| j                  j                  d            S r   r   r5   rowcols      r;   _get_intXintz_dok_base._get_intXint,  s(    zz~~sCj$**//!*<==rQ   c                 @    | j                  t        ||dz         |      S Nr   _get_sliceXsliceslicer   s      r;   _get_intXslicez_dok_base._get_intXslice/  s     $$U3a%8#>>rQ   c                 @    | j                  |t        ||dz               S r   r   r   s      r;   _get_sliceXintz_dok_base._get_sliceXint2  s     $$S%S1W*=>>rQ   c                    |j                  | j                  d         \  }}}|j                  | j                  d         \  }}}t        |||      }	t        |||      }
t        |	      t        |
      f}t        |       d|d   z  |d   z  k\  r| j	                  |	|
      S | j                  || j                        }| j                         D ]  }t        t        |d         |z
  |      \  }}|dk7  s|dk  s||d   k\  r4t        t        |d         |z
  |      \  }}|dk7  s|dk  s||d   k\  re| j                  |   |j                  ||f<    |S )Nr   r   r   r   )r   r!   r   rG   _get_columnXarrayr   r"   r   divmodr   r(   )r5   r   r   	row_startrow_stoprow_step	col_startcol_stopcol_step	row_range	col_ranger!   newdokrN   r8   rijrjs                     r;   r   z_dok_base._get_sliceXslice5  sY   (+DJJqM(B%	8X(+DJJqM(B%	8X)Xx8	)Xx8	YY0 t9E!HuQx//)))Y??$$U$**$=99; 	1C3s1v;2H=EArQw!a%1a=3s1v;2H=EArQw!a%1a=!%CFLLA	1 rQ   c                 D    | j                  |g|j                               S r`   )r   r   r   s      r;   _get_intXarrayz_dok_base._get_intXarrayL  s    %%seSYY[99rQ   c                     | j                  |j                         |g      }|j                  dkD  r|j                  |j                        S |S r   )r   r   r1   reshaper!   )r5   r   r   ress       r;   _get_arrayXintz_dok_base._get_arrayXintO  s?    $$SYY[3%888a<;;syy))
rQ   c                     t        t        |j                  | j                  d                }| j	                  ||      S r   r   r   r   r!   r   r   s      r;   _get_sliceXarrayz_dok_base._get_sliceXarrayU  5    5#++djjm456%%c3//rQ   c                     t        t        |j                  | j                  d                }| j	                  ||      S r   r   r   s      r;   _get_arrayXslicez_dok_base._get_arrayXsliceY  r   rQ   c                    | j                  t        |      t        |      f| j                        }t        |      D ]J  \  }}t        |      D ]7  \  }}| j                  j                  ||fd      }|s'||j                  ||f<   9 L |S )Nr   r   )r   rG   r"   r3   r(   r   )	r5   r   r   r   r8   rr   cr9   s	            r;   r   z_dok_base._get_columnXarray]  s    $$c#hC%9$LcN 	+DAq!# +1JJNNAq61-)*FLLA&+	+
 rQ   c                    t        t        j                  t        j                  ||            \  }}| j	                  |j
                  | j                        }t        j                  t        |j
                  d         t        |j
                  d               D ]8  }| j                  j                  ||   ||   fd      }|s*||j                  |<   : |S )Nr   r   r   )mapr-   
atleast_2dbroadcast_arraysr   r!   r"   	itertoolsproductr   r(   r   )r5   r   r   r8   r   r   rN   r9   s           r;   _get_arrayXarrayz_dok_base._get_arrayXarrayh  s    2==""5"5c3"?@1$$QWWDJJ$?$$U1771:%6aggaj8IJ 	&C

##/3A$%S!	& rQ   c                 `    |r|| j                   |<   y || j                   v r| j                   |= y y r`   rd   )r5   rB   rZ   s      r;   _set_intz_dok_base._set_intt  s.    DJJsODJJ

3 rQ   c                    |j                         }|j                         }t        |      t        |      k7  rHt        |      dk(  r/t        j                  t        |      |d   | j                        }nt        d      t        ||      D ]3  \  }}|r|| j                  |<   || j                  v s'| j                  |= 5 y )Nr   r   r   z*Need len(index)==len(data) or len(data)==1)r   rG   r-   fullr"   r2   rJ   r(   )r5   rB   rZ   idx_setx_setr8   r9   s          r;   
_set_arrayz_dok_base._set_arrayz  s    ))+	w<3u:%5zQGeAhdjjIKLL' 	"DAq !

1djjJJqM		"rQ   c                 h    ||f}|r|| j                   |<   y || j                   v r| j                   |= y y r`   rd   )r5   r   r   rZ   rN   s        r;   _set_intXintz_dok_base._set_intXint  s7    CjDJJsODJJ

3 rQ   c                    t        t        t        |j                                     }t        t        t        |j                                     }|j                         }| j                  j                  t        t        ||      |             t        j                  |dk(        d   D ],  }||   ||   f}| j                  |   dk(  s | j                  |= . y r   )	r   r   r   r   r(   rK   rJ   r-   nonzero)r5   r   r   rZ   r8   rN   s         r;   _set_arrayXarrayz_dok_base._set_arrayXarray  s    3sCIIK()3sCIIK()GGI

#c#smQ/0AF#A& 	$Aq63q6"Czz#!#JJsO		$rQ   c                 f   t        |      rt        | j                  |      }| j                  | j                  |      t        j                  | j                  D cg c]  }t        |       c} D ])  }| j                  j                  |d      |z   }|s%||<   + S t        |      rN|j                  | j                  k7  rt        d      t        | j                  |j                        }| j                  | j                  |      | j                  j                         _        |j                  dk(  r|j                         }ni|j!                         }| j"                  dk(  r$t%        |j&                  d   |j(                        }n&t%        t%        |j&                   |j(                        }t+        j,                  d      5  j                  j/                  fd|D               d d d        S t1        |      r| j3                         |z   S t4        S c c}w # 1 sw Y   S xY w)	Nr   r   z Matrix dimensions are not equal.r   r   ignore)overc              3   8   K   | ]  \  }}||   |z   f  y wr`   rY   )rA   kr9   news      r;   rD   z$_dok_base.__add__.<locals>.<genexpr>  s!      ETQ!SVaZ Es   )r   r   r"   r   r!   r   r   r   r(   r   r
   r2   r   r   r*   rF   r[   r1   rJ   coordsdatar-   errstaterK   r   todenseNotImplemented)r5   ry   	res_dtyper:   rN   aijo_itemsr   s          @r;   __add__z_dok_base.__add__  s   %djj%8I%%djj	%BC ((TZZ*H58*HI #jjnnS!,u4"CH#0 
) e_{{djj( !CDDtzz5;;7I%%djj	%BC

)CI||u$++-99>!%,,q/5::>G!#u||"4ejjAG(+ F		   EW EEF 
	 U^,,.5(C 
 "!/ +I$F 
s   H!%H&&H0c                     | |z   S r`   rY   r5   ry   s     r;   __radd__z_dok_base.__radd__  s    e|rQ   c                     | j                   j                  dk(  rt        d      | j                  | j                  | j                         }|j
                  j                  d | j                         D               |S )Nbz2Negating a sparse boolean matrix is not supported.r   c              3   ,   K   | ]  \  }}|| f  y wr`   rY   )rA   r   r9   s      r;   rD   z$_dok_base.__neg__.<locals>.<genexpr>  s     :TQ!aR:s   )r"   kindrS   r   r!   r(   rK   rF   r5   r   s     r;   __neg__z_dok_base.__neg__  sd    ::??c!%D  !!$**DJJ!?		:TZZ\::
rQ   c                     t        | j                        }| j                  | j                  |      }|j                  j                  fd| j                         D               |S )Nr   c              3   2   K   | ]  \  }}||z  f  y wr`   rY   rA   r   r9   ry   s      r;   rD   z(_dok_base._mul_scalar.<locals>.<genexpr>  s     BTQ1a%i.B   )r   r"   r   r!   r(   rK   rF   r5   ry   r   r   s    `  r;   _mul_scalarz_dok_base._mul_scalar  sO    !$**e4	!!$**I!>		BTZZ\BC
rQ   c                     t         j                  j                        } j                  dk(  rt              rzj                  dk(  r" j                         j                         z  }n. j                         j                         j                  d   z  } |t         fd|D                    S t              r( |t        fd j                         D                    S t        S t        j                   j                  d   |      } j                         D ]  \  \  }}}||xx   ||   z  z  cc<    |S )Nr   r   r   c              3   \   K   | ]#  }j                   |   j                   |   z   % y wr`   rd   )rA   r   ry   r5   s     r;   rD   z+_dok_base._matmul_vector.<locals>.<genexpr>  s&     $RTZZ]U[[^%C$Rs   ),c              3   4   K   | ]  \  }}|   |z    y wr`   rY   r   s      r;   rD   z+_dok_base._matmul_vector.<locals>.<genexpr>  s     $KdaU1X\$K   r   )r   r"   r1   r
   r*   r   r[   r   r]   r   rF   r   r-   zerosr!   )r5   ry   r   r   resultr8   r   r9   s   ``      r;   _matmul_vectorz_dok_base._matmul_vector  s    4::u{{3	 99><<5(99;5D99;)=)=a)@@D $RT$R!RSS $Kdjjl$K!KLL%% $**Q-y9 	&IFQA1IU1X%I	&rQ   c                    t        | j                  j                        }| j                  dk(  r,t        fd| j                  j                         D              S | j                  d   }j                  dk(  r|fn|j                  d   f}t        j                  ||      }| j                         D ]  \  \  }}}||xx   ||   z  z  cc<    |S )Nr   c              3   4   K   | ]  \  }}||   z    y wr`   rY   )rA   r   r9   ry   s      r;   rD   z0_dok_base._matmul_multivector.<locals>.<genexpr>  s     C1q58|Cr  r   r   )	r   r"   r1   r]   r(   rF   r!   r-   r  )	r5   ry   result_dtyper   	new_shaper	  r8   r   r9   s	    `       r;   _matmul_multivectorz_dok_base._matmul_multivector  s    djj%++699>C

0@0@0BCCC JJqM!JJ!OQD!U[[^1D	)<8 	&IFQA1IU1X%I	&rQ   c                     t              r4| j                  j                  fd| j                         D               | S t        S )Nc              3   2   K   | ]  \  }}||z  f  y wr`   rY   r   s      r;   rD   z%_dok_base.__imul__.<locals>.<genexpr>       FAq!e)nFr  r   r(   rK   rF   r   r   s    `r;   __imul__z_dok_base.__imul__  4    JJFFFKrQ   c                    t              rgt        | j                        }| j                  | j                  |      }|j
                  j                  fd| j                         D               |S | j                         z  S )Nr   c              3   2   K   | ]  \  }}||z  f  y wr`   rY   r   s      r;   rD   z(_dok_base.__truediv__.<locals>.<genexpr>  r  r  )	r   r   r"   r   r!   r(   rK   rF   tocsrr  s    `  r;   __truediv__z_dok_base.__truediv__  sh    %djj%8I%%djj	%BCIIFFGJzz|e##rQ   c                     t              r4| j                  j                  fd| j                         D               | S t        S )Nc              3   2   K   | ]  \  }}||z  f  y wr`   rY   r   s      r;   rD   z)_dok_base.__itruediv__.<locals>.<genexpr>  r  r  r  r   s    `r;   __itruediv__z_dok_base.__itruediv__  r  rQ   c                 ,    t         j                  |       S r`   )rE   
__reduce__ra   s    r;   r  z_dok_base.__reduce__  s     t$$rQ   c                 V    | j                   dk(  rt        | 	  |      S t        d      )Nr   z diagonal requires two dimensions)r1   superdiagonalr2   )r5   r   	__class__s     r;   r!  z_dok_base.diagonal  s*    99>7#A&&;<<rQ   c                 &   | j                   dk(  r| j                         S ||dk7  rt        d      | j                  \  }}| j	                  ||f| j
                  |      }|j                  j                  d | j                         D               |S )Nr   )r   r   zvSparse arrays/matrices do not support an 'axes' parameter because swapping dimensions is the only logical permutation.)r"   r   c              3   4   K   | ]  \  \  }}}||f|f  y wr`   rY   )rA   leftrightrL   s       r;   rD   z&_dok_base.transpose.<locals>.<genexpr>!  s"     V3E=D%#E4=#.Vs   )	r1   r   r2   r!   r   r"   r(   rK   rF   )r5   axesr   r   Nr   s         r;   	transposez_dok_base.transpose  s    99>99;>  zz1!!1a&

!F		VVW
rQ   c                     | j                  | j                  | j                        }|j                  j	                  | j                         |S Nr   )r   r!   r"   r(   rK   r   s     r;   r   z_dok_base.copy&  s:    !!$**DJJ!?		$
rQ   c                    t         j                  ||      }t        t        t	        |            t
              rt        d t        | D              }nt        |      dz   f} | |t        |            }||_	        |S )Nc              3   8   K   | ]  }t        |      d z     yw)r   N)max)rA   rB   s     r;   rD   z%_dok_base.fromkeys.<locals>.<genexpr>1  s     <3#c(Q,<s   r   r   )
rE   fromkeysr$   nextiterr%   rJ   r.  rv   r(   )clsiterablerO   tmpr!   r	  s         r;   r/  z_dok_base.fromkeys-  sc    mmHe,d49ou-<#s)<<EX\OEU$u+.rQ   c                    | j                   dk(  r'| j                  | j                  | j                        S | j	                  t        | j                              t        j                  | j                         | j                        }| j                  dkD  rt        | j                          n| j                         f}t        fd|D              }| j                  ||f| j                  | j                        }d|_        |S )	Nr   r   )maxvalr"   countr   c              3   N   K   | ]  }t        j                  |         yw)r7  N)r-   fromiter)rA   ix	idx_dtypennzs     r;   rD   z"_dok_base.tocoo.<locals>.<genexpr>A  s!     Rrr{{2YcBBRs   "%r    T)r=  r4   r!   r"   _get_index_dtyper.  r-   r:  r^   r1   rJ   r   r%   has_canonical_format)r5   r   r   indsr   Ar<  r=  s         @@r;   r[   z_dok_base.tocoo8  s    hh!8&&tzz&DD))TZZ)A	{{4;;=

#F$(IIMsDIIK 		~RTRRvdjj

S!%rQ   c                 *    |r| j                         S | S r`   r   r5   r   s     r;   r+   z_dok_base.todokH  s    99;rQ   c                 z    | j                   dk(  rt        d      | j                  d      j                  |      S )Nr   z%tocsr() not valid for 1d sparse arrayFr   )r1   rS   r[   tocscrC  s     r;   rE  z_dok_base.tocscO  s8    99>%&MNNzzuz%+++66rQ   c                    t        || j                        }t        |      t        | j                        k7  rt        | j
                  dk(  r:|d   }t        | j                        D ]  }||k\  s	| j                  |=  || _        y |\  }}| j                  \  }}||k  s||k  r;t        | j                               D ]  \  }}||k\  s||k\  s| j                  ||f= ! || _        y )Nr   r   )
r   r&   rG   r!   rS   r1   r   r(   r'   r   )r5   r!   newNr8   newMr   r(  r   s           r;   resizez_dok_base.resizeV  s    EDNN;u:TZZ(%%99>9D$**% &9

1&  DK
dzz1!8taxTYY[) )19T	

1a4() rQ   c                 h   t        j                  |      }| j                  |k7  r{| j                  | j                  |      }t        j                  t        | j                  j                               |      }t        t        | j                  |            |_        |S |r| j                         S | S r+  )r-   r"   r   r!   r   r   r(   r^   rE   rJ   r   )r5   r"   castingr   r	  r   s         r;   r,   z_dok_base.astypep  s    ::((5(AF88D!2!2!45UCDDJJ 56FLM99;rQ   )NNFr`   )g        )r   )NF)r   )F)unsafeT)Arw   
__module____qualname___formatr&   r#   rK   rV   r\   r   __doc__rb   rf   rh   rk   rm   ro   rp   rr   r{   r~   r   r   rF   r   r^   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r
  r  r  r  r  r  r!  r)  r   classmethodr/  r[   r+   rE  rJ  r,   __classcell__)r"  s   @r;   r   r      s	   GI%KT %KN#"J2
 oo--GO#1199M!3"  ( 04FIII$&	"	!	#,D7.*>??.:00		 " 
$>,$%=
 ''//H   ))11I
 <<''DL  MM))EM
 MM))EM7
 MM))EM. ^^++FN	 ^^++FNNrQ   r   c                 "    t        | t              S )a  Is `x` of dok_array type?

    .. warning::

       SciPy sparse is shifting from a sparse matrix interface to a sparse
       array interface. In the next few releases we expect to deprecate the
       sparse matrix interface. For documentation of the matrix
       interface, see the :ref:`spmatrix interface docs <spmatrix_api>`.
       For guidance on converting existing code to sparse arrays, see
       :ref:`Migration from spmatrix to sparray <migration_to_sparray>`.

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

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

    Examples
    --------
    >>> from scipy.sparse import dok_array, dok_matrix, coo_matrix, isspmatrix_dok
    >>> isspmatrix_dok(dok_matrix([[5]]))
    True
    >>> isspmatrix_dok(dok_array([[5]]))
    False
    >>> isspmatrix_dok(coo_matrix([[5]]))
    False
    )r$   r   )rZ   s    r;   r   r   ~  s    @ a$$rQ   c                       e Zd ZdZy)r   a+  
    Dictionary Of Keys based sparse array.

    This is an efficient structure for constructing sparse
    arrays incrementally.

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

        dok_array(S)
            with another sparse array or matrix S (equivalent to S.todok())

        dok_array((M,N), [dtype])
            create the array with initial shape (M,N)
            dtype is optional, defaulting to dtype='d'

    Attributes
    ----------
    dtype : dtype
        Data type of the array
    shape : 2-tuple
        Shape of the array
    ndim : int
        Number of dimensions (this is always 2)
    format : str
        Three letter code for the format of the array storage, e.g. 'dok'
    nnz : int
        Number of values stored in the array
    size : int
        Number of values stored in the array
    T : dok_array
        The transpose of the array
    mT : dok_array
        The matrix transpose of the array

    Notes
    -----

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

    - Allows for efficient O(1) access of individual elements.
    - Duplicates are not allowed.
    - Can be efficiently converted to a coo_array once constructed.

    Examples
    --------
    >>> import numpy as np
    >>> from scipy.sparse import dok_array
    >>> S = dok_array((5, 5), dtype=np.float32)
    >>> for i in range(5):
    ...     for j in range(5):
    ...         S[i, j] = i + j    # Update element

    N)rw   rN  rO  rQ  rY   rQ   r;   r   r     s    7rQ   r   c                   H    e Zd ZdZd Zd Z eee      Zd Zd Z	d Z
d Zy	)
r   a  
    Dictionary Of Keys based sparse matrix.

    This is an efficient structure for constructing sparse
    matrices incrementally.

    .. warning::

       SciPy sparse is shifting from a sparse matrix interface to a sparse
       array interface. In the next few releases we expect to deprecate the
       sparse matrix interface. For documentation of the matrix
       interface, see the :ref:`spmatrix interface docs <spmatrix_api>`.
       For guidance on converting existing code to sparse arrays, see
       :ref:`Migration from spmatrix to sparray <migration_to_sparray>`.

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

        dok_matrix(S)
            with another sparse array or matrix S (equivalent to S.todok())

        dok_matrix((M,N), [dtype])
            create the matrix with initial shape (M,N)
            dtype is optional, defaulting to dtype='d'

    Attributes
    ----------
    dtype : dtype
        Data type of the matrix
    shape : 2-tuple
        Shape of the matrix
    ndim : int
        Number of dimensions (this is always 2)
    format : str
        Three letter code for the format of the matrix storage, e.g. 'dok'
    nnz : int
        Number of values stored in the matrix
    size : int
        Number of values stored in the matrix
    T : dok_matrix
        The transpose of the matrix
    mT : dok_matrix
        The matrix transpose

    Notes
    -----

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

    - Allows for efficient O(1) access of individual elements.
    - Duplicates are not allowed.
    - Can be efficiently converted to a coo_matrix once constructed.

    Examples
    --------
    >>> import numpy as np
    >>> from scipy.sparse import dok_matrix
    >>> S = dok_matrix((5, 5), dtype=np.float32)
    >>> for i in range(5):
    ...     for j in range(5):
    ...         S[i, j] = i + j    # Update element

    c                 ~    | j                  |d      j                  | j                        }|j                  | _        y )NFr   )r   asformatr*   __dict__)r5   r!   
new_matrixs      r;   	set_shapezdok_matrix.set_shape   s0    \\%e\4==dkkJ
"++rQ   c                     | j                   S )zGet shape of a sparse matrix.)r'   ra   s    r;   	get_shapezdok_matrix.get_shape$  s    {{rQ   )fgetfsetc                 6    | j                   j                         S r`   )r(   rr   ra   s    r;   rr   zdok_matrix.__reversed__*  s    zz&&((rQ   c                 r    t        |t              r| j                  |j                  z  S | j                  |z  S r`   r$   r   r(   r   s     r;   r{   zdok_matrix.__or__-  .    eY'::++zzE!!rQ   c                 r    t        |t              r| j                  |j                  z  S | j                  |z  S r`   rb  r   s     r;   r~   zdok_matrix.__ror__2  rc  rQ   c                     t        |t              r!| xj                  |j                  z  c_        | S | xj                  |z  c_        | S r`   rb  r   s     r;   r   zdok_matrix.__ior__7  s:    eY'JJ%++%J  JJ%JrQ   N)rw   rN  rO  rQ  r[  r]  propertyr!   rr   r{   r~   r   rY   rQ   r;   r   r     s5    @D, ))4E)"
"
rQ   r   )rQ  __docformat____all__r   numpyr-   _matrixr   _baser   r	   r
   _indexr   _sputilsr   r   r   r   r   r   r   r   objectro   rE   r   r   r   r   rY   rQ   r;   <module>ro     st    %%
7    - - ; ; ; 8g	,T g	,T %H8	7 8v_9 _rQ   