Removed occurences of "FunctionEvaluationException".


git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/trunk@1034996 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Gilles Sadowski 2010-11-14 13:41:13 +00:00
parent 77c4e2f0c6
commit 3f353cbde4
28 changed files with 175 additions and 356 deletions

View File

@ -17,7 +17,6 @@
package org.apache.commons.math.optimization; package org.apache.commons.math.optimization;
import org.apache.commons.math.exception.FunctionEvaluationException;
import org.apache.commons.math.analysis.MultivariateRealFunction; import org.apache.commons.math.analysis.MultivariateRealFunction;
/** /**
@ -45,8 +44,6 @@ public interface BaseMultivariateRealOptimizer<FUNC extends MultivariateRealFunc
* @param startPoint Start point for optimization. * @param startPoint Start point for optimization.
* @return the point/value pair giving the optimal value for objective * @return the point/value pair giving the optimal value for objective
* function. * function.
* @throws org.apache.commons.math.exception.FunctionEvaluationException if the
* objective function throws one during the search.
* @throws org.apache.commons.math.exception.DimensionMismatchException * @throws org.apache.commons.math.exception.DimensionMismatchException
* if the start point dimension is wrong. * if the start point dimension is wrong.
* @throws org.apache.commons.math.exception.TooManyEvaluationsException * @throws org.apache.commons.math.exception.TooManyEvaluationsException
@ -54,6 +51,5 @@ public interface BaseMultivariateRealOptimizer<FUNC extends MultivariateRealFunc
* @throws org.apache.commons.math.exception.NullArgumentException if * @throws org.apache.commons.math.exception.NullArgumentException if
* any argument is {@code null}. * any argument is {@code null}.
*/ */
RealPointValuePair optimize(FUNC f, GoalType goalType, double[] startPoint) RealPointValuePair optimize(FUNC f, GoalType goalType, double[] startPoint);
throws FunctionEvaluationException;
} }

View File

@ -17,7 +17,6 @@
package org.apache.commons.math.optimization; package org.apache.commons.math.optimization;
import org.apache.commons.math.exception.FunctionEvaluationException;
import org.apache.commons.math.analysis.MultivariateVectorialFunction; import org.apache.commons.math.analysis.MultivariateVectorialFunction;
/** /**
@ -47,8 +46,6 @@ public interface BaseMultivariateVectorialOptimizer<FUNC extends MultivariateVec
* @param startPoint Start point for optimization. * @param startPoint Start point for optimization.
* @return the point/value pair giving the optimal value for objective * @return the point/value pair giving the optimal value for objective
* function. * function.
* @throws FunctionEvaluationException if the objective function throws one
* during the search.
* @throws org.apache.commons.math.exception.DimensionMismatchException * @throws org.apache.commons.math.exception.DimensionMismatchException
* if the start point dimension is wrong. * if the start point dimension is wrong.
* @throws org.apache.commons.math.exception.TooManyEvaluationsException * @throws org.apache.commons.math.exception.TooManyEvaluationsException
@ -57,6 +54,5 @@ public interface BaseMultivariateVectorialOptimizer<FUNC extends MultivariateVec
* any argument is {@code null}. * any argument is {@code null}.
*/ */
VectorialPointValuePair optimize(FUNC f, double[] target, double[] weight, VectorialPointValuePair optimize(FUNC f, double[] target, double[] weight,
double[] startPoint) double[] startPoint);
throws FunctionEvaluationException;
} }

View File

