# write your python code here
# you can take the above example as sample input for your program to test
# it should work for any general input try not to hard code for only given input examples
# you can feel free to change all these codes/structure
# here A and B are list of lists
import numpy as np
def matrix_mul():
a=[]
ac=0
ace=0
bc=0
bce=0
result=[]
A = [[1,3,4],
[2,5,7],
[5,9,6]]
B = [[1,0,0],
[0,1,0],
[0,0,1]]
print(A,B,len(A[0]),len(B[0]),len(A),len(B))
for i in range(len(A)):
ac+=1
for j in range(len(A[0])):
ace+=1
for i in range(len(B)):
bc+=1
for j in range(len(B[0])):
bce+=1
if ace==bc:
for b in range(bce):
ele=0
a.append(ele)
for c in range(len(A)):
result.append(a)
print(result)
print(A,B,len(A[0]),len(B[0]),len(A),len(B))
for i in range(len(A)):
# iterating by coloum by B
for j in range(len(B[0])):
# iterating by rows of B
for k in range(len(B)):
result[i][j] += A[i][k] * B[k][j]
print(result)
result=np.dot(A,B)
print(result)
print(len(A),len(B),ac,ace,bc,bce)
else:
print("Can't multiply",A,B)
print(len(A),len(B),ac,ace,bc,bce)
matrix_mul()
thats my code for matrix multiplication in which the key code is
for i in range(len(A)):
# iterating by coloum by B
for j in range(len(B[0])):
# iterating by rows of B
for k in range(len(B)):
result[i][j] += A[i][k] * B[k][j]
It works fine on geeksforgeeks,programmiz and for people on stackoverflow but doesnt work on my jupyter notebook. I don’ t understand the reason. Please help.
It produces output:
[[8, 17, 17], [8, 17, 17], [8, 17, 17]]
Output It should produce:
[[1 3 4]
[2 5 7]
[5 9 6]]
also it should also work for inputs:
A = [[1,2],
[3,4]]
B = [[1,2,3,4,5],
[5,6,7,8,9]]
Thanks.