001: /*
002: * Copyright 2003-2005 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.apache.commons.math.complex;
018:
019: import junit.framework.TestCase;
020:
021: /**
022: * @version $Revision: 331126 $ $Date: 2005-11-06 11:06:46 -0700 (Sun, 06 Nov 2005) $
023: */
024: public class ComplexTest extends TestCase {
025:
026: private double inf = Double.POSITIVE_INFINITY;
027: private double neginf = Double.NEGATIVE_INFINITY;
028: private double nan = Double.NaN;
029: private Complex oneInf = new Complex(1, inf);
030: private Complex oneNegInf = new Complex(1, neginf);
031: private Complex infOne = new Complex(inf, 1);
032: private Complex negInfInf = new Complex(neginf, inf);
033: private Complex negInfNegInf = new Complex(neginf, neginf);
034: private Complex oneNaN = new Complex(1, nan);
035:
036: public void testConstructor() {
037: Complex z = new Complex(3.0, 4.0);
038: assertEquals(3.0, z.getReal(), 1.0e-5);
039: assertEquals(4.0, z.getImaginary(), 1.0e-5);
040: }
041:
042: public void testConstructorNaN() {
043: Complex z = new Complex(3.0, Double.NaN);
044: assertTrue(z.isNaN());
045:
046: z = new Complex(nan, 4.0);
047: assertTrue(z.isNaN());
048:
049: z = new Complex(3.0, 4.0);
050: assertFalse(z.isNaN());
051: }
052:
053: public void testAbs() {
054: Complex z = new Complex(3.0, 4.0);
055: assertEquals(5.0, z.abs(), 1.0e-5);
056: }
057:
058: public void testAbsNaN() {
059: assertTrue(Double.isNaN(Complex.NaN.abs()));
060: Complex z = new Complex(inf, nan);
061: assertTrue(Double.isNaN(z.abs()));
062: }
063:
064: public void testAbsInfinite() {
065: Complex z = new Complex(inf, 0);
066: assertEquals(inf, z.abs(), 0);
067: z = new Complex(0, neginf);
068: assertEquals(inf, z.abs(), 0);
069: z = new Complex(inf, neginf);
070: assertEquals(inf, z.abs(), 0);
071: }
072:
073: public void testAdd() {
074: Complex x = new Complex(3.0, 4.0);
075: Complex y = new Complex(5.0, 6.0);
076: Complex z = x.add(y);
077: assertEquals(8.0, z.getReal(), 1.0e-5);
078: assertEquals(10.0, z.getImaginary(), 1.0e-5);
079: }
080:
081: public void testAddNaN() {
082: Complex x = new Complex(3.0, 4.0);
083: Complex z = x.add(Complex.NaN);
084: assertTrue(z.isNaN());
085: z = new Complex(1, nan);
086: Complex w = x.add(z);
087: assertEquals(w.real, 4.0, 0);
088: assertTrue(Double.isNaN(w.imaginary));
089: }
090:
091: public void testAddInfinite() {
092: Complex x = new Complex(1, 1);
093: Complex z = new Complex(inf, 0);
094: Complex w = x.add(z);
095: assertEquals(w.imaginary, 1, 0);
096: assertEquals(inf, w.real, 0);
097:
098: x = new Complex(neginf, 0);
099: assertTrue(Double.isNaN(x.add(z).real));
100: }
101:
102: public void testConjugate() {
103: Complex x = new Complex(3.0, 4.0);
104: Complex z = x.conjugate();
105: assertEquals(3.0, z.getReal(), 1.0e-5);
106: assertEquals(-4.0, z.getImaginary(), 1.0e-5);
107: }
108:
109: public void testConjugateNaN() {
110: Complex z = Complex.NaN.conjugate();
111: assertTrue(z.isNaN());
112: }
113:
114: public void testConjugateInfiinite() {
115: Complex z = new Complex(0, inf);
116: assertEquals(neginf, z.conjugate().imaginary, 0);
117: z = new Complex(0, neginf);
118: assertEquals(inf, z.conjugate().imaginary, 0);
119: }
120:
121: public void testDivide() {
122: Complex x = new Complex(3.0, 4.0);
123: Complex y = new Complex(5.0, 6.0);
124: Complex z = x.divide(y);
125: assertEquals(39.0 / 61.0, z.getReal(), 1.0e-5);
126: assertEquals(2.0 / 61.0, z.getImaginary(), 1.0e-5);
127: }
128:
129: public void testDivideInfinite() {
130: Complex x = new Complex(3, 4);
131: Complex w = new Complex(neginf, inf);
132: assertTrue(x.divide(w).equals(Complex.ZERO));
133:
134: Complex z = w.divide(x);
135: assertTrue(Double.isNaN(z.real));
136: assertEquals(inf, z.imaginary, 0);
137:
138: w = new Complex(inf, inf);
139: z = w.divide(x);
140: assertTrue(Double.isNaN(z.imaginary));
141: assertEquals(inf, z.real, 0);
142:
143: w = new Complex(1, inf);
144: z = w.divide(w);
145: assertTrue(Double.isNaN(z.real));
146: assertTrue(Double.isNaN(z.imaginary));
147: }
148:
149: public void testDivideNaN() {
150: Complex x = new Complex(3.0, 4.0);
151: Complex z = x.divide(Complex.NaN);
152: assertTrue(z.isNaN());
153: }
154:
155: public void testDivideNaNInf() {
156: Complex z = oneInf.divide(Complex.ONE);
157: assertTrue(Double.isNaN(z.real));
158: assertEquals(inf, z.imaginary, 0);
159:
160: z = negInfNegInf.divide(oneNaN);
161: assertTrue(Double.isNaN(z.real));
162: assertTrue(Double.isNaN(z.imaginary));
163:
164: z = negInfInf.divide(Complex.ONE);
165: assertTrue(Double.isNaN(z.real));
166: assertTrue(Double.isNaN(z.imaginary));
167: }
168:
169: public void testMultiply() {
170: Complex x = new Complex(3.0, 4.0);
171: Complex y = new Complex(5.0, 6.0);
172: Complex z = x.multiply(y);
173: assertEquals(-9.0, z.getReal(), 1.0e-5);
174: assertEquals(38.0, z.getImaginary(), 1.0e-5);
175: }
176:
177: public void testMultiplyNaN() {
178: Complex x = new Complex(3.0, 4.0);
179: Complex z = x.multiply(Complex.NaN);
180: assertTrue(z.isNaN());
181: }
182:
183: public void testMultiplyNaNInf() {
184: Complex z = new Complex(1, 1);
185: Complex w = z.multiply(infOne);
186: assertEquals(w.real, inf, 0);
187: assertEquals(w.imaginary, inf, 0);
188:
189: w = oneInf.multiply(oneNegInf);
190: assertEquals(w.real, inf, 0);
191: assertTrue(Double.isNaN(w.imaginary));
192:
193: w = negInfNegInf.multiply(oneNaN);
194: assertTrue(Double.isNaN(w.real));
195: assertTrue(Double.isNaN(w.imaginary));
196: }
197:
198: public void testNegate() {
199: Complex x = new Complex(3.0, 4.0);
200: Complex z = x.negate();
201: assertEquals(-3.0, z.getReal(), 1.0e-5);
202: assertEquals(-4.0, z.getImaginary(), 1.0e-5);
203: }
204:
205: public void testNegateNaN() {
206: Complex z = Complex.NaN.negate();
207: assertTrue(z.isNaN());
208: }
209:
210: public void testSubtract() {
211: Complex x = new Complex(3.0, 4.0);
212: Complex y = new Complex(5.0, 6.0);
213: Complex z = x.subtract(y);
214: assertEquals(-2.0, z.getReal(), 1.0e-5);
215: assertEquals(-2.0, z.getImaginary(), 1.0e-5);
216: }
217:
218: public void testSubtractNaN() {
219: Complex x = new Complex(3.0, 4.0);
220: Complex z = x.subtract(Complex.NaN);
221: assertTrue(z.isNaN());
222: }
223:
224: public void testEqualsNull() {
225: Complex x = new Complex(3.0, 4.0);
226: assertFalse(x.equals(null));
227: }
228:
229: public void testEqualsClass() {
230: Complex x = new Complex(3.0, 4.0);
231: assertFalse(x.equals(this ));
232: }
233:
234: public void testEqualsSame() {
235: Complex x = new Complex(3.0, 4.0);
236: assertTrue(x.equals(x));
237: }
238:
239: public void testEqualsTrue() {
240: Complex x = new Complex(3.0, 4.0);
241: Complex y = new Complex(3.0, 4.0);
242: assertTrue(x.equals(y));
243: }
244:
245: public void testEqualsRealDifference() {
246: Complex x = new Complex(0.0, 0.0);
247: Complex y = new Complex(0.0 + Double.MIN_VALUE, 0.0);
248: assertFalse(x.equals(y));
249: }
250:
251: public void testEqualsImaginaryDifference() {
252: Complex x = new Complex(0.0, 0.0);
253: Complex y = new Complex(0.0, 0.0 + Double.MIN_VALUE);
254: assertFalse(x.equals(y));
255: }
256:
257: public void testEqualsNaN() {
258: Complex realNaN = new Complex(Double.NaN, 0.0);
259: Complex imaginaryNaN = new Complex(0.0, Double.NaN);
260: Complex complexNaN = Complex.NaN;
261: assertTrue(realNaN.equals(imaginaryNaN));
262: assertTrue(imaginaryNaN.equals(complexNaN));
263: assertTrue(realNaN.equals(complexNaN));
264: }
265:
266: public void testHashCode() {
267: Complex x = new Complex(0.0, 0.0);
268: Complex y = new Complex(0.0, 0.0 + Double.MIN_VALUE);
269: assertFalse(x.hashCode() == y.hashCode());
270: y = new Complex(0.0 + Double.MIN_VALUE, 0.0);
271: assertFalse(x.hashCode() == y.hashCode());
272: Complex realNaN = new Complex(Double.NaN, 0.0);
273: Complex imaginaryNaN = new Complex(0.0, Double.NaN);
274: assertEquals(realNaN.hashCode(), imaginaryNaN.hashCode());
275: assertEquals(imaginaryNaN.hashCode(), Complex.NaN.hashCode());
276: }
277: }
|