added scalar multiply to the Complex class

JIRA: MATH-277

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/trunk@791237 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Luc Maisonobe 2009-07-05 12:53:13 +00:00
parent e4a5900147
commit 720a0b2626
4 changed files with 69 additions and 0 deletions

View File

@ -102,6 +102,9 @@
<contributor>
<name>C. Scott Ananian</name>
</contributor>
<contributor>
<name>Mark Anderson</name>
</contributor>
<contributor>
<name>R&#233;mi Arntzen</name>
</contributor>

View File

@ -386,6 +386,44 @@ public class Complex implements FieldElement<Complex>, Serializable {
real * rhs.imaginary + imaginary * rhs.real);
}
/**
* 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>

View File

@ -45,6 +45,9 @@ The <action> type attribute can be add,update,fix,remove.
<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>

View File

@ -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();