o
    icN                     @   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| _	nt|trt|rqt|| _	tdt|td| _| jt| jd}tjd|d| _nz|\}}	W n ty }
 zd}t||
d }
~
ww |d u rtd	|st}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rt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   Z
atleast_2darrayZ
atleast_1dZasarrayr   ndim_coo_containerastypelenunique)selfZarg1r   r   r   r   A	idx_dtyper"   r#   emessageZnewdtype r:   K/home/kim/smarthome/.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 )Nr/   matrixr   <z sparse z of dtype 'z'
	with z stored elements (z diagonals) and shape >)r
   r!   r&   r   r"   r   r   nnz)r5   _fmtZ
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#   )r5   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)NotImplementedErrorrJ   r(   count_nonzeror"   )r5   axisrI   r:   r:   r;   rL   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   )rK   r   r#   minint)r5   rM   MNr?   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   rM   r   r   )r   outr:   )rM   r   rU   )r   r   r   r   rJ   r"   sumr(   r)   Z_ascontainerZonesr   r3   r#   )r5   rM   r   rU   Z	res_dtyperE   rF   retrI   xresZrow_sumsoner:   r:   r;   rV      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rt|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(   Zarray_equalr#   
_with_datar"   Zunion1dZsearchsortedr   r3   _invert_indexrN   r)   Zresult_type_dia_container)	r5   otherZnew_offsetsZself_idxZ	other_idxZself_dZother_dZnew_datarB   r:   r:   r;   r]      s.   


**z_dia_base._add_sparsec                 C   s   |  | j| S N)r^   r"   )r5   ra   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   r3   r#   ravel)r5   ra   rX   yLrP   rQ   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   r[   )r   r0   r(   infr3   rN   r"   r#   r)   r   appendtyper,   )r5   valuesrR   rP   rQ   Zvalues_nn	min_indexZ	max_indexZ	data_rowsZ	data_colsr"   mr:   r:   r;   _setdiag   s4   





z_dia_base._setdiagc                 C   s   |r|   S | S rb   r   )r5   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(   rD   r3   Zintcr"   Zhstackr)   r   r`   )r5   Zaxesr   rE   rF   Zmax_dimr#   rcZ
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   Zconstant)mode)r   r(   emptyr"   r   Znonzeror#   r,   rN   sizer)   r3   pad)
r5   rR   rowscolsidxZ	first_colZlast_colZ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   rT   Frq   )r   r   )r?   Z_csc_containerr   r   r"   r(   rD   r#   r+   r,   r)   ZcumsumrV   Tr2   )r5   r   rE   rF   num_offsets
offset_lenrG   rH   rI   r7   Z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   )Zarraysr   Frq   )r   r   r   )r   r"   r(   rD   r#   Ztilere   r+   r,   r2   r1   r   )r5   r   rE   rF   r   r   rG   rH   rI   colr7   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 )zReturns a matrix with the same sparsity structure as self,
        but with different data.  By default the structure arrays are copied.
        r\   )r`   r#   r   r   )r5   r"   r   r:   r:   r;   r^   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#   rD   r$   )r5   r   rP   rQ   rI   r:   r:   r;   resize  s    

z_dia_base.resize)NNFrb   )NNN)r   )F)NF)T)__name__
__module____qualname___formatr    rC   rJ   rL   r   __doc__rS   rV   r]   rc   rh   rp   r%   rt   r~   r   r   r^   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(   Z
zeros_likerD   r3   )r{   invr:   r:   r;   r_     s   
r_   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   )rX   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(   Z
_lib._utilr   Z_matrixr   Z_baser	   r
   r   r   _datar   Z_sputilsr   r   r   r   r   r   Z_sparsetoolsr   r   r_   r   r   r   r:   r:   r:   r;   <module>   s$        L