
    ^j                        d 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	m
Z
mZmZmZmZmZmZmZ ddlmZmZ ddlmZ g dZ G d	 d
e      Z G d de      Z G d de      Zi dddgdddgdddgdddgdddgdddgdd d!gd"d#d$gd%d&d'gd(d)d*gd+d,d-gd.d/d0gd1d2d3gd4d5d6gd7d8d9gd:d;d<gd=d>d?gd@dAgdBdCgdDdEgdFZ eej<                  ej>                  ej@                  ejB                  ejD                  ejF                  ejH                  ejJ                  ejL                  ejN                  ejP                  ejR                  ejT                  ejV                  ejX                  ejZ                  ej\                  ej^                  g      Z0dGZ1ejd                  ejf                  ejf                  ejd                  ejh                  ejj                  ejj                  ejh                  ejl                  ejn                  ejn                  ejl                  iZ8ejd                  dHejj                  dIejn                  dJejf                  dKejl                  dLejh                  dMiZ9 G dN dOe      Z: G dP dQ      Z;e:j                   e;_         dR Z<y)SzBase class for sparse matrices    )warnN   )
asmatrixcheck_shapeget_sum_dtypeisdenseisscalarlike_todatamatrixvalidateaxisgetdtypeis_pydata_spmatrix)	SparseABCissparse)spmatrix)
isspmatrixr   sparraySparseWarningSparseEfficiencyWarningc                       e Zd ZdZy)r   z(General warning for :mod:`scipy.sparse`.N__name__
__module____qualname____doc__     K/opt/ringagent/.cad-venv/lib/python3.12/site-packages/scipy/sparse/_base.pyr   r      s    2r   r   c                       e Zd Zy)SparseFormatWarningN)r   r   r   r   r   r   r    r       s    r   r    c                       e Zd ZdZy)r   zSThe warning emitted when the operation is
    inefficient for sparse matrices.
    Nr   r   r   r   r   r      s     	r   r   csczCompressed Sparse ColumncsrzCompressed Sparse Rowdok   zDictionary Of Keyslil   zList of Listsdod   zDictionary of Dictionariessss   zSymmetric Sparse Skylinecoo   
COOrdinatelba   zLinpack BAndedegd   z#Ellpack-itpack Generalized Diagonaldia	   DIAgonalbsr
   zBlock Sparse Rowmsr   zModified compressed Sparse Rowbsc   zBlock Sparse Columnmsc   z!Modified compressed Sparse Columnssk   zSymmetric SKylinensk   zNonsymmetric SKylinejad   zJAgged Diagonal   zUnsymmetric Sparse Skyline   zVariable Block Row   	Undefined)ussvbrund2   z==z>=z<=z!=><c                      e Zd ZU dZdZdZdZeedf   e	d<   e
defd       Ze
d	        Ze
d
        Ze
d        Ze
d        Ze
d        Ze
d        Ze
d        Ze
d        ZdddZe
d        ZddddZd ZdudZed        Zed        Zd Zd Zd Zdvd Zdvd!Z e
defd"       Z!e
defd#       Z"e
de#fd$       Z$e
d%        Z%e
d&        Z&e
d'        Z'e
d(        Z(d) Z)d* Z*d+ Z+d, Z,dwd-Z-d. Z.d/ Z/d0 Z0d1 Z1d2 Z2dvd3Z3dwd4Z4d5 Z5d6 Z6d7 Z7d8 Z8d9 Z9d: Z:d; Z;d< Z<dxd=Z=d> Z>d? Z?d@ Z@dA ZAdB ZBdC ZCdD ZDdE ZEdF ZFdG ZGdH ZHdI ZIdJ ZJdK ZKdL ZLdM ZMdN ZNdO ZOdP ZPddQdRZQdS ZRdT ZSdU ZTdV ZUdW ZVdX ZWdY ZXdZ ZYdyd[ZZdzd\Z[dzd]Z\e[j                  e\_        d^ Z]d_ Z^d` Z_da Z`db Zad{dcZbd{ddZcdwdeZddwdfZedwdgZfdwdhZgdwdiZhdydjZidwdkZjdl Zkd|dmZld|dnZmdxdoZndxdpZodxdqZpdr Zqds Zrd}dtZsy)~_spbasez This class provides a base class for all sparse array and matrix classes.
    It cannot be instantiated.  Most of the work is provided by subclasses.
    g333333$@rJ   )r%   .	_allow_ndreturnc                 ,    t        | j                        S N)len_shapeselfs    r   ndimz_spbase.ndim^   s    4;;r   c                 H    | j                   }t        |      dk(  rd|d   fS |S )Nr   )rU   rT   )rW   ss     r   _shape_as_2dz_spbase._shape_as_2db   s(    KK Vq[1R5z/a/r   c                     ddl m} |S )Nr   )	bsr_array)_bsrr^   )rW   r^   s     r   _bsr_containerz_spbase._bsr_containerg       #r   c                     ddl m} |S )Nr   )	coo_array)_coorc   )rW   rc   s     r   _coo_containerz_spbase._coo_containerl   ra   r   c                     ddl m} |S )Nr   )	csc_array)_cscrg   )rW   rg   s     r   _csc_containerz_spbase._csc_containerq   ra   r   c                     ddl m} |S )Nr   )	csr_array)_csrrk   )rW   rk   s     r   _csr_containerz_spbase._csr_containerv   ra   r   c                     ddl m} |S )Nr   )	dia_array)_diaro   )rW   ro   s     r   _dia_containerz_spbase._dia_container{   ra   r   c                     ddl m} |S )Nr   )	dok_array)_dokrs   )rW   rs   s     r   _dok_containerz_spbase._dok_container   ra   r   c                     ddl m} |S )Nr   )	lil_array)_lilrw   )rW   rw   s     r   _lil_containerz_spbase._lil_container   ra   r   Nmaxprintc                    d | _         | j                  j                  dk(  rt        d      t	        | t
              r t        j                  |      rt        d      |t        | _	        y || _	        y )NrO   z7This class is not intended to be instantiated directly.zEscipy sparse array classes do not support instantiation from a scalar)
