U
    -e                     @   sr  d Z ddlm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 dddd	d
g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 d7d dZ*d8d Z+e+Z,dS )9zBase class for sparse matrices    )warnN   )	asmatrixcheck_reshape_kwargscheck_shapeget_sum_dtypeisdense	isintlikeisscalarlikematrixvalidateaxisspmatrix
isspmatrixissparseSparseWarningSparseEfficiencyWarningc                   @   s   e Zd ZdS )r   N__name__
__module____qualname__ r   r   S/var/www/html/Darija-Ai-Train/env/lib/python3.8/site-packages/scipy/sparse/_base.pyr      s   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   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                   @   s  e Zd ZdZdZ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dZefddZdd Zdd Zeee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/d0Zed1d2 Zd3d4 Z d5d6 Z!d7d8 Z"d9d: Z#e#Z$d;d< Z%dd=d>Z&d?d@ Z'dAdB Z(dCdD Z)dEdF Z*ddGdHZ+dIdJ Z,dKdL Z-dMdN Z.dOdP Z/dQdR Z0dSdT Z1dUdV Z2ddXdYZ3dZd[ Z4d\d] Z5d^d_ Z6d`da Z7dbdc Z8ddde Z9dfdg Z:dhdi Z;djdk Z<dldm Z=dndo Z>dpdq Z?drds Z@dtdu ZAdvdw ZBdxdy ZCdzd{ ZDd|d} ZEd~d ZFdddZGdd ZHdd ZIdd ZJdd ZKdd ZLdd ZMdd ZNdd ZOdd ZPdd ZQdd ZRdd ZSdddZTdddZUdddZVeUjeV_dd ZWdd ZXdd ZYdd ZZdd Z[dd Z\dddZ]dddZ^dddZ_dddZ`dddZadddZbdddZcdddZddddZedd ZfdddZgdddÄZhdddńZidddǄZjdddɄZkdd˄ Zldd̈́ Zmd.S )r   z This class provides a base class for all sparse matrices.  It
    cannot be instantiated.  Most of the work is provided by subclasses.
    g333333$@r   c                 C   s   ddl m} |S )Nr   )
bsr_matrix)Z_bsrr/   )selfr/   r   r   r   _bsr_containerD   s    zspmatrix._bsr_containerc                 C   s   ddl m} |S )Nr   )
coo_matrix)Z_coor2   )r0   r2   r   r   r   _coo_containerI   s    zspmatrix._coo_containerc                 C   s   ddl m} |S )Nr   )
csc_matrix)Z_cscr4   )r0   r4   r   r   r   _csc_containerN   s    zspmatrix._csc_containerc                 C   s   ddl m} |S )Nr   )
csr_matrix)Z_csrr6   )r0   r6   r   r   r   _csr_containerS   s    zspmatrix._csr_containerc                 C   s   ddl m} |S )Nr   )
dia_matrix)Z_diar8   )r0   r8   r   r   r   _dia_containerX   s    zspmatrix._dia_containerc                 C   s   ddl m} |S )Nr   )
dok_matrix)Z_dokr:   )r0   r:   r   r   r   _dok_container]   s    zspmatrix._dok_containerc                 C   s   ddl m} |S )Nr   )
lil_matrix)Z_lilr<   )r0   r<   r   r   r   _lil_containerb   s    zspmatrix._lil_containerFc                 C   s$   d | _ | jjdkrtd|| _d S )Nr   z7This class is not intended to be instantiated directly.)_shape	__class__r   
ValueErrormaxprint)r0   rA   r   r   r   __init__i   s    zspmatrix.__init__c                 C   s"   | j |dd| j}|j| _dS )zSee `reshape`.FcopyN)reshapeasformatformat__dict__)r0   shapeZ
new_matrixr   r   r   	set_shapep   s    zspmatrix.set_shapec                 C   s   | j S )zGet shape of a matrix.)r>   r0   r   r   r   	get_shapew   s    zspmatrix.get_shape)fgetfsetc                 O   sJ   t || j}t|\}}|| jkr2|r.|  S | S | j|dj||ddS )a  reshape(self, shape, order='C', copy=False)

        Gives a new shape to a sparse 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 matrix being used.

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

        See Also
        --------
        numpy.matrix.reshape : NumPy's implementation of 'reshape' for
                               matrices
        rC   F)orderrD   )r   rI   r   rD   tocoorE   )r0   argskwargsrI   rO   rD   r   r   r   rE   }   s    !
zspmatrix.reshapec                 C   s   t dt| jdS )a  Resize the 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 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 matrix 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)NotImplementedErrorrG   typer   )r0   rI   r   r   r   resize   s    zspmatrix.resizeunsafeTc                 C   sD   t |}| j|kr0|  j|||d| jS |r<|  S | S dS )a  Cast the 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
            matrix. If `copy` is `True`, it is guaranteed that the result and
            this matrix do not share any memory.
        )castingrD   N)npdtypetocsrastyperF   rG   rD   )r0   rY   rW   rD   r   r   r   r[      s    

  zspmatrix.astypec                 K   s$   | j rtj|f|S t|f|S d S N)	_is_arrayrX   asarrayr   clsXrR   r   r   r   _ascontainer   s    zspmatrix._ascontainerc                 K   s$   | j rtj|f|S t|f|S d S r\   )r]   rX   arrayr   r_   r   r   r   
_container   s    zspmatrix._containerc                 C   sX   ddddg}| j j|kr| S |D ]"}| j t |kr | |  S q td| j j dS )z7Upcast matrix to a floating point format (if necessary)fdFDz-cannot upcast [%s] to a floating point formatN)rY   charrX   r[   	TypeErrorname)r0   Zfp_typesZfp_typer   r   r   asfptype   s    zspmatrix.asfptypec                 c   s*   t | jd D ]}| |d d f V  qd S Nr   )rangerI   )r0   rr   r   r   __iter__   s    zspmatrix.__iter__c                 C   s   | j S )z3Maximum number of elements to display when printed.)rA   rK   r   r   r   getmaxprint  s    zspmatrix.getmaxprintc                 C   s   t d| jj dS )a(  Number of non-zero entries, equivalent to

        np.count_nonzero(a.toarray())

        Unlike getnnz() and 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rS   r?   r   rK   r   r   r   count_nonzero  s    	zspmatrix.count_nonzeroNc                 C   s   t d| jj dS )aJ  Number of stored values, including explicit zeros.

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

        See also
        --------
        count_nonzero : Number of non-zero entries
        zgetnnz not implemented for %s.Nrr   )r0   axisr   r   r   getnnz  s    zspmatrix.getnnzc                 C   s   |   S )zNumber of stored values, including explicit zeros.

        See also
        --------
        count_nonzero : Number of non-zero entries
        )ru   rK   r   r   r   nnz#  s    zspmatrix.nnzc                 C   s   t | ddS )z.Format of a matrix representation as a string.rG   r-   )getattrrK   r   r   r   	getformat-  s    zspmatrix.getformatc                 C   s@   t |   \}}| jrdnd}d| d| j| jj| j|f  S )Nrc   r   z<%dx%d sparse z4 of type '%s'
	with %d stored elements in %s format>)_formatsrx   r]   rI   rY   rT   rv   )r0   _format_nameZ
sparse_clsr   r   r   __repr__1  s
    
zspmatrix.__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 | qS )z  %s	%sr   ).0tr   r   r   
<listcomp>@  s     z3spmatrix.__str__.<locals>.tostr.<locals>.<listcomp>)ziplistjoin)rowcoldataZtriplesr   r   r   tostr>  s    zspmatrix.__str__.<locals>.tostrr   z
  :	:
)rq   rP   rv   r   r   r   )r0   rA   Ar   Zhalfoutr   r   r   __str__8  s    
*6zspmatrix.__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().)rI   rv   r@   rK   r   r   r   __bool__M  s    

zspmatrix.__bool__c                 C   s   t dd S )Nz;sparse matrix length is ambiguous; use getnnz() or shape[0])rj   rK   r   r   r   __len__X  s    zspmatrix.__len__c              
   C   s   |dks|| j kr$|r|  S | S nnzt| d| }W n2 tk
rh } ztd ||W 5 d}~X Y nX z||dW S  tk
r   |  Y S X dS )a  Return this matrix in the passed format.

        Parameters
        ----------
        format : {str, None}
            The desired matrix 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 matrix in the passed format.
        NtozFormat {} is unknown.rC   )rG   rD   rw   AttributeErrorr@   rj   )r0   rG   rD   Zconvert_methoder   r   r   rF   \  s    "zspmatrix.asformatc                 C   s   |   |S )z4Point-wise multiplication by another matrix
        )rZ   multiplyr0   otherr   r   r   r     s    zspmatrix.multiplyc                 C   s   |   |S )z5Element-wise maximum between this and another matrix.)rZ   maximumr   r   r   r   r     s    zspmatrix.maximumc                 C   s   |   |S )z5Element-wise minimum between this and another matrix.)rZ   minimumr   r   r   r   r     s    zspmatrix.minimumc                 C   s   t |r| | S | | S dS )a1  Ordinary dot product

        Examples
        --------
        >>> import numpy as np
        >>> from scipy.sparse import csr_matrix
        >>> A = csr_matrix([[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)rX   Zisscalarr   r   r   r   dot  s    
zspmatrix.dotc                 C   s   |   j||dS )zElement-wise power.rY   )rZ   power)r0   nrY   r   r   r   r     s    zspmatrix.powerc                 C   s   |   |S r\   )rZ   __eq__r   r   r   r   r     s    zspmatrix.__eq__c                 C   s   |   |S r\   )rZ   __ne__r   r   r   r   r     s    zspmatrix.__ne__c                 C   s   |   |S r\   )rZ   __lt__r   r   r   r   r     s    zspmatrix.__lt__c                 C   s   |   |S r\   )rZ   __gt__r   r   r   r   r     s    zspmatrix.__gt__c                 C   s   |   |S r\   )rZ   __le__r   r   r   r   r     s    zspmatrix.__le__c                 C   s   |   |S r\   )rZ   __ge__r   r   r   r   r     s    zspmatrix.__ge__c                 C   s   t |  S r\   )absrZ   rK   r   r   r   __abs__  s    zspmatrix.__abs__r   c                 C   s   t |  |dS )N)ndigits)roundrZ   )r0   r   r   r   r   	__round__  s    zspmatrix.__round__c                 C   s   |   |S r\   )rZ   _add_sparser   r   r   r   r     s    zspmatrix._add_sparsec                 C   s   |   |S r\   )rP   
_add_denser   r   r   r   r     s    zspmatrix._add_densec                 C   s   |   |S r\   )rZ   _sub_sparser   r   r   r   r     s    zspmatrix._sub_sparsec                 C   s   |   | S r\   todenser   r   r   r   
_sub_dense  s    zspmatrix._sub_densec                 C   s   ||    S r\   r   r   r   r   r   _rsub_dense  s    zspmatrix._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 matrix is not supportedinconsistent shapes)r
   rD   rS   r   rI   r@   r   r   rX   broadcast_tor   NotImplementedr   r   r   r   __add__  s    


zspmatrix.__add__c                 C   s
   |  |S r\   )r   r   r   r   r   __radd__  s    zspmatrix.__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   zBsubtracting a nonzero scalar from a sparse matrix is not supportedr   )r
   rD   rS   r   rI   r@   r   r   rX   r   r   r   r   r   r   r   __sub__  s    


zspmatrix.__sub__c                 C   sL   t |r$|dkr|   S tdn$t|rDt|| j}| |S tS d S )Nr   zBsubtracting a sparse matrix from a nonzero scalar is not supported)	r
   rD   rS   r   rX   r   rI   r   r   r   r   r   r   __rsub__  s    


zspmatrix.__rsub__c                 C   s  | j \}}|jtjkrr|j |fkr,| |S |j |dfkrP| | |dS |jdkrr|j d |krr| |S t	|r| 
|S t|r| j d |j d krtd| |S t|}|jdkr|jtjkrtS z
|j  W n tk
r   |}Y nX |jdks"|jdkr|j d dkr|j |fkrH|j |dfkrHtd| t|}t|tjrp| |}|jdkr|j d dkr|dd}|S |jdkr|j d | j d krtd| t|}t|tjr| |}|S tddS )z`np.matrix`-compatible mul, i.e. `dot` or `NotImplemented`

        interpret other and call one of the following
        self._mul_scalar()
        self._mul_vector()
        self._mul_multivector()
        self._mul_sparse_matrix()
        r   r   r   zdimension mismatchzcould not interpret dimensionsN)rI   r?   rX   Zndarray_mul_vectorZravelrE   ndim_mul_multivectorr
   _mul_scalarr   r@   _mul_sparse_matrixZ
asanyarrayrY   Zobject_r   r   
isinstancer   rb   r^   )r0   r   MNZother_aresultr   r   r   _mul_dispatch  sL    







(

zspmatrix._mul_dispatchc                 C   s
   |  |S r\   )r   r   r   r   r   __mul__M  s    zspmatrix.__mul__c                 C   s   |   |S r\   )rZ   r   r   r   r   r   r   Q  s    zspmatrix._mul_scalarc                 C   s   |   |S r\   )rZ   r   r   r   r   r   r   T  s    zspmatrix._mul_vectorc                 C   s   |   |S r\   )rZ   r   r   r   r   r   r   W  s    zspmatrix._mul_multivectorc                 C   s   |   |S r\   )rZ   r   r   r   r   r   r   Z  s    zspmatrix._mul_sparse_matrixc                 C   sh   t |r| |S z| }W n" tk
r@   t| }Y nX |  |}|tkr\tS | S d S r\   )r
   r   	transposer   rX   r^   r   r   )r0   r   trretr   r   r   _rmul_dispatch]  s    
zspmatrix._rmul_dispatchc                 C   s
   |  |S r\   )r   r   r   r   r   __rmul__k  s    zspmatrix.__rmul__c                 C   s   t |rtd| |S Nz0Scalar operands are not allowed, use '*' instead)r
   r@   r   r   r   r   r   
__matmul__r  s    zspmatrix.__matmul__c                 C   s   t |rtd| |S r   )r
   r@   r   r   r   r   r   __rmatmul__x  s    zspmatrix.__rmatmul__c                 C   sd  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|  |S t|  |S n$|rt||  S t||  S nbt|r\|r|j| |ddS |  }|rPt| jtjrP|tj|S ||S ntS d S )N      ?F)rdivide)r
   rX   true_divider   divideZcan_castrY   Zfloat_r[   r   r^   
issubdtypeintegerr   r   _dividerZ   Z_divide_sparser   )r0   r   r   r   ro   Zscalar_dtypeZself_csrr   r   r   r     s<    

zspmatrix._dividec                 C   s   | j |ddS NT)r   r   r   r   r   r   __truediv__  s    zspmatrix.__truediv__c                 C   s   | j |ddS r   r   r   r   r   r   __div__  s    zspmatrix.__div__c                 C   s   t S r\   r   r   r   r   r   __rtruediv__  s    zspmatrix.__rtruediv__c                 C   s   t S r\   r   r   r   r   r   __rdiv__  s    zspmatrix.__rdiv__c                 C   s
   |    S r\   )rZ   rK   r   r   r   __neg__  s    zspmatrix.__neg__c                 C   s   t S r\   r   r   r   r   r   __iadd__  s    zspmatrix.__iadd__c                 C   s   t S r\   r   r   r   r   r   __isub__  s    zspmatrix.__isub__c                 C   s   t S r\   r   r   r   r   r   __imul__  s    zspmatrix.__imul__c                 C   s
   |  |S r\   )__itruediv__r   r   r   r   __idiv__  s    zspmatrix.__idiv__c                 C   s   t S r\   r   r   r   r   r   r     s    zspmatrix.__itruediv__c                 C   s   | j d | j d  }}||kr&tdt|rt|}|dk rFtd|dkrddlm} ||| jd}| jrddl	m
} ||}|S |dkr|  S | |d }|d r| | | S || S nt|rtd	ntS d S )
Nr   r   zmatrix is not squarezexponent must be >= 0)eyer   )	dia_arrayr   zexponent must be an integer)rI   rj   r	   intr@   Z
_constructr   rY   r]   Z_arraysr   rD   __pow__r
   r   )r0   r   r   r   r   Er   tmpr   r   r   r     s.    

