Repository: spark Updated Branches: refs/heads/master 9812f7d53 -> c0949dc94
[SPARK-17207][MLLIB] fix comparing Vector bug in TestingUtils ## What changes were proposed in this pull request? fix comparing Vector bug in TestingUtils. There is the same bug for Matrix comparing. How to check the length of Matrix should be discussed first. ## How was this patch tested? (Please explain how this patch was tested. E.g. unit tests, integration tests, manual tests) (If this patch involves UI changes, please attach a screenshot; otherwise, remove this) Author: Peng, Meng <peng.m...@intel.com> Closes #14785 from mpjlu/testUtils. Project: http://git-wip-us.apache.org/repos/asf/spark/repo Commit: http://git-wip-us.apache.org/repos/asf/spark/commit/c0949dc9 Tree: http://git-wip-us.apache.org/repos/asf/spark/tree/c0949dc9 Diff: http://git-wip-us.apache.org/repos/asf/spark/diff/c0949dc9 Branch: refs/heads/master Commit: c0949dc944b7e2fc8a4465acc68a8f2713b3fa13 Parents: 9812f7d Author: Peng, Meng <peng.m...@intel.com> Authored: Fri Aug 26 11:54:10 2016 -0700 Committer: DB Tsai <d...@netflix.com> Committed: Fri Aug 26 11:54:10 2016 -0700 ---------------------------------------------------------------------- .../org/apache/spark/ml/util/TestingUtils.scala | 10 +- .../spark/ml/util/TestingUtilsSuite.scala | 277 ++++++++++++++++++- .../spark/ml/feature/CountVectorizerSuite.scala | 8 +- .../apache/spark/mllib/util/TestingUtils.scala | 10 +- .../spark/mllib/util/TestingUtilsSuite.scala | 277 ++++++++++++++++++- 5 files changed, 566 insertions(+), 16 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/spark/blob/c0949dc9/mllib-local/src/test/scala/org/apache/spark/ml/util/TestingUtils.scala ---------------------------------------------------------------------- diff --git a/mllib-local/src/test/scala/org/apache/spark/ml/util/TestingUtils.scala b/mllib-local/src/test/scala/org/apache/spark/ml/util/TestingUtils.scala index 2bebaa35..2327917 100644 --- a/mllib-local/src/test/scala/org/apache/spark/ml/util/TestingUtils.scala +++ b/mllib-local/src/test/scala/org/apache/spark/ml/util/TestingUtils.scala @@ -154,7 +154,7 @@ object TestingUtils { */ def absTol(eps: Double): CompareVectorRightSide = CompareVectorRightSide( (x: Vector, y: Vector, eps: Double) => { - x.toArray.zip(y.toArray).forall(x => x._1 ~= x._2 absTol eps) + x.size == y.size && x.toArray.zip(y.toArray).forall(x => x._1 ~= x._2 absTol eps) }, x, eps, ABS_TOL_MSG) /** @@ -164,7 +164,7 @@ object TestingUtils { */ def relTol(eps: Double): CompareVectorRightSide = CompareVectorRightSide( (x: Vector, y: Vector, eps: Double) => { - x.toArray.zip(y.toArray).forall(x => x._1 ~= x._2 relTol eps) + x.size == y.size && x.toArray.zip(y.toArray).forall(x => x._1 ~= x._2 relTol eps) }, x, eps, REL_TOL_MSG) override def toString: String = x.toString @@ -217,7 +217,8 @@ object TestingUtils { */ def absTol(eps: Double): CompareMatrixRightSide = CompareMatrixRightSide( (x: Matrix, y: Matrix, eps: Double) => { - x.toArray.zip(y.toArray).forall(x => x._1 ~= x._2 absTol eps) + x.numRows == y.numRows && x.numCols == y.numCols && + x.toArray.zip(y.toArray).forall(x => x._1 ~= x._2 absTol eps) }, x, eps, ABS_TOL_MSG) /** @@ -227,7 +228,8 @@ object TestingUtils { */ def relTol(eps: Double): CompareMatrixRightSide = CompareMatrixRightSide( (x: Matrix, y: Matrix, eps: Double) => { - x.toArray.zip(y.toArray).forall(x => x._1 ~= x._2 relTol eps) + x.numRows == y.numRows && x.numCols == y.numCols && + x.toArray.zip(y.toArray).forall(x => x._1 ~= x._2 relTol eps) }, x, eps, REL_TOL_MSG) override def toString: String = x.toString http://git-wip-us.apache.org/repos/asf/spark/blob/c0949dc9/mllib-local/src/test/scala/org/apache/spark/ml/util/TestingUtilsSuite.scala ---------------------------------------------------------------------- diff --git a/mllib-local/src/test/scala/org/apache/spark/ml/util/TestingUtilsSuite.scala b/mllib-local/src/test/scala/org/apache/spark/ml/util/TestingUtilsSuite.scala index e374165..5cbf2f0 100644 --- a/mllib-local/src/test/scala/org/apache/spark/ml/util/TestingUtilsSuite.scala +++ b/mllib-local/src/test/scala/org/apache/spark/ml/util/TestingUtilsSuite.scala @@ -20,7 +20,7 @@ package org.apache.spark.ml.util import org.scalatest.exceptions.TestFailedException import org.apache.spark.ml.SparkMLFunSuite -import org.apache.spark.ml.linalg.Vectors +import org.apache.spark.ml.linalg.{Matrices, Vectors} import org.apache.spark.ml.util.TestingUtils._ class TestingUtilsSuite extends SparkMLFunSuite { @@ -109,6 +109,10 @@ class TestingUtilsSuite extends SparkMLFunSuite { assert(Vectors.dense(Array(3.1, 3.5)) !~= Vectors.dense(Array(3.135, 3.534)) relTol 0.01) assert(!(Vectors.dense(Array(3.1, 3.5)) !~= Vectors.dense(Array(3.130, 3.534)) relTol 0.01)) assert(!(Vectors.dense(Array(3.1, 3.5)) ~= Vectors.dense(Array(3.135, 3.534)) relTol 0.01)) + assert(Vectors.dense(Array(3.1)) !~= Vectors.dense(Array(3.130, 3.534)) relTol 0.01) + assert(Vectors.dense(Array[Double]()) !~= Vectors.dense(Array(3.130, 3.534)) relTol 0.01) + assert(Vectors.dense(Array(3.1)) !~== Vectors.dense(Array(3.130, 3.534)) relTol 0.01) + assert(Vectors.dense(Array[Double]()) !~== Vectors.dense(Array(3.130, 3.534)) relTol 0.01) // Should throw exception with message when test fails. intercept[TestFailedException]( @@ -117,6 +121,12 @@ class TestingUtilsSuite extends SparkMLFunSuite { intercept[TestFailedException]( Vectors.dense(Array(3.1, 3.5)) ~== Vectors.dense(Array(3.135, 3.534)) relTol 0.01) + intercept[TestFailedException]( + Vectors.dense(Array(3.1)) ~== Vectors.dense(Array(3.535, 3.534)) relTol 0.01) + + intercept[TestFailedException]( + Vectors.dense(Array[Double]()) ~== Vectors.dense(Array(3.135)) relTol 0.01) + // Comparing against zero should fail the test and throw exception with message // saying that the relative error is meaningless in this situation. intercept[TestFailedException]( @@ -125,12 +135,18 @@ class TestingUtilsSuite extends SparkMLFunSuite { intercept[TestFailedException]( Vectors.dense(Array(3.1, 0.01)) ~== Vectors.sparse(2, Array(0), Array(3.13)) relTol 0.01) - // Comparisons of two sparse vectors + // Comparisons of a sparse vector and a dense vector assert(Vectors.dense(Array(3.1, 3.5)) ~== Vectors.sparse(2, Array(0, 1), Array(3.130, 3.534)) relTol 0.01) assert(Vectors.dense(Array(3.1, 3.5)) !~== Vectors.sparse(2, Array(0, 1), Array(3.135, 3.534)) relTol 0.01) + + assert(Vectors.dense(Array(3.1)) !~== + Vectors.sparse(2, Array(0, 1), Array(3.130, 3.534)) relTol 0.01) + + assert(Vectors.dense(Array[Double]()) !~== + Vectors.sparse(2, Array(0, 1), Array(3.130, 3.534)) relTol 0.01) } test("Comparing vectors using absolute error.") { @@ -154,6 +170,21 @@ class TestingUtilsSuite extends SparkMLFunSuite { assert(!(Vectors.dense(Array(3.1, 3.5, 0.0)) ~= Vectors.dense(Array(3.1 + 1E-5, 3.5 + 2E-7, 1 + 1E-3)) absTol 1E-6)) + assert(Vectors.dense(Array(3.1)) !~= + Vectors.dense(Array(3.1 + 1E-6, 3.5 + 2E-7)) absTol 1E-5) + + assert(!(Vectors.dense(Array(3.1)) ~= + Vectors.dense(Array(3.1 + 1E-6, 3.5 + 2E-7)) absTol 1E-5)) + + assert(Vectors.dense(Array[Double]()) !~= + Vectors.dense(Array(3.1 + 1E-6, 3.5 + 2E-7)) absTol 1E-5) + + assert(!(Vectors.dense(Array[Double]()) ~= + Vectors.dense(Array(3.1 + 1E-6, 3.5 + 2E-7)) absTol 1E-5)) + + assert(Vectors.dense(Array[Double]()) ~= + Vectors.dense(Array[Double]()) absTol 1E-5) + // Should throw exception with message when test fails. intercept[TestFailedException](Vectors.dense(Array(3.1, 3.5, 0.0)) !~== Vectors.dense(Array(3.1 + 1E-8, 3.5 + 2E-7, 1E-8)) absTol 1E-6) @@ -161,6 +192,12 @@ class TestingUtilsSuite extends SparkMLFunSuite { intercept[TestFailedException](Vectors.dense(Array(3.1, 3.5, 0.0)) ~== Vectors.dense(Array(3.1 + 1E-5, 3.5 + 2E-7, 1 + 1E-3)) absTol 1E-6) + intercept[TestFailedException](Vectors.dense(Array(3.1)) ~== + Vectors.dense(Array(3.1 + 1E-5, 3.5 + 2E-7)) absTol 1E-6) + + intercept[TestFailedException](Vectors.dense(Array[Double]()) ~== + Vectors.dense(Array(3.1 + 1E-5, 3.5 + 2E-7)) absTol 1E-6) + // Comparisons of two sparse vectors assert(Vectors.sparse(3, Array(0, 2), Array(3.1, 2.4)) ~== Vectors.sparse(3, Array(0, 2), Array(3.1 + 1E-8, 2.4 + 1E-7)) absTol 1E-6) @@ -174,6 +211,12 @@ class TestingUtilsSuite extends SparkMLFunSuite { assert(Vectors.sparse(3, Array(0, 2), Array(3.1 + 1E-3, 2.4)) !~== Vectors.sparse(3, Array(0, 2), Array(3.1, 2.4)) absTol 1E-6) + assert(Vectors.sparse(3, Array(0, 2), Array(3.1 + 1E-6, 2.4)) !~== + Vectors.sparse(1, Array(0), Array(3.1)) absTol 1E-3) + + assert(Vectors.sparse(0, Array[Int](), Array[Double]()) !~== + Vectors.sparse(1, Array(0), Array(3.1)) absTol 1E-3) + // Comparisons of a dense vector and a sparse vector assert(Vectors.sparse(3, Array(0, 2), Array(3.1, 2.4)) ~== Vectors.dense(Array(3.1 + 1E-8, 0, 2.4 + 1E-7)) absTol 1E-6) @@ -183,5 +226,235 @@ class TestingUtilsSuite extends SparkMLFunSuite { assert(Vectors.sparse(3, Array(0, 2), Array(3.1, 2.4)) !~== Vectors.dense(Array(3.1, 1E-3, 2.4)) absTol 1E-6) + + assert(Vectors.sparse(3, Array(0, 2), Array(3.1, 2.4)) !~== + Vectors.dense(Array(3.1)) absTol 1E-6) + + assert(Vectors.dense(Array[Double]()) !~== + Vectors.sparse(3, Array(0, 2), Array(0, 2.4)) absTol 1E-6) + + assert(Vectors.sparse(1, Array(0), Array(3.1)) !~== + Vectors.dense(Array(3.1, 3.2)) absTol 1E-6) + + assert(Vectors.dense(Array(3.1)) !~== + Vectors.sparse(0, Array[Int](), Array[Double]()) absTol 1E-6) + } + + test("Comparing Matrices using absolute error.") { + + // Comparisons of two dense Matrices + assert(Matrices.dense(2, 2, Array(3.1, 3.5, 3.1, 3.5)) ~== + Matrices.dense(2, 2, Array(3.1 + 1E-8, 3.5 + 2E-7, 3.1 + 1E-8, 3.5 + 1E-7)) absTol 1E-6) + + assert(Matrices.dense(2, 2, Array(3.1, 3.5, 3.1, 3.5)) ~= + Matrices.dense(2, 2, Array(3.1 + 1E-8, 3.5 + 2E-7, 3.1 + 1E-8, 3.5 + 1E-7)) absTol 1E-6) + + assert(Matrices.dense(2, 2, Array(3.1, 3.5, 3.1, 3.5)) !~== + Matrices.dense(2, 2, Array(3.1 + 1E-5, 3.5 + 2E-6, 3.1 + 1E-8, 3.5 + 1E-7)) absTol 1E-6) + + assert(Matrices.dense(2, 2, Array(3.1, 3.5, 3.1, 3.5)) !~= + Matrices.dense(2, 2, Array(3.1 + 1E-5, 3.5 + 2E-6, 3.1 + 1E-8, 3.5 + 1E-7)) absTol 1E-6) + + assert(!(Matrices.dense(2, 2, Array(3.1, 3.5, 3.1, 3.5)) ~= + Matrices.dense(2, 2, Array(3.1 + 1E-5, 3.5 + 2E-6, 3.1 + 1E-8, 3.5 + 1E-7)) absTol 1E-6)) + + assert(!(Matrices.dense(2, 2, Array(3.1, 3.5, 3.1, 3.5)) !~= + Matrices.dense(2, 2, Array(3.1 + 1E-7, 3.5 + 2E-8, 3.1 + 1E-8, 3.5 + 1E-7)) absTol 1E-6)) + + assert(Matrices.dense(2, 1, Array(3.1, 3.5)) !~== + Matrices.dense(2, 2, Array(3.1 + 1E-7, 3.5 + 2E-8, 3.1 + 1E-8, 3.5 + 1E-7)) absTol 1E-6) + + assert(Matrices.dense(2, 1, Array(3.1, 3.5)) !~= + Matrices.dense(2, 2, Array(3.1 + 1E-7, 3.5 + 2E-8, 3.1 + 1E-8, 3.5 + 1E-7)) absTol 1E-6) + + assert(Matrices.dense(0, 0, Array()) !~= + Matrices.dense(2, 2, Array(3.1 + 1E-7, 3.5 + 2E-8, 3.1 + 1E-8, 3.5 + 1E-7)) absTol 1E-6) + + assert(Matrices.dense(0, 0, Array()) !~== + Matrices.dense(2, 2, Array(3.1 + 1E-7, 3.5 + 2E-8, 3.1 + 1E-8, 3.5 + 1E-7)) absTol 1E-6) + + // Should throw exception with message when test fails. + intercept[TestFailedException](Matrices.dense(2, 2, Array(3.1, 3.5, 3.1, 3.5)) !~== + Matrices.dense(2, 2, Array(3.1 + 1E-8, 3.5 + 2E-7, 3.1 + 1E-8, 3.5 + 1E-7)) absTol 1E-6) + + intercept[TestFailedException](Matrices.dense(2, 2, Array(3.1, 3.5, 3.1, 3.5)) ~== + Matrices.dense(2, 2, Array(3.1 + 1E-8, 3.5 + 2E-7, 3.1 + 1E-8, 3.5 + 1E-7)) absTol 1E-9) + + intercept[TestFailedException](Matrices.dense(2, 1, Array(3.1, 3.5)) ~== + Matrices.dense(2, 2, Array(3.1 + 1E-8, 3.5 + 2E-7, 3.1 + 1E-8, 3.5 + 1E-7)) absTol 1E-5) + + intercept[TestFailedException](Matrices.dense(0, 0, Array()) ~== + Matrices.dense(2, 2, Array(3.1 + 1E-8, 3.5 + 2E-7, 3.1 + 1E-8, 3.5 + 1E-7)) absTol 1E-5) + + // Comparisons of two sparse Matrices + assert(Matrices.sparse(3, 2, Array(0, 1, 2), Array(1, 2), Array(3.1, 3.5)) ~== + Matrices.sparse(3, 2, Array(0, 1, 2), Array(1, 2), Array(3.1 + 1E-8, 3.5 + 1E-7)) absTol 1E-6) + + assert(Matrices.sparse(3, 2, Array(0, 1, 2), Array(1, 2), Array(3.1, 3.5)) ~= + Matrices.sparse(3, 2, Array(0, 1, 2), Array(1, 2), Array(3.1 + 1E-8, 3.5 + 1E-7)) absTol 1E-6) + + assert(Matrices.sparse(3, 2, Array(0, 1, 2), Array(1, 2), Array(3.1, 3.5)) !~= + Matrices.sparse(3, 2, Array(0, 1, 2), Array(1, 2), Array(3.1 + 1E-8, 3.5 + 1E-7)) absTol 1E-9) + + assert(Matrices.sparse(3, 2, Array(0, 1, 2), Array(1, 2), Array(3.1, 3.5)) !~== + Matrices.sparse(3, 2, Array(0, 1, 2), Array(1, 2), Array(3.1 + 1E-8, 3.5 + 1E-7)) absTol 1E-9) + + assert(!(Matrices.sparse(3, 2, Array(0, 1, 2), Array(1, 2), Array(3.1, 3.5)) ~= + Matrices.sparse(3, 2, Array(0, 1, 2), Array(1, 2), Array(3.1 + 1E-8, 3.5)) absTol 1E-9)) + + assert(!(Matrices.sparse(3, 2, Array(0, 1, 2), Array(1, 2), Array(3.1, 3.5)) !~= + Matrices.sparse(3, 2, Array(0, 1, 2), Array(1, 2), Array(3.1 + 1E-8, 3.5)) absTol 1E-6)) + + assert(Matrices.sparse(2, 2, Array(0, 1, 2), Array(0, 1), Array(3.1, 3.5)) !~= + Matrices.sparse(3, 2, Array(0, 1, 2), Array(1, 2), Array(3.1 + 1E-8, 3.5 + 1E-7)) absTol 1E-9) + + assert(Matrices.sparse(2, 2, Array(0, 1, 2), Array(0, 1), Array(3.1, 3.5)) !~== + Matrices.sparse(3, 2, Array(0, 1, 2), Array(1, 2), Array(3.1 + 1E-8, 3.5 + 1E-7)) absTol 1E-6) + + assert(Matrices.sparse(0, 0, Array(1), Array(0), Array(0)) !~== + Matrices.sparse(3, 2, Array(0, 1, 2), Array(1, 2), Array(3.1 + 1E-8, 3.5 + 1E-7)) absTol 1E-6) + + assert(Matrices.sparse(0, 0, Array(1), Array(0), Array(0)) !~= + Matrices.sparse(3, 2, Array(0, 1, 2), Array(1, 2), Array(3.1 + 1E-8, 3.5 + 1E-7)) absTol 1E-6) + + // Comparisons of a dense Matrix and a sparse Matrix + assert(Matrices.sparse(2, 2, Array(0, 1, 2), Array(0, 1), Array(3.1, 3.5)) ~= + Matrices.dense(2, 2, Array(3.1 + 1E-8, 0, 0, 3.5 + 1E-7)) absTol 1E-6) + + assert(Matrices.sparse(2, 2, Array(0, 1, 2), Array(0, 1), Array(3.1, 3.5)) ~== + Matrices.dense(2, 2, Array(3.1 + 1E-8, 0, 0, 3.5 + 1E-7)) absTol 1E-6) + + assert(Matrices.sparse(2, 2, Array(0, 1, 2), Array(0, 1), Array(3.1, 3.5)) !~== + Matrices.dense(2, 2, Array(3.1 + 1E-8, 0, 0, 3.5 + 1E-7)) absTol 1E-9) + + assert(Matrices.sparse(2, 2, Array(0, 1, 2), Array(0, 1), Array(3.1, 3.5)) !~== + Matrices.dense(2, 2, Array(3.1 + 1E-8, 0, 0, 3.5 + 1E-7)) absTol 1E-9) + + assert(!(Matrices.sparse(2, 2, Array(0, 1, 2), Array(0, 1), Array(3.1, 3.5)) ~= + Matrices.dense(2, 2, Array(3.1 + 1E-8, 0, 0, 3.5 + 1E-7)) absTol 1E-9)) + + assert(!(Matrices.sparse(2, 2, Array(0, 1, 2), Array(0, 1), Array(3.1, 3.5)) !~= + Matrices.dense(2, 2, Array(3.1 + 1E-8, 0, 0, 3.5 + 1E-7)) absTol 1E-6)) + + assert(Matrices.sparse(2, 2, Array(0, 1, 2), Array(0, 1), Array(3.1, 3.5)) !~= + Matrices.dense(2, 1, Array(3.1 + 1E-8, 0)) absTol 1E-6) + + assert(Matrices.sparse(2, 2, Array(0, 1, 2), Array(0, 1), Array(3.1, 3.5)) !~== + Matrices.dense(2, 1, Array(3.1 + 1E-8, 0)) absTol 1E-6) + + assert(Matrices.sparse(2, 2, Array(0, 1, 2), Array(0, 1), Array(3.1, 3.5)) !~== + Matrices.dense(0, 0, Array()) absTol 1E-6) + + assert(Matrices.sparse(2, 2, Array(0, 1, 2), Array(0, 1), Array(3.1, 3.5)) !~= + Matrices.dense(0, 0, Array()) absTol 1E-6) + } + + test("Comparing Matrices using relative error.") { + + // Comparisons of two dense Matrices + assert(Matrices.dense(2, 2, Array(3.1, 3.5, 3.1, 3.5)) ~== + Matrices.dense(2, 2, Array(3.130, 3.534, 3.130, 3.534)) relTol 0.01) + + assert(Matrices.dense(2, 2, Array(3.1, 3.5, 3.1, 3.5)) ~= + Matrices.dense(2, 2, Array(3.130, 3.534, 3.130, 3.534)) relTol 0.01) + + assert(Matrices.dense(2, 2, Array(3.1, 3.5, 3.1, 3.5)) !~== + Matrices.dense(2, 2, Array(3.135, 3.534, 3.135, 3.534)) relTol 0.01) + + assert(Matrices.dense(2, 2, Array(3.1, 3.5, 3.1, 3.5)) !~= + Matrices.dense(2, 2, Array(3.135, 3.534, 3.135, 3.534)) relTol 0.01) + + assert(!(Matrices.dense(2, 2, Array(3.1, 3.5, 3.1, 3.5)) ~= + Matrices.dense(2, 2, Array(3.134, 3.535, 3.134, 3.535)) relTol 0.01)) + + assert(!(Matrices.dense(2, 2, Array(3.1, 3.5, 3.1, 3.5)) !~= + Matrices.dense(2, 2, Array(3.130, 3.534, 3.130, 3.534)) relTol 0.01)) + + assert(Matrices.dense(2, 1, Array(3.1, 3.5)) !~== + Matrices.dense(2, 2, Array(3.1, 3.5, 3.1, 3.5)) relTol 0.01) + + assert(Matrices.dense(2, 1, Array(3.1, 3.5)) !~= + Matrices.dense(2, 2, Array(3.1, 3.5, 3.1, 3.5)) relTol 0.01) + + assert(Matrices.dense(0, 0, Array()) !~= + Matrices.dense(2, 2, Array(3.1, 3.5, 3.1, 3.5)) relTol 0.01) + + assert(Matrices.dense(0, 0, Array()) !~== + Matrices.dense(2, 2, Array(3.1, 3.5, 3.1, 3.5)) relTol 0.01) + + // Should throw exception with message when test fails. + intercept[TestFailedException](Matrices.dense(2, 2, Array(3.1, 3.5, 3.1, 3.5)) !~== + Matrices.dense(2, 2, Array(3.130, 3.534, 3.130, 3.534)) relTol 0.01) + + intercept[TestFailedException](Matrices.dense(2, 2, Array(3.1, 3.5, 3.1, 3.5)) ~== + Matrices.dense(2, 2, Array(3.135, 3.534, 3.135, 3.534)) relTol 0.01) + + intercept[TestFailedException](Matrices.dense(2, 1, Array(3.1, 3.5)) ~== + Matrices.dense(2, 2, Array(3.1, 3.5, 3.1, 3.5)) relTol 0.01) + + intercept[TestFailedException](Matrices.dense(0, 0, Array()) ~== + Matrices.dense(2, 2, Array(3.1, 3.5, 3.1, 3.5)) relTol 0.01) + + // Comparisons of two sparse Matrices + assert(Matrices.sparse(3, 2, Array(0, 1, 2), Array(1, 2), Array(3.1, 3.5)) ~== + Matrices.sparse(3, 2, Array(0, 1, 2), Array(1, 2), Array(3.130, 3.534)) relTol 0.01) + + assert(Matrices.sparse(3, 2, Array(0, 1, 2), Array(1, 2), Array(3.1, 3.5)) ~= + Matrices.sparse(3, 2, Array(0, 1, 2), Array(1, 2), Array(3.130, 3.534)) relTol 0.01) + + assert(Matrices.sparse(3, 2, Array(0, 1, 2), Array(1, 2), Array(3.1, 3.5)) !~= + Matrices.sparse(3, 2, Array(0, 1, 2), Array(1, 2), Array(3.135, 3.534)) relTol 0.01) + + assert(Matrices.sparse(3, 2, Array(0, 1, 2), Array(1, 2), Array(3.1, 3.5)) !~== + Matrices.sparse(3, 2, Array(0, 1, 2), Array(1, 2), Array(3.135, 3.534)) relTol 0.01) + + assert(!(Matrices.sparse(3, 2, Array(0, 1, 2), Array(1, 2), Array(3.1, 3.5)) ~= + Matrices.sparse(3, 2, Array(0, 1, 2), Array(1, 2), Array(3.135, 3.534)) relTol 0.01)) + + assert(!(Matrices.sparse(3, 2, Array(0, 1, 2), Array(1, 2), Array(3.1, 3.5)) !~= + Matrices.sparse(3, 2, Array(0, 1, 2), Array(1, 2), Array(3.130, 3.534)) relTol 0.01)) + + assert(Matrices.sparse(2, 2, Array(0, 1, 2), Array(0, 1), Array(3.1, 3.5)) !~= + Matrices.sparse(3, 2, Array(0, 1, 2), Array(1, 2), Array(3.1, 3.5)) relTol 0.01) + + assert(Matrices.sparse(2, 2, Array(0, 1, 2), Array(0, 1), Array(3.1, 3.5)) !~== + Matrices.sparse(3, 2, Array(0, 1, 2), Array(1, 2), Array(3.1, 3.5)) relTol 0.01) + + assert(Matrices.sparse(0, 0, Array(1), Array(0), Array(0)) !~== + Matrices.sparse(3, 2, Array(0, 1, 2), Array(1, 2), Array(3.1, 3.5)) relTol 0.01) + + assert(Matrices.sparse(0, 0, Array(1), Array(0), Array(0)) !~= + Matrices.sparse(3, 2, Array(0, 1, 2), Array(1, 2), Array(3.1, 3.5)) relTol 0.01) + + // Comparisons of a dense Matrix and a sparse Matrix + assert(Matrices.sparse(2, 2, Array(0, 1, 2), Array(0, 1), Array(3.1, 3.5)) ~= + Matrices.dense(2, 2, Array(3.130, 0, 0, 3.534)) relTol 0.01) + + assert(Matrices.sparse(2, 2, Array(0, 1, 2), Array(0, 1), Array(3.1, 3.5)) ~== + Matrices.dense(2, 2, Array(3.130, 0, 0, 3.534)) relTol 0.01) + + assert(Matrices.sparse(2, 2, Array(0, 1, 2), Array(0, 1), Array(3.1, 3.5)) !~= + Matrices.dense(2, 2, Array(3.135, 0, 0, 3.534)) relTol 0.01) + + assert(Matrices.sparse(2, 2, Array(0, 1, 2), Array(0, 1), Array(3.1, 3.5)) !~== + Matrices.dense(2, 2, Array(3.135, 0, 0, 3.534)) relTol 0.01) + + assert(!(Matrices.sparse(2, 2, Array(0, 1, 2), Array(0, 1), Array(3.1, 3.5)) ~= + Matrices.dense(2, 2, Array(3.135, 0, 0, 3.534)) relTol 0.01)) + + assert(!(Matrices.sparse(2, 2, Array(0, 1, 2), Array(0, 1), Array(3.1, 3.5)) !~= + Matrices.dense(2, 2, Array(3.130, 0, 0, 3.534)) relTol 0.01)) + + assert(Matrices.sparse(2, 2, Array(0, 1, 2), Array(0, 1), Array(3.1, 3.5)) !~= + Matrices.dense(2, 1, Array(3.1, 0)) relTol 0.01) + + assert(Matrices.sparse(2, 2, Array(0, 1, 2), Array(0, 1), Array(3.1, 3.5)) !~== + Matrices.dense(2, 1, Array(3.1, 0)) relTol 0.01) + + assert(Matrices.sparse(2, 2, Array(0, 1, 2), Array(0, 1), Array(3.1, 3.5)) !~== + Matrices.dense(0, 0, Array()) relTol 0.01) + + assert(Matrices.sparse(2, 2, Array(0, 1, 2), Array(0, 1), Array(3.1, 3.5)) !~= + Matrices.dense(0, 0, Array()) relTol 0.01) } } http://git-wip-us.apache.org/repos/asf/spark/blob/c0949dc9/mllib/src/test/scala/org/apache/spark/ml/feature/CountVectorizerSuite.scala ---------------------------------------------------------------------- diff --git a/mllib/src/test/scala/org/apache/spark/ml/feature/CountVectorizerSuite.scala b/mllib/src/test/scala/org/apache/spark/ml/feature/CountVectorizerSuite.scala index a59203c..863b66b 100644 --- a/mllib/src/test/scala/org/apache/spark/ml/feature/CountVectorizerSuite.scala +++ b/mllib/src/test/scala/org/apache/spark/ml/feature/CountVectorizerSuite.scala @@ -77,10 +77,10 @@ class CountVectorizerSuite extends SparkFunSuite with MLlibTestSparkContext test("CountVectorizer vocabSize and minDF") { val df = spark.createDataFrame(Seq( - (0, split("a b c d"), Vectors.sparse(3, Seq((0, 1.0), (1, 1.0)))), - (1, split("a b c"), Vectors.sparse(3, Seq((0, 1.0), (1, 1.0)))), - (2, split("a b"), Vectors.sparse(3, Seq((0, 1.0), (1, 1.0)))), - (3, split("a"), Vectors.sparse(3, Seq((0, 1.0))))) + (0, split("a b c d"), Vectors.sparse(2, Seq((0, 1.0), (1, 1.0)))), + (1, split("a b c"), Vectors.sparse(2, Seq((0, 1.0), (1, 1.0)))), + (2, split("a b"), Vectors.sparse(2, Seq((0, 1.0), (1, 1.0)))), + (3, split("a"), Vectors.sparse(2, Seq((0, 1.0))))) ).toDF("id", "words", "expected") val cvModel = new CountVectorizer() .setInputCol("words") http://git-wip-us.apache.org/repos/asf/spark/blob/c0949dc9/mllib/src/test/scala/org/apache/spark/mllib/util/TestingUtils.scala ---------------------------------------------------------------------- diff --git a/mllib/src/test/scala/org/apache/spark/mllib/util/TestingUtils.scala b/mllib/src/test/scala/org/apache/spark/mllib/util/TestingUtils.scala index 6de9aaf..39a6bc3 100644 --- a/mllib/src/test/scala/org/apache/spark/mllib/util/TestingUtils.scala +++ b/mllib/src/test/scala/org/apache/spark/mllib/util/TestingUtils.scala @@ -154,7 +154,7 @@ object TestingUtils { */ def absTol(eps: Double): CompareVectorRightSide = CompareVectorRightSide( (x: Vector, y: Vector, eps: Double) => { - x.toArray.zip(y.toArray).forall(x => x._1 ~= x._2 absTol eps) + x.size == y.size && x.toArray.zip(y.toArray).forall(x => x._1 ~= x._2 absTol eps) }, x, eps, ABS_TOL_MSG) /** @@ -164,7 +164,7 @@ object TestingUtils { */ def relTol(eps: Double): CompareVectorRightSide = CompareVectorRightSide( (x: Vector, y: Vector, eps: Double) => { - x.toArray.zip(y.toArray).forall(x => x._1 ~= x._2 relTol eps) + x.size == y.size && x.toArray.zip(y.toArray).forall(x => x._1 ~= x._2 relTol eps) }, x, eps, REL_TOL_MSG) override def toString: String = x.toString @@ -217,7 +217,8 @@ object TestingUtils { */ def absTol(eps: Double): CompareMatrixRightSide = CompareMatrixRightSide( (x: Matrix, y: Matrix, eps: Double) => { - x.toArray.zip(y.toArray).forall(x => x._1 ~= x._2 absTol eps) + x.numRows == y.numRows && x.numCols == y.numCols && + x.toArray.zip(y.toArray).forall(x => x._1 ~= x._2 absTol eps) }, x, eps, ABS_TOL_MSG) /** @@ -227,7 +228,8 @@ object TestingUtils { */ def relTol(eps: Double): CompareMatrixRightSide = CompareMatrixRightSide( (x: Matrix, y: Matrix, eps: Double) => { - x.toArray.zip(y.toArray).forall(x => x._1 ~= x._2 relTol eps) + x.numRows == y.numRows && x.numCols == y.numCols && + x.toArray.zip(y.toArray).forall(x => x._1 ~= x._2 relTol eps) }, x, eps, REL_TOL_MSG) override def toString: String = x.toString http://git-wip-us.apache.org/repos/asf/spark/blob/c0949dc9/mllib/src/test/scala/org/apache/spark/mllib/util/TestingUtilsSuite.scala ---------------------------------------------------------------------- diff --git a/mllib/src/test/scala/org/apache/spark/mllib/util/TestingUtilsSuite.scala b/mllib/src/test/scala/org/apache/spark/mllib/util/TestingUtilsSuite.scala index 44c3970..1aff444 100644 --- a/mllib/src/test/scala/org/apache/spark/mllib/util/TestingUtilsSuite.scala +++ b/mllib/src/test/scala/org/apache/spark/mllib/util/TestingUtilsSuite.scala @@ -20,7 +20,7 @@ package org.apache.spark.mllib.util import org.scalatest.exceptions.TestFailedException import org.apache.spark.SparkFunSuite -import org.apache.spark.mllib.linalg.Vectors +import org.apache.spark.mllib.linalg.{Matrices, Vectors} import org.apache.spark.mllib.util.TestingUtils._ class TestingUtilsSuite extends SparkFunSuite { @@ -109,6 +109,10 @@ class TestingUtilsSuite extends SparkFunSuite { assert(Vectors.dense(Array(3.1, 3.5)) !~= Vectors.dense(Array(3.135, 3.534)) relTol 0.01) assert(!(Vectors.dense(Array(3.1, 3.5)) !~= Vectors.dense(Array(3.130, 3.534)) relTol 0.01)) assert(!(Vectors.dense(Array(3.1, 3.5)) ~= Vectors.dense(Array(3.135, 3.534)) relTol 0.01)) + assert(Vectors.dense(Array(3.1)) !~= Vectors.dense(Array(3.130, 3.534)) relTol 0.01) + assert(Vectors.dense(Array[Double]()) !~= Vectors.dense(Array(3.130, 3.534)) relTol 0.01) + assert(Vectors.dense(Array(3.1)) !~== Vectors.dense(Array(3.130, 3.534)) relTol 0.01) + assert(Vectors.dense(Array[Double]()) !~== Vectors.dense(Array(3.130, 3.534)) relTol 0.01) // Should throw exception with message when test fails. intercept[TestFailedException]( @@ -117,6 +121,12 @@ class TestingUtilsSuite extends SparkFunSuite { intercept[TestFailedException]( Vectors.dense(Array(3.1, 3.5)) ~== Vectors.dense(Array(3.135, 3.534)) relTol 0.01) + intercept[TestFailedException]( + Vectors.dense(Array(3.1)) ~== Vectors.dense(Array(3.535, 3.534)) relTol 0.01) + + intercept[TestFailedException]( + Vectors.dense(Array[Double]()) ~== Vectors.dense(Array(3.135)) relTol 0.01) + // Comparing against zero should fail the test and throw exception with message // saying that the relative error is meaningless in this situation. intercept[TestFailedException]( @@ -125,12 +135,18 @@ class TestingUtilsSuite extends SparkFunSuite { intercept[TestFailedException]( Vectors.dense(Array(3.1, 0.01)) ~== Vectors.sparse(2, Array(0), Array(3.13)) relTol 0.01) - // Comparisons of two sparse vectors + // Comparisons of a sparse vector and a dense vector assert(Vectors.dense(Array(3.1, 3.5)) ~== Vectors.sparse(2, Array(0, 1), Array(3.130, 3.534)) relTol 0.01) assert(Vectors.dense(Array(3.1, 3.5)) !~== Vectors.sparse(2, Array(0, 1), Array(3.135, 3.534)) relTol 0.01) + + assert(Vectors.dense(Array(3.1)) !~== + Vectors.sparse(2, Array(0, 1), Array(3.130, 3.534)) relTol 0.01) + + assert(Vectors.dense(Array[Double]()) !~== + Vectors.sparse(2, Array(0, 1), Array(3.130, 3.534)) relTol 0.01) } test("Comparing vectors using absolute error.") { @@ -154,6 +170,21 @@ class TestingUtilsSuite extends SparkFunSuite { assert(!(Vectors.dense(Array(3.1, 3.5, 0.0)) ~= Vectors.dense(Array(3.1 + 1E-5, 3.5 + 2E-7, 1 + 1E-3)) absTol 1E-6)) + assert(Vectors.dense(Array(3.1)) !~= + Vectors.dense(Array(3.1 + 1E-6, 3.5 + 2E-7)) absTol 1E-5) + + assert(!(Vectors.dense(Array(3.1)) ~= + Vectors.dense(Array(3.1 + 1E-6, 3.5 + 2E-7)) absTol 1E-5)) + + assert(Vectors.dense(Array[Double]()) !~= + Vectors.dense(Array(3.1 + 1E-6, 3.5 + 2E-7)) absTol 1E-5) + + assert(!(Vectors.dense(Array[Double]()) ~= + Vectors.dense(Array(3.1 + 1E-6, 3.5 + 2E-7)) absTol 1E-5)) + + assert(Vectors.dense(Array[Double]()) ~= + Vectors.dense(Array[Double]()) absTol 1E-5) + // Should throw exception with message when test fails. intercept[TestFailedException](Vectors.dense(Array(3.1, 3.5, 0.0)) !~== Vectors.dense(Array(3.1 + 1E-8, 3.5 + 2E-7, 1E-8)) absTol 1E-6) @@ -161,6 +192,12 @@ class TestingUtilsSuite extends SparkFunSuite { intercept[TestFailedException](Vectors.dense(Array(3.1, 3.5, 0.0)) ~== Vectors.dense(Array(3.1 + 1E-5, 3.5 + 2E-7, 1 + 1E-3)) absTol 1E-6) + intercept[TestFailedException](Vectors.dense(Array(3.1)) ~== + Vectors.dense(Array(3.1 + 1E-5, 3.5 + 2E-7)) absTol 1E-6) + + intercept[TestFailedException](Vectors.dense(Array[Double]()) ~== + Vectors.dense(Array(3.1 + 1E-5, 3.5 + 2E-7)) absTol 1E-6) + // Comparisons of two sparse vectors assert(Vectors.sparse(3, Array(0, 2), Array(3.1, 2.4)) ~== Vectors.sparse(3, Array(0, 2), Array(3.1 + 1E-8, 2.4 + 1E-7)) absTol 1E-6) @@ -174,6 +211,12 @@ class TestingUtilsSuite extends SparkFunSuite { assert(Vectors.sparse(3, Array(0, 2), Array(3.1 + 1E-3, 2.4)) !~== Vectors.sparse(3, Array(0, 2), Array(3.1, 2.4)) absTol 1E-6) + assert(Vectors.sparse(3, Array(0, 2), Array(3.1 + 1E-6, 2.4)) !~== + Vectors.sparse(1, Array(0), Array(3.1)) absTol 1E-3) + + assert(Vectors.sparse(0, Array[Int](), Array[Double]()) !~== + Vectors.sparse(1, Array(0), Array(3.1)) absTol 1E-3) + // Comparisons of a dense vector and a sparse vector assert(Vectors.sparse(3, Array(0, 2), Array(3.1, 2.4)) ~== Vectors.dense(Array(3.1 + 1E-8, 0, 2.4 + 1E-7)) absTol 1E-6) @@ -183,5 +226,235 @@ class TestingUtilsSuite extends SparkFunSuite { assert(Vectors.sparse(3, Array(0, 2), Array(3.1, 2.4)) !~== Vectors.dense(Array(3.1, 1E-3, 2.4)) absTol 1E-6) + + assert(Vectors.sparse(3, Array(0, 2), Array(3.1, 2.4)) !~== + Vectors.dense(Array(3.1)) absTol 1E-6) + + assert(Vectors.dense(Array[Double]()) !~== + Vectors.sparse(3, Array(0, 2), Array(0, 2.4)) absTol 1E-6) + + assert(Vectors.sparse(1, Array(0), Array(3.1)) !~== + Vectors.dense(Array(3.1, 3.2)) absTol 1E-6) + + assert(Vectors.dense(Array(3.1)) !~== + Vectors.sparse(0, Array[Int](), Array[Double]()) absTol 1E-6) + } + + test("Comparing Matrices using absolute error.") { + + // Comparisons of two dense Matrices + assert(Matrices.dense(2, 2, Array(3.1, 3.5, 3.1, 3.5)) ~== + Matrices.dense(2, 2, Array(3.1 + 1E-8, 3.5 + 2E-7, 3.1 + 1E-8, 3.5 + 1E-7)) absTol 1E-6) + + assert(Matrices.dense(2, 2, Array(3.1, 3.5, 3.1, 3.5)) ~= + Matrices.dense(2, 2, Array(3.1 + 1E-8, 3.5 + 2E-7, 3.1 + 1E-8, 3.5 + 1E-7)) absTol 1E-6) + + assert(Matrices.dense(2, 2, Array(3.1, 3.5, 3.1, 3.5)) !~== + Matrices.dense(2, 2, Array(3.1 + 1E-5, 3.5 + 2E-6, 3.1 + 1E-8, 3.5 + 1E-7)) absTol 1E-6) + + assert(Matrices.dense(2, 2, Array(3.1, 3.5, 3.1, 3.5)) !~= + Matrices.dense(2, 2, Array(3.1 + 1E-5, 3.5 + 2E-6, 3.1 + 1E-8, 3.5 + 1E-7)) absTol 1E-6) + + assert(!(Matrices.dense(2, 2, Array(3.1, 3.5, 3.1, 3.5)) ~= + Matrices.dense(2, 2, Array(3.1 + 1E-5, 3.5 + 2E-6, 3.1 + 1E-8, 3.5 + 1E-7)) absTol 1E-6)) + + assert(!(Matrices.dense(2, 2, Array(3.1, 3.5, 3.1, 3.5)) !~= + Matrices.dense(2, 2, Array(3.1 + 1E-7, 3.5 + 2E-8, 3.1 + 1E-8, 3.5 + 1E-7)) absTol 1E-6)) + + assert(Matrices.dense(2, 1, Array(3.1, 3.5)) !~== + Matrices.dense(2, 2, Array(3.1 + 1E-7, 3.5 + 2E-8, 3.1 + 1E-8, 3.5 + 1E-7)) absTol 1E-6) + + assert(Matrices.dense(2, 1, Array(3.1, 3.5)) !~= + Matrices.dense(2, 2, Array(3.1 + 1E-7, 3.5 + 2E-8, 3.1 + 1E-8, 3.5 + 1E-7)) absTol 1E-6) + + assert(Matrices.dense(0, 0, Array()) !~= + Matrices.dense(2, 2, Array(3.1 + 1E-7, 3.5 + 2E-8, 3.1 + 1E-8, 3.5 + 1E-7)) absTol 1E-6) + + assert(Matrices.dense(0, 0, Array()) !~== + Matrices.dense(2, 2, Array(3.1 + 1E-7, 3.5 + 2E-8, 3.1 + 1E-8, 3.5 + 1E-7)) absTol 1E-6) + + // Should throw exception with message when test fails. + intercept[TestFailedException](Matrices.dense(2, 2, Array(3.1, 3.5, 3.1, 3.5)) !~== + Matrices.dense(2, 2, Array(3.1 + 1E-8, 3.5 + 2E-7, 3.1 + 1E-8, 3.5 + 1E-7)) absTol 1E-6) + + intercept[TestFailedException](Matrices.dense(2, 2, Array(3.1, 3.5, 3.1, 3.5)) ~== + Matrices.dense(2, 2, Array(3.1 + 1E-8, 3.5 + 2E-7, 3.1 + 1E-8, 3.5 + 1E-7)) absTol 1E-9) + + intercept[TestFailedException](Matrices.dense(2, 1, Array(3.1, 3.5)) ~== + Matrices.dense(2, 2, Array(3.1 + 1E-8, 3.5 + 2E-7, 3.1 + 1E-8, 3.5 + 1E-7)) absTol 1E-5) + + intercept[TestFailedException](Matrices.dense(0, 0, Array()) ~== + Matrices.dense(2, 2, Array(3.1 + 1E-8, 3.5 + 2E-7, 3.1 + 1E-8, 3.5 + 1E-7)) absTol 1E-5) + + // Comparisons of two sparse Matrices + assert(Matrices.sparse(3, 2, Array(0, 1, 2), Array(1, 2), Array(3.1, 3.5)) ~== + Matrices.sparse(3, 2, Array(0, 1, 2), Array(1, 2), Array(3.1 + 1E-8, 3.5 + 1E-7)) absTol 1E-6) + + assert(Matrices.sparse(3, 2, Array(0, 1, 2), Array(1, 2), Array(3.1, 3.5)) ~= + Matrices.sparse(3, 2, Array(0, 1, 2), Array(1, 2), Array(3.1 + 1E-8, 3.5 + 1E-7)) absTol 1E-6) + + assert(Matrices.sparse(3, 2, Array(0, 1, 2), Array(1, 2), Array(3.1, 3.5)) !~= + Matrices.sparse(3, 2, Array(0, 1, 2), Array(1, 2), Array(3.1 + 1E-8, 3.5 + 1E-7)) absTol 1E-9) + + assert(Matrices.sparse(3, 2, Array(0, 1, 2), Array(1, 2), Array(3.1, 3.5)) !~== + Matrices.sparse(3, 2, Array(0, 1, 2), Array(1, 2), Array(3.1 + 1E-8, 3.5 + 1E-7)) absTol 1E-9) + + assert(!(Matrices.sparse(3, 2, Array(0, 1, 2), Array(1, 2), Array(3.1, 3.5)) ~= + Matrices.sparse(3, 2, Array(0, 1, 2), Array(1, 2), Array(3.1 + 1E-8, 3.5)) absTol 1E-9)) + + assert(!(Matrices.sparse(3, 2, Array(0, 1, 2), Array(1, 2), Array(3.1, 3.5)) !~= + Matrices.sparse(3, 2, Array(0, 1, 2), Array(1, 2), Array(3.1 + 1E-8, 3.5)) absTol 1E-6)) + + assert(Matrices.sparse(2, 2, Array(0, 1, 2), Array(0, 1), Array(3.1, 3.5)) !~= + Matrices.sparse(3, 2, Array(0, 1, 2), Array(1, 2), Array(3.1 + 1E-8, 3.5 + 1E-7)) absTol 1E-9) + + assert(Matrices.sparse(2, 2, Array(0, 1, 2), Array(0, 1), Array(3.1, 3.5)) !~== + Matrices.sparse(3, 2, Array(0, 1, 2), Array(1, 2), Array(3.1 + 1E-8, 3.5 + 1E-7)) absTol 1E-6) + + assert(Matrices.sparse(0, 0, Array(1), Array(0), Array(0)) !~== + Matrices.sparse(3, 2, Array(0, 1, 2), Array(1, 2), Array(3.1 + 1E-8, 3.5 + 1E-7)) absTol 1E-6) + + assert(Matrices.sparse(0, 0, Array(1), Array(0), Array(0)) !~= + Matrices.sparse(3, 2, Array(0, 1, 2), Array(1, 2), Array(3.1 + 1E-8, 3.5 + 1E-7)) absTol 1E-6) + + // Comparisons of a dense Matrix and a sparse Matrix + assert(Matrices.sparse(2, 2, Array(0, 1, 2), Array(0, 1), Array(3.1, 3.5)) ~= + Matrices.dense(2, 2, Array(3.1 + 1E-8, 0, 0, 3.5 + 1E-7)) absTol 1E-6) + + assert(Matrices.sparse(2, 2, Array(0, 1, 2), Array(0, 1), Array(3.1, 3.5)) ~== + Matrices.dense(2, 2, Array(3.1 + 1E-8, 0, 0, 3.5 + 1E-7)) absTol 1E-6) + + assert(Matrices.sparse(2, 2, Array(0, 1, 2), Array(0, 1), Array(3.1, 3.5)) !~== + Matrices.dense(2, 2, Array(3.1 + 1E-8, 0, 0, 3.5 + 1E-7)) absTol 1E-9) + + assert(Matrices.sparse(2, 2, Array(0, 1, 2), Array(0, 1), Array(3.1, 3.5)) !~== + Matrices.dense(2, 2, Array(3.1 + 1E-8, 0, 0, 3.5 + 1E-7)) absTol 1E-9) + + assert(!(Matrices.sparse(2, 2, Array(0, 1, 2), Array(0, 1), Array(3.1, 3.5)) ~= + Matrices.dense(2, 2, Array(3.1 + 1E-8, 0, 0, 3.5 + 1E-7)) absTol 1E-9)) + + assert(!(Matrices.sparse(2, 2, Array(0, 1, 2), Array(0, 1), Array(3.1, 3.5)) !~= + Matrices.dense(2, 2, Array(3.1 + 1E-8, 0, 0, 3.5 + 1E-7)) absTol 1E-6)) + + assert(Matrices.sparse(2, 2, Array(0, 1, 2), Array(0, 1), Array(3.1, 3.5)) !~= + Matrices.dense(2, 1, Array(3.1 + 1E-8, 0)) absTol 1E-6) + + assert(Matrices.sparse(2, 2, Array(0, 1, 2), Array(0, 1), Array(3.1, 3.5)) !~== + Matrices.dense(2, 1, Array(3.1 + 1E-8, 0)) absTol 1E-6) + + assert(Matrices.sparse(2, 2, Array(0, 1, 2), Array(0, 1), Array(3.1, 3.5)) !~== + Matrices.dense(0, 0, Array()) absTol 1E-6) + + assert(Matrices.sparse(2, 2, Array(0, 1, 2), Array(0, 1), Array(3.1, 3.5)) !~= + Matrices.dense(0, 0, Array()) absTol 1E-6) + } + + test("Comparing Matrices using relative error.") { + + // Comparisons of two dense Matrices + assert(Matrices.dense(2, 2, Array(3.1, 3.5, 3.1, 3.5)) ~== + Matrices.dense(2, 2, Array(3.130, 3.534, 3.130, 3.534)) relTol 0.01) + + assert(Matrices.dense(2, 2, Array(3.1, 3.5, 3.1, 3.5)) ~= + Matrices.dense(2, 2, Array(3.130, 3.534, 3.130, 3.534)) relTol 0.01) + + assert(Matrices.dense(2, 2, Array(3.1, 3.5, 3.1, 3.5)) !~== + Matrices.dense(2, 2, Array(3.135, 3.534, 3.135, 3.534)) relTol 0.01) + + assert(Matrices.dense(2, 2, Array(3.1, 3.5, 3.1, 3.5)) !~= + Matrices.dense(2, 2, Array(3.135, 3.534, 3.135, 3.534)) relTol 0.01) + + assert(!(Matrices.dense(2, 2, Array(3.1, 3.5, 3.1, 3.5)) ~= + Matrices.dense(2, 2, Array(3.134, 3.535, 3.134, 3.535)) relTol 0.01)) + + assert(!(Matrices.dense(2, 2, Array(3.1, 3.5, 3.1, 3.5)) !~= + Matrices.dense(2, 2, Array(3.130, 3.534, 3.130, 3.534)) relTol 0.01)) + + assert(Matrices.dense(2, 1, Array(3.1, 3.5)) !~== + Matrices.dense(2, 2, Array(3.1, 3.5, 3.1, 3.5)) relTol 0.01) + + assert(Matrices.dense(2, 1, Array(3.1, 3.5)) !~= + Matrices.dense(2, 2, Array(3.1, 3.5, 3.1, 3.5)) relTol 0.01) + + assert(Matrices.dense(0, 0, Array()) !~= + Matrices.dense(2, 2, Array(3.1, 3.5, 3.1, 3.5)) relTol 0.01) + + assert(Matrices.dense(0, 0, Array()) !~== + Matrices.dense(2, 2, Array(3.1, 3.5, 3.1, 3.5)) relTol 0.01) + + // Should throw exception with message when test fails. + intercept[TestFailedException](Matrices.dense(2, 2, Array(3.1, 3.5, 3.1, 3.5)) !~== + Matrices.dense(2, 2, Array(3.130, 3.534, 3.130, 3.534)) relTol 0.01) + + intercept[TestFailedException](Matrices.dense(2, 2, Array(3.1, 3.5, 3.1, 3.5)) ~== + Matrices.dense(2, 2, Array(3.135, 3.534, 3.135, 3.534)) relTol 0.01) + + intercept[TestFailedException](Matrices.dense(2, 1, Array(3.1, 3.5)) ~== + Matrices.dense(2, 2, Array(3.1, 3.5, 3.1, 3.5)) relTol 0.01) + + intercept[TestFailedException](Matrices.dense(0, 0, Array()) ~== + Matrices.dense(2, 2, Array(3.1, 3.5, 3.1, 3.5)) relTol 0.01) + + // Comparisons of two sparse Matrices + assert(Matrices.sparse(3, 2, Array(0, 1, 2), Array(1, 2), Array(3.1, 3.5)) ~== + Matrices.sparse(3, 2, Array(0, 1, 2), Array(1, 2), Array(3.130, 3.534)) relTol 0.01) + + assert(Matrices.sparse(3, 2, Array(0, 1, 2), Array(1, 2), Array(3.1, 3.5)) ~= + Matrices.sparse(3, 2, Array(0, 1, 2), Array(1, 2), Array(3.130, 3.534)) relTol 0.01) + + assert(Matrices.sparse(3, 2, Array(0, 1, 2), Array(1, 2), Array(3.1, 3.5)) !~= + Matrices.sparse(3, 2, Array(0, 1, 2), Array(1, 2), Array(3.135, 3.534)) relTol 0.01) + + assert(Matrices.sparse(3, 2, Array(0, 1, 2), Array(1, 2), Array(3.1, 3.5)) !~== + Matrices.sparse(3, 2, Array(0, 1, 2), Array(1, 2), Array(3.135, 3.534)) relTol 0.01) + + assert(!(Matrices.sparse(3, 2, Array(0, 1, 2), Array(1, 2), Array(3.1, 3.5)) ~= + Matrices.sparse(3, 2, Array(0, 1, 2), Array(1, 2), Array(3.135, 3.534)) relTol 0.01)) + + assert(!(Matrices.sparse(3, 2, Array(0, 1, 2), Array(1, 2), Array(3.1, 3.5)) !~= + Matrices.sparse(3, 2, Array(0, 1, 2), Array(1, 2), Array(3.130, 3.534)) relTol 0.01)) + + assert(Matrices.sparse(2, 2, Array(0, 1, 2), Array(0, 1), Array(3.1, 3.5)) !~= + Matrices.sparse(3, 2, Array(0, 1, 2), Array(1, 2), Array(3.1, 3.5)) relTol 0.01) + + assert(Matrices.sparse(2, 2, Array(0, 1, 2), Array(0, 1), Array(3.1, 3.5)) !~== + Matrices.sparse(3, 2, Array(0, 1, 2), Array(1, 2), Array(3.1, 3.5)) relTol 0.01) + + assert(Matrices.sparse(0, 0, Array(1), Array(0), Array(0)) !~== + Matrices.sparse(3, 2, Array(0, 1, 2), Array(1, 2), Array(3.1, 3.5)) relTol 0.01) + + assert(Matrices.sparse(0, 0, Array(1), Array(0), Array(0)) !~= + Matrices.sparse(3, 2, Array(0, 1, 2), Array(1, 2), Array(3.1, 3.5)) relTol 0.01) + + // Comparisons of a dense Matrix and a sparse Matrix + assert(Matrices.sparse(2, 2, Array(0, 1, 2), Array(0, 1), Array(3.1, 3.5)) ~= + Matrices.dense(2, 2, Array(3.130, 0, 0, 3.534)) relTol 0.01) + + assert(Matrices.sparse(2, 2, Array(0, 1, 2), Array(0, 1), Array(3.1, 3.5)) ~== + Matrices.dense(2, 2, Array(3.130, 0, 0, 3.534)) relTol 0.01) + + assert(Matrices.sparse(2, 2, Array(0, 1, 2), Array(0, 1), Array(3.1, 3.5)) !~= + Matrices.dense(2, 2, Array(3.135, 0, 0, 3.534)) relTol 0.01) + + assert(Matrices.sparse(2, 2, Array(0, 1, 2), Array(0, 1), Array(3.1, 3.5)) !~== + Matrices.dense(2, 2, Array(3.135, 0, 0, 3.534)) relTol 0.01) + + assert(!(Matrices.sparse(2, 2, Array(0, 1, 2), Array(0, 1), Array(3.1, 3.5)) ~= + Matrices.dense(2, 2, Array(3.135, 0, 0, 3.534)) relTol 0.01)) + + assert(!(Matrices.sparse(2, 2, Array(0, 1, 2), Array(0, 1), Array(3.1, 3.5)) !~= + Matrices.dense(2, 2, Array(3.130, 0, 0, 3.534)) relTol 0.01)) + + assert(Matrices.sparse(2, 2, Array(0, 1, 2), Array(0, 1), Array(3.1, 3.5)) !~= + Matrices.dense(2, 1, Array(3.1, 0)) relTol 0.01) + + assert(Matrices.sparse(2, 2, Array(0, 1, 2), Array(0, 1), Array(3.1, 3.5)) !~== + Matrices.dense(2, 1, Array(3.1, 0)) relTol 0.01) + + assert(Matrices.sparse(2, 2, Array(0, 1, 2), Array(0, 1), Array(3.1, 3.5)) !~== + Matrices.dense(0, 0, Array()) relTol 0.01) + + assert(Matrices.sparse(2, 2, Array(0, 1, 2), Array(0, 1), Array(3.1, 3.5)) !~= + Matrices.dense(0, 0, Array()) relTol 0.01) } } --------------------------------------------------------------------- To unsubscribe, e-mail: commits-unsubscr...@spark.apache.org For additional commands, e-mail: commits-h...@spark.apache.org