-
Notifications
You must be signed in to change notification settings - Fork 29.3k
[SPARK-9750][MLlib] Improve equals on SparseMatrix and DenseMatrix #8042
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 4 commits
6416fa0
43d28fa
78f9426
22782df
ab6f3c8
bb70d5e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -257,7 +257,7 @@ class DenseMatrix( | |
| this(numRows, numCols, values, false) | ||
|
|
||
| override def equals(o: Any): Boolean = o match { | ||
| case m: DenseMatrix => | ||
| case m: Matrix => | ||
| m.numRows == numRows && m.numCols == numCols && Arrays.equals(toArray, m.toArray) | ||
| case _ => false | ||
| } | ||
|
|
@@ -519,6 +519,12 @@ class SparseMatrix( | |
| rowIndices: Array[Int], | ||
| values: Array[Double]) = this(numRows, numCols, colPtrs, rowIndices, values, false) | ||
|
|
||
| override def equals(o: Any): Boolean = o match { | ||
| case m: Matrix => | ||
| m.numRows == numRows && m.numCols == numCols && Arrays.equals(toArray, m.toArray) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I like what you had before better. Using Breeze, I think you'd avoid converting to dense representations. With toArray, it becomes dense.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Totally forgot about that, thanks. |
||
| case _ => false | ||
| } | ||
|
|
||
| private[mllib] def toBreeze: BM[Double] = { | ||
| if (!isTransposed) { | ||
| new BSM[Double](values, numRows, numCols, colPtrs, rowIndices) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -74,6 +74,24 @@ class MatricesSuite extends SparkFunSuite { | |
| } | ||
| } | ||
|
|
||
| test("equals") { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you expand this to compare dense with sparse?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. OK |
||
| val dm1 = Matrices.dense(2, 2, Array(0.0, 1.0, 2.0, 3.0)) | ||
| assert(dm1 === dm1) | ||
| assert(dm1 !== dm1.transpose) | ||
|
|
||
| val dm2 = Matrices.dense(2, 2, Array(0.0, 2.0, 1.0, 3.0)) | ||
| assert(dm1 === dm2.transpose) | ||
|
|
||
| val sm1 = dm1.asInstanceOf[DenseMatrix].toSparse | ||
| assert(sm1 === sm1) | ||
| assert(sm1 === dm1) | ||
| assert(sm1 !== sm1.transpose) | ||
|
|
||
| val sm2 = dm2.asInstanceOf[DenseMatrix].toSparse | ||
| assert(sm1 === sm2.transpose) | ||
| assert(sm1 === dm2.transpose) | ||
| } | ||
|
|
||
| test("matrix copies are deep copies") { | ||
| val m = 3 | ||
| val n = 2 | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You could replace toArray with toBreeze here too, in case "o" is sparse
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done