Skip to content

Commit 9c80515

Browse files
author
Li Pu
committed
use non-sparse implementation when k = n
1 parent fe983b0 commit 9c80515

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

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

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,9 @@ class RowMatrix(
246246
* Then we compute U via easy matrix multiplication as U = A * (V * S-1).
247247
* Note that this approach requires `O(nnz(A))` time.
248248
*
249+
* When the requested eigenvalues k = n, a non-sparse implementation will be used, which requires
250+
* `n^2` doubles to fit in memory and `O(n^3)` time on the master node.
251+
*
249252
* At most k largest non-zero singular values and associated vectors are returned.
250253
* If there are k such values, then the dimensions of the return will be:
251254
*
@@ -269,8 +272,16 @@ class RowMatrix(
269272
val n = numCols().toInt
270273
require(k > 0 && k <= n, s"Request up to n singular values k=$k n=$n.")
271274

272-
val (sigmaSquares: BDV[Double], u: BDM[Double]) =
275+
val (sigmaSquares: BDV[Double], u: BDM[Double]) = if (k < n) {
273276
EigenValueDecomposition.symmetricEigs(multiplyGramianMatrix, n, k, tol)
277+
} else {
278+
logWarning(s"Request full SVD (k = n = $k), while ARPACK requires k strictly less than n. " +
279+
s"Using non-sparse implementation.")
280+
val G = computeGramianMatrix()
281+
val (uFull: BDM[Double], sigmaSquaresFull: BDV[Double], vFull: BDM[Double]) =
282+
brzSvd(G.toBreeze.asInstanceOf[BDM[Double]])
283+
(sigmaSquaresFull, uFull)
284+
}
274285
val sigmas: BDV[Double] = brzSqrt(sigmaSquares)
275286

276287
// Determine effective rank.
@@ -508,4 +519,4 @@ object RowMatrix {
508519

509520
Matrices.dense(n, n, G.data)
510521
}
511-
}
522+
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ class RowMatrixSuite extends FunSuite with LocalSparkContext {
9999
val localMat = mat.toBreeze()
100100
val (localU, localSigma, localVt) = brzSvd(localMat)
101101
val localV: BDM[Double] = localVt.t.toDenseMatrix
102-
for (k <- 1 to (n - 1)) {
102+
for (k <- 1 to n) {
103103
val svd = mat.computeSVD(k, computeU = true)
104104
val U = svd.U
105105
val s = svd.s

0 commit comments

Comments
 (0)