Filled throws clauses for the analysis package.

JIRA: MATH-854

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/trunk@1455194 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Luc Maisonobe 2013-03-11 15:45:54 +00:00
parent 3ad5595706
commit 6f3165670c
41 changed files with 357 additions and 193 deletions

View File

@ -172,8 +172,11 @@ public class FunctionUtils {
return r; return r;
} }
/** {@inheritDoc} */ /** {@inheritDoc}
public DerivativeStructure value(final DerivativeStructure t) { * @throws DimensionMismatchException if functions are not consistent with each other
*/
public DerivativeStructure value(final DerivativeStructure t)
throws DimensionMismatchException {
DerivativeStructure r = f[0].value(t); DerivativeStructure r = f[0].value(t);
for (int i = 1; i < f.length; i++) { for (int i = 1; i < f.length; i++) {
r = r.add(f[i].value(t)); r = r.add(f[i].value(t));
@ -418,8 +421,8 @@ public class FunctionUtils {
* @throws NotStrictlyPositiveException if the number of sample points * @throws NotStrictlyPositiveException if the number of sample points
* {@code n} is negative. * {@code n} is negative.
*/ */
public static double[] sample(UnivariateFunction f, public static double[] sample(UnivariateFunction f, double min, double max, int n)
double min, double max, int n) { throws NumberIsTooLargeException, NotStrictlyPositiveException {
if (n <= 0) { if (n <= 0) {
throw new NotStrictlyPositiveException( throw new NotStrictlyPositiveException(
@ -609,8 +612,8 @@ public class FunctionUtils {
} }
/** {@inheritDoc} /** {@inheritDoc}
* @exception DimensionMismatchException if number of parameters or derivation * @exception NumberIsTooLargeException if derivation order is higher than 1
* order are higher than 1 * @exception DimensionMismatchException if numbers of free parameters are inconsistent
*/ */
public DerivativeStructure value(final DerivativeStructure[] t) public DerivativeStructure value(final DerivativeStructure[] t)
throws DimensionMismatchException, NumberIsTooLargeException { throws DimensionMismatchException, NumberIsTooLargeException {
@ -732,8 +735,8 @@ public class FunctionUtils {
} }
/** {@inheritDoc} /** {@inheritDoc}
* @exception DimensionMismatchException if number of parameters or derivation * @exception NumberIsTooLargeException if derivation order is higher than 1
* order are higher than 1 * @exception DimensionMismatchException if numbers of free parameters are inconsistent
*/ */
public DerivativeStructure[] value(final DerivativeStructure[] t) public DerivativeStructure[] value(final DerivativeStructure[] t)
throws DimensionMismatchException, NumberIsTooLargeException { throws DimensionMismatchException, NumberIsTooLargeException {

View File

@ -22,6 +22,9 @@ import java.util.List;
import java.util.concurrent.atomic.AtomicReference; import java.util.concurrent.atomic.AtomicReference;
import org.apache.commons.math3.exception.DimensionMismatchException; 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.exception.NumberIsTooLargeException;
import org.apache.commons.math3.util.ArithmeticUtils; import org.apache.commons.math3.util.ArithmeticUtils;
import org.apache.commons.math3.util.FastMath; import org.apache.commons.math3.util.FastMath;
@ -154,9 +157,11 @@ public class DSCompiler {
* @param order derivation order * @param order derivation order
* @param valueCompiler compiler for the value part * @param valueCompiler compiler for the value part
* @param derivativeCompiler compiler for the derivative part * @param derivativeCompiler compiler for the derivative part
* @throws NumberIsTooLargeException if order is too large
*/ */
private DSCompiler(final int parameters, final int order, 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.parameters = parameters;
this.order = order; this.order = order;
@ -181,8 +186,10 @@ public class DSCompiler {
* @param parameters number of free parameters * @param parameters number of free parameters
* @param order derivation order * @param order derivation order
* @return cached rules set * @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 // get the cached compilers
final DSCompiler[][] cache = compilers.get(); final DSCompiler[][] cache = compilers.get();
@ -400,12 +407,14 @@ public class DSCompiler {
* @param sizes sizes array * @param sizes sizes array
* @param derivativesIndirection derivatives indirection array * @param derivativesIndirection derivatives indirection array
* @return multiplication indirection array * @return multiplication indirection array
* @throws NumberIsTooLargeException if order is too large
*/ */
private static int[][][] compileCompositionIndirection(final int parameters, final int order, private static int[][][] compileCompositionIndirection(final int parameters, final int order,
final DSCompiler valueCompiler, final DSCompiler valueCompiler,
final DSCompiler derivativeCompiler, final DSCompiler derivativeCompiler,
final int[][] sizes, final int[][] sizes,
final int[][] derivativesIndirection) { final int[][] derivativesIndirection)
throws NumberIsTooLargeException {
if ((parameters == 0) || (order == 0)) { if ((parameters == 0) || (order == 0)) {
return new int[][][] { { { 1, 0 } } }; return new int[][][] { { { 1, 0 } } };
@ -596,10 +605,12 @@ public class DSCompiler {
* @param destSizes sizes array for the destination derivative structure * @param destSizes sizes array for the destination derivative structure
* @return index of the partial derivative with the <em>same</em> characteristics * @return index of the partial derivative with the <em>same</em> characteristics
* in destination derivative structure * in destination derivative structure
* @throws NumberIsTooLargeException if order is too large
*/ */
private static int convertIndex(final int index, private static int convertIndex(final int index,
final int srcP, final int[][] srcDerivativesIndirection, 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]; int[] orders = new int[destP];
System.arraycopy(srcDerivativesIndirection[index], 0, orders, 0, FastMath.min(srcP, destP)); System.arraycopy(srcDerivativesIndirection[index], 0, orders, 0, FastMath.min(srcP, destP));
return getPartialDerivativeIndex(destP, destO, destSizes, orders); return getPartialDerivativeIndex(destP, destO, destSizes, orders);
@ -1730,15 +1741,23 @@ public class DSCompiler {
* @param dsOffset offset of the derivative structure in its array * @param dsOffset offset of the derivative structure in its array
* @param delta parameters offsets (&Delta;x, &Delta;y, ...) * @param delta parameters offsets (&Delta;x, &Delta;y, ...)
* @return value of the Taylor expansion at x + &Delta;x, y + &Delta;y, ... * @return value of the Taylor expansion at x + &Delta;x, y + &Delta;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; double value = 0;
for (int i = getSize() - 1; i >= 0; --i) { for (int i = getSize() - 1; i >= 0; --i) {
final int[] orders = getPartialDerivativeOrders(i); final int[] orders = getPartialDerivativeOrders(i);
double term = ds[dsOffset + i]; double term = ds[dsOffset + i];
for (int k = 0; k < orders.length; ++k) { for (int k = 0; k < orders.length; ++k) {
if (orders[k] > 0) { 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; value += term;

View File

@ -22,6 +22,7 @@ import org.apache.commons.math3.RealFieldElement;
import org.apache.commons.math3.Field; import org.apache.commons.math3.Field;
import org.apache.commons.math3.FieldElement; import org.apache.commons.math3.FieldElement;
import org.apache.commons.math3.exception.DimensionMismatchException; 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.exception.NumberIsTooLargeException;
import org.apache.commons.math3.util.FastMath; import org.apache.commons.math3.util.FastMath;
import org.apache.commons.math3.util.MathArrays; import org.apache.commons.math3.util.MathArrays;
@ -80,8 +81,10 @@ public class DerivativeStructure implements RealFieldElement<DerivativeStructure
/** Build an instance with all values and derivatives set to 0. /** Build an instance with all values and derivatives set to 0.
* @param parameters number of free parameters * @param parameters number of free parameters
* @param order derivation order * @param order derivation order
* @throws NumberIsTooLargeException if order is too large
*/ */
public DerivativeStructure(final int parameters, final int order) { public DerivativeStructure(final int parameters, final int order)
throws NumberIsTooLargeException {
this(DSCompiler.getCompiler(parameters, order)); this(DSCompiler.getCompiler(parameters, order));
} }
@ -89,9 +92,11 @@ public class DerivativeStructure implements RealFieldElement<DerivativeStructure
* @param parameters number of free parameters * @param parameters number of free parameters
* @param order derivation order * @param order derivation order
* @param value value of the constant * @param value value of the constant
* @throws NumberIsTooLargeException if order is too large
* @see #DerivativeStructure(int, int, int, double) * @see #DerivativeStructure(int, int, int, double)
*/ */
public DerivativeStructure(final int parameters, final int order, final double value) { public DerivativeStructure(final int parameters, final int order, final double value)
throws NumberIsTooLargeException {
this(parameters, order); this(parameters, order);
this.data[0] = value; this.data[0] = value;
} }
@ -193,10 +198,11 @@ public class DerivativeStructure implements RealFieldElement<DerivativeStructure
* {@link DSCompiler#getPartialDerivativeIndex(int...)} * {@link DSCompiler#getPartialDerivativeIndex(int...)}
* @exception DimensionMismatchException if derivatives array does not match the * @exception DimensionMismatchException if derivatives array does not match the
* {@link DSCompiler#getSize() size} expected by the compiler * {@link DSCompiler#getSize() size} expected by the compiler
* @throws NumberIsTooLargeException if order is too large
* @see #getAllDerivatives() * @see #getAllDerivatives()
*/ */
public DerivativeStructure(final int parameters, final int order, final double ... derivatives) public DerivativeStructure(final int parameters, final int order, final double ... derivatives)
throws DimensionMismatchException { throws DimensionMismatchException, NumberIsTooLargeException {
this(parameters, order); this(parameters, order);
if (derivatives.length != data.length) { if (derivatives.length != data.length) {
throw new DimensionMismatchException(derivatives.length, data.length); throw new DimensionMismatchException(derivatives.length, data.length);
@ -269,7 +275,10 @@ public class DerivativeStructure implements RealFieldElement<DerivativeStructure
return ds; return ds;
} }
/** {@inheritDoc} */ /** {@inheritDoc}
* @exception DimensionMismatchException if number of free parameters
* or orders do not match
*/
public DerivativeStructure add(final DerivativeStructure a) public DerivativeStructure add(final DerivativeStructure a)
throws DimensionMismatchException { throws DimensionMismatchException {
compiler.checkCompatibility(a.compiler); compiler.checkCompatibility(a.compiler);
@ -283,7 +292,10 @@ public class DerivativeStructure implements RealFieldElement<DerivativeStructure
return add(-a); return add(-a);
} }
/** {@inheritDoc} */ /** {@inheritDoc}
* @exception DimensionMismatchException if number of free parameters
* or orders do not match
*/
public DerivativeStructure subtract(final DerivativeStructure a) public DerivativeStructure subtract(final DerivativeStructure a)
throws DimensionMismatchException { throws DimensionMismatchException {
compiler.checkCompatibility(a.compiler); compiler.checkCompatibility(a.compiler);
@ -306,7 +318,10 @@ public class DerivativeStructure implements RealFieldElement<DerivativeStructure
return ds; return ds;
} }
/** {@inheritDoc} */ /** {@inheritDoc}
* @exception DimensionMismatchException if number of free parameters
* or orders do not match
*/
public DerivativeStructure multiply(final DerivativeStructure a) public DerivativeStructure multiply(final DerivativeStructure a)
throws DimensionMismatchException { throws DimensionMismatchException {
compiler.checkCompatibility(a.compiler); compiler.checkCompatibility(a.compiler);
@ -324,7 +339,10 @@ public class DerivativeStructure implements RealFieldElement<DerivativeStructure
return ds; return ds;
} }
/** {@inheritDoc} */ /** {@inheritDoc}
* @exception DimensionMismatchException if number of free parameters
* or orders do not match
*/
public DerivativeStructure divide(final DerivativeStructure a) public DerivativeStructure divide(final DerivativeStructure a)
throws DimensionMismatchException { throws DimensionMismatchException {
compiler.checkCompatibility(a.compiler); compiler.checkCompatibility(a.compiler);
@ -340,7 +358,10 @@ public class DerivativeStructure implements RealFieldElement<DerivativeStructure
return ds; return ds;
} }
/** {@inheritDoc} */ /** {@inheritDoc}
* @exception DimensionMismatchException if number of free parameters
* or orders do not match
*/
public DerivativeStructure remainder(final DerivativeStructure a) public DerivativeStructure remainder(final DerivativeStructure a)
throws DimensionMismatchException { throws DimensionMismatchException {
compiler.checkCompatibility(a.compiler); compiler.checkCompatibility(a.compiler);
@ -442,7 +463,10 @@ public class DerivativeStructure implements RealFieldElement<DerivativeStructure
return ds; return ds;
} }
/** {@inheritDoc} */ /** {@inheritDoc}
* @exception DimensionMismatchException if number of free parameters
* or orders do not match
*/
public DerivativeStructure hypot(final DerivativeStructure y) public DerivativeStructure hypot(final DerivativeStructure y)
throws DimensionMismatchException { throws DimensionMismatchException {
@ -500,7 +524,8 @@ public class DerivativeStructure implements RealFieldElement<DerivativeStructure
* @param x a value * @param x a value
* @param y a value * @param y a value
* @return sqrt(<i>x</i><sup>2</sup>&nbsp;+<i>y</i><sup>2</sup>) * @return sqrt(<i>x</i><sup>2</sup>&nbsp;+<i>y</i><sup>2</sup>)
* @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) public static DerivativeStructure hypot(final DerivativeStructure x, final DerivativeStructure y)
throws DimensionMismatchException { throws DimensionMismatchException {
@ -515,7 +540,8 @@ public class DerivativeStructure implements RealFieldElement<DerivativeStructure
* @exception DimensionMismatchException if the number of derivatives * @exception DimensionMismatchException if the number of derivatives
* in the array is not equal to {@link #getOrder() order} + 1 * in the array is not equal to {@link #getOrder() order} + 1
*/ */
public DerivativeStructure compose(final double ... f) { public DerivativeStructure compose(final double ... f)
throws DimensionMismatchException {
if (f.length != getOrder() + 1) { if (f.length != getOrder() + 1) {
throw new DimensionMismatchException(f.length, getOrder() + 1); throw new DimensionMismatchException(f.length, getOrder() + 1);
} }
@ -584,7 +610,10 @@ public class DerivativeStructure implements RealFieldElement<DerivativeStructure
return result; return result;
} }
/** {@inheritDoc} */ /** {@inheritDoc}
* @exception DimensionMismatchException if number of free parameters
* or orders do not match
*/
public DerivativeStructure pow(final DerivativeStructure e) public DerivativeStructure pow(final DerivativeStructure e)
throws DimensionMismatchException { throws DimensionMismatchException {
compiler.checkCompatibility(e.compiler); compiler.checkCompatibility(e.compiler);
@ -685,7 +714,8 @@ public class DerivativeStructure implements RealFieldElement<DerivativeStructure
* @param y first argument of the arc tangent * @param y first argument of the arc tangent
* @param x second argument of the arc tangent * @param x second argument of the arc tangent
* @return atan2(y, x) * @return atan2(y, x)
* @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 atan2(final DerivativeStructure y, final DerivativeStructure x) public static DerivativeStructure atan2(final DerivativeStructure y, final DerivativeStructure x)
throws DimensionMismatchException { throws DimensionMismatchException {
@ -759,12 +789,16 @@ public class DerivativeStructure implements RealFieldElement<DerivativeStructure
/** Evaluate Taylor expansion a derivative structure. /** Evaluate Taylor expansion a derivative structure.
* @param delta parameters offsets (&Delta;x, &Delta;y, ...) * @param delta parameters offsets (&Delta;x, &Delta;y, ...)
* @return value of the Taylor expansion at x + &Delta;x, y + &Delta;y, ... * @return value of the Taylor expansion at x + &Delta;x, y + &Delta;y, ...
* @throws MathArithmeticException if factorials becomes too large
*/ */
public double taylor(final double ... delta) { public double taylor(final double ... delta) throws MathArithmeticException {
return compiler.taylor(data, 0, delta); return compiler.taylor(data, 0, delta);
} }
/** {@inheritDoc} */ /** {@inheritDoc}
* @exception DimensionMismatchException if number of free parameters
* or orders do not match
*/
public DerivativeStructure linearCombination(final DerivativeStructure[] a, final DerivativeStructure[] b) public DerivativeStructure linearCombination(final DerivativeStructure[] a, final DerivativeStructure[] b)
throws DimensionMismatchException { throws DimensionMismatchException {
@ -792,7 +826,10 @@ public class DerivativeStructure implements RealFieldElement<DerivativeStructure
} }
/** {@inheritDoc} */ /** {@inheritDoc}
* @exception DimensionMismatchException if number of free parameters
* or orders do not match
*/
public DerivativeStructure linearCombination(final double[] a, final DerivativeStructure[] b) public DerivativeStructure linearCombination(final double[] a, final DerivativeStructure[] b)
throws DimensionMismatchException { throws DimensionMismatchException {
@ -816,9 +853,13 @@ public class DerivativeStructure implements RealFieldElement<DerivativeStructure
} }
/** {@inheritDoc} */ /** {@inheritDoc}
* @exception DimensionMismatchException if number of free parameters
* or orders do not match
*/
public DerivativeStructure linearCombination(final DerivativeStructure a1, final DerivativeStructure b1, public DerivativeStructure linearCombination(final DerivativeStructure a1, final DerivativeStructure b1,
final DerivativeStructure a2, final DerivativeStructure b2) { final DerivativeStructure a2, final DerivativeStructure b2)
throws DimensionMismatchException {
// compute an accurate value, taking care of cancellations // compute an accurate value, taking care of cancellations
final double accurateValue = MathArrays.linearCombination(a1.getValue(), b1.getValue(), final double accurateValue = MathArrays.linearCombination(a1.getValue(), b1.getValue(),
@ -834,9 +875,13 @@ public class DerivativeStructure implements RealFieldElement<DerivativeStructure
} }
/** {@inheritDoc} */ /** {@inheritDoc}
* @exception DimensionMismatchException if number of free parameters
* or orders do not match
*/
public DerivativeStructure linearCombination(final double a1, final DerivativeStructure b1, public DerivativeStructure linearCombination(final double a1, final DerivativeStructure b1,
final double a2, final DerivativeStructure b2) { final double a2, final DerivativeStructure b2)
throws DimensionMismatchException {
// compute an accurate value, taking care of cancellations // compute an accurate value, taking care of cancellations
final double accurateValue = MathArrays.linearCombination(a1, b1.getValue(), final double accurateValue = MathArrays.linearCombination(a1, b1.getValue(),
@ -852,10 +897,14 @@ public class DerivativeStructure implements RealFieldElement<DerivativeStructure
} }
/** {@inheritDoc} */ /** {@inheritDoc}
* @exception DimensionMismatchException if number of free parameters
* or orders do not match
*/
public DerivativeStructure linearCombination(final DerivativeStructure a1, final DerivativeStructure b1, public DerivativeStructure linearCombination(final DerivativeStructure a1, final DerivativeStructure b1,
final DerivativeStructure a2, final DerivativeStructure b2, final DerivativeStructure a2, final DerivativeStructure b2,
final DerivativeStructure a3, final DerivativeStructure b3) { final DerivativeStructure a3, final DerivativeStructure b3)
throws DimensionMismatchException {
// compute an accurate value, taking care of cancellations // compute an accurate value, taking care of cancellations
final double accurateValue = MathArrays.linearCombination(a1.getValue(), b1.getValue(), final double accurateValue = MathArrays.linearCombination(a1.getValue(), b1.getValue(),
@ -872,10 +921,14 @@ public class DerivativeStructure implements RealFieldElement<DerivativeStructure
} }
/** {@inheritDoc} */ /** {@inheritDoc}
* @exception DimensionMismatchException if number of free parameters
* or orders do not match
*/
public DerivativeStructure linearCombination(final double a1, final DerivativeStructure b1, public DerivativeStructure linearCombination(final double a1, final DerivativeStructure b1,
final double a2, final DerivativeStructure b2, final double a2, final DerivativeStructure b2,
final double a3, final DerivativeStructure b3) { final double a3, final DerivativeStructure b3)
throws DimensionMismatchException {
// compute an accurate value, taking care of cancellations // compute an accurate value, taking care of cancellations
final double accurateValue = MathArrays.linearCombination(a1, b1.getValue(), final double accurateValue = MathArrays.linearCombination(a1, b1.getValue(),
@ -892,11 +945,15 @@ public class DerivativeStructure implements RealFieldElement<DerivativeStructure
} }
/** {@inheritDoc} */ /** {@inheritDoc}
* @exception DimensionMismatchException if number of free parameters
* or orders do not match
*/
public DerivativeStructure linearCombination(final DerivativeStructure a1, final DerivativeStructure b1, public DerivativeStructure linearCombination(final DerivativeStructure a1, final DerivativeStructure b1,
final DerivativeStructure a2, final DerivativeStructure b2, final DerivativeStructure a2, final DerivativeStructure b2,
final DerivativeStructure a3, final DerivativeStructure b3, final DerivativeStructure a3, final DerivativeStructure b3,
final DerivativeStructure a4, final DerivativeStructure b4) { final DerivativeStructure a4, final DerivativeStructure b4)
throws DimensionMismatchException {
// compute an accurate value, taking care of cancellations // compute an accurate value, taking care of cancellations
final double accurateValue = MathArrays.linearCombination(a1.getValue(), b1.getValue(), final double accurateValue = MathArrays.linearCombination(a1.getValue(), b1.getValue(),
@ -914,11 +971,15 @@ public class DerivativeStructure implements RealFieldElement<DerivativeStructure
} }
/** {@inheritDoc} */ /** {@inheritDoc}
* @exception DimensionMismatchException if number of free parameters
* or orders do not match
*/
public DerivativeStructure linearCombination(final double a1, final DerivativeStructure b1, public DerivativeStructure linearCombination(final double a1, final DerivativeStructure b1,
final double a2, final DerivativeStructure b2, final double a2, final DerivativeStructure b2,
final double a3, final DerivativeStructure b3, final double a3, final DerivativeStructure b3,
final double a4, final DerivativeStructure b4) { final double a4, final DerivativeStructure b4)
throws DimensionMismatchException {
// compute an accurate value, taking care of cancellations // compute an accurate value, taking care of cancellations
final double accurateValue = MathArrays.linearCombination(a1, b1.getValue(), final double accurateValue = MathArrays.linearCombination(a1, b1.getValue(),

View File

@ -39,8 +39,7 @@ public class GradientFunction implements MultivariateVectorFunction {
} }
/** {@inheritDoc} */ /** {@inheritDoc} */
public double[] value(double[] point) public double[] value(double[] point) {
throws IllegalArgumentException {
// set up parameters // set up parameters
final DerivativeStructure[] dsX = new DerivativeStructure[point.length]; final DerivativeStructure[] dsX = new DerivativeStructure[point.length];

View File

@ -41,8 +41,7 @@ public class JacobianFunction implements MultivariateMatrixFunction {
} }
/** {@inheritDoc} */ /** {@inheritDoc} */
public double[][] value(double[] point) public double[][] value(double[] point) {
throws IllegalArgumentException {
// set up parameters // set up parameters
final DerivativeStructure[] dsX = new DerivativeStructure[point.length]; final DerivativeStructure[] dsX = new DerivativeStructure[point.length];

View File

@ -17,7 +17,7 @@
package org.apache.commons.math3.analysis.differentiation; package org.apache.commons.math3.analysis.differentiation;
import org.apache.commons.math3.analysis.UnivariateFunction; import org.apache.commons.math3.analysis.UnivariateFunction;
import org.apache.commons.math3.exception.MathIllegalArgumentException; import org.apache.commons.math3.exception.DimensionMismatchException;
/** Interface for univariate functions derivatives. /** Interface for univariate functions derivatives.
* <p>This interface represents a simple function which computes * <p>This interface represents a simple function which computes
@ -35,10 +35,10 @@ public interface UnivariateDifferentiableFunction extends UnivariateFunction {
* value and the first derivative of the function.</p> * value and the first derivative of the function.</p>
* @param t function input value * @param t function input value
* @return function result * @return function result
* @exception MathIllegalArgumentException if {@code t} does not * @exception DimensionMismatchException if t is inconsistent with
* fulfill functions constraints (argument out of bound, or unsupported * function free parameters or order
* derivative order for example)
*/ */
DerivativeStructure value(DerivativeStructure t) throws MathIllegalArgumentException; DerivativeStructure value(DerivativeStructure t)
throws DimensionMismatchException;
} }

View File

@ -209,7 +209,8 @@ public class Gaussian implements UnivariateDifferentiableFunction, Differentiabl
/** {@inheritDoc} /** {@inheritDoc}
* @since 3.1 * @since 3.1
*/ */
public DerivativeStructure value(final DerivativeStructure t) { public DerivativeStructure value(final DerivativeStructure t)
throws DimensionMismatchException {
final double u = is * (t.getValue() - mean); final double u = is * (t.getValue() - mean);
double[] f = new double[t.getOrder() + 1]; double[] f = new double[t.getOrder() + 1];

View File

@ -162,7 +162,8 @@ public class HarmonicOscillator implements UnivariateDifferentiableFunction, Dif
/** {@inheritDoc} /** {@inheritDoc}
* @since 3.1 * @since 3.1
*/ */
public DerivativeStructure value(final DerivativeStructure t) { public DerivativeStructure value(final DerivativeStructure t)
throws DimensionMismatchException {
final double x = t.getValue(); final double x = t.getValue();
double[] f = new double[t.getOrder() + 1]; double[] f = new double[t.getOrder() + 1];

View File

@ -164,7 +164,8 @@ public class Sigmoid implements UnivariateDifferentiableFunction, Differentiable
/** {@inheritDoc} /** {@inheritDoc}
* @since 3.1 * @since 3.1
*/ */
public DerivativeStructure value(final DerivativeStructure t) { public DerivativeStructure value(final DerivativeStructure t)
throws DimensionMismatchException {
double[] f = new double[t.getOrder() + 1]; double[] f = new double[t.getOrder() + 1];
final double exp = FastMath.exp(-t.getValue()); final double exp = FastMath.exp(-t.getValue());

View File

@ -22,6 +22,7 @@ import org.apache.commons.math3.analysis.FunctionUtils;
import org.apache.commons.math3.analysis.UnivariateFunction; import org.apache.commons.math3.analysis.UnivariateFunction;
import org.apache.commons.math3.analysis.differentiation.DerivativeStructure; import org.apache.commons.math3.analysis.differentiation.DerivativeStructure;
import org.apache.commons.math3.analysis.differentiation.UnivariateDifferentiableFunction; import org.apache.commons.math3.analysis.differentiation.UnivariateDifferentiableFunction;
import org.apache.commons.math3.exception.DimensionMismatchException;
import org.apache.commons.math3.util.FastMath; import org.apache.commons.math3.util.FastMath;
/** /**
@ -105,7 +106,8 @@ public class Sinc implements UnivariateDifferentiableFunction, DifferentiableUni
/** {@inheritDoc} /** {@inheritDoc}
* @since 3.1 * @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 scaledX = (normalized ? FastMath.PI : 1) * t.getValue();
final double scaledX2 = scaledX * scaledX; final double scaledX2 = scaledX * scaledX;

View File

@ -20,6 +20,7 @@ package org.apache.commons.math3.analysis.function;
import java.util.Arrays; import java.util.Arrays;
import org.apache.commons.math3.analysis.UnivariateFunction; import org.apache.commons.math3.analysis.UnivariateFunction;
import org.apache.commons.math3.exception.DimensionMismatchException; 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.NullArgumentException;
import org.apache.commons.math3.exception.NoDataException; import org.apache.commons.math3.exception.NoDataException;
import org.apache.commons.math3.util.MathArrays; 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 x Domain values where the function changes value.
* @param y Values of the function. * @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. * if the {@code x} array is not sorted in strictly increasing order.
* @throws NullArgumentException if {@code x} or {@code y} are {@code null}. * @throws NullArgumentException if {@code x} or {@code y} are {@code null}.
* @throws NoDataException if {@code x} or {@code y} are zero-length. * @throws NoDataException if {@code x} or {@code y} are zero-length.
@ -59,9 +60,8 @@ public class StepFunction implements UnivariateFunction {
*/ */
public StepFunction(double[] x, public StepFunction(double[] x,
double[] y) double[] y)
throws NullArgumentException, throws NullArgumentException, NoDataException,
NoDataException, DimensionMismatchException, NonMonotonicSequenceException {
DimensionMismatchException {
if (x == null || if (x == null ||
y == null) { y == null) {
throw new NullArgumentException(); throw new NullArgumentException();

View File

@ -19,10 +19,12 @@ package org.apache.commons.math3.analysis.integration;
import org.apache.commons.math3.analysis.UnivariateFunction; import org.apache.commons.math3.analysis.UnivariateFunction;
import org.apache.commons.math3.analysis.integration.gauss.GaussIntegratorFactory; import org.apache.commons.math3.analysis.integration.gauss.GaussIntegratorFactory;
import org.apache.commons.math3.analysis.integration.gauss.GaussIntegrator; 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.MaxCountExceededException;
import org.apache.commons.math3.exception.NotStrictlyPositiveException; import org.apache.commons.math3.exception.NotStrictlyPositiveException;
import org.apache.commons.math3.exception.NumberIsTooSmallException; import org.apache.commons.math3.exception.NumberIsTooSmallException;
import org.apache.commons.math3.exception.TooManyEvaluationsException; import org.apache.commons.math3.exception.TooManyEvaluationsException;
import org.apache.commons.math3.exception.util.LocalizedFormats;
import org.apache.commons.math3.util.FastMath; import org.apache.commons.math3.util.FastMath;
/** /**
@ -52,7 +54,7 @@ public class IterativeLegendreGaussIntegrator
* @param minimalIterationCount Minimum number of iterations. * @param minimalIterationCount Minimum number of iterations.
* @param maximalIterationCount Maximum number of iterations. * @param maximalIterationCount Maximum number of iterations.
* @throws NotStrictlyPositiveException if minimal 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 * @throws NumberIsTooSmallException if maximal number of iterations
* is smaller than or equal to the minimal number of iterations. * is smaller than or equal to the minimal number of iterations.
*/ */
@ -63,6 +65,9 @@ public class IterativeLegendreGaussIntegrator
final int maximalIterationCount) final int maximalIterationCount)
throws NotStrictlyPositiveException, NumberIsTooSmallException { throws NotStrictlyPositiveException, NumberIsTooSmallException {
super(relativeAccuracy, absoluteAccuracy, minimalIterationCount, maximalIterationCount); super(relativeAccuracy, absoluteAccuracy, minimalIterationCount, maximalIterationCount);
if (n <= 0) {
throw new NotStrictlyPositiveException(LocalizedFormats.NUMBER_OF_POINTS, n);
}
numberOfPoints = n; numberOfPoints = n;
} }
@ -72,10 +77,12 @@ public class IterativeLegendreGaussIntegrator
* @param n Number of integration points. * @param n Number of integration points.
* @param relativeAccuracy Relative accuracy of the result. * @param relativeAccuracy Relative accuracy of the result.
* @param absoluteAccuracy Absolute accuracy of the result. * @param absoluteAccuracy Absolute accuracy of the result.
* @throws NotStrictlyPositiveException if {@code n < 1}.
*/ */
public IterativeLegendreGaussIntegrator(final int n, public IterativeLegendreGaussIntegrator(final int n,
final double relativeAccuracy, final double relativeAccuracy,
final double absoluteAccuracy) { final double absoluteAccuracy)
throws NotStrictlyPositiveException {
this(n, relativeAccuracy, absoluteAccuracy, this(n, relativeAccuracy, absoluteAccuracy,
DEFAULT_MIN_ITERATIONS_COUNT, DEFAULT_MAX_ITERATIONS_COUNT); DEFAULT_MIN_ITERATIONS_COUNT, DEFAULT_MAX_ITERATIONS_COUNT);
} }
@ -90,10 +97,12 @@ public class IterativeLegendreGaussIntegrator
* is not strictly positive. * is not strictly positive.
* @throws NumberIsTooSmallException if maximal number of iterations * @throws NumberIsTooSmallException if maximal number of iterations
* is smaller than or equal to the minimal 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, public IterativeLegendreGaussIntegrator(final int n,
final int minimalIterationCount, final int minimalIterationCount,
final int maximalIterationCount) { final int maximalIterationCount)
throws NotStrictlyPositiveException, NumberIsTooSmallException {
this(n, DEFAULT_RELATIVE_ACCURACY, DEFAULT_ABSOLUTE_ACCURACY, this(n, DEFAULT_RELATIVE_ACCURACY, DEFAULT_ABSOLUTE_ACCURACY,
minimalIterationCount, maximalIterationCount); minimalIterationCount, maximalIterationCount);
} }
@ -101,7 +110,7 @@ public class IterativeLegendreGaussIntegrator
/** {@inheritDoc} */ /** {@inheritDoc} */
@Override @Override
protected double doIntegrate() protected double doIntegrate()
throws TooManyEvaluationsException, MaxCountExceededException { throws MathIllegalArgumentException, TooManyEvaluationsException, MaxCountExceededException {
// Compute first estimate with a single step. // Compute first estimate with a single step.
double oldt = stage(1); double oldt = stage(1);
@ -142,7 +151,8 @@ public class IterativeLegendreGaussIntegrator
throws TooManyEvaluationsException { throws TooManyEvaluationsException {
// Function to be integrated is stored in the base class. // Function to be integrated is stored in the base class.
final UnivariateFunction f = new UnivariateFunction() { final UnivariateFunction f = new UnivariateFunction() {
public double value(double x) { public double value(double x)
throws MathIllegalArgumentException, TooManyEvaluationsException {
return computeObjectiveValue(x); return computeObjectiveValue(x);
} }
}; };

View File

@ -128,6 +128,7 @@ public class LegendreGaussIntegrator extends BaseAbstractUnivariateIntegrator {
* @param absoluteAccuracy absolute accuracy of the result * @param absoluteAccuracy absolute accuracy of the result
* @param minimalIterationCount minimum number of iterations * @param minimalIterationCount minimum number of iterations
* @param maximalIterationCount maximum 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 * @exception NotStrictlyPositiveException if minimal number of iterations
* is not strictly positive * is not strictly positive
* @exception NumberIsTooSmallException if maximal number of iterations * @exception NumberIsTooSmallException if maximal number of iterations
@ -138,7 +139,7 @@ public class LegendreGaussIntegrator extends BaseAbstractUnivariateIntegrator {
final double absoluteAccuracy, final double absoluteAccuracy,
final int minimalIterationCount, final int minimalIterationCount,
final int maximalIterationCount) final int maximalIterationCount)
throws NotStrictlyPositiveException, NumberIsTooSmallException { throws MathIllegalArgumentException, NotStrictlyPositiveException, NumberIsTooSmallException {
super(relativeAccuracy, absoluteAccuracy, minimalIterationCount, maximalIterationCount); super(relativeAccuracy, absoluteAccuracy, minimalIterationCount, maximalIterationCount);
switch(n) { switch(n) {
case 2 : 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 n number of points desired (must be between 2 and 5 inclusive)
* @param relativeAccuracy relative accuracy of the result * @param relativeAccuracy relative accuracy of the result
* @param absoluteAccuracy absolute 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, public LegendreGaussIntegrator(final int n,
final double relativeAccuracy, final double relativeAccuracy,
final double absoluteAccuracy) { final double absoluteAccuracy)
throws MathIllegalArgumentException {
this(n, relativeAccuracy, absoluteAccuracy, this(n, relativeAccuracy, absoluteAccuracy,
DEFAULT_MIN_ITERATIONS_COUNT, DEFAULT_MAX_ITERATIONS_COUNT); 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 n number of points desired (must be between 2 and 5 inclusive)
* @param minimalIterationCount minimum number of iterations * @param minimalIterationCount minimum number of iterations
* @param maximalIterationCount maximum 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 * @exception NotStrictlyPositiveException if minimal number of iterations
* is not strictly positive * is not strictly positive
* @exception NumberIsTooSmallException if maximal number of iterations * @exception NumberIsTooSmallException if maximal number of iterations
@ -190,7 +194,8 @@ public class LegendreGaussIntegrator extends BaseAbstractUnivariateIntegrator {
*/ */
public LegendreGaussIntegrator(final int n, public LegendreGaussIntegrator(final int n,
final int minimalIterationCount, final int minimalIterationCount,
final int maximalIterationCount) { final int maximalIterationCount)
throws MathIllegalArgumentException {
this(n, DEFAULT_RELATIVE_ACCURACY, DEFAULT_ABSOLUTE_ACCURACY, this(n, DEFAULT_RELATIVE_ACCURACY, DEFAULT_ABSOLUTE_ACCURACY,
minimalIterationCount, maximalIterationCount); minimalIterationCount, maximalIterationCount);
} }
@ -198,7 +203,7 @@ public class LegendreGaussIntegrator extends BaseAbstractUnivariateIntegrator {
/** {@inheritDoc} */ /** {@inheritDoc} */
@Override @Override
protected double doIntegrate() protected double doIntegrate()
throws TooManyEvaluationsException, MaxCountExceededException { throws MathIllegalArgumentException, TooManyEvaluationsException, MaxCountExceededException {
// compute first estimate with a single step // compute first estimate with a single step
double oldt = stage(1); double oldt = stage(1);

View File

@ -16,6 +16,7 @@
*/ */
package org.apache.commons.math3.analysis.integration; 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.MaxCountExceededException;
import org.apache.commons.math3.exception.NotStrictlyPositiveException; import org.apache.commons.math3.exception.NotStrictlyPositiveException;
import org.apache.commons.math3.exception.NumberIsTooLargeException; import org.apache.commons.math3.exception.NumberIsTooLargeException;
@ -144,7 +145,7 @@ public class TrapezoidIntegrator extends BaseAbstractUnivariateIntegrator {
/** {@inheritDoc} */ /** {@inheritDoc} */
@Override @Override
protected double doIntegrate() protected double doIntegrate()
throws TooManyEvaluationsException, MaxCountExceededException { throws MathIllegalArgumentException, TooManyEvaluationsException, MaxCountExceededException {
double oldt = stage(this, 0); double oldt = stage(this, 0);
iterations.incrementCount(); iterations.incrementCount();

View File

@ -21,6 +21,7 @@ import java.util.TreeMap;
import org.apache.commons.math3.util.Pair; import org.apache.commons.math3.util.Pair;
import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.exception.DimensionMismatchException;
import org.apache.commons.math3.exception.NotStrictlyPositiveException; 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 * Base class for rules that determines the integration nodes and their
@ -48,9 +49,17 @@ public abstract class BaseRuleFactory<T extends Number> {
* @param numberOfPoints Number of integration points. * @param numberOfPoints Number of integration points.
* @return a copy of the integration rule. * @return a copy of the integration rule.
* @throws NotStrictlyPositiveException if {@code numberOfPoints < 1}. * @throws NotStrictlyPositiveException if {@code numberOfPoints < 1}.
* @throws DimensionMismatchException if the elements of the rule pair do not
* have the same length.
*/ */
public Pair<double[], double[]> getRule(int numberOfPoints) public Pair<double[], double[]> 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. // Try to obtain the rule from the cache.
Pair<double[], double[]> cached = pointsAndWeightsDouble.get(numberOfPoints); Pair<double[], double[]> cached = pointsAndWeightsDouble.get(numberOfPoints);
@ -78,10 +87,11 @@ public abstract class BaseRuleFactory<T extends Number> {
* *
* @param numberOfPoints Order of the rule to be retrieved. * @param numberOfPoints Order of the rule to be retrieved.
* @return the points and weights corresponding to the given order. * @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<T[], T[]> getRuleInternal(int numberOfPoints) protected synchronized Pair<T[], T[]> getRuleInternal(int numberOfPoints)
throws NotStrictlyPositiveException { throws DimensionMismatchException {
final Pair<T[], T[]> rule = pointsAndWeights.get(numberOfPoints); final Pair<T[], T[]> rule = pointsAndWeights.get(numberOfPoints);
if (rule == null) { if (rule == null) {
addRule(computeRule(numberOfPoints)); addRule(computeRule(numberOfPoints));
@ -98,7 +108,7 @@ public abstract class BaseRuleFactory<T extends Number> {
* @throws DimensionMismatchException if the elements of the pair do not * @throws DimensionMismatchException if the elements of the pair do not
* have the same length. * have the same length.
*/ */
protected void addRule(Pair<T[], T[]> rule) { protected void addRule(Pair<T[], T[]> rule) throws DimensionMismatchException {
if (rule.getFirst().length != rule.getSecond().length) { if (rule.getFirst().length != rule.getSecond().length) {
throw new DimensionMismatchException(rule.getFirst().length, throw new DimensionMismatchException(rule.getFirst().length,
rule.getSecond().length); rule.getSecond().length);
@ -112,8 +122,11 @@ public abstract class BaseRuleFactory<T extends Number> {
* *
* @param numberOfPoints Order of the rule to be computed. * @param numberOfPoints Order of the rule to be computed.
* @return the computed rule. * @return the computed rule.
* @throws DimensionMismatchException if the elements of the pair do not
* have the same length.
*/ */
protected abstract Pair<T[], T[]> computeRule(int numberOfPoints); protected abstract Pair<T[], T[]> computeRule(int numberOfPoints)
throws DimensionMismatchException;
/** /**
* Converts the from the actual {@code Number} type to {@code double} * Converts the from the actual {@code Number} type to {@code double}

View File

@ -45,10 +45,11 @@ public class GaussIntegrator {
* @param weights Weights of the corresponding integration nodes. * @param weights Weights of the corresponding integration nodes.
* @throws NonMonotonicSequenceException if the {@code points} are not * @throws NonMonotonicSequenceException if the {@code points} are not
* sorted in increasing order. * sorted in increasing order.
* @throws DimensionMismatchException if points and weights don't have the same length
*/ */
public GaussIntegrator(double[] points, public GaussIntegrator(double[] points,
double[] weights) double[] weights)
throws NonMonotonicSequenceException { throws NonMonotonicSequenceException, DimensionMismatchException {
if (points.length != weights.length) { if (points.length != weights.length) {
throw new DimensionMismatchException(points.length, throw new DimensionMismatchException(points.length,
weights.length); weights.length);

View File

@ -18,6 +18,8 @@ package org.apache.commons.math3.analysis.integration.gauss;
import java.math.BigDecimal; 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; import org.apache.commons.math3.util.Pair;
/** /**
@ -55,10 +57,12 @@ public class GaussIntegratorFactory {
* @param lowerBound Lower bound of the integration interval. * @param lowerBound Lower bound of the integration interval.
* @param upperBound Upper bound of the integration interval. * @param upperBound Upper bound of the integration interval.
* @return a Gauss-Legendre integrator. * @return a Gauss-Legendre integrator.
* @throws NotStrictlyPositiveException if number of points is not positive
*/ */
public GaussIntegrator legendre(int numberOfPoints, public GaussIntegrator legendre(int numberOfPoints,
double lowerBound, double lowerBound,
double upperBound) { double upperBound)
throws NotStrictlyPositiveException {
return new GaussIntegrator(transform(getRule(legendre, numberOfPoints), return new GaussIntegrator(transform(getRule(legendre, numberOfPoints),
lowerBound, upperBound)); lowerBound, upperBound));
} }
@ -71,8 +75,10 @@ public class GaussIntegratorFactory {
* *
* @param numberOfPoints Order of the integration rule. * @param numberOfPoints Order of the integration rule.
* @return a Gauss-Legendre integrator. * @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)); return new GaussIntegrator(getRule(legendreHighPrecision, numberOfPoints));
} }
@ -85,10 +91,12 @@ public class GaussIntegratorFactory {
* @param lowerBound Lower bound of the integration interval. * @param lowerBound Lower bound of the integration interval.
* @param upperBound Upper bound of the integration interval. * @param upperBound Upper bound of the integration interval.
* @return a Gauss-Legendre integrator. * @return a Gauss-Legendre integrator.
* @throws NotStrictlyPositiveException if number of points is not positive
*/ */
public GaussIntegrator legendreHighPrecision(int numberOfPoints, public GaussIntegrator legendreHighPrecision(int numberOfPoints,
double lowerBound, double lowerBound,
double upperBound) { double upperBound)
throws NotStrictlyPositiveException {
return new GaussIntegrator(transform(getRule(legendreHighPrecision, numberOfPoints), return new GaussIntegrator(transform(getRule(legendreHighPrecision, numberOfPoints),
lowerBound, upperBound)); lowerBound, upperBound));
} }
@ -97,9 +105,13 @@ public class GaussIntegratorFactory {
* @param factory Integration rule factory. * @param factory Integration rule factory.
* @param numberOfPoints Order of the integration rule. * @param numberOfPoints Order of the integration rule.
* @return the integration nodes and weights. * @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<double[], double[]> getRule(BaseRuleFactory<? extends Number> factory, private static Pair<double[], double[]> getRule(BaseRuleFactory<? extends Number> factory,
int numberOfPoints) { int numberOfPoints)
throws NotStrictlyPositiveException, DimensionMismatchException {
return factory.getRule(numberOfPoints); return factory.getRule(numberOfPoints);
} }

View File

@ -16,11 +16,11 @@
*/ */
package org.apache.commons.math3.analysis.integration.gauss; package org.apache.commons.math3.analysis.integration.gauss;
import java.math.MathContext;
import java.math.BigDecimal; 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.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. * Factory that creates Gauss-type quadrature rule using Legendre polynomials.
@ -60,17 +60,10 @@ public class LegendreHighPrecisionRuleFactory extends BaseRuleFactory<BigDecimal
oneHalf = new BigDecimal("0.5", mContext); oneHalf = new BigDecimal("0.5", mContext);
} }
/** /** {@inheritDoc} */
* {@inheritDoc}
*
* @throws NotStrictlyPositiveException if {@code numberOfPoints < 1}.
*/
@Override @Override
protected Pair<BigDecimal[], BigDecimal[]> computeRule(int numberOfPoints) { protected Pair<BigDecimal[], BigDecimal[]> computeRule(int numberOfPoints)
if (numberOfPoints <= 0) { throws DimensionMismatchException {
throw new NotStrictlyPositiveException(LocalizedFormats.NUMBER_OF_POINTS,
numberOfPoints);
}
if (numberOfPoints == 1) { if (numberOfPoints == 1) {
// Break recursion. // Break recursion.

View File

@ -16,9 +16,8 @@
*/ */
package org.apache.commons.math3.analysis.integration.gauss; 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.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. * Factory that creates Gauss-type quadrature rule using Legendre polynomials.
@ -32,16 +31,10 @@ import org.apache.commons.math3.exception.util.LocalizedFormats;
* @version $Id$ * @version $Id$
*/ */
public class LegendreRuleFactory extends BaseRuleFactory<Double> { public class LegendreRuleFactory extends BaseRuleFactory<Double> {
/** /** {@inheritDoc} */
* {@inheritDoc}
*/
@Override @Override
protected Pair<Double[], Double[]> computeRule(int numberOfPoints) protected Pair<Double[], Double[]> computeRule(int numberOfPoints)
throws NotStrictlyPositiveException { throws DimensionMismatchException {
if (numberOfPoints <= 0) {
throw new NotStrictlyPositiveException(LocalizedFormats.NUMBER_OF_POINTS,
numberOfPoints);
}
if (numberOfPoints == 1) { if (numberOfPoints == 1) {
// Break recursion. // Break recursion.

View File

@ -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.DimensionMismatchException;
import org.apache.commons.math3.exception.NoDataException; import org.apache.commons.math3.exception.NoDataException;
import org.apache.commons.math3.exception.NonMonotonicSequenceException; import org.apache.commons.math3.exception.NonMonotonicSequenceException;
import org.apache.commons.math3.exception.NumberIsTooSmallException;
import org.apache.commons.math3.util.MathArrays; import org.apache.commons.math3.util.MathArrays;
/** /**
@ -37,9 +38,8 @@ public class BicubicSplineInterpolator
public BicubicSplineInterpolatingFunction interpolate(final double[] xval, public BicubicSplineInterpolatingFunction interpolate(final double[] xval,
final double[] yval, final double[] yval,
final double[][] fval) final double[][] fval)
throws NoDataException, throws NoDataException, DimensionMismatchException,
DimensionMismatchException, NonMonotonicSequenceException, NumberIsTooSmallException {
NonMonotonicSequenceException {
if (xval.length == 0 || yval.length == 0 || fval.length == 0) { if (xval.length == 0 || yval.length == 0 || fval.length == 0) {
throw new NoDataException(); throw new NoDataException();
} }

View File

@ -19,6 +19,8 @@ package org.apache.commons.math3.analysis.interpolation;
import org.apache.commons.math3.analysis.BivariateFunction; import org.apache.commons.math3.analysis.BivariateFunction;
import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.exception.DimensionMismatchException;
import org.apache.commons.math3.exception.NoDataException; 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 * Interface representing a bivariate real interpolating function where the
@ -39,9 +41,12 @@ public interface BivariateGridInterpolator {
* @return a function which interpolates the dataset. * @return a function which interpolates the dataset.
* @throws NoDataException if any of the arrays has zero length. * @throws NoDataException if any of the arrays has zero length.
* @throws DimensionMismatchException if the array lengths are inconsistent. * @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, BivariateFunction interpolate(double[] xval, double[] yval,
double[][] fval) double[][] fval)
throws NoDataException, throws NoDataException, DimensionMismatchException,
DimensionMismatchException; NonMonotonicSequenceException, NumberIsTooSmallException;
} }

View File

@ -20,11 +20,14 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
import org.apache.commons.math3.FieldElement; 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.MathArithmeticException;
import org.apache.commons.math3.exception.NoDataException; 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.ZeroException;
import org.apache.commons.math3.exception.util.LocalizedFormats; import org.apache.commons.math3.exception.util.LocalizedFormats;
import org.apache.commons.math3.util.MathArrays; import org.apache.commons.math3.util.MathArrays;
import org.apache.commons.math3.util.MathUtils;
/** Polynomial interpolator using both sample values and sample derivatives. /** Polynomial interpolator using both sample values and sample derivatives.
* <p> * <p>
@ -82,10 +85,14 @@ public class FieldHermiteInterpolator<T extends FieldElement<T>> {
* and a previous point is zero (i.e. the two points are at same abscissa) * and a previous point is zero (i.e. the two points are at same abscissa)
* @exception MathArithmeticException if the number of derivatives is larger * @exception MathArithmeticException if the number of derivatives is larger
* than 20, which prevents computation of a factorial * 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) 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(); T factorial = x.getField().getOne();
for (int i = 0; i < value.length; ++i) { for (int i = 0; i < value.length; ++i) {
@ -128,10 +135,12 @@ public class FieldHermiteInterpolator<T extends FieldElement<T>> {
* @param x interpolation abscissa * @param x interpolation abscissa
* @return interpolated value * @return interpolated value
* @exception NoDataException if sample is empty * @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 // safety check
MathUtils.checkNotNull(x);
if (abscissae.isEmpty()) { if (abscissae.isEmpty()) {
throw new NoDataException(LocalizedFormats.EMPTY_INTERPOLATION_SAMPLE); throw new NoDataException(LocalizedFormats.EMPTY_INTERPOLATION_SAMPLE);
} }
@ -157,10 +166,12 @@ public class FieldHermiteInterpolator<T extends FieldElement<T>> {
* @return interpolated value and derivatives (value in row 0, * @return interpolated value and derivatives (value in row 0,
* 1<sup>st</sup> derivative in row 1, ... n<sup>th</sup> derivative in row n) * 1<sup>st</sup> derivative in row 1, ... n<sup>th</sup> derivative in row n)
* @exception NoDataException if sample is empty * @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 // safety check
MathUtils.checkNotNull(x);
if (abscissae.isEmpty()) { if (abscissae.isEmpty()) {
throw new NoDataException(LocalizedFormats.EMPTY_INTERPOLATION_SAMPLE); throw new NoDataException(LocalizedFormats.EMPTY_INTERPOLATION_SAMPLE);
} }

View File

@ -193,8 +193,9 @@ public class MicrosphereInterpolatingFunction
/** /**
* @param point Interpolation point. * @param point Interpolation point.
* @return the interpolated value. * @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); final RealVector p = new ArrayRealVector(point);
// Reset. // Reset.

View File

@ -17,6 +17,10 @@
package org.apache.commons.math3.analysis.interpolation; package org.apache.commons.math3.analysis.interpolation;
import org.apache.commons.math3.analysis.MultivariateFunction; 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. * 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). * point (where {@code d} is thus the dimension of the space).
* @param yval the values for the interpolation points * @param yval the values for the interpolation points
* @return a function which interpolates the data set * @return a function which interpolates the data set
* @throws org.apache.commons.math3.exception.MathIllegalArgumentException * @throws MathIllegalArgumentException if the arguments violate assumptions
* if the arguments violate assumptions made by the interpolation * made by the interpolation algorithm.
* algorithm. * @throws DimensionMismatchException when the array dimensions are not consistent.
* @throws org.apache.commons.math3.exception.DimensionMismatchException * @throws NoDataException if an array has zero-length.
* when the array dimensions are not consistent. * @throws NullArgumentException if the arguments are {@code null}.
* @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}.
*/ */
MultivariateFunction interpolate(double[][] xval, double[] yval); MultivariateFunction interpolate(double[][] xval, double[] yval)
throws MathIllegalArgumentException, DimensionMismatchException,
NoDataException, NullArgumentException;
} }

View File

@ -18,7 +18,9 @@ package org.apache.commons.math3.analysis.interpolation;
import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.exception.DimensionMismatchException;
import org.apache.commons.math3.exception.NoDataException; 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.NotPositiveException;
import org.apache.commons.math3.exception.NullArgumentException;
import org.apache.commons.math3.util.MathArrays; import org.apache.commons.math3.util.MathArrays;
import org.apache.commons.math3.util.Precision; import org.apache.commons.math3.util.Precision;
import org.apache.commons.math3.optim.nonlinear.vector.jacobian.GaussNewtonOptimizer; 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. * @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); this(degree, degree);
} }
@ -64,9 +68,10 @@ public class SmoothingPolynomialBicubicSplineInterpolator
* x-dimension. * x-dimension.
* @param yDegree Degree of the polynomial fitting functions along the * @param yDegree Degree of the polynomial fitting functions along the
* y-dimension. * y-dimension.
* @exception NotPositiveException if degrees are not positive
*/ */
public SmoothingPolynomialBicubicSplineInterpolator(int xDegree, public SmoothingPolynomialBicubicSplineInterpolator(int xDegree, int yDegree)
int yDegree) { throws NotPositiveException {
if (xDegree < 0) { if (xDegree < 0) {
throw new NotPositiveException(xDegree); throw new NotPositiveException(xDegree);
} }
@ -91,8 +96,8 @@ public class SmoothingPolynomialBicubicSplineInterpolator
public BicubicSplineInterpolatingFunction interpolate(final double[] xval, public BicubicSplineInterpolatingFunction interpolate(final double[] xval,
final double[] yval, final double[] yval,
final double[][] fval) final double[][] fval)
throws NoDataException, throws NoDataException, NullArgumentException,
DimensionMismatchException { DimensionMismatchException, NonMonotonicSequenceException {
if (xval.length == 0 || yval.length == 0 || fval.length == 0) { if (xval.length == 0 || yval.length == 0 || fval.length == 0) {
throw new NoDataException(); throw new NoDataException();
} }

View File

@ -19,6 +19,7 @@ package org.apache.commons.math3.analysis.interpolation;
import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.exception.DimensionMismatchException;
import org.apache.commons.math3.exception.NoDataException; import org.apache.commons.math3.exception.NoDataException;
import org.apache.commons.math3.exception.NonMonotonicSequenceException; import org.apache.commons.math3.exception.NonMonotonicSequenceException;
import org.apache.commons.math3.exception.NumberIsTooSmallException;
import org.apache.commons.math3.util.MathArrays; import org.apache.commons.math3.util.MathArrays;
/** /**
@ -36,9 +37,8 @@ public class TricubicSplineInterpolator
final double[] yval, final double[] yval,
final double[] zval, final double[] zval,
final double[][][] fval) final double[][][] fval)
throws NoDataException, throws NoDataException, NumberIsTooSmallException,
DimensionMismatchException, DimensionMismatchException, NonMonotonicSequenceException {
NonMonotonicSequenceException {
if (xval.length == 0 || yval.length == 0 || zval.length == 0 || fval.length == 0) { if (xval.length == 0 || yval.length == 0 || zval.length == 0 || fval.length == 0) {
throw new NoDataException(); throw new NoDataException();
} }

View File

@ -19,6 +19,8 @@ package org.apache.commons.math3.analysis.interpolation;
import org.apache.commons.math3.analysis.TrivariateFunction; import org.apache.commons.math3.analysis.TrivariateFunction;
import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.exception.DimensionMismatchException;
import org.apache.commons.math3.exception.NoDataException; 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 * Interface representing a trivariate real interpolating function where the
@ -42,9 +44,12 @@ public interface TrivariateGridInterpolator {
* @return a function that interpolates the data set. * @return a function that interpolates the data set.
* @throws NoDataException if any of the arrays has zero length. * @throws NoDataException if any of the arrays has zero length.
* @throws DimensionMismatchException if the array lengths are inconsistent. * @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, TrivariateFunction interpolate(double[] xval, double[] yval, double[] zval,
double[][][] fval) double[][][] fval)
throws NoDataException, throws NoDataException, NumberIsTooSmallException,
DimensionMismatchException; DimensionMismatchException, NonMonotonicSequenceException;
} }

View File

@ -17,6 +17,8 @@
package org.apache.commons.math3.analysis.interpolation; package org.apache.commons.math3.analysis.interpolation;
import org.apache.commons.math3.analysis.UnivariateFunction; 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. * Interface representing a univariate real interpolating function.
@ -30,9 +32,11 @@ public interface UnivariateInterpolator {
* @param xval Arguments for the interpolation points. * @param xval Arguments for the interpolation points.
* @param yval Values for the interpolation points. * @param yval Values for the interpolation points.
* @return a function which interpolates the dataset. * @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 * if the arguments violate assumptions made by the interpolation
* algorithm. * algorithm.
* @throws DimensionMismatchException if arrays lengthes do not match
*/ */
UnivariateFunction interpolate(double xval[], double yval[]); UnivariateFunction interpolate(double xval[], double yval[])
throws MathIllegalArgumentException, DimensionMismatchException;
} }

View File

@ -19,6 +19,8 @@ package org.apache.commons.math3.analysis.interpolation;
import org.apache.commons.math3.analysis.UnivariateFunction; import org.apache.commons.math3.analysis.UnivariateFunction;
import org.apache.commons.math3.util.MathUtils; import org.apache.commons.math3.util.MathUtils;
import org.apache.commons.math3.util.MathArrays; 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; import org.apache.commons.math3.exception.NumberIsTooSmallException;
/** /**
@ -82,7 +84,7 @@ public class UnivariatePeriodicInterpolator
*/ */
public UnivariateFunction interpolate(double[] xval, public UnivariateFunction interpolate(double[] xval,
double[] yval) double[] yval)
throws NumberIsTooSmallException { throws NumberIsTooSmallException, NonMonotonicSequenceException {
if (xval.length < extend) { if (xval.length < extend) {
throw new NumberIsTooSmallException(xval.length, extend, true); throw new NumberIsTooSmallException(xval.length, extend, true);
} }
@ -114,7 +116,7 @@ public class UnivariatePeriodicInterpolator
final UnivariateFunction f = interpolator.interpolate(x, y); final UnivariateFunction f = interpolator.interpolate(x, y);
return new UnivariateFunction() { 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)); return f.value(MathUtils.reduce(x, period, offset));
} }
}; };

View File

@ -405,7 +405,8 @@ public class PolynomialFunction implements UnivariateDifferentiableFunction, Dif
} }
/** {@inheritDoc} */ /** {@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); return PolynomialFunction.evaluate(parameters, x);
} }
} }

View File

@ -20,6 +20,7 @@ import org.apache.commons.math3.analysis.UnivariateFunction;
import org.apache.commons.math3.util.FastMath; import org.apache.commons.math3.util.FastMath;
import org.apache.commons.math3.util.MathArrays; import org.apache.commons.math3.util.MathArrays;
import org.apache.commons.math3.exception.DimensionMismatchException; 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.NumberIsTooSmallException;
import org.apache.commons.math3.exception.util.LocalizedFormats; import org.apache.commons.math3.exception.util.LocalizedFormats;
@ -65,10 +66,11 @@ public class PolynomialFunctionLagrangeForm implements UnivariateFunction {
* @param y function values at interpolating points * @param y function values at interpolating points
* @throws DimensionMismatchException if the array lengths are different. * @throws DimensionMismatchException if the array lengths are different.
* @throws NumberIsTooSmallException if the number of points is less than 2. * @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. * 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.x = new double[x.length];
this.y = new double[y.length]; this.y = new double[y.length];
System.arraycopy(x, 0, this.x, 0, x.length); System.arraycopy(x, 0, this.x, 0, x.length);
@ -163,12 +165,13 @@ public class PolynomialFunctionLagrangeForm implements UnivariateFunction {
* @return the function value. * @return the function value.
* @throws DimensionMismatchException if {@code x} and {@code y} have * @throws DimensionMismatchException if {@code x} and {@code y} have
* different lengths. * different lengths.
* @throws org.apache.commons.math3.exception.NonMonotonicSequenceException * @throws NonMonotonicSequenceException
* if {@code x} is not sorted in strictly increasing order. * if {@code x} is not sorted in strictly increasing order.
* @throws NumberIsTooSmallException if the size of {@code x} is less * @throws NumberIsTooSmallException if the size of {@code x} is less
* than 2. * 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)) { if (verifyInterpolationArray(x, y, false)) {
return evaluateInternal(x, y, z); return evaluateInternal(x, y, z);
} }
@ -310,7 +313,8 @@ public class PolynomialFunctionLagrangeForm implements UnivariateFunction {
* @see #evaluate(double[], double[], double) * @see #evaluate(double[], double[], double)
* @see #computeCoefficients() * @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) { if (x.length != y.length) {
throw new DimensionMismatchException(x.length, y.length); throw new DimensionMismatchException(x.length, y.length);
} }

View File

@ -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.analysis.differentiation.UnivariateDifferentiableFunction;
import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.exception.DimensionMismatchException;
import org.apache.commons.math3.exception.NoDataException; 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.exception.util.LocalizedFormats;
import org.apache.commons.math3.util.MathUtils;
/** /**
* Implements the representation of a real polynomial function in * 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 a Coefficients in Newton form formula.
* @param c Centers. * @param c Centers.
* @throws org.apache.commons.math3.exception.NullArgumentException if * @throws NullArgumentException if any argument is {@code null}.
* any argument is {@code null}.
* @throws NoDataException if any array has zero length. * @throws NoDataException if any array has zero length.
* @throws DimensionMismatchException if the size difference between * @throws DimensionMismatchException if the size difference between
* {@code a} and {@code c} is not equal to 1. * {@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); verifyInputArray(a, c);
this.a = new double[a.length]; this.a = new double[a.length];
@ -172,13 +174,13 @@ public class PolynomialFunctionNewtonForm implements UnivariateDifferentiableFun
* @param c Centers. * @param c Centers.
* @param z Point at which the function value is to be computed. * @param z Point at which the function value is to be computed.
* @return the function value. * @return the function value.
* @throws org.apache.commons.math3.exception.NullArgumentException if * @throws NullArgumentException if any argument is {@code null}.
* any argument is {@code null}.
* @throws NoDataException if any array has zero length. * @throws NoDataException if any array has zero length.
* @throws DimensionMismatchException if the size difference between * @throws DimensionMismatchException if the size difference between
* {@code a} and {@code c} is not equal to 1. * {@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); verifyInputArray(a, c);
final int n = c.length; final int n = c.length;
@ -221,17 +223,18 @@ public class PolynomialFunctionNewtonForm implements UnivariateDifferentiableFun
* *
* @param a the coefficients in Newton form formula * @param a the coefficients in Newton form formula
* @param c the centers * @param c the centers
* @throws org.apache.commons.math3.exception.NullArgumentException if * @throws NullArgumentException if any argument is {@code null}.
* any argument is {@code null}.
* @throws NoDataException if any array has zero length. * @throws NoDataException if any array has zero length.
* @throws DimensionMismatchException if the size difference between * @throws DimensionMismatchException if the size difference between
* {@code a} and {@code c} is not equal to 1. * {@code a} and {@code c} is not equal to 1.
* @see org.apache.commons.math3.analysis.interpolation.DividedDifferenceInterpolator#computeDividedDifference(double[], * @see org.apache.commons.math3.analysis.interpolation.DividedDifferenceInterpolator#computeDividedDifference(double[],
* double[]) * double[])
*/ */
protected static void verifyInputArray(double a[], double c[]) { protected static void verifyInputArray(double a[], double c[])
if (a.length == 0 || throws NullArgumentException, NoDataException, DimensionMismatchException {
c.length == 0) { MathUtils.checkNotNull(a);
MathUtils.checkNotNull(c);
if (a.length == 0 || c.length == 0) {
throw new NoDataException(LocalizedFormats.EMPTY_POLYNOMIALS_COEFFICIENTS_ARRAY); throw new NoDataException(LocalizedFormats.EMPTY_POLYNOMIALS_COEFFICIENTS_ARRAY);
} }
if (a.length != c.length + 1) { if (a.length != c.length + 1) {

View File

@ -23,6 +23,7 @@ import org.apache.commons.math3.analysis.DifferentiableUnivariateFunction;
import org.apache.commons.math3.analysis.UnivariateFunction; import org.apache.commons.math3.analysis.UnivariateFunction;
import org.apache.commons.math3.analysis.differentiation.DerivativeStructure; import org.apache.commons.math3.analysis.differentiation.DerivativeStructure;
import org.apache.commons.math3.analysis.differentiation.UnivariateDifferentiableFunction; 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.OutOfRangeException;
import org.apache.commons.math3.exception.NumberIsTooSmallException; import org.apache.commons.math3.exception.NumberIsTooSmallException;
import org.apache.commons.math3.exception.DimensionMismatchException; 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 NullArgumentException if either of the input arrays is {@code null}.
* @throws NumberIsTooSmallException if knots has length less than 2. * @throws NumberIsTooSmallException if knots has length less than 2.
* @throws DimensionMismatchException if {@code polynomials.length != knots.length - 1}. * @throws DimensionMismatchException if {@code polynomials.length != knots.length - 1}.
* @throws org.apache.commons.math3.exception.NonMonotonicSequenceException if * @throws NonMonotonicSequenceException if the {@code knots} array is not strictly increasing.
* 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 || if (knots == null ||
polynomials == null) { polynomials == null) {
throw new NullArgumentException(); throw new NullArgumentException();

View File

@ -63,8 +63,7 @@ public abstract class AbstractDifferentiableUnivariateSolver
* *
* @param point Point at which the objective function must be evaluated. * @param point Point at which the objective function must be evaluated.
* @return the objective function value at specified point. * @return the objective function value at specified point.
* @throws org.apache.commons.math3.exception.TooManyEvaluationsException * @throws TooManyEvaluationsException if the maximal number of evaluations is exceeded.
* if the maximal number of evaluations is exceeded.
*/ */
protected double computeDerivativeObjectiveValue(double point) protected double computeDerivativeObjectiveValue(double point)
throws TooManyEvaluationsException { throws TooManyEvaluationsException {

View File

@ -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.DerivativeStructure;
import org.apache.commons.math3.analysis.differentiation.UnivariateDifferentiableFunction; 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 * 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. * @param point Point at which the objective function must be evaluated.
* @return the objective function value and derivative at specified point. * @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. * if the maximal number of evaluations is exceeded.
*/ */
protected DerivativeStructure computeObjectiveValueAndDerivative(double point) { protected DerivativeStructure computeObjectiveValueAndDerivative(double point)
throws TooManyEvaluationsException {
incrementEvaluationCount(); incrementEvaluationCount();
return function.value(new DerivativeStructure(1, 1, 0, point)); return function.value(new DerivativeStructure(1, 1, 0, point));
} }

View File

@ -166,11 +166,13 @@ public abstract class BaseAbstractUnivariateSolver<FUNC extends UnivariateFuncti
* @param max Upper bound for the interval. * @param max Upper bound for the interval.
* @param startValue Start value to use. * @param startValue Start value to use.
* @param maxEval Maximum number of evaluations. * @param maxEval Maximum number of evaluations.
* @exception NullArgumentException if f is null
*/ */
protected void setup(int maxEval, protected void setup(int maxEval,
FUNC f, FUNC f,
double min, double max, double min, double max,
double startValue) { double startValue)
throws NullArgumentException {
// Checks. // Checks.
MathUtils.checkNotNull(f); MathUtils.checkNotNull(f);

View File

@ -133,8 +133,7 @@ public abstract class BaseSecantSolver
*/ */
@Override @Override
protected final double doSolve() protected final double doSolve()
throws ConvergenceException, throws ConvergenceException {
MathInternalError {
// Get initial solution // Get initial solution
double x0 = getMin(); double x0 = getMin();
double x1 = getMax(); double x1 = getMax();

View File

@ -17,6 +17,8 @@
package org.apache.commons.math3.analysis.solvers; package org.apache.commons.math3.analysis.solvers;
import org.apache.commons.math3.analysis.UnivariateFunction; import org.apache.commons.math3.analysis.UnivariateFunction;
import org.apache.commons.math3.exception.MathIllegalArgumentException;
import org.apache.commons.math3.exception.TooManyEvaluationsException;
/** /**
@ -97,12 +99,13 @@ public interface BaseUnivariateSolver<FUNC extends UnivariateFunction> {
* @param min Lower bound for the interval. * @param min Lower bound for the interval.
* @param max Upper bound for the interval. * @param max Upper bound for the interval.
* @return a value where the function is zero. * @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. * 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. * 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}. * Solve for a zero in the given interval, start at {@code startValue}.
@ -116,12 +119,13 @@ public interface BaseUnivariateSolver<FUNC extends UnivariateFunction> {
* @param max Upper bound for the interval. * @param max Upper bound for the interval.
* @param startValue Start value to use. * @param startValue Start value to use.
* @return a value where the function is zero. * @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. * 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. * 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}. * Solve for a zero in the vicinity of {@code startValue}.

View File

@ -62,7 +62,7 @@ public class Incrementor {
this(max, this(max,
new MaxCountExceededCallback() { new MaxCountExceededCallback() {
/** {@inheritDoc} */ /** {@inheritDoc} */
public void trigger(int max) { public void trigger(int max) throws MaxCountExceededException {
throw new MaxCountExceededException(max); throw new MaxCountExceededException(max);
} }
}); });
@ -76,8 +76,8 @@ public class Incrementor {
* @param cb Function to be called when the maximal count has been reached. * @param cb Function to be called when the maximal count has been reached.
* @throws NullArgumentException if {@code cb} is {@code null} * @throws NullArgumentException if {@code cb} is {@code null}
*/ */
public Incrementor(int max, public Incrementor(int max, MaxCountExceededCallback cb)
MaxCountExceededCallback cb) { throws NullArgumentException {
if (cb == null){ if (cb == null){
throw new NullArgumentException(); throw new NullArgumentException();
} }
@ -132,7 +132,7 @@ public class Incrementor {
* @param value Number of increments. * @param value Number of increments.
* @throws MaxCountExceededException at counter exhaustion. * @throws MaxCountExceededException at counter exhaustion.
*/ */
public void incrementCount(int value) { public void incrementCount(int value) throws MaxCountExceededException {
for (int i = 0; i < value; i++) { for (int i = 0; i < value; i++) {
incrementCount(); incrementCount();
} }
@ -151,7 +151,7 @@ public class Incrementor {
* custom {@link MaxCountExceededCallback callback} has been set at * custom {@link MaxCountExceededCallback callback} has been set at
* construction. * construction.
*/ */
public void incrementCount() { public void incrementCount() throws MaxCountExceededException {
if (++count > maximalCount) { if (++count > maximalCount) {
maxCountCallback.trigger(maximalCount); maxCountCallback.trigger(maximalCount);
} }
@ -173,7 +173,8 @@ public class Incrementor {
* Function called when the maximal count has been reached. * Function called when the maximal count has been reached.
* *
* @param maximalCount Maximal count. * @param maximalCount Maximal count.
* @throws MaxCountExceededException at counter exhaustion
*/ */
void trigger(int maximalCount); void trigger(int maximalCount) throws MaxCountExceededException;
} }
} }

View File

@ -83,8 +83,8 @@ public class MathArrays {
* @throws DimensionMismatchException if the array lengths differ. * @throws DimensionMismatchException if the array lengths differ.
* @since 3.1 * @since 3.1
*/ */
public static double[] ebeAdd(double[] a, public static double[] ebeAdd(double[] a, double[] b)
double[] b) { throws DimensionMismatchException {
if (a.length != b.length) { if (a.length != b.length) {
throw new DimensionMismatchException(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. * @throws DimensionMismatchException if the array lengths differ.
* @since 3.1 * @since 3.1
*/ */
public static double[] ebeSubtract(double[] a, public static double[] ebeSubtract(double[] a, double[] b)
double[] b) { throws DimensionMismatchException {
if (a.length != b.length) { if (a.length != b.length) {
throw new DimensionMismatchException(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. * @throws DimensionMismatchException if the array lengths differ.
* @since 3.1 * @since 3.1
*/ */
public static double[] ebeMultiply(double[] a, public static double[] ebeMultiply(double[] a, double[] b)
double[] b) { throws DimensionMismatchException {
if (a.length != b.length) { if (a.length != b.length) {
throw new DimensionMismatchException(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. * @throws DimensionMismatchException if the array lengths differ.
* @since 3.1 * @since 3.1
*/ */
public static double[] ebeDivide(double[] a, public static double[] ebeDivide(double[] a, double[] b)
double[] b) { throws DimensionMismatchException {
if (a.length != b.length) { if (a.length != b.length) {
throw new DimensionMismatchException(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. * @param strict Whether the order should be strict.
* @return {@code true} if sorted, {@code false} otherwise. * @return {@code true} if sorted, {@code false} otherwise.
*/ */
public static boolean isMonotonic(double[] val, public static boolean isMonotonic(double[] val, OrderDirection dir, boolean strict) {
OrderDirection dir,
boolean strict) {
return checkOrder(val, dir, strict, false); return checkOrder(val, dir, strict, false);
} }