You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@spark.apache.org by rx...@apache.org on 2014/01/11 00:32:24 UTC

[2/3] git commit: SPARK-961. Add a Vector.random() method - update 1

SPARK-961. Add a Vector.random() method - update 1


Project: http://git-wip-us.apache.org/repos/asf/incubator-spark/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-spark/commit/8d021b42
Tree: http://git-wip-us.apache.org/repos/asf/incubator-spark/tree/8d021b42
Diff: http://git-wip-us.apache.org/repos/asf/incubator-spark/diff/8d021b42

Branch: refs/heads/master
Commit: 8d021b42bc53a81172d98b556a340f7c2c4de0f3
Parents: 1814719
Author: Pillis <pi...@gmail.com>
Authored: Fri Jan 10 00:07:36 2014 -0800
Committer: Pillis <pi...@gmail.com>
Committed: Fri Jan 10 00:07:36 2014 -0800

----------------------------------------------------------------------
 .../main/scala/org/apache/spark/util/Vector.scala   |  6 +++++-
 .../scala/org/apache/spark/util/VectorSuite.scala   | 16 +++++++---------
 2 files changed, 12 insertions(+), 10 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-spark/blob/8d021b42/core/src/main/scala/org/apache/spark/util/Vector.scala
----------------------------------------------------------------------
diff --git a/core/src/main/scala/org/apache/spark/util/Vector.scala b/core/src/main/scala/org/apache/spark/util/Vector.scala
index f9c6cdf..62fd6d8 100644
--- a/core/src/main/scala/org/apache/spark/util/Vector.scala
+++ b/core/src/main/scala/org/apache/spark/util/Vector.scala
@@ -126,7 +126,11 @@ object Vector {
 
   def ones(length: Int) = Vector(length, _ => 1)
 
-  def random(length: Int, random: Random = new Random()) = Vector(length, _ => random.nextDouble());
+  /**
+   * Creates this [[org.apache.spark.util.Vector]] of given length containing random numbers 
+   * between 0.0 and 1.0. Optional [[scala.util.Random]] number generator can be provided.
+   */
+  def random(length: Int, random: Random = new XORShiftRandom()) = Vector(length, _ => random.nextDouble())
 
   class Multiplier(num: Double) {
     def * (vec: Vector) = vec * num

http://git-wip-us.apache.org/repos/asf/incubator-spark/blob/8d021b42/core/src/test/scala/org/apache/spark/util/VectorSuite.scala
----------------------------------------------------------------------
diff --git a/core/src/test/scala/org/apache/spark/util/VectorSuite.scala b/core/src/test/scala/org/apache/spark/util/VectorSuite.scala
index 23d1bdb..7006571 100644
--- a/core/src/test/scala/org/apache/spark/util/VectorSuite.scala
+++ b/core/src/test/scala/org/apache/spark/util/VectorSuite.scala
@@ -27,20 +27,18 @@ import org.scalatest.FunSuite
 class VectorSuite extends FunSuite {
 
   def verifyVector(vector: Vector, expectedLength: Int) = {
-    assert(vector.length == expectedLength); // Array must be of expected length
-    assert(vector.length == vector.elements.distinct.length); // Values should not repeat
-    assert(vector.sum > 0); // All values must not be 0
-    assert(vector.sum < vector.length); // All values must not be 1
-    assert(vector.elements.product > 0); // No value is 0
+    assert(vector.length == expectedLength)
+    assert(vector.elements.min > 0.0)
+    assert(vector.elements.max < 1.0)
   }
 
   test("random with default random number generator") {
-    val vector100 = Vector.random(100);
-    verifyVector(vector100, 100);
+    val vector100 = Vector.random(100)
+    verifyVector(vector100, 100)
   }
 
   test("random with given random number generator") {
-    val vector100 = Vector.random(100, new Random(100));
-    verifyVector(vector100, 100);
+    val vector100 = Vector.random(100, new Random(100))
+    verifyVector(vector100, 100)
   }
 }