a
    aH2                     @   s   d Z ddlZddlmZmZ ddlmZ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 qHd
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   )spmatrix _ufuncs_with_fixed_point_at_zero)isscalarlikevalidateaxismatrixc                   @   s   e Zd Zdd Zdd Zdd Zeee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   a/Users/vegardjervell/Documents/master/model/venv/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   
_get_dtype   s    z_data_matrix._get_dtypec                 C   s   || j _d S r	   r   )r   Znewtyper   r   r   
_set_dtype   s    z_data_matrix._set_dtype)fgetfsetc                 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bz1negating a sparse boolean matrix is not supported)r   kindNotImplementedErrorr   r   r   r   r   r   __neg__/   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__5   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   sF   t |}| j|kr2| j|  j|||d|dS |r>|  S | S d S )N)castingcopyr/   )r   r   r   r   astyper/   )r   r   r.   r/   r   r   r   r1   D   s    

z_data_matrix.astypec                 C   s8   t | jt jr$| j| j |dS |r0|  S | S d S )Nr0   )r   Z
issubdtyper   Zcomplexfloatingr   r   conjr/   )r   r/   r   r   r   r2   Q   s
    z_data_matrix.conjc                 C   s   | j | j ddS NTr0   )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   r4   `   s    z_data_matrix.count_nonzeroNc                 C   s8   t |std|  }|dur*||}| || S )z
        This function performs element-wise power.

        Parameters
        ----------
        n : n is a scalar

        dtype : If dtype is not specified, the current dtype will be preserved.
        zinput is not scalarN)r   r%   r   r1   r   )r   nr   r   r   r   r   powere   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
   r   r   propertyr   r   r   r   r    r"   r&   r+   r,   r1   r   __doc__r2   r/   r4   r6   r7   r   r   r   r   r      s,   







r   c                    s$    fdd}dt t f |_t |_|S )Nc                    s    |   }| j|ddS r3   )r   r   )r   resultopr   r   method   s    z_create_method.<locals>.methodz6Element-wise %s.

See `numpy.%s` for more information.)namer<   r8   )r?   r@   r   r>   r   _create_method   s    rB   c                 C   s>   t | D ]\}}||kr|  S q|d7 }||k r6|S dS d S )Nr   )	enumerate)indr5   kar   r   r   _find_missing_index   s    
rH   c                   @   sX   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
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|  }|dkr8|  n|  }|  ||\}}t|j| |k }||| d||< |dk}	t	|	|}t	|	|}ddl
m}
 |dkr|
|tt||ff| jd|fdS |
||tt|ff| j|dfdS d S )Nr   &zero-size array to reduction operationr   )
coo_matrix)r   shape)rL   
ValueErrortocsctocsrr   Z_minor_reducer   diffindptrcompress rK   zeroslenr   )r   axis
min_or_maxNMmatZmajor_indexvaluenot_fullmaskrK   r   r   r   _min_or_max_axis   s(    


z_minmax_mixin._min_or_max_axisc                 C   s   |d urt dt| |d u r~d| jv r2t d| jd}| jdkrL|S ||   }| jt	
| jkrz|||}|S |dk r|d7 }|dks|dkr| ||S t dd S )N2Sparse matrices do not support an 'out' parameter.r   rJ      r   zaxis out of range)rM   r   rL   r   typennzreducer   Zravelr   prodr^   )r   rV   outrW   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}t|S )Nr   z7Can't apply the operation along a zero-sized dimension.r`   )r   r   rC   )rL   rM   r   ra   rN   rO   r   Z_swapr   rT   intZnonzerorP   rQ   r   indicesrH   minZreshaper   )r   rV   r?   comparerf   rZ   Zret_sizeZ	line_sizeretZnz_linesipqr   rj   amrg   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| |d u rd| jv r2t d| jdkr@dS | jd}|  }|  ||j}|j| }|||rt	|j
| |jd  t	|j|  S t|j}	|	|jkr|S |j
|jd  |j }
t|
|	}||krt||S |S | |||S )Nr_   r   z-Can't apply the operation to an empty matrix.r   )rM   r   rL   rb   r   ra   Ztocoor   r   ri   rowcolr   rd   rH   rk   rs   )r   rV   re   r?   rl   rf   rZ   rq   rg   sizerE   rr   r   r   r   _arg_min_or_max   s0    




&


z_minmax_mixin._arg_min_or_maxNc                 C   s   |  ||tjS )a6  
        Return the maximum of the 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 the matrix 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 matrix along a given axis.
        numpy.matrix.max : NumPy's implementation of 'max' for matrices

        )rh   r   maximumr   rV   re   r   r   r   max#  s    z_minmax_mixin.maxc                 C   s   |  ||tjS )a6  
        Return the minimum of the 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 the matrix 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 matrix along a given axis.
        numpy.matrix.min : NumPy's implementation of 'min' for matrices

        )rh   r   Zminimumry   r   r   r   rk   C  s    z_minmax_mixin.minc                 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{   c  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   argminZlessry   r   r   r   r|   z  s    z_minmax_mixin.argmin)NN)NN)NN)NN)r8   r9   r:   r<   r^   rh   rs   rw   rz   rk   r{   r|   r   r   r   r   rI      s   %'
 
 
rI   )r<   Znumpyr   baser   r   Zsputilsr   r   r   __all__r   Znpfuncr8   rA   rB   setattrrH   rI   r   r   r   r   <module>   s   m