
    wgJ_                         d Z ddlmZ  G d de      Zd Zd Zd Zedd       Zd	 Z	d
 Z
edd       Zd Zd Zedd       Zedd       Zy)a+  
The eigenvalue problem
----------------------

This file contains routines for the eigenvalue problem.

high level routines:

  hessenberg : reduction of a real or complex square matrix to upper Hessenberg form
  schur : reduction of a real or complex square matrix to upper Schur form
  eig : eigenvalues and eigenvectors of a real or complex square matrix

low level routines:

  hessenberg_reduce_0 : reduction of a real or complex square matrix to upper Hessenberg form
  hessenberg_reduce_1 : auxiliary routine to hessenberg_reduce_0
  qr_step : a single implicitly shifted QR step for an upper Hessenberg matrix
  hessenberg_qr : Schur decomposition of an upper Hessenberg matrix
  eig_tr_r : right eigenvectors of an upper triangular matrix
  eig_tr_l : left  eigenvectors of an upper triangular matrix
   )xrangec                       e Zd Zy)EigenN)__name__
__module____qualname__     Z/home/mcse/projects/flask/flask-venv/lib/python3.12/site-packages/mpmath/matrices/eigen.pyr   r   &   s    r
   r   c                 <    t        t        | j                  |        | S )N)setattrr   r   )fs    r   defunr   )   s    E1::q!Hr
   c                    |j                   }|dk  ryt        |dz
  dd      D ]  }d}t        d|      D ]D  }|t        | j                  |||f               t        | j	                  |||f               z   z  }F d}|dk7  rd|z  }|dk(  s| j                  |      rd||<   d|||dz
  f<   d}t        d|      D ]K  }|||fxx   |z  cc<   | j                  |||f         }	| j	                  |||f         }
||	|	z  |
|
z  z   z  }M |||dz
  f   }t        |      }| j                  |      }| |z  |||dz
  f<   |dk(  r|||<   n"||z  }|||z  z   ||<   |||dz
  fxx   |z  cc<   |||z  z  }d| j                  |      z  }||xx   |z  cc<   t        d|dz
        D ]  }|||fxx   |z  cc<    t        d|      D ]  }| j                  ||         |||dz
  f   z  }t        d|dz
        D ]#  }|| j                  |||f         |||f   z  z  }% |||dz
  fxx   |||   z  z  cc<   t        d|dz
        D ]  }|||fxx   ||||f   z  z  cc<     t        d|      D ]  }||   ||dz
  |f   z  }t        d|dz
        D ]  }||||f   |||f   z  z  } ||dz
  |fxx   || j                  ||         z  z  cc<   t        d|dz
        D ](  }|||fxx   || j                  |||f         z  z  cc<   *   y)a  
    This routine computes the (upper) Hessenberg decomposition of a square matrix A.
    Given A, an unitary matrix Q is calculated such that

               Q' A Q = H              and             Q' Q = Q Q' = 1

    where H is an upper Hessenberg matrix, meaning that it only contains zeros
    below the first subdiagonal. Here ' denotes the hermitian transpose (i.e.
    transposition and conjugation).

    parameters:
      A         (input/output) On input, A contains the square matrix A of
                dimension (n,n). On output, A contains a compressed representation
                of Q and H.
      T         (output) An array of length n containing the first elements of
                the Householder reflectors.
    r   N       )rowsr   absreimisinfsqrtconj)ctxATniscalek	scale_invHrriiFr   Gffjs                   r   hessenberg_reduce_0r*   -   s   @ 	
AAvvAaCB B/ 1 	?AS!A#(3svva!f~+>>>E	? 	A:E	IA:9-AaDAa!eH 1 	#AacFiF!A#B!A#Bb27""A		# a!eHFHHQK3;!AaC%6AaDQBq2v:AaDa!eHNH	QU
O	!	1q5! 	AacFaKF	 1 		%A 11QqS5)AAqs^ /SXXa!f%!A#../ a!eHAaD HAqs^ %!A#!a!f*$%		% 1 		/A !q1QxAAqs^ %QqsVa!f_$% ac!eHCHHQqTN**HAqs^ /!A#!chhq1v.../		/sB/r
   c                 (   |j                   }|dk(  rd|d<   ydx|d<   |d<   dx|d<   |d<   t        d|      D ]  }||   dk7  rt        d|      D ]  }||   ||dz
  |f   z  }t        d|dz
        D ]  }||||f   |||f   z  z  } ||dz
  |fxx   || j                  ||         z  z  cc<   t        d|dz
        D ](  }|||fxx   || j                  |||f         z  z  cc<   *  d|||f<   t        d|      D ]  }dx|||f<   |||f<     y)	a>  
    This routine forms the unitary matrix Q described in hessenberg_reduce_0.

    parameters:
      A    (input/output) On input, A is the same matrix as delivered by
           hessenberg_reduce_0. On output, A is set to Q.

      T    (input) On input, T is the same array as delivered by hessenberg_reduce_0.
    r   )r   r   N)r   r   r   )r   r   )r   r   r   )r   r   r   )r   r   r   r   r   r)   r'   r!   s           r   hessenberg_reduce_1r,      sq    	
AAv#AcFQsVAcFQsVAq\  Q419Aq\ 3aD1QqSU8O1Q3 )A1Q3!AaC&(A) !A#a%A1..1Q3 3AacFa#((1QqS6"222F33 !A#1 	 AAacFQqsV	  r
   c                 H   |j                   }|dk(  r| j                  dgg      |fS |s|j                         }| j                  |d      }t        | ||       |j                         }t	        | ||       t        |      D ]  }t        |dz   |      D ]	  }d|||f<     ||fS )a  
    This routine computes the Hessenberg decomposition of a square matrix A.
    Given A, an unitary matrix Q is determined such that

          Q' A Q = H                and               Q' Q = Q Q' = 1

    where H is an upper right Hessenberg matrix. Here ' denotes the hermitian
    transpose (i.e. transposition and conjugation).

    input:
      A            : a real or complex square matrix
      overwrite_a  : if true, allows modification of A which may improve
                     performance. if false, A is not modified.

    output:
      Q : an unitary matrix
      H : an upper right Hessenberg matrix

    example:
      >>> from mpmath import mp
      >>> A = mp.matrix([[3, -1, 2], [2, 5, -5], [-2, -3, 7]])
      >>> Q, H = mp.hessenberg(A)
      >>> mp.nprint(H, 3) # doctest:+SKIP
      [  3.15  2.23  4.44]
      [-0.769  4.85  3.05]
      [   0.0  3.61   7.0]
      >>> print(mp.chop(A - Q * H * Q.transpose_conj()))
      [0.0  0.0  0.0]
      [0.0  0.0  0.0]
      [0.0  0.0  0.0]

    return value:   (Q, H)
    r   r   r   )r   matrixcopyr*   r,   r   r   r   overwrite_ar   r   Qxys           r   
hessenbergr5      s    H 	
AAv

QC5!1%%FFH

1aAQ"	AQ"AY !Q 	AAacF	 a4Kr
   c           
      F   |j                   }|||f   |z
  }||dz   |f   }| j                  | j                  | j                  |      | j                  |            | j                  | j                  |      | j                  |                  }	|	dk(  rd}	d}d}n
||	z  }||	z  }| j	                  |      }
| j	                  |      }t        ||      D ]6  }|||f   }||dz   |f   }|
|z  ||z  z   |||f<   ||z  ||z  z
  ||dz   |f<   8 t        t        ||dz               D ]6  }|||f   }|||dz   f   }||z  ||z  z   |||f<   |
|z  ||z  z
  |||dz   f<   8 t        |t              sDt        |      D ]6  }|||f   }|||dz   f   }||z  ||z  z   |||f<   |
|z  ||z  z
  |||dz   f<   8 t        ||dz
        D ]  }||dz   |f   }||dz   |f   }| j                  | j                  | j                  |      | j                  |            | j                  | j                  |      | j                  |                  }	|	dk(  rd||dz   |f<   d}	d}d}n|	||dz   |f<   ||	z  }||	z  }d||dz   |f<   | j	                  |      }
| j	                  |      }t        |dz   |      D ]<  }||dz   |f   }||dz   |f   }|
|z  ||z  z   ||dz   |f<   ||z  ||z  z
  ||dz   |f<   > t        dt        ||dz               D ]<  }|||dz   f   }|||dz   f   }||z  ||z  z   |||dz   f<   |
