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

    https://github.com/apache/spark/pull/15628#discussion_r107442966
  
    --- Diff: 
mllib-local/src/main/scala/org/apache/spark/ml/linalg/Matrices.scala ---
    @@ -291,31 +395,60 @@ class DenseMatrix @Since("2.0.0") (
       override def numActives: Int = values.length
     
       /**
    -   * Generate a `SparseMatrix` from the given `DenseMatrix`. The new 
matrix will have isTransposed
    -   * set to false.
    +   * Generate a `SparseMatrix` from the given `DenseMatrix`.
    +   *
    +   * @param colMajor Whether the resulting `SparseMatrix` values will be 
in column major order.
        */
    -  @Since("2.0.0")
    -  def toSparse: SparseMatrix = {
    -    val spVals: MArrayBuilder[Double] = new MArrayBuilder.ofDouble
    -    val colPtrs: Array[Int] = new Array[Int](numCols + 1)
    -    val rowIndices: MArrayBuilder[Int] = new MArrayBuilder.ofInt
    -    var nnz = 0
    -    var j = 0
    -    while (j < numCols) {
    -      var i = 0
    -      while (i < numRows) {
    -        val v = values(index(i, j))
    -        if (v != 0.0) {
    -          rowIndices += i
    -          spVals += v
    -          nnz += 1
    +  private[ml] override def toSparseMatrix(colMajor: Boolean): SparseMatrix 
= {
    +    if (!colMajor) this.transpose.toSparseMatrix(colMajor = true).transpose
    +    else {
    +      val spVals: MArrayBuilder[Double] = new MArrayBuilder.ofDouble
    +      val colPtrs: Array[Int] = new Array[Int](numCols + 1)
    +      val rowIndices: MArrayBuilder[Int] = new MArrayBuilder.ofInt
    +      var nnz = 0
    +      var j = 0
    +      while (j < numCols) {
    +        var i = 0
    +        while (i < numRows) {
    +          val v = values(index(i, j))
    +          if (v != 0.0) {
    +            rowIndices += i
    +            spVals += v
    +            nnz += 1
    +          }
    +          i += 1
             }
    -        i += 1
    +        j += 1
    +        colPtrs(j) = nnz
           }
    -      j += 1
    -      colPtrs(j) = nnz
    +      new SparseMatrix(numRows, numCols, colPtrs, rowIndices.result(), 
spVals.result())
    +    }
    +  }
    +
    +  /**
    +   * Generate a `DenseMatrix` from this `DenseMatrix`.
    +   *
    +   * @param colMajor Whether the resulting `DenseMatrix` values will be in 
column major order.
    +   */
    +  private[ml] override def toDenseMatrix(colMajor: Boolean): DenseMatrix = 
{
    +    if (!(isTransposed ^ colMajor)) {
    +      val newValues = new Array[Double](numCols * numRows)
    --- End diff --
    
    I don't think we can put this in the trait, since when it is called on 
dense matrix we would like to return `this` in some cases (when no layout 
change is needed). But, yes, I think it simpler to use `toArray` which calls 
`foreachActive`. Thanks!


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