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)
329 [6, -2*pi/3]:"[6,-2pi/3]"
330 [0.5, -9*pi/11]:"[0.5,-9pi/11]"
332 { (4,3); [3,2]; (-3,4); (0,2); [2,1] }
335 |'z - ~z':'2*i*Im(z)'
336 |'z * ~z':'abs(z) * abs(z)'
338 { (2,3); [3,2]; (-3,2); (0,2); 3; 1.2; -3; (-3, 0); (-2, -1); [2,1] }
340 |'(root(z, 4))[1] ** 4':'z'
341 |'(root(z, 5))[3] ** 5':'z'
342 |'(root(z, 8))[7] ** 8':'z'
344 |'acot(z)':'acotan(z)'
345 |'acsc(z)':'acosec(z)'
346 |'acsc(z)':'asin(1 / z)'
347 |'asec(z)':'acos(1 / z)'
348 |'cbrt(z)':'cbrt(r) * exp(i * t/3)'
350 |'cos(z) ** 2 + sin(z) ** 2':1
351 |'cos(z)':'cosh(i*z)'
352 |'cosh(z) ** 2 - sinh(z) ** 2':1
354 |'cot(z)':'1 / tan(z)'
357 |'csc(z)':'1 / sin(z)'
360 |'exp(z)':'exp(a) * exp(i * b)'
363 |'log(z)':'log(r) + i*t'
364 |'log10(z)':'log(z) / log(10)'
365 |'logn(z, 2)':'log(z) / log(2)'
366 |'logn(z, 3)':'log(z) / log(3)'
368 |'sec(z)':'1 / cos(z)'
370 |'sin(i * z)':'i * sinh(z)'
371 |'sqrt(z) * sqrt(z)':'z'
372 |'sqrt(z)':'sqrt(r) * exp(i * t/2)'
374 |'z**z':'exp(z * log(z))'
376 { (1,1); [1,0.5]; (-2, -1); 2; -3; (-1,0.5); (0,0.5); 0.5; (2, 0); (-1, -2) }
378 |'cosh(acosh(z))':'z'
379 |'coth(acoth(z))':'z'
380 |'coth(z)':'1 / tanh(z)'
381 |'coth(z)':'cotanh(z)'
382 |'csch(acsch(z))':'z'
383 |'csch(z)':'1 / sinh(z)'
384 |'csch(z)':'cosech(z)'
385 |'sech(asech(z))':'z'
386 |'sech(z)':'1 / cosh(z)'
387 |'sinh(asinh(z))':'z'
388 |'tanh(atanh(z))':'z'
390 { (0.2,-0.4); [1,0.5]; -1.2; (-1,0.5); 0.5; (1.1, 0) }
392 |'acos(cos(z)) ** 2':'z * z'
393 |'acosh(cosh(z)) ** 2':'z * z'
394 |'acoth(z)':'acotanh(z)'
395 |'acoth(z)':'atanh(1 / z)'
396 |'acsch(z)':'acosech(z)'
397 |'acsch(z)':'asinh(1 / z)'
398 |'asech(z)':'acosh(1 / z)'
400 |'asinh(sinh(z))':'z'
402 |'atanh(tanh(z))':'z'
405 ( 2, 3):( 9.15449914691143, -4.16890695996656)
406 (-2, 3):( -9.15449914691143, -4.16890695996656)
407 (-2,-3):( -9.15449914691143, 4.16890695996656)
408 ( 2,-3):( 9.15449914691143, 4.16890695996656)
411 ( 2, 3):( -4.18962569096881, -9.10922789375534)
412 (-2, 3):( -4.18962569096881, 9.10922789375534)
413 (-2,-3):( -4.18962569096881, -9.10922789375534)
414 ( 2,-3):( -4.18962569096881, 9.10922789375534)
417 ( 2, 3):( -0.00376402564150, 1.00323862735361)
418 (-2, 3):( 0.00376402564150, 1.00323862735361)
419 (-2,-3):( 0.00376402564150, -1.00323862735361)
420 ( 2,-3):( -0.00376402564150, -1.00323862735361)
423 ( 2, 3):( -0.04167496441114, 0.09061113719624)
424 (-2, 3):( -0.04167496441114, -0.09061113719624)
425 (-2,-3):( -0.04167496441114, 0.09061113719624)
426 ( 2,-3):( -0.04167496441114, -0.09061113719624)
429 ( 2, 3):( 0.09047320975321, 0.04120098628857)
430 (-2, 3):( -0.09047320975321, 0.04120098628857)
431 (-2,-3):( -0.09047320975321, -0.04120098628857)
432 ( 2,-3):( 0.09047320975321, -0.04120098628857)
435 ( 2, 3):( -0.00373971037634, -0.99675779656936)
436 (-2, 3):( 0.00373971037634, -0.99675779656936)
437 (-2,-3):( 0.00373971037634, 0.99675779656936)
438 ( 2,-3):( -0.00373971037634, 0.99675779656936)
441 ( 2, 3):( 0.57065278432110, 1.98338702991654)
442 (-2, 3):( -0.57065278432110, 1.98338702991654)
443 (-2,-3):( -0.57065278432110, -1.98338702991654)
444 ( 2,-3):( 0.57065278432110, -1.98338702991654)
447 ( 2, 3):( 1.00014354247380, -1.98338702991654)
448 (-2, 3):( 2.14144911111600, -1.98338702991654)
449 (-2,-3):( 2.14144911111600, 1.98338702991654)
450 ( 2,-3):( 1.00014354247380, 1.98338702991654)
453 ( 2, 3):( 1.40992104959658, 0.22907268296854)
454 (-2, 3):( -1.40992104959658, 0.22907268296854)
455 (-2,-3):( -1.40992104959658, -0.22907268296854)
456 ( 2,-3):( 1.40992104959658, -0.22907268296854)
459 ( 2, 3):( 1.42041072246703, 0.23133469857397)
460 (-2, 3):( 1.72118193112276, 0.23133469857397)
461 (-2,-3):( 1.72118193112276, -0.23133469857397)
462 ( 2,-3):( 1.42041072246703, -0.23133469857397)
465 ( 2, 3):( 0.15038560432786, -0.23133469857397)
466 (-2, 3):( -0.15038560432786, -0.23133469857397)
467 (-2,-3):( -0.15038560432786, 0.23133469857397)
468 ( 2,-3):( 0.15038560432786, 0.23133469857397)
471 ( 2, 3):( 0.16087527719832, -0.22907268296854)
472 (-2, 3):( -0.16087527719832, -0.22907268296854)
473 (-2,-3):( -0.16087527719832, 0.22907268296854)
474 ( 2,-3):( 0.16087527719832, 0.22907268296854)
477 ( 2, 3):( -3.59056458998578, 0.53092108624852)
478 (-2, 3):( 3.59056458998578, 0.53092108624852)
479 (-2,-3):( 3.59056458998578, -0.53092108624852)
480 ( 2,-3):( -3.59056458998578, -0.53092108624852)
483 ( 2, 3):( -3.72454550491532, 0.51182256998738)
484 (-2, 3):( -3.72454550491532, -0.51182256998738)
485 (-2,-3):( -3.72454550491532, 0.51182256998738)
486 ( 2,-3):( -3.72454550491532, -0.51182256998738)
489 ( 2, 3):( 0.96538587902213, -0.00988437503832)
490 (-2, 3):( -0.96538587902213, -0.00988437503832)
491 (-2,-3):( -0.96538587902213, 0.00988437503832)
492 ( 2,-3):( 0.96538587902213, 0.00988437503832)
495 ( 2, 3):( -0.26351297515839, -0.03621163655877)
496 (-2, 3):( -0.26351297515839, 0.03621163655877)
497 (-2,-3):( -0.26351297515839, -0.03621163655877)
498 ( 2,-3):( -0.26351297515839, 0.03621163655877)
501 ( 2, 3):( -0.27254866146294, -0.04030057885689)
502 (-2, 3):( 0.27254866146294, -0.04030057885689)
503 (-2,-3):( 0.27254866146294, 0.04030057885689)
504 ( 2,-3):( -0.27254866146294, 0.04030057885689)
507 ( 2, 3):( 1.03574663776500, 0.01060478347034)
508 (-2, 3):( -1.03574663776500, 0.01060478347034)
509 (-2,-3):( -1.03574663776500, -0.01060478347034)
510 ( 2,-3):( 1.03574663776500, -0.01060478347034)
513 ( 2, 3):( 1.96863792579310, 0.96465850440760)
514 (-2, 3):( -1.96863792579310, 0.96465850440761)
515 (-2,-3):( -1.96863792579310, -0.96465850440761)
516 ( 2,-3):( 1.96863792579310, -0.96465850440760)
519 ( 2, 3):( 1.98338702991654, 1.00014354247380)
520 (-2, 3):( -1.98338702991653, -2.14144911111600)
521 (-2,-3):( -1.98338702991653, 2.14144911111600)
522 ( 2,-3):( 1.98338702991654, -1.00014354247380)
525 ( 2, 3):( 0.14694666622553, 1.33897252229449)
526 (-2, 3):( -0.14694666622553, 1.33897252229449)
527 (-2,-3):( -0.14694666622553, -1.33897252229449)
528 ( 2,-3):( 0.14694666622553, -1.33897252229449)
531 ( 2, 3):( 0.23133469857397, -1.42041072246703)
532 (-2, 3):( -0.23133469857397, 1.72118193112276)
533 (-2,-3):( -0.23133469857397, -1.72118193112276)
534 ( 2,-3):( 0.23133469857397, 1.42041072246703)
537 ( 2, 3):( 0.15735549884499, -0.22996290237721)
538 (-2, 3):( -0.15735549884499, -0.22996290237721)
539 (-2,-3):( -0.15735549884499, 0.22996290237721)
540 ( 2,-3):( 0.15735549884499, 0.22996290237721)
543 ( 2, 3):( 0.14694666622553, -0.23182380450040)
544 (-2, 3):( -0.14694666622553, -0.23182380450040)
545 (-2,-3):( -0.14694666622553, 0.23182380450040)
546 ( 2,-3):( 0.14694666622553, 0.23182380450040)