forked from ShrohanMohapatra/matrix_multiply_quadratic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
matrix_multiply_quadratic.py
63 lines (63 loc) · 1.86 KB
/
matrix_multiply_quadratic.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
from math import log10
from random import randint
def my_matrix_multiply(A,B):
flag = True
flag = flag and (len(A) == len(B))
if not flag: return None
N = len(A)
for i in range(N): flag = flag and (len(A[i])==len(B[i]))
if not flag: return None
maxi=0
for i in range(N):
for j in range(N):
if maxi<A[i][j]: maxi=A[i][j]
if maxi<B[i][j]: maxi=B[i][j]
M=int(log10(maxi))+1
P=int(log10((10**(2*M)-1)*N))+1
C,D,E=[0 for i in range(N)],[0 for i in range(N)],[[0 for i in range(N)] for j in range(N)]
for i in range(N):
for j in range(N):
C[i] = C[i]*(10**P)+A[i][j]
for j in range(N):
for i in range(N):
D[j] = D[j]*(10**P)+B[N-1-i][j]
for i in range(N):
for j in range(N):
E[i][j]=int(C[i]*D[j]/(10**(P*(N-1))))%(10**P)
return E
def ijk_method(A,B): # for verification
flag = True
flag = flag and (len(A) == len(B))
if not flag: return None
N = len(A)
for i in range(N): flag = flag and (len(A[i])==len(B[i]))
if not flag: return None
C = [ [ 0 for j in range(N) ] for i in range(N) ]
for i in range(N):
for j in range(N):
for k in range(N):
C[i][j] = C[i][j] + A[i][k]*B[k][j]
return C
N = randint(1,30)
A = [ [ randint(1,150) for j in range(N) ] for i in range(N) ]
B = [ [ randint(1,150) for j in range(N) ] for i in range(N) ]
C1 = my_matrix_multiply(A,B)
C2 = ijk_method(A,B)
flag = True
for i in range(N):
for j in range(N): flag = flag and C1[i][j] == C2[i][j]
if flag:
print 'Input size',N
print 'Input matrix A'
for i in range(N):
for j in range(N): print A[i][j],
print
print 'Input matrix B'
for i in range(N):
for j in range(N): print B[i][j],
print
print 'Output matrix C1'
for i in range(N):
for j in range(N): print C1[i][j],
print
else: print 'Debug needed'