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 org.apache.commons.math.TestUtils;
020:
021: import junit.framework.TestCase;
022:
023: /**
024: * @version $Revision: 349309 $ $Date: 2005-11-27 13:55:08 -0700 (Sun, 27 Nov 2005) $
025: */
026: public class ComplexUtilsTest extends TestCase {
027:
028: private double inf = Double.POSITIVE_INFINITY;
029: private double negInf = Double.NEGATIVE_INFINITY;
030: private double nan = Double.NaN;
031: private double pi = Math.PI;
032:
033: private Complex oneInf = new Complex(1, inf);
034: private Complex oneNegInf = new Complex(1, negInf);
035: private Complex infOne = new Complex(inf, 1);
036: private Complex negInfOne = new Complex(negInf, 1);
037: private Complex negInfInf = new Complex(negInf, inf);
038: private Complex infNegInf = new Complex(inf, negInf);
039: private Complex infInf = new Complex(inf, inf);
040: private Complex negInfNegInf = new Complex(negInf, negInf);
041: private Complex oneNaN = new Complex(1, nan);
042: private Complex infNaN = new Complex(inf, nan);
043: private Complex negInfNaN = new Complex(negInf, nan);
044: private Complex nanInf = new Complex(nan, inf);
045: private Complex nanNegInf = new Complex(nan, negInf);
046: private Complex zeroNaN = new Complex(0, nan);
047: private Complex nanZero = new Complex(nan, 0);
048: private Complex infZero = new Complex(inf, 0);
049: private Complex zeroInf = new Complex(0, inf);
050: private Complex zeroNegInf = new Complex(0, negInf);
051: private Complex negInfZero = new Complex(negInf, 0);
052:
053: private ComplexFormat fmt = new ComplexFormat();
054:
055: public void testAcos() {
056: Complex z = new Complex(3, 4);
057: Complex expected = new Complex(0.936812, -2.30551);
058: TestUtils.assertEquals(expected, ComplexUtils.acos(z), 1.0e-5);
059: TestUtils.assertEquals(new Complex(Math.acos(0), 0),
060: ComplexUtils.acos(Complex.ZERO), 1.0e-12);
061: }
062:
063: public void testAcosInf() {
064: TestUtils.assertSame(Complex.NaN, ComplexUtils.acos(oneInf));
065: TestUtils.assertSame(Complex.NaN, ComplexUtils.acos(oneNegInf));
066: TestUtils.assertSame(Complex.NaN, ComplexUtils.acos(infOne));
067: TestUtils.assertSame(Complex.NaN, ComplexUtils.acos(negInfOne));
068: TestUtils.assertSame(Complex.NaN, ComplexUtils.acos(infInf));
069: TestUtils.assertSame(Complex.NaN, ComplexUtils.acos(infNegInf));
070: TestUtils.assertSame(Complex.NaN, ComplexUtils.acos(negInfInf));
071: TestUtils.assertSame(Complex.NaN, ComplexUtils
072: .acos(negInfNegInf));
073: }
074:
075: public void testAcosNaN() {
076: assertTrue(ComplexUtils.acos(Complex.NaN).isNaN());
077: }
078:
079: public void testAcosNull() {
080: try {
081: Complex z = ComplexUtils.acos(null);
082: fail("Expecting NullPointerException");
083: } catch (NullPointerException ex) {
084: // expected
085: }
086: }
087:
088: public void testAsin() {
089: Complex z = new Complex(3, 4);
090: Complex expected = new Complex(0.633984, 2.30551);
091: TestUtils.assertEquals(expected, ComplexUtils.asin(z), 1.0e-5);
092: }
093:
094: public void testAsinNaN() {
095: assertTrue(ComplexUtils.asin(Complex.NaN).isNaN());
096: }
097:
098: public void testAsinInf() {
099: TestUtils.assertSame(Complex.NaN, ComplexUtils.asin(oneInf));
100: TestUtils.assertSame(Complex.NaN, ComplexUtils.asin(oneNegInf));
101: TestUtils.assertSame(Complex.NaN, ComplexUtils.asin(infOne));
102: TestUtils.assertSame(Complex.NaN, ComplexUtils.asin(negInfOne));
103: TestUtils.assertSame(Complex.NaN, ComplexUtils.asin(infInf));
104: TestUtils.assertSame(Complex.NaN, ComplexUtils.asin(infNegInf));
105: TestUtils.assertSame(Complex.NaN, ComplexUtils.asin(negInfInf));
106: TestUtils.assertSame(Complex.NaN, ComplexUtils
107: .asin(negInfNegInf));
108: }
109:
110: public void testAsinNull() {
111: try {
112: Complex z = ComplexUtils.asin(null);
113: fail("Expecting NullPointerException");
114: } catch (NullPointerException ex) {
115: // expected
116: }
117: }
118:
119: public void testAtan() {
120: Complex z = new Complex(3, 4);
121: Complex expected = new Complex(1.44831, 0.158997);
122: TestUtils.assertEquals(expected, ComplexUtils.atan(z), 1.0e-5);
123: }
124:
125: public void testAtanInf() {
126: TestUtils.assertSame(Complex.NaN, ComplexUtils.atan(oneInf));
127: TestUtils.assertSame(Complex.NaN, ComplexUtils.atan(oneNegInf));
128: TestUtils.assertSame(Complex.NaN, ComplexUtils.atan(infOne));
129: TestUtils.assertSame(Complex.NaN, ComplexUtils.atan(negInfOne));
130: TestUtils.assertSame(Complex.NaN, ComplexUtils.atan(infInf));
131: TestUtils.assertSame(Complex.NaN, ComplexUtils.atan(infNegInf));
132: TestUtils.assertSame(Complex.NaN, ComplexUtils.atan(negInfInf));
133: TestUtils.assertSame(Complex.NaN, ComplexUtils
134: .atan(negInfNegInf));
135: }
136:
137: public void testAtanNaN() {
138: assertTrue(ComplexUtils.atan(Complex.NaN).isNaN());
139: assertTrue(ComplexUtils.atan(Complex.I).isNaN());
140: }
141:
142: public void testAtanNull() {
143: try {
144: Complex z = ComplexUtils.atan(null);
145: fail("Expecting NullPointerException");
146: } catch (NullPointerException ex) {
147: // expected
148: }
149: }
150:
151: public void testCos() {
152: Complex z = new Complex(3, 4);
153: Complex expected = new Complex(-27.03495, -3.851153);
154: TestUtils.assertEquals(expected, ComplexUtils.cos(z), 1.0e-5);
155: }
156:
157: public void testCosNaN() {
158: assertTrue(ComplexUtils.cos(Complex.NaN).isNaN());
159: }
160:
161: public void testCosInf() {
162: TestUtils.assertSame(infNegInf, ComplexUtils.cos(oneInf));
163: TestUtils.assertSame(infInf, ComplexUtils.cos(oneNegInf));
164: TestUtils.assertSame(Complex.NaN, ComplexUtils.cos(infOne));
165: TestUtils.assertSame(Complex.NaN, ComplexUtils.cos(negInfOne));
166: TestUtils.assertSame(Complex.NaN, ComplexUtils.cos(infInf));
167: TestUtils.assertSame(Complex.NaN, ComplexUtils.cos(infNegInf));
168: TestUtils.assertSame(Complex.NaN, ComplexUtils.cos(negInfInf));
169: TestUtils.assertSame(Complex.NaN, ComplexUtils
170: .cos(negInfNegInf));
171: }
172:
173: public void testCosNull() {
174: try {
175: Complex z = ComplexUtils.cos(null);
176: fail("Expecting NullPointerException");
177: } catch (NullPointerException ex) {
178: // expected
179: }
180: }
181:
182: public void testCosh() {
183: Complex z = new Complex(3, 4);
184: Complex expected = new Complex(-6.58066, -7.58155);
185: TestUtils.assertEquals(expected, ComplexUtils.cosh(z), 1.0e-5);
186: }
187:
188: public void testCoshNaN() {
189: assertTrue(ComplexUtils.cosh(Complex.NaN).isNaN());
190: }
191:
192: public void testCoshInf() {
193: TestUtils.assertSame(Complex.NaN, ComplexUtils.cosh(oneInf));
194: TestUtils.assertSame(Complex.NaN, ComplexUtils.cosh(oneNegInf));
195: TestUtils.assertSame(infInf, ComplexUtils.cosh(infOne));
196: TestUtils.assertSame(infNegInf, ComplexUtils.cosh(negInfOne));
197: TestUtils.assertSame(Complex.NaN, ComplexUtils.cosh(infInf));
198: TestUtils.assertSame(Complex.NaN, ComplexUtils.cosh(infNegInf));
199: TestUtils.assertSame(Complex.NaN, ComplexUtils.cosh(negInfInf));
200: TestUtils.assertSame(Complex.NaN, ComplexUtils
201: .cosh(negInfNegInf));
202: }
203:
204: public void testCoshNull() {
205: try {
206: Complex z = ComplexUtils.cosh(null);
207: fail("Expecting NullPointerException");
208: } catch (NullPointerException ex) {
209: // expected
210: }
211: }
212:
213: public void testExp() {
214: Complex z = new Complex(3, 4);
215: Complex expected = new Complex(-13.12878, -15.20078);
216: TestUtils.assertEquals(expected, ComplexUtils.exp(z), 1.0e-5);
217: TestUtils.assertEquals(Complex.ONE, ComplexUtils
218: .exp(Complex.ZERO), 10e-12);
219: Complex iPi = Complex.I.multiply(new Complex(pi, 0));
220: TestUtils.assertEquals(Complex.ONE.negate(), ComplexUtils
221: .exp(iPi), 10e-12);
222: }
223:
224: public void testExpNaN() {
225: assertTrue(ComplexUtils.exp(Complex.NaN).isNaN());
226: }
227:
228: public void testExpInf() {
229: TestUtils.assertSame(Complex.NaN, ComplexUtils.exp(oneInf));
230: TestUtils.assertSame(Complex.NaN, ComplexUtils.exp(oneNegInf));
231: TestUtils.assertSame(infInf, ComplexUtils.exp(infOne));
232: TestUtils.assertSame(Complex.ZERO, ComplexUtils.exp(negInfOne));
233: TestUtils.assertSame(Complex.NaN, ComplexUtils.exp(infInf));
234: TestUtils.assertSame(Complex.NaN, ComplexUtils.exp(infNegInf));
235: TestUtils.assertSame(Complex.NaN, ComplexUtils.exp(negInfInf));
236: TestUtils.assertSame(Complex.NaN, ComplexUtils
237: .exp(negInfNegInf));
238: }
239:
240: public void testExpNull() {
241: try {
242: Complex z = ComplexUtils.exp(null);
243: fail("Expecting NullPointerException");
244: } catch (NullPointerException ex) {
245: // expected
246: }
247: }
248:
249: public void testLog() {
250: Complex z = new Complex(3, 4);
251: Complex expected = new Complex(1.60944, 0.927295);
252: TestUtils.assertEquals(expected, ComplexUtils.log(z), 1.0e-5);
253: }
254:
255: public void testLogNaN() {
256: assertTrue(ComplexUtils.log(Complex.NaN).isNaN());
257: }
258:
259: public void testLogInf() {
260: TestUtils.assertEquals(new Complex(inf, pi / 2), ComplexUtils
261: .log(oneInf), 10e-12);
262: TestUtils.assertEquals(new Complex(inf, -pi / 2), ComplexUtils
263: .log(oneNegInf), 10e-12);
264: TestUtils.assertEquals(infZero, ComplexUtils.log(infOne),
265: 10e-12);
266: TestUtils.assertEquals(new Complex(inf, pi), ComplexUtils
267: .log(negInfOne), 10e-12);
268: TestUtils.assertEquals(new Complex(inf, pi / 4), ComplexUtils
269: .log(infInf), 10e-12);
270: TestUtils.assertEquals(new Complex(inf, -pi / 4), ComplexUtils
271: .log(infNegInf), 10e-12);
272: TestUtils.assertEquals(new Complex(inf, 3d * pi / 4),
273: ComplexUtils.log(negInfInf), 10e-12);
274: TestUtils.assertEquals(new Complex(inf, -3d * pi / 4),
275: ComplexUtils.log(negInfNegInf), 10e-12);
276: }
277:
278: public void testLogZero() {
279: TestUtils
280: .assertSame(negInfZero, ComplexUtils.log(Complex.ZERO));
281: }
282:
283: public void testlogNull() {
284: try {
285: Complex z = ComplexUtils.log(null);
286: fail("Expecting NullPointerException");
287: } catch (NullPointerException ex) {
288: // expected
289: }
290: }
291:
292: public void testPolar2Complex() {
293: TestUtils.assertEquals(Complex.ONE, ComplexUtils.polar2Complex(
294: 1, 0), 10e-12);
295: TestUtils.assertEquals(Complex.ZERO, ComplexUtils
296: .polar2Complex(0, 1), 10e-12);
297: TestUtils.assertEquals(Complex.ZERO, ComplexUtils
298: .polar2Complex(0, -1), 10e-12);
299: TestUtils.assertEquals(Complex.I, ComplexUtils.polar2Complex(1,
300: pi / 2), 10e-12);
301: TestUtils.assertEquals(Complex.I.negate(), ComplexUtils
302: .polar2Complex(1, -pi / 2), 10e-12);
303: double r = 0;
304: for (int i = 0; i < 5; i++) {
305: r += i;
306: double theta = 0;
307: for (int j = 0; j < 20; j++) {
308: theta += pi / 6;
309: TestUtils.assertEquals(altPolar(r, theta), ComplexUtils
310: .polar2Complex(r, theta), 10e-12);
311: }
312: theta = -2 * pi;
313: for (int j = 0; j < 20; j++) {
314: theta -= pi / 6;
315: TestUtils.assertEquals(altPolar(r, theta), ComplexUtils
316: .polar2Complex(r, theta), 10e-12);
317: }
318: }
319: }
320:
321: protected Complex altPolar(double r, double theta) {
322: return ComplexUtils.exp(
323: Complex.I.multiply(new Complex(theta, 0))).multiply(
324: new Complex(r, 0));
325: }
326:
327: public void testPolar2ComplexIllegalModulus() {
328: try {
329: Complex z = ComplexUtils.polar2Complex(-1, 0);
330: fail("Expecting IllegalArgumentException");
331: } catch (IllegalArgumentException ex) {
332: // expected
333: }
334: }
335:
336: public void testPolar2ComplexNaN() {
337: TestUtils.assertSame(Complex.NaN, ComplexUtils.polar2Complex(
338: nan, 1));
339: TestUtils.assertSame(Complex.NaN, ComplexUtils.polar2Complex(1,
340: nan));
341: TestUtils.assertSame(Complex.NaN, ComplexUtils.polar2Complex(
342: nan, nan));
343: }
344:
345: public void testPolar2ComplexInf() {
346: TestUtils.assertSame(Complex.NaN, ComplexUtils.polar2Complex(1,
347: inf));
348: TestUtils.assertSame(Complex.NaN, ComplexUtils.polar2Complex(1,
349: negInf));
350: TestUtils.assertSame(Complex.NaN, ComplexUtils.polar2Complex(
351: inf, inf));
352: TestUtils.assertSame(Complex.NaN, ComplexUtils.polar2Complex(
353: inf, negInf));
354: TestUtils.assertSame(infInf, ComplexUtils.polar2Complex(inf,
355: pi / 4));
356: TestUtils
357: .assertSame(infNaN, ComplexUtils.polar2Complex(inf, 0));
358: TestUtils.assertSame(infNegInf, ComplexUtils.polar2Complex(inf,
359: -pi / 4));
360: TestUtils.assertSame(negInfInf, ComplexUtils.polar2Complex(inf,
361: 3 * pi / 4));
362: TestUtils.assertSame(negInfNegInf, ComplexUtils.polar2Complex(
363: inf, 5 * pi / 4));
364: }
365:
366: public void testPow() {
367: Complex x = new Complex(3, 4);
368: Complex y = new Complex(5, 6);
369: Complex expected = new Complex(-1.860893, 11.83677);
370: TestUtils
371: .assertEquals(expected, ComplexUtils.pow(x, y), 1.0e-5);
372: }
373:
374: public void testPowNaNBase() {
375: Complex x = new Complex(3, 4);
376: assertTrue(ComplexUtils.pow(Complex.NaN, x).isNaN());
377: }
378:
379: public void testPowNaNExponent() {
380: Complex x = new Complex(3, 4);
381: assertTrue(ComplexUtils.pow(x, Complex.NaN).isNaN());
382: }
383:
384: public void testPowInf() {
385: TestUtils.assertSame(Complex.NaN, ComplexUtils.pow(Complex.ONE,
386: oneInf));
387: TestUtils.assertSame(Complex.NaN, ComplexUtils.pow(Complex.ONE,
388: oneNegInf));
389: TestUtils.assertSame(Complex.NaN, ComplexUtils.pow(Complex.ONE,
390: infOne));
391: TestUtils.assertSame(Complex.NaN, ComplexUtils.pow(Complex.ONE,
392: infInf));
393: TestUtils.assertSame(Complex.NaN, ComplexUtils.pow(Complex.ONE,
394: infNegInf));
395: TestUtils.assertSame(Complex.NaN, ComplexUtils.pow(Complex.ONE,
396: negInfInf));
397: TestUtils.assertSame(Complex.NaN, ComplexUtils.pow(Complex.ONE,
398: negInfNegInf));
399: TestUtils.assertSame(Complex.NaN, ComplexUtils.pow(infOne,
400: Complex.ONE));
401: TestUtils.assertSame(Complex.NaN, ComplexUtils.pow(negInfOne,
402: Complex.ONE));
403: TestUtils.assertSame(Complex.NaN, ComplexUtils.pow(infInf,
404: Complex.ONE));
405: TestUtils.assertSame(Complex.NaN, ComplexUtils.pow(infNegInf,
406: Complex.ONE));
407: TestUtils.assertSame(Complex.NaN, ComplexUtils.pow(negInfInf,
408: Complex.ONE));
409: TestUtils.assertSame(Complex.NaN, ComplexUtils.pow(
410: negInfNegInf, Complex.ONE));
411: TestUtils.assertSame(Complex.NaN, ComplexUtils.pow(
412: negInfNegInf, infNegInf));
413: TestUtils.assertSame(Complex.NaN, ComplexUtils.pow(
414: negInfNegInf, negInfNegInf));
415: TestUtils.assertSame(Complex.NaN, ComplexUtils.pow(
416: negInfNegInf, infInf));
417: TestUtils.assertSame(Complex.NaN, ComplexUtils.pow(infInf,
418: infNegInf));
419: TestUtils.assertSame(Complex.NaN, ComplexUtils.pow(infInf,
420: negInfNegInf));
421: TestUtils.assertSame(Complex.NaN, ComplexUtils.pow(infInf,
422: infInf));
423: TestUtils.assertSame(Complex.NaN, ComplexUtils.pow(infNegInf,
424: infNegInf));
425: TestUtils.assertSame(Complex.NaN, ComplexUtils.pow(infNegInf,
426: negInfNegInf));
427: TestUtils.assertSame(Complex.NaN, ComplexUtils.pow(infNegInf,
428: infInf));
429: }
430:
431: public void testPowZero() {
432: TestUtils.assertSame(Complex.NaN, ComplexUtils.pow(
433: Complex.ZERO, Complex.ONE));
434: TestUtils.assertSame(Complex.NaN, ComplexUtils.pow(
435: Complex.ZERO, Complex.ZERO));
436: TestUtils.assertSame(Complex.NaN, ComplexUtils.pow(
437: Complex.ZERO, Complex.I));
438: TestUtils.assertEquals(Complex.ONE, ComplexUtils.pow(
439: Complex.ONE, Complex.ZERO), 10e-12);
440: TestUtils.assertEquals(Complex.ONE, ComplexUtils.pow(Complex.I,
441: Complex.ZERO), 10e-12);
442: TestUtils.assertEquals(Complex.ONE, ComplexUtils.pow(
443: new Complex(-1, 3), Complex.ZERO), 10e-12);
444: }
445:
446: public void testpowNull() {
447: try {
448: Complex z = ComplexUtils.pow(null, Complex.ONE);
449: fail("Expecting NullPointerException");
450: } catch (NullPointerException ex) {
451: // expected
452: }
453: try {
454: Complex z = ComplexUtils.pow(Complex.ONE, null);
455: fail("Expecting NullPointerException");
456: } catch (NullPointerException ex) {
457: // expected
458: }
459: }
460:
461: public void testSin() {
462: Complex z = new Complex(3, 4);
463: Complex expected = new Complex(3.853738, -27.01681);
464: TestUtils.assertEquals(expected, ComplexUtils.sin(z), 1.0e-5);
465: }
466:
467: public void testSinInf() {
468: TestUtils.assertSame(infInf, ComplexUtils.sin(oneInf));
469: TestUtils.assertSame(infNegInf, ComplexUtils.sin(oneNegInf));
470: TestUtils.assertSame(Complex.NaN, ComplexUtils.sin(infOne));
471: TestUtils.assertSame(Complex.NaN, ComplexUtils.sin(negInfOne));
472: TestUtils.assertSame(Complex.NaN, ComplexUtils.sin(infInf));
473: TestUtils.assertSame(Complex.NaN, ComplexUtils.sin(infNegInf));
474: TestUtils.assertSame(Complex.NaN, ComplexUtils.sin(negInfInf));
475: TestUtils.assertSame(Complex.NaN, ComplexUtils
476: .sin(negInfNegInf));
477: }
478:
479: public void testSinNaN() {
480: assertTrue(ComplexUtils.sin(Complex.NaN).isNaN());
481: }
482:
483: public void testSinNull() {
484: try {
485: Complex z = ComplexUtils.sin(null);
486: fail("Expecting NullPointerException");
487: } catch (NullPointerException ex) {
488: // expected
489: }
490: }
491:
492: public void testSinh() {
493: Complex z = new Complex(3, 4);
494: Complex expected = new Complex(-6.54812, -7.61923);
495: TestUtils.assertEquals(expected, ComplexUtils.sinh(z), 1.0e-5);
496: }
497:
498: public void testSinhNaN() {
499: assertTrue(ComplexUtils.sinh(Complex.NaN).isNaN());
500: }
501:
502: public void testSinhInf() {
503: TestUtils.assertSame(Complex.NaN, ComplexUtils.sinh(oneInf));
504: TestUtils.assertSame(Complex.NaN, ComplexUtils.sinh(oneNegInf));
505: TestUtils.assertSame(infInf, ComplexUtils.sinh(infOne));
506: TestUtils.assertSame(negInfInf, ComplexUtils.sinh(negInfOne));
507: TestUtils.assertSame(Complex.NaN, ComplexUtils.sinh(infInf));
508: TestUtils.assertSame(Complex.NaN, ComplexUtils.sinh(infNegInf));
509: TestUtils.assertSame(Complex.NaN, ComplexUtils.sinh(negInfInf));
510: TestUtils.assertSame(Complex.NaN, ComplexUtils
511: .sinh(negInfNegInf));
512: }
513:
514: public void testsinhNull() {
515: try {
516: Complex z = ComplexUtils.sinh(null);
517: fail("Expecting NullPointerException");
518: } catch (NullPointerException ex) {
519: // expected
520: }
521: }
522:
523: public void testSqrtRealPositive() {
524: Complex z = new Complex(3, 4);
525: Complex expected = new Complex(2, 1);
526: TestUtils.assertEquals(expected, ComplexUtils.sqrt(z), 1.0e-5);
527: }
528:
529: public void testSqrtRealZero() {
530: Complex z = new Complex(0.0, 4);
531: Complex expected = new Complex(1.41421, 1.41421);
532: TestUtils.assertEquals(expected, ComplexUtils.sqrt(z), 1.0e-5);
533: }
534:
535: public void testSqrtRealNegative() {
536: Complex z = new Complex(-3.0, 4);
537: Complex expected = new Complex(1, 2);
538: TestUtils.assertEquals(expected, ComplexUtils.sqrt(z), 1.0e-5);
539: }
540:
541: public void testSqrtImaginaryZero() {
542: Complex z = new Complex(-3.0, 0.0);
543: Complex expected = new Complex(0.0, 1.73205);
544: TestUtils.assertEquals(expected, ComplexUtils.sqrt(z), 1.0e-5);
545: }
546:
547: public void testSqrtImaginaryNegative() {
548: Complex z = new Complex(-3.0, -4.0);
549: Complex expected = new Complex(1.0, -2.0);
550: TestUtils.assertEquals(expected, ComplexUtils.sqrt(z), 1.0e-5);
551: }
552:
553: public void testSqrtPolar() {
554: double r = 1;
555: for (int i = 0; i < 5; i++) {
556: r += i;
557: double theta = 0;
558: for (int j = 0; j < 11; j++) {
559: theta += pi / 12;
560: Complex z = ComplexUtils.polar2Complex(r, theta);
561: Complex sqrtz = ComplexUtils.polar2Complex(
562: Math.sqrt(r), theta / 2);
563: TestUtils.assertEquals(sqrtz, ComplexUtils.sqrt(z),
564: 10e-12);
565: }
566: }
567: }
568:
569: public void testSqrtNaN() {
570: assertTrue(ComplexUtils.sqrt(Complex.NaN).isNaN());
571: }
572:
573: public void testSqrtInf() {
574: TestUtils.assertSame(infNaN, ComplexUtils.sqrt(oneInf));
575: TestUtils.assertSame(infNaN, ComplexUtils.sqrt(oneNegInf));
576: TestUtils.assertSame(infZero, ComplexUtils.sqrt(infOne));
577: TestUtils.assertSame(zeroInf, ComplexUtils.sqrt(negInfOne));
578: TestUtils.assertSame(infNaN, ComplexUtils.sqrt(infInf));
579: TestUtils.assertSame(infNaN, ComplexUtils.sqrt(infNegInf));
580: TestUtils.assertSame(nanInf, ComplexUtils.sqrt(negInfInf));
581: TestUtils
582: .assertSame(nanNegInf, ComplexUtils.sqrt(negInfNegInf));
583: }
584:
585: public void testSqrtNull() {
586: try {
587: Complex z = ComplexUtils.sqrt(null);
588: fail("Expecting NullPointerException");
589: } catch (NullPointerException ex) {
590: // expected
591: }
592: }
593:
594: public void testSqrt1z() {
595: Complex z = new Complex(3, 4);
596: Complex expected = new Complex(4.08033, -2.94094);
597: TestUtils
598: .assertEquals(expected, ComplexUtils.sqrt1z(z), 1.0e-5);
599: }
600:
601: public void testSqrt1zNaN() {
602: assertTrue(ComplexUtils.sqrt1z(Complex.NaN).isNaN());
603: }
604:
605: public void testSqrt1zNull() {
606: try {
607: Complex z = ComplexUtils.sqrt1z(null);
608: fail("Expecting NullPointerException");
609: } catch (NullPointerException ex) {
610: // expected
611: }
612: }
613:
614: public void testTan() {
615: Complex z = new Complex(3, 4);
616: Complex expected = new Complex(-0.000187346, 0.999356);
617: TestUtils.assertEquals(expected, ComplexUtils.tan(z), 1.0e-5);
618: }
619:
620: public void testTanNaN() {
621: assertTrue(ComplexUtils.tan(Complex.NaN).isNaN());
622: }
623:
624: public void testTanInf() {
625: TestUtils.assertSame(zeroNaN, ComplexUtils.tan(oneInf));
626: TestUtils.assertSame(zeroNaN, ComplexUtils.tan(oneNegInf));
627: TestUtils.assertSame(Complex.NaN, ComplexUtils.tan(infOne));
628: TestUtils.assertSame(Complex.NaN, ComplexUtils.tan(negInfOne));
629: TestUtils.assertSame(Complex.NaN, ComplexUtils.tan(infInf));
630: TestUtils.assertSame(Complex.NaN, ComplexUtils.tan(infNegInf));
631: TestUtils.assertSame(Complex.NaN, ComplexUtils.tan(negInfInf));
632: TestUtils.assertSame(Complex.NaN, ComplexUtils
633: .tan(negInfNegInf));
634: }
635:
636: public void testTanCritical() {
637: TestUtils.assertSame(infNaN, ComplexUtils.tan(new Complex(
638: pi / 2, 0)));
639: TestUtils.assertSame(negInfNaN, ComplexUtils.tan(new Complex(
640: -pi / 2, 0)));
641: }
642:
643: public void testTanNull() {
644: try {
645: Complex z = ComplexUtils.tan(null);
646: fail("Expecting NullPointerException");
647: } catch (NullPointerException ex) {
648: // expected
649: }
650: }
651:
652: public void testTanh() {
653: Complex z = new Complex(3, 4);
654: Complex expected = new Complex(1.00071, 0.00490826);
655: TestUtils.assertEquals(expected, ComplexUtils.tanh(z), 1.0e-5);
656: }
657:
658: public void testTanhNaN() {
659: assertTrue(ComplexUtils.tanh(Complex.NaN).isNaN());
660: }
661:
662: public void testTanhInf() {
663: TestUtils.assertSame(Complex.NaN, ComplexUtils.tanh(oneInf));
664: TestUtils.assertSame(Complex.NaN, ComplexUtils.tanh(oneNegInf));
665: TestUtils.assertSame(nanZero, ComplexUtils.tanh(infOne));
666: TestUtils.assertSame(nanZero, ComplexUtils.tanh(negInfOne));
667: TestUtils.assertSame(Complex.NaN, ComplexUtils.tanh(infInf));
668: TestUtils.assertSame(Complex.NaN, ComplexUtils.tanh(infNegInf));
669: TestUtils.assertSame(Complex.NaN, ComplexUtils.tanh(negInfInf));
670: TestUtils.assertSame(Complex.NaN, ComplexUtils
671: .tanh(negInfNegInf));
672: }
673:
674: public void testTanhCritical() {
675: TestUtils.assertSame(nanInf, ComplexUtils.tanh(new Complex(0,
676: pi / 2)));
677: }
678:
679: public void testTanhNull() {
680: try {
681: Complex z = ComplexUtils.tanh(null);
682: fail("Expecting NullPointerException");
683: } catch (NullPointerException ex) {
684: // expected
685: }
686: }
687: }
|