# import libraries import numpy as np # create a function to exchange rows def RwColexchange(A1,b1): M,N = A1.shape flag = 0 for i in range(1,M): # check if 1st element of row is non zero if A1[i,0] != 0: # note: list function is used to create new copy of list. temp_v = list(A1[0,:]) # create temp varibale to store 1st row temp_u = list(b1[0]) A1[0,:] = A1[i,:] # row exchange A1[i,:] = temp_v # exchange with temp variable b1[0] = b[i] b1[i] = temp_u flag += 1 break # break the loop after one-row exchange return A1, b1, flag # create function for row echelon def Rwechelon(A1,b1): (M,N) = A1.shape for i in range(1,M): if A1[i,0] != 0: a = A1[i,0]/A1[0,0] A1[i,:] = A1[i,:] - a*A1[0,:] b1[i] = b1[i] - a*b1[0] # multiplication factor # row echelon if A1[0,0] != 1: b1[0] = b1[0]/A1[0,0] # first divide b otherwise 1st element changes A1[0,:] = A1[0,:]/A1[0,0] # make pivot elememt to 1 return A1, b1 # reduced row echelon form def rref(A,b,P): M,N = A.shape index = 0 for j in P: for i in range(M-1,-1,-1): if (j!=0) and (i!=0): if (A[i,j]!=0): index = i break for i in range(index): a = A[i,j]/A[index,j] A[i,:] = A[i,:] - a*A[index,:] b[i] = b[i] - a*b[index] return A, b # create a function for solving Ax = b. def AxbSlv(A,b): (M,N) = A.shape # get shape of Matrix "A" i=0 j=0 fr_var = [] # free variable pv_var = [] # pivot variable A2 = A b2 = b while (i<M) and (j<N): A1 = A[i:M, j:N] b1 = b[i:M] if A1[0,0] == 0: # check 1st element of row A1, b1, flag = RwColexchange(A1,b1) # row exchange # if entire row is zero, store j value in free variable if flag == 0: fr_var.append(j) j += 1 continue else: pv_var.append(j) else: pv_var.append(j) A1, b1 = Rwechelon(A1,b1) # row echelon A2[i:M,j:N] = A1 # change a section of matrix to get reduced matrix b2[i:M] = b1 i += 1 # go to next diagonal element j += 1 A2, b2 = rref(A2,b2, pv_var) # reduced row echelon form return A2, b2, fr_var, pv_var # back substitution method # null solution def null(A,p,f): M,N = A.shape null_matrix = np.zeros((N, len(f))) for i,c in enumerate(f): null_vector = np.zeros(N) null_vector[c] = 1 for j in range(len(p)-1,-1,-1): p_col = p[j] for k in range(p_col + 1, N): null_vector[p_col] -= A[j, k] * null_vector[k] # Assign the null space vector to the null space matrix null_matrix[:, i] = null_vector return null_matrix # particular solution def particular(A,b,p,f): M,N = A.shape b = b.astype(float) par_vector = np.zeros(N) for j in range(len(p)-1,-1,-1): p_col = p[j] for k in range(p_col + 1, N): b[j] -= A[j, k] * par_vector[k] par_vector[p_col] = b[j] return par_vector def Solution(A,b,p,f): N = null(A,p,f) P = particular(A,b,p,f) return N, P # input matrix A and vector b A = np.array([[1, 3, 3, 2], [2, 6, 9, 7,], [-1, -3, 3, 4,],]) b = np.array([[1],[5],[5],]) print(A) print(b) [[ 1 3 [ 2 6 [-1 -3 [[1] [5] [5]] 3 9 3 2] 7] 4]] # get the Solution of Ax = b C, d ,Fv ,Pv = AxbSlv(A, b) Xp, Xn = Solution(C, d, Pv, Fv) print("A ="+str(C)) print("b ="+str(d)) print("Free variable ="+str(Fv)) print("Pivot variable ="+str(Pv)) print(f"Xp = {Xp}") print(f"Xn = {Xn}") A =[[ 1 3 0 -1] [ 0 0 1 1] [ 0 0 0 0]] b =[[-2] [ 1] [ 0]] Free variable =[1, 3] Pivot variable =[0, 2] Xp = [[-3. 1.] [ 1. 0.] [ 0. -1.] [ 0. 1.]] Xn = [-2. 0. 1. 0.]