You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by er...@apache.org on 2016/12/14 16:40:26 UTC
[2/6] [math] MATH-1393: Remove functionality provided by "Commons
RNG".
MATH-1393: Remove functionality provided by "Commons RNG".
Project: http://git-wip-us.apache.org/repos/asf/commons-math/repo
Commit: http://git-wip-us.apache.org/repos/asf/commons-math/commit/657b1b49
Tree: http://git-wip-us.apache.org/repos/asf/commons-math/tree/657b1b49
Diff: http://git-wip-us.apache.org/repos/asf/commons-math/diff/657b1b49
Branch: refs/heads/master
Commit: 657b1b49da5ea1593dd7f950eae99a88a8ada87a
Parents: d2e40b6
Author: Gilles <er...@apache.org>
Authored: Wed Dec 14 17:27:11 2016 +0100
Committer: Gilles <er...@apache.org>
Committed: Wed Dec 14 17:38:42 2016 +0100
----------------------------------------------------------------------
.../apache/commons/math4/util/MathArrays.java | 106 -------------------
.../org/apache/commons/math4/PerfTestUtils.java | 6 +-
.../commons/math4/util/ArithmeticUtilsTest.java | 6 +-
.../commons/math4/util/MathArraysTest.java | 65 ------------
.../commons/math4/util/MathUtilsTest.java | 5 +-
5 files changed, 11 insertions(+), 177 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/commons-math/blob/657b1b49/src/main/java/org/apache/commons/math4/util/MathArrays.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/math4/util/MathArrays.java b/src/main/java/org/apache/commons/math4/util/MathArrays.java
index b56196f..cb9e360 100644
--- a/src/main/java/org/apache/commons/math4/util/MathArrays.java
+++ b/src/main/java/org/apache/commons/math4/util/MathArrays.java
@@ -1562,112 +1562,6 @@ public class MathArrays {
}
/**
- * Specification for indicating that some operation applies
- * before or after a given index.
- */
- public enum Position {
- /** Designates the beginning of the array (near index 0). */
- HEAD,
- /** Designates the end of the array. */
- TAIL
- }
-
- /**
- * Shuffle the entries of the given array.
- * The {@code start} and {@code pos} parameters select which portion
- * of the array is randomized and which is left untouched.
- *
- * @see #shuffle(int[],int,Position,UniformRandomProvider)
- *
- * @param list Array whose entries will be shuffled (in-place).
- * @param start Index at which shuffling begins.
- * @param pos Shuffling is performed for index positions between
- * {@code start} and either the end (if {@link Position#TAIL})
- * or the beginning (if {@link Position#HEAD}) of the array.
- */
- public static void shuffle(int[] list,
- int start,
- Position pos) {
- shuffle(list, start, pos, RandomSource.create(RandomSource.WELL_19937_C));
- }
-
- /**
- * Shuffle the entries of the given array, using the
- * <a href="http://en.wikipedia.org/wiki/Fisher\u2013Yates_shuffle#The_modern_algorithm">
- * Fisher\u2013Yates</a> algorithm.
- * The {@code start} and {@code pos} parameters select which portion
- * of the array is randomized and which is left untouched.
- *
- * @param list Array whose entries will be shuffled (in-place).
- * @param start Index at which shuffling begins.
- * @param pos Shuffling is performed for index positions between
- * {@code start} and either the end (if {@link Position#TAIL})
- * or the beginning (if {@link Position#HEAD}) of the array.
- * @param rng Random number generator.
- */
- public static void shuffle(int[] list,
- int start,
- Position pos,
- UniformRandomProvider rng) {
- switch (pos) {
- case TAIL: {
- for (int i = list.length - 1; i >= start; i--) {
- final int target;
- if (i == start) {
- target = start;
- } else {
- target = rng.nextInt(i - start + 1) + start;
- }
- final int temp = list[target];
- list[target] = list[i];
- list[i] = temp;
- }
- }
- break;
- case HEAD: {
- for (int i = 0; i <= start; i++) {
- final int target;
- if (i == start) {
- target = start;
- } else {
- target = rng.nextInt(start - i + 1) + i;
- }
- final int temp = list[target];
- list[target] = list[i];
- list[i] = temp;
- }
- }
- break;
- default:
- throw new MathInternalError(); // Should never happen.
- }
- }
-
- /**
- * Shuffle the entries of the given array.
- *
- * @see #shuffle(int[],int,Position,UniformRandomProvider)
- *
- * @param list Array whose entries will be shuffled (in-place).
- * @param rng Random number generator.
- */
- public static void shuffle(int[] list,
- UniformRandomProvider rng) {
- shuffle(list, 0, Position.TAIL, rng);
- }
-
- /**
- * Shuffle the entries of the given array.
- *
- * @see #shuffle(int[],int,Position,UniformRandomProvider)
- *
- * @param list Array whose entries will be shuffled (in-place).
- */
- public static void shuffle(int[] list) {
- shuffle(list, RandomSource.create(RandomSource.WELL_19937_C));
- }
-
- /**
* Returns an array representing the natural number {@code n}.
*
* @param n Natural number.
http://git-wip-us.apache.org/repos/asf/commons-math/blob/657b1b49/src/test/java/org/apache/commons/math4/PerfTestUtils.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/math4/PerfTestUtils.java b/src/test/java/org/apache/commons/math4/PerfTestUtils.java
index eb965ba..f1170a4 100644
--- a/src/test/java/org/apache/commons/math4/PerfTestUtils.java
+++ b/src/test/java/org/apache/commons/math4/PerfTestUtils.java
@@ -21,9 +21,9 @@ import java.util.regex.Matcher;
import java.util.regex.MatchResult;
import java.util.concurrent.Callable;
-import org.apache.commons.math4.util.MathArrays;
import org.apache.commons.rng.UniformRandomProvider;
import org.apache.commons.rng.simple.RandomSource;
+import org.apache.commons.rng.sampling.PermutationSampler;
import org.apache.commons.math4.exception.MathIllegalStateException;
import org.apache.commons.math4.exception.NumberIsTooLargeException;
import org.apache.commons.math4.exception.util.LocalizedFormats;
@@ -124,11 +124,11 @@ public class PerfTestUtils {
// Indices into the array containing the methods to benchmark.
// The purpose is that at each repeat, the "methods" are called in a different order.
- final int[] methodSequence = MathArrays.natural(numMethods);
+ final int[] methodSequence = PermutationSampler.natural(numMethods);
try {
for (int k = 0; k < repeatStat; k++) {
- MathArrays.shuffle(methodSequence, rng);
+ PermutationSampler.shuffle(rng, methodSequence);
for (int n = 0; n < numMethods; n++) {
final int j = methodSequence[n]; // Index of the timed method.
http://git-wip-us.apache.org/repos/asf/commons-math/blob/657b1b49/src/test/java/org/apache/commons/math4/util/ArithmeticUtilsTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/math4/util/ArithmeticUtilsTest.java b/src/test/java/org/apache/commons/math4/util/ArithmeticUtilsTest.java
index 2de8ee1..f90b234 100644
--- a/src/test/java/org/apache/commons/math4/util/ArithmeticUtilsTest.java
+++ b/src/test/java/org/apache/commons/math4/util/ArithmeticUtilsTest.java
@@ -20,6 +20,9 @@ import java.util.ArrayList;
import java.util.Arrays;
import java.math.BigInteger;
+import org.apache.commons.rng.UniformRandomProvider;
+import org.apache.commons.rng.simple.RandomSource;
+import org.apache.commons.rng.sampling.PermutationSampler;
import org.apache.commons.math4.exception.MathArithmeticException;
import org.apache.commons.math4.exception.MathIllegalArgumentException;
import org.junit.Assert;
@@ -124,8 +127,9 @@ public class ArithmeticUtilsTest {
public void testGcdConsistency() {
int[] primeList = {19, 23, 53, 67, 73, 79, 101, 103, 111, 131};
+ final UniformRandomProvider rng = RandomSource.create(RandomSource.WELL_19937_A);
for (int i = 0; i < 20; i++) {
- MathArrays.shuffle(primeList);
+ PermutationSampler.shuffle(rng, primeList);
int p1 = primeList[0];
int p2 = primeList[1];
int p3 = primeList[2];
http://git-wip-us.apache.org/repos/asf/commons-math/blob/657b1b49/src/test/java/org/apache/commons/math4/util/MathArraysTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/math4/util/MathArraysTest.java b/src/test/java/org/apache/commons/math4/util/MathArraysTest.java
index 8d491b2..0f0f06c 100644
--- a/src/test/java/org/apache/commons/math4/util/MathArraysTest.java
+++ b/src/test/java/org/apache/commons/math4/util/MathArraysTest.java
@@ -1109,71 +1109,6 @@ public class MathArraysTest {
}
@Test
- public void testShuffleNoDuplicates() {
- final int n = 100;
- final int[] orig = MathArrays.natural(n);
- MathArrays.shuffle(orig);
-
- // Test that all (unique) entries exist in the shuffled array.
- final int[] count = new int[n];
- for (int i = 0; i < n; i++) {
- count[orig[i]] += 1;
- }
-
- for (int i = 0; i < n; i++) {
- Assert.assertEquals(1, count[i]);
- }
- }
-
- @Test
- public void testShuffleTail() {
- final int[] orig = new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
- final int[] list = orig.clone();
- final int start = 4;
- MathArrays.shuffle(list, start, MathArrays.Position.TAIL,
- RandomSource.create(RandomSource.WELL_19937_C, 7654321L));
-
- // Ensure that all entries below index "start" did not move.
- for (int i = 0; i < start; i++) {
- Assert.assertEquals(orig[i], list[i]);
- }
-
- // Ensure that at least one entry has moved.
- boolean ok = false;
- for (int i = start; i < orig.length - 1; i++) {
- if (orig[i] != list[i]) {
- ok = true;
- break;
- }
- }
- Assert.assertTrue(ok);
- }
-
- @Test
- public void testShuffleHead() {
- final int[] orig = new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
- final int[] list = orig.clone();
- final int start = 4;
- MathArrays.shuffle(list, start, MathArrays.Position.HEAD,
- RandomSource.create(RandomSource.WELL_19937_C, 1234567L));
-
- // Ensure that all entries above index "start" did not move.
- for (int i = start + 1; i < orig.length; i++) {
- Assert.assertEquals(orig[i], list[i]);
- }
-
- // Ensure that at least one entry has moved.
- boolean ok = false;
- for (int i = 0; i <= start; i++) {
- if (orig[i] != list[i]) {
- ok = true;
- break;
- }
- }
- Assert.assertTrue(ok);
- }
-
- @Test
public void testNatural() {
final int n = 4;
final int[] expected = {0, 1, 2, 3};
http://git-wip-us.apache.org/repos/asf/commons-math/blob/657b1b49/src/test/java/org/apache/commons/math4/util/MathUtilsTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/math4/util/MathUtilsTest.java b/src/test/java/org/apache/commons/math4/util/MathUtilsTest.java
index 17fe761..2e116a2 100644
--- a/src/test/java/org/apache/commons/math4/util/MathUtilsTest.java
+++ b/src/test/java/org/apache/commons/math4/util/MathUtilsTest.java
@@ -21,6 +21,7 @@ import org.apache.commons.math4.exception.NullArgumentException;
import org.apache.commons.math4.exception.util.LocalizedFormats;
import org.apache.commons.rng.UniformRandomProvider;
import org.apache.commons.rng.simple.RandomSource;
+import org.apache.commons.rng.sampling.PermutationSampler;
import org.junit.Assert;
import org.junit.Test;
@@ -109,8 +110,8 @@ public final class MathUtilsTest {
// Generate a random permutation, making sure it is not the identity
boolean isIdentity = true;
do {
- int[] permutation = MathArrays.natural(10);
- MathArrays.shuffle(permutation, random);
+ int[] permutation = PermutationSampler.natural(10);
+ PermutationSampler.shuffle(random, permutation);
for (int i = 0; i < 10; i++) {
if (i != permutation[i]) {
isIdentity = false;