|z  ||z  z
  |||dz   f<   > t        |t              rt        d|      D ]<  }|||dz   f   }|||dz   f   }||z  ||z  z   |||dz   f<   |
|z  ||z  z
  |||dz   f<   >  y)a  
    This subroutine executes a single implicitly shifted QR step applied to an
    upper Hessenberg matrix A. Given A and shift as input, first an QR
    decomposition is calculated:

      Q R = A - shift * 1 .

    The output is then following matrix:

      R Q + shift * 1

    parameters:
      n0, n1    (input) Two integers which specify the submatrix A[n0:n1,n0:n1]
                on which this subroutine operators. The subdiagonal elements
                to the left and below this submatrix must be deflated (i.e. zero).
                following restriction is imposed: n1>=n0+2
      A         (input/output) On input, A is an upper Hessenberg matrix.
                On output, A is replaced by "R Q + shift * 1"
      Q         (input/output) The parameter Q is multiplied by the unitary matrix
                Q arising from the QR decomposition. Q can also be false, in which
                case the unitary matrix Q is not computated.
      shift     (input) a complex number specifying the shift. idealy close to an
                eigenvalue of the bottemmost part of the submatrix A[n0:n1,n0:n1].

    references:
      Stoer, Bulirsch - Introduction to Numerical Analysis.
      Kresser : Numerical Methods for General and Structured Eigenvalue Problems
    r   r      r      N)	r   hypotr   r   r   r   min
isinstancebool)r   n0n1r   r2   shiftr   csvcccsr!   r3   r4   r)   s                   r   qr_steprE      sx   V 	
A
 	
"r'
UA	"Q$r'
A		#))CFF1Isvvay1399SVVAYq	3RSAAv	Q	Q	!B	!BB] "b1fIbd1fIFR!VO"q&	EAEM"Q$q&	" CBqDM" $adIa1fIEAEM!B$	FR!VO!BqD&	$ a 	(A!B$	A!BqD&	AAAAadIQaAa1fI	( BQ ++ ac!eHac!eHIIciiq	366!95syyCFFSTI7VW6Aac!eHAAAAac!eHFAFA!A#a%XXa[XXa[!Q 	%A!A#a%A!A#a%AAvQAac!eH1uq1u}Aac!eH	% 3r1Q3<( 	'A!AaC%A!AaC%A1uq1u}Aa!eHAvQAa!eH	' !T"Aq\ +a!eHa!eHq51q5=!AaC%6BF?!AaC%+M++r
   c           	         |j                   }d}t        |      D ]U  }t        t        |dz   |            D ]8  }|| j                  |||f         dz  | j	                  |||f         dz  z   z  }: W | j                  |      |z  }|dk(  ryd}|}| j                  d|z  z  }	| j                  dz  }
dx}}	 |}|dz   |k  rt        | j                  |||f               t        | j	                  |||f               z   t        | j                  ||dz   |dz   f               z   t        | j	                  ||dz   |dz   f               z   }||	|z  k  r|}t        ||dz   |f         |	|z  k  rn|dz  }|dz   |k  r|dz   |k  r(d||dz   |f<   |dz   }d}|dz   |k\  rWd}|dz   }|dk  rJy|dz  dk(  r||dz
  |dz
  f   }n|dz  d	k(  rt        ||dz
  |dz
  f         }n|dz  d
k(  r|}n||dz
  |dz
  f   ||dz
  |dz
  f   z   }||dz
  |dz
  f   ||dz
  |dz
  f   z
  dz  d||dz
  |dz
  f   z  ||dz
  |dz
  f   z  z   }| j                  |      dkD  r| j                  |      }n| j                  |       dz  }||z   dz  }||z
  dz  }t        ||dz
  |dz
  f   |z
        t        ||dz
  |dz
  f   |z
        kD  r|}n|}|dz  }|dz  }t        | |||||       ||
