You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by lu...@apache.org on 2008/02/08 12:22:40 UTC

svn commit: r619836 - in /commons/proper/math/trunk/src: java/org/apache/commons/math/util/MathUtils.java site/xdoc/changes.xml test/org/apache/commons/math/util/MathUtilsTest.java

Author: luc
Date: Fri Feb  8 03:22:35 2008
New Revision: 619836

URL: http://svn.apache.org/viewvc?rev=619836&view=rev
Log:
added equals and hash methods for double arrays (supporting null) in MathUtils

Modified:
    commons/proper/math/trunk/src/java/org/apache/commons/math/util/MathUtils.java
    commons/proper/math/trunk/src/site/xdoc/changes.xml
    commons/proper/math/trunk/src/test/org/apache/commons/math/util/MathUtilsTest.java

Modified: commons/proper/math/trunk/src/java/org/apache/commons/math/util/MathUtils.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/java/org/apache/commons/math/util/MathUtils.java?rev=619836&r1=619835&r2=619836&view=diff
==============================================================================
--- commons/proper/math/trunk/src/java/org/apache/commons/math/util/MathUtils.java (original)
+++ commons/proper/math/trunk/src/java/org/apache/commons/math/util/MathUtils.java Fri Feb  8 03:22:35 2008
@@ -276,6 +276,30 @@
     }
 
     /**
+     * Returns true iff both arguments aren null or have same dimensions
+     * and all their elements are {@link #equals(double,double) equals}
+     * 
+     * @param x first array
+     * @param y second array
+     * @return true if the values are both null or have same dimension
+     * and equal elements
+     */
+    public static boolean equals(double[] x, double[] y) {
+        if ((x == null) || (y == null)) {
+            return !((x == null) ^ (y == null));
+        }
+        if (x.length != y.length) {
+            return false;
+        }
+        for (int i = 0; i < x.length; ++i) {
+            if (!equals(x[i], y[i])) {
+                return false;
+            }
+        }
+        return true;
+    }
+
+    /**
      * Returns n!. Shorthand for <code>n</code> <a
      * href="http://mathworld.wolfram.com/Factorial.html"> Factorial</a>, the
      * product of the numbers <code>1,...,n</code>.
@@ -430,6 +454,23 @@
     public static int hash(double value) {
         long bits = Double.doubleToLongBits(value);
         return (int)(bits ^ (bits >>> 32));
+    }
+
+    /**
+     * Returns an integer hash code representing the given double array value.
+     * 
+     * @param value the value to be hashed (may be null)
+     * @return the hash code
+     */
+    public static int hash(double[] value) {
+        if (value == null) {
+            return 0;
+        }
+        int result = value.length;
+        for (int i = 0; i < value.length; ++i) {
+            result = result * 31 + hash(value[i]);
+        }
+        return result;
     }
 
     /**

Modified: commons/proper/math/trunk/src/site/xdoc/changes.xml
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/site/xdoc/changes.xml?rev=619836&r1=619835&r2=619836&view=diff
==============================================================================
--- commons/proper/math/trunk/src/site/xdoc/changes.xml (original)
+++ commons/proper/math/trunk/src/site/xdoc/changes.xml Fri Feb  8 03:22:35 2008
@@ -143,6 +143,9 @@
         Throw EOFException when using empty files with ValueServer in replay and
         digest modes.
       </action>     
+      <action dev="luc" type="update" >
+        Added a equals and hash methods in MathUtils to check for double arrays
+      </action> 
     </release>
     <release version="1.1" date="2005-12-17"  
  description="This is a maintenance release containing bug fixes and enhancements.

Modified: commons/proper/math/trunk/src/test/org/apache/commons/math/util/MathUtilsTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/org/apache/commons/math/util/MathUtilsTest.java?rev=619836&r1=619835&r2=619836&view=diff
==============================================================================
--- commons/proper/math/trunk/src/test/org/apache/commons/math/util/MathUtilsTest.java (original)
+++ commons/proper/math/trunk/src/test/org/apache/commons/math/util/MathUtilsTest.java Fri Feb  8 03:22:35 2008
@@ -209,6 +209,27 @@
         }
     }
 
+    public void testArrayEquals() {
+        assertFalse(MathUtils.equals(new double[] { 1d }, null));
+        assertFalse(MathUtils.equals(null, new double[] { 1d }));
+        assertTrue(MathUtils.equals((double[]) null, (double[]) null));
+
+        assertFalse(MathUtils.equals(new double[] { 1d }, new double[0]));
+        assertTrue(MathUtils.equals(new double[] { 1d }, new double[] { 1d }));
+        assertTrue(MathUtils.equals(new double[] {
+                                      Double.NaN, Double.POSITIVE_INFINITY,
+                                      Double.NEGATIVE_INFINITY, 1d, 0d
+                                    }, new double[] {
+                                      Double.NaN, Double.POSITIVE_INFINITY,
+                                      Double.NEGATIVE_INFINITY, 1d, 0d
+                                    }));
+        assertFalse(MathUtils.equals(new double[] { Double.POSITIVE_INFINITY },
+                                     new double[] { Double.NEGATIVE_INFINITY }));
+        assertFalse(MathUtils.equals(new double[] { 1d },
+                                     new double[] { MathUtils.nextAfter(1d, 2d) }));
+
+    }
+
     public void testFactorial() {
         for (int i = 1; i < 10; i++) {
             assertEquals(i + "! ", factorial(i), MathUtils.factorial(i));
@@ -293,6 +314,22 @@
                 }
             }
         }
+    }
+
+    public void testArrayHash() {
+        assertEquals(0, MathUtils.hash((double[]) null));
+        assertEquals(MathUtils.hash(new double[] {
+                                      Double.NaN, Double.POSITIVE_INFINITY,
+                                      Double.NEGATIVE_INFINITY, 1d, 0d
+                                    }),
+                     MathUtils.hash(new double[] {
+                                      Double.NaN, Double.POSITIVE_INFINITY,
+                                      Double.NEGATIVE_INFINITY, 1d, 0d
+                                    }));
+        assertFalse(MathUtils.hash(new double[] { 1d }) ==
+                    MathUtils.hash(new double[] { MathUtils.nextAfter(1d, 2d) }));
+        assertFalse(MathUtils.hash(new double[] { 1d }) ==
+                    MathUtils.hash(new double[] { 1d, 1d }));
     }
 
     public void testIndicatorByte() {