MATH-990
Reduced amount of copying. Moved consistency checks out of the double loop. git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/trunk@1489871 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
d8aca35b7a
commit
b23c8567c3
|
@ -675,59 +675,69 @@ public class MathArrays {
|
||||||
public static void sortInPlace(double[] x,
|
public static void sortInPlace(double[] x,
|
||||||
final OrderDirection dir,
|
final OrderDirection dir,
|
||||||
double[] ... yList)
|
double[] ... yList)
|
||||||
throws NullArgumentException, DimensionMismatchException {
|
throws NullArgumentException,
|
||||||
|
DimensionMismatchException {
|
||||||
|
final int yListLen = yList.length;
|
||||||
|
final int len = x.length;
|
||||||
|
|
||||||
|
// Consistency checks.
|
||||||
if (x == null) {
|
if (x == null) {
|
||||||
throw new NullArgumentException();
|
throw new NullArgumentException();
|
||||||
}
|
}
|
||||||
|
|
||||||
final int len = x.length;
|
|
||||||
final List<Pair<Double, double[]>> list
|
|
||||||
= new ArrayList<Pair<Double, double[]>>(len);
|
|
||||||
|
|
||||||
final int yListLen = yList.length;
|
|
||||||
for (int i = 0; i < len; i++) {
|
|
||||||
final double[] yValues = new double[yListLen];
|
|
||||||
for (int j = 0; j < yListLen; j++) {
|
for (int j = 0; j < yListLen; j++) {
|
||||||
double[] y = yList[j];
|
final double[] y = yList[j];
|
||||||
if (y == null) {
|
if (y == null) {
|
||||||
throw new NullArgumentException();
|
throw new NullArgumentException();
|
||||||
}
|
}
|
||||||
if (y.length != len) {
|
if (y.length != len) {
|
||||||
throw new DimensionMismatchException(y.length, len);
|
throw new DimensionMismatchException(y.length, len);
|
||||||
}
|
}
|
||||||
yValues[j] = y[i];
|
|
||||||
}
|
|
||||||
list.add(new Pair<Double, double[]>(x[i], yValues));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
final Comparator<Pair<Double, double[]>> comp
|
// Associate each abscissa "x[i]" with its index "i".
|
||||||
= new Comparator<Pair<Double, double[]>>() {
|
final List<Pair<Double, Integer>> list
|
||||||
public int compare(Pair<Double, double[]> o1,
|
= new ArrayList<Pair<Double, Integer>>(len);
|
||||||
Pair<Double, double[]> o2) {
|
for (int i = 0; i < len; i++) {
|
||||||
int val;
|
list.add(new Pair<Double, Integer>(x[i], i));
|
||||||
switch (dir) {
|
|
||||||
case INCREASING:
|
|
||||||
val = o1.getKey().compareTo(o2.getKey());
|
|
||||||
break;
|
|
||||||
case DECREASING:
|
|
||||||
val = o2.getKey().compareTo(o1.getKey());
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
// Should never happen.
|
|
||||||
throw new MathInternalError();
|
|
||||||
}
|
}
|
||||||
return val;
|
|
||||||
|
// Create comparators for increasing and decreasing orders.
|
||||||
|
final Comparator<Pair<Double, Integer>> comp
|
||||||
|
= dir == MathArrays.OrderDirection.INCREASING ?
|
||||||
|
new Comparator<Pair<Double, Integer>>() {
|
||||||
|
public int compare(Pair<Double, Integer> o1,
|
||||||
|
Pair<Double, Integer> o2) {
|
||||||
|
return o1.getKey().compareTo(o2.getKey());
|
||||||
|
}
|
||||||
|
} : new Comparator<Pair<Double,Integer>>() {
|
||||||
|
public int compare(Pair<Double, Integer> o1,
|
||||||
|
Pair<Double, Integer> o2) {
|
||||||
|
return o2.getKey().compareTo(o1.getKey());
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Sort.
|
||||||
Collections.sort(list, comp);
|
Collections.sort(list, comp);
|
||||||
|
|
||||||
|
// Modify the original array so that its elements are in
|
||||||
|
// the prescribed order.
|
||||||
|
// Retrieve indices of original locations.
|
||||||
|
final int[] indices = new int[len];
|
||||||
for (int i = 0; i < len; i++) {
|
for (int i = 0; i < len; i++) {
|
||||||
final Pair<Double, double[]> e = list.get(i);
|
final Pair<Double, Integer> e = list.get(i);
|
||||||
x[i] = e.getKey();
|
x[i] = e.getKey();
|
||||||
final double[] yValues = e.getValue();
|
indices[i] = e.getValue();
|
||||||
|
}
|
||||||
|
|
||||||
|
// In each of the associated arrays, move the
|
||||||
|
// elements to their new location.
|
||||||
for (int j = 0; j < yListLen; j++) {
|
for (int j = 0; j < yListLen; j++) {
|
||||||
yList[j][i] = yValues[j];
|
// Input array will be modified in place.
|
||||||
|
final double[] yInPlace = yList[j];
|
||||||
|
final double[] yOrig = yInPlace.clone();
|
||||||
|
|
||||||
|
for (int i = 0; i < len; i++) {
|
||||||
|
yInPlace[i] = yOrig[indices[i]];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue