commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From er...@apache.org
Subject svn commit: r983968 - /commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/general/PowellOptimizer.java
Date Tue, 10 Aug 2010 12:45:11 GMT
Author: erans
Date: Tue Aug 10 12:45:10 2010
New Revision: 983968

URL: http://svn.apache.org/viewvc?rev=983968&view=rev
Log:
Minor clean-up (to avoid changing a method arguments in-place).

Modified:
    commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/general/PowellOptimizer.java

Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/general/PowellOptimizer.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/general/PowellOptimizer.java?rev=983968&r1=983967&r2=983968&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/general/PowellOptimizer.java
(original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/general/PowellOptimizer.java
Tue Aug 10 12:45:10 2010
@@ -112,16 +112,16 @@ public class PowellOptimizer
             int bigInd = 0;
             double alphaMin = 0;
 
-            double[] direc1 = null;
             for (int i = 0; i < n; i++) {
-                direc1 = Arrays.copyOf(direc[i], n);
+                final double[] d = Arrays.copyOf(direc[i], n);
 
                 fX2 = fVal;
 
-                line.search(x, direc1);
+                line.search(x, d);
                 fVal = line.getValueAtOptimum();
                 alphaMin = line.getOptimum();
-                setNewPointAndDirection(x, direc1, alphaMin);
+                final double[][] result = newPointAndDirection(x, d, alphaMin);
+                x = result[0];
 
                 if ((fX2 - fVal) > delta) {
                     delta = fX2 - fVal;
@@ -139,9 +139,10 @@ public class PowellOptimizer
                 }
             }
 
-            double[] x2 = new double[n];
+            final double[] d = new double[n];
+            final double[] x2 = new double[n];
             for (int i = 0; i < n; i++) {
-                direc1[i] = x[i] - x1[i];
+                d[i] = x[i] - x1[i];
                 x2[i] = 2 * x[i] - x1[i];
             }
 
@@ -156,14 +157,15 @@ public class PowellOptimizer
                 t -= delta * temp * temp;
 
                 if (t < 0.0) {
-                    line.search(x, direc1);
+                    line.search(x, d);
                     fVal = line.getValueAtOptimum();
                     alphaMin = line.getOptimum();
-                    setNewPointAndDirection(x, direc1, alphaMin);
+                    final double[][] result = newPointAndDirection(x, d, alphaMin);
+                    x = result[0];
 
                     final int lastInd = n - 1;
                     direc[bigInd] = direc[lastInd];
-                    direc[lastInd] = direc1;
+                    direc[lastInd] = result[1];
                 }
             }
         }
@@ -177,15 +179,21 @@ public class PowellOptimizer
      * @param p Point used in the line search.
      * @param d Direction used in the line search.
      * @param optimum Optimum found by the line search.
+     * @return a 2-element array containing the new point (at index 0) and
+     * the new direction (at index 1).
      */
-    private void setNewPointAndDirection(double[] p,
-                                         double[] d,
-                                         double optimum) {
+    private double[][] newPointAndDirection(double[] p,
+                                            double[] d,
+                                            double optimum) {
         final int n = p.length;
+        final double[][] result = new double[2][n];
+        final double[] nP = result[0];
+        final double[] nD = result[1];
         for (int i = 0; i < n; i++) {
-            d[i] *= optimum;
-            p[i] += d[i];
+            nD[i] = d[i] * optimum;
+            nP[i] = p[i] + nD[i];
         }
+        return result;
     }
 
     /**



Mime
View raw message