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

    https://github.com/apache/spark/pull/15628#discussion_r107832751
  
    --- Diff: 
mllib-local/src/main/scala/org/apache/spark/ml/linalg/Matrices.scala ---
    @@ -587,18 +720,67 @@ class SparseMatrix @Since("2.0.0") (
         }
       }
     
    +  override def numNonzeros: Int = values.count(_ != 0)
    +
    +  override def numActives: Int = values.length
    +
       /**
    -   * Generate a `DenseMatrix` from the given `SparseMatrix`. The new 
matrix will have isTransposed
    -   * set to false.
    +   * Generate a `SparseMatrix` from this `SparseMatrix`, removing explicit 
zero values if they
    +   * exist.
    +   *
    +   * @param colMajor Whether or not the resulting `SparseMatrix` values 
are in column major
    +   *                    order.
        */
    -  @Since("2.0.0")
    -  def toDense: DenseMatrix = {
    -    new DenseMatrix(numRows, numCols, toArray)
    +  private[ml] override def toSparseMatrix(colMajor: Boolean): SparseMatrix 
= {
    +    if (!isTransposed && !colMajor) {
    +      // it is row major and we want col major, use breeze to remove 
explicit zeros
    +      val breezeTransposed = asBreeze.asInstanceOf[BSM[Double]].t
    +      
Matrices.fromBreeze(breezeTransposed).transpose.asInstanceOf[SparseMatrix]
    +    } else if (isTransposed && colMajor) {
    +      // it is col major and we want row major, use breeze to remove 
explicit zeros
    +      val breezeTransposed = asBreeze.asInstanceOf[BSM[Double]]
    +      Matrices.fromBreeze(breezeTransposed).asInstanceOf[SparseMatrix]
    +    } else {
    +      val nnz = numNonzeros
    +      if (nnz != numActives) {
    +        // remove explicit zeros
    +        val rr = new Array[Int](nnz)
    +        val vv = new Array[Double](nnz)
    +        val numPtrs = if (isTransposed) numRows else numCols
    +        val cc = new Array[Int](numPtrs + 1)
    +        var nzIdx = 0
    +        var j = 0
    +        while (j < numPtrs) {
    +          var idx = colPtrs(j)
    +          val idxEnd = colPtrs(j + 1)
    +          cc(j) = nzIdx
    +          while (idx < idxEnd) {
    +            if (values(idx) != 0.0) {
    +              vv(nzIdx) = values(idx)
    +              rr(nzIdx) = rowIndices(idx)
    +              nzIdx += 1
    +            }
    +            idx += 1
    +          }
    +          j += 1
    +        }
    +        cc(j) = nnz
    +        new SparseMatrix(numRows, numCols, cc, rr, vv, isTransposed = 
isTransposed)
    +      } else {
    +        this
    +      }
    +    }
       }
     
    -  override def numNonzeros: Int = values.count(_ != 0)
    -
    -  override def numActives: Int = values.length
    +  /**
    +   * Generate a `DenseMatrix` from the given `SparseMatrix`.
    +   *
    +   * @param colMajor Whether the resulting `DenseMatrix` values are in 
column major order.
    +   */
    +  private[ml] override def toDenseMatrix(colMajor: Boolean): DenseMatrix = 
{
    +    if (colMajor) new DenseMatrix(numRows, numCols, toArray)
    --- End diff --
    
    `new DenseMatrix(numRows, numCols, this.toArray, isTransposed = false)` to 
make the style consistent. 


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