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

    https://github.com/apache/spark/pull/2294#discussion_r17255336
  
    --- Diff: mllib/src/main/scala/org/apache/spark/mllib/linalg/Matrices.scala 
---
    @@ -63,7 +88,162 @@ class DenseMatrix(val numRows: Int, val numCols: Int, 
val values: Array[Double])
     
       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(r: Int, c: Int): Double = values(index(r, c))
    +
    +  private [mllib] def index(r: Int, c: Int): Int = r + numRows * c
    +
    +  private [mllib] def update(r: Int, c: Int, v: Double){
    +    values(index(r, c)) = v
    +  }
    +
    +  def copy = new DenseMatrix(numRows, numCols, values.clone())
    +}
    +
    +/**
    + * Factory methods for [[org.apache.spark.mllib.linalg.DenseMatrix]].
    + *
    + * These methods can be used to generate common matrix types such as the 
Identity matrix, any
    + * diagonal matrix, zero matrix, and random matrices.
    + */
    +object DenseMatrix {
    +
    +  /**
    +   * Generate a `DenseMatrix` consisting of zeros.
    +   * @param numRows number of rows of the matrix
    +   * @param numCols number of columns of the matrix
    +   * @return `DenseMatrix` with size `numRows` x `numCols` and values of 
zeros
    +   */
    +  def zeros(numRows: Int, numCols: Int) =
    +    new DenseMatrix(numRows, numCols, Array.fill(numRows * numCols)(0.0))
    +
    +  /**
    +   * Generate a `DenseMatrix` consisting of ones.
    +   * @param numRows number of rows of the matrix
    +   * @param numCols number of columns of the matrix
    +   * @return `DenseMatrix` with size `numRows` x `numCols` and values of 
ones
    +   */
    +  def ones(numRows: Int, numCols: Int) =
    +    new DenseMatrix(numRows, numCols, Array.fill(numRows * numCols)(1.0))
    +
    +  /**
    +   * Generate an Identity Matrix in `DenseMatrix` format.
    +   * @param n number of rows and columns of the matrix
    +   * @return `DenseMatrix` with size `n` x `n` and values of ones on the 
diagonal
    +   */
    +  def eye(n: Int) = {
    +    val identity = DenseMatrix.zeros(n,n)
    +    for (i <- 0 until n){
    +      identity.update(i, i, 1.0)
    +    }
    +    identity
    +  }
    +
    +  /**
    +   * Generate a `DenseMatrix` consisting of i.i.d. uniform random numbers.
    +   * @param numRows number of rows of the matrix
    +   * @param numCols number of columns of the matrix
    +   * @return `DenseMatrix` with size `numRows` x `numCols` and values in 
U(0, 1)
    +   */
    +  def rand(numRows: Int, numCols: Int) = {
    +    val rand = new scala.util.Random
    +    new DenseMatrix(numRows, numCols, Array.fill(numRows * 
numCols)(rand.nextDouble()))
    +  }
    +
    +  /**
    +   * Generate a `DenseMatrix` consisting of i.i.d. gaussian random numbers.
    +   * @param numRows number of rows of the matrix
    +   * @param numCols number of columns of the matrix
    +   * @return `DenseMatrix` with size `numRows` x `numCols` and values in 
N(0, 1)
    +   */
    +  def randn(numRows: Int, numCols: Int) = {
    +    val rand = new scala.util.Random
    +    new DenseMatrix(numRows, numCols, Array.fill(numRows * 
numCols)(rand.nextGaussian()))
    +  }
    +
    +  /**
    +   * Generate a diagonal matrix in `DenseMatrix` format from the supplied 
values.
    +   * @param values values on the diagonal of the matrix
    +   * @return Square `DenseMatrix` with size `values.length` x 
`values.length` and `values`
    +   *         on the diagonal
    +   */
    +  def diag(values: Array[Double]) = {
    +    val n = values.length
    +    val matrix = DenseMatrix.eye(n)
    +    for (i <- 0 until n) matrix.update(i, i, values(i))
    +    matrix
    +  }
    +}
    +
    +/**
    + * Column-majored sparse matrix.
    + * The entry values are stored in Compressed-Column Storage (CCS) 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 colPointers 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 colPointers: 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!")
    +  require(colPointers.length == numCols + 1, "The length of the column 
indices should be the " +
    +    s"number of columns + 1. Currently, colPointers.length: 
${colPointers.length}, " +
    +    s"numCols: $numCols")
    +
    +  override def toArray: Array[Double] = values
    --- End diff --
    
    This is wrong. `toArray` should return the dense representation stored in a 
column-majored flat array.


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