1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.math.complex;
19
20 import org.apache.commons.math.TestUtils;
21
22 import junit.framework.TestCase;
23
24
25
26
27 public class ComplexUtilsTest extends TestCase {
28
29 private double inf = Double.POSITIVE_INFINITY;
30 private double negInf = Double.NEGATIVE_INFINITY;
31 private double nan = Double.NaN;
32 private double pi = Math.PI;
33
34 private Complex oneInf = new Complex(1, inf);
35 private Complex oneNegInf = new Complex(1, negInf);
36 private Complex infOne = new Complex(inf, 1);
37 private Complex negInfOne = new Complex(negInf, 1);
38 private Complex negInfInf = new Complex(negInf, inf);
39 private Complex infNegInf = new Complex(inf, negInf);
40 private Complex infInf = new Complex(inf, inf);
41 private Complex negInfNegInf = new Complex(negInf, negInf);
42 private Complex infNaN = new Complex(inf, nan);
43 private Complex negInfNaN = new Complex(negInf, nan);
44 private Complex nanInf = new Complex(nan, inf);
45 private Complex nanNegInf = new Complex(nan, negInf);
46 private Complex zeroNaN = new Complex(0, nan);
47 private Complex nanZero = new Complex(nan, 0);
48 private Complex infZero = new Complex(inf, 0);
49 private Complex zeroInf = new Complex(0, inf);
50 private Complex negInfZero = new Complex(negInf, 0);
51
52
53 public void testAcos() {
54 Complex z = new Complex(3, 4);
55 Complex expected = new Complex(0.936812, -2.30551);
56 TestUtils.assertEquals(expected, ComplexUtils.acos(z), 1.0e-5);
57 TestUtils.assertEquals(new Complex(Math.acos(0), 0),
58 ComplexUtils.acos(Complex.ZERO), 1.0e-12);
59 }
60
61
62 public void testAcosInf() {
63 TestUtils.assertSame(Complex.NaN, ComplexUtils.acos(oneInf));
64 TestUtils.assertSame(Complex.NaN, ComplexUtils.acos(oneNegInf));
65 TestUtils.assertSame(Complex.NaN, ComplexUtils.acos(infOne));
66 TestUtils.assertSame(Complex.NaN, ComplexUtils.acos(negInfOne));
67 TestUtils.assertSame(Complex.NaN, ComplexUtils.acos(infInf));
68 TestUtils.assertSame(Complex.NaN, ComplexUtils.acos(infNegInf));
69 TestUtils.assertSame(Complex.NaN, ComplexUtils.acos(negInfInf));
70 TestUtils.assertSame(Complex.NaN, ComplexUtils.acos(negInfNegInf));
71 }
72
73
74 public void testAcosNaN() {
75 assertTrue(ComplexUtils.acos(Complex.NaN).isNaN());
76 }
77
78
79 public void testAcosNull() {
80 try {
81 ComplexUtils.acos(null);
82 fail("Expecting NullPointerException");
83 } catch (NullPointerException ex) {
84
85 }
86 }
87
88
89 public void testAsin() {
90 Complex z = new Complex(3, 4);
91 Complex expected = new Complex(0.633984, 2.30551);
92 TestUtils.assertEquals(expected, ComplexUtils.asin(z), 1.0e-5);
93 }
94
95
96 public void testAsinNaN() {
97 assertTrue(ComplexUtils.asin(Complex.NaN).isNaN());
98 }
99
100
101 public void testAsinInf() {
102 TestUtils.assertSame(Complex.NaN, ComplexUtils.asin(oneInf));
103 TestUtils.assertSame(Complex.NaN, ComplexUtils.asin(oneNegInf));
104 TestUtils.assertSame(Complex.NaN, ComplexUtils.asin(infOne));
105 TestUtils.assertSame(Complex.NaN, ComplexUtils.asin(negInfOne));
106 TestUtils.assertSame(Complex.NaN, ComplexUtils.asin(infInf));
107 TestUtils.assertSame(Complex.NaN, ComplexUtils.asin(infNegInf));
108 TestUtils.assertSame(Complex.NaN, ComplexUtils.asin(negInfInf));
109 TestUtils.assertSame(Complex.NaN, ComplexUtils.asin(negInfNegInf));
110 }
111
112
113 public void testAsinNull() {
114 try {
115 ComplexUtils.asin(null);
116 fail("Expecting NullPointerException");
117 } catch (NullPointerException ex) {
118
119 }
120 }
121
122
123 public void testAtan() {
124 Complex z = new Complex(3, 4);
125 Complex expected = new Complex(1.44831, 0.158997);
126 TestUtils.assertEquals(expected, ComplexUtils.atan(z), 1.0e-5);
127 }
128
129
130 public void testAtanInf() {
131 TestUtils.assertSame(Complex.NaN, ComplexUtils.atan(oneInf));
132 TestUtils.assertSame(Complex.NaN, ComplexUtils.atan(oneNegInf));
133 TestUtils.assertSame(Complex.NaN, ComplexUtils.atan(infOne));
134 TestUtils.assertSame(Complex.NaN, ComplexUtils.atan(negInfOne));
135 TestUtils.assertSame(Complex.NaN, ComplexUtils.atan(infInf));
136 TestUtils.assertSame(Complex.NaN, ComplexUtils.atan(infNegInf));
137 TestUtils.assertSame(Complex.NaN, ComplexUtils.atan(negInfInf));
138 TestUtils.assertSame(Complex.NaN, ComplexUtils.atan(negInfNegInf));
139 }
140
141
142 public void testAtanNaN() {
143 assertTrue(ComplexUtils.atan(Complex.NaN).isNaN());
144 assertTrue(ComplexUtils.atan(Complex.I).isNaN());
145 }
146
147
148 public void testAtanNull() {
149 try {
150 ComplexUtils.atan(null);
151 fail("Expecting NullPointerException");
152 } catch (NullPointerException ex) {
153
154 }
155 }
156
157
158 public void testCos() {
159 Complex z = new Complex(3, 4);
160 Complex expected = new Complex(-27.03495, -3.851153);
161 TestUtils.assertEquals(expected, ComplexUtils.cos(z), 1.0e-5);
162 }
163
164
165 public void testCosNaN() {
166 assertTrue(ComplexUtils.cos(Complex.NaN).isNaN());
167 }
168
169
170 public void testCosInf() {
171 TestUtils.assertSame(infNegInf, ComplexUtils.cos(oneInf));
172 TestUtils.assertSame(infInf, ComplexUtils.cos(oneNegInf));
173 TestUtils.assertSame(Complex.NaN, ComplexUtils.cos(infOne));
174 TestUtils.assertSame(Complex.NaN, ComplexUtils.cos(negInfOne));
175 TestUtils.assertSame(Complex.NaN, ComplexUtils.cos(infInf));
176 TestUtils.assertSame(Complex.NaN, ComplexUtils.cos(infNegInf));
177 TestUtils.assertSame(Complex.NaN, ComplexUtils.cos(negInfInf));
178 TestUtils.assertSame(Complex.NaN, ComplexUtils.cos(negInfNegInf));
179 }
180
181
182 public void testCosNull() {
183 try {
184 ComplexUtils.cos(null);
185 fail("Expecting NullPointerException");
186 } catch (NullPointerException ex) {
187
188 }
189 }
190
191
192 public void testCosh() {
193 Complex z = new Complex(3, 4);
194 Complex expected = new Complex(-6.58066, -7.58155);
195 TestUtils.assertEquals(expected, ComplexUtils.cosh(z), 1.0e-5);
196 }
197
198
199 public void testCoshNaN() {
200 assertTrue(ComplexUtils.cosh(Complex.NaN).isNaN());
201 }
202
203
204 public void testCoshInf() {
205 TestUtils.assertSame(Complex.NaN, ComplexUtils.cosh(oneInf));
206 TestUtils.assertSame(Complex.NaN, ComplexUtils.cosh(oneNegInf));
207 TestUtils.assertSame(infInf, ComplexUtils.cosh(infOne));
208 TestUtils.assertSame(infNegInf, ComplexUtils.cosh(negInfOne));
209 TestUtils.assertSame(Complex.NaN, ComplexUtils.cosh(infInf));
210 TestUtils.assertSame(Complex.NaN, ComplexUtils.cosh(infNegInf));
211 TestUtils.assertSame(Complex.NaN, ComplexUtils.cosh(negInfInf));
212 TestUtils.assertSame(Complex.NaN, ComplexUtils.cosh(negInfNegInf));
213 }
214
215
216 public void testCoshNull() {
217 try {
218 ComplexUtils.cosh(null);
219 fail("Expecting NullPointerException");
220 } catch (NullPointerException ex) {
221
222 }
223 }
224
225
226 public void testExp() {
227 Complex z = new Complex(3, 4);
228 Complex expected = new Complex(-13.12878, -15.20078);
229 TestUtils.assertEquals(expected, ComplexUtils.exp(z), 1.0e-5);
230 TestUtils.assertEquals(Complex.ONE,
231 ComplexUtils.exp(Complex.ZERO), 10e-12);
232 Complex iPi = Complex.I.multiply(new Complex(pi,0));
233 TestUtils.assertEquals(Complex.ONE.negate(),
234 ComplexUtils.exp(iPi), 10e-12);
235 }
236
237
238 public void testExpNaN() {
239 assertTrue(ComplexUtils.exp(Complex.NaN).isNaN());
240 }
241
242
243 public void testExpInf() {
244 TestUtils.assertSame(Complex.NaN, ComplexUtils.exp(oneInf));
245 TestUtils.assertSame(Complex.NaN, ComplexUtils.exp(oneNegInf));
246 TestUtils.assertSame(infInf, ComplexUtils.exp(infOne));
247 TestUtils.assertSame(Complex.ZERO, ComplexUtils.exp(negInfOne));
248 TestUtils.assertSame(Complex.NaN, ComplexUtils.exp(infInf));
249 TestUtils.assertSame(Complex.NaN, ComplexUtils.exp(infNegInf));
250 TestUtils.assertSame(Complex.NaN, ComplexUtils.exp(negInfInf));
251 TestUtils.assertSame(Complex.NaN, ComplexUtils.exp(negInfNegInf));
252 }
253
254
255 public void testExpNull() {
256 try {
257 ComplexUtils.exp(null);
258 fail("Expecting NullPointerException");
259 } catch (NullPointerException ex) {
260
261 }
262 }
263
264
265 public void testLog() {
266 Complex z = new Complex(3, 4);
267 Complex expected = new Complex(1.60944, 0.927295);
268 TestUtils.assertEquals(expected, ComplexUtils.log(z), 1.0e-5);
269 }
270
271
272 public void testLogNaN() {
273 assertTrue(ComplexUtils.log(Complex.NaN).isNaN());
274 }
275
276
277 public void testLogInf() {
278 TestUtils.assertEquals(new Complex(inf, pi / 2),
279 ComplexUtils.log(oneInf), 10e-12);
280 TestUtils.assertEquals(new Complex(inf, -pi / 2),
281 ComplexUtils.log(oneNegInf), 10e-12);
282 TestUtils.assertEquals(infZero, ComplexUtils.log(infOne), 10e-12);
283 TestUtils.assertEquals(new Complex(inf, pi),
284 ComplexUtils.log(negInfOne), 10e-12);
285 TestUtils.assertEquals(new Complex(inf, pi / 4),
286 ComplexUtils.log(infInf), 10e-12);
287 TestUtils.assertEquals(new Complex(inf, -pi / 4),
288 ComplexUtils.log(infNegInf), 10e-12);
289 TestUtils.assertEquals(new Complex(inf, 3d * pi / 4),
290 ComplexUtils.log(negInfInf), 10e-12);
291 TestUtils.assertEquals(new Complex(inf, - 3d * pi / 4),
292 ComplexUtils.log(negInfNegInf), 10e-12);
293 }
294
295
296 public void testLogZero() {
297 TestUtils.assertSame(negInfZero, ComplexUtils.log(Complex.ZERO));
298 }
299
300
301 public void testlogNull() {
302 try {
303 ComplexUtils.log(null);
304 fail("Expecting NullPointerException");
305 } catch (NullPointerException ex) {
306
307 }
308 }
309
310 public void testPolar2Complex() {
311 TestUtils.assertEquals(Complex.ONE,
312 ComplexUtils.polar2Complex(1, 0), 10e-12);
313 TestUtils.assertEquals(Complex.ZERO,
314 ComplexUtils.polar2Complex(0, 1), 10e-12);
315 TestUtils.assertEquals(Complex.ZERO,
316 ComplexUtils.polar2Complex(0, -1), 10e-12);
317 TestUtils.assertEquals(Complex.I,
318 ComplexUtils.polar2Complex(1, pi/2), 10e-12);
319 TestUtils.assertEquals(Complex.I.negate(),
320 ComplexUtils.polar2Complex(1, -pi/2), 10e-12);
321 double r = 0;
322 for (int i = 0; i < 5; i++) {
323 r += i;
324 double theta = 0;
325 for (int j =0; j < 20; j++) {
326 theta += pi / 6;
327 TestUtils.assertEquals(altPolar(r, theta),
328 ComplexUtils.polar2Complex(r, theta), 10e-12);
329 }
330 theta = -2 * pi;
331 for (int j =0; j < 20; j++) {
332 theta -= pi / 6;
333 TestUtils.assertEquals(altPolar(r, theta),
334 ComplexUtils.polar2Complex(r, theta), 10e-12);
335 }
336 }
337 }
338
339
340 protected Complex altPolar(double r, double theta) {
341 return ComplexUtils.exp(Complex.I.multiply
342 (new Complex(theta, 0))).multiply(new Complex(r, 0));
343 }
344
345 public void testPolar2ComplexIllegalModulus() {
346 try {
347 ComplexUtils.polar2Complex(-1, 0);
348 fail("Expecting IllegalArgumentException");
349 } catch (IllegalArgumentException ex) {
350
351 }
352 }
353
354 public void testPolar2ComplexNaN() {
355 TestUtils.assertSame(Complex.NaN, ComplexUtils.polar2Complex(nan, 1));
356 TestUtils.assertSame(Complex.NaN, ComplexUtils.polar2Complex(1, nan));
357 TestUtils.assertSame(Complex.NaN,
358 ComplexUtils.polar2Complex(nan, nan));
359 }
360
361 public void testPolar2ComplexInf() {
362 TestUtils.assertSame(Complex.NaN, ComplexUtils.polar2Complex(1, inf));
363 TestUtils.assertSame(Complex.NaN,
364 ComplexUtils.polar2Complex(1, negInf));
365 TestUtils.assertSame(Complex.NaN, ComplexUtils.polar2Complex(inf, inf));
366 TestUtils.assertSame(Complex.NaN,
367 ComplexUtils.polar2Complex(inf, negInf));
368 TestUtils.assertSame(infInf, ComplexUtils.polar2Complex(inf, pi/4));
369 TestUtils.assertSame(infNaN, ComplexUtils.polar2Complex(inf, 0));
370 TestUtils.assertSame(infNegInf, ComplexUtils.polar2Complex(inf, -pi/4));
371 TestUtils.assertSame(negInfInf, ComplexUtils.polar2Complex(inf, 3*pi/4));
372 TestUtils.assertSame(negInfNegInf, ComplexUtils.polar2Complex(inf, 5*pi/4));
373 }
374
375
376 public void testPow() {
377 Complex x = new Complex(3, 4);
378 Complex y = new Complex(5, 6);
379 Complex expected = new Complex(-1.860893, 11.83677);
380 TestUtils.assertEquals(expected, ComplexUtils.pow(x, y), 1.0e-5);
381 }
382
383
384 public void testPowNaNBase() {
385 Complex x = new Complex(3, 4);
386 assertTrue(ComplexUtils.pow(Complex.NaN, x).isNaN());
387 }
388
389
390 public void testPowNaNExponent() {
391 Complex x = new Complex(3, 4);
392 assertTrue(ComplexUtils.pow(x, Complex.NaN).isNaN());
393 }
394
395
396 public void testPowInf() {
397 TestUtils.assertSame(Complex.NaN,ComplexUtils.pow(Complex.ONE, oneInf));
398 TestUtils.assertSame(Complex.NaN,ComplexUtils.pow(Complex.ONE, oneNegInf));
399 TestUtils.assertSame(Complex.NaN,ComplexUtils.pow(Complex.ONE, infOne));
400 TestUtils.assertSame(Complex.NaN,ComplexUtils.pow(Complex.ONE, infInf));
401 TestUtils.assertSame(Complex.NaN,ComplexUtils.pow(Complex.ONE, infNegInf));
402 TestUtils.assertSame(Complex.NaN,ComplexUtils.pow(Complex.ONE, negInfInf));
403 TestUtils.assertSame(Complex.NaN,ComplexUtils.pow(Complex.ONE, negInfNegInf));
404 TestUtils.assertSame(Complex.NaN,ComplexUtils.pow(infOne, Complex.ONE));
405 TestUtils.assertSame(Complex.NaN,ComplexUtils.pow(negInfOne, Complex.ONE));
406 TestUtils.assertSame(Complex.NaN,ComplexUtils.pow(infInf, Complex.ONE));
407 TestUtils.assertSame(Complex.NaN,ComplexUtils.pow(infNegInf, Complex.ONE));
408 TestUtils.assertSame(Complex.NaN,ComplexUtils.pow(negInfInf, Complex.ONE));
409 TestUtils.assertSame(Complex.NaN,ComplexUtils.pow(negInfNegInf, Complex.ONE));
410 TestUtils.assertSame(Complex.NaN,ComplexUtils.pow(negInfNegInf, infNegInf));
411 TestUtils.assertSame(Complex.NaN,ComplexUtils.pow(negInfNegInf, negInfNegInf));
412 TestUtils.assertSame(Complex.NaN,ComplexUtils.pow(negInfNegInf, infInf));
413 TestUtils.assertSame(Complex.NaN,ComplexUtils.pow(infInf, infNegInf));
414 TestUtils.assertSame(Complex.NaN,ComplexUtils.pow(infInf, negInfNegInf));
415 TestUtils.assertSame(Complex.NaN,ComplexUtils.pow(infInf, infInf));
416 TestUtils.assertSame(Complex.NaN,ComplexUtils.pow(infNegInf, infNegInf));
417 TestUtils.assertSame(Complex.NaN,ComplexUtils.pow(infNegInf, negInfNegInf));
418 TestUtils.assertSame(Complex.NaN,ComplexUtils.pow(infNegInf, infInf));
419 }
420
421
422 public void testPowZero() {
423 TestUtils.assertSame(Complex.NaN,
424 ComplexUtils.pow(Complex.ZERO, Complex.ONE));
425 TestUtils.assertSame(Complex.NaN,
426 ComplexUtils.pow(Complex.ZERO, Complex.ZERO));
427 TestUtils.assertSame(Complex.NaN,
428 ComplexUtils.pow(Complex.ZERO, Complex.I));
429 TestUtils.assertEquals(Complex.ONE,
430 ComplexUtils.pow(Complex.ONE, Complex.ZERO), 10e-12);
431 TestUtils.assertEquals(Complex.ONE,
432 ComplexUtils.pow(Complex.I, Complex.ZERO), 10e-12);
433 TestUtils.assertEquals(Complex.ONE,
434 ComplexUtils.pow(new Complex(-1, 3), Complex.ZERO), 10e-12);
435 }
436
437
438 public void testpowNull() {
439 try {
440 ComplexUtils.pow(null, Complex.ONE);
441 fail("Expecting NullPointerException");
442 } catch (NullPointerException ex) {
443
444 }
445 try {
446 ComplexUtils.pow(Complex.ONE, null);
447 fail("Expecting NullPointerException");
448 } catch (NullPointerException ex) {
449
450 }
451 }
452
453
454 public void testSin() {
455 Complex z = new Complex(3, 4);
456 Complex expected = new Complex(3.853738, -27.01681);
457 TestUtils.assertEquals(expected, ComplexUtils.sin(z), 1.0e-5);
458 }
459
460
461 public void testSinInf() {
462 TestUtils.assertSame(infInf, ComplexUtils.sin(oneInf));
463 TestUtils.assertSame(infNegInf, ComplexUtils.sin(oneNegInf));
464 TestUtils.assertSame(Complex.NaN, ComplexUtils.sin(infOne));
465 TestUtils.assertSame(Complex.NaN, ComplexUtils.sin(negInfOne));
466 TestUtils.assertSame(Complex.NaN, ComplexUtils.sin(infInf));
467 TestUtils.assertSame(Complex.NaN, ComplexUtils.sin(infNegInf));
468 TestUtils.assertSame(Complex.NaN, ComplexUtils.sin(negInfInf));
469 TestUtils.assertSame(Complex.NaN, ComplexUtils.sin(negInfNegInf));
470 }
471
472
473 public void testSinNaN() {
474 assertTrue(ComplexUtils.sin(Complex.NaN).isNaN());
475 }
476
477
478 public void testSinNull() {
479 try {
480 ComplexUtils.sin(null);
481 fail("Expecting NullPointerException");
482 } catch (NullPointerException ex) {
483
484 }
485 }
486
487
488 public void testSinh() {
489 Complex z = new Complex(3, 4);
490 Complex expected = new Complex(-6.54812, -7.61923);
491 TestUtils.assertEquals(expected, ComplexUtils.sinh(z), 1.0e-5);
492 }
493
494
495 public void testSinhNaN() {
496 assertTrue(ComplexUtils.sinh(Complex.NaN).isNaN());
497 }
498
499
500 public void testSinhInf() {
501 TestUtils.assertSame(Complex.NaN, ComplexUtils.sinh(oneInf));
502 TestUtils.assertSame(Complex.NaN, ComplexUtils.sinh(oneNegInf));
503 TestUtils.assertSame(infInf, ComplexUtils.sinh(infOne));
504 TestUtils.assertSame(negInfInf, ComplexUtils.sinh(negInfOne));
505 TestUtils.assertSame(Complex.NaN, ComplexUtils.sinh(infInf));
506 TestUtils.assertSame(Complex.NaN, ComplexUtils.sinh(infNegInf));
507 TestUtils.assertSame(Complex.NaN, ComplexUtils.sinh(negInfInf));
508 TestUtils.assertSame(Complex.NaN, ComplexUtils.sinh(negInfNegInf));
509 }
510
511
512 public void testsinhNull() {
513 try {
514 ComplexUtils.sinh(null);
515 fail("Expecting NullPointerException");
516 } catch (NullPointerException ex) {
517
518 }
519 }
520
521
522 public void testSqrtRealPositive() {
523 Complex z = new Complex(3, 4);
524 Complex expected = new Complex(2, 1);
525 TestUtils.assertEquals(expected, ComplexUtils.sqrt(z), 1.0e-5);
526 }
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
536 public void testSqrtRealNegative() {
537 Complex z = new Complex(-3.0, 4);
538 Complex expected = new Complex(1, 2);
539 TestUtils.assertEquals(expected, ComplexUtils.sqrt(z), 1.0e-5);
540 }
541
542
543 public void testSqrtImaginaryZero() {
544 Complex z = new Complex(-3.0, 0.0);
545 Complex expected = new Complex(0.0, 1.73205);
546 TestUtils.assertEquals(expected, ComplexUtils.sqrt(z), 1.0e-5);
547 }
548
549
550 public void testSqrtImaginaryNegative() {
551 Complex z = new Complex(-3.0, -4.0);
552 Complex expected = new Complex(1.0, -2.0);
553 TestUtils.assertEquals(expected, ComplexUtils.sqrt(z), 1.0e-5);
554 }
555
556
557 public void testSqrtPolar() {
558 double r = 1;
559 for (int i = 0; i < 5; i++) {
560 r += i;
561 double theta = 0;
562 for (int j =0; j < 11; j++) {
563 theta += pi /12;
564 Complex z = ComplexUtils.polar2Complex(r, theta);
565 Complex sqrtz = ComplexUtils.polar2Complex(Math.sqrt(r), theta / 2);
566 TestUtils.assertEquals(sqrtz, ComplexUtils.sqrt(z), 10e-12);
567 }
568 }
569 }
570
571
572 public void testSqrtNaN() {
573 assertTrue(ComplexUtils.sqrt(Complex.NaN).isNaN());
574 }
575
576
577 public void testSqrtInf() {
578 TestUtils.assertSame(infNaN, ComplexUtils.sqrt(oneInf));
579 TestUtils.assertSame(infNaN, ComplexUtils.sqrt(oneNegInf));
580 TestUtils.assertSame(infZero, ComplexUtils.sqrt(infOne));
581 TestUtils.assertSame(zeroInf, ComplexUtils.sqrt(negInfOne));
582 TestUtils.assertSame(infNaN, ComplexUtils.sqrt(infInf));
583 TestUtils.assertSame(infNaN, ComplexUtils.sqrt(infNegInf));
584 TestUtils.assertSame(nanInf, ComplexUtils.sqrt(negInfInf));
585 TestUtils.assertSame(nanNegInf, ComplexUtils.sqrt(negInfNegInf));
586 }
587
588
589 public void testSqrtNull() {
590 try {
591 ComplexUtils.sqrt(null);
592 fail("Expecting NullPointerException");
593 } catch (NullPointerException ex) {
594
595 }
596 }
597
598
599 public void testSqrt1z() {
600 Complex z = new Complex(3, 4);
601 Complex expected = new Complex(4.08033, -2.94094);
602 TestUtils.assertEquals(expected, ComplexUtils.sqrt1z(z), 1.0e-5);
603 }
604
605
606 public void testSqrt1zNaN() {
607 assertTrue(ComplexUtils.sqrt1z(Complex.NaN).isNaN());
608 }
609
610
611 public void testSqrt1zNull() {
612 try {
613 ComplexUtils.sqrt1z(null);
614 fail("Expecting NullPointerException");
615 } catch (NullPointerException ex) {
616
617 }
618 }
619
620
621 public void testTan() {
622 Complex z = new Complex(3, 4);
623 Complex expected = new Complex(-0.000187346, 0.999356);
624 TestUtils.assertEquals(expected, ComplexUtils.tan(z), 1.0e-5);
625 }
626
627
628 public void testTanNaN() {
629 assertTrue(ComplexUtils.tan(Complex.NaN).isNaN());
630 }
631
632
633 public void testTanInf() {
634 TestUtils.assertSame(zeroNaN, ComplexUtils.tan(oneInf));
635 TestUtils.assertSame(zeroNaN, ComplexUtils.tan(oneNegInf));
636 TestUtils.assertSame(Complex.NaN, ComplexUtils.tan(infOne));
637 TestUtils.assertSame(Complex.NaN, ComplexUtils.tan(negInfOne));
638 TestUtils.assertSame(Complex.NaN, ComplexUtils.tan(infInf));
639 TestUtils.assertSame(Complex.NaN, ComplexUtils.tan(infNegInf));
640 TestUtils.assertSame(Complex.NaN, ComplexUtils.tan(negInfInf));
641 TestUtils.assertSame(Complex.NaN, ComplexUtils.tan(negInfNegInf));
642 }
643
644
645 public void testTanCritical() {
646 TestUtils.assertSame(infNaN, ComplexUtils.tan(new Complex(pi/2, 0)));
647 TestUtils.assertSame(negInfNaN, ComplexUtils.tan(new Complex(-pi/2, 0)));
648 }
649
650
651 public void testTanNull() {
652 try {
653 ComplexUtils.tan(null);
654 fail("Expecting NullPointerException");
655 } catch (NullPointerException ex) {
656
657 }
658 }
659
660
661 public void testTanh() {
662 Complex z = new Complex(3, 4);
663 Complex expected = new Complex(1.00071, 0.00490826);
664 TestUtils.assertEquals(expected, ComplexUtils.tanh(z), 1.0e-5);
665 }
666
667
668 public void testTanhNaN() {
669 assertTrue(ComplexUtils.tanh(Complex.NaN).isNaN());
670 }
671
672
673 public void testTanhInf() {
674 TestUtils.assertSame(Complex.NaN, ComplexUtils.tanh(oneInf));
675 TestUtils.assertSame(Complex.NaN, ComplexUtils.tanh(oneNegInf));
676 TestUtils.assertSame(nanZero, ComplexUtils.tanh(infOne));
677 TestUtils.assertSame(nanZero, ComplexUtils.tanh(negInfOne));
678 TestUtils.assertSame(Complex.NaN, ComplexUtils.tanh(infInf));
679 TestUtils.assertSame(Complex.NaN, ComplexUtils.tanh(infNegInf));
680 TestUtils.assertSame(Complex.NaN, ComplexUtils.tanh(negInfInf));
681 TestUtils.assertSame(Complex.NaN, ComplexUtils.tanh(negInfNegInf));
682 }
683
684
685 public void testTanhCritical() {
686 TestUtils.assertSame(nanInf, ComplexUtils.tanh(new Complex(0, pi/2)));
687 }
688
689
690 public void testTanhNull() {
691 try {
692 ComplexUtils.tanh(null);
693 fail("Expecting NullPointerException");
694 } catch (NullPointerException ex) {
695
696 }
697 }
698 }