Advertisement
parthosutradhor

Reduced Row Echelon Form

Feb 10th, 2025
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.82 KB | Science | 0 0
  1. from sympy import Matrix, pprint
  2. from functools import reduce
  3. from math import gcd
  4.  
  5. # Author: Partho Sutra Dhor
  6. # Date: 2/10/25
  7. # Description: Transforming a matrix into Row Echelon Form (REF) and Reduced Row Echelon Form (RREF)
  8.  
  9. def print_matrix(matrix, step_desc):
  10.     input()
  11.     print(f"\n{step_desc}")
  12.     pprint(matrix)
  13.    
  14. def row_gcd(row):
  15.     return reduce(gcd, row)
  16.    
  17. def divide_by_gcd(matrix):
  18.     global step
  19.     for i in range(matrix.shape[0]):  
  20.         row = matrix.row(i)
  21.         row_gcd_value = row_gcd(row)
  22.         if row_gcd_value != 1 and row_gcd_value != 0:
  23.             for j in range(matrix.shape[1]):
  24.                 matrix[i, j] = matrix[i, j] // row_gcd_value
  25.             step=step+1
  26.             print_matrix(matrix, f"Step-{step}: Dividing by GCD: (Row {i+1}) → (Row {i+1}) / {row_gcd_value}")    
  27.     return matrix
  28.  
  29. def row_echelon_form(matrix):
  30.     global step
  31.     rows, cols = matrix.shape
  32.     lead = 0
  33.  
  34.     for r in range(rows):
  35.         if lead >= cols:
  36.             return matrix
  37.  
  38.         # Find pivot in the current column
  39.         i = r
  40.         while matrix[i, lead] == 0:
  41.             i += 1
  42.             if i == rows:
  43.                 i = r
  44.                 lead += 1
  45.                 if lead == cols:
  46.                     return matrix
  47.  
  48.         # Swap rows
  49.         if i != r:
  50.             matrix.row_swap(i, r)
  51.             step=step+1
  52.             print_matrix(matrix, f"Step-{step}: Row Swap: Swap Row {i+1} with Row {r+1}")
  53.  
  54.         # Eliminate below pivot
  55.         for i in range(r + 1, rows):
  56.             matrix = divide_by_gcd(matrix)
  57.             lv = matrix[i, lead]
  58.             pivot = matrix[r, lead]
  59.             s = lv * pivot
  60.             G = gcd(lv, pivot)
  61.             lv = abs(lv // G)
  62.             pivot = abs(pivot // G)
  63.             if lv != 0:
  64.                 if s < 0:
  65.                     for j in range(cols):
  66.                         matrix[i, j] = pivot * matrix[i, j] + lv * matrix[r, j]
  67.                     step=step+1
  68.                     print_matrix(matrix, f"Step-{step}: Eliminate below: (Row {i+1}) → {pivot} * (Row {i+1}) + {lv} * (Row {r+1})")
  69.                 else:
  70.                     for j in range(cols):
  71.                         matrix[i, j] = pivot * matrix[i, j] - lv * matrix[r, j]
  72.                     step=step+1
  73.                     print_matrix(matrix, f"Step-{step}: Eliminate below: (Row {i+1}) → {pivot} * (Row {i+1}) - {lv} * (Row {r+1})")
  74.  
  75.         lead += 1
  76.  
  77.     return matrix
  78.  
  79. def reduced_row_echelon_form(matrix):
  80.     global step
  81.     rows, cols = matrix.shape
  82.     lead_positions = []
  83.  
  84.     # Identify pivot positions
  85.     for r in range(rows):
  86.         for c in range(cols):
  87.             if matrix[r, c] != 0:
  88.                 lead_positions.append((r, c))
  89.                 break
  90.  
  91.     # Eliminate above pivot
  92.     for r, lead_col in reversed(lead_positions):
  93.         matrix = divide_by_gcd(matrix)
  94.         for i in range(r):
  95.             lv = matrix[i, lead_col]
  96.             pivot = matrix[r, lead_col]
  97.             s = lv * pivot
  98.             G = gcd(lv, pivot)
  99.             lv = abs(lv // G)
  100.             pivot = abs(pivot // G)
  101.             if lv != 0:
  102.                 if s < 0:
  103.                     for j in range(cols):
  104.                         matrix[i, j] = pivot * matrix[i, j] + lv * matrix[r, j]
  105.                     step=step+1
  106.                     print_matrix(matrix, f"Step-{step}: Eliminate above: (Row {i+1}) → {pivot} * (Row {i+1}) + {lv} * (Row {r+1})")
  107.                 else:
  108.                     for j in range(cols):
  109.                         matrix[i, j] = pivot * matrix[i, j] - lv * matrix[r, j]
  110.                     step=step+1
  111.                     print_matrix(matrix, f"Step-{step}: Eliminate above: (Row {i+1}) → {pivot} * (Row {i+1}) - {lv} * (Row {r+1})")
  112.    
  113.     # Normalize pivot  
  114.     for r, lead_col in lead_positions:
  115.         pivot = matrix[r, lead_col]
  116.         if pivot != 1:
  117.             for j in range(cols):
  118.                 matrix[r, j] = matrix[r, j] / pivot
  119.             step=step+1
  120.             print_matrix(matrix, f"Step-{step}: Normalize Pivot: (Row {r+1}) → (Row {r+1}) / {pivot}")
  121.            
  122.     return matrix
  123.  
  124.  
  125.  
  126. # Imput Your Augmented Matrix
  127.  
  128. aug_matrix = Matrix([
  129.     [1, 3, 2, 0, 2, 0, 0],
  130.     [2, 6, -5, -2, 4, -3, -1],
  131.     [0, 0, 5, 10, 0, 15, 5],
  132.     [2, 6, 0, 8, 4, 18, 6]
  133. ])
  134.  
  135.  
  136.  
  137. step=0
  138. # Row Echelon Form (REF)
  139. print("\n\n------ Solving System ------\n")
  140. print("--- Press ENTER to Start ---")
  141. print("--- Press CTRL+C to Exit ---")
  142. print_matrix(aug_matrix, "Original Augmented Matrix")
  143. print("\n")
  144. row_echelon_form(aug_matrix)
  145. print("\n")
  146. print_matrix(aug_matrix, "Row Echelon Form (REF)")
  147.  
  148. # Reduced Row Echelon Form
  149. print("\n")
  150. reduced_row_echelon_form(aug_matrix)
  151. print("\n")
  152. print_matrix(aug_matrix, "Final Reduced Row Echelon Form (RREF)")
  153. input()
  154. input()
  155.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement