You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by ah...@apache.org on 2021/08/13 19:32:11 UTC
[commons-rng] 11/12: Use lambdas
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 d7be418f2e2e5ff4e7913152e965a6f52d7a0ff1
Author: Alex Herbert <ah...@apache.org>
AuthorDate: Fri Aug 13 20:20:20 2021 +0100
Use lambdas
---
.../jmh/sampling/UnitSphereSamplerBenchmark.java | 64 +++------
.../EnumeratedDistributionSamplersPerformance.java | 63 ++-------
.../PoissonSamplerCachePerformance.java | 21 +--
.../distribution/PoissonSamplersPerformance.java | 58 ++------
.../distribution/StableSamplerPerformance.java | 154 +++++++--------------
.../distribution/ZigguratSamplerPerformance.java | 32 ++---
.../shape/TetrahedronSamplerBenchmark.java | 13 +-
.../sampling/shape/TriangleSamplerBenchmark.java | 104 ++++++--------
.../sampling/shape/UnitBallSamplerBenchmark.java | 59 ++------
9 files changed, 155 insertions(+), 413 deletions(-)
diff --git a/commons-rng-examples/examples-jmh/src/main/java/org/apache/commons/rng/examples/jmh/sampling/UnitSphereSamplerBenchmark.java b/commons-rng-examples/examples-jmh/src/main/java/org/apache/commons/rng/examples/jmh/sampling/UnitSphereSamplerBenchmark.java
index ed93ca0..154d281 100644
--- a/commons-rng-examples/examples-jmh/src/main/java/org/apache/commons/rng/examples/jmh/sampling/UnitSphereSamplerBenchmark.java
+++ b/commons-rng-examples/examples-jmh/src/main/java/org/apache/commons/rng/examples/jmh/sampling/UnitSphereSamplerBenchmark.java
@@ -144,43 +144,28 @@ public class UnitSphereSamplerBenchmark {
@Override
protected Sampler createSampler(final UniformRandomProvider rng) {
if (BASELINE.equals(type)) {
- return new Sampler() {
- @Override
- public double[] sample() {
- return new double[] {1.0};
- }
+ return () -> {
+ return new double[] {1.0};
};
} else if (SIGNED_DOUBLE.equals(type)) {
- return new Sampler() {
- @Override
- public double[] sample() {
- // (1 - 0) or (1 - 2)
- // Use the sign bit
- return new double[] {1.0 - ((rng.nextInt() >>> 30) & 0x2)};
- }
+ return () -> {
+ // (1 - 0) or (1 - 2)
+ // Use the sign bit
+ return new double[] {1.0 - ((rng.nextInt() >>> 30) & 0x2)};
};
} else if (MASKED_INT.equals(type)) {
- return new Sampler() {
- @Override
- public double[] sample() {
- // Shift the sign bit and combine with the bits for a double of 1.0
- return new double[] {Double.longBitsToDouble(ONE | ((rng.nextInt() & SIGN_BIT) << 32))};
- }
+ return () -> {
+ // Shift the sign bit and combine with the bits for a double of 1.0
+ return new double[] {Double.longBitsToDouble(ONE | ((rng.nextInt() & SIGN_BIT) << 32))};
};
} else if (MASKED_LONG.equals(type)) {
- return new Sampler() {
- @Override
- public double[] sample() {
- // Combine the sign bit with the bits for a double of 1.0
- return new double[] {Double.longBitsToDouble(ONE | (rng.nextLong() & Long.MIN_VALUE))};
- }
+ return () -> {
+ // Combine the sign bit with the bits for a double of 1.0
+ return new double[] {Double.longBitsToDouble(ONE | (rng.nextLong() & Long.MIN_VALUE))};
};
} else if (BOOLEAN.equals(type)) {
- return new Sampler() {
- @Override
- public double[] sample() {
- return new double[] {rng.nextBoolean() ? -1.0 : 1.0};
- }
+ return () -> {
+ return new double[] {rng.nextBoolean() ? -1.0 : 1.0};
};
} else if (ARRAY.equals(type)) {
return new ArrayBasedUnitSphereSampler(1, rng);
@@ -202,11 +187,8 @@ public class UnitSphereSamplerBenchmark {
@Override
protected Sampler createSampler(final UniformRandomProvider rng) {
if (BASELINE.equals(type)) {
- return new Sampler() {
- @Override
- public double[] sample() {
- return new double[] {1.0, 0.0};
- }
+ return () -> {
+ return new double[] {1.0, 0.0};
};
} else if (ARRAY.equals(type)) {
return new ArrayBasedUnitSphereSampler(2, rng);
@@ -260,11 +242,8 @@ public class UnitSphereSamplerBenchmark {
@Override
protected Sampler createSampler(final UniformRandomProvider rng) {
if (BASELINE.equals(type)) {
- return new Sampler() {
- @Override
- public double[] sample() {
- return new double[] {1.0, 0.0, 0.0};
- }
+ return () -> {
+ return new double[] {1.0, 0.0, 0.0};
};
} else if (ARRAY.equals(type)) {
return new ArrayBasedUnitSphereSampler(3, rng);
@@ -319,11 +298,8 @@ public class UnitSphereSamplerBenchmark {
@Override
protected Sampler createSampler(final UniformRandomProvider rng) {
if (BASELINE.equals(type)) {
- return new Sampler() {
- @Override
- public double[] sample() {
- return new double[] {1.0, 0.0, 0.0, 0.0};
- }
+ return () -> {
+ return new double[] {1.0, 0.0, 0.0, 0.0};
};
} else if (ARRAY.equals(type)) {
return new ArrayBasedUnitSphereSampler(4, rng);
diff --git a/commons-rng-examples/examples-jmh/src/main/java/org/apache/commons/rng/examples/jmh/sampling/distribution/EnumeratedDistributionSamplersPerformance.java b/commons-rng-examples/examples-jmh/src/main/java/org/apache/commons/rng/examples/jmh/sampling/distribution/EnumeratedDistributionSamplersPerformance.java
index 3307048..2828f44 100644
--- a/commons-rng-examples/examples-jmh/src/main/java/org/apache/commons/rng/examples/jmh/sampling/distribution/EnumeratedDistributionSamplersPerformance.java
+++ b/commons-rng-examples/examples-jmh/src/main/java/org/apache/commons/rng/examples/jmh/sampling/distribution/EnumeratedDistributionSamplersPerformance.java
@@ -182,68 +182,23 @@ public class EnumeratedDistributionSamplersPerformance {
final double[] probabilities) {
// This would benefit from Java 8 lambda functions
if ("BinarySearchDiscreteSampler".equals(samplerType)) {
- factory = new DiscreteSamplerFactory() {
- @Override
- public DiscreteSampler create() {
- return new BinarySearchDiscreteSampler(rng, probabilities);
- }
- };
+ factory = () -> new BinarySearchDiscreteSampler(rng, probabilities);
} else if ("AliasMethodDiscreteSampler".equals(samplerType)) {
- factory = new DiscreteSamplerFactory() {
- @Override
- public DiscreteSampler create() {
- return AliasMethodDiscreteSampler.of(rng, probabilities);
- }
- };
+ factory = () -> AliasMethodDiscreteSampler.of(rng, probabilities);
} else if ("AliasMethodDiscreteSamplerNoPad".equals(samplerType)) {
- factory = new DiscreteSamplerFactory() {
- @Override
- public DiscreteSampler create() {
- return AliasMethodDiscreteSampler.of(rng, probabilities, -1);
- }
- };
+ factory = () -> AliasMethodDiscreteSampler.of(rng, probabilities, -1);
} else if ("AliasMethodDiscreteSamplerAlpha1".equals(samplerType)) {
- factory = new DiscreteSamplerFactory() {
- @Override
- public DiscreteSampler create() {
- return AliasMethodDiscreteSampler.of(rng, probabilities, 1);
- }
- };
+ factory = () -> AliasMethodDiscreteSampler.of(rng, probabilities, 1);
} else if ("AliasMethodDiscreteSamplerAlpha2".equals(samplerType)) {
- factory = new DiscreteSamplerFactory() {
- @Override
- public DiscreteSampler create() {
- return AliasMethodDiscreteSampler.of(rng, probabilities, 2);
- }
- };
+ factory = () -> AliasMethodDiscreteSampler.of(rng, probabilities, 2);
} else if ("GuideTableDiscreteSampler".equals(samplerType)) {
- factory = new DiscreteSamplerFactory() {
- @Override
- public DiscreteSampler create() {
- return GuideTableDiscreteSampler.of(rng, probabilities);
- }
- };
+ factory = () -> GuideTableDiscreteSampler.of(rng, probabilities);
} else if ("GuideTableDiscreteSamplerAlpha2".equals(samplerType)) {
- factory = new DiscreteSamplerFactory() {
- @Override
- public DiscreteSampler create() {
- return GuideTableDiscreteSampler.of(rng, probabilities, 2);
- }
- };
+ factory = () -> GuideTableDiscreteSampler.of(rng, probabilities, 2);
} else if ("GuideTableDiscreteSamplerAlpha8".equals(samplerType)) {
- factory = new DiscreteSamplerFactory() {
- @Override
- public DiscreteSampler create() {
- return GuideTableDiscreteSampler.of(rng, probabilities, 8);
- }
- };
+ factory = () -> GuideTableDiscreteSampler.of(rng, probabilities, 8);
} else if ("MarsagliaTsangWangDiscreteSampler".equals(samplerType)) {
- factory = new DiscreteSamplerFactory() {
- @Override
- public DiscreteSampler create() {
- return MarsagliaTsangWangDiscreteSampler.Enumerated.of(rng, probabilities);
- }
- };
+ factory = () -> MarsagliaTsangWangDiscreteSampler.Enumerated.of(rng, probabilities);
} else {
throw new IllegalStateException();
}
diff --git a/commons-rng-examples/examples-jmh/src/main/java/org/apache/commons/rng/examples/jmh/sampling/distribution/PoissonSamplerCachePerformance.java b/commons-rng-examples/examples-jmh/src/main/java/org/apache/commons/rng/examples/jmh/sampling/distribution/PoissonSamplerCachePerformance.java
index 6a80639..4495374 100644
--- a/commons-rng-examples/examples-jmh/src/main/java/org/apache/commons/rng/examples/jmh/sampling/distribution/PoissonSamplerCachePerformance.java
+++ b/commons-rng-examples/examples-jmh/src/main/java/org/apache/commons/rng/examples/jmh/sampling/distribution/PoissonSamplerCachePerformance.java
@@ -307,12 +307,7 @@ public class PoissonSamplerCachePerformance {
MeanRange range,
Blackhole bh) {
final UniformRandomProvider r = sources.getGenerator();
- final PoissonSamplerFactory factory = new PoissonSamplerFactory() {
- @Override
- public DiscreteSampler createPoissonSampler(double mean) {
- return PoissonSampler.of(r, mean);
- }
- };
+ final PoissonSamplerFactory factory = mean -> PoissonSampler.of(r, mean);
runSample(factory, range, bh);
}
@@ -327,12 +322,7 @@ public class PoissonSamplerCachePerformance {
Blackhole bh) {
final UniformRandomProvider r = sources.getGenerator();
final PoissonSamplerCache cache = new PoissonSamplerCache(0, 0);
- final PoissonSamplerFactory factory = new PoissonSamplerFactory() {
- @Override
- public DiscreteSampler createPoissonSampler(double mean) {
- return cache.createSharedStateSampler(r, mean);
- }
- };
+ final PoissonSamplerFactory factory = mean -> cache.createSharedStateSampler(r, mean);
runSample(factory, range, bh);
}
@@ -348,12 +338,7 @@ public class PoissonSamplerCachePerformance {
final UniformRandomProvider r = sources.getGenerator();
final PoissonSamplerCache cache = new PoissonSamplerCache(
range.getMin(), range.getMax());
- final PoissonSamplerFactory factory = new PoissonSamplerFactory() {
- @Override
- public DiscreteSampler createPoissonSampler(double mean) {
- return cache.createSharedStateSampler(r, mean);
- }
- };
+ final PoissonSamplerFactory factory = mean -> cache.createSharedStateSampler(r, mean);
runSample(factory, range, bh);
}
}
diff --git a/commons-rng-examples/examples-jmh/src/main/java/org/apache/commons/rng/examples/jmh/sampling/distribution/PoissonSamplersPerformance.java b/commons-rng-examples/examples-jmh/src/main/java/org/apache/commons/rng/examples/jmh/sampling/distribution/PoissonSamplersPerformance.java
index f167a4d..8537431 100644
--- a/commons-rng-examples/examples-jmh/src/main/java/org/apache/commons/rng/examples/jmh/sampling/distribution/PoissonSamplersPerformance.java
+++ b/commons-rng-examples/examples-jmh/src/main/java/org/apache/commons/rng/examples/jmh/sampling/distribution/PoissonSamplersPerformance.java
@@ -177,63 +177,21 @@ public class PoissonSamplersPerformance {
// This would benefit from Java 8 Supplier<DiscreteSampler> lambda function
if ("SmallMeanPoissonSampler".equals(samplerType)) {
- factory = new DiscreteSamplerFactory() {
- @Override
- public DiscreteSampler create() {
- return SmallMeanPoissonSampler.of(generator, mean);
- }
- };
+ factory = () -> SmallMeanPoissonSampler.of(generator, mean);
} else if ("KempSmallMeanPoissonSampler".equals(samplerType)) {
- factory = new DiscreteSamplerFactory() {
- @Override
- public DiscreteSampler create() {
- return KempSmallMeanPoissonSampler.of(generator, mean);
- }
- };
+ factory = () -> KempSmallMeanPoissonSampler.of(generator, mean);
} else if ("BoundedKempSmallMeanPoissonSampler".equals(samplerType)) {
- factory = new DiscreteSamplerFactory() {
- @Override
- public DiscreteSampler create() {
- return new BoundedKempSmallMeanPoissonSampler(generator, mean);
- }
- };
+ factory = () -> new BoundedKempSmallMeanPoissonSampler(generator, mean);
} else if ("KempSmallMeanPoissonSamplerP50".equals(samplerType)) {
- factory = new DiscreteSamplerFactory() {
- @Override
- public DiscreteSampler create() {
- return new KempSmallMeanPoissonSamplerP50(generator, mean);
- }
- };
+ factory = () -> new KempSmallMeanPoissonSamplerP50(generator, mean);
} else if ("KempSmallMeanPoissonSamplerBinarySearch".equals(samplerType)) {
- factory = new DiscreteSamplerFactory() {
- @Override
- public DiscreteSampler create() {
- return new KempSmallMeanPoissonSamplerBinarySearch(generator, mean);
- }
- };
+ factory = () -> new KempSmallMeanPoissonSamplerBinarySearch(generator, mean);
} else if ("KempSmallMeanPoissonSamplerGuideTable".equals(samplerType)) {
- factory = new DiscreteSamplerFactory() {
- @Override
- public DiscreteSampler create() {
- return new KempSmallMeanPoissonSamplerGuideTable(generator, mean);
- }
- };
+ factory = () -> new KempSmallMeanPoissonSamplerGuideTable(generator, mean);
} else if ("LargeMeanPoissonSampler".equals(samplerType)) {
- factory = new DiscreteSamplerFactory() {
- @Override
- public DiscreteSampler create() {
- // Note this is not valid when mean < 1
- return LargeMeanPoissonSampler.of(generator, mean);
- }
- };
+ factory = () -> LargeMeanPoissonSampler.of(generator, mean);
} else if ("TinyMeanPoissonSampler".equals(samplerType)) {
- factory = new DiscreteSamplerFactory() {
- @Override
- public DiscreteSampler create() {
- // Note this is only valid when mean < -Math.exp(0x1p-32) == 22.18
- return new TinyMeanPoissonSampler(generator, mean);
- }
- };
+ factory = () -> new TinyMeanPoissonSampler(generator, mean);
}
sampler = factory.create();
}
diff --git a/commons-rng-examples/examples-jmh/src/main/java/org/apache/commons/rng/examples/jmh/sampling/distribution/StableSamplerPerformance.java b/commons-rng-examples/examples-jmh/src/main/java/org/apache/commons/rng/examples/jmh/sampling/distribution/StableSamplerPerformance.java
index 22c1328..d8800a5 100644
--- a/commons-rng-examples/examples-jmh/src/main/java/org/apache/commons/rng/examples/jmh/sampling/distribution/StableSamplerPerformance.java
+++ b/commons-rng-examples/examples-jmh/src/main/java/org/apache/commons/rng/examples/jmh/sampling/distribution/StableSamplerPerformance.java
@@ -681,23 +681,15 @@ public class StableSamplerPerformance {
public void setup() {
final UniformRandomProvider rng = getRNG();
if (BASELINE.equals(method)) {
- sampler = new ContinuousSampler() {
- @Override
- public double sample() {
- return rng.nextDouble();
- }
- };
+ sampler = rng::nextDouble;
} else if ("nextDoubleNot0".equals(method)) {
- sampler = new ContinuousSampler() {
- @Override
- public double sample() {
- // Sample the 2^53 dyadic rationals in [0, 1) with zero excluded
- double x;
- do {
- x = rng.nextDouble();
- } while (x == 0);
- return x - 0.5;
- }
+ sampler = () -> {
+ // Sample the 2^53 dyadic rationals in [0, 1) with zero excluded
+ double x;
+ do {
+ x = rng.nextDouble();
+ } while (x == 0);
+ return x - 0.5;
};
} else if ("nextDoubleNot0Recurse".equals(method)) {
sampler = new ContinuousSampler() {
@@ -714,34 +706,25 @@ public class StableSamplerPerformance {
}
};
} else if ("nextLongNot0".equals(method)) {
- sampler = new ContinuousSampler() {
- @Override
- public double sample() {
- // Sample the 2^53 dyadic rationals in [0, 1) with zero excluded
- long x;
- do {
- x = rng.nextLong() >>> 11;
- } while (x == 0);
- return x * 0x1.0p-53 - 0.5;
- }
+ sampler = () -> {
+ // Sample the 2^53 dyadic rationals in [0, 1) with zero excluded
+ long x;
+ do {
+ x = rng.nextLong() >>> 11;
+ } while (x == 0);
+ return x * 0x1.0p-53 - 0.5;
};
} else if ("nextDoubleShifted".equals(method)) {
- sampler = new ContinuousSampler() {
- @Override
- public double sample() {
- // Sample the 2^52 dyadic rationals in [0, 1) and shift by 2^-53.
- // No infinite loop but the deviate loses 1 bit of randomness.
- return 0x1.0p-53 + (rng.nextLong() >>> 12) * 0x1.0p-52 - 0.5;
- }
+ sampler = () -> {
+ // Sample the 2^52 dyadic rationals in [0, 1) and shift by 2^-53.
+ // No infinite loop but the deviate loses 1 bit of randomness.
+ return 0x1.0p-53 + (rng.nextLong() >>> 12) * 0x1.0p-52 - 0.5;
};
} else if ("nextLongShifted".equals(method)) {
- sampler = new ContinuousSampler() {
- @Override
- public double sample() {
- // Sample the 2^53 dyadic rationals in [0, 1) but set the lowest
- // bit. This result in 2^52 dyadic rationals in (0, 1) to avoid 0.
- return ((rng.nextLong() >>> 11) | 0x1L) * 0x1.0p-53 - 0.5;
- }
+ sampler = () -> {
+ // Sample the 2^53 dyadic rationals in [0, 1) but set the lowest
+ // bit. This result in 2^52 dyadic rationals in (0, 1) to avoid 0.
+ return ((rng.nextLong() >>> 11) | 0x1L) * 0x1.0p-53 - 0.5;
};
} else if ("signedShift".equals(method)) {
sampler = new ContinuousSampler() {
@@ -832,53 +815,35 @@ public class StableSamplerPerformance {
public void setup() {
final UniformRandomProvider rng = getRNG();
if (BASELINE.equals(method)) {
- sampler = new ContinuousSampler() {
- @Override
- public double sample() {
- // A value in [-pi/4, pi/4]
- return PI_2 * (rng.nextDouble() - 0.5);
- }
+ sampler = () -> {
+ // A value in [-pi/4, pi/4]
+ return PI_2 * (rng.nextDouble() - 0.5);
};
} else if ("tan".equals(method)) {
- sampler = new ContinuousSampler() {
- @Override
- public double sample() {
- final double x = PI_2 * (rng.nextDouble() - 0.5);
- // Require tan(0) / 0 = 1 and not NaN
- return x == 0 ? 1.0 : Math.tan(x) / x;
- }
+ sampler = () -> {
+ final double x = PI_2 * (rng.nextDouble() - 0.5);
+ // Require tan(0) / 0 = 1 and not NaN
+ return x == 0 ? 1.0 : Math.tan(x) / x;
};
} else if ("tan4283".equals(method)) {
- sampler = new ContinuousSampler() {
- @Override
- public double sample() {
- final double x = PI_2 * (rng.nextDouble() - 0.5);
- return x * tan4283(x);
- }
+ sampler = () -> {
+ final double x = PI_2 * (rng.nextDouble() - 0.5);
+ return x * tan4283(x);
};
} else if ("tan4288".equals(method)) {
- sampler = new ContinuousSampler() {
- @Override
- public double sample() {
- final double x = PI_2 * (rng.nextDouble() - 0.5);
- return x * tan4288(x);
- }
+ sampler = () -> {
+ final double x = PI_2 * (rng.nextDouble() - 0.5);
+ return x * tan4288(x);
};
} else if ("tan4288b".equals(method)) {
- sampler = new ContinuousSampler() {
- @Override
- public double sample() {
- final double x = PI_2 * (rng.nextDouble() - 0.5);
- return x * tan4288b(x);
- }
+ sampler = () -> {
+ final double x = PI_2 * (rng.nextDouble() - 0.5);
+ return x * tan4288b(x);
};
} else if ("tan4288c".equals(method)) {
- sampler = new ContinuousSampler() {
- @Override
- public double sample() {
- final double x = PI_2 * (rng.nextDouble() - 0.5);
- return x * tan4288c(x);
- }
+ sampler = () -> {
+ final double x = PI_2 * (rng.nextDouble() - 0.5);
+ return x * tan4288c(x);
};
} else {
throw new IllegalStateException("Unknown tan method: " + method);
@@ -1104,40 +1069,15 @@ public class StableSamplerPerformance {
final double s = scale;
final UniformRandomProvider rng = getRNG();
if (BASELINE.equals(method)) {
- sampler = new ContinuousSampler() {
- @Override
- public double sample() {
- return s * (rng.nextDouble() - 0.5);
- }
- };
+ sampler = () -> s * (rng.nextDouble() - 0.5);
} else if ("expm1".equals(method)) {
- sampler = new ContinuousSampler() {
- @Override
- public double sample() {
- return expm1(s * (rng.nextDouble() - 0.5));
- }
- };
+ sampler = () -> expm1(s * (rng.nextDouble() - 0.5));
} else if ("expm1b".equals(method)) {
- sampler = new ContinuousSampler() {
- @Override
- public double sample() {
- return expm1b(s * (rng.nextDouble() - 0.5));
- }
- };
+ sampler = () -> expm1b(s * (rng.nextDouble() - 0.5));
} else if ("exp".equals(method)) {
- sampler = new ContinuousSampler() {
- @Override
- public double sample() {
- return exp(s * (rng.nextDouble() - 0.5));
- }
- };
+ sampler = () -> exp(s * (rng.nextDouble() - 0.5));
} else if ("hybrid".equals(method)) {
- sampler = new ContinuousSampler() {
- @Override
- public double sample() {
- return hybrid(s * (rng.nextDouble() - 0.5));
- }
- };
+ sampler = () -> hybrid(s * (rng.nextDouble() - 0.5));
} else {
throw new IllegalStateException("Unknown d2 method: " + method);
}
diff --git a/commons-rng-examples/examples-jmh/src/main/java/org/apache/commons/rng/examples/jmh/sampling/distribution/ZigguratSamplerPerformance.java b/commons-rng-examples/examples-jmh/src/main/java/org/apache/commons/rng/examples/jmh/sampling/distribution/ZigguratSamplerPerformance.java
index c1c5309..59a845a 100644
--- a/commons-rng-examples/examples-jmh/src/main/java/org/apache/commons/rng/examples/jmh/sampling/distribution/ZigguratSamplerPerformance.java
+++ b/commons-rng-examples/examples-jmh/src/main/java/org/apache/commons/rng/examples/jmh/sampling/distribution/ZigguratSamplerPerformance.java
@@ -133,20 +133,14 @@ public class ZigguratSamplerPerformance {
// Use a fast generator
final UniformRandomProvider rng = RandomSource.XO_RO_SHI_RO_128_PP.create();
if ("CastMask".equals(method)) {
- sampler = new DiscreteSampler() {
- @Override
- public int sample() {
- final long x = rng.nextLong();
- return ((int) x) & 0xff;
- }
+ sampler = () -> {
+ final long x = rng.nextLong();
+ return ((int) x) & 0xff;
};
} else if ("MaskCast".equals(method)) {
- sampler = new DiscreteSampler() {
- @Override
- public int sample() {
- final long x = rng.nextLong();
- return (int) (x & 0xff);
- }
+ sampler = () -> {
+ final long x = rng.nextLong();
+ return (int) (x & 0xff);
};
} else {
throwIllegalStateException(method);
@@ -189,19 +183,9 @@ public class ZigguratSamplerPerformance {
// Use a fast generator
final UniformRandomProvider rng = RandomSource.XO_RO_SHI_RO_128_PP.create();
if ("Mask".equals(method)) {
- sampler = new LongSampler() {
- @Override
- public long sample() {
- return rng.nextLong() & Long.MAX_VALUE;
- }
- };
+ sampler = () -> rng.nextLong() & Long.MAX_VALUE;
} else if ("Shift".equals(method)) {
- sampler = new LongSampler() {
- @Override
- public long sample() {
- return rng.nextLong() >>> 1;
- }
- };
+ sampler = () -> rng.nextLong() >>> 1;
} else {
throwIllegalStateException(method);
}
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 0340ed4..5a5f858 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
@@ -452,14 +452,11 @@ public class TetrahedronSamplerBenchmark {
private Sampler createSampler(final UniformRandomProvider rng,
double[] a, double[] b, double[] c, double[] d) {
if (BASELINE.equals(type)) {
- return new Sampler() {
- @Override
- public double[] sample() {
- final double s = rng.nextDouble();
- final double t = rng.nextDouble();
- final double u = rng.nextDouble();
- return new double[] {s, t, u};
- }
+ return () -> {
+ final double s = rng.nextDouble();
+ final double t = rng.nextDouble();
+ final double u = rng.nextDouble();
+ return new double[] {s, t, u};
};
} else if (ARRAY.equals(type)) {
return new ArrayTetrahedronSampler(rng, a, b, c, d);
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 2997c96..67632e9 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
@@ -251,25 +251,19 @@ public class TriangleSamplerBenchmark {
protected Sampler createSampler(final UniformRandomProvider rng,
final double[] a, final double[] b, final double[] c) {
if (BASELINE.equals(type)) {
- return new Sampler() {
- @Override
- public double[] sample() {
- final double s = rng.nextDouble();
- final double t = rng.nextDouble();
- return new double[] {s, t};
- }
+ return () -> {
+ final double s = rng.nextDouble();
+ final double t = rng.nextDouble();
+ return new double[] {s, t};
};
} else if (BASELINE_IF.equals(type)) {
- return new Sampler() {
- @Override
- public double[] sample() {
- final double s = rng.nextDouble();
- final double t = rng.nextDouble();
- if (s + t > 1) {
- return new double[] {s, t};
- }
- return new double[] {t, s};
+ return () -> {
+ final double s = rng.nextDouble();
+ final double t = rng.nextDouble();
+ if (s + t > 1) {
+ return new double[] {s, t};
}
+ return new double[] {t, s};
};
} else if (VECTORS.equals(type)) {
return new VectorTriangleSampler2D(rng, a, b, c);
@@ -371,25 +365,19 @@ public class TriangleSamplerBenchmark {
protected Sampler createSampler(final UniformRandomProvider rng,
final double[] a, final double[] b, final double[] c) {
if (BASELINE.equals(type)) {
- return new Sampler() {
- @Override
- public double[] sample() {
- final double s = rng.nextDouble();
- final double t = rng.nextDouble();
- return new double[] {s, t, s};
- }
+ return () -> {
+ final double s = rng.nextDouble();
+ final double t = rng.nextDouble();
+ return new double[] {s, t, s};
};
} else if (BASELINE_IF.equals(type)) {
- return new Sampler() {
- @Override
- public double[] sample() {
- final double s = rng.nextDouble();
- final double t = rng.nextDouble();
- if (s + t > 1) {
- return new double[] {s, t, s};
- }
- return new double[] {t, s, t};
+ return () -> {
+ final double s = rng.nextDouble();
+ final double t = rng.nextDouble();
+ if (s + t > 1) {
+ return new double[] {s, t, s};
}
+ return new double[] {t, s, t};
};
} else if (VECTORS.equals(type)) {
return new VectorTriangleSampler3D(rng, a, b, c);
@@ -509,42 +497,36 @@ public class TriangleSamplerBenchmark {
protected Sampler createSampler(final UniformRandomProvider rng,
final double[] a, final double[] b, final double[] c) {
if (BASELINE.equals(type)) {
- return new Sampler() {
- @Override
- public double[] sample() {
- double s = rng.nextDouble();
- double t = rng.nextDouble();
- final double[] x = new double[a.length];
- for (int i = 0; i < x.length; i++) {
- x[i] = s;
- s = t;
- t = x[i];
- }
- return x;
+ return () -> {
+ double s = rng.nextDouble();
+ double t = rng.nextDouble();
+ final double[] x = new double[a.length];
+ for (int i = 0; i < x.length; i++) {
+ x[i] = s;
+ s = t;
+ t = x[i];
}
+ return x;
};
} else if (BASELINE_IF.equals(type)) {
- return new Sampler() {
- @Override
- public double[] sample() {
- double s = rng.nextDouble();
- double t = rng.nextDouble();
- final double[] x = new double[a.length];
- if (s + t > 1) {
- for (int i = 0; i < x.length; i++) {
- x[i] = t;
- t = s;
- s = x[i];
- }
- return x;
- }
+ return () -> {
+ double s = rng.nextDouble();
+ double t = rng.nextDouble();
+ final double[] x = new double[a.length];
+ if (s + t > 1) {
for (int i = 0; i < x.length; i++) {
- x[i] = s;
- s = t;
- t = x[i];
+ x[i] = t;
+ t = s;
+ s = x[i];
}
return x;
}
+ for (int i = 0; i < x.length; i++) {
+ x[i] = s;
+ s = t;
+ t = x[i];
+ }
+ return x;
};
} else if (VECTORS.equals(type)) {
return new VectorTriangleSamplerND(rng, a, b, c);
diff --git a/commons-rng-examples/examples-jmh/src/main/java/org/apache/commons/rng/examples/jmh/sampling/shape/UnitBallSamplerBenchmark.java b/commons-rng-examples/examples-jmh/src/main/java/org/apache/commons/rng/examples/jmh/sampling/shape/UnitBallSamplerBenchmark.java
index 04c098d..8fc30ff 100644
--- a/commons-rng-examples/examples-jmh/src/main/java/org/apache/commons/rng/examples/jmh/sampling/shape/UnitBallSamplerBenchmark.java
+++ b/commons-rng-examples/examples-jmh/src/main/java/org/apache/commons/rng/examples/jmh/sampling/shape/UnitBallSamplerBenchmark.java
@@ -166,45 +166,20 @@ public class UnitBallSamplerBenchmark {
@Override
protected Sampler createSampler(final UniformRandomProvider rng) {
if (BASELINE.equals(type)) {
- return new Sampler() {
- @Override
- public double[] sample() {
- return new double[] {0.5};
- }
- };
+ return () -> new double[] {0.5};
} else if (SIGNED_DOUBLE.equals(type)) {
- return new Sampler() {
- @Override
- public double[] sample() {
- // Sample [-1, 1) uniformly
- return new double[] {makeSignedDouble(rng.nextLong())};
- }
- };
+ // Sample [-1, 1) uniformly
+ return () -> new double[] {makeSignedDouble(rng.nextLong())};
} else if (SIGNED_DOUBLE2.equals(type)) {
- return new Sampler() {
- @Override
- public double[] sample() {
- // Sample [-1, 1) uniformly
- return new double[] {makeSignedDouble2(rng.nextLong())};
- }
- };
+ // Sample [-1, 1) uniformly
+ return () -> new double[] {makeSignedDouble2(rng.nextLong())};
} else if (TWO_DOUBLES.equals(type)) {
- return new Sampler() {
- @Override
- public double[] sample() {
- // Sample [-1, 1) excluding -0.0 but also missing the final 1.0 - 2^-53.
- // The 1.0 could be adjusted to 1.0 - 2^-53 to create the interval (-1, 1).
- return new double[] {rng.nextDouble() + rng.nextDouble() - 1.0};
- }
- };
+ // Sample [-1, 1) excluding -0.0 but also missing the final 1.0 - 2^-53.
+ // The 1.0 could be adjusted to 1.0 - 2^-53 to create the interval (-1, 1).
+ return () -> new double[] {rng.nextDouble() + rng.nextDouble() - 1.0};
} else if (BOOLEAN_DOUBLE.equals(type)) {
- return new Sampler() {
- @Override
- public double[] sample() {
- // This will sample (-1, 1) including -0.0 and 0.0
- return new double[] {rng.nextBoolean() ? -rng.nextDouble() : rng.nextDouble()};
- }
- };
+ // This will sample (-1, 1) including -0.0 and 0.0
+ return () -> new double[] {rng.nextBoolean() ? -rng.nextDouble() : rng.nextDouble()};
}
throw new IllegalStateException(UNKNOWN_SAMPLER + type);
}
@@ -223,12 +198,7 @@ public class UnitBallSamplerBenchmark {
@Override
protected Sampler createSampler(final UniformRandomProvider rng) {
if (BASELINE.equals(type)) {
- return new Sampler() {
- @Override
- public double[] sample() {
- return new double[] {0.5, 0};
- }
- };
+ return () -> new double[] {0.5, 0};
} else if (REJECTION.equals(type)) {
return new RejectionSampler(rng);
} else if (DISK_POINT.equals(type)) {
@@ -373,12 +343,7 @@ public class UnitBallSamplerBenchmark {
@Override
protected Sampler createSampler(final UniformRandomProvider rng) {
if (BASELINE.equals(type)) {
- return new Sampler() {
- @Override
- public double[] sample() {
- return new double[] {0.5, 0, 0};
- }
- };
+ return () -> new double[] {0.5, 0, 0};
} else if (REJECTION.equals(type)) {
return new RejectionSampler(rng);
} else if (BALL_POINT.equals(type)) {