o
    oh[                     @   sr  d dl mZ d dlmZ ddlmZ ddlmZ ddlm	Z	m
Z
mZ ddlmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZ ddlmZmZmZm Z  dd	l!m"Z"m#Z#m$Z$m%Z% dd
l&m'Z'm(Z(m)Z)m*Z*m+Z+m,Z,m-Z-m.Z.m/Z/m0Z0m1Z1m2Z2m3Z3m4Z4 ddl5m6Z6 ddgiZ7G dd deZ8G dd de8Z9G dd de9Z:G dd de:Z;G dd deZ<G dd deZ=dS )    )Basic)Dummy   )MatrixCommon)NonSquareMatrixError)_iszero_is_zero_after_expand_mul	_simplify)_find_reasonable_pivot_find_reasonable_pivot_naive	_adjugate	_charpoly	_cofactor_cofactor_matrix_per_det_det_bareiss_det_berkowitz	_det_bird_det_laplace_det_LU_minor_minor_submatrix)_is_echelon_echelon_form_rank_rref)_columnspace
_nullspace	_rowspace_orthogonalize)
_eigenvals_eigenvects_bidiagonalize_bidiagonal_decomposition_is_diagonalizable_diagonalize_is_positive_definite_is_positive_semidefinite_is_negative_definite_is_negative_semidefinite_is_indefinite_jordan_form_left_eigenvects_singular_values)
MatrixBase)MatrixEigen.is_indefinite MatrixEigen.is_negative_definite$MatrixEigen.is_negative_semidefinite MatrixEigen.is_positive_definite$MatrixEigen.is_positive_semidefinite
matplotlibc                   @   s  e Zd ZdZefddZdd ZedfddZd	d
 Z	dd Z
dd Zd"ddZdefddZd"ddZd"ddZd#ddZdd Zd"ddZd d! Zeje_eje_eje_eje_eje	_eje
_eje_eje_eje_eje_eje_e je_eje_e!je_e"je_e#je_dS )$MatrixDeterminantzProvides basic matrix determinant operations. Should not be instantiated
    directly. See ``determinant.py`` for their implementations.c                 C      t | |dS )N)
iszerofuncr   )selfr8    r;   k/var/www/html/construction_image-detection-poc/venv/lib/python3.10/site-packages/sympy/matrices/matrices.py_eval_det_bareiss3      z#MatrixDeterminant._eval_det_bareissc                 C      t | S N)r   r:   r;   r;   r<   _eval_det_berkowitz6      z%MatrixDeterminant._eval_det_berkowitzNc                 C      t | ||dS )N)r8   simpfunc)r   )r:   r8   rE   r;   r;   r<   _eval_det_lu9      zMatrixDeterminant._eval_det_luc                 C   r?   r@   )r   rA   r;   r;   r<   _eval_det_bird<   rC   z MatrixDeterminant._eval_det_birdc                 C   r?   r@   )r   rA   r;   r;   r<   _eval_det_laplace?   rC   z#MatrixDeterminant._eval_det_laplacec                 C   r?   r@   r   rA   r;   r;   r<   _eval_determinantB   rC   z#MatrixDeterminant._eval_determinant	berkowitzc                 C   r7   Nmethod)r   r:   rO   r;   r;   r<   adjugateE   r>   zMatrixDeterminant.adjugatelambdac                 C   rD   )N)xsimplify)r   r:   rS   rT   r;   r;   r<   charpolyH   rG   zMatrixDeterminant.charpolyc                 C      t | |||dS rM   )r   r:   ijrO   r;   r;   r<   cofactorK      zMatrixDeterminant.cofactorc                 C   r7   rM   )r   rP   r;   r;   r<   cofactor_matrixN   r>   z!MatrixDeterminant.cofactor_matrixbareissc                 C   rD   )N)rO   r8   rJ   )r:   rO   r8   r;   r;   r<   detQ   rG   zMatrixDeterminant.detc                 C   r?   r@   )r   rA   r;   r;   r<   perT   rC   zMatrixDeterminant.perc                 C   rW   rM   )r   rX   r;   r;   r<   minorW   r\   zMatrixDeterminant.minorc                 C   s   t | ||S r@   )r   r:   rY   rZ   r;   r;   r<   minor_submatrixZ   r>   z!MatrixDeterminant.minor_submatrixrL   )r^   N)$__name__
__module____qualname____doc__r   r=   rB   r   rF   rH   rI   rK   rQ   r	   rV   r[   r]   r_   r`   ra   rc   r
   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r;   r;   r;   r<   r6   /   s@    




r6   c                   @   s   e Zd ZdZeddfddZedd ZedfddZd	d
 Z	edddfddZ
eje_eje_eje_eje
_d#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dS )%MatrixReductionszProvides basic matrix row/column operations. Should not be instantiated
    directly. See ``reductions.py`` for some of their implementations.Fc                 C   rW   )N)r8   rT   with_pivots)r   )r:   r8   rT   rj   r;   r;   r<   echelon_forms      zMatrixReductions.echelon_formc                 C   r?   r@   )r   rA   r;   r;   r<   
is_echelonw      zMatrixReductions.is_echelonc                 C   rD   )N)r8   rT   )r   )r:   r8   rT   r;   r;   r<   rank{   rG   zMatrixReductions.rankc                 C   sL   t | | | | j|\}}|ddd| jf |dd|j df fS )a  Return reduced row-echelon form of matrix, matrix showing
        rhs after reduction steps. ``rhs`` must have the same number
        of rows as ``self``.

        Examples
        ========

        >>> from sympy import Matrix, symbols
        >>> r1, r2 = symbols('r1 r2')
        >>> Matrix([[1, 1], [2, 1]]).rref_rhs(Matrix([r1, r2]))
        (Matrix([
        [1, 0],
        [0, 1]]), Matrix([
        [ -r1 + r2],
        [2*r1 - r2]]))
        N)r   hstackeyerowscols)r:   rhsr_r;   r;   r<   rref_rhs~   s   .zMatrixReductions.rref_rhsTc                 C   s   t | ||||dS )N)r8   rT   pivotsnormalize_last)r   )r:   r8   rT   rx   ry   r;   r;   r<   rref   s   zMatrixReductions.rrefcolc           	      C   s  |dvrt d|||dkr| jn| j}|dkrE|dur |n|}|du s*|du r1t d|d|  kr;|k sDn t d||n|d	kr||||hdg}t|d
krb|||hdg}t|d
krot d||\}}d|  kr}|k sn t d||d|  kr|k sn t d||na|dkr|du r|n|}|du r|n|}|du s|du s|du rt d|||krt d|d|  kr|k sn t d||d|  kr|k sn t d||nt dt| |||||fS )zValidate the arguments for a row/column operation.  ``error_str``
        can be one of "row" or "col" depending on the arguments being parsed.)n->knn<->mn->n+kmzOUnknown {} operation '{}'. Valid col operations are 'n->kn', 'n<->m', 'n->n+km'r{   r|   NzEFor a {0} operation 'n->kn' you must provide the kwargs `{0}` and `k`r   z#This matrix does not have a {} '{}'r}      zIFor a {0} operation 'n<->m' you must provide the kwargs `{0}1` and `{0}2`r~   zPFor a {0} operation 'n->n+km' you must provide the kwargs `{0}`, `k`, and `{0}2`zAFor a {0} operation 'n->n+km' `{0}` and `{0}2` must be different.zinvalid operation %s)
ValueErrorformatrs   rr   
differencelenrepr)	r:   opr{   kcol1col2	error_str	self_colsrs   r;   r;   r<   _normalize_op_args   s\   z#MatrixReductions._normalize_op_argsc                    "    fdd} jj|S )Nc                    s$   | kr| |f  S | |f S r@   r;   rY   rZ   r{   r   r:   r;   r<   entry      zBMatrixReductions._eval_col_op_multiply_col_by_const.<locals>.entry_newrr   rs   )r:   r{   r   r   r;   r   r<   "_eval_col_op_multiply_col_by_const      z3MatrixReductions._eval_col_op_multiply_col_by_constc                    r   )Nc                    s4   | kr
| f S |kr|  f S | |f S r@   r;   r   r   r   r:   r;   r<   r      
   z1MatrixReductions._eval_col_op_swap.<locals>.entryr   )r:   r   r   r   r;   r   r<   _eval_col_op_swap      z"MatrixReductions._eval_col_op_swapc                    $    fdd} jj|S )Nc                    s0   | kr| |f | f   S | |f S r@   r;   r   r{   r   r   r:   r;   r<   r         zFMatrixReductions._eval_col_op_add_multiple_to_other_col.<locals>.entryr   )r:   r{   r   r   r   r;   r   r<   &_eval_col_op_add_multiple_to_other_col      z7MatrixReductions._eval_col_op_add_multiple_to_other_colc                    r   )Nc                    s4   |  kr
|f S | kr |f S | |f S r@   r;   r   row1row2r:   r;   r<   r      r   z1MatrixReductions._eval_row_op_swap.<locals>.entryr   )r:   r   r   r   r;   r   r<   _eval_row_op_swap   r   z"MatrixReductions._eval_row_op_swapc                    r   )Nc                    s$   | kr | |f  S | |f S r@   r;   r   r   rowr:   r;   r<   r      r   zBMatrixReductions._eval_row_op_multiply_row_by_const.<locals>.entryr   )r:   r   r   r   r;   r   r<   "_eval_row_op_multiply_row_by_const   r   z3MatrixReductions._eval_row_op_multiply_row_by_constc                    r   )Nc                    s0   | kr| |f  |f   S | |f S r@   r;   r   r   r   r   r:   r;   r<   r      r   zFMatrixReductions._eval_row_op_add_multiple_to_other_row.<locals>.entryr   )r:   r   r   r   r   r;   r   r<   &_eval_row_op_add_multiple_to_other_row   r   z7MatrixReductions._eval_row_op_add_multiple_to_other_rowr|   Nc                 C   `   |  |||||d\}}}}}|dkr| ||S |dkr#| ||S |dkr.| |||S dS )ad  Performs the elementary column operation `op`.

        `op` may be one of

            * ``"n->kn"`` (column n goes to k*n)
            * ``"n<->m"`` (swap column n and column m)
            * ``"n->n+km"`` (column n goes to column n + k*column m)

        Parameters
        ==========

        op : string; the elementary row operation
        col : the column to apply the column operation
        k : the multiple to apply in the column operation
        col1 : one column of a column swap
        col2 : second column of a column swap or column "m" in the column operation
               "n->n+km"
        r{   r|   r}   r~   N)r   r   r   r   )r:   r   r{   r   r   r   r;   r;   r<   elementary_col_op     z"MatrixReductions.elementary_col_opc                 C   r   )a4  Performs the elementary row operation `op`.

        `op` may be one of

            * ``"n->kn"`` (row n goes to k*n)
            * ``"n<->m"`` (swap row n and row m)
            * ``"n->n+km"`` (row n goes to row n + k*row m)

        Parameters
        ==========

        op : string; the elementary row operation
        row : the row to apply the row operation
        k : the multiple to apply in the row operation
        row1 : one row of a row swap
        row2 : second row of a row swap or row "m" in the row operation
               "n->n+km"
        r   r|   r}   r~   N)r   r   r   r   )r:   r   r   r   r   r   r;   r;   r<   elementary_row_op  r   z"MatrixReductions.elementary_row_op)r{   )r|   NNNN)re   rf   rg   rh   r   rk   propertyrm   ro   rw   rz   r   r   r   r   r   r   r   r   r   r   r   r   r   r;   r;   r;   r<   ri   o   s.    


7		
ri   c                   @   sb   e Zd ZdZdddZdefddZdddZd	d
 Ze	je_e
je_eje_eje_eeZdS )MatrixSubspaceszProvides methods relating to the fundamental subspaces of a matrix.
    Should not be instantiated directly. See ``subspaces.py`` for their
    implementations.Fc                 C   r7   N)rT   )r   r:   rT   r;   r;   r<   columnspaceC  r>   zMatrixSubspaces.columnspacec                 C   rD   )N)rT   r8   )r   )r:   rT   r8   r;   r;   r<   	nullspaceF  rG   zMatrixSubspaces.nullspacec                 C   r7   r   )r   r   r;   r;   r<   rowspaceI  r>   zMatrixSubspaces.rowspacec                 O   s   t | g|R i |S r@   )r    )clsvecskwargsr;   r;   r<   orthogonalizeO  s   zMatrixSubspaces.orthogonalizeNF)re   rf   rg   rh   r   r   r   r   r   r   r   r   r    classmethodr;   r;   r;   r<   r   >  s    

r   c                   @   s  e Zd ZdZd!ddZdefddZd"dd	Zd#d
dZd!ddZ	d!ddZ
edd Zedd Zedd Zedd Ze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_eje_eje_eje_eje_eje_eje_e je	_e!je
_d S )$MatrixEigenzProvides basic matrix eigenvalue/vector operations.
    Should not be instantiated directly. See ``eigen.py`` for their
    implementations.Tc                 K      t | fd|i|S )Nerror_when_incomplete)r!   )r:   r   flagsr;   r;   r<   	eigenvals_     zMatrixEigen.eigenvalsc                 K   s   t | f||d|S )N)r   r8   )r"   )r:   r   r8   r   r;   r;   r<   
eigenvectsb  s
   zMatrixEigen.eigenvectsFc                 K   r   )N
reals_only)r%   )r:   r   r   r;   r;   r<   is_diagonalizablef  r   zMatrixEigen.is_diagonalizablec                 C   rW   )N)r   sort	normalize)r&   )r:   r   r   r   r;   r;   r<   diagonalizei  rl   zMatrixEigen.diagonalizec                 C   r7   N)upper)r#   r:   r   r;   r;   r<   bidiagonalizem  r>   zMatrixEigen.bidiagonalizec                 C   r7   r   )r$   r   r;   r;   r<   bidiagonal_decompositionp  r>   z$MatrixEigen.bidiagonal_decompositionc                 C   r?   r@   )r'   rA   r;   r;   r<   is_positive_definites  rn   r3   c                 C   r?   r@   )r(   rA   r;   r;   r<   is_positive_semidefinitew  rn   r4   c                 C   r?   r@   )r)   rA   r;   r;   r<   is_negative_definite{  rn   r1   c                 C   r?   r@   )r*   rA   r;   r;   r<   is_negative_semidefinite  rn   r2   c                 C   r?   r@   )r+   rA   r;   r;   r<   is_indefinite  rn   r0   c                 K   r   )Ncalc_transform)r,   )r:   r   r   r;   r;   r<   jordan_form  r   zMatrixEigen.jordan_formc                 K   s   t | fi |S r@   )r-   r:   r   r;   r;   r<   left_eigenvects  r\   zMatrixEigen.left_eigenvectsc                 C   r?   r@   )r.   rA   r;   r;   r<   singular_values  rC   zMatrixEigen.singular_valuesNTr   )FFF)"re   rf   rg   rh   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.   r#   r$   r;   r;   r;   r<   r   Z  sF    










r   c                   @   s>   e Zd ZdZddddZdd Zdd	 Zd
d Zdd ZdS )MatrixCalculusz,Provides calculus-related matrix operations.T)evaluatec                O   s<   ddl m} || g|R d|i}t| ts|r| S |S )ae  Calculate the derivative of each element in the matrix.

        Examples
        ========

        >>> from sympy import Matrix
        >>> from sympy.abc import x, y
        >>> M = Matrix([[x, y], [1, 0]])
        >>> M.diff(x)
        Matrix([
        [1, 0],
        [0, 0]])

        See Also
        ========

        integrate
        limit
        r   )ArrayDerivativer   )$sympy.tensor.array.array_derivativesr   
isinstancer   
as_mutable)r:   r   argsr   r   derivr;   r;   r<   diff  s
   zMatrixCalculus.diffc                       |   fddS )Nc                    s
   |   S r@   r   rS   argr;   r<   <lambda>     
 z1MatrixCalculus._eval_derivative.<locals>.<lambda>	applyfunc)r:   r   r;   r   r<   _eval_derivative  s   zMatrixCalculus._eval_derivativec                    s   |   fddS )a  Integrate each element of the matrix.  ``args`` will
        be passed to the ``integrate`` function.

        Examples
        ========

        >>> from sympy import Matrix
        >>> from sympy.abc import x, y
        >>> M = Matrix([[x, y], [1, 0]])
        >>> M.integrate((x, ))
        Matrix([
        [x**2/2, x*y],
        [     x,   0]])
        >>> M.integrate((x, 0, 2))
        Matrix([
        [2, 2*y],
        [2,   0]])

        See Also
        ========

        limit
        diff
        c                    s   | j  i S r@   )	integrater   r   r   r;   r<   r     s    z*MatrixCalculus.integrate.<locals>.<lambda>r   )r:   r   r   r;   r   r<   r     s   zMatrixCalculus.integratec                    s   t  ts
  jd dkrjd }njd dkr$jd }ntd jd dkr5 jd }n jd dkrB jd }ntd|| fddS )a  Calculates the Jacobian matrix (derivative of a vector-valued function).

        Parameters
        ==========

        ``self`` : vector of expressions representing functions f_i(x_1, ..., x_n).
        X : set of x_i's in order, it can be a list or a Matrix

        Both ``self`` and X can be a row or a column matrix in any order
        (i.e., jacobian() should always work).

        Examples
        ========

        >>> from sympy import sin, cos, Matrix
        >>> from sympy.abc import rho, phi
        >>> X = Matrix([rho*cos(phi), rho*sin(phi), rho**2])
        >>> Y = Matrix([rho, phi])
        >>> X.jacobian(Y)
        Matrix([
        [cos(phi), -rho*sin(phi)],
        [sin(phi),  rho*cos(phi)],
        [   2*rho,             0]])
        >>> X = Matrix([rho*cos(phi), rho*sin(phi)])
        >>> X.jacobian(Y)
        Matrix([
        [cos(phi), -rho*sin(phi)],
        [sin(phi),  rho*cos(phi)]])

        See Also
        ========

        hessian
        wronskian
        r   r   z)``self`` must be a row or a column matrixz"X must be a row or a column matrixc                    s   |    | S r@   r   )rZ   rY   Xr:   r;   r<   r     s    z)MatrixCalculus.jacobian.<locals>.<lambda>)r   r/   r   shape	TypeError)r:   r   mnr;   r   r<   jacobian  s   
$
zMatrixCalculus.jacobianc                    r   )a  Calculate the limit of each element in the matrix.
        ``args`` will be passed to the ``limit`` function.

        Examples
        ========

        >>> from sympy import Matrix
        >>> from sympy.abc import x, y
        >>> M = Matrix([[x, y], [1, 0]])
        >>> M.limit(x, 2)
        Matrix([
        [2, y],
        [1, 0]])

        See Also
        ========

        integrate
        diff
        c                    s
   | j   S r@   )limitr   r   r;   r<   r   +  r   z&MatrixCalculus.limit.<locals>.<lambda>r   )r:   r   r;   r   r<   r     s   zMatrixCalculus.limitN)	re   rf   rg   rh   r   r   r   r   r   r;   r;   r;   r<   r     s    9r   c                   @   s   e Zd ZdZedefddZdd Zdd Zd	d
 Z	dd Z
d"ddZdd Zdd Zdd Zd#ddZd"ddZdd Zdd Zdd  Zd!S )$MatrixDeprecatedz+A class to house deprecated matrix methods.rR   c                 C      | j |dS )Nr   )rV   rU   r;   r;   r<   berkowitz_charpoly1  r>   z#MatrixDeprecated.berkowitz_charpolyc                 C      | j ddS )zwComputes determinant using Berkowitz method.

        See Also
        ========

        det
        berkowitz
        rL   rN   r_   rA   r;   r;   r<   berkowitz_det4  s   	zMatrixDeprecated.berkowitz_detc                 K   s   | j di |S )zwComputes eigenvalues of a Matrix using Berkowitz method.

        See Also
        ========

        berkowitz
        Nr;   )r   r   r;   r;   r<   berkowitz_eigenvals?  s   z$MatrixDeprecated.berkowitz_eigenvalsc                 C   s:   | j g }}|  D ]}|||d   | }q
t|S )zpComputes principal minors using Berkowitz method.

        See Also
        ========

        berkowitz
        )onerL   appendtuple)r:   signminorspolyr;   r;   r<   berkowitz_minorsI  s
   z!MatrixDeprecated.berkowitz_minorsc                 C   s  ddl m} d}| s|S | jst | | j}}dg|d  }t|ddD ]}}||d ||d }}||d |f  |d ||f }	}
|d |d |f |||f  }}|
g}td|d D ]}||||   qat|D ]\}}|	| d ||< qq| j|g| }t|D ]}|d || d  ||d |f< q|||d < q%| 	| j|d  gg}t|D ]\}}||||   q|t
tt
| S )Nr   )zeros))r   r   r   r   )r   r   )sympy.matricesr  	is_squarer   rr   ranger   	enumerater   r   r   map)r:   r  berkAN
transformsr   Tr   RCaitemsrY   Bpolysr;   r;   r<   rL   Y  s2   $$"zMatrixDeprecated.berkowitzrL   c                 C   r   rM   )r]   rP   r;   r;   r<   cofactorMatrix  r>   zMatrixDeprecated.cofactorMatrixc                 C   r?   r@   r9   rA   r;   r;   r<   
det_bareis  rC   zMatrixDeprecated.det_bareisc                 C   r   )a  Compute matrix determinant using LU decomposition.


        Note that this method fails if the LU decomposition itself
        fails. In particular, if the matrix has no inverse this method
        will fail.

        TODO: Implement algorithm for sparse matrices (SFF),
        https://www.eecis.udel.edu/~saunders/papers/sffge/it5.ps

        See Also
        ========


        det
        det_bareiss
        berkowitz_det
        lurN   r   rA   r;   r;   r<   det_LU_decomposition  s   z%MatrixDeprecated.det_LU_decompositionc                 C   s   | j ||dS )N)size
eigenvalue)jordan_block)r:   eigenvalr   r;   r;   r<   jordan_cell  rG   zMatrixDeprecated.jordan_cellTc                 C   s   |   \}}|| fS r@   )r   get_diag_blocks)r:   calc_transformationPJr;   r;   r<   jordan_cells  s   zMatrixDeprecated.jordan_cellsc                 C   s   | j |||dS rM   )ra   rX   r;   r;   r<   
minorEntry  r\   zMatrixDeprecated.minorEntryc                 C   s   |  ||S r@   )rc   rb   r;   r;   r<   minorMatrix  r>   zMatrixDeprecated.minorMatrixc                 C      | j |ddS )zEPermute the rows of the matrix with the given permutation in reverse.backward	directionpermute_rowsr:   permr;   r;   r<   permuteBkwd     zMatrixDeprecated.permuteBkwdc                 C   r"  )z:Permute the rows of the matrix with the given permutation.forwardr$  r&  r(  r;   r;   r<   
permuteFwd  r+  zMatrixDeprecated.permuteFwdNrd   r   )re   rf   rg   rh   r   r	   r   r   r   r   rL   r  r  r  r  r  r   r!  r*  r-  r;   r;   r;   r<   r   /  s     

(

r   N)>sympy.core.basicr   sympy.core.symbolr   commonr   
exceptionsr   	utilitiesr   r   r	   determinantr
   r   r   r   r   r   r   r   r   r   r   r   r   r   r   
reductionsr   r   r   r   	subspacesr   r   r   r    eigenr!   r"   r#   r$   r%   r&   r'   r(   r)   r*   r+   r,   r-   r.   
matrixbaser/   __doctest_requires__r6   ri   r   r   r   r   r;   r;   r;   r<   <module>   s*   D@	@ PF 