From 102fa83866ef03f75c6198cb9a026822af90e1f2 Mon Sep 17 00:00:00 2001 From: WeichenXu Date: Tue, 1 Nov 2016 10:31:46 +0800 Subject: [PATCH] init pr --- .../org/apache/spark/ml/linalg/Matrices.scala | 20 ++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/mllib-local/src/main/scala/org/apache/spark/ml/linalg/Matrices.scala b/mllib-local/src/main/scala/org/apache/spark/ml/linalg/Matrices.scala index 4d4b06b0952bd..f4d7dd88b51d3 100644 --- a/mllib-local/src/main/scala/org/apache/spark/ml/linalg/Matrices.scala +++ b/mllib-local/src/main/scala/org/apache/spark/ml/linalg/Matrices.scala @@ -153,6 +153,18 @@ sealed trait Matrix extends Serializable { */ @Since("2.0.0") def numActives: Int + + /** + * Converts this matrix to a sparse matrix. + */ + @Since("2.1.0") + def toSparse: SparseMatrix + + /** + * Converts this matrix to a dense matrix. + */ + @Since("2.1.0") + def toDense: DenseMatrix } /** @@ -282,12 +294,15 @@ class DenseMatrix @Since("2.0.0") ( override def numActives: Int = values.length + @Since("2.1.0") + override def toDense: DenseMatrix = this + /** * Generate a `SparseMatrix` from the given `DenseMatrix`. The new matrix will have isTransposed * set to false. */ @Since("2.0.0") - def toSparse: SparseMatrix = { + override def toSparse: SparseMatrix = { val spVals: MArrayBuilder[Double] = new MArrayBuilder.ofDouble val colPtrs: Array[Int] = new Array[Int](numCols + 1) val rowIndices: MArrayBuilder[Int] = new MArrayBuilder.ofInt @@ -588,6 +603,9 @@ class SparseMatrix @Since("2.0.0") ( new DenseMatrix(numRows, numCols, toArray) } + @Since("2.1.0") + def toSparse: SparseMatrix = this + override def numNonzeros: Int = values.count(_ != 0) override def numActives: Int = values.length