You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by er...@apache.org on 2012/12/12 15:11:04 UTC
svn commit: r1420684 [9/15] - in /commons/proper/math/trunk/src:
main/java/org/apache/commons/math3/exception/
main/java/org/apache/commons/math3/exception/util/
main/java/org/apache/commons/math3/fitting/
main/java/org/apache/commons/math3/optim/ main...
Added: commons/proper/math/trunk/src/main/java/org/apache/commons/math3/optim/univariate/SimpleUnivariateValueChecker.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math3/optim/univariate/SimpleUnivariateValueChecker.java?rev=1420684&view=auto
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math3/optim/univariate/SimpleUnivariateValueChecker.java (added)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math3/optim/univariate/SimpleUnivariateValueChecker.java Wed Dec 12 14:10:38 2012
@@ -0,0 +1,130 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.math3.optim.univariate;
+
+import org.apache.commons.math3.util.FastMath;
+import org.apache.commons.math3.exception.NotStrictlyPositiveException;
+import org.apache.commons.math3.optim.AbstractConvergenceChecker;
+
+/**
+ * Simple implementation of the
+ * {@link org.apache.commons.math3.optimization.ConvergenceChecker} interface
+ * that uses only objective function values.
+ *
+ * Convergence is considered to have been reached if either the relative
+ * difference between the objective function values is smaller than a
+ * threshold or if either the absolute difference between the objective
+ * function values is smaller than another threshold.
+ * <br/>
+ * The {@link #converged(int,UnivariatePointValuePair,UnivariatePointValuePair)
+ * converged} method will also return {@code true} if the number of iterations
+ * has been set (see {@link #SimpleUnivariateValueChecker(double,double,int)
+ * this constructor}).
+ *
+ * @version $Id: SimpleUnivariateValueChecker.java 1413171 2012-11-24 11:11:10Z erans $
+ * @since 3.1
+ */
+public class SimpleUnivariateValueChecker
+ extends AbstractConvergenceChecker<UnivariatePointValuePair> {
+ /**
+ * If {@link #maxIterationCount} is set to this value, the number of
+ * iterations will never cause
+ * {@link #converged(int,UnivariatePointValuePair,UnivariatePointValuePair)}
+ * to return {@code true}.
+ */
+ private static final int ITERATION_CHECK_DISABLED = -1;
+ /**
+ * Number of iterations after which the
+ * {@link #converged(int,UnivariatePointValuePair,UnivariatePointValuePair)}
+ * method will return true (unless the check is disabled).
+ */
+ private final int maxIterationCount;
+
+ /** Build an instance with specified thresholds.
+ *
+ * In order to perform only relative checks, the absolute tolerance
+ * must be set to a negative value. In order to perform only absolute
+ * checks, the relative tolerance must be set to a negative value.
+ *
+ * @param relativeThreshold relative tolerance threshold
+ * @param absoluteThreshold absolute tolerance threshold
+ */
+ public SimpleUnivariateValueChecker(final double relativeThreshold,
+ final double absoluteThreshold) {
+ super(relativeThreshold, absoluteThreshold);
+ maxIterationCount = ITERATION_CHECK_DISABLED;
+ }
+
+ /**
+ * Builds an instance with specified thresholds.
+ *
+ * In order to perform only relative checks, the absolute tolerance
+ * must be set to a negative value. In order to perform only absolute
+ * checks, the relative tolerance must be set to a negative value.
+ *
+ * @param relativeThreshold relative tolerance threshold
+ * @param absoluteThreshold absolute tolerance threshold
+ * @param maxIter Maximum iteration count.
+ * @throws NotStrictlyPositiveException if {@code maxIter <= 0}.
+ *
+ * @since 3.1
+ */
+ public SimpleUnivariateValueChecker(final double relativeThreshold,
+ final double absoluteThreshold,
+ final int maxIter) {
+ super(relativeThreshold, absoluteThreshold);
+
+ if (maxIter <= 0) {
+ throw new NotStrictlyPositiveException(maxIter);
+ }
+ maxIterationCount = maxIter;
+ }
+
+ /**
+ * Check if the optimization algorithm has converged considering the
+ * last two points.
+ * This method may be called several time from the same algorithm
+ * iteration with different points. This can be detected by checking the
+ * iteration number at each call if needed. Each time this method is
+ * called, the previous and current point correspond to points with the
+ * same role at each iteration, so they can be compared. As an example,
+ * simplex-based algorithms call this method for all points of the simplex,
+ * not only for the best or worst ones.
+ *
+ * @param iteration Index of current iteration
+ * @param previous Best point in the previous iteration.
+ * @param current Best point in the current iteration.
+ * @return {@code true} if the algorithm has converged.
+ */
+ @Override
+ public boolean converged(final int iteration,
+ final UnivariatePointValuePair previous,
+ final UnivariatePointValuePair current) {
+ if (maxIterationCount != ITERATION_CHECK_DISABLED) {
+ if (iteration >= maxIterationCount) {
+ return true;
+ }
+ }
+
+ final double p = previous.getValue();
+ final double c = current.getValue();
+ final double difference = FastMath.abs(p - c);
+ final double size = FastMath.max(FastMath.abs(p), FastMath.abs(c));
+ return difference <= size * getRelativeThreshold() ||
+ difference <= getAbsoluteThreshold();
+ }
+}
Propchange: commons/proper/math/trunk/src/main/java/org/apache/commons/math3/optim/univariate/SimpleUnivariateValueChecker.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: commons/proper/math/trunk/src/main/java/org/apache/commons/math3/optim/univariate/UnivariateObjectiveFunction.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math3/optim/univariate/UnivariateObjectiveFunction.java?rev=1420684&view=auto
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math3/optim/univariate/UnivariateObjectiveFunction.java (added)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math3/optim/univariate/UnivariateObjectiveFunction.java Wed Dec 12 14:10:38 2012
@@ -0,0 +1,47 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.math3.optim.univariate;
+
+import org.apache.commons.math3.analysis.UnivariateFunction;
+import org.apache.commons.math3.optim.OptimizationData;
+
+/**
+ * Scalar function to be optimized.
+ *
+ * @version $Id$
+ * @since 3.1
+ */
+public class UnivariateObjectiveFunction implements OptimizationData {
+ /** Function to be optimized. */
+ private final UnivariateFunction function;
+
+ /**
+ * @param f Function to be optimized.
+ */
+ public UnivariateObjectiveFunction(UnivariateFunction f) {
+ function = f;
+ }
+
+ /**
+ * Gets the function to be optimized.
+ *
+ * @return the objective function.
+ */
+ public UnivariateFunction getObjectiveFunction() {
+ return function;
+ }
+}
Propchange: commons/proper/math/trunk/src/main/java/org/apache/commons/math3/optim/univariate/UnivariateObjectiveFunction.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: commons/proper/math/trunk/src/main/java/org/apache/commons/math3/optim/univariate/UnivariateOptimizer.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math3/optim/univariate/UnivariateOptimizer.java?rev=1420684&view=auto
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math3/optim/univariate/UnivariateOptimizer.java (added)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math3/optim/univariate/UnivariateOptimizer.java Wed Dec 12 14:10:38 2012
@@ -0,0 +1,148 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.math3.optim.univariate;
+
+import org.apache.commons.math3.analysis.UnivariateFunction;
+import org.apache.commons.math3.optim.BaseOptimizer;
+import org.apache.commons.math3.optim.OptimizationData;
+import org.apache.commons.math3.optim.GoalType;
+import org.apache.commons.math3.optim.ConvergenceChecker;
+import org.apache.commons.math3.exception.TooManyEvaluationsException;
+
+/**
+ * Base class for a univariate scalar function optimizer.
+ *
+ * @version $Id$
+ * @since 3.1
+ */
+public abstract class UnivariateOptimizer
+ extends BaseOptimizer<UnivariatePointValuePair> {
+ /** Objective function. */
+ private UnivariateFunction function;
+ /** Type of optimization. */
+ private GoalType goal;
+ /** Initial guess. */
+ private double start;
+ /** Lower bound. */
+ private double min;
+ /** Upper bound. */
+ private double max;
+
+ /**
+ * @param checker Convergence checker.
+ */
+ protected UnivariateOptimizer(ConvergenceChecker<UnivariatePointValuePair> checker) {
+ super(checker);
+ }
+
+ /**
+ * {@inheritDoc}
+ *
+ * @param optData Optimization data.
+ * The following data will be looked for:
+ * <ul>
+ * <li>{@link GoalType}</li>
+ * <li>{@link SearchInterval}</li>
+ * <li>{@link UnivariateObjectiveFunction}</li>
+ * </ul>
+ * @return {@inheritDoc}
+ * @throws TooManyEvaluationsException if the maximal number of
+ * evaluations is exceeded.
+ */
+ public UnivariatePointValuePair optimize(OptimizationData... optData)
+ throws TooManyEvaluationsException {
+ // Retrieve settings.
+ parseOptimizationData(optData);
+ // Perform computation.
+ return super.optimize(optData);
+ }
+
+ /**
+ * @return the optimization type.
+ */
+ public GoalType getGoalType() {
+ return goal;
+ }
+
+ /**
+ * Scans the list of (required and optional) optimization data that
+ * characterize the problem.
+ *
+ * @param optData Optimization data.
+ * The following data will be looked for:
+ * <ul>
+ * <li>{@link GoalType}</li>
+ * <li>{@link SearchInterval}</li>
+ * <li>{@link UnivariateObjectiveFunction}</li>
+ * </ul>
+ */
+ private void parseOptimizationData(OptimizationData... optData) {
+ // The existing values (as set by the previous call) are reused if
+ // not provided in the argument list.
+ for (OptimizationData data : optData) {
+ if (data instanceof SearchInterval) {
+ final SearchInterval interval = (SearchInterval) data;
+ min = interval.getMin();
+ max = interval.getMax();
+ start = interval.getStartValue();
+ continue;
+ }
+ if (data instanceof UnivariateObjectiveFunction) {
+ function = ((UnivariateObjectiveFunction) data).getObjectiveFunction();
+ continue;
+ }
+ if (data instanceof GoalType) {
+ goal = (GoalType) data;
+ continue;
+ }
+ }
+ }
+
+ /**
+ * @return the initial guess.
+ */
+ public double getStartValue() {
+ return start;
+ }
+ /**
+ * @return the lower bounds.
+ */
+ public double getMin() {
+ return min;
+ }
+ /**
+ * @return the upper bounds.
+ */
+ public double getMax() {
+ return max;
+ }
+
+ /**
+ * Computes the objective function value.
+ * This method <em>must</em> be called by subclasses to enforce the
+ * evaluation counter limit.
+ *
+ * @param x Point at which the objective function must be evaluated.
+ * @return the objective function value at the specified point.
+ * @throws TooManyEvaluationsException if the maximal number of
+ * evaluations is exceeded.
+ */
+ protected double computeObjectiveValue(double x) {
+ super.incrementEvaluationCount();
+ return function.value(x);
+ }
+}
Propchange: commons/proper/math/trunk/src/main/java/org/apache/commons/math3/optim/univariate/UnivariateOptimizer.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: commons/proper/math/trunk/src/main/java/org/apache/commons/math3/optim/univariate/UnivariatePointValuePair.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math3/optim/univariate/UnivariatePointValuePair.java?rev=1420684&view=auto
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math3/optim/univariate/UnivariatePointValuePair.java (added)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math3/optim/univariate/UnivariatePointValuePair.java Wed Dec 12 14:10:38 2012
@@ -0,0 +1,67 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.commons.math3.optim.univariate;
+
+import java.io.Serializable;
+
+/**
+ * This class holds a point and the value of an objective function at this
+ * point.
+ * This is a simple immutable container.
+ *
+ * @version $Id: UnivariatePointValuePair.java 1364392 2012-07-22 18:27:12Z tn $
+ * @since 3.0
+ */
+public class UnivariatePointValuePair implements Serializable {
+ /** Serializable version identifier. */
+ private static final long serialVersionUID = 1003888396256744753L;
+ /** Point. */
+ private final double point;
+ /** Value of the objective function at the point. */
+ private final double value;
+
+ /**
+ * Build a point/objective function value pair.
+ *
+ * @param point Point.
+ * @param value Value of an objective function at the point
+ */
+ public UnivariatePointValuePair(final double point,
+ final double value) {
+ this.point = point;
+ this.value = value;
+ }
+
+ /**
+ * Get the point.
+ *
+ * @return the point.
+ */
+ public double getPoint() {
+ return point;
+ }
+
+ /**
+ * Get the value of the objective function.
+ *
+ * @return the stored value of the objective function.
+ */
+ public double getValue() {
+ return value;
+ }
+}
Propchange: commons/proper/math/trunk/src/main/java/org/apache/commons/math3/optim/univariate/UnivariatePointValuePair.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: commons/proper/math/trunk/src/main/java/org/apache/commons/math3/optim/univariate/package-info.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math3/optim/univariate/package-info.java?rev=1420684&view=auto
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math3/optim/univariate/package-info.java (added)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math3/optim/univariate/package-info.java Wed Dec 12 14:10:38 2012
@@ -0,0 +1,21 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.math3.optim.univariate;
+
+/**
+ * One-dimensional optimization algorithms.
+ */
Propchange: commons/proper/math/trunk/src/main/java/org/apache/commons/math3/optim/univariate/package-info.java
------------------------------------------------------------------------------
svn:eol-style = native
Modified: commons/proper/math/trunk/src/main/resources/assets/org/apache/commons/math3/exception/util/LocalizedFormats_fr.properties
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/resources/assets/org/apache/commons/math3/exception/util/LocalizedFormats_fr.properties?rev=1420684&r1=1420683&r2=1420684&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/resources/assets/org/apache/commons/math3/exception/util/LocalizedFormats_fr.properties (original)
+++ commons/proper/math/trunk/src/main/resources/assets/org/apache/commons/math3/exception/util/LocalizedFormats_fr.properties Wed Dec 12 14:10:38 2012
@@ -119,6 +119,7 @@ INVALID_REGRESSION_ARRAY= la longueur du
INVALID_REGRESSION_OBSERVATION = la longueur du tableau de variables explicatives ({0}) ne correspond pas au nombre de variables dans le mod\u00e8le ({1})
INVALID_ROUNDING_METHOD = m\u00e9thode d''arondi {0} invalide, m\u00e9thodes valides : {1} ({2}), {3} ({4}), {5} ({6}), {7} ({8}), {9} ({10}), {11} ({12}), {13} ({14}), {15} ({16})
ITERATOR_EXHAUSTED = it\u00e9ration achev\u00e9e
+ITERATIONS = it\u00e9rations
LCM_OVERFLOW_32_BITS = d\u00e9passement de capacit\u00e9 : le MCM de {0} et {1} vaut 2^31
LCM_OVERFLOW_64_BITS = d\u00e9passement de capacit\u00e9 : le MCM de {0} et {1} vaut 2^63
LIST_OF_CHROMOSOMES_BIGGER_THAN_POPULATION_SIZE = la liste des chromosomes d\u00e9passe maxPopulationSize
Modified: commons/proper/math/trunk/src/test/java/org/apache/commons/math3/exception/util/LocalizedFormatsTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math3/exception/util/LocalizedFormatsTest.java?rev=1420684&r1=1420683&r2=1420684&view=diff
==============================================================================
--- commons/proper/math/trunk/src/test/java/org/apache/commons/math3/exception/util/LocalizedFormatsTest.java (original)
+++ commons/proper/math/trunk/src/test/java/org/apache/commons/math3/exception/util/LocalizedFormatsTest.java Wed Dec 12 14:10:38 2012
@@ -30,7 +30,7 @@ public class LocalizedFormatsTest {
@Test
public void testMessageNumber() {
- Assert.assertEquals(311, LocalizedFormats.values().length);
+ Assert.assertEquals(312, LocalizedFormats.values().length);
}
@Test
Added: commons/proper/math/trunk/src/test/java/org/apache/commons/math3/fitting/CurveFitterTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math3/fitting/CurveFitterTest.java?rev=1420684&view=auto
==============================================================================
--- commons/proper/math/trunk/src/test/java/org/apache/commons/math3/fitting/CurveFitterTest.java (added)
+++ commons/proper/math/trunk/src/test/java/org/apache/commons/math3/fitting/CurveFitterTest.java Wed Dec 12 14:10:38 2012
@@ -0,0 +1,141 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.math3.fitting;
+
+import org.apache.commons.math3.optim.nonlinear.vector.jacobian.LevenbergMarquardtOptimizer;
+import org.apache.commons.math3.analysis.ParametricUnivariateFunction;
+import org.apache.commons.math3.util.FastMath;
+import org.junit.Assert;
+import org.junit.Test;
+
+public class CurveFitterTest {
+ @Test
+ public void testMath303() {
+ LevenbergMarquardtOptimizer optimizer = new LevenbergMarquardtOptimizer();
+ CurveFitter<ParametricUnivariateFunction> fitter = new CurveFitter<ParametricUnivariateFunction>(optimizer);
+ fitter.addObservedPoint(2.805d, 0.6934785852953367d);
+ fitter.addObservedPoint(2.74333333333333d, 0.6306772025518496d);
+ fitter.addObservedPoint(1.655d, 0.9474675497289684);
+ fitter.addObservedPoint(1.725d, 0.9013594835804194d);
+
+ ParametricUnivariateFunction sif = new SimpleInverseFunction();
+
+ double[] initialguess1 = new double[1];
+ initialguess1[0] = 1.0d;
+ Assert.assertEquals(1, fitter.fit(sif, initialguess1).length);
+
+ double[] initialguess2 = new double[2];
+ initialguess2[0] = 1.0d;
+ initialguess2[1] = .5d;
+ Assert.assertEquals(2, fitter.fit(sif, initialguess2).length);
+ }
+
+ @Test
+ public void testMath304() {
+ LevenbergMarquardtOptimizer optimizer = new LevenbergMarquardtOptimizer();
+ CurveFitter<ParametricUnivariateFunction> fitter = new CurveFitter<ParametricUnivariateFunction>(optimizer);
+ fitter.addObservedPoint(2.805d, 0.6934785852953367d);
+ fitter.addObservedPoint(2.74333333333333d, 0.6306772025518496d);
+ fitter.addObservedPoint(1.655d, 0.9474675497289684);
+ fitter.addObservedPoint(1.725d, 0.9013594835804194d);
+
+ ParametricUnivariateFunction sif = new SimpleInverseFunction();
+
+ double[] initialguess1 = new double[1];
+ initialguess1[0] = 1.0d;
+ Assert.assertEquals(1.6357215104109237, fitter.fit(sif, initialguess1)[0], 1.0e-14);
+
+ double[] initialguess2 = new double[1];
+ initialguess2[0] = 10.0d;
+ Assert.assertEquals(1.6357215104109237, fitter.fit(sif, initialguess1)[0], 1.0e-14);
+ }
+
+ @Test
+ public void testMath372() {
+ LevenbergMarquardtOptimizer optimizer = new LevenbergMarquardtOptimizer();
+ CurveFitter<ParametricUnivariateFunction> curveFitter = new CurveFitter<ParametricUnivariateFunction>(optimizer);
+
+ curveFitter.addObservedPoint( 15, 4443);
+ curveFitter.addObservedPoint( 31, 8493);
+ curveFitter.addObservedPoint( 62, 17586);
+ curveFitter.addObservedPoint(125, 30582);
+ curveFitter.addObservedPoint(250, 45087);
+ curveFitter.addObservedPoint(500, 50683);
+
+ ParametricUnivariateFunction f = new ParametricUnivariateFunction() {
+ public double value(double x, double ... parameters) {
+ double a = parameters[0];
+ double b = parameters[1];
+ double c = parameters[2];
+ double d = parameters[3];
+
+ return d + ((a - d) / (1 + FastMath.pow(x / c, b)));
+ }
+
+ public double[] gradient(double x, double ... parameters) {
+ double a = parameters[0];
+ double b = parameters[1];
+ double c = parameters[2];
+ double d = parameters[3];
+
+ double[] gradients = new double[4];
+ double den = 1 + FastMath.pow(x / c, b);
+
+ // derivative with respect to a
+ gradients[0] = 1 / den;
+
+ // derivative with respect to b
+ // in the reported (invalid) issue, there was a sign error here
+ gradients[1] = -((a - d) * FastMath.pow(x / c, b) * FastMath.log(x / c)) / (den * den);
+
+ // derivative with respect to c
+ gradients[2] = (b * FastMath.pow(x / c, b - 1) * (x / (c * c)) * (a - d)) / (den * den);
+
+ // derivative with respect to d
+ gradients[3] = 1 - (1 / den);
+
+ return gradients;
+
+ }
+ };
+
+ double[] initialGuess = new double[] { 1500, 0.95, 65, 35000 };
+ double[] estimatedParameters = curveFitter.fit(f, initialGuess);
+
+ Assert.assertEquals( 2411.00, estimatedParameters[0], 500.00);
+ Assert.assertEquals( 1.62, estimatedParameters[1], 0.04);
+ Assert.assertEquals( 111.22, estimatedParameters[2], 0.30);
+ Assert.assertEquals(55347.47, estimatedParameters[3], 300.00);
+ Assert.assertTrue(optimizer.getRMS() < 600.0);
+ }
+
+ private static class SimpleInverseFunction implements ParametricUnivariateFunction {
+
+ public double value(double x, double ... parameters) {
+ return parameters[0] / x + (parameters.length < 2 ? 0 : parameters[1]);
+ }
+
+ public double[] gradient(double x, double ... doubles) {
+ double[] gradientVector = new double[doubles.length];
+ gradientVector[0] = 1 / x;
+ if (doubles.length >= 2) {
+ gradientVector[1] = 1;
+ }
+ return gradientVector;
+ }
+ }
+}
Propchange: commons/proper/math/trunk/src/test/java/org/apache/commons/math3/fitting/CurveFitterTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: commons/proper/math/trunk/src/test/java/org/apache/commons/math3/fitting/GaussianFitterTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math3/fitting/GaussianFitterTest.java?rev=1420684&view=auto
==============================================================================
--- commons/proper/math/trunk/src/test/java/org/apache/commons/math3/fitting/GaussianFitterTest.java (added)
+++ commons/proper/math/trunk/src/test/java/org/apache/commons/math3/fitting/GaussianFitterTest.java Wed Dec 12 14:10:38 2012
@@ -0,0 +1,363 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.math3.fitting;
+
+import org.apache.commons.math3.optim.nonlinear.vector.jacobian.LevenbergMarquardtOptimizer;
+import org.apache.commons.math3.exception.MathIllegalArgumentException;
+import org.junit.Assert;
+import org.junit.Test;
+
+/**
+ * Tests {@link GaussianFitter}.
+ *
+ * @since 2.2
+ * @version $Id: GaussianFitterTest.java 1349707 2012-06-13 09:30:56Z erans $
+ */
+public class GaussianFitterTest {
+ /** Good data. */
+ protected static final double[][] DATASET1 = new double[][] {
+ {4.0254623, 531026.0},
+ {4.02804905, 664002.0},
+ {4.02934242, 787079.0},
+ {4.03128248, 984167.0},
+ {4.03386923, 1294546.0},
+ {4.03580929, 1560230.0},
+ {4.03839603, 1887233.0},
+ {4.0396894, 2113240.0},
+ {4.04162946, 2375211.0},
+ {4.04421621, 2687152.0},
+ {4.04550958, 2862644.0},
+ {4.04744964, 3078898.0},
+ {4.05003639, 3327238.0},
+ {4.05132976, 3461228.0},
+ {4.05326982, 3580526.0},
+ {4.05585657, 3576946.0},
+ {4.05779662, 3439750.0},
+ {4.06038337, 3220296.0},
+ {4.06167674, 3070073.0},
+ {4.0636168, 2877648.0},
+ {4.06620355, 2595848.0},
+ {4.06749692, 2390157.0},
+ {4.06943698, 2175960.0},
+ {4.07202373, 1895104.0},
+ {4.0733171, 1687576.0},
+ {4.07525716, 1447024.0},
+ {4.0778439, 1130879.0},
+ {4.07978396, 904900.0},
+ {4.08237071, 717104.0},
+ {4.08366408, 620014.0}
+ };
+ /** Poor data: right of peak not symmetric with left of peak. */
+ protected static final double[][] DATASET2 = new double[][] {
+ {-20.15, 1523.0},
+ {-19.65, 1566.0},
+ {-19.15, 1592.0},
+ {-18.65, 1927.0},
+ {-18.15, 3089.0},
+ {-17.65, 6068.0},
+ {-17.15, 14239.0},
+ {-16.65, 34124.0},
+ {-16.15, 64097.0},
+ {-15.65, 110352.0},
+ {-15.15, 164742.0},
+ {-14.65, 209499.0},
+ {-14.15, 267274.0},
+ {-13.65, 283290.0},
+ {-13.15, 275363.0},
+ {-12.65, 258014.0},
+ {-12.15, 225000.0},
+ {-11.65, 200000.0},
+ {-11.15, 190000.0},
+ {-10.65, 185000.0},
+ {-10.15, 180000.0},
+ { -9.65, 179000.0},
+ { -9.15, 178000.0},
+ { -8.65, 177000.0},
+ { -8.15, 176000.0},
+ { -7.65, 175000.0},
+ { -7.15, 174000.0},
+ { -6.65, 173000.0},
+ { -6.15, 172000.0},
+ { -5.65, 171000.0},
+ { -5.15, 170000.0}
+ };
+ /** Poor data: long tails. */
+ protected static final double[][] DATASET3 = new double[][] {
+ {-90.15, 1513.0},
+ {-80.15, 1514.0},
+ {-70.15, 1513.0},
+ {-60.15, 1514.0},
+ {-50.15, 1513.0},
+ {-40.15, 1514.0},
+ {-30.15, 1513.0},
+ {-20.15, 1523.0},
+ {-19.65, 1566.0},
+ {-19.15, 1592.0},
+ {-18.65, 1927.0},
+ {-18.15, 3089.0},
+ {-17.65, 6068.0},
+ {-17.15, 14239.0},
+ {-16.65, 34124.0},
+ {-16.15, 64097.0},
+ {-15.65, 110352.0},
+ {-15.15, 164742.0},
+ {-14.65, 209499.0},
+ {-14.15, 267274.0},
+ {-13.65, 283290.0},
+ {-13.15, 275363.0},
+ {-12.65, 258014.0},
+ {-12.15, 214073.0},
+ {-11.65, 182244.0},
+ {-11.15, 136419.0},
+ {-10.65, 97823.0},
+ {-10.15, 58930.0},
+ { -9.65, 35404.0},
+ { -9.15, 16120.0},
+ { -8.65, 9823.0},
+ { -8.15, 5064.0},
+ { -7.65, 2575.0},
+ { -7.15, 1642.0},
+ { -6.65, 1101.0},
+ { -6.15, 812.0},
+ { -5.65, 690.0},
+ { -5.15, 565.0},
+ { 5.15, 564.0},
+ { 15.15, 565.0},
+ { 25.15, 564.0},
+ { 35.15, 565.0},
+ { 45.15, 564.0},
+ { 55.15, 565.0},
+ { 65.15, 564.0},
+ { 75.15, 565.0}
+ };
+ /** Poor data: right of peak is missing. */
+ protected static final double[][] DATASET4 = new double[][] {
+ {-20.15, 1523.0},
+ {-19.65, 1566.0},
+ {-19.15, 1592.0},
+ {-18.65, 1927.0},
+ {-18.15, 3089.0},
+ {-17.65, 6068.0},
+ {-17.15, 14239.0},
+ {-16.65, 34124.0},
+ {-16.15, 64097.0},
+ {-15.65, 110352.0},
+ {-15.15, 164742.0},
+ {-14.65, 209499.0},
+ {-14.15, 267274.0},
+ {-13.65, 283290.0}
+ };
+ /** Good data, but few points. */
+ protected static final double[][] DATASET5 = new double[][] {
+ {4.0254623, 531026.0},
+ {4.03128248, 984167.0},
+ {4.03839603, 1887233.0},
+ {4.04421621, 2687152.0},
+ {4.05132976, 3461228.0},
+ {4.05326982, 3580526.0},
+ {4.05779662, 3439750.0},
+ {4.0636168, 2877648.0},
+ {4.06943698, 2175960.0},
+ {4.07525716, 1447024.0},
+ {4.08237071, 717104.0},
+ {4.08366408, 620014.0}
+ };
+
+ /**
+ * Basic.
+ */
+ @Test
+ public void testFit01() {
+ GaussianFitter fitter = new GaussianFitter(new LevenbergMarquardtOptimizer());
+ addDatasetToGaussianFitter(DATASET1, fitter);
+ double[] parameters = fitter.fit();
+
+ Assert.assertEquals(3496978.1837704973, parameters[0], 1e-4);
+ Assert.assertEquals(4.054933085999146, parameters[1], 1e-4);
+ Assert.assertEquals(0.015039355620304326, parameters[2], 1e-4);
+ }
+
+ /**
+ * Zero points is not enough observed points.
+ */
+ @Test(expected=MathIllegalArgumentException.class)
+ public void testFit02() {
+ GaussianFitter fitter = new GaussianFitter(new LevenbergMarquardtOptimizer());
+ fitter.fit();
+ }
+
+ /**
+ * Two points is not enough observed points.
+ */
+ @Test(expected=MathIllegalArgumentException.class)
+ public void testFit03() {
+ GaussianFitter fitter = new GaussianFitter(new LevenbergMarquardtOptimizer());
+ addDatasetToGaussianFitter(new double[][] {
+ {4.0254623, 531026.0},
+ {4.02804905, 664002.0}},
+ fitter);
+ fitter.fit();
+ }
+
+ /**
+ * Poor data: right of peak not symmetric with left of peak.
+ */
+ @Test
+ public void testFit04() {
+ GaussianFitter fitter = new GaussianFitter(new LevenbergMarquardtOptimizer());
+ addDatasetToGaussianFitter(DATASET2, fitter);
+ double[] parameters = fitter.fit();
+
+ Assert.assertEquals(233003.2967252038, parameters[0], 1e-4);
+ Assert.assertEquals(-10.654887521095983, parameters[1], 1e-4);
+ Assert.assertEquals(4.335937353196641, parameters[2], 1e-4);
+ }
+
+ /**
+ * Poor data: long tails.
+ */
+ @Test
+ public void testFit05() {
+ GaussianFitter fitter = new GaussianFitter(new LevenbergMarquardtOptimizer());
+ addDatasetToGaussianFitter(DATASET3, fitter);
+ double[] parameters = fitter.fit();
+
+ Assert.assertEquals(283863.81929180305, parameters[0], 1e-4);
+ Assert.assertEquals(-13.29641995105174, parameters[1], 1e-4);
+ Assert.assertEquals(1.7297330293549908, parameters[2], 1e-4);
+ }
+
+ /**
+ * Poor data: right of peak is missing.
+ */
+ @Test
+ public void testFit06() {
+ GaussianFitter fitter = new GaussianFitter(new LevenbergMarquardtOptimizer());
+ addDatasetToGaussianFitter(DATASET4, fitter);
+ double[] parameters = fitter.fit();
+
+ Assert.assertEquals(285250.66754309234, parameters[0], 1e-4);
+ Assert.assertEquals(-13.528375695228455, parameters[1], 1e-4);
+ Assert.assertEquals(1.5204344894331614, parameters[2], 1e-4);
+ }
+
+ /**
+ * Basic with smaller dataset.
+ */
+ @Test
+ public void testFit07() {
+ GaussianFitter fitter = new GaussianFitter(new LevenbergMarquardtOptimizer());
+ addDatasetToGaussianFitter(DATASET5, fitter);
+ double[] parameters = fitter.fit();
+
+ Assert.assertEquals(3514384.729342235, parameters[0], 1e-4);
+ Assert.assertEquals(4.054970307455625, parameters[1], 1e-4);
+ Assert.assertEquals(0.015029412832160017, parameters[2], 1e-4);
+ }
+
+ @Test
+ public void testMath519() {
+ // The optimizer will try negative sigma values but "GaussianFitter"
+ // will catch the raised exceptions and return NaN values instead.
+
+ final double[] data = {
+ 1.1143831578403364E-29,
+ 4.95281403484594E-28,
+ 1.1171347211930288E-26,
+ 1.7044813962636277E-25,
+ 1.9784716574832164E-24,
+ 1.8630236407866774E-23,
+ 1.4820532905097742E-22,
+ 1.0241963854632831E-21,
+ 6.275077366673128E-21,
+ 3.461808994532493E-20,
+ 1.7407124684715706E-19,
+ 8.056687953553974E-19,
+ 3.460193945992071E-18,
+ 1.3883326374011525E-17,
+ 5.233894983671116E-17,
+ 1.8630791465263745E-16,
+ 6.288759227922111E-16,
+ 2.0204433920597856E-15,
+ 6.198768938576155E-15,
+ 1.821419346860626E-14,
+ 5.139176445538471E-14,
+ 1.3956427429045787E-13,
+ 3.655705706448139E-13,
+ 9.253753324779779E-13,
+ 2.267636001476696E-12,
+ 5.3880460095836855E-12,
+ 1.2431632654852931E-11
+ };
+
+ GaussianFitter fitter = new GaussianFitter(new LevenbergMarquardtOptimizer());
+ for (int i = 0; i < data.length; i++) {
+ fitter.addObservedPoint(i, data[i]);
+ }
+ final double[] p = fitter.fit();
+
+ Assert.assertEquals(53.1572792, p[1], 1e-7);
+ Assert.assertEquals(5.75214622, p[2], 1e-8);
+ }
+
+ @Test
+ public void testMath798() {
+ final GaussianFitter fitter = new GaussianFitter(new LevenbergMarquardtOptimizer());
+
+ // When the data points are not commented out below, the fit stalls.
+ // This is expected however, since the whole dataset hardly looks like
+ // a Gaussian.
+ // When commented out, the fit proceeds fine.
+
+ fitter.addObservedPoint(0.23, 395.0);
+ //fitter.addObservedPoint(0.68, 0.0);
+ fitter.addObservedPoint(1.14, 376.0);
+ //fitter.addObservedPoint(1.59, 0.0);
+ fitter.addObservedPoint(2.05, 163.0);
+ //fitter.addObservedPoint(2.50, 0.0);
+ fitter.addObservedPoint(2.95, 49.0);
+ //fitter.addObservedPoint(3.41, 0.0);
+ fitter.addObservedPoint(3.86, 16.0);
+ //fitter.addObservedPoint(4.32, 0.0);
+ fitter.addObservedPoint(4.77, 1.0);
+
+ final double[] p = fitter.fit();
+
+ // Values are copied from a previous run of this test.
+ Assert.assertEquals(420.8397296167364, p[0], 1e-12);
+ Assert.assertEquals(0.603770729862231, p[1], 1e-15);
+ Assert.assertEquals(1.0786447936766612, p[2], 1e-14);
+ }
+
+ /**
+ * Adds the specified points to specified <code>GaussianFitter</code>
+ * instance.
+ *
+ * @param points data points where first dimension is a point index and
+ * second dimension is an array of length two representing the point
+ * with the first value corresponding to X and the second value
+ * corresponding to Y
+ * @param fitter fitter to which the points in <code>points</code> should be
+ * added as observed points
+ */
+ protected static void addDatasetToGaussianFitter(double[][] points,
+ GaussianFitter fitter) {
+ for (int i = 0; i < points.length; i++) {
+ fitter.addObservedPoint(points[i][0], points[i][1]);
+ }
+ }
+}
Propchange: commons/proper/math/trunk/src/test/java/org/apache/commons/math3/fitting/GaussianFitterTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: commons/proper/math/trunk/src/test/java/org/apache/commons/math3/fitting/HarmonicFitterTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math3/fitting/HarmonicFitterTest.java?rev=1420684&view=auto
==============================================================================
--- commons/proper/math/trunk/src/test/java/org/apache/commons/math3/fitting/HarmonicFitterTest.java (added)
+++ commons/proper/math/trunk/src/test/java/org/apache/commons/math3/fitting/HarmonicFitterTest.java Wed Dec 12 14:10:38 2012
@@ -0,0 +1,185 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.math3.fitting;
+
+import java.util.Random;
+import org.apache.commons.math3.optim.nonlinear.vector.jacobian.LevenbergMarquardtOptimizer;
+import org.apache.commons.math3.analysis.function.HarmonicOscillator;
+import org.apache.commons.math3.exception.NumberIsTooSmallException;
+import org.apache.commons.math3.exception.MathIllegalStateException;
+import org.apache.commons.math3.util.FastMath;
+import org.apache.commons.math3.util.MathUtils;
+
+import org.junit.Test;
+import org.junit.Assert;
+
+public class HarmonicFitterTest {
+ @Test(expected=NumberIsTooSmallException.class)
+ public void testPreconditions1() {
+ HarmonicFitter fitter =
+ new HarmonicFitter(new LevenbergMarquardtOptimizer());
+
+ fitter.fit();
+ }
+
+ @Test
+ public void testNoError() {
+ final double a = 0.2;
+ final double w = 3.4;
+ final double p = 4.1;
+ HarmonicOscillator f = new HarmonicOscillator(a, w, p);
+
+ HarmonicFitter fitter =
+ new HarmonicFitter(new LevenbergMarquardtOptimizer());
+ for (double x = 0.0; x < 1.3; x += 0.01) {
+ fitter.addObservedPoint(1, x, f.value(x));
+ }
+
+ final double[] fitted = fitter.fit();
+ Assert.assertEquals(a, fitted[0], 1.0e-13);
+ Assert.assertEquals(w, fitted[1], 1.0e-13);
+ Assert.assertEquals(p, MathUtils.normalizeAngle(fitted[2], p), 1e-13);
+
+ HarmonicOscillator ff = new HarmonicOscillator(fitted[0], fitted[1], fitted[2]);
+
+ for (double x = -1.0; x < 1.0; x += 0.01) {
+ Assert.assertTrue(FastMath.abs(f.value(x) - ff.value(x)) < 1e-13);
+ }
+ }
+
+ @Test
+ public void test1PercentError() {
+ Random randomizer = new Random(64925784252l);
+ final double a = 0.2;
+ final double w = 3.4;
+ final double p = 4.1;
+ HarmonicOscillator f = new HarmonicOscillator(a, w, p);
+
+ HarmonicFitter fitter =
+ new HarmonicFitter(new LevenbergMarquardtOptimizer());
+ for (double x = 0.0; x < 10.0; x += 0.1) {
+ fitter.addObservedPoint(1, x,
+ f.value(x) + 0.01 * randomizer.nextGaussian());
+ }
+
+ final double[] fitted = fitter.fit();
+ Assert.assertEquals(a, fitted[0], 7.6e-4);
+ Assert.assertEquals(w, fitted[1], 2.7e-3);
+ Assert.assertEquals(p, MathUtils.normalizeAngle(fitted[2], p), 1.3e-2);
+ }
+
+ @Test
+ public void testTinyVariationsData() {
+ Random randomizer = new Random(64925784252l);
+
+ HarmonicFitter fitter =
+ new HarmonicFitter(new LevenbergMarquardtOptimizer());
+ for (double x = 0.0; x < 10.0; x += 0.1) {
+ fitter.addObservedPoint(1, x, 1e-7 * randomizer.nextGaussian());
+ }
+
+ fitter.fit();
+ // This test serves to cover the part of the code of "guessAOmega"
+ // when the algorithm using integrals fails.
+ }
+
+ @Test
+ public void testInitialGuess() {
+ Random randomizer = new Random(45314242l);
+ final double a = 0.2;
+ final double w = 3.4;
+ final double p = 4.1;
+ HarmonicOscillator f = new HarmonicOscillator(a, w, p);
+
+ HarmonicFitter fitter =
+ new HarmonicFitter(new LevenbergMarquardtOptimizer());
+ for (double x = 0.0; x < 10.0; x += 0.1) {
+ fitter.addObservedPoint(1, x,
+ f.value(x) + 0.01 * randomizer.nextGaussian());
+ }
+
+ final double[] fitted = fitter.fit(new double[] { 0.15, 3.6, 4.5 });
+ Assert.assertEquals(a, fitted[0], 1.2e-3);
+ Assert.assertEquals(w, fitted[1], 3.3e-3);
+ Assert.assertEquals(p, MathUtils.normalizeAngle(fitted[2], p), 1.7e-2);
+ }
+
+ @Test
+ public void testUnsorted() {
+ Random randomizer = new Random(64925784252l);
+ final double a = 0.2;
+ final double w = 3.4;
+ final double p = 4.1;
+ HarmonicOscillator f = new HarmonicOscillator(a, w, p);
+
+ HarmonicFitter fitter =
+ new HarmonicFitter(new LevenbergMarquardtOptimizer());
+
+ // build a regularly spaced array of measurements
+ int size = 100;
+ double[] xTab = new double[size];
+ double[] yTab = new double[size];
+ for (int i = 0; i < size; ++i) {
+ xTab[i] = 0.1 * i;
+ yTab[i] = f.value(xTab[i]) + 0.01 * randomizer.nextGaussian();
+ }
+
+ // shake it
+ for (int i = 0; i < size; ++i) {
+ int i1 = randomizer.nextInt(size);
+ int i2 = randomizer.nextInt(size);
+ double xTmp = xTab[i1];
+ double yTmp = yTab[i1];
+ xTab[i1] = xTab[i2];
+ yTab[i1] = yTab[i2];
+ xTab[i2] = xTmp;
+ yTab[i2] = yTmp;
+ }
+
+ // pass it to the fitter
+ for (int i = 0; i < size; ++i) {
+ fitter.addObservedPoint(1, xTab[i], yTab[i]);
+ }
+
+ final double[] fitted = fitter.fit();
+ Assert.assertEquals(a, fitted[0], 7.6e-4);
+ Assert.assertEquals(w, fitted[1], 3.5e-3);
+ Assert.assertEquals(p, MathUtils.normalizeAngle(fitted[2], p), 1.5e-2);
+ }
+
+ @Test(expected=MathIllegalStateException.class)
+ public void testMath844() {
+ final double[] y = { 0, 1, 2, 3, 2, 1,
+ 0, -1, -2, -3, -2, -1,
+ 0, 1, 2, 3, 2, 1,
+ 0, -1, -2, -3, -2, -1,
+ 0, 1, 2, 3, 2, 1, 0 };
+ final int len = y.length;
+ final WeightedObservedPoint[] points = new WeightedObservedPoint[len];
+ for (int i = 0; i < len; i++) {
+ points[i] = new WeightedObservedPoint(1, i, y[i]);
+ }
+
+ // The guesser fails because the function is far from an harmonic
+ // function: It is a triangular periodic function with amplitude 3
+ // and period 12, and all sample points are taken at integer abscissae
+ // so function values all belong to the integer subset {-3, -2, -1, 0,
+ // 1, 2, 3}.
+ final HarmonicFitter.ParameterGuesser guesser
+ = new HarmonicFitter.ParameterGuesser(points);
+ }
+}
Propchange: commons/proper/math/trunk/src/test/java/org/apache/commons/math3/fitting/HarmonicFitterTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: commons/proper/math/trunk/src/test/java/org/apache/commons/math3/fitting/PolynomialFitterTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math3/fitting/PolynomialFitterTest.java?rev=1420684&view=auto
==============================================================================
--- commons/proper/math/trunk/src/test/java/org/apache/commons/math3/fitting/PolynomialFitterTest.java (added)
+++ commons/proper/math/trunk/src/test/java/org/apache/commons/math3/fitting/PolynomialFitterTest.java Wed Dec 12 14:10:38 2012
@@ -0,0 +1,256 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.math3.fitting;
+
+import java.util.Random;
+import org.apache.commons.math3.analysis.polynomials.PolynomialFunction;
+import org.apache.commons.math3.analysis.polynomials.PolynomialFunction.Parametric;
+import org.apache.commons.math3.exception.ConvergenceException;
+import org.apache.commons.math3.exception.TooManyEvaluationsException;
+import org.apache.commons.math3.optim.nonlinear.vector.MultivariateVectorOptimizer;
+import org.apache.commons.math3.optim.nonlinear.vector.jacobian.LevenbergMarquardtOptimizer;
+import org.apache.commons.math3.optim.nonlinear.vector.jacobian.GaussNewtonOptimizer;
+import org.apache.commons.math3.optim.SimpleVectorValueChecker;
+import org.apache.commons.math3.util.FastMath;
+import org.apache.commons.math3.distribution.RealDistribution;
+import org.apache.commons.math3.distribution.UniformRealDistribution;
+import org.apache.commons.math3.TestUtils;
+import org.junit.Test;
+import org.junit.Assert;
+
+/**
+ * Test for class {@link CurveFitter} where the function to fit is a
+ * polynomial.
+ */
+public class PolynomialFitterTest {
+ @Test
+ public void testFit() {
+ final RealDistribution rng = new UniformRealDistribution(-100, 100);
+ rng.reseedRandomGenerator(64925784252L);
+
+ final LevenbergMarquardtOptimizer optim = new LevenbergMarquardtOptimizer();
+ final PolynomialFitter fitter = new PolynomialFitter(optim);
+ final double[] coeff = { 12.9, -3.4, 2.1 }; // 12.9 - 3.4 x + 2.1 x^2
+ final PolynomialFunction f = new PolynomialFunction(coeff);
+
+ // Collect data from a known polynomial.
+ for (int i = 0; i < 100; i++) {
+ final double x = rng.sample();
+ fitter.addObservedPoint(x, f.value(x));
+ }
+
+ // Start fit from initial guesses that are far from the optimal values.
+ final double[] best = fitter.fit(new double[] { -1e-20, 3e15, -5e25 });
+
+ TestUtils.assertEquals("best != coeff", coeff, best, 1e-12);
+ }
+
+ @Test
+ public void testNoError() {
+ Random randomizer = new Random(64925784252l);
+ for (int degree = 1; degree < 10; ++degree) {
+ PolynomialFunction p = buildRandomPolynomial(degree, randomizer);
+
+ PolynomialFitter fitter = new PolynomialFitter(new LevenbergMarquardtOptimizer());
+ for (int i = 0; i <= degree; ++i) {
+ fitter.addObservedPoint(1.0, i, p.value(i));
+ }
+
+ final double[] init = new double[degree + 1];
+ PolynomialFunction fitted = new PolynomialFunction(fitter.fit(init));
+
+ for (double x = -1.0; x < 1.0; x += 0.01) {
+ double error = FastMath.abs(p.value(x) - fitted.value(x)) /
+ (1.0 + FastMath.abs(p.value(x)));
+ Assert.assertEquals(0.0, error, 1.0e-6);
+ }
+ }
+ }
+
+ @Test
+ public void testSmallError() {
+ Random randomizer = new Random(53882150042l);
+ double maxError = 0;
+ for (int degree = 0; degree < 10; ++degree) {
+ PolynomialFunction p = buildRandomPolynomial(degree, randomizer);
+
+ PolynomialFitter fitter = new PolynomialFitter(new LevenbergMarquardtOptimizer());
+ for (double x = -1.0; x < 1.0; x += 0.01) {
+ fitter.addObservedPoint(1.0, x,
+ p.value(x) + 0.1 * randomizer.nextGaussian());
+ }
+
+ final double[] init = new double[degree + 1];
+ PolynomialFunction fitted = new PolynomialFunction(fitter.fit(init));
+
+ for (double x = -1.0; x < 1.0; x += 0.01) {
+ double error = FastMath.abs(p.value(x) - fitted.value(x)) /
+ (1.0 + FastMath.abs(p.value(x)));
+ maxError = FastMath.max(maxError, error);
+ Assert.assertTrue(FastMath.abs(error) < 0.1);
+ }
+ }
+ Assert.assertTrue(maxError > 0.01);
+ }
+
+ @Test
+ public void testMath798() {
+ final double tol = 1e-14;
+ final SimpleVectorValueChecker checker = new SimpleVectorValueChecker(tol, tol);
+ final double[] init = new double[] { 0, 0 };
+ final int maxEval = 3;
+
+ final double[] lm = doMath798(new LevenbergMarquardtOptimizer(checker), maxEval, init);
+ final double[] gn = doMath798(new GaussNewtonOptimizer(checker), maxEval, init);
+
+ for (int i = 0; i <= 1; i++) {
+ Assert.assertEquals(lm[i], gn[i], tol);
+ }
+ }
+
+ /**
+ * This test shows that the user can set the maximum number of iterations
+ * to avoid running for too long.
+ * But in the test case, the real problem is that the tolerance is way too
+ * stringent.
+ */
+ @Test(expected=TooManyEvaluationsException.class)
+ public void testMath798WithToleranceTooLow() {
+ final double tol = 1e-100;
+ final SimpleVectorValueChecker checker = new SimpleVectorValueChecker(tol, tol);
+ final double[] init = new double[] { 0, 0 };
+ final int maxEval = 10000; // Trying hard to fit.
+
+ final double[] gn = doMath798(new GaussNewtonOptimizer(checker), maxEval, init);
+ }
+
+ /**
+ * This test shows that the user can set the maximum number of iterations
+ * to avoid running for too long.
+ * Even if the real problem is that the tolerance is way too stringent, it
+ * is possible to get the best solution so far, i.e. a checker will return
+ * the point when the maximum iteration count has been reached.
+ */
+ @Test
+ public void testMath798WithToleranceTooLowButNoException() {
+ final double tol = 1e-100;
+ final double[] init = new double[] { 0, 0 };
+ final int maxEval = 10000; // Trying hard to fit.
+ final SimpleVectorValueChecker checker = new SimpleVectorValueChecker(tol, tol, maxEval);
+
+ final double[] lm = doMath798(new LevenbergMarquardtOptimizer(checker), maxEval, init);
+ final double[] gn = doMath798(new GaussNewtonOptimizer(checker), maxEval, init);
+
+ for (int i = 0; i <= 1; i++) {
+ Assert.assertEquals(lm[i], gn[i], 1e-15);
+ }
+ }
+
+ /**
+ * @param optimizer Optimizer.
+ * @param maxEval Maximum number of function evaluations.
+ * @param init First guess.
+ * @return the solution found by the given optimizer.
+ */
+ private double[] doMath798(MultivariateVectorOptimizer optimizer,
+ int maxEval,
+ double[] init) {
+ final CurveFitter<Parametric> fitter = new CurveFitter<Parametric>(optimizer);
+
+ fitter.addObservedPoint(-0.2, -7.12442E-13);
+ fitter.addObservedPoint(-0.199, -4.33397E-13);
+ fitter.addObservedPoint(-0.198, -2.823E-13);
+ fitter.addObservedPoint(-0.197, -1.40405E-13);
+ fitter.addObservedPoint(-0.196, -7.80821E-15);
+ fitter.addObservedPoint(-0.195, 6.20484E-14);
+ fitter.addObservedPoint(-0.194, 7.24673E-14);
+ fitter.addObservedPoint(-0.193, 1.47152E-13);
+ fitter.addObservedPoint(-0.192, 1.9629E-13);
+ fitter.addObservedPoint(-0.191, 2.12038E-13);
+ fitter.addObservedPoint(-0.19, 2.46906E-13);
+ fitter.addObservedPoint(-0.189, 2.77495E-13);
+ fitter.addObservedPoint(-0.188, 2.51281E-13);
+ fitter.addObservedPoint(-0.187, 2.64001E-13);
+ fitter.addObservedPoint(-0.186, 2.8882E-13);
+ fitter.addObservedPoint(-0.185, 3.13604E-13);
+ fitter.addObservedPoint(-0.184, 3.14248E-13);
+ fitter.addObservedPoint(-0.183, 3.1172E-13);
+ fitter.addObservedPoint(-0.182, 3.12912E-13);
+ fitter.addObservedPoint(-0.181, 3.06761E-13);
+ fitter.addObservedPoint(-0.18, 2.8559E-13);
+ fitter.addObservedPoint(-0.179, 2.86806E-13);
+ fitter.addObservedPoint(-0.178, 2.985E-13);
+ fitter.addObservedPoint(-0.177, 2.67148E-13);
+ fitter.addObservedPoint(-0.176, 2.94173E-13);
+ fitter.addObservedPoint(-0.175, 3.27528E-13);
+ fitter.addObservedPoint(-0.174, 3.33858E-13);
+ fitter.addObservedPoint(-0.173, 2.97511E-13);
+ fitter.addObservedPoint(-0.172, 2.8615E-13);
+ fitter.addObservedPoint(-0.171, 2.84624E-13);
+
+ final double[] coeff = fitter.fit(maxEval,
+ new PolynomialFunction.Parametric(),
+ init);
+ return coeff;
+ }
+
+ @Test
+ public void testRedundantSolvable() {
+ // Levenberg-Marquardt should handle redundant information gracefully
+ checkUnsolvableProblem(new LevenbergMarquardtOptimizer(), true);
+ }
+
+ @Test
+ public void testRedundantUnsolvable() {
+ // Gauss-Newton should not be able to solve redundant information
+ checkUnsolvableProblem(new GaussNewtonOptimizer(true, new SimpleVectorValueChecker(1e-15, 1e-15)), false);
+ }
+
+ private void checkUnsolvableProblem(MultivariateVectorOptimizer optimizer,
+ boolean solvable) {
+ Random randomizer = new Random(1248788532l);
+ for (int degree = 0; degree < 10; ++degree) {
+ PolynomialFunction p = buildRandomPolynomial(degree, randomizer);
+
+ PolynomialFitter fitter = new PolynomialFitter(optimizer);
+
+ // reusing the same point over and over again does not bring
+ // information, the problem cannot be solved in this case for
+ // degrees greater than 1 (but one point is sufficient for
+ // degree 0)
+ for (double x = -1.0; x < 1.0; x += 0.01) {
+ fitter.addObservedPoint(1.0, 0.0, p.value(0.0));
+ }
+
+ try {
+ final double[] init = new double[degree + 1];
+ fitter.fit(init);
+ Assert.assertTrue(solvable || (degree == 0));
+ } catch(ConvergenceException e) {
+ Assert.assertTrue((! solvable) && (degree > 0));
+ }
+ }
+ }
+
+ private PolynomialFunction buildRandomPolynomial(int degree, Random randomizer) {
+ final double[] coefficients = new double[degree + 1];
+ for (int i = 0; i <= degree; ++i) {
+ coefficients[i] = randomizer.nextGaussian();
+ }
+ return new PolynomialFunction(coefficients);
+ }
+}
Propchange: commons/proper/math/trunk/src/test/java/org/apache/commons/math3/fitting/PolynomialFitterTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: commons/proper/math/trunk/src/test/java/org/apache/commons/math3/optim/PointValuePairTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math3/optim/PointValuePairTest.java?rev=1420684&view=auto
==============================================================================
--- commons/proper/math/trunk/src/test/java/org/apache/commons/math3/optim/PointValuePairTest.java (added)
+++ commons/proper/math/trunk/src/test/java/org/apache/commons/math3/optim/PointValuePairTest.java Wed Dec 12 14:10:38 2012
@@ -0,0 +1,35 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.math3.optim;
+
+import org.apache.commons.math3.TestUtils;
+import org.junit.Assert;
+import org.junit.Test;
+
+public class PointValuePairTest {
+ @Test
+ public void testSerial() {
+ PointValuePair pv1 = new PointValuePair(new double[] { 1.0, 2.0, 3.0 }, 4.0);
+ PointValuePair pv2 = (PointValuePair) TestUtils.serializeAndRecover(pv1);
+ Assert.assertEquals(pv1.getKey().length, pv2.getKey().length);
+ for (int i = 0; i < pv1.getKey().length; ++i) {
+ Assert.assertEquals(pv1.getKey()[i], pv2.getKey()[i], 1.0e-15);
+ }
+ Assert.assertEquals(pv1.getValue(), pv2.getValue(), 1.0e-15);
+ }
+
+}
Propchange: commons/proper/math/trunk/src/test/java/org/apache/commons/math3/optim/PointValuePairTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: commons/proper/math/trunk/src/test/java/org/apache/commons/math3/optim/PointVectorValuePairTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math3/optim/PointVectorValuePairTest.java?rev=1420684&view=auto
==============================================================================
--- commons/proper/math/trunk/src/test/java/org/apache/commons/math3/optim/PointVectorValuePairTest.java (added)
+++ commons/proper/math/trunk/src/test/java/org/apache/commons/math3/optim/PointVectorValuePairTest.java Wed Dec 12 14:10:38 2012
@@ -0,0 +1,39 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.commons.math3.optim;
+
+import org.apache.commons.math3.TestUtils;
+import org.junit.Assert;
+import org.junit.Test;
+
+public class PointVectorValuePairTest {
+ @Test
+ public void testSerial() {
+ PointVectorValuePair pv1 = new PointVectorValuePair(new double[] { 1.0, 2.0, 3.0 },
+ new double[] { 4.0, 5.0 });
+ PointVectorValuePair pv2 = (PointVectorValuePair) TestUtils.serializeAndRecover(pv1);
+ Assert.assertEquals(pv1.getKey().length, pv2.getKey().length);
+ for (int i = 0; i < pv1.getKey().length; ++i) {
+ Assert.assertEquals(pv1.getKey()[i], pv2.getKey()[i], 1.0e-15);
+ }
+ Assert.assertEquals(pv1.getValue().length, pv2.getValue().length);
+ for (int i = 0; i < pv1.getValue().length; ++i) {
+ Assert.assertEquals(pv1.getValue()[i], pv2.getValue()[i], 1.0e-15);
+ }
+ }
+}
Propchange: commons/proper/math/trunk/src/test/java/org/apache/commons/math3/optim/PointVectorValuePairTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: commons/proper/math/trunk/src/test/java/org/apache/commons/math3/optim/SimplePointCheckerTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math3/optim/SimplePointCheckerTest.java?rev=1420684&view=auto
==============================================================================
--- commons/proper/math/trunk/src/test/java/org/apache/commons/math3/optim/SimplePointCheckerTest.java (added)
+++ commons/proper/math/trunk/src/test/java/org/apache/commons/math3/optim/SimplePointCheckerTest.java Wed Dec 12 14:10:38 2012
@@ -0,0 +1,53 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.math3.optim;
+
+import org.apache.commons.math3.exception.NotStrictlyPositiveException;
+import org.junit.Test;
+import org.junit.Assert;
+
+public class SimplePointCheckerTest {
+ @Test(expected=NotStrictlyPositiveException.class)
+ public void testIterationCheckPrecondition() {
+ new SimplePointChecker<PointValuePair>(1e-1, 1e-2, 0);
+ }
+
+ @Test
+ public void testIterationCheck() {
+ final int max = 10;
+ final SimplePointChecker<PointValuePair> checker
+ = new SimplePointChecker<PointValuePair>(1e-1, 1e-2, max);
+ Assert.assertTrue(checker.converged(max, null, null));
+ Assert.assertTrue(checker.converged(max + 1, null, null));
+ }
+
+ @Test
+ public void testIterationCheckDisabled() {
+ final SimplePointChecker<PointValuePair> checker
+ = new SimplePointChecker<PointValuePair>(1e-8, 1e-8);
+
+ final PointValuePair a = new PointValuePair(new double[] { 1d }, 1d);
+ final PointValuePair b = new PointValuePair(new double[] { 10d }, 10d);
+
+ Assert.assertFalse(checker.converged(-1, a, b));
+ Assert.assertFalse(checker.converged(0, a, b));
+ Assert.assertFalse(checker.converged(1000000, a, b));
+
+ Assert.assertTrue(checker.converged(-1, a, a));
+ Assert.assertTrue(checker.converged(-1, b, b));
+ }
+}
Propchange: commons/proper/math/trunk/src/test/java/org/apache/commons/math3/optim/SimplePointCheckerTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: commons/proper/math/trunk/src/test/java/org/apache/commons/math3/optim/SimpleValueCheckerTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math3/optim/SimpleValueCheckerTest.java?rev=1420684&view=auto
==============================================================================
--- commons/proper/math/trunk/src/test/java/org/apache/commons/math3/optim/SimpleValueCheckerTest.java (added)
+++ commons/proper/math/trunk/src/test/java/org/apache/commons/math3/optim/SimpleValueCheckerTest.java Wed Dec 12 14:10:38 2012
@@ -0,0 +1,52 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.math3.optim;
+
+import org.apache.commons.math3.exception.NotStrictlyPositiveException;
+import org.junit.Test;
+import org.junit.Assert;
+
+public class SimpleValueCheckerTest {
+ @Test(expected=NotStrictlyPositiveException.class)
+ public void testIterationCheckPrecondition() {
+ new SimpleValueChecker(1e-1, 1e-2, 0);
+ }
+
+ @Test
+ public void testIterationCheck() {
+ final int max = 10;
+ final SimpleValueChecker checker = new SimpleValueChecker(1e-1, 1e-2, max);
+ Assert.assertTrue(checker.converged(max, null, null));
+ Assert.assertTrue(checker.converged(max + 1, null, null));
+ }
+
+ @Test
+ public void testIterationCheckDisabled() {
+ final SimpleValueChecker checker = new SimpleValueChecker(1e-8, 1e-8);
+
+ final PointValuePair a = new PointValuePair(new double[] { 1d }, 1d);
+ final PointValuePair b = new PointValuePair(new double[] { 10d }, 10d);
+
+ Assert.assertFalse(checker.converged(-1, a, b));
+ Assert.assertFalse(checker.converged(0, a, b));
+ Assert.assertFalse(checker.converged(1000000, a, b));
+
+ Assert.assertTrue(checker.converged(-1, a, a));
+ Assert.assertTrue(checker.converged(-1, b, b));
+ }
+
+}
Propchange: commons/proper/math/trunk/src/test/java/org/apache/commons/math3/optim/SimpleValueCheckerTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: commons/proper/math/trunk/src/test/java/org/apache/commons/math3/optim/SimpleVectorValueCheckerTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math3/optim/SimpleVectorValueCheckerTest.java?rev=1420684&view=auto
==============================================================================
--- commons/proper/math/trunk/src/test/java/org/apache/commons/math3/optim/SimpleVectorValueCheckerTest.java (added)
+++ commons/proper/math/trunk/src/test/java/org/apache/commons/math3/optim/SimpleVectorValueCheckerTest.java Wed Dec 12 14:10:38 2012
@@ -0,0 +1,53 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.math3.optim;
+
+import org.apache.commons.math3.exception.NotStrictlyPositiveException;
+import org.junit.Test;
+import org.junit.Assert;
+
+public class SimpleVectorValueCheckerTest {
+ @Test(expected=NotStrictlyPositiveException.class)
+ public void testIterationCheckPrecondition() {
+ new SimpleVectorValueChecker(1e-1, 1e-2, 0);
+ }
+
+ @Test
+ public void testIterationCheck() {
+ final int max = 10;
+ final SimpleVectorValueChecker checker = new SimpleVectorValueChecker(1e-1, 1e-2, max);
+ Assert.assertTrue(checker.converged(max, null, null));
+ Assert.assertTrue(checker.converged(max + 1, null, null));
+ }
+
+ @Test
+ public void testIterationCheckDisabled() {
+ final SimpleVectorValueChecker checker = new SimpleVectorValueChecker(1e-8, 1e-8);
+
+ final PointVectorValuePair a = new PointVectorValuePair(new double[] { 1d },
+ new double[] { 1d });
+ final PointVectorValuePair b = new PointVectorValuePair(new double[] { 10d },
+ new double[] { 10d });
+
+ Assert.assertFalse(checker.converged(-1, a, b));
+ Assert.assertFalse(checker.converged(0, a, b));
+ Assert.assertFalse(checker.converged(1000000, a, b));
+
+ Assert.assertTrue(checker.converged(-1, a, a));
+ Assert.assertTrue(checker.converged(-1, b, b));
+ }
+}
Propchange: commons/proper/math/trunk/src/test/java/org/apache/commons/math3/optim/SimpleVectorValueCheckerTest.java
------------------------------------------------------------------------------
svn:eol-style = native