簡體   English   中英

使用標准庫在 Scala 中計算兩個稀疏向量的點積(並生成它們)

[英]Calculating the Dot Product of two Sparse Vectors (and generating them) in Scala using the standard library

我正在嘗試計算 Scala 中兩個稀疏向量的點積(標量積)。 我編寫的代碼正在做我想做的一切,除了將兩個向量的相似元素相乘時,它沒有考慮 0 值。

我希望得到 72 作為我的答案,因為 3 和 18 是唯一非零的鍵,它們的計算結果為:(3 -> 21) + (18 -> 51) = 72

我使用 withDefaultValue(0) 希望它能“填充”未提及的鍵/值對,但我認為情況並非如此,而且我相信這就是我的問題的來源,一開始。 我想我的問題也可能是“如何使用標准庫在 Scala 中生成稀疏向量”。

如果我輸入相應的 0 並且兩個映射(向量)具有相同數量的鍵/值對,則我的代碼可以正常工作。

```
  val Sparse1 = Map(0 -> 4, 3 -> 7, 6 -> 11, 18 -> 17).withDefaultValue(0)
  val Sparse2 = Map(1 -> 3, 3 -> 3, 11 -> 2,18 -> 3, 20 -> 6).withDefaultValue(0)
  //println(Sparse2.toSeq)//to see what it is....0's missing
  val SparseSum = (Sparse1.toSeq ++ Sparse2.toSeq).groupBy(_._1).mapValues(_.map(_._2).sum)
  //println(SparseSum)
  val productOfValues = ((Sparse1.toSeq ++ Sparse2.toSeq).groupBy(_._1).mapValues(_.map(_._2).reduce(_*_)))
  //println(productOfValues)
  var dotProduct = 0
  for ((h,i) <- productOfValues) {
    dotProduct += i
  }
  //println(dotProduct)
  //If I specify some zero values, lets see what happens:
  val Sparse3 = Map(0 -> 4, 1 -> 0, 3 -> 7, 6 -> 11, 11 -> 0, 18 -> 17, 20 -> 0).withDefaultValue(0)
  val Sparse4 = Map(0 -> 0, 1 -> 3, 3 -> 3, 6 -> 0, 11 -> 2,18 -> 3, 20 -> 6).withDefaultValue(0)
  val productOfValues2 = ((Sparse3.toSeq ++ Sparse4.toSeq).groupBy(_._1).mapValues(_.map(_._2).reduce(_*_)))
  var dotProduct2 = 0
  for ((l, m) <- productOfValues2) {
    dotProduct2 += m
  }
  println(productOfValues2)
  println(dotProduct2)//I get 72

```

我可以通過這種方式創建一個稀疏向量,然后更新值

  import scala.collection.mutable.Map
  val Sparse1 = Map[Int, Int]()
  for (k <- 0 to 20) {
    Sparse1 getOrElseUpdate (k, 0)
  }
  val Sparse2 = Map[Int, Int]()
  for (k <- 0 to 20) {
    Sparse2 getOrElseUpdate (k, 0)
  }

但我想知道是否有“更好”的方法。 更多關於我嘗試使用“withDefaultValue(0)”但未能做到的事情

由於您使用的是稀疏向量,因此您可以忽略不在兩個向量上的所有鍵。
因此,我將計算兩個鍵集之間的intersection ,然后執行一個簡單的map-reduce來計算點積。

type SparseVector[T] = Map[Int, T]

/** Generic function for any type T that can be multiplied & summed. */
def sparseDotProduct[T: Numeric](v1: SparseVector[T], v2: SparseVector[T]): T = {
  import Numeric.Implicits._

  val commonIndexes = v1.keySet & v2.keySet

  commonIndexes
    .map(i => v1(i) * v2(i))
    .foldLeft(implicitly[Numeric[T]].zero)(_ + _)
}

然后,您可以像這樣使用它:

// The withDefault(0) is optional now.
val sparse1 = Map(0 -> 4, 3 -> 7, 6 -> 11, 18 -> 17).withDefaultValue(0)
val sparse2 = Map(1 -> 3, 3 -> 3, 11 -> 2, 18 -> 3, 20 -> 6).withDefaultValue(0)

sparseDotProduct(sparse1, sparse2)
// res: Int = 72

編輯 - 相同的方法,但沒有上下文邊界和隱式語法。

type SparseVector[T] = Map[Int, T]

/** Generic function for any type T that can be multiplied & summed. */
def sparseDotProduct[T](v1: SparseVector[T], v2: SparseVector[T])(implicit N: Numeric[T]): T = {      
  val commonIndexes = v1.keySet & v2.keySet

  commonIndexes
    .map(i => N.times(v1(i), v2(i)))
    .foldLeft(N.zero)((acc, element) => N.plus(acc, element))
}

獎勵 - 非備用向量的通用方法。

可以修改上述方法以適用於任何類型的向量,而不僅僅是備用。 在這種情況下,我們需要鍵的並union ,並考慮一個鍵在另一個上不存在的情況。

type MyVector[T] = Map[Int, T]

/** Generic function for any type T that can be multiplied & summed. */
def dotProduct[T: Numeric](v1: MyVector[T], v2: MyVector[T]): T = {
  import Numeric.Implicits._
  val zero = implicitly[Numeric[T]].zero

  val allIndexes = v1.keySet | v2.keySet

  allIndexes.map { i =>
     v1.getOrElse(
       key = i,
       default = zero
     ) * v2.getOrElse(
       key = i,
       default = zero
     )
   }.foldLeft(zero)(_ + _)
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM