a
    CCCfM                     @   s  d Z ddlmZ ddlZddlmZ ddlmZm	Z	m
Z
mZmZmZmZmZ ddlmZ g dZG d	d
 d
eZG dd deZG dd deZddgddgddgddgddgddgddgddgddgdd gd!d"gd#d$gd%d&gd'd(gd)d*gd+d,gd-d.gd/d0gd1d2gd3d4gd5Zeejejejejejejejej ej!ej"ej#ej$ej%ej&ej'ej(ej)ej*gZ+d6Z,G d7d8 d8Z-G d9d: d:Z.e-j e._ d;d< Z/d=d> Z0dS )?zBase class for sparse matrices    )warnN)VisibleDeprecationWarning   )asmatrixcheck_reshape_kwargscheck_shapeget_sum_dtypeisdenseisscalarlikematrixvalidateaxis)spmatrix)
isspmatrixissparsesparraySparseWarningSparseEfficiencyWarningc                   @   s   e Zd ZdS )r   N__name__
__module____qualname__ r   r   N/var/www/html/django/DPS/env/lib/python3.9/site-packages/scipy/sparse/_base.pyr      s   r   c                   @   s   e Zd ZdS )SparseFormatWarningNr   r   r   r   r   r      s   r   c                   @   s   e Zd ZdS )r   Nr   r   r   r   r   r      s   r   zCompressed Sparse ColumnzCompressed Sparse Row   zDictionary Of Keys   zList of Lists   zDictionary of Dictionaries   zSymmetric Sparse Skyline   Z
COOrdinate   zLinpack BAnded   z#Ellpack-itpack Generalized Diagonal	   ZDIAgonal
   zBlock Sparse Row   zModified compressed Sparse Row   zBlock Sparse Column   z!Modified compressed Sparse Column   zSymmetric SKyline   zNonsymmetric SKyline   zJAgged Diagonal   zUnsymmetric Sparse Skyline   zVariable Block Row   	Undefined)ZcscZcsrZdokZlilZdodZsssZcooZlbaZegdZdiaZbsrZmsrZbscmscZsskZnskZjadZussZvbrund2   c                   @   sh  e Zd ZdZdZdZeedddZedd Z	ed	d
 Z
edd Zedd Zedd Zedd Zedd Zedd ZefddZdd Zdd Zddd Zed!d" Zed#d$ Zd%d& Zd'd( Zd)d* Zd+d, Zdd.d/Zeedd0d1Zeedd2d3Zee dd4d5Z!ee"j#dd6d7Z$ed8d9 Z%ed:d; Z&ed<d= Z'ed>d? Z(d@dA Z)dBdC Z*dDdE Z+e+Z,dFdG Z-ddIdJZ.dKdL Z/dMdN Z0dOdP Z1dQdR Z2ddSdTZ3dUdV Z4dWdX Z5dYdZ Z6d[d\ Z7d]d^ Z8d_d` Z9dadb Z:ddddeZ;dfdg Z<dhdi Z=djdk Z>dldm Z?dndo Z@dpdq ZAdrds ZBdtdu ZCdvdw ZDdxdy ZEdzd{ ZFd|d} ZGd~d ZHdd ZIdd ZJdd ZKdd ZLdd ZMdd ZNdddZOdd ZPdd ZQdd ZRdd ZSdd ZTdd ZUdd ZVdd ZWdd ZXdd ZYdd ZZdddZ[dddZ\dddZ]e\je]_dd Z^dd Z_dd Z`dd Zadd ZbdddZcdddZddddZedddZfdddZgdddZhdddZidddÄZjd ddńZkddǄ ZldddɄZmddd˄Znddd̈́ZodddτZpdddфZqddӄ ZrddՄ Zsddd؄Ztddڄ Zudd܄ Zveddބ evddZwdd Zxdd Zydd ZzdddZ{dd Z|dd Z}dd Z~d-S (  _spbasez This class provides a base class for all sparse arrays.  It
    cannot be instantiated.  Most of the work is provided by subclasses.
    g333333$@r.   )returnc                 C   s
   t | jS N)len_shapeselfr   r   r   ndimG   s    z_spbase.ndimc                 C   s"   | j }t|dkrd|d fS |S )Nr   )r4   r3   )r6   sr   r   r   _shape_as_2dK   s    z_spbase._shape_as_2dc                 C   s   ddl m} |S )Nr   )	bsr_array)Z_bsrr;   )r6   r;   r   r   r   _bsr_containerP   s    z_spbase._bsr_containerc                 C   s   ddl m} |S )Nr   )	coo_array)Z_coor=   )r6   r=   r   r   r   _coo_containerU   s    z_spbase._coo_containerc                 C   s   ddl m} |S )Nr   )	csc_array)Z_cscr?   )r6   r?   r   r   r   _csc_containerZ   s    z_spbase._csc_containerc                 C   s   ddl m} |S )Nr   )	csr_array)Z_csrrA   )r6   rA   r   r   r   _csr_container_   s    z_spbase._csr_containerc                 C   s   ddl m} |S )Nr   )	dia_array)Z_diarC   )r6   rC   r   r   r   _dia_containerd   s    z_spbase._dia_containerc                 C   s   ddl m} |S )Nr   )	dok_array)Z_dokrE   )r6   rE   r   r   r   _dok_containeri   s    z_spbase._dok_containerc                 C   s   ddl m} |S )Nr   )	lil_array)Z_lilrG   )r6   rG   r   r   r   _lil_containern   s    z_spbase._lil_containerc                 C   s$   d | _ | jjdkrtd|| _d S )Nr0   z7This class is not intended to be instantiated directly.)r4   	__class__r   
