forked from keon/algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsparse_dot_vector.py
More file actions
69 lines (52 loc) · 1.61 KB
/
sparse_dot_vector.py
File metadata and controls
69 lines (52 loc) · 1.61 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
"""
Sparse Dot Vector
Compute the dot product of two large sparse vectors efficiently by
converting them to index-value pair representations and merging.
Reference: https://leetcode.com/problems/dot-product-of-two-sparse-vectors/
Complexity:
Time: O(n) for conversion, O(k) for dot product where k = non-zero count
Space: O(k)
"""
from __future__ import annotations
def vector_to_index_value_list(
vector: list[float],
) -> list[tuple[int, float]]:
"""Convert a dense vector to a sparse index-value list.
Args:
vector: Dense vector of floats.
Returns:
List of (index, value) tuples for non-zero elements.
Examples:
>>> vector_to_index_value_list([0.0, 2.0, 0.0, 3.0])
[(1, 2.0), (3, 3.0)]
"""
return [(i, v) for i, v in enumerate(vector) if v != 0.0]
def dot_product(
iv_list1: list[tuple[int, float]],
iv_list2: list[tuple[int, float]],
) -> float:
"""Compute the dot product of two sparse index-value lists.
Args:
iv_list1: First sparse vector as index-value pairs.
iv_list2: Second sparse vector as index-value pairs.
Returns:
Dot product of the two vectors.
Examples:
>>> dot_product([(0, 1.0), (1, 2.0), (2, 3.0)], [(1, 2.0), (2, 2.0)])
10.0
"""
product = 0
p1 = len(iv_list1) - 1
p2 = len(iv_list2) - 1
while p1 >= 0 and p2 >= 0:
i1, v1 = iv_list1[p1]
i2, v2 = iv_list2[p2]
if i1 < i2:
p1 -= 1
elif i2 < i1:
p2 -= 1
else:
product += v1 * v2
p1 -= 1
p2 -= 1
return product