Question: Using Python, write a program that will use matrix reduction to calculate the inverse of a matrix. This is the structure of the code inp
Using Python, write a program that will use matrix reduction to calculate the inverse of a matrix.

This is the structure of the code
inp = input() #reads in the matrix inp_strip = inp.strip("(),[] ") #removes brackets or parantheses and spaces matrix = [list(map(float, t.split(","))) for t in inp_strip.split(";")] #creates a matrix for input
def make_extended_matrix(mat): R = len(mat) # number of rows C = len(mat[0]) # number of columns '''Your Code Goes Here''' #we need to go to each row and then through each column. If we are on the diagonal, we add a 1.0 to the row. Otherwise we add a 0.0 '''Your Code Goes Here''' '''Your Code Goes Here''' mat[m] = mat[m]+[1.0] '''Your Code Goes Here''' '''Your Code Goes Here''' #extends the matrix with an identity matrix on the right return mat
'''Your Code Goes Here''' #You need to insert the code for the reduced echelon calculator, but you need to modify it to define the length of the inputs locally. # For instance, mult_row_by_num(row,a) can use len(row) to determine the value used in the for loop range
def inverse_calculator(mat): C = len(mat[0]) R = len(mat) '''Your Code Goes Here''' #this is the case where the matrix is not square else: '''Your Code Goes Here''' #use make_extended_matrix to create the augmented matrix reduced_extended_matrix = reduced_echelon_calculator(mat) #do the matrix reduction to produce a reduced row echelon on one side and the potential inverse of the other '''Your Code Goes Here''' #This is the case where the matrix is square but not invertible. Hint: If the last column of the left side matrix does not have a one (it would be zero) at the bottom, there is a free variable and the matrix is not invertible '''Your Code Goes Here''' #Otherwise we continue inverse = [[0 for _ in range(C)] for _ in range(R)] # creates matrix of the correct size '''Your Code Goes Here''' '''Your Code Goes Here''' #we need a double for loop to run through right side of the augmented matrix inverse[i][j-C] = reduced_extended_matrix[i][j] return inverse
simplified = inverse_calculator(matrix)
if type(simplified) == str: print(simplified) else: for r in simpified: print(r)
15.8 Inverse Matrix Calculator ***Note: This is a continuation of the Reduced Echelon for Calculator. You will need a modified version of the Reduced Echelon Calculator as a part of this program We want to write a program that will use matrix reduction to calculate the inverse of a matrix. Your goal is to complete the program below so that it does the following: 1) If the matrix has an inverse, it returns only the inverse matrix. 2) If the matrix is not square returns the message There is no inverse matrix as this matrix is not square." 3) If the matrix is square but not invertible it returns the message "The matrix is square but it is not invertible." Ex: If the input is: (1,2,3,0,1,0,0,1,1) the program would return; (1.0, 1.0, -3.0] [0.0, 1.0; 0.01 [0.0, -1.0, 1.0) Ex: If the input is: (1,2,3;0,1,0,0,1,1;1,0,1) the program would return: There is no inverse matrix as this matrix is not square. Ex: If the input is: (1,2,3;1,2,3;0,1,0) the program would return: The matrix is square but it is not invertible. Note: The code will already be set up to accept a matrix with row entries separated by commas and rows separated by semicolons, i.e. (1,2,3,4,5,6) is the matrix 123 456. Note: The matrix is read in using a list comprehension which is a compact way to do loops. It is also possible to read the matrix in using loops, loops and set comprehensions, and more depending on how you want the input to be formed. Note: It is recommended that you download the code. This will allow you to use print statements to check individual procedures and access the code for checking work and later projects. Note: If you want to print the matrix in a "cleaner" notation, you can use: mat_print = echelon calculator (matrix) for i in range (R) : for j in range (C): print (mat_printillil, end = "") print
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
