Skip to content

Commit 0fb1012

Browse files
committed
hierarchy R computing
1 parent 3fbdb61 commit 0fb1012

File tree

2 files changed

+13
-8
lines changed

2 files changed

+13
-8
lines changed

mllib/src/main/scala/org/apache/spark/mllib/linalg/distributed/RowMatrix.scala

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -510,32 +510,33 @@ class RowMatrix(
510510
def tallSkinnyQR(computeQ: Boolean = false): QRDecomposition[RowMatrix, Matrix] = {
511511
val col = numCols().toInt
512512
// split rows horizontally into smaller matrices, and compute QR for each of them
513-
val blockQRs = rows.glom().map{ partRows =>
513+
val blockQRs = rows.glom().map { partRows =>
514514
val bdm = BDM.zeros[Double](partRows.length, col)
515515
var i = 0
516-
partRows.foreach{ row =>
516+
partRows.foreach { row =>
517517
bdm(i, ::) := row.toBreeze.t
518518
i += 1
519519
}
520520
breeze.linalg.qr.reduced(bdm).r
521-
}.cache()
521+
}
522522

523523
// combine the R part from previous results vertically into a tall matrix
524-
val combinedR = blockQRs.treeReduce((r1, r2) => BDM.vertcat(r1, r2))
524+
val combinedR = blockQRs.treeReduce{ (r1, r2) =>
525+
val partialR = BDM.vertcat(r1, r2)
526+
breeze.linalg.qr.reduced(partialR).r
527+
}
525528
val breezeR = breeze.linalg.qr.reduced(combinedR).r.toDenseMatrix
526529
val finalR = Matrices.fromBreeze(breezeR)
527530
val finalQ = if (computeQ) {
528531
try {
529532
val invR = inv(breezeR)
530533
this.multiply(Matrices.fromBreeze(invR))
531-
}
532-
catch {
534+
} catch {
533535
case err: MatrixSingularException =>
534536
logWarning("R is not invertible and return Q as null")
535537
null
536538
}
537-
}
538-
else {
539+
} else {
539540
null
540541
}
541542
QRDecomposition(finalQ, finalR)

mllib/src/test/scala/org/apache/spark/mllib/linalg/distributed/RowMatrixSuite.scala

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,10 @@ class RowMatrixSuite extends SparkFunSuite with MLlibTestSparkContext {
249249
assert(closeToZero(abs(expected.q) - abs(calcQ.toBreeze())))
250250
assert(closeToZero(abs(expected.r) - abs(calcR.toBreeze.asInstanceOf[BDM[Double]])))
251251
assert(closeToZero(calcQ.multiply(calcR).toBreeze - mat.toBreeze()))
252+
// Decomposition without computing Q
253+
val rOnly = mat.tallSkinnyQR(computeQ = false)
254+
assert(rOnly.Q == null)
255+
assert(closeToZero(abs(expected.r) - abs(rOnly.R.toBreeze.asInstanceOf[BDM[Double]])))
252256
}
253257
}
254258
}

0 commit comments

Comments
 (0)