In: Computer Science
It is a strict requirement that this code is implemented in python. The code must be implemented from scratch not using e.g. numpy etc.
a. Create a function that takes a matrix X of all sizea as input and gives the transposed matrix as output. Example
transposeMatrix([[1,2] ,[3,4] ,[5,6]]) = [[1,3,5] ,[2,4,6]]
b. Create a function that multilpies a matrix X1 and X2 of all sizes. Example
x1 = [[0,1] ,[1,0] ] x2 = [[1, 0] ,[0,-1] ] matrtixMultl(x1,x2) = [[0, -1] ,[1, 0] ]
c. Create a function that takes a square matrix as input and creates its inverse matrix as output. And that is such that matrixMult(A,matrixInvert(A)) is the unit matrix
Please find below the code, code screenshots and output screenshots. Please refer to the screenshot of the code to understand the indentation of the code. Please get back to me if you need any change in code. Else please upvote.
Code for matrix transpose:
def transposeMatrix(matrix):
print("The matrix is:")
for row in matrix :
print(row)
# iterate through each element of matrix in column major manner and assign the value to result matrix
result = [[matrix[j][i] for j in range(len(matrix))] for i in range(len(matrix[0]))]
print("\nThe transpose matrix is:")
for row in result:
print(row)
transposeMatrix([[1,2],[3,4],[5,6]])
Code for matrix multiplication:
def matrtixMultl(m1, m2):
print("The matrixes are:")
for row in m1 :
print(row)
print("\n")
for row in m2 :
print(row)
result = [[sum(a*b for a,b in zip(m1_row,m2_col)) for m2_col in zip(*m2)] for m1_row in m1]
print("\nThe result matrix after multiplication is:")
for row in result:
print(row)
x1 = [[0,1] ,[1,0]]
x2 = [[1, 0],[0,-1]]
matrtixMultl(x1,x2)