MATH-164 add a special handling for multiplication of complex

numbers with infinite parts

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/trunk@610288 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Luc Maisonobe 2008-01-09 07:30:13 +00:00
parent a3dc59a94d
commit 7c7b56e9f3
3 changed files with 29 additions and 10 deletions

View File

@ -48,6 +48,9 @@ public class Complex implements Serializable {
/** A complex number representing "NaN + NaNi" */
public static final Complex NaN = new Complex(Double.NaN, Double.NaN);
/** A complex number representing "+INF + INFi" */
public static final Complex INF = new Complex(Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY);
/** A complex number representing "1.0 + 0.0i" */
public static final Complex ONE = new Complex(1.0, 0.0);
@ -324,22 +327,25 @@ public class Complex implements Serializable {
/**
* Return the product of this complex number and the given complex number.
* <p>
* Implements the definitional formula:
* Implements preliminary checks for NaN and infinity followed by
* the definitional formula:
* <pre><code>
* (a + bi)(c + di) = (ac - bd) + (ad + bc)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 NaN or infinite values in components of the result per the
* definitional formula and and the rules for {@link java.lang.Double}
* arithmetic. Examples:
* <pre><code>
* (1 + i) (INF + i) = INF + INFi
* (1 + INFi) (1 - INFi) = INF + NaNi
* (-INF + -INFi)(1 + NaNi) = NaN + NaNi
* </code></pre>
* Returns finite values in components of the result per the
* definitional formula in all remaining cases.
* </p>
*
* @param rhs the other complex number
* @return the complex number product
@ -349,6 +355,11 @@ public class Complex implements Serializable {
if (isNaN() || rhs.isNaN()) {
return NaN;
}
if (Double.isInfinite(real) || Double.isInfinite(imaginary) ||
Double.isInfinite(rhs.real)|| Double.isInfinite(rhs.imaginary)) {
// we don't use Complex.isInfinite() to avoid testing for NaN again
return INF;
}
return createComplex(real * rhs.real - imaginary * rhs.imaginary,
real * rhs.imaginary + imaginary * rhs.real);
}

View File

@ -201,10 +201,15 @@ public class ComplexTest extends TestCase {
Complex w = z.multiply(infOne);
assertEquals(w.real, inf, 0);
assertEquals(w.imaginary, inf, 0);
// [MATH-164]
assertTrue(new Complex( 1,0).multiply(infInf).equals(Complex.INF));
assertTrue(new Complex(-1,0).multiply(infInf).equals(Complex.INF));
assertTrue(new Complex( 1,0).multiply(negInfZero).equals(Complex.INF));
w = oneInf.multiply(oneNegInf);
assertEquals(w.real, inf, 0);
assertTrue(Double.isNaN(w.imaginary));
assertEquals(w.imaginary, inf, 0);
w = negInfNegInf.multiply(oneNaN);
assertTrue(Double.isNaN(w.real));

View File

@ -121,6 +121,9 @@ Commons Math Release Notes</title>
Added check and rescaling of expected counts to sum to sum of expected
counts if necessary in ChiSquare test.
</action>
<action dev="luc" type="fix" issue="MATH-164">
Handle multiplication of Complex numbers with infinite parts specially.
</action>
</release>
<release version="1.1" date="2005-12-17"
description="This is a maintenance release containing bug fixes and enhancements.