�
��gQ9 � �� � d dl mZ d dlmZ d dlmZ d dlmZ d dlm Z ddl
mZ ddlm
Z
mZ dd lmZ dd
lmZmZmZmZ ddlmZmZ G d� d
e� � Z G d� dee
� � ZeZdS )� )�Callable)�Dict)�sympy_deprecation_warning��is_sequence)�as_int� )�
MatrixBase)�MutableRepMatrix� RepMatrix)�_iszero)�_liupc� _row_structure_symbolic_cholesky�_cholesky_sparse�_LDLdecomposition_sparse)�_lower_triangular_solve_sparse�_upper_triangular_solve_sparsec �x � � e Zd ZdZe� fd�� � Zed� � � Zd� Zd� Z d� Z
d� Zd� Zd � Z
d
� Zd� Zdd
�Zdd�Z eeddd� � Z eeddd� � Zd� Zd� Zdd�Zdd�Zd� Zd� Zej e_ ej e_ ej e_ ej e_ ej e_ ej e_ � xZS )�SparseRepMatrixa
A sparse matrix (a matrix with a large number of zero elements).
Examples
========
>>> from sympy 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 �� �� �� t |� � dk rTt |d t � � r9|d j }|d j }|d � � � �||�fS i �t |� � dk r|d �d d |d g}t |� � dk �r�|d d� \ }}||cxu r�n nd x}}n?d ||fv rt
d� � �t |d � � t |d � � }}t |d t � � r�|d }d ||fv r#t
d� ||� � � � �� fd�t |� � D � � }� fd�t |� � D � � } |D ]8}
| D ]3}� � ||
|� � � � }|� j k r|�|
|f<