Question

Function name : matrixMultiplication Parameters : aMatrix (list), bMatrix (list) Returns Type: list of tuples Description...

Function name : matrixMultiplication

Parameters : aMatrix (list), bMatrix (list)

Returns Type: list of tuples

Description : Write a function in PYTHON that takes in two matrices (list of tuples or list of lists) and

multiplies the two matrices together. Assume that the given matrices will be valid, i.e.

aMatrix will be n x m (n rows and m columns) and bMatrix will be m x ℓ (m rows and

ℓ columns).

*Assume elements of aMatrix & bMatrix will be numeric values and aMatrix &

bMatrix will be a valid matrix

Test Cases:

>>> matrixMultiplication( [(1,2),(3,4)] , [(-1,0),(0,1)] )

[(-1, 2), (-3, 4)]

>>> matrixMultiplication( [(1,2),(3,4),(5,6)] , [(-1,-1,0),(0,1,1)] )

[(-1, 1, 2), (-3, 1, 4), (-5, 1, 6)]

Homework Answers

Answer #1

Please find the answer below, all the details are mentioned in the comments and code snippet of the python code is also attached.

Matrix_Multiplication.py

def matrixMultiplication(aMatrix, bMatrix):
  
#create a result matrix based on the mxn and nxk = mxk
result = []
for i in range(len(aMatrix)):
list_0 = []
for j in range(len(bMatrix[0])):
list_0.append(0)
result.append(list_0)
  
# iterate through rows of aMatrix
for i in range(len(aMatrix)):
# iterate through columns of bMatrix
for j in range(len(bMatrix[0])):
# iterate through rows of bMatrix
for k in range(len(bMatrix)):
#print(aMatrix[i], aMatrix[k], bMatrix[k], bMatrix[j])
result[i][j] += aMatrix[i][k] * bMatrix[k][j]
#to convert internal lists to tuple
answer = []
for i in range(len(result)):
x = tuple(result[i])
answer.append(x)
print(answer)

matrixMultiplication( [(1,2),(3,4)] , [(-1,0),(0,1)] )
matrixMultiplication( [(1,2),(3,4),(5,6)] , [(-1,-1,0),(0,1,1)] )

Output (code snippet and output):

Please let us know in the comments if there is any problem.

1 def matrixMultiplication (aMatrix, bMatrix): 2 #create a result matrix based on the mxn and nxk = mxk [] for i in range(len(aMatrix)): list_e for j in range(len(bMatrix [0] )): list_0.append (0) result.append ( list_0) 4 result [] 6 = 7 10 # iterate through rows of aMatrix for i in range(len(aMatrix)): # iterate through columns of bMatrix for j in range(len(bMatrix[0])): # iterate through rows of bMatrix for k in range (len(bMatrix)) 11 12 13 14 15 16 #print(aMatrix[i], aMatrix[k], bMatrix[k], bMatrix[j]) 17 bMatrix[k][j] += aMatrix[i][k] result[i][j] 18 print(result 19 20 21 matrixMultiplication( [(1,2), (3,4)] , [( -1,0),(0,1)] )] 22 matrixMultiplication( [(1,2),(3,4), (5,6)] , [(-1,-1,0), (0,1,1)] ) [-1, 2], -3, 4]] [I-1, 1, 21, [-3, 1, 4], [-5, 1, 6]]

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Implement function reverse that takes a 2D list (a list of list of integers) and returns...
Implement function reverse that takes a 2D list (a list of list of integers) and returns a new 2D list where the items in each row are in reverse order. You maynot use slicing. You may not modify the input list. The returned list should be completely new in memory – no aliases with the input 2D list. From list methods, you may use only the .append(). Examples: reverse([[1,2,3],[4,5,6]])       ->    [[3,2,1],[6,5,4]] reverse([[1,2],[3,4],[5,6]])     ->    [[2,1],[4,3][6,5]] True False In python please
Write a Python function called sumNxN with three parameters. The first parameter is a nested list...
Write a Python function called sumNxN with three parameters. The first parameter is a nested list (matrix) representing a matrix of size N x N. The second parameter is the integer N, and the third is a list of N zeros. Modify the list of zeros so that each entry is the sum of the corresponding column. There is no return. Note: You may assume the sizes provided are all correct.
Python pls create a function called search_position. This function returns a list of 2 tuples and...
Python pls create a function called search_position. This function returns a list of 2 tuples and the number should be start highest number. The first index is the number, and second are list of 2 tuples that sorted by position in alphabetical order: The first index will be position and second index will be champion's name(This also should be sorted by alphabetical order). team1 = {'Fiora': {'Top': 1, 'Mid': 4, 'Bottom': 3},'Olaf': {'Top': 3, 'Mid': 2, 'Support': 4},'Yasuo': {'Mid': 2,...
1. Find the orthogonal projection of the matrix [[3,2][4,5]] onto the space of diagonal 2x2 matrices...
1. Find the orthogonal projection of the matrix [[3,2][4,5]] onto the space of diagonal 2x2 matrices of the form lambda?I.   [[4.5,0][0,4.5]]  [[5.5,0][0,5.5]]  [[4,0][0,4]]  [[3.5,0][0,3.5]]  [[5,0][0,5]]  [[1.5,0][0,1.5]] 2. Find the orthogonal projection of the matrix [[2,1][2,6]] onto the space of symmetric 2x2 matrices of trace 0.   [[-1,3][3,1]]  [[1.5,1][1,-1.5]]  [[0,4][4,0]]  [[3,3.5][3.5,-3]]  [[0,1.5][1.5,0]]  [[-2,1.5][1.5,2]]  [[0.5,4.5][4.5,-0.5]]  [[-1,6][6,1]]  [[0,3.5][3.5,0]]  [[-1.5,3.5][3.5,1.5]] 3. Find the orthogonal projection of the matrix [[1,5][1,2]] onto the space of anti-symmetric 2x2 matrices.   [[0,-1] [1,0]]  [[0,2] [-2,0]]  [[0,-1.5] [1.5,0]]  [[0,2.5] [-2.5,0]]  [[0,0] [0,0]]  [[0,-0.5] [0.5,0]]  [[0,1] [-1,0]] [[0,1.5] [-1.5,0]]  [[0,-2.5] [2.5,0]]  [[0,0.5] [-0.5,0]] 4. Let p be the orthogonal projection of u=[40,-9,91]T onto the...
Use Python 3.8: Problem Description Many recipes tend to be rather small, producing the fewest number...
Use Python 3.8: Problem Description Many recipes tend to be rather small, producing the fewest number of servings that are really possible with the included ingredients. Sometimes one will want to be able to scale those recipes upwards for serving larger groups. This program's task is to determine how much of each ingredient in a recipe will be required for a target party size. The first inputs to the program will be the recipe itself. Here is an example recipe...
Problem Description Many recipes tend to be rather small, producing the fewest number of servings that...
Problem Description Many recipes tend to be rather small, producing the fewest number of servings that are really possible with the included ingredients. Sometimes one will want to be able to scale those recipes upwards for serving larger groups. This program's task is to determine how much of each ingredient in a recipe will be required for a target party size. The first inputs to the program will be the recipe itself. Here is an example recipe that comes from...
Use python language please #One of the early common methods for encrypting text was the #Playfair...
Use python language please #One of the early common methods for encrypting text was the #Playfair cipher. You can read more about the Playfair cipher #here: https://en.wikipedia.org/wiki/Playfair_cipher # #The Playfair cipher starts with a 5x5 matrix of letters, #such as this one: # # D A V I O # Y N E R B # C F G H K # L M P Q S # T U W X Z # #To fit the 26-letter alphabet into...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT