commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From l..@apache.org
Subject svn commit: r1569356 - in /commons/proper/math/trunk/src: main/java/org/apache/commons/math3/fitting/leastsquares/ test/java/org/apache/commons/math3/fitting/leastsquares/
Date Tue, 18 Feb 2014 14:33:08 GMT
Author: luc
Date: Tue Feb 18 14:33:08 2014
New Revision: 1569356

URL: http://svn.apache.org/r1569356
Log:
Clean up LeastSquaresBuilder

Provide methods for using old and new interfaces. Data is stored internally
using the new interfaces now.

Modified:
    commons/proper/math/trunk/src/main/java/org/apache/commons/math3/fitting/leastsquares/LeastSquaresBuilder.java
    commons/proper/math/trunk/src/test/java/org/apache/commons/math3/fitting/leastsquares/AbstractLeastSquaresOptimizerAbstractTest.java
    commons/proper/math/trunk/src/test/java/org/apache/commons/math3/fitting/leastsquares/EvaluationTest.java
    commons/proper/math/trunk/src/test/java/org/apache/commons/math3/fitting/leastsquares/EvaluationTestValidation.java
    commons/proper/math/trunk/src/test/java/org/apache/commons/math3/fitting/leastsquares/LevenbergMarquardtOptimizerTest.java
    commons/proper/math/trunk/src/test/java/org/apache/commons/math3/fitting/leastsquares/MinpackTest.java

Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math3/fitting/leastsquares/LeastSquaresBuilder.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math3/fitting/leastsquares/LeastSquaresBuilder.java?rev=1569356&r1=1569355&r2=1569356&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math3/fitting/leastsquares/LeastSquaresBuilder.java
(original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math3/fitting/leastsquares/LeastSquaresBuilder.java
Tue Feb 18 14:33:08 2014
@@ -3,7 +3,9 @@ package org.apache.commons.math3.fitting
 import org.apache.commons.math3.analysis.MultivariateMatrixFunction;
 import org.apache.commons.math3.analysis.MultivariateVectorFunction;
 import org.apache.commons.math3.fitting.leastsquares.LeastSquaresProblem.Evaluation;
+import org.apache.commons.math3.linear.ArrayRealVector;
 import org.apache.commons.math3.linear.RealMatrix;
+import org.apache.commons.math3.linear.RealVector;
 import org.apache.commons.math3.optim.ConvergenceChecker;
 import org.apache.commons.math3.optim.PointVectorValuePair;
 
@@ -22,13 +24,11 @@ public class LeastSquaresBuilder {
     /** convergence checker */
     private ConvergenceChecker<Evaluation> checker;
     /** model function */
-    private MultivariateVectorFunction model;
-    /** Jacobian function */
-    private MultivariateMatrixFunction jacobian;
+    private MultivariateJacobianFunction model;
     /** observed values */
-    private double[] target;
+    private RealVector target;
     /** initial guess */
-    private double[] start;
+    private RealVector start;
     /** weight matrix */
     private RealMatrix weight;
 
@@ -39,7 +39,7 @@ public class LeastSquaresBuilder {
      * @return a new {@link LeastSquaresProblem}.
      */
     public LeastSquaresProblem build() {
-        return LeastSquaresFactory.create(model, jacobian, target, start, weight, checker,
maxEvaluations, maxIterations);
+        return LeastSquaresFactory.create(model, target, start, weight, checker, maxEvaluations,
maxIterations);
     }
 
     /**
@@ -90,22 +90,34 @@ public class LeastSquaresBuilder {
     /**
      * Configure the model function.
      *
-     * @param model the model function
+     * @param value the model function value
+     * @param jacobian the Jacobian of {@code value}
      * @return this
      */
-    public LeastSquaresBuilder model(final MultivariateVectorFunction model) {
+    public LeastSquaresBuilder model(final MultivariateVectorFunction value,
+                                     final MultivariateMatrixFunction jacobian) {
+        return model(LeastSquaresFactory.model(value, jacobian));
+    }
+
+    /**
+     * Configure the model function.
+     *
+     * @param model the model function value and Jacobian
+     * @return this
+     */
+    public LeastSquaresBuilder model(final MultivariateJacobianFunction model) {
         this.model = model;
         return this;
     }
 
     /**
-     * Configure the Jacobian function.
+     * Configure the observed data.
      *
-     * @param jacobian the Jacobian function
+     * @param target the observed data.
      * @return this
      */
-    public LeastSquaresBuilder jacobian(final MultivariateMatrixFunction jacobian) {
-        this.jacobian = jacobian;
+    public LeastSquaresBuilder target(final RealVector target) {
+        this.target = target;
         return this;
     }
 
@@ -116,8 +128,7 @@ public class LeastSquaresBuilder {
      * @return this
      */
     public LeastSquaresBuilder target(final double[] target) {
-        this.target = target;
-        return this;
+        return target(new ArrayRealVector(target, false));
     }
 
     /**
@@ -126,12 +137,22 @@ public class LeastSquaresBuilder {
      * @param start the initial guess.
      * @return this
      */
-    public LeastSquaresBuilder start(final double[] start) {
+    public LeastSquaresBuilder start(final RealVector start) {
         this.start = start;
         return this;
     }
 
     /**
+     * Configure the initial guess.
+     *
+     * @param start the initial guess.
+     * @return this
+     */
+    public LeastSquaresBuilder start(final double[] start) {
+        return start(new ArrayRealVector(start, false));
+    }
+
+    /**
      * Configure the weight matrix.
      *
      * @param weight the weight matrix

Modified: commons/proper/math/trunk/src/test/java/org/apache/commons/math3/fitting/leastsquares/AbstractLeastSquaresOptimizerAbstractTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math3/fitting/leastsquares/AbstractLeastSquaresOptimizerAbstractTest.java?rev=1569356&r1=1569355&r2=1569356&view=diff
==============================================================================
--- commons/proper/math/trunk/src/test/java/org/apache/commons/math3/fitting/leastsquares/AbstractLeastSquaresOptimizerAbstractTest.java
(original)
+++ commons/proper/math/trunk/src/test/java/org/apache/commons/math3/fitting/leastsquares/AbstractLeastSquaresOptimizerAbstractTest.java
Tue Feb 18 14:33:08 2014
@@ -22,12 +22,15 @@ import org.apache.commons.math3.exceptio
 import org.apache.commons.math3.exception.DimensionMismatchException;
 import org.apache.commons.math3.fitting.leastsquares.LeastSquaresOptimizer.Optimum;
 import org.apache.commons.math3.geometry.euclidean.twod.Vector2D;
+import org.apache.commons.math3.linear.Array2DRowRealMatrix;
+import org.apache.commons.math3.linear.ArrayRealVector;
 import org.apache.commons.math3.linear.BlockRealMatrix;
 import org.apache.commons.math3.linear.DiagonalMatrix;
 import org.apache.commons.math3.linear.RealMatrix;
 import org.apache.commons.math3.linear.RealVector;
 import org.apache.commons.math3.optim.SimpleVectorValueChecker;
 import org.apache.commons.math3.util.FastMath;
+import org.apache.commons.math3.util.Pair;
 import org.junit.Assert;
 
 import java.io.IOException;
@@ -59,8 +62,7 @@ public abstract class AbstractLeastSquar
         final double[] weights = new double[c.getN()];
         Arrays.fill(weights, 1.0);
         return base()
-                .model(c.getModelFunction())
-                .jacobian(c.getModelFunctionJacobian())
+                .model(c.getModelFunction(), c.getModelFunctionJacobian())
                 .target(new double[c.getN()])
                 .weight(new DiagonalMatrix(weights));
     }
@@ -71,8 +73,7 @@ public abstract class AbstractLeastSquar
         final double[] weights = new double[dataset.getNumObservations()];
         Arrays.fill(weights, 1.0);
         return base()
-                .model(problem.getModelFunction())
-                .jacobian(problem.getModelFunctionJacobian())
+                .model(problem.getModelFunction(), problem.getModelFunctionJacobian())
                 .target(dataset.getData()[1])
                 .weight(new DiagonalMatrix(weights))
                 .start(dataset.getStartingPoint(0));
@@ -133,24 +134,22 @@ public abstract class AbstractLeastSquar
                 .target(new double[]{1})
                 .weight(new DiagonalMatrix(new double[]{1}))
                 .start(new double[]{3})
-                .model(
-                        new MultivariateVectorFunction() {
-                            public double[] value(double[] point) {
-                                return new double[]{
-                                        FastMath.pow(point[0], 4)
-                                };
-                            }
-                        }
-                )
-                .jacobian(
-                        new MultivariateMatrixFunction() {
-                            public double[][] value(double[] point) {
-                                return new double[][]{
-                                        {0.25 * FastMath.pow(point[0], 3)}
-                                };
-                            }
-                        }
-                )
+                .model(new MultivariateJacobianFunction() {
+                    public Pair<RealVector, RealMatrix> value(final RealVector point)
{
+                        return new Pair<RealVector, RealMatrix>(
+                                new ArrayRealVector(
+                                        new double[]{
+                                                FastMath.pow(point.getEntry(0), 4)
+                                        },
+                                        false),
+                                new Array2DRowRealMatrix(
+                                        new double[][]{
+                                                {0.25 * FastMath.pow(point.getEntry(0), 3)}
+                                        },
+                                        false)
+                        );
+                    }
+                })
                 .build();
 
         Optimum optimum = optimizer.optimize(lsp);
@@ -554,8 +553,7 @@ public abstract class AbstractLeastSquar
             final double[] weights = new double[target.length];
             Arrays.fill(weights, 1.0);
             return base()
-                    .model(getModelFunction())
-                    .jacobian(getModelFunctionJacobian())
+                    .model(getModelFunction(), getModelFunctionJacobian())
                     .target(target)
                     .weight(new DiagonalMatrix(weights))
                     .start(new double[factors.getColumnDimension()]);

Modified: commons/proper/math/trunk/src/test/java/org/apache/commons/math3/fitting/leastsquares/EvaluationTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math3/fitting/leastsquares/EvaluationTest.java?rev=1569356&r1=1569355&r2=1569356&view=diff
==============================================================================
--- commons/proper/math/trunk/src/test/java/org/apache/commons/math3/fitting/leastsquares/EvaluationTest.java
(original)
+++ commons/proper/math/trunk/src/test/java/org/apache/commons/math3/fitting/leastsquares/EvaluationTest.java
Tue Feb 18 14:33:08 2014
@@ -44,8 +44,7 @@ public class EvaluationTest {
         Arrays.fill(weights, 1d);
 
         return new LeastSquaresBuilder()
-                .model(problem.getModelFunction())
-                .jacobian(problem.getModelFunctionJacobian())
+                .model(problem.getModelFunction(), problem.getModelFunctionJacobian())
                 .target(observed)
                 .weight(new DiagonalMatrix(weights))
                 .start(start);

Modified: commons/proper/math/trunk/src/test/java/org/apache/commons/math3/fitting/leastsquares/EvaluationTestValidation.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math3/fitting/leastsquares/EvaluationTestValidation.java?rev=1569356&r1=1569355&r2=1569356&view=diff
==============================================================================
--- commons/proper/math/trunk/src/test/java/org/apache/commons/math3/fitting/leastsquares/EvaluationTestValidation.java
(original)
+++ commons/proper/math/trunk/src/test/java/org/apache/commons/math3/fitting/leastsquares/EvaluationTestValidation.java
Tue Feb 18 14:33:08 2014
@@ -288,8 +288,7 @@ public class EvaluationTestValidation {
 
     LeastSquaresBuilder builder(StraightLineProblem problem){
         return new LeastSquaresBuilder()
-                .model(problem.getModelFunction())
-                .jacobian(problem.getModelFunctionJacobian())
+                .model(problem.getModelFunction(), problem.getModelFunctionJacobian())
                 .target(problem.target())
                 .weight(new DiagonalMatrix(problem.weight()))
                 //unused start point to avoid NPE

Modified: commons/proper/math/trunk/src/test/java/org/apache/commons/math3/fitting/leastsquares/LevenbergMarquardtOptimizerTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math3/fitting/leastsquares/LevenbergMarquardtOptimizerTest.java?rev=1569356&r1=1569355&r2=1569356&view=diff
==============================================================================
--- commons/proper/math/trunk/src/test/java/org/apache/commons/math3/fitting/leastsquares/LevenbergMarquardtOptimizerTest.java
(original)
+++ commons/proper/math/trunk/src/test/java/org/apache/commons/math3/fitting/leastsquares/LevenbergMarquardtOptimizerTest.java
Tue Feb 18 14:33:08 2014
@@ -49,14 +49,12 @@ public class LevenbergMarquardtOptimizer
 
     public LeastSquaresBuilder builder(BevingtonProblem problem){
         return base()
-                .model(problem.getModelFunction())
-                .jacobian(problem.getModelFunctionJacobian());
+                .model(problem.getModelFunction(), problem.getModelFunctionJacobian());
     }
 
     public LeastSquaresBuilder builder(CircleProblem problem){
         return base()
-                .model(problem.getModelFunction())
-                .jacobian(problem.getModelFunctionJacobian())
+                .model(problem.getModelFunction(), problem.getModelFunctionJacobian())
                 .target(problem.target())
                 .weight(new DiagonalMatrix(problem.weight()));
     }

Modified: commons/proper/math/trunk/src/test/java/org/apache/commons/math3/fitting/leastsquares/MinpackTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math3/fitting/leastsquares/MinpackTest.java?rev=1569356&r1=1569355&r2=1569356&view=diff
==============================================================================
--- commons/proper/math/trunk/src/test/java/org/apache/commons/math3/fitting/leastsquares/MinpackTest.java
(original)
+++ commons/proper/math/trunk/src/test/java/org/apache/commons/math3/fitting/leastsquares/MinpackTest.java
Tue Feb 18 14:33:08 2014
@@ -507,8 +507,7 @@ public class MinpackTest {
         LeastSquaresProblem problem = new LeastSquaresBuilder()
                 .maxEvaluations(400 * (function.getN() + 1))
                 .maxIterations(2000)
-                .model(function.getModelFunction())
-                .jacobian(function.getModelFunctionJacobian())
+                .model(function.getModelFunction(), function.getModelFunctionJacobian())
                 .target(function.getTarget())
                 .weight(new DiagonalMatrix(function.getWeight()))
                 .start(function.getStartPoint())



Mime
View raw message