ValueErrormaxprint)r6   rK   r   r   r   __init__s   s    z_spbase.__init__c                 O   sX   t | t}t|| j|d}t|\}}|| jkr@|r<|  S | S | j|dj||d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 : length-2 tuple of ints
            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
        )Zallow_1dcopyF)orderrN   )
isinstancer   r   shaper   rN   tocooreshape)r6   argskwargsZis_arrayrQ   rO   rN   r   r   r   rS      s     

z_spbase.reshapec                 C   s   t t| j ddS )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, 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 implementedN)NotImplementedErrortyper   )r6   rQ   r   r   r   resize   s    z_spbase.resizeunsafeTc                 C   sD   t |}| j|kr0|  j|||d| jS |r<|  S | S dS )a  Cast the array/matrix elements to a specified type.

        Parameters
        ----------
        dtype : string 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.
        )castingrN   N)npdtypetocsrastypeasformatformatrN   )r6   r\   rZ   rN   r   r   r   r^      s    

z_spbase.astypec                 K   s0   t | trtj|fi |S t|fi |S d S r2   )
issubclassr   r[   asarrayr   clsXrU   r   r   r   _ascontainer   s    
z_spbase._ascontainerc                 K   s0   t | trtj|fi |S t|fi |S d S r2   )ra   r   r[   arrayr   rc   r   r   r   
_container   s    
z_spbase._containerc                 C   sT   g d}| j j|v r| S |D ]"}| j t |kr| |  S qtd| j j dS )z6Upcast array to a floating point format (if necessary))fdFDz-cannot upcast [%s] to a floating point formatN)r\   charr[   r^   	TypeErrorname)r6   Zfp_typesZfp_typer   r   r   	_asfptype   s    z_spbase._asfptypec                 c   s"   t | jd D ]}| | V  qd S )Nr   )rangerQ   )r6   rr   r   r   __iter__  s    z_spbase.__iter__c                 C   s   | j S )z3Maximum number of elements to display when printed.)rK   r5   r   r   r   _getmaxprint  s    z_spbase._getmaxprintc                 C   s   t d| jj dS )a  Number of non-zero entries, equivalent to

        np.count_nonzero(a.toarray())

        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.
        z%count_nonzero not implemented for %s.NrV   rI   r   r5   r   r   r   count_nonzero
  s    	z_spbase.count_nonzeroNc                 C   s   t d| jj dS )aI  Number of stored values, including explicit zeros.

        Parameters
        ----------
        axis : None, 0, or 1
            Select between the number of values across the whole array, in
            each column, or in each row.

        See also
        --------
        count_nonzero : Number of non-zero entries
        zgetnnz not implemented for %s.Nru   r6   axisr   r   r   _getnnz  s    z_spbase._getnnzc                 C   s   |   S )zNumber of stored values, including explicit zeros.

        See also
        --------
        count_nonzero : Number of non-zero entries
        ry   r5   r   r   r   nnz&  s    z_spbase.nnzc                 C   s   |   S )zwNumber of stored values.

        See also
        --------
        count_nonzero : Number of non-zero values.
        rz   r5   r   r   r   size0  s    z_spbase.sizec                 C   s   | j S )zFormat string for matrix.)_formatr5   r   r   r   r`   :  s    z_spbase.formatc                 C   s&   t | trd}tt|dd |  S )zDEPRECATED: Return a dense array.

        .. deprecated:: 1.11.0

            `.A` is deprecated and will be removed in v1.14.0.
            Use `.toarray()` instead.
        zL`.A` is deprecated and will be removed in v1.14.0. Use `.toarray()` instead.r   
stacklevel)rP   r   r   r   toarrayr6   messager   r   r   A?  s    	
z	_spbase.Ac                 C   s   |   S )z
Transpose.)	transposer5   r   r   r   TN  s    z	_spbase.Tc                 C   s(   t | trd}tt|dd | j S )zDEPRECATED: Returns the (complex) conjugate transpose.

        .. deprecated:: 1.11.0

            `.H` is deprecated and will be removed in v1.14.0.
            Please use `.T.conjugate()` instead.
        zW`.H` is deprecated and will be removed in v1.14.0. Please use `.T.conjugate()` instead.r   r~   )rP   r   r   r   r   	conjugater   r   r   r   HS  s    	
z	_spbase.Hc                 C   s   |   S r2   )_realr5   r   r   r   realb  s    z_spbase.realc                 C   s   |   S r2   )_imagr5   r   r   r   imagf  s    z_spbase.imagc                 C   s`   t | j \}}t| trdnd}ddd | jD }d| d| d| jj d	| j d
| dS )Nrg   r   xc                 s   s   | ]}t |V  qd S r2   )str).0r   r   r   r   	<genexpr>m      z#_spbase.__repr__.<locals>.<genexpr><z sparse z
 of type 'z'
	with z stored elements in z format>)	_formatsr`   rP   r   joinrQ   r\   rW   r{   )r6   _format_nameZ
sparse_clsZ	shape_strr   r   r   __repr__j  s    z_spbase.__repr__c                 C   s   |   }|  }dd }| j|kr|d }||jd | |jd | |jd | }|d7 }||d  }|||j| d  |j| d  |j| d  7 }n||j|j|j}|S )Nc                 S   s(   t tt | ||}ddd |D S )N
c                 S   s   g | ]}d j | qS )z  {}	{})r`   )r   tr   r   r   
<listcomp>{  r   z2_spbase.__str__.<locals>.tostr.<locals>.<listcomp>)ziplistr   )rowcoldataZtriplesr   r   r   tostry  s    z_spbase.__str__.<locals>.tostrr   z
  :	:
)rt   rR   r{   r   r   r   )r6   rK   r   r   Zhalfoutr   r   r   __str__s  s    
*6z_spbase.__str__c                 C   s    | j dkr| jdkS tdd S )N)r   r   r   z\The truth value of an array with more than one element is ambiguous. Use a.any() or a.all().)rQ   r{   rJ   r5   r   r   r   __bool__  s    

z_spbase.__bool__c                 C   s   t dd S )Nz:sparse array length is ambiguous; use getnnz() or shape[0])rn   r5   r   r   r   __len__  s    z_spbase.__len__Fc              
   C   s   |du s|| j kr$|r|  S | S npzt| d| }W n6 tyl } ztd| d|W Y d}~n
