
    \h                     b   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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JrJrJr  S SKJr  S SKJ r J!r!  S SK"J#r#  SSK$J%r%J&r&  SSK'J(r(  SSK)J*r*J+r+  SS/0r,S r-S:S jr.S r/ S;SSSS.S jjr0Sr1 S<S jr2 S<S jr3\*S4S jr4S  r5S;S! jr6S\*4SS".S# jjr7S:S$ jr8S:S% jr9S& r:S' r;S( r<S;S) jr=S;S* jr>S=S+ jr?S, r@S- rAS. rBS/ rCS0 rDS1 rES2 rFS3 rGS4 rHS5rI\I\BlJ        \I\ClJ        \I\DlJ        \I\ElJ        \I\FlJ        S;SS".S6 jjrKS7 rLS8 rMg9)>    )FunctionType)Counter)mpworkprec)prec_to_dpsdefault_sort_key)DEFAULT_MAXPRECPrecisionExhausted)	fuzzy_andfuzzy_or)Float)_sympify)sqrt)rootsCRootOfZZQQEX)DomainMatrix)dom_eigenvectsdom_eigenvects_to_sympy)gcd   )MatrixErrorNonSquareMatrixError)_find_reasonable_pivot)_iszero	_simplify)_is_indefinite_is_negative_definite_is_negative_semidefinite_is_positive_definite_is_positive_semidefinite
