K iJ_dZddlmZGddeZdZdZdZeddZd Z d Z edd Z d Z d Z eddZeddZy)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 eZdZy)EigenN)__name__ __module__ __qualname__[/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/mpmath/matrices/eigen.pyrr&sr rc<tt|j||S)N)setattrrr)fs r defunr)s E1::q! Hr c |j}|dkryt|dz ddD]}d}td|D]D}|t|j|||ft|j |||fzz }Fd}|dk7rd|z }|dk(s|j |rd||<d|||dz f<d}td|D]K}|||fxx|zcc<|j|||f} |j |||f} || | z| | zzz }M|||dz f} t| } |j |} | |z|||dz f<| dk(r| ||<n"| | z }| | |zz||<|||dz fxx|zcc<|| | zz }d|j |z }||xx|zcc<td|dz D]}|||fxx|zcc<td|D]}|j|||||dz fz} td|dz D]#}| |j|||f|||fzz } %|||dz fxx| ||zzcc<td|dz D]}|||fxx| |||fzzcc<td|D]}||||dz |fz} td|dz D]}| |||f|||fzz } ||dz |fxx| |j||zzcc<td|dz D](}|||fxx| |j|||fzzcc<*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. rN)rowsrabsreimisinfsqrtconj)ctxATniscalek scale_invHrriiFrGffjs r hessenberg_reduce_0r*-s@ AAvv AaCB B/1 ?A S!A#(3svva!f~+>> >E ? A:E I A:9-AaDAa!eH  1 #A acFi F!A#B!A#B b27" "A  # a!eH F HHQK3;!AaC% 6AaDQBq2v:AaD a!eHNH QU   O ! 1q5! A acFaKF 1 %A11QqS5)AAqs^ /SXXa!f%!A#.. / a!eHAaD HAqs^ %!A#!a!f*$ % %1 /A!q1QxAAqs^ %QqsVa!f_$ % ac!eHCHHQqTN* *HAqs^ /!A#!chhq1v... / /sB/r c (|j}|dk(rd|d<ydx|d<|d<dx|d<|d<td|D]}||dk7rtd|D]}||||dz |fz}td|dz D]}||||f|||fzz }||dz |fxx||j||zzcc<td|dz D](}|||fxx||j|||fzzcc<*d|||f<td|D]}dx|||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)rrN)rrr)rr)rrr)rrr)rrrrrr)r'r!s r hessenberg_reduce_1r,sq AAv#AcFQsVAcFQsV Aq\  Q419Aq\ 3aD1QqSU8O1Q3)A1Q3!AaC&(A)!A#a%A1..1Q33AacFa#((1QqS6"222F3  3!A#1 A AacFQqsV  r cH|j}|dk(r|jdgg|fS|s|j}|j|d}t||||j}t |||t |D]}t |dz|D] }d|||f< ||fS)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) rrr)rmatrixcopyr*r,rrr overwrite_arrQxys r hessenbergr5sH AAv QC5!1%%  FFH 1aAQ" AQ" AY!Q AAacF  a4Kr c F|j}|||f|z }||dz|f}|j|j|j||j||j|j||j|} | dk(rd} d}d}n || z}|| z}|j |} |j |} t ||D]6} ||| f} ||dz| f}| | z| |zz||| f<||z|| zz ||dz| f<8t t ||dzD]6} || |f} || |dzf}|| z||zz|| |f<| |z| | zz || |dzf<8t|tsDt |D]6} || |f} || |dzf}|| z||zz|| |f<| |z| | zz || |dzf<8t ||dz D]}||dz|f}||dz|f}|j|j|j||j||j|j||j|} | dk(rd||dz|f<d} d}d}n| ||dz|f<|| z}|| z}d||dz|f<|j |} |j |} t |dz|D]<} ||dz| f} ||dz| f}| | z| |zz||dz| f<||z|| zz ||dz| f<>t dt ||dzD]<} || |dzf} || |dzf}|| z||zz|| |dzf<| |z| | zz || |dzf<>t|trt d|D]<} || |dzf} || |dzf}|| z||zz|| |dzf<| |z| | zz || |dzf<>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 rrrN) rhypotrrrrmin isinstancebool)rn0n1rr2shiftrcsvcccsr!r3r4r)s r qr_steprEsxV A "r' UA "Q$r' A #))CFF1Isvvay1399SVVAYq 3RSAAv    Q Q !B !B B]" b1fI bd1fIFR!VO"q& EAEM"Q$q& "CBqDM "$ adI a1fIEAEM!B$ FR!VO!BqD& $ a  (A!B$ A!BqD& AAA AadIQaAa1fI  (BQ ++ ac!eH ac!eH IIciiq 366!95syyCFFSTI7V W 6Aac!eHAAAAac!eH FA FA!A#a% XXa[ XXa[!Q %A!A#a%A!A#a%AAvQAac!eH1uq1u}Aac!eH  %3r1Q3<( 'A!AaC%A!AaC%A1uq1u}Aa!eHAvQAa!eH  '!T"Aq\ +a!eHa!eHq51q5=!AaC%6BF?!AaC%  +M++r c |j}d}t|D]U}tt|dz|D]8}||j|||fdz|j |||fdzzz }:W|j ||z }|dk(ryd}|}|j d|zz } |jdz} dx} } |} | dz|krt|j|| | ft|j || | fzt|j|| dz| dzfzt|j || dz| dzfz}|| |zkr|}t|| dz| f| |zkrn| dz } | dz|kr| dz|kr(d|| dz| f<| dz}d} |dz|k\rWd}| dz}|dkrJy| dzdk(r||dz |dz f}n| dzd k(rt||dz |dz f}n| dzd k(r|}n||dz |dz f||dz |dz fz}||dz |dz f||dz |dz fz dzd||dz |dz fz||dz |dz fzz}|j|dkDr|j |}n|j | d z}||zdz }||z dz }t||dz |dz f|z t||dz |dz f|z kDr|}n|}| dz } | dz } t||||||| | kDrtd | zB) 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. rrNdr8r y?z%qr: failed to converge after %d steps) rrr:rrrepsdpsrrE RuntimeError)rrr2rnormr3r4r=r>rLmaxitsitstotalitsr!rAr?tabs r hessenberg_qrrVs& A D AY>AaC $ >A CFF1QqS6Na'#&&1Q3.A*== =D >> 88D>A D qy B B ''S1W C WWq[FC(  !ebjCFF1QqS6N#c#&&1Q3.&99Cq1QqSz@R>> 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. rrr)rr.r/r*r,rrVr0s r schurrXsL AAv QC5!1%%  FFH 1aAQ" AQ" AYAq! AAacF #q! a4Kr c|j}|j|}|j}|j|j|j dz}|||z z}d|j |z }d}td|D]} || | f} t|t| z|} t| dz ddD]} d} t| dz| dzD]}| || |f||| fzz } || | f| z }t|| kr| }| |z } | || | f<t|t| }||kDspt| | dzD]}||| fxx|zcc<d}|dk7std| dzD]}||| fxx|zcc<|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 rHrrr reyerLldexponeprecrrmaxr)rrrERrLunflsmlnumsiminrmaxrrAsminr)rr!rSs r eig_tr_rrg(s A B ''C 99SWWsxxi"n -DQW F  E D Aq\  acF3Q<(Ar2& AAAE1q5) &QqsVb1g%% &!A# A1v}QABqsGtSV$De|1Q3$AqsGtOG$# & 19Aq1u% 1Q34 3 8 Ir c|j}|j|}|j}|j|j|j dz}|||z z}d|j |z }d}td|dz D]} || | f} t|t| z|} t| dz|D]} d} t| | D]}| || |f||| fzz } || | f| z }t|| kr| }| |z } | || | f<t|t| }||kDsjt| | dzD]}|| |fxx|zcc<d}|dk7st| |D]}|| |fxx|zcc<|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 rHrrrZ)rrrELrLrarbrcrdrrArer)rfr!rSs r eig_tr_lrjds A B ''C 99SWWsxxi"n -DQW F  E D Aq1u   acF3Q<(Aq! AAAq\ &R!Wq1v%% &!A# A1v}QABqsGtSV$De|1q5)$AqsGtOG$# & 19Aq\ 1Q34 3 8 Ir c|j}|dk(re|r|s|dg|jdggfS|r|s|dg|jdggfS|dg|jdgg|jdggfS|s|j}|j|d}t ||||s|r|j}t |||nd}t |D]}t |dz|D] } d|| |f< t|||t |D cgc]} d} } t |D] } || | f| | <|s|s| S|rt||} | |jz} |rt||} || z} |r|s|  fS|r|s|  fS|   fScc} 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 rrFr) rr.r/zerosr*r,rrVrjtranspose_conjrg)rrleftrightr1rrr2r3r4rErir`s r eigrqs@ AAv qTFCJJu-. . $qTFCJJu-. .1 QC5)3::se+<==  FFH !QAQ" u FFHCA&  AYAq! AAacF #q!1IqA AY1v! E c1  !""$ $ c1  V U2w d2w r2;+ s Fc8t|tr>|dk(r |j}n,|dk(r |j}n|dk(rt}nt d|zt |}t|D]}|}|||}t|dz|D]} ||| } | |ks| }| }||k7s?||} ||||<| ||<t|ts*t|D]} ||| f} ||| f||| f<| ||| f<t|trt|D]} || |f} || |f|| |f<| || |f<t|trt|tr|St|trt|ts||fSt|trt|ts||fS|||fS)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] realimagrzunknown function %sr) r;strrrrrNlenrr<) rrprir`rrrimaxrAr)r@zs r eig_sortrysL!S ;A &[A %ZA4q89 9 AAAY# adGAq! A!A$A1u   19!AT7AaDAdGb$'#A1Q3A ajBqsG!"BtAvJ# b$'#A1Q3A 4jBqsG!"BqvJ#1#:"d 2t 4"dJr4$82w"dJr4$82w r2;r N)F)FTF)FFrs)__doc__ libmp.backendrobjectrrr*r,r5rErVrXrgrjrqryr r r r}s,# F  e/R" L55vC+NiRX99x:x8trrhYYr