Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Linear Equations

A linear equation is a mathematical statement that represents a straight line when graphed.

Then let’s take input from user, in the following format,

Enter number of unknowns: 3
Enter row 1 in the format (ax + by + cz = d)
# User input (auto)
# N = int(input("Enter number of unknowns: "))

# arr = []
# for i in range(N):
#     arr.append(list(map(int, input().split())))

# User input (manual)
N = 3
arr = [[2, 1, -1, 8], [-3, -1, 2, -11], [-2, 1, 2, -3]]
# User input's output
print(N)

def print_arr(arr):
  for i in range(N):
    print(arr[i])

print_arr(arr)
3
[2, 1, -1, 8]
[-3, -1, 2, -11]
[-2, 1, 2, -3]

Gaussian Elimination

import copy

def gaussian_elimination(N, arr):
  arr = copy.deepcopy(arr)
  for i in range(N):
    for j in range(N, -1, -1):
      arr[i][j] /= arr[i][0+i]
      
    for j in range(i+1, N):  
      for k in range(N, -1, -1):
        arr[j][k] -= arr[i][k] * arr[j][0+i]

    print_arr(arr)
    print()

  solve = [0 for j in range(N)]
  for i in range(N-1, -1, -1):
    for j in range(N):
      solve[i] = arr[i][N]
      for k in range(i+1, N):
        solve[i] -= arr[i][k] * solve[k]
    solve[i] /= arr[i][i]

  print(solve)

gaussian_elimination(N, arr)
[1.0, 0.5, -0.5, 4.0]
[0.0, 0.5, 0.5, 1.0]
[0.0, 2.0, 1.0, 5.0]

[1.0, 0.5, -0.5, 4.0]
[0.0, 1.0, 1.0, 2.0]
[0.0, 0.0, -1.0, 1.0]

[1.0, 0.5, -0.5, 4.0]
[0.0, 1.0, 1.0, 2.0]
[0.0, 0.0, 1.0, -1.0]

[2.0, 3.0, -1.0]

Gauss Jordan

import copy
arr = copy.deepcopy(arr)

def gauss_jordan(arr, N):
    for i in range(N):
        for j in range(N):
            if i != j:
                p = arr[j][i] / arr[i][i]
                for k in range(N+1):
                    arr[j][k] -= arr[i][k] * p
        print_arr(arr)
        print()

    for i in range(N):
        print(arr[i][3] / arr[i][i])

gauss_jordan(arr, N)
[2, 1, -1, 8]
[0.0, 0.5, 0.5, 1.0]
[0.0, 2.0, 1.0, 5.0]

[2.0, 0.0, -2.0, 6.0]
[0.0, 0.5, 0.5, 1.0]
[0.0, 0.0, -1.0, 1.0]

[2.0, 0.0, 0.0, 4.0]
[0.0, 0.5, 0.0, 1.5]
[0.0, 0.0, -1.0, 1.0]

2.0
3.0
-1.0

Cramers’ rule

Another one easiest way is to use cramer’s rule. It’s not so dynamic but it works for small systems of equations. The idea is to express the solution in terms of determinants.

import numpy as np

main_array = np.array(arr)
D = np.array(main_array[:,:-1])

last_col  = np.array(main_array[:,-1])

D1 = np.array([last_col, main_array[:,1], main_array[:,2]])
D2 = np.array([main_array[:,0], last_col, main_array[:,2]])
D3 = np.array([main_array[:,0], main_array[:,1], last_col])

# We can also define our own det function for n specific variable easily like,
# def det(arr):
#     a =   arr[0][0] * ( arr[1][1] * arr[2][2] - arr[1][2] * arr[2][1] )
#     b = - arr[1][0] * ( arr[0][1] * arr[2][2] - arr[0][2] * arr[2][1] )
#     c =   arr[2][0] * ( arr[0][1] * arr[1][2] - arr[0][2] * arr[1][1] )
#     return a + b + c

D_det = np.linalg.det(D)
D1_det = np.linalg.det(D1)
D2_det = np.linalg.det(D2)
D3_det = np.linalg.det(D3)

print("x = ", D1_det / D_det)
print("y = ", D2_det / D_det)
print("z = ", D3_det / D_det)
x =  2.0
y =  2.9999999999999996
z =  -0.9999999999999998