commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From celes...@apache.org
Subject svn commit: r1178715 - in /commons/proper/math/trunk/src: main/java/org/apache/commons/math/ main/java/org/apache/commons/math/complex/ main/java/org/apache/commons/math/util/ test/java/org/apache/commons/math/complex/ test/java/org/apache/commons/math...
Date Tue, 04 Oct 2011 08:12:05 GMT
Author: celestin
Date: Tue Oct  4 08:12:05 2011
New Revision: 1178715

URL: http://svn.apache.org/viewvc?rev=1178715&view=rev
Log:
Added method multiply(int) to FieldElement, and updated implementing classes and tests accordingly.
Solves MATH-684.

Modified:
    commons/proper/math/trunk/src/main/java/org/apache/commons/math/FieldElement.java
    commons/proper/math/trunk/src/main/java/org/apache/commons/math/complex/Complex.java
    commons/proper/math/trunk/src/main/java/org/apache/commons/math/util/BigReal.java
    commons/proper/math/trunk/src/test/java/org/apache/commons/math/complex/ComplexTest.java
    commons/proper/math/trunk/src/test/java/org/apache/commons/math/util/BigRealTest.java

Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math/FieldElement.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math/FieldElement.java?rev=1178715&r1=1178714&r2=1178715&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math/FieldElement.java (original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math/FieldElement.java Tue
Oct  4 08:12:05 2011
@@ -38,6 +38,16 @@ public interface FieldElement<T> {
      */
     T subtract(T a);
 
+    /** Compute n &times; this. Multiplication by an integer number is defined
+     * as the following sum
+     * <center>
+     * n &times; this = &sum;<sub>i=1</sub><sup>n</sup> this.
+     * </center>
+     * @param n Number of times {@code this} must be added to itself.
+     * @return A new element representing n &times; this.
+     */
+    T multiply(int n);
+
     /** Compute this &times; a.
      * @param a element to multiply
      * @return a new element representing this &times; a

Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math/complex/Complex.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math/complex/Complex.java?rev=1178715&r1=1178714&r2=1178715&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math/complex/Complex.java (original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math/complex/Complex.java Tue
Oct  4 08:12:05 2011
@@ -426,6 +426,25 @@ public class Complex implements FieldEle
 
     /**
      * Returns a {@code Complex} whose value is {@code this * factor}, with {@code factor}
+     * interpreted as a integer number.
+     *
+     * @param  factor value to be multiplied by this {@code Complex}.
+     * @return {@code this * factor}.
+     * @see #multiply(Complex)
+     */
+    public Complex multiply(final int factor) {
+        if (isNaN) {
+            return NaN;
+        }
+        if (Double.isInfinite(real) ||
+            Double.isInfinite(imaginary)) {
+            return INF;
+        }
+        return createComplex(real * factor, imaginary * factor);
+    }
+
+    /**
+     * Returns a {@code Complex} whose value is {@code this * factor}, with {@code factor}
      * interpreted as a real number.
      *
      * @param  factor value to be multiplied by this {@code Complex}.

Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math/util/BigReal.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math/util/BigReal.java?rev=1178715&r1=1178714&r2=1178715&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math/util/BigReal.java (original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math/util/BigReal.java Tue
Oct  4 08:12:05 2011
@@ -247,6 +247,11 @@ public class BigReal implements FieldEle
     }
 
     /** {@inheritDoc} */
+    public BigReal multiply(final int n) {
+        return new BigReal(d.multiply(new BigDecimal(n)));
+    }
+
+    /** {@inheritDoc} */
     public int compareTo(BigReal a) {
         return d.compareTo(a.d);
     }
@@ -288,5 +293,4 @@ public class BigReal implements FieldEle
     public Field<BigReal> getField() {
         return BigRealField.getInstance();
     }
-
 }

Modified: commons/proper/math/trunk/src/test/java/org/apache/commons/math/complex/ComplexTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math/complex/ComplexTest.java?rev=1178715&r1=1178714&r2=1178715&view=diff
==============================================================================
--- commons/proper/math/trunk/src/test/java/org/apache/commons/math/complex/ComplexTest.java
(original)
+++ commons/proper/math/trunk/src/test/java/org/apache/commons/math/complex/ComplexTest.java
Tue Oct  4 08:12:05 2011
@@ -129,7 +129,7 @@ public class ComplexTest {
         Assert.assertTrue(Double.isNaN(x.add(z).getReal()));
     }
 
-    
+
     @Test
     public void testScalarAdd() {
         Complex x = new Complex(3.0, 4.0);
@@ -150,7 +150,7 @@ public class ComplexTest {
     public void testScalarAddInf() {
         Complex x = new Complex(1, 1);
         double yDouble = Double.POSITIVE_INFINITY;
-        
+
         Complex yComplex = new Complex(yDouble);
         Assert.assertEquals(x.add(yComplex), x.add(yDouble));
 
@@ -288,11 +288,11 @@ public class ComplexTest {
         yDouble = Double.NEGATIVE_INFINITY;
         yComplex = new Complex(yDouble);
         TestUtils.assertEquals(x.divide(yComplex), x.divide(yDouble), 0);
-        
+
         x = new Complex(1, Double.NEGATIVE_INFINITY);
         TestUtils.assertEquals(x.divide(yComplex), x.divide(yDouble), 0);
     }
-    
+
     @Test
     public void testScalarDivideZero() {
         Complex x = new Complex(1,1);
@@ -313,10 +313,12 @@ public class ComplexTest {
         Complex x = new Complex(3.0, 4.0);
         Complex z = x.multiply(Complex.NaN);
         Assert.assertSame(Complex.NaN, z);
+        z = Complex.NaN.multiply(5);
+        Assert.assertSame(Complex.NaN, z);
     }
 
     @Test
-    public void testMultiplyInInf() {
+    public void testMultiplyInfInf() {
         // Assert.assertTrue(infInf.multiply(infInf).isNaN()); // MATH-620
         Assert.assertTrue(infInf.multiply(infInf).isInfinite());
     }
@@ -340,7 +342,7 @@ public class ComplexTest {
         w = negInfNegInf.multiply(oneNaN);
         Assert.assertTrue(Double.isNaN(w.getReal()));
         Assert.assertTrue(Double.isNaN(w.getImaginary()));
-        
+
         z = new Complex(1, neginf);
         Assert.assertSame(Complex.INF, z.multiply(z));
     }
@@ -351,6 +353,9 @@ public class ComplexTest {
         double yDouble = 2.0;
         Complex yComplex = new Complex(yDouble);
         Assert.assertEquals(x.multiply(yComplex), x.multiply(yDouble));
+        int zInt = -5;
+        Complex zComplex = new Complex(zInt);
+        Assert.assertEquals(x.multiply(zComplex), x.multiply(zInt));
     }
 
     @Test
@@ -367,7 +372,7 @@ public class ComplexTest {
         double yDouble = Double.POSITIVE_INFINITY;
         Complex yComplex = new Complex(yDouble);
         Assert.assertEquals(x.multiply(yComplex), x.multiply(yDouble));
-        
+
         yDouble = Double.NEGATIVE_INFINITY;
         yComplex = new Complex(yDouble);
         Assert.assertEquals(x.multiply(yComplex), x.multiply(yDouble));
@@ -417,7 +422,7 @@ public class ComplexTest {
         x = new Complex(neginf, 0);
         Assert.assertTrue(Double.isNaN(x.subtract(z).getReal()));
     }
-    
+
     @Test
     public void testScalarSubtract() {
         Complex x = new Complex(3.0, 4.0);
@@ -445,7 +450,7 @@ public class ComplexTest {
         Assert.assertEquals(x.subtract(yComplex), x.subtract(yDouble));
     }
 
-    
+
     @Test
     public void testEqualsNull() {
         Complex x = new Complex(3.0, 4.0);
@@ -507,7 +512,7 @@ public class ComplexTest {
         Assert.assertEquals(realNaN.hashCode(), imaginaryNaN.hashCode());
         Assert.assertEquals(imaginaryNaN.hashCode(), Complex.NaN.hashCode());
     }
-    
+
     @Test
     public void testAcos() {
         Complex z = new Complex(3, 4);

Modified: commons/proper/math/trunk/src/test/java/org/apache/commons/math/util/BigRealTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math/util/BigRealTest.java?rev=1178715&r1=1178714&r2=1178715&view=diff
==============================================================================
--- commons/proper/math/trunk/src/test/java/org/apache/commons/math/util/BigRealTest.java
(original)
+++ commons/proper/math/trunk/src/test/java/org/apache/commons/math/util/BigRealTest.java
Tue Oct  4 08:12:05 2011
@@ -87,6 +87,8 @@ public class BigRealTest {
         BigReal a = new BigReal("1024.0");
         BigReal b = new BigReal("0.0009765625");
         Assert.assertEquals(1.0, a.multiply(b).doubleValue(), 1.0e-15);
+        int n = 1024;
+        Assert.assertEquals(1.0, b.multiply(n).doubleValue(), 1.0e-15);
     }
 
     @Test



Mime
View raw message