commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From l..@apache.org
Subject svn commit: r791237 - in /commons/proper/math/trunk: pom.xml src/java/org/apache/commons/math/complex/Complex.java src/site/xdoc/changes.xml src/test/org/apache/commons/math/complex/ComplexTest.java
Date Sun, 05 Jul 2009 12:53:14 GMT
Author: luc
Date: Sun Jul  5 12:53:13 2009
New Revision: 791237

URL: http://svn.apache.org/viewvc?rev=791237&view=rev
Log:
added scalar multiply to the Complex class
JIRA: MATH-277

Modified:
    commons/proper/math/trunk/pom.xml
    commons/proper/math/trunk/src/java/org/apache/commons/math/complex/Complex.java
    commons/proper/math/trunk/src/site/xdoc/changes.xml
    commons/proper/math/trunk/src/test/org/apache/commons/math/complex/ComplexTest.java

Modified: commons/proper/math/trunk/pom.xml
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/pom.xml?rev=791237&r1=791236&r2=791237&view=diff
==============================================================================
--- commons/proper/math/trunk/pom.xml (original)
+++ commons/proper/math/trunk/pom.xml Sun Jul  5 12:53:13 2009
@@ -103,6 +103,9 @@
       <name>C. Scott Ananian</name>
     </contributor>
     <contributor>
+      <name>Mark Anderson</name>
+    </contributor>
+    <contributor>
       <name>R&#233;mi Arntzen</name>
     </contributor>
     <contributor>

Modified: commons/proper/math/trunk/src/java/org/apache/commons/math/complex/Complex.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/java/org/apache/commons/math/complex/Complex.java?rev=791237&r1=791236&r2=791237&view=diff
==============================================================================
--- commons/proper/math/trunk/src/java/org/apache/commons/math/complex/Complex.java (original)
+++ commons/proper/math/trunk/src/java/org/apache/commons/math/complex/Complex.java Sun Jul
 5 12:53:13 2009
@@ -387,6 +387,44 @@
     }
     
     /**
+     * Return the product of this complex number and the given scalar number.
+     * <p>
+     * Implements preliminary checks for NaN and infinity followed by
+     * the definitional formula:
+     * <pre><code>
+     * c(a + bi) = (ca) + (cb)i
+     * </code></pre>
+     * </p>
+     * <p>
+     * Returns {@link #NaN} if either this or <code>rhs</code> has one or more
+     * NaN parts.
+     * </p>
+     * Returns {@link #INF} if neither this nor <code>rhs</code> has one or more
+     * NaN parts and if either this or <code>rhs</code> has one or more
+     * infinite parts (same result is returned regardless of the sign of the
+     * components).
+     * </p>
+     * <p>
+     * Returns finite values in components of the result per the
+     * definitional formula in all remaining cases.
+     *  </p>
+     * 
+     * @param rhs the scalar number
+     * @return the complex number product
+     */
+    public Complex multiply(double rhs) {
+        if (isNaN() || Double.isNaN(rhs)) {
+            return NaN;
+        }
+        if (Double.isInfinite(real) || Double.isInfinite(imaginary) ||
+            Double.isInfinite(rhs)) {
+            // we don't use Complex.isInfinite() to avoid testing for NaN again
+            return INF;
+        }
+        return createComplex(real * rhs, imaginary * rhs);
+    }
+    
+    /**
      * Return the additive inverse of this complex number.
      * <p>
      * Returns <code>Complex.NaN</code> if either real or imaginary

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=791237&r1=791236&r2=791237&view=diff
==============================================================================
--- commons/proper/math/trunk/src/site/xdoc/changes.xml (original)
+++ commons/proper/math/trunk/src/site/xdoc/changes.xml Sun Jul  5 12:53:13 2009
@@ -45,6 +45,9 @@
       <action dev="luc" type="add" issue="MATH-278" due-to="Eugene Kirpichov">
         Added robust locally weighted regression (Loess).
       </action>
+      <action dev="luc" type="add" issue="MATH-277" due-to="Mark Anderson">
+        Added a scalar multiply to the Complex class
+      </action>
       <action dev="luc" type="add" >
         Added curve fitting with a general case and two specific cases (polynomial and harmonic).
       </action>

Modified: commons/proper/math/trunk/src/test/org/apache/commons/math/complex/ComplexTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/org/apache/commons/math/complex/ComplexTest.java?rev=791237&r1=791236&r2=791237&view=diff
==============================================================================
--- commons/proper/math/trunk/src/test/org/apache/commons/math/complex/ComplexTest.java (original)
+++ commons/proper/math/trunk/src/test/org/apache/commons/math/complex/ComplexTest.java Sun
Jul  5 12:53:13 2009
@@ -219,6 +219,31 @@
         assertTrue(Double.isNaN(w.getImaginary()));  
     }
     
+    public void testScalarMultiply() {
+        Complex x = new Complex(3.0, 4.0);
+        double y = 2.0;
+        Complex z = x.multiply(y);
+        assertEquals(6.0, z.getReal(), 1.0e-5);
+        assertEquals(8.0, z.getImaginary(), 1.0e-5);
+    }
+    
+    public void testScalarMultiplyNaN() {
+        Complex x = new Complex(3.0, 4.0);
+        Complex z = x.multiply(Double.NaN);
+        assertTrue(z.isNaN());
+    }
+    
+    public void testScalarMultiplyInf() {
+        Complex z = new Complex(1,1);
+        Complex w = z.multiply(Double.POSITIVE_INFINITY);
+        assertEquals(w.getReal(), inf, 0);
+        assertEquals(w.getImaginary(), inf, 0);
+
+        w = z.multiply(Double.NEGATIVE_INFINITY);
+        assertEquals(w.getReal(), inf, 0);
+        assertEquals(w.getImaginary(), inf, 0);
+    }
+    
     public void testNegate() {
         Complex x = new Complex(3.0, 4.0);
         Complex z = x.negate();



Mime
View raw message