Dot product of sparse vectors

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)

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

时间: 2024-10-08 03:27:43

Dot product of sparse vectors的相关文章

Dot Product

These are vectors: They can be multiplied using the "Dot Product" (also see Cross Product). Calculating You can calculate the Dot Product of two vectors this way: a · b = |a| × |b| × cos(θ) Where:|a| is the magnitude (length) of vector a|b| is t

(转载)Cross product

原文地址:https://en.wikipedia.org/wiki/Cross_product Cross product From Wikipedia, the free encyclopedia This article is about the cross product of two vectors in three-dimensional Euclidean space. For other uses, see Cross product (disambiguation). In m

A.Kaw矩阵代数初步学习笔记:2. Vectors

“矩阵代数初步”(Introduction to MATRIX ALGEBRA)课程由Prof. A.K.Kaw(University of South Florida)设计并讲授. PDF格式学习笔记下载(Academia.edu) 第2章课程讲义下载(PDF) Summary Vector A vector is a collection of numbers in a definite order. If it is a collection of $n$ numbers, it is c

MatterTrack Route Of Network Traffic :: Matter

Python 1.1?基础 while语句 字符串边缘填充 列出文件夹中的指定文件类型 All Combinations For A List Of Objects Apply Operations Over Items In A List Applying Functions To List Items Arithmetic Basics Assignment Operators Basic Operations With NumPy Array Breaking Up String Vari

u3d_Shader_effects笔记2 自定义surfaceDiffuseLight

1.前面的心情 今晚7点半睡着后,9点半左右被吵醒.醒来后非常失落,感觉人生到底在追求什么,我又在追求什么.昨晚梦到妈妈了.最近不时会想到爷爷的去世.人世的险恶,良心的缺失.不过一切总要向前看,至少我有一个快乐幸福的家庭.有支持我的父母姐妹. 但自己到底想要什么,追求什么. 近期工作也不太顺利,业余时间也比较堕落. 感情方面还是空白,而且经常想性事. 不管怎样,为了活着的人和自己,每天都要开心.进步.阳光. 2.本文主要内容 本文主要是18页到20页内容. 自定义diffuse光照处理. 3.参

shader函数

Intrinsic Functions (DirectX HLSL) The following table lists the intrinsic functions available in HLSL. Each function has a brief description, and a link to a reference page that has more detail about the input argument and return type. Name Syntax D

Unity 源码(伪)

A B C D E F G H I J K L M N O P Q R S T U V W X Y Z A: B: C: D: E: F: G: H: I: J: K: L: M: 1 using System; 2 using System.Runtime.CompilerServices; 3 using UnityEngine.Internal; 4 using UnityEngine.Scripting; 5 using UnityEngineInternal; 6 7 namespac

Blas 基本函数功能

CAXPY constant times a vector plus a vector. //常数乘以一个向量加上一个向量. CCOPY copies a vector x to a vector y.//复制一个向量x到y的向量. CDOTC forms the dot product of two vectors, conjugating the first vector.//结合第一个向量,形成了两个向量的点积 CDOTU forms the dot product of two vect

Extending Justin’s Guide to MATLAB in MATH 240

Extending Justin’s Guide to MATLAB in MATH 240 - Part 41. MethodWe assume you are comfortable with (and remember or can review) commands used in the earlierprojects.2. New Commands(a) Eigenvalues can be found easily. If A is a matrix then:>> eig(A)w