Scala Spark中两个数组之间的交叉产品

时间:2017-11-22 09:51:50

标签: arrays scala apache-spark

考虑scala-spark中的两个数组

val mole_1 = Array(2.0,1.0,0.0,0.0,0.0,0.0)
val mole_2 = Array(2.0,2.0,0.0,0.0,0.0,0.0)

现在,两个数组的元素乘法的有效方法是

val dot_Mole = (mole_1, mole_2).zipped.map(_ * _).sum

Which yields  >> 6.0

如何使用zipped和map来实现这些数组之间的交叉产品,以提供>> 12.0

仅使用zipped, map and reduce

1 个答案:

答案 0 :(得分:3)

你可以这样做:

val dot_mole = Mole_1.map{ x => Mole_2.map(b => b * x)}.flatten.sum
dot_mole: Double = 12.0