kD  rt        d|z        B)a  
    This routine computes the Schur decomposition of an upper Hessenberg matrix A.
    Given A, an unitary matrix Q is determined such that

          Q' A Q = R                   and                  Q' Q = Q Q' = 1

    where R is an upper right triangular matrix. Here ' denotes the hermitian
    transpose (i.e. transposition and conjugation).

    parameters:
      A         (input/output) On input, A contains an upper Hessenberg matrix.
                On output, A is replace by the upper right triangluar matrix R.

      Q         (input/output) The parameter Q is multiplied by the unitary
                matrix Q arising from the Schur decomposition. Q can also be
                false, in which case the unitary matrix Q is not computated.
    r   r   Nd   r8   r      
         y              ?z%qr: failed to converge after %d steps)r   r   r:   r   r   r   epsdpsr   rE   RuntimeError)r   r   r2   r   normr3   r4   r=   r>   rL   maxitsitstotalitsr!   rA   r?   tabs                      r   hessenberg_qrrV     s   & 	
ADAY >AaC$ 	>ACFF1QqS6Na'#&&1Q3.A*===D	>> 88D>ADqy	
B	
B
''S1W
CWWq[FC(
 !ebjCFF1QqS6N#c#&&1Q3.&99Cq1QqSz@R<SSVYZ]Z`Z`abcdefcfghijgjcjakZlVmmA3:~1QqSU8}sQw&FA !ebj q52: Aac!eHQBCAv|U6bR"Q$r!t)(r!Abd2a4iL)(r! r!tBqDy\Abd2a4iL0r!tBqDy\Abd2a4iL0Q6Qr!tBqDy\9IAbQRdSUVWSWiL9XX66!9q=A!r)AUaKUaKqAbd|a'(3qAbd|a/?+@@EE1HCMHCRAu-V|"#JS#PQQG r
   c                 b   |j                   }|dk(  r| j                  dgg      |fS |s|j                         }| j                  |d      }t        | ||       |j                         }t	        | ||       t        |      D ]  }t        |dz   |      D ]	  }d|||f<     t        | ||       ||fS )a;  
    This routine computes the Schur decomposition of a square matrix A.
    Given A, an unitary matrix Q is determined such that

          Q' A Q = R                and               Q' Q = Q Q' = 1

    where R is an upper right triangular matrix. Here ' denotes the
    hermitian transpose (i.e. transposition and conjugation).

    input:
      A            : a real or complex square matrix
      overwrite_a  : if true, allows modification of A which may improve
                     performance. if false, A is not modified.

    output:
      Q : an unitary matrix
      R : an upper right triangular matrix

    return value:   (Q, R)

    example:
      >>> from mpmath import mp
      >>> A = mp.matrix([[3, -1, 2], [2, 5, -5], [-2, -3, 7]])
      >>> Q, R = mp.schur(A)
      >>> mp.nprint(R, 3) # doctest:+SKIP
      [2.0  0.417  -2.53]
      [0.0    4.0  -4.74]
      [0.0    0.0    9.0]
      >>> print(mp.chop(A - Q * R * Q.transpose_conj()))
      [0.0  0.0  0.0]
      [0.0  0.0  0.0]
      [0.0  0.0  0.0]

    warning: The Schur decomposition is not unique.
    r   r   r   )r   r.   r/   r*   r,   r   rV   r0   s           r   schurrX     s    L 	
AAv

QC5!1%%FFH

1aAQ"	AQ"AY Aq! 	AAacF	 #q!a4Kr
   c                    |j                   }| j                  |      }| j                  }| j                  | j                  | j
                   dz        }|||z  z  }d| j                  |      z  }d}t        d|      D ]  }	||	|	f   }
t        |t        |
      z  |      }t        |	dz
  dd      D ]  }d}t        |dz   |	dz         D ]  }||||f   |||	f   z  z  } |||f   |
z
  }t        |      |k  r|}| |z  }||||	f<   t        |t        |            }||kD  spt        ||	dz         D ]  }|||	fxx   |z  cc<    d} |dk7  st        d|	dz         D ]  }|||	fxx   |z  cc<     |S )z
    This routine calculates the right eigenvectors of an upper right triangular matrix.

    input:
      A      an upper right triangular matrix

    output:
      ER     a matrix whose columns form the right eigenvectors of A

    return value: ER
    rH   r   r   r   
