diff --git a/pom.xml b/pom.xml index a633c7c56..7ccf6acfd 100644 --- a/pom.xml +++ b/pom.xml @@ -102,6 +102,9 @@ C. Scott Ananian + + Mark Anderson + Rémi Arntzen diff --git a/src/java/org/apache/commons/math/complex/Complex.java b/src/java/org/apache/commons/math/complex/Complex.java index 2f8a437ce..f12e22b11 100644 --- a/src/java/org/apache/commons/math/complex/Complex.java +++ b/src/java/org/apache/commons/math/complex/Complex.java @@ -386,6 +386,44 @@ public class Complex implements FieldElement, Serializable { real * rhs.imaginary + imaginary * rhs.real); } + /** + * Return the product of this complex number and the given scalar number. + *

+ * Implements preliminary checks for NaN and infinity followed by + * the definitional formula: + *


+     * c(a + bi) = (ca) + (cb)i
+     * 
+ *

+ *

+ * Returns {@link #NaN} if either this or rhs has one or more + * NaN parts. + *

+ * Returns {@link #INF} if neither this nor rhs has one or more + * NaN parts and if either this or rhs has one or more + * infinite parts (same result is returned regardless of the sign of the + * components). + *

+ *

+ * Returns finite values in components of the result per the + * definitional formula in all remaining cases. + *

+ * + * @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. *

diff --git a/src/site/xdoc/changes.xml b/src/site/xdoc/changes.xml index e3802376e..1682ce538 100644 --- a/src/site/xdoc/changes.xml +++ b/src/site/xdoc/changes.xml @@ -45,6 +45,9 @@ The type attribute can be add,update,fix,remove. Added robust locally weighted regression (Loess). + + Added a scalar multiply to the Complex class + Added curve fitting with a general case and two specific cases (polynomial and harmonic). diff --git a/src/test/org/apache/commons/math/complex/ComplexTest.java b/src/test/org/apache/commons/math/complex/ComplexTest.java index 0112f9dcd..97cf65b0a 100644 --- a/src/test/org/apache/commons/math/complex/ComplexTest.java +++ b/src/test/org/apache/commons/math/complex/ComplexTest.java @@ -219,6 +219,31 @@ public class ComplexTest extends TestCase { 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();