Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.apache.commons.math4.legacy.exception.DimensionMismatchException;
import org.apache.commons.math4.legacy.exception.NotPositiveException;
import org.apache.commons.math4.legacy.exception.NullArgumentException;
import org.apache.commons.math4.legacy.exception.NumberIsTooSmallException;
import org.apache.commons.math4.legacy.exception.OutOfRangeException;

/**
Expand Down Expand Up @@ -95,18 +96,20 @@ public HaltonSequenceGenerator(final int dimension) {
* The length of the bases array defines the space dimension and is required to be > 0.
*
* @param dimension the space dimension
* @param bases the base number for each dimension, entries should be (pairwise) prime, may not be null
* @param bases the base number for each dimension, entries should be (pairwise) prime and at least 2,
* may not be null
* @param weights the weights used during scrambling, may be null in which case no scrambling will be performed
* @throws NullArgumentException if base is null
* @throws OutOfRangeException if the space dimension is outside the range [1, len], where
* len refers to the length of the bases array
* @throws DimensionMismatchException if weights is non-null and the length of the input arrays differ
* @throws NumberIsTooSmallException if any of the bases is smaller than 2
*/
public HaltonSequenceGenerator(final int dimension, final int[] bases, final int[] weights) {
NullArgumentException.check(bases);

if (dimension < 1 || dimension > bases.length) {
throw new OutOfRangeException(dimension, 1, PRIMES.length);
throw new OutOfRangeException(dimension, 1, bases.length);
}

if (weights != null && weights.length != bases.length) {
Expand All @@ -117,6 +120,12 @@ public HaltonSequenceGenerator(final int dimension, final int[] bases, final int
this.base = bases.clone();
this.weight = weights == null ? null : weights.clone();
count = 0;

for (final int b : base) {
if (b < 2) {
throw new NumberIsTooSmallException(b, 2, true);
}
}
}

/** {@inheritDoc} */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.apache.commons.math4.legacy.exception.DimensionMismatchException;
import org.apache.commons.math4.legacy.exception.NotPositiveException;
import org.apache.commons.math4.legacy.exception.NullArgumentException;
import org.apache.commons.math4.legacy.exception.NumberIsTooSmallException;
import org.apache.commons.math4.legacy.exception.OutOfRangeException;
import org.junit.Before;
import org.junit.Test;
Expand Down Expand Up @@ -117,6 +118,27 @@ public void testConstructor2() throws Exception{
} catch (DimensionMismatchException e) {
// expected
}

try {
new HaltonSequenceGenerator(3, new int[] { 2, 3 }, null);
Assert.fail("an exception should have been thrown");
} catch (OutOfRangeException e) {
Assert.assertEquals(2, e.getHi());
}
}

@Test
public void testConstructorInvalidBase() {
// A base of 1 never reduces the index (infinite loop), 0 divides by zero
// and a negative base yields points outside [0, 1].
for (int b : new int[] { 1, 0, -2 }) {
try {
new HaltonSequenceGenerator(2, new int[] { 2, b }, null);
Assert.fail("an exception should have been thrown for base " + b);
} catch (NumberIsTooSmallException e) {
// expected
}
}
}

@Test
Expand Down
Loading