MATH-441
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:
parent
77c4e2f0c6
commit
3f353cbde4
|
@ -17,7 +17,6 @@
|
|||
|
||||
package org.apache.commons.math.optimization;
|
||||
|
||||
import org.apache.commons.math.exception.FunctionEvaluationException;
|
||||
import org.apache.commons.math.analysis.MultivariateRealFunction;
|
||||
|
||||
/**
|
||||
|
@ -45,8 +44,6 @@ public interface BaseMultivariateRealOptimizer<FUNC extends MultivariateRealFunc
|
|||
* @param startPoint Start point for optimization.
|
||||
* @return the point/value pair giving the optimal value for objective
|
||||
* function.
|
||||
* @throws org.apache.commons.math.exception.FunctionEvaluationException if the
|
||||
* objective function throws one during the search.
|
||||
* @throws org.apache.commons.math.exception.DimensionMismatchException
|
||||
* if the start point dimension is wrong.
|
||||
* @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
|
||||
* any argument is {@code null}.
|
||||
*/
|
||||
RealPointValuePair optimize(FUNC f, GoalType goalType, double[] startPoint)
|
||||
throws FunctionEvaluationException;
|
||||
RealPointValuePair optimize(FUNC f, GoalType goalType, double[] startPoint);
|
||||
}
|
||||
|
|
|
@ -17,7 +17,6 @@
|
|||
|
||||
package org.apache.commons.math.optimization;
|
||||
|
||||
import org.apache.commons.math.exception.FunctionEvaluationException;
|
||||
import org.apache.commons.math.analysis.MultivariateVectorialFunction;
|
||||
|
||||
/**
|
||||
|
@ -47,8 +46,6 @@ public interface BaseMultivariateVectorialOptimizer<FUNC extends MultivariateVec
|
|||
* @param startPoint Start point for optimization.
|
||||
* @return the point/value pair giving the optimal value for objective
|
||||
* function.
|
||||
* @throws FunctionEvaluationException if the objective function throws one
|
||||
* during the search.
|
||||
* @throws org.apache.commons.math.exception.DimensionMismatchException
|
||||
* if the start point dimension is wrong.
|
||||
* @throws org.apache.commons.math.exception.TooManyEvaluationsException
|
||||
|
@ -57,6 +54,5 @@ public interface BaseMultivariateVectorialOptimizer<FUNC extends MultivariateVec
|
|||
* any argument is {@code null}.
|
||||
*/
|
||||
VectorialPointValuePair optimize(FUNC f, double[] target, double[] weight,
|
||||
double[] startPoint)
|
||||
throws FunctionEvaluationException;
|
||||
double[] startPoint);
|
||||
}
|
||||
|
|
|
@ -17,8 +17,7 @@
|
|||
|
||||
package org.apache.commons.math.optimization;
|
||||
|
||||
import org.apache.commons.math.exception.FunctionEvaluationException;
|
||||
import org.apache.commons.math.MathRuntimeException;
|
||||
import org.apache.commons.math.exception.DimensionMismatchException;
|
||||
import org.apache.commons.math.analysis.MultivariateRealFunction;
|
||||
import org.apache.commons.math.analysis.MultivariateVectorialFunction;
|
||||
import org.apache.commons.math.exception.util.LocalizedFormats;
|
||||
|
@ -99,23 +98,21 @@ public class LeastSquaresConverter implements MultivariateRealFunction {
|
|||
* </p>
|
||||
* <p>
|
||||
* The array computed by the objective function, the observations array and the
|
||||
* weights array must have consistent sizes or a {@link FunctionEvaluationException} will be
|
||||
* triggered while computing the scalar objective.
|
||||
* weights array must have consistent sizes or a {@link DimensionMismatchException}
|
||||
* will be triggered while computing the scalar objective.
|
||||
* </p>
|
||||
* @param function vectorial residuals function to wrap
|
||||
* @param observations observations to be compared to objective function to compute residuals
|
||||
* @param weights weights to apply to the residuals
|
||||
* @exception IllegalArgumentException if the observations vector and the weights
|
||||
* vector dimensions don't match (objective function dimension is checked only when
|
||||
* @exception DimensionMismatchException if the observations vector and the weights
|
||||
* vector dimensions do not match (objective function dimension is checked only when
|
||||
* the {@link #value(double[])} method is called)
|
||||
*/
|
||||
public LeastSquaresConverter(final MultivariateVectorialFunction function,
|
||||
final double[] observations, final double[] weights)
|
||||
throws IllegalArgumentException {
|
||||
if (observations.length != weights.length) {
|
||||
throw MathRuntimeException.createIllegalArgumentException(
|
||||
LocalizedFormats.DIMENSIONS_MISMATCH_SIMPLE,
|
||||
observations.length, weights.length);
|
||||
throw new DimensionMismatchException(observations.length, weights.length);
|
||||
}
|
||||
this.function = function;
|
||||
this.observations = observations.clone();
|
||||
|
@ -132,23 +129,20 @@ public class LeastSquaresConverter implements MultivariateRealFunction {
|
|||
* </p>
|
||||
* <p>
|
||||
* 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.
|
||||
* </p>
|
||||
* @param function vectorial residuals function to wrap
|
||||
* @param observations observations to be compared to objective function to compute residuals
|
||||
* @param scale scaling matrix
|
||||
* @exception IllegalArgumentException if the observations vector and the scale
|
||||
* matrix dimensions don't match (objective function dimension is checked only when
|
||||
* @throws DimensionMismatchException if the observations vector and the scale
|
||||
* matrix dimensions do not match (objective function dimension is checked only when
|
||||
* the {@link #value(double[])} method is called)
|
||||
*/
|
||||
public LeastSquaresConverter(final MultivariateVectorialFunction function,
|
||||
final double[] observations, final RealMatrix scale)
|
||||
throws IllegalArgumentException {
|
||||
final double[] observations, final RealMatrix scale) {
|
||||
if (observations.length != scale.getColumnDimension()) {
|
||||
throw MathRuntimeException.createIllegalArgumentException(
|
||||
LocalizedFormats.DIMENSIONS_MISMATCH_SIMPLE,
|
||||
observations.length, scale.getColumnDimension());
|
||||
throw new DimensionMismatchException(observations.length, scale.getColumnDimension());
|
||||
}
|
||||
this.function = function;
|
||||
this.observations = observations.clone();
|
||||
|
@ -157,13 +151,11 @@ public class LeastSquaresConverter implements MultivariateRealFunction {
|
|||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
public double value(final double[] point) throws FunctionEvaluationException {
|
||||
|
||||
public double value(final double[] point) {
|
||||
// compute residuals
|
||||
final double[] residuals = function.value(point);
|
||||
if (residuals.length != observations.length) {
|
||||
throw new FunctionEvaluationException(point, LocalizedFormats.DIMENSIONS_MISMATCH_SIMPLE,
|
||||
residuals.length, observations.length);
|
||||
throw new DimensionMismatchException(residuals.length, observations.length);
|
||||
}
|
||||
for (int i = 0; i < residuals.length; ++i) {
|
||||
residuals[i] -= observations[i];
|
||||
|
@ -187,7 +179,5 @@ public class LeastSquaresConverter implements MultivariateRealFunction {
|
|||
}
|
||||
|
||||
return sumSquares;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -20,7 +20,6 @@ package org.apache.commons.math.optimization.direct;
|
|||
import java.util.Arrays;
|
||||
import java.util.Comparator;
|
||||
|
||||
import org.apache.commons.math.exception.FunctionEvaluationException;
|
||||
import org.apache.commons.math.analysis.MultivariateRealFunction;
|
||||
import org.apache.commons.math.exception.NotStrictlyPositiveException;
|
||||
import org.apache.commons.math.exception.DimensionMismatchException;
|
||||
|
@ -200,14 +199,11 @@ public abstract class AbstractSimplex {
|
|||
* @param evaluationFunction Evaluation function.
|
||||
* @param comparator Comparator to use to sort simplex vertices from best
|
||||
* to worst.
|
||||
* @throws FunctionEvaluationException if the function cannot be evaluated
|
||||
* at some point.
|
||||
* @throws org.apache.commons.math.exception.TooManyEvaluationsException
|
||||
* if the algorithm fails to converge.
|
||||
*/
|
||||
public abstract void iterate(final MultivariateRealFunction evaluationFunction,
|
||||
final Comparator<RealPointValuePair> comparator)
|
||||
throws FunctionEvaluationException;
|
||||
final Comparator<RealPointValuePair> comparator);
|
||||
|
||||
/**
|
||||
* Build an initial simplex.
|
||||
|
@ -241,14 +237,11 @@ public abstract class AbstractSimplex {
|
|||
*
|
||||
* @param evaluationFunction Evaluation function.
|
||||
* @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
|
||||
* if the maximal number of evaluations is exceeded.
|
||||
*/
|
||||
public void evaluate(final MultivariateRealFunction evaluationFunction,
|
||||
final Comparator<RealPointValuePair> comparator)
|
||||
throws FunctionEvaluationException {
|
||||
|
||||
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];
|
||||
|
|
|
@ -17,7 +17,6 @@
|
|||
|
||||
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.exception.MaxCountExceededException;
|
||||
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.
|
||||
* @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 exceeded.
|
||||
*/
|
||||
protected double computeObjectiveValue(double[] point)
|
||||
throws FunctionEvaluationException {
|
||||
protected double computeObjectiveValue(double[] point) {
|
||||
try {
|
||||
evaluations.incrementCount();
|
||||
} catch (MaxCountExceededException e) {
|
||||
|
@ -117,8 +114,7 @@ public abstract class BaseAbstractScalarOptimizer<FUNC extends MultivariateRealF
|
|||
/** {@inheritDoc} */
|
||||
public RealPointValuePair optimize(FUNC f,
|
||||
GoalType goalType,
|
||||
double[] startPoint)
|
||||
throws FunctionEvaluationException {
|
||||
double[] startPoint) {
|
||||
// Checks.
|
||||
if (f == null) {
|
||||
throw new NullArgumentException();
|
||||
|
@ -159,10 +155,7 @@ public abstract class BaseAbstractScalarOptimizer<FUNC extends MultivariateRealF
|
|||
/**
|
||||
* Perform the bulk of the optimization algorithm.
|
||||
*
|
||||
* @return the point/value pair giving the optimal value for objective function
|
||||
* @throws FunctionEvaluationException if the objective function throws one during
|
||||
* the search
|
||||
* @return the point/value pair giving the optimal value for objective function.
|
||||
*/
|
||||
protected abstract RealPointValuePair doOptimize()
|
||||
throws FunctionEvaluationException;
|
||||
protected abstract RealPointValuePair doOptimize();
|
||||
}
|
||||
|
|
|
@ -17,7 +17,6 @@
|
|||
|
||||
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.exception.MaxCountExceededException;
|
||||
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.
|
||||
* @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
|
||||
* exceeded.
|
||||
*/
|
||||
protected double[] computeObjectiveValue(double[] point)
|
||||
throws FunctionEvaluationException {
|
||||
protected double[] computeObjectiveValue(double[] point) {
|
||||
try {
|
||||
evaluations.incrementCount();
|
||||
} catch (MaxCountExceededException e) {
|
||||
|
@ -119,8 +116,7 @@ public abstract class BaseAbstractVectorialOptimizer<FUNC extends MultivariateVe
|
|||
/** {@inheritDoc} */
|
||||
public VectorialPointValuePair optimize(FUNC f,
|
||||
double[] t, double[] w,
|
||||
double[] startPoint)
|
||||
throws FunctionEvaluationException {
|
||||
double[] startPoint) {
|
||||
// Checks.
|
||||
if (f == null) {
|
||||
throw new NullArgumentException();
|
||||
|
@ -162,11 +158,8 @@ public abstract class BaseAbstractVectorialOptimizer<FUNC extends MultivariateVe
|
|||
* Perform the bulk of the optimization algorithm.
|
||||
*
|
||||
* @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()
|
||||
throws FunctionEvaluationException;
|
||||
protected abstract VectorialPointValuePair doOptimize();
|
||||
|
||||
/**
|
||||
* @return a reference to the {@link #target array}.
|
||||
|
|
|
@ -19,7 +19,6 @@ package org.apache.commons.math.optimization.direct;
|
|||
|
||||
import java.util.Comparator;
|
||||
|
||||
import org.apache.commons.math.exception.FunctionEvaluationException;
|
||||
import org.apache.commons.math.analysis.MultivariateRealFunction;
|
||||
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 FunctionEvaluationException {
|
||||
final Comparator<RealPointValuePair> comparator) {
|
||||
// Save the original simplex.
|
||||
final RealPointValuePair[] original = getPoints();
|
||||
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
|
||||
* to poorest.
|
||||
* @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
|
||||
* if the maximal number of evaluations is exceeded.
|
||||
*/
|
||||
private RealPointValuePair evaluateNewSimplex(final MultivariateRealFunction evaluationFunction,
|
||||
final RealPointValuePair[] original,
|
||||
final double coeff,
|
||||
final Comparator<RealPointValuePair> comparator)
|
||||
throws FunctionEvaluationException {
|
||||
final Comparator<RealPointValuePair> comparator) {
|
||||
final double[] xSmallest = original[0].getPointRef();
|
||||
// Perform a linear transformation on all the simplex points,
|
||||
// except the first one.
|
||||
|
|
|
@ -19,7 +19,6 @@ package org.apache.commons.math.optimization.direct;
|
|||
|
||||
import java.util.Comparator;
|
||||
|
||||
import org.apache.commons.math.exception.FunctionEvaluationException;
|
||||
import org.apache.commons.math.optimization.RealPointValuePair;
|
||||
import org.apache.commons.math.analysis.MultivariateRealFunction;
|
||||
|
||||
|
@ -186,9 +185,7 @@ public class NelderMeadSimplex extends AbstractSimplex {
|
|||
/** {@inheritDoc} */
|
||||
@Override
|
||||
public void iterate(final MultivariateRealFunction evaluationFunction,
|
||||
final Comparator<RealPointValuePair> comparator)
|
||||
throws FunctionEvaluationException {
|
||||
|
||||
final Comparator<RealPointValuePair> comparator) {
|
||||
// The simplex has n + 1 points if dimension is n.
|
||||
final int n = getDimension();
|
||||
|
||||
|
|
|
@ -17,7 +17,6 @@
|
|||
|
||||
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.analysis.UnivariateRealFunction;
|
||||
import org.apache.commons.math.analysis.MultivariateRealFunction;
|
||||
|
@ -108,8 +107,7 @@ public class PowellOptimizer
|
|||
|
||||
/** {@inheritDoc} */
|
||||
@Override
|
||||
protected RealPointValuePair doOptimize()
|
||||
throws FunctionEvaluationException {
|
||||
protected RealPointValuePair doOptimize() {
|
||||
final GoalType goal = getGoalType();
|
||||
final double[] guess = getStartPoint();
|
||||
final int n = guess.length;
|
||||
|
@ -254,19 +252,14 @@ public class PowellOptimizer
|
|||
* @param p Starting point.
|
||||
* @param d Search direction.
|
||||
* @return the optimum.
|
||||
* @throws FunctionEvaluationException if the function evaluation
|
||||
* fails.
|
||||
* @throws org.apache.commons.math.exception.TooManyEvaluationsException
|
||||
* if the number of evaluations is exceeded.
|
||||
*/
|
||||
public UnivariateRealPointValuePair search(final double[] p,
|
||||
final double[] d)
|
||||
throws FunctionEvaluationException {
|
||||
|
||||
final double[] d) {
|
||||
final int n = p.length;
|
||||
final UnivariateRealFunction f = new UnivariateRealFunction() {
|
||||
public double value(double alpha)
|
||||
throws FunctionEvaluationException {
|
||||
public double value(double alpha) {
|
||||
final double[] x = new double[n];
|
||||
for (int i = 0; i < n; i++) {
|
||||
x[i] = p[i] + alpha * d[i];
|
||||
|
|
|
@ -19,7 +19,6 @@ package org.apache.commons.math.optimization.direct;
|
|||
|
||||
import java.util.Comparator;
|
||||
|
||||
import org.apache.commons.math.exception.FunctionEvaluationException;
|
||||
import org.apache.commons.math.analysis.MultivariateRealFunction;
|
||||
import org.apache.commons.math.exception.NullArgumentException;
|
||||
import org.apache.commons.math.optimization.GoalType;
|
||||
|
@ -108,8 +107,7 @@ public class SimplexOptimizer
|
|||
|
||||
/** {@inheritDoc} */
|
||||
@Override
|
||||
protected RealPointValuePair doOptimize()
|
||||
throws FunctionEvaluationException {
|
||||
protected RealPointValuePair doOptimize() {
|
||||
if (simplex == null) {
|
||||
throw new NullArgumentException();
|
||||
}
|
||||
|
@ -118,8 +116,7 @@ public class SimplexOptimizer
|
|||
// evaluations counter.
|
||||
final MultivariateRealFunction evalFunc
|
||||
= new MultivariateRealFunction() {
|
||||
public double value(double[] point)
|
||||
throws FunctionEvaluationException {
|
||||
public double value(double[] point) {
|
||||
return computeObjectiveValue(point);
|
||||
}
|
||||
};
|
||||
|
|
|
@ -20,7 +20,6 @@ package org.apache.commons.math.optimization.fitting;
|
|||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.apache.commons.math.exception.FunctionEvaluationException;
|
||||
import org.apache.commons.math.analysis.DifferentiableMultivariateVectorialFunction;
|
||||
import org.apache.commons.math.analysis.MultivariateMatrixFunction;
|
||||
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.
|
||||
* <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 y observed value of the point at x, after fitting we should
|
||||
* have f(x) as close as possible to this value
|
||||
|
@ -117,17 +116,13 @@ public class CurveFitter {
|
|||
* @param f parametric function to fit
|
||||
* @param initialGuess first guess of the function parameters
|
||||
* @return fitted parameters
|
||||
* @exception FunctionEvaluationException if the objective function throws one during
|
||||
* the search
|
||||
* @exception org.apache.commons.math.exception.ConvergenceException
|
||||
* if the algorithm failed to converge.
|
||||
* @exception org.apache.commons.math.exception.DimensionMismatchException
|
||||
* if the start point dimension is wrong.
|
||||
*/
|
||||
public double[] fit(final ParametricRealFunction f,
|
||||
final double[] initialGuess)
|
||||
throws FunctionEvaluationException {
|
||||
|
||||
final double[] initialGuess) {
|
||||
// prepare least squares problem
|
||||
double[] target = new double[observations.size()];
|
||||
double[] weights = new double[observations.size()];
|
||||
|
@ -149,7 +144,6 @@ public class CurveFitter {
|
|||
/** Vectorial function computing function theoretical values. */
|
||||
private class TheoreticalValuesFunction
|
||||
implements DifferentiableMultivariateVectorialFunction {
|
||||
|
||||
/** Function to fit. */
|
||||
private final ParametricRealFunction f;
|
||||
|
||||
|
@ -163,9 +157,7 @@ public class CurveFitter {
|
|||
/** {@inheritDoc} */
|
||||
public MultivariateMatrixFunction jacobian() {
|
||||
return new MultivariateMatrixFunction() {
|
||||
public double[][] value(double[] point)
|
||||
throws FunctionEvaluationException, IllegalArgumentException {
|
||||
|
||||
public double[][] value(double[] point) {
|
||||
final double[][] jacobian = new double[observations.size()][];
|
||||
|
||||
int i = 0;
|
||||
|
@ -174,15 +166,12 @@ public class CurveFitter {
|
|||
}
|
||||
|
||||
return jacobian;
|
||||
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
public double[] value(double[] point)
|
||||
throws FunctionEvaluationException, IllegalArgumentException {
|
||||
|
||||
public double[] value(double[] point) {
|
||||
// compute the residuals
|
||||
final double[] values = new double[observations.size()];
|
||||
int i = 0;
|
||||
|
@ -191,9 +180,6 @@ public class CurveFitter {
|
|||
}
|
||||
|
||||
return values;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -19,7 +19,6 @@ package org.apache.commons.math.optimization.fitting;
|
|||
|
||||
import java.io.Serializable;
|
||||
|
||||
import org.apache.commons.math.exception.FunctionEvaluationException;
|
||||
import org.apache.commons.math.analysis.UnivariateRealFunction;
|
||||
import org.apache.commons.math.exception.DimensionMismatchException;
|
||||
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:
|
||||
* <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>
|
||||
* Notation key:
|
||||
* <ul>
|
||||
* <li><tt>x^n</tt>: <tt>x</tt> raised to the power of <tt>n</tt>
|
||||
* <li><tt>exp(x)</tt>: <i>e</i><tt>^x</tt>
|
||||
* <li>{@code x^n}: {@code x} raised to the power of {@code n}
|
||||
* <li>{@code exp(x)}: <i>e</i><sup>x</sup>
|
||||
* </ul>
|
||||
*
|
||||
* @since 2.2
|
||||
* @version $Revision$ $Date$
|
||||
*/
|
||||
public class GaussianDerivativeFunction implements UnivariateRealFunction, Serializable {
|
||||
|
||||
/** Serializable version identifier. */
|
||||
private static final long serialVersionUID = -6500229089670174766L;
|
||||
|
||||
/** Parameter b of this function. */
|
||||
private final double b;
|
||||
|
||||
/** Parameter c of this function. */
|
||||
private final double c;
|
||||
|
||||
/** Square of the parameter d of this function. */
|
||||
private final double d2;
|
||||
|
||||
/**
|
||||
* Constructs an instance with the specified parameters.
|
||||
*
|
||||
* @param b <tt>b</tt> parameter value
|
||||
* @param c <tt>c</tt> parameter value
|
||||
* @param d <tt>d</tt> parameter value
|
||||
* @param b {@code b} parameter value.
|
||||
* @param c {@code c} parameter value.
|
||||
* @param d {@code d} parameter value.
|
||||
*
|
||||
* @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.
|
||||
*
|
||||
* @param parameters <tt>b</tt>, <tt>c</tt>, and <tt>d</tt> parameter values
|
||||
*
|
||||
* @throws IllegalArgumentException if <code>parameters</code> is null,
|
||||
* <code>parameters</code> length is not 3, or if
|
||||
* <code>parameters[2]</code> is 0
|
||||
* @param parameters {@code b}, {@code c} and {@code d} parameter values.
|
||||
* @throws NullArgumentException if {@code parameters} is {@code null}.
|
||||
* @throws DimensionMismatchException if the size of {@code parameters} is
|
||||
* not 3.
|
||||
* @throws ZeroException if {@code parameters[2]} is 0.
|
||||
*/
|
||||
public GaussianDerivativeFunction(double[] parameters) {
|
||||
if (parameters == null) {
|
||||
|
@ -97,9 +92,8 @@ public class GaussianDerivativeFunction implements UnivariateRealFunction, Seria
|
|||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
public double value(double x) throws FunctionEvaluationException {
|
||||
public double value(double x) {
|
||||
final double xMc = x - c;
|
||||
return (-b / d2) * xMc * Math.exp(-(xMc * xMc) / (2.0 * d2));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -17,7 +17,6 @@
|
|||
|
||||
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.OptimizationException;
|
||||
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.08237071, 717104.0);
|
||||
* fitter.addObservedPoint(4.08366408, 620014.0);
|
||||
* GaussianFunction fitFunction = fitter.fit();
|
||||
* GaussianFunction fitFunction = fitter.fit();
|
||||
* </pre>
|
||||
*
|
||||
* @see ParametricGaussianFunction
|
||||
|
@ -50,7 +49,6 @@ import org.apache.commons.math.optimization.fitting.WeightedObservedPoint;
|
|||
* @version $Revision$ $Date$
|
||||
*/
|
||||
public class GaussianFitter {
|
||||
|
||||
/** Fitter used for fitting. */
|
||||
private final CurveFitter fitter;
|
||||
|
||||
|
@ -64,23 +62,23 @@ public class GaussianFitter {
|
|||
}
|
||||
|
||||
/**
|
||||
* Adds point (<code>x</code>, <code>y</code>) to list of observed points
|
||||
* with a weight of 1.0.
|
||||
* Adds point ({@code x}, {@code y}) to list of observed points
|
||||
* with a weight of 1.
|
||||
*
|
||||
* @param x <tt>x</tt> point value
|
||||
* @param y <tt>y</tt> point value
|
||||
* @param x Abscissa value.
|
||||
* @param y Ordinate value.
|
||||
*/
|
||||
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
|
||||
* with a weight of <code>weight</code>.
|
||||
* Adds point ({@code x}, {@code y}) to list of observed points
|
||||
* with a weight of {@code weight}.
|
||||
*
|
||||
* @param weight weight assigned to point
|
||||
* @param x <tt>x</tt> point value
|
||||
* @param y <tt>y</tt> point value
|
||||
* @param weight Weight assigned to the given point.
|
||||
* @param x Abscissa value.
|
||||
* @param y Ordinate value.
|
||||
*/
|
||||
public void addObservedPoint(double weight, double x, double y) {
|
||||
fitter.addObservedPoint(weight, x, y);
|
||||
|
@ -88,31 +86,23 @@ public class GaussianFitter {
|
|||
|
||||
/**
|
||||
* Fits Gaussian function to the observed points.
|
||||
* It will call {@link CurveFitter#fit()}.
|
||||
*
|
||||
* @return Gaussian function best fitting 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
|
||||
*
|
||||
* @return the Gaussian function that best fits the observed points.
|
||||
* @see CurveFitter
|
||||
*/
|
||||
public GaussianFunction fit()
|
||||
throws FunctionEvaluationException, OptimizationException {
|
||||
public GaussianFunction fit() {
|
||||
return new GaussianFunction(fitter.fit(new ParametricGaussianFunction(),
|
||||
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.
|
||||
*
|
||||
* @param observations points used to initialize the created
|
||||
* <code>GaussianParametersGuesser</code> instance
|
||||
*
|
||||
* @return new <code>GaussianParametersGuesser</code> instance
|
||||
* {@code GaussianParametersGuesser} instance.
|
||||
* @return a new {@code GaussianParametersGuesser} instance.
|
||||
*/
|
||||
protected GaussianParametersGuesser createParametersGuesser(WeightedObservedPoint[] observations) {
|
||||
return new GaussianParametersGuesser(observations);
|
||||
|
|
|
@ -19,7 +19,6 @@ package org.apache.commons.math.optimization.fitting;
|
|||
|
||||
import java.io.Serializable;
|
||||
|
||||
import org.apache.commons.math.exception.FunctionEvaluationException;
|
||||
import org.apache.commons.math.analysis.DifferentiableUnivariateRealFunction;
|
||||
import org.apache.commons.math.analysis.UnivariateRealFunction;
|
||||
import org.apache.commons.math.exception.DimensionMismatchException;
|
||||
|
@ -117,7 +116,7 @@ public class GaussianFunction implements DifferentiableUnivariateRealFunction, S
|
|||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
public double value(double x) throws FunctionEvaluationException {
|
||||
public double value(double x) {
|
||||
final double xMc = x - c;
|
||||
return a + b * Math.exp(-xMc * xMc / (2.0 * (d * d)));
|
||||
}
|
||||
|
|
|
@ -17,8 +17,7 @@
|
|||
|
||||
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.exception.NumberIsTooSmallException;
|
||||
import org.apache.commons.math.exception.util.LocalizedFormats;
|
||||
import org.apache.commons.math.optimization.DifferentiableMultivariateVectorialOptimizer;
|
||||
import org.apache.commons.math.optimization.OptimizationException;
|
||||
|
@ -72,39 +71,33 @@ public class HarmonicFitter {
|
|||
fitter.addObservedPoint(weight, x, y);
|
||||
}
|
||||
|
||||
/** Fit an harmonic function to the observed points.
|
||||
* @return harmonic function best fitting the observed points
|
||||
* @throws OptimizationException if the sample is too short or if
|
||||
* the first guess cannot be computed
|
||||
/**
|
||||
* Fit an harmonic function to the observed points.
|
||||
*
|
||||
* @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 {
|
||||
try {
|
||||
|
||||
// shall we compute the first guess of the parameters ourselves ?
|
||||
if (parameters == null) {
|
||||
final WeightedObservedPoint[] observations = fitter.getObservations();
|
||||
if (observations.length < 4) {
|
||||
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()
|
||||
};
|
||||
|
||||
// shall we compute the first guess of the parameters ourselves ?
|
||||
if (parameters == null) {
|
||||
final WeightedObservedPoint[] observations = fitter.getObservations();
|
||||
if (observations.length < 4) {
|
||||
throw new NumberIsTooSmallException(LocalizedFormats.INSUFFICIENT_OBSERVED_POINTS_IN_SAMPLE,
|
||||
observations.length, 4, true);
|
||||
}
|
||||
|
||||
double[] fitted = fitter.fit(new ParametricHarmonicFunction(), parameters);
|
||||
return new HarmonicFunction(fitted[0], fitted[1], fitted[2]);
|
||||
|
||||
} catch (FunctionEvaluationException fee) {
|
||||
// this should never happen
|
||||
throw MathRuntimeException.createInternalError(fee);
|
||||
HarmonicCoefficientsGuesser guesser = new HarmonicCoefficientsGuesser(observations);
|
||||
guesser.guess();
|
||||
parameters = new double[] {
|
||||
guesser.getGuessedAmplitude(),
|
||||
guesser.getGuessedPulsation(),
|
||||
guesser.getGuessedPhase()
|
||||
};
|
||||
}
|
||||
|
||||
double[] fitted = fitter.fit(new ParametricHarmonicFunction(), parameters);
|
||||
return new HarmonicFunction(fitted[0], fitted[1], fitted[2]);
|
||||
}
|
||||
|
||||
/** Parametric harmonic function. */
|
||||
|
|
|
@ -19,7 +19,6 @@ package org.apache.commons.math.optimization.fitting;
|
|||
|
||||
import java.io.Serializable;
|
||||
|
||||
import org.apache.commons.math.exception.FunctionEvaluationException;
|
||||
import org.apache.commons.math.exception.DimensionMismatchException;
|
||||
import org.apache.commons.math.exception.util.LocalizedFormats;
|
||||
import org.apache.commons.math.exception.ZeroException;
|
||||
|
@ -29,21 +28,21 @@ import org.apache.commons.math.optimization.fitting.ParametricRealFunction;
|
|||
/**
|
||||
* A Gaussian function. Specifically:
|
||||
* <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>
|
||||
* The parameters have the following meaning:
|
||||
* <ul>
|
||||
* <li><tt>a</tt> is a constant offset that shifts <tt>f(x)</tt> up or down
|
||||
* <li><tt>b</tt> is the height of the peak
|
||||
* <li><tt>c</tt> 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 a} is a constant offset that shifts {@code f(x)} up or down
|
||||
* <li>{@code b} is the height of the peak
|
||||
* <li>{@code c} is the position of the center of the peak
|
||||
* <li>{@code d} is related to the FWHM by {@code FWHM = 2*sqrt(2*ln(2))*d}
|
||||
* </ul>
|
||||
* Notation key:
|
||||
* <ul>
|
||||
* <li><tt>x^n</tt>: <tt>x</tt> raised to the power of <tt>n</tt>
|
||||
* <li><tt>exp(x)</tt>: <i>e</i><tt>^x</tt>
|
||||
* <li><tt>sqrt(x)</tt>: the square root of <tt>x</tt>
|
||||
* <li><tt>ln(x)</tt>: the natural logarithm of <tt>x</tt>
|
||||
* <li>{@code x^n}: {@code x} raised to the power of {@code n}
|
||||
* <li>{@code exp(x)}: e<sup>x</sup>
|
||||
* <li>{@code sqrt(x)}: square root of {@code x}
|
||||
* <li>{@code ln(x)}: natural logarithm of {@code x}
|
||||
* </ul>
|
||||
* References:
|
||||
* <ul>
|
||||
|
@ -55,33 +54,22 @@ import org.apache.commons.math.optimization.fitting.ParametricRealFunction;
|
|||
* @version $Revision$ $Date$
|
||||
*/
|
||||
public class ParametricGaussianFunction implements ParametricRealFunction, Serializable {
|
||||
|
||||
/** Serializable version Id. */
|
||||
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() {
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 {
|
||||
public double value(double x, double[] parameters) {
|
||||
validateParameters(parameters);
|
||||
final double a = parameters[0];
|
||||
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
|
||||
* where the parameters, <tt>a</tt>, <tt>b</tt>, <tt>c</tt>, and <tt>d</tt>,
|
||||
* are considered the variables, not <tt>x</tt>. That is, instead of
|
||||
* computing the gradient vector for the function <tt>f(x)</tt> (which would
|
||||
* just be the derivative of <tt>f(x)</tt> with respect to <tt>x</tt> since
|
||||
* where the parameters, {@code a}, {@code b}, {@code c}, and {@code d},
|
||||
* are considered the variables, not {@code x}. That is, instead of
|
||||
* computing the gradient vector for the function {@code f(x)} (which would
|
||||
* 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
|
||||
* function <tt>f(a, b, c, d) = a + b*exp(-((x - c)^2 / (2*d^2)))</tt>
|
||||
* treating the specified <tt>x</tt> as a constant.
|
||||
* function {@code f(a, b, c, d) = a + b*exp(-((x - c)^2 / (2*d^2)))}
|
||||
* treating the specified {@code x} as a constant.
|
||||
* <p>
|
||||
* The components of the computed gradient vector are the partial
|
||||
* derivatives of <tt>f(a, b, c, d)</tt> with respect to each variable.
|
||||
* That is, the partial derivative of <tt>f(a, b, c, d)</tt> with respect to
|
||||
* <tt>a</tt>, the partial derivative of <tt>f(a, b, c, d)</tt> with respect
|
||||
* to <tt>b</tt>, the partial derivative of <tt>f(a, b, c, d)</tt> with
|
||||
* respect to <tt>c</tt>, and the partial derivative of <tt>f(a, b, c,
|
||||
* d)</tt> with respect to <tt>d</tt>.
|
||||
* derivatives of {@code f(a, b, c, d)} with respect to each variable.
|
||||
* That is, the partial derivative of {@code f(a, b, c, d)} with respect to
|
||||
* {@code a}, the partial derivative of {@code f(a, b, c, d)} with respect
|
||||
* to {@code b}, the partial derivative of {@code f(a, b, c, d)} with
|
||||
* respect to {@code c}, and the partial derivative of {@code f(a, b, c,
|
||||
* d)} with respect to {@code d}.
|
||||
*
|
||||
* @param x <tt>x</tt> value to be used as constant in <tt>f(a, b, c,
|
||||
* d)</tt>
|
||||
* @param parameters values of <tt>a</tt>, <tt>b</tt>, <tt>c</tt>, and
|
||||
* <tt>d</tt> for computation of gradient vector of <tt>f(a, b, c,
|
||||
* d)</tt>
|
||||
*
|
||||
* @return gradient vector of <tt>f(a, b, c, d)</tt>
|
||||
*
|
||||
* @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[])}
|
||||
* @param x {@code x} value to be used as constant in {@code f(a, b, c, d)}.
|
||||
* @param parameters values of {@code a}, {@code b}, {@code c}, and
|
||||
* {@code d} for computation of gradient vector of {@code f(a, b, c, d)}.
|
||||
* @return the gradient vector of {@code f(a, b, c, d)}.
|
||||
* @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 double[] gradient(double x, double[] parameters) throws FunctionEvaluationException {
|
||||
|
||||
public double[] gradient(double x, double[] parameters) {
|
||||
validateParameters(parameters);
|
||||
final double b = parameters[1];
|
||||
final double c = parameters[2];
|
||||
|
@ -135,32 +119,27 @@ public class ParametricGaussianFunction implements ParametricRealFunction, Seria
|
|||
final double f = b * exp * xMc / d2;
|
||||
|
||||
return new double[] { 1.0, exp, f, f * xMc / d };
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
* <tt>d</tt>
|
||||
*
|
||||
* @throws IllegalArgumentException if <code>parameters</code> is
|
||||
* <code>null</code> or if <code>parameters</code> does not have
|
||||
* length == 4
|
||||
* @throws FunctionEvaluationException if <code>parameters[3]</code>
|
||||
* (<tt>d</tt>) is 0
|
||||
* @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.
|
||||
*/
|
||||
private void validateParameters(double[] parameters) throws FunctionEvaluationException {
|
||||
private void validateParameters(double[] parameters) {
|
||||
if (parameters == null) {
|
||||
throw new NullArgumentException(LocalizedFormats.INPUT_ARRAY);
|
||||
}
|
||||
if (parameters.length != 4) {
|
||||
throw new DimensionMismatchException(4, parameters.length);
|
||||
}
|
||||
if (parameters[3] == 0.0) {
|
||||
if (parameters[3] == 0) {
|
||||
throw new ZeroException();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -17,8 +17,6 @@
|
|||
|
||||
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
|
||||
* variable plus some extra parameters.
|
||||
|
@ -26,25 +24,21 @@ import org.apache.commons.math.exception.FunctionEvaluationException;
|
|||
* @version $Revision$ $Date$
|
||||
*/
|
||||
public interface ParametricRealFunction {
|
||||
|
||||
/**
|
||||
* Compute the value of the function.
|
||||
* @param x the point for which the function value should be computed
|
||||
* @param parameters function parameters
|
||||
* @return the value
|
||||
* @throws FunctionEvaluationException if the function evaluation fails
|
||||
*
|
||||
* @param x Point for which the function value should be computed.
|
||||
* @param parameters Function parameters.
|
||||
* @return the value.
|
||||
*/
|
||||
double value(double x, double[] parameters)
|
||||
throws FunctionEvaluationException;
|
||||
double value(double x, double[] 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
|
||||
* @return the value
|
||||
* @throws FunctionEvaluationException if the function evaluation fails
|
||||
*
|
||||
* @param x Point for which the function value should be computed.
|
||||
* @param parameters Function parameters.
|
||||
* @return the value.
|
||||
*/
|
||||
double[] gradient(double x, double[] parameters)
|
||||
throws FunctionEvaluationException;
|
||||
|
||||
double[] gradient(double x, double[] parameters);
|
||||
}
|
||||
|
|
|
@ -17,8 +17,6 @@
|
|||
|
||||
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.optimization.DifferentiableMultivariateVectorialOptimizer;
|
||||
|
||||
|
@ -76,12 +74,7 @@ public class PolynomialFitter {
|
|||
* if the algorithm failed to converge.
|
||||
*/
|
||||
public PolynomialFunction fit() {
|
||||
try {
|
||||
return new PolynomialFunction(fitter.fit(new ParametricPolynomial(), new double[degree + 1]));
|
||||
} catch (FunctionEvaluationException fee) {
|
||||
// this should never happen
|
||||
throw MathRuntimeException.createInternalError(fee);
|
||||
}
|
||||
return new PolynomialFunction(fitter.fit(new ParametricPolynomial(), new double[degree + 1]));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -90,8 +83,7 @@ public class PolynomialFitter {
|
|||
private static class ParametricPolynomial implements ParametricRealFunction {
|
||||
|
||||
/** {@inheritDoc} */
|
||||
public double[] gradient(double x, double[] parameters)
|
||||
throws FunctionEvaluationException {
|
||||
public double[] gradient(double x, double[] parameters) {
|
||||
final double[] gradient = new double[parameters.length];
|
||||
double xn = 1.0;
|
||||
for (int i = 0; i < parameters.length; ++i) {
|
||||
|
|
|
@ -95,8 +95,6 @@ public abstract class AbstractLeastSquaresOptimizer
|
|||
/**
|
||||
* 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
|
||||
* match problem dimension.
|
||||
*/
|
||||
|
@ -121,8 +119,6 @@ public abstract class AbstractLeastSquaresOptimizer
|
|||
|
||||
/**
|
||||
* 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
|
||||
* problem dimension.
|
||||
* @throws org.apache.commons.math.exception.TooManyEvaluationsException
|
||||
|
@ -176,8 +172,6 @@ public abstract class AbstractLeastSquaresOptimizer
|
|||
* Get the covariance matrix of the optimized parameters.
|
||||
*
|
||||
* @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
|
||||
* 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.
|
||||
*
|
||||
* @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
|
||||
* the covariances matrix cannot be computed.
|
||||
* @throws NumberIsTooSmallException if the number of degrees of freedom is not
|
||||
|
|
|
@ -17,7 +17,6 @@
|
|||
|
||||
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.MultivariateVectorialFunction;
|
||||
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.
|
||||
* @return the gradient at the specified point.
|
||||
* @throws FunctionEvaluationException if the function gradient cannot be
|
||||
* evaluated.
|
||||
* @throws org.apache.commons.math.exception.TooManyEvaluationsException
|
||||
* if the allowed number of evaluations is exceeded.
|
||||
*/
|
||||
protected double[] computeObjectiveGradient(final double[] evaluationPoint)
|
||||
throws FunctionEvaluationException {
|
||||
protected double[] computeObjectiveGradient(final double[] evaluationPoint) {
|
||||
return gradient.value(evaluationPoint);
|
||||
}
|
||||
|
||||
|
@ -77,8 +73,7 @@ public abstract class AbstractScalarDifferentiableOptimizer
|
|||
@Override
|
||||
public RealPointValuePair optimize(final DifferentiableMultivariateRealFunction f,
|
||||
final GoalType goalType,
|
||||
final double[] startPoint)
|
||||
throws FunctionEvaluationException {
|
||||
final double[] startPoint) {
|
||||
// Store optimization problem characteristics.
|
||||
gradient = f.gradient();
|
||||
|
||||
|
|
|
@ -18,7 +18,6 @@
|
|||
package org.apache.commons.math.optimization.general;
|
||||
|
||||
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.util.LocalizedFormats;
|
||||
import org.apache.commons.math.linear.BlockRealMatrix;
|
||||
|
@ -62,8 +61,7 @@ public class GaussNewtonOptimizer extends AbstractLeastSquaresOptimizer {
|
|||
|
||||
/** {@inheritDoc} */
|
||||
@Override
|
||||
public VectorialPointValuePair doOptimize()
|
||||
throws FunctionEvaluationException {
|
||||
public VectorialPointValuePair doOptimize() {
|
||||
|
||||
final ConvergenceChecker<VectorialPointValuePair> checker
|
||||
= getConvergenceChecker();
|
||||
|
|
|
@ -18,7 +18,6 @@ package org.apache.commons.math.optimization.general;
|
|||
|
||||
import java.util.Arrays;
|
||||
|
||||
import org.apache.commons.math.exception.FunctionEvaluationException;
|
||||
import org.apache.commons.math.exception.ConvergenceException;
|
||||
import org.apache.commons.math.exception.util.LocalizedFormats;
|
||||
import org.apache.commons.math.optimization.VectorialPointValuePair;
|
||||
|
@ -215,8 +214,7 @@ public class LevenbergMarquardtOptimizer extends AbstractLeastSquaresOptimizer {
|
|||
|
||||
/** {@inheritDoc} */
|
||||
@Override
|
||||
protected VectorialPointValuePair doOptimize()
|
||||
throws FunctionEvaluationException {
|
||||
protected VectorialPointValuePair doOptimize() {
|
||||
// arrays shared with the other private methods
|
||||
solvedCols = FastMath.min(rows, cols);
|
||||
diagR = new double[cols];
|
||||
|
|
|
@ -17,7 +17,6 @@
|
|||
|
||||
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.ConvergenceException;
|
||||
import org.apache.commons.math.analysis.UnivariateRealFunction;
|
||||
|
@ -109,8 +108,7 @@ public class NonLinearConjugateGradientOptimizer
|
|||
|
||||
/** {@inheritDoc} */
|
||||
@Override
|
||||
protected RealPointValuePair doOptimize()
|
||||
throws FunctionEvaluationException {
|
||||
protected RealPointValuePair doOptimize() {
|
||||
// Initialization.
|
||||
if (preconditioner == null) {
|
||||
preconditioner = new IdentityPreconditioner();
|
||||
|
@ -212,17 +210,16 @@ public class NonLinearConjugateGradientOptimizer
|
|||
}
|
||||
|
||||
/**
|
||||
* 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 h initial step to try
|
||||
* @return b such that f(a) and f(b) have opposite signs
|
||||
* @exception FunctionEvaluationException if the function cannot be computed
|
||||
* @exception MathIllegalStateException if no bracket can be found
|
||||
* 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 h initial step to try.
|
||||
* @return b such that f(a) and f(b) have opposite signs.
|
||||
* @exception MathIllegalStateException if no bracket can be found.
|
||||
*/
|
||||
private double findUpperBound(final UnivariateRealFunction f,
|
||||
final double a, final double h)
|
||||
throws FunctionEvaluationException {
|
||||
final double a, final double h) {
|
||||
final double yA = f.value(a);
|
||||
double yB = yA;
|
||||
for (double step = h; step < Double.MAX_VALUE; step *= FastMath.max(2, yA / yB)) {
|
||||
|
@ -266,7 +263,7 @@ public class NonLinearConjugateGradientOptimizer
|
|||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
public double value(double x) throws FunctionEvaluationException {
|
||||
public double value(double x) {
|
||||
|
||||
// current point in the search direction
|
||||
final double[] shiftedPoint = point.clone();
|
||||
|
|
|
@ -17,8 +17,6 @@
|
|||
|
||||
package org.apache.commons.math.optimization.general;
|
||||
|
||||
import org.apache.commons.math.exception.FunctionEvaluationException;
|
||||
|
||||
/**
|
||||
* This interface represents a preconditioner for differentiable scalar
|
||||
* objective function optimizers.
|
||||
|
@ -26,7 +24,6 @@ import org.apache.commons.math.exception.FunctionEvaluationException;
|
|||
* @since 2.0
|
||||
*/
|
||||
public interface Preconditioner {
|
||||
|
||||
/**
|
||||
* Precondition a search direction.
|
||||
* <p>
|
||||
|
@ -43,10 +40,6 @@ public interface Preconditioner {
|
|||
* @param point current point at which the search direction was computed
|
||||
* @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
|
||||
* @exception FunctionEvaluationException if no cost can be computed for the parameters
|
||||
* @exception IllegalArgumentException if point dimension is wrong
|
||||
*/
|
||||
double[] precondition(double[] point, double[] r)
|
||||
throws FunctionEvaluationException, IllegalArgumentException;
|
||||
|
||||
double[] precondition(double[] point, double[] r);
|
||||
}
|
||||
|
|
|
@ -17,7 +17,6 @@
|
|||
|
||||
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.exception.MaxCountExceededException;
|
||||
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.
|
||||
* @return the objective function value at specified point.
|
||||
* @throws FunctionEvaluationException if the function cannot be
|
||||
* evaluated.
|
||||
* @throws TooManyEvaluationsException if the maximal number of evaluations
|
||||
* is exceeded.
|
||||
*/
|
||||
protected double computeObjectiveValue(double point)
|
||||
throws FunctionEvaluationException {
|
||||
protected double computeObjectiveValue(double point) {
|
||||
try {
|
||||
evaluations.incrementCount();
|
||||
} catch (MaxCountExceededException e) {
|
||||
|
@ -114,8 +110,7 @@ public abstract class AbstractUnivariateRealOptimizer
|
|||
public UnivariateRealPointValuePair optimize(UnivariateRealFunction f,
|
||||
GoalType goalType,
|
||||
double min, double max,
|
||||
double startValue)
|
||||
throws FunctionEvaluationException {
|
||||
double startValue) {
|
||||
// Checks.
|
||||
if (f == null) {
|
||||
throw new NullArgumentException();
|
||||
|
@ -139,8 +134,7 @@ public abstract class AbstractUnivariateRealOptimizer
|
|||
/** {@inheritDoc} */
|
||||
public UnivariateRealPointValuePair optimize(UnivariateRealFunction f,
|
||||
GoalType goalType,
|
||||
double min, double max)
|
||||
throws FunctionEvaluationException {
|
||||
double min, double max) {
|
||||
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.
|
||||
* @throws TooManyEvaluationsException if the maximal number of evaluations
|
||||
* is exceeded.
|
||||
* @throws FunctionEvaluationException if an error occurs evaluating
|
||||
* the function.
|
||||
*/
|
||||
protected abstract UnivariateRealPointValuePair doOptimize()
|
||||
throws FunctionEvaluationException;
|
||||
protected abstract UnivariateRealPointValuePair doOptimize();
|
||||
}
|
||||
|
|
|
@ -17,7 +17,6 @@
|
|||
|
||||
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.optimization.BaseOptimizer;
|
||||
import org.apache.commons.math.optimization.GoalType;
|
||||
|
@ -52,14 +51,11 @@ public interface BaseUnivariateRealOptimizer<FUNC extends UnivariateRealFunction
|
|||
* if the maximum evaluation count is exceeded.
|
||||
* @throws org.apache.commons.math.exception.ConvergenceException
|
||||
* 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
|
||||
* do not satisfy the requirements specified by the optimizer.
|
||||
*/
|
||||
UnivariateRealPointValuePair optimize(FUNC f, GoalType goalType,
|
||||
double min, double max)
|
||||
throws FunctionEvaluationException;
|
||||
double min, double max);
|
||||
|
||||
/**
|
||||
* 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.
|
||||
* @throws org.apache.commons.math.exception.ConvergenceException 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
|
||||
* do not satisfy the requirements specified by the optimizer.
|
||||
* @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,
|
||||
double min, double max,
|
||||
double startValue)
|
||||
throws FunctionEvaluationException;
|
||||
double startValue);
|
||||
}
|
||||
|
|
|
@ -20,7 +20,6 @@ import org.apache.commons.math.util.Incrementor;
|
|||
import org.apache.commons.math.exception.NotStrictlyPositiveException;
|
||||
import org.apache.commons.math.exception.TooManyEvaluationsException;
|
||||
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.optimization.GoalType;
|
||||
|
||||
|
@ -109,14 +108,11 @@ public class BracketFinder {
|
|||
* @param xB Initial point.
|
||||
* @throws TooManyEvaluationsException if the maximum number of evaluations
|
||||
* is exceeded.
|
||||
* @throws FunctionEvaluationException if an error occurs evaluating
|
||||
* the function.
|
||||
*/
|
||||
public void search(UnivariateRealFunction func,
|
||||
GoalType goal,
|
||||
double xA,
|
||||
double xB)
|
||||
throws FunctionEvaluationException {
|
||||
double xB) {
|
||||
evaluations.resetCount();
|
||||
final boolean isMinim = goal == GoalType.MINIMIZE;
|
||||
|
||||
|
@ -280,13 +276,11 @@ public class BracketFinder {
|
|||
* @param f Function.
|
||||
* @param x Argument.
|
||||
* @return {@code f(x)}
|
||||
* @throws FunctionEvaluationException if function cannot be evaluated.
|
||||
* @throws TooManyEvaluationsException if the maximal number of evaluations is
|
||||
* exceeded.
|
||||
*/
|
||||
private double eval(UnivariateRealFunction f,
|
||||
double x)
|
||||
throws FunctionEvaluationException {
|
||||
double x) {
|
||||
try {
|
||||
evaluations.incrementCount();
|
||||
} catch (MaxCountExceededException e) {
|
||||
|
|
|
@ -16,7 +16,6 @@
|
|||
*/
|
||||
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.FastMath;
|
||||
import org.apache.commons.math.exception.NumberIsTooSmallException;
|
||||
|
@ -86,8 +85,7 @@ public class BrentOptimizer extends AbstractUnivariateRealOptimizer {
|
|||
|
||||
/** {@inheritDoc} */
|
||||
@Override
|
||||
protected UnivariateRealPointValuePair doOptimize()
|
||||
throws FunctionEvaluationException {
|
||||
protected UnivariateRealPointValuePair doOptimize() {
|
||||
final boolean isMinim = getGoalType() == GoalType.MINIMIZE;
|
||||
final double lo = getMin();
|
||||
final double mid = getStartValue();
|
||||
|
|
Loading…
Reference in New Issue