o
    ohZ                     @  s   d 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mZ ddlmZ edZed	ed
Zd5ddZd6ddZd6ddZd7ddZd8ddZd8ddZd9d!d"Zd:d$d%Zd&d' Zd(d) Zd*d+ Zd,d- Zd.d/ Zd0d1 Zd2d3 Zd4S );a"  

Module for the ddm_* routines for operating on a matrix in list of lists
matrix representation.

These routines are used internally by the DDM class which also provides a
friendlier interface for them. The idea here is to implement core matrix
routines in a way that can be applied to any simple list representation
without the need to use any particular matrix class. For example we can
compute the RREF of a matrix like:

    >>> from sympy.polys.matrices.dense import ddm_irref
    >>> M = [[1, 2, 3], [4, 5, 6]]
    >>> pivots = ddm_irref(M)
    >>> M
    [[1.0, 0.0, -1.0], [0, 1.0, 2.0]]

These are lower-level routines that work mostly in place.The routines at this
level should not need to know what the domain of the elements is but should
ideally document what operations they will use and what functions they need to
be provided with.

The next-level up is the DDM class which uses these routines but wraps them up
with an interface that handles copying etc and keeps track of the Domain of
the elements of the matrix:

    >>> from sympy.polys.domains import QQ
    >>> from sympy.polys.matrices.ddm import DDM
    >>> M = DDM([[QQ(1), QQ(2), QQ(3)], [QQ(4), QQ(5), QQ(6)]], (2, 3), QQ)
    >>> M
    [[1, 2, 3], [4, 5, 6]]
    >>> Mrref, pivots = M.rref()
    >>> Mrref
    [[1, 0, -1], [0, 1, 2]]

    )annotations)mul   )DMShapeErrorDMDomainErrorDMNonInvertibleMatrixErrorDMNonSquareMatrixError)SequenceTypeVar)RingElementTR)boundmatrixSequence[Sequence[T]]returnlist[list[T]]c                 C  s   t tt t|  S )zmatrix transpose)listmapzip)r    r   n/var/www/html/construction_image-detection-poc/venv/lib/python3.10/site-packages/sympy/polys/matrices/dense.pyddm_transpose8   s   r   alist[list[R]]bSequence[Sequence[R]]Nonec                 C  s:   t | |D ]\}}t|D ]\}}||  |7  < qqdS )za += bNr   	enumerater   r   aibijbijr   r   r   ddm_iadd=   
   r%   c                 C  s:   t | |D ]\}}t|D ]\}}||  |8  < qqdS )za -= bNr   r    r   r   r   ddm_isubD   r&   r'   c                 C  s*   | D ]}t |D ]	\}}| ||< qqdS )za <-- -aNr   )r   r!   r#   aijr   r   r   ddm_inegK   s
   r*   c                 C  s,   | D ]}t |D ]
\}}|| ||< qqdS )z	a <-- a*bNr(   r   r   r!   r#   r)   r   r   r   ddm_imulR   
   r,   c                 C  s,   | D ]}t |D ]
\}}|| ||< qqdS )z	a <-- b*aNr(   r+   r   r   r   	ddm_irmulY   r-   r.   cc                 C  sP   t t| }t|| D ]\}}t|D ]\}}ttt|||| ||< qqdS )z
a += b @ cN)r   r   r   sumr   r   )r   r   r/   cTr"   r!   r#   cTjr   r   r   ddm_imatmul`   s   r3   Fc              	     s|  t  }|sg S t  d }d}g }t|D ]|r5tt|| fddd} |  |  |<  |<  |  }|s_t|d |D ]} |  }|r] |  |  |<  |<  nqDq | }|d }	t|D ]
}
||
  |	9  < qlt D ]/\}}||ks| sq{| }|  |8  < td |D ]}
||
  |||
  8  < qq{| |d7 }||kr |S q|S )a  In-place reduced row echelon form of a matrix.

    Compute the reduced row echelon form of $a$. Modifies $a$ in place and
    returns a list of the pivot columns.

    Uses naive Gauss-Jordan elimination in the ground domain which must be a
    field.

    This routine is only really suitable for use with simple field domains like
    :ref:`GF(p)`, :ref:`QQ` and :ref:`QQ(a)` although even for :ref:`QQ` with
    larger matrices it is possibly more efficient to use fraction free
    approaches.

    This method is not suitable for use with rational function fields
    (:ref:`K(x)`) because the elements will blowup leading to costly gcd
    operations. In this case clearing denominators and using fraction free
    approaches is likely to be more efficient.

    For inexact numeric domains like :ref:`RR` and :ref:`CC` pass
    ``_partial_pivot=True`` to use partial pivoting to control rounding errors.

    Examples
    ========

    >>> from sympy.polys.matrices.dense import ddm_irref
    >>> from sympy import QQ
    >>> M = [[QQ(1), QQ(2), QQ(3)], [QQ(4), QQ(5), QQ(6)]]
    >>> pivots = ddm_irref(M)
    >>> M
    [[1, 0, -1], [0, 1, 2]]
    >>> pivots
    [0, 1]

    See Also
    ========

    sympy.polys.matrices.domainmatrix.DomainMatrix.rref
        Higher level interface to this routine.
    ddm_irref_den
        The fraction free version of this routine.
    sdm_irref
        A sparse version of this routine.

    References
    ==========

    .. [1] https://en.wikipedia.org/wiki/Row_echelon_form#Reduced_row_echelon_form
    r   c                   s   t  |   S N)abs)ipr   r#   r   r   <lambda>   s    zddm_irref.<locals>.<lambda>)keyr   )lenrangemaxr   append)r   _partial_pivotmnipivotsr6   r)   r!   aijinvlkakakjr   r7   r   	ddm_irrefk   sL   I
rI   c                 C  s
  t | }|s|jg fS t | d }d}g }g }d}t|D ]}| | | }	|	sNt|d |D ]}
| |
 | }	|	rG| |
 | | | |< | |
<  nq.|| q|rs|	| d |d   }|durd|||}t|D ]
\}
}|| |
 |< qh|D ]%}t|D ]}
| |
 | }|r||	9 }|dur|||}|| |
 |< q{qut| D ]6\}}||krqt|d |D ] }|	||  || | | |   }|dur|||}|||< q|j||< q|| |d7 }||kr n||	s|	}qd}q|s|j}||fS | d |d  }||fS )a#  a  <--  rref(a); return (den, pivots)

    Compute the fraction-free reduced row echelon form (RREF) of $a$. Modifies
    $a$ in place and returns a tuple containing the denominator of the RREF and
    a list of the pivot columns.

    Explanation
    ===========

    The algorithm used is the fraction-free version of Gauss-Jordan elimination
    described as FFGJ in [1]_. Here it is modified to handle zero or missing
    pivots and to avoid redundant arithmetic.

    The domain $K$ must support exact division (``K.exquo``) but does not need
    to be a field. This method is suitable for most exact rings and fields like
    :ref:`ZZ`, :ref:`QQ` and :ref:`QQ(a)`. In the case of :ref:`QQ` or
    :ref:`K(x)` it might be more efficient to clear denominators and use
    :ref:`ZZ` or :ref:`K[x]` instead.

    For inexact domains like :ref:`RR` and :ref:`CC` use ``ddm_irref`` instead.

    Examples
    ========

    >>> from sympy.polys.matrices.dense import ddm_irref_den
    >>> from sympy import ZZ, Matrix
    >>> M = [[ZZ(1), ZZ(2), ZZ(3)], [ZZ(4), ZZ(5), ZZ(6)]]
    >>> den, pivots = ddm_irref_den(M, ZZ)
    >>> M
    [[-3, 0, 3], [0, -3, -6]]
    >>> den
    -3
    >>> pivots
    [0, 1]
    >>> Matrix(M).rref()[0]
    Matrix([
    [1, 0, -1],
    [0, 1,  2]])

    See Also
    ========

    ddm_irref
        A version of this routine that uses field division.
    sdm_irref
        A sparse version of :func:`ddm_irref`.
    sdm_rref_den
        A sparse version of :func:`ddm_irref_den`.
    sympy.polys.matrices.domainmatrix.DomainMatrix.rref_den
        Higher level interface.

    References
    ==========

    .. [1] Fraction-free algorithms for linear and polynomial equations.
        George C. Nakos , Peter R. Turner , Robert M. Williams.
        https://dl.acm.org/doi/10.1145/271130.271133
    r   Nr   )r;   oner<   r>   exquor   zerois_one)r   Kr@   rA   drC   	no_pivotsrB   r#   r)   r6   	pivot_valjpjnpaijpajkpajkdenomr   r   r   ddm_irref_den   sp   W

 


rY   c           
      C  s*  t | }|s	|jS t | d }|j}|j}t|d D ]q}| | | sJt|d |D ]}| | | rD| | | | | |< | |< | } nq*|j  S |rV| |d  |d  n|j}t|d |D ]+}t|d |D ]!}	|| | |	 | | |  | | | | | |	   || | |	< qiq`q|| d d  S )a  a  <--  echelon(a); return det

    Explanation
    ===========

    Compute the determinant of $a$ using the Bareiss fraction-free algorithm.
    The matrix $a$ is modified in place. Its diagonal elements are the
    determinants of the leading principal minors. The determinant of $a$ is
    returned.

    The domain $K$ must support exact division (``K.exquo``). This method is
    suitable for most exact rings and fields like :ref:`ZZ`, :ref:`QQ` and
    :ref:`QQ(a)` but not for inexact domains like :ref:`RR` and :ref:`CC`.

    Examples
    ========

    >>> from sympy import ZZ
    >>> from sympy.polys.matrices.ddm import ddm_idet
    >>> a = [[ZZ(1), ZZ(2), ZZ(3)], [ZZ(4), ZZ(5), ZZ(6)], [ZZ(7), ZZ(8), ZZ(9)]]
    >>> a
    [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
    >>> ddm_idet(a, ZZ)
    0
    >>> a
    [[1, 2, 3], [4, -3, -6], [7, -6, 0]]
    >>> [a[i][i] for i in range(len(a))]
    [1, -3, 0]

    See Also
    ========

    sympy.polys.matrices.domainmatrix.DomainMatrix.det

    References
    ==========

    .. [1] https://en.wikipedia.org/wiki/Bareiss_algorithm
    .. [2] https://www.math.usm.edu/perry/Research/Thesis_DRL.pdf
    r   r   r:   )r;   rJ   rK   r<   rL   )
r   rN   r@   rA   rK   ufrF   rB   akkm1r#   r   r   r   ddm_idet  s,   -
@r\   c                   s    j stdt|}|sdS t|d |krt fddtD }dd t||D }t|}|ttkrAtdfdd|D | dd< dS )	a  ainv  <--  inv(a)

    Compute the inverse of a matrix $a$ over a field $K$ using Gauss-Jordan
    elimination. The result is stored in $ainv$.

    Uses division in the ground domain which should be an exact field.

    Examples
    ========

    >>> from sympy.polys.matrices.ddm import ddm_iinv, ddm_imatmul
    >>> from sympy import QQ
    >>> a = [[QQ(1), QQ(2)], [QQ(3), QQ(4)]]
    >>> ainv = [[None, None], [None, None]]
    >>> ddm_iinv(ainv, a, QQ)
    >>> ainv
    [[-2, 1], [3/2, -1/2]]
    >>> result = [[QQ(0), QQ(0)], [QQ(0), QQ(0)]]
    >>> ddm_imatmul(result, a, ainv)
    >>> result
    [[1, 0], [0, 1]]

    See Also
    ========

    ddm_irref: the underlying routine.
    zNot a fieldNr   c                   s$   g | ]  fd dt D qS )c                   s    g | ]}|kr j n jqS r   )rJ   rL   ).0r#   )rN   rB   r   r   
<listcomp>  s     z'ddm_iinv.<locals>.<listcomp>.<listcomp>)r<   )r]   rN   rA   )rB   r   r^     s   $ zddm_iinv.<locals>.<listcomp>c                 S  s   g | ]\}}|| qS r   r   )r]   roweyerowr   r   r   r^         z Matrix det == 0; not invertible.c                   s   g | ]}| d  qS r4   r   r]   r`   )rA   r   r   r^      rb   )	is_Fieldr   r;   r   r<   r   rI   r   r   )ainvr   rN   r@   eyeAaugrC   r   r_   r   ddm_iinv  s   rh   c           	      C  s   t |}|sg S t |d }t|}|jgt|| }td|D ]!}t||}|| d| | | d|< |d| || d|< q |S )a  L, U  <--  LU(U)

    Compute the LU decomposition of a matrix $L$ in place and store the lower
    and upper triangular matrices in $L$ and $U$, respectively. Returns a list
    of row swaps that were performed.

    Uses division in the ground domain which should be an exact field.

    Examples
    ========

    >>> from sympy.polys.matrices.ddm import ddm_ilu_split
    >>> from sympy import QQ
    >>> L = [[QQ(0), QQ(0)], [QQ(0), QQ(0)]]
    >>> U = [[QQ(1), QQ(2)], [QQ(3), QQ(4)]]
    >>> swaps = ddm_ilu_split(L, U, QQ)
    >>> swaps
    []
    >>> L
    [[0, 0], [3, 0]]
    >>> U
    [[1, 2], [0, -2]]

    See Also
    ========

    ddm_ilu
    ddm_ilu_solve
    r   r   N)r;   ddm_ilurL   minr<   )	LUrN   r@   rA   swapszerosrB   r#   r   r   r   ddm_ilu_split#  s   
ro   c           	   	   C  s   t | }|sg S t | d }g }tt||D ]e}| | | sFt|d |D ]}| | | rD|||f | | | | | |< | |<  nq&qt|d |D ].}| | | | | |  }|| | |< t|d |D ]}| | |  || | |  8  < qhqMq|S )a  a  <--  LU(a)

    Computes the LU decomposition of a matrix in place. Returns a list of
    row swaps that were performed.

    Uses division in the ground domain which should be an exact field.

    This is only suitable for domains like :ref:`GF(p)`, :ref:`QQ`, :ref:`QQ_I`
    and :ref:`QQ(a)`. With a rational function field like :ref:`K(x)` it is
    better to clear denominators and use division-free algorithms. Pivoting is
    used to avoid exact zeros but not for floating point accuracy so :ref:`RR`
    and :ref:`CC` are not suitable (use :func:`ddm_irref` instead).

    Examples
    ========

    >>> from sympy.polys.matrices.dense import ddm_ilu
    >>> from sympy import QQ
    >>> a = [[QQ(1, 2), QQ(1, 3)], [QQ(1, 4), QQ(1, 5)]]
    >>> swaps = ddm_ilu(a)
    >>> swaps
    []
    >>> a
    [[1/2, 1/3], [1/2, 1/30]]

    The same example using ``Matrix``:

    >>> from sympy import Matrix, S
    >>> M = Matrix([[S(1)/2, S(1)/3], [S(1)/4, S(1)/5]])
    >>> L, U, swaps = M.LUdecomposition()
    >>> L
    Matrix([
    [  1, 0],
    [1/2, 1]])
    >>> U
    Matrix([
    [1/2,  1/3],
    [  0, 1/30]])
    >>> swaps
    []

    See Also
    ========

    ddm_irref
    ddm_ilu_solve
    sympy.matrices.matrixbase.MatrixBase.LUdecomposition
    r   r   )r;   r<   rj   r>   )	r   r@   rA   rm   rB   r6   r#   l_jirF   r   r   r   ri   Q  s,   1"ri   c                   s  t |}|sdS t |d }t |}|stdt |d  ||kr&td||k r.td|rKdd |D }|D ]\}}	||	 || ||< ||	< q9 fddt|D }
