commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From er...@apache.org
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 GMT
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);
+        }
+    }
 }



Mime
View raw message