r   eyerL   ldexponeprecr   r   maxr   )r   r   r   ERrL   unflsmlnumsiminrmaxr   rA   sminr)   rr!   rS   s                   r   eig_tr_rrg   (  s    	
A	B
''C99SWWsxxi"n-D QWFEDAq\  acF3Q<(Ar2& 	AAAE1q5) &QqsVb1g%%& !A#
A1v}QABqsGtSV$De|1Q3 $AqsGtOG$#	& 19Aq1u%  1Q34 3 8 Ir
   c                    |j                   }| j                  |      }| j                  }| j                  | j                  | j
                   dz        }|||z  z  }d| j                  |      z  }d}t        d|dz
        D ]  }	||	|	f   }
t        |t        |
      z  |      }t        |	dz   |      D ]  }d}t        |	|      D ]  }|||	|f   |||f   z  z  } |||f   |
z
  }t        |      |k  r|}| |z  }|||	|f<   t        |t        |            }||kD  sjt        |	|dz         D ]  }||	|fxx   |z  cc<    d} |dk7  st        |	|      D ]  }||	|fxx   |z  cc<     |S )z
    This routine calculates the left eigenvectors of an upper right triangular matrix.

    input:
      A      an upper right triangular matrix

    output:
      EL     a matrix whose rows form the left eigenvectors of A

    return value:  EL
    rH   r   r   rZ   )r   r   r   ELrL   ra   rb   rc   rd   r   rA   re   r)   rf   r!   rS   s                   r   eig_tr_lrj   d  s    	
A	B
''C99SWWsxxi"n-D QWFEDAq1u  acF3Q<(Aq! 	AAAq\ &R!Wq1v%%& !A#
A1v}QABqsGtSV$De|1q5) $AqsGtOG$#	& 19Aq\  1Q34 3 8 Ir
   c                    |j                   }|dk(  re|r|s|d   g| j                  dgg      fS |r|s|d   g| j                  dgg      fS |d   g| j                  dgg      | j                  dgg      fS |s|j                         }| j                  |d      }t	        | ||       |s|r|j                         }t        | ||       nd}t        |      D ]  }t        |dz   |      D ]	  }	d||	|f<     t        | ||       t        |      D 
cg c]  }
d }}
t        |      D ]  }
||
|
f   ||
<    |s|s|S |rt        | |      }||j                         z  }|rt        | |      }||z  }|r|s|fS |r|s|fS |fS c c}
w )a  
    This routine computes the eigenvalues and optionally the left and right
    eigenvectors of a square matrix A. Given A, a vector E and matrices ER
    and EL are calculated such that

                        A ER[:,i] =         E[i] ER[:,i]
                EL[i,:] A         = EL[i,:] E[i]

    E contains the eigenvalues of A. The columns of ER contain the right eigenvectors
    of A whereas the rows of EL contain the left eigenvectors.


    input:
      A           : a real or complex square matrix of shape (n, n)
      left        : if true, the left eigenvectors are calculated.
      right       : if true, the right eigenvectors are calculated.
      overwrite_a : if true, allows modification of A which may improve
                    performance. if false, A is not modified.

    output:
      E    : a list of length n containing the eigenvalues of A.
      ER   : a matrix whose columns contain the right eigenvectors of A.
      EL   : a matrix whose rows contain the left eigenvectors of A.

    return values:
       E            if left and right are both false.
      (E, ER)       if right is true and left is false.
      (E, EL)       if left is true and right is false.
      (E, EL, ER)   if left and right are true.


    examples:
      >>> from mpmath import mp
      >>> A = mp.matrix([[3, -1, 2], [2, 5, -5], [-2, -3, 7]])
      >>> E, ER = mp.eig(A)
      >>> print(mp.chop(A * ER[:,0] - E[0] * ER[:,0]))
      [0.0]
      [0.0]
      [0.0]

      >>> E, EL, ER = mp.eig(A,left = True, right = True)
      >>> E, EL, ER = mp.eig_sort(E, EL, ER)
      >>> mp.nprint(E)
      [2.0, 4.0, 9.0]
      >>> print(mp.chop(A * ER[:,0] - E[0] * ER[:,0]))
      [0.0]
      [0.0]
      [0.0]
      >>> print(mp.chop( EL[0,:] * A - EL[0,:] * E[0]))
      [0.0  0.0  0.0]

    warning:
     - If there are multiple eigenvalues, the eigenvectors do not necessarily
       span the whole vectorspace, i.e. ER and EL may have not full rank.
       Furthermore in that case the eigenvectors are numerical ill-conditioned.
     - In the general case the eigenvalues have no natural order.

    see also:
      - eigh (or eigsy, eighe) for the symmetric eigenvalue problem.
      - eig_sort for sorting of eigenvalues and eigenvectors
    r   r   Fr   )r   r.   r/   zerosr*   r,   r   rV   rj   transpose_conjrg   )r   r   leftrightr1   r   r   r2   r3   r4   r   Eri   r`   s                 r   eigrq     s   @ 	
AAvqTFCJJu-..$qTFCJJu-..1

QC5)3::se+<==FFH		!QAQ"uFFHCA&AY Aq! 	AAacF	 #q!1IqAAY 1v! Ec1!""$$c1VU2wd2wr2;+ 	s   	Fc                 8   t        |t              r>|dk(  r| j                  }n,|dk(  r| j                  }n|dk(  rt        }nt        d|z        t        |      }t        |      D ]  }|} |||         }t        |dz   |      D ]  }	 |||	         }
|
|k  s|
}|	} ||k7  s?||   }||   ||<   |||<   t        |t              s*t        |      D ]  }	|||	f   }|||	f   |||	f<   ||||	f<    t        |t              rt        |      D ]  }	||	|f   }||	|f   ||	|f<   |||	|f<     t        |t              rt        |t              r|S t        |t              rt        |t              s||fS t        |t              rt        |t              s||fS |||fS )aA  
    This routine sorts the eigenvalues and eigenvectors delivered by ``eig``.

    parameters:
      E  : the eigenvalues as delivered by eig
      EL : the left  eigenvectors as delivered by eig, or false
      ER : the right eigenvectors as delivered by eig, or false
      f  : either a string ("real" sort by increasing real part, "imag" sort by
           increasing imag part, "abs" sort by absolute value) or a function
           mapping complexs to the reals, i.e. ``f = lambda x: -mp.re(x) ``
           would sort the eigenvalues by decreasing real part.

    return values:
       E            if EL and ER are both false.
      (E, ER)       if ER is not false and left is false.
      (E, EL)       if EL is not false and right is false.
      (E, EL, ER)   if EL and ER are not false.

    example:
      >>> from mpmath import mp
      >>> A = mp.matrix([[3, -1, 2], [2, 5, -5], [-2, -3, 7]])
      >>> E, EL, ER = mp.eig(A,left = True, right = True)
      >>> E, EL, ER = mp.eig_sort(E, EL, ER)
      >>> mp.nprint(E)
      [2.0, 4.0, 9.0]
      >>> E, EL, ER = mp.eig_sort(E, EL, ER,f = lambda x: -mp.re(x))
      >>> mp.nprint(E)
      [9.0, 4.0, 2.0]
      >>> print(mp.chop(A * ER[:,0] - E[0] * ER[:,0]))
      [0.0]
      [0.0]
      [0.0]
      >>> print(mp.chop( EL[0,:] * A - EL[0,:] * E[0]))
      [0.0  0.0  0.0]
    realimagr   zunknown function %sr   )	r;   strr   r   r   rN   lenr   r<   )r   rp   ri   r`   r   r   r   imaxrA   r)   r@   zs               r   eig_sortry     s   L !S;A&[A%ZA4q899AA AY #adGAq! 	A!A$A1u		 19 !AT7AaDAdGb$' #A1Q3A ajBqsG!"BtAvJ#
 b$' #A1Q3A 4jBqsG!"BqvJ#1#: "d
2t 4"dJr4$82w"dJr4$82wr2;r
   N)F)FTF)FFrs   )__doc__libmp.backendr   objectr   r   r*   r,   r5   rE   rV   rX   rg   rj   rq   ry   r	   r
   r   <module>r}      s   , #	F 	e/R" L 5 5vC+NiRX 9 9x:x8t r rh Y Yr
   