o
    Ö­jcN  ã                   @   sÈ   d Z dZg d¢ZddlZddlmZ ddlmZ dd	l	m
Z
mZmZmZ dd
lmZ ddlmZmZmZmZmZmZ ddlmZ G dd„ deƒZdd„ Zdd„ ZG dd„ deeƒZG dd„ deeƒZdS )zSparse DIAgonal formatzrestructuredtext en)Ú	dia_arrayÚ
dia_matrixÚisspmatrix_diaé    Né   )Úcopy_if_neededé   )Úspmatrix)ÚissparseÚ_formatsÚ_spbaseÚsparray)Ú_data_matrix)ÚisshapeÚupcast_charÚgetdtypeÚget_sum_dtypeÚvalidateaxisÚcheck_shape)Ú
dia_matvecc                   @   s  e Zd ZdZd)ddœ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d„Zejj	e_	dd„ Zdd„ Zdd„ Zd,dd„Zd-dd„Zejj	e_	d.dd„Zejj	e_	d,dd„Zejj	e_	d-d d!„Zejj	e_	d-d"d#„Zejj	e_	d/d%d&„Zd'd(„ Zejj	e_	dS )0Ú	_dia_baseÚdiaNF©Úmaxprintc             
   C   s¾  t j| ||d� t|ƒrF|jdkr&|r| ¡ }|j| _|j| _t|jƒ| _	nä|j| jkr3|r3| ¡ }n| 
¡ }|j| _|j| _t|jƒ| _	nÄt|tƒrÁt|ƒrqt|ƒ| _	t dt|td�¡| _| jt| jƒd�}tjd|d�| _n™z|\}}	W n tyŠ }
 zd}t|ƒ|
‚d }
~
ww |d u r“td	ƒ‚|s—t}t tj|d ||d
�¡| _tj|d | jt|ƒd�|d
�}	t |	¡| _t|ƒ| _	nIzt |¡}W n tyÞ }
 z
td| j› d�ƒ|
‚d }
~
ww t| tƒrò|jdkròtd|j› d�ƒ‚| j|||d� 
¡ }|j| _|j| _t|jƒ| _	|d u�rt|ƒ}| j |¡| _| jjdk�r%tdƒ‚| jjdk�r0tdƒ‚| jjd t| jƒk�rKtd| jjd t| jƒf ƒ‚tt  | j¡ƒt| jƒk�r]tdƒ‚d S )Nr   r   )r   r   )Údefault©Úmaxvalr   ©Údtypez+unrecognized form for dia_array constructorzexpected a shape argument)r   Úcopyr   zunrecognized form for z_matrix constructorr   zDIA arrays don't support zD input. Use 2D)r   Úshapezoffsets array must have rank 1zdata array must have rank 2zBnumber of diagonals (%d) does not match the number of offsets (%d)z&offset array contains duplicate values)!r   Ú__init__r	   Úformatr   ÚdataÚoffsetsr   r   Ú_shapeÚtodiaÚ
isinstanceÚtupler   ÚnpÚzerosr   ÚfloatÚ_get_index_dtypeÚmaxÚ	ExceptionÚ
ValueErrorr   Ú
atleast_2dÚarrayÚ
atleast_1dÚasarrayr   ÚndimÚ_coo_containerÚastypeÚlenÚunique)ÚselfÚarg1r   r   r   r   ÚAÚ	idx_dtyper"   r#   ÚeÚmessageÚnewdtype© r?   úN/var/www/html/CropPilot/venv/lib/python3.10/site-packages/scipy/sparse/_dia.pyr       sˆ   




€þ
þÿÿ€ÿ
þÿz_dia_base.__init__c                 C   s\   t | j \}}t| tƒrdnd}| jjd }d|› d|› d| j› d| j› d|› d	| j› d
�S )Nr0   Úmatrixr   ú<z sparse z of dtype 'z'
	with z stored elements (z diagonals) and shape ú>)r
   r!   r&   r   r"   r   r   Únnz)r8   Ú_ÚfmtÚ
sparse_clsÚdr?   r?   r@   Ú__repr__c   s   ÿÿÿÿz_dia_base.__repr__c                 C   sV   | j \}}t | jj d ¡}|| jdd…df  }|dk}|||k M }|||k M }|S )z~Returns a mask of the same shape as self.data, where
        mask[i,j] is True when data[i,j] corresponds to a stored element.r   Nr   )r   r(   Úaranger"   r#   )r8   Únum_rowsÚnum_colsÚoffset_indsÚrowÚmaskr?   r?   r@   Ú
_data_maskl   s   
z_dia_base._data_maskc                 C   s(   |d urt dƒ‚|  ¡ }t | j| ¡S )Nz<count_nonzero over an axis is not implemented for DIA format)ÚNotImplementedErrorrP   r(   Úcount_nonzeror"   )r8   ÚaxisrO   r?   r?   r@   rR   w   s   ÿz_dia_base.count_nonzeroc                 C   s`   |d urt dƒ‚| j\}}d}| jD ]}|dkr"|t||| ƒ7 }q|t|| |ƒ7 }qt|ƒS )Nz6_getnnz over an axis is not implemented for DIA formatr   )rQ   r   r#   ÚminÚint)r8   rS   ÚMÚNrD   Úkr?   r?   r@   Ú_getnnz�   s   

z_dia_base._getnnzc              	   C   s*  t |ƒ |d ur|dk r|d7 }t| jƒ}| j\}}d }|dkrP|  ¡ }| j| jdd�}	|	jd |kr7|	}
ntj||	jd�}
|	|
d |	jd …< | j	|
|d�}n=tj|df|d�}tj
||d�}t||t| jƒ| jjd | j| j||ƒ |  	|¡}|d u r„|j||d�S |  	|j|d�¡}|jd||d�S )	Nr   r   ©rS   r   r   )r   Úoutr?   )rS   r   r[   )r   r   r   r   rP   r"   Úsumr(   r)   Ú_ascontainerÚonesr   r6   r#   )r8   rS   r   r[   Ú	res_dtyperK   rL   ÚretrO   ÚxÚresÚrow_sumsÚoner?   r?   r@   r\   �   s0   

ÿ
z_dia_base.sumc           	      C   sª  t |tƒs
| | ¡S t | j|j¡r|  | j|j ¡S t | j|j¡}t 	|| j¡}t 	||j¡}| jj
d }|jj
d }||kr_t|ƒt| jƒkr_| jt|ƒ }||d d …f  |j7  < nl||kr�t|ƒt|jƒkr�|jt|ƒ }||d d …f  | j7  < nJt| j
d |d  | j
d ƒ}tjt|ƒ|ft | j|j¡d�}||d |…f  | jd d …d |…f 7  < ||d |…f  |jd d …d |…f 7  < | j||f| j
d�S )Nr   r   éÿÿÿÿr   ©r   )r&   r   Ú_add_sparser(   Úarray_equalr#   Ú
_with_datar"   Úunion1dÚsearchsortedr   r6   Ú_invert_indexrT   r)   Úresult_typeÚ_dia_container)	r8   ÚotherÚnew_offsetsÚself_idxÚ	other_idxÚself_dÚother_dÚnew_datarH   r?   r?   r@   rg   µ   s.   


þ**z_dia_base._add_sparsec                 C   s   |   | j| ¡S ©N)ri   r"   )r8   ro   r?   r?   r@   Ú_mul_scalarÚ   s   z_dia_base._mul_scalarc              
   C   sh   |}t j| jd t| jj|jjƒd�}| jjd }| j\}}t||t| j	ƒ|| j	| j| 
¡ | 
¡ ƒ |S )Nr   r   r   )r(   r)   r   r   r   Úcharr"   r   r6   r#   Úravel)r8   ro   ra   ÚyÚLrV   rW   r?   r?   r@   Ú_matmul_vectorÝ   s   ÿ
ÿz_dia_base._matmul_vectorr   c                 C   sR  | j \}}|jdkrtj}nt|ƒ}|dk r#t|| ||ƒ}d}|}nt||| |ƒ}|}|| }|jdkr<|d |… }| jj \}	}
|| jv rr||
krdtj|	|f| jj	d�}| j|d d …d |
…f< || _|| j| j|k||…f< d S t 
| j| jj	 |¡¡| _t||
ƒ}tj|	d |f| jj	d�}| j|d d…d |
…f< ||d||…f< || _d S )Nr   r   r   re   )r   r3   r(   Úinfr6   rT   r"   r#   r)   r   ÚappendÚtyper,   )r8   ÚvaluesrX   rV   rW   Úvalues_nÚnÚ	min_indexÚ	max_indexÚ	data_rowsÚ	data_colsr"   Úmr?   r?   r@   Ú_setdiagì   s4   





z_dia_base._setdiagc                 C   s   |r|   ¡ S | S rv   ©r   )r8   r   r?   r?   r@   r%     s   z_dia_base.todiac                 C   sÜ   |d ur|dkrt dƒ‚| j\}}t| jƒ}| j }tjt|ƒtjd�d d …d f }tj|tjd�|| d d …d f  }td|| jjd  ƒ}	t 	| jtj
| jjd |	f| jjd�f¡}
|
||f }
| j|
|f||f|d�S )N)r   r   zvSparse arrays/matrices do not support an 'axes' parameter because swapping dimensions is the only logical permutation.r   r   r   )r   r   )r.   r   r,   r#   r(   rJ   r6   Úintcr"   Úhstackr)   r   rn   )r8   Úaxesr   rK   rL   Úmax_dimr#   ÚrÚcÚ
pad_amountr"   r?   r?   r@   Ú	transpose  s"   

 $
ÿ
ÿÿz_dia_base.transposec           
      C   s¾   | j \}}|| ks||krtjd| jjd�S t | j|k¡\}td|ƒ}t|| |ƒ}|| }|j	dkr>tj
|| jjd�S | j|d ||…f }|t|ƒ }	|	dkr]tj|d|	fdd�}|S )Nr   r   Úconstant)Úmode)r   r(   Úemptyr"   r   Únonzeror#   r,   rT   Úsizer)   r6   Úpad)
r8   rX   ÚrowsÚcolsÚidxÚ	first_colÚlast_colÚresult_sizeÚresultÚpaddingr?   r?   r@   Údiagonal1  s   


z_dia_base.diagonalc                 C   s$  | j dkr| j| j| jd�S | j\}}| jj\}}t |¡}|| jd d …d f  }|dk}|||k M }|||k M }|| jdkM }| jt	| jƒd�}	tj
|d |	d�}
t |jdd�d |… ¡|
d|d …< ||k rs|
| |
|d d …< |j|j j|	dd�}| jj|j }| j|||
f| j| jd�S )	Nr   r   r   r   rZ   Fr‰   )r   r   )rD   Ú_csc_containerr   r   r"   r(   rJ   r#   r+   r,   r)   Úcumsumr\   ÚTr5   )r8   r   rK   rL   Únum_offsetsÚ
offset_lenrM   rN   rO   r;   ÚindptrÚindicesr"   r?   r?   r@   ÚtocscC  s(   


&ÿz_dia_base.tocscc                 C   sÜ   | j \}}| jj \}}t |¡}|| jd d …d f  }|dk}|||k M }|||k M }|| jdkM }|| }t ||¡| ¡  }	| j| jft| j ƒd�}
|j	|
dd�}|	j	|
dd�}	| j| }| j
|||	ff| j | jdd�S )Nr   )Úarraysr   Fr‰   )r   r   r   )r   r"   r(   rJ   r#   Útilery   r+   r,   r5   r4   r   )r8   r   rK   rL   r¤   r¥   rM   rN   rO   Úcolr;   r"   r?   r?   r@   Útocoo]  s&   

ÿ
ÿz_dia_base.tocooTc                 C   s4   |r| j || j ¡ f| jd�S | j || jf| jd�S )z‘Returns a matrix with the same sparsity structure as self,
        but with different data.  By default the structure arrays are copied.
        rf   )rn   r#   r   r   )r8   r"   r   r?   r?   r@   ri   x  s   ÿÿz_dia_base._with_datac                 G   sš   t |ƒ}|\}}| jd d …d |…f | _|| jd krHt | j| jd  | jjd k ¡rH| jd d …d f | jd  t | jjd ¡k}d| j|< || _d S )Nr   r   )r   r"   r   r(   Úanyr#   rJ   r$   )r8   r   rV   rW   rO   r?   r?   r@   Úresize…  s    ÿÿ

z_dia_base.resize)NNFrv   )NNN)r   )F)NF)T)Ú__name__Ú
__module__Ú__qualname__Ú_formatr    rI   rP   rR   r   Ú__doc__rY   r\   rg   rw   r|   rˆ   r%   r‘   r    r¨   r¬   ri   r®   r?   r?   r?   r@   r      s8    M	





#%

%









r   c                 C   s    t  | ¡}t  t| ƒ¡|| < |S )z)Helper function to invert an index array.)r(   Ú
zeros_likerJ   r6   )rš   Úinvr?   r?   r@   rl   —  s   
rl   c                 C   s
   t | tƒS )aÒ  Is `x` of dia_matrix type?

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

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

    Examples
    --------
    >>> from scipy.sparse import dia_array, dia_matrix, coo_matrix, isspmatrix_dia
    >>> isspmatrix_dia(dia_matrix([[5]]))
    True
    >>> isspmatrix_dia(dia_array([[5]]))
    False
    >>> isspmatrix_dia(coo_matrix([[5]]))
    False
    )r&   r   )ra   r?   r?   r@   r   ž  s   
r   c                   @   ó   e Zd ZdZdS )r   a<  
    Sparse array with DIAgonal storage.

    This can be instantiated in several ways:
        dia_array(D)
            where D is a 2-D ndarray

        dia_array(S)
            with another sparse array or matrix S (equivalent to S.todia())

        dia_array((M, N), [dtype])
            to construct an empty array with shape (M, N),
            dtype is optional, defaulting to dtype='d'.

        dia_array((data, offsets), shape=(M, N))
            where the ``data[k,:]`` stores the diagonal entries for
            diagonal ``offsets[k]`` (See example below)

    Attributes
    ----------
    dtype : dtype
        Data type of the array
    shape : 2-tuple
        Shape of the array
    ndim : int
        Number of dimensions (this is always 2)
    nnz
    size
    data
        DIA format data array of the array
    offsets
        DIA format offset array of the array
    T

    Notes
    -----

    Sparse arrays can be used in arithmetic operations: they support
    addition, subtraction, multiplication, division, and matrix power.
    Sparse arrays with DIAgonal storage do not support slicing.

    Examples
    --------

    >>> import numpy as np
    >>> from scipy.sparse import dia_array
    >>> dia_array((3, 4), dtype=np.int8).toarray()
    array([[0, 0, 0, 0],
           [0, 0, 0, 0],
           [0, 0, 0, 0]], dtype=int8)

    >>> data = np.array([[1, 2, 3, 4]]).repeat(3, axis=0)
    >>> offsets = np.array([0, -1, 2])
    >>> dia_array((data, offsets), shape=(4, 4)).toarray()
    array([[1, 0, 3, 0],
           [1, 2, 0, 4],
           [0, 2, 3, 0],
           [0, 0, 3, 4]])

    >>> from scipy.sparse import dia_array
    >>> n = 10
    >>> ex = np.ones(n)
    >>> data = np.array([ex, 2 * ex, ex])
    >>> offsets = np.array([-1, 0, 1])
    >>> dia_array((data, offsets), shape=(n, n)).toarray()
    array([[2., 1., 0., ..., 0., 0., 0.],
           [1., 2., 1., ..., 0., 0., 0.],
           [0., 1., 2., ..., 0., 0., 0.],
           ...,
           [0., 0., 0., ..., 2., 1., 0.],
           [0., 0., 0., ..., 1., 2., 1.],
           [0., 0., 0., ..., 0., 1., 2.]])
    N©r¯   r°   r±   r³   r?   r?   r?   r@   r   ¹  ó    r   c                   @   r¶   )r   aO  
    Sparse matrix with DIAgonal storage.

    This can be instantiated in several ways:
        dia_matrix(D)
            where D is a 2-D ndarray

        dia_matrix(S)
            with another sparse array or matrix S (equivalent to S.todia())

        dia_matrix((M, N), [dtype])
            to construct an empty matrix with shape (M, N),
            dtype is optional, defaulting to dtype='d'.

        dia_matrix((data, offsets), shape=(M, N))
            where the ``data[k,:]`` stores the diagonal entries for
            diagonal ``offsets[k]`` (See example below)

    Attributes
    ----------
    dtype : dtype
        Data type of the matrix
    shape : 2-tuple
        Shape of the matrix
    ndim : int
        Number of dimensions (this is always 2)
    nnz
    size
    data
        DIA format data array of the matrix
    offsets
        DIA format offset array of the matrix
    T

    Notes
    -----

    Sparse matrices can be used in arithmetic operations: they support
    addition, subtraction, multiplication, division, and matrix power.
    Sparse matrices with DIAgonal storage do not support slicing.

    Examples
    --------

    >>> import numpy as np
    >>> from scipy.sparse import dia_matrix
    >>> dia_matrix((3, 4), dtype=np.int8).toarray()
    array([[0, 0, 0, 0],
           [0, 0, 0, 0],
           [0, 0, 0, 0]], dtype=int8)

    >>> data = np.array([[1, 2, 3, 4]]).repeat(3, axis=0)
    >>> offsets = np.array([0, -1, 2])
    >>> dia_matrix((data, offsets), shape=(4, 4)).toarray()
    array([[1, 0, 3, 0],
           [1, 2, 0, 4],
           [0, 2, 3, 0],
           [0, 0, 3, 4]])

    >>> from scipy.sparse import dia_matrix
    >>> n = 10
    >>> ex = np.ones(n)
    >>> data = np.array([ex, 2 * ex, ex])
    >>> offsets = np.array([-1, 0, 1])
    >>> dia_matrix((data, offsets), shape=(n, n)).toarray()
    array([[2., 1., 0., ..., 0., 0., 0.],
           [1., 2., 1., ..., 0., 0., 0.],
           [0., 1., 2., ..., 0., 0., 0.],
           ...,
           [0., 0., 0., ..., 2., 1., 0.],
           [0., 0., 0., ..., 1., 2., 1.],
           [0., 0., 0., ..., 0., 1., 2.]])
    Nr·   r?   r?   r?   r@   r     r¸   r   )r³   Ú__docformat__Ú__all__Únumpyr(   Ú
_lib._utilr   Ú_matrixr   Ú_baser	   r
   r   r   Ú_datar   Ú_sputilsr   r   r   r   r   r   Ú_sparsetoolsr   r   rl   r   r   r   r?   r?   r?   r@   Ú<module>   s$        L