Github user mengxr commented on a diff in the pull request: https://github.com/apache/spark/pull/2294#discussion_r17255317 --- 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 --- End diff -- use `XORShiftRandom` for speed
--- 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