d}~0 0 z||dW S  ty   |  Y S 0 dS )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 : This array/matrix in the passed format.
        NtozFormat z is unknown.rM   )r`   rN   getattrAttributeErrorrJ   rn   )r6   r`   rN   Zconvert_methoder   r   r   r_     s    (z_spbase.asformatc                 C   s   |   |S )z2Point-wise multiplication by another array/matrix.)r]   multiplyr6   otherr   r   r   r     s    z_spbase.multiplyc                 C   s   |   |S )z;Element-wise maximum between this and another array/matrix.)r]   maximumr   r   r   r   r     s    z_spbase.maximumc                 C   s   |   |S )z;Element-wise minimum between this and another array/matrix.)r]   minimumr   r   r   r   r     s    z_spbase.minimumc                 C   s   t |r| | S | | S dS )a/  Ordinary dot product

        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)

        N)r[   Zisscalarr   r   r   r   dot  s    
z_spbase.dotc                 C   s   |   j||dS )zElement-wise power.r\   )r]   power)r6   nr\   r   r   r   r     s    z_spbase.powerc                 C   s   |   |S r2   )r]   __eq__r   r   r   r   r     s    z_spbase.__eq__c                 C   s   |   |S r2   )r]   __ne__r   r   r   r   r     s    z_spbase.__ne__c                 C   s   |   |S r2   )r]   __lt__r   r   r   r   r     s    z_spbase.__lt__c                 C   s   |   |S r2   )r]   __gt__r   r   r   r   r     s    z_spbase.__gt__c                 C   s   |   |S r2   )r]   __le__r   r   r   r   r     s    z_spbase.__le__c                 C   s   |   |S r2   )r]   __ge__r   r   r   r   r     s    z_spbase.__ge__c                 C   s   t |  S r2   )absr]   r5   r   r   r   __abs__  s    z_spbase.__abs__r   c                 C   s   t |  |dS )N)ndigits)roundr]   )r6   r   r   r   r   	__round__  s    z_spbase.__round__c                 C   s   |   |S r2   )r]   _add_sparser   r   r   r   r     s    z_spbase._add_sparsec                 C   s   |   |S r2   )rR   
_add_denser   r   r   r   r     s    z_spbase._add_densec                 C   s   |   |S r2   )r]   _sub_sparser   r   r   r   r     s    z_spbase._sub_sparsec                 C   s   |   | S r2   todenser   r   r   r   
_sub_dense  s    z_spbase._sub_densec                 C   s   ||    S r2   r   r   r   r   r   _rsub_dense  s    z_spbase._rsub_densec                 C   sp   t |r"|dkr|  S tdnJt|rH|j| jkr>td| |S t|rht	|| j}| 
|S tS d S )Nr   z:adding a nonzero scalar to a sparse array is not supportedinconsistent shapes)r
   rN   rV   r   rQ   rJ   r   r	   r[   broadcast_tor   NotImplementedr   r   r   r   __add__  s    


z_spbase.__add__c                 C   s
   |  |S r2   )r   r   r   r   r   __radd__  s    z_spbase.__radd__c                 C   sp   t |r"|dkr|  S tdnJt|rH|j| jkr>td| |S t|rht	|| j}| 
|S tS d S )Nr   zAsubtracting a nonzero scalar from a sparse array is not supportedr   )r
   rN   rV   r   rQ   rJ   r   r	   r[   r   r   r   r   r   r   r   __sub__  s    


z_spbase.__sub__c                 C   sL   t |r$|dkr|   S tdn$t|rDt|| j}| |S tS d S )Nr   zAsubtracting a sparse array from a nonzero scalar is not supported)	r
   rN   rV   r	   r[   r   rQ   r   r   r   r   r   r   __rsub__,  s    


z_spbase.__rsub__c                 C   s  | j \}}|jtju r|j|fkr,| |S |j|dfkrb| | }| jdkrV|S ||dS |jdkr|jd |kr| 	|S t
|r| |S t|r| jd |jd krtd|jdkrtd| |S t|}|jdkr|jtjkrtS z
|j W n ty   |}Y n0 |jdksF|jdkr|jd dkr|j|fkrl|j|dfkrltd| t|}t|tjr| |}|jdkr|jd dkr|dd}|S |jdkr|jd |krtd| 	t|}t|tjr| |}|S tddS )	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   r8   zdimension mismatchz%Cannot yet multiply a 1d sparse arrayzcould not interpret dimensionsN)r:   rI   r[   ndarrayrQ   _matmul_vectorZravelr7   rS   _matmul_multivectorr
   _mul_scalarr   rJ   _matmul_sparseZ
asanyarrayr\   Zobject_r   r   rP   r   rf   rb   )r6   r   MNresultZother_ar   r   r   _matmul_dispatch8  sV    









(

z_spbase._matmul_dispatchc                 O   s   | j |i |S r2   r   r6   rT   rU   r   r   r   __mul__  s    z_spbase.__mul__c                 O   s   | j |i |S r2   r   r   r   r   r   __rmul__  s    z_spbase.__rmul__c                 C   s   |   |S r2   )r]   r   r   r   r   r   r     s    z_spbase._mul_scalarc                 C   s   |   |S r2   )r]   r   r   r   r   r   r     s    z_spbase._matmul_vectorc                 C   s   |   |S r2   )r]   r   r   r   r   r   r     s    z_spbase._matmul_multivectorc                 C   s   |   |S r2   )r]   r   r   r   r   r   r     s    z_spbase._matmul_sparsec                 C   sf   t |r| |S z| }W n  ty>   t| }Y n0 |  |}|tu rZtS | S d S r2   )r
   r   r   r   r[   rb   r   r   )r6   r   trretr   r   r   _rmatmul_dispatch  s    
z_spbase._rmatmul_dispatchc                 C   s   t |rtd| |S Nz0Scalar operands are not allowed, use '*' instead)r
   rJ   r   r   r   r   r   
__matmul__  s    z_spbase.__matmul__c                 C   s   t |rtd| |S r   )r
   rJ   r   r   r   r   r   __rmatmul__  s    z_spbase.__rmatmul__c                 C   sf  t |r|r0|r t||  S t||  S |rZt| jtjrZ| tj	d| S | 	d| }t
|j}t| jtjrt|tjr|| jS |S nt|r |s|rtd|}ntd|}| |S |rt||  S t||  S nbt|r^|r |j| |ddS |  }|rRt| jtjrR|tj|S ||S ntS d S )N      ?F)rdivide)r
   r[   true_divider   divideZcan_castr\   float64r^   r   rb   
issubdtypeintegerr	   r   r   _divider]   Z_divide_sparser   )r6   r   r   r   rr   Zscalar_dtypeZrecipZself_csrr   r   r   r     s>    


z_spbase._dividec                 C   s   | j |ddS NT)r   r   r   r   r   r   __truediv__  s    z_spbase.__truediv__c                 C   s   | j |ddS r   r   r   r   r   r   __div__  s    z_spbase.__div__c                 C   s   t S r2   r   r   r   r   r   __rtruediv__  s    z_spbase.__rtruediv__c                 C   s   t S r2   r   r   r   r   r   __rdiv__  s    z_spbase.__rdiv__c                 C   s
   |    S r2   )r]   r5   r   r   r   __neg__  s    z_spbase.__neg__c                 C   s   t S r2   r   r   r   r   r   __iadd__  s    z_spbase.__iadd__c                 C   s   t S r2   r   r   r   r   r   __isub__  s    z_spbase.__isub__c                 C   s   t S r2   r   r   r   r   r   __imul__  s    z_spbase.__imul__c                 C   s
   |  |S r2   )__itruediv__r   r   r   r   __idiv__
  s    z_spbase.__idiv__c                 C   s   t S r2   r   r   r   r   r   r     s    z_spbase.__itruediv__c                 O   s   | j |i |S r2   )r   r   r   r   r   __pow__  s    z_spbase.__pow__c                 C   s   | j |dj|d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 : `self` with the dimensions reversed.

        Notes
        -----
        If `self` is a `csr_array` or a `csc_array`, then this will return a
        `csc_array` or a `csr_array`, respectively.

        See Also
        --------
        numpy.transpose : NumPy's implementation of 'transpose' for ndarrays
        rM   F)axesrN   )r]   r   )r6   r   rN   r   r   r   r     s    z_spbase.transposec                 C   s8   t | jt jr$| j|djddS |r0|  S | S d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 : The element-wise complex conjugate.

        rM   FN)r[   r   r\   Zcomplexfloatingr]   r   rN   r6   rN   r   r   r   r   2  s
    z_spbase.conjugatec                 C   s   | j |dS )NrM   )r   r   r   r   r   conjI  s    z_spbase.conjc                 C   s   |    S r2   )r]   r   r5   r   r   r   r   N  s    z_spbase._realc                 C   s   |    S r2   )r]   r   r5   r   r   r   r   Q  s    z_spbase._imagc                 C   s&   |   }|jdk}|j| |j| fS )ay  Nonzero indices of the array/matrix.

        Returns a tuple of arrays (row,col) containing the indices
        of the non-zero elements of the array.

        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]), array([0, 1, 2, 0, 2]))

        r   )rR   r   r   r   )r6   r   Znz_maskr   r   r   nonzeroT  s    
z_spbase.nonzeroc                 C   st   | j dkrtd| jd }|dk r,||7 }|dk s<||krDtd| jdg|gdggf|df| jd}| | }|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]r8   r   index out of boundsrQ   r\   )r7   rJ   rQ   
IndexErrorr@   r\   )r6   jr   Zcol_selectorr   r   r   r   _getcolh  s    


z_spbase._getcolc                 C   sp   | j dkrtd| jd }|dk r,||7 }|dk s<||krDtd| jdgdg|ggfd|f| jd}||  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   )r7   rJ   rQ   r   rB   r\   )r6   ir   Zrow_selectorr   r   r   _getrow{  s    


z_spbase._getrowc                 C   s   |  | j||dS )a  
        Return a dense representation of this sparse array/matrix.

        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 (or `numpy.matrix`) 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.

        Returns
        -------
        arr : numpy.matrix, 2-D
            A NumPy matrix object with the same shape and containing
            the same data represented by the sparse array/matrix, with the
            requested memory order. If `out` was passed and was an
            array (rather than a `numpy.matrix`), it will be filled
            with the appropriate values and returned wrapped in a
            `numpy.matrix` object that shares the same memory.
        rO   r   )rf   r   r6   rO   r   r   r   r   r     s    z_spbase.todensec                 C   s   | j ddj||d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.
        FrM   r   )rR   r   r   r   r   r   r     s    z_spbase.toarrayc                 C   s   | j |djddS )zConvert this array/matrix to Compressed Sparse Row format.

        With copy=False, the data/indices may be shared between this array/matrix and
        the resultant csr_array/matrix.
        rM   F)rR   r]   r   r   r   r   r]     s    z_spbase.tocsrc                 C   s   | j |djddS )zConvert this array/matrix to Dictionary Of Keys format.

        With copy=False, the data/indices may be shared between this array/matrix and
        the resultant dok_array/matrix.
        rM   F)rR   todokr   r   r   r   r     s    z_spbase.todokc                 C   s   | j ddj|dS )zConvert this array/matrix to COOrdinate format.

        With copy=False, the data/indices may be shared between this array/matrix and
        the resultant coo_array/matrix.
        FrM   )r]   rR   r   r   r   r   rR     s    z_spbase.tocooc                 C   s   | j ddj|dS )zConvert this array/matrix to List of Lists format.

        With copy=False, the data/indices may be shared between this array/matrix and
        the resultant lil_array/matrix.
        FrM   )r]   tolilr   r   r   r   r    s    z_spbase.tolilc                 C   s   | j |djddS )zConvert this array/matrix to sparse DIAgonal format.

        With copy=False, the data/indices may be shared between this array/matrix and
        the resultant dia_array/matrix.
        rM   F)rR   todiar   r   r   r   r    s    z_spbase.todiac                 C   s   | j ddj||dS )a+  Convert this array/matrix to Block Sparse Row format.

        With copy=False, the data/indices may be shared between this array/matrix and
        the resultant bsr_array/matrix.

        When blocksize=(R, C) is provided, it will be used for construction of
        the bsr_array/matrix.
        FrM   )	blocksizerN   )r]   tobsr)r6   r  rN   r   r   r   r    s    	z_spbase.tobsrc                 C   s   | j |djddS )zConvert this array/matrix to Compressed Sparse Column format.

        With copy=False, the data/indices may be shared between this array/matrix and
        the resultant csc_array/matrix.
        rM   F)r]   tocscr   r   r   r   r    s    z_spbase.tocscc                 C   s   | j | ddS )zReturns a copy of this array/matrix.

        No data/indices will be shared between the returned value and current
        array/matrix.
        TrM   )rI   r5   r   r   r   rN     s    z_spbase.copyc                 C   s0  t | t| j}| jdkrv|dvr,td| tj| j|d |}|durrt	dd |jD rjtd||d	< |S | j\}}|du r| | 
tj|df|d j||d
S |dk r|d7 }|dkr| 
tjd|f|d|  }n| | 
tj|df|d }|dur |j|jkr td|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

        r   Nr8   r   axis must be None, -1 or 0r   Nc                 s   s   | ]}|d kV  qdS )r   Nr   )r   dimr   r   r   r   X  r   z_spbase.sum.<locals>.<genexpr>zdimensions do not match.r\   r   r   r   rx   r\   r   )r   r   r\   r7   rJ   r[   ZonesrQ   r^   anyrf   sum)r6   rx   r\   r   	res_dtyper   r   r   r   r   r   r  '  s>    &


