Github user mengxr commented on a diff in the pull request:
https://github.com/apache/spark/pull/3319#discussion_r22120308
--- Diff: mllib/src/main/scala/org/apache/spark/mllib/linalg/Matrices.scala
---
@@ -256,72 +555,244 @@ object Matrices {
* 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
+ * @return `Matrix` with size `numRows` x `numCols` and values of zeros
*/
- def zeros(numRows: Int, numCols: Int): Matrix =
- new DenseMatrix(numRows, numCols, new Array[Double](numRows * numCols))
+ def zeros(numRows: Int, numCols: Int): Matrix =
DenseMatrix.zeros(numRows, numCols)
/**
* 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
+ * @return `Matrix` with size `numRows` x `numCols` and values of ones
*/
- def ones(numRows: Int, numCols: Int): Matrix =
- new DenseMatrix(numRows, numCols, Array.fill(numRows * numCols)(1.0))
+ def ones(numRows: Int, numCols: Int): Matrix = DenseMatrix.ones(numRows,
numCols)
/**
- * Generate an Identity Matrix in `DenseMatrix` format.
+ * Generate a dense Identity Matrix in `Matrix` 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
+ * @return `Matrix` with size `n` x `n` and values of ones on the
diagonal
*/
- def eye(n: Int): Matrix = {
- val identity = Matrices.zeros(n, n)
- var i = 0
- while (i < n){
- identity.update(i, i, 1.0)
- i += 1
- }
- identity
- }
+ def eye(n: Int): Matrix = DenseMatrix.eye(n)
+
+ /**
+ * Generate a sparse Identity Matrix in `Matrix` format.
+ * @param n number of rows and columns of the matrix
+ * @return `Matrix` with size `n` x `n` and values of ones on the
diagonal
+ */
+ def speye(n: Int): Matrix = SparseMatrix.speye(n)
/**
* 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
* @param rng a random number generator
- * @return `DenseMatrix` with size `numRows` x `numCols` and values in
U(0, 1)
+ * @return `Matrix` with size `numRows` x `numCols` and values in U(0, 1)
*/
- def rand(numRows: Int, numCols: Int, rng: Random): Matrix = {
- new DenseMatrix(numRows, numCols, Array.fill(numRows *
numCols)(rng.nextDouble()))
- }
+ def rand(numRows: Int, numCols: Int, rng: Random): Matrix =
+ DenseMatrix.rand(numRows, numCols, rng)
+
+ /**
+ * Generate a `SparseMatrix` consisting of i.i.d. gaussian random
numbers.
+ * @param numRows number of rows of the matrix
+ * @param numCols number of columns of the matrix
+ * @param density the desired density for the matrix
+ * @param rng a random number generator
+ * @return `Matrix` with size `numRows` x `numCols` and values in U(0, 1)
+ */
+ def sprand(numRows: Int, numCols: Int, density: Double, rng: Random):
Matrix =
+ SparseMatrix.sprand(numRows, numCols, density, rng)
/**
* 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
* @param rng a random number generator
- * @return `DenseMatrix` with size `numRows` x `numCols` and values in
N(0, 1)
+ * @return `Matrix` with size `numRows` x `numCols` and values in N(0, 1)
*/
- def randn(numRows: Int, numCols: Int, rng: Random): Matrix = {
- new DenseMatrix(numRows, numCols, Array.fill(numRows *
numCols)(rng.nextGaussian()))
- }
+ def randn(numRows: Int, numCols: Int, rng: Random): Matrix =
+ DenseMatrix.randn(numRows, numCols, rng)
+
+ /**
+ * Generate a `SparseMatrix` consisting of i.i.d. gaussian random
numbers.
+ * @param numRows number of rows of the matrix
+ * @param numCols number of columns of the matrix
+ * @param density the desired density for the matrix
+ * @param rng a random number generator
+ * @return `Matrix` with size `numRows` x `numCols` and values in N(0, 1)
+ */
+ def sprandn(numRows: Int, numCols: Int, density: Double, rng: Random):
Matrix =
+ SparseMatrix.sprandn(numRows, numCols, density, rng)
/**
* Generate a diagonal matrix in `DenseMatrix` format from the supplied
values.
* @param vector a `Vector` tat will form the values on the diagonal of
the matrix
- * @return Square `DenseMatrix` with size `values.length` x
`values.length` and `values`
+ * @return Square `Matrix` with size `values.length` x `values.length`
and `values`
* on the diagonal
*/
- def diag(vector: Vector): Matrix = {
- val n = vector.size
- val matrix = Matrices.eye(n)
- val values = vector.toArray
- var i = 0
- while (i < n) {
- matrix.update(i, i, values(i))
- i += 1
+ def diag(vector: Vector): Matrix = DenseMatrix.diag(vector)
+
+ /**
+ * Horizontally concatenate a sequence of matrices. The returned matrix
will be in the format
+ * the matrices are supplied in. Supplying a mix of dense and sparse
matrices will result in
+ * a sparse matrix. If the Array is empty, an empty `DenseMatrix` will
be returned.
+ * @param matrices array of matrices
+ * @return a single `Matrix` composed of the matrices that were
horizontally concatenated
+ */
+ def horzcat(matrices: Array[Matrix]): Matrix = {
+ if (matrices.isEmpty) {
+ return new DenseMatrix(0, 0, Array[Double]())
+ } else if (matrices.size == 1) {
+ return matrices(0)
+ }
+ val numRows = matrices(0).numRows
+ var rowsMatch = true
+ var hasSparse = false
+ var numCols = 0
+ matrices.foreach { mat =>
+ if (numRows != mat.numRows) rowsMatch = false
+ mat match {
+ case sparse: SparseMatrix => hasSparse = true
+ case dense: DenseMatrix => // empty on purpose
+ case _ => throw new IllegalArgumentException("Unsupported matrix
format. Expected " +
+ s"SparseMatrix or DenseMatrix. Instead got: ${mat.getClass}")
+ }
+ numCols += mat.numCols
+ }
+ require(rowsMatch, "The number of rows of the matrices in this
sequence, don't match!")
--- End diff --
This could be moved inside the `foreach` block. Then we can remove
`rowsMatch`.
---
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]