rU   	__class__r   
ValueError
isinstancer   npisscalarMAXPRINTr{   )rW   arg1r{   s      r   __init__z_spbase.__init__   sf    >>""i/ = > >dG$T):W  %-$4(r   c                     | j                   S rS   )rU   rV   s    r   shapez_spbase.shape   s    {{r   CFordercopyc                    t        || j                  t        dd            }|| j                  k(  r|r| j                         S | S | j	                  |      j                  ||d      S )a  reshape(self, shape, order='C', copy=False)

        Gives a new shape to a sparse array/matrix without changing its data.

        Parameters
        ----------
        *shape : int
            Integers specifying the new shape.
            The new shape should be compatible with the original shape.
        order : {'C', 'F'}, optional
            Read the elements using this index order. 'C' means to read and
            write the elements using C-like index order; e.g., read entire first
            row, then second row, etc. 'F' means to read and write the elements
            using Fortran-like index order; e.g., read entire first column, then
            second column, etc.
        copy : bool, optional
            Indicates whether or not attributes of self should be copied
            whenever possible. The degree to which attributes are copied varies
            depending on the type of sparse array being used.

        Returns
        -------
        reshaped : sparse array/matrix
            A sparse array/matrix with the given `shape`, not necessarily of the same
            format as the current object.

        See Also
        --------
        numpy.reshape : NumPy's implementation of 'reshape' for ndarrays
        r   A   )allow_ndr   Fr   )r   r   ranger   tocooreshape)rW   r   r   r   s       r   r   z_spbase.reshape   s`    D E4::aEDJJyy{"zztz$,,U%e,LLr   c                 D    t        t        |       j                   d      )a  Resize the array/matrix in-place to dimensions given by ``shape``.

        Any elements that lie within the new shape will remain at the same
        indices, while non-zero elements lying outside the new shape are
        removed.

        Parameters
        ----------
        *shape : int
            number of rows and columns in the new array/matrix

        Notes
        -----
        The semantics are not identical to `numpy.ndarray.resize` or
        `numpy.resize`. Here, the same data will be maintained at each index
        before and after reshape, if that index is within the new bounds. In
        numpy, resizing maintains contiguity of the array, moving elements
        around in the logical array but not within a flattened representation.

        We give no guarantees about whether the underlying data attributes
        (arrays, etc.) will be modified in place or replaced with new objects.
        z.resize is not implemented)NotImplementedErrortyper   )rW   r   s     r   resizez_spbase.resize   s)    0 "Dz""##=>@ 	@r   c                     t        |      }| j                  |k7  r;| j                         j                  |||      j	                  | j
                        S |r| j                         S | S )aj  Cast the array/matrix elements to a specified type.

        Parameters
        ----------
        dtype : str or numpy dtype
            Typecode or data-type to which to cast the data.
        casting : {'no', 'equiv', 'safe', 'same_kind', 'unsafe'}, optional
            Controls what kind of data casting may occur.
            Defaults to 'unsafe' for backwards compatibility.
            'no' means the data types should not be cast at all.
            'equiv' means only byte-order changes are allowed.
            'safe' means only casts which can preserve values are allowed.
            'same_kind' means only safe casts or casts within a kind,
            like float64 to float32, are allowed.
            'unsafe' means any data conversions may be done.
        copy : bool, optional
            If `copy` is `False`, the result might share some memory with this
            array/matrix. If `copy` is `True`, it is guaranteed that the result and
            this array/matrix do not share any memory.

        Returns
        -------
        array/matrix
            This array/matrix with data type `dtype`.
        )castingr   )r   dtypetocsrastypeasformatformatr   )rW   r   r   r   s       r   r   z_spbase.astype   sa    6 ::::<&&wT ' 33;8DKK3HI99;Kr   c                 f    t        | t              rt        j                  |fi |S t	        |fi |S rS   )
issubclassr   r   asarrayr   clsXkwargss      r   _ascontainerz_spbase._ascontainer  s0    c7#::a*6**A(((r   c                 f    t        | t              rt        j                  |fi |S t	        |fi |S rS   )r   r   r   arrayr   r   s      r   
_containerz_spbase._container
  s0    c7#88A(((!&v&&r   c                     g d}| j                   j                  |v r| S |D ]9  }| j                   t        j                   |      k  s&| j                  |d      c S  t	        d| j                   j
                   d      )z6Upcast array to a floating point format (if necessary))fdFDFr   zcannot upcast [z] to a floating point format)r   charr   r   	TypeErrorname)rW   fp_typesfp_types      r   	_asfptypez_spbase._asfptype  sz     (::??h&K# <::'!22;;wU;;;< !$**//!22NO r   c              #   T   K   t        | j                  d         D ]	  }| |     y w)Nr   )r   r   )rW   rs     r   __iter__z_spbase.__iter__!  s+     tzz!}% 	Aq'M	s   &(c                     | j                   S )z3Maximum number of elements to display when printed.rz   rV   s    r   _getmaxprintz_spbase._getmaxprint%  s    }}r   c                 L    | j                   j                  }t        d| d      )a  Number of non-zero entries.

        This is equivalent to::

            np.count_nonzero(a.toarray(), axis=axis)

        Unlike the nnz property, which return the number of stored
        entries (the length of the data attribute), this method counts the
        actual number of non-zero entries in data.

        Duplicate entries are summed before counting.

        Parameters
        ----------
        axis : {-2, -1, 0, 1, None} optional
            Count nonzeros for the whole array, or along a specified axis.

            .. versionadded:: 1.15.0

        Returns
        -------
        numpy array
            A reduced array (no axis `axis`) holding the number of nonzero values
            for each of the indices of the nonaxis dimensions.

        Notes
        -----
        If you want to count nonzero and explicit zero stored values (e.g. nnz)
        along an axis, two fast idioms are provided by `numpy` functions for the
        common CSR, CSC, COO formats.

        For the major axis in CSR (rows) and CSC (cols) use `np.diff`:

            >>> import numpy as np
            >>> import scipy as sp
            >>> A = sp.sparse.csr_array([[4, 5, 0], [7, 0, 0]])
            >>> major_axis_stored_values = np.diff(A.indptr)  # -> np.array([2, 1])

        For the minor axis in CSR (cols) and CSC (rows) use `numpy.bincount` with
        minlength ``A.shape[1]`` for CSR and ``A.shape[0]`` for CSC:

            >>> csr_minor_stored_values = np.bincount(A.indices, minlength=A.shape[1])

        For COO, use the minor axis approach for either `axis`:

            >>> A = A.tocoo()
            >>> coo_axis0_stored_values = np.bincount(A.coords[0], minlength=A.shape[1])
            >>> coo_axis1_stored_values = np.bincount(A.coords[1], minlength=A.shape[0])

        Examples
        --------

            >>> A = sp.sparse.csr_array([[4, 5, 0], [7, 0, 0]])
            >>> A.count_nonzero(axis=0)
            array([2, 1, 0])
        z"count_nonzero not implemented for .r}   r   r   rW   axisclsnames      r   count_nonzeroz_spbase.count_nonzero)  s)    r ..))!$Fwiq"QRRr   c                 L    | j                   j                  }t        d| d      )a6  Number of stored values, including explicit zeros.

        Parameters
        ----------
        axis : {-2, -1, 0, 1, None} optional
            Report stored values for the whole array, or along a specified axis.

        See Also
        --------
        count_nonzero : Number of non-zero entries
        zgetnnz not implemented for r   r   r   s      r   _getnnzz_spbase._getnnze  s(     ..))!$?y"JKKr   c                 "    | j                         S )zNumber of stored values, including explicit zeros.

        See Also
        --------
        count_nonzero : Number of non-zero entries
        r   rV   s    r   nnzz_spbase.nnzt       ||~r   c                 "    | j                         S )zwNumber of stored values.

        See Also
        --------
        count_nonzero : Number of non-zero values.
        r   rV   s    r   sizez_spbase.size~  r   r   c                     | j                   S )zFormat string for matrix.)_formatrV   s    r   r   z_spbase.format  s     ||r   c                 "    | j                         S )z
Transpose.)	transposerV   s    r   Tz	_spbase.T  s     ~~r   c                 p    | j                   x}dk  rt        d| d      |dk(  sJ | j                         S )zMatrix transpose.
        
        See Also
        --------
        scipy.sparse.matrix_transpose : equivalent function
        r%   z0Array must be at least 2-dimensional, but it is z-D)rX   r~   r   )rW   ns     r   mTz
_spbase.mT  sC     NAaOPQsRTUVVAvv~~r   c                 "    | j                         S rS   )_realrV   s    r   realz_spbase.real      zz|r   c                 "    | j                         S rS   )_imagrV   s    r   imagz_spbase.imag  r   r   c                     t         | j                     \  }}t        | t              rdnd}d| d| d| j                   d| j
                   d| j                   dS )	Nr   r   rM   z sparse z of dtype 'z'
	with z stored elements and shape rL   )_formatsr   r   r   r   r   r   )rW   _format_name
sparse_clss       r   __repr__z_spbase.__repr__  sb    !$++.; *4 9Wx
}HZLDJJ< HhhZ:4::,aI	
r   c                    | j                         }| j                         }d }t        |       }| j                  dk(  r|S |dz  }| j                  |kD  r||dz  | |t	        fd|j
                  D              |j                  d        z  }|dz  }|z
  | |t	        fd|j
                  D              |j                   d        z  }|S | ||j
                  |j                        z  }|S )Nc                 d    t        t        d | D         |      }dj                  d |D              S )Nc              3   <   K   | ]  }|j                           y wrS   )tolist).0cs     r   	<genexpr>z1_spbase.__str__.<locals>.tostr.<locals>.<genexpr>  s     9Qahhj9s   
c              3   2   K   | ]  \  }}d | d|   yw)z  	Nr   )r   idxvals      r   r   z1_spbase.__str__.<locals>.tostr.<locals>.<genexpr>  s!     E83r#b.Es   )zipjoin)coordsdatapairss      r   tostrz_spbase.__str__.<locals>.tostr  s.    9&9:DAE99EuEEEr   r   z
  Coords	Values
r%   c              3   (   K   | ]	  }|d    y wrS   r   r   r   halfs     r   r   z"_spbase.__str__.<locals>.<genexpr>  s     :Aq$x:   z
  :	:
c              3   *   K   | ]
  }| d    y wrS   r   r   s     r   r   z"_spbase.__str__.<locals>.<genexpr>  s     ;Qq$y;s   )r   r   reprr   tupler   r   )rW   r{   Ar   outr   s        @r   __str__z_spbase.__str__  s    $$&JJL	F 4j88q=J%%88hq=D5:::AFF5DMJJC<Cd?D5;!((;;QVVTEF^LLC 
 5166**C
r   c                 T    | j                   dk(  r| j                  dk7  S t        d      )N)r   r   r   z\The truth value of an array with more than one element is ambiguous. Use a.any() or a.all().)r   r   r~   rV   s    r   __bool__z_spbase.__bool__  s1    ::88q=  M N Nr   c                     t        d      )Nz:sparse array length is ambiguous; use getnnz() or shape[0])r   rV   s    r   __len__z_spbase.__len__  s     ' ( 	(r   c                     ||| j                   k(  r|r| j                         S | S 	 t        | d|z         }	  ||      S # t        $ r}t	        d| d      |d}~ww xY w# t
        $ r
  |       cY S w xY w)a  Return this array/matrix in the passed format.

        Parameters
        ----------
        format : {str, None}
            The desired sparse format ("csr", "csc", "lil", "dok", "array", ...)
            or None for no conversion.
        copy : bool, optional
            If True, the result is guaranteed to not share data with self.

        Returns
        -------
        A : array/matrix
            This array/matrix in the passed format.
        NtozFormat z is unknown.r   )r   r   getattrAttributeErrorr~   r   )rW   r   r   convert_methodes        r   r   z_spbase.asformat  s      >Vt{{2yy{"H!(tf}!=
(%400 " H 76(,!?@aGH  (%''(s(   A  A!  	A	AA!A43A4c                    t        |      r| j                  |      S | j                  dk  r	 | j                  |      S t        |      s_t        |      sTt        j                  |      }|j                  dk(  r#|j                  t        j                  k(  rt        S 	 |j                   | j                  |j                  k7  rt        d      t        |      rqt        j                  | j                   || j"                           }| j%                         }|j'                  t        j(                        j+                         |_        |S t        |      rk| j-                  dd      j                         }|j-                  dd      j                         }|j/                  |d      j-                  | j                        S t        S # t        $ r" | j                         j                  |      cY S w xY w# t        $ r |}Y Xw xY w)a!  Element-wise multiplication by another array/matrix.

        Parameters
        ----------
        other : sparse array or array_like
            Array to multiply by.

        Returns
        -------
        sparse array/matrix
            Result of element-wise multiplication.
        r'   r   zEinconsistent shapes: >2D multiply() does not yet support broadcastingr   rZ   _elmul_)r	   _mul_scalarrX   _multiply_2d_with_broadcastingr   r   r   r   r   
asanyarrayr   object_NotImplementedr   r~   multiplyr   r   r   viewndarrayravelr   _binopt)rW   otherother_ar   resultcsr_self	csr_others          r   r	  z_spbase.multiply  s    ##E**99q=J::5AA 75>mmE*G||q W]]bjj%@ &%  ::$ 4 5 5 5>;;tyy%*<=DYY[F))BJJ/557FKMe_||Ar*002Ha,224I##Iy9AA$**MM
 "!I " Jzz|BB5IIJ "   s#   G G2 (G/.G/2H Hc                    t        |      sot        |      sdt        |      sYt        j                  |      }|j
                  dk(  r(|j                  t        j                  k(  rt        d      	 |j                   t        |      r |d|      r`|t        j                  k(  rdnd}t        d|j                   d| dt        d	       | j                   || j!                         |            S | j
                  dk  r2| j"                  d
