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

    https://github.com/apache/spark/pull/2294#discussion_r17709088
  
    --- Diff: mllib/src/main/scala/org/apache/spark/mllib/linalg/Matrices.scala 
---
    @@ -59,11 +93,113 @@ trait Matrix extends Serializable {
      */
     class DenseMatrix(val numRows: Int, val numCols: Int, val values: 
Array[Double]) extends Matrix {
     
    -  require(values.length == numRows * numCols)
    +  require(values.length == numRows * numCols, "The number of values 
supplied doesn't match the " +
    +    s"size of the matrix! values.length: ${values.length}, numRows * 
numCols: ${numRows * numCols}")
     
       override def toArray: Array[Double] = values
     
    -  private[mllib] override def toBreeze: BM[Double] = new 
BDM[Double](numRows, numCols, values)
    +  private[mllib] def toBreeze: BM[Double] = new BDM[Double](numRows, 
numCols, values)
    +
    +  private[mllib] def apply(i: Int): Double = values(i)
    +
    +  private[mllib] def apply(i: Int, j: Int): Double = values(index(i, j))
    +
    +  private[mllib] def index(i: Int, j: Int): Int = i + numRows * j
    +
    +  private[mllib] def update(i: Int, j: Int, v: Double){
    +    values(index(i, j)) = v
    +  }
    +
    +  override def copy = new DenseMatrix(numRows, numCols, values.clone())
    +}
    +
    +/**
    + * Column-majored sparse matrix.
    + * The entry values are stored in Compressed Sparse Column (CSC) format.
    + * For example, the following matrix
    + * {{{
    + *   1.0 0.0 4.0
    + *   0.0 3.0 5.0
    + *   2.0 0.0 6.0
    + * }}}
    + * is stored as `values: [1.0, 2.0, 3.0, 4.0, 5.0, 6.0]`,
    + * `rowIndices=[0, 2, 1, 0, 1, 2]`, `colPointers=[0, 2, 3, 6]`.
    + *
    + * @param numRows number of rows
    + * @param numCols number of columns
    + * @param colPtrs the index corresponding to the start of a new column
    + * @param rowIndices the row index of the entry
    + * @param values non-zero matrix entries in column major
    + */
    +class SparseMatrix(
    +    val numRows: Int,
    +    val numCols: Int,
    +    val colPtrs: Array[Int],
    +    val rowIndices: Array[Int],
    +    val values: Array[Double]) extends Matrix {
    +
    +  require(values.length == rowIndices.length, "The number of row indices 
and values don't match! " +
    +    s"values.length: ${values.length}, rowIndices.length: 
${rowIndices.length}")
    +  require(colPtrs.length == numCols + 1, "The length of the column indices 
should be the " +
    +    s"number of columns + 1. Currently, colPointers.length: 
${colPtrs.length}, " +
    +    s"numCols: $numCols")
    +
    +  override def toArray: Array[Double] = {
    +    val arr = Array.fill(numRows * numCols)(0.0)
    +    var j = 0
    +    while (j < numCols) {
    +      var i = colPtrs(j)
    +      val indEnd = colPtrs(j + 1)
    +      val offset = j * numRows
    +      while (i < indEnd) {
    +        val rowIndex = rowIndices(i)
    +        arr(offset + rowIndex) = values(i)
    +        i += 1
    +      }
    +      j += 1
    +    }
    +    arr
    +  }
    +
    +  private[mllib] def toBreeze: BM[Double] =
    +    new BSM[Double](values, numRows, numCols, colPtrs, rowIndices)
    +
    +  private[mllib] def apply(i: Int): Double = values(i)
    +
    +  private[mllib] def apply(i: Int, j: Int): Double = {
    +    val ind = index(i, j)
    +    if (ind == -1) 0.0 else values(ind)
    +  }
    +
    +  private[mllib] def index(i: Int, j: Int): Int = {
    +    var regionStart = colPtrs(j)
    +    var regionEnd = colPtrs(j + 1)
    +    while (regionStart <= regionEnd) {
    +      val mid = (regionStart + regionEnd) / 2
    +      if (rowIndices(mid) == i){
    +        return mid
    +      } else if (regionStart == regionEnd) {
    +        return -1
    +      } else if (rowIndices(mid) > i) {
    +        regionEnd = mid
    +      } else {
    +        regionStart = mid
    +      }
    +    }
    +    -1
    +  }
    +
    +  private[mllib] def update(i: Int, j: Int, v: Double){
    --- End diff --
    
    `: Unit = {`


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