Return-Path: Delivered-To: apmail-commons-commits-archive@minotaur.apache.org Received: (qmail 52970 invoked from network); 31 Jan 2011 13:14:04 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.3) by minotaur.apache.org with SMTP; 31 Jan 2011 13:14:04 -0000 Received: (qmail 97827 invoked by uid 500); 31 Jan 2011 13:14:04 -0000 Delivered-To: apmail-commons-commits-archive@commons.apache.org Received: (qmail 97550 invoked by uid 500); 31 Jan 2011 13:14:01 -0000 Mailing-List: contact commits-help@commons.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@commons.apache.org Delivered-To: mailing list commits@commons.apache.org Received: (qmail 97543 invoked by uid 99); 31 Jan 2011 13:14:01 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 31 Jan 2011 13:14:01 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=5.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 31 Jan 2011 13:14:00 +0000 Received: by eris.apache.org (Postfix, from userid 65534) id 1A32B2388A6C; Mon, 31 Jan 2011 13:13:39 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r1065595 - in /commons/proper/math/trunk/src: main/java/org/apache/commons/math/optimization/direct/ main/java/org/apache/commons/math/util/ test/java/org/apache/commons/math/util/ Date: Mon, 31 Jan 2011 13:13:38 -0000 To: commits@commons.apache.org From: erans@apache.org X-Mailer: svnmailer-1.0.8 Message-Id: <20110131131339.1A32B2388A6C@eris.apache.org> Author: erans Date: Mon Jan 31 13:13:38 2011 New Revision: 1065595 URL: http://svn.apache.org/viewvc?rev=1065595&view=rev Log: Added utility method "copyOf" in "MathUtils". Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/direct/PowellOptimizer.java commons/proper/math/trunk/src/main/java/org/apache/commons/math/util/MathUtils.java commons/proper/math/trunk/src/main/java/org/apache/commons/math/util/MultidimensionalCounter.java commons/proper/math/trunk/src/test/java/org/apache/commons/math/util/MathUtilsTest.java Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/direct/PowellOptimizer.java URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/direct/PowellOptimizer.java?rev=1065595&r1=1065594&r2=1065595&view=diff ============================================================================== --- commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/direct/PowellOptimizer.java (original) +++ commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/direct/PowellOptimizer.java Mon Jan 31 13:13:38 2011 @@ -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; - } } Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math/util/MathUtils.java URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math/util/MathUtils.java?rev=1065595&r1=1065594&r2=1065595&view=diff ============================================================================== --- commons/proper/math/trunk/src/main/java/org/apache/commons/math/util/MathUtils.java (original) +++ commons/proper/math/trunk/src/main/java/org/apache/commons/math/util/MathUtils.java Mon Jan 31 13:13:38 2011 @@ -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; + } } Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math/util/MultidimensionalCounter.java URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math/util/MultidimensionalCounter.java?rev=1065595&r1=1065594&r2=1065595&view=diff ============================================================================== --- commons/proper/math/trunk/src/main/java/org/apache/commons/math/util/MultidimensionalCounter.java (original) +++ commons/proper/math/trunk/src/main/java/org/apache/commons/math/util/MultidimensionalCounter.java Mon Jan 31 13:13:38 2011 @@ -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 imp * @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 imp */ 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 imp * @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 imp } 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; - } } Modified: commons/proper/math/trunk/src/test/java/org/apache/commons/math/util/MathUtilsTest.java URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math/util/MathUtilsTest.java?rev=1065595&r1=1065594&r2=1065595&view=diff ============================================================================== --- commons/proper/math/trunk/src/test/java/org/apache/commons/math/util/MathUtilsTest.java (original) +++ commons/proper/math/trunk/src/test/java/org/apache/commons/math/util/MathUtilsTest.java Mon Jan 31 13:13:38 2011 @@ -1544,4 +1544,33 @@ public final class MathUtilsTest extends 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); + } + } }