Skip to content

Commit 27f5bdd

Browse files
committed
update linalg docs and some new method signatures
1 parent 371721b commit 27f5bdd

File tree

4 files changed

+40
-28
lines changed

4 files changed

+40
-28
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
@@ -115,7 +115,7 @@ sealed trait Matrix extends Serializable {
115115
*
116116
* @param numRows number of rows
117117
* @param numCols number of columns
118-
* @param values matrix entries in column major
118+
* @param values matrix entries in column major if not transposed or in row major otherwise
119119
* @param isTransposed whether the matrix is transposed. If true, `values` stores the matrix in
120120
* row major.
121121
*/
@@ -187,7 +187,7 @@ class DenseMatrix(
187187
this
188188
}
189189

190-
override def transpose: Matrix = new DenseMatrix(numCols, numRows, values, !isTransposed)
190+
override def transpose: DenseMatrix = new DenseMatrix(numCols, numRows, values, !isTransposed)
191191

192192
private[spark] override def foreachActive(f: (Int, Int, Double) => Unit): Unit = {
193193
if (!isTransposed) {
@@ -217,9 +217,11 @@ class DenseMatrix(
217217
}
218218
}
219219

220-
/** Generate a `SparseMatrix` from the given `DenseMatrix`. The new matrix will have isTransposed
221-
* set to false. */
222-
def toSparse(): SparseMatrix = {
220+
/**
221+
* Generate a `SparseMatrix` from the given `DenseMatrix`. The new matrix will have isTransposed
222+
* set to false.
223+
*/
224+
def toSparse: SparseMatrix = {
223225
val spVals: MArrayBuilder[Double] = new MArrayBuilder.ofDouble
224226
val colPtrs: Array[Int] = new Array[Int](numCols + 1)
225227
val rowIndices: MArrayBuilder[Int] = new MArrayBuilder.ofInt
@@ -282,7 +284,7 @@ object DenseMatrix {
282284
}
283285

284286
/**
285-
* Generate a `DenseMatrix` consisting of i.i.d. uniform random numbers.
287+
* Generate a `DenseMatrix` consisting of `i.i.d.` uniform random numbers.
286288
* @param numRows number of rows of the matrix
287289
* @param numCols number of columns of the matrix
288290
* @param rng a random number generator
@@ -293,7 +295,7 @@ object DenseMatrix {
293295
}
294296

295297
/**
296-
* Generate a `DenseMatrix` consisting of i.i.d. gaussian random numbers.
298+
* Generate a `DenseMatrix` consisting of `i.i.d.` gaussian random numbers.
297299
* @param numRows number of rows of the matrix
298300
* @param numCols number of columns of the matrix
299301
* @param rng a random number generator
@@ -336,10 +338,10 @@ object DenseMatrix {
336338
*
337339
* @param numRows number of rows
338340
* @param numCols number of columns
339-
* @param colPtrs the index corresponding to the start of a new column
340-
* @param rowIndices the row index of the entry. They must be in strictly increasing order for each
341-
* column
342-
* @param values non-zero matrix entries in column major
341+
* @param colPtrs the index corresponding to the start of a new column (if not transposed)
342+
* @param rowIndices the row index of the entry (if not transposed). They must be in strictly
343+
* increasing order for each column
344+
* @param values nonzero matrix entries in column major (if not transposed)
343345
* @param isTransposed whether the matrix is transposed. If true, the matrix can be considered
344346
* Compressed Sparse Row (CSR) format, where `colPtrs` behaves as rowPtrs,
345347
* and `rowIndices` behave as colIndices, and `values` are stored in row major.
@@ -434,7 +436,7 @@ class SparseMatrix(
434436
this
435437
}
436438

437-
override def transpose: Matrix =
439+
override def transpose: SparseMatrix =
438440
new SparseMatrix(numCols, numRows, colPtrs, rowIndices, values, !isTransposed)
439441

440442
private[spark] override def foreachActive(f: (Int, Int, Double) => Unit): Unit = {
@@ -464,9 +466,11 @@ class SparseMatrix(
464466
}
465467
}
466468

467-
/** Generate a `DenseMatrix` from the given `SparseMatrix`. The new matrix will have isTransposed
468-
* set to false. */
469-
def toDense(): DenseMatrix = {
469+
/**
470+
* Generate a `DenseMatrix` from the given `SparseMatrix`. The new matrix will have isTransposed
471+
* set to false.
472+
*/
473+
def toDense: DenseMatrix = {
470474
new DenseMatrix(numRows, numCols, toArray)
471475
}
472476
}
@@ -593,7 +597,7 @@ object SparseMatrix {
593597
}
594598

595599
/**
596-
* Generate a `SparseMatrix` consisting of i.i.d. uniform random numbers. The number of non-zero
600+
* Generate a `SparseMatrix` consisting of `i.i.d`. uniform random numbers. The number of non-zero
597601
* elements equal the ceiling of `numRows` x `numCols` x `density`
598602
*
599603
* @param numRows number of rows of the matrix
@@ -608,7 +612,7 @@ object SparseMatrix {
608612
}
609613

610614
/**
611-
* Generate a `SparseMatrix` consisting of i.i.d. gaussian random numbers.
615+
* Generate a `SparseMatrix` consisting of `i.i.d`. gaussian random numbers.
612616
* @param numRows number of rows of the matrix
613617
* @param numCols number of columns of the matrix
614618
* @param density the desired density for the matrix
@@ -626,7 +630,7 @@ object SparseMatrix {
626630
* @return Square `SparseMatrix` with size `values.length` x `values.length` and non-zero
627631
* `values` on the diagonal
628632
*/
629-
def diag(vector: Vector): SparseMatrix = {
633+
def spdiag(vector: Vector): SparseMatrix = {
630634
val n = vector.size
631635
vector match {
632636
case sVec: SparseVector =>
@@ -722,7 +726,7 @@ object Matrices {
722726
def speye(n: Int): Matrix = SparseMatrix.speye(n)
723727

724728
/**
725-
* Generate a `DenseMatrix` consisting of i.i.d. uniform random numbers.
729+
* Generate a `DenseMatrix` consisting of `i.i.d.` uniform random numbers.
726730
* @param numRows number of rows of the matrix
727731
* @param numCols number of columns of the matrix
728732
* @param rng a random number generator
@@ -732,7 +736,7 @@ object Matrices {
732736
DenseMatrix.rand(numRows, numCols, rng)
733737

734738
/**
735-
* Generate a `SparseMatrix` consisting of i.i.d. gaussian random numbers.
739+
* Generate a `SparseMatrix` consisting of `i.i.d.` gaussian random numbers.
736740
* @param numRows number of rows of the matrix
737741
* @param numCols number of columns of the matrix
738742
* @param density the desired density for the matrix
@@ -743,7 +747,7 @@ object Matrices {
743747
SparseMatrix.sprand(numRows, numCols, density, rng)
744748

745749
/**
746-
* Generate a `DenseMatrix` consisting of i.i.d. gaussian random numbers.
750+
* Generate a `DenseMatrix` consisting of `i.i.d.` gaussian random numbers.
747751
* @param numRows number of rows of the matrix
748752
* @param numCols number of columns of the matrix
749753
* @param rng a random number generator
@@ -753,7 +757,7 @@ object Matrices {
753757
DenseMatrix.randn(numRows, numCols, rng)
754758

755759
/**
756-
* Generate a `SparseMatrix` consisting of i.i.d. gaussian random numbers.
760+
* Generate a `SparseMatrix` consisting of `i.i.d.` gaussian random numbers.
757761
* @param numRows number of rows of the matrix
758762
* @param numCols number of columns of the matrix
759763
* @param density the desired density for the matrix

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -244,8 +244,7 @@ object Vectors {
244244
}
245245

246246
/**
247-
* Parses a string resulted from `Vector#toString` into
248-
* an [[org.apache.spark.mllib.linalg.Vector]].
247+
* Parses a string resulted from [[Vector.toString]] into a [[Vector]].
249248
*/
250249
def parse(s: String): Vector = {
251250
parseNumeric(NumericParser.parse(s))
@@ -483,6 +482,7 @@ class DenseVector(val values: Array[Double]) extends Vector {
483482
}
484483

485484
object DenseVector {
485+
/** Extracts the value array from a dense vector. */
486486
def unapply(dv: DenseVector): Option[Array[Double]] = Some(dv.values)
487487
}
488488

mllib/src/main/scala/org/apache/spark/mllib/linalg/distributed/BlockMatrix.scala

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ import scala.collection.mutable.ArrayBuffer
2121

2222
import breeze.linalg.{DenseMatrix => BDM}
2323

24-
import org.apache.spark.{SparkException, Logging, Partitioner}
24+
import org.apache.spark.{Logging, Partitioner, SparkException}
25+
import org.apache.spark.annotation.Experimental
2526
import org.apache.spark.mllib.linalg.{DenseMatrix, Matrices, Matrix, SparseMatrix}
2627
import org.apache.spark.rdd.RDD
2728
import org.apache.spark.storage.StorageLevel
@@ -104,6 +105,8 @@ private[mllib] object GridPartitioner {
104105
}
105106

106107
/**
108+
* :: Experimental ::
109+
*
107110
* Represents a distributed matrix in blocks of local matrices.
108111
*
109112
* @param blocks The RDD of sub-matrix blocks ((blockRowIndex, blockColIndex), sub-matrix) that
@@ -118,6 +121,7 @@ private[mllib] object GridPartitioner {
118121
* @param nCols Number of columns of this matrix. If the supplied value is less than or equal to
119122
* zero, the number of columns will be calculated when `numCols` is invoked.
120123
*/
124+
@Experimental
121125
class BlockMatrix(
122126
val blocks: RDD[((Int, Int), Matrix)],
123127
val rowsPerBlock: Int,
@@ -177,6 +181,10 @@ class BlockMatrix(
177181
assert(cols <= nCols, s"The number of columns $cols is more than claimed $nCols.")
178182
}
179183

184+
/**
185+
* Validates the block matrix info against the matrix data (`blocks`) and throws an exception if
186+
* any error is found.
187+
*/
180188
def validate(): Unit = {
181189
logDebug("Validating BlockMatrix...")
182190
// check if the matrix is larger than the claimed dimensions
@@ -351,7 +359,7 @@ class BlockMatrix(
351359
if (a.nonEmpty && b.nonEmpty) {
352360
val C = b.head match {
353361
case dense: DenseMatrix => a.head.multiply(dense)
354-
case sparse: SparseMatrix => a.head.multiply(sparse.toDense())
362+
case sparse: SparseMatrix => a.head.multiply(sparse.toDense)
355363
case _ => throw new SparkException(s"Unrecognized matrix type ${b.head.getClass}.")
356364
}
357365
Iterator(((blockRowIndex, blockColIndex), C.toBreeze))

mllib/src/test/java/org/apache/spark/mllib/linalg/JavaMatricesSuite.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,8 @@ public void diagonalMatrixConstruction() {
7171
Matrix sm = Matrices.diag(sv);
7272
DenseMatrix d = DenseMatrix.diag(v);
7373
DenseMatrix sd = DenseMatrix.diag(sv);
74-
SparseMatrix s = SparseMatrix.diag(v);
75-
SparseMatrix ss = SparseMatrix.diag(sv);
74+
SparseMatrix s = SparseMatrix.spdiag(v);
75+
SparseMatrix ss = SparseMatrix.spdiag(sv);
7676

7777
assertArrayEquals(m.toArray(), sm.toArray(), 0.0);
7878
assertArrayEquals(d.toArray(), sm.toArray(), 0.0);

0 commit comments

Comments
 (0)