matplotlibc                 x   S nS n[        S U R                  [        5       5       5      nSU* -  nU[        :  a  [	        U5         [
        R                  " U R                  [        U5      S95      n[
        R                  " U5      u  pgU" U VV	s/ s H8  n[
        R                  " U5      [
        R                  " U5      4  H  oPM     M:     sn	n5      n
Ub(  [
        R                  " X*-
  5      U:  a  Xg4sS S S 5        $ U
nS S S 5        US-  nU[        :  a  M  [        es  sn	nf ! , (       d  f       N+= f)Nc                 N    [         R                  " [        S U  5       5      5      $ )Nc              3   *   #    U  H	  oS -  v   M     g7f)   N ).0is     L/var/www/auris/envauris/lib/python3.13/site-packages/sympy/matrices/eigen.py	<genexpr>A_eigenvals_eigenvects_mpmath.<locals>.<lambda>.<locals>.<genexpr>"   s     !21Q$   )r   r   sum)vs    r-   <lambda>._eigenvals_eigenvects_mpmath.<locals>.<lambda>"   s    bggc!2!223    c              3   8   #    U  H  oR                   v   M     g 7fN)_precr+   xs     r-   r.   /_eigenvals_eigenvects_mpmath.<locals>.<genexpr>%   s     /1ww   r)   )n)maxatomsr   r
   r   r   matrixevalfr   eigreimfabsr   )Mnorm2v1precepsAEERer,   v2s              r-   _eigenvals_eigenvects_mpmathrP   !   s    3E	B///D
dU(C

 d^		!''K$5'67AFF1IEA1C1aruuQxq.B.B1CDB~"''"'"2S"8u ^ B  		 
    D ^s%   AD+?D%)D+D+%D++
D9Fc                     [        U 5      u  p#U Vs/ s H  n[        U5      PM     nnU(       a  U$ [        [        U5      5      $ s  snf )z Compute eigenvalues using mpmath)rP   r   dictr   )rF   multiplerL   _r:   results         r-   _eigenvals_mpmathrV   ;   sB    '*DA#$%1ahqk1F%   &s   Ac                     [        U 5      u  p/ n[        U R                  5       H7  n[        X   5      n[        US S 2U4   5      nUR	                  USU/45        M9     U$ )Nr   )rP   rangerowsr   append)rF   rL   rM   rU   r,   eigenval	eigenvects          r-   _eigenvects_mpmathr]   D   s^    (+EAF166]AD>R1X&	xYK01 
 Mr5   T)simplifyrS   rationalc                  ^ U (       d  U(       a  / $ 0 $ U R                   (       d  [        SR                  U 5      5      eU R                  R                  [
        [        4;  a:  [        S U  5       5      (       a#  U R                  [        5      (       a	  [        XS9$ U(       a  SSKJm  U R                  U4S j5      n U(       a  [        U 4XS.UD6$ [        U 4XS.UD6$ )a  Compute eigenvalues of the matrix.

Parameters
==========

error_when_incomplete : bool, optional
    If it is set to ``True``, it will raise an error if not all
    eigenvalues are computed. This is caused by ``roots`` not returning
    a full list of eigenvalues.

simplify : bool or function, optional
    If it is set to ``True``, it attempts to return the most
    simplified form of expressions returned by applying default
    simplification method in every routine.

    If it is set to ``False``, it will skip simplification in this
    particular routine to save computation resources.

    If a function is passed to, it will attempt to apply
    the particular function as simplification method.

rational : bool, optional
    If it is set to ``True``, every floating point numbers would be
    replaced with rationals before computation. It can solve some
    issues of ``roots`` routine not working well with floats.

multiple : bool, optional
    If it is set to ``True``, the result will be in the form of a
    list.

    If it is set to ``False``, the result will be in the form of a
    dictionary.

Returns
=======

eigs : list or dict
    Eigenvalues of a matrix. The return format would be specified by
    the key ``multiple``.

Raises
======

MatrixError
    If not enough roots had got computed.

NonSquareMatrixError
    If attempted to compute eigenvalues from a non-square matrix.

Examples
========

>>> from sympy import Matrix
>>> M = Matrix(3, 3, [0, 1, 1, 1, 0, 0, 1, 1, 1])
>>> M.eigenvals()
{-1: 1, 0: 1, 2: 1}

See Also
========

MatrixBase.charpoly
eigenvects

Notes
=====

Eigenvalues of a matrix $A$ can be computed by solving a matrix
equation $\det(A - \lambda I) = 0$

It's not always possible to return radical solutions for
eigenvalues for matrices larger than $4, 4$ shape due to
Abel-Ruffini theorem.

If there is no radical solution is found for the eigenvalue,
it may return eigenvalues in the form of
:class:`sympy.polys.rootoftools.ComplexRootOf`.
z{} must be a square matrix.c              3   8   #    U  H  oR                   v   M     g 7fr7   	is_numberr9   s     r-   r.   _eigenvals.<locals>.<genexpr>        &Aq{{Ar<   rS   r   	nsimplifyc                 J   > U R                  [        5      (       a  T" U SS9$ U $ NT)r_   )hasr   r:   rh   s    r-   r3   _eigenvals.<locals>.<lambda>   s     QUU5\\iD1HqHr5   )error_when_incompleter^   )	is_squarer   format_repdomainr   r   allrk   r   rV   sympy.simplifyrh   	applyfunc_eigenvals_list_eigenvals_dict)rF   rn   r^   rS   r_   flagsrh   s         @r-   
_eigenvalsry   P   s    ` I	;;"#@#G#G#JKKvv}}RH$&A&&&155<<$Q::,KKHJ %: 	 	!6
 r5   ad  It is not always possible to express the eigenvalues of a matrix of size 5x5 or higher in radicals. We have CRootOf, but domains other than the rationals are not currently supported. If there are no symbols in the matrix, it should still be possible to compute numeric approximations of the eigenvalues using M.evalf().eigenvals() or M.charpoly().nroots().c                    U R                  5       n/ nU R                  R                  [        [        4;   nU H  nU(       a-  [        U5      S:X  a  US   nXU4   n	UR                  U	5        M7  XU4   n
[        U[        5      (       a  U
R                  US9nOU
R                  5       n[        U4SS0UD6n[        U5      U
R                  :w  a   UR                  SS9nX\-  nM     U(       d  U$ [        U[        5      (       d  [         nU Vs/ s H
  o" U5      PM     sn$ ! [         a    U(       a  [        [        5      e/ n Nhf = fs  snf )Nr   r   r^   rS   Trf   )strongly_connected_componentsrq   rr   r   r   lenrZ   
isinstancer   charpolyr   rY   	all_rootsNotImplementedErrorr   eigenvals_error_messager   )rF   rn   r^   rx   iblocksall_eigsis_dombindexvalblockr   eigsvalues                 r-   rv   rv      s?   --/GHVV]]r2h&F c!fkaDE5L/COOC Q$h--~~x~8H~~'HX666t9

"))4)8 	7 : h--)12HUO22 ' (%&=>>D	 3s   D%E%"E
	E
c                 b   U R                  5       n0 nU R                  R                  [        [        4;   nU H  nU(       a3  [        U5      S:X  a$  US   nXU4   n	UR                  U	S5      S-   XY'   M=  XU4   n
[        U[        5      (       a  U
R                  US9nOU
R                  5       n[        U4SS0UD6n[        UR                  5       5      U
R                  :w  a   [        UR                  SS95      nUR'                  5        H  u  pX;   a  X]==   U-  ss'   M  XU'   M     M     U(       d  U$ [        U[        5      (       d  [(        nUR'                  5        VVs0 s H  u  nnU" U5      U_M     snn$ ! [          a    U(       a  [#        [$        5      e0 n Nf = fs  snnf )Nr   r   r{   rS   Frf   )r|   rq   rr   r   r   r}   getr~   r   r   r   r1   valuesrY   rR   r   r   r   r   itemsr   )rF   rn   r^   rx   r   r   r   r   r   r   r   r   r   kr2   keyr   s                    r-   rw   rw      s   --/GHVV]]r2h&F c!fkaDE5L/C$LLa014HMQ$h--~~x~8H~~'HX777t{{}+H...>? JJLDA}q 	 !7 B h--3;>>3CD3CZS%HSM5 3CDD! ' (%&=>>D	  Es   &F)F+"F('F(c                    X R                  U R                  5      U-  -
  nUR                  US9n[        U5      S:X  a  U(       a  UR                  USS9n[        U5      S:X  a  [	        SR                  U5      5      eU$ )z:Get a basis for the eigenspace for a particular eigenvalue)
iszerofuncr   Tr   r^   z,Can't evaluate eigenvector for eigenvalue {})eyerY   	nullspacer}   r   rp   )rF   r[   r   r^   mrets         r-   _eigenspacer     sy    
eeAFFmh&
&A
+++
,C 3x1}kkZ$k?
3x1}!:AA(KM 	MJr5   c                     [         R                  " U SSS9nUR                  5       nUR                  [        :w  a1  [        U5      u  p4[        X4U R                  40 UD6n[        US S9nU$ g )NT)field	extensionc                     [        U S   5      $ )Nr   r   r:   s    r-   r3   !_eigenvects_DOM.<locals>.<lambda>3  s    6Fqt6Lr5   r   )	r   from_Matrixto_denserr   r   r   r   	__class__sorted)rF   kwargsDOMr_   	algebraic
eigenvectss         r-   _eigenvects_DOMr   +  sk    

"
"1DD
AC
,,.C
zzR,S1,8068
J,LM
r5   c                    U R                   " SSS0UD6nU H(  nUR                  [        5      (       d  M  [        S5      e   [	        UR                  5       [        S9n/ nU H"  u  px[        XXS9n	UR                  XxU	45        M$     U$ )Nr_   FzYEigenvector computation is not implemented if the matrix have eigenvalues in CRootOf formr   r   r*   )		eigenvalsrk   r   r   r   r   r	   r   rZ   )
rF   r   r^   rx   r   r:   r   r   multvectss
             r-   _eigenvects_sympyr   9  s    4U4e4I 55>>./ /  y(.>?I
C	AzM

Cu%&  Jr5   chopc                  ^^ UR                  SS5      nUR                  SS5      nUR                  SS5        UR                  SS5        [        U[        5      (       d  U(       a  [        OS mU R                  [        5      nU(       a=  [        S U  5       5      (       a  [        U 5      $ SS	K	J
m  U R                  U4S
 j5      n [        U 5      nUc  [        X4SU0UD6nU(       a&  U4S jn	U V
VVs/ s H  u  poX" U5      4PM     nnn
nU(       aG  U V
VVVs/ s H4  u  pnU
R                  US9X Vs/ s H  oR                  US9PM     sn4PM6     nnnn
nU$ s  snnn
f s  snf s  snnnn
f )a  Compute eigenvectors of the matrix.

Parameters
==========

error_when_incomplete : bool, optional
    Raise an error when not all eigenvalues are computed. This is
    caused by ``roots`` not returning a full list of eigenvalues.

iszerofunc : function, optional
    Specifies a zero testing function to be used in ``rref``.

    Default value is ``_iszero``, which uses SymPy's naive and fast
    default assumption handler.

    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
    is tested as non-zero, and ``None`` if it is undecidable.

simplify : bool or function, optional
    If ``True``, ``as_content_primitive()`` will be used to tidy up
    normalization artifacts.

    It will also be used by the ``nullspace`` routine.

chop : bool or positive number, optional
    If the matrix contains any Floats, they will be changed to Rationals
    for computation purposes, but the answers will be returned after
    being evaluated with evalf. The ``chop`` flag is passed to ``evalf``.
    When ``chop=True`` a default precision will be used; a number will
    be interpreted as the desired level of precision.

Returns
=======

ret : [(eigenval, multiplicity, eigenspace), ...]
    A ragged list containing tuples of data obtained by ``eigenvals``
    and ``nullspace``.

    ``eigenspace`` is a list containing the ``eigenvector`` for each
    eigenvalue.

    ``eigenvector`` is a vector in the form of a ``Matrix``. e.g.
    a vector of length 3 is returned as ``Matrix([a_1, a_2, a_3])``.

Raises
======

NotImplementedError
    If failed to compute nullspace.

Examples
========

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

See Also
========

eigenvals
MatrixBase.nullspace
r^   TFNrS   c                     U $ r7   r*   r   s    r-   r3   _eigenvects.<locals>.<lambda>  s    r5   c              3   8   #    U  H  oR                   v   M     g 7fr7   rb   r9   s     r-   r.   _eigenvects.<locals>.<genexpr>  re   r<   r   rg   c                    > T" U SS9$ rj   r*   rl   s    r-   r3   r     s    )A"=r5   c           	      z   > U  Vs/ s H(  o[        [        U5      5      -  R                  T5      PM*     sn$ s  snf r7   )r   listru   )lr2   simpfuncs     r-   denom_clean _eigenvects.<locals>.denom_clean  s1    DEFAqT!W%00:AFFFs   /8r   )r   popr~   r   r   rk   r   rs   r]   rt   rh   ru   r   r   rA   )rF   rn   r   r   rx   r^   	primitive
has_floatsr   r   r   r   esr2   rh   r   s                 @@r-   _eigenvectsr   L  sR   X yyT*H		*e,I	IIj$	IIj$h-- (9kuJ&A&&&%a((,KK=>
!
C
{JJEJ	G BEE2T;r?+E &)*%(MCr 		t	$d,LAWW$W-?,LM%( 	 * J F -M *s   8E" E.
;E)E.
)E.
c                     U R                   (       d  S/ 4$ U R                  SS9nU H9  u  p4nU(       a  UR                  (       d  SU4s  $ U[        U5      :w  d  M5  SU4s  $    SU4$ )zSee _is_diagonalizable. This function returns the bool along with the
eigenvectors to avoid calculating them again in functions like
``diagonalize``.FTr{   )ro   r   is_realr}   )rF   
reals_only	eigenvecsr   r   basiss         r-   _is_diagonalizable_with_eigenr     sk    
 ;;byd+I%5ckk)##3u:)## & ?r5   c                     U R                   (       d  g[        S U  5       5      (       a  U R                  5       (       a  g[        S U  5       5      (       a  U R                  (       a  g[	        XS9S   $ )a|  Returns ``True`` if a matrix is diagonalizable.

Parameters
==========

reals_only : bool, optional
    If ``True``, it tests whether the matrix can be diagonalized
    to contain only real numbers on the diagonal.


    If ``False``, it tests whether the matrix can be diagonalized
    at all, even with numbers that may not be real.

Examples
========

Example of a diagonalizable matrix:

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

Example of a non-diagonalizable matrix:

>>> M = Matrix([[0, 1], [0, 0]])
>>> M.is_diagonalizable()
False

Example of a matrix that is diagonalized in terms of non-real entries:

>>> M = Matrix([[0, 1], [-1, 0]])
>>> M.is_diagonalizable(reals_only=False)
True
>>> M.is_diagonalizable(reals_only=True)
False

See Also
========

sympy.matrices.matrixbase.MatrixBase.is_diagonal
diagonalize
Fc              3   8   #    U  H  oR                   v   M     g 7fr7   )r   r+   rN   s     r-   r.   %_is_diagonalizable.<locals>.<genexpr>  s     
 a99ar<   Tc              3   8   #    U  H  oR                   v   M     g 7fr7   )
is_complexr   s     r-   r.   r      s     
#A<<r<   r   r   )ro   rs   is_symmetricis_hermitianr   )rF   r   r   s      r-   _is_diagonalizabler     sY    X ;;

 a
   Q^^%5%5

#
###(B1EEr5   c                    U R                   S:X  d  [        S5      eU R                  5       nU R                  5       nU R                  5       nU S   [        U S   5      -  nU R	                  5       nU S   XE-  -   US'   U S   XE-  -
  US'   U SS 2S4   R	                  5       S:X  a
  SnSUS'   X4$ UR	                  5       UR	                  5       ::  a  UnOUnXS   -  nSUR	                  5       S-  -  nX4$ )Nr   zInput must be a column matrix)r   r   r   r)   )cols
ValueErrorcopyabsnorm)r:   r2   v_plusv_minusqnorm_xbets          r-   _householder_vectorr     s    66Q;899	AVVXFffhG	$#ag,AVVXFT7QZ'F4LdGaj(GDMQx}}!$ 6M ;;=GLLN*AA!H1668q=!6Mr5   c                    U R                   nU R                  nU R                  5       nUR                  U5      UR                  U5      pT[	        [        X5      5       GH  n[        X6S 2U4   5      u  pxUR                  X-
  5      X-  UR                  -  -
  n	XUS 2US 24   -  X6S 2US 24'   UR                  U5      n
XUS 2US 24'   XJ-  nUS-   US-
  ::  d  M~  [        X6US-   S 24   R                  5      u  pxUR                  X&-
  S-
  5      X-  UR                  -  -
  n	X6S 2US-   S 24   U	-  X6S 2US-   S 24'   UR                  U5      n
XUS-   S 2US-   S 24'   X-  nGM	     XCU4$ Nr   r)   )	rY   r   
as_mutabler   rX   minr   HT)rF   r   r=   rK   UVr,   r2   r   hh_mattemps              r-   _bidiagonal_decmp_hholderr     sg   	A	A	A558QUU1Xq3q9$Qr1uX.qu!##-qr12vY&"ab&	uuQxQRVHq5AE>(acd76FAUU1519%!##5FB!H+.Ab!A#$hK558D%1qstA  7Nr5   c                    U R                   nU R                  nU R                  5       n[        [	        X5      5       H  n[        X4S 2U4   5      u  pVUR                  X-
  5      Xe-  UR                  -  -
  nXsUS 2US 24   -  X4S 2US 24'   US-   US-
  ::  d  M^  [        X4US-   S 24   R                  5      u  pVUR                  X$-
  S-
  5      Xe-  UR                  -  -
  nX4S 2US-   S 24   U-  X4S 2US-   S 24'   M     U$ r   )	rY   r   r   rX   r   r   r   r   r   )rF   r   r=   rK   r,   r2   r   r   s           r-   _eval_bidiag_hholderr   4  s    	A	A	A3q9$Qr1uX.qscgm+qr12vY&"ab&	q5AE>(acd76FAUU1519%!##5FB!H+.Ab!A#$hK  Hr5   c                     [        U[        5      (       d  [        S5      eU(       a  [        U 5      $ [        U R                  5      nUS   R                  US   R                  US   R                  4$ )a  
Returns $(U,B,V.H)$ for

$$A = UBV^{H}$$

where $A$ is the input matrix, and $B$ is its Bidiagonalized form

Note: Bidiagonal Computation can hang for symbolic matrices.

Parameters
==========

upper : bool. Whether to do upper bidiagnalization or lower.
            True for upper and False for lower.

References
==========

.. [1] Algorithm 5.4.2, Matrix computations by Golub and Van Loan, 4th edition
.. [2] Complex Matrix Bidiagonalization, https://github.com/vslobody/Householder-Bidiagonalization

upper must be a booleanr)   r   r   )r~   boolr   r   r   )rF   upperXs      r-   _bidiagonal_decompositionr   C  s^    0 eT""233(++!!##&AQ4661Q4661Q466!!r5   c                     [        U[        5      (       d  [        S5      eU(       a  [        U 5      $ [        U R                  5      R                  $ )a  
Returns $B$, the Bidiagonalized form of the input matrix.

Note: Bidiagonal Computation can hang for symbolic matrices.

Parameters
==========

upper : bool. Whether to do upper bidiagnalization or lower.
            True for upper and False for lower.

References
==========

.. [1] Algorithm 5.4.2, Matrix computations by Golub and Van Loan, 4th edition
.. [2] Complex Matrix Bidiagonalization : https://github.com/vslobody/Householder-Bidiagonalization

r   )r~   r   r   r   r   )rF   r   s     r-   _bidiagonalizer   e  s@    ( eT""233#A&&$&&&r5   c                 l   U R                   (       d
  [        5       e[        U US9u  pEU(       d  [        S5      eU(       a  [	        U[
        S9n/ / pvU H  u  pn
Xx/U	-  -  nXj-  nM     U(       a"  U Vs/ s H  oUR                  5       -  PM     nnU R                  " U6 U R                  " U6 4$ s  snf )a0  
Return (P, D), where D is diagonal and

    D = P^-1 * M * P

where M is current matrix.

Parameters
==========

reals_only : bool. Whether to throw an error if complex numbers are need
                to diagonalize. (Default: False)

sort : bool. Sort the eigenvalues along the diagonal. (Default: False)

normalize : bool. If True, normalize the columns of P. (Default: False)

Examples
========

>>> from sympy import Matrix
>>> M = Matrix(3, 3, [1, 2, 0, 0, 3, 0, 2, -4, 2])
>>> M
Matrix([
[1,  2, 0],
[0,  3, 0],
[2, -4, 2]])
>>> (P, D) = M.diagonalize()
>>> D
Matrix([
[1, 0, 0],
[0, 2, 0],
[0, 0, 3]])
>>> P
Matrix([
[-1, 0, -1],
[ 0, 0, -1],
[ 2, 1,  2]])
>>> P.inv() * M * P
Matrix([
[1, 0, 0],
[0, 2, 0],
[0, 0, 3]])

See Also
========

sympy.matrices.matrixbase.MatrixBase.is_diagonal
is_diagonalizable
r   zMatrix is not diagonalizabler   )	ro   r   r   r   r   r	   r   hstackdiag)rF   r   sort	normalizeis_diagonalizabler   p_colsr   r   r   r   r2   s               r-   _diagonalizer     s    h ;;"$$#@%$'  8999*:;	rD%5%$, & (./1affh,/88Vaffdm++ 0s   5B1c                 b    U R                  5       nUSL a  gU(       a  U R                  (       a  gg NFT)_has_positive_diagonalsis_strongly_diagonally_dominant)rF   positive_diagonalss     r-   _fuzzy_positive_definiter     s-    224U"a??r5   c                 b    U R                  5       nUSL a  gU(       a  U R                  (       a  gg r   )_has_nonnegative_diagonalsis_weakly_diagonally_dominant)rF   nonnegative_diagonalss     r-   _fuzzy_positive_semidefiniter    s-    88:%!@!@r5   c                     U R                   (       d   U R                  (       d  gX R                  -   n [        U 5      nUb  U$ [	        U 5      $ NF)r   ro   r   r   _is_positive_definite_GErF   fuzzys     r-   r#   r#     s>    >>{{G$Q'E#A&&r5   c                     U R                   (       d   U R                  (       d  gX R                  -   n [        U 5      nUb  U$ [	        U 5      $ r  )r   ro   r   r  "_is_positive_semidefinite_choleskyr	  s     r-   r$   r$     s>    >>{{G(+E-a00r5   c                     [        U * 5      $ r7   )r#   rF   s    r-   r!   r!     s     !$$r5   c                     [        U * 5      $ r7   )r$   r  s    r-   r"   r"     s    $aR((r5   c                    U R                   (       a  U R                  5       nUR                  5        Vs/ s H  o"R                  PM     nn[	        U5      nUR                  5        Vs/ s H  o"R
                  PM     nn[	        U5      n[        XF/5      $ U R                  (       a  X R                  -   R                  $ gs  snf s  snf r  )
r   r   keysis_positiver   is_negativer   ro   r   is_indefinite)rF   eigenr:   args1any_positiveargs2any_negatives          r-   r    r      s    ~~/4zz|<|!|</4zz|<|!|<,566	
CC&&& =<s   C (Cc                    U R                  5       n U R                  n[        U5       H`  nXU4   R                  nUSLa  Us  $ [        US-   U5       H1  nXU4   XUS-   S24   -  XU4   XUS-   S24   -  -
  XUS-   S24'   M3     Mb     g)zNA division-free gaussian elimination method for testing
positive-definiteness.Tr   N)r   rY   rX   r  )rF   sizer,   r  js        r-   r  r    s     	
A66D4[1g))d"qsD!Aa41!W:-Q$!qstG*0DDA1gJ "	  r5   c           	         U R                  5       n [        U R                  5       GH  n[        XR                  5       Vs/ s H  o X"4   PM
     nn[        U5      u  pEpgU(       a    gUc^  [        US-   U R                  5       H?  n[        XR                  5       H#  nXU4   R
                  n	U	c        gU	SL d  M!        g   MA       gXU4   R                  (       d  UR                  (       a    gXU4   R                  (       a  UR                  (       d    gUS:  a(  U R                  XU-   5        U R                  XU-   5        [        XU4   5      XU4'   XUS-   S24==   XU4   -  ss'   XS-   S2US-   S24==   XUS-   S24   R                  XUS-   S24   -  -  ss'   GM     U S   R                  $ s  snf )zUses Cholesky factorization with complete pivoting

References
==========

.. [1] http://eprints.ma.man.ac.uk/1199/1/covered/MIMS_ep2008_116.pdf

.. [2] https://www.value-at-risk.net/cholesky-factorization/
Nr   FTr   )r  )r   rX   rY   r   r   is_zeror  is_nonnegativecol_swaprow_swapr   r   )
rF   r   r,   diagspivot	pivot_valnonzerorT   r  iszeros
             r-   r  r  $  s    	
A166]"'66"23"2Q14"23'=e'D$'=1Q3'q&&)A!tW__F~#5$ * ( T7)"7"7qD'((Y-E-E19JJqE'"JJqE'"qAw-Q$	QqST'
a1g
	A#$!*acd7QqST'
229 < V9###; 4s   F=aP  Finds out the definiteness of a matrix.

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

    A square real matrix $A$ is:

    - A positive definite matrix if $x^T A x > 0$
      for all non-zero real vectors $x$.
    - A positive semidefinite matrix if $x^T A x \geq 0$
      for all non-zero real vectors $x$.
    - A negative definite matrix if $x^T A x < 0$
      for all non-zero real vectors $x$.
    - A negative semidefinite matrix if $x^T A x \leq 0$
      for all non-zero real vectors $x$.
    - An indefinite matrix if there exists non-zero real vectors
      $x, y$ with $x^T A x > 0 > y^T A y$.

    A square complex matrix $A$ is:

    - A positive definite matrix if $\text{re}(x^H A x) > 0$
      for all non-zero complex vectors $x$.
    - A positive semidefinite matrix if $\text{re}(x^H A x) \geq 0$
      for all non-zero complex vectors $x$.
    - A negative definite matrix if $\text{re}(x^H A x) < 0$
      for all non-zero complex vectors $x$.
    - A negative semidefinite matrix if $\text{re}(x^H A x) \leq 0$
      for all non-zero complex vectors $x$.
    - An indefinite matrix if there exists non-zero complex vectors
      $x, y$ with $\text{re}(x^H A x) > 0 > \text{re}(y^H A y)$.

    A matrix need not be symmetric or hermitian to be positive definite.

    - A real non-symmetric matrix is positive definite if and only if
      $\frac{A + A^T}{2}$ is positive definite.
    - A complex non-hermitian matrix is positive definite if and only if
      $\frac{A + A^H}{2}$ is positive definite.

    And this extension can apply for all the definitions above.

    However, for complex cases, you can restrict the definition of
    $\text{re}(x^H A x) > 0$ to $x^H A x > 0$ and require the matrix
    to be hermitian.
    But we do not present this restriction for computation because you
    can check ``M.is_hermitian`` independently with this and use
    the same procedure.

    Examples
    ========

    An example of symmetric positive definite matrix:

    .. plot::
        :context: reset
        :format: doctest
        :include-source: True

        >>> from sympy import Matrix, symbols
        >>> from sympy.plotting import plot3d
        >>> a, b = symbols('a b')
        >>> x = Matrix([a, b])

        >>> A = Matrix([[1, 0], [0, 1]])
        >>> A.is_positive_definite
        True
        >>> A.is_positive_semidefinite
        True

        >>> p = plot3d((x.T*A*x)[0, 0], (a, -1, 1), (b, -1, 1))

    An example of symmetric positive semidefinite matrix:

    .. plot::
        :context: close-figs
        :format: doctest
        :include-source: True

        >>> A = Matrix([[1, -1], [-1, 1]])
        >>> A.is_positive_definite
        False
        >>> A.is_positive_semidefinite
        True

        >>> p = plot3d((x.T*A*x)[0, 0], (a, -1, 1), (b, -1, 1))

    An example of symmetric negative definite matrix:

    .. plot::
        :context: close-figs
        :format: doctest
        :include-source: True

        >>> A = Matrix([[-1, 0], [0, -1]])
        >>> A.is_negative_definite
        True
        >>> A.is_negative_semidefinite
        True
        >>> A.is_indefinite
        False

        >>> p = plot3d((x.T*A*x)[0, 0], (a, -1, 1), (b, -1, 1))

    An example of symmetric indefinite matrix:

    .. plot::
        :context: close-figs
        :format: doctest
        :include-source: True

        >>> A = Matrix([[1, 2], [2, -1]])
        >>> A.is_indefinite
        True

        >>> p = plot3d((x.T*A*x)[0, 0], (a, -1, 1), (b, -1, 1))

    An example of non-symmetric positive definite matrix.

    .. plot::
        :context: close-figs
        :format: doctest
        :include-source: True

        >>> A = Matrix([[1, 2], [-2, 1]])
        >>> A.is_positive_definite
        True
        >>> A.is_positive_semidefinite
        True

        >>> p = plot3d((x.T*A*x)[0, 0], (a, -1, 1), (b, -1, 1))

    Notes
    =====

    Although some people trivialize the definition of positive definite
    matrices only for symmetric or hermitian matrices, this restriction
    is not correct because it does not classify all instances of
    positive definite matrices from the definition $x^T A x > 0$ or
    $\text{re}(x^H A x) > 0$.

    For instance, ``Matrix([[1, 2], [-2, 1]])`` presented in
    the example above is an example of real positive definite matrix
    that is not symmetric.

    However, since the following formula holds true;

    .. math::
        \text{re}(x^H A x) > 0 \iff
        \text{re}(x^H \frac{A + A^H}{2} x) > 0

    We can classify all positive definite matrices that may or may not
    be symmetric or hermitian by transforming the matrix to
    $\frac{A + A^T}{2}$ or $\frac{A + A^H}{2}$
    (which is guaranteed to be always real symmetric or complex
    hermitian) and we can defer most of the studies to symmetric or
    hermitian positive definite matrices.

    But it is a different problem for the existence of Cholesky
    decomposition. Because even though a non symmetric or a non
    hermitian matrix can be positive definite, Cholesky or LDL
    decomposition does not exist because the decompositions require the
    matrix to be symmetric or hermitian.

    References
    ==========

    .. [1] https://en.wikipedia.org/wiki/Definiteness_of_a_matrix#Eigenvalues

    .. [2] https://mathworld.wolfram.com/PositiveDefiniteMatrix.html

    .. [3] Johnson, C. R. "Positive Definite Matrices." Amer.
        Math. Monthly 77, 259-264 1970.
    c                  ^ ^^^ ^!^"^#^$ T R                   (       d  [        S5      eT m!T R                  [        5      m T (       a6   [	        S T R                  5        5       5      n[	        [        U5      S5      m#UU U#4S jn0 m"U U!U"4S jmU U4S jnS nU 4S	 jnT (       a  S
SKJ	m$  T!R                  U$4S j5      m!T!R                  5       nU H(  n	U	R                  [        5      (       d  M  [        S5      e   [        UR                  5       5      T!R                   :X  a{  [#        UR                  5       [$        S9n
T!R&                  " U
6 nU(       d  U" U5      $ U
 Vs/ s H  nT" US5      R)                  5       S
   PM     nnT!R*                  " U6 nU" X5      $ / n[#        UR                  5       [$        S9 H  nX   nU" UU5      nU" U5      n[-        U5       VVs/ s H  u  nnUS-   U4PM     nnnUR/                  5         UR1                  U VVVs/ s H  u  nn[3        U5        H  nUU4PM	     M     snnn5        M     [5        S U 5       5      nUT R6                  :w  a  [        SR9                  T 5      5      eU!4S jU 5       n
T!R&                  " U
6 nU(       d  U" U5      $ / n[#        UR                  5       [$        S9 H  n/ nU H  u  nnUU:w  a  M  T" UU5      R)                  5       nT" UUS-
  5      R)                  5       nU" UU-   U5      n[3        U5       Vs/ s H  nT" UU5      R;                  USS9PM     nnUR1                  U5        UR1                  [=        U5      5        M     M     T!R*                  " U6 nU" X5      $ ! [         a    Sn GNEf = fs  snf s  snnf s  snnnf s  snf )a  Return $(P, J)$ where $J$ is a Jordan block
matrix and $P$ is a matrix such that $M = P J P^{-1}$

Parameters
==========

calc_transform : bool
    If ``False``, then only $J$ is returned.

chop : bool
    All matrices are converted to exact types when computing
    eigenvalues and eigenvectors.  As a result, there may be
    approximation errors.  If ``chop==True``, these errors
    will be truncated.

Examples
========

>>> from sympy import Matrix
>>> M = Matrix([[ 6,  5, -2, -3], [-3, -1,  3,  3], [ 2,  1, -2, -3], [-1,  1,  5,  5]])
>>> P, J = M.jordan_form()
>>> J
Matrix([
[2, 1, 0, 0],
[0, 2, 0, 0],
[0, 0, 2, 1],
[0, 0, 0, 2]])

See Also
========

jordan_block
z&Only square matrices have Jordan formsc              3   h   #    U  H(  n[        U[        5      (       d  M  UR                  v   M*     g 7fr7   )r~   r   r8   )r+   terms     r-   r.   _jordan_form.<locals>.<genexpr>0  s      X*$
4QV@W:4::*s   225      c                     > T(       a  U  Vs/ s H  oR                  TTS9PM     n n[        U 5      S:X  a  U S   $ U $ s  snf )zEIf ``has_floats`` is `True`, cast all ``args`` as
matrices of floats.)r=   r   r   r   )rA   r}   )argsr   r   r   max_dpss     r-   restore_floats$_jordan_form.<locals>.restore_floats:  sF     ;?@4aGGgDG14D@t9>7N	 As   >c                    > X4T;   a  TX4   $ XS-
  4T;   a!  TXS-
  4   R                  TU S4   SS9TX4'   O4TU TR                  TR                  5      -  -
  R                  U5      TX4'   TX4   $ )z>Cache computations of ``(M - val*I)**pow`` for quick
retrievalr   Ndotprodsimp)multiplyr   rY   pow)r   r7  rF   mat	mat_caches     r-   eig_mat_jordan_form.<locals>.eig_matH  s     :"cZ((q>Y&$-s!Gn$=$F$FsAh'T %G %;Isj! &)3quuQVV}+<%<$A$A#$FIsj!#$$r5   c                 >  > TR                   nS/nUT" U S5      R                  5       -
  nSnXCS   :w  ah  UR                  U5        XA:X  a   U$ UT" X5      R                  5       -
  nUS-  nXCS   :  d  XA:  a  [        SR	                  T5      5      eXCS   :w  a  Mh  U$ )zhCalculate the sequence  [0, nullity(E), nullity(E**2), ...]
until it is constant where ``E = M - val*I``r   r   r)   r  zMSymPy had encountered an inconsistent result while computing Jordan block: {})r   rankrZ   r   rp   )r   algebraic_multiplicityr   r   nullityr,   rF   r:  s         r-   nullity_chain#_jordan_form.<locals>.nullity_chainX  s     &&#a--//R JJw0 
 gco2244GqLA
 R G$D!$ $ R $ 
r5   c                     [        S[        U 5      S-
  5       Vs/ s H  nSX   -  XS-
     -
  XS-      -
  PM     nn[        U 5      S:  a  U S   U S   -
  /OU S   /nX#-   $ s  snf )zReturn a list of the size of each Jordan block.
If d_n is the nullity of E**n, then the number
of Jordan blocks of size n is

    2*d_n - d_(n-1) - d_(n+1)r   r)   r  r   )rX   r}   )dr=   midends       r-   blocks_from_nullity_chain/_jordan_form.<locals>.blocks_from_nullity_chainw  sz     6;1c!fqj5IJ5Iqva% 1U8+5IJ "%Q!ququ}o!A$y Ks   !A#c                    > [        U 5      S:X  a  US   $ U H:  nTR                  " X/-   6 R                  SS9u  p4US   [        U 5      :X  d  M8  Us  $    g)zOPicks a vector from big_basis that isn't in
the subspace spanned by small_basisr   T)with_pivotsr  N)r}   r   echelon_form)small_basis	big_basisr2   rT   pivotsrF   s        r-   pick_vec_jordan_form.<locals>.pick_vec  sg     {q Q<A;#46CC $ D &IA bzS-- r5   r   rg   c                    > T" U SS9$ rj   r*   rl   s    r-   r3   _jordan_form.<locals>.<lambda>  s    iD&Ar5   zTJordan normal form is not implemented if the matrix have eigenvalues in CRootOf formr   r   c              3   *   #    U  H	  u  pUv   M     g 7fr7   r*   )r+   rB   r  s      r-   r.   r+    s     AIC4r0   zOSymPy had encountered an inconsistent result while computing Jordan block. : {}c              3   F   >#    U  H  u  pTR                  X!S 9v   M     g7f))r  
eigenvalueN)jordan_block)r+   rB   r  r8  s      r-   r.   r+    s!     _)##"""=s   !Nr4  )ro   r   rk   r   r>   r   r   r   rt   rh   ru   r   r   r   r}   r  r   r   r	   r   r   r   	enumeratereverseextendrX   r1   rY   rp   r6  reversed)%rF   calc_transformr   max_precr1  r@  rG  rO  r   r:   blocks
jordan_matrB   jordan_basis	basis_matblock_structurer>  chainblock_sizesr,   num	size_numsr  rT   jordan_form_size	eig_basis	block_eignull_big
null_smallvecnew_vecsr:  r   r8  r9  r0  rh   s%   ` `                            @@@@@@r-   _jordan_formrm    s   F ;;"#KLLCuJ	X!((*XXH k(+R0	 I% > ,mmAB ==?D 55>>./ /  499;388#DIIK-=>XXv&
!*-- "#!C  Q113A6! 	 #zz<0	i44Odiik'78!%c#9:/6 /8.DE.DFAsac3Z.D	E 	*3H)YT3U3Zc4[Z[)H	J 9" AAA166!++16!96 	6 `_F6"Jj)) Ldiik'78	.OItC!#t,779H!#tax0;;=J  
Y 6AC"4[*(  Q00$0G(  * X& 23!  / 9* 

L)I)00M  	 H	x#" F IV*s*    N  $N-!N2$N8-!N?N*)N*c                     U R                  5       R                  " S0 UD6nU VVVVs/ s H'  u  p4oSXE Vs/ s H  ofR                  5       PM     sn4PM)     snnnn$ s  snf s  snnnnf )ak  Returns left eigenvectors and eigenvalues.

This function returns the list of triples (eigenval, multiplicity,
basis) for the left eigenvectors. Options are the same as for
eigenvects(), i.e. the ``**flags`` arguments gets passed directly to
eigenvects().

Examples
========

>>> from sympy import Matrix
>>> M = Matrix([[0, 1, 1], [1, 0, 0], [1, 1, 1]])
>>> M.eigenvects()
[(-1, 1, [Matrix([
[-1],
[ 1],
[ 0]])]), (0, 1, [Matrix([
[ 0],
[-1],
[ 1]])]), (2, 1, [Matrix([
[2/3],
[1/3],
[  1]])])]
>>> M.left_eigenvects()
[(-1, 1, [Matrix([[-2, 1, 1]])]), (0, 1, [Matrix([[-1, -1, 1]])]), (2,
1, [Matrix([[1, 1, 1]])])]

r*   )	transposer   )rF   rx   r   r   r   r   r   s          r-   _left_eigenvectsrp    sW    < ;;=##,e,DPTUPT<LCu$6167PTUU6Us   A!
AA!
A!
c                    U R                   U R                  :  a*  U R                  R                  U 5      R	                  5       nO)U R                  U R                  5      R	                  5       n/ nUR                  5        H  u  p4U[        U5      /U-  -  nM     [        U5      U R                  :  a(  X R                  /U R                  [        U5      -
  -  -  nUR                  S[        S9  U$ )a  Compute the singular values of a Matrix

Examples
========

>>> from sympy import Matrix, Symbol
>>> x = Symbol('x', real=True)
>>> M = Matrix([[0, 1, 0], [0, x, 0], [-1, 0, 0]])
>>> M.singular_values()
[sqrt(x**2 + 1), 1, 0]

See Also
========

condition_number
T)rX  r   )rY   r   r   r6  r   r   r   r}   zeror   r	   )rF   valmultpairsvalsr   r2   s        r-   _singular_valuesru    s    $ 	vvss||A002zz!##002 D""$a	A %
 4y166AFFSY.// 	IId 0I1Kr5   N)F)T)TF)FFF)Ntypesr   collectionsr   mpmathr   r   mpmath.libmp.libmpfr   sympy.core.sortingr	   sympy.core.evalfr
   r   sympy.core.logicr   r   sympy.core.numbersr   sympy.core.sympifyr   (sympy.functions.elementary.miscellaneousr   sympy.polysr   r   r   r   r   sympy.polys.matricesr   sympy.polys.matrices.eigenr   r   sympy.polys.polytoolsr   
exceptionsr   r   determinantr   	utilitiesr   r   __doctest_requires__rP   rV   r]   ry   r   rv   rw   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r#   r$   r!   r"   r    r  r  _doc_positive_definite__doc__rm  rp  ru  r*   r5   r-   <module>r     su      + / @ 0 $ ' 9 2 2 - N % 9 / )" %1> 4! "h/4uhX2  -2&3T -2*EZ )0% & *.' l5 l^&5Fr.,"D'8I,X
'
1%) )$Zk \ %;  $:  !$:  $:  !$: r1 r1j VF%r5   