Question
Matrix multiplication. Using Python, write a program that will multiply two matrices. This is the structure of the code inp1 = input() #reads in the
Matrix multiplication. Using Python, write a program that will multiply two matrices.
This is the structure of the code
inp1 = input() #reads in the matrix inp1_strip = inp1.strip("(),[] ") #removes brackets or parantheses and spaces matrix1 = [list(map(float, t.split(","))) for t in inp1_strip.split(";")] #creates a matrix for input
inp2 = input() #reads in the second matrix inp2_strip = inp2.strip("(),[] ") #removes brackets or parantheses and spaces matrix2 = [list(map(float, t.split(","))) for t in inp2_strip.split(";")] #creates a matrix for input
def row_times_column(row,col): #don't need to check they are the right size here as it was already done result = 0 length = len(row) #you need to set a local variable for the length of a row/column '''Your Code Goes Here''' #for each element in the row and column, you need to compute the product and add it to result
return result
def matrix_multiplication(mat1,mat2): Cols_2 = len(mat2[0]) '''Your Code Goes Here''' #number of rows and columns is needed '''Your Code Goes Here''' Rows_1 = len(mat1) '''Your Code Goes Here''' #if the product is not possible, return the correct message '''Your Code Goes Here''' #otherwise we want to calculate the product product_matrix = [ [0 for _ in range(Cols_2) ] for _ in range(Rows_1)] #creates matrix of the correct size for the product column = [0 for _ in range(Rows_2)] # initialize the column '''Your Code Goes Here''' #you need to iterate through each row times each of the columns. However, you need to set the column each time to pass it to the function. You will have 3 for loops. '''Your Code Goes Here''' '''Your Code Goes Here''' #call row_times_column for each element after creating the column return product_matrix
to_print = matrix_multiplication(matrix1,matrix2)
'''Your Code Goes Here''' #if the type is a string print(to_print) '''Your Code Goes Here''' #Otherwise for r in to_print: print(r)
We want to write a program that will take in two matrices, A and B, and then compute the matrix product AB if the product is possible of return a message that the product is not defined. In particular: 1) If the matrix product is undefined, the program should return "Undefined as the number of columns in the first matrix does not equal the number of rows in the second matrix." 2) Otherwise, the program uses the rowtimescolumn function to compute the entries of the product matrix. Ex: If the input is: (1,2,3,4,5,6) (1;2;3;4) We would return Undefined as the number of columns in the first matrix does not equal the number of rows in the second matrix. Ex: If the input is: (1,2,3,4,5,6) (1,2;2,3;3,4) we would return [14.0, 20.01 [32.0, 47.0] Note: The code will already be set up to accept a matrices 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: You will want to define the number of rows and columns locally instead of globally for later use of this program. 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 formedStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started