Skip to content

reject bases below 2 in HaltonSequenceGenerator - #355

Open
insaf021 wants to merge 1 commit into
apache:masterfrom
insaf021:halton-reject-small-bases
Open

insaf021 wants to merge 1 commit into
apache:masterfrom
insaf021:halton-reject-small-bases

Conversation

@insaf021

Copy link
Copy Markdown
Contributor

On master, with custom bases:

new HaltonSequenceGenerator(1, new int[] {1}, null).skipTo(1)    // never returns
new HaltonSequenceGenerator(1, new int[] {0}, null).skipTo(1)    // ArithmeticException: / by zero
new HaltonSequenceGenerator(1, new int[] {-2}, null).skipTo(1)   // [-0.5]
new HaltonSequenceGenerator(3, new int[] {2, 3}, null)           // "3 out of [1, 40] range"

The constructor clones bases without looking at the values, and get() decomposes the index with while (index > 0) { ...; index /= base[i]; }. A base of 1 never reduces the index, so every point after the first spins forever. A base of 0 divides by zero on the second point instead of failing at construction, and a negative base gives coordinates outside the documented [0, 1] interval. No base below 2 is meaningful for a Halton sequence, so the constructor now rejects it with NumberIsTooSmallException, after the existing argument checks.

The dimension check in the same constructor reported PRIMES.length as the upper bound, although the javadoc documents [1, len] with len the length of bases. It now reports bases.length.

HaltonSequenceGeneratorTest gains testConstructorInvalidBase and an upper-bound assertion in testConstructor2; both fail on master and pass with the patch. mvn clean install apache-rat:check checkstyle:check spotbugs:check javadoc:javadoc -pl commons-math-legacy -am is green on JDK 21 (3265 tests in legacy; the only reruns are the randomised SimplexOptimizerTest cases).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant