diff --git a/src/main/java/org/apache/commons/math3/analysis/FunctionUtils.java b/src/main/java/org/apache/commons/math3/analysis/FunctionUtils.java index c83e2e45e..2c1937627 100644 --- a/src/main/java/org/apache/commons/math3/analysis/FunctionUtils.java +++ b/src/main/java/org/apache/commons/math3/analysis/FunctionUtils.java @@ -172,8 +172,11 @@ public class FunctionUtils { return r; } - /** {@inheritDoc} */ - public DerivativeStructure value(final DerivativeStructure t) { + /** {@inheritDoc} + * @throws DimensionMismatchException if functions are not consistent with each other + */ + public DerivativeStructure value(final DerivativeStructure t) + throws DimensionMismatchException { DerivativeStructure r = f[0].value(t); for (int i = 1; i < f.length; i++) { r = r.add(f[i].value(t)); @@ -418,8 +421,8 @@ public class FunctionUtils { * @throws NotStrictlyPositiveException if the number of sample points * {@code n} is negative. */ - public static double[] sample(UnivariateFunction f, - double min, double max, int n) { + public static double[] sample(UnivariateFunction f, double min, double max, int n) + throws NumberIsTooLargeException, NotStrictlyPositiveException { if (n <= 0) { throw new NotStrictlyPositiveException( @@ -609,8 +612,8 @@ public class FunctionUtils { } /** {@inheritDoc} - * @exception DimensionMismatchException if number of parameters or derivation - * order are higher than 1 + * @exception NumberIsTooLargeException if derivation order is higher than 1 + * @exception DimensionMismatchException if numbers of free parameters are inconsistent */ public DerivativeStructure value(final DerivativeStructure[] t) throws DimensionMismatchException, NumberIsTooLargeException { @@ -732,8 +735,8 @@ public class FunctionUtils { } /** {@inheritDoc} - * @exception DimensionMismatchException if number of parameters or derivation - * order are higher than 1 + * @exception NumberIsTooLargeException if derivation order is higher than 1 + * @exception DimensionMismatchException if numbers of free parameters are inconsistent */ public DerivativeStructure[] value(final DerivativeStructure[] t) throws DimensionMismatchException, NumberIsTooLargeException { diff --git a/src/main/java/org/apache/commons/math3/analysis/differentiation/DSCompiler.java b/src/main/java/org/apache/commons/math3/analysis/differentiation/DSCompiler.java index 1018dc8ee..99564a52f 100644 --- a/src/main/java/org/apache/commons/math3/analysis/differentiation/DSCompiler.java +++ b/src/main/java/org/apache/commons/math3/analysis/differentiation/DSCompiler.java @@ -22,6 +22,9 @@ import java.util.List; import java.util.concurrent.atomic.AtomicReference; import org.apache.commons.math3.exception.DimensionMismatchException; +import org.apache.commons.math3.exception.MathArithmeticException; +import org.apache.commons.math3.exception.MathInternalError; +import org.apache.commons.math3.exception.NotPositiveException; import org.apache.commons.math3.exception.NumberIsTooLargeException; import org.apache.commons.math3.util.ArithmeticUtils; import org.apache.commons.math3.util.FastMath; @@ -154,9 +157,11 @@ public class DSCompiler { * @param order derivation order * @param valueCompiler compiler for the value part * @param derivativeCompiler compiler for the derivative part + * @throws NumberIsTooLargeException if order is too large */ private DSCompiler(final int parameters, final int order, - final DSCompiler valueCompiler, final DSCompiler derivativeCompiler) { + final DSCompiler valueCompiler, final DSCompiler derivativeCompiler) + throws NumberIsTooLargeException { this.parameters = parameters; this.order = order; @@ -181,8 +186,10 @@ public class DSCompiler { * @param parameters number of free parameters * @param order derivation order * @return cached rules set + * @throws NumberIsTooLargeException if order is too large */ - public static DSCompiler getCompiler(int parameters, int order) { + public static DSCompiler getCompiler(int parameters, int order) + throws NumberIsTooLargeException { // get the cached compilers final DSCompiler[][] cache = compilers.get(); @@ -400,12 +407,14 @@ public class DSCompiler { * @param sizes sizes array * @param derivativesIndirection derivatives indirection array * @return multiplication indirection array + * @throws NumberIsTooLargeException if order is too large */ private static int[][][] compileCompositionIndirection(final int parameters, final int order, - final DSCompiler valueCompiler, - final DSCompiler derivativeCompiler, - final int[][] sizes, - final int[][] derivativesIndirection) { + final DSCompiler valueCompiler, + final DSCompiler derivativeCompiler, + final int[][] sizes, + final int[][] derivativesIndirection) + throws NumberIsTooLargeException { if ((parameters == 0) || (order == 0)) { return new int[][][] { { { 1, 0 } } }; @@ -596,10 +605,12 @@ public class DSCompiler { * @param destSizes sizes array for the destination derivative structure * @return index of the partial derivative with the same characteristics * in destination derivative structure + * @throws NumberIsTooLargeException if order is too large */ private static int convertIndex(final int index, final int srcP, final int[][] srcDerivativesIndirection, - final int destP, final int destO, final int[][] destSizes) { + final int destP, final int destO, final int[][] destSizes) + throws NumberIsTooLargeException { int[] orders = new int[destP]; System.arraycopy(srcDerivativesIndirection[index], 0, orders, 0, FastMath.min(srcP, destP)); return getPartialDerivativeIndex(destP, destO, destSizes, orders); @@ -1730,15 +1741,23 @@ public class DSCompiler { * @param dsOffset offset of the derivative structure in its array * @param delta parameters offsets (Δx, Δy, ...) * @return value of the Taylor expansion at x + Δx, y + Δy, ... + * @throws MathArithmeticException if factorials becomes too large */ - public double taylor(final double[] ds, final int dsOffset, final double ... delta) { + public double taylor(final double[] ds, final int dsOffset, final double ... delta) + throws MathArithmeticException { double value = 0; for (int i = getSize() - 1; i >= 0; --i) { final int[] orders = getPartialDerivativeOrders(i); double term = ds[dsOffset + i]; for (int k = 0; k < orders.length; ++k) { if (orders[k] > 0) { - term *= FastMath.pow(delta[k], orders[k]) / ArithmeticUtils.factorial(orders[k]); + try { + term *= FastMath.pow(delta[k], orders[k]) / + ArithmeticUtils.factorial(orders[k]); + } catch (NotPositiveException e) { + // this cannot happen + throw new MathInternalError(e); + } } } value += term; diff --git a/src/main/java/org/apache/commons/math3/analysis/differentiation/DerivativeStructure.java b/src/main/java/org/apache/commons/math3/analysis/differentiation/DerivativeStructure.java index dd7f98e42..f26b0eb35 100644 --- a/src/main/java/org/apache/commons/math3/analysis/differentiation/DerivativeStructure.java +++ b/src/main/java/org/apache/commons/math3/analysis/differentiation/DerivativeStructure.java @@ -22,6 +22,7 @@ import org.apache.commons.math3.RealFieldElement; import org.apache.commons.math3.Field; import org.apache.commons.math3.FieldElement; import org.apache.commons.math3.exception.DimensionMismatchException; +import org.apache.commons.math3.exception.MathArithmeticException; import org.apache.commons.math3.exception.NumberIsTooLargeException; import org.apache.commons.math3.util.FastMath; import org.apache.commons.math3.util.MathArrays; @@ -80,8 +81,10 @@ public class DerivativeStructure implements RealFieldElement= 0 && s >= 0) || (m < 0 && s < 0)) { // Sign is currently OK @@ -442,7 +463,10 @@ public class DerivativeStructure implements RealFieldElementx2 +y2) - * @exception DimensionMismatchException if number of free parameters or orders are inconsistent + * @exception DimensionMismatchException if number of free parameters + * or orders do not match */ public static DerivativeStructure hypot(final DerivativeStructure x, final DerivativeStructure y) throws DimensionMismatchException { @@ -515,7 +540,8 @@ public class DerivativeStructure implements RealFieldElementThis interface represents a simple function which computes @@ -35,10 +35,10 @@ public interface UnivariateDifferentiableFunction extends UnivariateFunction { * value and the first derivative of the function.

* @param t function input value * @return function result - * @exception MathIllegalArgumentException if {@code t} does not - * fulfill functions constraints (argument out of bound, or unsupported - * derivative order for example) + * @exception DimensionMismatchException if t is inconsistent with + * function free parameters or order */ - DerivativeStructure value(DerivativeStructure t) throws MathIllegalArgumentException; + DerivativeStructure value(DerivativeStructure t) + throws DimensionMismatchException; } diff --git a/src/main/java/org/apache/commons/math3/analysis/function/Gaussian.java b/src/main/java/org/apache/commons/math3/analysis/function/Gaussian.java index 13c1d0736..08ea43b56 100644 --- a/src/main/java/org/apache/commons/math3/analysis/function/Gaussian.java +++ b/src/main/java/org/apache/commons/math3/analysis/function/Gaussian.java @@ -209,7 +209,8 @@ public class Gaussian implements UnivariateDifferentiableFunction, Differentiabl /** {@inheritDoc} * @since 3.1 */ - public DerivativeStructure value(final DerivativeStructure t) { + public DerivativeStructure value(final DerivativeStructure t) + throws DimensionMismatchException { final double u = is * (t.getValue() - mean); double[] f = new double[t.getOrder() + 1]; diff --git a/src/main/java/org/apache/commons/math3/analysis/function/HarmonicOscillator.java b/src/main/java/org/apache/commons/math3/analysis/function/HarmonicOscillator.java index 85f4267fb..c82b7d49e 100644 --- a/src/main/java/org/apache/commons/math3/analysis/function/HarmonicOscillator.java +++ b/src/main/java/org/apache/commons/math3/analysis/function/HarmonicOscillator.java @@ -162,7 +162,8 @@ public class HarmonicOscillator implements UnivariateDifferentiableFunction, Dif /** {@inheritDoc} * @since 3.1 */ - public DerivativeStructure value(final DerivativeStructure t) { + public DerivativeStructure value(final DerivativeStructure t) + throws DimensionMismatchException { final double x = t.getValue(); double[] f = new double[t.getOrder() + 1]; diff --git a/src/main/java/org/apache/commons/math3/analysis/function/Sigmoid.java b/src/main/java/org/apache/commons/math3/analysis/function/Sigmoid.java index 05129e84d..f9afc4cd0 100644 --- a/src/main/java/org/apache/commons/math3/analysis/function/Sigmoid.java +++ b/src/main/java/org/apache/commons/math3/analysis/function/Sigmoid.java @@ -164,7 +164,8 @@ public class Sigmoid implements UnivariateDifferentiableFunction, Differentiable /** {@inheritDoc} * @since 3.1 */ - public DerivativeStructure value(final DerivativeStructure t) { + public DerivativeStructure value(final DerivativeStructure t) + throws DimensionMismatchException { double[] f = new double[t.getOrder() + 1]; final double exp = FastMath.exp(-t.getValue()); diff --git a/src/main/java/org/apache/commons/math3/analysis/function/Sinc.java b/src/main/java/org/apache/commons/math3/analysis/function/Sinc.java index a8d55604f..feb33a384 100644 --- a/src/main/java/org/apache/commons/math3/analysis/function/Sinc.java +++ b/src/main/java/org/apache/commons/math3/analysis/function/Sinc.java @@ -22,6 +22,7 @@ import org.apache.commons.math3.analysis.FunctionUtils; import org.apache.commons.math3.analysis.UnivariateFunction; import org.apache.commons.math3.analysis.differentiation.DerivativeStructure; import org.apache.commons.math3.analysis.differentiation.UnivariateDifferentiableFunction; +import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.util.FastMath; /** @@ -105,7 +106,8 @@ public class Sinc implements UnivariateDifferentiableFunction, DifferentiableUni /** {@inheritDoc} * @since 3.1 */ - public DerivativeStructure value(final DerivativeStructure t) { + public DerivativeStructure value(final DerivativeStructure t) + throws DimensionMismatchException { final double scaledX = (normalized ? FastMath.PI : 1) * t.getValue(); final double scaledX2 = scaledX * scaledX; diff --git a/src/main/java/org/apache/commons/math3/analysis/function/StepFunction.java b/src/main/java/org/apache/commons/math3/analysis/function/StepFunction.java index f142426aa..85da85779 100644 --- a/src/main/java/org/apache/commons/math3/analysis/function/StepFunction.java +++ b/src/main/java/org/apache/commons/math3/analysis/function/StepFunction.java @@ -20,6 +20,7 @@ package org.apache.commons.math3.analysis.function; import java.util.Arrays; import org.apache.commons.math3.analysis.UnivariateFunction; import org.apache.commons.math3.exception.DimensionMismatchException; +import org.apache.commons.math3.exception.NonMonotonicSequenceException; import org.apache.commons.math3.exception.NullArgumentException; import org.apache.commons.math3.exception.NoDataException; import org.apache.commons.math3.util.MathArrays; @@ -50,7 +51,7 @@ public class StepFunction implements UnivariateFunction { * * @param x Domain values where the function changes value. * @param y Values of the function. - * @throws org.apache.commons.math3.exception.NonMonotonicSequenceException + * @throws NonMonotonicSequenceException * if the {@code x} array is not sorted in strictly increasing order. * @throws NullArgumentException if {@code x} or {@code y} are {@code null}. * @throws NoDataException if {@code x} or {@code y} are zero-length. @@ -59,9 +60,8 @@ public class StepFunction implements UnivariateFunction { */ public StepFunction(double[] x, double[] y) - throws NullArgumentException, - NoDataException, - DimensionMismatchException { + throws NullArgumentException, NoDataException, + DimensionMismatchException, NonMonotonicSequenceException { if (x == null || y == null) { throw new NullArgumentException(); diff --git a/src/main/java/org/apache/commons/math3/analysis/integration/BaseAbstractUnivariateIntegrator.java b/src/main/java/org/apache/commons/math3/analysis/integration/BaseAbstractUnivariateIntegrator.java index adb2d8bb8..7746e3969 100644 --- a/src/main/java/org/apache/commons/math3/analysis/integration/BaseAbstractUnivariateIntegrator.java +++ b/src/main/java/org/apache/commons/math3/analysis/integration/BaseAbstractUnivariateIntegrator.java @@ -107,9 +107,9 @@ public abstract class BaseAbstractUnivariateIntegrator implements UnivariateInte * is lesser than or equal to the minimal number of iterations */ protected BaseAbstractUnivariateIntegrator(final double relativeAccuracy, - final double absoluteAccuracy, - final int minimalIterationCount, - final int maximalIterationCount) + final double absoluteAccuracy, + final int minimalIterationCount, + final int maximalIterationCount) throws NotStrictlyPositiveException, NumberIsTooSmallException { // accuracy settings diff --git a/src/main/java/org/apache/commons/math3/analysis/integration/IterativeLegendreGaussIntegrator.java b/src/main/java/org/apache/commons/math3/analysis/integration/IterativeLegendreGaussIntegrator.java index 8f2c1683e..c17cbf934 100644 --- a/src/main/java/org/apache/commons/math3/analysis/integration/IterativeLegendreGaussIntegrator.java +++ b/src/main/java/org/apache/commons/math3/analysis/integration/IterativeLegendreGaussIntegrator.java @@ -19,10 +19,12 @@ package org.apache.commons.math3.analysis.integration; import org.apache.commons.math3.analysis.UnivariateFunction; import org.apache.commons.math3.analysis.integration.gauss.GaussIntegratorFactory; import org.apache.commons.math3.analysis.integration.gauss.GaussIntegrator; +import org.apache.commons.math3.exception.MathIllegalArgumentException; import org.apache.commons.math3.exception.MaxCountExceededException; import org.apache.commons.math3.exception.NotStrictlyPositiveException; import org.apache.commons.math3.exception.NumberIsTooSmallException; import org.apache.commons.math3.exception.TooManyEvaluationsException; +import org.apache.commons.math3.exception.util.LocalizedFormats; import org.apache.commons.math3.util.FastMath; /** @@ -52,7 +54,7 @@ public class IterativeLegendreGaussIntegrator * @param minimalIterationCount Minimum number of iterations. * @param maximalIterationCount Maximum number of iterations. * @throws NotStrictlyPositiveException if minimal number of iterations - * is not strictly positive. + * or number of points are not strictly positive. * @throws NumberIsTooSmallException if maximal number of iterations * is smaller than or equal to the minimal number of iterations. */ @@ -63,7 +65,10 @@ public class IterativeLegendreGaussIntegrator final int maximalIterationCount) throws NotStrictlyPositiveException, NumberIsTooSmallException { super(relativeAccuracy, absoluteAccuracy, minimalIterationCount, maximalIterationCount); - numberOfPoints = n; + if (n <= 0) { + throw new NotStrictlyPositiveException(LocalizedFormats.NUMBER_OF_POINTS, n); + } + numberOfPoints = n; } /** @@ -72,10 +77,12 @@ public class IterativeLegendreGaussIntegrator * @param n Number of integration points. * @param relativeAccuracy Relative accuracy of the result. * @param absoluteAccuracy Absolute accuracy of the result. + * @throws NotStrictlyPositiveException if {@code n < 1}. */ public IterativeLegendreGaussIntegrator(final int n, final double relativeAccuracy, - final double absoluteAccuracy) { + final double absoluteAccuracy) + throws NotStrictlyPositiveException { this(n, relativeAccuracy, absoluteAccuracy, DEFAULT_MIN_ITERATIONS_COUNT, DEFAULT_MAX_ITERATIONS_COUNT); } @@ -90,10 +97,12 @@ public class IterativeLegendreGaussIntegrator * is not strictly positive. * @throws NumberIsTooSmallException if maximal number of iterations * is smaller than or equal to the minimal number of iterations. + * @throws NotStrictlyPositiveException if {@code n < 1}. */ public IterativeLegendreGaussIntegrator(final int n, final int minimalIterationCount, - final int maximalIterationCount) { + final int maximalIterationCount) + throws NotStrictlyPositiveException, NumberIsTooSmallException { this(n, DEFAULT_RELATIVE_ACCURACY, DEFAULT_ABSOLUTE_ACCURACY, minimalIterationCount, maximalIterationCount); } @@ -101,7 +110,7 @@ public class IterativeLegendreGaussIntegrator /** {@inheritDoc} */ @Override protected double doIntegrate() - throws TooManyEvaluationsException, MaxCountExceededException { + throws MathIllegalArgumentException, TooManyEvaluationsException, MaxCountExceededException { // Compute first estimate with a single step. double oldt = stage(1); @@ -142,7 +151,8 @@ public class IterativeLegendreGaussIntegrator throws TooManyEvaluationsException { // Function to be integrated is stored in the base class. final UnivariateFunction f = new UnivariateFunction() { - public double value(double x) { + public double value(double x) + throws MathIllegalArgumentException, TooManyEvaluationsException { return computeObjectiveValue(x); } }; diff --git a/src/main/java/org/apache/commons/math3/analysis/integration/LegendreGaussIntegrator.java b/src/main/java/org/apache/commons/math3/analysis/integration/LegendreGaussIntegrator.java index c8a9d87ab..c594fb447 100644 --- a/src/main/java/org/apache/commons/math3/analysis/integration/LegendreGaussIntegrator.java +++ b/src/main/java/org/apache/commons/math3/analysis/integration/LegendreGaussIntegrator.java @@ -128,6 +128,7 @@ public class LegendreGaussIntegrator extends BaseAbstractUnivariateIntegrator { * @param absoluteAccuracy absolute accuracy of the result * @param minimalIterationCount minimum number of iterations * @param maximalIterationCount maximum number of iterations + * @exception MathIllegalArgumentException if number of points is out of [2; 5] * @exception NotStrictlyPositiveException if minimal number of iterations * is not strictly positive * @exception NumberIsTooSmallException if maximal number of iterations @@ -138,7 +139,7 @@ public class LegendreGaussIntegrator extends BaseAbstractUnivariateIntegrator { final double absoluteAccuracy, final int minimalIterationCount, final int maximalIterationCount) - throws NotStrictlyPositiveException, NumberIsTooSmallException { + throws MathIllegalArgumentException, NotStrictlyPositiveException, NumberIsTooSmallException { super(relativeAccuracy, absoluteAccuracy, minimalIterationCount, maximalIterationCount); switch(n) { case 2 : @@ -170,10 +171,12 @@ public class LegendreGaussIntegrator extends BaseAbstractUnivariateIntegrator { * @param n number of points desired (must be between 2 and 5 inclusive) * @param relativeAccuracy relative accuracy of the result * @param absoluteAccuracy absolute accuracy of the result + * @exception MathIllegalArgumentException if number of points is out of [2; 5] */ public LegendreGaussIntegrator(final int n, final double relativeAccuracy, - final double absoluteAccuracy) { + final double absoluteAccuracy) + throws MathIllegalArgumentException { this(n, relativeAccuracy, absoluteAccuracy, DEFAULT_MIN_ITERATIONS_COUNT, DEFAULT_MAX_ITERATIONS_COUNT); } @@ -183,6 +186,7 @@ public class LegendreGaussIntegrator extends BaseAbstractUnivariateIntegrator { * @param n number of points desired (must be between 2 and 5 inclusive) * @param minimalIterationCount minimum number of iterations * @param maximalIterationCount maximum number of iterations + * @exception MathIllegalArgumentException if number of points is out of [2; 5] * @exception NotStrictlyPositiveException if minimal number of iterations * is not strictly positive * @exception NumberIsTooSmallException if maximal number of iterations @@ -190,7 +194,8 @@ public class LegendreGaussIntegrator extends BaseAbstractUnivariateIntegrator { */ public LegendreGaussIntegrator(final int n, final int minimalIterationCount, - final int maximalIterationCount) { + final int maximalIterationCount) + throws MathIllegalArgumentException { this(n, DEFAULT_RELATIVE_ACCURACY, DEFAULT_ABSOLUTE_ACCURACY, minimalIterationCount, maximalIterationCount); } @@ -198,7 +203,7 @@ public class LegendreGaussIntegrator extends BaseAbstractUnivariateIntegrator { /** {@inheritDoc} */ @Override protected double doIntegrate() - throws TooManyEvaluationsException, MaxCountExceededException { + throws MathIllegalArgumentException, TooManyEvaluationsException, MaxCountExceededException { // compute first estimate with a single step double oldt = stage(1); diff --git a/src/main/java/org/apache/commons/math3/analysis/integration/TrapezoidIntegrator.java b/src/main/java/org/apache/commons/math3/analysis/integration/TrapezoidIntegrator.java index c3eafa4a9..d5b73bed9 100644 --- a/src/main/java/org/apache/commons/math3/analysis/integration/TrapezoidIntegrator.java +++ b/src/main/java/org/apache/commons/math3/analysis/integration/TrapezoidIntegrator.java @@ -16,6 +16,7 @@ */ package org.apache.commons.math3.analysis.integration; +import org.apache.commons.math3.exception.MathIllegalArgumentException; import org.apache.commons.math3.exception.MaxCountExceededException; import org.apache.commons.math3.exception.NotStrictlyPositiveException; import org.apache.commons.math3.exception.NumberIsTooLargeException; @@ -144,7 +145,7 @@ public class TrapezoidIntegrator extends BaseAbstractUnivariateIntegrator { /** {@inheritDoc} */ @Override protected double doIntegrate() - throws TooManyEvaluationsException, MaxCountExceededException { + throws MathIllegalArgumentException, TooManyEvaluationsException, MaxCountExceededException { double oldt = stage(this, 0); iterations.incrementCount(); diff --git a/src/main/java/org/apache/commons/math3/analysis/integration/gauss/BaseRuleFactory.java b/src/main/java/org/apache/commons/math3/analysis/integration/gauss/BaseRuleFactory.java index 0262eff2b..37a3b5e99 100644 --- a/src/main/java/org/apache/commons/math3/analysis/integration/gauss/BaseRuleFactory.java +++ b/src/main/java/org/apache/commons/math3/analysis/integration/gauss/BaseRuleFactory.java @@ -21,6 +21,7 @@ import java.util.TreeMap; import org.apache.commons.math3.util.Pair; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.exception.NotStrictlyPositiveException; +import org.apache.commons.math3.exception.util.LocalizedFormats; /** * Base class for rules that determines the integration nodes and their @@ -48,9 +49,17 @@ public abstract class BaseRuleFactory { * @param numberOfPoints Number of integration points. * @return a copy of the integration rule. * @throws NotStrictlyPositiveException if {@code numberOfPoints < 1}. + * @throws DimensionMismatchException if the elements of the rule pair do not + * have the same length. */ public Pair getRule(int numberOfPoints) - throws NotStrictlyPositiveException { + throws NotStrictlyPositiveException, DimensionMismatchException { + + if (numberOfPoints <= 0) { + throw new NotStrictlyPositiveException(LocalizedFormats.NUMBER_OF_POINTS, + numberOfPoints); + } + // Try to obtain the rule from the cache. Pair cached = pointsAndWeightsDouble.get(numberOfPoints); @@ -78,10 +87,11 @@ public abstract class BaseRuleFactory { * * @param numberOfPoints Order of the rule to be retrieved. * @return the points and weights corresponding to the given order. - * @throws NotStrictlyPositiveException if {@code numberOfPoints < 1}. + * @throws DimensionMismatchException if the elements of the rule pair do not + * have the same length. */ protected synchronized Pair getRuleInternal(int numberOfPoints) - throws NotStrictlyPositiveException { + throws DimensionMismatchException { final Pair rule = pointsAndWeights.get(numberOfPoints); if (rule == null) { addRule(computeRule(numberOfPoints)); @@ -98,7 +108,7 @@ public abstract class BaseRuleFactory { * @throws DimensionMismatchException if the elements of the pair do not * have the same length. */ - protected void addRule(Pair rule) { + protected void addRule(Pair rule) throws DimensionMismatchException { if (rule.getFirst().length != rule.getSecond().length) { throw new DimensionMismatchException(rule.getFirst().length, rule.getSecond().length); @@ -112,8 +122,11 @@ public abstract class BaseRuleFactory { * * @param numberOfPoints Order of the rule to be computed. * @return the computed rule. + * @throws DimensionMismatchException if the elements of the pair do not + * have the same length. */ - protected abstract Pair computeRule(int numberOfPoints); + protected abstract Pair computeRule(int numberOfPoints) + throws DimensionMismatchException; /** * Converts the from the actual {@code Number} type to {@code double} diff --git a/src/main/java/org/apache/commons/math3/analysis/integration/gauss/GaussIntegrator.java b/src/main/java/org/apache/commons/math3/analysis/integration/gauss/GaussIntegrator.java index 6f8aa17bd..feeffaa6d 100644 --- a/src/main/java/org/apache/commons/math3/analysis/integration/gauss/GaussIntegrator.java +++ b/src/main/java/org/apache/commons/math3/analysis/integration/gauss/GaussIntegrator.java @@ -45,10 +45,11 @@ public class GaussIntegrator { * @param weights Weights of the corresponding integration nodes. * @throws NonMonotonicSequenceException if the {@code points} are not * sorted in increasing order. + * @throws DimensionMismatchException if points and weights don't have the same length */ public GaussIntegrator(double[] points, double[] weights) - throws NonMonotonicSequenceException { + throws NonMonotonicSequenceException, DimensionMismatchException { if (points.length != weights.length) { throw new DimensionMismatchException(points.length, weights.length); diff --git a/src/main/java/org/apache/commons/math3/analysis/integration/gauss/GaussIntegratorFactory.java b/src/main/java/org/apache/commons/math3/analysis/integration/gauss/GaussIntegratorFactory.java index df75ccf1a..c9a5acbee 100644 --- a/src/main/java/org/apache/commons/math3/analysis/integration/gauss/GaussIntegratorFactory.java +++ b/src/main/java/org/apache/commons/math3/analysis/integration/gauss/GaussIntegratorFactory.java @@ -18,6 +18,8 @@ package org.apache.commons.math3.analysis.integration.gauss; import java.math.BigDecimal; +import org.apache.commons.math3.exception.DimensionMismatchException; +import org.apache.commons.math3.exception.NotStrictlyPositiveException; import org.apache.commons.math3.util.Pair; /** @@ -55,10 +57,12 @@ public class GaussIntegratorFactory { * @param lowerBound Lower bound of the integration interval. * @param upperBound Upper bound of the integration interval. * @return a Gauss-Legendre integrator. + * @throws NotStrictlyPositiveException if number of points is not positive */ public GaussIntegrator legendre(int numberOfPoints, double lowerBound, - double upperBound) { + double upperBound) + throws NotStrictlyPositiveException { return new GaussIntegrator(transform(getRule(legendre, numberOfPoints), lowerBound, upperBound)); } @@ -71,8 +75,10 @@ public class GaussIntegratorFactory { * * @param numberOfPoints Order of the integration rule. * @return a Gauss-Legendre integrator. + * @throws NotStrictlyPositiveException if number of points is not positive */ - public GaussIntegrator legendreHighPrecision(int numberOfPoints) { + public GaussIntegrator legendreHighPrecision(int numberOfPoints) + throws NotStrictlyPositiveException { return new GaussIntegrator(getRule(legendreHighPrecision, numberOfPoints)); } @@ -85,10 +91,12 @@ public class GaussIntegratorFactory { * @param lowerBound Lower bound of the integration interval. * @param upperBound Upper bound of the integration interval. * @return a Gauss-Legendre integrator. + * @throws NotStrictlyPositiveException if number of points is not positive */ public GaussIntegrator legendreHighPrecision(int numberOfPoints, double lowerBound, - double upperBound) { + double upperBound) + throws NotStrictlyPositiveException { return new GaussIntegrator(transform(getRule(legendreHighPrecision, numberOfPoints), lowerBound, upperBound)); } @@ -97,9 +105,13 @@ public class GaussIntegratorFactory { * @param factory Integration rule factory. * @param numberOfPoints Order of the integration rule. * @return the integration nodes and weights. + * @throws NotStrictlyPositiveException if number of points is not positive + * @throws DimensionMismatchException if the elements of the rule pair do not + * have the same length. */ private static Pair getRule(BaseRuleFactory factory, - int numberOfPoints) { + int numberOfPoints) + throws NotStrictlyPositiveException, DimensionMismatchException { return factory.getRule(numberOfPoints); } diff --git a/src/main/java/org/apache/commons/math3/analysis/integration/gauss/LegendreHighPrecisionRuleFactory.java b/src/main/java/org/apache/commons/math3/analysis/integration/gauss/LegendreHighPrecisionRuleFactory.java index ef40e0d90..cbfa95dde 100644 --- a/src/main/java/org/apache/commons/math3/analysis/integration/gauss/LegendreHighPrecisionRuleFactory.java +++ b/src/main/java/org/apache/commons/math3/analysis/integration/gauss/LegendreHighPrecisionRuleFactory.java @@ -16,11 +16,11 @@ */ package org.apache.commons.math3.analysis.integration.gauss; -import java.math.MathContext; import java.math.BigDecimal; +import java.math.MathContext; + +import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.util.Pair; -import org.apache.commons.math3.exception.NotStrictlyPositiveException; -import org.apache.commons.math3.exception.util.LocalizedFormats; /** * Factory that creates Gauss-type quadrature rule using Legendre polynomials. @@ -60,17 +60,10 @@ public class LegendreHighPrecisionRuleFactory extends BaseRuleFactory computeRule(int numberOfPoints) { - if (numberOfPoints <= 0) { - throw new NotStrictlyPositiveException(LocalizedFormats.NUMBER_OF_POINTS, - numberOfPoints); - } + protected Pair computeRule(int numberOfPoints) + throws DimensionMismatchException { if (numberOfPoints == 1) { // Break recursion. diff --git a/src/main/java/org/apache/commons/math3/analysis/integration/gauss/LegendreRuleFactory.java b/src/main/java/org/apache/commons/math3/analysis/integration/gauss/LegendreRuleFactory.java index 339af1189..1916b92ee 100644 --- a/src/main/java/org/apache/commons/math3/analysis/integration/gauss/LegendreRuleFactory.java +++ b/src/main/java/org/apache/commons/math3/analysis/integration/gauss/LegendreRuleFactory.java @@ -16,9 +16,8 @@ */ package org.apache.commons.math3.analysis.integration.gauss; +import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.util.Pair; -import org.apache.commons.math3.exception.NotStrictlyPositiveException; -import org.apache.commons.math3.exception.util.LocalizedFormats; /** * Factory that creates Gauss-type quadrature rule using Legendre polynomials. @@ -32,16 +31,10 @@ import org.apache.commons.math3.exception.util.LocalizedFormats; * @version $Id$ */ public class LegendreRuleFactory extends BaseRuleFactory { - /** - * {@inheritDoc} - */ + /** {@inheritDoc} */ @Override protected Pair computeRule(int numberOfPoints) - throws NotStrictlyPositiveException { - if (numberOfPoints <= 0) { - throw new NotStrictlyPositiveException(LocalizedFormats.NUMBER_OF_POINTS, - numberOfPoints); - } + throws DimensionMismatchException { if (numberOfPoints == 1) { // Break recursion. diff --git a/src/main/java/org/apache/commons/math3/analysis/interpolation/BicubicSplineInterpolator.java b/src/main/java/org/apache/commons/math3/analysis/interpolation/BicubicSplineInterpolator.java index 0b5be8842..9465c2cdc 100644 --- a/src/main/java/org/apache/commons/math3/analysis/interpolation/BicubicSplineInterpolator.java +++ b/src/main/java/org/apache/commons/math3/analysis/interpolation/BicubicSplineInterpolator.java @@ -21,6 +21,7 @@ import org.apache.commons.math3.analysis.polynomials.PolynomialSplineFunction; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.exception.NoDataException; import org.apache.commons.math3.exception.NonMonotonicSequenceException; +import org.apache.commons.math3.exception.NumberIsTooSmallException; import org.apache.commons.math3.util.MathArrays; /** @@ -37,9 +38,8 @@ public class BicubicSplineInterpolator public BicubicSplineInterpolatingFunction interpolate(final double[] xval, final double[] yval, final double[][] fval) - throws NoDataException, - DimensionMismatchException, - NonMonotonicSequenceException { + throws NoDataException, DimensionMismatchException, + NonMonotonicSequenceException, NumberIsTooSmallException { if (xval.length == 0 || yval.length == 0 || fval.length == 0) { throw new NoDataException(); } diff --git a/src/main/java/org/apache/commons/math3/analysis/interpolation/BivariateGridInterpolator.java b/src/main/java/org/apache/commons/math3/analysis/interpolation/BivariateGridInterpolator.java index 318aed8b8..0ba1b90d5 100644 --- a/src/main/java/org/apache/commons/math3/analysis/interpolation/BivariateGridInterpolator.java +++ b/src/main/java/org/apache/commons/math3/analysis/interpolation/BivariateGridInterpolator.java @@ -19,6 +19,8 @@ package org.apache.commons.math3.analysis.interpolation; import org.apache.commons.math3.analysis.BivariateFunction; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.exception.NoDataException; +import org.apache.commons.math3.exception.NonMonotonicSequenceException; +import org.apache.commons.math3.exception.NumberIsTooSmallException; /** * Interface representing a bivariate real interpolating function where the @@ -39,9 +41,12 @@ public interface BivariateGridInterpolator { * @return a function which interpolates the dataset. * @throws NoDataException if any of the arrays has zero length. * @throws DimensionMismatchException if the array lengths are inconsistent. + * @throws NonMonotonicSequenceException if the array is not sorted. + * @throws NumberIsTooSmallException if the number of points is too small for + * the order of the interpolation */ BivariateFunction interpolate(double[] xval, double[] yval, double[][] fval) - throws NoDataException, - DimensionMismatchException; + throws NoDataException, DimensionMismatchException, + NonMonotonicSequenceException, NumberIsTooSmallException; } diff --git a/src/main/java/org/apache/commons/math3/analysis/interpolation/FieldHermiteInterpolator.java b/src/main/java/org/apache/commons/math3/analysis/interpolation/FieldHermiteInterpolator.java index ed8dc30b6..14199eb14 100644 --- a/src/main/java/org/apache/commons/math3/analysis/interpolation/FieldHermiteInterpolator.java +++ b/src/main/java/org/apache/commons/math3/analysis/interpolation/FieldHermiteInterpolator.java @@ -20,11 +20,14 @@ import java.util.ArrayList; import java.util.List; import org.apache.commons.math3.FieldElement; +import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.exception.MathArithmeticException; import org.apache.commons.math3.exception.NoDataException; +import org.apache.commons.math3.exception.NullArgumentException; import org.apache.commons.math3.exception.ZeroException; import org.apache.commons.math3.exception.util.LocalizedFormats; import org.apache.commons.math3.util.MathArrays; +import org.apache.commons.math3.util.MathUtils; /** Polynomial interpolator using both sample values and sample derivatives. *

@@ -82,10 +85,14 @@ public class FieldHermiteInterpolator> { * and a previous point is zero (i.e. the two points are at same abscissa) * @exception MathArithmeticException if the number of derivatives is larger * than 20, which prevents computation of a factorial + * @throws DimensionMismatchException if derivative structures are inconsistent + * @throws NullArgumentException if x is null */ public void addSamplePoint(final T x, final T[] ... value) - throws ZeroException, MathArithmeticException { + throws ZeroException, MathArithmeticException, + DimensionMismatchException, NullArgumentException { + MathUtils.checkNotNull(x); T factorial = x.getField().getOne(); for (int i = 0; i < value.length; ++i) { @@ -128,10 +135,12 @@ public class FieldHermiteInterpolator> { * @param x interpolation abscissa * @return interpolated value * @exception NoDataException if sample is empty + * @throws NullArgumentException if x is null */ - public T[] value(T x) throws NoDataException { + public T[] value(T x) throws NoDataException, NullArgumentException { // safety check + MathUtils.checkNotNull(x); if (abscissae.isEmpty()) { throw new NoDataException(LocalizedFormats.EMPTY_INTERPOLATION_SAMPLE); } @@ -157,10 +166,12 @@ public class FieldHermiteInterpolator> { * @return interpolated value and derivatives (value in row 0, * 1st derivative in row 1, ... nth derivative in row n) * @exception NoDataException if sample is empty + * @throws NullArgumentException if x is null */ - public T[][] derivatives(T x, int order) throws NoDataException { + public T[][] derivatives(T x, int order) throws NoDataException, NullArgumentException { // safety check + MathUtils.checkNotNull(x); if (abscissae.isEmpty()) { throw new NoDataException(LocalizedFormats.EMPTY_INTERPOLATION_SAMPLE); } diff --git a/src/main/java/org/apache/commons/math3/analysis/interpolation/MicrosphereInterpolatingFunction.java b/src/main/java/org/apache/commons/math3/analysis/interpolation/MicrosphereInterpolatingFunction.java index a8ee17697..2139e6800 100644 --- a/src/main/java/org/apache/commons/math3/analysis/interpolation/MicrosphereInterpolatingFunction.java +++ b/src/main/java/org/apache/commons/math3/analysis/interpolation/MicrosphereInterpolatingFunction.java @@ -193,8 +193,9 @@ public class MicrosphereInterpolatingFunction /** * @param point Interpolation point. * @return the interpolated value. + * @throws DimensionMismatchException if point dimension does not math sample */ - public double value(double[] point) { + public double value(double[] point) throws DimensionMismatchException { final RealVector p = new ArrayRealVector(point); // Reset. diff --git a/src/main/java/org/apache/commons/math3/analysis/interpolation/MultivariateInterpolator.java b/src/main/java/org/apache/commons/math3/analysis/interpolation/MultivariateInterpolator.java index 39239567f..04c6b0bdc 100644 --- a/src/main/java/org/apache/commons/math3/analysis/interpolation/MultivariateInterpolator.java +++ b/src/main/java/org/apache/commons/math3/analysis/interpolation/MultivariateInterpolator.java @@ -17,6 +17,10 @@ package org.apache.commons.math3.analysis.interpolation; import org.apache.commons.math3.analysis.MultivariateFunction; +import org.apache.commons.math3.exception.DimensionMismatchException; +import org.apache.commons.math3.exception.MathIllegalArgumentException; +import org.apache.commons.math3.exception.NoDataException; +import org.apache.commons.math3.exception.NullArgumentException; /** * Interface representing a univariate real interpolating function. @@ -36,15 +40,13 @@ public interface MultivariateInterpolator { * point (where {@code d} is thus the dimension of the space). * @param yval the values for the interpolation points * @return a function which interpolates the data set - * @throws org.apache.commons.math3.exception.MathIllegalArgumentException - * if the arguments violate assumptions made by the interpolation - * algorithm. - * @throws org.apache.commons.math3.exception.DimensionMismatchException - * when the array dimensions are not consistent. - * @throws org.apache.commons.math3.exception.NoDataException if an - * array has zero-length. - * @throws org.apache.commons.math3.exception.NullArgumentException if - * the arguments are {@code null}. + * @throws MathIllegalArgumentException if the arguments violate assumptions + * made by the interpolation algorithm. + * @throws DimensionMismatchException when the array dimensions are not consistent. + * @throws NoDataException if an array has zero-length. + * @throws NullArgumentException if the arguments are {@code null}. */ - MultivariateFunction interpolate(double[][] xval, double[] yval); + MultivariateFunction interpolate(double[][] xval, double[] yval) + throws MathIllegalArgumentException, DimensionMismatchException, + NoDataException, NullArgumentException; } diff --git a/src/main/java/org/apache/commons/math3/analysis/interpolation/SmoothingPolynomialBicubicSplineInterpolator.java b/src/main/java/org/apache/commons/math3/analysis/interpolation/SmoothingPolynomialBicubicSplineInterpolator.java index 785cf1a83..0c62e897a 100644 --- a/src/main/java/org/apache/commons/math3/analysis/interpolation/SmoothingPolynomialBicubicSplineInterpolator.java +++ b/src/main/java/org/apache/commons/math3/analysis/interpolation/SmoothingPolynomialBicubicSplineInterpolator.java @@ -18,7 +18,9 @@ package org.apache.commons.math3.analysis.interpolation; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.exception.NoDataException; +import org.apache.commons.math3.exception.NonMonotonicSequenceException; import org.apache.commons.math3.exception.NotPositiveException; +import org.apache.commons.math3.exception.NullArgumentException; import org.apache.commons.math3.util.MathArrays; import org.apache.commons.math3.util.Precision; import org.apache.commons.math3.optim.nonlinear.vector.jacobian.GaussNewtonOptimizer; @@ -54,8 +56,10 @@ public class SmoothingPolynomialBicubicSplineInterpolator /** * @param degree Degree of the polynomial fitting functions. + * @exception NotPositiveException if degree is not positive */ - public SmoothingPolynomialBicubicSplineInterpolator(int degree) { + public SmoothingPolynomialBicubicSplineInterpolator(int degree) + throws NotPositiveException { this(degree, degree); } @@ -64,9 +68,10 @@ public class SmoothingPolynomialBicubicSplineInterpolator * x-dimension. * @param yDegree Degree of the polynomial fitting functions along the * y-dimension. + * @exception NotPositiveException if degrees are not positive */ - public SmoothingPolynomialBicubicSplineInterpolator(int xDegree, - int yDegree) { + public SmoothingPolynomialBicubicSplineInterpolator(int xDegree, int yDegree) + throws NotPositiveException { if (xDegree < 0) { throw new NotPositiveException(xDegree); } @@ -91,8 +96,8 @@ public class SmoothingPolynomialBicubicSplineInterpolator public BicubicSplineInterpolatingFunction interpolate(final double[] xval, final double[] yval, final double[][] fval) - throws NoDataException, - DimensionMismatchException { + throws NoDataException, NullArgumentException, + DimensionMismatchException, NonMonotonicSequenceException { if (xval.length == 0 || yval.length == 0 || fval.length == 0) { throw new NoDataException(); } diff --git a/src/main/java/org/apache/commons/math3/analysis/interpolation/TricubicSplineInterpolator.java b/src/main/java/org/apache/commons/math3/analysis/interpolation/TricubicSplineInterpolator.java index 386b0cf10..0fce8577a 100644 --- a/src/main/java/org/apache/commons/math3/analysis/interpolation/TricubicSplineInterpolator.java +++ b/src/main/java/org/apache/commons/math3/analysis/interpolation/TricubicSplineInterpolator.java @@ -19,6 +19,7 @@ package org.apache.commons.math3.analysis.interpolation; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.exception.NoDataException; import org.apache.commons.math3.exception.NonMonotonicSequenceException; +import org.apache.commons.math3.exception.NumberIsTooSmallException; import org.apache.commons.math3.util.MathArrays; /** @@ -36,9 +37,8 @@ public class TricubicSplineInterpolator final double[] yval, final double[] zval, final double[][][] fval) - throws NoDataException, - DimensionMismatchException, - NonMonotonicSequenceException { + throws NoDataException, NumberIsTooSmallException, + DimensionMismatchException, NonMonotonicSequenceException { if (xval.length == 0 || yval.length == 0 || zval.length == 0 || fval.length == 0) { throw new NoDataException(); } diff --git a/src/main/java/org/apache/commons/math3/analysis/interpolation/TrivariateGridInterpolator.java b/src/main/java/org/apache/commons/math3/analysis/interpolation/TrivariateGridInterpolator.java index ec81fc722..bc040fc75 100644 --- a/src/main/java/org/apache/commons/math3/analysis/interpolation/TrivariateGridInterpolator.java +++ b/src/main/java/org/apache/commons/math3/analysis/interpolation/TrivariateGridInterpolator.java @@ -19,6 +19,8 @@ package org.apache.commons.math3.analysis.interpolation; import org.apache.commons.math3.analysis.TrivariateFunction; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.exception.NoDataException; +import org.apache.commons.math3.exception.NonMonotonicSequenceException; +import org.apache.commons.math3.exception.NumberIsTooSmallException; /** * Interface representing a trivariate real interpolating function where the @@ -42,9 +44,12 @@ public interface TrivariateGridInterpolator { * @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 NonMonotonicSequenceException if arrays are not sorted + * @throws NumberIsTooSmallException if the number of points is too small for + * the order of the interpolation */ TrivariateFunction interpolate(double[] xval, double[] yval, double[] zval, double[][][] fval) - throws NoDataException, - DimensionMismatchException; + throws NoDataException, NumberIsTooSmallException, + DimensionMismatchException, NonMonotonicSequenceException; } diff --git a/src/main/java/org/apache/commons/math3/analysis/interpolation/UnivariateInterpolator.java b/src/main/java/org/apache/commons/math3/analysis/interpolation/UnivariateInterpolator.java index e32acd64b..0edc98040 100644 --- a/src/main/java/org/apache/commons/math3/analysis/interpolation/UnivariateInterpolator.java +++ b/src/main/java/org/apache/commons/math3/analysis/interpolation/UnivariateInterpolator.java @@ -17,6 +17,8 @@ package org.apache.commons.math3.analysis.interpolation; import org.apache.commons.math3.analysis.UnivariateFunction; +import org.apache.commons.math3.exception.DimensionMismatchException; +import org.apache.commons.math3.exception.MathIllegalArgumentException; /** * Interface representing a univariate real interpolating function. @@ -30,9 +32,11 @@ public interface UnivariateInterpolator { * @param xval Arguments for the interpolation points. * @param yval Values for the interpolation points. * @return a function which interpolates the dataset. - * @throws org.apache.commons.math3.exception.MathIllegalArgumentException + * @throws MathIllegalArgumentException * if the arguments violate assumptions made by the interpolation * algorithm. + * @throws DimensionMismatchException if arrays lengthes do not match */ - UnivariateFunction interpolate(double xval[], double yval[]); + UnivariateFunction interpolate(double xval[], double yval[]) + throws MathIllegalArgumentException, DimensionMismatchException; } diff --git a/src/main/java/org/apache/commons/math3/analysis/interpolation/UnivariatePeriodicInterpolator.java b/src/main/java/org/apache/commons/math3/analysis/interpolation/UnivariatePeriodicInterpolator.java index 5fecb4cb8..585df2915 100644 --- a/src/main/java/org/apache/commons/math3/analysis/interpolation/UnivariatePeriodicInterpolator.java +++ b/src/main/java/org/apache/commons/math3/analysis/interpolation/UnivariatePeriodicInterpolator.java @@ -19,6 +19,8 @@ package org.apache.commons.math3.analysis.interpolation; import org.apache.commons.math3.analysis.UnivariateFunction; import org.apache.commons.math3.util.MathUtils; import org.apache.commons.math3.util.MathArrays; +import org.apache.commons.math3.exception.MathIllegalArgumentException; +import org.apache.commons.math3.exception.NonMonotonicSequenceException; import org.apache.commons.math3.exception.NumberIsTooSmallException; /** @@ -82,7 +84,7 @@ public class UnivariatePeriodicInterpolator */ public UnivariateFunction interpolate(double[] xval, double[] yval) - throws NumberIsTooSmallException { + throws NumberIsTooSmallException, NonMonotonicSequenceException { if (xval.length < extend) { throw new NumberIsTooSmallException(xval.length, extend, true); } @@ -114,7 +116,7 @@ public class UnivariatePeriodicInterpolator final UnivariateFunction f = interpolator.interpolate(x, y); return new UnivariateFunction() { - public double value(final double x) { + public double value(final double x) throws MathIllegalArgumentException { return f.value(MathUtils.reduce(x, period, offset)); } }; diff --git a/src/main/java/org/apache/commons/math3/analysis/polynomials/PolynomialFunction.java b/src/main/java/org/apache/commons/math3/analysis/polynomials/PolynomialFunction.java index 2a5d079f2..f00799d9c 100644 --- a/src/main/java/org/apache/commons/math3/analysis/polynomials/PolynomialFunction.java +++ b/src/main/java/org/apache/commons/math3/analysis/polynomials/PolynomialFunction.java @@ -405,7 +405,8 @@ public class PolynomialFunction implements UnivariateDifferentiableFunction, Dif } /** {@inheritDoc} */ - public double value(final double x, final double ... parameters) { + public double value(final double x, final double ... parameters) + throws NoDataException { return PolynomialFunction.evaluate(parameters, x); } } diff --git a/src/main/java/org/apache/commons/math3/analysis/polynomials/PolynomialFunctionLagrangeForm.java b/src/main/java/org/apache/commons/math3/analysis/polynomials/PolynomialFunctionLagrangeForm.java index aa05628a6..315c3ddf1 100644 --- a/src/main/java/org/apache/commons/math3/analysis/polynomials/PolynomialFunctionLagrangeForm.java +++ b/src/main/java/org/apache/commons/math3/analysis/polynomials/PolynomialFunctionLagrangeForm.java @@ -20,6 +20,7 @@ import org.apache.commons.math3.analysis.UnivariateFunction; import org.apache.commons.math3.util.FastMath; import org.apache.commons.math3.util.MathArrays; import org.apache.commons.math3.exception.DimensionMismatchException; +import org.apache.commons.math3.exception.NonMonotonicSequenceException; import org.apache.commons.math3.exception.NumberIsTooSmallException; import org.apache.commons.math3.exception.util.LocalizedFormats; @@ -65,10 +66,11 @@ public class PolynomialFunctionLagrangeForm implements UnivariateFunction { * @param y function values at interpolating points * @throws DimensionMismatchException if the array lengths are different. * @throws NumberIsTooSmallException if the number of points is less than 2. - * @throws org.apache.commons.math3.exception.NonMonotonicSequenceException + * @throws NonMonotonicSequenceException * if two abscissae have the same value. */ - public PolynomialFunctionLagrangeForm(double x[], double y[]) { + public PolynomialFunctionLagrangeForm(double x[], double y[]) + throws DimensionMismatchException, NumberIsTooSmallException, NonMonotonicSequenceException { this.x = new double[x.length]; this.y = new double[y.length]; System.arraycopy(x, 0, this.x, 0, x.length); @@ -163,12 +165,13 @@ public class PolynomialFunctionLagrangeForm implements UnivariateFunction { * @return the function value. * @throws DimensionMismatchException if {@code x} and {@code y} have * different lengths. - * @throws org.apache.commons.math3.exception.NonMonotonicSequenceException + * @throws NonMonotonicSequenceException * if {@code x} is not sorted in strictly increasing order. * @throws NumberIsTooSmallException if the size of {@code x} is less * than 2. */ - public static double evaluate(double x[], double y[], double z) { + public static double evaluate(double x[], double y[], double z) + throws DimensionMismatchException, NumberIsTooSmallException, NonMonotonicSequenceException { if (verifyInterpolationArray(x, y, false)) { return evaluateInternal(x, y, z); } @@ -310,7 +313,8 @@ public class PolynomialFunctionLagrangeForm implements UnivariateFunction { * @see #evaluate(double[], double[], double) * @see #computeCoefficients() */ - public static boolean verifyInterpolationArray(double x[], double y[], boolean abort) { + public static boolean verifyInterpolationArray(double x[], double y[], boolean abort) + throws DimensionMismatchException, NumberIsTooSmallException, NonMonotonicSequenceException { if (x.length != y.length) { throw new DimensionMismatchException(x.length, y.length); } diff --git a/src/main/java/org/apache/commons/math3/analysis/polynomials/PolynomialFunctionNewtonForm.java b/src/main/java/org/apache/commons/math3/analysis/polynomials/PolynomialFunctionNewtonForm.java index 0db046413..4042a2d51 100644 --- a/src/main/java/org/apache/commons/math3/analysis/polynomials/PolynomialFunctionNewtonForm.java +++ b/src/main/java/org/apache/commons/math3/analysis/polynomials/PolynomialFunctionNewtonForm.java @@ -20,7 +20,9 @@ import org.apache.commons.math3.analysis.differentiation.DerivativeStructure; import org.apache.commons.math3.analysis.differentiation.UnivariateDifferentiableFunction; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.exception.NoDataException; +import org.apache.commons.math3.exception.NullArgumentException; import org.apache.commons.math3.exception.util.LocalizedFormats; +import org.apache.commons.math3.util.MathUtils; /** * Implements the representation of a real polynomial function in @@ -69,13 +71,13 @@ public class PolynomialFunctionNewtonForm implements UnivariateDifferentiableFun * * @param a Coefficients in Newton form formula. * @param c Centers. - * @throws org.apache.commons.math3.exception.NullArgumentException if - * any argument is {@code null}. + * @throws NullArgumentException if any argument is {@code null}. * @throws NoDataException if any array has zero length. * @throws DimensionMismatchException if the size difference between * {@code a} and {@code c} is not equal to 1. */ - public PolynomialFunctionNewtonForm(double a[], double c[]) { + public PolynomialFunctionNewtonForm(double a[], double c[]) + throws NullArgumentException, NoDataException, DimensionMismatchException { verifyInputArray(a, c); this.a = new double[a.length]; @@ -172,13 +174,13 @@ public class PolynomialFunctionNewtonForm implements UnivariateDifferentiableFun * @param c Centers. * @param z Point at which the function value is to be computed. * @return the function value. - * @throws org.apache.commons.math3.exception.NullArgumentException if - * any argument is {@code null}. + * @throws NullArgumentException if any argument is {@code null}. * @throws NoDataException if any array has zero length. * @throws DimensionMismatchException if the size difference between * {@code a} and {@code c} is not equal to 1. */ - public static double evaluate(double a[], double c[], double z) { + public static double evaluate(double a[], double c[], double z) + throws NullArgumentException, DimensionMismatchException, NoDataException { verifyInputArray(a, c); final int n = c.length; @@ -221,17 +223,18 @@ public class PolynomialFunctionNewtonForm implements UnivariateDifferentiableFun * * @param a the coefficients in Newton form formula * @param c the centers - * @throws org.apache.commons.math3.exception.NullArgumentException if - * any argument is {@code null}. + * @throws NullArgumentException if any argument is {@code null}. * @throws NoDataException if any array has zero length. * @throws DimensionMismatchException if the size difference between * {@code a} and {@code c} is not equal to 1. * @see org.apache.commons.math3.analysis.interpolation.DividedDifferenceInterpolator#computeDividedDifference(double[], * double[]) */ - protected static void verifyInputArray(double a[], double c[]) { - if (a.length == 0 || - c.length == 0) { + protected static void verifyInputArray(double a[], double c[]) + throws NullArgumentException, NoDataException, DimensionMismatchException { + MathUtils.checkNotNull(a); + MathUtils.checkNotNull(c); + if (a.length == 0 || c.length == 0) { throw new NoDataException(LocalizedFormats.EMPTY_POLYNOMIALS_COEFFICIENTS_ARRAY); } if (a.length != c.length + 1) { diff --git a/src/main/java/org/apache/commons/math3/analysis/polynomials/PolynomialSplineFunction.java b/src/main/java/org/apache/commons/math3/analysis/polynomials/PolynomialSplineFunction.java index 875fdc0b3..78906c947 100644 --- a/src/main/java/org/apache/commons/math3/analysis/polynomials/PolynomialSplineFunction.java +++ b/src/main/java/org/apache/commons/math3/analysis/polynomials/PolynomialSplineFunction.java @@ -23,6 +23,7 @@ import org.apache.commons.math3.analysis.DifferentiableUnivariateFunction; import org.apache.commons.math3.analysis.UnivariateFunction; import org.apache.commons.math3.analysis.differentiation.DerivativeStructure; import org.apache.commons.math3.analysis.differentiation.UnivariateDifferentiableFunction; +import org.apache.commons.math3.exception.NonMonotonicSequenceException; import org.apache.commons.math3.exception.OutOfRangeException; import org.apache.commons.math3.exception.NumberIsTooSmallException; import org.apache.commons.math3.exception.DimensionMismatchException; @@ -95,11 +96,12 @@ public class PolynomialSplineFunction implements UnivariateDifferentiableFunctio * @throws NullArgumentException if either of the input arrays is {@code null}. * @throws NumberIsTooSmallException if knots has length less than 2. * @throws DimensionMismatchException if {@code polynomials.length != knots.length - 1}. - * @throws org.apache.commons.math3.exception.NonMonotonicSequenceException if - * the {@code knots} array is not strictly increasing. + * @throws NonMonotonicSequenceException if the {@code knots} array is not strictly increasing. * */ - public PolynomialSplineFunction(double knots[], PolynomialFunction polynomials[]) { + public PolynomialSplineFunction(double knots[], PolynomialFunction polynomials[]) + throws NullArgumentException, NumberIsTooSmallException, + DimensionMismatchException, NonMonotonicSequenceException{ if (knots == null || polynomials == null) { throw new NullArgumentException(); diff --git a/src/main/java/org/apache/commons/math3/analysis/solvers/AbstractDifferentiableUnivariateSolver.java b/src/main/java/org/apache/commons/math3/analysis/solvers/AbstractDifferentiableUnivariateSolver.java index 8dbcbe8c0..91869c5ff 100644 --- a/src/main/java/org/apache/commons/math3/analysis/solvers/AbstractDifferentiableUnivariateSolver.java +++ b/src/main/java/org/apache/commons/math3/analysis/solvers/AbstractDifferentiableUnivariateSolver.java @@ -63,8 +63,7 @@ public abstract class AbstractDifferentiableUnivariateSolver * * @param point Point at which the objective function must be evaluated. * @return the objective function value at specified point. - * @throws org.apache.commons.math3.exception.TooManyEvaluationsException - * if the maximal number of evaluations is exceeded. + * @throws TooManyEvaluationsException if the maximal number of evaluations is exceeded. */ protected double computeDerivativeObjectiveValue(double point) throws TooManyEvaluationsException { diff --git a/src/main/java/org/apache/commons/math3/analysis/solvers/AbstractUnivariateDifferentiableSolver.java b/src/main/java/org/apache/commons/math3/analysis/solvers/AbstractUnivariateDifferentiableSolver.java index 4b44da3f8..c9fd38d3f 100644 --- a/src/main/java/org/apache/commons/math3/analysis/solvers/AbstractUnivariateDifferentiableSolver.java +++ b/src/main/java/org/apache/commons/math3/analysis/solvers/AbstractUnivariateDifferentiableSolver.java @@ -19,6 +19,7 @@ package org.apache.commons.math3.analysis.solvers; import org.apache.commons.math3.analysis.differentiation.DerivativeStructure; import org.apache.commons.math3.analysis.differentiation.UnivariateDifferentiableFunction; +import org.apache.commons.math3.exception.TooManyEvaluationsException; /** * Provide a default implementation for several functions useful to generic @@ -61,10 +62,11 @@ public abstract class AbstractUnivariateDifferentiableSolver * * @param point Point at which the objective function must be evaluated. * @return the objective function value and derivative at specified point. - * @throws org.apache.commons.math3.exception.TooManyEvaluationsException + * @throws TooManyEvaluationsException * if the maximal number of evaluations is exceeded. */ - protected DerivativeStructure computeObjectiveValueAndDerivative(double point) { + protected DerivativeStructure computeObjectiveValueAndDerivative(double point) + throws TooManyEvaluationsException { incrementEvaluationCount(); return function.value(new DerivativeStructure(1, 1, 0, point)); } diff --git a/src/main/java/org/apache/commons/math3/analysis/solvers/BaseAbstractUnivariateSolver.java b/src/main/java/org/apache/commons/math3/analysis/solvers/BaseAbstractUnivariateSolver.java index 2f2afeb0f..253fcf012 100644 --- a/src/main/java/org/apache/commons/math3/analysis/solvers/BaseAbstractUnivariateSolver.java +++ b/src/main/java/org/apache/commons/math3/analysis/solvers/BaseAbstractUnivariateSolver.java @@ -166,11 +166,13 @@ public abstract class BaseAbstractUnivariateSolver { * @param min Lower bound for the interval. * @param max Upper bound for the interval. * @return a value where the function is zero. - * @throws org.apache.commons.math3.exception.MathIllegalArgumentException + * @throws MathIllegalArgumentException * if the arguments do not satisfy the requirements specified by the solver. - * @throws org.apache.commons.math3.exception.TooManyEvaluationsException if + * @throws TooManyEvaluationsException if * the allowed number of evaluations is exceeded. */ - double solve(int maxEval, FUNC f, double min, double max); + double solve(int maxEval, FUNC f, double min, double max) + throws MathIllegalArgumentException, TooManyEvaluationsException; /** * Solve for a zero in the given interval, start at {@code startValue}. @@ -116,12 +119,13 @@ public interface BaseUnivariateSolver { * @param max Upper bound for the interval. * @param startValue Start value to use. * @return a value where the function is zero. - * @throws org.apache.commons.math3.exception.MathIllegalArgumentException + * @throws MathIllegalArgumentException * if the arguments do not satisfy the requirements specified by the solver. - * @throws org.apache.commons.math3.exception.TooManyEvaluationsException if + * @throws TooManyEvaluationsException if * the allowed number of evaluations is exceeded. */ - double solve(int maxEval, FUNC f, double min, double max, double startValue); + double solve(int maxEval, FUNC f, double min, double max, double startValue) + throws MathIllegalArgumentException, TooManyEvaluationsException; /** * Solve for a zero in the vicinity of {@code startValue}. diff --git a/src/main/java/org/apache/commons/math3/util/Incrementor.java b/src/main/java/org/apache/commons/math3/util/Incrementor.java index 79e695ee4..8f798e8f0 100644 --- a/src/main/java/org/apache/commons/math3/util/Incrementor.java +++ b/src/main/java/org/apache/commons/math3/util/Incrementor.java @@ -62,7 +62,7 @@ public class Incrementor { this(max, new MaxCountExceededCallback() { /** {@inheritDoc} */ - public void trigger(int max) { + public void trigger(int max) throws MaxCountExceededException { throw new MaxCountExceededException(max); } }); @@ -76,8 +76,8 @@ public class Incrementor { * @param cb Function to be called when the maximal count has been reached. * @throws NullArgumentException if {@code cb} is {@code null} */ - public Incrementor(int max, - MaxCountExceededCallback cb) { + public Incrementor(int max, MaxCountExceededCallback cb) + throws NullArgumentException { if (cb == null){ throw new NullArgumentException(); } @@ -132,7 +132,7 @@ public class Incrementor { * @param value Number of increments. * @throws MaxCountExceededException at counter exhaustion. */ - public void incrementCount(int value) { + public void incrementCount(int value) throws MaxCountExceededException { for (int i = 0; i < value; i++) { incrementCount(); } @@ -151,7 +151,7 @@ public class Incrementor { * custom {@link MaxCountExceededCallback callback} has been set at * construction. */ - public void incrementCount() { + public void incrementCount() throws MaxCountExceededException { if (++count > maximalCount) { maxCountCallback.trigger(maximalCount); } @@ -173,7 +173,8 @@ public class Incrementor { * Function called when the maximal count has been reached. * * @param maximalCount Maximal count. + * @throws MaxCountExceededException at counter exhaustion */ - void trigger(int maximalCount); + void trigger(int maximalCount) throws MaxCountExceededException; } } diff --git a/src/main/java/org/apache/commons/math3/util/MathArrays.java b/src/main/java/org/apache/commons/math3/util/MathArrays.java index 045a9cd86..0ca75e94c 100644 --- a/src/main/java/org/apache/commons/math3/util/MathArrays.java +++ b/src/main/java/org/apache/commons/math3/util/MathArrays.java @@ -83,8 +83,8 @@ public class MathArrays { * @throws DimensionMismatchException if the array lengths differ. * @since 3.1 */ - public static double[] ebeAdd(double[] a, - double[] b) { + public static double[] ebeAdd(double[] a, double[] b) + throws DimensionMismatchException { if (a.length != b.length) { throw new DimensionMismatchException(a.length, b.length); } @@ -105,8 +105,8 @@ public class MathArrays { * @throws DimensionMismatchException if the array lengths differ. * @since 3.1 */ - public static double[] ebeSubtract(double[] a, - double[] b) { + public static double[] ebeSubtract(double[] a, double[] b) + throws DimensionMismatchException { if (a.length != b.length) { throw new DimensionMismatchException(a.length, b.length); } @@ -127,8 +127,8 @@ public class MathArrays { * @throws DimensionMismatchException if the array lengths differ. * @since 3.1 */ - public static double[] ebeMultiply(double[] a, - double[] b) { + public static double[] ebeMultiply(double[] a, double[] b) + throws DimensionMismatchException { if (a.length != b.length) { throw new DimensionMismatchException(a.length, b.length); } @@ -149,8 +149,8 @@ public class MathArrays { * @throws DimensionMismatchException if the array lengths differ. * @since 3.1 */ - public static double[] ebeDivide(double[] a, - double[] b) { + public static double[] ebeDivide(double[] a, double[] b) + throws DimensionMismatchException { if (a.length != b.length) { throw new DimensionMismatchException(a.length, b.length); } @@ -323,9 +323,7 @@ public class MathArrays { * @param strict Whether the order should be strict. * @return {@code true} if sorted, {@code false} otherwise. */ - public static boolean isMonotonic(double[] val, - OrderDirection dir, - boolean strict) { + public static boolean isMonotonic(double[] val, OrderDirection dir, boolean strict) { return checkOrder(val, dir, strict, false); }