-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathmultiSparse.py
57 lines (51 loc) · 1.53 KB
/
multiSparse.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
'''
利用三元组完成稀疏矩阵相乘
'''
from numpy import *
def sparseToTriple(matrix):
m, n = shape(matrix)
triple = []
for i in range(m):
for j in range(n):
if matrix[i][j] != 0:
triple.append([i, j, matrix[i][j]])
return triple
def multiTriple(tripleA, tripleB):
rowA = shape(tripleA)[0]
rowB = shape(tripleB)[0]
multiMatrix = []
for i in range(rowA):
for j in range(rowB):
if tripleA[i][1] == tripleB[j][0]:
multiMatrix.append([tripleA[i][0], tripleB[j][1], tripleA[i][2]*tripleB[j][2]])
return multiMatrix
def tripleToSparse(triple, m, n):
outMatrix = zeros([m, n])
for pointTuple in triple:
mLocation = pointTuple[0]
nLocation = pointTuple[1]
value = pointTuple[2]
outMatrix[mLocation][nLocation] = value
return outMatrix
def matrixMultiple(matrixA, matrixB):
mA, nA = shape(matrixA)
mB, nB = shape(matrixB)
if nA != mB:
print("the two matries doesn't match!")
return -1
tripleA = sparseToTriple(matrixA)
tripleB = sparseToTriple(matrixB)
multiTriples = multiTriple(tripleA, tripleB)
print(multiTriples)
multiMatrix = tripleToSparse(multiTriples, mA, nB)
return multiMatrix
matrixA = [[3, 0, 0, 7],
[0, 0, -1, 0],
[-1, -2, 0, 0],
[0, 0, 0, 2]]
matrixB = [[0, 0, -2, 0, -1],
[0, 0, -3, 0, 0],
[-1, 0, 0, 0, 0],
[0, 0, 0, 3, 0]]
ans = matrixMultiple(matrixA, matrixB)
print(ans)