Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ```
- 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
- ```
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement