This is an automated email from the ASF dual-hosted git repository. aherbert pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/commons-rng.git
commit 621b53d39f9b796e3986ffdea93ead04eb1e9377 Author: Alex Herbert <aherb...@apache.org> AuthorDate: Fri Jul 23 21:09:45 2021 +0100 Pass RNG as first argument to shape sampler constructors This is consistent with the usage in the distribution package. --- .../shape/TetrahedronSamplerBenchmark.java | 38 +++++++-------- .../sampling/shape/TriangleSamplerBenchmark.java | 55 +++++++++++----------- .../commons/rng/sampling/CompositeSamplers.java | 6 +-- .../commons/rng/sampling/shape/BoxSampler.java | 26 +++++----- .../commons/rng/sampling/shape/LineSampler.java | 32 ++++++------- .../rng/sampling/shape/TetrahedronSampler.java | 14 +++--- .../rng/sampling/shape/TriangleSampler.java | 26 +++++----- .../rng/sampling/shape/UnitBallSampler.java | 14 +++--- .../rng/sampling/CompositeSamplersTest.java | 34 ++++++------- .../commons/rng/sampling/shape/BoxSamplerTest.java | 24 +++++----- .../rng/sampling/shape/LineSamplerTest.java | 24 +++++----- .../rng/sampling/shape/TetrahedronSamplerTest.java | 32 ++++++------- .../rng/sampling/shape/TriangleSamplerTest.java | 31 ++++++------ .../rng/sampling/shape/UnitBallSamplerTest.java | 9 ++-- 14 files changed, 185 insertions(+), 180 deletions(-) diff --git a/commons-rng-examples/examples-jmh/src/main/java/org/apache/commons/rng/examples/jmh/sampling/shape/TetrahedronSamplerBenchmark.java b/commons-rng-examples/examples-jmh/src/main/java/org/apache/commons/rng/examples/jmh/sampling/shape/TetrahedronSamplerBenchmark.java index e02a949..68bc847 100644 --- a/commons-rng-examples/examples-jmh/src/main/java/org/apache/commons/rng/examples/jmh/sampling/shape/TetrahedronSamplerBenchmark.java +++ b/commons-rng-examples/examples-jmh/src/main/java/org/apache/commons/rng/examples/jmh/sampling/shape/TetrahedronSamplerBenchmark.java @@ -164,14 +164,14 @@ public class TetrahedronSamplerBenchmark { // CHECKSTYLE: resume JavadocVariableCheck /** + * @param rng the source of randomness * @param a The first vertex. * @param b The second vertex. * @param c The third vertex. * @param d The fourth vertex. - * @param rng Source of randomness. */ - ArrayTetrahedronSampler(double[] a, double[] b, double[] c, double[] d, - UniformRandomProvider rng) { + ArrayTetrahedronSampler(UniformRandomProvider rng, + double[] a, double[] b, double[] c, double[] d) { super(rng); this.a = a.clone(); this.b = b.clone(); @@ -207,14 +207,14 @@ public class TetrahedronSamplerBenchmark { // CHECKSTYLE: resume JavadocVariableCheck /** + * @param rng the source of randomness * @param a The first vertex. * @param b The second vertex. * @param c The third vertex. * @param d The fourth vertex. - * @param rng Source of randomness. */ - NonArrayTetrahedronSampler(double[] a, double[] b, double[] c, double[] d, - UniformRandomProvider rng) { + NonArrayTetrahedronSampler(UniformRandomProvider rng, + double[] a, double[] b, double[] c, double[] d) { super(rng); ax = a[0]; ay = a[1]; @@ -252,14 +252,14 @@ public class TetrahedronSamplerBenchmark { // CHECKSTYLE: resume JavadocVariableCheck /** + * @param rng the source of randomness * @param a The first vertex. * @param b The second vertex. * @param c The third vertex. * @param d The fourth vertex. - * @param rng Source of randomness. */ - ArrayInlineTetrahedronSampler(double[] a, double[] b, double[] c, double[] d, - UniformRandomProvider rng) { + ArrayInlineTetrahedronSampler(UniformRandomProvider rng, + double[] a, double[] b, double[] c, double[] d) { this.a = a.clone(); this.b = b.clone(); this.c = c.clone(); @@ -328,14 +328,14 @@ public class TetrahedronSamplerBenchmark { // CHECKSTYLE: resume JavadocVariableCheck /** + * @param rng the source of randomness * @param a The first vertex. * @param b The second vertex. * @param c The third vertex. * @param d The fourth vertex. - * @param rng Source of randomness. */ - NonArrayInlineTetrahedronSampler(double[] a, double[] b, double[] c, double[] d, - UniformRandomProvider rng) { + NonArrayInlineTetrahedronSampler(UniformRandomProvider rng, + double[] a, double[] b, double[] c, double[] d) { ax = a[0]; ay = a[1]; az = a[2]; @@ -436,7 +436,7 @@ public class TetrahedronSamplerBenchmark { final double[] b = s.sample(); final double[] c = s.sample(); final double[] d = s.sample(); - sampler = createSampler(a, b, c, d, rng); + sampler = createSampler(rng, a, b, c, d); } /** @@ -449,8 +449,8 @@ public class TetrahedronSamplerBenchmark { * @param rng the source of randomness * @return the sampler */ - private Sampler createSampler(double[] a, double[] b, double[] c, double[] d, - final UniformRandomProvider rng) { + private Sampler createSampler(final UniformRandomProvider rng, + double[] a, double[] b, double[] c, double[] d) { if (BASELINE.equals(type)) { return new Sampler() { @Override @@ -462,13 +462,13 @@ public class TetrahedronSamplerBenchmark { } }; } else if (ARRAY.equals(type)) { - return new ArrayTetrahedronSampler(a, b, c, d, rng); + return new ArrayTetrahedronSampler(rng, a, b, c, d); } else if (NON_ARRAY.equals(type)) { - return new NonArrayTetrahedronSampler(a, b, c, d, rng); + return new NonArrayTetrahedronSampler(rng, a, b, c, d); } else if (ARRAY_INLINE.equals(type)) { - return new ArrayInlineTetrahedronSampler(a, b, c, d, rng); + return new ArrayInlineTetrahedronSampler(rng, a, b, c, d); } else if (NON_ARRAY_INLINE.equals(type)) { - return new NonArrayInlineTetrahedronSampler(a, b, c, d, rng); + return new NonArrayInlineTetrahedronSampler(rng, a, b, c, d); } throw new IllegalStateException(UNKNOWN_SAMPLER + type); } diff --git a/commons-rng-examples/examples-jmh/src/main/java/org/apache/commons/rng/examples/jmh/sampling/shape/TriangleSamplerBenchmark.java b/commons-rng-examples/examples-jmh/src/main/java/org/apache/commons/rng/examples/jmh/sampling/shape/TriangleSamplerBenchmark.java index 073ca2c..6558a65 100644 --- a/commons-rng-examples/examples-jmh/src/main/java/org/apache/commons/rng/examples/jmh/sampling/shape/TriangleSamplerBenchmark.java +++ b/commons-rng-examples/examples-jmh/src/main/java/org/apache/commons/rng/examples/jmh/sampling/shape/TriangleSamplerBenchmark.java @@ -208,7 +208,7 @@ public class TriangleSamplerBenchmark { final double[] a = s.sample(); final double[] b = s.sample(); final double[] c = s.sample(); - sampler = createSampler(a, b, c, rng); + sampler = createSampler(rng, a, b, c); } /** @@ -221,13 +221,14 @@ public class TriangleSamplerBenchmark { /** * Creates the triangle sampler. * + * @param rng the source of randomness * @param a The first vertex. * @param b The second vertex. * @param c The third vertex. - * @param rng the source of randomness * @return the sampler */ - protected abstract Sampler createSampler(double[] a, double[] b, double[] c, UniformRandomProvider rng); + protected abstract Sampler createSampler(UniformRandomProvider rng, + double[] a, double[] b, double[] c); } /** @@ -247,8 +248,8 @@ public class TriangleSamplerBenchmark { /** {@inheritDoc} */ @Override - protected Sampler createSampler(final double[] a, final double[] b, final double[] c, - final UniformRandomProvider rng) { + protected Sampler createSampler(final UniformRandomProvider rng, + final double[] a, final double[] b, final double[] c) { if (BASELINE.equals(type)) { return new Sampler() { @Override @@ -271,9 +272,9 @@ public class TriangleSamplerBenchmark { } }; } else if (VECTORS.equals(type)) { - return new VectorTriangleSampler2D(a, b, c, rng); + return new VectorTriangleSampler2D(rng, a, b, c); } else if (COORDINATES.equals(type)) { - return new CoordinateTriangleSampler2D(a, b, c, rng); + return new CoordinateTriangleSampler2D(rng, a, b, c); } throw new IllegalStateException(UNKNOWN_SAMPLER + type); } @@ -292,12 +293,12 @@ public class TriangleSamplerBenchmark { // CHECKSTYLE: resume JavadocVariableCheck /** + * @param rng the source of randomness * @param a The first vertex. * @param b The second vertex. * @param c The third vertex. - * @param rng the source of randomness */ - VectorTriangleSampler2D(double[] a, double[] b, double[] c, UniformRandomProvider rng) { + VectorTriangleSampler2D(UniformRandomProvider rng, double[] a, double[] b, double[] c) { super(rng); ax = a[0]; ay = a[1]; @@ -328,12 +329,12 @@ public class TriangleSamplerBenchmark { // CHECKSTYLE: resume JavadocVariableCheck /** + * @param rng the source of randomness * @param a The first vertex. * @param b The second vertex. * @param c The third vertex. - * @param rng the source of randomness */ - CoordinateTriangleSampler2D(double[] a, double[] b, double[] c, UniformRandomProvider rng) { + CoordinateTriangleSampler2D(UniformRandomProvider rng, double[] a, double[] b, double[] c) { super(rng); ax = a[0]; ay = a[1]; @@ -367,8 +368,8 @@ public class TriangleSamplerBenchmark { /** {@inheritDoc} */ @Override - protected Sampler createSampler(final double[] a, final double[] b, final double[] c, - final UniformRandomProvider rng) { + protected Sampler createSampler(final UniformRandomProvider rng, + final double[] a, final double[] b, final double[] c) { if (BASELINE.equals(type)) { return new Sampler() { @Override @@ -391,9 +392,9 @@ public class TriangleSamplerBenchmark { } }; } else if (VECTORS.equals(type)) { - return new VectorTriangleSampler3D(a, b, c, rng); + return new VectorTriangleSampler3D(rng, a, b, c); } else if (COORDINATES.equals(type)) { - return new CoordinateTriangleSampler3D(a, b, c, rng); + return new CoordinateTriangleSampler3D(rng, a, b, c); } throw new IllegalStateException(UNKNOWN_SAMPLER + type); } @@ -415,12 +416,12 @@ public class TriangleSamplerBenchmark { // CHECKSTYLE: resume JavadocVariableCheck /** + * @param rng the source of randomness * @param a The first vertex. * @param b The second vertex. * @param c The third vertex. - * @param rng the source of randomness */ - VectorTriangleSampler3D(double[] a, double[] b, double[] c, UniformRandomProvider rng) { + VectorTriangleSampler3D(UniformRandomProvider rng, double[] a, double[] b, double[] c) { super(rng); ax = a[0]; ay = a[1]; @@ -458,12 +459,12 @@ public class TriangleSamplerBenchmark { // CHECKSTYLE: resume JavadocVariableCheck /** + * @param rng the source of randomness * @param a The first vertex. * @param b The second vertex. * @param c The third vertex. - * @param rng the source of randomness */ - CoordinateTriangleSampler3D(double[] a, double[] b, double[] c, UniformRandomProvider rng) { + CoordinateTriangleSampler3D(UniformRandomProvider rng, double[] a, double[] b, double[] c) { super(rng); ax = a[0]; ay = a[1]; @@ -505,8 +506,8 @@ public class TriangleSamplerBenchmark { /** {@inheritDoc} */ @Override - protected Sampler createSampler(final double[] a, final double[] b, final double[] c, - final UniformRandomProvider rng) { + protected Sampler createSampler(final UniformRandomProvider rng, + final double[] a, final double[] b, final double[] c) { if (BASELINE.equals(type)) { return new Sampler() { @Override @@ -546,9 +547,9 @@ public class TriangleSamplerBenchmark { } }; } else if (VECTORS.equals(type)) { - return new VectorTriangleSamplerND(a, b, c, rng); + return new VectorTriangleSamplerND(rng, a, b, c); } else if (COORDINATES.equals(type)) { - return new CoordinateTriangleSamplerND(a, b, c, rng); + return new CoordinateTriangleSamplerND(rng, a, b, c); } throw new IllegalStateException(UNKNOWN_SAMPLER + type); } @@ -564,12 +565,12 @@ public class TriangleSamplerBenchmark { // CHECKSTYLE: resume JavadocVariableCheck /** + * @param rng the source of randomness * @param a The first vertex. * @param b The second vertex. * @param c The third vertex. - * @param rng the source of randomness */ - VectorTriangleSamplerND(double[] a, double[] b, double[] c, UniformRandomProvider rng) { + VectorTriangleSamplerND(UniformRandomProvider rng, double[] a, double[] b, double[] c) { super(rng); this.a = a.clone(); v = new double[a.length]; @@ -601,12 +602,12 @@ public class TriangleSamplerBenchmark { // CHECKSTYLE: resume JavadocVariableCheck /** + * @param rng the source of randomness * @param a The first vertex. * @param b The second vertex. * @param c The third vertex. - * @param rng the source of randomness */ - CoordinateTriangleSamplerND(double[] a, double[] b, double[] c, UniformRandomProvider rng) { + CoordinateTriangleSamplerND(UniformRandomProvider rng, double[] a, double[] b, double[] c) { super(rng); this.a = a.clone(); this.b = b.clone(); diff --git a/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/CompositeSamplers.java b/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/CompositeSamplers.java index aaa91ac..7679aba 100644 --- a/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/CompositeSamplers.java +++ b/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/CompositeSamplers.java @@ -58,9 +58,9 @@ import org.apache.commons.rng.sampling.distribution.SharedStateLongSampler; * double[] c = {3.45, 2.34}; * ObjectSampler<double[]> sampler = * CompositeSamplers.<double[]>newObjectSamplerBuilder() - * .add(LineSampler.of(a, b, rng), Math.hypot(a[0] - b[0], a[1] - b[1])) - * .add(LineSampler.of(b, c, rng), Math.hypot(b[0] - c[0], b[1] - c[1])) - * .add(LineSampler.of(c, a, rng), Math.hypot(c[0] - a[0], c[1] - a[1])) + * .add(LineSampler.of(rng, a, b), Math.hypot(a[0] - b[0], a[1] - b[1])) + * .add(LineSampler.of(rng, b, c), Math.hypot(b[0] - c[0], b[1] - c[1])) + * .add(LineSampler.of(rng, c, a), Math.hypot(c[0] - a[0], c[1] - a[1])) * .build(rng); * </pre> * diff --git a/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/shape/BoxSampler.java b/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/shape/BoxSampler.java index f5e6a93..fd8b52b 100644 --- a/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/shape/BoxSampler.java +++ b/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/shape/BoxSampler.java @@ -61,11 +61,11 @@ public abstract class BoxSampler implements SharedStateObjectSampler<double[]> { private final double by; /** + * @param rng Source of randomness. * @param a Bound a. * @param b Bound b. - * @param rng Source of randomness. */ - BoxSampler2D(double[] a, double[] b, UniformRandomProvider rng) { + BoxSampler2D(UniformRandomProvider rng, double[] a, double[] b) { super(rng); ax = a[0]; ay = a[1]; @@ -116,11 +116,11 @@ public abstract class BoxSampler implements SharedStateObjectSampler<double[]> { private final double bz; /** + * @param rng Source of randomness. * @param a Bound a. * @param b Bound b. - * @param rng Source of randomness. */ - BoxSampler3D(double[] a, double[] b, UniformRandomProvider rng) { + BoxSampler3D(UniformRandomProvider rng, double[] a, double[] b) { super(rng); ax = a[0]; ay = a[1]; @@ -167,11 +167,11 @@ public abstract class BoxSampler implements SharedStateObjectSampler<double[]> { private final double[] b; /** + * @param rng Source of randomness. * @param a Bound a. * @param b Bound b. - * @param rng Source of randomness. */ - BoxSamplerND(double[] a, double[] b, UniformRandomProvider rng) { + BoxSamplerND(UniformRandomProvider rng, double[] a, double[] b) { super(rng); // Defensive copy this.a = a.clone(); @@ -244,16 +244,16 @@ public abstract class BoxSampler implements SharedStateObjectSampler<double[]> { * <p>Note: There is no requirement that {@code a <= b}. The samples will be uniformly * distributed in the range {@code a} to {@code b} for each dimension. * + * @param rng Source of randomness. * @param a Bound a. * @param b Bound b. - * @param rng Source of randomness. * @return the sampler * @throws IllegalArgumentException If the bounds do not have the same * dimension; the dimension is less than 2; or bounds have non-finite coordinates. */ - public static BoxSampler of(double[] a, - double[] b, - UniformRandomProvider rng) { + public static BoxSampler of(UniformRandomProvider rng, + double[] a, + double[] b) { final int dimension = a.length; if (dimension != b.length) { throw new IllegalArgumentException( @@ -265,11 +265,11 @@ public abstract class BoxSampler implements SharedStateObjectSampler<double[]> { Coordinates.requireFinite(b, "Bound b"); // Low dimension specialisations if (dimension == TWO_D) { - return new BoxSampler2D(a, b, rng); + return new BoxSampler2D(rng, a, b); } else if (dimension == THREE_D) { - return new BoxSampler3D(a, b, rng); + return new BoxSampler3D(rng, a, b); } else if (dimension > THREE_D) { - return new BoxSamplerND(a, b, rng); + return new BoxSamplerND(rng, a, b); } // Less than 2D throw new IllegalArgumentException("Unsupported dimension: " + dimension); diff --git a/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/shape/LineSampler.java b/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/shape/LineSampler.java index ccf3e29..37975e4 100644 --- a/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/shape/LineSampler.java +++ b/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/shape/LineSampler.java @@ -62,11 +62,11 @@ public abstract class LineSampler implements SharedStateObjectSampler<double[]> private final double bx; /** + * @param rng Source of randomness. * @param a The first vertex. * @param b The second vertex. - * @param rng Source of randomness. */ - LineSampler1D(double[] a, double[] b, UniformRandomProvider rng) { + LineSampler1D(UniformRandomProvider rng, double[] a, double[] b) { super(rng); ax = a[0]; bx = b[0]; @@ -108,11 +108,11 @@ public abstract class LineSampler implements SharedStateObjectSampler<double[]> private final double by; /** + * @param rng Source of randomness. * @param a The first vertex. * @param b The second vertex. - * @param rng Source of randomness. */ - LineSampler2D(double[] a, double[] b, UniformRandomProvider rng) { + LineSampler2D(UniformRandomProvider rng, double[] a, double[] b) { super(rng); ax = a[0]; ay = a[1]; @@ -163,11 +163,11 @@ public abstract class LineSampler implements SharedStateObjectSampler<double[]> private final double bz; /** + * @param rng Source of randomness. * @param a The first vertex. * @param b The second vertex. - * @param rng Source of randomness. */ - LineSampler3D(double[] a, double[] b, UniformRandomProvider rng) { + LineSampler3D(UniformRandomProvider rng, double[] a, double[] b) { super(rng); ax = a[0]; ay = a[1]; @@ -214,11 +214,11 @@ public abstract class LineSampler implements SharedStateObjectSampler<double[]> private final double[] b; /** + * @param rng Source of randomness. * @param a The first vertex. * @param b The second vertex. - * @param rng Source of randomness. */ - LineSamplerND(double[] a, double[] b, UniformRandomProvider rng) { + LineSamplerND(UniformRandomProvider rng, double[] a, double[] b) { super(rng); // Defensive copy this.a = a.clone(); @@ -292,16 +292,16 @@ public abstract class LineSampler implements SharedStateObjectSampler<double[]> * * <p>Sampling is supported in dimensions of 1 or above. * + * @param rng Source of randomness. * @param a The first vertex. * @param b The second vertex. - * @param rng Source of randomness. * @return the sampler * @throws IllegalArgumentException If the vertices do not have the same * dimension; the dimension is less than 1; or vertices have non-finite coordinates. */ - public static LineSampler of(double[] a, - double[] b, - UniformRandomProvider rng) { + public static LineSampler of(UniformRandomProvider rng, + double[] a, + double[] b) { final int dimension = a.length; if (dimension != b.length) { throw new IllegalArgumentException( @@ -313,15 +313,15 @@ public abstract class LineSampler implements SharedStateObjectSampler<double[]> Coordinates.requireFinite(b, "Vertex b"); // Low dimension specialisations if (dimension == TWO_D) { - return new LineSampler2D(a, b, rng); + return new LineSampler2D(rng, a, b); } else if (dimension == THREE_D) { - return new LineSampler3D(a, b, rng); + return new LineSampler3D(rng, a, b); } else if (dimension > THREE_D) { - return new LineSamplerND(a, b, rng); + return new LineSamplerND(rng, a, b); } else if (dimension == ONE_D) { // Unlikely case of 1D is placed last. // Use o.a.c.rng.sampling.distribution.ContinuousUniformSampler for non-array samples. - return new LineSampler1D(a, b, rng); + return new LineSampler1D(rng, a, b); } // Less than 1D throw new IllegalArgumentException("Unsupported dimension: " + dimension); diff --git a/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/shape/TetrahedronSampler.java b/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/shape/TetrahedronSampler.java index 1ac5724..d7c281e 100644 --- a/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/shape/TetrahedronSampler.java +++ b/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/shape/TetrahedronSampler.java @@ -69,13 +69,13 @@ public class TetrahedronSampler implements SharedStateObjectSampler<double[]> { private final UniformRandomProvider rng; /** + * @param rng Source of randomness. * @param a The first vertex. * @param b The second vertex. * @param c The third vertex. * @param d The fourth vertex. - * @param rng Source of randomness. */ - TetrahedronSampler(double[] a, double[] b, double[] c, double[] d, UniformRandomProvider rng) { + TetrahedronSampler(UniformRandomProvider rng, double[] a, double[] b, double[] c, double[] d) { // Defensive copy this.a = a.clone(); this.b = b.clone(); @@ -175,20 +175,20 @@ public class TetrahedronSampler implements SharedStateObjectSampler<double[]> { * <p>No test for a volume is performed. If the vertices are coplanar the sampling * distribution is undefined. * + * @param rng Source of randomness. * @param a The first vertex. * @param b The second vertex. * @param c The third vertex. * @param d The fourth vertex. - * @param rng Source of randomness. * @return the sampler * @throws IllegalArgumentException If the vertices do not have length 3; * or vertices have non-finite coordinates */ - public static TetrahedronSampler of(double[] a, + public static TetrahedronSampler of(UniformRandomProvider rng, + double[] a, double[] b, double[] c, - double[] d, - UniformRandomProvider rng) { + double[] d) { // Must be 3D Coordinates.requireLength(a, THREE_D, VERTEX_A); Coordinates.requireLength(b, THREE_D, VERTEX_B); @@ -199,6 +199,6 @@ public class TetrahedronSampler implements SharedStateObjectSampler<double[]> { Coordinates.requireFinite(b, VERTEX_B); Coordinates.requireFinite(c, VERTEX_C); Coordinates.requireFinite(d, VERTEX_D); - return new TetrahedronSampler(a, b, c, d, rng); + return new TetrahedronSampler(rng, a, b, c, d); } } diff --git a/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/shape/TriangleSampler.java b/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/shape/TriangleSampler.java index 5961c4c..662874c 100644 --- a/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/shape/TriangleSampler.java +++ b/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/shape/TriangleSampler.java @@ -97,12 +97,12 @@ public abstract class TriangleSampler implements SharedStateObjectSampler<double private final double cy; /** + * @param rng Source of randomness. * @param a The first vertex. * @param b The second vertex. * @param c The third vertex. - * @param rng Source of randomness. */ - TriangleSampler2D(double[] a, double[] b, double[] c, UniformRandomProvider rng) { + TriangleSampler2D(UniformRandomProvider rng, double[] a, double[] b, double[] c) { super(rng); ax = a[0]; ay = a[1]; @@ -163,12 +163,12 @@ public abstract class TriangleSampler implements SharedStateObjectSampler<double private final double cz; /** + * @param rng Source of randomness. * @param a The first vertex. * @param b The second vertex. * @param c The third vertex. - * @param rng Source of randomness. */ - TriangleSampler3D(double[] a, double[] b, double[] c, UniformRandomProvider rng) { + TriangleSampler3D(UniformRandomProvider rng, double[] a, double[] b, double[] c) { super(rng); ax = a[0]; ay = a[1]; @@ -223,12 +223,12 @@ public abstract class TriangleSampler implements SharedStateObjectSampler<double private final double[] c; /** + * @param rng Source of randomness. * @param a The first vertex. * @param b The second vertex. * @param c The third vertex. - * @param rng Source of randomness. */ - TriangleSamplerND(double[] a, double[] b, double[] c, UniformRandomProvider rng) { + TriangleSamplerND(UniformRandomProvider rng, double[] a, double[] b, double[] c) { super(rng); // Defensive copy this.a = a.clone(); @@ -322,18 +322,18 @@ public abstract class TriangleSampler implements SharedStateObjectSampler<double * <p>No test for collinear points is performed. If the points are collinear the sampling * distribution is undefined. * + * @param rng Source of randomness. * @param a The first vertex. * @param b The second vertex. * @param c The third vertex. - * @param rng Source of randomness. * @return the sampler * @throws IllegalArgumentException If the vertices do not have the same * dimension; the dimension is less than 2; or vertices have non-finite coordinates */ - public static TriangleSampler of(double[] a, + public static TriangleSampler of(UniformRandomProvider rng, + double[] a, double[] b, - double[] c, - UniformRandomProvider rng) { + double[] c) { final int dimension = a.length; if (dimension != b.length || dimension != c.length) { throw new IllegalArgumentException( @@ -347,11 +347,11 @@ public abstract class TriangleSampler implements SharedStateObjectSampler<double Coordinates.requireFinite(c, "Vertex c"); // Low dimension specialisations if (dimension == TWO_D) { - return new TriangleSampler2D(a, b, c, rng); + return new TriangleSampler2D(rng, a, b, c); } else if (dimension == THREE_D) { - return new TriangleSampler3D(a, b, c, rng); + return new TriangleSampler3D(rng, a, b, c); } else if (dimension > THREE_D) { - return new TriangleSamplerND(a, b, c, rng); + return new TriangleSamplerND(rng, a, b, c); } // Less than 2D throw new IllegalArgumentException( diff --git a/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/shape/UnitBallSampler.java b/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/shape/UnitBallSampler.java index 8e78c0f..b84b9cc 100644 --- a/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/shape/UnitBallSampler.java +++ b/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/shape/UnitBallSampler.java @@ -163,10 +163,10 @@ public abstract class UnitBallSampler implements SharedStateObjectSampler<double private final ContinuousSampler exp; /** - * @param dimension Space dimension. * @param rng Source of randomness. + * @param dimension Space dimension. */ - UnitBallSamplerND(int dimension, UniformRandomProvider rng) { + UnitBallSamplerND(UniformRandomProvider rng, int dimension) { this.dimension = dimension; normal = ZigguratSampler.NormalizedGaussian.of(rng); // Require an Exponential(mean=2). @@ -197,7 +197,7 @@ public abstract class UnitBallSampler implements SharedStateObjectSampler<double @Override public UnitBallSampler withUniformRandomProvider(UniformRandomProvider rng) { - return new UnitBallSamplerND(dimension, rng); + return new UnitBallSamplerND(rng, dimension); } } @@ -218,13 +218,13 @@ public abstract class UnitBallSampler implements SharedStateObjectSampler<double * * <p>Sampling is supported in dimensions of 1 or above. * - * @param dimension Space dimension. * @param rng Source of randomness. + * @param dimension Space dimension. * @return the sampler * @throws IllegalArgumentException If {@code dimension <= 0} */ - public static UnitBallSampler of(int dimension, - UniformRandomProvider rng) { + public static UnitBallSampler of(UniformRandomProvider rng, + int dimension) { if (dimension <= 0) { throw new IllegalArgumentException("Dimension must be strictly positive"); } else if (dimension == ONE_D) { @@ -234,7 +234,7 @@ public abstract class UnitBallSampler implements SharedStateObjectSampler<double } else if (dimension == THREE_D) { return new UnitBallSampler3D(rng); } - return new UnitBallSamplerND(dimension, rng); + return new UnitBallSamplerND(rng, dimension); } /** diff --git a/commons-rng-sampling/src/test/java/org/apache/commons/rng/sampling/CompositeSamplersTest.java b/commons-rng-sampling/src/test/java/org/apache/commons/rng/sampling/CompositeSamplersTest.java index 01dfa85..e5f06c4 100644 --- a/commons-rng-sampling/src/test/java/org/apache/commons/rng/sampling/CompositeSamplersTest.java +++ b/commons-rng-sampling/src/test/java/org/apache/commons/rng/sampling/CompositeSamplersTest.java @@ -423,7 +423,7 @@ public class CompositeSamplersTest { // Sample within the ranges between the ticks final int before = builder.size(); for (int i = 1; i < ticks.length; i++) { - final IntRangeSampler sampler = new IntRangeSampler(ticks[i - 1], ticks[i], rng); + final IntRangeSampler sampler = new IntRangeSampler(rng, ticks[i - 1], ticks[i]); // Weight using the range builder.add(sampler, sampler.range); } @@ -503,7 +503,7 @@ public class CompositeSamplersTest { // For double values it is extremely unlikely the same value will be generated. // An assertion is performed to ensure we create the correct number of samplers. - DoubleRangeSampler sampler = new DoubleRangeSampler(min, max, rng); + DoubleRangeSampler sampler = new DoubleRangeSampler(rng, min, max); final double[] ticks = new double[n + 1]; ticks[0] = min; ticks[1] = max; @@ -516,7 +516,7 @@ public class CompositeSamplersTest { // Sample within the ranges between the ticks final int before = builder.size(); for (int i = 1; i < ticks.length; i++) { - sampler = new DoubleRangeSampler(ticks[i - 1], ticks[i], rng); + sampler = new DoubleRangeSampler(rng, ticks[i - 1], ticks[i]); // Weight using the range builder.add(sampler, sampler.range()); } @@ -599,7 +599,7 @@ public class CompositeSamplersTest { // For long values it is extremely unlikely the same value will be generated. // An assertion is performed to ensure we create the correct number of samplers. - LongRangeSampler sampler = new LongRangeSampler(min, max, rng); + LongRangeSampler sampler = new LongRangeSampler(rng, min, max); final long[] ticks = new long[n + 1]; ticks[0] = min; ticks[1] = max; @@ -613,7 +613,7 @@ public class CompositeSamplersTest { // Sample within the ranges between the ticks final int before = builder.size(); for (int i = 1; i < ticks.length; i++) { - sampler = new LongRangeSampler(ticks[i - 1], ticks[i], rng); + sampler = new LongRangeSampler(rng, ticks[i - 1], ticks[i]); // Weight using the range builder.add(sampler, sampler.range); } @@ -731,7 +731,7 @@ public class CompositeSamplersTest { // Sample within the ranges between the ticks final int[] ticks = {-3, 5, 14, 22}; for (int i = 1; i < ticks.length; i++) { - final IntRangeSampler sampler = new IntRangeSampler(ticks[i - 1], ticks[i], rng1); + final IntRangeSampler sampler = new IntRangeSampler(rng1, ticks[i - 1], ticks[i]); // Weight using the range builder.add(sampler, sampler.range); } @@ -780,7 +780,7 @@ public class CompositeSamplersTest { // Sample within the ranges between the ticks final double[] ticks = {7.89, 13.99, 21.7, 35.6, 45.5}; for (int i = 1; i < ticks.length; i++) { - final DoubleRangeSampler sampler = new DoubleRangeSampler(ticks[i - 1], ticks[i], rng1); + final DoubleRangeSampler sampler = new DoubleRangeSampler(rng1, ticks[i - 1], ticks[i]); // Weight using the range builder.add(sampler, sampler.range()); } @@ -851,7 +851,7 @@ public class CompositeSamplersTest { // Sample within the ranges between the ticks final long[] ticks = {-32634628368L, 516234712, 1472839427384234L, 72364572187368423L}; for (int i = 1; i < ticks.length; i++) { - final LongRangeSampler sampler = new LongRangeSampler(ticks[i - 1], ticks[i], rng1); + final LongRangeSampler sampler = new LongRangeSampler(rng1, ticks[i - 1], ticks[i]); // Weight using the range builder.add(sampler, sampler.range); } @@ -900,11 +900,11 @@ public class CompositeSamplersTest { private final UniformRandomProvider rng; /** + * @param rng the source of randomness * @param min the minimum (inclusive) * @param max the maximum (exclusive) - * @param rng the source of randomness */ - IntRangeSampler(int min, int max, UniformRandomProvider rng) { + IntRangeSampler(UniformRandomProvider rng, int min, int max) { this.min = min; this.range = max - min; this.rng = rng; @@ -917,7 +917,7 @@ public class CompositeSamplersTest { @Override public SharedStateDiscreteSampler withUniformRandomProvider(UniformRandomProvider generator) { - return new IntRangeSampler(min, min + range, generator); + return new IntRangeSampler(generator, min, min + range); } } @@ -930,11 +930,11 @@ public class CompositeSamplersTest { private final UniformRandomProvider rng; /** + * @param rng the source of randomness * @param a bound a * @param b bound b - * @param rng the source of randomness */ - DoubleRangeSampler(double a, double b, UniformRandomProvider rng) { + DoubleRangeSampler(UniformRandomProvider rng, double a, double b) { this.a = a; this.b = b; this.rng = rng; @@ -958,7 +958,7 @@ public class CompositeSamplersTest { @Override public SharedStateContinuousSampler withUniformRandomProvider(UniformRandomProvider generator) { - return new DoubleRangeSampler(a, b, generator); + return new DoubleRangeSampler(generator, a, b); } } @@ -971,11 +971,11 @@ public class CompositeSamplersTest { private final UniformRandomProvider rng; /** + * @param rng the source of randomness * @param min the minimum (inclusive) * @param max the maximum (exclusive) - * @param rng the source of randomness */ - LongRangeSampler(long min, long max, UniformRandomProvider rng) { + LongRangeSampler(UniformRandomProvider rng, long min, long max) { this.min = min; this.range = max - min; this.rng = rng; @@ -988,7 +988,7 @@ public class CompositeSamplersTest { @Override public SharedStateLongSampler withUniformRandomProvider(UniformRandomProvider generator) { - return new LongRangeSampler(min, min + range, generator); + return new LongRangeSampler(generator, min, min + range); } } } diff --git a/commons-rng-sampling/src/test/java/org/apache/commons/rng/sampling/shape/BoxSamplerTest.java b/commons-rng-sampling/src/test/java/org/apache/commons/rng/sampling/shape/BoxSamplerTest.java index 446ffe4..59ce874 100644 --- a/commons-rng-sampling/src/test/java/org/apache/commons/rng/sampling/shape/BoxSamplerTest.java +++ b/commons-rng-sampling/src/test/java/org/apache/commons/rng/sampling/shape/BoxSamplerTest.java @@ -38,7 +38,7 @@ public class BoxSamplerTest { @Test(expected = IllegalArgumentException.class) public void testInvalidDimensionThrows() { final UniformRandomProvider rng = RandomSource.SPLIT_MIX_64.create(0L); - BoxSampler.of(new double[1], new double[1], rng); + BoxSampler.of(rng, new double[1], new double[1]); } /** @@ -54,7 +54,7 @@ public class BoxSamplerTest { {c3, c2}, }) { try { - BoxSampler.of(c[0], c[1], rng); + BoxSampler.of(rng, c[0], c[1]); Assert.fail(String.format("Did not detect dimension mismatch: %d,%d", c[0].length, c[1].length)); } catch (IllegalArgumentException ex) { @@ -73,7 +73,7 @@ public class BoxSamplerTest { final double[][] c = new double[][] { {0, 1, 2}, {-1, 2, 3} }; - Assert.assertNotNull(BoxSampler.of(c[0], c[1], rng)); + Assert.assertNotNull(BoxSampler.of(rng, c[0], c[1])); final double[] bad = {Double.POSITIVE_INFINITY, Double.NEGATIVE_INFINITY, Double.NaN}; for (int i = 0; i < c.length; i++) { for (int j = 0; j < c[0].length; j++) { @@ -81,7 +81,7 @@ public class BoxSamplerTest { final double value = c[i][j]; c[i][j] = d; try { - BoxSampler.of(c[0], c[1], rng); + BoxSampler.of(rng, c[0], c[1]); Assert.fail(String.format("Did not detect non-finite coordinate: %d,%d = %s", i, j, d)); } catch (IllegalArgumentException ex) { // Expected @@ -145,10 +145,10 @@ public class BoxSamplerTest { Assert.assertEquals("Expect vector b - a to be infinite in the x dimension", Double.POSITIVE_INFINITY, c2[1][0] - c2[0][0], 0.0); - final BoxSampler sampler1 = BoxSampler.of(c1[0], c1[1], - RandomSource.XO_RO_SHI_RO_128_PP.create(seed)); - final BoxSampler sampler2 = BoxSampler.of(c2[0], c2[1], - RandomSource.XO_RO_SHI_RO_128_PP.create(seed)); + final BoxSampler sampler1 = BoxSampler.of( + RandomSource.XO_RO_SHI_RO_128_PP.create(seed), c1[0], c1[1]); + final BoxSampler sampler2 = BoxSampler.of( + RandomSource.XO_RO_SHI_RO_128_PP.create(seed), c2[0], c2[1]); for (int n = 0; n < 10; n++) { final double[] a = sampler1.sample(); @@ -220,7 +220,7 @@ public class BoxSamplerTest { Arrays.fill(expected, 1.0); // Increase the loops and use a null seed (i.e. randomly generated) to verify robustness - final BoxSampler sampler = BoxSampler.of(a, b, rng); + final BoxSampler sampler = BoxSampler.of(rng, a, b); final int samples = expected.length * samplesPerBin; for (int n = 0; n < 1; n++) { // Assign each coordinate to a region inside the box @@ -277,7 +277,7 @@ public class BoxSamplerTest { final UniformRandomProvider rng2 = RandomSource.SPLIT_MIX_64.create(0L); final double[] c1 = createCoordinate(1, dimension); final double[] c2 = createCoordinate(2, dimension); - final BoxSampler sampler1 = BoxSampler.of(c1, c2, rng1); + final BoxSampler sampler1 = BoxSampler.of(rng1, c1, c2); final BoxSampler sampler2 = sampler1.withUniformRandomProvider(rng2); RandomAssert.assertProduceSameSequence(sampler1, sampler2); } @@ -317,7 +317,7 @@ public class BoxSamplerTest { final UniformRandomProvider rng2 = RandomSource.SPLIT_MIX_64.create(0L); final double[] c1 = createCoordinate(1, dimension); final double[] c2 = createCoordinate(2, dimension); - final BoxSampler sampler1 = BoxSampler.of(c1, c2, rng1); + final BoxSampler sampler1 = BoxSampler.of(rng1, c1, c2); // Check the input vectors are copied and not used by reference. // Change them in place and create a new sampler. It should have different output // translated by the offset. @@ -326,7 +326,7 @@ public class BoxSamplerTest { c1[i] += offset; c2[i] += offset; } - final BoxSampler sampler2 = BoxSampler.of(c1, c2, rng2); + final BoxSampler sampler2 = BoxSampler.of(rng2, c1, c2); for (int n = 0; n < 3; n++) { final double[] s1 = sampler1.sample(); final double[] s2 = sampler2.sample(); diff --git a/commons-rng-sampling/src/test/java/org/apache/commons/rng/sampling/shape/LineSamplerTest.java b/commons-rng-sampling/src/test/java/org/apache/commons/rng/sampling/shape/LineSamplerTest.java index 046a186..32f9137 100644 --- a/commons-rng-sampling/src/test/java/org/apache/commons/rng/sampling/shape/LineSamplerTest.java +++ b/commons-rng-sampling/src/test/java/org/apache/commons/rng/sampling/shape/LineSamplerTest.java @@ -38,7 +38,7 @@ public class LineSamplerTest { @Test(expected = IllegalArgumentException.class) public void testInvalidDimensionThrows() { final UniformRandomProvider rng = RandomSource.SPLIT_MIX_64.create(0L); - LineSampler.of(new double[0], new double[0], rng); + LineSampler.of(rng, new double[0], new double[0]); } /** @@ -54,7 +54,7 @@ public class LineSamplerTest { {c3, c2}, }) { try { - LineSampler.of(c[0], c[1], rng); + LineSampler.of(rng, c[0], c[1]); Assert.fail(String.format("Did not detect dimension mismatch: %d,%d", c[0].length, c[1].length)); } catch (IllegalArgumentException ex) { @@ -73,7 +73,7 @@ public class LineSamplerTest { final double[][] c = new double[][] { {0, 1, 2}, {-1, 2, 3} }; - Assert.assertNotNull(LineSampler.of(c[0], c[1], rng)); + Assert.assertNotNull(LineSampler.of(rng, c[0], c[1])); final double[] bad = {Double.POSITIVE_INFINITY, Double.NEGATIVE_INFINITY, Double.NaN}; for (int i = 0; i < c.length; i++) { for (int j = 0; j < c[0].length; j++) { @@ -81,7 +81,7 @@ public class LineSamplerTest { final double value = c[i][j]; c[i][j] = d; try { - LineSampler.of(c[0], c[1], rng); + LineSampler.of(rng, c[0], c[1]); Assert.fail(String.format("Did not detect non-finite coordinate: %d,%d = %s", i, j, d)); } catch (IllegalArgumentException ex) { @@ -155,10 +155,10 @@ public class LineSamplerTest { Assert.assertEquals("Expect vector b - a to be infinite in the x dimension", Double.POSITIVE_INFINITY, c2[1][0] - c2[0][0], 0.0); - final LineSampler sampler1 = LineSampler.of(c1[0], c1[1], - RandomSource.XO_RO_SHI_RO_128_PP.create(seed)); - final LineSampler sampler2 = LineSampler.of(c2[0], c2[1], - RandomSource.XO_RO_SHI_RO_128_PP.create(seed)); + final LineSampler sampler1 = LineSampler.of( + RandomSource.XO_RO_SHI_RO_128_PP.create(seed), c1[0], c1[1]); + final LineSampler sampler2 = LineSampler.of( + RandomSource.XO_RO_SHI_RO_128_PP.create(seed), c2[0], c2[1]); for (int n = 0; n < 10; n++) { final double[] a = sampler1.sample(); @@ -243,7 +243,7 @@ public class LineSamplerTest { Arrays.fill(expected, 1.0 / bins); // Increase the loops and use a null seed (i.e. randomly generated) to verify robustness - final LineSampler sampler = LineSampler.of(a, b, rng); + final LineSampler sampler = LineSampler.of(rng, a, b); final int samples = expected.length * samplesPerBin; for (int n = 0; n < 1; n++) { // Assign each coordinate to a region inside the line @@ -304,7 +304,7 @@ public class LineSamplerTest { final UniformRandomProvider rng2 = RandomSource.SPLIT_MIX_64.create(0L); final double[] c1 = createCoordinate(1, dimension); final double[] c2 = createCoordinate(2, dimension); - final LineSampler sampler1 = LineSampler.of(c1, c2, rng1); + final LineSampler sampler1 = LineSampler.of(rng1, c1, c2); final LineSampler sampler2 = sampler1.withUniformRandomProvider(rng2); RandomAssert.assertProduceSameSequence(sampler1, sampler2); } @@ -352,7 +352,7 @@ public class LineSamplerTest { final UniformRandomProvider rng2 = RandomSource.SPLIT_MIX_64.create(0L); final double[] c1 = createCoordinate(1, dimension); final double[] c2 = createCoordinate(2, dimension); - final LineSampler sampler1 = LineSampler.of(c1, c2, rng1); + final LineSampler sampler1 = LineSampler.of(rng1, c1, c2); // Check the input vectors are copied and not used by reference. // Change them in place and create a new sampler. It should have different output // translated by the offset. @@ -361,7 +361,7 @@ public class LineSamplerTest { c1[i] += offset; c2[i] += offset; } - final LineSampler sampler2 = LineSampler.of(c1, c2, rng2); + final LineSampler sampler2 = LineSampler.of(rng2, c1, c2); for (int n = 0; n < 3; n++) { final double[] s1 = sampler1.sample(); final double[] s2 = sampler2.sample(); diff --git a/commons-rng-sampling/src/test/java/org/apache/commons/rng/sampling/shape/TetrahedronSamplerTest.java b/commons-rng-sampling/src/test/java/org/apache/commons/rng/sampling/shape/TetrahedronSamplerTest.java index b66171c..ab4dec2 100644 --- a/commons-rng-sampling/src/test/java/org/apache/commons/rng/sampling/shape/TetrahedronSamplerTest.java +++ b/commons-rng-sampling/src/test/java/org/apache/commons/rng/sampling/shape/TetrahedronSamplerTest.java @@ -43,7 +43,7 @@ public class TetrahedronSamplerTest { for (int i = 0; i < c.length; i++) { c[i] = bad; try { - TetrahedronSampler.of(c[0], c[1], c[2], c[3], rng); + TetrahedronSampler.of(rng, c[0], c[1], c[2], c[3]); Assert.fail(String.format("Did not detect invalid dimension for vertex: %d", i)); } catch (IllegalArgumentException ex) { // Expected @@ -62,7 +62,7 @@ public class TetrahedronSamplerTest { final double[][] c = new double[][] { {1, 1, 1}, {1, -1, 1}, {-1, 1, 1}, {1, 1, -1} }; - Assert.assertNotNull(TetrahedronSampler.of(c[0], c[1], c[2], c[3], rng)); + Assert.assertNotNull(TetrahedronSampler.of(rng, c[0], c[1], c[2], c[3])); final double[] bad = {Double.POSITIVE_INFINITY, Double.NEGATIVE_INFINITY, Double.NaN}; for (int i = 0; i < c.length; i++) { for (int j = 0; j < c[0].length; j++) { @@ -70,7 +70,7 @@ public class TetrahedronSamplerTest { final double value = c[i][j]; c[i][j] = d; try { - TetrahedronSampler.of(c[0], c[1], c[2], c[3], rng); + TetrahedronSampler.of(rng, c[0], c[1], c[2], c[3]); Assert.fail(String.format("Did not detect non-finite coordinate: %d,%d = %s", i, j, d)); } catch (IllegalArgumentException ex) { // Expected @@ -110,10 +110,10 @@ public class TetrahedronSamplerTest { Assert.assertEquals("Expect vector c - b to be infinite in the z dimension", Double.POSITIVE_INFINITY, c2[2][2] - c2[1][2], 0.0); - final TetrahedronSampler sampler1 = TetrahedronSampler.of(c1[0], c1[1], c1[2], c1[3], - RandomSource.XO_RO_SHI_RO_128_PP.create(seed)); - final TetrahedronSampler sampler2 = TetrahedronSampler.of(c2[0], c2[1], c2[2], c2[3], - RandomSource.XO_RO_SHI_RO_128_PP.create(seed)); + final TetrahedronSampler sampler1 = TetrahedronSampler.of( + RandomSource.XO_RO_SHI_RO_128_PP.create(seed), c1[0], c1[1], c1[2], c1[3]); + final TetrahedronSampler sampler2 = TetrahedronSampler.of( + RandomSource.XO_RO_SHI_RO_128_PP.create(seed), c2[0], c2[1], c2[2], c2[3]); for (int n = 0; n < 10; n++) { final double[] a = sampler1.sample(); @@ -174,12 +174,12 @@ public class TetrahedronSamplerTest { // the tetrahedra all share one of the main diagonals of the box (d-f). // See the cuts used for the marching tetrahedra algorithm: // https://en.wikipedia.org/wiki/Marching_tetrahedra - final TetrahedronSampler[] samplers = {TetrahedronSampler.of(d, f, b, c, rng), - TetrahedronSampler.of(d, f, c, g, rng), - TetrahedronSampler.of(d, f, g, h, rng), - TetrahedronSampler.of(d, f, h, e, rng), - TetrahedronSampler.of(d, f, e, a, rng), - TetrahedronSampler.of(d, f, a, b, rng)}; + final TetrahedronSampler[] samplers = {TetrahedronSampler.of(rng, d, f, b, c), + TetrahedronSampler.of(rng, d, f, c, g), + TetrahedronSampler.of(rng, d, f, g, h), + TetrahedronSampler.of(rng, d, f, h, e), + TetrahedronSampler.of(rng, d, f, e, a), + TetrahedronSampler.of(rng, d, f, a, b)}; // To determine the sample is inside the correct tetrahedron it is projected to the // 4 faces of the tetrahedron along the face normals. The distance should be negative // when the face normals are orientated outwards. @@ -257,7 +257,7 @@ public class TetrahedronSamplerTest { final double[] c2 = createCoordinate(2); final double[] c3 = createCoordinate(-3); final double[] c4 = createCoordinate(4); - final TetrahedronSampler sampler1 = TetrahedronSampler.of(c1, c2, c3, c4, rng1); + final TetrahedronSampler sampler1 = TetrahedronSampler.of(rng1, c1, c2, c3, c4); final TetrahedronSampler sampler2 = sampler1.withUniformRandomProvider(rng2); RandomAssert.assertProduceSameSequence(sampler1, sampler2); } @@ -273,7 +273,7 @@ public class TetrahedronSamplerTest { final double[] c2 = createCoordinate(2); final double[] c3 = createCoordinate(-3); final double[] c4 = createCoordinate(-4); - final TetrahedronSampler sampler1 = TetrahedronSampler.of(c1, c2, c3, c4, rng1); + final TetrahedronSampler sampler1 = TetrahedronSampler.of(rng1, c1, c2, c3, c4); // Check the input vectors are copied and not used by reference. // Change them in place and create a new sampler. It should have different output // translated by the offset. @@ -284,7 +284,7 @@ public class TetrahedronSamplerTest { c3[i] += offset; c4[i] += offset; } - final TetrahedronSampler sampler2 = TetrahedronSampler.of(c1, c2, c3, c4, rng2); + final TetrahedronSampler sampler2 = TetrahedronSampler.of(rng2, c1, c2, c3, c4); for (int n = 0; n < 5; n++) { final double[] s1 = sampler1.sample(); final double[] s2 = sampler2.sample(); diff --git a/commons-rng-sampling/src/test/java/org/apache/commons/rng/sampling/shape/TriangleSamplerTest.java b/commons-rng-sampling/src/test/java/org/apache/commons/rng/sampling/shape/TriangleSamplerTest.java index 6d0dadd..fadb3c5 100644 --- a/commons-rng-sampling/src/test/java/org/apache/commons/rng/sampling/shape/TriangleSamplerTest.java +++ b/commons-rng-sampling/src/test/java/org/apache/commons/rng/sampling/shape/TriangleSamplerTest.java @@ -95,7 +95,8 @@ public class TriangleSamplerTest { */ @Test(expected = IllegalArgumentException.class) public void testInvalidDimensionThrows() { - TriangleSampler.of(new double[1], new double[1], new double[1], null); + final UniformRandomProvider rng = RandomSource.SPLIT_MIX_64.create(0L); + TriangleSampler.of(rng, new double[1], new double[1], new double[1]); } /** @@ -103,6 +104,7 @@ public class TriangleSamplerTest { */ @Test public void testDimensionMismatchThrows() { + final UniformRandomProvider rng = RandomSource.SPLIT_MIX_64.create(0L); final double[] c2 = new double[2]; final double[] c3 = new double[3]; for (double[][] c : new double[][][] { @@ -114,7 +116,7 @@ public class TriangleSamplerTest { {c3, c2, c3}, }) { try { - TriangleSampler.of(c[0], c[1], c[2], null); + TriangleSampler.of(rng, c[0], c[1], c[2]); Assert.fail(String.format("Did not detect dimension mismatch: %d,%d,%d", c[0].length, c[1].length, c[2].length)); } catch (IllegalArgumentException ex) { @@ -128,11 +130,12 @@ public class TriangleSamplerTest { */ @Test public void testNonFiniteVertexCoordinates() { + final UniformRandomProvider rng = RandomSource.SPLIT_MIX_64.create(0L); // A valid triangle final double[][] c = new double[][] { {0, 0, 1}, {2, 1, 0}, {-1, 2, 3} }; - Assert.assertNotNull(TriangleSampler.of(c[0], c[1], c[2], null)); + Assert.assertNotNull(TriangleSampler.of(rng, c[0], c[1], c[2])); final double[] bad = {Double.POSITIVE_INFINITY, Double.NEGATIVE_INFINITY, Double.NaN}; for (int i = 0; i < c.length; i++) { for (int j = 0; j < c[0].length; j++) { @@ -140,7 +143,7 @@ public class TriangleSamplerTest { final double value = c[i][j]; c[i][j] = d; try { - TriangleSampler.of(c[0], c[1], c[2], null); + TriangleSampler.of(rng, c[0], c[1], c[2]); Assert.fail(String.format("Did not detect non-finite coordinate: %d,%d = %s", i, j, d)); } catch (IllegalArgumentException ex) { // Expected @@ -214,10 +217,10 @@ public class TriangleSamplerTest { Assert.assertEquals("Expect vector c - b to be infinite in the y dimension", Double.NEGATIVE_INFINITY, c2[2][1] - c2[1][1], 0.0); - final TriangleSampler sampler1 = TriangleSampler.of(c1[0], c1[1], c1[2], - RandomSource.XO_RO_SHI_RO_128_PP.create(seed)); - final TriangleSampler sampler2 = TriangleSampler.of(c2[0], c2[1], c2[2], - RandomSource.XO_RO_SHI_RO_128_PP.create(seed)); + final TriangleSampler sampler1 = TriangleSampler.of( + RandomSource.XO_RO_SHI_RO_128_PP.create(seed), c1[0], c1[1], c1[2]); + final TriangleSampler sampler2 = TriangleSampler.of( + RandomSource.XO_RO_SHI_RO_128_PP.create(seed), c2[0], c2[1], c2[2]); for (int n = 0; n < 10; n++) { final double[] a = sampler1.sample(); @@ -313,9 +316,9 @@ public class TriangleSamplerTest { // Increase the loops and use a null seed (i.e. randomly generated) to verify robustness final UniformRandomProvider rng = RandomSource.XO_SHI_RO_512_PP.create(0xfabcab); - final TriangleSampler sampler1 = TriangleSampler.of(forward.apply(a), forward.apply(d), forward.apply(b), rng); - final TriangleSampler sampler2 = TriangleSampler.of(forward.apply(b), forward.apply(c), forward.apply(e), rng); - final TriangleSampler sampler3 = TriangleSampler.of(forward.apply(c), forward.apply(d), forward.apply(e), rng); + final TriangleSampler sampler1 = TriangleSampler.of(rng, forward.apply(a), forward.apply(d), forward.apply(b)); + final TriangleSampler sampler2 = TriangleSampler.of(rng, forward.apply(b), forward.apply(c), forward.apply(e)); + final TriangleSampler sampler3 = TriangleSampler.of(rng, forward.apply(c), forward.apply(d), forward.apply(e)); final Triangle triangle1 = new Triangle(a, d, b); final Triangle triangle2 = new Triangle(b, c, e); final Triangle triangle3 = new Triangle(c, d, e); @@ -395,7 +398,7 @@ public class TriangleSamplerTest { final double[] c1 = createCoordinate(1, dimension); final double[] c2 = createCoordinate(2, dimension); final double[] c3 = createCoordinate(-3, dimension); - final TriangleSampler sampler1 = TriangleSampler.of(c1, c2, c3, rng1); + final TriangleSampler sampler1 = TriangleSampler.of(rng1, c1, c2, c3); final TriangleSampler sampler2 = sampler1.withUniformRandomProvider(rng2); RandomAssert.assertProduceSameSequence(sampler1, sampler2); } @@ -436,7 +439,7 @@ public class TriangleSamplerTest { final double[] c1 = createCoordinate(1, dimension); final double[] c2 = createCoordinate(2, dimension); final double[] c3 = createCoordinate(-3, dimension); - final TriangleSampler sampler1 = TriangleSampler.of(c1, c2, c3, rng1); + final TriangleSampler sampler1 = TriangleSampler.of(rng1, c1, c2, c3); // Check the input vectors are copied and not used by reference. // Change them in place and create a new sampler. It should have different output // translated by the offset. @@ -446,7 +449,7 @@ public class TriangleSamplerTest { c2[i] += offset; c3[i] += offset; } - final TriangleSampler sampler2 = TriangleSampler.of(c1, c2, c3, rng2); + final TriangleSampler sampler2 = TriangleSampler.of(rng2, c1, c2, c3); for (int n = 0; n < 3; n++) { final double[] s1 = sampler1.sample(); final double[] s2 = sampler2.sample(); diff --git a/commons-rng-sampling/src/test/java/org/apache/commons/rng/sampling/shape/UnitBallSamplerTest.java b/commons-rng-sampling/src/test/java/org/apache/commons/rng/sampling/shape/UnitBallSamplerTest.java index d1f625a..5a030c8 100644 --- a/commons-rng-sampling/src/test/java/org/apache/commons/rng/sampling/shape/UnitBallSamplerTest.java +++ b/commons-rng-sampling/src/test/java/org/apache/commons/rng/sampling/shape/UnitBallSamplerTest.java @@ -37,7 +37,8 @@ public class UnitBallSamplerTest { */ @Test(expected = IllegalArgumentException.class) public void testInvalidDimensionThrows() { - UnitBallSampler.of(0, null); + final UniformRandomProvider rng = RandomSource.SPLIT_MIX_64.create(0L); + UnitBallSampler.of(rng, 0); } /** @@ -126,7 +127,7 @@ public class UnitBallSamplerTest { // Increase the loops and use a null seed (i.e. randomly generated) to verify robustness final UniformRandomProvider rng = RandomSource.XO_SHI_RO_512_PP.create(0xa1b2c3d4L); - final UnitBallSampler sampler = UnitBallSampler.of(dimension, rng); + final UnitBallSampler sampler = UnitBallSampler.of(rng, dimension); for (int loop = 0; loop < 1; loop++) { // Assign each coordinate to a layer inside the ball and an orthant using the sign final long[] observed = new long[layers * orthants]; @@ -182,7 +183,7 @@ public class UnitBallSamplerTest { } }; - final double[] vector = UnitBallSampler.of(dimension, bad).sample(); + final double[] vector = UnitBallSampler.of(bad, dimension).sample(); Assert.assertEquals(dimension, vector.length); // A non-zero coordinate should occur with a SplitMix which returns 0 only once. Assert.assertNotEquals(0.0, length(vector)); @@ -226,7 +227,7 @@ public class UnitBallSamplerTest { private static void testSharedStateSampler(int dimension) { final UniformRandomProvider rng1 = RandomSource.SPLIT_MIX_64.create(0L); final UniformRandomProvider rng2 = RandomSource.SPLIT_MIX_64.create(0L); - final UnitBallSampler sampler1 = UnitBallSampler.of(dimension, rng1); + final UnitBallSampler sampler1 = UnitBallSampler.of(rng1, dimension); final UnitBallSampler sampler2 = sampler1.withUniformRandomProvider(rng2); RandomAssert.assertProduceSameSequence(sampler1, sampler2); }