''' mbinary ######################################################################### # File : solve-linear-by-iteration.py # Author: mbinary # Mail: zhuheqin1@gmail.com # Blog: https://mbinary.xyz # Github: https://github.com/mbinary # Created Time: 2018-10-02 21:14 # Description: ######################################################################### ''' ''' ######################################################################### # File : solve-linear-by-iteration.py # Author: mbinary # Mail: zhuheqin1@gmail.com # Blog: https://mbinary.xyz # Github: https://github.com/mbinary # Created Time: 2018-05-04 07:42 # Description: ######################################################################### ''' import numpy as np from operator import le,lt def jacob(A,b,x,accuracy=None,times=6): ''' Ax=b, arg x is the init val, times is the time of iterating''' A,b,x = np.matrix(A),np.matrix(b),np.matrix(x) n,m = A.shape if n!=m:raise Exception("Not square matrix: {A}".format(A=A)) if b.shape !=( n,1) : raise Exception('Error: {b} must be {n} x1 in dimension'.format(b = b,n=n)) D = np.diag(np.diag(A)) DI = np.zeros([n,n]) for i in range(n):DI[i,i]= 1/D[i,i] R = np.eye(n) - DI * A g = DI * b print('R =\n{}'.format(R)) print('g =\n{}'.format(g)) last = -x if accuracy != None: ct=0 while 1: ct+=1 tmp = x-last last = x mx = max ( abs(i) for i in tmp) if mx