Functionality has been moved to "Commons Numbers" (cf. NUMBERS-164).

This commit is contained in:
Gilles Sadowski 2021-06-17 15:22:48 +02:00
parent a1f2a98c28
commit 0a3c4023dd
2 changed files with 0 additions and 240 deletions

View File

@ -609,145 +609,6 @@ public class MathArrays {
}
}
/**
* Sort an array in ascending order in place and perform the same reordering
* of entries on other arrays. For example, if
* {@code x = [3, 1, 2], y = [1, 2, 3]} and {@code z = [0, 5, 7]}, then
* {@code sortInPlace(x, y, z)} will update {@code x} to {@code [1, 2, 3]},
* {@code y} to {@code [2, 3, 1]} and {@code z} to {@code [5, 7, 0]}.
*
* @param x Array to be sorted and used as a pattern for permutation
* of the other arrays.
* @param yList Set of arrays whose permutations of entries will follow
* those performed on {@code x}.
* @throws DimensionMismatchException if any {@code y} is not the same
* size as {@code x}.
* @throws NullArgumentException if {@code x} or any {@code y} is null.
* @since 3.0
*/
public static void sortInPlace(double[] x, double[] ... yList) {
sortInPlace(x, OrderDirection.INCREASING, yList);
}
/**
* Helper data structure holding a (double, integer) pair.
*/
private static class PairDoubleInteger {
/** Key */
private final double key;
/** Value */
private final int value;
/**
* @param key Key.
* @param value Value.
*/
PairDoubleInteger(double key, int value) {
this.key = key;
this.value = value;
}
/** @return the key. */
public double getKey() {
return key;
}
/** @return the value. */
public int getValue() {
return value;
}
}
/**
* Sort an array in place and perform the same reordering of entries on
* other arrays. This method works the same as the other
* {@link #sortInPlace(double[], double[][]) sortInPlace} method, but
* allows the order of the sort to be provided in the {@code dir}
* parameter.
*
* @param x Array to be sorted and used as a pattern for permutation
* of the other arrays.
* @param dir Order direction.
* @param yList Set of arrays whose permutations of entries will follow
* those performed on {@code x}.
* @throws DimensionMismatchException if any {@code y} is not the same
* size as {@code x}.
* @throws NullArgumentException if {@code x} or any {@code y} is null
* @since 3.0
*/
public static void sortInPlace(double[] x,
final OrderDirection dir,
double[] ... yList) {
// Consistency checks.
if (x == null) {
throw new NullArgumentException();
}
final int yListLen = yList.length;
final int len = x.length;
for (int j = 0; j < yListLen; j++) {
final double[] y = yList[j];
if (y == null) {
throw new NullArgumentException();
}
if (y.length != len) {
throw new DimensionMismatchException(y.length, len);
}
}
// Associate each abscissa "x[i]" with its index "i".
final List<PairDoubleInteger> list
= new ArrayList<>(len);
for (int i = 0; i < len; i++) {
list.add(new PairDoubleInteger(x[i], i));
}
// Create comparators for increasing and decreasing orders.
final Comparator<PairDoubleInteger> comp
= dir == MathArrays.OrderDirection.INCREASING ?
new Comparator<PairDoubleInteger>() {
/** {@inheritDoc} */
@Override
public int compare(PairDoubleInteger o1,
PairDoubleInteger o2) {
return Double.compare(o1.getKey(), o2.getKey());
}
} : new Comparator<PairDoubleInteger>() {
/** {@inheritDoc} */
@Override
public int compare(PairDoubleInteger o1,
PairDoubleInteger o2) {
return Double.compare(o2.getKey(), o1.getKey());
}
};
// Sort.
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++) {
final PairDoubleInteger e = list.get(i);
x[i] = e.getKey();
indices[i] = e.getValue();
}
// In each of the associated arrays, move the
// elements to their new location.
for (int j = 0; j < yListLen; 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]];
}
}
}
/**
* Returns true iff both arguments are null or have same dimensions and all
* their elements are equal as defined by

View File

@ -437,107 +437,6 @@ public class MathArraysTest {
Assert.assertTrue(MathArrays.checkEqualLength(a, b, false));
}
@Test
public void testSortInPlace() {
final double[] x1 = {2, 5, -3, 1, 4};
final double[] x2 = {4, 25, 9, 1, 16};
final double[] x3 = {8, 125, -27, 1, 64};
MathArrays.sortInPlace(x1, x2, x3);
Assert.assertEquals(-3, x1[0], AccurateMath.ulp(1d));
Assert.assertEquals(9, x2[0], AccurateMath.ulp(1d));
Assert.assertEquals(-27, x3[0], AccurateMath.ulp(1d));
Assert.assertEquals(1, x1[1], AccurateMath.ulp(1d));
Assert.assertEquals(1, x2[1], AccurateMath.ulp(1d));
Assert.assertEquals(1, x3[1], AccurateMath.ulp(1d));
Assert.assertEquals(2, x1[2], AccurateMath.ulp(1d));
Assert.assertEquals(4, x2[2], AccurateMath.ulp(1d));
Assert.assertEquals(8, x3[2], AccurateMath.ulp(1d));
Assert.assertEquals(4, x1[3], AccurateMath.ulp(1d));
Assert.assertEquals(16, x2[3], AccurateMath.ulp(1d));
Assert.assertEquals(64, x3[3], AccurateMath.ulp(1d));
Assert.assertEquals(5, x1[4], AccurateMath.ulp(1d));
Assert.assertEquals(25, x2[4], AccurateMath.ulp(1d));
Assert.assertEquals(125, x3[4], AccurateMath.ulp(1d));
}
@Test
public void testSortInPlaceDecreasingOrder() {
final double[] x1 = {2, 5, -3, 1, 4};
final double[] x2 = {4, 25, 9, 1, 16};
final double[] x3 = {8, 125, -27, 1, 64};
MathArrays.sortInPlace(x1,
MathArrays.OrderDirection.DECREASING,
x2, x3);
Assert.assertEquals(-3, x1[4], AccurateMath.ulp(1d));
Assert.assertEquals(9, x2[4], AccurateMath.ulp(1d));
Assert.assertEquals(-27, x3[4], AccurateMath.ulp(1d));
Assert.assertEquals(1, x1[3], AccurateMath.ulp(1d));
Assert.assertEquals(1, x2[3], AccurateMath.ulp(1d));
Assert.assertEquals(1, x3[3], AccurateMath.ulp(1d));
Assert.assertEquals(2, x1[2], AccurateMath.ulp(1d));
Assert.assertEquals(4, x2[2], AccurateMath.ulp(1d));
Assert.assertEquals(8, x3[2], AccurateMath.ulp(1d));
Assert.assertEquals(4, x1[1], AccurateMath.ulp(1d));
Assert.assertEquals(16, x2[1], AccurateMath.ulp(1d));
Assert.assertEquals(64, x3[1], AccurateMath.ulp(1d));
Assert.assertEquals(5, x1[0], AccurateMath.ulp(1d));
Assert.assertEquals(25, x2[0], AccurateMath.ulp(1d));
Assert.assertEquals(125, x3[0], AccurateMath.ulp(1d));
}
/** Example in javadoc */
@Test
public void testSortInPlaceExample() {
final double[] x = {3, 1, 2};
final double[] y = {1, 2, 3};
final double[] z = {0, 5, 7};
MathArrays.sortInPlace(x, y, z);
final double[] sx = {1, 2, 3};
final double[] sy = {2, 3, 1};
final double[] sz = {5, 7, 0};
Assert.assertTrue(Arrays.equals(sx, x));
Assert.assertTrue(Arrays.equals(sy, y));
Assert.assertTrue(Arrays.equals(sz, z));
}
@Test
public void testSortInPlaceFailures() {
final double[] nullArray = null;
final double[] one = {1};
final double[] two = {1, 2};
final double[] onep = {2};
try {
MathArrays.sortInPlace(one, two);
Assert.fail("Expecting DimensionMismatchException");
} catch (DimensionMismatchException ex) {
// expected
}
try {
MathArrays.sortInPlace(one, nullArray);
Assert.fail("Expecting NullArgumentException");
} catch (NullArgumentException ex) {
// expected
}
try {
MathArrays.sortInPlace(one, onep, nullArray);
Assert.fail("Expecting NullArgumentException");
} catch (NullArgumentException ex) {
// expected
}
}
@Test
public void testArrayEquals() {
Assert.assertFalse(MathArrays.equals(new double[] { 1d }, null));