Minor clean-up (to avoid changing a method arguments in-place).

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/trunk@983968 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Gilles Sadowski 2010-08-10 12:45:10 +00:00
parent d2fd025403
commit 31fef8fab1
1 changed files with 22 additions and 14 deletions

View File

@ -112,16 +112,16 @@ public class PowellOptimizer
int bigInd = 0; int bigInd = 0;
double alphaMin = 0; double alphaMin = 0;
double[] direc1 = null;
for (int i = 0; i < n; i++) { for (int i = 0; i < n; i++) {
direc1 = Arrays.copyOf(direc[i], n); final double[] d = Arrays.copyOf(direc[i], n);
fX2 = fVal; fX2 = fVal;
line.search(x, direc1); line.search(x, d);
fVal = line.getValueAtOptimum(); fVal = line.getValueAtOptimum();
alphaMin = line.getOptimum(); alphaMin = line.getOptimum();
setNewPointAndDirection(x, direc1, alphaMin); final double[][] result = newPointAndDirection(x, d, alphaMin);
x = result[0];
if ((fX2 - fVal) > delta) { if ((fX2 - fVal) > delta) {
delta = fX2 - fVal; 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++) { 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]; x2[i] = 2 * x[i] - x1[i];
} }
@ -156,14 +157,15 @@ public class PowellOptimizer
t -= delta * temp * temp; t -= delta * temp * temp;
if (t < 0.0) { if (t < 0.0) {
line.search(x, direc1); line.search(x, d);
fVal = line.getValueAtOptimum(); fVal = line.getValueAtOptimum();
alphaMin = line.getOptimum(); alphaMin = line.getOptimum();
setNewPointAndDirection(x, direc1, alphaMin); final double[][] result = newPointAndDirection(x, d, alphaMin);
x = result[0];
final int lastInd = n - 1; final int lastInd = n - 1;
direc[bigInd] = direc[lastInd]; 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 p Point used in the line search.
* @param d Direction used in the line search. * @param d Direction used in the line search.
* @param optimum Optimum found by 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, private double[][] newPointAndDirection(double[] p,
double[] d, double[] d,
double optimum) { double optimum) {
final int n = p.length; 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++) { for (int i = 0; i < n; i++) {
d[i] *= optimum; nD[i] = d[i] * optimum;
p[i] += d[i]; nP[i] = p[i] + nD[i];
} }
return result;
} }
/** /**