z_spbase.sumc           	      C   s"  t | | jj}t| jtjp.t| jtj}|du rD|rPtj}nt|j}|rZtjn|}| |}| j	dkr|dvrt
d|| jd  }|j||dS |du r|| jd | jd   j||dS |dk r|d7 }|dkr |d	| jd   jd||d
S |d	| jd   jd||d
S d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

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

        Nr   r  r  r8   r	  r   r   r   r
  )r   r\   rW   r[   r   r   Zbool_r   r^   r7   rJ   rQ   r  )	r6   rx   r\   r   r  ZintegralZinter_dtypeZ
inter_selfresr   r   r   mean|  s:    %


z_spbase.meanc                 C   s   |   j|dS )aX  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

        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)r6   r  r   r   r   r    s    z_spbase.diagonalc                 C   s   | j |d S )a  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).

        r  )r  r  )r6   offsetr   r   r   trace  s    
z_spbase.tracec                 C   sJ   | j \}}|dkr||ks,|dk r4| |kr4td| t|| dS )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)rQ   rJ   _setdiagr[   rb   )r6   valuesr  r   r   r   r   r   setdiag  s    
"z_spbase.setdiagc                 C   s  | j \}}|dk r|jdkrJt|| |}t|D ]}|| || |f< q2nJt|| |t|}|dkrjdS t|d| D ]\}}|| || |f< qzn|jdkrt||| }t|D ]}|| ||| f< qnJt||| t|}|dkrdS t|d| D ]\}}|| ||| f< qdS )zZThis part of the implementation gets overridden by the
        different formats.
        r   N)rQ   r7   minrq   r3   	enumerate)r6   r  r  r   r   Z	max_indexr   vr   r   r   r    s(    


z_spbase._setdiagc                 C   s\   |d urD|d urt d|j| jks0|j| jkr8t dd|d< |S tj| j| j|dS d S )Nz,order cannot be specified if out is not Nonez6out array must be same dtype and shape as sparse arrayg        .)r\   rO   )rJ   rQ   r\   r[   Zzerosr   r   r   r   _process_toarray_args$  s    z_spbase._process_toarray_argsr   c                 C   s$   ddl m} ||||o 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  rP   r   )r6   ZarraysmaxvalZcheck_contentsr  r   r   r   _get_index_dtype1  s
    z_spbase._get_index_dtypec                 C   s   d}t |tdd | jS )zGet shape of a sparse array/matrix.

        .. deprecated:: 1.11.0
           This method will be removed in SciPy 1.14.0.
           Use `X.shape` instead.
        zP`get_shape` is deprecated and will be removed in v1.14.0; use `X.shape` instead.r   r~   )r   DeprecationWarningr4   r6   msgr   r   r   	get_shapeO  s    z_spbase.get_shapec                 C   s4   d}t |tdd | j|dd| j}|j| _dS )zSee `reshape`.

        .. deprecated:: 1.11.0
           This method will be removed in SciPy 1.14.0.
           Use `X.reshape` instead.
        zUShape assignment is deprecated and will be removed in v1.14.0; use `reshape` instead.r   r~   FrM   N)r   r   rS   r_   r`   __dict__)r6   rQ   r"  Znew_selfr   r   r   	set_shape^  s
    z_spbase.set_shapec                 C   s   | j S r2   )r4   r5   r   r   r   <lambda>q  r   z_spbase.<lambda>zThe shape of the array.

Note that, starting in SciPy 1.14.0, this property will no longer be
settable. To change the array shape, use `X.reshape` instead.
)fgetfsetdocc                 C   s   d}t |tdd |  S )zUpcast array/matrix to a floating point format (if necessary)

        .. deprecated:: 1.11.0
           This method is for internal use only, and will be removed from the
           public API in SciPy 1.14.0.
        zo`asfptype` is an internal function, and is deprecated as part of the public API. It will be removed in v1.14.0.r   r~   )r   r   rp   r!  r   r   r   asfptypez  s    z_spbase.asfptypec                 C   s   d}t |tdd |  S )zMaximum number of elements to display when printed.

        .. deprecated:: 1.11.0
           This method is for internal use only, and will be removed from the
           public API in SciPy 1.14.0.
        zr`getmaxprint` is an internal function, and is deprecated as part of the public API. It will be removed in v1.14.0.r   r~   )r   r   rt   r!  r   r   r   getmaxprint  s    z_spbase.getmaxprintc                 C   s   d}t |tdd | jS )zSparse array/matrix storage format.

        .. deprecated:: 1.11.0
           This method will be removed in SciPy 1.14.0.
           Use `X.format` instead.
        zQ`getformat` is deprecated and will be removed in v1.14.0; use `X.format` instead.r   r~   )r   r   r`   r!  r   r   r   	getformat  s    z_spbase.getformatc                 C   s   | j |dS )aP  Number of stored values, including explicit zeros.

        Parameters
        ----------
        axis : None, 0, or 1
            Select between the number of values across the whole array/matrix, in
            each column, or in each row.

        See also
        --------
        count_nonzero : Number of non-zero entries
        )rx   rz   rw   r   r   r   getnnz  s    z_spbase.getnnzc                 C   s   d}t |tdd |   S )zReturn the Hermitian transpose of this array/matrix.

        .. deprecated:: 1.11.0
           This method will be removed in SciPy 1.14.0.
           Use `X.conj().T` instead.
        zN`getH` is deprecated and will be removed in v1.14.0; use `X.conj().T` instead.r   r~   )r   r   r   r   r!  r   r   r   getH  s    z_spbase.getHc                 C   s$   d| d}t |tdd | |S )zReturns a copy of column j of the array/matrix, as an (m x 1) sparse
        array/matrix (column vector).

        .. deprecated:: 1.11.0
           This method will be removed in SciPy 1.14.0.
           Use array/matrix indexing instead.
        zB`getcol` is deprecated and will be removed in v1.14.0; use `X[:, []]` instead.r   r~   )r   r   r   )r6   r   r"  r   r   r   getcol  s    	z_spbase.getcolc                 C   s$   d| d}t |tdd | |S )zReturns a copy of row i of the array/matrix, as a (1 x n) sparse
        array/matrix (row vector).

        .. deprecated:: 1.11.0
           This method will be removed in SciPy 1.14.0.
           Use array/matrix indexing instead.
        z?`getrow` is deprecated and will be removed in v1.14.0; use `X[[r/  r   r~   )r   r   r   )r6   r   r"  r   r   r   getrow  s    	z_spbase.getrow)rY   T)N)F)N)r   )FF)NF)T)T)NN)NN)F)F)F)F)F)NF)F)NNN)NNN)r   )r   )r   )r   NF)N)r   r   r   __doc__Z__array_priority__r}   propertyintr7   r:   r<   r>   r@   rB   rD   rF   rH   MAXPRINTrL   rS   rX   r^   classmethodrf   rh   rp   rs   rt   rv   ry   r{   r|   r   r`   r[   r   r   r   r   r   r   r   r   r   __nonzero__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   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r]   r   rR   r  r  r  r  rN   r  r  r  r  r  r  r  r  r#  r%  rQ   r*  r+  r,  r-  r.  r0  r1  r   r   r   r   r0   ?   s  







+



		



	
'

V

,


&
 
#





UK
r0   c                   @   s   e Zd ZdZdS )r   z3A namespace class to separate sparray from spmatrixN)r   r   r   r2  r   r   r   r   r     s   r   c                 C   s
   t | tS )a!  Is `x` of a sparse array or sparse matrix type?

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

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

    Examples
    --------
    >>> import numpy as np
    >>> from scipy.sparse import csr_array, csr_matrix, issparse
    >>> issparse(csr_matrix([[5]]))
    True
    >>> issparse(csr_array([[5]]))
    True
    >>> issparse(np.array([[5]]))
    False
    >>> issparse(5)
    False
    )rP   r0   r   r   r   r   r     s    r   c                 C   s
   t | tS )a  Is `x` of a sparse matrix type?

    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
    )rP   r   r8  r   r   r   r     s    r   )1r2  warningsr   numpyr[   Zscipy._lib._utilr   r  r   r   r   r   r	   r
   r   r   Z_matrixr   __all__Warningr   r   r   r   	frozensetsintanZarcsinZarctansinhtanhZarcsinhZarctanhZrintsignexpm1log1pZdeg2radZrad2degfloorceiltruncsqrtZ _ufuncs_with_fixed_point_at_zeror5  r0   r   r   r   r   r   r   r   <module>   sh   (           .