Repository: commons-math Updated Branches: refs/heads/master 2aa4681cb -> 66d0a9a0a
MATH-1279 Check precondition. Thanks to David Georg Reichelt for the report. Project: http://git-wip-us.apache.org/repos/asf/commons-math/repo Commit: http://git-wip-us.apache.org/repos/asf/commons-math/commit/66d0a9a0 Tree: http://git-wip-us.apache.org/repos/asf/commons-math/tree/66d0a9a0 Diff: http://git-wip-us.apache.org/repos/asf/commons-math/diff/66d0a9a0 Branch: refs/heads/master Commit: 66d0a9a0a00dbb259425f17745f7dddd7e676f71 Parents: 2aa4681 Author: Gilles <[email protected]> Authored: Wed Sep 30 15:01:32 2015 +0200 Committer: Gilles <[email protected]> Committed: Wed Sep 30 15:01:32 2015 +0200 ---------------------------------------------------------------------- src/changes/changes.xml | 3 +++ .../commons/math4/random/EmpiricalDistribution.java | 13 ++++++++++--- .../math4/random/EmpiricalDistributionTest.java | 7 +++++++ 3 files changed, 20 insertions(+), 3 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/commons-math/blob/66d0a9a0/src/changes/changes.xml ---------------------------------------------------------------------- diff --git a/src/changes/changes.xml b/src/changes/changes.xml index 1b51e95..91553f2 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -54,6 +54,9 @@ If the output is not quite correct, check for invisible trailing spaces! </release> <release version="4.0" date="XXXX-XX-XX" description=""> + <action dev="erans" type="fix" issue="MATH-1279"> <!-- backported to 3.6 --> + Check precondition (class "o.a.c.m.random.EmpiricalDistribution"). + </action> <action dev="erans" type="add" issue="MATH-1278"> <!-- backported to 3.6 --> Deep copy of "Network" (package "o.a.c.m.ml.neuralnet") to allow evaluation of of intermediate states during training. http://git-wip-us.apache.org/repos/asf/commons-math/blob/66d0a9a0/src/main/java/org/apache/commons/math4/random/EmpiricalDistribution.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/math4/random/EmpiricalDistribution.java b/src/main/java/org/apache/commons/math4/random/EmpiricalDistribution.java index ac65a73..f1a86b1 100644 --- a/src/main/java/org/apache/commons/math4/random/EmpiricalDistribution.java +++ b/src/main/java/org/apache/commons/math4/random/EmpiricalDistribution.java @@ -37,6 +37,7 @@ import org.apache.commons.math4.exception.MathInternalError; import org.apache.commons.math4.exception.NullArgumentException; import org.apache.commons.math4.exception.OutOfRangeException; import org.apache.commons.math4.exception.ZeroException; +import org.apache.commons.math4.exception.NotStrictlyPositiveException; import org.apache.commons.math4.exception.util.LocalizedFormats; import org.apache.commons.math4.stat.descriptive.StatisticalSummary; import org.apache.commons.math4.stat.descriptive.SummaryStatistics; @@ -147,7 +148,8 @@ public class EmpiricalDistribution extends AbstractRealDistribution { /** * Creates a new EmpiricalDistribution with the specified bin count. * - * @param binCount number of bins + * @param binCount number of bins. Must be strictly positive. + * @throws NotStrictlyPositiveException if {@code binCount <= 0}. */ public EmpiricalDistribution(int binCount) { this(binCount, new RandomDataGenerator()); @@ -157,8 +159,9 @@ public class EmpiricalDistribution extends AbstractRealDistribution { * Creates a new EmpiricalDistribution with the specified bin count using the * provided {@link RandomGenerator} as the source of random data. * - * @param binCount number of bins + * @param binCount number of bins. Must be strictly positive. * @param generator random data generator (may be null, resulting in default JDK generator) + * @throws NotStrictlyPositiveException if {@code binCount <= 0}. * @since 3.0 */ public EmpiricalDistribution(int binCount, RandomGenerator generator) { @@ -180,12 +183,16 @@ public class EmpiricalDistribution extends AbstractRealDistribution { * Private constructor to allow lazy initialisation of the RNG contained * in the {@link #randomData} instance variable. * - * @param binCount number of bins + * @param binCount number of bins. Must be strictly positive. * @param randomData Random data generator. + * @throws NotStrictlyPositiveException if {@code binCount <= 0}. */ private EmpiricalDistribution(int binCount, RandomDataGenerator randomData) { super(randomData.getRandomGenerator()); + if (binCount <= 0) { + throw new NotStrictlyPositiveException(binCount); + } this.binCount = binCount; this.randomData = randomData; binStats = new ArrayList<SummaryStatistics>(); http://git-wip-us.apache.org/repos/asf/commons-math/blob/66d0a9a0/src/test/java/org/apache/commons/math4/random/EmpiricalDistributionTest.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/commons/math4/random/EmpiricalDistributionTest.java b/src/test/java/org/apache/commons/math4/random/EmpiricalDistributionTest.java index 9633ff7..4854979 100644 --- a/src/test/java/org/apache/commons/math4/random/EmpiricalDistributionTest.java +++ b/src/test/java/org/apache/commons/math4/random/EmpiricalDistributionTest.java @@ -35,6 +35,7 @@ import org.apache.commons.math4.distribution.RealDistributionAbstractTest; import org.apache.commons.math4.distribution.UniformRealDistribution; import org.apache.commons.math4.exception.MathIllegalStateException; import org.apache.commons.math4.exception.NullArgumentException; +import org.apache.commons.math4.exception.NotStrictlyPositiveException; import org.apache.commons.math4.random.EmpiricalDistribution; import org.apache.commons.math4.random.RandomGenerator; import org.apache.commons.math4.stat.descriptive.SummaryStatistics; @@ -85,6 +86,12 @@ public final class EmpiricalDistributionTest extends RealDistributionAbstractTes } } + // MATH-1279 + @Test(expected=NotStrictlyPositiveException.class) + public void testPrecondition1() { + new EmpiricalDistribution(0); + } + /** * Test EmpiricalDistrbution.load() using sample data file.<br> * Check that the sampleCount, mu and sigma match data in