@ -17,8 +17,7 @@
package org.apache.commons.math.optimization; package org.apache.commons.math.optimization;
import org.apache.commons.math.exception.FunctionEvaluationException; import org.apache.commons.math.exception.DimensionMismatchException;
import org.apache.commons.math.MathRuntimeException;
import org.apache.commons.math.analysis.MultivariateRealFunction; import org.apache.commons.math.analysis.MultivariateRealFunction;
import org.apache.commons.math.analysis.MultivariateVectorialFunction; import org.apache.commons.math.analysis.MultivariateVectorialFunction;
import org.apache.commons.math.exception.util.LocalizedFormats; import org.apache.commons.math.exception.util.LocalizedFormats;
@ -99,23 +98,21 @@ public class LeastSquaresConverter implements MultivariateRealFunction {
* </p> * </p>
* <p> * <p>
* The array computed by the objective function, the observations array and the * The array computed by the objective function, the observations array and the
* weights array must have consistent sizes or a {@link FunctionEvaluationException} will be * weights array must have consistent sizes or a {@link DimensionMismatchException}
* triggered while computing the scalar objective. * will be triggered while computing the scalar objective.
* </p> * </p>
* @param function vectorial residuals function to wrap * @param function vectorial residuals function to wrap
* @param observations observations to be compared to objective function to compute residuals * @param observations observations to be compared to objective function to compute residuals
* @param weights weights to apply to the residuals * @param weights weights to apply to the residuals
* @exception IllegalArgumentException if the observations vector and the weights * @exception DimensionMismatchException if the observations vector and the weights
* vector dimensions don't match (objective function dimension is checked only when * vector dimensions do not match (objective function dimension is checked only when
* the {@link #value(double[])} method is called) * the {@link #value(double[])} method is called)
*/ */
public LeastSquaresConverter(final MultivariateVectorialFunction function, public LeastSquaresConverter(final MultivariateVectorialFunction function,
final double[] observations, final double[] weights) final double[] observations, final double[] weights)
throws IllegalArgumentException { throws IllegalArgumentException {
if (observations.length != weights.length) { if (observations.length != weights.length) {
throw MathRuntimeException.createIllegalArgumentException( throw new DimensionMismatchException(observations.length, weights.length);
LocalizedFormats.DIMENSIONS_MISMATCH_SIMPLE,
observations.length, weights.length);
} }
this.function = function; this.function = function;
this.observations = observations.clone(); this.observations = observations.clone();
@ -132,23 +129,20 @@ public class LeastSquaresConverter implements MultivariateRealFunction {
* </p> * </p>
* <p> * <p>
* The array computed by the objective function, the observations array and the * The array computed by the objective function, the observations array and the
* the scaling matrix must have consistent sizes or a {@link FunctionEvaluationException} * the scaling matrix must have consistent sizes or a {@link DimensionMismatchException}
* will be triggered while computing the scalar objective. * will be triggered while computing the scalar objective.
* </p> * </p>
* @param function vectorial residuals function to wrap * @param function vectorial residuals function to wrap
* @param observations observations to be compared to objective function to compute residuals * @param observations observations to be compared to objective function to compute residuals
* @param scale scaling matrix * @param scale scaling matrix
* @exception IllegalArgumentException if the observations vector and the scale * @throws DimensionMismatchException if the observations vector and the scale
* matrix dimensions don't match (objective function dimension is checked only when * matrix dimensions do not match (objective function dimension is checked only when
* the {@link #value(double[])} method is called) * the {@link #value(double[])} method is called)
*/ */
public LeastSquaresConverter(final MultivariateVectorialFunction function, public LeastSquaresConverter(final MultivariateVectorialFunction function,
final double[] observations, final RealMatrix scale) final double[] observations, final RealMatrix scale) {
throws IllegalArgumentException {
if (observations.length != scale.getColumnDimension()) { if (observations.length != scale.getColumnDimension()) {
throw MathRuntimeException.createIllegalArgumentException( throw new DimensionMismatchException(observations.length, scale.getColumnDimension());
LocalizedFormats.DIMENSIONS_MISMATCH_SIMPLE,
observations.length, scale.getColumnDimension());
} }
this.function = function; this.function = function;
this.observations = observations.clone(); this.observations = observations.clone();
@ -157,13 +151,11 @@ public class LeastSquaresConverter implements MultivariateRealFunction {
} }
/** {@inheritDoc} */ /** {@inheritDoc} */
public double value(final double[] point) throws FunctionEvaluationException { public double value(final double[] point) {
// compute residuals // compute residuals
final double[] residuals = function.value(point); final double[] residuals = function.value(point);
if (residuals.length != observations.length) { if (residuals.length != observations.length) {
throw new FunctionEvaluationException(point, LocalizedFormats.DIMENSIONS_MISMATCH_SIMPLE, throw new DimensionMismatchException(residuals.length, observations.length);
residuals.length, observations.length);
} }
for (int i = 0; i < residuals.length; ++i) { for (int i = 0; i < residuals.length; ++i) {
residuals[i] -= observations[i]; residuals[i] -= observations[i];
@ -187,7 +179,5 @@ public class LeastSquaresConverter implements MultivariateRealFunction {
} }
return sumSquares; return sumSquares;
} }
} }

View File

@ -20,7 +20,6 @@ package org.apache.commons.math.optimization.direct;
import java.util.Arrays; import java.util.Arrays;
import java.util.Comparator; import java.util.Comparator;
import org.apache.commons.math.exception.FunctionEvaluationException;
import org.apache.commons.math.analysis.MultivariateRealFunction; import org.apache.commons.math.analysis.MultivariateRealFunction;
import org.apache.commons.math.exception.NotStrictlyPositiveException; import org.apache.commons.math.exception.NotStrictlyPositiveException;
import org.apache.commons.math.exception.DimensionMismatchException; import org.apache.commons.math.exception.DimensionMismatchException;
@ -200,14 +199,11 @@ public abstract class AbstractSimplex {
* @param evaluationFunction Evaluation function. * @param evaluationFunction Evaluation function.
* @param comparator Comparator to use to sort simplex vertices from best * @param comparator Comparator to use to sort simplex vertices from best
* to worst. * to worst.
* @throws FunctionEvaluationException if the function cannot be evaluated
* at some point.
* @throws org.apache.commons.math.exception.TooManyEvaluationsException * @throws org.apache.commons.math.exception.TooManyEvaluationsException
* if the algorithm fails to converge. * if the algorithm fails to converge.
*/ */
public abstract void iterate(final MultivariateRealFunction evaluationFunction, public abstract void iterate(final MultivariateRealFunction evaluationFunction,
final Comparator<RealPointValuePair> comparator) final Comparator<RealPointValuePair> comparator);
throws FunctionEvaluationException;
/** /**
* Build an initial simplex. * Build an initial simplex.
@ -241,14 +237,11 @@ public abstract class AbstractSimplex {
* *
* @param evaluationFunction Evaluation function. * @param evaluationFunction Evaluation function.
* @param comparator Comparator to use to sort simplex vertices from best to worst. * @param comparator Comparator to use to sort simplex vertices from best to worst.
* @throws FunctionEvaluationException if no value can be computed for the parameters.
* @throws org.apache.commons.math.exception.TooManyEvaluationsException * @throws org.apache.commons.math.exception.TooManyEvaluationsException
* if the maximal number of evaluations is exceeded. * if the maximal number of evaluations is exceeded.
*/ */
public void evaluate(final MultivariateRealFunction evaluationFunction, public void evaluate(final MultivariateRealFunction evaluationFunction,
final Comparator<RealPointValuePair> comparator) final Comparator<RealPointValuePair> comparator) {
throws FunctionEvaluationException {
// Evaluate the objective function at all non-evaluated simplex points. // Evaluate the objective function at all non-evaluated simplex points.
for (int i = 0; i < simplex.length; i++) { for (int i = 0; i < simplex.length; i++) {
final RealPointValuePair vertex = simplex[i]; final RealPointValuePair vertex = simplex[i];

View File

@ -17,7 +17,6 @@
package org.apache.commons.math.optimization.direct; package org.apache.commons.math.optimization.direct;
import org.apache.commons.math.exception.FunctionEvaluationException;
import org.apache.commons.math.util.Incrementor; import org.apache.commons.math.util.Incrementor;
import org.apache.commons.math.exception.MaxCountExceededException; import org.apache.commons.math.exception.MaxCountExceededException;
import org.apache.commons.math.exception.TooManyEvaluationsException; import org.apache.commons.math.exception.TooManyEvaluationsException;
@ -100,12 +99,10 @@ public abstract class BaseAbstractScalarOptimizer<FUNC extends MultivariateRealF
* *
* @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 the specified point. * @return the objective function value at the specified point.
* @throws FunctionEvaluationException if the function cannot be evaluated.
* @throws TooManyEvaluationsException if the maximal number of * @throws TooManyEvaluationsException if the maximal number of
* evaluations is exceeded. * evaluations is exceeded.
*/ */
protected double computeObjectiveValue(double[] point) protected double computeObjectiveValue(double[] point) {
throws FunctionEvaluationException {
try { try {
evaluations.incrementCount(); evaluations.incrementCount();
} catch (MaxCountExceededException e) { } catch (MaxCountExceededException e) {
@ -117,8 +114,7 @@ public abstract class BaseAbstractScalarOptimizer<FUNC extends MultivariateRealF
/** {@inheritDoc} */ /** {@inheritDoc} */
public RealPointValuePair optimize(FUNC f, public RealPointValuePair optimize(FUNC f,
GoalType goalType, GoalType goalType,
double[] startPoint) double[] startPoint) {
throws FunctionEvaluationException {
// Checks. // Checks.
if (f == null) { if (f == null) {
throw new NullArgumentException(); throw new NullArgumentException();
@ -159,10 +155,7 @@ public abstract class BaseAbstractScalarOptimizer<FUNC extends MultivariateRealF
/** /**
* Perform the bulk of the optimization algorithm. * Perform the bulk of the optimization algorithm.
* *
* @return the point/value pair giving the optimal value for objective function * @return the point/value pair giving the optimal value for objective function.
* @throws FunctionEvaluationException if the objective function throws one during
* the search
*/ */
protected abstract RealPointValuePair doOptimize() protected abstract RealPointValuePair doOptimize();
throws FunctionEvaluationException;
} }

View File

@ -17,7 +17,6 @@
package org.apache.commons.math.optimization.direct; package org.apache.commons.math.optimization.direct;
import org.apache.commons.math.exception.FunctionEvaluationException;
import org.apache.commons.math.util.Incrementor; import org.apache.commons.math.util.Incrementor;
import org.apache.commons.math.exception.MaxCountExceededException; import org.apache.commons.math.exception.MaxCountExceededException;
import org.apache.commons.math.exception.TooManyEvaluationsException; import org.apache.commons.math.exception.TooManyEvaluationsException;
@ -102,12 +101,10 @@ public abstract class BaseAbstractVectorialOptimizer<FUNC extends MultivariateVe
* *
* @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 the specified point. * @return the objective function value at the specified point.
* @throws FunctionEvaluationException if the function cannot be evaluated.
* @throws TooManyEvaluationsException if the maximal number of evaluations is * @throws TooManyEvaluationsException if the maximal number of evaluations is
* exceeded. * exceeded.
*/ */
protected double[] computeObjectiveValue(double[] point) protected double[] computeObjectiveValue(double[] point) {
throws FunctionEvaluationException {
try { try {
evaluations.incrementCount(); evaluations.incrementCount();
} catch (MaxCountExceededException e) { } catch (MaxCountExceededException e) {
@ -119,8 +116,7 @@ public abstract class BaseAbstractVectorialOptimizer<FUNC extends MultivariateVe
/** {@inheritDoc} */ /** {@inheritDoc} */
public VectorialPointValuePair optimize(FUNC f, public VectorialPointValuePair optimize(FUNC f,
double[] t, double[] w, double[] t, double[] w,
double[] startPoint) double[] startPoint) {
throws FunctionEvaluationException {
// Checks. // Checks.
if (f == null) { if (f == null) {
throw new NullArgumentException(); throw new NullArgumentException();
@ -162,11 +158,8 @@ public abstract class BaseAbstractVectorialOptimizer<FUNC extends MultivariateVe
* Perform the bulk of the optimization algorithm. * Perform the bulk of the optimization algorithm.
* *
* @return the point/value pair giving the optimal value for objective function * @return the point/value pair giving the optimal value for objective function
* @throws FunctionEvaluationException if the objective function throws one during
* the search
*/ */
protected abstract VectorialPointValuePair doOptimize() protected abstract VectorialPointValuePair doOptimize();
throws FunctionEvaluationException;
/** /**
* @return a reference to the {@link #target array}. * @return a reference to the {@link #target array}.

View File

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

View File

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

View File

@ -17,7 +17,6 @@
package org.apache.commons.math.optimization.direct; package org.apache.commons.math.optimization.direct;
import org.apache.commons.math.exception.FunctionEvaluationException;
import org.apache.commons.math.util.FastMath; import org.apache.commons.math.util.FastMath;
import org.apache.commons.math.analysis.UnivariateRealFunction; import org.apache.commons.math.analysis.UnivariateRealFunction;
import org.apache.commons.math.analysis.MultivariateRealFunction; import org.apache.commons.math.analysis.MultivariateRealFunction;
@ -108,8 +107,7 @@ public class PowellOptimizer
/** {@inheritDoc} */ /** {@inheritDoc} */
@Override @Override
protected RealPointValuePair doOptimize() protected RealPointValuePair doOptimize() {
throws FunctionEvaluationException {
final GoalType goal = getGoalType(); final GoalType goal = getGoalType();
final double[] guess = getStartPoint(); final double[] guess = getStartPoint();
final int n = guess.length; final int n = guess.length;
@ -254,19 +252,14 @@ public class PowellOptimizer
* @param p Starting point. * @param p Starting point.
* @param d Search direction. * @param d Search direction.
* @return the optimum. * @return the optimum.
* @throws FunctionEvaluationException if the function evaluation
* fails.
* @throws org.apache.commons.math.exception.TooManyEvaluationsException * @throws org.apache.commons.math.exception.TooManyEvaluationsException
* if the number of evaluations is exceeded. * if the number of evaluations is exceeded.
*/ */
public UnivariateRealPointValuePair search(final double[] p, public UnivariateRealPointValuePair search(final double[] p,
final double[] d) final double[] d) {
throws FunctionEvaluationException {
final int n = p.length; final int n = p.length;
final UnivariateRealFunction f = new UnivariateRealFunction() { final UnivariateRealFunction f = new UnivariateRealFunction() {
public double value(double alpha) public double value(double alpha) {
throws FunctionEvaluationException {
final double[] x = new double[n]; final double[] x = new double[n];
for (int i = 0; i < n; i++) { for (int i = 0; i < n; i++) {
x[i] = p[i] + alpha * d[i]; x[i] = p[i] + alpha * d[i];

View File

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

View File

@ -20,7 +20,6 @@ package org.apache.commons.math.optimization.fitting;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import org.apache.commons.math.exception.FunctionEvaluationException;
import org.apache.commons.math.analysis.DifferentiableMultivariateVectorialFunction; import org.apache.commons.math.analysis.DifferentiableMultivariateVectorialFunction;
import org.apache.commons.math.analysis.MultivariateMatrixFunction; import org.apache.commons.math.analysis.MultivariateMatrixFunction;
import org.apache.commons.math.optimization.DifferentiableMultivariateVectorialOptimizer; import org.apache.commons.math.optimization.DifferentiableMultivariateVectorialOptimizer;
@ -57,7 +56,7 @@ public class CurveFitter {
/** Add an observed (x,y) point to the sample with unit weight. /** Add an observed (x,y) point to the sample with unit weight.
* <p>Calling this method is equivalent to call * <p>Calling this method is equivalent to call
* <code>addObservedPoint(1.0, x, y)</code>.</p> * {@code addObservedPoint(1.0, x, y)}.</p>
* @param x abscissa of the point * @param x abscissa of the point
* @param y observed value of the point at x, after fitting we should * @param y observed value of the point at x, after fitting we should
* have f(x) as close as possible to this value * have f(x) as close as possible to this value
@ -117,17 +116,13 @@ public class CurveFitter {
* @param f parametric function to fit * @param f parametric function to fit
* @param initialGuess first guess of the function parameters * @param initialGuess first guess of the function parameters
* @return fitted parameters * @return fitted parameters
* @exception FunctionEvaluationException if the objective function throws one during
* the search
* @exception org.apache.commons.math.exception.ConvergenceException * @exception org.apache.commons.math.exception.ConvergenceException
* if the algorithm failed to converge. * if the algorithm failed to converge.
* @exception org.apache.commons.math.exception.DimensionMismatchException * @exception org.apache.commons.math.exception.DimensionMismatchException
* if the start point dimension is wrong. * if the start point dimension is wrong.
*/ */
public double[] fit(final ParametricRealFunction f, public double[] fit(final ParametricRealFunction f,
final double[] initialGuess) final double[] initialGuess) {
throws FunctionEvaluationException {
// prepare least squares problem // prepare least squares problem
double[] target = new double[observations.size()]; double[] target = new double[observations.size()];
double[] weights = new double[observations.size()]; double[] weights = new double[observations.size()];
@ -149,7 +144,6 @@ public class CurveFitter {
/** Vectorial function computing function theoretical values. */ /** Vectorial function computing function theoretical values. */
private class TheoreticalValuesFunction private class TheoreticalValuesFunction
implements DifferentiableMultivariateVectorialFunction { implements DifferentiableMultivariateVectorialFunction {
/** Function to fit. */ /** Function to fit. */
private final ParametricRealFunction f; private final ParametricRealFunction f;
@ -163,9 +157,7 @@ public class CurveFitter {
/** {@inheritDoc} */ /** {@inheritDoc} */
public MultivariateMatrixFunction jacobian() { public MultivariateMatrixFunction jacobian() {
return new MultivariateMatrixFunction() { return new MultivariateMatrixFunction() {
public double[][] value(double[] point) public double[][] value(double[] point) {
throws FunctionEvaluationException, IllegalArgumentException {
final double[][] jacobian = new double[observations.size()][]; final double[][] jacobian = new double[observations.size()][];
int i = 0; int i = 0;
@ -174,15 +166,12 @@ public class CurveFitter {
} }
return jacobian; return jacobian;
} }
}; };
} }
/** {@inheritDoc} */ /** {@inheritDoc} */
public double[] value(double[] point) public double[] value(double[] point) {
throws FunctionEvaluationException, IllegalArgumentException {
// compute the residuals // compute the residuals
final double[] values = new double[observations.size()]; final double[] values = new double[observations.size()];
int i = 0; int i = 0;
@ -191,9 +180,6 @@ public class CurveFitter {
} }
return values; return values;
} }
} }
} }

View File

@ -19,7 +19,6 @@ package org.apache.commons.math.optimization.fitting;
import java.io.Serializable; import java.io.Serializable;
import org.apache.commons.math.exception.FunctionEvaluationException;
import org.apache.commons.math.analysis.UnivariateRealFunction; import org.apache.commons.math.analysis.UnivariateRealFunction;
import org.apache.commons.math.exception.DimensionMismatchException; import org.apache.commons.math.exception.DimensionMismatchException;
import org.apache.commons.math.exception.util.LocalizedFormats; import org.apache.commons.math.exception.util.LocalizedFormats;
@ -29,37 +28,33 @@ import org.apache.commons.math.exception.NullArgumentException;
/** /**
* The derivative of {@link GaussianFunction}. Specifically: * The derivative of {@link GaussianFunction}. Specifically:
* <p> * <p>
* <tt>f'(x) = (-b / (d^2)) * (x - c) * exp(-((x - c)^2) / (2*(d^2)))</tt> * {@code f'(x) = (-b / (d^2)) * (x - c) * exp(-((x - c)^2) / (2*(d^2)))}
* <p> * <p>
* Notation key: * Notation key:
* <ul> * <ul>
* <li><tt>x^n</tt>: <tt>x</tt> raised to the power of <tt>n</tt> * <li>{@code x^n}: {@code x} raised to the power of {@code n}
* <li><tt>exp(x)</tt>: <i>e</i><tt>^x</tt> * <li>{@code exp(x)}: <i>e</i><sup>x</sup>
* </ul> * </ul>
* *
* @since 2.2 * @since 2.2
* @version $Revision$ $Date$ * @version $Revision$ $Date$
*/ */
public class GaussianDerivativeFunction implements UnivariateRealFunction, Serializable { public class GaussianDerivativeFunction implements UnivariateRealFunction, Serializable {
/** Serializable version identifier. */ /** Serializable version identifier. */
private static final long serialVersionUID = -6500229089670174766L; private static final long serialVersionUID = -6500229089670174766L;
/** Parameter b of this function. */ /** Parameter b of this function. */
private final double b; private final double b;
/** Parameter c of this function. */ /** Parameter c of this function. */
private final double c; private final double c;
/** Square of the parameter d of this function. */ /** Square of the parameter d of this function. */
private final double d2; private final double d2;
/** /**
* Constructs an instance with the specified parameters. * Constructs an instance with the specified parameters.
* *
* @param b <tt>b</tt> parameter value * @param b {@code b} parameter value.
* @param c <tt>c</tt> parameter value * @param c {@code c} parameter value.
* @param d <tt>d</tt> parameter value * @param d {@code d} parameter value.
* *
* @throws IllegalArgumentException if <code>d</code> is 0 * @throws IllegalArgumentException if <code>d</code> is 0
*/ */
@ -75,11 +70,11 @@ public class GaussianDerivativeFunction implements UnivariateRealFunction, Seria
/** /**
* Constructs an instance with the specified parameters. * Constructs an instance with the specified parameters.
* *
* @param parameters <tt>b</tt>, <tt>c</tt>, and <tt>d</tt> parameter values * @param parameters {@code b}, {@code c} and {@code d} parameter values.
* * @throws NullArgumentException if {@code parameters} is {@code null}.
* @throws IllegalArgumentException if <code>parameters</code> is null, * @throws DimensionMismatchException if the size of {@code parameters} is
* <code>parameters</code> length is not 3, or if * not 3.
* <code>parameters[2]</code> is 0 * @throws ZeroException if {@code parameters[2]} is 0.
*/ */
public GaussianDerivativeFunction(double[] parameters) { public GaussianDerivativeFunction(double[] parameters) {
if (parameters == null) { if (parameters == null) {
@ -97,9 +92,8 @@ public class GaussianDerivativeFunction implements UnivariateRealFunction, Seria
} }
/** {@inheritDoc} */ /** {@inheritDoc} */
public double value(double x) throws FunctionEvaluationException { public double value(double x) {
final double xMc = x - c; final double xMc = x - c;
return (-b / d2) * xMc * Math.exp(-(xMc * xMc) / (2.0 * d2)); return (-b / d2) * xMc * Math.exp(-(xMc * xMc) / (2.0 * d2));
} }
} }

View File

@ -17,7 +17,6 @@
package org.apache.commons.math.optimization.fitting; package org.apache.commons.math.optimization.fitting;
import org.apache.commons.math.exception.FunctionEvaluationException;
import org.apache.commons.math.optimization.DifferentiableMultivariateVectorialOptimizer; import org.apache.commons.math.optimization.DifferentiableMultivariateVectorialOptimizer;
import org.apache.commons.math.optimization.OptimizationException; import org.apache.commons.math.optimization.OptimizationException;
import org.apache.commons.math.optimization.fitting.CurveFitter; import org.apache.commons.math.optimization.fitting.CurveFitter;
@ -42,7 +41,7 @@ import org.apache.commons.math.optimization.fitting.WeightedObservedPoint;
* fitter.addObservedPoint(4.07525716, 1447024.0); * fitter.addObservedPoint(4.07525716, 1447024.0);
* fitter.addObservedPoint(4.08237071, 717104.0); * fitter.addObservedPoint(4.08237071, 717104.0);
* fitter.addObservedPoint(4.08366408, 620014.0); * fitter.addObservedPoint(4.08366408, 620014.0);
* GaussianFunction fitFunction = fitter.fit(); * GaussianFunction fitFunction = fitter.fit();
* </pre> * </pre>
* *
* @see ParametricGaussianFunction * @see ParametricGaussianFunction
@ -50,7 +49,6 @@ import org.apache.commons.math.optimization.fitting.WeightedObservedPoint;
* @version $Revision$ $Date$ * @version $Revision$ $Date$
*/ */
public class GaussianFitter { public class GaussianFitter {
/** Fitter used for fitting. */ /** Fitter used for fitting. */
private final CurveFitter fitter; private final CurveFitter fitter;
@ -64,23 +62,23 @@ public class GaussianFitter {
} }
/** /**
* Adds point (<code>x</code>, <code>y</code>) to list of observed points * Adds point ({@code x}, {@code y}) to list of observed points
* with a weight of 1.0. * with a weight of 1.
* *
* @param x <tt>x</tt> point value * @param x Abscissa value.
* @param y <tt>y</tt> point value * @param y Ordinate value.
*/ */
public void addObservedPoint(double x, double y) { public void addObservedPoint(double x, double y) {
addObservedPoint(1.0, x, y); addObservedPoint(1, x, y);
} }
/** /**
* Adds point (<code>x</code>, <code>y</code>) to list of observed points * Adds point ({@code x}, {@code y}) to list of observed points
* with a weight of <code>weight</code>. * with a weight of {@code weight}.
* *
* @param weight weight assigned to point * @param weight Weight assigned to the given point.
* @param x <tt>x</tt> point value * @param x Abscissa value.
* @param y <tt>y</tt> point value * @param y Ordinate value.
*/ */
public void addObservedPoint(double weight, double x, double y) { public void addObservedPoint(double weight, double x, double y) {
fitter.addObservedPoint(weight, x, y); fitter.addObservedPoint(weight, x, y);
@ -88,31 +86,23 @@ public class GaussianFitter {
/** /**
* Fits Gaussian function to the observed points. * Fits Gaussian function to the observed points.
* It will call {@link CurveFitter#fit()}.
* *
* @return Gaussian function best fitting the observed points * @return the Gaussian function that best fits the observed points.
*
* @throws FunctionEvaluationException if <code>CurveFitter.fit</code>
* throws it
* @throws OptimizationException if <code>CurveFitter.fit</code> throws it
* @throws IllegalArgumentException if <code>CurveFitter.fit</code> throws
* it
*
* @see CurveFitter * @see CurveFitter
*/ */
public GaussianFunction fit() public GaussianFunction fit() {
throws FunctionEvaluationException, OptimizationException {
return new GaussianFunction(fitter.fit(new ParametricGaussianFunction(), return new GaussianFunction(fitter.fit(new ParametricGaussianFunction(),
createParametersGuesser(fitter.getObservations()).guess())); createParametersGuesser(fitter.getObservations()).guess()));
} }
/** /**
* Factory method to create a <code>GaussianParametersGuesser</code> * Factory method to create a {@code GaussianParametersGuesser}
* instance initialized with the specified observations. * instance initialized with the specified observations.
* *
* @param observations points used to initialize the created * @param observations points used to initialize the created
* <code>GaussianParametersGuesser</code> instance * {@code GaussianParametersGuesser} instance.
* * @return a new {@code GaussianParametersGuesser} instance.
* @return new <code>GaussianParametersGuesser</code> instance
*/ */
protected GaussianParametersGuesser createParametersGuesser(WeightedObservedPoint[] observations) { protected GaussianParametersGuesser createParametersGuesser(WeightedObservedPoint[] observations) {
return new GaussianParametersGuesser(observations); return new GaussianParametersGuesser(observations);

View File

@ -19,7 +19,6 @@ package org.apache.commons.math.optimization.fitting;
import java.io.Serializable; import java.io.Serializable;
import org.apache.commons.math.exception.FunctionEvaluationException;
import org.apache.commons.math.analysis.DifferentiableUnivariateRealFunction; import org.apache.commons.math.analysis.DifferentiableUnivariateRealFunction;
import org.apache.commons.math.analysis.UnivariateRealFunction; import org.apache.commons.math.analysis.UnivariateRealFunction;
import org.apache.commons.math.exception.DimensionMismatchException; import org.apache.commons.math.exception.DimensionMismatchException;
@ -117,7 +116,7 @@ public class GaussianFunction implements DifferentiableUnivariateRealFunction, S
} }
/** {@inheritDoc} */ /** {@inheritDoc} */
public double value(double x) throws FunctionEvaluationException { public double value(double x) {
final double xMc = x - c; final double xMc = x - c;
return a + b * Math.exp(-xMc * xMc / (2.0 * (d * d))); return a + b * Math.exp(-xMc * xMc / (2.0 * (d * d)));
} }

View File

@ -17,8 +17,7 @@
package org.apache.commons.math.optimization.fitting; package org.apache.commons.math.optimization.fitting;
import org.apache.commons.math.exception.FunctionEvaluationException; import org.apache.commons.math.exception.NumberIsTooSmallException;
import org.apache.commons.math.MathRuntimeException;
import org.apache.commons.math.exception.util.LocalizedFormats; import org.apache.commons.math.exception.util.LocalizedFormats;
import org.apache.commons.math.optimization.DifferentiableMultivariateVectorialOptimizer; import org.apache.commons.math.optimization.DifferentiableMultivariateVectorialOptimizer;
import org.apache.commons.math.optimization.OptimizationException; import org.apache.commons.math.optimization.OptimizationException;
@ -72,39 +71,33 @@ public class HarmonicFitter {
fitter.addObservedPoint(weight, x, y); fitter.addObservedPoint(weight, x, y);
} }
/** Fit an harmonic function to the observed points. /**
* @return harmonic function best fitting the observed points * Fit an harmonic function to the observed points.
* @throws OptimizationException if the sample is too short or if *
* the first guess cannot be computed * @return harmonic Function that best fits the observed points.
* @throws NumberIsTooSmallException if the sample is too short or if
* the first guess cannot be computed.
*/ */
public HarmonicFunction fit() throws OptimizationException { public HarmonicFunction fit() throws OptimizationException {
try { // shall we compute the first guess of the parameters ourselves ?
if (parameters == null) {
// shall we compute the first guess of the parameters ourselves ? final WeightedObservedPoint[] observations = fitter.getObservations();
if (parameters == null) { if (observations.length < 4) {
final WeightedObservedPoint[] observations = fitter.getObservations(); throw new NumberIsTooSmallException(LocalizedFormats.INSUFFICIENT_OBSERVED_POINTS_IN_SAMPLE,
if (observations.length < 4) { observations.length, 4, true);
throw new OptimizationException(LocalizedFormats.INSUFFICIENT_OBSERVED_POINTS_IN_SAMPLE,
observations.length, 4);
}
HarmonicCoefficientsGuesser guesser = new HarmonicCoefficientsGuesser(observations);
guesser.guess();
parameters = new double[] {
guesser.getGuessedAmplitude(),
guesser.getGuessedPulsation(),
guesser.getGuessedPhase()
};
} }
double[] fitted = fitter.fit(new ParametricHarmonicFunction(), parameters); HarmonicCoefficientsGuesser guesser = new HarmonicCoefficientsGuesser(observations);
return new HarmonicFunction(fitted[0], fitted[1], fitted[2]); guesser.guess();
parameters = new double[] {
} catch (FunctionEvaluationException fee) { guesser.getGuessedAmplitude(),
// this should never happen guesser.getGuessedPulsation(),
throw MathRuntimeException.createInternalError(fee); guesser.getGuessedPhase()
};
} }
double[] fitted = fitter.fit(new ParametricHarmonicFunction(), parameters);
return new HarmonicFunction(fitted[0], fitted[1], fitted[2]);
} }
/** Parametric harmonic function. */ /** Parametric harmonic function. */

View File

@ -19,7 +19,6 @@ package org.apache.commons.math.optimization.fitting;
import java.io.Serializable; import java.io.Serializable;
import org.apache.commons.math.exception.FunctionEvaluationException;
import org.apache.commons.math.exception.DimensionMismatchException; import org.apache.commons.math.exception.DimensionMismatchException;
import org.apache.commons.math.exception.util.LocalizedFormats; import org.apache.commons.math.exception.util.LocalizedFormats;
import org.apache.commons.math.exception.ZeroException; import org.apache.commons.math.exception.ZeroException;
@ -29,21 +28,21 @@ import org.apache.commons.math.optimization.fitting.ParametricRealFunction;
/** /**
* A Gaussian function. Specifically: * A Gaussian function. Specifically:
* <p> * <p>
* <tt>f(x) = a + b*exp(-((x - c)^2 / (2*d^2)))</tt> * {@code f(x) = a + b*exp(-((x - c)^2 / (2*d^2)))}
* <p> * <p>
* The parameters have the following meaning: * The parameters have the following meaning:
* <ul> * <ul>
* <li><tt>a</tt> is a constant offset that shifts <tt>f(x)</tt> up or down * <li>{@code a} is a constant offset that shifts {@code f(x)} up or down
* <li><tt>b</tt> is the height of the peak * <li>{@code b} is the height of the peak
* <li><tt>c</tt> is the position of the center of the peak * <li>{@code c} is the position of the center of the peak
* <li><tt>d</tt> is related to the FWHM by <tt>FWHM = 2*sqrt(2*ln(2))*d</tt> * <li>{@code d} is related to the FWHM by {@code FWHM = 2*sqrt(2*ln(2))*d}
* </ul> * </ul>
* Notation key: * Notation key:
* <ul> * <ul>
* <li><tt>x^n</tt>: <tt>x</tt> raised to the power of <tt>n</tt> * <li>{@code x^n}: {@code x} raised to the power of {@code n}
* <li><tt>exp(x)</tt>: <i>e</i><tt>^x</tt> * <li>{@code exp(x)}: e<sup>x</sup>
* <li><tt>sqrt(x)</tt>: the square root of <tt>x</tt> * <li>{@code sqrt(x)}: square root of {@code x}
* <li><tt>ln(x)</tt>: the natural logarithm of <tt>x</tt> * <li>{@code ln(x)}: natural logarithm of {@code x}
* </ul> * </ul>
* References: * References:
* <ul> * <ul>
@ -55,33 +54,22 @@ import org.apache.commons.math.optimization.fitting.ParametricRealFunction;
* @version $Revision$ $Date$ * @version $Revision$ $Date$
*/ */
public class ParametricGaussianFunction implements ParametricRealFunction, Serializable { public class ParametricGaussianFunction implements ParametricRealFunction, Serializable {
/** Serializable version Id. */ /** Serializable version Id. */
private static final long serialVersionUID = -3875578602503903233L; private static final long serialVersionUID = -3875578602503903233L;
/** /**
* Constructs an instance. * Computes value of function {@code f(x)} for the specified {@code x} and
* parameters {@code a}, {@code b}, {@code c}, and {@code d}.
*
* @param x Value at which to compute the function.
* @return {@code f(x)}.
* @param parameters Values of {@code a}, {@code b}, {@code c}, and {@code d}.
* @throws NullArgumentException if {@code parameters} is {@code null}.
* @throws DimensionMismatchException if the size of {@code parameters} is
* not 4.
* @throws ZeroException if {@code parameters[3]} is 0.
*/ */
public ParametricGaussianFunction() { public double value(double x, double[] parameters) {
}
/**
* Computes value of function <tt>f(x)</tt> for the specified <tt>x</tt> and
* parameters <tt>a</tt>, <tt>b</tt>, <tt>c</tt>, and <tt>d</tt>.
*
* @param x <tt>x</tt> value
* @param parameters values of <tt>a</tt>, <tt>b</tt>, <tt>c</tt>, and
* <tt>d</tt>
*
* @return value of <tt>f(x)</tt> evaluated at <tt>x</tt> with the specified
* parameters
*
* @throws IllegalArgumentException if <code>parameters</code> is invalid as
* determined by {@link #validateParameters(double[])}
* @throws FunctionEvaluationException if <code>parameters</code> values are
* invalid as determined by {@link #validateParameters(double[])}
*/
public double value(double x, double[] parameters) throws FunctionEvaluationException {
validateParameters(parameters); validateParameters(parameters);
final double a = parameters[0]; final double a = parameters[0];
final double b = parameters[1]; final double b = parameters[1];
@ -93,37 +81,33 @@ public class ParametricGaussianFunction implements ParametricRealFunction, Seria
/** /**
* Computes the gradient vector for a four variable version of the function * Computes the gradient vector for a four variable version of the function
* where the parameters, <tt>a</tt>, <tt>b</tt>, <tt>c</tt>, and <tt>d</tt>, * where the parameters, {@code a}, {@code b}, {@code c}, and {@code d},
* are considered the variables, not <tt>x</tt>. That is, instead of * are considered the variables, not {@code x}. That is, instead of
* computing the gradient vector for the function <tt>f(x)</tt> (which would * computing the gradient vector for the function {@code f(x)} (which would
* just be the derivative of <tt>f(x)</tt> with respect to <tt>x</tt> since * just be the derivative of {@code f(x)} with respect to {@code x} since
* it's a one-dimensional function), computes the gradient vector for the * it's a one-dimensional function), computes the gradient vector for the
* function <tt>f(a, b, c, d) = a + b*exp(-((x - c)^2 / (2*d^2)))</tt> * function {@code f(a, b, c, d) = a + b*exp(-((x - c)^2 / (2*d^2)))}
* treating the specified <tt>x</tt> as a constant. * treating the specified {@code x} as a constant.
* <p> * <p>
* The components of the computed gradient vector are the partial * The components of the computed gradient vector are the partial
* derivatives of <tt>f(a, b, c, d)</tt> with respect to each variable. * derivatives of {@code f(a, b, c, d)} with respect to each variable.
* That is, the partial derivative of <tt>f(a, b, c, d)</tt> with respect to * That is, the partial derivative of {@code f(a, b, c, d)} with respect to
* <tt>a</tt>, the partial derivative of <tt>f(a, b, c, d)</tt> with respect * {@code a}, the partial derivative of {@code f(a, b, c, d)} with respect
* to <tt>b</tt>, the partial derivative of <tt>f(a, b, c, d)</tt> with * to {@code b}, the partial derivative of {@code f(a, b, c, d)} with
* respect to <tt>c</tt>, and the partial derivative of <tt>f(a, b, c, * respect to {@code c}, and the partial derivative of {@code f(a, b, c,
* d)</tt> with respect to <tt>d</tt>. * d)} with respect to {@code d}.
* *
* @param x <tt>x</tt> value to be used as constant in <tt>f(a, b, c, * @param x {@code x} value to be used as constant in {@code f(a, b, c, d)}.
* d)</tt> * @param parameters values of {@code a}, {@code b}, {@code c}, and
* @param parameters values of <tt>a</tt>, <tt>b</tt>, <tt>c</tt>, and * {@code d} for computation of gradient vector of {@code f(a, b, c, d)}.
* <tt>d</tt> for computation of gradient vector of <tt>f(a, b, c, * @return the gradient vector of {@code f(a, b, c, d)}.
* d)</tt> * @param parameters Values of {@code a}, {@code b}, {@code c}, and {@code d}.
* * @throws NullArgumentException if {@code parameters} is {@code null}.
* @return gradient vector of <tt>f(a, b, c, d)</tt> * @throws DimensionMismatchException if the size of {@code parameters} is
* * not 4.
* @throws IllegalArgumentException if <code>parameters</code> is invalid as * @throws ZeroException if {@code parameters[3]} is 0.
* determined by {@link #validateParameters(double[])}
* @throws FunctionEvaluationException if <code>parameters</code> values are
* invalid as determined by {@link #validateParameters(double[])}
*/ */
public double[] gradient(double x, double[] parameters) throws FunctionEvaluationException { public double[] gradient(double x, double[] parameters) {
validateParameters(parameters); validateParameters(parameters);
final double b = parameters[1]; final double b = parameters[1];
final double c = parameters[2]; final double c = parameters[2];
@ -135,32 +119,27 @@ public class ParametricGaussianFunction implements ParametricRealFunction, Seria
final double f = b * exp * xMc / d2; final double f = b * exp * xMc / d2;
return new double[] { 1.0, exp, f, f * xMc / d }; return new double[] { 1.0, exp, f, f * xMc / d };
} }
/** /**
* Validates parameters to ensure they are appropriate for the evaluation of * Validates parameters to ensure they are appropriate for the evaluation of
* the <code>value</code> and <code>gradient</code> methods. * the {@code value} and {@code gradient} methods.
* *
* @param parameters values of <tt>a</tt>, <tt>b</tt>, <tt>c</tt>, and * @param parameters Values of {@code a}, {@code b}, {@code c}, and {@code d}.
* <tt>d</tt> * @throws NullArgumentException if {@code parameters} is {@code null}.
* * @throws DimensionMismatchException if the size of {@code parameters} is
* @throws IllegalArgumentException if <code>parameters</code> is * not 4.
* <code>null</code> or if <code>parameters</code> does not have * @throws ZeroException if {@code parameters[3]} is 0.
* length == 4
* @throws FunctionEvaluationException if <code>parameters[3]</code>
* (<tt>d</tt>) is 0
*/ */
private void validateParameters(double[] parameters) throws FunctionEvaluationException { private void validateParameters(double[] parameters) {
if (parameters == null) { if (parameters == null) {
throw new NullArgumentException(LocalizedFormats.INPUT_ARRAY); throw new NullArgumentException(LocalizedFormats.INPUT_ARRAY);
} }
if (parameters.length != 4) { if (parameters.length != 4) {
throw new DimensionMismatchException(4, parameters.length); throw new DimensionMismatchException(4, parameters.length);
} }
if (parameters[3] == 0.0) { if (parameters[3] == 0) {
throw new ZeroException(); throw new ZeroException();
} }
} }
} }

View File

@ -17,8 +17,6 @@
package org.apache.commons.math.optimization.fitting; package org.apache.commons.math.optimization.fitting;
import org.apache.commons.math.exception.FunctionEvaluationException;
/** /**
* An interface representing a real function that depends on one independent * An interface representing a real function that depends on one independent
* variable plus some extra parameters. * variable plus some extra parameters.
@ -26,25 +24,21 @@ import org.apache.commons.math.exception.FunctionEvaluationException;
* @version $Revision$ $Date$ * @version $Revision$ $Date$
*/ */
public interface ParametricRealFunction { public interface ParametricRealFunction {
/** /**
* Compute the value of the function. * Compute the value of the function.
* @param x the point for which the function value should be computed *
* @param parameters function parameters * @param x Point for which the function value should be computed.
* @return the value * @param parameters Function parameters.
* @throws FunctionEvaluationException if the function evaluation fails * @return the value.
*/ */
double value(double x, double[] parameters) double value(double x, double[] parameters);
throws FunctionEvaluationException;
/** /**
* Compute the gradient of the function with respect to its parameters. * Compute the gradient of the function with respect to its parameters.
* @param x the point for which the function value should be computed *
* @param parameters function parameters * @param x Point for which the function value should be computed.
* @return the value * @param parameters Function parameters.
* @throws FunctionEvaluationException if the function evaluation fails * @return the value.
*/ */
double[] gradient(double x, double[] parameters) double[] gradient(double x, double[] parameters);
throws FunctionEvaluationException;
} }

View File

@ -17,8 +17,6 @@
package org.apache.commons.math.optimization.fitting; package org.apache.commons.math.optimization.fitting;
import org.apache.commons.math.exception.FunctionEvaluationException;
import org.apache.commons.math.MathRuntimeException;
import org.apache.commons.math.analysis.polynomials.PolynomialFunction; import org.apache.commons.math.analysis.polynomials.PolynomialFunction;
import org.apache.commons.math.optimization.DifferentiableMultivariateVectorialOptimizer; import org.apache.commons.math.optimization.DifferentiableMultivariateVectorialOptimizer;
@ -76,12 +74,7 @@ public class PolynomialFitter {
* if the algorithm failed to converge. * if the algorithm failed to converge.
*/ */
public PolynomialFunction fit() { public PolynomialFunction fit() {
try { return new PolynomialFunction(fitter.fit(new ParametricPolynomial(), new double[degree + 1]));
return new PolynomialFunction(fitter.fit(new ParametricPolynomial(), new double[degree + 1]));
} catch (FunctionEvaluationException fee) {
// this should never happen
throw MathRuntimeException.createInternalError(fee);
}
} }
/** /**
@ -90,8 +83,7 @@ public class PolynomialFitter {
private static class ParametricPolynomial implements ParametricRealFunction { private static class ParametricPolynomial implements ParametricRealFunction {
/** {@inheritDoc} */ /** {@inheritDoc} */
public double[] gradient(double x, double[] parameters) public double[] gradient(double x, double[] parameters) {
throws FunctionEvaluationException {
final double[] gradient = new double[parameters.length]; final double[] gradient = new double[parameters.length];
double xn = 1.0; double xn = 1.0;
for (int i = 0; i < parameters.length; ++i) { for (int i = 0; i < parameters.length; ++i) {

View File

@ -95,8 +95,6 @@ public abstract class AbstractLeastSquaresOptimizer
/** /**
* Update the jacobian matrix. * Update the jacobian matrix.
* *
* @throws org.apache.commons.math.exception.FunctionEvaluationException
* if the function Jacobian cannot be evaluated.
* @throws DimensionMismatchException if the Jacobian dimension does not * @throws DimensionMismatchException if the Jacobian dimension does not
* match problem dimension. * match problem dimension.
*/ */
@ -121,8 +119,6 @@ public abstract class AbstractLeastSquaresOptimizer
/** /**
* Update the residuals array and cost function value. * Update the residuals array and cost function value.
* @throws org.apache.commons.math.exception.FunctionEvaluationException
* if the function cannot be evaluated.
* @throws DimensionMismatchException if the dimension does not match the * @throws DimensionMismatchException if the dimension does not match the
* problem dimension. * problem dimension.
* @throws org.apache.commons.math.exception.TooManyEvaluationsException * @throws org.apache.commons.math.exception.TooManyEvaluationsException
@ -176,8 +172,6 @@ public abstract class AbstractLeastSquaresOptimizer
* Get the covariance matrix of the optimized parameters. * Get the covariance matrix of the optimized parameters.
* *
* @return the covariance matrix. * @return the covariance matrix.
* @throws org.apache.commons.math.exception.FunctionEvaluationException
* if the function Jacobian cannot be evaluated.
* @throws org.apache.commons.math.exception.SingularMatrixException * @throws org.apache.commons.math.exception.SingularMatrixException
* if the covariance matrix cannot be computed (singular problem). * if the covariance matrix cannot be computed (singular problem).
*/ */
@ -209,8 +203,6 @@ public abstract class AbstractLeastSquaresOptimizer
* Guessing is covariance-based: It only gives a rough order of magnitude. * Guessing is covariance-based: It only gives a rough order of magnitude.
* *
* @return errors in optimized parameters * @return errors in optimized parameters
* @throws org.apache.commons.math.exception.FunctionEvaluationException
* if the function Jacobian cannot be evaluated.
* @throws org.apache.commons.math.exception.SingularMatrixException if * @throws org.apache.commons.math.exception.SingularMatrixException if
* the covariances matrix cannot be computed. * the covariances matrix cannot be computed.
* @throws NumberIsTooSmallException if the number of degrees of freedom is not * @throws NumberIsTooSmallException if the number of degrees of freedom is not

View File

@ -17,7 +17,6 @@
package org.apache.commons.math.optimization.general; package org.apache.commons.math.optimization.general;
import org.apache.commons.math.exception.FunctionEvaluationException;
import org.apache.commons.math.analysis.DifferentiableMultivariateRealFunction; import org.apache.commons.math.analysis.DifferentiableMultivariateRealFunction;
import org.apache.commons.math.analysis.MultivariateVectorialFunction; import org.apache.commons.math.analysis.MultivariateVectorialFunction;
import org.apache.commons.math.optimization.DifferentiableMultivariateRealOptimizer; import org.apache.commons.math.optimization.DifferentiableMultivariateRealOptimizer;
@ -63,13 +62,10 @@ public abstract class AbstractScalarDifferentiableOptimizer
* *
* @param evaluationPoint Point at which the gradient must be evaluated. * @param evaluationPoint Point at which the gradient must be evaluated.
* @return the gradient at the specified point. * @return the gradient at the specified point.
* @throws FunctionEvaluationException if the function gradient cannot be
* evaluated.
* @throws org.apache.commons.math.exception.TooManyEvaluationsException * @throws org.apache.commons.math.exception.TooManyEvaluationsException
* if the allowed number of evaluations is exceeded. * if the allowed number of evaluations is exceeded.
*/ */
protected double[] computeObjectiveGradient(final double[] evaluationPoint) protected double[] computeObjectiveGradient(final double[] evaluationPoint) {
throws FunctionEvaluationException {
return gradient.value(evaluationPoint); return gradient.value(evaluationPoint);
} }
@ -77,8 +73,7 @@ public abstract class AbstractScalarDifferentiableOptimizer
@Override @Override
public RealPointValuePair optimize(final DifferentiableMultivariateRealFunction f, public RealPointValuePair optimize(final DifferentiableMultivariateRealFunction f,
final GoalType goalType, final GoalType goalType,
final double[] startPoint) final double[] startPoint) {
throws FunctionEvaluationException {
// Store optimization problem characteristics. // Store optimization problem characteristics.
gradient = f.gradient(); gradient = f.gradient();

View File

@ -18,7 +18,6 @@
package org.apache.commons.math.optimization.general; package org.apache.commons.math.optimization.general;
import org.apache.commons.math.exception.SingularMatrixException; import org.apache.commons.math.exception.SingularMatrixException;
import org.apache.commons.math.exception.FunctionEvaluationException;
import org.apache.commons.math.exception.ConvergenceException; import org.apache.commons.math.exception.ConvergenceException;
import org.apache.commons.math.exception.util.LocalizedFormats; import org.apache.commons.math.exception.util.LocalizedFormats;
import org.apache.commons.math.linear.BlockRealMatrix; import org.apache.commons.math.linear.BlockRealMatrix;
@ -62,8 +61,7 @@ public class GaussNewtonOptimizer extends AbstractLeastSquaresOptimizer {
/** {@inheritDoc} */ /** {@inheritDoc} */
@Override @Override
public VectorialPointValuePair doOptimize() public VectorialPointValuePair doOptimize() {
throws FunctionEvaluationException {
final ConvergenceChecker<VectorialPointValuePair> checker final ConvergenceChecker<VectorialPointValuePair> checker
= getConvergenceChecker(); = getConvergenceChecker();

View File

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

View File

@ -17,7 +17,6 @@
package org.apache.commons.math.optimization.general; package org.apache.commons.math.optimization.general;
import org.apache.commons.math.exception.FunctionEvaluationException;
import org.apache.commons.math.exception.MathIllegalStateException; import org.apache.commons.math.exception.MathIllegalStateException;
import org.apache.commons.math.exception.ConvergenceException; import org.apache.commons.math.exception.ConvergenceException;
import org.apache.commons.math.analysis.UnivariateRealFunction; import org.apache.commons.math.analysis.UnivariateRealFunction;
@ -109,8 +108,7 @@ public class NonLinearConjugateGradientOptimizer
/** {@inheritDoc} */ /** {@inheritDoc} */
@Override @Override
protected RealPointValuePair doOptimize() protected RealPointValuePair doOptimize() {
throws FunctionEvaluationException {
// Initialization. // Initialization.
if (preconditioner == null) { if (preconditioner == null) {
preconditioner = new IdentityPreconditioner(); preconditioner = new IdentityPreconditioner();
@ -212,17 +210,16 @@ public class NonLinearConjugateGradientOptimizer
} }
/** /**
* Find the upper bound b ensuring bracketing of a root between a and b * Find the upper bound b ensuring bracketing of a root between a and b.
* @param f function whose root must be bracketed *
* @param a lower bound of the interval * @param f function whose root must be bracketed.
* @param h initial step to try * @param a lower bound of the interval.
* @return b such that f(a) and f(b) have opposite signs * @param h initial step to try.
* @exception FunctionEvaluationException if the function cannot be computed * @return b such that f(a) and f(b) have opposite signs.
* @exception MathIllegalStateException if no bracket can be found * @exception MathIllegalStateException if no bracket can be found.
*/ */
private double findUpperBound(final UnivariateRealFunction f, private double findUpperBound(final UnivariateRealFunction f,
final double a, final double h) final double a, final double h) {
throws FunctionEvaluationException {
final double yA = f.value(a); final double yA = f.value(a);
double yB = yA; double yB = yA;
for (double step = h; step < Double.MAX_VALUE; step *= FastMath.max(2, yA / yB)) { for (double step = h; step < Double.MAX_VALUE; step *= FastMath.max(2, yA / yB)) {
@ -266,7 +263,7 @@ public class NonLinearConjugateGradientOptimizer
} }
/** {@inheritDoc} */ /** {@inheritDoc} */
public double value(double x) throws FunctionEvaluationException { public double value(double x) {
// current point in the search direction // current point in the search direction
final double[] shiftedPoint = point.clone(); final double[] shiftedPoint = point.clone();

View File

@ -17,8 +17,6 @@
package org.apache.commons.math.optimization.general; package org.apache.commons.math.optimization.general;
import org.apache.commons.math.exception.FunctionEvaluationException;
/** /**
* This interface represents a preconditioner for differentiable scalar * This interface represents a preconditioner for differentiable scalar
* objective function optimizers. * objective function optimizers.
@ -26,7 +24,6 @@ import org.apache.commons.math.exception.FunctionEvaluationException;
* @since 2.0 * @since 2.0
*/ */
public interface Preconditioner { public interface Preconditioner {
/** /**
* Precondition a search direction. * Precondition a search direction.
* <p> * <p>
@ -43,10 +40,6 @@ public interface Preconditioner {
* @param point current point at which the search direction was computed * @param point current point at which the search direction was computed
* @param r raw search direction (i.e. opposite of the gradient) * @param r raw search direction (i.e. opposite of the gradient)
* @return approximation of H<sup>-1</sup>r where H is the objective function hessian * @return approximation of H<sup>-1</sup>r where H is the objective function hessian
* @exception FunctionEvaluationException if no cost can be computed for the parameters
* @exception IllegalArgumentException if point dimension is wrong
*/ */
double[] precondition(double[] point, double[] r) double[] precondition(double[] point, double[] r);
throws FunctionEvaluationException, IllegalArgumentException;
} }

View File

@ -17,7 +17,6 @@
package org.apache.commons.math.optimization.univariate; package org.apache.commons.math.optimization.univariate;
import org.apache.commons.math.exception.FunctionEvaluationException;
import org.apache.commons.math.util.Incrementor; import org.apache.commons.math.util.Incrementor;
import org.apache.commons.math.exception.MaxCountExceededException; import org.apache.commons.math.exception.MaxCountExceededException;
import org.apache.commons.math.exception.TooManyEvaluationsException; import org.apache.commons.math.exception.TooManyEvaluationsException;
@ -95,13 +94,10 @@ public abstract class AbstractUnivariateRealOptimizer
* *
* @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 FunctionEvaluationException if the function cannot be
* evaluated.
* @throws TooManyEvaluationsException if the maximal number of evaluations * @throws TooManyEvaluationsException if the maximal number of evaluations
* is exceeded. * is exceeded.
*/ */
protected double computeObjectiveValue(double point) protected double computeObjectiveValue(double point) {
throws FunctionEvaluationException {
try { try {
evaluations.incrementCount(); evaluations.incrementCount();
} catch (MaxCountExceededException e) { } catch (MaxCountExceededException e) {
@ -114,8 +110,7 @@ public abstract class AbstractUnivariateRealOptimizer
public UnivariateRealPointValuePair optimize(UnivariateRealFunction f, public UnivariateRealPointValuePair optimize(UnivariateRealFunction f,
GoalType goalType, GoalType goalType,
double min, double max, double min, double max,
double startValue) double startValue) {
throws FunctionEvaluationException {
// Checks. // Checks.
if (f == null) { if (f == null) {
throw new NullArgumentException(); throw new NullArgumentException();
@ -139,8 +134,7 @@ public abstract class AbstractUnivariateRealOptimizer
/** {@inheritDoc} */ /** {@inheritDoc} */
public UnivariateRealPointValuePair optimize(UnivariateRealFunction f, public UnivariateRealPointValuePair optimize(UnivariateRealFunction f,
GoalType goalType, GoalType goalType,
double min, double max) double min, double max) {
throws FunctionEvaluationException {
return optimize(f, goalType, min, max, min + 0.5 * (max - min)); return optimize(f, goalType, min, max, min + 0.5 * (max - min));
} }
@ -165,9 +159,6 @@ public abstract class AbstractUnivariateRealOptimizer
* @return the optimum and its corresponding function value. * @return the optimum and its corresponding function value.
* @throws TooManyEvaluationsException if the maximal number of evaluations * @throws TooManyEvaluationsException if the maximal number of evaluations
* is exceeded. * is exceeded.
* @throws FunctionEvaluationException if an error occurs evaluating
* the function.
*/ */
protected abstract UnivariateRealPointValuePair doOptimize() protected abstract UnivariateRealPointValuePair doOptimize();
throws FunctionEvaluationException;
} }

View File

@ -17,7 +17,6 @@
package org.apache.commons.math.optimization.univariate; package org.apache.commons.math.optimization.univariate;
import org.apache.commons.math.exception.FunctionEvaluationException;
import org.apache.commons.math.analysis.UnivariateRealFunction; import org.apache.commons.math.analysis.UnivariateRealFunction;
import org.apache.commons.math.optimization.BaseOptimizer; import org.apache.commons.math.optimization.BaseOptimizer;
import org.apache.commons.math.optimization.GoalType; import org.apache.commons.math.optimization.GoalType;
@ -52,14 +51,11 @@ public interface BaseUnivariateRealOptimizer<FUNC extends UnivariateRealFunction
* if the maximum evaluation count is exceeded. * if the maximum evaluation count is exceeded.
* @throws org.apache.commons.math.exception.ConvergenceException * @throws org.apache.commons.math.exception.ConvergenceException
* if the optimizer detects a convergence problem. * if the optimizer detects a convergence problem.
* @throws FunctionEvaluationException if an error occurs evaluating the
* function.
* @throws IllegalArgumentException if {@code min > max} or the endpoints * @throws IllegalArgumentException if {@code min > max} or the endpoints
* do not satisfy the requirements specified by the optimizer. * do not satisfy the requirements specified by the optimizer.
*/ */
UnivariateRealPointValuePair optimize(FUNC f, GoalType goalType, UnivariateRealPointValuePair optimize(FUNC f, GoalType goalType,
double min, double max) double min, double max);
throws FunctionEvaluationException;
/** /**
* Find an optimum in the given interval, start at startValue. * Find an optimum in the given interval, start at startValue.
@ -76,8 +72,6 @@ public interface BaseUnivariateRealOptimizer<FUNC extends UnivariateRealFunction
* if the maximum evaluation count is exceeded. * if the maximum evaluation count is exceeded.
* @throws org.apache.commons.math.exception.ConvergenceException if the * @throws org.apache.commons.math.exception.ConvergenceException if the
* optimizer detects a convergence problem. * optimizer detects a convergence problem.
* @throws FunctionEvaluationException if an error occurs evaluating the
* function.
* @throws IllegalArgumentException if {@code min > max} or the endpoints * @throws IllegalArgumentException if {@code min > max} or the endpoints
* do not satisfy the requirements specified by the optimizer. * do not satisfy the requirements specified by the optimizer.
* @throws org.apache.commons.math.exception.NullArgumentException if any * @throws org.apache.commons.math.exception.NullArgumentException if any
@ -85,6 +79,5 @@ public interface BaseUnivariateRealOptimizer<FUNC extends UnivariateRealFunction
*/ */
UnivariateRealPointValuePair optimize(FUNC f, GoalType goalType, UnivariateRealPointValuePair optimize(FUNC f, GoalType goalType,
double min, double max, double min, double max,
double startValue) double startValue);
throws FunctionEvaluationException;
} }

View File

@ -20,7 +20,6 @@ import org.apache.commons.math.util.Incrementor;
import org.apache.commons.math.exception.NotStrictlyPositiveException; import org.apache.commons.math.exception.NotStrictlyPositiveException;
import org.apache.commons.math.exception.TooManyEvaluationsException; import org.apache.commons.math.exception.TooManyEvaluationsException;
import org.apache.commons.math.exception.MaxCountExceededException; import org.apache.commons.math.exception.MaxCountExceededException;
import org.apache.commons.math.exception.FunctionEvaluationException;
import org.apache.commons.math.analysis.UnivariateRealFunction; import org.apache.commons.math.analysis.UnivariateRealFunction;
import org.apache.commons.math.optimization.GoalType; import org.apache.commons.math.optimization.GoalType;
@ -109,14 +108,11 @@ public class BracketFinder {
* @param xB Initial point. * @param xB Initial point.
* @throws TooManyEvaluationsException if the maximum number of evaluations * @throws TooManyEvaluationsException if the maximum number of evaluations
* is exceeded. * is exceeded.
* @throws FunctionEvaluationException if an error occurs evaluating
* the function.
*/ */
public void search(UnivariateRealFunction func, public void search(UnivariateRealFunction func,
GoalType goal, GoalType goal,
double xA, double xA,
double xB) double xB) {
throws FunctionEvaluationException {
evaluations.resetCount(); evaluations.resetCount();
final boolean isMinim = goal == GoalType.MINIMIZE; final boolean isMinim = goal == GoalType.MINIMIZE;
@ -280,13 +276,11 @@ public class BracketFinder {
* @param f Function. * @param f Function.
* @param x Argument. * @param x Argument.
* @return {@code f(x)} * @return {@code f(x)}
* @throws FunctionEvaluationException if function cannot be evaluated.
* @throws TooManyEvaluationsException if the maximal number of evaluations is * @throws TooManyEvaluationsException if the maximal number of evaluations is
* exceeded. * exceeded.
*/ */
private double eval(UnivariateRealFunction f, private double eval(UnivariateRealFunction f,
double x) double x) {
throws FunctionEvaluationException {
try { try {
evaluations.incrementCount(); evaluations.incrementCount();
} catch (MaxCountExceededException e) { } catch (MaxCountExceededException e) {

View File

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