commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From er...@apache.org
Subject svn commit: r1414435 - in /commons/proper/math/trunk/src: changes/changes.xml main/java/org/apache/commons/math3/util/MathArrays.java test/java/org/apache/commons/math3/util/MathArraysTest.java
Date Tue, 27 Nov 2012 22:19:37 GMT
Author: erans
Date: Tue Nov 27 22:19:36 2012
New Revision: 1414435

URL: http://svn.apache.org/viewvc?rev=1414435&view=rev
Log:
MATH-910
Element-by-element basic operations on arrays.

Modified:
    commons/proper/math/trunk/src/changes/changes.xml
    commons/proper/math/trunk/src/main/java/org/apache/commons/math3/util/MathArrays.java
    commons/proper/math/trunk/src/test/java/org/apache/commons/math3/util/MathArraysTest.java

Modified: commons/proper/math/trunk/src/changes/changes.xml
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/changes/changes.xml?rev=1414435&r1=1414434&r2=1414435&view=diff
==============================================================================
--- commons/proper/math/trunk/src/changes/changes.xml (original)
+++ commons/proper/math/trunk/src/changes/changes.xml Tue Nov 27 22:19:36 2012
@@ -52,6 +52,10 @@ If the output is not quite correct, chec
   <body>
     <release version="3.1" date="TBD" description="
 ">
+      <action dev="erans" type="add" issue="MATH-910">
+        Added element-by-element addition, subtraction, multiplication and division
+        (in class "o.a.c.m.util.MathArrays").
+      </action>
       <action dev="erans" type="fix" issue="MATH-904" due-to="Jeff Hain">
         Fixed "pow" method in class "FastMath".
       </action>

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=1414435&r1=1414434&r2=1414435&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
Tue Nov 27 22:19:36 2012
@@ -70,6 +70,91 @@ public class MathArrays {
     }
 
     /**
+     * Creates an array whose contents will be the element-by-element
+     * addition of the arguments.
+     *
+     * @param a First term of the addition.
+     * @param b Second term of the addition.
+     * @return a new array {@code r} where {@code r[i] = a[i] + b[i]}.
+     * @throws DimensionMismatchException if the array lengths differ.
+     */
+    public static double[] ebeAdd(double[] a,
+                                  double[] b) {
+        if (a.length != b.length) {
+            throw new DimensionMismatchException(a.length, b.length);
+        }
+
+        final double[] result = a.clone();
+        for (int i = 0; i < a.length; i++) {
+            result[i] += b[i];
+        }
+        return result;
+    }
+    /**
+     * Creates an array whose contents will be the element-by-element
+     * subtraction of the second argument from the first.
+     *
+     * @param a First term.
+     * @param b Element to be subtracted.
+     * @return a new array {@code r} where {@code r[i] = a[i] - b[i]}.
+     * @throws DimensionMismatchException if the array lengths differ.
+     */
+    public static double[] ebeSubtract(double[] a,
+                                       double[] b) {
+        if (a.length != b.length) {
+            throw new DimensionMismatchException(a.length, b.length);
+        }
+
+        final double[] result = a.clone();
+        for (int i = 0; i < a.length; i++) {
+            result[i] -= b[i];
+        }
+        return result;
+    }
+    /**
+     * Creates an array whose contents will be the element-by-element
+     * multiplication of the arguments.
+     *
+     * @param a First factor of the multiplication.
+     * @param b Second factor of the multiplication.
+     * @return a new array {@code r} where {@code r[i] = a[i] * b[i]}.
+     * @throws DimensionMismatchException if the array lengths differ.
+     */
+    public static double[] ebeMultiply(double[] a,
+                                       double[] b) {
+        if (a.length != b.length) {
+            throw new DimensionMismatchException(a.length, b.length);
+        }
+
+        final double[] result = a.clone();
+        for (int i = 0; i < a.length; i++) {
+            result[i] *= b[i];
+        }
+        return result;
+    }
+    /**
+     * Creates an array whose contents will be the element-by-element
+     * division of the first argument by the second.
+     *
+     * @param a Numerator of the division.
+     * @param b Denominator of the division.
+     * @return a new array {@code r} where {@code r[i] = a[i] / b[i]}.
+     * @throws DimensionMismatchException if the array lengths differ.
+     */
+    public static double[] ebeDivide(double[] a,
+                                     double[] b) {
+        if (a.length != b.length) {
+            throw new DimensionMismatchException(a.length, b.length);
+        }
+
+        final double[] result = a.clone();
+        for (int i = 0; i < a.length; i++) {
+            result[i] /= b[i];
+        }
+        return result;
+    }
+
+    /**
      * Calculates the L<sub>1</sub> (sum of abs) distance between two points.
      *
      * @param p1 the first point

Modified: commons/proper/math/trunk/src/test/java/org/apache/commons/math3/util/MathArraysTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math3/util/MathArraysTest.java?rev=1414435&r1=1414434&r2=1414435&view=diff
==============================================================================
--- commons/proper/math/trunk/src/test/java/org/apache/commons/math3/util/MathArraysTest.java
(original)
+++ commons/proper/math/trunk/src/test/java/org/apache/commons/math3/util/MathArraysTest.java
Tue Nov 27 22:19:36 2012
@@ -33,6 +33,64 @@ import org.junit.Test;
  * @version $Id$
  */
 public class MathArraysTest {
+    @Test(expected=DimensionMismatchException.class)
+    public void testEbeAddPrecondition() {
+        MathArrays.ebeAdd(new double[3], new double[4]);
+    }
+    @Test(expected=DimensionMismatchException.class)
+    public void testEbeSubtractPrecondition() {
+        MathArrays.ebeSubtract(new double[3], new double[4]);
+    }
+    @Test(expected=DimensionMismatchException.class)
+    public void testEbeMultiplyPrecondition() {
+        MathArrays.ebeMultiply(new double[3], new double[4]);
+    }
+    @Test(expected=DimensionMismatchException.class)
+    public void testEbeDividePrecondition() {
+        MathArrays.ebeDivide(new double[3], new double[4]);
+    }
+
+    @Test
+    public void testEbeAdd() {
+        final double[] a = { 0, 1, 2 };
+        final double[] b = { 3, 5, 7 };
+        final double[] r = MathArrays.ebeAdd(a, b);
+
+        for (int i = 0; i < a.length; i++) {
+            Assert.assertEquals(a[i] + b[i], r[i], 0);
+        }
+    }
+    @Test
+    public void testEbeSubtract() {
+        final double[] a = { 0, 1, 2 };
+        final double[] b = { 3, 5, 7 };
+        final double[] r = MathArrays.ebeSubtract(a, b);
+
+        for (int i = 0; i < a.length; i++) {
+            Assert.assertEquals(a[i] - b[i], r[i], 0);
+        }
+    }
+    @Test
+    public void testEbeMultiply() {
+        final double[] a = { 0, 1, 2 };
+        final double[] b = { 3, 5, 7 };
+        final double[] r = MathArrays.ebeMultiply(a, b);
+
+        for (int i = 0; i < a.length; i++) {
+            Assert.assertEquals(a[i] * b[i], r[i], 0);
+        }
+    }
+    @Test
+    public void testEbeDivide() {
+        final double[] a = { 0, 1, 2 };
+        final double[] b = { 3, 5, 7 };
+        final double[] r = MathArrays.ebeDivide(a, b);
+
+        for (int i = 0; i < a.length; i++) {
+            Assert.assertEquals(a[i] / b[i], r[i], 0);
+        }
+    }
+
     @Test
     public void testL1DistanceDouble() {
         double[] p1 = { 2.5,  0.0 };



Mime
View raw message