aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--mllib-local/src/test/scala/org/apache/spark/ml/util/TestingUtils.scala10
-rw-r--r--mllib-local/src/test/scala/org/apache/spark/ml/util/TestingUtilsSuite.scala277
-rw-r--r--mllib/src/test/scala/org/apache/spark/ml/feature/CountVectorizerSuite.scala8
-rw-r--r--mllib/src/test/scala/org/apache/spark/mllib/util/TestingUtils.scala10
-rw-r--r--mllib/src/test/scala/org/apache/spark/mllib/util/TestingUtilsSuite.scala277
5 files changed, 566 insertions, 16 deletions
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 2bebaa35ba..2327917e2c 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
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 e374165f75..5cbf2f04e6 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)
}
}
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 a59203c33d..863b66bf49 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")
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 6de9aaf94f..39a6bc37d9 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
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 44c39704e5..1aff44480a 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)
}
}