K iNi6dZddlmZddlmZGddeZy)a Linear algebra -------------- Linear equations ................ Basic linear algebra is implemented; you can for example solve the linear equation system:: x + 2*y = -10 3*x + 4*y = 10 using ``lu_solve``:: >>> from mpmath import * >>> mp.pretty = False >>> A = matrix([[1, 2], [3, 4]]) >>> b = matrix([-10, 10]) >>> x = lu_solve(A, b) >>> x matrix( [['30.0'], ['-20.0']]) If you don't trust the result, use ``residual`` to calculate the residual ||A*x-b||:: >>> residual(A, x, b) matrix( [['3.46944695195361e-18'], ['3.46944695195361e-18']]) >>> str(eps) '2.22044604925031e-16' As you can see, the solution is quite accurate. The error is caused by the inaccuracy of the internal floating point arithmetic. Though, it's even smaller than the current machine epsilon, which basically means you can trust the result. If you need more speed, use NumPy, or ``fp.lu_solve`` for a floating-point computation. >>> fp.lu_solve(A, b) # doctest: +ELLIPSIS matrix(...) ``lu_solve`` accepts overdetermined systems. It is usually not possible to solve such systems, so the residual is minimized instead. Internally this is done using Cholesky decomposition to compute a least squares approximation. This means that that ``lu_solve`` will square the errors. If you can't afford this, use ``qr_solve`` instead. It is twice as slow but more accurate, and it calculates the residual automatically. Matrix factorization .................... The function ``lu`` computes an explicit LU factorization of a matrix:: >>> P, L, U = lu(matrix([[0,2,3],[4,5,6],[7,8,9]])) >>> print(P) [0.0 0.0 1.0] [1.0 0.0 0.0] [0.0 1.0 0.0] >>> print(L) [ 1.0 0.0 0.0] [ 0.0 1.0 0.0] [0.571428571428571 0.214285714285714 1.0] >>> print(U) [7.0 8.0 9.0] [0.0 2.0 3.0] [0.0 0.0 0.214285714285714] >>> print(P.T*L*U) [0.0 2.0 3.0] [4.0 5.0 6.0] [7.0 8.0 9.0] Interval matrices ----------------- Matrices may contain interval elements. This allows one to perform basic linear algebra operations such as matrix multiplication and equation solving with rigorous error bounds:: >>> a = iv.matrix([['0.1','0.3','1.0'], ... ['7.1','5.5','4.8'], ... ['3.2','4.4','5.6']]) >>> >>> b = iv.matrix(['4','0.6','0.5']) >>> c = iv.lu_solve(a, b) >>> print(c) [ [5.2582327113062568605927528666, 5.25823271130625686059275702219]] [[-13.1550493962678375411635581388, -13.1550493962678375411635540152]] [ [7.42069154774972557628979076189, 7.42069154774972557628979190734]] >>> print(a*c) [ [3.99999999999999999999999844904, 4.00000000000000000000000155096]] [[0.599999999999999999999968898009, 0.600000000000000000000031763736]] [[0.499999999999999999999979320485, 0.500000000000000000000020679515]] )copy)xrangeceZdZddZddZdZdZddZdZdZ d Z d Z d Z dd Z dd ZdZdZddZdZddZy)LinearAlgebraMethodsc ~|j|jk(s td|r.t||jr|j r |j S|s|}|j }|j|j|d|jz}|j}dg|dz z}t|dz D]-}d} t||D]} |jt||D cgc]} |j|| | fc} } |j| |kr tdd| z |j|| |fz} | | kDs| } | ||<|j|||||j|||f|kr tdt|dz|D]F}|||fxx|||fzcc<t|dz|D]} ||| fxx|||f||| fzzcc< H0|j||dz |dz f|kr td|st|jr ||f|_||fScc} w)z LU-factorization of a n*n matrix using the Gauss algorithm. Returns L and U in one matrix and the pivot indices. Use overwrite to specify whether A will be overwritten with L and U. need n*n matrixNrmatrix is numerically singular)rowscols ValueError isinstancematrix_LUrabsminmnormepsrfsumZeroDivisionErrorswap_row)ctxA overwrite use_cacheorigtolnpjbiggestklscurrentis \/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/mpmath/matrices/linalg.py LU_decompzLinearAlgebraMethods.LU_decompnsEvv./ / Aszz2quu55LDAjj1Q#''12 FF FAENA ,AGAq\ HH&A,GQcjj1Q30GH::a=C'+,LMMA# 1QqS6 22W$%GAaD  LLAqt $zz!AaC&!S('(HIIAE1% ,!A#!AaC& Aq),AacFa!fQqsVm+F, ,! ,( ::aAa!e n % ,#$DE EZcjj91vDH!t +Hs(H: Nc|j|jk7r td|j}t||k7r t dt |}|r0t dt|D]}|j||||t d|D]*}t |D]}||xx|||f||zzcc<,|S)zG Solve the lower part of a LU factorized matrix for y. r Value should be equal to nrr )r r RuntimeErrorlenrrrr)rLbrrr"r&r s r'L_solvezLinearAlgebraMethods.L_solves 66QVV 01 1 FF q6Q;9: : G As1v& ) Q1Q4( )1 &AAY &!!A#1 % & &cT|j|jk7r td|j}t||k7r t dt |}t |dz ddD]@}t |dz|D]}||xx|||f||zzcc<||xx|||fzcc<B|S)zG Solve the upper part of a LU factorized matrix for x. r r*r )r r r+r,rrr)rUyrxr&r s r'U_solvezLinearAlgebraMethods.U_solves 66QVV 01 1 FF q6Q;9: : GAr2& AAE1% &!!A#1 % & aDAacFND r0c j} xjdz c_j|fi|jj|fi|j}}|j|jkr t d|j|jkDrb|j }||z}||z}|jddstfd|Dsj||}nLj||}n9j|\}}j|||}j||}|_|S#|_wxYw)a Ax = b => x Solve a determined or overdetermined linear equations system. Fast LU decomposition is used, which is less accurate than QR decomposition (especially for overdetermined systems), but it's twice as efficient. Use qr_solve if you want more precision or have to solve a very ill- conditioned system. If you specify real=True, it does not check for overdeterminded complex systems. #cannot solve underdetermined systemrealFc3LK|]}t|juywNtypempc).0r&rs r' z0LinearAlgebraMethods.lu_solve..s:1DGsww.:!$)precrrr r rHgetsumcholesky_solvelu_solver(r/r6)rrr.kwargsrCAHr5rs` r'rHzLinearAlgebraMethods.lu_solves-xx  HHNH3::a*6*//1:3::a3J63J3O3O3QqAvv !FGGvvSSFFJJvu-:::**1a0A Q*A}}Q'1KK1a(KK1%CHCHs D/E Ec|j|jk7r tdt|D]R}|j |||}|j |dd|j zkr|S|j|| }||z }T|S)z Improve a solution to a linear equation system iteratively. This re-uses the LU decomposition and is thus cheap. Usually 3 up to 4 iterations are giving the maximal improvement. r rr8)r r r+rresidualnormrrH)rrr5r.maxsteps_rdxs r'improve_solutionz%LinearAlgebraMethods.improve_solutions 66QVV 01 1! A Q1%Axx1~377 *a!$B GA  r0c|j|\}}|j}|j|}|j|}t|D]I}t|D]9}||kDr |||f|||f<||k(rd|||f<|||f|||f<.|||f|||f<;K|j |}tt |D]} |j || || |||fS)a7 A -> P, L, U LU factorisation of a square matrix A. L is the lower, U the upper part. P is the permutation matrix indicating the row swaps. P*A = L*U If you need efficiency, use the low-level method LU_decomp instead, it's much more memory efficient. r )r(r rreyer,r) rrrrr-r3r&r Pr"s r'luzLinearAlgebraMethods.lus}}Q1 FF JJqM JJqM $AAY $q5qsVAacF!VAacFqsVAacFqsVAacF $ $ GGAJA %A LLAqt $ %!Qwr0cd|cxkr |ksJdJd|jg|dz z|jgz|jg||z zzS)z< Return the i-th n-dimensional unit vector. rzthis unit vector does not existr )zeroone)rrr&s r' unitvectorzLinearAlgebraMethods.unitvectorsY1zz<<>>r0c h|j} |xjdz c_|j|fi|j}|j}|j |\}}g}t d|dzD]H}|j ||}|j|||} |j|j|| Jg} t |D]<}g} t |D]} | j|| || j| >|j| fi|} ||_| S#||_wxYw)z Calculate the inverse of a matrix. If you want to solve an equation system Ax = b, it's recommended to use solve(A, b) instead, it's about 3 times more efficient. r8r ) rCrrr r(rrZr/appendr6)rrrIrCrrr r&er4invrowr results r'inversezLinearAlgebraMethods.inverse s/xx  HHNH 1'',,.AA==#DAqDAq1u% /NN1a(KK1a( CKK1-. / CAY +AJJtAwqz*+ 3   SZZ.v.FCH CHs DD(( D1c  tjs tdj}j}||dz kr t dg}t d|dz D]' j fdt |D}t|jkDs td|jjj f j|zj||  fzz z } fxx| zcc<t dz|D]T j fdt |D|z}t |D] fxx f|zzcc<V*t |dz Dcgc] }||dz fc} t |dz d d D]I xxj fd t dz|dz Dzcc< xx|zcc<K||dz k(s-t ||z dzDcgc]}|dz |z |dz f} }ndg|z} | | fScc}wcc}w) a (A|b) -> H, p, x, res (A|b) is the coefficient matrix with left hand side of an optionally overdetermined linear equation system. H and p contain all information about the transformation matrices. x is the solution, res the residual. A should be a type of ctx.matrixr z$Columns should not be less than rowsrc3DK|]}t|fdzywrN)absr@r&rr s r'rAz3LinearAlgebraMethods.householder..Ss!>AQqsVa>s r c3ZK|]"}j|f|fz$ywr<conjr@r&rrr r"s r'rAz3LinearAlgebraMethods.householder..Zs/M1SXXa!f-!A#6M(+rr2c38K|]}|f|zywr<)r@r rr&r5s r'rAz3LinearAlgebraMethods.householder..`s!IqQqsVad]I)rr TypeErrorr r r+rrrfrrr\signresqrtrY) rrmrrr$kappar4r&rPr r"r5s `` ` @@@r' householderz LinearAlgebraMethods.householderAs[!SZZ(>? ? FF FF q1u9EF F 1q5! )A>1>>Aq6CGG# !ABB HHchhsvva!f~..!< =GGq1Q4!AaC&=01E acFadNFAaC^ )HHMq! MMPUU1)AacFa!fqj(F) ) )"(A /AQqQwZ /Ar2& A aDCHHIF1q5!a%4HII ID aDAaDLD AEz(.q1uqy(9:11Q3q5!A#::A:AA!Qz 0 ;s I*Ic |j} |xjdzc_|j|fi||j|fi||j|fi|}}}||z|z ||_S#||_wxYw)zt Calculate the residual of a solution to a linear equation system. r = A*x - b for A*x = b r)rCr)rrr5r.rIoldprecs r'rLzLinearAlgebraMethods.residualvsx ((  HHMH cjj-f-zszz!/Fv/F  STH_X^H_!qAQ37CHwCHs AA++ A4c :| |j}|j} |xjdz c_|j|fi|j|j|fi|j}}|j|j kr t d|j|j||\}}}} |j| } | dk(r"|j|j|||} |j|fi|| f||_S#||_wxYw)aa Ax = b => x, ||Ax - b|| Solve a determined or overdetermined linear equations system and calculate the norm of the residual (error). QR decomposition using Householder factorization is applied, which gives very accurate results even for ill-conditioned matrices. qr_solve is twice as efficient. r8r9r) rMrCrrr r rrvextendrL) rrr.rMrIrCrDrr5rPress r'qr_solvezLinearAlgebraMethods.qr_solves <88Dxx  HHNH3::a*6*//1:3::a3J63J3O3O3QqAvv !FGGAq)9:JAq!Q((1+Caxhhs||Aq!453::a*6*C/CHtCHs C-D Dc t||js td|j|jk(s t d| |j }|j}|j| t|D] |j| f}t|| fz |kDr t d||j fdt Dddz }||kr t d|j| f<t |D]W fdt D} fd t D}|j||d }| f|z fz f<Y S) a} Cholesky decomposition of a symmetric positive-definite matrix `A`. Returns a lower triangular matrix `L` such that `A = L \times L^T`. More generally, for a complex Hermitian positive-definite matrix, a Cholesky decomposition satisfying `A = L \times L^H` is returned. The Cholesky decomposition can be used to solve linear equation systems twice as efficiently as LU decomposition, or to test whether `A` is positive-definite. The optional parameter ``tol`` determines the tolerance for verifying positive-definiteness. **Examples** Cholesky decomposition of a positive-definite symmetric matrix:: >>> from mpmath import * >>> mp.dps = 25; mp.pretty = True >>> A = eye(3) + hilbert(3) >>> nprint(A) [ 2.0 0.5 0.333333] [ 0.5 1.33333 0.25] [0.333333 0.25 1.2] >>> L = cholesky(A) >>> nprint(L) [ 1.41421 0.0 0.0] [0.353553 1.09924 0.0] [0.235702 0.15162 1.05899] >>> chop(A - L*L.T) [0.0 0.0 0.0] [0.0 0.0 0.0] [0.0 0.0 0.0] Cholesky decomposition of a Hermitian matrix:: >>> A = eye(3) + matrix([[0,0.25j,-0.5j],[-0.25j,0,0],[0.5j,0,0]]) >>> L = cholesky(A) >>> nprint(L) [ 1.0 0.0 0.0] [(0.0 - 0.25j) (0.968246 + 0.0j) 0.0] [ (0.0 + 0.5j) (0.129099 + 0.0j) (0.856349 + 0.0j)] >>> chop(A - L*L.H) [0.0 0.0 0.0] [0.0 0.0 0.0] [0.0 0.0 0.0] Attempted Cholesky decomposition of a matrix that is not positive definite:: >>> A = -eye(3) + hilbert(3) >>> L = cholesky(A) Traceback (most recent call last): ... ValueError: matrix is not positive-definite **References** 1. [Wikipedia]_ http://en.wikipedia.org/wiki/Cholesky_decomposition rcr zmatrix is not Hermitianc3,K|] }|f ywr<rnr@r"r-r s r'rAz0LinearAlgebraMethods.cholesky..s8a!f8T)absolutesquaredzmatrix is not positive-definitec3,K|] }|f ywr<rn)r@r"r-r&s r'rAz0LinearAlgebraMethods.cholesky..1!q1v1rc3,K|] }|f ywr<rnrs r'rAz0LinearAlgebraMethods.cholesky..rr) conjugate) rrr+r r rrrrrrfrrsfdot) rrrrcr$it1it2tr-r&r s @@@r'choleskyzLinearAlgebraMethods.choleskyss|!SZZ(AB Bvv./ / ;77(C FF JJqM /Aq1vA1QqsV8}s" !:;;CHH8fQi8t--A3w !BCCXXa[AacFAq\ /1vay11vay1HHS#H6AaC&1*!A#.!A#  / /r0c |j} |xjdz c_|j|fi|j|jfi|jc}|j|jk7r t d|j |j}t|k7r t dt|D]Dxx|jfdtDzcc<xxfzcc<F|jj}|||_S#||_wxYw)z Ax = b => x Solve a symmetric positive-definite linear equation system. This is twice as efficient as lu_solve. Typical use cases: * A.T*A * Hessian matrix * differential equations r8z can only solve determined systemr*c38K|]}|f|zywr<rn)r@r r-r.r&s r'rAz6LinearAlgebraMethods.cholesky_solve..s! B11Q3!A$ Bro) rCrrr r rrr,rrr6T) rrr.rIrCrr5r-r&s ` @@r'rGz#LinearAlgebraMethods.cholesky_solvesxx  HHNH3::a*6*//1:3::a3J63J3O3O3QDAqvv!&&  !CDD QAA1v{ !=>>AY ! Bq BBB!!A#  ACC#ACHtCHs D!D:: Ecb|j} |j|j} |j|\}}d}t |D]\}}||k7s |dz}t |jD] }||||fz}|||_S#t$r Y||_ywxYw#||_wxYw)z8 Calculate the determinant of a matrix. rr r2)rCrrr(r enumeraterr )rrrCRrzr&r]s r'detzLinearAlgebraMethods.detsxx  1 ""$A }}Q'1A!!  16GA AFF^ QqsV  CH% CH CHs4B%BB%,B% B"B%!B""B%% B.cR|fd}|||j|zS)a) Calculate the condition number of a matrix using a specified matrix norm. The condition number estimates the sensitivity of a matrix to errors. Example: small input errors for ill-conditioned coefficient matrices alter the solution of the system dramatically. For ill-conditioned matrices it's recommended to use qr_solve() instead of lu_solve(). This does not help with input errors however, it just avoids to add additional errors. Definition: cond(A) = ||A|| * ||A**-1|| c(j|dS)Nr )r)r5rs r'z+LinearAlgebraMethods.cond..=sSYYq^r0)ra)rrrMs` r'condzLinearAlgebraMethods.cond.s+ <+DAwckk!n---r0c|j|j|j}t|jD]F}|j ||j |}tt |D] }|||||f<H|S)z,Solve a * x = b where a and b are matrices.)rr r rangerHcolumnr,)rar.rPr&rr s r' lu_solve_matz!LinearAlgebraMethods.lu_solve_mat@sx JJqvvqvv &qvv A Q ,A3q6] A$!Q$  r0c  tjsJj}j}|dk\sJ||k\sJ|dk\sJt fdD}j |5j|d}j |rjdd}jdd} jd} td|D]f} j| } j| } |z dk\rJjfdtdz|D}jj|}n| }|| k(r | | k(r| |<| | kr!j| dz| dzz|dzz}n!j| dz| dzz|dzz }j|| z |z | |z |<j| }|| |z z }tdz|D]}|fxx|zcc<|f<tdz|D]ejfdt|D}|j|z}t|D]}|fxx|f|zz cc<gj|df<njd}jd} td|D]af} |z dkDr:jfd tdz|D}j|}n|z dk(rt|dz f}n| }|| k(r| |<v| | krj| dz|dzz}nj| dz|dzz }|| z |z |<| }|| |z z }tdz|D]}|fxx|zcc<|f<tdz|D]Ujfd t|D}||z}t|D]}|fxx|f|zz cc<W|f<d|d k(s|d k(r |fcd d d Sj }td|D]tdz|D] }| ||f< |}|dk(s|dk(r|}xj||z z c_td|D]!|f<tdD] }| |f< #t|dz ddD]| }fxx|z cc<tdz|D]|r?jfdtdz|D}|j|z}n.jfdtdz|D}||z}|f<tdz|D]}|fxx|f|zz cc<tdz|D]}|fxx|zcc<|d|d|ffcd d d S#1swYy xYw)al Compute a QR factorization $A = QR$ where A is an m x n matrix of real or complex numbers where m >= n mode has following meanings: (1) mode = 'raw' returns two matrixes (A, tau) in the internal format used by LAPACK (2) mode = 'skinny' returns the leading n columns of Q and n rows of R (3) Any other value returns the leading m columns of Q and m rows of R edps is the increase in mp precision used for calculations **Examples** >>> from mpmath import * >>> mp.dps = 15 >>> mp.pretty = True >>> A = matrix([[1, 2], [3, 4], [1, 1]]) >>> Q, R = qr(A) >>> Q [-0.301511344577764 0.861640436855329 0.408248290463863] [-0.904534033733291 -0.123091490979333 -0.408248290463863] [-0.301511344577764 -0.492365963917331 0.816496580927726] >>> R [-3.3166247903554 -4.52267016866645] [ 0.0 0.738548945875996] [ 0.0 0.0] >>> Q * R [1.0 2.0] [3.0 4.0] [1.0 1.0] >>> chop(Q.T * Q) [1.0 0.0 0.0] [0.0 1.0 0.0] [0.0 0.0 1.0] >>> B = matrix([[1+0j, 2-3j], [3+j, 4+5j]]) >>> Q, R = qr(B) >>> nprint(Q) [ (-0.301511 + 0.0j) (0.0695795 - 0.95092j)] [(-0.904534 - 0.301511j) (-0.115966 + 0.278318j)] >>> nprint(R) [(-3.31662 + 0.0j) (-5.72872 - 2.41209j)] [ 0.0 (3.91965 + 0.0j)] >>> Q * R [(1.0 + 0.0j) (2.0 - 3.0j)] [(3.0 + 1.0j) (4.0 + 5.0j)] >>> chop(Q.T * Q.conjugate()) [1.0 0.0] [0.0 1.0] rc3LK|]}t|juywr<r=)r@r5rs r'rAz*LinearAlgebraMethods.qr..s21DGsww&2rBr z1.0z0.0rc3ZK|]"}|fj|fz$ywr<ri)r@r&rrr s r'rAz*LinearAlgebraMethods.qr..s/([a!AaC&!AaC&1A*A([rlc3ZK|]"}|fj|fz$ywr<rirks r'rAz*LinearAlgebraMethods.qr..s/$U1QqsVchhq1v.>%>$Urlc32K|]}|fdzywrernrgs r'rAz*LinearAlgebraMethods.qr..s(O!1QqS6A+(Osc3<K|]}|f|fzywr<rnr@r&rr r"s r'rAz*LinearAlgebraMethods.qr..s%$M!a!fq1vo$MrawRAWNskinnySKINNYr2c3ZK|]"}|fj|fz$ywr<rirks r'rAz*LinearAlgebraMethods.qr..s/$W1QqsVchhq1v.>%>$Wrlc3<K|]}|f|fzywr<rnrs r'rAz*LinearAlgebraMethods.qr.. s%$MQqsVa!f_$Mr)rrr r anyextradpsrr?mpfrrrimrrsrjrf)rrmodeedpsrtrcmplxtaurYrXrzeroalphaalphralphixnormbetarzar&r4tempdarrr r"s`` @@r'qrzLinearAlgebraMethods.qrIsp!SZZ((( FF FFAv vAv vqyy222\\$ F !**Qq/CA ggeU+wwue, 1"2AacFEFF5MEFF5ME!z #([6RSTURUWX>([ [ # 9 %Ue^!%A u}"xxq5!8(;eQh(FG #E1H)rs"`N"f !6f !r0