t D ]*}t|D ]#}|| | }t|D ]}||| | |
| |  8 }ql||
| |< q`qZ||krt||D ]}t D ]
}|
| | rtqqt D ]=}tt|D ]4}|| | st|
| | }t|d |D ]}||| | | | |  8 }q||| |  | | |< qqdS )	a  x  <--  solve(L*U*x = swaps(b))

    Solve a linear system, $A*x = b$, given an LU factorization of $A$.

    Uses division in the ground domain which must be a field.

    Modifies $x$ in place.

    Examples
    ========

    Compute the LU decomposition of $A$ (in place):

    >>> from sympy import QQ
    >>> from sympy.polys.matrices.dense import ddm_ilu, ddm_ilu_solve
    >>> A = [[QQ(1), QQ(2)], [QQ(3), QQ(4)]]
    >>> swaps = ddm_ilu(A)
    >>> A
    [[1, 2], [3, -2]]
    >>> L = U = A

    Solve the linear system:

    >>> b = [[QQ(5)], [QQ(6)]]
    >>> x = [[None], [None]]
    >>> ddm_ilu_solve(x, L, U, swaps, b)
    >>> x
    [[-4], [9/2]]

    See Also
    ========

    ddm_ilu
        Compute the LU decomposition of a matrix in place.
    ddm_ilu_split
        Compute the LU decomposition of a matrix and separate $L$ and $U$.
    sympy.polys.matrices.domainmatrix.DomainMatrix.lu_solve
        Higher level interface to this function.
    Nr   zShape mismtchUnderdeterminedc                 S  s   g | ]}|d d  qS r4   r   rc   r   r   r   r^     rb   z!ddm_ilu_solve.<locals>.<listcomp>c                   s   g | ]}d g  qS r4   r   r]   _or   r   r^         r   )r;   r   NotImplementedErrorr<   r   reversed)xrk   rl   rm   r   r@   rA   m2i1i2yrF   rB   rhsr#   r   rt   r   ddm_ilu_solve  sR   (r   c                   s  t | }|s jggS t | d |krtddkr) jg| d d  ggS | d d }| d dd g}dd | dd D }dd | dd D }t| } fddtd D }tD ]}	 j||	 |	< | ||	d  |	< qetd	d D ]>}	|	d	kr|}
n|
} fd
d|D }
t|
||  jgg}t|||
 tdd |	 D ]}|d d  ||	|  |< qq fddtd D }t||| |S )a|  
    Berkowitz algorithm for computing the characteristic polynomial.

    Explanation
    ===========

    The Berkowitz algorithm is a division-free algorithm for computing the
    characteristic polynomial of a matrix over any commutative ring using only
    arithmetic in the coefficient ring.

    Examples
    ========

    >>> from sympy import Matrix
    >>> from sympy.polys.matrices.dense import ddm_berk
    >>> from sympy.polys.domains import ZZ
    >>> M = [[ZZ(1), ZZ(2)], [ZZ(3), ZZ(4)]]
    >>> ddm_berk(M, ZZ)
    [[1], [-5], [-2]]
    >>> Matrix(M).charpoly()
    PurePoly(lambda**2 - 5*lambda - 2, lambda, domain='ZZ')

    See Also
    ========

    sympy.polys.matrices.domainmatrix.DomainMatrix.charpoly
        The high-level interface to this function.

    References
    ==========

    .. [1] https://en.wikipedia.org/wiki/Samuelson%E2%80%93Berkowitz_algorithm
    r   z
Not squarer   Nc                 S  s   g | ]}|d  gqS )r   r   rc   r   r   r   r^   !  rv   zddm_berk.<locals>.<listcomp>c                 S  s   g | ]}|d d qS )r   Nr   rc   r   r   r   r^   "  rb   c                   s   g | ]} j g qS r   rL   rr   r_   r   r   r^   &  rb      c                      g | ]} j gqS r   r   rc   rN   r   r   r^   /      c                   r   r   r   rr   r   r   r   r^   6  r   )r;   rJ   r   ddm_berkr<   r3   rL   )MrN   r@   r   r   CAqr   rB   AnCRAnCr#   qoutr   r_   r   r     s>   "


r   N)r   r   r   r   )r   r   r   r   r   r   )r   r   r   r   )r   r   r   r   r   r   )r   r   r   r   r/   r   r   r   )F) __doc__
__future__r   operatorr   
exceptionsr   r   r   r   typingr	   r
   sympy.polys.matrices._typingr   r   r   r   r%   r'   r*   r,   r.   r3   rI   rY   r\   rh   ro   ri   r   r   r   r   r   r   <module>   s2    $







  =I/.KV