Repository: commons-math Updated Branches: refs/heads/MATH_3_X 4025b62c7 -> 5e4739fb7
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/5e4739fb Tree: http://git-wip-us.apache.org/repos/asf/commons-math/tree/5e4739fb Diff: http://git-wip-us.apache.org/repos/asf/commons-math/diff/5e4739fb Branch: refs/heads/MATH_3_X Commit: 5e4739fb72468145436fd794b62718b00561d3d7 Parents: 4025b62 Author: Gilles <[email protected]> Authored: Wed Sep 30 14:41:47 2015 +0200 Committer: Gilles <[email protected]> Committed: Wed Sep 30 14:41:47 2015 +0200 ---------------------------------------------------------------------- src/changes/changes.xml | 3 +++ .../commons/math3/random/EmpiricalDistribution.java | 13 ++++++++++--- .../math3/random/EmpiricalDistributionTest.java | 7 +++++++ 3 files changed, 20 insertions(+), 3 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/commons-math/blob/5e4739fb/src/changes/changes.xml ---------------------------------------------------------------------- diff --git a/src/changes/changes.xml b/src/changes/changes.xml index e6983dd..1d0de9a 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -51,6 +51,9 @@ If the output is not quite correct, check for invisible trailing spaces! </properties> <body> <release version="3.6" date="XXXX-XX-XX" description=""> + <action dev="erans" type="fix" issue="MATH-1279"> + Check precondition (class "o.a.c.m.random.EmpiricalDistribution"). + </action> <action dev="erans" type="add" issue="MATH-1278"> 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/5e4739fb/src/main/java/org/apache/commons/math3/random/EmpiricalDistribution.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/math3/random/EmpiricalDistribution.java b/src/main/java/org/apache/commons/math3/random/EmpiricalDistribution.java index f5c920b..74b1bdd 100644 --- a/src/main/java/org/apache/commons/math3/random/EmpiricalDistribution.java +++ b/src/main/java/org/apache/commons/math3/random/EmpiricalDistribution.java @@ -37,6 +37,7 @@ import org.apache.commons.math3.exception.MathInternalError; import org.apache.commons.math3.exception.NullArgumentException; import org.apache.commons.math3.exception.OutOfRangeException; import org.apache.commons.math3.exception.ZeroException; +import org.apache.commons.math3.exception.NotStrictlyPositiveException; import org.apache.commons.math3.exception.util.LocalizedFormats; import org.apache.commons.math3.stat.descriptive.StatisticalSummary; import org.apache.commons.math3.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) { @@ -207,12 +210,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/5e4739fb/src/test/java/org/apache/commons/math3/random/EmpiricalDistributionTest.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/commons/math3/random/EmpiricalDistributionTest.java b/src/test/java/org/apache/commons/math3/random/EmpiricalDistributionTest.java index 23dd346..71763e1 100644 --- a/src/test/java/org/apache/commons/math3/random/EmpiricalDistributionTest.java +++ b/src/test/java/org/apache/commons/math3/random/EmpiricalDistributionTest.java @@ -34,6 +34,7 @@ import org.apache.commons.math3.distribution.RealDistribution; import org.apache.commons.math3.distribution.RealDistributionAbstractTest; import org.apache.commons.math3.distribution.UniformRealDistribution; import org.apache.commons.math3.exception.NullArgumentException; +import org.apache.commons.math3.exception.NotStrictlyPositiveException; import org.apache.commons.math3.stat.descriptive.SummaryStatistics; import org.apache.commons.math3.util.FastMath; import org.junit.Assert; @@ -86,6 +87,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
