MATH-1596: Remove dependency on "RandomVectorGenerator".

This commit is contained in:
Gilles Sadowski 2021-05-31 02:42:34 +02:00
parent 7eadea3167
commit 55e7cf0a51
4 changed files with 18 additions and 16 deletions

View File

@ -16,6 +16,8 @@
*/ */
package org.apache.commons.math4.legacy.random; package org.apache.commons.math4.legacy.random;
import java.util.function.Supplier;
import org.apache.commons.math4.legacy.exception.DimensionMismatchException; import org.apache.commons.math4.legacy.exception.DimensionMismatchException;
import org.apache.commons.math4.legacy.exception.NotPositiveException; import org.apache.commons.math4.legacy.exception.NotPositiveException;
import org.apache.commons.math4.legacy.exception.NullArgumentException; import org.apache.commons.math4.legacy.exception.NullArgumentException;
@ -41,7 +43,7 @@ import org.apache.commons.math4.legacy.exception.OutOfRangeException;
* <p> * <p>
* The generator supports two modes: * The generator supports two modes:
* <ul> * <ul>
* <li>sequential generation of points: {@link #nextVector()}</li> * <li>sequential generation of points: {@link #get()}</li>
* <li>random access to the i-th point in the sequence: {@link #skipTo(int)}</li> * <li>random access to the i-th point in the sequence: {@link #skipTo(int)}</li>
* </ul> * </ul>
* *
@ -50,7 +52,7 @@ import org.apache.commons.math4.legacy.exception.OutOfRangeException;
* On the Halton sequence and its scramblings</a> * On the Halton sequence and its scramblings</a>
* @since 3.3 * @since 3.3
*/ */
public class HaltonSequenceGenerator implements RandomVectorGenerator { public class HaltonSequenceGenerator implements Supplier<double[]> {
/** The first 40 primes. */ /** The first 40 primes. */
private static final int[] PRIMES = new int[] { private static final int[] PRIMES = new int[] {
@ -119,7 +121,7 @@ public class HaltonSequenceGenerator implements RandomVectorGenerator {
/** {@inheritDoc} */ /** {@inheritDoc} */
@Override @Override
public double[] nextVector() { public double[] get() {
final double[] v = new double[dimension]; final double[] v = new double[dimension];
for (int i = 0; i < dimension; i++) { for (int i = 0; i < dimension; i++) {
int index = count; int index = count;
@ -165,12 +167,12 @@ public class HaltonSequenceGenerator implements RandomVectorGenerator {
*/ */
public double[] skipTo(final int index) { public double[] skipTo(final int index) {
count = index; count = index;
return nextVector(); return get();
} }
/** /**
* Returns the index i of the next point in the Halton sequence that will be returned * Returns the index i of the next point in the Halton sequence that will be returned
* by calling {@link #nextVector()}. * by calling {@link #get()}.
* *
* @return the index of the next point * @return the index of the next point
*/ */

View File

@ -24,6 +24,7 @@ import java.nio.charset.Charset;
import java.util.Arrays; import java.util.Arrays;
import java.util.NoSuchElementException; import java.util.NoSuchElementException;
import java.util.StringTokenizer; import java.util.StringTokenizer;
import java.util.function.Supplier;
import org.apache.commons.math4.legacy.exception.MathInternalError; import org.apache.commons.math4.legacy.exception.MathInternalError;
import org.apache.commons.math4.legacy.exception.MathParseException; import org.apache.commons.math4.legacy.exception.MathParseException;
@ -44,7 +45,7 @@ import org.apache.commons.math4.legacy.util.FastMath;
* <p> * <p>
* The generator supports two modes: * The generator supports two modes:
* <ul> * <ul>
* <li>sequential generation of points: {@link #nextVector()}</li> * <li>sequential generation of points: {@link #get()}</li>
* <li>random access to the i-th point in the sequence: {@link #skipTo(int)}</li> * <li>random access to the i-th point in the sequence: {@link #skipTo(int)}</li>
* </ul> * </ul>
* *
@ -53,8 +54,7 @@ import org.apache.commons.math4.legacy.util.FastMath;
* *
* @since 3.3 * @since 3.3
*/ */
public class SobolSequenceGenerator implements RandomVectorGenerator { public class SobolSequenceGenerator implements Supplier<double[]> {
/** The number of bits to use. */ /** The number of bits to use. */
private static final int BITS = 52; private static final int BITS = 52;
@ -253,7 +253,7 @@ public class SobolSequenceGenerator implements RandomVectorGenerator {
/** {@inheritDoc} */ /** {@inheritDoc} */
@Override @Override
public double[] nextVector() { public double[] get() {
final double[] v = new double[dimension]; final double[] v = new double[dimension];
if (count == 0) { if (count == 0) {
count++; count++;
@ -308,12 +308,12 @@ public class SobolSequenceGenerator implements RandomVectorGenerator {
} }
} }
count = index; count = index;
return nextVector(); return get();
} }
/** /**
* Returns the index i of the next point in the Sobol sequence that will be returned * Returns the index i of the next point in the Sobol sequence that will be returned
* by calling {@link #nextVector()}. * by calling {@link #get()}.
* *
* @return the index of the next point * @return the index of the next point
*/ */

View File

@ -61,7 +61,7 @@ public class HaltonSequenceGeneratorTest {
@Test @Test
public void test3DReference() { public void test3DReference() {
for (int i = 0; i < referenceValues.length; i++) { for (int i = 0; i < referenceValues.length; i++) {
double[] result = generator.nextVector(); double[] result = generator.get();
Assert.assertArrayEquals(referenceValues[i], result, 1e-3); Assert.assertArrayEquals(referenceValues[i], result, 1e-3);
Assert.assertEquals(i + 1, generator.getNextIndex()); Assert.assertEquals(i + 1, generator.getNextIndex());
} }
@ -71,7 +71,7 @@ public class HaltonSequenceGeneratorTest {
public void test2DUnscrambledReference() { public void test2DUnscrambledReference() {
generator = new HaltonSequenceGenerator(2, new int[] {2, 3}, null); generator = new HaltonSequenceGenerator(2, new int[] {2, 3}, null);
for (int i = 0; i < referenceValuesUnscrambled.length; i++) { for (int i = 0; i < referenceValuesUnscrambled.length; i++) {
double[] result = generator.nextVector(); double[] result = generator.get();
Assert.assertArrayEquals(referenceValuesUnscrambled[i], result, 1e-3); Assert.assertArrayEquals(referenceValuesUnscrambled[i], result, 1e-3);
Assert.assertEquals(i + 1, generator.getNextIndex()); Assert.assertEquals(i + 1, generator.getNextIndex());
} }
@ -125,7 +125,7 @@ public class HaltonSequenceGeneratorTest {
Assert.assertEquals(6, generator.getNextIndex()); Assert.assertEquals(6, generator.getNextIndex());
for (int i = 6; i < referenceValues.length; i++) { for (int i = 6; i < referenceValues.length; i++) {
result = generator.nextVector(); result = generator.get();
Assert.assertArrayEquals(referenceValues[i], result, 1e-3); Assert.assertArrayEquals(referenceValues[i], result, 1e-3);
Assert.assertEquals(i + 1, generator.getNextIndex()); Assert.assertEquals(i + 1, generator.getNextIndex());
} }

View File

@ -50,7 +50,7 @@ public class SobolSequenceGeneratorTest {
@Test @Test
public void test3DReference() { public void test3DReference() {
for (int i = 0; i < referenceValues.length; i++) { for (int i = 0; i < referenceValues.length; i++) {
double[] result = generator.nextVector(); double[] result = generator.get();
Assert.assertArrayEquals(referenceValues[i], result, 1e-6); Assert.assertArrayEquals(referenceValues[i], result, 1e-6);
Assert.assertEquals(i + 1, generator.getNextIndex()); Assert.assertEquals(i + 1, generator.getNextIndex());
} }
@ -98,7 +98,7 @@ public class SobolSequenceGeneratorTest {
Assert.assertEquals(6, generator.getNextIndex()); Assert.assertEquals(6, generator.getNextIndex());
for (int i = 6; i < referenceValues.length; i++) { for (int i = 6; i < referenceValues.length; i++) {
result = generator.nextVector(); result = generator.get();
Assert.assertArrayEquals(referenceValues[i], result, 1e-6); Assert.assertArrayEquals(referenceValues[i], result, 1e-6);
Assert.assertEquals(i + 1, generator.getNextIndex()); Assert.assertEquals(i + 1, generator.getNextIndex());
} }