commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From l..@apache.org
Subject svn commit: r1571014 - in /commons/proper/math/trunk/src/main/java/org/apache/commons/math3/fitting/leastsquares: LeastSquaresFactory.java LeastSquaresProblemImpl.java
Date Sun, 23 Feb 2014 14:00:20 GMT
Author: luc
Date: Sun Feb 23 14:00:20 2014
New Revision: 1571014

URL: http://svn.apache.org/r1571014
Log:
Change private least square problem implementation to internal class.

Removed:
    commons/proper/math/trunk/src/main/java/org/apache/commons/math3/fitting/leastsquares/LeastSquaresProblemImpl.java
Modified:
    commons/proper/math/trunk/src/main/java/org/apache/commons/math3/fitting/leastsquares/LeastSquaresFactory.java

Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math3/fitting/leastsquares/LeastSquaresFactory.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math3/fitting/leastsquares/LeastSquaresFactory.java?rev=1571014&r1=1571013&r2=1571014&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math3/fitting/leastsquares/LeastSquaresFactory.java
(original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math3/fitting/leastsquares/LeastSquaresFactory.java
Sun Feb 23 14:00:20 2014
@@ -25,6 +25,7 @@ import org.apache.commons.math3.linear.D
 import org.apache.commons.math3.linear.EigenDecomposition;
 import org.apache.commons.math3.linear.RealMatrix;
 import org.apache.commons.math3.linear.RealVector;
+import org.apache.commons.math3.optim.AbstractOptimizationProblem;
 import org.apache.commons.math3.optim.ConvergenceChecker;
 import org.apache.commons.math3.optim.PointVectorValuePair;
 import org.apache.commons.math3.util.FastMath;
@@ -61,7 +62,7 @@ public class LeastSquaresFactory {
                                              final ConvergenceChecker<Evaluation> checker,
                                              final int maxEvaluations,
                                              final int maxIterations) {
-        return new LeastSquaresProblemImpl(
+        return new LocalLeastSquaresProblem(
                 model,
                 observed,
                 start,
@@ -269,5 +270,129 @@ public class LeastSquaresFactory {
             }
         };
     }
+
+    /**
+     * A private, "field" immutable (not "real" immutable) implementation of {@link
+     * LeastSquaresProblem}.
+     * @since 3.3
+     */
+    private static class LocalLeastSquaresProblem
+            extends AbstractOptimizationProblem<Evaluation>
+            implements LeastSquaresProblem {
+
+        /** Target values for the model function at optimum. */
+        private RealVector target;
+        /** Model function. */
+        private MultivariateJacobianFunction model;
+        /** Initial guess. */
+        private RealVector start;
+
+        /**
+         * Create a {@link LeastSquaresProblem} from the given data.
+         *
+         * @param model          the model function
+         * @param target         the observed data
+         * @param start          the initial guess
+         * @param checker        the convergence checker
+         * @param maxEvaluations the allowed evaluations
+         * @param maxIterations  the allowed iterations
+         */
+        LocalLeastSquaresProblem(final MultivariateJacobianFunction model,
+                                final RealVector target,
+                                final RealVector start,
+                                final ConvergenceChecker<Evaluation> checker,
+                                final int maxEvaluations,
+                                final int maxIterations) {
+            super(maxEvaluations, maxIterations, checker);
+            this.target = target;
+            this.model = model;
+            this.start = start;
+        }
+
+        /** {@inheritDoc} */
+        public int getObservationSize() {
+            return target.getDimension();
+        }
+
+        /** {@inheritDoc} */
+        public int getParameterSize() {
+            return start.getDimension();
+        }
+
+        /** {@inheritDoc} */
+        public RealVector getStart() {
+            return start == null ? null : start.copy();
+        }
+
+        /** {@inheritDoc} */
+        public Evaluation evaluate(final RealVector point) {
+            //evaluate value and jacobian in one function call
+            final Pair<RealVector, RealMatrix> value = this.model.value(point);
+            return new UnweightedEvaluation(
+                    value.getFirst(),
+                    value.getSecond(),
+                    this.target,
+                    point);
+        }
+
+        /**
+         * Container with the model evaluation at a particular point.
+         * <p/>
+         * TODO revisit lazy evaluation
+         */
+        private static class UnweightedEvaluation extends AbstractEvaluation {
+
+            /** the point of evaluation */
+            private final RealVector point;
+            /** value at point */
+            private final RealVector values;
+            /** deriviative at point */
+            private final RealMatrix jacobian;
+            /** reference to the observed values */
+            private final RealVector target;
+
+            /**
+             * Create an {@link Evaluation} with no weights.
+             *
+             * @param values   the computed function values
+             * @param jacobian the computed function Jacobian
+             * @param target   the observed values
+             * @param point    the abscissa
+             */
+            private UnweightedEvaluation(final RealVector values,
+                                         final RealMatrix jacobian,
+                                         final RealVector target,
+                                         final RealVector point) {
+                super(target.getDimension());
+                this.values = values;
+                this.jacobian = jacobian;
+                this.target = target;
+                this.point = point;
+            }
+
+            /** {@inheritDoc} */
+            public RealVector getValue() {
+                return this.values;
+            }
+
+            /** {@inheritDoc} */
+            public RealMatrix getJacobian() {
+                return this.jacobian;
+            }
+
+            /** {@inheritDoc} */
+            public RealVector getPoint() {
+                return this.point;
+            }
+
+            /** {@inheritDoc} */
+            public RealVector getResiduals() {
+                return target.subtract(this.getValue());
+            }
+
+        }
+
+    }
+
 }
 



Mime
View raw message