Skip to content

Commit ab431f8

Browse files
mengxrDB Tsai
authored andcommitted
[SPARK-10238] [MLLIB] update since versions in mllib.linalg
Same as #8421 but for `mllib.linalg`. cc dbtsai Author: Xiangrui Meng <[email protected]> Closes #8440 from mengxr/SPARK-10238 and squashes the following commits: b38437e [Xiangrui Meng] update since versions in mllib.linalg
1 parent 8668ead commit ab431f8

File tree

8 files changed

+64
-31
lines changed

8 files changed

+64
-31
lines changed

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

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -32,18 +32,23 @@ import org.apache.spark.sql.types._
3232
* Trait for a local matrix.
3333
*/
3434
@SQLUserDefinedType(udt = classOf[MatrixUDT])
35+
@Since("1.0.0")
3536
sealed trait Matrix extends Serializable {
3637

3738
/** Number of rows. */
39+
@Since("1.0.0")
3840
def numRows: Int
3941

4042
/** Number of columns. */
43+
@Since("1.0.0")
4144
def numCols: Int
4245

4346
/** Flag that keeps track whether the matrix is transposed or not. False by default. */
47+
@Since("1.3.0")
4448
val isTransposed: Boolean = false
4549

4650
/** Converts to a dense array in column major. */
51+
@Since("1.0.0")
4752
def toArray: Array[Double] = {
4853
val newArray = new Array[Double](numRows * numCols)
4954
foreachActive { (i, j, v) =>
@@ -56,6 +61,7 @@ sealed trait Matrix extends Serializable {
5661
private[mllib] def toBreeze: BM[Double]
5762

5863
/** Gets the (i, j)-th element. */
64+
@Since("1.3.0")
5965
def apply(i: Int, j: Int): Double
6066

6167
/** Return the index for the (i, j)-th element in the backing array. */
@@ -65,24 +71,29 @@ sealed trait Matrix extends Serializable {
6571
private[mllib] def update(i: Int, j: Int, v: Double): Unit
6672

6773
/** Get a deep copy of the matrix. */
74+
@Since("1.2.0")
6875
def copy: Matrix
6976

7077
/** Transpose the Matrix. Returns a new `Matrix` instance sharing the same underlying data. */
78+
@Since("1.3.0")
7179
def transpose: Matrix
7280

7381
/** Convenience method for `Matrix`-`DenseMatrix` multiplication. */
82+
@Since("1.2.0")
7483
def multiply(y: DenseMatrix): DenseMatrix = {
7584
val C: DenseMatrix = DenseMatrix.zeros(numRows, y.numCols)
7685
BLAS.gemm(1.0, this, y, 0.0, C)
7786
C
7887
}
7988

8089
/** Convenience method for `Matrix`-`DenseVector` multiplication. For binary compatibility. */
90+
@Since("1.2.0")
8191
def multiply(y: DenseVector): DenseVector = {
8292
multiply(y.asInstanceOf[Vector])
8393
}
8494

8595
/** Convenience method for `Matrix`-`Vector` multiplication. */
96+
@Since("1.4.0")
8697
def multiply(y: Vector): DenseVector = {
8798
val output = new DenseVector(new Array[Double](numRows))
8899
BLAS.gemv(1.0, this, y, 0.0, output)
@@ -93,6 +104,7 @@ sealed trait Matrix extends Serializable {
93104
override def toString: String = toBreeze.toString()
94105

95106
/** A human readable representation of the matrix with maximum lines and width */
107+
@Since("1.4.0")
96108
def toString(maxLines: Int, maxLineWidth: Int): String = toBreeze.toString(maxLines, maxLineWidth)
97109

98110
/** Map the values of this matrix using a function. Generates a new matrix. Performs the
@@ -118,11 +130,13 @@ sealed trait Matrix extends Serializable {
118130
/**
119131
* Find the number of non-zero active values.
120132
*/
133+
@Since("1.5.0")
121134
def numNonzeros: Int
122135

123136
/**
124137
* Find the number of values stored explicitly. These values can be zero as well.
125138
*/
139+
@Since("1.5.0")
126140
def numActives: Int
127141
}
128142

@@ -230,11 +244,11 @@ private[spark] class MatrixUDT extends UserDefinedType[Matrix] {
230244
*/
231245
@Since("1.0.0")
232246
@SQLUserDefinedType(udt = classOf[MatrixUDT])
233-
class DenseMatrix(
234-
val numRows: Int,
235-
val numCols: Int,
236-
val values: Array[Double],
237-
override val isTransposed: Boolean) extends Matrix {
247+
class DenseMatrix @Since("1.3.0") (
248+
@Since("1.0.0") val numRows: Int,
249+
@Since("1.0.0") val numCols: Int,
250+
@Since("1.0.0") val values: Array[Double],
251+
@Since("1.3.0") override val isTransposed: Boolean) extends Matrix {
238252

239253
require(values.length == numRows * numCols, "The number of values supplied doesn't match the " +
240254
s"size of the matrix! values.length: ${values.length}, numRows * numCols: ${numRows * numCols}")
@@ -254,7 +268,7 @@ class DenseMatrix(
254268
* @param numCols number of columns
255269
* @param values matrix entries in column major
256270
*/
257-
@Since("1.3.0")
271+
@Since("1.0.0")
258272
def this(numRows: Int, numCols: Int, values: Array[Double]) =
259273
this(numRows, numCols, values, false)
260274

@@ -491,13 +505,13 @@ object DenseMatrix {
491505
*/
492506
@Since("1.2.0")
493507
@SQLUserDefinedType(udt = classOf[MatrixUDT])
494-
class SparseMatrix(
495-
val numRows: Int,
496-
val numCols: Int,
497-
val colPtrs: Array[Int],
498-
val rowIndices: Array[Int],
499-
val values: Array[Double],
500-
override val isTransposed: Boolean) extends Matrix {
508+
class SparseMatrix @Since("1.3.0") (
509+
@Since("1.2.0") val numRows: Int,
510+
@Since("1.2.0") val numCols: Int,
511+
@Since("1.2.0") val colPtrs: Array[Int],
512+
@Since("1.2.0") val rowIndices: Array[Int],
513+
@Since("1.2.0") val values: Array[Double],
514+
@Since("1.3.0") override val isTransposed: Boolean) extends Matrix {
501515

502516
require(values.length == rowIndices.length, "The number of row indices and values don't match! " +
503517
s"values.length: ${values.length}, rowIndices.length: ${rowIndices.length}")
@@ -527,7 +541,7 @@ class SparseMatrix(
527541
* order for each column
528542
* @param values non-zero matrix entries in column major
529543
*/
530-
@Since("1.3.0")
544+
@Since("1.2.0")
531545
def this(
532546
numRows: Int,
533547
numCols: Int,
@@ -549,8 +563,6 @@ class SparseMatrix(
549563
}
550564
}
551565

552-
/**
553-
*/
554566
@Since("1.3.0")
555567
override def apply(i: Int, j: Int): Double = {
556568
val ind = index(i, j)

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ case class SingularValueDecomposition[UType, VType](U: UType, s: Vector, V: VTyp
3131
* :: Experimental ::
3232
* Represents QR factors.
3333
*/
34+
@Since("1.5.0")
3435
@Experimental
3536
case class QRDecomposition[QType, RType](Q: QType, R: RType)
3637

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

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,16 +38,19 @@ import org.apache.spark.sql.types._
3838
* Note: Users should not implement this interface.
3939
*/
4040
@SQLUserDefinedType(udt = classOf[VectorUDT])
41+
@Since("1.0.0")
4142
sealed trait Vector extends Serializable {
4243

4344
/**
4445
* Size of the vector.
4546
*/
47+
@Since("1.0.0")
4648
def size: Int
4749

4850
/**
4951
* Converts the instance to a double array.
5052
*/
53+
@Since("1.0.0")
5154
def toArray: Array[Double]
5255

5356
override def equals(other: Any): Boolean = {
@@ -99,11 +102,13 @@ sealed trait Vector extends Serializable {
99102
* Gets the value of the ith element.
100103
* @param i index
101104
*/
105+
@Since("1.1.0")
102106
def apply(i: Int): Double = toBreeze(i)
103107

104108
/**
105109
* Makes a deep copy of this vector.
106110
*/
111+
@Since("1.1.0")
107112
def copy: Vector = {
108113
throw new NotImplementedError(s"copy is not implemented for ${this.getClass}.")
109114
}
@@ -121,26 +126,31 @@ sealed trait Vector extends Serializable {
121126
* Number of active entries. An "active entry" is an element which is explicitly stored,
122127
* regardless of its value. Note that inactive entries have value 0.
123128
*/
129+
@Since("1.4.0")
124130
def numActives: Int
125131

126132
/**
127133
* Number of nonzero elements. This scans all active values and count nonzeros.
128134
*/
135+
@Since("1.4.0")
129136
def numNonzeros: Int
130137

131138
/**
132139
* Converts this vector to a sparse vector with all explicit zeros removed.
133140
*/
141+
@Since("1.4.0")
134142
def toSparse: SparseVector
135143

136144
/**
137145
* Converts this vector to a dense vector.
138146
*/
147+
@Since("1.4.0")
139148
def toDense: DenseVector = new DenseVector(this.toArray)
140149

141150
/**
142151
* Returns a vector in either dense or sparse format, whichever uses less storage.
143152
*/
153+
@Since("1.4.0")
144154
def compressed: Vector = {
145155
val nnz = numNonzeros
146156
// A dense vector needs 8 * size + 8 bytes, while a sparse vector needs 12 * nnz + 20 bytes.
@@ -155,6 +165,7 @@ sealed trait Vector extends Serializable {
155165
* Find the index of a maximal element. Returns the first maximal element in case of a tie.
156166
* Returns -1 if vector has length 0.
157167
*/
168+
@Since("1.5.0")
158169
def argmax: Int
159170
}
160171

@@ -532,7 +543,8 @@ object Vectors {
532543
*/
533544
@Since("1.0.0")
534545
@SQLUserDefinedType(udt = classOf[VectorUDT])
535-
class DenseVector(val values: Array[Double]) extends Vector {
546+
class DenseVector @Since("1.0.0") (
547+
@Since("1.0.0") val values: Array[Double]) extends Vector {
536548

537549
@Since("1.0.0")
538550
override def size: Int = values.length
@@ -632,7 +644,9 @@ class DenseVector(val values: Array[Double]) extends Vector {
632644

633645
@Since("1.3.0")
634646
object DenseVector {
647+
635648
/** Extracts the value array from a dense vector. */
649+
@Since("1.3.0")
636650
def unapply(dv: DenseVector): Option[Array[Double]] = Some(dv.values)
637651
}
638652

@@ -645,10 +659,10 @@ object DenseVector {
645659
*/
646660
@Since("1.0.0")
647661
@SQLUserDefinedType(udt = classOf[VectorUDT])
648-
class SparseVector(
649-
override val size: Int,
650-
val indices: Array[Int],
651-
val values: Array[Double]) extends Vector {
662+
class SparseVector @Since("1.0.0") (
663+
@Since("1.0.0") override val size: Int,
664+
@Since("1.0.0") val indices: Array[Int],
665+
@Since("1.0.0") val values: Array[Double]) extends Vector {
652666

653667
require(indices.length == values.length, "Sparse vectors require that the dimension of the" +
654668
s" indices match the dimension of the values. You provided ${indices.length} indices and " +
@@ -819,6 +833,7 @@ class SparseVector(
819833

820834
@Since("1.3.0")
821835
object SparseVector {
836+
@Since("1.3.0")
822837
def unapply(sv: SparseVector): Option[(Int, Array[Int], Array[Double])] =
823838
Some((sv.size, sv.indices, sv.values))
824839
}

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -131,10 +131,10 @@ private[mllib] object GridPartitioner {
131131
*/
132132
@Since("1.3.0")
133133
@Experimental
134-
class BlockMatrix(
135-
val blocks: RDD[((Int, Int), Matrix)],
136-
val rowsPerBlock: Int,
137-
val colsPerBlock: Int,
134+
class BlockMatrix @Since("1.3.0") (
135+
@Since("1.3.0") val blocks: RDD[((Int, Int), Matrix)],
136+
@Since("1.3.0") val rowsPerBlock: Int,
137+
@Since("1.3.0") val colsPerBlock: Int,
138138
private var nRows: Long,
139139
private var nCols: Long) extends DistributedMatrix with Logging {
140140

@@ -171,7 +171,9 @@ class BlockMatrix(
171171
nCols
172172
}
173173

174+
@Since("1.3.0")
174175
val numRowBlocks = math.ceil(numRows() * 1.0 / rowsPerBlock).toInt
176+
@Since("1.3.0")
175177
val numColBlocks = math.ceil(numCols() * 1.0 / colsPerBlock).toInt
176178

177179
private[mllib] def createPartitioner(): GridPartitioner =

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ case class MatrixEntry(i: Long, j: Long, value: Double)
4646
*/
4747
@Since("1.0.0")
4848
@Experimental
49-
class CoordinateMatrix(
50-
val entries: RDD[MatrixEntry],
49+
class CoordinateMatrix @Since("1.0.0") (
50+
@Since("1.0.0") val entries: RDD[MatrixEntry],
5151
private var nRows: Long,
5252
private var nCols: Long) extends DistributedMatrix {
5353

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,11 @@ import org.apache.spark.annotation.Since
2828
trait DistributedMatrix extends Serializable {
2929

3030
/** Gets or computes the number of rows. */
31+
@Since("1.0.0")
3132
def numRows(): Long
3233

3334
/** Gets or computes the number of columns. */
35+
@Since("1.0.0")
3436
def numCols(): Long
3537

3638
/** Collects data and assembles a local dense breeze matrix (for test only). */

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ case class IndexedRow(index: Long, vector: Vector)
4545
*/
4646
@Since("1.0.0")
4747
@Experimental
48-
class IndexedRowMatrix(
49-
val rows: RDD[IndexedRow],
48+
class IndexedRowMatrix @Since("1.0.0") (
49+
@Since("1.0.0") val rows: RDD[IndexedRow],
5050
private var nRows: Long,
5151
private var nCols: Int) extends DistributedMatrix {
5252

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ import org.apache.spark.storage.StorageLevel
4747
*/
4848
@Since("1.0.0")
4949
@Experimental
50-
class RowMatrix(
51-
val rows: RDD[Vector],
50+
class RowMatrix @Since("1.0.0") (
51+
@Since("1.0.0") val rows: RDD[Vector],
5252
private var nRows: Long,
5353
private var nCols: Int) extends DistributedMatrix with Logging {
5454

@@ -519,6 +519,7 @@ class RowMatrix(
519519
* @param computeQ whether to computeQ
520520
* @return QRDecomposition(Q, R), Q = null if computeQ = false.
521521
*/
522+
@Since("1.5.0")
522523
def tallSkinnyQR(computeQ: Boolean = false): QRDecomposition[RowMatrix, Matrix] = {
523524
val col = numCols().toInt
524525
// split rows horizontally into smaller matrices, and compute QR for each of them

0 commit comments

Comments
 (0)