Github user rxin commented on a diff in the pull request:

    https://github.com/apache/spark/pull/88#discussion_r10739162
  
    --- Diff: mllib/src/main/scala/org/apache/spark/mllib/linalg/SVD.scala ---
    @@ -142,17 +172,189 @@ object SVD {
         val vsirdd = sc.makeRDD(Array.tabulate(V.rows, sigma.length)
                     { (i,j) => ((i, j), V.get(i,j) / sigma(j))  }.flatten)
     
    -    // Multiply A by VS^-1
    -    val aCols = data.map(entry => (entry.j, (entry.i, entry.mval)))
    -    val bRows = vsirdd.map(entry => (entry._1._1, (entry._1._2, entry._2)))
    -    val retUdata = aCols.join(bRows).map( {case (key, ( (rowInd, rowVal), 
(colInd, colVal)))
    -        => ((rowInd, colInd), rowVal*colVal)}).reduceByKey(_ + _)
    -          .map{ case ((row, col), mval) => MatrixEntry(row, col, mval)}
    -    val retU = SparseMatrix(retUdata, m, sigma.length)
    -   
    -    MatrixSVD(retU, retS, retV)  
    +    if (computeU) {
    +      // Multiply A by VS^-1
    +      val aCols = data.map(entry => (entry.j, (entry.i, entry.mval)))
    +      val bRows = vsirdd.map(entry => (entry._1._1, (entry._1._2, 
entry._2)))
    +      val retUdata = aCols.join(bRows).map {
    +        case (key, ( (rowInd, rowVal), (colInd, colVal)) ) => 
    +          ((rowInd, colInd), rowVal * colVal)
    +      }.reduceByKey(_ + _).map{ case ((row, col), mval) => 
MatrixEntry(row, col, mval)}
    +      
    +      val retU = SparseMatrix(retUdata, m, sigma.length)
    +      MatrixSVD(retU, retS, retV)  
    +    } else {
    +      MatrixSVD(null, retS, retV)
    +    }
    +  }
    +
    +/**
    + * Singular Value Decomposition for Tall and Skinny matrices.
    + * Given an m x n matrix A, this will compute matrices U, S, V such that
    + * A = U * S * V'
    + * 
    + * There is no restriction on m, but we require n^2 doubles to fit in 
memory.
    + * Further, n should be less than m.
    + * 
    + * The decomposition is computed by first computing A'A = V S^2 V',
    + * computing svd locally on that (since n x n is small),
    + * from which we recover S and V. 
    + * Then we compute U via easy matrix multiplication
    + * as U =  A * V * S^-1
    + * 
    + * Only the k largest singular values and associated vectors are found.
    + * If there are k such values, then the dimensions of the return will be:
    + *
    + * S is k x k and diagonal, holding the singular values on diagonal
    + * U is m x k and satisfies U'U = eye(k)
    + * V is n x k and satisfies V'V = eye(k)
    + *
    + * @param matrix dense matrix to factorize
    + * @param k Recover k singular values and vectors
    + * @param computeU gives the option of skipping the U computation
    + * @param rcond smallest singular value considered nonzero
    + * @return Three dense matrices: U, S, V such that A = USV^T
    + */
    + private def denseSVD(matrix: TallSkinnyDenseMatrix, k: Int,
    +              computeU: Boolean, rcond: Double): TallSkinnyMatrixSVD = {
    +    val rows = matrix.rows
    +    val m = matrix.m
    +    val n = matrix.n
    +    val sc = matrix.rows.sparkContext
    +
    +    if (m < n || m <= 0 || n <= 0) {
    +      throw new IllegalArgumentException("Expecting a tall and skinny 
matrix m=" + m + " n=" + n)
    +    }
    +
    +    if (k < 1 || k > n) {
    +      throw new IllegalArgumentException("Request up to n singular values 
n=" + n + " k=" + k)
    +    }
    +
    +    val rowIndices = matrix.rows.map(_.i)
    +
    +    // compute SVD
    +    val (u, sigma, v) = denseSVD(matrix.rows.map(_.data), k, computeU, 
rcond)
    +    
    +    if(computeU) {
    +      // prep u for returning
    +      val retU = TallSkinnyDenseMatrix(u.zip(rowIndices).map{
    +                  case (row, i) => MatrixRow(i, row) }, m, k)
    +    
    +      TallSkinnyMatrixSVD(retU, sigma, v)
    +    } else {
    +      TallSkinnyMatrixSVD(null, sigma, v)
    +    }
    + }
    +
    +/**
    + * Singular Value Decomposition for Tall and Skinny matrices.
    + * Given an m x n matrix A, this will compute matrices U, S, V such that
    + * A = U * S * V'
    + * 
    + * There is no restriction on m, but we require n^2 doubles to fit in 
memory.
    + * Further, n should be less than m.
    + * 
    + * The decomposition is computed by first computing A'A = V S^2 V',
    + * computing svd locally on that (since n x n is small),
    + * from which we recover S and V. 
    + * Then we compute U via easy matrix multiplication
    + * as U =  A * V * S^-1
    + * 
    + * Only the k largest singular values and associated vectors are found.
    + * If there are k such values, then the dimensions of the return will be:
    + *
    + * S is k x k and diagonal, holding the singular values on diagonal
    + * U is m x k and satisfies U'U = eye(k)
    + * V is n x k and satisfies V'V = eye(k)
    + *
    + * The return values are as lean as possible: an RDD of rows for U,
    + * a simple array for sigma, and a dense 2d matrix array for V
    + *
    + * @param matrix dense matrix to factorize
    + * @param k Recover k singular values and vectors
    + * @return Three matrices: U, S, V such that A = USV^T
    + */
    + private def denseSVD(matrix: RDD[Array[Double]], k: Int,
    +                      computeU: Boolean, rcond: Double) : 
    +               (RDD[Array[Double]], Array[Double], Array[Array[Double]])  
= {
    +    val n = matrix.first.size
    +
    +    if (k < 1 || k > n) {
    +      throw new IllegalArgumentException(
    +        "Request up to n singular valuesi k=" + k + " n= " + n)
    +    }
    +
    +    // Compute A^T A
    +    val fullata = matrix.mapPartitions{
    --- End diff --
    
    add a space after mapParititons, put iter on the same line, and only indent 
the body using two spaces


---
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 infrastruct...@apache.org or file a JIRA ticket
with INFRA.
---

Reply via email to