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

    https://github.com/apache/spark/pull/15628#discussion_r107549283
  
    --- 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 --
    
    Hm, I don't think this solution is better. The entire point of abstract 
methods is to allow subclasses to implement a method differently. Since we need 
different implementations depending on the subclass, we should just implement 
them in the subclasses. We can do this with the following:
    
    ````scala
    trait Matrix {
      def toDenseMatrix(colMajor: Boolean): Matrix
    }
    class DenseMatrix extends Matrix {
        private[ml] override def toDenseMatrix(colMajor: Boolean): DenseMatrix 
= {
        if (isTransposed && colMajor) {
          new DenseMatrix(numRows, numCols, toArray, isTransposed = false)
        } else if (!isTransposed && !colMajor) {
          new DenseMatrix(numRows, numCols, transpose.toArray, isTransposed = 
true)
        } else {
          this
        }
      }
    }
    class SparseMatrix extends Matrix {
      private[ml] override def toDenseMatrix(colMajor: Boolean): DenseMatrix = {
        if (colMajor) new DenseMatrix(numRows, numCols, toArray)
        else new DenseMatrix(numRows, numCols, this.transpose.toArray, 
isTransposed = true)
      }
    }
    ````
    
    Which is less verbose than the previous code. I'm going to put that in the 
next commit. Let me know what you think.


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

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org
For additional commands, e-mail: reviews-h...@spark.apache.org

Reply via email to