v r| n| j%                         }|j'                  ||      S | j)                  dd      j%                         }|j'                  ||      }|j+                         j)                  | j                        S t        |      r || j-                         |      S t        |      r| j                  |j                  k7  r%t/        d| j                  d|j                        | j
                  dk  r@| j"                  d
v r| n| j%                         }|j1                  |d|j                   d      S | j)                  dd      j%                         }|j)                  dd      j%                         }|j1                  |d|j                   d      }|j+                         j)                  | j                        S t/        d      # t        $ r |}Y nw xY w)Nr   zCmaximum or minimum with an unrecognized array type is not supportedpositivenegativezTaking z with a z" number results in a dense matrix.r'   
stacklevel)r#   r"   r   rZ   zinconsistent shapes self.shape=z other.shape=r   zOperands not compatible.)r   r   r	   r   r  rX   r   r  r   r   r   maximumr   r   r   r}   toarrayr   r   _scalar_binoptr   r   todenser~   r  )	rW   r  np_opr  pos_negcs_selfr  r  r  s	            r   _maximum_minimumz_spbase._maximum_minimum8  sl   75>\%5HmmE*G||q W]]bjj%@ * +H I I  Q(-(;*wu~~.hwi @& &'>1N~~eDLLNE&BCC99q=&*kk^&CdG"11%??<<2.446!00>||~--djj99U^//e_zzU[[( #C

}Nekk^!TUUyy1}"&++"?$TZZ\u%..1A.CDD||Ar*002Ha,224I%%i1U^^4DA1FGF<<>))$**55788; "   s   /K K*)K*c                 B    | j                  |t        j                        S )a  Element-wise maximum between this and another array/matrix.

        Parameters
        ----------
        other : array_like or sparse array
            Array to compare against.

        Returns
        -------
        sparse array/matrix or ndarray
            Result of element-wise minimum. The type depends on `other`:

            - If `other` is a scalar, the result is a sparse array/matrix.
            - If `other` is an ndarray, the result is an ndarray.
        )r  r   r  rW   r  s     r   r  z_spbase.maximumc        $$UBJJ77r   c                 B    | j                  |t        j                        S )a  Element-wise minimum between this and another array/matrix.

        Parameters
        ----------
        other : array_like or sparse array
            Array to compare against.

        Returns
        -------
        sparse array/matrix or ndarray
            Result of element-wise minimum. The type depends on `other`:

            - If `other` is a scalar, the result is a sparse array/matrix.
            - If `other` is an ndarray, the result is an ndarray.
        )r  r   minimumr!  s     r   r$  z_spbase.minimumu  r"  r   c                 @    t        j                  |      r| |z  S | |z  S )a  Ordinary dot product.

        Parameters
        ----------
        other : array_like or sparse array
            Array to take dot product with.

        Returns
        -------
        sparse array/matrix or ndarray
            Result of dot product. The type depends on `other`:

            - If `other` is a scalar, the result is a sparse array/matrix.
            - If `other` is an ndarray, the result is an ndarray.

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

        )r   r   r!  s     r   dotz_spbase.dot  s$    4 ;;u%<%<r   c                 D    | j                         j                  ||      S )a  Element-wise power.

        Parameters
        ----------
        n : scalar
            `n` is a non-zero scalar (nonzero avoids dense ones creation)
            If zero power is desired, special case it to use `numpy.ones`
        dtype : dtype, optional
            If `dtype` is not specified, the current dtype will be preserved.

        Returns
        -------
        csr array/matrix
            Result of raising all elements to the power `n`.
        r   )r   power)rW   r   r   s      r   r)  z_spbase.power  s       zz|!!!5!11r   c                    | j                   |k(  r|r| j                         S | S | j                         j                  ||      S rS   )r   r   r   _broadcast_to)rW   r   r   s      r   r+  z_spbase._broadcast_to  s:    ::"&499;0D0::<--eT::r   c           
      
   t        |      s{t        |      spt        |      set        |      rt        S t        j                  |      }|j                  dk(  r#|j                  t
        j                  k(  rt        S 	 |j                   t        |      r |d|      st        j                  |      r+| j                  | j                  t
        j                        S | j                  dk  r2| j                  dv r| n| j!                         }|j#                  ||      S | j%                  dd      j!                         }|j#                  ||      }|j'                         j%                  | j                        S t)        d| dt*        |    d	t*        t,        |       d
t.        d       t        j                  |      r>| j                  t        j0                  | j                  t
        j                              S | j                  dk  r|| j                  dv r| n| j!                         }|j#                  |t,        |         }|j                  t        j0                  |j                  t
        j                              }||z
  S | j%                  dd      j!                         }|j#                  |t,        |         }|j                  t        j0                  |j                  t
        j                              }||z
  }|j'                         j%                  | j                        S t        |      r || j3                         |      S t        |      r| j                  |j                  k7  r?|t4        j6                  t4        j8                  fv r|t4        j8                  u S t;        d      | j                  dk  r#| j                  dv r| n| j!                         }|}	n@| j%                  dd      j!                         }|j%                  dd      j!                         }	 |dd      sZ|j=                  |	d|j>                   d      }| j                  dk  r|S |j'                         j%                  | j                        S t)        dt*        |    d	t*        t,        |       d
t.        d       |j=                  |	dt,        |   j>                   d      }|j                  t        j0                  |j                  t
        j                              }||z
  }| j                  dk  r|S |j'                         j%                  | j                        S t        S # t        $ r |}Y w xY w)Nr   r(  r'   )r"   r#   r   rZ   zComparing a sparse matrix with z using z is inefficient. Try using z	 instead.r  zinconsistent shaper   z$Comparing two sparse matrices using ) r   r   r	   r   r  r   r  rX   r   r  r   r   isnanr}   bool_r   r   r  r   r   r   op_symop_negr   onesr  operatoreqner~   r  r   )
rW   r  opr  r  r  r  invall_truecs_others
             r   _comparisonz_spbase._comparison  s?    75>\%5H!%(%%mmE*G||q W]]bjj%@ &%  a<88E?>>$**BHH>EE99q=&*kk^&CdG"11%<<<<2.446!00;||~--djj996ugWVBZL Q2282D1EYP,< 88E?>>"''$**BHH*MNN 99q=&*kk^&CdG!00r
CC&00bhh1WXH#c>)<<2.446--eVBZ@#--bgghnnBHH.UV!C||~--djj99U^dllne,,e_zzU[[( (++x{{33,, !566yy1}"&++"?$TZZ\ ,,q"-335 ==B/557a8 Qr{{m13EF!%QvVFLLN4J4J4::4VV ;F2J< H2282D1EYP,< ooh!F2J4G4G3H0JK",,RWWW]]"((-ST!C!%QvVFLLN4J4J4::4VV "!E "   s   ;U U.-U.c                 B    | j                  |t        j                        S rS   )r9  r2  r3  r!  s     r   __eq__z_spbase.__eq__      x{{33r   c                 B    | j                  |t        j                        S rS   )r9  r2  r4  r!  s     r   __ne__z_spbase.__ne__  r<  r   c                 B    | j                  |t        j                        S rS   )r9  r2  ltr!  s     r   __lt__z_spbase.__lt__  r<  r   c                 B    | j                  |t        j                        S rS   )r9  r2  gtr!  s     r   __gt__z_spbase.__gt__  r<  r   c                 B    | j                  |t        j                        S rS   )r9  r2  ler!  s     r   __le__z_spbase.__le__  r<  r   c                 B    | j                  |t        j                        S rS   )r9  r2  ger!  s     r   __ge__z_spbase.__ge__"  r<  r   c                 4    t        | j                               S rS   )absr   rV   s    r   __abs__z_spbase.__abs__%  s    4::<  r   c                 8    t        | j                         |      S )N)ndigits)roundr   )rW   rO  s     r   	__round__z_spbase.__round__(  s    TZZ\733r   c                 @    | j                         j                  |      S rS   )r   _add_sparser!  s     r   rS  z_spbase._add_sparse+      zz|''..r   c                 @    | j                         j                  |      S rS   )r   
_add_denser!  s     r   rV  z_spbase._add_dense.  s    zz|&&u--r   c                 @    | j                         j                  |      S rS   )r   _sub_sparser!  s     r   rX  z_spbase._sub_sparse1  rT  r   c                 (    | j                         |z
  S rS   r  r!  s     r   
_sub_densez_spbase._sub_dense4  s    ||~%%r   c                 (    || j                         z
  S rS   rZ  r!  s     r   _rsub_densez_spbase._rsub_dense7  s    t||~%%r   c                 \   t        |      r |dk(  r| j                         S t        d      t        |      r5|j                  | j                  k7  rt        d      | j                  |      S t        |      r1t        j                  || j                        }| j                  |      S t        S )Nr   z:adding a nonzero scalar to a sparse array is not supportedinconsistent shapes)r	   r   r   r   r   r~   rS  r   r   broadcast_torV  r  r!  s     r   __add__z_spbase.__add__;  s    zyy{"% 'F G Ge_{{djj( !677##E**U^OOE4::6E??5))!!r   c                 $    | j                  |      S rS   )ra  r!  s     r   __radd__z_spbase.__radd__L      ||E""r   c                 \   t        |      r |dk(  r| j                         S t        d      t        |      r5|j                  | j                  k7  rt        d      | j                  |      S t        |      r1t        j                  || j                        }| j                  |      S t        S )Nr   zAsubtracting a nonzero scalar from a sparse array is not supportedr_  )r	   r   r   r   r   r~   rX  r   r   r`  r[  r  r!  s     r   __sub__z_spbase.__sub__O  s    zyy{"% 'F G Ge_{{djj( !677##E**U^OOE4::6E??5))!!r   c                     t        |      r!|dk(  r| j                          S t        d      t        |      r1t	        j
                  || j                        }| j                  |      S t        S )Nr   zAsubtracting a sparse array from a nonzero scalar is not supported)	r	   r   r   r   r   r`  r   r]  r  r!  s     r   __rsub__z_spbase.__rsub___  sf    z		|#% 'H I IU^OOE4::6E##E**!!r   c                    | j                   \  }}|j                  t        j                  u r|j                  |fk(  r| j                  |      S |j                  |dfk(  rQ| j                  |j                               }| j                  dk(  r|j                  d      S |j                  |d      S |j                  dk(  r#|j                  d   |k(  r| j                  |      S t        |      r| j                  |      S d}t        |      rD||j                  d   k7  r!t        | d| d|j                  d    d      | j                  |      S t        j                  |      }|j                  dk(  r#|j                   t        j"                  k(  rt$        S 	 |j                   |j                  dk(  s!|j                  dk(  r|j                  d   dk(  r|j                  d   |k7  r!t        | d| d|j                  d    d      | j                  t        j                  |            }t)        |t        j*                        r| j-                  |      }|j                  dk(  rF|j                  d   dk(  r4| j                  dk(  r|j                  d      }|S |j                  d	d      }|S |j                  dk(  r|j                  d   |k7  r!t        | d| d|j                  d    d      | j                  t        j.                  |            }t)        |t        j*                        r| j-                  |      }|S t        d
      # t&        $ r |}Y w xY w)a  np.array-like matmul & `np.matrix`-like mul, i.e. `dot` or `NotImplemented`

        interpret other and call one of the following
        self._mul_scalar()
        self._matmul_vector()
        self._matmul_multivector()
        self._matmul_sparse()
        r   r%   r   z)matmul: dimension mismatch with signaturez (n,k=z),(k=z
,m)->(n,m)z,1?)->(n,1?)rZ   zcould not interpret dimensions)r\   r}   r   r  r   _matmul_vectorr  rX   r   _matmul_multivectorr	   r  r   r~   _matmul_sparser  r   r  r  r   r   r   r   r   )rW   r  MNr  
err_prefixr  s          r   _matmul_dispatchz_spbase._matmul_dispatchk  s      1??bjj({{qd"**511A&,,U[[];99>!>>!,,~~a++qU[[^q%8//66##E**@
E?EKKN" !l&5Q0@
K  &&u-- --&<<1"**!< "!	KK ::?ejjAo%++a.A2E{{1~" !l&5Q0@M  ((%9F%+**62zzQ5;;q>Q#699>#^^A.F M $^^B2FMZZ1_ {{1~" !l&5Q0@
K  --bjj.?@F%+**62M =>>S  	E	s   M	 	MMc                 $    | j                  |      S rS   r	  r!  s     r   __mul__z_spbase.__mul__      }}U##r   c                 $    | j                  |      S rS   rr  r!  s     r   __rmul__z_spbase.__rmul__  rt  r   c                 @    | j                         j                  |      S rS   )r   r  r!  s     r   r  z_spbase._mul_scalar  rT  r   c                 @    | j                         j                  |      S rS   )r   rj  r!  s     r   rj  z_spbase._matmul_vector      zz|**511r   c                 @    | j                         j                  |      S rS   )r   rk  r!  s     r   rk  z_spbase._matmul_multivector  s    zz|//66r   c                 @    | j                         j                  |      S rS   )r   rl  r!  s     r   rl  z_spbase._matmul_sparse  ry  r   c                 :   t        |      r| j                  |      S 	 |j                         }| j                         j                  |      }|t        u rt        S |j                         S # t        $ r& t	        j
                  |      j                         }Y kw xY wrS   )r	   r  r   r   r   r   rp  r  )rW   r  trrets       r   _rmatmul_dispatchz_spbase._rmatmul_dispatch  s    ##E**3__& .."33B7Cn$%%==?" " 3ZZ&0023s   A+ +,BBc                 P    t        |      rt        d      | j                  |      S Nz0Scalar operands are not allowed, use '*' instead)r	   r~   rp  r!  s     r   
__matmul__z_spbase.__matmul__  s-     / 0 0$$U++r   c                 P    t        |      rt        d      | j                  |      S r  )r	   r~   r  r!  s     r   __rmatmul__z_spbase.__rmatmul__  s-     / 0 0%%e,,r   rdividec                T   t        |      sjt        |      s_t        |      sTt        j                  |      }|j
                  dk(  r#|j                  t        j                  k(  rt        S 	 |j                   t        |      r+|r$t        j                  || j                               S t        j                  | j                  t        j                        r3| j                  t        j                  d      j!                  d|z        S | j!                  d|z        }t        j"                  |      j                  }t        j$                  | j                  t        j&                        rAt        j$                  |t        j&                        r|j                  | j                  d      S |S t        |      rK|r$t        j                  || j                               S | j)                  t        j                  d|            S t        |      r|r|j+                  | d      S | j
                  dk  r| n| j-                  dd      j/                         }| j
                  dk  r|n|j-                  dd      j/                         }t        j                  | j                  t        j                        r!|j                  t        j                  d      }|j1                  |      }| j
                  dk  r|S |j-                  | j                        S t        S # t        $ r |}Y w xY w)Nr   Fr   r   r  r'   rZ   )r   r   r	   r   r  rX   r   r  r  r   r   divider  can_castfloat64r   r  r   
issubdtypeintegerr	  _divider   r   _divide_sparse)	rW   r  r  r  r   scalar_dtyper  r  r  s	            r   r  z_spbase._divide  s>   75>\%5HmmE*G||q W]]bjj%@ &%  yy77{{4::rzz2{{2::E{:FFq5yQQ$$QY/!zz%066MM$**bjj9lBJJ?88DJJU8;;HU^yy77==1e!455e_}}T5}99 $		A4<<23FMMOH"&))a-U]]1b5IPPRI{{4::rzz2#??2::E?B,,Y7F!YY]6Jtzz0JJ "!M "   s   *L L'&L'c                 $    | j                  |      S rS   )r  r!  s     r   __truediv__z_spbase.__truediv__2  rd  r   c                     t         S rS   r  r!  s     r   __rtruediv__z_spbase.__rtruediv__5  s    r   c                 $    | j                          S rS   )r   rV   s    r   __neg__z_spbase.__neg__9  s    

}r   c                     t         S rS   r  r!  s     r   __iadd__z_spbase.__iadd__<      r   c                     t         S rS   r  r!  s     r   __isub__z_spbase.__isub__?  r  r   c                     t         S rS   r  r!  s     r   __imul__z_spbase.__imul__B  r  r   c                     t         S rS   r  r!  s     r   __itruediv__z_spbase.__itruediv__E  r  r   c                 &     | j                   |i |S rS   )r)  )rW   argsr   s      r   __pow__z_spbase.__pow__H  s    tzz4*6**r   c                 H    | j                  |      j                  |d      S )a  
        Reverses the dimensions of the sparse array/matrix.

        Parameters
        ----------
        axes : None, optional
            This argument is in the signature *solely* for NumPy
            compatibility reasons. Do not pass in anything except
            for the default value.
        copy : bool, optional
            Indicates whether or not attributes of `self` should be
            copied whenever possible. The degree to which attributes
            are copied varies depending on the type of sparse array/matrix
            being used.

        Returns
        -------
        p : sparse array/matrix
            The transpose of the array/matrix.

        See Also
        --------
        numpy.transpose : NumPy's implementation of 'transpose' for ndarrays

        Notes
        -----
        If `self` is a `csr_array` or a `csc_array`, then this will return a
        `csc_array` or a `csr_array`, respectively.
        r   F)axesr   )r   r   )rW   r  r   s      r   r   z_spbase.transposeK  s%    < zztz$..Du.EEr   c                     t        j                  | j                  t         j                        r"| j	                  |      j                  d      S |r| j                         S | S )a  Element-wise complex conjugation.

        If the array/matrix is of non-complex data type and `copy` is False,
        this method does nothing and the data is not copied.

        Parameters
        ----------
        copy : bool, optional
            If True, the result is guaranteed to not share data with self.

        Returns
        -------
        A : sparse array/matrix
            The element-wise complex conjugate.
        r   F)r   r  r   complexfloatingr   	conjugater   rW   r   s     r   r  z_spbase.conjugatek  sO      ==R%7%78::4:(222>>99;Kr   c                 &    | j                  |      S )Nr   )r  r  s     r   conjz_spbase.conj  s    ~~4~((r   c                 >    | j                         j                         S rS   )r   r   rV   s    r   r   z_spbase._real      zz|!!##r   c                 >    | j                         j                         S rS   )r   r   rV   s    r   r   z_spbase._imag  r  r   c                 ~    | j                         }|j                  dk7  t        fd|j                  D              S )a  Nonzero indices of the array/matrix.

        Returns
        -------
        row : ndarray
            Row indices of non-zero elements.
        col : ndarray
            Column indices of non-zero elements.

        Examples
        --------
        >>> from scipy.sparse import csr_array
        >>> A = csr_array([[1, 2, 0], [0, 0, 3], [4, 0, 5]])
        >>> A.nonzero()
        (array([0, 0, 1, 2, 2], dtype=int32), array([0, 1, 2, 0, 2], dtype=int32))

        r   c              3   (   K   | ]	  }|     y wrS   r   )r   r   nz_masks     r   r   z"_spbase.nonzero.<locals>.<genexpr>  s     6cS\6r   )r   r   r   r   )rW   r   r  s     @r   nonzeroz_spbase.nonzero  s2    ( JJL&&A+6QXX666r   c                    | j                   dk(  rt        d      | j                  d   }|dk  r||z  }|dk  s||k\  rt        d      | j	                  dg|gdggf|df| j
                        }| |z  }|S )zeReturns a copy of column j of the array, as an (m x 1) sparse
        array (column vector).
        r   z4getcol not provided for 1d arrays. Use indexing A[j]rZ   r   index out of boundsr   r   )rX   r~   r   
IndexErrorri   r   )rW   jrn  col_selectorr  s        r   _getcolz_spbase._getcol  s     99>STT JJrNq5FAq5AF233**QC1#s+<23Qtzz + K$r   c                     | j                   dk(  rt        d      | j                  d   }|dk  r||z  }|dk  s||k\  rt        d      | j	                  dgdg|ggfd|f| j
                        }|| z  S )z^Returns a copy of row i of the array, as a (1 x n) sparse
        array (row vector).
        r   z$getrow not meaningful for a 1d arrayr   r  r  )rX   r~   r   r  rm   r   )rW   irm  row_selectors       r   _getrowz_spbase._getrow  s     99>CDD JJqMq5FAq5AF233**QC1#s+<23Qtzz + Kd""r   c                 F    | j                  | j                  ||            S )a6  
        Return a dense representation of this sparse array.

        Parameters
        ----------
        order : {'C', 'F'}, optional
            Whether to store multi-dimensional data in C (row-major)
            or Fortran (column-major) order in memory. The default
            is 'None', which provides no ordering guarantees.
            Cannot be specified in conjunction with the `out`
            argument.

        out : ndarray, 2-D, optional
            If specified, uses this array as the output buffer
            instead of allocating a new array to return. The
            provided array must have the same shape and dtype as
            the sparse array on which you are calling the method.

        Returns
        -------
        arr : ndarray, 2-D
            An array with the same shape and containing the same
            data represented by the sparse array, with the requested
            memory order. If `out` was passed, the same object is
            returned after being modified in-place to contain the
            appropriate values.
        r   r   )r   r  rW   r   r   s      r   r  z_spbase.todense  s"    8   Es!CDDr   c                 H    | j                  d      j                  ||      S )a  
        Return a dense ndarray representation of this sparse array/matrix.

        Parameters
        ----------
        order : {'C', 'F'}, optional
            Whether to store multidimensional data in C (row-major)
            or Fortran (column-major) order in memory. The default
            is 'None', which provides no ordering guarantees.
            Cannot be specified in conjunction with the `out`
            argument.

        out : ndarray, 2-D, optional
            If specified, uses this array as the output buffer
            instead of allocating a new array to return. The provided
            array must have the same shape and dtype as the sparse
            array/matrix on which you are calling the method. For most
            sparse types, `out` is required to be memory contiguous
            (either C or Fortran ordered).

        Returns
        -------
        arr : ndarray, 2-D
            An array with the same shape and containing the same
            data represented by the sparse array/matrix, with the requested
            memory order. If `out` was passed, the same object is
            returned after being modified in-place to contain the
            appropriate values.
        Fr   r  )r   r  r  s      r   r  z_spbase.toarray  s%    < zzuz%--Es-CCr   c                 F    | j                  |      j                  d      S )a  Convert this array/matrix to Compressed Sparse Row format.

        Parameters
        ----------
        copy : bool, optional
            With ``copy=False``, the data/indices may be shared between this
            array/matrix and the resultant csr_array/matrix.

        Returns
        -------
        csr array/matrix
            The converted array/matrix in CSR format.
        r   F)r   r   r  s     r   r   z_spbase.tocsr  #     zztz$***66r   c                 F    | j                  |      j                  d      S )a  Convert this array/matrix to Dictionary Of Keys format.

        Parameters
        ----------
        copy : bool, optional
            With ``copy=False``, the data/indices may be shared between this
            array/matrix and the resultant dok_array/matrix.

        Returns
        -------
        dok array/matrix
            The converted array/matrix in DOK format.
        r   F)r   todokr  s     r   r  z_spbase.todok/  r  r   c                 F    | j                  d      j                  |      S )aw  Convert this array/matrix to COOrdinate format.

        Parameters
        ----------
        copy : bool, optional
            With ``copy=False``, the data/indices may be shared between this
            array/matrix and the resultant coo_array/matrix.

        Returns
        -------
        coo array/matrix
            The converted array/matrix in COO format.
        Fr   )r   r   r  s     r   r   z_spbase.tocoo?  #     zzuz%+++66r   c                 F    | j                  d      j                  |      S )az  Convert this array/matrix to List of Lists format.

        Parameters
        ----------
        copy : bool, optional
            With ``copy=False``, the data/indices may be shared between this
            array/matrix and the resultant lil_array/matrix.

        Returns
        -------
        lil array/matrix
            The converted array/matrix in LIL format.
        Fr   )r   tolilr  s     r   r  z_spbase.tolilO  r  r   c                 F    | j                  |      j                  d      S )a|  Convert this array/matrix to sparse DIAgonal format.

        Parameters
        ----------
        copy : bool, optional
            With ``copy=False``, the data/indices may be shared between this
            array/matrix and the resultant dia_array/matrix.

        Returns
        -------
        dia array/matrix
            The converted array/matrix in DIA format.
        r   F)r   todiar  s     r   r  z_spbase.todia_  r  r   c                 H    | j                  d      j                  ||      S )a   Convert this array/matrix to Block Sparse Row format.

        Parameters
        ----------
        blocksize : tuple of ints, optional
            When ``blocksize=(R, C)`` is provided, it will be used for construction of
            the bsr_array/matrix.
        copy : bool, option
            With ``copy=False``, the data/indices may be shared between this
            array/matrix and the resultant bsr_array/matrix.

        Returns
        -------
        bsr array/matrix
            The converted array/matrix in BSR format.
        Fr   )	blocksizer   )r   tobsr)rW   r  r   s      r   r  z_spbase.tobsro  s%    " zzuz%++id+KKr   c                 F    | j                  |      j                  d      S )a  Convert this array/matrix to Compressed Sparse Column format.

        Parameters
        ----------
        copy : bool, optional
            With ``copy=False``, the data/indices may be shared between this
            array/matrix and the resultant csc_array/matrix.

        Returns
        -------
        csc array/matrix
            The converted array/matrix in CSC format.
        r   F)r   tocscr  s     r   r  z_spbase.tocsc  r  r   c                 (    | j                  | d      S )zReturns a copy of this array/matrix.

        No data/indices will be shared between the returned value and current
        array/matrix.

        Returns
        -------
        sparse array/matrix
            A copy of this array/matrix.
        Tr   )r}   rV   s    r   r   z_spbase.copy  s     ~~d~..r   c                     t         j                        t         j                        }|Tt	         d      r j                           j                  |d      j                  d|      }|j                  |d      S k j                  dk(  r,t        j                   j                  dg      |xs ||      S t        j                   j                  t                     ||      S t         t              r)t         fd	t         j                        D              }n'd
k(  rd j                   d   fn j                   d   df}|+ j                  t        j"                  ||xs |            }n|j                   |k7  rt%        d       j                  dkD  r j'                  ||      S d
k(  rN j                  t        j(                  d j                   d   f|            }| z  j+                  |      |d<   |S  j                  t        j(                   j                   d   df|            } |z  j+                  |      |d<   |S )a  
        Sum the array/matrix elements over a given axis.

        Parameters
        ----------
        axis : {-2, -1, 0, 1, None} optional
            Axis along which the sum is computed. The default is to
            compute the sum of all the array/matrix elements, returning a scalar
            (i.e., `axis` = `None`).
        dtype : dtype, optional
            The type of the returned array/matrix and of the accumulator in which
            the elements are summed.  The dtype of `a` is used by default
            unless `a` has an integer dtype of less precision than the default
            platform integer.  In that case, if `a` is signed then the platform
            integer is used while if `a` is unsigned then an unsigned integer
            of the same precision as the platform integer is used.

            .. versionadded:: 0.18.0

        out : np.matrix, optional
            Alternative output matrix in which to place the result. It must
            have the same shape as the expected output, but the type of the
            output values will be cast if necessary.

            .. versionadded:: 0.18.0

        Returns
        -------
        sum_along_axis : np.matrix
            A matrix with the same shape as `self`, with the specified
            axis removed.

        See Also
        --------
        numpy.matrix.sum : NumPy's implementation of 'sum' for matrices

        rX   Nsum_duplicatesFr   r   r   r   r   )r   r   c              3   F   K   | ]  }|vsj                   |     y wrS   r   )r   r  r   rW   s     r   r   z_spbase.sum.<locals>.<genexpr>  s      WRVdjjmWs   	!!r   r   r(  z!out dimensions do not match shaper%   .)r   rX   r   r   hasattrr  r   sumr   r   r   r
   r   r   r   r   r   zerosr~   _sum_ndr1  r   )rW   r   r   r   	res_dtypetemp	new_shaper1  s   ``      r   r  z_spbase.sum  s   L Dtyy1 "$**-	t-.##%;;u5;1554tQT5UD;;u5;11 <xx1}vvd//4E<NYTWXX66$++GDM:%SQQg&WU4995EWWI /3dlDJJqM*APQ@RI;##BHHYe>Py$QRCyyI% !DEE99q=<<i55
 4<$$RWWaA-?y%QRDt,,Y7CH
 
 $$RWWdjjmQ-?y%QRDt,,Y7CH
r   c                 6    t        | j                        }t        j                   j                  t        j
                        xs. t        j                   j                  t        j                        }|rt        j                  n j                  } j                  |d      }| t        j                   j                        }nt        j                   fd|D              }|d|z  z  j                  |||      }|||j                  |d      S |S )a   
        Compute the arithmetic mean along the specified axis.

        Returns the average of the array/matrix elements. The average is taken
        over all elements in the array/matrix by default, otherwise over the
        specified axis. `float64` intermediate and return values are used
        for integer inputs.

        Parameters
        ----------
        axis : {-2, -1, 0, 1, None} optional
            Axis along which the mean is computed. The default is to compute
            the mean of all elements in the array/matrix (i.e., `axis` = `None`).
        dtype : data-type, optional
            Type to use in computing the mean. For integer inputs, the default
            is `float64`; for floating point inputs, it is the same as the
            input dtype.

            .. versionadded:: 0.18.0

        out : np.matrix, optional
            Alternative output matrix in which to place the result. It must
            have the same shape as the expected output, but the type of the
            output values will be cast if necessary.

            .. versionadded:: 0.18.0

        Returns
        -------
        m : np.matrix
            Mean along the specified axis.

        See Also
        --------
        numpy.matrix.mean : NumPy's implementation of 'mean' for matrices

        r  Fr   c              3   <   K   | ]  }j                   |     y wrS   r  )r   axrW   s     r   r   z_spbase.mean.<locals>.<genexpr>%  s     <djjn<s   g      ?r  )r   rX   r   r  r   r  r.  r  r   mathprodr   r  )	rW   r   r   r   integralinter_dtype
inter_selfdenomress	   `        r   meanz_spbase.mean  s    L Dtyy1MM$**bjj9 8MM$**bhh7 	 %-bjj$**[[5[9
<IIdjj)EII<t<<ES5[)..DQT.U::e%:00
r   c                 B    | j                         j                  |      S )a  Returns the kth diagonal of the array/matrix.

        Parameters
        ----------
        k : int, optional
            Which diagonal to get, corresponding to elements a[i, i+k].
            Default: 0 (the main diagonal).

            .. versionadded:: 1.0

        Returns
        -------
        d : ndarray
            The specified diagonal of the array/matrix.

        See Also
        --------
        numpy.diagonal : Equivalent numpy function.

        Examples
        --------
        >>> from scipy.sparse import csr_array
        >>> A = csr_array([[1, 2, 0], [0, 0, 3], [4, 0, 5]])
        >>> A.diagonal()
        array([1, 0, 5])
        >>> A.diagonal(k=1)
        array([2, 3])
        k)r   diagonal)rW   r  s     r   r  z_spbase.diagonal,  s    : zz|$$q$))r   c                 B    | j                  |      j                         S )av  Returns the sum along diagonals of the sparse array/matrix.

        Parameters
        ----------
        offset : int, optional
            Which diagonal to get, corresponding to elements a[i, i+offset].
            Default: 0 (the main diagonal).

        Returns
        -------
        scalar
            The sum of the specified diagonal of the array/matrix.
        r  )r  r  )rW   offsets     r   tracez_spbase.traceK  s     }}v}&**,,r   c                     | j                   \  }}|dkD  r||k\  s|dk  r| |k\  rt        d      | j                  t        j                  |      |       y)a  
        Set diagonal or off-diagonal elements of the array/matrix.

        Parameters
        ----------
        values : array_like
            New values of the diagonal elements.

            Values may have any length. If the diagonal is longer than values,
            then the remaining diagonal entries will not be set. If values are
            longer than the diagonal, then the remaining values are ignored.

            If a scalar value is given, all of the diagonal is set to it.

        k : int, optional
            Which off-diagonal to set, corresponding to elements a[i,i+k].
            Default: 0 (the main diagonal).

        r   zk exceeds array dimensionsN)r   r~   _setdiagr   r   )rW   valuesr  rm  rn  s        r   setdiagz_spbase.setdiag[  sO    ( zz1Ea1f!a%QB!G9::bjj(!,r   c                    | j                   \  }}|dk  ry|j                  dk(  r*t        ||z   |      }t        |      D ]  }|| ||z
  |f<    yt        ||z   |t	        |            }|dk  ryt        |d|       D ]  \  }}|| ||z
  |f<    y|j                  dk(  r*t        |||z
        }t        |      D ]  }|| |||z   f<    yt        |||z
  t	        |            }|dk  ryt        |d|       D ]  \  }}|| |||z   f<    y)zZThis part of the implementation gets overridden by the
        different formats.
        r   N)r   rX   minr   rT   	enumerate)rW   r  r  rm  rn  	max_indexr  vs           r   r  z_spbase._setdiagt  s>    zz1q5{{a!QK	y) ,A%+DQN,  !QF4	>%fZi&89 'DAq%&DQN' {{a1Q3K	y) ,A%+DAEN,  1Q3F4	>%fZi&89 'DAq%&DAEN'r   c                     |Q|t        d      |j                  | j                  k7  s|j                  | j                  k7  rt        d      d|d<   |S t        j                  | j                  | j                  |      S )Nz,order cannot be specified if out is not Nonez6out array must be same dtype and shape as sparse arrayg        .)r   r   )r~   r   r   r   r  r  s      r   _process_toarray_argsz_spbase._process_toarray_args  sy    ?   "/ 0 0yyDJJ&#))tzz*A  "0 1 1CHJ88DJJdjjFFr   c                 H    ddl m}  ||||xr t        | t                     S )a  
        Determine index dtype for array.

        This wraps _sputils.get_index_dtype, providing compatibility for both
        array and matrix API sparse matrices. Matrix API sparse matrices would
        attempt to downcast the indices - which can be computationally
        expensive and undesirable for users. The array API changes this
        behaviour.

        See discussion: https://github.com/scipy/scipy/issues/16774

        The get_index_dtype import is due to implementation details of the test
        suite. It allows the decorator ``with_64bit_maxval_limit`` to mock a
        lower int32 max value for checks on the matrix API's downcasting
        behaviour.
        r   )get_index_dtype)_sputilsr  r   r   )rW   arraysmaxvalcheck_contentsr  s        r   _get_index_dtypez_spbase._get_index_dtype  s/    " 	. v% . Pz$7P3PS 	Sr   )unsafeTrS   )Fr  )NF)T)NN)NNN)r   NF)tr   r   r   r   __array_priority__r   rP   r   int__annotations__propertyrX   r\   r`   re   ri   rm   rq   ru   ry   r   r   r   r   r   classmethodr   r   r   r   r   r   r   r   r   strr   r   r   r   r   r   r   r   r   r   r	  r  r  r$  r&  r)  r+  r9  r;  r>  rA  rD  rG  rJ  rM  rQ  rS  rV  rX  r[  r]  ra  rc  rf  rh  rp  rs  rv  r  rj  rk  rl  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  r  r  r  r  r  r   r   r   rO   rO   U   s    G!%IuS#X% c     0 0               *. 	C   %(e )MV@6"H ) ) ' ' :SxL S   c          
  
     
4N((P7"r)9V8$8$ >2$;S"j444444!4/./&&""#" 
"\?|$$/272#$,- ). 2"h#+F@.) $$DL$$70&#LE<DF7 7 7 7 7 L&7 /Rh6r*>- -2'<GSr   rO   c                        e Zd ZdZed        Zy)r   zA namespace class to separate sparray from spmatrix.

    This class serves as the namespace for SciPy sparse array types.
    It cannot be instantiated and is designed as a mixin class.
    c                     ddl m}  || |      S )a  
        Return a parametrized wrapper around the `~scipy.sparse.sparray` type.

        .. versionadded:: 1.16.0

        Returns
        -------
        alias : types.GenericAlias
            A parametrized `~scipy.sparse.sparray` type.

        Examples
        --------
        >>> import numpy as np
        >>> from scipy.sparse import coo_array

        >>> coo_array[np.int8, tuple[int]]
        scipy.sparse._coo.coo_array[numpy.int8, tuple[int]]
        r   )GenericAlias)typesr  )r   argr  s      r   __class_getitem__zsparray.__class_getitem__  s    ( 	'C%%r   N)r   r   r   r   r  r  r   r   r   r   r     s     & &r   r   c                 "    t        | t              S )a0  Is `x` of a sparse matrix type?

    .. warning::

        scipy.sparse is switching to the sparse array interface.

        The ``isspmatrix`` function returns ``False`` for sparse arrays.
        It will remain after the switch to sparse arrays (sparray).
        So this is a future proof way to check for sparray vs spmatrix.
        If you just want to check for sparse, use ``issparse(A)``.
        For more general information about sparrays, see
        :ref:`Migration from spmatrix to sparray <migration_to_sparray>`.
        The switch to sparse arrays will occur no earlier than v1.21.

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

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

    Examples
    --------
    >>> import numpy as np
    >>> from scipy.sparse import csr_array, csr_matrix, isspmatrix
    >>> isspmatrix(csr_matrix([[5]]))
    True
    >>> isspmatrix(csr_array([[5]]))
    False
    >>> isspmatrix(np.array([[5]]))
    False
    >>> isspmatrix(5)
    False
    )r   r   )xs    r   r   r     s    L a""r   )=r   warningsr   r  numpyr   r2  r  r   r   r   r   r	   r
   r   r   r   r   scipy._lib._sparser   r   _matrixr   __all__Warningr   r    r   r   	frozensetsintanarcsinarctansinhtanharcsinharctanhrintsignexpm1log1pdeg2radrad2degfloorceiltruncsqrt _ufuncs_with_fixed_point_at_zeror   r3  r4  r@  rI  rC  rF  r0  r/  rO   r   r   r   r   r   <module>r,     s   $    K K K 3 7	G 	
	- 		m 	EA12 A./A+, A' A34	
 A12 A|$ A'( A<= Az? B*+ B89 B-. B;< B+, B./  B)*!" 45,-$'0 $-
		299bggrww




BGGRWWbhh"**


BHHbggrxx.: $;   
 ++x{{HKK
++x{{HKK
++x{{HKK
> ++tX[[$T
++tX[[#x{{C
A`Si `SF3& &> //&#r   