Python plurality of linear equations iterative algorithm --Gauss-Seidel iterative algorithm multi-linear equations iterative algorithm iterative algorithm implemented in Python --Jacobi

The principle of multi-linear equations (tensor) iterative algorithm see here: the principle part please leave a message, not to reveal Share

Jacobi iterative algorithm, there are detailed notes: Python multi-linear equations iterative algorithm iterative algorithm to achieve --Jacobi

import numpy as np
import time

1.1 Gauss-Seidel iterative algorithm

def GaussSeidel_tensor_V2(A,b,Delta,m,n,M):
    start=time.perf_counter()
    find=0
    X=np.ones(n)
    d=np.ones(n)
    m1=m-1
    m2=2-m
    for i in range(M):
        print('X',X)
        x=np.copy(X)
        #迭代更新
        for j in range(n):
            a=np.copy(A)
            for k inRange (M2 ): 
                A = np.dot (A, X)
             for K in Range (n-): 
                D [K] = A [K, K] 
                A [K, K] = M2 * A [K, K ]             
            X [J] = (B [J] -np.dot (A [J], X)) / (M1 * D [J])
         # determines whether or not meet the accuracy requirements 
        iF np.max (np.fabs (Xx) ) < of Delta: 
            Find =. 1
             BREAK   
        X- = np.copy (X) 
    End = time.perf_counter ()
     Print ( ' time: ' , END-start)
    print('迭代',i)
    return X,find,i,end-start

1.2 tensor A generating function generating function and the vector b:

DEF Creat_A (m, n-): # generates tensor A 
    size = np.full (m, n-) 
    X- = np.ones (n-)
     the while . 1 :
         # randomly generated tensor given shape A 
        A = np.random. the randint (-49,50, size = size)
         # Analyzing Dx ** (m-2) whether the non-singular, if so, to meet the requirement, out of the loop 
        D = np.copy (A)
         for I1 in Range (n-):
             for I2 in Range (n-):
                 IF I1 =! I2: 
                    D [I1, I2] = 0
         for I in Range (m-2 ):
                D = np.dot (D, the X-) 
        DET = np.linalg.det (D)
         IF DET =! 0:
             BREAK 
    # The diagonal tensor A was extended ten times, so diagonal planes dominant 
    for I1 in the Range (n-):
         for I2 in Range (n-):
             IF I1 == I2: 
                A [I1, I2] = A [I1, I2] * 10
     Print ( ' A: ' )
     Print (A)
     return A 

# of A and according to given X Ax ** (m-1) = b generates vector B 
DEF Creat_b (a, X, m): 
    a =np.copy(A)
    for i in range(m-1):
        a=np.dot(a,X)
    print('b:')
    print(a)
    return a

1.3 S symmetric tensor generation function of:

DEF Creat_S (m, n-): # generate a symmetric tensor B 
    size = np.full (m, n-) 
    S = np.zeros (size)
     Print ( ' S ' , S)
     for I in Range (. 4 ):
         # generated n is the vector a 
        a = np.random.random (n) * np.random.randint (-5,6 ) 
        B = np.copy (a)
         # of times m 1-a for the outer product to obtain a symmetric tensor of rank an amount b 
        for J in Range (. 1-m ): 
            b = Outer (b, a)
         # different b superposition low-rank tensor S 
        S = S + b
    print('S:')
    print(S)
    return S
def outer(a,b):
    c=[]
    for i in b:
        c.append(i*a)
    return np.array(c)
    return a

1.4 second experiment

DEF test_2 (): 
    of Delta = 0.01 # precision 
    m =. 3 # order of A 
    n-=. 3 # dimension A of 
    M = 200 is # maximum number of iteration steps 
    X_real np.array = ([2,3,4 ]) 
    A = Creat_A (m, n-) 
    B = Creat_b (A, X_real, m) 
    GaussSeidel_tensor_V2 (A, B, of Delta, m, n-)

 

Guess you like

Origin www.cnblogs.com/Fengqiao/p/Gauss-Seidel-tensor.html