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

    https://github.com/apache/spark/pull/3319#discussion_r22120278
  
    --- Diff: mllib/src/main/scala/org/apache/spark/mllib/linalg/Matrices.scala 
---
    @@ -123,6 +135,130 @@ class DenseMatrix(val numRows: Int, val numCols: Int, 
val values: Array[Double])
       }
     
       override def copy = new DenseMatrix(numRows, numCols, values.clone())
    +
    +  private[mllib] def map(f: Double => Double) = new DenseMatrix(numRows, 
numCols, values.map(f))
    +
    +  private[mllib] def update(f: Double => Double): DenseMatrix = {
    +    val len = values.length
    +    var i = 0
    +    while (i < len) {
    +      values(i) = f(values(i))
    +      i += 1
    +    }
    +    this
    +  }
    +
    +  /** Generate a `SparseMatrix` from the given `DenseMatrix`. */
    +  def toSparse(): SparseMatrix = {
    +    val spVals: ArrayBuilder[Double] = new ArrayBuilder.ofDouble
    +    val colPtrs: Array[Int] = new Array[Int](numCols + 1)
    +    val rowIndices: ArrayBuilder[Int] = new ArrayBuilder.ofInt
    +    var nnz = 0
    +    var lastCol = -1
    +    var j = 0
    +    while (j < numCols) {
    +      var i = 0
    +      val indStart = j * numRows
    +      while (i < numRows) {
    +        val v = values(indStart + i)
    +        if (v != 0.0) {
    +          rowIndices += i
    +          spVals += v
    +          while (j != lastCol) {
    +            colPtrs(lastCol + 1) = nnz
    +            lastCol += 1
    +          }
    +          nnz += 1
    +        }
    +        i += 1
    +      }
    +      j += 1
    +    }
    +    while (numCols > lastCol) {
    --- End diff --
    
    This check is not necessary. At the end of the `while (j < numCols)` loop, 
`j = numCols + 1`. So it is `colPtrs(j) = nnz`.


---
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]

Reply via email to