title: How to perform matrix dot product with a vector using Spark's RDD
body: <p>There is a matrix and I want to perform the dot product of it with a vector. Following is the Scala code:</p>
  
  <pre><code>val matrix = sc.parallelize(List(
    (("v1","v1"),2),(("v1","v2"),4),(("v1","v3"),1),(("v2","v2"),5),
    (("v2","v3"),1),(("v3","v3"),2)))
  
  val vector = sc.parallelize(List(("v1",4),("v2",1),("v3",5)))
  
  val dotproduct = matrix.flatMap{x =&gt; { 
    vector.flatMap { y =&gt; { 
      if(x._1._2 == y._1) Tuple2(x._1._1, x._2 * y._2)
    }}
  }}.reduceByKey((_,_) =&gt; _+_)
  </code></pre>
  
  <p>But the following error occurred: </p>
  
  <pre><code>&lt;console&gt;:25: error: type mismatch;
  found   : (String, Int)
  required: TraversableOnce[?]
  val dotproduct = matrix.flatMap{ x =&gt; { vector.flatMap { y =&gt; { if(x._1._2 == y._1) (x._1._1, x._2 * y._2) }}}}.reduceByKey((_,_) =&gt; _+_)
                                                                                           ^
  </code></pre>
  
  <p>I don't know if the nesting operation in RDD is OK. Does Spark MLlib provide any API to perform the dot product between matrix and vector?</p>