From 6d767220edb7a5c821b37834250bfd4d9ce562b7 Mon Sep 17 00:00:00 2001 From: aherbert Date: Fri, 2 Dec 2022 13:33:39 +0000 Subject: [PATCH] Use method reference to create a sampler --- .../AbstractIntegerDistribution.java | 30 ++----------------- .../AbstractRealDistribution.java | 30 ++----------------- 2 files changed, 4 insertions(+), 56 deletions(-) diff --git a/commons-math-legacy/src/main/java/org/apache/commons/math4/legacy/distribution/AbstractIntegerDistribution.java b/commons-math-legacy/src/main/java/org/apache/commons/math4/legacy/distribution/AbstractIntegerDistribution.java index 15a7c5340..b18246022 100644 --- a/commons-math-legacy/src/main/java/org/apache/commons/math4/legacy/distribution/AbstractIntegerDistribution.java +++ b/commons-math-legacy/src/main/java/org/apache/commons/math4/legacy/distribution/AbstractIntegerDistribution.java @@ -23,8 +23,6 @@ import org.apache.commons.math4.legacy.exception.OutOfRangeException; import org.apache.commons.math4.legacy.exception.util.LocalizedFormats; import org.apache.commons.rng.UniformRandomProvider; import org.apache.commons.rng.sampling.distribution.InverseTransformDiscreteSampler; -import org.apache.commons.rng.sampling.distribution.DiscreteInverseCumulativeProbabilityFunction; -import org.apache.commons.rng.sampling.distribution.DiscreteSampler; import org.apache.commons.math4.core.jdkmath.JdkMath; /** @@ -194,31 +192,7 @@ public abstract class AbstractIntegerDistribution /**{@inheritDoc} */ @Override public DiscreteDistribution.Sampler createSampler(final UniformRandomProvider rng) { - return new DiscreteDistribution.Sampler() { - /** - * Inversion method distribution sampler. - */ - private final DiscreteSampler sampler = - new InverseTransformDiscreteSampler(rng, createICPF()); - - /** {@inheritDoc} */ - @Override - public int sample() { - return sampler.sample(); - } - }; - } - - /** - * @return an instance for use by {@link #createSampler(UniformRandomProvider)} - */ - private DiscreteInverseCumulativeProbabilityFunction createICPF() { - return new DiscreteInverseCumulativeProbabilityFunction() { - /** {@inheritDoc} */ - @Override - public int inverseCumulativeProbability(double p) { - return AbstractIntegerDistribution.this.inverseCumulativeProbability(p); - } - }; + // Inversion method distribution sampler. + return InverseTransformDiscreteSampler.of(rng, this::inverseCumulativeProbability)::sample; } } diff --git a/commons-math-legacy/src/main/java/org/apache/commons/math4/legacy/distribution/AbstractRealDistribution.java b/commons-math-legacy/src/main/java/org/apache/commons/math4/legacy/distribution/AbstractRealDistribution.java index 632d65461..30bfafb39 100644 --- a/commons-math-legacy/src/main/java/org/apache/commons/math4/legacy/distribution/AbstractRealDistribution.java +++ b/commons-math-legacy/src/main/java/org/apache/commons/math4/legacy/distribution/AbstractRealDistribution.java @@ -24,8 +24,6 @@ import org.apache.commons.math4.legacy.exception.OutOfRangeException; import org.apache.commons.math4.legacy.exception.util.LocalizedFormats; import org.apache.commons.rng.UniformRandomProvider; import org.apache.commons.rng.sampling.distribution.InverseTransformContinuousSampler; -import org.apache.commons.rng.sampling.distribution.ContinuousInverseCumulativeProbabilityFunction; -import org.apache.commons.rng.sampling.distribution.ContinuousSampler; import org.apache.commons.math4.core.jdkmath.JdkMath; /** @@ -211,31 +209,7 @@ public abstract class AbstractRealDistribution /**{@inheritDoc} */ @Override public ContinuousDistribution.Sampler createSampler(final UniformRandomProvider rng) { - return new ContinuousDistribution.Sampler() { - /** - * Inversion method distribution sampler. - */ - private final ContinuousSampler sampler = - new InverseTransformContinuousSampler(rng, createICPF()); - - /** {@inheritDoc} */ - @Override - public double sample() { - return sampler.sample(); - } - }; - } - - /** - * @return an instance for use by {@link #createSampler(UniformRandomProvider)} - */ - private ContinuousInverseCumulativeProbabilityFunction createICPF() { - return new ContinuousInverseCumulativeProbabilityFunction() { - /** {@inheritDoc} */ - @Override - public double inverseCumulativeProbability(double p) { - return AbstractRealDistribution.this.inverseCumulativeProbability(p); - } - }; + // Inversion method distribution sampler. + return InverseTransformContinuousSampler.of(rng, this::inverseCumulativeProbability)::sample; } }