Added utility method "copyOf" in "MathUtils".
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/trunk@1065595 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
09d782ad0c
commit
36656ded2d
|
@ -18,6 +18,7 @@
|
|||
package org.apache.commons.math.optimization.direct;
|
||||
|
||||
import org.apache.commons.math.util.FastMath;
|
||||
import org.apache.commons.math.util.MathUtils;
|
||||
import org.apache.commons.math.analysis.UnivariateRealFunction;
|
||||
import org.apache.commons.math.analysis.MultivariateRealFunction;
|
||||
import org.apache.commons.math.exception.NumberIsTooSmallException;
|
||||
|
@ -122,7 +123,7 @@ public class PowellOptimizer
|
|||
double alphaMin = 0;
|
||||
|
||||
for (int i = 0; i < n; i++) {
|
||||
final double[] d = /* Arrays.*/ copyOf(direc[i], n); // Java 1.5 does not support Arrays.copyOf()
|
||||
final double[] d = MathUtils.copyOf(direc[i]);
|
||||
|
||||
fX2 = fVal;
|
||||
|
||||
|
@ -267,17 +268,4 @@ public class PowellOptimizer
|
|||
bracket.getLo(), bracket.getHi(), bracket.getMid());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Java 1.5 does not support Arrays.copyOf()
|
||||
*
|
||||
* @param source Array to be copied.
|
||||
* @param newLen Length of the copy to be returned.
|
||||
* @return the copied array, truncated or padded as necessary.
|
||||
*/
|
||||
private double[] copyOf(double[] source, int newLen) {
|
||||
double[] output = new double[newLen];
|
||||
System.arraycopy(source, 0, output, 0, Math.min(source.length, newLen));
|
||||
return output;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2212,4 +2212,30 @@ public final class MathUtils {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a copy of the {@code source} array.
|
||||
*
|
||||
* @param source Array to be copied.
|
||||
* @return the copied array.
|
||||
*/
|
||||
public static int[] copyOf(int[] source) {
|
||||
final int len = source.length;
|
||||
final int[] output = new int[len];
|
||||
System.arraycopy(source, 0, output, 0, len);
|
||||
return output;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a copy of the {@code source} array.
|
||||
*
|
||||
* @param source Array to be copied.
|
||||
* @return the copied array.
|
||||
*/
|
||||
public static double[] copyOf(double[] source) {
|
||||
final int len = source.length;
|
||||
final double[] output = new double[len];
|
||||
System.arraycopy(source, 0, output, 0, len);
|
||||
return output;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -20,6 +20,7 @@ package org.apache.commons.math.util;
|
|||
import org.apache.commons.math.exception.DimensionMismatchException;
|
||||
import org.apache.commons.math.exception.OutOfRangeException;
|
||||
import org.apache.commons.math.exception.NotStrictlyPositiveException;
|
||||
import org.apache.commons.math.util.MathUtils;
|
||||
|
||||
/**
|
||||
* Converter between unidimensional storage structure and multidimensional
|
||||
|
@ -128,7 +129,7 @@ public class MultidimensionalCounter implements Iterable<Integer> {
|
|||
* @return the indices within the multidimensional counter.
|
||||
*/
|
||||
public int[] getCounts() {
|
||||
return /* Arrays.*/ copyOf(counter, dimension); // Java 1.5 does not support Arrays.copyOf()
|
||||
return MathUtils.copyOf(counter);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -163,7 +164,7 @@ public class MultidimensionalCounter implements Iterable<Integer> {
|
|||
*/
|
||||
public MultidimensionalCounter(int ... size) {
|
||||
dimension = size.length;
|
||||
this.size = /* Arrays.*/ copyOf(size, dimension); // Java 1.5 does not support Arrays.copyOf()
|
||||
this.size = MathUtils.copyOf(size);
|
||||
|
||||
uniCounterOffset = new int[dimension];
|
||||
|
||||
|
@ -285,7 +286,7 @@ public class MultidimensionalCounter implements Iterable<Integer> {
|
|||
* @return the sizes of the multidimensional counter in each dimension.
|
||||
*/
|
||||
public int[] getSizes() {
|
||||
return /* Arrays.*/ copyOf(size, dimension); // Java 1.5 does not support Arrays.copyOf()
|
||||
return MathUtils.copyOf(size);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -299,17 +300,4 @@ public class MultidimensionalCounter implements Iterable<Integer> {
|
|||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* Java 1.5 does not support Arrays.copyOf()
|
||||
*
|
||||
* @param source Array to be copied.
|
||||
* @param newLen Length of the copy to be returned.
|
||||
* @return the copied array, truncated or padded as necessary.
|
||||
*/
|
||||
private int[] copyOf(int[] source, int newLen) {
|
||||
int[] output = new int[newLen];
|
||||
System.arraycopy(source, 0, output, 0, Math.min(source.length, newLen));
|
||||
return output;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1544,4 +1544,33 @@ public final class MathUtilsTest extends TestCase {
|
|||
assertEquals(25, x2[4], Math.ulp(1d));
|
||||
assertEquals(125, x3[4], Math.ulp(1d));
|
||||
}
|
||||
|
||||
public void testCopyOfInt() {
|
||||
final int[] source = { Integer.MIN_VALUE,
|
||||
-1, 0, 1, 3, 113, 4769,
|
||||
Integer.MAX_VALUE };
|
||||
final int[] dest = MathUtils.copyOf(source);
|
||||
|
||||
assertEquals(dest.length, source.length);
|
||||
for (int i = 0; i < source.length; i++) {
|
||||
assertEquals(source[i], dest[i]);
|
||||
}
|
||||
}
|
||||
|
||||
public void testCopyOfDouble() {
|
||||
final double[] source = { Double.NEGATIVE_INFINITY,
|
||||
-Double.MAX_VALUE,
|
||||
-1, 0,
|
||||
Double.MIN_VALUE,
|
||||
Math.ulp(1d),
|
||||
1, 3, 113, 4769,
|
||||
Double.MAX_VALUE,
|
||||
Double.POSITIVE_INFINITY };
|
||||
final double[] dest = MathUtils.copyOf(source);
|
||||
|
||||
assertEquals(dest.length, source.length);
|
||||
for (int i = 0; i < source.length; i++) {
|
||||
assertEquals(source[i], dest[i], 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue