o
    8Va8                     @   s   d dl mZ d dlmZmZ d dlmZ d dlmZ ddl	m
Z
 ddlmZmZ ddlmZ dd	lmZmZmZmZ dd
lmZmZ G dd deZG dd deeZdS )    )Callable)as_intis_sequence)Dict)SymPyDeprecationWarning   )
MatrixBase)MutableRepMatrix	RepMatrix)_iszero)_liupc _row_structure_symbolic_cholesky_cholesky_sparse_LDLdecomposition_sparse)_lower_triangular_solve_sparse_upper_triangular_solve_sparsec                       s  e Zd ZdZe fddZedd Zdd Zdd	 Z	d
d Z
dd Zdd Zdd Zdd Zdd Zd*ddZd*ddZeedddZeedddZdd Zdd  Zd+d"d#Zd+d$d%Zd&d' Zd(d) Zeje_eje_eje_eje_eje_eje_  ZS ),SparseMatrixa"  
    A sparse matrix (a matrix with a large number of zero elements).

    Examples
    ========

    >>> from sympy.matrices import SparseMatrix, ones
    >>> SparseMatrix(2, 2, range(4))
    Matrix([
    [0, 1],
    [2, 3]])
    >>> SparseMatrix(2, 2, {(1, 1): 2})
    Matrix([
    [0, 0],
    [0, 2]])

    A SparseMatrix can be instantiated from a ragged list of lists:

    >>> SparseMatrix([[1, 2, 3], [1, 2], [1]])
    Matrix([
    [1, 2, 3],
    [1, 2, 0],
    [1, 0, 0]])

    For safety, one may include the expected size and then an error
    will be raised if the indices of any element are out of range or
    (for a flat list) if the total number of elements does not match
    the expected shape:

    >>> SparseMatrix(2, 2, [1, 2])
    Traceback (most recent call last):
    ...
    ValueError: List length (2) != rows*columns (4)

    Here, an error is not raised because the list is not flat and no
    element is out of range:

    >>> SparseMatrix(2, 2, [[1, 2]])
    Matrix([
    [1, 2],
    [0, 0]])

    But adding another element to the first (and only) row will cause
    an error to be raised:

    >>> SparseMatrix(2, 2, [[1, 2, 3]])
    Traceback (most recent call last):
    ...
    ValueError: The location (0, 2) is out of designated range: (1, 1)

    To autosize the matrix, pass None for rows:

    >>> SparseMatrix(None, [[1, 2, 3]])
    Matrix([[1, 2, 3]])
    >>> SparseMatrix(None, {(1, 1): 1, (3, 3): 3})
    Matrix([
    [0, 0, 0, 0],
    [0, 1, 0, 0],
    [0, 0, 0, 0],
    [0, 0, 0, 3]])

    Values that are themselves a Matrix are automatically expanded:

    >>> SparseMatrix(4, 4, {(1, 1): ones(2)})
    Matrix([
    [0, 0, 0, 0],
    [0, 1, 1, 0],
    [0, 1, 1, 0],
    [0, 0, 0, 0]])

    A ValueError is raised if the expanding matrix tries to overwrite
    a different element already present:

    >>> SparseMatrix(3, 3, {(0, 0): ones(2), (1, 1): 2})
    Traceback (most recent call last):
    ...
    ValueError: collision at (1, 1)

    See Also
    ========
    DenseMatrix
    MutableSparseMatrix
    ImmutableSparseMatrix
    c              
      s  t |dkr"t|d tr"|d j}|d j}|d  ||fS i t |dkr7|d d u r7d d |d g}t |dkr|d d \}}||  u rPd u rWn nd  }}nd ||fv ratdt|d t|d }}t|d tr|d }d ||fv rtd	|| fddt
|D } fd	dt
|D }	|D ]}
|	D ]} ||
|}| jkr||
|f< qq||fS t|d ttfr3fd
d}|d  D ]\\\}}}t|tr|  D ]\\}
}}|||
 || | qqt|ttfr# j|fi |\}}D ]\}
}|||
 || |
|f  qqՈ |}||| | qnft|d rtdd |d D  }|sX j|d fi |\}}nA|d }t ||| krptd	t |||t
|D ]$}
t
|D ]}||
| |  } |}| jkr||
|f< qzqt|d u rÈ }|rtdd |D d nd}|rtdd |D d nd}n+ D ]&\}
}|
r|
|ks|r||krtd	|
|fd|d d|d q||fS t |dkrOt|d ttfrO|d }d}t|D ]0\}
}t|ttfs|g}t|D ]\}}| jkr4 ||
|f< q!t|t |}q|rFt |nd}|}||fS t j| \}}}t
|D ]}
t
|D ]}|||
 |  }| jkrx||
|f< qbq\||fS )Nr   r         z*Pass rows=None and no cols for autosizing.z2{} and {} must be integers for this specification.c                       g | ]}  |qS  _sympify.0iclsr   7/usr/lib/python3/dist-packages/sympy/matrices/sparse.py
<listcomp>       z8SparseMatrix._handle_creation_inputs.<locals>.<listcomp>c                    r   r   r   )r   jr   r   r   r      r    c                    sR   |r'| |f v r| | |f krt d| |f| | |f | | |f< d S d S )Nz)There is a collision at {} for {} and {}.)
ValueErrorformat)r   r!   v)smatr   r   update   s   z4SparseMatrix._handle_creation_inputs.<locals>.updatec                 s   s    | ]}t |V  qd S N)r   r   r   r   r   	<genexpr>   s    z7SparseMatrix._handle_creation_inputs.<locals>.<genexpr>zMThe length of the flat list ({}) does not match the specified size ({} * {}).c                 S   s   g | ]\}}|qS r   r   )r   r_r   r   r   r          c                 S   s   g | ]\}}|qS r   r   )r   r*   cr   r   r   r      r+   z?The location {} is out of the designated range[{}, {}]x[{}, {}])len
isinstancer   rowscolstodokr"   r   r   r#   ranger   Zzerodictr   itemslisttuple_handle_creation_inputsr   anykeysmax	enumeratesuper)r   argskwargsr/   r0   r)   r,   opZrow_indicesZcol_indicesr   r!   valuer&   r$   Zvvr*   ZflatZ	flat_listr9   rowZmat	__class__)r   r%   r   r7   j   s   






 



 " 
"

z$SparseMatrix._handle_creation_inputsc                 C   s   t ddddd  |  S )Nz+The private _smat attribute of SparseMatrixzthe .todok() methodiT  z1.9)ZfeatureZ
useinsteadZissueZdeprecated_since_version)r   warnr1   selfr   r   r   _smat   s   zSparseMatrix._smatc                 K   s(   | j |dd|dt|dddS )NmethodLDL
iszerofunctry_block_diagF)rH   rJ   rK   )invgetr   )rF   r>   r   r   r   _eval_inverse   s   

zSparseMatrix._eval_inversec                 C   sT   t |stdi }|   D ]\}}||}|dkr |||< q| | j| j|S )aa  Apply a function to each element of the matrix.

        Examples
        ========

        >>> from sympy.matrices import SparseMatrix
        >>> m = SparseMatrix(2, 2, lambda i, j: i*2+j)
        >>> m
        Matrix([
        [0, 1],
        [2, 3]])
        >>> m.applyfunc(lambda i: 2*i)
        Matrix([
        [0, 2],
        [4, 6]])

        z`f` must be callable.r   )callable	TypeErrorr1   r4   _newr/   r0   )rF   fZdokkr$   Zfvr   r   r   	applyfunc   s   zSparseMatrix.applyfuncc                 C   s   ddl m} || S )z,Returns an Immutable version of this Matrix.r   )ImmutableSparseMatrix)Z	immutablerU   )rF   rU   r   r   r   as_immutable  s   zSparseMatrix.as_immutablec                 C      t | S )aC  Returns a mutable version of this matrix.

        Examples
        ========

        >>> from sympy import ImmutableMatrix
        >>> X = ImmutableMatrix([[1, 2], [3, 4]])
        >>> Y = X.as_mutable()
        >>> Y[1, 1] = 5 # Can set values in Y
        >>> Y
        Matrix([
        [1, 2],
        [3, 5]])
        )MutableSparseMatrixrE   r   r   r   
as_mutable   s   zSparseMatrix.as_mutablec                    s*    fddt t   dd dD S )a  Returns a column-sorted list of non-zero elements of the matrix.

        Examples
        ========

        >>> from sympy.matrices import SparseMatrix
        >>> a=SparseMatrix(((1, 2), (3, 4)))
        >>> a
        Matrix([
        [1, 2],
        [3, 4]])
        >>> a.CL
        [(0, 0, 1), (1, 0, 3), (0, 1, 2), (1, 1, 4)]

        See Also
        ========

        sympy.matrices.sparse.SparseMatrix.row_list
        c                       g | ]}t | | f qS r   r6   r   rS   rE   r   r   r   E      z)SparseMatrix.col_list.<locals>.<listcomp>c                 S   s   t t| S r'   )r5   reversedrS   r   r   r   <lambda>E  s    z'SparseMatrix.col_list.<locals>.<lambda>key)sortedr5   r1   r9   rE   r   rE   r   col_list1  s   *zSparseMatrix.col_listc                 C   s   t |  S )z2Returns the number of non-zero elements in Matrix.)r-   r1   rE   r   r   r   nnzG  s   zSparseMatrix.nnzc                    s&    fddt    dd dD S )a  Returns a row-sorted list of non-zero elements of the matrix.

        Examples
        ========

        >>> from sympy.matrices import SparseMatrix
        >>> a = SparseMatrix(((1, 2), (3, 4)))
        >>> a
        Matrix([
        [1, 2],
        [3, 4]])
        >>> a.RL
        [(0, 0, 1), (0, 1, 2), (1, 0, 3), (1, 1, 4)]

        See Also
        ========

        sympy.matrices.sparse.SparseMatrix.col_list
        c                    rZ   r   r[   r\   rE   r   r   r   _  r]   z)SparseMatrix.row_list.<locals>.<listcomp>c                 S   rW   r'   )r5   r_   r   r   r   r`   `  s    z'SparseMatrix.row_list.<locals>.<lambda>ra   )rc   r1   r9   rE   r   rE   r   row_listK  s   
zSparseMatrix.row_listc                 C   s   ||  S )z"Scalar element-wise multiplicationr   )rF   Zscalarr   r   r   scalar_multiplyb  s   zSparseMatrix.scalar_multiplyrI   c                 C   s   | j }||  j|d| | S )a  Return the least-square fit to the data.

        By default the cholesky_solve routine is used (method='CH'); other
        methods of matrix inversion can be used. To find out which are
        available, see the docstring of the .inv() method.

        Examples
        ========

        >>> from sympy.matrices import SparseMatrix, Matrix, ones
        >>> A = Matrix([1, 2, 3])
        >>> B = Matrix([2, 3, 4])
        >>> S = SparseMatrix(A.row_join(B))
        >>> S
        Matrix([
        [1, 2],
        [2, 3],
        [3, 4]])

        If each line of S represent coefficients of Ax + By
        and x and y are [2, 3] then S*xy is:

        >>> r = S*Matrix([2, 3]); r
        Matrix([
        [ 8],
        [13],
        [18]])

        But let's add 1 to the middle value and then solve for the
        least-squares value of xy:

        >>> xy = S.solve_least_squares(Matrix([8, 14, 18])); xy
        Matrix([
        [ 5/3],
        [10/3]])

        The error is given by S*xy - r:

        >>> S*xy - r
        Matrix([
        [1/3],
        [1/3],
        [1/3]])
        >>> _.norm().n(2)
        0.58

        If a different xy is used, the norm will be higher:

        >>> xy += ones(2, 1)/10
        >>> (S*xy - r).norm().n(2)
        1.5

        rH   )TrL   )rF   rhsrH   tr   r   r   solve_least_squaresf  s   6z SparseMatrix.solve_least_squaresc                 C   sD   | j s| j| jk rtd| j| jkrtddS | j|d|S )zReturn solution to self*soln = rhs using given inversion method.

        For a list of possible inversion methods, see the .inv() docstring.
        zUnder-determined system.z]For over-determined system, M, having more rows than columns, try M.solve_least_squares(rhs).rh   N)Z	is_squarer/   r0   r"   rL   Zmultiply)rF   rj   rH   r   r   r   solve  s   zSparseMatrix.solveNzAlternate faster representationc                 C   rW   r'   )r   rE   r   r   r   liupc     zSparseMatrix.liupcc                 C   rW   r'   )r   rE   r   r   r   row_structure_symbolic_cholesky  ro   z,SparseMatrix.row_structure_symbolic_choleskyTc                 C      t | |dS N)	hermitian)r   rF   rs   r   r   r   cholesky     zSparseMatrix.choleskyc                 C   rq   rr   )r   rt   r   r   r   LDLdecomposition  rv   zSparseMatrix.LDLdecompositionc                 C   
   t | |S r'   )r   rF   rj   r   r   r   lower_triangular_solve     
z#SparseMatrix.lower_triangular_solvec                 C   rx   r'   )r   ry   r   r   r   upper_triangular_solve  r{   z#SparseMatrix.upper_triangular_solve)rI   )T)__name__
__module____qualname____doc__classmethodr7   propertyrG   rN   rT   rV   rY   rd   re   rf   rg   rl   rm   ZRLZCLrn   rp   ru   rw   rz   r|   r   r   r   r   __classcell__r   r   rB   r   r      s>    U 

 

9

r   c                   @   s   e Zd Zedd ZdS )rX   c                 O   s.   | j |i |\}}}| |||}| |S r'   )r7   Z_smat_to_DomainMatrixZ_fromrep)r   r=   r>   r/   r0   r%   Zrepr   r   r   rQ     s   
zMutableSparseMatrix._newN)r}   r~   r   r   rQ   r   r   r   r   rX     s    rX   N)Zcollections.abcr   Zsympy.core.compatibilityr   r   Zsympy.core.containersr   Zsympy.utilities.exceptionsr   Zmatricesr   Z	repmatrixr	   r
   Z	utilitiesr   Zdecompositionsr   r   r   r   Zsolversr   r   r   rX   r   r   r   r   <module>   s       9