
    \h                     f   S SK Jr  S SKJr  S SKJrJr  S SKJr  S SK	J
r
  S SKJr  S SKJrJr  S SKJr  S S	KJr  S S
KJr  SSKJr  SSKJrJrJrJrJrJr  \\4S jr\S4S jr S r!S r"S#S jr#S\4S jr$S#S jr%S#S jr&S r'S r(S$S jr)\4S jr*S r+\S4S jr,\S 5       r-S r.S  r/S#S! jr0S" r1g)%    )FunctionType)cacheit)FloatInteger)S)uniquely_named_symbol)Mul)PurePolycancel)nC)DomainMatrix)DDM   )NonSquareMatrixError)_get_intermediate_simp_get_intermediate_simp_bool_iszero_is_zero_after_expand_mul_dotprodsimp	_simplifyc                 2   / n[        U 5      n [        S U  5       5      (       a  [        S U  5       5      (       a  U  Vs/ s H  n[        U5      PM     nn[	        U5      nU" U5      (       a5  US:w  a)  [        U 5       VVs/ s H  u  ptUS:w  d  M  US4PM     nnnSSSU4$ UR                  U5      nXU   SU4$ / n	[        U 5       H+  u  ptU" U5      n
U
S:X  a  XtSU4s  $ U	R                  U
5        M-     [        U	5      (       a  SSSU4$ [        U 5       HE  u  ptX   b  M  U" U5      nU" U5      n
U
S;   a  UR                  X{45        U
S:X  a  X{SU4s  $ XU'   MG     [        U	5      (       a  SSSU4$ [        U 5       HW  u  ptX   b  M  UR                  [        R                  5      (       d  M2  SX'   UR                  U[        R                  45        MY     [        U	5      (       a  SSSU4$ U	R                  S5      nXpU   SU4$ s  snf s  snnf )a  Find the lowest index of an item in ``col`` that is
suitable for a pivot.  If ``col`` consists only of
Floats, the pivot with the largest norm is returned.
Otherwise, the first element where ``iszerofunc`` returns
False is used.  If ``iszerofunc`` does not return false,
items are simplified and retested until a suitable
pivot is found.

Returns a 4-tuple
    (pivot_offset, pivot_val, assumed_nonzero, newly_determined)
where pivot_offset is the index of the pivot, pivot_val is
the (possibly simplified) value of the pivot, assumed_nonzero
is True if an assumption that the pivot was non-zero
was made without being proved, and newly_determined are
elements that were simplified during the process of pivot
finding.c              3   N   #    U  H  n[        U[        [        45      v   M     g 7fN)
isinstancer   r   .0xs     R/var/www/auris/envauris/lib/python3.13/site-packages/sympy/matrices/determinant.py	<genexpr>)_find_reasonable_pivot.<locals>.<genexpr>*   s     
8Cq:a%)**Cs   #%c              3   B   #    U  H  n[        U[        5      v   M     g 7fr   )r   r   r   s     r   r   r    *   s      A/*-QJq%  #s   r   NF)TFT)listallanyabsmax	enumerateindexappendequalsr   Zero)col
iszerofuncsimpfuncnewly_determinedr   col_abs	max_valueir(   possible_zerosis_zerosimpeds               r   _find_reasonable_pivotr6      sV   $ 
s)C 
8C
888S A/*-A/ >/ >/#&'3a3q63'L	i   A~7@~#P~tqaFQF~ #P$'788i(5z5*:;; N#Q- e%!122g&  > dE#344 #(!V$m###QK0eu&677#q  > dE#344 #(88AFF
 !%N##QK0  >dE#344 	T"A1vt-..Y ( $Qs    HHHNc                    / n[        U 5       H1  u  pEU" U5      nUS:X  a  XES/ 4s  $ Ub  M  UR                  XE45        M3     [        U5      S:X  a  SSS/ 4$ Uc  US   S   US   S   S/ 4$ / nU HL  u  pEU" U5      n[        U5      [        U5      :w  d  M'  UR                  XH45        U" U5      S:X  d  MG  XHSU4s  $    US   S   US   S   SU4$ )au  
Helper that computes the pivot value and location from a
sequence of contiguous matrix column elements. As a side effect
of the pivot search, this function may simplify some of the elements
of the input column. A list of these simplified entries and their
indices are also returned.
This function mimics the behavior of _find_reasonable_pivot(),
but does less work trying to determine if an indeterminate candidate
pivot simplifies to zero. This more naive approach can be much faster,
with the trade-off that it may erroneously return a pivot that is zero.

``col`` is a sequence of contiguous column entries to be searched for
a suitable pivot.
``iszerofunc`` is a callable that returns a Boolean that indicates
if its input is zero, or None if no such determination can be made.
``simpfunc`` is a callable that simplifies its input. It must return
its input if it does not simplify its input. Passing in
``simpfunc=None`` indicates that the pivot search should not attempt
to simplify any candidate pivots.

Returns a 4-tuple:
(pivot_offset, pivot_val, assumed_nonzero, newly_determined)
``pivot_offset`` is the sequence index of the pivot.
``pivot_val`` is the value of the pivot.
pivot_val and col[pivot_index] are equivalent, but will be different
when col[pivot_index] was simplified during the pivot search.
``assumed_nonzero`` is a boolean indicating if the pivot cannot be
guaranteed to be zero. If assumed_nonzero is true, then the pivot
may or may not be non-zero. If assumed_nonzero is false, then
the pivot is non-zero.
``newly_determined`` is a list of index-value pairs of pivot candidates
that were simplified during the pivot search.
FNr   r   T)r'   r)   lenid)	r,   r-   r.   indeterminatesr2   col_valcol_val_is_zeror/   tmp_col_vals	            r   _find_reasonable_pivot_naiver>   {   s   V Nn
$W-e#ub(($ !!1,/ % >a T5"$$
 a #^A%6q%94CC $
w'g;"[/)##Q$45+&%/u.>>> % !Q!21!5t=MMM    c                 (  ^ ^	 T R                   S:X  a.  T R                  S:X  a  T R                  SST R                  /5      $ T S   T SSS24   p!T SS2S4   T SS2SS24   pCU/m	[	        T R                   S-
  5       H%  nT	R                  UR                  T	U   SS95        M'     T	 Vs/ s H  ob* R                  USS9S   PM     snm	T R                  U* /T	-   m	U U	4S jnT R                  T R                  S-   T R                   U5      nXH4$ s  snf )zReturn (A,T) where T the Toeplitz matrix used in the Berkowitz algorithm
corresponding to ``M`` and A is the first principal submatrix.
r   r   r   r   N   dotprodsimpc                 4   > X:  a  TR                   $ TX-
     $ r   )zero)r2   jMdiagss     r   entry)_berkowitz_toeplitz_matrix.<locals>.entry   s    566MQU|r?   )rowscols_newoneranger)   multiply)
rH   aRCAr2   drJ   toeplitzrI   s
   `        @r   _berkowitz_toeplitz_matrixrX      s    	vv{qvv{vvaAEE7## S6Qq!"uXqQRU8Qqr!"uXq  CE166A:QZZadZ;< ?DEu!b]]1$]/5uEEUUQBK%E
 vvaffqj!&&%0H= Fs   )Dc                 P   U R                   S:X  a.  U R                  S:X  a  U R                  SSU R                  /5      $ U R                   S:X  a3  U R                  S:X  a#  U R                  SSU R                  U S   * /5      $ [	        U 5      u  pUR                  [        U5      SS9$ )a  Run the Berkowitz algorithm and return a vector whose entries
are the coefficients of the characteristic polynomial of ``M``.

Given N x N matrix, efficiently compute
coefficients of characteristic polynomials of ``M``
without division in the ground domain.

This method is particularly useful for computing determinant,
principal minors and characteristic polynomial when ``M``
has complicated coefficients e.g. polynomials. Semi-direct
usage of this algorithm is also important in computing
efficiently sub-resultant PRS.

Assuming that M is a square matrix of dimension N x N and
I is N x N identity matrix, then the Berkowitz vector is
an N x 1 vector whose entries are coefficients of the
polynomial

                charpoly(M) = det(t*I - M)

As a consequence, all polynomials generated by Berkowitz
algorithm are monic.

For more information on the implemented algorithm refer to:

[1] S.J. Berkowitz, On computing the determinant in small
    parallel time using a small number of processors, ACM,
    Information Processing Letters 18, 1984, pp. 147-150

[2] M. Keber, Division-Free computation of sub-resultants
    using Bezout matrices, Tech. Report MPI-I-2006-1-006,
    Saarbrucken, 2006
r   r   rB   rA   NrC   )rL   rM   rN   rO   rX   rQ   _berkowitz_vector)rH   submatrW   s      r   rZ   rZ      s    H 	vv{qvv{vvaQUUG$$	
11vvaQUUQsVG,--1!4F.v6DIIr?   c                 <    U R                  US9R                  5       $ )a  Returns the adjugate, or classical adjoint, of
a matrix.  That is, the transpose of the matrix of cofactors.

https://en.wikipedia.org/wiki/Adjugate

Parameters
==========

method : string, optional
    Method to use to find the cofactors, can be "bareiss", "berkowitz",
    "bird", "laplace" or "lu".

Examples
========

>>> from sympy import Matrix
>>> M = Matrix([[1, 2], [3, 4]])
>>> M.adjugate()
Matrix([
[ 4, -2],
[-3,  1]])

See Also
========

cofactor_matrix
sympy.matrices.matrixbase.MatrixBase.transpose
method)cofactor_matrix	transposerH   r^   s     r   	_adjugaterb   *  s!    < F+5577r?   lambdac                    U R                   (       d
  [        5       eU R                  5       nUR                  nUR	                  5       n[        X/S S9nUR                  (       d	  U[        LaD  U Vs/ s H  odR                  U5      PM     nnU Vs/ s H
  o" U5      PM     nn[        Xq5      n	U	$ [        XQUS9n	U	$ s  snf s  snf )a  Computes characteristic polynomial det(x*I - M) where I is
the identity matrix.

A PurePoly is returned, so using different variables for ``x`` does
not affect the comparison or the polynomials:

Parameters
==========

x : string, optional
    Name for the "lambda" variable, defaults to "lambda".

simplify : function, optional
    Simplification function to use on the characteristic polynomial
    calculated. Defaults to ``simplify``.

Examples
========

>>> from sympy import Matrix
>>> from sympy.abc import x, y
>>> M = Matrix([[1, 3], [2, 0]])
>>> M.charpoly()
PurePoly(lambda**2 - lambda - 6, lambda, domain='ZZ')
>>> M.charpoly(x) == M.charpoly(y)
True
>>> M.charpoly(x) == M.charpoly(y)
True

Specifying ``x`` is optional; a symbol named ``lambda`` is used by
default (which looks good when pretty-printed in unicode):

>>> M.charpoly().as_expr()
lambda**2 - lambda - 6

And if ``x`` clashes with an existing symbol, underscores will
be prepended to the name to make it unique:

>>> M = Matrix([[1, 2], [x, 0]])
>>> M.charpoly(x).as_expr()
_x**2 - _x - 2*x

Whether you pass a symbol or not, the generator can be obtained
with the gen attribute since it may not be the same as the symbol
that was passed:

>>> M.charpoly(x).gen
_x
>>> M.charpoly(x).gen == x
False

Notes
=====

The Samuelson-Berkowitz algorithm is used to compute
the characteristic polynomial efficiently and without any
division operations.  Thus the characteristic polynomial over any
commutative ring without zero divisors can be computed.

If the determinant det(x*I - M) can be found out easily as
in the case of an upper or a lower triangular matrix, then
instead of Samuelson-Berkowitz algorithm, eigenvalues are computed
and the characteristic polynomial with their help.

See Also
========

det
c                     SU -   $ )N_ )ss    r   <lambda>_charpoly.<locals>.<lambda>  s    sQwr?   )modify)domain)
	is_squarer   to_DMrl   charpolyr   is_EXRAWr   to_sympyr
   )
rH   r   simplifydMKcpcberk_vectorrR   ps
             r   	_charpolyry   L  s    N ;;"$$ 
B
		A	Ba->?AzzXY. /11bzz!}b1,78Kqx{K8[$ H R1%H 28s   2B>Cc                     U R                   (       a  U R                  S:  a
  [        5       e[        R                  X-   S-  -  U R                  XU5      -  $ )ac  Calculate the cofactor of an element.

Parameters
==========

method : string, optional
    Method to use to find the cofactors, can be "bareiss", "berkowitz",
    "bird", "laplace" or "lu".

Examples
========

>>> from sympy import Matrix
>>> M = Matrix([[1, 2], [3, 4]])
>>> M.cofactor(0, 1)
-3

See Also
========

cofactor_matrix
minor
minor_submatrix
r   rB   )rm   rL   r   r   NegativeOneminorrH   r2   rG   r^   s       r   	_cofactorr~     sC    4 ;;!&&1*"$$==AEQ;'!''!*???r?   c                    ^ ^ T R                   (       d
  [        5       eT R                  T R                  T R                  U U4S j5      $ )a  Return a matrix containing the cofactor of each element.

Parameters
==========

method : string, optional
    Method to use to find the cofactors, can be "bareiss", "berkowitz",
    "bird", "laplace" or "lu".

Examples
========

>>> from sympy import Matrix
>>> M = Matrix([[1, 2], [3, 4]])
>>> M.cofactor_matrix()
Matrix([
[ 4, -3],
[-2,  1]])

See Also
========

cofactor
minor
minor_submatrix
c                 (   > TR                  XT5      $ r   )cofactor)r2   rG   rH   r^   s     r   ri   "_cofactor_matrix.<locals>.<lambda>  s    A&1r?   )rm   r   rN   rL   rM   ra   s   ``r   _cofactor_matrixr     s5    8 ;;"$$66!&&!&&13 3r?   c                 $  ^ ^
 SSK nT R                  u  p#X#:  a  T R                  m X2p2[        [	        U5      5      n/ n[	        SUS-   5       H/  m
U[        [        [        UR                  UT
5      5      5      -  nM1     SnU Hd  nSn[        U5      n	[	        U5       H  m
U[        U U
4S jU 5       5      -  nM     Xh[        R                  U	-  -  [        X9-
  X)-
  5      -  -  nMf     U[        R                  U-  -  nUR                  5       $ )av  Returns the permanent of a matrix. Unlike determinant,
permanent is defined for both square and non-square matrices.

For an m x n matrix, with m less than or equal to n,
it is given as the sum over the permutations s of size
less than or equal to m on [1, 2, . . . n] of the product
from i = 1 to m of M[i, s[i]]. Taking the transpose will
not affect the value of the permanent.

In the case of a square matrix, this is the same as the permutation
definition of the determinant, but it does not take the sign of the
permutation into account. Computing the permanent with this definition
is quite inefficient, so here the Ryser formula is used.

Examples
========

>>> from sympy import Matrix
>>> M = Matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
>>> M.per()
450
>>> M = Matrix([1, 5, 7])
>>> M.per()
13

References
==========

.. [1] Prof. Frank Ben's notes: https://math.berkeley.edu/~bernd/ban275.pdf
.. [2] Wikipedia article on Permanent: https://en.wikipedia.org/wiki/Permanent_%28mathematics%29
.. [3] https://reference.wolfram.com/language/ref/Permanent.html
.. [4] Permanent of a rectangular matrix : https://arxiv.org/pdf/0904.3251.pdf
r   Nr   c              3   2   >#    U  H  nTTU4   v   M     g 7fr   rg   )r   rG   rH   r2   s     r   r   _per.<locals>.<genexpr>.  s     0A!Q$s   )	itertoolsshapeTr"   rP   mapcombinationsr8   sumr   r{   r   rr   )rH   r   mnrh   subsetspermsubsetprodsub_lenr2   s   `         @r   _perr     s    D 77DAuCC1U1XAG1a!e_4D)"8"8A">?@@  Df+qAC0000D q}}g--1;0LLL  	AMM1D==?r?   c                     [         R                  " U SSS9nUR                  nUR                  UR	                  5       5      $ )NT)field	extension)r   from_Matrixrl   rq   det)rH   DOMrt   s      r   _det_DOMr   3  s4    

"
"1DD
AC

A::cggi  r?   c                 d   UR                  5       nUS:X  a  SnOUS:X  a  SnUS;  a  [        SU-  5      eUc  US:X  a  [        nO0US:X  a  [        nO#[	        U[
        5      (       d  [        SU-  5      eU R                  nX0R                  :X  a  US:X  a  U R                  $ US	:X  a  U S
   $ US:X  a,  U S
   U S   -  U S   U S   -  -
  n[        [        5      " U5      $ US:X  a  U S
   U S   -  U S   -  U S   U S   -  U S   -  -   U S   U S   -  U S   -  -   U S   U S   -  U S   -  -
  U S
   U S   -  U S   -  -
  U S   U S   -  U S   -  -
  n[        [        5      " U5      $ / nU R                  5        H  nUS:X  a  [        XU4   5      nOUS:X  a  XU4   R                  US9nOjUS:X  a  XU4   R                  5       nOOUS:X  a  XU4   R                  US9nO5US:X  a  XU4   R!                  5       nOUS:X  a  XU4   R#                  5       nUR%                  W5        M     ['        U6 $ )a	  Computes the determinant of a matrix if ``M`` is a concrete matrix object
otherwise return an expressions ``Determinant(M)`` if ``M`` is a
``MatrixSymbol`` or other expression.

Parameters
==========

method : string, optional
    Specifies the algorithm used for computing the matrix determinant.

    If the matrix is at most 3x3, a hard-coded formula is used and the
    specified method is ignored. Otherwise, it defaults to
    ``'bareiss'``.

    Also, if the matrix is an upper or a lower triangular matrix, determinant
    is computed by simple multiplication of diagonal elements, and the
    specified method is ignored.

    If it is set to ``'domain-ge'``, then Gaussian elimination method will
    be used via using DomainMatrix.

    If it is set to ``'bareiss'``, Bareiss' fraction-free algorithm will
    be used.

    If it is set to ``'berkowitz'``, Berkowitz' algorithm will be used.

    If it is set to ``'bird'``, Bird's algorithm will be used [1]_.

    If it is set to ``'laplace'``, Laplace's algorithm will be used.

    Otherwise, if it is set to ``'lu'``, LU decomposition will be used.

    .. note::
        For backward compatibility, legacy keys like "bareis" and
        "det_lu" can still be used to indicate the corresponding
        methods.
        And the keys are also case-insensitive for now. However, it is
        suggested to use the precise keys for specifying the method.

iszerofunc : FunctionType or None, optional
    If it is set to ``None``, it will be defaulted to ``_iszero`` if the
    method is set to ``'bareiss'``, and ``_is_zero_after_expand_mul`` if
    the method is set to ``'lu'``.

    It can also accept any user-specified zero testing function, if it
    is formatted as a function which accepts a single symbolic argument
    and returns ``True`` if it is tested as zero and ``False`` if it
    tested as non-zero, and also ``None`` if it is undecidable.

Returns
=======

det : Basic
    Result of determinant.

Raises
======

ValueError
    If unrecognized keys are given for ``method`` or ``iszerofunc``.

NonSquareMatrixError
    If attempted to calculate determinant from a non-square matrix.

Examples
========

>>> from sympy import Matrix, eye, det
>>> I3 = eye(3)
>>> det(I3)
1
>>> M = Matrix([[1, 2], [3, 4]])
>>> det(M)
-2
>>> det(M) == M.det()
True
>>> M.det(method="domain-ge")
-2

References
==========

.. [1] Bird, R. S. (2011). A simple division-free algorithm for computing
       determinants. Inf. Process. Lett., 111(21), 1072-1074. doi:
       10.1016/j.ipl.2011.08.006
bareisbareissdet_lulu)r   	berkowitzr   	domain-gebirdlaplacez$Determinant method '%s' unrecognizedz%Zero testing method '%s' unrecognizedr   r   rA   rB   r   r   r   r   r   r      )rB   rB   )r   rB   )rB   r   )r   rB   )rB   r   r   r-   r   r   r   )lower
ValueErrorr   r   r   r   rL   rM   rO   r   r   strongly_connected_componentsr   _eval_det_bareiss_eval_det_berkowitz_eval_det_lu_eval_det_bird_eval_det_laplacer)   r	   )rH   r^   r-   r   r   detsbr   s           r   _detr   9  s   r \\^F	8	 ! !?&HIIY2Jt^ J
L11@:MNN	AFF{655L!VT7N!V$!D'!AdGag$55A),7::!VD'AdG#ag-D'AdG#ag-.D'AdG#ag-. D'AdG#ag-. D'AdG#ag-	.
 D'AdG#ag-.A *,7::D,,.[ 1T7#Cy qD'++z+BC{"qD'--/Ct^qD'&&*&=CvqD'((*Cy qD'++-CC / :r?   c                    ^ ^^ SU UU4S jjmT R                   (       d
  [        5       eT R                  S:X  a  T R                  $ T" T 5      $ )aa  Compute matrix determinant using Bareiss' fraction-free
algorithm which is an extension of the well known Gaussian
elimination method. This approach is best suited for dense
symbolic matrices and will result in a determinant with
minimal number of fractions. It means that less term
rewriting is needed on resulting formulae.

Parameters
==========

iszerofunc : function, optional
    The function to use to determine zeros when doing an LU decomposition.
    Defaults to ``lambda x: x.is_zero``.

TODO: Implement algorithm for sparse matrices (SFF),
http://www.eecis.udel.edu/~saunders/papers/sffge/it5.ps.
c                   >^ ^^^	^
 T R                   S:X  a  T R                  $ T R                   S:X  a  T S   $ [        T S S 2S4   TS9u  mm	  nTc  T R                  $ STS-  -  n[	        T R                   5       Vs/ s H  oDT:w  d  M
  UPM     nn[        [	        T R                  5      5      nT R                  XV5      m
UU UU	U
4S jnUT" TR                  T R                   S-
  T R                  S-
  U5      T	5      -  $ s  snf )Nr   r   rA   r   rB   c                    > TTXS-   4   -  TTUS-   4   TU S4   -  -
  T-  n[        S5      (       a  [        U5      $ UR                  (       d  [        U5      $ U$ )Nr   r   T)r   r   is_Atomr   )r2   rG   retcummmat	pivot_pos	pivot_valtmp_mats      r   rJ   ,_det_bareiss.<locals>.bareiss.<locals>.entry  sk    WQAX..YA5E1FwqRSt}1TTX\\C*400#C(([[c{"Jr?   )	rL   rO   r6   rF   rP   r"   rM   extractrN   )r   r   rf   signr2   rL   rM   rJ   r   r   r   rH   r   r-   s   ``      @@@r   r   _det_bareiss.<locals>.bareiss  s    88q=77NXX]t9 &<C1IR\%]"	9a88O 	A& !?=?a9n?=E#((O$++d)	 	 GAFF388a<AuEyQQQ >s   <	D	Dr   )r   )rm   r   rL   rO   )rH   r-   r   s   ``@r   _det_bareissr     sB    *R RB ;;"$$vv{uu
 1:r?   c                     U R                   (       d
  [        5       eU R                  S:X  a  U R                  $ [	        U 5      nS[        U5      S-
  -  US   -  $ )z7Use the Berkowitz algorithm to compute the determinant.r   r   r   )rm   r   rL   rO   rZ   r8   )rH   rw   s     r   _det_berkowitzr     sR     ;;"$$vv{uu
 $A&K#k"Q&'+b/99r?   c                    U R                   (       d
  [        5       eU R                  S:X  a  U R                  $ U R	                  UUS9u  p4U" X3R                  S-
  UR                  S-
  4   5      (       a  U R
                  $ [        U5      S-  (       a  U R                  * OU R                  n[        UR                  5       H  nXSXf4   -  nM     U$ )a  Computes the determinant of a matrix from its LU decomposition.
This function uses the LU decomposition computed by
LUDecomposition_Simple().

The keyword arguments iszerofunc and simpfunc are passed to
LUDecomposition_Simple().
iszerofunc is a callable that returns a boolean indicating if its
input is zero, or None if it cannot make the determination.
simpfunc is a callable that simplifies its input.
The default is simpfunc=None, which indicate that the pivot search
algorithm should not attempt to simplify any candidate pivots.
If simpfunc fails to simplify its input, then it must return its input
instead of a copy.

Parameters
==========

iszerofunc : function, optional
    The function to use to determine zeros when doing an LU decomposition.
    Defaults to ``lambda x: x.is_zero``.

simpfunc : function, optional
    The simplification function to use when looking for zeros for pivots.
r   )r-   r.   r   rB   )rm   r   rL   rO   LUdecomposition_SimplerF   r8   rP   )rH   r-   r.   r   	row_swapsr   ks          r   _det_LUr     s    4 ;;"$$vv{uu
 ,,
 - MB "WWQY	)*++vv 	N1$155&!%%C
 277^!$x  Jr?   c                    ^  T R                   S   nUS:X  a  T S   $ US:X  a  T S   T S   -  T S   T S   -  -
  $ [        U 4S j[        U5       5       5      $ )	a  Compute the determinant of a matrix using Laplace expansion.

This is a recursive function, and it should not be called directly.
Use _det_laplace() instead. The reason for splitting this function
into two is to allow caching of determinants of submatrices. While
one could also define this function inside _det_laplace(), that
would remove the advantage of using caching in Cramer Solve.
r   r   rB   rA   r   r   r   c              3   v   >#    U  H.  nS U-  TSU4   -  [        TR                  SU5      5      -  v   M0     g7f)r   r   N)__det_laplaceminor_submatrix)r   r2   rH   s     r   r    __det_laplace.<locals>.<genexpr>n  sD      MCKa 19qAw& !2!21a!89:CKs   69)r   r   rP   )rH   r   s   ` r   r   r   ^  sp     	

AAvt	
aw4 1T7QtW#444 MCH8M M 	Mr?   c                     U R                   (       d
  [        5       eU R                  S   S:X  a  U R                  $ [	        U R                  5       5      $ )a<  Compute the determinant of a matrix using Laplace expansion.

While Laplace expansion is not the most efficient method of computing
a determinant, it is a simple one, and it has the advantage of
being division free. To improve efficiency, this function uses
caching to avoid recomputing determinants of submatrices.
r   )rm   r   r   rO   r   as_immutable)rH   s    r   _det_laplacer   r  s@     ;;"$$wwqzQuu )**r?   c                 8   S nU R                   R                  5       nU R                  S   nUS:X  a  U R                  $ Un[	        US-
  5       H  nU" U5      R                  U5      nM     US   S   nUS-  S:X  a  U* nUR                  R                  U5      $ )a  Compute the determinant of a matrix using Bird's algorithm.

Bird's algorithm is a simple division-free algorithm for computing, which
is of lower order than the Laplace's algorithm. It is described in [1]_.

References
==========

.. [1] Bird, R. S. (2011). A simple division-free algorithm for computing
       determinants. Inf. Process. Lett., 111(21), 1072-1074. doi:
       10.1016/j.ipl.2011.08.006
c                    U R                   S   nU R                  R                  nUnU/n[        [	        SU5      5       H  nX0U   U   -  nUR                  U5        M      US S S2   n[        U 5       VVs/ s H  u  pVU/U-  XE   /-   XeS-   S  -   PM     nnn[        XpR                   U R                  5      $ s  snnf )Nr   r   r   )r   rl   rF   reversedrP   r)   r'   r   )Xr   rF   total	diag_sumsr2   X_ielemss           r   mu_det_bird.<locals>.mu  s    GGAJxx}}F	%1+&AqT!WEU# ' ddO	 1 @Fq$!y|n,sq56{: 	 5''188,,s   8 B;r   r   rB   )_repto_ddmr   rO   rP   matmulrl   rq   )rH   r   Mddmr   Fn1rf   detAs          r   	_det_birdr     s    - 66==?D	
AAvuu C1q5\gnnT" q6!9D1uzu;;%%r?   c                 t    U R                   (       d
  [        5       eU R                  X5      R                  US9$ )a-  Return the (i,j) minor of ``M``.  That is,
return the determinant of the matrix obtained by deleting
the `i`th row and `j`th column from ``M``.

Parameters
==========

i, j : int
    The row and column to exclude to obtain the submatrix.

method : string, optional
    Method to use to find the determinant of the submatrix, can be
    "bareiss", "berkowitz", "bird", "laplace" or "lu".

Examples
========

>>> from sympy import Matrix
>>> M = Matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
>>> M.minor(1, 1)
-12

See Also
========

minor_submatrix
cofactor
det
r]   )rm   r   r   r   r}   s       r   _minorr     s3    > ;;"$$Q"&&f&55r?   c                    US:  a  XR                   -  nUS:  a  X R                  -  nSUs=::  a  U R                   :  a  O  OSUs=::  a  U R                  :  d*  O  [        SU R                   -  SU R                  -  -   5      e[        U R                   5       Vs/ s H  o3U:w  d  M
  UPM     nn[        U R                  5       Vs/ s H  o3U:w  d  M
  UPM     nnU R	                  XE5      $ s  snf s  snf )a  Return the submatrix obtained by removing the `i`th row
and `j`th column from ``M`` (works with Pythonic negative indices).

Parameters
==========

i, j : int
    The row and column to exclude to obtain the submatrix.

Examples
========

>>> from sympy import Matrix
>>> M = Matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
>>> M.minor_submatrix(1, 1)
Matrix([
[1, 3],
[7, 9]])

See Also
========

minor
cofactor
r   z1`i` and `j` must satisfy 0 <= i < ``M.rows`` (%d)zand 0 <= j < ``M.cols`` (%d).)rL   rM   r   rP   r   )rH   r2   rG   rR   rL   rM   s         r   _minor_submatrixr     s    6 	1u	VV1u	VV?AFF?!q/166/ #%&VV,.MPQPVPV.VW X 	X QVV}/}!QA}D/QVV}/}!QA}D/99T   0/s   	C-(C-	C2C2)r   )r   N)2typesr   sympy.core.cacher   sympy.core.numbersr   r   sympy.core.singletonr   sympy.core.symbolr   sympy.core.mulr	   sympy.polysr
   r   %sympy.functions.combinatorial.numbersr   !sympy.polys.matrices.domainmatrixr   sympy.polys.matrices.ddmr   
exceptionsr   	utilitiesr   r   r   r   r   r   r6   r>   rX   rZ   rb   ry   r~   r   r   r   r   r   r   r   r   r   r   r   r   rg   r?   r   <module>r      s     $ - " 3  ( 4 : ( ,A A
 ,3Y e/P 294 NNd+^+J\8D i jZ@@ 3D6p!Of  9 ?D:" "D <~ 	M 	M&+$*&Z"6J'!r?   