Author: luc
Date: Tue Jan 8 23:30:13 2008
New Revision: 610288
URL: http://svn.apache.org/viewvc?rev=610288&view=rev
Log:
MATH-164 add a special handling for multiplication of complex
numbers with infinite parts
Modified:
commons/proper/math/trunk/src/java/org/apache/commons/math/complex/Complex.java
commons/proper/math/trunk/src/test/org/apache/commons/math/complex/ComplexTest.java
commons/proper/math/trunk/xdocs/changes.xml
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=610288&r1=610287&r2=610288&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
Tue Jan 8 23:30:13 2008
@@ -48,6 +48,9 @@
/** 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 @@
/**
* 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 [EMAIL PROTECTED] #NaN} if either this or <code>rhs</code> has
one or more
* NaN parts.
- * <p>
- * Returns NaN or infinite values in components of the result per the
- * definitional formula and and the rules for [EMAIL PROTECTED]
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>
+ * </p>
+ * Returns [EMAIL PROTECTED] #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 other complex number
* @return the complex number product
@@ -348,6 +354,11 @@
public Complex multiply(Complex rhs) {
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);
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=610288&r1=610287&r2=610288&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
Tue Jan 8 23:30:13 2008
@@ -201,10 +201,15 @@
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));
Modified: commons/proper/math/trunk/xdocs/changes.xml
URL:
http://svn.apache.org/viewvc/commons/proper/math/trunk/xdocs/changes.xml?rev=610288&r1=610287&r2=610288&view=diff
==============================================================================
--- commons/proper/math/trunk/xdocs/changes.xml (original)
+++ commons/proper/math/trunk/xdocs/changes.xml Tue Jan 8 23:30:13 2008
@@ -121,6 +121,9 @@
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.