
    wg#                     V    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 Zd Zd	 Zy
)    )Dict)Dummy)is_sequence)as_int
filldedent   )MutableSparseMatrixc                    t        | j                          D cg c]  }t        |       c}\  }}}dg|r|d   nddz   z  }t        |      D ]#  \  }}|j	                  |g|||dz
     z
  z         % |j	                  t        |      g| j                  t        |      z
  dz   z         | j                  | j                  g}||||gS c c}w )a  Converts a sparse matrix to Compressed Sparse Row (CSR) format.

    Parameters
    ==========

    A : contains non-zero elements sorted by key (row, column)
    JA : JA[i] is the column corresponding to A[i]
    IA : IA[i] contains the index in A for the first non-zero element
        of row[i]. Thus IA[i+1] - IA[i] gives number of non-zero
        elements row[i]. The length of IA is always 1 more than the
        number of rows in the matrix.

    Examples
    ========

    >>> from sympy.matrices.sparsetools import _doktocsr
    >>> from sympy import SparseMatrix, diag
    >>> m = SparseMatrix(diag(1, 2, 3))
    >>> m[2, 0] = -1
    >>> _doktocsr(m)
    [[1, 2, -1, 3], [0, 1, 0, 2], [0, 1, 2, 4], [3, 3]]

    r   r   )ziprow_listlist	enumerateextendlenrowscols)dokirowJAAIArshapes           _/home/mcse/projects/flask/flask-venv/lib/python3.12/site-packages/sympy/matrices/sparsetools.py	_doktocsrr   	   s    0 $'#78a$q'8JCQ
s1v!q(	)B# (1
		1#q3q1u:~&'(IIs1vh3r7*Q./0XXsxx Er2u 9s   Cc                     i }| \  }}}}t        t        |      dz
        D ]8  }t        ||   ||dz            }t        ||   ||         D ]  \  }}	||||	f<    : t	        g || S )a"  Converts a CSR representation to DOK representation.

    Examples
    ========

    >>> from sympy.matrices.sparsetools import _csrtodok
    >>> _csrtodok([[5, 8, 3, 6], [0, 1, 2, 1], [0, 0, 2, 3, 4], [4, 3]])
    Matrix([
    [0, 0, 0],
    [5, 8, 0],
    [0, 0, 3],
    [0, 6, 0]])

    r   )ranger   slicer   SparseMatrix)
csrsmatr   r   r   r   r   indiceslms
             r   	_csrtodokr&   *   s     DAr2u3r7Q; 1r!a%y)'
BwK0 	DAqDAJ	 %%%%    c                     	 t        |       dvrt        t        | d   t        t        f      st        t        |       dk(  r?|j                  dd      }|j                  dd      }|t        |      }|At        |      }n5t        |       dk(  rt        | d         x}}nt        t        | dd       \  }}t        d	 | d   D              }d }i g }t               | d   j                         D ]  \  }} ||      \  }	}
t        |t        t        f      rgd}t        |      D ]V  \  }||z  }t!              r4t#              |	|z   |
|z   f<   |t%        j&                        dz
  z  }J|	|z   |
|z   f<   X t!        |      rt#        |      }|j&                  \  }}|r9|r7t)        ||	z
  |      \  }}t)        ||
z
  |      \  }}|xs |}t%        ||      }n.|rt)        ||	z
  |      \  }}n|rt)        ||
z
  |      \  }}nd}d}|rt        t        d            t%        |j&                        }t+        |      D ]  }||	|
f<   |	|z  }	|
|z  }
 k|so|	|
