Skip to content

Commit 065b531

Browse files
committed
[SPARK-4409] First pass after code review
1 parent a8120d2 commit 065b531

File tree

1 file changed

+26
-22
lines changed

1 file changed

+26
-22
lines changed

mllib/src/main/scala/org/apache/spark/mllib/linalg/Matrices.scala

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -82,10 +82,14 @@ sealed trait Matrix extends Serializable {
8282
/** A human readable representation of the matrix */
8383
override def toString: String = toBreeze.toString()
8484

85-
/** Map the values of this matrix using a function. Generates a new matrix. */
85+
/** Map the values of this matrix using a function. Generates a new matrix. Performs the
86+
* function on only the backing array. For example, an operation such as addition or
87+
* subtraction will only be performed on the non-zero values in a `SparseMatrix`. */
8688
private[mllib] def map(f: Double => Double): Matrix
8789

88-
/** Update all the values of this matrix using the function f. Performed in-place. */
90+
/** Update all the values of this matrix using the function f. Performed in-place on the
91+
* backing array. For example, an operation such as addition or subtraction will only be
92+
* performed on the non-zero values in a `SparseMatrix`. */
8993
private[mllib] def update(f: Double => Double): Matrix
9094
}
9195

@@ -175,7 +179,7 @@ object DenseMatrix {
175179
def eye(n: Int): DenseMatrix = {
176180
val identity = DenseMatrix.zeros(n, n)
177181
var i = 0
178-
while (i < n){
182+
while (i < n) {
179183
identity.update(i, i, 1.0)
180184
i += 1
181185
}
@@ -286,7 +290,7 @@ class SparseMatrix(
286290

287291
private[mllib] def update(i: Int, j: Int, v: Double): Unit = {
288292
val ind = index(i, j)
289-
if (ind == -1){
293+
if (ind == -1) {
290294
throw new NoSuchElementException("The given row and column indices correspond to a zero " +
291295
"value. Only non-zero elements in Sparse Matrices can be updated.")
292296
} else {
@@ -341,18 +345,18 @@ object SparseMatrix {
341345
raw.foreach { v =>
342346
val r = i % numRows
343347
val c = (i - r) / numRows
344-
if ( v != 0.0) {
348+
if (v != 0.0) {
345349
sRows.append(r)
346350
sparseA.append(v)
347-
while (c != lastCol){
351+
while (c != lastCol) {
348352
sCols.append(nnz)
349353
lastCol += 1
350354
}
351355
nnz += 1
352356
}
353357
i += 1
354358
}
355-
while (numCols > lastCol){
359+
while (numCols > lastCol) {
356360
sCols.append(sparseA.length)
357361
lastCol += 1
358362
}
@@ -368,8 +372,8 @@ object SparseMatrix {
368372
* @return `SparseMatrix` with size `numRows` x `numCols` and values in U(0, 1)
369373
*/
370374
def sprand(numRows: Int, numCols: Int, density: Double, rng: Random): SparseMatrix = {
371-
require(density > 0.0 && density < 1.0, "density must be a double in the range " +
372-
s"0.0 < d < 1.0. Currently, density: $density")
375+
require(density >= 0.0 && density <= 1.0, "density must be a double in the range " +
376+
s"0.0 <= d <= 1.0. Currently, density: $density")
373377
val length = numRows * numCols
374378
val rawA = new Array[Double](length)
375379
var nnz = 0
@@ -392,8 +396,8 @@ object SparseMatrix {
392396
* @return `SparseMatrix` with size `numRows` x `numCols` and values in N(0, 1)
393397
*/
394398
def sprandn(numRows: Int, numCols: Int, density: Double, rng: Random): SparseMatrix = {
395-
require(density > 0.0 && density < 1.0, "density must be a double in the range " +
396-
s"0.0 < d < 1.0. Currently, density: $density")
399+
require(density >= 0.0 && density <= 1.0, "density must be a double in the range " +
400+
s"0.0 <= d <= 1.0. Currently, density: $density")
397401
val length = numRows * numCols
398402
val rawA = new Array[Double](length)
399403
var nnz = 0
@@ -408,7 +412,7 @@ object SparseMatrix {
408412
}
409413

410414
/**
411-
* Generate a diagonal matrix in `DenseMatrix` format from the supplied values.
415+
* Generate a diagonal matrix in `SparseMatrix` format from the supplied values.
412416
* @param vector a `Vector` that will form the values on the diagonal of the matrix
413417
* @return Square `SparseMatrix` with size `values.length` x `values.length` and non-zero
414418
* `values` on the diagonal
@@ -519,38 +523,38 @@ object Matrices {
519523
* Generate a `DenseMatrix` consisting of zeros.
520524
* @param numRows number of rows of the matrix
521525
* @param numCols number of columns of the matrix
522-
* @return `DenseMatrix` with size `numRows` x `numCols` and values of zeros
526+
* @return `Matrix` with size `numRows` x `numCols` and values of zeros
523527
*/
524528
def zeros(numRows: Int, numCols: Int): Matrix = DenseMatrix.zeros(numRows, numCols)
525529

526530
/**
527531
* Generate a `DenseMatrix` consisting of ones.
528532
* @param numRows number of rows of the matrix
529533
* @param numCols number of columns of the matrix
530-
* @return `DenseMatrix` with size `numRows` x `numCols` and values of ones
534+
* @return `Matrix` with size `numRows` x `numCols` and values of ones
531535
*/
532536
def ones(numRows: Int, numCols: Int): Matrix = DenseMatrix.ones(numRows, numCols)
533537

534538
/**
535539
* Generate a dense Identity Matrix in `Matrix` format.
536540
* @param n number of rows and columns of the matrix
537-
* @return `DenseMatrix` with size `n` x `n` and values of ones on the diagonal
541+
* @return `Matrix` with size `n` x `n` and values of ones on the diagonal
538542
*/
539543
def eye(n: Int): Matrix = DenseMatrix.eye(n)
540544

541545
/**
542546
* Generate a sparse Identity Matrix in `Matrix` format.
543547
* @param n number of rows and columns of the matrix
544-
* @return `SparseMatrix` with size `n` x `n` and values of ones on the diagonal
548+
* @return `Matrix` with size `n` x `n` and values of ones on the diagonal
545549
*/
546550
def speye(n: Int): Matrix = SparseMatrix.speye(n)
547551

548552
/**
549-
* Generate a dense `Matrix` consisting of i.i.d. uniform random numbers.
553+
* Generate a `DenseMatrix` consisting of i.i.d. uniform random numbers.
550554
* @param numRows number of rows of the matrix
551555
* @param numCols number of columns of the matrix
552556
* @param rng a random number generator
553-
* @return `DenseMatrix` with size `numRows` x `numCols` and values in U(0, 1)
557+
* @return `Matrix` with size `numRows` x `numCols` and values in U(0, 1)
554558
*/
555559
def rand(numRows: Int, numCols: Int, rng: Random): Matrix =
556560
DenseMatrix.rand(numRows, numCols, rng)
@@ -571,7 +575,7 @@ object Matrices {
571575
* @param numRows number of rows of the matrix
572576
* @param numCols number of columns of the matrix
573577
* @param rng a random number generator
574-
* @return `DenseMatrix` with size `numRows` x `numCols` and values in N(0, 1)
578+
* @return `Matrix` with size `numRows` x `numCols` and values in N(0, 1)
575579
*/
576580
def randn(numRows: Int, numCols: Int, rng: Random): Matrix =
577581
DenseMatrix.randn(numRows, numCols, rng)
@@ -590,7 +594,7 @@ object Matrices {
590594
/**
591595
* Generate a diagonal matrix in `DenseMatrix` format from the supplied values.
592596
* @param vector a `Vector` tat will form the values on the diagonal of the matrix
593-
* @return Square `DenseMatrix` with size `values.length` x `values.length` and `values`
597+
* @return Square `Matrix` with size `values.length` x `values.length` and `values`
594598
* on the diagonal
595599
*/
596600
def diag(vector: Vector): Matrix = DenseMatrix.diag(vector)
@@ -653,8 +657,8 @@ object Matrices {
653657
* Vertically concatenate a sequence of matrices. The returned matrix will be in the format
654658
* the matrices are supplied in. Supplying a mix of dense and sparse matrices will result in
655659
* a dense matrix.
656-
* @param matrices sequence of matrices
657-
* @return a single `Matrix` composed of the matrices that were horizontally concatenated
660+
* @param matrices array of matrices
661+
* @return a single `Matrix` composed of the matrices that were vertically concatenated
658662
*/
659663
def vertcat(matrices: Array[Matrix]): Matrix = {
660664
if (matrices.size == 1) {

0 commit comments

Comments
 (0)