更新时间:2021-10-28 21:51:22
lambda
functions, we have to reference the MatrixEntry
by a single variable e
.MatrixEntry
is not indexable so we must call the individual properties i
, j
and value
.def coordinateMatrixMultiply(leftmatrix, rightmatrix):
left = leftmatrix.entries.map(lambda e: (e.j, (e.i, e.value)))
right = rightmatrix.entries.map(lambda e: (e.i, (e.j, e.value)))
productEntries = left \
.join(right) \
.map(lambda e: ((e[1][0][0], e[1][1][0]), (e[1][0][1]*e[1][1][1]))) \
.reduceByKey(lambda x,y: x+y) \
.map(lambda e: (*e[0], e[1]))
return productEntries