Numerical analysis of the Beihang Operation II

Source: Internet
Author: User
Tags cmath

ImportCmath fromMathImport*SGN=LambdaX:1ifX > 0Else-1#the problem of the Father's assignment! Note Subscript starting from 1definit (): A= [[0 forIinchRange (10)] forIinchRange (10)]    forIinchRange (10):       forJinchRange (10): II, JJ= i + 1, j + 1A[i][j]= sin (0.5 * II + 0.2 * JJ)ifII! = JJElse1.52 * COS (ii + 1.2 *JJ)returnA#Matrix TransposedefTransposition (A): ans= [[0 forIinchRange (len (A))] forJinchRange (len (a[0))] forIinchRange (len (A)): forJinchRange (len (a[i)): Ans[j][i]=A[i][j]returnans#matrix and matrix multiplicationdefMatrix_mul_matrix (A, B):assertLen (a[0]) = =Len (B) ans= [[0] * len (b[0]) forIinchRange (len (A))] forIinchRange (len (ans)): forJinchRange (len (ans[i)): Ans[i][j]= SUM ([a[i][k] * B[k][j] forKinchRange (len (B))]) returnans#multiplication of matrices and vectorsdefMatrix_mul_vector (A, b):assertLen (a[0]) = =Len (b)return[Sum ([a[i][j] * B[j] forJinchRange (len (b))]) forIinchRange (len (A))]#vector and vector multiplicationdefVector_mul_vector (A, B):assertLen (A) = =Len (B)returnSUM ([a[i] * B[i] forIinchRange (len (A))])#vector divided by constant kdefDiv (V, k):return[V[i]/k forIinchRange (len (v))]#on the proposed triangulation of matricesdefquasi_upper_triangular (A): forIinchRange (0, Len (A)-2):#i = number of columns      if  notAny ([a[j][i] forJinchRange (i + 2, Len (A))]):ContinueD= sqrt (sum ([a[j][i] * * 2 forJinchRange (i + 1, Len (A))]) C=-SGN (A[i + 1][i]) *D H= c * (c-a[i + 1][i]) u= [0] * (i + 1) + [A[i + 1][i]-c] + [a[j][i] forJinchRange (i + 2, Len (A))] P=Div (matrix_mul_vector (transposition (A), u), h) Q=Div (Matrix_mul_vector (A, u), h) T= Vector_mul_vector (P, u)/h W= [Q[i]-T * u[i] forIinchRange (len (q))] forIinchRange (len (A)): forJinchRange (len (a[i)): A[i][j]= A[i][j]-w[i] * U[j]-u[i] *P[j]#Print MatrixdefPrintmatrix (a,s):Print(s) forIinchRange (len (A)): forJinchRange (len (a[i)):Print('%.3f'% a[i][j],' ', end=',')      Print()   Print("=========")defQR (B, C): forIinchRange (len (B)-1):      if  notAny ([b[j][i] forJinchRange (i + 1, len (B))]):ContinueD= sqrt (sum ([b[j][i] * * 2 forJinchRange (i, Len (B)))) C=-SGN (B[i][i]) *D H= c * (C-b[i][i]) u= [0] * (i) + [b[i][i]-c] + [b[j][i] forJinchRange (i + 1, Len (B))] V=Div (matrix_mul_vector (transposition (B), u), h) forIinchRange (len (B)): forJinchRange (len (b[i)): B[i][j]-= u[i] *V[j] P=Div (matrix_mul_vector (transposition (C), u), h) Q=Div (Matrix_mul_vector (C, u), h) T= Vector_mul_vector (P, u)/h W= [Q[i]-T * u[i] forIinchRange (len (q))] forIinchRange (len (C)): forJinchRange (len (c[i)): C[i][j]= C[i][j]-w[i] * U[j]-u[i] *P[j]#QR decomposition with two-step displacement to find the characteristic rootdefQr_with_double_shift (A): Root= [0] *Len (A) I= Len (A)-1k= 0#Number of steps    whileI >=0:ifi = =0:root[0]=A[0][0] Break      elifABS (A[i][i-1]) <Epsilon:root[i]=A[i][i] I-= 1ContinueD1, D2, D3, D4= A[i-1][i-1], a[i-1][i], a[i][i-1], A[i][i] Delta= Cmath.sqrt ((d1 + d4) * * 2-4 * (D1 * D4-D2 *D3)) ifi = = 1orABS (A[i-1][i-2]) <Epsilon: (Root[i], root[i-1]) = (((D1 + d4) + Delta)/2, ((D1 + D4)-Delta)/2) I-= 2Continue      ifK = =L:Print("didn ' t get all of the root after {} steps". Format (L)) BreakS, t= D1 + d4, D1 * D4-D2 *D3 M=Matrix_mul_matrix (A, a) forIinchRange (len (M)): forJinchRange (len (m[0)): M[i][j]-= S *A[i][j] M[i][i]+=T-qr (M, A) k+ = 1returnRoot"""Gaussian elimination method applicable scope: Each order master type is greater than 0 column main element Gaussian elimination method applies the scope: the determinant value is greater than 0 the whole main element Gaussian elimination method applies the scope: solves the arbitrary equation, may obtain a solution space to in this question, according to the characteristic value to obtain the characteristic vector to have two methods: 1. The 0-point translation inverse power method iterates the nearest Lamda eigenvalue, and can also find the eigenvector, but it can only find a eigenvector 2. All-principal-element Gaussian elimination method, which is the most perfect"""#A feature vector of the root of a square matrix A with all-principal-element Gaussian elimination methoddefSolve (A, root): N=Len (A) forIinchrange (N): A[i][i]-=Root IND= [I forIinchrange (n)] rank=N forIinchrange (N): x, y=I, I forRowinchRange (I, N): forColinchRange (I, N):ifABS (A[x][y]) <ABS (A[row][col]): x, y=Row, colifABS (A[x][y]) < epsilon:#The maximum value is also 0, starting backRank =I Break      #swap the largest row with the current rowA[X], a[i] =A[i], a[x]#Column y and column I       forRowinchrange (N): A[row][y], A[row][i]=A[row][i], A[row][y] ind[i], Ind[y]=Ind[y], Ind[i]#A single line of unitst=A[i][i] forJinchRange (I, N): A[i][j]/=T forRowinchRange (i + 1, N):#Line J-a[j][i] Timest =A[row][i]ifT==0:Continue          forColinchRange (I,n): A[row][col]-=t*A[i][col]#The process of generation back    forRowinchRange (Rank-1,-1,-1):       forJinchRange (row + 1, N):#Row J of line I-a[i][j] times         #It is important to note that A[i][j must be saved with T, otherwise a[i][j] becomes 0.t =A[row][j] forKinchRange (j, N): A[row][k]-= T *A[j][k]#construct eigenvector space, be sure to pay attention to the various columns to change back!ans = [[0] * Len (A) forIinchRange (N-rank)]  forIinchRange (rank, n): forJinchrange (N): Ans[i-RANK][IND[J]] =-A[j][i] Ans[i-Rank][ind[i]] = 1returnAnsepsilon= 1e-12L= 0xFFFFFFA=init () Printmatrix (A,'the original matrix') Quasi_upper_triangular (a) Printmatrix (a),'a matrix after a quasi-diagonalization') Roots=Qr_with_double_shift (A)Print('each feature root', Roots) Printmatrix (A,'The matrix after the QR two-step displacement method ends') forIinchRoots:ifType (i) = =float:a=init () x=Solve (A, i)Print('feature Root'I'the corresponding eigenvector is \ n', x)

Numerical analysis of the Beihang Operation II

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.