mongolol mongolol - 1 month ago 9
Scala Question

Combining Block Matrices into a Larger Block Matrix

I have a BlockMatrix M which represents the neighbors of one set of vertices in a bipartite graph. I was hoping to construct a full adjacency matrix for the graph of the form:

Adjacency Matrix

However, I am unsure of an efficient way to go about combining block matrices. Any assistance would be greatly appreciated. I was hoping to just use

M
and
M.transpose
.

Answer

Here is what you can do. Let's say you have a simple matrix defined as:

import org.apache.spark.mllib.linalg.distributed._

val rowsPerBlock = 2
val colsPerBlock = rowsPerBlock

val mat = new CoordinateMatrix(sc.parallelize(Seq(
  MatrixEntry(5, 0, 1), MatrixEntry(4, 1, 2),
  MatrixEntry(3, 2, 3), MatrixEntry(2, 3, 4),
  MatrixEntry(1, 4, 5), MatrixEntry(0, 5, 6)
))).toBlockMatrix(rowsPerBlock, colsPerBlock)

which give us:

0.0  0.0  0.0  0.0  0.0  6.0
0.0  0.0  0.0  0.0  5.0  0.0
0.0  0.0  0.0  4.0  0.0  0.0
0.0  0.0  3.0  0.0  0.0  0.0
0.0  2.0  0.0  0.0  0.0  0.0
1.0  0.0  0.0  0.0  0.0  0.0

We can find number of rows and number of blocks:

val n = mat.numRows
val m = n / rowsPerBlock

And this two create two RDDs, one shifted by m blocks to right:

val ur = mat.blocks.map { case ((i, j), mat) => ((i, j + m), mat) }

and one transposed and shifted m blocks down:

val lr = mat.transpose.blocks.map { case ((i, j), mat) => ((i + m, j),mat) }

Finally we can combine these two into a single matrix:

new BlockMatrix(
  ur ++ lr,
  rowsPerBlock, colsPerBlock
)

with result being:

0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  6.0
0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  5.0  0.0
0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  4.0  0.0  0.0
0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  3.0  0.0  0.0  0.0
0.0  0.0  0.0  0.0  0.0  0.0  0.0  2.0  0.0  0.0  0.0  0.0
0.0  0.0  0.0  0.0  0.0  0.0  1.0  0.0  0.0  0.0  0.0  0.0
0.0  0.0  0.0  0.0  0.0  1.0  0.0  0.0  0.0  0.0  0.0  0.0
0.0  0.0  0.0  0.0  2.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0
0.0  0.0  0.0  3.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0
0.0  0.0  4.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0
0.0  5.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0
6.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0
Comments