zspmatrix.__pow__c                 C   s   |dkr$| j rttd |  S |dkr4|  S |dkrX| j rPttd |  S |dkrh|  S |dkrx|  S |dkr| 	 S t
|d	 d S )
Nr   zPlease use `.todense()` insteadTHzPlease use `.conj().T` insteadrealimagsizez
 not found)r]   r   rX   ZVisibleDeprecationWarningtoarrayr   getH_real_imagru   r   )r0   attrr   r   r   __getattr__  s*    zspmatrix.__getattr__c                 C   s   | j |dj|ddS )a  
        Reverses the dimensions of the sparse 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 matrix
            being used.

        Returns
        -------
        p : `self` with the dimensions reversed.

        See Also
        --------
        numpy.matrix.transpose : NumPy's implementation of 'transpose'
                                 for matrices
        rC   F)axesrD   )rZ   r   )r0   r   rD   r   r   r   r     s    zspmatrix.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 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.

        rC   FN)rX   r   rY   ZcomplexfloatingrZ   conjrD   r0   rD   r   r   r   r      s
    zspmatrix.conjc                 C   s   | j |dS )NrC   )r   r   r   r   r   	conjugate7  s    zspmatrix.conjugatec                 C   s   |    S )zReturn the Hermitian transpose of this matrix.

        See Also
        --------
        numpy.matrix.getH : NumPy's implementation of `getH` for matrices
        )r   r   rK   r   r   r   r   =  s    zspmatrix.getHc                 C   s   |    S r\   )rZ   r   rK   r   r   r   r   F  s    zspmatrix._realc                 C   s   |    S r\   )rZ   r   rK   r   r   r   r   I  s    zspmatrix._imagc                 C   s&   |   }|jdk}|j| |j| fS )ag  nonzero indices

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

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

        r   )rP   r   r   r   )r0   r   Znz_maskr   r   r   nonzeroL  s    
zspmatrix.nonzeroc                 C   s^   | j d }|dk r||7 }|dk s*||kr2td| jdg|gdggf|df| jd}| | S )zgReturns a copy of column j of the matrix, as an (m x 1) sparse
        matrix (column vector).
        r   r   index out of boundsrI   rY   )rI   
IndexErrorr5   rY   )r0   jr   Zcol_selectorr   r   r   getcol`  s    
 zspmatrix.getcolc                 C   s^   | j d }|dk r||7 }|dk s*||kr2td| jdgdg|ggfd|f| jd}||  S )z`Returns a copy of row i of the matrix, as a (1 x n) sparse
        matrix (row vector).
        r   r   r   r   )rI   r   r7   rY   )r0   imZrow_selectorr   r   r   getrowp  s    
 zspmatrix.getrowc                 C   s   |  | j||dS )a  
        Return a dense matrix representation of this 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 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 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   )rb   r   r0   rO   r   r   r   r   r     s    zspmatrix.todensec                 C   s   | j ddj||dS )a  
        Return a dense ndarray representation of this 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
            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 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.
        FrC   r   )rP   r   r   r   r   r   r     s    zspmatrix.toarrayc                 C   s   | j |djddS )zConvert this matrix to Compressed Sparse Row format.

        With copy=False, the data/indices may be shared between this matrix and
        the resultant csr_matrix.
        rC   F)rP   rZ   r   r   r   r   rZ     s    zspmatrix.tocsrc                 C   s   | j |djddS )zConvert this matrix to Dictionary Of Keys format.

        With copy=False, the data/indices may be shared between this matrix and
        the resultant dok_matrix.
        rC   F)rP   todokr   r   r   r   r     s    zspmatrix.todokc                 C   s   | j ddj|dS )zConvert this matrix to COOrdinate format.

        With copy=False, the data/indices may be shared between this matrix and
        the resultant coo_matrix.
        FrC   )rZ   rP   r   r   r   r   rP     s    zspmatrix.tocooc                 C   s   | j ddj|dS )zConvert this matrix to List of Lists format.

        With copy=False, the data/indices may be shared between this matrix and
        the resultant lil_matrix.
        FrC   )rZ   tolilr   r   r   r   r     s    zspmatrix.tolilc                 C   s   | j |djddS )zConvert this matrix to sparse DIAgonal format.

        With copy=False, the data/indices may be shared between this matrix and
        the resultant dia_matrix.
        rC   F)rP   todiar   r   r   r   r     s    zspmatrix.todiac                 C   s   | j ddj||dS )a  Convert this matrix to Block Sparse Row format.

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

        When blocksize=(R, C) is provided, it will be used for construction of
        the bsr_matrix.
        FrC   )	blocksizerD   )rZ   tobsr)r0   r   rD   r   r   r   r     s    	zspmatrix.tobsrc                 C   s   | j |djddS )zConvert this matrix to Compressed Sparse Column format.

        With copy=False, the data/indices may be shared between this matrix and
        the resultant csc_matrix.
        rC   F)rZ   tocscr   r   r   r   r   
  s    zspmatrix.tocscc                 C   s   | j | ddS )zReturns a copy of this matrix.

        No data/indices will be shared between the returned value and current
        matrix.
        TrC   )r?   rK   r   r   r   rD     s    zspmatrix.copyc                 C   s   t | | j\}}t| j}|dkrJ| | tj|df|d j||dS |dk rZ|d7 }|dkr| tjd|f|d|  }n| | tj|df|d }|dk	r|j|jkrtd|j|||dS )	aw  
        Sum the 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 matrix elements, returning a scalar
            (i.e., `axis` = `None`).
        dtype : dtype, optional
            The type of the returned 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

        Nr   r   rY   r   r   r   zdimensions do not matchrt   rY   r   )	r   rI   r   rY   rb   rX   Zonessumr@   )r0   rt   rY   r   r   r   	res_dtyper   r   r   r   r    s.    &

 zspmatrix.sumc           	      C   s   dd }t | | jj}|| j}|dkr6|rBtj}nt|j}|rLtjn|}| |}|dkr|t| 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 matrix elements. The average is taken
        over all elements in the 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 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

        c                 S   s   t | t jpt | t jS r\   )rX   r   r   Zbool_r   r   r   r   _is_integral  s    z#spmatrix.mean.<locals>._is_integralNr   r   r   r   r   r  )	r   rY   rT   rX   Zfloat64r[   rc   rI   r  )	r0   rt   rY   r   r  r  ZintegralZinter_dtypeZ
inter_selfr   r   r   meand  s<    %

     zspmatrix.meanc                 C   s   |   j|dS )aT  Returns the kth diagonal of the 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_matrix
        >>> A = csr_matrix([[1, 2, 0], [0, 0, 3], [4, 0, 5]])
        >>> A.diagonal()
        array([1, 0, 5])
        >>> A.diagonal(k=1)
        array([2, 3])
        k)rZ   diagonal)r0   r  r   r   r   r    s    zspmatrix.diagonalc                 C   s   | j |d S )zReturns the sum along diagonals of the sparse matrix.

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

        r  )r  r  )r0   offsetr   r   r   trace  s    
zspmatrix.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.

        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 matrix dimensionsN)rI   r@   _setdiagrX   r^   )r0   valuesr  r   r   r   r   r   setdiag  s    
"zspmatrix.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 rm   )rI   r   minrn   len	enumerate)r0   r  r  r   r   Z	max_indexr   vr   r   r   r    s(    


zspmatrix._setdiagc                 C   s\   |d k	rD|d k	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 Nonez7out array must be same dtype and shape as sparse matrixg        .)rY   rO   )r@   rI   rY   rX   Zzerosr   r   r   r   _process_toarray_args  s    zspmatrix._process_toarray_args)rV   T)N)F)N)r   )FF)NF)T)T)NN)NN)F)F)F)F)F)NF)F)NNN)NNN)r   )r   )r   )nr   r   r   __doc__Z__array_priority__r   propertyr1   r3   r5   r7   r9   r;   r=   r]   MAXPRINTrB   rJ   rL   rI   rE   rU   r[   classmethodrb   rd   rl   rp   rq   rs   ru   rv   rx   r|   r   r   __nonzero__r   rF   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rZ   r   rP   r   r   r   r   rD   r  r  r  r
  r  r  r  r   r   r   r   r   <   s   






+




	
'

O

+


	$
 
#







J
I


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

    Notes
    -----
    issparse and isspmatrix are aliases for the same function.

    Examples
    --------
    >>> from scipy.sparse import csr_matrix, isspmatrix
    >>> isspmatrix(csr_matrix([[5]]))
    True

    >>> from scipy.sparse import isspmatrix
    >>> isspmatrix(5)
    False
    )r   r   )xr   r   r   r     s    )-r  warningsr   numpyrX   Z_sputilsr   r   r   r   r   r	   r
   r   r   __all__Warningr   r   r   ry   	frozensetsintanZarcsinZarctansinhtanhZarcsinhZarctanhZrintsignexpm1log1pZdeg2radZrad2degfloorceiltruncsqrtZ _ufuncs_with_fixed_point_at_zeror  r   r   r   r   r   r   r   <module>   s   ,                         b