码迷,mamicode.com
首页 > 其他好文 > 详细

Dot product of sparse vectors

时间:2020-02-06 12:45:46      阅读:44      评论:0      收藏:0      [点我收藏+]

标签:The   ros   span   store   most   col   style   tor   color   

Suppose we have very large sparse vectors (most of the elements in vector are zeros) 

  1. Find a data structure to store them
  2. Compute the Dot Product.

Follow-up:
What if one of the vectors is very small?

 1 a = [(1,2),(2,3),(100,5)]
 2 b = [(0,5),(1,1),(100,6)]
 3 
 4 i = 0; j = 0
 5 result = 0
 6 while i < len(a) and j < len(b):
 7     if a[i][0] == b[j][0]:
 8         result += a[i][1] * b[j][1]
 9         i += 1
10         j += 1
11     elif a[i][0] < b[j][0]:
12         i += 1
13     else:
14         j += 1
15 print(result)

 

Dot product of sparse vectors

标签:The   ros   span   store   most   col   style   tor   color   

原文地址:https://www.cnblogs.com/beiyeqingteng/p/12268049.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!