f<   |j-                  ||f        t#        d      }|j/                         |*||j0                  k  rt        d|d|j0                        |*||j2                  k  rt        d|d|j2                        ||cxu rn n|j0                  }|j2                  }n5||t5        ||j2                        }n||t5        ||j0                        }fd}|rh|D ]c  \  } ||      \  }	}
t7              rnfd}d}|	|z   |k  s.|
|z   |k  s7 ||	|z   |
|z    ||             |dz  }|	|z   |k  s[|
|z   |k  r-e t#        ||      S # t        t        f$ r t        t        d
            w xY w)aW  Returns a SparseMatrix from the given dictionary describing
    the diagonals of the matrix. The keys are positive for upper
    diagonals and negative for those below the main diagonal. The
    values may be:

    * expressions or single-argument functions,

    * lists or tuples of values,

    * matrices

    Unless dimensions are given, the size of the returned matrix will
    be large enough to contain the largest non-zero value provided.

    kwargs
    ======

    rows : rows of the resulting matrix; computed if
           not given.

    cols : columns of the resulting matrix; computed if
           not given.

    Examples
    ========

    >>> from sympy import banded, ones, Matrix
    >>> from sympy.abc import x

    If explicit values are given in tuples,
    the matrix will autosize to contain all values, otherwise
    a single value is filled onto the entire diagonal:

    >>> banded({1: (1, 2, 3), -1: (4, 5, 6), 0: x})
    Matrix([
    [x, 1, 0, 0],
    [4, x, 2, 0],
    [0, 5, x, 3],
    [0, 0, 6, x]])

    A function accepting a single argument can be used to fill the
    diagonal as a function of diagonal index (which starts at 0).
    The size (or shape) of the matrix must be given to obtain more
    than a 1x1 matrix:

    >>> s = lambda d: (1 + d)**2
    >>> banded(5, {0: s, 2: s, -2: 2})
    Matrix([
    [1, 0, 1,  0,  0],
    [0, 4, 0,  4,  0],
    [2, 0, 9,  0,  9],
    [0, 2, 0, 16,  0],
    [0, 0, 2,  0, 25]])

    The diagonal of matrices placed on a diagonal will coincide
    with the indicated diagonal:

    >>> vert = Matrix([1, 2, 3])
    >>> banded({0: vert}, cols=3)
    Matrix([
    [1, 0, 0],
    [2, 1, 0],
    [3, 2, 1],
    [0, 3, 2],
    [0, 0, 3]])

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

    Errors are raised if the designated size will not hold
    all values an integral number of times. Here, the rows
    are designated as odd (but an even number is required to
    hold the off-diagonal 2x2 ones):

    >>> banded({0: 2, 1: ones(2)}, rows=5)
    Traceback (most recent call last):
    ...
    ValueError:
    sequence does not fit an integral number of times in the matrix

    And here, an even number of rows is given...but the square
    matrix has an even number of columns, too. As we saw
    in the previous example, an odd number is required:

    >>> banded(4, {0: 2, 1: ones(2)})  # trying to make 4x4 and cols must be odd
    Traceback (most recent call last):
    ...
    ValueError:
    sequence does not fit an integral number of times in the matrix

    A way around having to count rows is to enclosing matrix elements
    in a tuple and indicate the desired number of them to the right:

    >>> banded({0: 2, 2: (ones(2),)*3})
    Matrix([
    [2, 0, 1, 1, 0, 0, 0, 0],
    [0, 2, 1, 1, 0, 0, 0, 0],
    [0, 0, 2, 0, 1, 1, 0, 0],
    [0, 0, 0, 2, 1, 1, 0, 0],
    [0, 0, 0, 0, 2, 0, 1, 1],
    [0, 0, 0, 0, 0, 2, 1, 1]])

    An error will be raised if more than one value
    is written to a given entry. Here, the ones overlap
    with the main diagonal if they are placed on the
    first diagonal:

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

    By placing a 0 at the bottom left of the 2x2 matrix of
    ones, the collision is avoided:

    >>> u2 = Matrix([
    ... [1, 1],
    ... [0, 1]])
    >>> banded({0: [2]*5, 1: [u2]*3})
    Matrix([
    [2, 1, 1, 0, 0, 0, 0],
    [0, 2, 1, 0, 0, 0, 0],
    [0, 0, 2, 1, 1, 0, 0],
    [0, 0, 0, 2, 1, 0, 0],
    [0, 0, 0, 0, 2, 1, 1],
    [0, 0, 0, 0, 0, 0, 1]])
    )r         r   r   Nr   r)   r   c              3   2   K   | ]  }t        |        y wN)r   ).0ks     r   	<genexpr>zbanded.<locals>.<genexpr>   s     ,aq	,s   zNunrecognized input to banded:
            expecting [[row,] col,] {int: value}c                 *    | dk  r|  nd}|rdn| }||fS )Nr    )dr   cs      r   rczbanded.<locals>.rc   s#    a%QBQA!tr'   zh
                    sequence does not fit an integral number of times
                    in the matrixzDesignated rows z
 < needed zDesignated cols c                 \    |r)| |fv r| |f   |fvrt        d| |f      || |f<   y y )Nzcollision at )
ValueError)r   jvr"   tbas      r   updatezbanded.<locals>.update  sJ     1v~$q!t*S!H"< q!f!>??DAJ r'   c                     S r-   r2   )_vis    r   <lambda>zbanded.<locals>.<lambda>'  s    " r'   )r   	TypeError
isinstancedictr   getr   mapallr7   r   r   itemsr   tupler   r   r    minr   divmodr   appendtodokr   r   maxcallable)argskwargsr   r   r=   r5   undoner3   r9   r   r4   extrar   rvcvnrxrncxcxdor8   sr;   r"   r:   r>   s                           @@@r   bandedr[   B   s   H6t9I%O$r(T4L1Ot9>::fd+D::fd+Dd|d|Y!^ a/)D4VT"1X.JD$,48,,

 DF
'CR  )"1!u1 a$'E"1 ,2U
r?%b)B)+DQA&S]Q..E)+DQA&, ^QAWWFBq"-Bq"-BH"R[tax,Atax,A  -% "& ' ' AGGA2Y QT
QQ DAJMM1a&!S)"T 	T4 A779DD166MT166JKKD166MT166JKKtvvvv		dl4 		dl4   	EAra5DAqrlAAa%$,1q54<q1ua!eQqT*Q a%$,1q54<		 dD))i 	" 6
45 6 	66s   C	O $O+N)sympy.core.containersr   sympy.core.symbolr   sympy.utilities.iterablesr   sympy.utilities.miscr   r   sparser	   r    r   r&   r[   r2   r'   r   <module>ra      s%    & # 1 3 7B&0j*r'   