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

    https://github.com/apache/spark/pull/17940#discussion_r117480337
  
    --- Diff: mllib/src/main/scala/org/apache/spark/mllib/linalg/Matrices.scala 
---
    @@ -992,7 +992,24 @@ object Matrices {
             new DenseMatrix(dm.rows, dm.cols, dm.data, dm.isTranspose)
           case sm: BSM[Double] =>
             // There is no isTranspose flag for sparse matrices in Breeze
    -        new SparseMatrix(sm.rows, sm.cols, sm.colPtrs, sm.rowIndices, 
sm.data)
    +
    +        // Some Breeze CSCMatrices may have extra trailing zeros in
    +        // .rowIndices and .data, which are added after some matrix
    +        // operations for efficiency.
    +        //
    +        // Therefore the last element of sm.colPtrs would no longer be
    +        // coherent with the size of sm.rowIndices and sm.data
    +        // despite sm being a valid CSCMatrix.
    +        // We need to truncate both arrays (rowIndices, data)
    +        // to the real size of the vector sm.activeSize to allow valid 
conversion
    +
    +        if (sm.rowIndices.length > sm.activeSize) {
    +          val smC = sm.copy
    +          smC.compact()
    +          new SparseMatrix(smC.rows, smC.cols, smC.colPtrs, 
smC.rowIndices, smC.data)
    +        } else {
    +          new SparseMatrix(sm.rows, sm.cols, sm.colPtrs, sm.rowIndices, 
sm.data)
    --- End diff --
    
    @hhbyyh what do you think of the current state? I wasn't clear if you were 
requesting a specific change to the comment.
    
    Here, if you're going to change it again, you could simplify by not 
repeating the `new SparseMatrix(...)` call. Just pick which sparse matrix 
you're copying in the if statement (original or compacted) and then return the 
result of converting that.


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