commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From er...@apache.org
Subject svn commit: r1410121 - in /commons/proper/math/trunk/src: main/java/org/apache/commons/math3/util/MathArrays.java main/java/org/apache/commons/math3/util/ResizableDoubleArray.java test/java/org/apache/commons/math3/util/ResizableDoubleArrayTest.java
Date Fri, 16 Nov 2012 00:18:31 GMT
Author: erans
Date: Fri Nov 16 00:18:30 2012
New Revision: 1410121

URL: http://svn.apache.org/viewvc?rev=1410121&view=rev
Log:
MATH-894
Introducing new interface for operating on a "double[]" (added in
the "MathArrays" class). Using this interface, operations can be
performed on the internal array of a "ResizeableDoubleArray" through
a new "compute" method. Thanks to Phil Steitz.


Modified:
    commons/proper/math/trunk/src/main/java/org/apache/commons/math3/util/MathArrays.java
    commons/proper/math/trunk/src/main/java/org/apache/commons/math3/util/ResizableDoubleArray.java
    commons/proper/math/trunk/src/test/java/org/apache/commons/math3/util/ResizableDoubleArrayTest.java

Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math3/util/MathArrays.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math3/util/MathArrays.java?rev=1410121&r1=1410120&r2=1410121&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math3/util/MathArrays.java
(original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math3/util/MathArrays.java
Fri Nov 16 00:18:30 2012
@@ -48,6 +48,28 @@ public class MathArrays {
     private MathArrays() {}
 
     /**
+     * Real-valued function that operate on an array or a part of it.
+     */
+    public interface Function {
+        /**
+         * Operates on an entire array.
+         *
+         * @param array Array to operate on.
+         * @return the result of the operation.
+         */
+        double evaluate(double[] array);
+        /**
+         * @param array Array to operate on.
+         * @param startIndex Index of the first element to take into account.
+         * @param numElements Number of elements to take into account.
+         * @return the result of the operation.
+         */
+        double evaluate(double[] array,
+                        int startIndex,
+                        int numElements);
+    }
+
+    /**
      * Calculates the L<sub>1</sub> (sum of abs) distance between two points.
      *
      * @param p1 the first point

Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math3/util/ResizableDoubleArray.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math3/util/ResizableDoubleArray.java?rev=1410121&r1=1410120&r2=1410121&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math3/util/ResizableDoubleArray.java
(original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math3/util/ResizableDoubleArray.java
Fri Nov 16 00:18:30 2012
@@ -805,6 +805,15 @@ public class ResizableDoubleArray implem
         }
     }
 
+    /**
+     * Performs an operation on the addressable elements of the array.
+     *
+     * @param f Function to be applied on this array.
+     * @return the result.
+     */
+    public double compute(MathArrays.Function f) {
+        return f.evaluate(internalArray, startIndex, numElements);
+    }
 
     /**
      * Sets the element at the specified index.  If the specified index is greater than

Modified: commons/proper/math/trunk/src/test/java/org/apache/commons/math3/util/ResizableDoubleArrayTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math3/util/ResizableDoubleArrayTest.java?rev=1410121&r1=1410120&r2=1410121&view=diff
==============================================================================
--- commons/proper/math/trunk/src/test/java/org/apache/commons/math3/util/ResizableDoubleArrayTest.java
(original)
+++ commons/proper/math/trunk/src/test/java/org/apache/commons/math3/util/ResizableDoubleArrayTest.java
Fri Nov 16 00:18:30 2012
@@ -558,6 +558,32 @@ public class ResizableDoubleArrayTest ex
         Assert.assertEquals(v2, a.getElement(index), 0d);
     }
 
+    @Test
+    public void testCompute() {
+        final ResizableDoubleArray a = new ResizableDoubleArray();
+        final int max = 20;
+        for (int i = 1; i <= max; i++) {
+            a.setElement(i, i);
+        }
+
+        final MathArrays.Function add = new MathArrays.Function() {
+                public double evaluate(double[] a, int index, int num) {
+                    double sum = 0;
+                    final int max = index + num;
+                    for (int i = index; i < max; i++) {
+                        sum += a[i];
+                    }
+                    return sum;
+                }
+                public double evaluate(double[] a) {
+                    return evaluate(a, 0, a.length);
+                }
+            };
+
+        final double sum = a.compute(add);
+        Assert.assertEquals(0.5 * max * (max + 1), sum, 0);
+    }
+
     private void verifyEquality(ResizableDoubleArray a, ResizableDoubleArray b) {
         Assert.assertTrue(b.equals(a));
         Assert.assertTrue(a.equals(b));



Mime
View raw message