From ed39d2dbe2522a7842d8f173af46b483d983cb0a Mon Sep 17 00:00:00 2001 From: Luc Maisonobe Date: Sat, 29 Dec 2012 12:10:52 +0000 Subject: [PATCH] reverting commit introduced in r1426616 git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/trunk@1426751 13f79535-47bb-0310-9956-ffa450edef68 --- pom.xml | 4 +- src/changes/changes.xml | 7 -- .../commons/math3/fitting/CurveFitter.java | 11 ++- ...MultiStartMultivariateVectorOptimizer.java | 19 ++--- .../vector/MultivariateVectorOptimizer.java | 62 +++------------ .../nonlinear/vector/NonCorrelatedWeight.java | 53 ------------- .../math3/optim/nonlinear/vector/Weight.java | 18 +++-- .../AbstractLeastSquaresOptimizer.java | 75 ++++++------------- .../vector/jacobian/GaussNewtonOptimizer.java | 9 ++- .../jacobian/LevenbergMarquardtOptimizer.java | 14 ++-- .../math3/fitting/PolynomialFitterTest.java | 27 ------- ...iStartMultivariateVectorOptimizerTest.java | 9 ++- ...ractLeastSquaresOptimizerAbstractTest.java | 53 ++++++------- .../AbstractLeastSquaresOptimizerTest.java | 13 ++-- ...ctLeastSquaresOptimizerTestValidation.java | 17 ++--- .../jacobian/GaussNewtonOptimizerTest.java | 9 ++- .../LevenbergMarquardtOptimizerTest.java | 28 +++---- .../vector/jacobian/MinpackTest.java | 14 ++-- 18 files changed, 145 insertions(+), 297 deletions(-) delete mode 100644 src/main/java/org/apache/commons/math3/optim/nonlinear/vector/NonCorrelatedWeight.java diff --git a/pom.xml b/pom.xml index 2ccae6c1d..430e23d18 100644 --- a/pom.xml +++ b/pom.xml @@ -24,7 +24,7 @@ 4.0.0 org.apache.commons commons-math3 - 3.1.1-SNAPSHOT + 3.2-SNAPSHOT Commons Math 2003 @@ -293,7 +293,7 @@ math3 - 3.1.1 + 3.2 (requires Java 1.5+) -bin diff --git a/src/changes/changes.xml b/src/changes/changes.xml index 56216536d..1941f1c2d 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -50,13 +50,6 @@ If the output is not quite correct, check for invisible trailing spaces! Commons Math Release Notes - - - Fix handling of large number of weights in the new optimizers API. - - - * Immutable class. - * - * @version $Id$ - * @since 3.1.1 - */ -public class NonCorrelatedWeight implements OptimizationData { - - /** Weight. */ - private final double[] weight; - - /** - * Creates a weight vector. - * - * @param weight weight of the observations - */ - public NonCorrelatedWeight(final double[] weight) { - this.weight = weight.clone(); - } - - /** - * Gets the weight. - * - * @return a fresh copy of the weight. - */ - public double[] getWeight() { - return weight.clone(); - } - -} diff --git a/src/main/java/org/apache/commons/math3/optim/nonlinear/vector/Weight.java b/src/main/java/org/apache/commons/math3/optim/nonlinear/vector/Weight.java index 1b58d81a3..789bc256d 100644 --- a/src/main/java/org/apache/commons/math3/optim/nonlinear/vector/Weight.java +++ b/src/main/java/org/apache/commons/math3/optim/nonlinear/vector/Weight.java @@ -28,20 +28,22 @@ import org.apache.commons.math3.linear.NonSquareMatrixException; * * @version $Id: Weight.java 1416643 2012-12-03 19:37:14Z tn $ * @since 3.1 - * @deprecated as of 3.1.1, replaced by {@link NonCorrelatedWeight} */ -@Deprecated public class Weight implements OptimizationData { /** Weight matrix. */ private final RealMatrix weightMatrix; /** - * Creates a weight matrix. + * Creates a diagonal weight matrix. * - * @param weight matrix elements. + * @param weight List of the values of the diagonal. */ - public Weight(double[][] weight) { - weightMatrix = MatrixUtils.createRealMatrix(weight); + public Weight(double[] weight) { + final int dim = weight.length; + weightMatrix = MatrixUtils.createRealMatrix(dim, dim); + for (int i = 0; i < dim; i++) { + weightMatrix.setEntry(i, i, weight[i]); + } } /** @@ -59,9 +61,9 @@ public class Weight implements OptimizationData { } /** - * Gets the weight. + * Gets the initial guess. * - * @return a fresh copy of the weight. + * @return the initial guess. */ public RealMatrix getWeight() { return weightMatrix.copy(); diff --git a/src/main/java/org/apache/commons/math3/optim/nonlinear/vector/jacobian/AbstractLeastSquaresOptimizer.java b/src/main/java/org/apache/commons/math3/optim/nonlinear/vector/jacobian/AbstractLeastSquaresOptimizer.java index 7b2c6b51e..b7bb6f575 100644 --- a/src/main/java/org/apache/commons/math3/optim/nonlinear/vector/jacobian/AbstractLeastSquaresOptimizer.java +++ b/src/main/java/org/apache/commons/math3/optim/nonlinear/vector/jacobian/AbstractLeastSquaresOptimizer.java @@ -19,18 +19,16 @@ package org.apache.commons.math3.optim.nonlinear.vector.jacobian; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.exception.TooManyEvaluationsException; import org.apache.commons.math3.linear.ArrayRealVector; +import org.apache.commons.math3.linear.RealMatrix; import org.apache.commons.math3.linear.DecompositionSolver; -import org.apache.commons.math3.linear.EigenDecomposition; import org.apache.commons.math3.linear.MatrixUtils; import org.apache.commons.math3.linear.QRDecomposition; -import org.apache.commons.math3.linear.RealMatrix; -import org.apache.commons.math3.optim.ConvergenceChecker; +import org.apache.commons.math3.linear.EigenDecomposition; import org.apache.commons.math3.optim.OptimizationData; +import org.apache.commons.math3.optim.ConvergenceChecker; import org.apache.commons.math3.optim.PointVectorValuePair; -import org.apache.commons.math3.optim.nonlinear.vector.JacobianMultivariateVectorOptimizer; -import org.apache.commons.math3.optim.nonlinear.vector.MultivariateVectorOptimizer; -import org.apache.commons.math3.optim.nonlinear.vector.NonCorrelatedWeight; import org.apache.commons.math3.optim.nonlinear.vector.Weight; +import org.apache.commons.math3.optim.nonlinear.vector.JacobianMultivariateVectorOptimizer; import org.apache.commons.math3.util.FastMath; /** @@ -42,13 +40,8 @@ import org.apache.commons.math3.util.FastMath; */ public abstract class AbstractLeastSquaresOptimizer extends JacobianMultivariateVectorOptimizer { - /** Square-root of the weight matrix. - * @deprecated as of 3.1.1, replaced by {@link #weight} - */ - @Deprecated + /** Square-root of the weight matrix. */ private RealMatrix weightMatrixSqrt; - /** Square-root of the weight vector. */ - private double[] weightSquareRoot; /** Cost value (square root of the sum of the residuals). */ private double cost; @@ -68,23 +61,7 @@ public abstract class AbstractLeastSquaresOptimizer * match problem dimension. */ protected RealMatrix computeWeightedJacobian(double[] params) { - - final double[][] jacobian = computeJacobian(params); - - if (weightSquareRoot != null) { - for (int i = 0; i < jacobian.length; ++i) { - final double wi = weightSquareRoot[i]; - final double[] row = jacobian[i]; - for (int j = 0; j < row.length; ++j) { - row[j] *= wi; - } - } - return MatrixUtils.createRealMatrix(jacobian); - } else { - // TODO: remove for 4.0, when the {@link Weight} class will be removed - return weightMatrixSqrt.multiply(MatrixUtils.createRealMatrix(jacobian)); - } - + return weightMatrixSqrt.multiply(MatrixUtils.createRealMatrix(computeJacobian(params))); } /** @@ -96,13 +73,7 @@ public abstract class AbstractLeastSquaresOptimizer */ protected double computeCost(double[] residuals) { final ArrayRealVector r = new ArrayRealVector(residuals); - final double[] weight = getNonCorrelatedWeight(); - double sum = 0; - for (int i = 0; i < r.getDimension(); ++i) { - final double ri = r.getEntry(i); - sum += ri * weight[i] * ri; - } - return FastMath.sqrt(sum); + return FastMath.sqrt(r.dotProduct(getWeight().operate(r))); } /** @@ -134,9 +105,7 @@ public abstract class AbstractLeastSquaresOptimizer * Gets the square-root of the weight matrix. * * @return the square-root of the weight matrix. - * @deprecated as of 3.1.1, replaced with {@link MultivariateVectorOptimizer#getNonCorrelatedWeight()} */ - @Deprecated public RealMatrix getWeightSquareRoot() { return weightMatrixSqrt.copy(); } @@ -214,7 +183,7 @@ public abstract class AbstractLeastSquaresOptimizer *
  • {@link org.apache.commons.math3.optim.InitialGuess}
  • *
  • {@link org.apache.commons.math3.optim.SimpleBounds}
  • *
  • {@link org.apache.commons.math3.optim.nonlinear.vector.Target}
  • - *
  • {@link org.apache.commons.math3.optim.nonlinear.vector.NonCorrelatedWeight}
  • + *
  • {@link org.apache.commons.math3.optim.nonlinear.vector.Weight}
  • *
  • {@link org.apache.commons.math3.optim.nonlinear.vector.ModelFunction}
  • *
  • {@link org.apache.commons.math3.optim.nonlinear.vector.ModelFunctionJacobian}
  • * @@ -266,7 +235,8 @@ public abstract class AbstractLeastSquaresOptimizer /** * Scans the list of (required and optional) optimization data that * characterize the problem. - * If the weight is specified, the {@link #weightSquareRoot} field is recomputed. + * If the weight matrix is specified, the {@link #weightMatrixSqrt} + * field is recomputed. * * @param optData Optimization data. The following data will be looked for: *