Removed completely MathUserException.

JIRA: MATH-195

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/trunk@1166311 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Luc Maisonobe 2011-09-07 18:48:06 +00:00
parent 22072e0060
commit 4107ac1db8
38 changed files with 101 additions and 251 deletions

View File

@ -21,7 +21,6 @@ import java.io.Serializable;
import org.apache.commons.math.MathException;
import org.apache.commons.math.analysis.UnivariateRealFunction;
import org.apache.commons.math.analysis.solvers.UnivariateRealSolverUtils;
import org.apache.commons.math.exception.MathUserException;
import org.apache.commons.math.exception.NotStrictlyPositiveException;
import org.apache.commons.math.exception.OutOfRangeException;
import org.apache.commons.math.exception.util.LocalizedFormats;
@ -76,6 +75,8 @@ public abstract class AbstractContinuousDistribution
*/
public double inverseCumulativeProbability(final double p)
throws MathException {
try {
if (p < 0.0 || p > 1.0) {
throw new OutOfRangeException(p, 0, 1);
}
@ -84,15 +85,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) {
double ret = Double.NaN;
try {
ret = cumulativeProbability(x) - p;
} catch (MathException ex) {
throw new MathUserException(ex);
throw new WrappingException(ex);
}
if (Double.isNaN(ret)) {
throw new MathUserException(LocalizedFormats.CUMULATIVE_PROBABILITY_RETURNED_NAN, x, p);
throw new WrappingException(new MathException(LocalizedFormats.CUMULATIVE_PROBABILITY_RETURNED_NAN, x, p));
}
return ret;
}
@ -128,6 +129,35 @@ public abstract class AbstractContinuousDistribution
// absolute accuracy different from the default.
bracket[0],bracket[1], getSolverAbsoluteAccuracy());
return root;
} catch (WrappingException we) {
throw we.getWrapped();
}
}
/** Local exception wrapping a MathException. */
private static class WrappingException extends RuntimeException {
/** Serializable UID. */
private static final long serialVersionUID = -2102700399222815344L;
/** Wrapped exception. */
private final MathException wrapped;
/** simple constructor.
* @param wrapped exception to wrap
*/
public WrappingException(final MathException wrapped) {
this.wrapped = wrapped;
}
/** Get the wrapped exception.
* @return wrapped exception
*/
public MathException getWrapped() {
return wrapped;
}
}
/**

View File

@ -1,114 +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.Localizable;
import org.apache.commons.math.exception.util.LocalizedFormats;
import org.apache.commons.math.exception.util.ExceptionContext;
import org.apache.commons.math.exception.util.ExceptionContextProvider;
/**
* This class is intended as a sort of communication channel between
* layers of <em>user</em> 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 $Id$
*/
public class MathUserException extends RuntimeException
implements ExceptionContextProvider {
/** Serializable version Id. */
private static final long serialVersionUID = -6024911025449780478L;
/** Context. */
private final ExceptionContext context;
/**
* Build an exception with a default message.
*/
public MathUserException() {
context = new ExceptionContext(this);
context.addMessage(LocalizedFormats.USER_EXCEPTION);
}
/**
* Build an exception with a default message.
* @param cause Cause of the error (may be null).
*/
public MathUserException(final Throwable cause) {
super(cause);
context = new ExceptionContext(this);
context.addMessage(LocalizedFormats.USER_EXCEPTION);
}
/**
* Builds an exception with a localizable message.
*
* @param pattern Format specifier.
* @param arguments Format arguments.
*/
public MathUserException(final Localizable pattern,
final Object ... arguments) {
context = new ExceptionContext(this);
context.addMessage(pattern, arguments);
}
/**
* Builds 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) {
super(cause);
context = new ExceptionContext(this);
context.addMessage(pattern, arguments);
}
/** {@inheritDoc} */
public ExceptionContext getContext() {
return context;
}
/** {@inheritDoc} */
@Override
public String getMessage() {
return context.getMessage();
}
/** {@inheritDoc} */
@Override
public String getLocalizedMessage() {
return context.getLocalizedMessage();
}
/**
* Gets the message in a specified locale.
*
* @param locale Locale in which the message should be translated
* @return localized message
*/
public String getMessage(final Locale locale) {
return context.getMessage(locale);
}
}

View File

@ -20,7 +20,6 @@ package org.apache.commons.math.ode.nonstiff;
import org.apache.commons.math.exception.DimensionMismatchException;
import org.apache.commons.math.exception.MathIllegalArgumentException;
import org.apache.commons.math.exception.MathIllegalStateException;
import org.apache.commons.math.exception.MathUserException;
import org.apache.commons.math.exception.NumberIsTooSmallException;
import org.apache.commons.math.exception.util.LocalizedFormats;
import org.apache.commons.math.ode.AbstractIntegrator;

View File

@ -20,11 +20,10 @@ package org.apache.commons.math.optimization;
import java.util.Arrays;
import java.util.Comparator;
import org.apache.commons.math.exception.MathUserException;
import org.apache.commons.math.exception.MathIllegalStateException;
import org.apache.commons.math.exception.NullArgumentException;
import org.apache.commons.math.exception.NotStrictlyPositiveException;
import org.apache.commons.math.analysis.MultivariateRealFunction;
import org.apache.commons.math.exception.MathIllegalStateException;
import org.apache.commons.math.exception.NotStrictlyPositiveException;
import org.apache.commons.math.exception.NullArgumentException;
import org.apache.commons.math.exception.util.LocalizedFormats;
import org.apache.commons.math.random.RandomVectorGenerator;
@ -101,7 +100,7 @@ public class BaseMultiStartMultivariateRealOptimizer<FUNC extends MultivariateRe
* descending order if maximizing), followed by and null elements
* corresponding to the runs that did not converge. This means all
* elements will be null if the {@link #optimize(int,MultivariateRealFunction,GoalType,double[])
* optimize} method did throw a {@link MathUserException}).
* optimize} method did throw an exception.
* This also means that if the first element is not {@code null}, it
* is the best point found across all starts.
*
@ -144,7 +143,7 @@ public class BaseMultiStartMultivariateRealOptimizer<FUNC extends MultivariateRe
final GoalType goal,
double[] startPoint) {
maxEvaluations = maxEval;
MathUserException lastException = null;
RuntimeException lastException = null;
optima = new RealPointValuePair[starts];
totalEvaluations = 0;
@ -153,7 +152,7 @@ public class BaseMultiStartMultivariateRealOptimizer<FUNC extends MultivariateRe
try {
optima[i] = optimizer.optimize(maxEval - totalEvaluations, f, goal,
i == 0 ? startPoint : generator.nextVector());
} catch (MathUserException mue) {
} catch (RuntimeException mue) {
lastException = mue;
optima[i] = null;
}

View File

@ -20,12 +20,11 @@ package org.apache.commons.math.optimization;
import java.util.Arrays;
import java.util.Comparator;
import org.apache.commons.math.exception.MathIllegalStateException;
import org.apache.commons.math.exception.ConvergenceException;
import org.apache.commons.math.exception.MathUserException;
import org.apache.commons.math.exception.NullArgumentException;
import org.apache.commons.math.exception.NotStrictlyPositiveException;
import org.apache.commons.math.analysis.MultivariateVectorialFunction;
import org.apache.commons.math.exception.ConvergenceException;
import org.apache.commons.math.exception.MathIllegalStateException;
import org.apache.commons.math.exception.NotStrictlyPositiveException;
import org.apache.commons.math.exception.NullArgumentException;
import org.apache.commons.math.exception.util.LocalizedFormats;
import org.apache.commons.math.random.RandomVectorGenerator;
@ -145,7 +144,7 @@ public class BaseMultiStartMultivariateVectorialOptimizer<FUNC extends Multivari
double[] target, double[] weights,
double[] startPoint) {
maxEvaluations = maxEval;
MathUserException lastException = null;
RuntimeException lastException = null;
optima = new VectorialPointValuePair[starts];
totalEvaluations = 0;
@ -155,11 +154,11 @@ public class BaseMultiStartMultivariateVectorialOptimizer<FUNC extends Multivari
try {
optima[i] = optimizer.optimize(maxEval - totalEvaluations, f, target, weights,
i == 0 ? startPoint : generator.nextVector());
} catch (MathUserException mue) {
lastException = mue;
optima[i] = null;
} catch (ConvergenceException oe) {
optima[i] = null;
} catch (RuntimeException mue) {
lastException = mue;
optima[i] = null;
}
totalEvaluations += optimizer.getEvaluations();

View File

@ -51,8 +51,6 @@ public interface BaseMultivariateRealOptimizer<FUNC extends MultivariateRealFunc
* if the maximal number of evaluations is exceeded.
* @throws org.apache.commons.math.exception.NullArgumentException if
* any argument is {@code null}.
* @throws org.apache.commons.math.exception.MathUserException if the
* objective function throws one during search.
*/
RealPointValuePair optimize(int maxEval, FUNC f, GoalType goalType,
double[] startPoint);

View File

@ -53,8 +53,6 @@ public interface BaseMultivariateVectorialOptimizer<FUNC extends MultivariateVec
* if the maximal number of evaluations is exceeded.
* @throws org.apache.commons.math.exception.NullArgumentException if
* any argument is {@code null}.
* @throws org.apache.commons.math.exception.MathUserException if the
* function throws one during search.
*/
VectorialPointValuePair optimize(int maxEval, FUNC f, double[] target,
double[] weight, double[] startPoint);

View File

@ -20,7 +20,6 @@ package org.apache.commons.math.optimization;
import org.apache.commons.math.analysis.MultivariateRealFunction;
import org.apache.commons.math.analysis.MultivariateVectorialFunction;
import org.apache.commons.math.exception.DimensionMismatchException;
import org.apache.commons.math.exception.MathUserException;
import org.apache.commons.math.linear.RealMatrix;
/** This class converts {@link MultivariateVectorialFunction vectorial
@ -150,7 +149,7 @@ public class LeastSquaresConverter implements MultivariateRealFunction {
}
/** {@inheritDoc} */
public double value(final double[] point) throws MathUserException {
public double value(final double[] point) {
// compute residuals
final double[] residuals = function.value(point);
if (residuals.length != observations.length) {

View File

@ -21,7 +21,6 @@ import java.util.Arrays;
import java.util.Comparator;
import org.apache.commons.math.analysis.MultivariateRealFunction;
import org.apache.commons.math.exception.MathUserException;
import org.apache.commons.math.exception.NotStrictlyPositiveException;
import org.apache.commons.math.exception.DimensionMismatchException;
import org.apache.commons.math.exception.ZeroException;
@ -202,11 +201,9 @@ public abstract class AbstractSimplex {
* to worst.
* @throws org.apache.commons.math.exception.TooManyEvaluationsException
* if the algorithm fails to converge.
* @throws MathUserException if evaluation function throws one
*/
public abstract void iterate(final MultivariateRealFunction evaluationFunction,
final Comparator<RealPointValuePair> comparator)
throws MathUserException;
final Comparator<RealPointValuePair> comparator);
/**
* Build an initial simplex.
@ -242,11 +239,9 @@ public abstract class AbstractSimplex {
* @param comparator Comparator to use to sort simplex vertices from best to worst.
* @throws org.apache.commons.math.exception.TooManyEvaluationsException
* if the maximal number of evaluations is exceeded.
* @throws MathUserException if evaluation function throws one
*/
public void evaluate(final MultivariateRealFunction evaluationFunction,
final Comparator<RealPointValuePair> comparator)
throws MathUserException {
final Comparator<RealPointValuePair> comparator) {
// Evaluate the objective function at all non-evaluated simplex points.
for (int i = 0; i < simplex.length; i++) {
final RealPointValuePair vertex = simplex[i];

View File

@ -93,8 +93,6 @@ public abstract class BaseAbstractScalarOptimizer<FUNC extends MultivariateRealF
* @return the objective function value at the specified point.
* @throws TooManyEvaluationsException if the maximal number of
* evaluations is exceeded.
* @throws org.apache.commons.math.exception.MathUserException if the
* objective function throws one.
*/
protected double computeObjectiveValue(double[] point) {
try {
@ -151,8 +149,6 @@ public abstract class BaseAbstractScalarOptimizer<FUNC extends MultivariateRealF
*
* @return the point/value pair giving the optimal value for the
* objective function.
* @throws org.apache.commons.math.exception.MathUserException if
* the objective function throws one.
*/
protected abstract RealPointValuePair doOptimize();
}

View File

@ -95,8 +95,6 @@ public abstract class BaseAbstractVectorialOptimizer<FUNC extends MultivariateVe
* @return the objective function value at the specified point.
* @throws TooManyEvaluationsException if the maximal number of evaluations is
* exceeded.
* @throws org.apache.commons.math.exception.MathUserException if the
* objective function throws one.
*/
protected double[] computeObjectiveValue(double[] point) {
try {
@ -153,8 +151,6 @@ public abstract class BaseAbstractVectorialOptimizer<FUNC extends MultivariateVe
*
* @return the point/value pair giving the optimal value for the
* objective function.
* @throws org.apache.commons.math.exception.MathUserException if
* the function throws one during search.
*/
protected abstract VectorialPointValuePair doOptimize();

View File

@ -20,7 +20,6 @@ package org.apache.commons.math.optimization.direct;
import java.util.Comparator;
import org.apache.commons.math.analysis.MultivariateRealFunction;
import org.apache.commons.math.exception.MathUserException;
import org.apache.commons.math.optimization.RealPointValuePair;
/**
@ -154,8 +153,7 @@ public class MultiDirectionalSimplex extends AbstractSimplex {
/** {@inheritDoc} */
@Override
public void iterate(final MultivariateRealFunction evaluationFunction,
final Comparator<RealPointValuePair> comparator)
throws MathUserException {
final Comparator<RealPointValuePair> comparator) {
// Save the original simplex.
final RealPointValuePair[] original = getPoints();
final RealPointValuePair best = original[0];
@ -192,13 +190,11 @@ public class MultiDirectionalSimplex extends AbstractSimplex {
* @return the best point in the transformed simplex.
* @throws org.apache.commons.math.exception.TooManyEvaluationsException
* if the maximal number of evaluations is exceeded.
* @throws MathUserException if function cannot be evaluated at some points
*/
private RealPointValuePair evaluateNewSimplex(final MultivariateRealFunction evaluationFunction,
final RealPointValuePair[] original,
final double coeff,
final Comparator<RealPointValuePair> comparator)
throws MathUserException {
final Comparator<RealPointValuePair> comparator) {
final double[] xSmallest = original[0].getPointRef();
// Perform a linear transformation on all the simplex points,
// except the first one.

View File

@ -21,7 +21,6 @@ import java.util.Comparator;
import org.apache.commons.math.optimization.RealPointValuePair;
import org.apache.commons.math.analysis.MultivariateRealFunction;
import org.apache.commons.math.exception.MathUserException;
/**
* This class implements the Nelder-Mead simplex algorithm.
@ -186,8 +185,7 @@ public class NelderMeadSimplex extends AbstractSimplex {
/** {@inheritDoc} */
@Override
public void iterate(final MultivariateRealFunction evaluationFunction,
final Comparator<RealPointValuePair> comparator)
throws MathUserException {
final Comparator<RealPointValuePair> comparator) {
// The simplex has n + 1 points if dimension is n.
final int n = getDimension();

View File

@ -261,8 +261,6 @@ public class PowellOptimizer
* @return the optimum.
* @throws org.apache.commons.math.exception.TooManyEvaluationsException
* if the number of evaluations is exceeded.
* @throws org.apache.commons.math.exception.MathUserException if the
* objective function throws one.
*/
public UnivariateRealPointValuePair search(final double[] p, final double[] d) {
final int n = p.length;

View File

@ -20,7 +20,6 @@ package org.apache.commons.math.optimization.direct;
import java.util.Comparator;
import org.apache.commons.math.analysis.MultivariateRealFunction;
import org.apache.commons.math.exception.MathUserException;
import org.apache.commons.math.exception.NullArgumentException;
import org.apache.commons.math.optimization.GoalType;
import org.apache.commons.math.optimization.ConvergenceChecker;
@ -112,7 +111,7 @@ public class SimplexOptimizer
/** {@inheritDoc} */
@Override
protected RealPointValuePair doOptimize() throws MathUserException {
protected RealPointValuePair doOptimize() {
if (simplex == null) {
throw new NullArgumentException();
}
@ -121,7 +120,7 @@ public class SimplexOptimizer
// evaluations counter.
final MultivariateRealFunction evalFunc
= new MultivariateRealFunction() {
public double value(double[] point) throws MathUserException {
public double value(double[] point) {
return computeObjectiveValue(point);
}
};

View File

@ -119,8 +119,6 @@ public class CurveFitter {
* @return the fitted parameters.
* @throws org.apache.commons.math.exception.DimensionMismatchException
* if the start point dimension is wrong.
* @throws org.apache.commons.math.exception.MathUserException if the
* parametric function throws one.
*/
public double[] fit(final ParametricUnivariateRealFunction f, final double[] initialGuess) {
return fit(Integer.MAX_VALUE, f, initialGuess);
@ -141,8 +139,6 @@ public class CurveFitter {
* if the number of allowed evaluations is exceeded.
* @throws org.apache.commons.math.exception.DimensionMismatchException
* if the start point dimension is wrong.
* @throws org.apache.commons.math.exception.MathUserException if the
* parametric function throws one.
*/
public double[] fit(int maxEval, final ParametricUnivariateRealFunction f,
final double[] initialGuess) {

View File

@ -103,8 +103,6 @@ public abstract class AbstractLeastSquaresOptimizer
*
* @throws DimensionMismatchException if the Jacobian dimension does not
* match problem dimension.
* @throws org.apache.commons.math.exception.MathUserException if the jacobian
* function throws one.
*/
protected void updateJacobian() {
++jacobianEvaluations;
@ -180,8 +178,6 @@ public abstract class AbstractLeastSquaresOptimizer
* @return the covariance matrix.
* @throws org.apache.commons.math.linear.SingularMatrixException
* if the covariance matrix cannot be computed (singular problem).
* @throws org.apache.commons.math.exception.MathUserException if the
* jacobian function throws one.
*/
public double[][] getCovariances() {
// set up the jacobian
@ -216,8 +212,6 @@ public abstract class AbstractLeastSquaresOptimizer
* @throws NumberIsTooSmallException if the number of degrees of freedom is not
* positive, i.e. the number of measurements is less or equal to the number of
* parameters.
* @throws org.apache.commons.math.exception.MathUserException if the jacobian
* function throws one.
*/
public double[] guessParametersErrors() {
if (rows <= cols) {

View File

@ -19,7 +19,6 @@ package org.apache.commons.math.optimization.general;
import org.apache.commons.math.analysis.DifferentiableMultivariateRealFunction;
import org.apache.commons.math.analysis.MultivariateVectorialFunction;
import org.apache.commons.math.exception.MathUserException;
import org.apache.commons.math.optimization.DifferentiableMultivariateRealOptimizer;
import org.apache.commons.math.optimization.GoalType;
import org.apache.commons.math.optimization.ConvergenceChecker;
@ -63,10 +62,8 @@ public abstract class AbstractScalarDifferentiableOptimizer
* @return the gradient at the specified point.
* @throws org.apache.commons.math.exception.TooManyEvaluationsException
* if the allowed number of evaluations is exceeded.
* @throws MathUserException if objective function gradient throws one
*/
protected double[] computeObjectiveGradient(final double[] evaluationPoint)
throws MathUserException {
protected double[] computeObjectiveGradient(final double[] evaluationPoint) {
return gradient.value(evaluationPoint);
}
@ -75,7 +72,7 @@ public abstract class AbstractScalarDifferentiableOptimizer
public RealPointValuePair optimize(int maxEval,
final DifferentiableMultivariateRealFunction f,
final GoalType goalType,
final double[] startPoint) throws MathUserException {
final double[] startPoint) {
// Store optimization problem characteristics.
gradient = f.gradient();

View File

@ -18,7 +18,6 @@
package org.apache.commons.math.optimization.general;
import org.apache.commons.math.exception.ConvergenceException;
import org.apache.commons.math.exception.MathUserException;
import org.apache.commons.math.exception.util.LocalizedFormats;
import org.apache.commons.math.linear.ArrayRealVector;
import org.apache.commons.math.linear.BlockRealMatrix;
@ -26,7 +25,6 @@ import org.apache.commons.math.linear.DecompositionSolver;
import org.apache.commons.math.linear.LUDecompositionImpl;
import org.apache.commons.math.linear.QRDecompositionImpl;
import org.apache.commons.math.linear.RealMatrix;
import org.apache.commons.math.linear.RealVector;
import org.apache.commons.math.linear.SingularMatrixException;
import org.apache.commons.math.optimization.ConvergenceChecker;
import org.apache.commons.math.optimization.SimpleVectorialValueChecker;
@ -97,7 +95,7 @@ public class GaussNewtonOptimizer extends AbstractLeastSquaresOptimizer {
/** {@inheritDoc} */
@Override
public VectorialPointValuePair doOptimize() throws MathUserException {
public VectorialPointValuePair doOptimize() {
final ConvergenceChecker<VectorialPointValuePair> checker
= getConvergenceChecker();

View File

@ -19,7 +19,6 @@ package org.apache.commons.math.optimization.general;
import java.util.Arrays;
import org.apache.commons.math.exception.ConvergenceException;
import org.apache.commons.math.exception.MathUserException;
import org.apache.commons.math.exception.util.LocalizedFormats;
import org.apache.commons.math.optimization.VectorialPointValuePair;
import org.apache.commons.math.optimization.ConvergenceChecker;
@ -270,7 +269,7 @@ public class LevenbergMarquardtOptimizer extends AbstractLeastSquaresOptimizer {
/** {@inheritDoc} */
@Override
protected VectorialPointValuePair doOptimize() throws MathUserException {
protected VectorialPointValuePair doOptimize() {
// arrays shared with the other private methods
solvedCols = FastMath.min(rows, cols);
diagR = new double[cols];

View File

@ -243,8 +243,6 @@ public class NonLinearConjugateGradientOptimizer
* @param h initial step to try.
* @return b such that f(a) and f(b) have opposite signs.
* @throws MathIllegalStateException if no bracket can be found.
* @throws org.apache.commons.math.exception.MathUserException if the
* function throws one.
*/
private double findUpperBound(final UnivariateRealFunction f,
final double a, final double h) {

View File

@ -91,8 +91,6 @@ public abstract class AbstractUnivariateRealOptimizer
* @return the objective function value at specified point.
* @throws TooManyEvaluationsException if the maximal number of evaluations
* is exceeded.
* @throws org.apache.commons.math.exception.MathUserException if the
* objective function throws one.
*/
protected double computeObjectiveValue(double point) {
try {
@ -158,8 +156,6 @@ public abstract class AbstractUnivariateRealOptimizer
* @return the optimum and its corresponding function value.
* @throws TooManyEvaluationsException if the maximal number of evaluations
* is exceeded.
* @throws org.apache.commons.math.exception.MathUserException if the
* function to optimize throws one during search.
*/
protected abstract UnivariateRealPointValuePair doOptimize();
}

View File

@ -54,8 +54,6 @@ public interface BaseUnivariateRealOptimizer<FUNC extends UnivariateRealFunction
* if the optimizer detects a convergence problem.
* @throws IllegalArgumentException if {@code min > max} or the endpoints
* do not satisfy the requirements specified by the optimizer.
* @throws org.apache.commons.math.exception.MathUserException if the
* function to optimize throws one during search.
*/
UnivariateRealPointValuePair optimize(int maxEval, FUNC f, GoalType goalType,
double min, double max);
@ -80,8 +78,6 @@ public interface BaseUnivariateRealOptimizer<FUNC extends UnivariateRealFunction
* do not satisfy the requirements specified by the optimizer.
* @throws org.apache.commons.math.exception.NullArgumentException if any
* argument is {@code null}.
* @throws org.apache.commons.math.exception.MathUserException if the
* function to optimize throws one during search.
*/
UnivariateRealPointValuePair optimize(int maxEval, FUNC f, GoalType goalType,
double min, double max,

View File

@ -17,7 +17,6 @@
package org.apache.commons.math.optimization.univariate;
import org.apache.commons.math.util.Incrementor;
import org.apache.commons.math.exception.MathUserException;
import org.apache.commons.math.exception.NotStrictlyPositiveException;
import org.apache.commons.math.exception.TooManyEvaluationsException;
import org.apache.commons.math.exception.MaxCountExceededException;
@ -109,10 +108,8 @@ public class BracketFinder {
* @param xB Initial point.
* @throws TooManyEvaluationsException if the maximum number of evaluations
* is exceeded.
* @throws MathUserException if function throw one
*/
public void search(UnivariateRealFunction func, GoalType goal, double xA, double xB)
throws MathUserException {
public void search(UnivariateRealFunction func, GoalType goal, double xA, double xB) {
evaluations.resetCount();
final boolean isMinim = goal == GoalType.MINIMIZE;
@ -278,9 +275,8 @@ public class BracketFinder {
* @return {@code f(x)}
* @throws TooManyEvaluationsException if the maximal number of evaluations is
* exceeded.
* @throws MathUserException if f throws one.
*/
private double eval(UnivariateRealFunction f, double x) throws MathUserException {
private double eval(UnivariateRealFunction f, double x) {
try {
evaluations.incrementCount();
} catch (MaxCountExceededException e) {

View File

@ -18,7 +18,6 @@ package org.apache.commons.math.optimization.univariate;
import org.apache.commons.math.util.MathUtils;
import org.apache.commons.math.util.FastMath;
import org.apache.commons.math.exception.MathUserException;
import org.apache.commons.math.exception.NumberIsTooSmallException;
import org.apache.commons.math.exception.NotStrictlyPositiveException;
import org.apache.commons.math.optimization.ConvergenceChecker;
@ -86,7 +85,7 @@ public class BrentOptimizer extends AbstractUnivariateRealOptimizer {
/** {@inheritDoc} */
@Override
protected UnivariateRealPointValuePair doOptimize() throws MathUserException {
protected UnivariateRealPointValuePair doOptimize() {
final boolean isMinim = getGoalType() == GoalType.MINIMIZE;
final double lo = getMin();
final double mid = getStartValue();

View File

@ -21,7 +21,6 @@ import java.util.Arrays;
import java.util.Comparator;
import org.apache.commons.math.analysis.UnivariateRealFunction;
import org.apache.commons.math.exception.MathUserException;
import org.apache.commons.math.exception.MathIllegalStateException;
import org.apache.commons.math.exception.NotStrictlyPositiveException;
import org.apache.commons.math.exception.NullArgumentException;
@ -128,7 +127,7 @@ public class MultiStartUnivariateRealOptimizer<FUNC extends UnivariateRealFuncti
* corresponding to the runs that did not converge. This means all
* elements will be {@code null} if the {@link
* #optimize(int,UnivariateRealFunction,GoalType,double,double) optimize}
* method did throw a {@link MathUserException}).
* method did throw an exception.
* This also means that if the first element is not {@code null}, it is
* the best point found across all starts.
*
@ -156,7 +155,7 @@ public class MultiStartUnivariateRealOptimizer<FUNC extends UnivariateRealFuncti
final GoalType goal,
final double min, final double max,
final double startValue) {
MathUserException lastException = null;
RuntimeException lastException = null;
optima = new UnivariateRealPointValuePair[starts];
totalEvaluations = 0;
@ -165,7 +164,7 @@ public class MultiStartUnivariateRealOptimizer<FUNC extends UnivariateRealFuncti
try {
final double s = (i == 0) ? startValue : min + generator.nextDouble() * (max - min);
optima[i] = optimizer.optimize(maxEval - totalEvaluations, f, goal, min, max, s);
} catch (MathUserException mue) {
} catch (RuntimeException mue) {
lastException = mue;
optima[i] = null;
}

View File

@ -52,6 +52,9 @@ The <action> type attribute can be add,update,fix,remove.
If the output is not quite correct, check for invisible trailing spaces!
-->
<release version="3.0" date="TBD" description="TBD">
<action dev="luc" type="update" issue="MATH-195" >
Removed completely MathUserException.
</action>
<action dev="luc" type="update" issue="MATH-488" >
Use the refactored exceptions framework for ODE.
</action>

View File

@ -130,7 +130,7 @@ dp853.integrate(ode, 0.0, y, 16.0, y); // now y contains final state at time t=1
StepHandler stepHandler = new StepHandler() {
public void reset() {}
public void handleStep(StepInterpolator interpolator, boolean isLast) throws MathUserException {
public void handleStep(StepInterpolator interpolator, boolean isLast) {
double t = interpolator.getCurrentTime();
double[] y = interpolator.getInterpolatedState();
System.out.println(t + " " + y[0] + " " + y[1]);

View File

@ -21,7 +21,6 @@ import org.junit.Assert;
import org.apache.commons.math.TestUtils;
import org.apache.commons.math.util.FastMath;
import org.apache.commons.math.exception.MathUserException;
import org.apache.commons.math.exception.DimensionMismatchException;
import org.apache.commons.math.exception.NullArgumentException;
import org.apache.commons.math.exception.OutOfRangeException;

View File

@ -23,7 +23,6 @@ import org.junit.Test;
import org.junit.Assert;
import org.apache.commons.math.TestUtils;
import org.apache.commons.math.exception.MathUserException;
import org.apache.commons.math.fraction.Fraction;
import org.apache.commons.math.fraction.FractionField;
import org.apache.commons.math.exception.NoDataException;

View File

@ -23,7 +23,6 @@ import org.junit.Test;
import org.junit.Assert;
import org.apache.commons.math.TestUtils;
import org.apache.commons.math.exception.MathUserException;
import org.apache.commons.math.util.FastMath;
import org.apache.commons.math.exception.NullArgumentException;
import org.apache.commons.math.exception.OutOfRangeException;

View File

@ -22,7 +22,6 @@ import org.junit.Assert;
import org.apache.commons.math.TestUtils;
import org.apache.commons.math.fraction.Fraction;
import org.apache.commons.math.fraction.FractionField;
import org.apache.commons.math.exception.MathUserException;
import org.apache.commons.math.exception.MathIllegalStateException;
import org.apache.commons.math.exception.DimensionMismatchException;
import org.apache.commons.math.exception.NoDataException;

View File

@ -21,7 +21,6 @@ package org.apache.commons.math.optimization;
import org.apache.commons.math.analysis.DifferentiableMultivariateVectorialFunction;
import org.apache.commons.math.analysis.MultivariateMatrixFunction;
import org.apache.commons.math.exception.MathIllegalStateException;
import org.apache.commons.math.exception.MathUserException;
import org.apache.commons.math.linear.BlockRealMatrix;
import org.apache.commons.math.linear.RealMatrix;
import org.apache.commons.math.optimization.general.GaussNewtonOptimizer;
@ -133,7 +132,7 @@ public class MultiStartDifferentiableMultivariateVectorialOptimizerTest {
Assert.assertEquals(100, optimizer.getMaxEvaluations());
}
@Test(expected = MathUserException.class)
@Test(expected = TestException.class)
public void testNoOptimum() {
DifferentiableMultivariateVectorialOptimizer underlyingOptimizer =
new GaussNewtonOptimizer(true);
@ -150,11 +149,15 @@ public class MultiStartDifferentiableMultivariateVectorialOptimizerTest {
return null;
}
public double[] value(double[] point) {
throw new MathUserException();
throw new TestException();
}
}, new double[] { 2 }, new double[] { 1 }, new double[] { 0 });
}
private static class TestException extends RuntimeException {
private static final long serialVersionUID = -7809988995389067683L;
}
private static class LinearProblem implements DifferentiableMultivariateVectorialFunction {
final RealMatrix factors;

View File

@ -19,9 +19,8 @@ package org.apache.commons.math.optimization;
import org.apache.commons.math.analysis.MultivariateRealFunction;
import org.apache.commons.math.exception.MathUserException;
import org.apache.commons.math.optimization.direct.SimplexOptimizer;
import org.apache.commons.math.optimization.direct.NelderMeadSimplex;
import org.apache.commons.math.optimization.direct.SimplexOptimizer;
import org.apache.commons.math.random.GaussianRandomGenerator;
import org.apache.commons.math.random.JDKRandomGenerator;
import org.apache.commons.math.random.RandomVectorGenerator;
@ -31,7 +30,7 @@ import org.junit.Test;
public class MultiStartMultivariateRealOptimizerTest {
@Test
public void testRosenbrock() throws MathUserException {
public void testRosenbrock() {
Rosenbrock rosenbrock = new Rosenbrock();
SimplexOptimizer underlying = new SimplexOptimizer();
NelderMeadSimplex simplex = new NelderMeadSimplex(new double[][] {

View File

@ -17,7 +17,6 @@
package org.apache.commons.math.optimization.fitting;
import org.apache.commons.math.exception.MathUserException;
import org.apache.commons.math.optimization.general.LevenbergMarquardtOptimizer;
import org.apache.commons.math.analysis.ParametricUnivariateRealFunction;
import org.apache.commons.math.util.FastMath;
@ -27,8 +26,7 @@ import org.junit.Test;
public class CurveFitterTest {
@Test
public void testMath303()
throws MathUserException {
public void testMath303() {
LevenbergMarquardtOptimizer optimizer = new LevenbergMarquardtOptimizer();
CurveFitter fitter = new CurveFitter(optimizer);
@ -51,8 +49,7 @@ public class CurveFitterTest {
}
@Test
public void testMath304()
throws MathUserException {
public void testMath304() {
LevenbergMarquardtOptimizer optimizer = new LevenbergMarquardtOptimizer();
CurveFitter fitter = new CurveFitter(optimizer);
@ -74,8 +71,7 @@ public class CurveFitterTest {
}
@Test
public void testMath372()
throws MathUserException {
public void testMath372() {
LevenbergMarquardtOptimizer optimizer = new LevenbergMarquardtOptimizer();
CurveFitter curveFitter = new CurveFitter(optimizer);

View File

@ -22,7 +22,6 @@ import java.io.Serializable;
import java.util.Arrays;
import org.apache.commons.math.exception.MathUserException;
import org.apache.commons.math.exception.ConvergenceException;
import org.apache.commons.math.exception.TooManyEvaluationsException;
import org.apache.commons.math.exception.DimensionMismatchException;
@ -30,7 +29,6 @@ import org.apache.commons.math.analysis.DifferentiableMultivariateVectorialFunct
import org.apache.commons.math.analysis.MultivariateMatrixFunction;
import org.apache.commons.math.linear.BlockRealMatrix;
import org.apache.commons.math.linear.RealMatrix;
import org.apache.commons.math.optimization.SimpleVectorialPointChecker;
import org.apache.commons.math.optimization.SimpleVectorialValueChecker;
import org.apache.commons.math.optimization.VectorialPointValuePair;
import org.apache.commons.math.util.FastMath;
@ -102,7 +100,7 @@ import org.junit.Test;
public class GaussNewtonOptimizerTest {
@Test
public void testTrivial() throws MathUserException {
public void testTrivial() {
LinearProblem problem =
new LinearProblem(new double[][] { { 2 } }, new double[] { 3 });
@ -117,7 +115,7 @@ public class GaussNewtonOptimizerTest {
}
@Test
public void testColumnsPermutation() throws MathUserException {
public void testColumnsPermutation() {
LinearProblem problem =
new LinearProblem(new double[][] { { 1.0, -1.0 }, { 0.0, 2.0 }, { 1.0, -2.0 } },
@ -138,7 +136,7 @@ public class GaussNewtonOptimizerTest {
}
@Test
public void testNoDependency() throws MathUserException {
public void testNoDependency() {
LinearProblem problem = new LinearProblem(new double[][] {
{ 2, 0, 0, 0, 0, 0 },
{ 0, 2, 0, 0, 0, 0 },
@ -161,7 +159,7 @@ public class GaussNewtonOptimizerTest {
}
@Test
public void testOneSet() throws MathUserException {
public void testOneSet() {
LinearProblem problem = new LinearProblem(new double[][] {
{ 1, 0, 0 },
@ -182,7 +180,7 @@ public class GaussNewtonOptimizerTest {
}
@Test
public void testTwoSets() throws MathUserException {
public void testTwoSets() {
double epsilon = 1.0e-7;
LinearProblem problem = new LinearProblem(new double[][] {
{ 2, 1, 0, 4, 0, 0 },
@ -225,7 +223,7 @@ public class GaussNewtonOptimizerTest {
}
@Test
public void testIllConditioned() throws MathUserException {
public void testIllConditioned() {
LinearProblem problem1 = new LinearProblem(new double[][] {
{ 10.0, 7.0, 8.0, 7.0 },
{ 7.0, 5.0, 6.0, 5.0 },
@ -296,7 +294,7 @@ public class GaussNewtonOptimizerTest {
}
@Test
public void testRedundantEquations() throws MathUserException {
public void testRedundantEquations() {
LinearProblem problem = new LinearProblem(new double[][] {
{ 1.0, 1.0 },
{ 1.0, -1.0 },
@ -315,7 +313,7 @@ public class GaussNewtonOptimizerTest {
}
@Test
public void testInconsistentEquations() throws MathUserException {
public void testInconsistentEquations() {
LinearProblem problem = new LinearProblem(new double[][] {
{ 1.0, 1.0 },
{ 1.0, -1.0 },
@ -331,7 +329,7 @@ public class GaussNewtonOptimizerTest {
}
@Test(expected=DimensionMismatchException.class)
public void testInconsistentSizes1() throws MathUserException {
public void testInconsistentSizes1() {
LinearProblem problem =
new LinearProblem(new double[][] { { 1, 0 }, { 0, 1 } }, new double[] { -1, 1 });
@ -350,7 +348,7 @@ public class GaussNewtonOptimizerTest {
}
@Test(expected=DimensionMismatchException.class)
public void testInconsistentSizes2() throws MathUserException {
public void testInconsistentSizes2() {
LinearProblem problem =
new LinearProblem(new double[][] { { 1, 0 }, { 0, 1 } }, new double[] { -1, 1 });
@ -386,7 +384,7 @@ public class GaussNewtonOptimizerTest {
}
@Test
public void testCircleFitting() throws MathUserException {
public void testCircleFitting() {
CircleVectorial circle = new CircleVectorial();
circle.addPoint( 30.0, 68.0);
circle.addPoint( 50.0, -6.0);
@ -409,7 +407,7 @@ public class GaussNewtonOptimizerTest {
}
@Test(expected=ConvergenceException.class)
public void testCircleFittingBadInit() throws MathUserException {
public void testCircleFittingBadInit() {
CircleVectorial circle = new CircleVectorial();
double[][] points = circlePoints;
double[] target = new double[points.length];
@ -427,7 +425,7 @@ public class GaussNewtonOptimizerTest {
}
@Test
public void testCircleFittingGoodInit() throws MathUserException {
public void testCircleFittingGoodInit() {
CircleVectorial circle = new CircleVectorial();
double[][] points = circlePoints;
double[] target = new double[points.length];

View File

@ -21,7 +21,6 @@ import java.io.Serializable;
import java.util.Arrays;
import org.apache.commons.math.exception.MathUserException;
import org.apache.commons.math.exception.TooManyEvaluationsException;
import org.apache.commons.math.analysis.DifferentiableMultivariateVectorialFunction;
import org.apache.commons.math.analysis.MultivariateMatrixFunction;
@ -519,8 +518,6 @@ public class MinpackTest {
function.checkTheoreticalMinParams(optimum);
} catch (TooManyEvaluationsException e) {
Assert.assertTrue(exceptionExpected);
} catch (MathUserException fe) {
Assert.assertTrue(exceptionExpected);
}
}

View File

@ -17,7 +17,6 @@
package org.apache.commons.math.optimization.univariate;
import org.apache.commons.math.exception.MathUserException;
import org.apache.commons.math.analysis.QuinticFunction;
import org.apache.commons.math.analysis.SinFunction;
import org.apache.commons.math.analysis.UnivariateRealFunction;
@ -79,7 +78,7 @@ public class MultiStartUnivariateRealOptimizerTest {
UnivariateRealFunction f = new UnivariateRealFunction() {
public double value(double x) {
if (x < 0) {
throw new MathUserException();
throw new LocalException();
}
return 0;
}
@ -93,11 +92,16 @@ public class MultiStartUnivariateRealOptimizerTest {
try {
optimizer.optimize(300, f, GoalType.MINIMIZE, -0.3, -0.2);
Assert.fail();
} catch (MathUserException e) {
} catch (LocalException e) {
// Expected.
}
// Ensure that the exception was thrown because no optimum was found.
Assert.assertTrue(optimizer.getOptima()[0] == null);
}
private static class LocalException extends RuntimeException {
private static final long serialVersionUID = 1194682757034350629L;
}
}