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

    https://github.com/apache/spark/pull/15628#discussion_r107352903
  
    --- Diff: 
mllib-local/src/main/scala/org/apache/spark/ml/linalg/Matrices.scala ---
    @@ -291,31 +396,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)
    +      var j = 0
    --- End diff --
    
    Can we move `if (isTransposed) {` out of a loop like the following since it 
is a loop invariant and we want to remove div/mod operations?
    ```
    if (isTransposed) {
      // it is row major and we want column major
      var j = 0
      var col = 0
      while (col < numCols) {
        var row = 0
        while (row < numRows) {
          ...
        }
        col += 1
      }
    } else {
      // it is column major and we want row major
      var j = 0
      var row = 0
      while (row < numRows) {
        var col = 0
        while (col < numCols) {
          ...
        }
        rows += 1
      }
    }
    ```



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