-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSparseVector.py
More file actions
78 lines (54 loc) · 1.41 KB
/
SparseVector.py
File metadata and controls
78 lines (54 loc) · 1.41 KB
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#!/anaconda3/bin/python
class SparseVector:
def __init__(self,denseArray):
self.size = len(denseArray)
self.sparseDic = {}
for i in range(len(denseArray)):
if denseArray[i] != 0:
self.sparseDic[i] = denseArray[i]
def get_element_at(self, index):
if index in self.sparseDic:
return self.sparseDic[index]
else:
return 0
def length(self):
return self.size
def dot(self, other_vector):
dotProductDic = {}
if (self.length() != other_vector.length()):
return 'Error! Vectors should have the same size.'
set1 = set(self.sparseDic)
set2 = set(other_vector.sparseDic)
commonIndices = set1.intersection(set2)
for i in commonIndices:
dotProductDic[i] =\
self.sparseDic[i] * other_vector.sparseDic[i]
return sum(list(dotProductDic.values()))
def iterate_non_zero(self):
for index in self.sparseDic:
yield (index, self.sparseDic[index])
def main():
denseArray1 = [0,1,0,1,0,8]
SP1 = SparseVector(denseArray1)
print(SP1.length())
#output: 6
for index in range(SP1.length()):
print(index,SP1.get_element_at(index))
#output:
# 0 0
# 1 1
# 2 0
# 3 1
# 4 0
# 5 8
for elm in SP1.iterate_non_zero():
print((elm[0],elm[1]))
#ouput:
# (1, 1)
# (3, 1)
# (5, 8)
denseArray2 = [0,1,0,0,0,1.5]
SP2 = SparseVector(denseArray2)
print(SP1.dot(SP2))
#output: 13.0
main()