From cab4f493398c1991218099c6cfa2c0cbf096b79a Mon Sep 17 00:00:00 2001 From: Gilles Sadowski Date: Thu, 1 Jul 2010 12:30:43 +0000 Subject: [PATCH] MATH-361 Added new exceptions. Replaced several instances of "IllegalArgumentException" with a more specific exception. For not yet released code, removed the checked "DimensionMismatchException" from the method signature and replaced its instances with the new (unchecked) version of the exception. Corrected typos in Javadoc comments. git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/trunk@959634 13f79535-47bb-0310-9956-ffa450edef68 --- .../BicubicSplineInterpolatingFunction.java | 4 +- .../BicubicSplineInterpolator.java | 4 +- ...ngPolynomialBicubicSplineInterpolator.java | 13 +- .../TricubicSplineInterpolatingFunction.java | 38 +++--- .../TricubicSplineInterpolator.java | 15 ++- .../TrivariateRealGridInterpolator.java | 5 +- .../exception/MathIllegalNumberException.java | 2 +- .../math/exception/NoDataException.java | 34 +++++ .../NonMonotonousSequenceException.java | 119 ++++++++++++++++++ .../commons/math/util/LocalizedFormats.java | 6 +- .../apache/commons/math/util/MathUtils.java | 110 +++++++++++----- .../math/util/MultidimensionalCounter.java | 2 +- .../LocalizedFormats_fr.properties | 4 + ...lynomialBicubicSplineInterpolatorTest.java | 2 +- ...icubicSplineInterpolatingFunctionTest.java | 2 +- .../TricubicSplineInterpolatorTest.java | 2 +- .../NonMonotonousSequenceExceptionTest.java | 47 +++++++ .../commons/math/util/MathUtilsTest.java | 33 +++-- 18 files changed, 347 insertions(+), 95 deletions(-) create mode 100644 src/main/java/org/apache/commons/math/exception/NoDataException.java create mode 100644 src/main/java/org/apache/commons/math/exception/NonMonotonousSequenceException.java create mode 100644 src/test/java/org/apache/commons/math/exception/NonMonotonousSequenceExceptionTest.java diff --git a/src/main/java/org/apache/commons/math/analysis/interpolation/BicubicSplineInterpolatingFunction.java b/src/main/java/org/apache/commons/math/analysis/interpolation/BicubicSplineInterpolatingFunction.java index 2467c9eb5..e50ef0096 100644 --- a/src/main/java/org/apache/commons/math/analysis/interpolation/BicubicSplineInterpolatingFunction.java +++ b/src/main/java/org/apache/commons/math/analysis/interpolation/BicubicSplineInterpolatingFunction.java @@ -114,8 +114,8 @@ public class BicubicSplineInterpolatingFunction throw new DimensionMismatchException(xLen, d2FdXdY.length); } - MathUtils.checkOrder(x, 1, true); - MathUtils.checkOrder(y, 1, true); + MathUtils.checkOrder(x); + MathUtils.checkOrder(y); xval = x.clone(); yval = y.clone(); diff --git a/src/main/java/org/apache/commons/math/analysis/interpolation/BicubicSplineInterpolator.java b/src/main/java/org/apache/commons/math/analysis/interpolation/BicubicSplineInterpolator.java index a2f29fc04..5a9dad5c1 100644 --- a/src/main/java/org/apache/commons/math/analysis/interpolation/BicubicSplineInterpolator.java +++ b/src/main/java/org/apache/commons/math/analysis/interpolation/BicubicSplineInterpolator.java @@ -46,8 +46,8 @@ public class BicubicSplineInterpolator throw new DimensionMismatchException(xval.length, fval.length); } - MathUtils.checkOrder(xval, 1, true); - MathUtils.checkOrder(yval, 1, true); + MathUtils.checkOrder(xval); + MathUtils.checkOrder(yval); final int xLen = xval.length; final int yLen = yval.length; diff --git a/src/main/java/org/apache/commons/math/analysis/interpolation/SmoothingPolynomialBicubicSplineInterpolator.java b/src/main/java/org/apache/commons/math/analysis/interpolation/SmoothingPolynomialBicubicSplineInterpolator.java index ea2597865..18d31a86f 100644 --- a/src/main/java/org/apache/commons/math/analysis/interpolation/SmoothingPolynomialBicubicSplineInterpolator.java +++ b/src/main/java/org/apache/commons/math/analysis/interpolation/SmoothingPolynomialBicubicSplineInterpolator.java @@ -16,10 +16,9 @@ */ package org.apache.commons.math.analysis.interpolation; -import org.apache.commons.math.DimensionMismatchException; -import org.apache.commons.math.MathRuntimeException; +import org.apache.commons.math.exception.DimensionMismatchException; +import org.apache.commons.math.exception.NoDataException; import org.apache.commons.math.MathException; -import org.apache.commons.math.util.LocalizedFormats; import org.apache.commons.math.util.MathUtils; import org.apache.commons.math.optimization.general.GaussNewtonOptimizer; import org.apache.commons.math.optimization.fitting.PolynomialFitter; @@ -70,9 +69,9 @@ public class SmoothingPolynomialBicubicSplineInterpolator public BicubicSplineInterpolatingFunction interpolate(final double[] xval, final double[] yval, final double[][] fval) - throws MathException, IllegalArgumentException { + throws MathException { if (xval.length == 0 || yval.length == 0 || fval.length == 0) { - throw MathRuntimeException.createIllegalArgumentException(LocalizedFormats.NO_DATA); + throw new NoDataException(); } if (xval.length != fval.length) { throw new DimensionMismatchException(xval.length, fval.length); @@ -87,8 +86,8 @@ public class SmoothingPolynomialBicubicSplineInterpolator } } - MathUtils.checkOrder(xval, 1, true); - MathUtils.checkOrder(yval, 1, true); + MathUtils.checkOrder(xval); + MathUtils.checkOrder(yval); // For each line y[j] (0 <= j < yLen), construct a polynomial, with // respect to variable x, fitting array fval[][j] diff --git a/src/main/java/org/apache/commons/math/analysis/interpolation/TricubicSplineInterpolatingFunction.java b/src/main/java/org/apache/commons/math/analysis/interpolation/TricubicSplineInterpolatingFunction.java index 9cfbd1ee3..4f2b77bcf 100644 --- a/src/main/java/org/apache/commons/math/analysis/interpolation/TricubicSplineInterpolatingFunction.java +++ b/src/main/java/org/apache/commons/math/analysis/interpolation/TricubicSplineInterpolatingFunction.java @@ -18,8 +18,9 @@ package org.apache.commons.math.analysis.interpolation; import org.apache.commons.math.util.LocalizedFormats; import org.apache.commons.math.util.MathUtils; -import org.apache.commons.math.MathRuntimeException; -import org.apache.commons.math.DimensionMismatchException; +import org.apache.commons.math.exception.NoDataException; +import org.apache.commons.math.exception.OutOfRangeException; +import org.apache.commons.math.exception.DimensionMismatchException; import org.apache.commons.math.analysis.TrivariateRealFunction; /** @@ -134,6 +135,7 @@ public class TricubicSplineInterpolatingFunction * every grid point. * @param d3FdXdYdZ Values of the cross partial derivative of function on * every grid point. + * @throws NoDataException if any of the arrays has zero length. * @throws DimensionMismatchException if the various arrays do not contain * the expected number of elements. * @throws IllegalArgumentException if {@code x}, {@code y} or {@code z} @@ -149,15 +151,14 @@ public class TricubicSplineInterpolatingFunction double[][][] d2FdXdY, double[][][] d2FdXdZ, double[][][] d2FdYdZ, - double[][][] d3FdXdYdZ) - throws DimensionMismatchException { + double[][][] d3FdXdYdZ) { final int xLen = x.length; final int yLen = y.length; final int zLen = z.length; if (xLen == 0 || yLen == 0 || z.length == 0 || f.length == 0 || f[0].length == 0) { - throw MathRuntimeException.createIllegalArgumentException(LocalizedFormats.NO_DATA); + throw new NoDataException(); } if (xLen != f.length) { throw new DimensionMismatchException(xLen, f.length); @@ -184,9 +185,9 @@ public class TricubicSplineInterpolatingFunction throw new DimensionMismatchException(xLen, d3FdXdYdZ.length); } - MathUtils.checkOrder(x, 1, true); - MathUtils.checkOrder(y, 1, true); - MathUtils.checkOrder(z, 1, true); + MathUtils.checkOrder(x); + MathUtils.checkOrder(y); + MathUtils.checkOrder(z); xval = x.clone(); yval = y.clone(); @@ -308,21 +309,15 @@ public class TricubicSplineInterpolatingFunction public double value(double x, double y, double z) { final int i = searchIndex(x, xval); if (i == -1) { - throw MathRuntimeException.createIllegalArgumentException( - LocalizedFormats.OUT_OF_RANGE_SIMPLE, - x, xval[0], xval[xval.length - 1]); + throw new OutOfRangeException(x, xval[0], xval[xval.length - 1]); } final int j = searchIndex(y, yval); if (j == -1) { - throw MathRuntimeException.createIllegalArgumentException( - LocalizedFormats.OUT_OF_RANGE_SIMPLE, - y, yval[0], yval[yval.length - 1]); + throw new OutOfRangeException(y, yval[0], yval[yval.length - 1]); } final int k = searchIndex(z, zval); if (k == -1) { - throw MathRuntimeException.createIllegalArgumentException( - LocalizedFormats.OUT_OF_RANGE_SIMPLE, - z, zval[0], zval[zval.length - 1]); + throw new OutOfRangeException(z, zval[0], zval[zval.length - 1]); } final double xN = (x - xval[i]) / (xval[i + 1] - xval[i]); @@ -453,16 +448,13 @@ class TricubicSplineFunction */ public double value(double x, double y, double z) { if (x < 0 || x > 1) { - throw MathRuntimeException.createIllegalArgumentException(LocalizedFormats.OUT_OF_RANGE_SIMPLE, - x, 0, 1); + throw new OutOfRangeException(x, 0, 1); } if (y < 0 || y > 1) { - throw MathRuntimeException.createIllegalArgumentException(LocalizedFormats.OUT_OF_RANGE_SIMPLE, - y, 0, 1); + throw new OutOfRangeException(y, 0, 1); } if (z < 0 || z > 1) { - throw MathRuntimeException.createIllegalArgumentException(LocalizedFormats.OUT_OF_RANGE_SIMPLE, - z, 0, 1); + throw new OutOfRangeException(z, 0, 1); } final double x2 = x * x; diff --git a/src/main/java/org/apache/commons/math/analysis/interpolation/TricubicSplineInterpolator.java b/src/main/java/org/apache/commons/math/analysis/interpolation/TricubicSplineInterpolator.java index 0a06a09b9..dac7f2666 100644 --- a/src/main/java/org/apache/commons/math/analysis/interpolation/TricubicSplineInterpolator.java +++ b/src/main/java/org/apache/commons/math/analysis/interpolation/TricubicSplineInterpolator.java @@ -16,10 +16,9 @@ */ package org.apache.commons.math.analysis.interpolation; -import org.apache.commons.math.DimensionMismatchException; -import org.apache.commons.math.MathRuntimeException; +import org.apache.commons.math.exception.DimensionMismatchException; +import org.apache.commons.math.exception.NoDataException; import org.apache.commons.math.MathException; -import org.apache.commons.math.util.LocalizedFormats; import org.apache.commons.math.util.MathUtils; /** @@ -37,17 +36,17 @@ public class TricubicSplineInterpolator final double[] yval, final double[] zval, final double[][][] fval) - throws MathException, IllegalArgumentException { + throws MathException { if (xval.length == 0 || yval.length == 0 || zval.length == 0 || fval.length == 0) { - throw MathRuntimeException.createIllegalArgumentException(LocalizedFormats.NO_DATA); + throw new NoDataException(); } if (xval.length != fval.length) { throw new DimensionMismatchException(xval.length, fval.length); } - MathUtils.checkOrder(xval, 1, true); - MathUtils.checkOrder(yval, 1, true); - MathUtils.checkOrder(zval, 1, true); + MathUtils.checkOrder(xval); + MathUtils.checkOrder(yval); + MathUtils.checkOrder(zval); final int xLen = xval.length; final int yLen = yval.length; diff --git a/src/main/java/org/apache/commons/math/analysis/interpolation/TrivariateRealGridInterpolator.java b/src/main/java/org/apache/commons/math/analysis/interpolation/TrivariateRealGridInterpolator.java index 5a6b94cf2..9333181eb 100644 --- a/src/main/java/org/apache/commons/math/analysis/interpolation/TrivariateRealGridInterpolator.java +++ b/src/main/java/org/apache/commons/math/analysis/interpolation/TrivariateRealGridInterpolator.java @@ -24,6 +24,7 @@ import org.apache.commons.math.analysis.TrivariateRealFunction; * sample points must be specified on a regular grid. * * @version $Revision$ $Date$ + * @since 2.2 */ public interface TrivariateRealGridInterpolator { /** @@ -37,7 +38,9 @@ public interface TrivariateRealGridInterpolator { * in increasing order. * @param fval the values of the interpolation points on all the grid knots: * {@code fval[i][j][k] = f(xval[i], yval[j], zval[k])}. - * @return a function which interpolates the data set. + * @return a function that interpolates the data set. + * @throws NoDataException if any of the arrays has zero length. + * @throws DimensionMismatchException if the array lengths are inconsistent. * @throws MathException if arguments violate assumptions made by the * interpolation algorithm. */ diff --git a/src/main/java/org/apache/commons/math/exception/MathIllegalNumberException.java b/src/main/java/org/apache/commons/math/exception/MathIllegalNumberException.java index cfac79dd8..dde06d746 100644 --- a/src/main/java/org/apache/commons/math/exception/MathIllegalNumberException.java +++ b/src/main/java/org/apache/commons/math/exception/MathIllegalNumberException.java @@ -34,7 +34,7 @@ public class MathIllegalNumberException extends MathIllegalArgumentException { /** * Construct an exception. * - * @param Localizable pattern. + * @param pattern Localizable pattern. * @param arguments Arguments. The first element must be the requested * value that raised the exception. */ diff --git a/src/main/java/org/apache/commons/math/exception/NoDataException.java b/src/main/java/org/apache/commons/math/exception/NoDataException.java new file mode 100644 index 000000000..6522c6f92 --- /dev/null +++ b/src/main/java/org/apache/commons/math/exception/NoDataException.java @@ -0,0 +1,34 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.commons.math.exception; + +import org.apache.commons.math.util.LocalizedFormats; + +/** + * Exception to be thrown when the required data is missing. + * + * @since 2.2 + * @version $Revision$ $Date$ + */ +public class NoDataException extends MathIllegalArgumentException { + /** + * Construct the exception. + */ + public NoDataException() { + super(LocalizedFormats.NO_DATA, null); + } +} diff --git a/src/main/java/org/apache/commons/math/exception/NonMonotonousSequenceException.java b/src/main/java/org/apache/commons/math/exception/NonMonotonousSequenceException.java new file mode 100644 index 000000000..9744c28f5 --- /dev/null +++ b/src/main/java/org/apache/commons/math/exception/NonMonotonousSequenceException.java @@ -0,0 +1,119 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.commons.math.exception; + +import org.apache.commons.math.util.LocalizedFormats; +import org.apache.commons.math.util.MathUtils; + +/** + * Exception to be thrown when the a sequence of values is not monotonously + * increasing or decreasing. + * + * @since 2.2 + * @version $Revision$ $Date$ + */ +public class NonMonotonousSequenceException extends MathIllegalNumberException { + /** + * Direction (positive for increasing, negative for decreasing). + */ + private final MathUtils.Order.Direction direction; + /** + * Whether the sequence must be strictly increasing or decreasing. + */ + private final boolean strict; + /** + * Index of the wrong value. + */ + private final int index; + /** + * Previous value. + */ + private final Number previous; + + /** + * Construct the exception. + * This constructor uses default values assuming that the sequence should + * have been strictly increasing. + * + * @param wrong Value that did not match the requirements. + * @param previous Previous value in the sequence. + * @param index Index of the value that did not match the requirements. + */ + public NonMonotonousSequenceException(Number wrong, + Number previous, + int index) { + this(wrong, previous, index, MathUtils.Order.Direction.INCREASING, true); + } + + /** + * Construct the exception. + * + * @param wrong Value that did not match the requirements. + * @param previous Previous value in the sequence. + * @param index Index of the value that did not match the requirements. + * @param direction Strictly positive for a sequence required to be + * increasing, negative (or zero) for a decreasing sequence. + * @param strict Whether the sequence must be strictly increasing or + * decreasing. + */ + public NonMonotonousSequenceException(Number wrong, + Number previous, + int index, + MathUtils.Order.Direction direction, + boolean strict) { + super(direction == MathUtils.Order.Direction.INCREASING ? + (strict ? + LocalizedFormats.NOT_STRICTLY_INCREASING_SEQUENCE : + LocalizedFormats.NOT_INCREASING_SEQUENCE) : + (strict ? + LocalizedFormats.NOT_STRICTLY_DECREASING_SEQUENCE : + LocalizedFormats.NOT_DECREASING_SEQUENCE), + wrong, previous, index, index - 1); + + this.direction = direction; + this.strict = strict; + this.index = index; + this.previous = previous; + } + + /** + * @return the order direction. + **/ + public MathUtils.Order.Direction getDirection() { + return direction; + } + /** + * @return {@code true} is the sequence should be strictly monotonous. + **/ + public boolean getStrict() { + return strict; + } + /** + * Get the index of the wrong value. + * + * @return the current index. + */ + public int getIndex() { + return index; + } + /** + * @return the previous value. + */ + public Number getPrevious() { + return previous; + } +} diff --git a/src/main/java/org/apache/commons/math/util/LocalizedFormats.java b/src/main/java/org/apache/commons/math/util/LocalizedFormats.java index 627ec8473..2334d40c3 100644 --- a/src/main/java/org/apache/commons/math/util/LocalizedFormats.java +++ b/src/main/java/org/apache/commons/math/util/LocalizedFormats.java @@ -160,9 +160,11 @@ public enum LocalizedFormats implements Localizable { NORMALIZE_NAN("Cannot normalize to NaN"), NOT_ADDITION_COMPATIBLE_MATRICES("{0}x{1} and {2}x{3} matrices are not addition compatible"), NOT_DECREASING_NUMBER_OF_POINTS("points {0} and {1} are not decreasing ({2} < {3})"), + NOT_DECREASING_SEQUENCE("points {3} and {2} are not decreasing ({1} < {0})"), /* keep */ NOT_ENOUGH_DATA_FOR_NUMBER_OF_PREDICTORS("not enough data ({0} rows) for this many predictors ({1} predictors)"), NOT_ENOUGH_POINTS_IN_SPLINE_PARTITION("spline partition must have at least {0} points, got {1}"), NOT_INCREASING_NUMBER_OF_POINTS("points {0} and {1} are not increasing ({2} > {3})"), + NOT_INCREASING_SEQUENCE("points {3} and {2} are not increasing ({1} > {0})"), /* keep */ NOT_MULTIPLICATION_COMPATIBLE_MATRICES("{0}x{1} and {2}x{3} matrices are not multiplication compatible"), NOT_STRICTLY_POSITIVE("{0} is not strictly positive"), /* keep */ NOT_POSITIVE("{0} is not positive"), /* keep */ @@ -190,13 +192,15 @@ public enum LocalizedFormats implements Localizable { NOT_POWER_OF_TWO_CONSIDER_PADDING("{0} is not a power of 2, consider padding for fix"), NOT_POWER_OF_TWO_PLUS_ONE("{0} is not a power of 2 plus one"), NOT_STRICTLY_DECREASING_NUMBER_OF_POINTS("points {0} and {1} are not strictly decreasing ({2} <= {3})"), + NOT_STRICTLY_DECREASING_SEQUENCE("points {3} and {2} are not strictly decreasing ({1} <= {0})"), /* keep */ NOT_STRICTLY_INCREASING_KNOT_VALUES("knot values must be strictly increasing"), NOT_STRICTLY_INCREASING_NUMBER_OF_POINTS("points {0} and {1} are not strictly increasing ({2} >= {3})"), + NOT_STRICTLY_INCREASING_SEQUENCE("points {3} and {2} are not strictly increasing ({1} >= {0})"), /* keep */ NOT_SUBTRACTION_COMPATIBLE_MATRICES("{0}x{1} and {2}x{3} matrices are not subtraction compatible"), NOT_SYMMETRIC_MATRIX("not symmetric matrix"), NO_BIN_SELECTED("no bin selected"), NO_CONVERGENCE_WITH_ANY_START_POINT("none of the {0} start points lead to convergence"), - NO_DATA("no data"), + NO_DATA("no data"), /* keep */ NO_DEGREES_OF_FREEDOM("no degrees of freedom ({0} measurements, {1} parameters)"), NO_DENSITY_FOR_THIS_DISTRIBUTION("This distribution does not have a density function implemented"), NO_FEASIBLE_SOLUTION("no feasible solution"), diff --git a/src/main/java/org/apache/commons/math/util/MathUtils.java b/src/main/java/org/apache/commons/math/util/MathUtils.java index e57eebafd..711d372bc 100644 --- a/src/main/java/org/apache/commons/math/util/MathUtils.java +++ b/src/main/java/org/apache/commons/math/util/MathUtils.java @@ -22,6 +22,7 @@ import java.math.BigInteger; import java.util.Arrays; import org.apache.commons.math.MathRuntimeException; +import org.apache.commons.math.exception.NonMonotonousSequenceException; /** * Some useful additions to the built-in functions in {@link Math}. @@ -413,7 +414,7 @@ public final class MathUtils { * 3.0, the semantics will change in order to comply with IEEE754 where it * is specified that {@code NaN != NaN}. * New methods have been added for those cases wher the old semantics is - * useful (see e.g. {@link equalsIncludingNaN(double,double) + * useful (see e.g. {@link #equalsIncludingNaN(double,double) * equalsIncludingNaN}. */ public static boolean equals(double x, double y) { @@ -525,7 +526,7 @@ public final class MathUtils { * 3.0, the semantics will change in order to comply with IEEE754 where it * is specified that {@code NaN != NaN}. * New methods have been added for those cases wher the old semantics is - * useful (see e.g. {@link equalsIncludingNaN(double[],double[]) + * useful (see e.g. {@link #equalsIncludingNaN(double[],double[]) * equalsIncludingNaN}. */ public static boolean equals(double[] x, double[] y) { @@ -1866,46 +1867,87 @@ public final class MathUtils { return max; } + public static class Order { + public static enum Direction { + INCREASING, + DECREASING + }; + } + + /** + * Checks that the given array is sorted. + * + * @param val Values. + * @param dir Order direction. + * @param strict Whether the order should be strict. + * @throws NonMonotonousSequenceException if the array is not sorted. + */ + public static void checkOrder(double[] val, Order.Direction dir, boolean strict) { + double previous = val[0]; + boolean ok = true; + + int max = val.length; + for (int i = 1; i < max; i++) { + switch (dir) { + case INCREASING: + if (strict) { + if (val[i] <= previous) { + ok = false; + } + } else { + if (val[i] < previous) { + ok = false; + } + } + break; + case DECREASING: + if (strict) { + if (val[i] >= previous) { + ok = false; + } + } else { + if (val[i] > previous) { + ok = false; + } + } + break; + default: + // Should never happen. + throw new IllegalArgumentException(); + } + + if (!ok) { + throw new NonMonotonousSequenceException(val[i], previous, i, dir, strict); + } + previous = val[i]; + } + } + + /** + * Checks that the given array is sorted in strictly increasing order. + * + * @param val Values. + * @throws NonMonotonousSequenceException if the array is not sorted. + */ + public static void checkOrder(double[] val) { + checkOrder(val, Order.Direction.INCREASING, true); + } + /** * Checks that the given array is sorted. * * @param val Values * @param dir Order direction (-1 for decreasing, 1 for increasing) * @param strict Whether the order should be strict - * @throws IllegalArgumentException if the array is not sorted. + * @throws NonMonotonousSequenceException if the array is not sorted. + * @deprecated as of 2.2 (please use the new {@link #checkOrder(double[],Order.Direction,boolean) + * checkOrder} method). To be removed in 3.0. */ public static void checkOrder(double[] val, int dir, boolean strict) { - double previous = val[0]; - - int max = val.length; - for (int i = 1; i < max; i++) { - if (dir > 0) { - if (strict) { - if (val[i] <= previous) { - throw MathRuntimeException.createIllegalArgumentException(LocalizedFormats.NOT_STRICTLY_INCREASING_NUMBER_OF_POINTS, - i - 1, i, previous, val[i]); - } - } else { - if (val[i] < previous) { - throw MathRuntimeException.createIllegalArgumentException(LocalizedFormats.NOT_INCREASING_NUMBER_OF_POINTS, - i - 1, i, previous, val[i]); - } - } - } else { - if (strict) { - if (val[i] >= previous) { - throw MathRuntimeException.createIllegalArgumentException(LocalizedFormats.NOT_STRICTLY_DECREASING_NUMBER_OF_POINTS, - i - 1, i, previous, val[i]); - } - } else { - if (val[i] > previous) { - throw MathRuntimeException.createIllegalArgumentException(LocalizedFormats.NOT_DECREASING_NUMBER_OF_POINTS, - i - 1, i, previous, val[i]); - } - } - } - - previous = val[i]; + if (dir > 0) { + checkOrder(val, Order.Direction.INCREASING, strict); + } else { + checkOrder(val, Order.Direction.DECREASING, strict); } } } diff --git a/src/main/java/org/apache/commons/math/util/MultidimensionalCounter.java b/src/main/java/org/apache/commons/math/util/MultidimensionalCounter.java index c08bb6b3b..d4967d565 100644 --- a/src/main/java/org/apache/commons/math/util/MultidimensionalCounter.java +++ b/src/main/java/org/apache/commons/math/util/MultidimensionalCounter.java @@ -206,7 +206,7 @@ public class MultidimensionalCounter implements Iterable { * Convert to multidimensional counter. * * @param index Index in unidimensional counter. - * @returns the multidimensional counts. + * @return the multidimensional counts. * @throws {@link OutOfRangeException} if {@code index} is not between * {@code 0} and the value returned by {@link #getSize()} (excluded). */ diff --git a/src/main/resources/META-INF/localization/LocalizedFormats_fr.properties b/src/main/resources/META-INF/localization/LocalizedFormats_fr.properties index 4849d2b30..8f419f35f 100644 --- a/src/main/resources/META-INF/localization/LocalizedFormats_fr.properties +++ b/src/main/resources/META-INF/localization/LocalizedFormats_fr.properties @@ -132,9 +132,11 @@ NORMALIZE_INFINITE = impossible de normaliser vers une valeur infinie NORMALIZE_NAN = impossible de normaliser vers NaN NOT_ADDITION_COMPATIBLE_MATRICES = les dimensions {0}x{1} et {2}x{3} sont incompatibles pour l''addition matricielle NOT_DECREASING_NUMBER_OF_POINTS = les points {0} et {1} ne sont pas d\u00e9croissants ({2} < {3}) +NOT_DECREASING_SEQUENCE = les points {3} et {2} ne sont pas d\u00e9croissants ({1} < {0}) NOT_ENOUGH_DATA_FOR_NUMBER_OF_PREDICTORS = pas assez de donn\u00e9es ({0} lignes) pour {1} pr\u00e9dicteurs NOT_ENOUGH_POINTS_IN_SPLINE_PARTITION = une partiction spline n\u00e9cessite au moins {0} points, seuls {1} ont \u00e9t\u00e9 fournis NOT_INCREASING_NUMBER_OF_POINTS = les points {0} et {1} ne sont pas croissants ({2} > {3}) +NOT_INCREASING_SEQUENCE = les points {3} et {2} ne sont pas croissants ({1} > {0}) NOT_MULTIPLICATION_COMPATIBLE_MATRICES = les dimensions {0}x{1} et {2}x{3} sont incompatibles pour la multiplication matricielle NOT_STRICTLY_POSITIVE = {0} n''est pas strictement positif NOT_POSITIVE = {0} n''est pas positif @@ -162,8 +164,10 @@ NOT_POWER_OF_TWO = {0} n''est pas une puissance de 2 NOT_POWER_OF_TWO_CONSIDER_PADDING = {0} n''est pas une puissance de 2, ajoutez des \u00e9l\u00e9ments pour corriger NOT_POWER_OF_TWO_PLUS_ONE = {0} n''est pas une puissance de 2 plus un NOT_STRICTLY_DECREASING_NUMBER_OF_POINTS = les points {0} et {1} ne sont pas strictement d\u00e9croissants ({2} <= {3}) +NOT_STRICTLY_DECREASING_SEQUENCE = les points {3} et {2} ne sont pas strictement d\u00e9croissants ({1} <= {0}) NOT_STRICTLY_INCREASING_KNOT_VALUES = les n\u0153uds d''interpolation doivent \u00eatre strictement croissants NOT_STRICTLY_INCREASING_NUMBER_OF_POINTS = les points {0} et {1} ne sont pas strictement croissants ({2} >= {3}) +NOT_STRICTLY_INCREASING_SEQUENCE = les points {3} et {2} ne sont pas strictement croissants ({1} >= {0}) NOT_SUBTRACTION_COMPATIBLE_MATRICES = les dimensions {0}x{1} et {2}x{3} sont incompatibles pour la soustraction matricielle NOT_SYMMETRIC_MATRIX = matrice non symm\u00e9trique NO_BIN_SELECTED = aucun compartiment s\u00e9lectionn\u00e9 diff --git a/src/test/java/org/apache/commons/math/analysis/interpolation/SmoothingPolynomialBicubicSplineInterpolatorTest.java b/src/test/java/org/apache/commons/math/analysis/interpolation/SmoothingPolynomialBicubicSplineInterpolatorTest.java index f57b712da..da2c09133 100644 --- a/src/test/java/org/apache/commons/math/analysis/interpolation/SmoothingPolynomialBicubicSplineInterpolatorTest.java +++ b/src/test/java/org/apache/commons/math/analysis/interpolation/SmoothingPolynomialBicubicSplineInterpolatorTest.java @@ -17,7 +17,7 @@ package org.apache.commons.math.analysis.interpolation; import org.apache.commons.math.MathException; -import org.apache.commons.math.DimensionMismatchException; +import org.apache.commons.math.exception.DimensionMismatchException; import org.apache.commons.math.analysis.BivariateRealFunction; import org.junit.Assert; import org.junit.Test; diff --git a/src/test/java/org/apache/commons/math/analysis/interpolation/TricubicSplineInterpolatingFunctionTest.java b/src/test/java/org/apache/commons/math/analysis/interpolation/TricubicSplineInterpolatingFunctionTest.java index de7f9c5a6..02cd017c0 100644 --- a/src/test/java/org/apache/commons/math/analysis/interpolation/TricubicSplineInterpolatingFunctionTest.java +++ b/src/test/java/org/apache/commons/math/analysis/interpolation/TricubicSplineInterpolatingFunctionTest.java @@ -17,7 +17,7 @@ package org.apache.commons.math.analysis.interpolation; import org.apache.commons.math.MathException; -import org.apache.commons.math.DimensionMismatchException; +import org.apache.commons.math.exception.DimensionMismatchException; import org.apache.commons.math.analysis.TrivariateRealFunction; import org.junit.Assert; import org.junit.Test; diff --git a/src/test/java/org/apache/commons/math/analysis/interpolation/TricubicSplineInterpolatorTest.java b/src/test/java/org/apache/commons/math/analysis/interpolation/TricubicSplineInterpolatorTest.java index 747975fa4..cc24cea84 100644 --- a/src/test/java/org/apache/commons/math/analysis/interpolation/TricubicSplineInterpolatorTest.java +++ b/src/test/java/org/apache/commons/math/analysis/interpolation/TricubicSplineInterpolatorTest.java @@ -17,7 +17,7 @@ package org.apache.commons.math.analysis.interpolation; import org.apache.commons.math.MathException; -import org.apache.commons.math.DimensionMismatchException; +import org.apache.commons.math.exception.DimensionMismatchException; import org.apache.commons.math.analysis.TrivariateRealFunction; import org.junit.Assert; import org.junit.Test; diff --git a/src/test/java/org/apache/commons/math/exception/NonMonotonousSequenceExceptionTest.java b/src/test/java/org/apache/commons/math/exception/NonMonotonousSequenceExceptionTest.java new file mode 100644 index 000000000..6ec3de35b --- /dev/null +++ b/src/test/java/org/apache/commons/math/exception/NonMonotonousSequenceExceptionTest.java @@ -0,0 +1,47 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.commons.math.exception; + +import org.apache.commons.math.util.MathUtils; + +import org.junit.Assert; +import org.junit.Test; + +/** + * Test for {@link NonMonotonousSequenceException}. + * + * @version $Revision$ $Date$ + */ +public class NonMonotonousSequenceExceptionTest { + @Test + public void testAccessors() { + NonMonotonousSequenceException e + = new NonMonotonousSequenceException(0, -1, 1, MathUtils.Order.Direction.DECREASING, false); + Assert.assertEquals(0, e.getArgument()); + Assert.assertEquals(-1, e.getPrevious()); + Assert.assertEquals(1, e.getIndex()); + Assert.assertTrue(e.getDirection() == MathUtils.Order.Direction.DECREASING); + Assert.assertFalse(e.getStrict()); + + e = new NonMonotonousSequenceException(-1, 0, 1); + Assert.assertEquals(-1, e.getArgument()); + Assert.assertEquals(0, e.getPrevious()); + Assert.assertEquals(1, e.getIndex()); + Assert.assertTrue(e.getDirection() == MathUtils.Order.Direction.INCREASING); + Assert.assertTrue(e.getStrict()); + } +} diff --git a/src/test/java/org/apache/commons/math/util/MathUtilsTest.java b/src/test/java/org/apache/commons/math/util/MathUtilsTest.java index fb0d03597..83a9290fe 100644 --- a/src/test/java/org/apache/commons/math/util/MathUtilsTest.java +++ b/src/test/java/org/apache/commons/math/util/MathUtilsTest.java @@ -24,6 +24,7 @@ import junit.framework.TestCase; import org.apache.commons.math.TestUtils; import org.apache.commons.math.random.RandomDataImpl; +import org.apache.commons.math.exception.NonMonotonousSequenceException; /** * Test cases for the MathUtils class. @@ -1469,33 +1470,41 @@ public final class MathUtilsTest extends TestCase { } public void testCheckOrder() { - MathUtils.checkOrder(new double[] {-15, -5.5, -1, 2, 15}, 1, true); - MathUtils.checkOrder(new double[] {-15, -5.5, -1, 2, 2}, 1, false); - MathUtils.checkOrder(new double[] {3, -5.5, -11, -27.5}, -1, true); - MathUtils.checkOrder(new double[] {3, 0, 0, -5.5, -11, -27.5}, -1, false); + MathUtils.checkOrder(new double[] {-15, -5.5, -1, 2, 15}, + MathUtils.Order.Direction.INCREASING, true); + MathUtils.checkOrder(new double[] {-15, -5.5, -1, 2, 2}, + MathUtils.Order.Direction.INCREASING, false); + MathUtils.checkOrder(new double[] {3, -5.5, -11, -27.5}, + MathUtils.Order.Direction.DECREASING, true); + MathUtils.checkOrder(new double[] {3, 0, 0, -5.5, -11, -27.5}, + MathUtils.Order.Direction.DECREASING, false); try { - MathUtils.checkOrder(new double[] {-15, -5.5, -1, -1, 2, 15}, 1, true); + MathUtils.checkOrder(new double[] {-15, -5.5, -1, -1, 2, 15}, + MathUtils.Order.Direction.INCREASING, true); fail("an exception should have been thrown"); - } catch (IllegalArgumentException e) { + } catch (NonMonotonousSequenceException e) { // Expected } try { - MathUtils.checkOrder(new double[] {-15, -5.5, -1, -2, 2}, 1, false); + MathUtils.checkOrder(new double[] {-15, -5.5, -1, -2, 2}, + MathUtils.Order.Direction.INCREASING, false); fail("an exception should have been thrown"); - } catch (IllegalArgumentException e) { + } catch (NonMonotonousSequenceException e) { // Expected } try { - MathUtils.checkOrder(new double[] {3, 3, -5.5, -11, -27.5}, -1, true); + MathUtils.checkOrder(new double[] {3, 3, -5.5, -11, -27.5}, + MathUtils.Order.Direction.DECREASING, true); fail("an exception should have been thrown"); - } catch (IllegalArgumentException e) { + } catch (NonMonotonousSequenceException e) { // Expected } try { - MathUtils.checkOrder(new double[] {3, -1, 0, -5.5, -11, -27.5}, -1, false); + MathUtils.checkOrder(new double[] {3, -1, 0, -5.5, -11, -27.5}, + MathUtils.Order.Direction.DECREASING, false); fail("an exception should have been thrown"); - } catch (IllegalArgumentException e) { + } catch (NonMonotonousSequenceException e) { // Expected } }