5 # Regression tests for the Math::Complex pacakge
6 # -- Raphael Manfredi, September 1996
7 # -- Jarkko Hietaniemi, March-April 1997
23 next if $_ eq '' || /^\#/;
25 $test_set = 0; # Assume not a test over a set of values
31 set($1, \@set, \@val);
35 $test_set = 1; # Requests we loop over the set...
37 my @args = split(/:/);
40 for ($i = 0; $i < @set; $i++) {
43 # textual value as found in set definition
45 test($zvalue, $target, @args);
48 test($op, undef, @args);
58 # push(@script, qq(print "# '$op'\n";));
59 push(@script, qq(eval '$op';));
60 push(@script, qq(print 'not ' unless (\$@ =~ /Division by zero/);));
61 push(@script, qq(print "ok $test\n";));
67 # 'tan(pi/2)', # may succeed thanks to floating point inaccuracies
68 # 'sec(pi/2)', # may succeed thanks to floating point inaccuracies
75 # 'tanh(pi/2)', # may succeed thanks to floating point inaccuracies
76 # 'sech(pi/2)', # may succeed thanks to floating point inaccuracies
90 # push(@script, qq(print "# 0**0\n";));
91 push(@script, qq(eval 'cplx(0)**cplx(0)';));
92 push(@script, qq(print 'not ' unless (\$@ =~ /zero raised to the/);));
93 push(@script, qq(print "ok $test\n";));
104 # push(@script, qq(print "# root(2, $op)\n";));
105 push(@script, qq(eval 'root(2, $op)';));
106 push(@script, qq(print 'not ' unless (\$@ =~ /root must be/);));
107 push(@script, qq(print "ok $test\n";));
111 test_broot(qw(-3 -2.1 0 0.99));
114 eval join '', @script;
120 push(@script, qq(print "# $op=\n";));
124 my ($op, $z, @args) = @_;
128 $baop = 1 if ($op =~ s/;=$//);
129 for ($i = 0; $i < @args; $i++) {
130 $val = value($args[$i]);
131 push @script, "\$z$i = $val;\n";
134 $args = "'$op'"; # Really the value
135 $try = "abs(\$z0 - \$z1) <= $eps ? \$z1 : \$z0";
136 push @script, "\$res = $try; ";
137 push @script, "check($test, $args[0], \$res, \$z$#args, $args);\n";
142 $args = "'$args[0]'";
144 $try = ($op =~ /^\w/) ? "$op(\$z0, \$z1)" : "\$z0 $op \$z1";
145 $args = "'$args[0]', '$args[1]'";
147 push @script, "\$res = $try; ";
148 push @script, "check($test, '$try', \$res, \$z$#args, $args);\n";
149 if (@args > 2 and $baop) { # binary assignment ops
151 # check the op= works
154 my \$za = cplx(ref \$z0 ? \@{\$z0->cartesian} : (\$z0, 0));
156 my (\$z1r, \$z1i) = ref \$z1 ? \@{\$z1->cartesian} : (\$z1, 0);
158 my \$zb = cplx(\$z1r, \$z1i);
161 my (\$zbr, \$zbi) = \@{\$zb->cartesian};
163 check($test, '\$z0 $op= \$z1', \$za, \$z$#args, $args);
166 # check that the rhs has not changed
167 push @script, qq(print "not " unless (\$zbr == \$z1r and \$zbi == \$z1i););
168 push @script, qq(print "ok $test\n";);
175 my ($set, $setref, $valref) = @_;
178 my @set = split(/;\s*/, $set);
181 for ($i = 0; $i < @set; $i++) {
182 push(@{$valref}, $set[$i]);
183 my $val = value($set[$i]);
184 push @script, "\$s$i = $val;\n";
185 push @{$setref}, "\$s$i";
191 if (/^\s*\((.*),(.*)\)/) {
192 return "cplx($1,$2)";
194 elsif (/^\s*\[(.*),(.*)\]/) {
195 return "cplxe($1,$2)";
197 elsif (/^\s*'(.*)'/) {
199 $ex =~ s/\bz\b/$target/g;
200 $ex =~ s/\br\b/abs($target)/g;
201 $ex =~ s/\bt\b/arg($target)/g;
202 $ex =~ s/\ba\b/Re($target)/g;
203 $ex =~ s/\bb\b/Im($target)/g;
206 elsif (/^\s*"(.*)"/) {
213 my ($test, $try, $got, $expected, @z) = @_;
217 if ("$got" eq "$expected"
219 ($expected =~ /^-?\d/ && $got == $expected)
221 (abs($got - $expected) < $eps)
225 print "not ok $test\n";
226 my $args = (@z == 1) ? "z = $z[0]" : "z0 = $z[0], z1 = $z[1]";
227 print "# '$try' expected: '$expected' got: '$got' for $args\n";
257 [2,2*pi/3]:(1,0):[2,2*pi/3]
269 [4,pi]:[2,pi/2]:[2,pi/2]
270 [2,pi/2]:[4,pi]:[0.5,-(pi)/2]
275 (2,3):(4,0):(-119,-120)
336 [6, -2*pi/3]:"[6,-2pi/3]"
337 [0.5, -9*pi/11]:"[0.5,-9pi/11]"
339 { (4,3); [3,2]; (-3,4); (0,2); [2,1] }
342 |'z - ~z':'2*i*Im(z)'
343 |'z * ~z':'abs(z) * abs(z)'
345 { (2,3); [3,2]; (-3,2); (0,2); 3; 1.2; (-3, 0); (-2, -1); [2,1] }
347 |'(root(z, 4))[1] ** 4':'z'
348 |'(root(z, 5))[3] ** 5':'z'
349 |'(root(z, 8))[7] ** 8':'z'
351 |'acot(z)':'acotan(z)'
352 |'acsc(z)':'acosec(z)'
353 |'acsc(z)':'asin(1 / z)'
354 |'asec(z)':'acos(1 / z)'
355 |'cbrt(z)':'cbrt(r) * exp(i * t/3)'
357 |'cos(z) ** 2 + sin(z) ** 2':1
358 |'cos(z)':'cosh(i*z)'
359 |'cosh(z) ** 2 - sinh(z) ** 2':1
361 |'cot(z)':'1 / tan(z)'
364 |'csc(z)':'1 / sin(z)'
367 |'exp(z)':'exp(a) * exp(i * b)'
370 |'log(z)':'log(r) + i*t'
371 |'log10(z)':'log(z) / log(10)'
372 |'logn(z, 2)':'log(z) / log(2)'
373 |'logn(z, 3)':'log(z) / log(3)'
375 |'sec(z)':'1 / cos(z)'
377 |'sin(i * z)':'i * sinh(z)'
378 |'sqrt(z) * sqrt(z)':'z'
379 |'sqrt(z)':'sqrt(r) * exp(i * t/2)'
381 |'z**z':'exp(z * log(z))'
383 { (1,1); [1,0.5]; (-2, -1); 2; -3; (-1,0.5); (0,0.5); 0.5; (2, 0); (-1, -2) }
385 |'cosh(acosh(z))':'z'
386 |'coth(acoth(z))':'z'
387 |'coth(z)':'1 / tanh(z)'
388 |'coth(z)':'cotanh(z)'
389 |'csch(acsch(z))':'z'
390 |'csch(z)':'1 / sinh(z)'
391 |'csch(z)':'cosech(z)'
392 |'sech(asech(z))':'z'
393 |'sech(z)':'1 / cosh(z)'
394 |'sinh(asinh(z))':'z'
395 |'tanh(atanh(z))':'z'
397 { (0.2,-0.4); [1,0.5]; -1.2; (-1,0.5); 0.5; (1.1, 0) }
399 |'acos(cos(z)) ** 2':'z * z'
400 |'acosh(cosh(z)) ** 2':'z * z'
401 |'acoth(z)':'acotanh(z)'
402 |'acoth(z)':'atanh(1 / z)'
403 |'acsch(z)':'acosech(z)'
404 |'acsch(z)':'asinh(1 / z)'
405 |'asech(z)':'acosh(1 / z)'
407 |'asinh(sinh(z))':'z'
409 |'atanh(tanh(z))':'z'
412 ( 2, 3):( 9.15449914691143, -4.16890695996656)
413 (-2, 3):( -9.15449914691143, -4.16890695996656)
414 (-2,-3):( -9.15449914691143, 4.16890695996656)
415 ( 2,-3):( 9.15449914691143, 4.16890695996656)
418 ( 2, 3):( -4.18962569096881, -9.10922789375534)
419 (-2, 3):( -4.18962569096881, 9.10922789375534)
420 (-2,-3):( -4.18962569096881, -9.10922789375534)
421 ( 2,-3):( -4.18962569096881, 9.10922789375534)
424 ( 2, 3):( -0.00376402564150, 1.00323862735361)
425 (-2, 3):( 0.00376402564150, 1.00323862735361)
426 (-2,-3):( 0.00376402564150, -1.00323862735361)
427 ( 2,-3):( -0.00376402564150, -1.00323862735361)
430 ( 2, 3):( -0.04167496441114, 0.09061113719624)
431 (-2, 3):( -0.04167496441114, -0.09061113719624)
432 (-2,-3):( -0.04167496441114, 0.09061113719624)
433 ( 2,-3):( -0.04167496441114, -0.09061113719624)
436 ( 2, 3):( 0.09047320975321, 0.04120098628857)
437 (-2, 3):( -0.09047320975321, 0.04120098628857)
438 (-2,-3):( -0.09047320975321, -0.04120098628857)
439 ( 2,-3):( 0.09047320975321, -0.04120098628857)
442 ( 2, 3):( -0.00373971037634, -0.99675779656936)
443 (-2, 3):( 0.00373971037634, -0.99675779656936)
444 (-2,-3):( 0.00373971037634, 0.99675779656936)
445 ( 2,-3):( -0.00373971037634, 0.99675779656936)
448 ( 2, 3):( 0.57065278432110, 1.98338702991654)
449 (-2, 3):( -0.57065278432110, 1.98338702991654)
450 (-2,-3):( -0.57065278432110, -1.98338702991654)
451 ( 2,-3):( 0.57065278432110, -1.98338702991654)
454 ( 2, 3):( 1.00014354247380, -1.98338702991654)
455 (-2, 3):( 2.14144911111600, -1.98338702991654)
456 (-2,-3):( 2.14144911111600, 1.98338702991654)
457 ( 2,-3):( 1.00014354247380, 1.98338702991654)
460 ( 2, 3):( 1.40992104959658, 0.22907268296854)
461 (-2, 3):( -1.40992104959658, 0.22907268296854)
462 (-2,-3):( -1.40992104959658, -0.22907268296854)
463 ( 2,-3):( 1.40992104959658, -0.22907268296854)
466 ( 2, 3):( 1.42041072246703, 0.23133469857397)
467 (-2, 3):( 1.72118193112276, 0.23133469857397)
468 (-2,-3):( 1.72118193112276, -0.23133469857397)
469 ( 2,-3):( 1.42041072246703, -0.23133469857397)
472 ( 2, 3):( 0.15038560432786, -0.23133469857397)
473 (-2, 3):( -0.15038560432786, -0.23133469857397)
474 (-2,-3):( -0.15038560432786, 0.23133469857397)
475 ( 2,-3):( 0.15038560432786, 0.23133469857397)
478 ( 2, 3):( 0.16087527719832, -0.22907268296854)
479 (-2, 3):( -0.16087527719832, -0.22907268296854)
480 (-2,-3):( -0.16087527719832, 0.22907268296854)
481 ( 2,-3):( 0.16087527719832, 0.22907268296854)
484 ( 2, 3):( -3.59056458998578, 0.53092108624852)
485 (-2, 3):( 3.59056458998578, 0.53092108624852)
486 (-2,-3):( 3.59056458998578, -0.53092108624852)
487 ( 2,-3):( -3.59056458998578, -0.53092108624852)
490 ( 2, 3):( -3.72454550491532, 0.51182256998738)
491 (-2, 3):( -3.72454550491532, -0.51182256998738)
492 (-2,-3):( -3.72454550491532, 0.51182256998738)
493 ( 2,-3):( -3.72454550491532, -0.51182256998738)
496 ( 2, 3):( 0.96538587902213, -0.00988437503832)
497 (-2, 3):( -0.96538587902213, -0.00988437503832)
498 (-2,-3):( -0.96538587902213, 0.00988437503832)
499 ( 2,-3):( 0.96538587902213, 0.00988437503832)
502 ( 2, 3):( -0.26351297515839, -0.03621163655877)
503 (-2, 3):( -0.26351297515839, 0.03621163655877)
504 (-2,-3):( -0.26351297515839, -0.03621163655877)
505 ( 2,-3):( -0.26351297515839, 0.03621163655877)
508 ( 2, 3):( -0.27254866146294, -0.04030057885689)
509 (-2, 3):( 0.27254866146294, -0.04030057885689)
510 (-2,-3):( 0.27254866146294, 0.04030057885689)
511 ( 2,-3):( -0.27254866146294, 0.04030057885689)
514 ( 2, 3):( 1.03574663776500, 0.01060478347034)
515 (-2, 3):( -1.03574663776500, 0.01060478347034)
516 (-2,-3):( -1.03574663776500, -0.01060478347034)
517 ( 2,-3):( 1.03574663776500, -0.01060478347034)
520 ( 2, 3):( 1.96863792579310, 0.96465850440760)
521 (-2, 3):( -1.96863792579310, 0.96465850440761)
522 (-2,-3):( -1.96863792579310, -0.96465850440761)
523 ( 2,-3):( 1.96863792579310, -0.96465850440760)
526 ( 2, 3):( 1.98338702991654, 1.00014354247380)
527 (-2, 3):( -1.98338702991653, -2.14144911111600)
528 (-2,-3):( -1.98338702991653, 2.14144911111600)
529 ( 2,-3):( 1.98338702991654, -1.00014354247380)
532 ( 2, 3):( 0.14694666622553, 1.33897252229449)
533 (-2, 3):( -0.14694666622553, 1.33897252229449)
534 (-2,-3):( -0.14694666622553, -1.33897252229449)
535 ( 2,-3):( 0.14694666622553, -1.33897252229449)
538 ( 2, 3):( 0.23133469857397, -1.42041072246703)
539 (-2, 3):( -0.23133469857397, 1.72118193112276)
540 (-2,-3):( -0.23133469857397, -1.72118193112276)
541 ( 2,-3):( 0.23133469857397, 1.42041072246703)
544 ( 2, 3):( 0.15735549884499, -0.22996290237721)
545 (-2, 3):( -0.15735549884499, -0.22996290237721)
546 (-2,-3):( -0.15735549884499, 0.22996290237721)
547 ( 2,-3):( 0.15735549884499, 0.22996290237721)
550 ( 2, 3):( 0.14694666622553, -0.23182380450040)
551 (-2, 3):( -0.14694666622553, -0.23182380450040)
552 (-2,-3):( -0.14694666622553, 0.23182380450040)
553 ( 2,-3):( 0.14694666622553, 0.23182380450040)