diff --git a/src/main/java/org/apache/commons/math/analysis/BinaryFunction.java b/src/main/java/org/apache/commons/math/analysis/BinaryFunction.java index f4e7f18b5..67f86c067 100644 --- a/src/main/java/org/apache/commons/math/analysis/BinaryFunction.java +++ b/src/main/java/org/apache/commons/math/analysis/BinaryFunction.java @@ -17,7 +17,7 @@ package org.apache.commons.math.analysis; -import org.apache.commons.math.exception.MathUserException; +import org.apache.commons.math.FunctionEvaluationException; import org.apache.commons.math.util.FastMath; @@ -87,7 +87,7 @@ public abstract class BinaryFunction implements BivariateRealFunction { }; /** {@inheritDoc} */ - public abstract double value(double x, double y) throws MathUserException; + public abstract double value(double x, double y) throws FunctionEvaluationException; /** Get a composable function by fixing the first argument of the instance. * @param fixedX fixed value of the first argument @@ -97,7 +97,7 @@ public abstract class BinaryFunction implements BivariateRealFunction { return new ComposableFunction() { @Override /** {@inheritDoc} */ - public double value(double x) throws MathUserException { + public double value(double x) throws FunctionEvaluationException { return BinaryFunction.this.value(fixedX, x); } }; @@ -111,7 +111,7 @@ public abstract class BinaryFunction implements BivariateRealFunction { return new ComposableFunction() { @Override /** {@inheritDoc} */ - public double value(double x) throws MathUserException { + public double value(double x) throws FunctionEvaluationException { return BinaryFunction.this.value(x, fixedY); } }; diff --git a/src/main/java/org/apache/commons/math/analysis/BivariateRealFunction.java b/src/main/java/org/apache/commons/math/analysis/BivariateRealFunction.java index 2e1e78a54..728d7878f 100644 --- a/src/main/java/org/apache/commons/math/analysis/BivariateRealFunction.java +++ b/src/main/java/org/apache/commons/math/analysis/BivariateRealFunction.java @@ -17,7 +17,7 @@ package org.apache.commons.math.analysis; -import org.apache.commons.math.exception.MathUserException; +import org.apache.commons.math.FunctionEvaluationException; /** * An interface representing a bivariate real function. @@ -32,9 +32,9 @@ public interface BivariateRealFunction { * @param x Abscissa for which the function value should be computed. * @param y Ordinate for which the function value should be computed. * @return the value. - * @throws MathUserException if the function evaluation fails. + * @throws FunctionEvaluationException if the function evaluation fails. */ double value(double x, double y) - throws MathUserException; + throws FunctionEvaluationException; } diff --git a/src/main/java/org/apache/commons/math/analysis/TrivariateRealFunction.java b/src/main/java/org/apache/commons/math/analysis/TrivariateRealFunction.java index fcd76a5cf..3ebf24dc7 100644 --- a/src/main/java/org/apache/commons/math/analysis/TrivariateRealFunction.java +++ b/src/main/java/org/apache/commons/math/analysis/TrivariateRealFunction.java @@ -17,7 +17,7 @@ package org.apache.commons.math.analysis; -import org.apache.commons.math.exception.MathUserException; +import org.apache.commons.math.FunctionEvaluationException; /** * An interface representing a trivariate real function. @@ -33,8 +33,8 @@ public interface TrivariateRealFunction { * @param y y-coordinate for which the function value should be computed. * @param z z-coordinate for which the function value should be computed. * @return the value. - * @throws MathUserException if the function evaluation fails. + * @throws FunctionEvaluationException if the function evaluation fails. */ double value(double x, double y, double z) - throws MathUserException; + throws FunctionEvaluationException; } diff --git a/src/main/java/org/apache/commons/math/analysis/UnivariateMatrixFunction.java b/src/main/java/org/apache/commons/math/analysis/UnivariateMatrixFunction.java index 98c4392a0..003b5680f 100644 --- a/src/main/java/org/apache/commons/math/analysis/UnivariateMatrixFunction.java +++ b/src/main/java/org/apache/commons/math/analysis/UnivariateMatrixFunction.java @@ -16,7 +16,7 @@ */ package org.apache.commons.math.analysis; -import org.apache.commons.math.exception.MathUserException; +import org.apache.commons.math.FunctionEvaluationException; /** * An interface representing a univariate matrix function. @@ -30,8 +30,8 @@ public interface UnivariateMatrixFunction { * Compute the value for the function. * @param x the point for which the function value should be computed * @return the value - * @throws MathUserException if the function evaluation fails + * @throws FunctionEvaluationException if the function evaluation fails */ - double[][] value(double x) throws MathUserException; + double[][] value(double x) throws FunctionEvaluationException; } diff --git a/src/main/java/org/apache/commons/math/analysis/UnivariateVectorialFunction.java b/src/main/java/org/apache/commons/math/analysis/UnivariateVectorialFunction.java index 13053923b..6349f0f4f 100644 --- a/src/main/java/org/apache/commons/math/analysis/UnivariateVectorialFunction.java +++ b/src/main/java/org/apache/commons/math/analysis/UnivariateVectorialFunction.java @@ -16,7 +16,7 @@ */ package org.apache.commons.math.analysis; -import org.apache.commons.math.exception.MathUserException; +import org.apache.commons.math.FunctionEvaluationException; /** * An interface representing a univariate vectorial function. @@ -30,8 +30,8 @@ public interface UnivariateVectorialFunction { * Compute the value for the function. * @param x the point for which the function value should be computed * @return the value - * @throws MathUserException if the function evaluation fails + * @throws FunctionEvaluationException if the function evaluation fails */ - double[] value(double x) throws MathUserException; + double[] value(double x) throws FunctionEvaluationException; } diff --git a/src/main/java/org/apache/commons/math/analysis/interpolation/BicubicSplineInterpolatingFunction.java b/src/main/java/org/apache/commons/math/analysis/interpolation/BicubicSplineInterpolatingFunction.java index 4f8d0a078..c786a4def 100644 --- a/src/main/java/org/apache/commons/math/analysis/interpolation/BicubicSplineInterpolatingFunction.java +++ b/src/main/java/org/apache/commons/math/analysis/interpolation/BicubicSplineInterpolatingFunction.java @@ -17,6 +17,7 @@ package org.apache.commons.math.analysis.interpolation; import org.apache.commons.math.DimensionMismatchException; +import org.apache.commons.math.FunctionEvaluationException; import org.apache.commons.math.analysis.BivariateRealFunction; import org.apache.commons.math.exception.NoDataException; import org.apache.commons.math.exception.OutOfRangeException; @@ -226,6 +227,7 @@ public class BicubicSplineInterpolatingFunction * @param x x-coordinate. * @param y y-coordinate. * @return the value at point (x, y) of the selected partial derivative. + * @throws FunctionEvaluationException */ private double partialDerivative(int which, double x, double y) { if (partialDerivatives == null) { @@ -244,10 +246,13 @@ public class BicubicSplineInterpolatingFunction final double xN = (x - xval[i]) / (xval[i + 1] - xval[i]); final double yN = (y - yval[j]) / (yval[j + 1] - yval[j]); - double result = Double.NaN; - result = partialDerivatives[which][i][j].value(xN, yN); + try { + return partialDerivatives[which][i][j].value(xN, yN); + } catch (FunctionEvaluationException fee) { + // this should never happen + throw new RuntimeException(fee); + } - return result; } /** diff --git a/src/main/java/org/apache/commons/math/analysis/polynomials/PolynomialFunctionLagrangeForm.java b/src/main/java/org/apache/commons/math/analysis/polynomials/PolynomialFunctionLagrangeForm.java index f665964e9..87d90c974 100644 --- a/src/main/java/org/apache/commons/math/analysis/polynomials/PolynomialFunctionLagrangeForm.java +++ b/src/main/java/org/apache/commons/math/analysis/polynomials/PolynomialFunctionLagrangeForm.java @@ -19,7 +19,7 @@ package org.apache.commons.math.analysis.polynomials; import org.apache.commons.math.DuplicateSampleAbscissaException; import org.apache.commons.math.MathRuntimeException; import org.apache.commons.math.analysis.UnivariateRealFunction; -import org.apache.commons.math.exception.MathUserException; +import org.apache.commons.math.FunctionEvaluationException; import org.apache.commons.math.exception.util.LocalizedFormats; import org.apache.commons.math.util.FastMath; @@ -81,11 +81,11 @@ public class PolynomialFunctionLagrangeForm implements UnivariateRealFunction { } /** {@inheritDoc} */ - public double value(double z) throws MathUserException { + public double value(double z) throws FunctionEvaluationException { try { return evaluate(x, y, z); } catch (DuplicateSampleAbscissaException e) { - throw new MathUserException(e, e.getSpecificPattern(), e.getGeneralPattern(), e.getArguments()); + throw new FunctionEvaluationException(z, e.getSpecificPattern(), e.getGeneralPattern(), e.getArguments()); } } diff --git a/src/main/java/org/apache/commons/math/analysis/polynomials/PolynomialFunctionNewtonForm.java b/src/main/java/org/apache/commons/math/analysis/polynomials/PolynomialFunctionNewtonForm.java index 51e11b100..9e37e94e3 100644 --- a/src/main/java/org/apache/commons/math/analysis/polynomials/PolynomialFunctionNewtonForm.java +++ b/src/main/java/org/apache/commons/math/analysis/polynomials/PolynomialFunctionNewtonForm.java @@ -18,7 +18,7 @@ package org.apache.commons.math.analysis.polynomials; import org.apache.commons.math.MathRuntimeException; import org.apache.commons.math.analysis.UnivariateRealFunction; -import org.apache.commons.math.exception.MathUserException; +import org.apache.commons.math.FunctionEvaluationException; import org.apache.commons.math.exception.util.LocalizedFormats; /** @@ -86,10 +86,10 @@ public class PolynomialFunctionNewtonForm implements UnivariateRealFunction { * * @param z the point at which the function value is to be computed * @return the function value - * @throws MathUserException if a runtime error occurs + * @throws FunctionEvaluationException if a runtime error occurs * @see UnivariateRealFunction#value(double) */ - public double value(double z) throws MathUserException { + public double value(double z) throws FunctionEvaluationException { return evaluate(a, c, z); } @@ -153,11 +153,11 @@ public class PolynomialFunctionNewtonForm implements UnivariateRealFunction { * @param c the centers * @param z the point at which the function value is to be computed * @return the function value - * @throws MathUserException if a runtime error occurs + * @throws FunctionEvaluationException if a runtime error occurs * @throws IllegalArgumentException if inputs are not valid */ public static double evaluate(double a[], double c[], double z) - throws MathUserException, IllegalArgumentException { + throws FunctionEvaluationException, IllegalArgumentException { verifyInputArray(a, c); diff --git a/src/main/java/org/apache/commons/math/distribution/AbstractContinuousDistribution.java b/src/main/java/org/apache/commons/math/distribution/AbstractContinuousDistribution.java index 7b3827e3d..a3d3756d4 100644 --- a/src/main/java/org/apache/commons/math/distribution/AbstractContinuousDistribution.java +++ b/src/main/java/org/apache/commons/math/distribution/AbstractContinuousDistribution.java @@ -24,7 +24,7 @@ import org.apache.commons.math.MathRuntimeException; import org.apache.commons.math.analysis.UnivariateRealFunction; import org.apache.commons.math.analysis.solvers.BrentSolver; import org.apache.commons.math.analysis.solvers.UnivariateRealSolverUtils; -import org.apache.commons.math.exception.MathUserException; +import org.apache.commons.math.FunctionEvaluationException; import org.apache.commons.math.exception.util.LocalizedFormats; import org.apache.commons.math.random.RandomDataImpl; import org.apache.commons.math.util.FastMath; @@ -96,15 +96,15 @@ public abstract class AbstractContinuousDistribution // subclasses can override if there is a better method. UnivariateRealFunction rootFindingFunction = new UnivariateRealFunction() { - public double value(double x) throws MathUserException { + public double value(double x) throws FunctionEvaluationException { double ret = Double.NaN; try { ret = cumulativeProbability(x) - p; } catch (MathException ex) { - throw new MathUserException(ex, ex.getSpecificPattern(), ex.getGeneralPattern(), ex.getArguments()); + throw new FunctionEvaluationException(x, ex.getSpecificPattern(), ex.getGeneralPattern(), ex.getArguments()); } if (Double.isNaN(ret)) { - throw new MathUserException(LocalizedFormats.CUMULATIVE_PROBABILITY_RETURNED_NAN, x, p); + throw new FunctionEvaluationException(x, LocalizedFormats.CUMULATIVE_PROBABILITY_RETURNED_NAN, x, p); } return ret; } diff --git a/src/main/java/org/apache/commons/math/exception/MathUserException.java b/src/main/java/org/apache/commons/math/exception/MathUserException.java deleted file mode 100644 index de1c77e6e..000000000 --- a/src/main/java/org/apache/commons/math/exception/MathUserException.java +++ /dev/null @@ -1,155 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.commons.math.exception; - -import java.util.Locale; - -import org.apache.commons.math.exception.util.ArgUtils; -import org.apache.commons.math.exception.util.Localizable; -import org.apache.commons.math.exception.util.LocalizedFormats; -import org.apache.commons.math.exception.util.MessageFactory; - -/** - * This class is intended as a sort of communication channel between - * layers of user code separated from each other by calls to - * the Commons-Math library. - * The Commons-Math code will never catch such an exception. - * - * @since 2.2 - * @version $Revision$ $Date$ - */ -public class MathUserException extends RuntimeException implements MathThrowable { - /** Serializable version Id. */ - private static final long serialVersionUID = -6024911025449780478L; - /** - * Pattern used to build the specific part of the message (problem description). - */ - private final Localizable specific; - /** - * Pattern used to build the general part of the message (problem description). - */ - private final Localizable general; - /** - * Arguments used to build the message. - */ - private final Object[] arguments; - - /** - * Build an exception with a default message. - */ - public MathUserException() { - this((Throwable) null); - } - - /** - * Build an exception with a default message. - * @param cause Cause of the error (may be null). - */ - public MathUserException(final Throwable cause) { - this(cause, LocalizedFormats.USER_EXCEPTION); - } - - /** - * Build an exception with a localizable message. - * @param pattern Format specifier. - * @param arguments Format arguments. - */ - public MathUserException(final Localizable pattern, final Object ... arguments) { - this((Throwable) null, pattern, arguments); - } - - /** - * Build an exception with a localizable message. - * @param cause Cause of the error (may be null). - * @param pattern Format specifier. - * @param arguments Format arguments. - */ - public MathUserException(final Throwable cause, - final Localizable pattern, final Object ... arguments) { - this(cause, (Localizable) null, pattern, arguments); - } - - /** - * Builds an exception from two patterns (specific and general) and - * an argument list. - * - * @param specific Format specifier for the specific part (may be null). - * @param general Format specifier for the general part (may be null). - * @param arguments Format arguments. They will be substituted in - * both the {@code general} and {@code specific} format specifiers. - */ - public MathUserException(final Localizable specific, final Localizable general, - final Object ... arguments) { - this((Throwable) null, specific, general, arguments); - } - - /** - * Builds an exception from two patterns (specific and general) and - * an argument list. - * - * @param cause Cause of the error (may be null). - * @param specific Format specifier for the specific part (may be null). - * @param general Format specifier for the general part (may be null). - * @param arguments Format arguments. They will be substituted in - * both the {@code general} and {@code specific} format specifiers. - */ - public MathUserException(final Throwable cause, - final Localizable specific, final Localizable general, - final Object ... arguments) { - super(cause); - this.specific = specific; - this.general = general; - this.arguments = ArgUtils.flatten(arguments); - } - - /** {@inheritDoc} */ - public Localizable getSpecificPattern() { - return specific; - } - - /** {@inheritDoc} */ - public Localizable getGeneralPattern() { - return general; - } - - /** {@inheritDoc} */ - public Object[] getArguments() { - return arguments.clone(); - } - - /** - * Get the message in a specified locale. - * - * @param locale Locale in which the message should be translated. - * @return the localized message. - */ - public String getMessage(final Locale locale) { - return MessageFactory.buildMessage(locale, specific, general, arguments); - } - - /** {@inheritDoc} */ - @Override - public String getMessage() { - return getMessage(Locale.US); - } - - /** {@inheritDoc} */ - @Override - public String getLocalizedMessage() { - return getMessage(Locale.getDefault()); - } -} diff --git a/src/test/java/org/apache/commons/math/analysis/interpolation/BicubicSplineInterpolatingFunctionTest.java b/src/test/java/org/apache/commons/math/analysis/interpolation/BicubicSplineInterpolatingFunctionTest.java index 4e8807281..32ca91b12 100644 --- a/src/test/java/org/apache/commons/math/analysis/interpolation/BicubicSplineInterpolatingFunctionTest.java +++ b/src/test/java/org/apache/commons/math/analysis/interpolation/BicubicSplineInterpolatingFunctionTest.java @@ -16,6 +16,7 @@ */ package org.apache.commons.math.analysis.interpolation; +import org.apache.commons.math.FunctionEvaluationException; import org.apache.commons.math.MathException; import org.apache.commons.math.DimensionMismatchException; import org.apache.commons.math.analysis.BivariateRealFunction; @@ -265,7 +266,7 @@ public final class BicubicSplineInterpolatingFunctionTest { * f(x, y) = ΣiΣj (i+1) (j+2) xi yj */ @Test - public void testSplinePartialDerivatives() { + public void testSplinePartialDerivatives() throws FunctionEvaluationException { final int N = 4; final double[] coeff = new double[16]; diff --git a/src/test/java/org/apache/commons/math/analysis/interpolation/NevilleInterpolatorTest.java b/src/test/java/org/apache/commons/math/analysis/interpolation/NevilleInterpolatorTest.java index dfd47b058..4d455294b 100644 --- a/src/test/java/org/apache/commons/math/analysis/interpolation/NevilleInterpolatorTest.java +++ b/src/test/java/org/apache/commons/math/analysis/interpolation/NevilleInterpolatorTest.java @@ -20,7 +20,7 @@ import org.apache.commons.math.MathException; import org.apache.commons.math.analysis.Expm1Function; import org.apache.commons.math.analysis.SinFunction; import org.apache.commons.math.analysis.UnivariateRealFunction; -import org.apache.commons.math.exception.MathUserException; +import org.apache.commons.math.FunctionEvaluationException; import org.apache.commons.math.util.FastMath; import junit.framework.TestCase; @@ -119,8 +119,8 @@ public final class NevilleInterpolatorTest extends TestCase { double y[] = { 0.0, 4.0, 4.0, 2.5 }; UnivariateRealFunction p = interpolator.interpolate(x, y); p.value(0.0); - fail("Expecting MathUserException - bad abscissas array"); - } catch (MathUserException ex) { + fail("Expecting FunctionEvaluationException - bad abscissas array"); + } catch (FunctionEvaluationException ex) { // expected } } diff --git a/src/test/java/org/apache/commons/math/analysis/interpolation/TricubicSplineInterpolatingFunctionTest.java b/src/test/java/org/apache/commons/math/analysis/interpolation/TricubicSplineInterpolatingFunctionTest.java index 1392d1142..1d9943df1 100644 --- a/src/test/java/org/apache/commons/math/analysis/interpolation/TricubicSplineInterpolatingFunctionTest.java +++ b/src/test/java/org/apache/commons/math/analysis/interpolation/TricubicSplineInterpolatingFunctionTest.java @@ -16,6 +16,7 @@ */ package org.apache.commons.math.analysis.interpolation; +import org.apache.commons.math.FunctionEvaluationException; import org.apache.commons.math.exception.DimensionMismatchException; import org.apache.commons.math.util.FastMath; import org.apache.commons.math.analysis.TrivariateRealFunction; @@ -275,7 +276,7 @@ public final class TricubicSplineInterpolatingFunctionTest { *

*/ @Test - public void testPlane() { + public void testPlane() throws FunctionEvaluationException { double[] xval = new double[] {3, 4, 5, 6.5}; double[] yval = new double[] {-4, -3, -1, 2, 2.5}; double[] zval = new double[] {-12, -8, -5.5, -3, 0, 2.5}; @@ -380,7 +381,7 @@ public final class TricubicSplineInterpolatingFunctionTest { * with A = 0.2, ω = 0.5, kx = 2, ky = 1. */ @Test - public void testWave() { + public void testWave() throws FunctionEvaluationException { double[] xval = new double[] {3, 4, 5, 6.5}; double[] yval = new double[] {-4, -3, -1, 2, 2.5}; double[] zval = new double[] {-12, -8, -5.5, -3, 0, 4}; diff --git a/src/test/java/org/apache/commons/math/analysis/polynomials/PolynomialFunctionLagrangeFormTest.java b/src/test/java/org/apache/commons/math/analysis/polynomials/PolynomialFunctionLagrangeFormTest.java index 9cfaad82b..f6b72533b 100644 --- a/src/test/java/org/apache/commons/math/analysis/polynomials/PolynomialFunctionLagrangeFormTest.java +++ b/src/test/java/org/apache/commons/math/analysis/polynomials/PolynomialFunctionLagrangeFormTest.java @@ -16,6 +16,8 @@ */ package org.apache.commons.math.analysis.polynomials; +import org.apache.commons.math.FunctionEvaluationException; + import junit.framework.TestCase; /** @@ -32,7 +34,7 @@ public final class PolynomialFunctionLagrangeFormTest extends TestCase { /** * Test of polynomial for the linear function. */ - public void testLinearFunction() { + public void testLinearFunction() throws FunctionEvaluationException { PolynomialFunctionLagrangeForm p; double c[], z, expected, result, tolerance = 1E-12; @@ -61,7 +63,7 @@ public final class PolynomialFunctionLagrangeFormTest extends TestCase { /** * Test of polynomial for the quadratic function. */ - public void testQuadraticFunction() { + public void testQuadraticFunction() throws FunctionEvaluationException { PolynomialFunctionLagrangeForm p; double c[], z, expected, result, tolerance = 1E-12; @@ -91,7 +93,7 @@ public final class PolynomialFunctionLagrangeFormTest extends TestCase { /** * Test of polynomial for the quintic function. */ - public void testQuinticFunction() { + public void testQuinticFunction() throws FunctionEvaluationException { PolynomialFunctionLagrangeForm p; double c[], z, expected, result, tolerance = 1E-12; diff --git a/src/test/java/org/apache/commons/math/analysis/polynomials/PolynomialFunctionNewtonFormTest.java b/src/test/java/org/apache/commons/math/analysis/polynomials/PolynomialFunctionNewtonFormTest.java index e6bb4306b..32b33d92b 100644 --- a/src/test/java/org/apache/commons/math/analysis/polynomials/PolynomialFunctionNewtonFormTest.java +++ b/src/test/java/org/apache/commons/math/analysis/polynomials/PolynomialFunctionNewtonFormTest.java @@ -16,6 +16,8 @@ */ package org.apache.commons.math.analysis.polynomials; +import org.apache.commons.math.FunctionEvaluationException; + import junit.framework.TestCase; /** @@ -30,7 +32,7 @@ public final class PolynomialFunctionNewtonFormTest extends TestCase { /** * Test of polynomial for the linear function. */ - public void testLinearFunction() { + public void testLinearFunction() throws FunctionEvaluationException { PolynomialFunctionNewtonForm p; double coefficients[], z, expected, result, tolerance = 1E-12; @@ -59,7 +61,7 @@ public final class PolynomialFunctionNewtonFormTest extends TestCase { /** * Test of polynomial for the quadratic function. */ - public void testQuadraticFunction() { + public void testQuadraticFunction() throws FunctionEvaluationException { PolynomialFunctionNewtonForm p; double coefficients[], z, expected, result, tolerance = 1E-12; @@ -89,7 +91,7 @@ public final class PolynomialFunctionNewtonFormTest extends TestCase { /** * Test of polynomial for the quintic function. */ - public void testQuinticFunction() { + public void testQuinticFunction() throws FunctionEvaluationException { PolynomialFunctionNewtonForm p; double coefficients[], z, expected, result, tolerance = 1E-12;