a
    CCCfCC                     @   s   d Z ddlZddlmZmZ ddlmZmZ g Z	G dd deZ
eD ]"ZejZdd	 Zee
eee qDd
d ZG dd dZdS )zBase class for sparse matrice with a .data attribute

    subclasses must provide a _with_data() method that
    creates a new matrix with the same sparsity pattern
    as self but with a different data array

    N   )_spbase _ufuncs_with_fixed_point_at_zero)isscalarlikevalidateaxisc                   @   s   e Zd Zdd Zedd Zejdd Zdd Zdd	 Zd&ddZ	dd Z
dd Zdd Zdd Zdd Zd'ddZejje_d(ddZejje_dd Zejje_dd  Zejje_d)d"d#Zd$d% Zd!S )*_data_matrixc                 C   s   t |  d S N)r   __init__self r   N/var/www/html/django/DPS/env/lib/python3.9/site-packages/scipy/sparse/_data.pyr	      s    z_data_matrix.__init__c                 C   s   | j jS r   datadtyper
   r   r   r   r      s    z_data_matrix.dtypec                 C   s   || j _d S r   r   )r   Znewtyper   r   r   r      s    c                 C   s   t | dr|   | jS )Nsum_duplicates)hasattrr   r   r
   r   r   r   _deduped_data   s    
z_data_matrix._deduped_datac                 C   s   |  t|  S r   )
_with_dataabsr   r
   r   r   r   __abs__$   s    z_data_matrix.__abs__r   c                 C   s   |  tj|  |dS )N)Zdecimals)r   npZaroundr   )r   ndigitsr   r   r   	__round__'   s    z_data_matrix.__round__c                 C   s   |  | jjS r   )r   r   realr
   r   r   r   _real*   s    z_data_matrix._realc                 C   s   |  | jjS r   )r   r   imagr
   r   r   r   _imag-   s    z_data_matrix._imagc                 C   s"   | j jdkrtd| | j S )Nbz0negating a boolean sparse array is not supported)r   kindNotImplementedErrorr   r   r
   r   r   r   __neg__0   s    z_data_matrix.__neg__c                 C   s"   t |r|  j|9  _| S tS d S r   r   r   NotImplementedr   otherr   r   r   __imul__6   s    z_data_matrix.__imul__c                 C   s*   t |r"d| }|  j|9  _| S tS d S )Ng      ?r"   )r   r%   Zrecipr   r   r   __itruediv__=   s
    z_data_matrix.__itruediv__unsafeTc                 C   sV   t |}| j|krB| j| jj||dddd}|j| ddS |rN|  S | S d S )NT)castingcopyr*   F)r   r   r   r   astyper   r*   )r   r   r)   r*   matrixr   r   r   r,   E   s    

z_data_matrix.astypec                 C   s8   t | jt jr$| j| j |dS |r0|  S | S d S )Nr+   )r   Z
issubdtyper   Zcomplexfloatingr   r   	conjugater*   )r   r*   r   r   r   r.   T   s
    z_data_matrix.conjugatec                 C   s   | j | j ddS NTr+   )r   r   r*   r
   r   r   r   r*   ^   s    z_data_matrix.copyc                 C   s   t |  S r   )r   count_nonzeror   r
   r   r   r   r0   c   s    z_data_matrix.count_nonzeroNc                 C   sD   t |std|std|  }|dur6||}| || S )a  
        This function performs 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 `np.ones`

        dtype : If dtype is not specified, the current dtype will be preserved.

        Raises
        ------
        NotImplementedError : if n is a zero scalar
            If zero power is desired, special case it to use
            `np.ones(A.shape, dtype=A.dtype)`
        zinput is not scalarzpzero power is not supported as it would densify the matrix.
Use `np.ones(A.shape, dtype=A.dtype)` for this case.N)r   r    r   r,   r   )r   nr   r   r   r   r   powerh   s    
z_data_matrix.powerc                 C   s   |  | j| S r   )r   r   r$   r   r   r   _mul_scalar   s    z_data_matrix._mul_scalar)r   )r(   T)T)N)__name__
__module____qualname__r	   propertyr   setterr   r   r   r   r   r!   r&   r'   r,   r   __doc__r.   r*   r0   r2   r3   r   r   r   r   r      s.   









#r   c                    s*    fdd}dt  dt  d|_t |_|S )Nc                    s    |   }| j|ddS r/   )r   r   )r   resultopr   r   method   s    z_create_method.<locals>.methodzElement-wise z.

See `numpy.z` for more information.)namer9   r4   )r<   r=   r   r;   r   _create_method   s    
r?   c                 C   s>   t | D ]\}}||kr|  S q|d7 }||k r6|S dS d S )Nr   )	enumerate)indr1   kar   r   r   _find_missing_index   s    
rE   c                   @   sl   e Zd ZdZdd Zdd Zdd Zdd	 ZdddZdddZ	dddZ
dddZdddZdddZd
S )_minmax_mixinzlMixin for min and max methods.

    These are not implemented for dia_matrix, hence the separate class.
    c                 C   s  | j | }|dkrtd| j d|  }| j|d}|dkrD|  n|  }|  ||\}}t|j	| |k }	|||	 d||	< |dk}
t
|
|}t
|
|}|dkr| j|tjt||d|ff| jd|fdS | j||tjt||dff| j|dfdS d S )Nr   &zero-size array to reduction operationr   )maxvalr   )r   shape)rJ   
ValueErrorZ_get_index_dtypetocsctocsrr   Z_minor_reducer   diffindptrcompressZ_coo_containerzeroslenr   )r   axis
min_or_maxNMZ	idx_dtypematZmajor_indexvaluenot_fullmaskr   r   r   _min_or_max_axis   s,    


z_minmax_mixin._min_or_max_axisc                 C   s   |d urt dt| | jdkr6|dvr2t dd }|d u rd| jv rPt d| jd}| jdkrj|S ||  	 }| jt
| jkr|||}|S |dk r|d7 }|dks|dkr| ||S t dd S )Nz0Sparse arrays do not support an 'out' parameter.r   Nr   r@   axis out of ranger   rG      )rK   r   ndimrJ   r   typennzreducer   Zravelr   prodr[   )r   rS   outrT   zeromr   r   r   _min_or_max   s,    



z_minmax_mixin._min_or_maxc                 C   s@  | j | dkrtd|dk r&|d7 }| jd}|dkrB|  n|  }|  ||j \}}tj	|t
d}tt|j\}	|	D ]}
|j|
|
d  \}}|j|| }|j|| }||}|| }|||s|| |kr|| ||
< qt||}||krt||||
< q|||
< q|dkr6|dd}| |S )Nr   z8Cannot apply the operation along a zero-sized dimension.r^   rI   r   r@   )rJ   rK   r   r`   rL   rM   r   Z_swapr   rQ   intZnonzerorN   rO   r   indicesrE   minZreshapeZ_ascontainer)r   rS   argmin_or_argmaxcomparere   rW   Zret_sizeZ	line_sizeretZnz_linesipqr   ri   extreme_indexextreme_valueZzero_indr   r   r   _arg_min_or_max_axis   s2    



z"_minmax_mixin._arg_min_or_max_axisc                 C   s:  |d urt dt| | jdkr6|dvr2t dd }|d urL| |||S d| jv r^t d| jdkrldS | jd}|  }|	  ||j
}|j
| }|jd }	|||rt|j| |	 t|j|  S t| j}
|
|jkrt|j| |	 t|j|  S |j|	 |j }t||
}||kr6t||S |S )Nz/Sparse types do not support an 'out' parameter.r   r\   r]   r   z.Cannot apply the operation to an empty matrix.r@   )rK   r   r_   rs   rJ   ra   r   r`   Ztocoor   r   rh   rowcolr   rc   rE   rj   )r   rS   rd   rk   rl   re   rW   rq   rr   Znum_colsizeZlinear_indicesZfirst_implicit_zero_indexr   r   r   _arg_min_or_max  s:    






  


z_minmax_mixin._arg_min_or_maxNc                 C   s   |  ||tjS )a7  
        Return the maximum of the array/matrix or maximum along an axis.
        This takes all elements into account, not just the non-zero ones.

        Parameters
        ----------
        axis : {-2, -1, 0, 1, None} optional
            Axis along which the sum is computed. The default is to
            compute the maximum over all elements, returning
            a scalar (i.e., `axis` = `None`).

        out : None, optional
            This argument is in the signature *solely* for NumPy
            compatibility reasons. Do not pass in anything except
            for the default value, as this argument is not used.

        Returns
        -------
        amax : coo_matrix or scalar
            Maximum of `a`. If `axis` is None, the result is a scalar value.
            If `axis` is given, the result is a sparse.coo_matrix of dimension
            ``a.ndim - 1``.

        See Also
        --------
        min : The minimum value of a sparse array/matrix along a given axis.
        numpy.matrix.max : NumPy's implementation of 'max' for matrices

        )rg   r   maximumr   rS   rd   r   r   r   maxB  s    z_minmax_mixin.maxc                 C   s   |  ||tjS )a7  
        Return the minimum of the array/matrix or maximum along an axis.
        This takes all elements into account, not just the non-zero ones.

        Parameters
        ----------
        axis : {-2, -1, 0, 1, None} optional
            Axis along which the sum is computed. The default is to
            compute the minimum over all elements, returning
            a scalar (i.e., `axis` = `None`).

        out : None, optional
            This argument is in the signature *solely* for NumPy
            compatibility reasons. Do not pass in anything except for
            the default value, as this argument is not used.

        Returns
        -------
        amin : coo_matrix or scalar
            Minimum of `a`. If `axis` is None, the result is a scalar value.
            If `axis` is given, the result is a sparse.coo_matrix of dimension
            ``a.ndim - 1``.

        See Also
        --------
        max : The maximum value of a sparse array/matrix along a given axis.
        numpy.matrix.min : NumPy's implementation of 'min' for matrices

        )rg   r   minimumry   r   r   r   rj   b  s    z_minmax_mixin.minc                 C   s   |  ||tjS )a  
        Return the maximum of the array/matrix or maximum along an axis, ignoring any
        NaNs. This takes all elements into account, not just the non-zero
        ones.

        .. versionadded:: 1.11.0

        Parameters
        ----------
        axis : {-2, -1, 0, 1, None} optional
            Axis along which the maximum is computed. The default is to
            compute the maximum over all elements, returning
            a scalar (i.e., `axis` = `None`).

        out : None, optional
            This argument is in the signature *solely* for NumPy
            compatibility reasons. Do not pass in anything except
            for the default value, as this argument is not used.

        Returns
        -------
        amax : coo_matrix or scalar
            Maximum of `a`. If `axis` is None, the result is a scalar value.
            If `axis` is given, the result is a sparse.coo_matrix of dimension
            ``a.ndim - 1``.

        See Also
        --------
        nanmin : The minimum value of a sparse array/matrix along a given axis,
                 ignoring NaNs.
        max : The maximum value of a sparse array/matrix along a given axis,
              propagating NaNs.
        numpy.nanmax : NumPy's implementation of 'nanmax'.

        )rg   r   Zfmaxry   r   r   r   nanmax  s    $z_minmax_mixin.nanmaxc                 C   s   |  ||tjS )a  
        Return the minimum of the array/matrix or minimum along an axis, ignoring any
        NaNs. This takes all elements into account, not just the non-zero
        ones.

        .. versionadded:: 1.11.0

        Parameters
        ----------
        axis : {-2, -1, 0, 1, None} optional
            Axis along which the minimum is computed. The default is to
            compute the minimum over all elements, returning
            a scalar (i.e., `axis` = `None`).

        out : None, optional
            This argument is in the signature *solely* for NumPy
            compatibility reasons. Do not pass in anything except for
            the default value, as this argument is not used.

        Returns
        -------
        amin : coo_matrix or scalar
            Minimum of `a`. If `axis` is None, the result is a scalar value.
            If `axis` is given, the result is a sparse.coo_matrix of dimension
            ``a.ndim - 1``.

        See Also
        --------
        nanmax : The maximum value of a sparse array/matrix along a given axis,
                 ignoring NaNs.
        min : The minimum value of a sparse array/matrix along a given axis,
              propagating NaNs.
        numpy.nanmin : NumPy's implementation of 'nanmin'.

        )rg   r   Zfminry   r   r   r   nanmin  s    $z_minmax_mixin.nanminc                 C   s   |  ||tjtjS )a.  Return indices of maximum elements along an axis.

        Implicit zero elements are also taken into account. If there are
        several maximum values, the index of the first occurrence is returned.

        Parameters
        ----------
        axis : {-2, -1, 0, 1, None}, optional
            Axis along which the argmax is computed. If None (default), index
            of the maximum element in the flatten data is returned.
        out : None, optional
            This argument is in the signature *solely* for NumPy
            compatibility reasons. Do not pass in anything except for
            the default value, as this argument is not used.

        Returns
        -------
        ind : numpy.matrix or int
            Indices of maximum elements. If matrix, its size along `axis` is 1.
        )rw   r   argmaxZgreaterry   r   r   r   r~     s    z_minmax_mixin.argmaxc                 C   s   |  ||tjtjS )a/  Return indices of minimum elements along an axis.

        Implicit zero elements are also taken into account. If there are
        several minimum values, the index of the first occurrence is returned.

        Parameters
        ----------
        axis : {-2, -1, 0, 1, None}, optional
            Axis along which the argmin is computed. If None (default), index
            of the minimum element in the flatten data is returned.
        out : None, optional
            This argument is in the signature *solely* for NumPy
            compatibility reasons. Do not pass in anything except for
            the default value, as this argument is not used.

        Returns
        -------
         ind : numpy.matrix or int
            Indices of minimum elements. If matrix, its size along `axis` is 1.
        )rw   r   argminlessry   r   r   r   r     s    z_minmax_mixin.argmin)NN)NN)NN)NN)NN)NN)r4   r5   r6   r9   r[   rg   rs   rw   rz   rj   r|   r}   r~   r   r   r   r   r   rF      s   $0
 
 
&
&
rF   )r9   numpyr   _baser   r   Z_sputilsr   r   __all__r   Znpfuncr4   r>   r?   setattrrE   rF   r   r   r   r   <module>   s   }