matrix_machine.py

Created by wperez274

Created on September 11, 2023

2.71 KB


#
# "Matrix Machine"
#------------------------
# 
# Written By: Wilson and 
# GPT-3
#
#######################
from math import *
from random import choice, randint as RAND
from time import sleep




def calculate_cofactor(matrix, i, j):
    submatrix = [row[:j] + row[j+1:] for row in (matrix[:i] + matrix[i+1:])]
    sign = (-1) ** (i + j)
    return sign * calculate_determinant(submatrix)

def calculate_determinant(matrix):
    if len(matrix) == 2:
        return matrix[0][0] * matrix[1][1] - matrix[0][1] * matrix[1][0]
    a, b, c = matrix[0]
    d, e, f = matrix[1]
    g, h, i = matrix[2]
    return a * (e * i - f * h) - b * (d * i - f * g) + c * (d * h - e * g)
    
    


try:
  a = float(input("a: "))
except:
  a = 1
  
try:
  b = float(input("b: "))
except:
  b = 1
    
try:
  c = float(input("c: "))
except:
  c = 1
    
try:
  d = float(input("d: "))
except:
  d = 1
    
try:
  e = float(input("e: "))
except:
  e = 1
    
try:
  f = float(input("f: "))
except:
  f = 1
    
try:
  g = float(input("g: "))
except:
  g = 1
    
try:
  h = float(input("h: "))
except:
  h = 1  
  
try:
  i = float(input("i: "))
except:
  i = 1
  
  
  
  
A = [
    [a, b, c],
    [d, e, f],
    [g, h, i]
]

cofact_A = [
    [calculate_cofactor(A, 0, 0), calculate_cofactor(A, 0, 1), calculate_cofactor(A, 0, 2)],
    [calculate_cofactor(A, 1, 0), calculate_cofactor(A, 1, 1), calculate_cofactor(A, 1, 2)],
    [calculate_cofactor(A, 2, 0), calculate_cofactor(A, 2, 1), calculate_cofactor(A, 2, 2)]
]

sleep(1)

print("\nOriginal Matrix:\n")
sleep(1)
print("================")
for row in A:
    print(row)
print("================")
sleep(1)
print("\nDeterminant of Matrix:\n")
sleep(1)
print("================")
print(det_A = calculate_determinant(A))
print("================")
sleep(2)

print("\nNew Cofactored Matrix: \n")
sleep(1)
print("================")
for row in cofact_A:
    print(row)
print("================")

transpose = [[cofact_A[j][i] for j in range(len(cofact_A))] for i in range(len(cofact_A[0]))]


# adj(A) = transpose of the # cofactored A
adj_A = transpose


sleep(2)


print("\nAdj(A):\n")
sleep(1)
print("================")

for row in adj_A:
    print(row)
    
print("================")

sleep(2)

if det_A != 0:
# Calculate the inverse of A
    inverse_A = [[adj_A[i][j] / det_A for j in range(3)] for i in range(3)]

# Print the inverse matrix
    print("\nInverse of A:\n","black")
    sleep(1)
#    for row in inverse_A:
#        print(row)
        
    for row in inverse_A:
      rounded_row = [round(element, 3) for element in row]
      print(rounded_row)      
        
        
else:
    print("The matrix A is not invertible since its determinant is zero.")
    
    
print("\n\n=====Finished.=========")

During your visit to our site, NumWorks needs to install "cookies" or use other technologies to collect data about you in order to:

With the exception of Cookies essential to the operation of the site, NumWorks leaves you the choice: you can accept Cookies for audience measurement by clicking on the "Accept and continue" button, or refuse these Cookies by clicking on the "Continue without accepting" button or by continuing your browsing. You can update your choice at any time by clicking on the link "Manage my cookies" at the bottom of the page. For more information, please consult our cookies policy.