Github user mengxr commented on a diff in the pull request:
https://github.com/apache/spark/pull/5909#discussion_r35774677
--- Diff:
mllib/src/main/scala/org/apache/spark/mllib/linalg/distributed/RowMatrix.scala
---
@@ -498,6 +498,50 @@ class RowMatrix(
}
/**
+ * Compute QR decomposition for [[RowMatrix]]. The implementation is
designed to optimize the QR
+ * decomposition (factorization) for the [[RowMatrix]] of a tall and
skinny shape.
+ * Reference:
+ * Paul G. Constantine, David F. Gleich. "Tall and skinny QR
factorizations in MapReduce
+ * architectures" ([[http://dx.doi.org/10.1145/1996092.1996103]])
+ *
+ * @param computeQ: whether to computeQ
+ * @return QRDecomposition(Q, R), Q = null if computeQ = false.
+ */
+ def tallSkinnyQR(computeQ: Boolean = false): QRDecomposition[RowMatrix,
Matrix] = {
+ val col = numCols().toInt
+ // split rows horizontally into smaller matrices, and compute QR for
each of them
+ val blockQRs = rows.glom().map{ partRows =>
+ val bdm = BDM.zeros[Double](partRows.length, col)
+ var i = 0
+ partRows.foreach{ row =>
+ bdm(i, ::) := row.toBreeze.t
+ i += 1
+ }
+ breeze.linalg.qr.reduced(bdm).r
+ }.cache()
+
+ // combine the R part from previous results vertically into a tall
matrix
+ val combinedR = blockQRs.treeReduce((r1, r2) => BDM.vertcat(r1, r2))
+ val breezeR = breeze.linalg.qr.reduced(combinedR).r.toDenseMatrix
+ val finalR = Matrices.fromBreeze(breezeR)
+ val finalQ = if (computeQ) {
+ try {
+ val invR = inv(breezeR)
+ this.multiply(Matrices.fromBreeze(invR))
+ }
+ catch {
+ case err: MatrixSingularException =>
+ logWarning("R is not invertible and return Q as null")
+ null
+ }
+ }
+ else {
--- End diff --
same here: `} else {`
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]