Math::Complex update
[p5sagit/p5-mst-13.2.git] / t / lib / complex.t
1 #!./perl
2
3 # $RCSfile$
4 #
5 # Regression tests for the new Math::Complex pacakge
6 # -- Raphael Manfredi, Septemeber 1996
7 # -- Jarkko Hietaniemi Manfredi, March 1997
8 BEGIN {
9     chdir 't' if -d 't';
10     @INC = '../lib';
11 }
12 use Math::Complex;
13
14 $test = 0;
15 $| = 1;
16 $script = '';
17 my $eps = 1e-4; # for example root() is quite bad
18
19 while (<DATA>) {
20         s/^\s+//;
21         next if $_ eq '' || /^\#/;
22         chomp;
23         $test_set = 0;          # Assume not a test over a set of values
24         if (/^&(.*)/) {
25                 $op = $1;
26                 next;
27         }
28         elsif (/^\{(.*)\}/) {
29                 set($1, \@set, \@val);
30                 next;
31         }
32         elsif (s/^\|//) {
33                 $test_set = 1;  # Requests we loop over the set...
34         }
35         my @args = split(/:/);
36         if ($test_set == 1) {
37                 my $i;
38                 for ($i = 0; $i < @set; $i++) {
39                         # complex number
40                         $target = $set[$i];
41                         # textual value as found in set definition
42                         $zvalue = $val[$i];
43                         test($zvalue, $target, @args);
44                 }
45         } else {
46                 test($op, undef, @args);
47         }
48 }
49
50 print "1..$test\n";
51 eval $script;
52 die $@ if $@;
53
54 sub test {
55         my ($op, $z, @args) = @_;
56         $test++;
57         my $i;
58         for ($i = 0; $i < @args; $i++) {
59                 $val = value($args[$i]);
60                 $script .= "\$z$i = $val;\n";
61         }
62         if (defined $z) {
63                 $args = "'$op'";                # Really the value
64                 $try = "abs(\$z0 - \$z1) <= $eps ? \$z1 : \$z0";
65                 $script .= "\$res = $try; ";
66                 $script .= "check($test, $args[0], \$res, \$z$#args, $args);\n";
67         } else {
68                 my ($try, $args);
69                 if (@args == 2) {
70                         $try = "$op \$z0";
71                         $args = "'$args[0]'";
72                 } else {
73                         $try = ($op =~ /^\w/) ? "$op(\$z0, \$z1)" : "\$z0 $op \$z1";
74                         $args = "'$args[0]', '$args[1]'";
75                 }
76                 $script .= "\$res = $try; ";
77                 $script .= "check($test, '$try', \$res, \$z$#args, $args);\n";
78         }
79 }
80
81 sub set {
82         my ($set, $setref, $valref) = @_;
83         @{$setref} = ();
84         @{$valref} = ();
85         my @set = split(/;\s*/, $set);
86         my @res;
87         my $i;
88         for ($i = 0; $i < @set; $i++) {
89                 push(@{$valref}, $set[$i]);
90                 my $val = value($set[$i]);
91                 $script .= "\$s$i = $val;\n";
92                 push(@{$setref}, "\$s$i");
93         }
94 }
95
96 sub value {
97         local ($_) = @_;
98         if (/^\s*\((.*),(.*)\)/) {
99                 return "cplx($1,$2)";
100         }
101         elsif (/^\s*\[(.*),(.*)\]/) {
102                 return "cplxe($1,$2)";
103         }
104         elsif (/^\s*'(.*)'/) {
105                 my $ex = $1;
106                 $ex =~ s/\bz\b/$target/g;
107                 $ex =~ s/\br\b/abs($target)/g;
108                 $ex =~ s/\bt\b/arg($target)/g;
109                 $ex =~ s/\ba\b/Re($target)/g;
110                 $ex =~ s/\bb\b/Im($target)/g;
111                 return $ex;
112         }
113         elsif (/^\s*"(.*)"/) {
114                 return "\"$1\"";
115         }
116         return $_;
117 }
118
119 sub check {
120         my ($test, $try, $got, $expected, @z) = @_;
121
122 #       print "# @_\n";
123
124         if ("$got" eq "$expected"
125             ||
126             ($expected =~ /^-?\d/ && $got == $expected)
127             ||
128             (abs($got - $expected) < $eps)
129             ) {
130                 print "ok $test\n";
131         } else {
132                 print "not ok $test\n";
133                 my $args = (@z == 1) ? "z = $z[0]" : "z0 = $z[0], z1 = $z[1]";
134                 print "# '$try' expected: '$expected' got: '$got' for $args\n";
135         }
136 }
137 __END__
138 &+
139 (3,4):(3,4):(6,8)
140 (-3,4):(3,-4):(0,0)
141 (3,4):-3:(0,4)
142 1:(4,2):(5,2)
143 [2,0]:[2,pi]:(0,0)
144
145 &++
146 (2,1):(3,1)
147
148 &-
149 (2,3):(-2,-3)
150 [2,pi/2]:[2,-(pi)/2]
151 2:[2,0]:(0,0)
152 [3,0]:2:(1,0)
153 3:(4,5):(-1,-5)
154 (4,5):3:(1,5)
155
156 &--
157 (1,2):(0,2)
158 [2,pi]:[3,pi]
159
160 &*
161 (0,1):(0,1):(-1,0)
162 (4,5):(1,0):(4,5)
163 [2,2*pi/3]:(1,0):[2,2*pi/3]
164 2:(0,1):(0,2)
165 (0,1):3:(0,3)
166 (0,1):(4,1):(-1,4)
167 (2,1):(4,-1):(9,2)
168
169 &/
170 (3,4):(3,4):(1,0)
171 (4,-5):1:(4,-5)
172 1:(0,1):(0,-1)
173 (0,6):(0,2):(3,0)
174 (9,2):(4,-1):(2,1)
175 [4,pi]:[2,pi/2]:[2,pi/2]
176 [2,pi/2]:[4,pi]:[0.5,-(pi)/2]
177
178 &Re
179 (3,4):3
180 (-3,4):-3
181 [1,pi/2]:0
182
183 &Im
184 (3,4):4
185 (3,-4):-4
186 [1,pi/2]:1
187
188 &abs
189 (3,4):5
190 (-3,4):5
191
192 &arg
193 [2,0]:0
194 [-2,0]:pi
195
196 &~
197 (4,5):(4,-5)
198 (-3,4):(-3,-4)
199 [2,pi/2]:[2,-(pi)/2]
200
201 &<
202 (3,4):(1,2):0
203 (3,4):(3,2):0
204 (3,4):(3,8):1
205 (4,4):(5,129):1
206
207 &==
208 (3,4):(4,5):0
209 (3,4):(3,5):0
210 (3,4):(2,4):0
211 (3,4):(3,4):1
212
213 &sqrt
214 -9:(0,3)
215 (-100,0):(0,10)
216 (16,-30):(5,-3)
217
218 &stringify_cartesian
219 (-100,0):"-100"
220 (0,1):"i"
221 (4,-3):"4-3i"
222 (4,0):"4"
223 (-4,0):"-4"
224 (-2,4):"-2+4i"
225 (-2,-1):"-2-i"
226
227 &stringify_polar
228 [-1, 0]:"[1,pi]"
229 [1, pi/3]:"[1,pi/3]"
230 [6, -2*pi/3]:"[6,-2pi/3]"
231 [0.5, -9*pi/11]:"[0.5,-9pi/11]"
232
233 { (4,3); [3,2]; (-3,4); (0,2); [2,1] }
234
235 |'z + ~z':'2*Re(z)'
236 |'z - ~z':'2*i*Im(z)'
237 |'z * ~z':'abs(z) * abs(z)'
238
239 { (2,3); [3,2]; (-3,2); (0,2); 3; 1.2; -3; (-3, 0); (-2, -1); [2,1] }
240
241 |'(root(z, 4))[1] ** 4':'z'
242 |'(root(z, 5))[3] ** 5':'z'
243 |'(root(z, 8))[7] ** 8':'z'
244 |'abs(z)':'r'
245 |'acot(z)':'acotan(z)'
246 |'acsc(z)':'acosec(z)'
247 |'acsc(z)':'asin(1 / z)'
248 |'asec(z)':'acos(1 / z)'
249 |'cbrt(z)':'cbrt(r) * exp(i * t/3)'
250 |'cos(acos(z))':'z'
251 |'cos(z) ** 2 + sin(z) ** 2':1
252 |'cos(z)':'cosh(i*z)'
253 |'cosh(z) ** 2 - sinh(z) ** 2':1
254 |'cot(acot(z))':'z'
255 |'cot(z)':'1 / tan(z)'
256 |'cot(z)':'cotan(z)'
257 |'csc(acsc(z))':'z'
258 |'csc(z)':'1 / sin(z)'
259 |'csc(z)':'cosec(z)'
260 |'exp(log(z))':'z'
261 |'exp(z)':'exp(a) * exp(i * b)'
262 |'ln(z)':'log(z)'
263 |'log(exp(z))':'z'
264 |'log(z)':'log(r) + i*t'
265 |'log10(z)':'log(z) / log(10)'
266 |'logn(z, 2)':'log(z) / log(2)'
267 |'logn(z, 3)':'log(z) / log(3)'
268 |'sec(asec(z))':'z'
269 |'sec(z)':'1 / cos(z)'
270 |'sin(asin(z))':'z'
271 |'sin(i * z)':'i * sinh(z)'
272 |'sqrt(z) * sqrt(z)':'z'
273 |'sqrt(z)':'sqrt(r) * exp(i * t/2)'
274 |'tan(atan(z))':'z'
275 |'z**z':'exp(z * log(z))'
276
277 { (1,1); [1,0.5]; (-2, -1); 2; -3; (-1,0.5); (0,0.5); 0.5; (2, 0); (-1, -2) }
278
279 |'cosh(acosh(z))':'z'
280 |'coth(acoth(z))':'z'
281 |'coth(z)':'1 / tanh(z)'
282 |'coth(z)':'cotanh(z)'
283 |'csch(acsch(z))':'z'
284 |'csch(z)':'1 / sinh(z)'
285 |'csch(z)':'cosech(z)'
286 |'sech(asech(z))':'z'
287 |'sech(z)':'1 / cosh(z)'
288 |'sinh(asinh(z))':'z'
289 |'tanh(atanh(z))':'z'
290
291 { (0.2,-0.4); [1,0.5]; -1.2; (-1,0.5); 0.5; (1.1, 0) }
292
293 |'acos(cos(z)) ** 2':'z * z'
294 |'acosh(cosh(z)) ** 2':'z * z'
295 |'acoth(z)':'acotanh(z)'
296 |'acoth(z)':'atanh(1 / z)'
297 |'acsch(z)':'acosech(z)'
298 |'acsch(z)':'asinh(1 / z)'
299 |'asech(z)':'acosh(1 / z)'
300 |'asin(sin(z))':'z'
301 |'asinh(sinh(z))':'z'
302 |'atan(tan(z))':'z'
303 |'atanh(tanh(z))':'z'
304
305 &sin
306 ( 2, 3):(  9.15449914691143, -4.16890695996656)
307 (-2, 3):( -9.15449914691143, -4.16890695996656)
308 (-2,-3):( -9.15449914691143,  4.16890695996656)
309 ( 2,-3):(  9.15449914691143,  4.16890695996656)
310
311 &cos
312 ( 2, 3):( -4.18962569096881, -9.10922789375534)
313 (-2, 3):( -4.18962569096881,  9.10922789375534)
314 (-2,-3):( -4.18962569096881, -9.10922789375534)
315 ( 2,-3):( -4.18962569096881,  9.10922789375534)
316
317 &tan
318 ( 2, 3):( -0.00376402564150,  1.00323862735361)
319 (-2, 3):(  0.00376402564150,  1.00323862735361)
320 (-2,-3):(  0.00376402564150, -1.00323862735361)
321 ( 2,-3):( -0.00376402564150, -1.00323862735361)
322
323 &sec
324 ( 2, 3):( -0.04167496441114,  0.09061113719624)
325 (-2, 3):( -0.04167496441114, -0.09061113719624)
326 (-2,-3):( -0.04167496441114,  0.09061113719624)
327 ( 2,-3):( -0.04167496441114, -0.09061113719624)
328
329 &csc
330 ( 2, 3):(  0.09047320975321,  0.04120098628857)
331 (-2, 3):( -0.09047320975321,  0.04120098628857)
332 (-2,-3):( -0.09047320975321, -0.04120098628857)
333 ( 2,-3):(  0.09047320975321, -0.04120098628857)
334
335 &cot
336 ( 2, 3):( -0.00373971037634, -0.99675779656936)
337 (-2, 3):(  0.00373971037634, -0.99675779656936)
338 (-2,-3):(  0.00373971037634,  0.99675779656936)
339 ( 2,-3):( -0.00373971037634,  0.99675779656936)
340
341 &asin
342 ( 2, 3):(  0.57065278432110,  1.98338702991654)
343 (-2, 3):( -0.57065278432110,  1.98338702991654)
344 (-2,-3):( -0.57065278432110, -1.98338702991654)
345 ( 2,-3):(  0.57065278432110, -1.98338702991654)
346
347 &acos
348 ( 2, 3):(  1.00014354247380, -1.98338702991654)
349 (-2, 3):(  2.14144911111600, -1.98338702991654)
350 (-2,-3):(  2.14144911111600,  1.98338702991654)
351 ( 2,-3):(  1.00014354247380,  1.98338702991654)
352
353 &atan
354 ( 2, 3):(  1.40992104959658,  0.22907268296854)
355 (-2, 3):( -1.40992104959658,  0.22907268296854)
356 (-2,-3):( -1.40992104959658, -0.22907268296854)
357 ( 2,-3):(  1.40992104959658, -0.22907268296854)
358
359 &asec
360 ( 2, 3):(  1.42041072246703,  0.23133469857397)
361 (-2, 3):(  1.72118193112276,  0.23133469857397)
362 (-2,-3):(  1.72118193112276, -0.23133469857397)
363 ( 2,-3):(  1.42041072246703, -0.23133469857397)
364
365 &acsc
366 ( 2, 3):(  0.15038560432786, -0.23133469857397)
367 (-2, 3):( -0.15038560432786, -0.23133469857397)
368 (-2,-3):( -0.15038560432786,  0.23133469857397)
369 ( 2,-3):(  0.15038560432786,  0.23133469857397)
370
371 &acot
372 ( 2, 3):(  0.16087527719832, -0.22907268296854)
373 (-2, 3):( -0.16087527719832, -0.22907268296854)
374 (-2,-3):( -0.16087527719832,  0.22907268296854)
375 ( 2,-3):(  0.16087527719832,  0.22907268296854)
376
377 &sinh
378 ( 2, 3):( -3.59056458998578,  0.53092108624852)
379 (-2, 3):(  3.59056458998578,  0.53092108624852)
380 (-2,-3):(  3.59056458998578, -0.53092108624852)
381 ( 2,-3):( -3.59056458998578, -0.53092108624852)
382
383 &cosh
384 ( 2, 3):( -3.72454550491532,  0.51182256998738)
385 (-2, 3):( -3.72454550491532, -0.51182256998738)
386 (-2,-3):( -3.72454550491532,  0.51182256998738)
387 ( 2,-3):( -3.72454550491532, -0.51182256998738)
388
389 &tanh
390 ( 2, 3):(  0.96538587902213, -0.00988437503832)
391 (-2, 3):( -0.96538587902213, -0.00988437503832)
392 (-2,-3):( -0.96538587902213,  0.00988437503832)
393 ( 2,-3):(  0.96538587902213,  0.00988437503832)
394
395 &sech
396 ( 2, 3):( -0.26351297515839, -0.03621163655877)
397 (-2, 3):( -0.26351297515839,  0.03621163655877)
398 (-2,-3):( -0.26351297515839, -0.03621163655877)
399 ( 2,-3):( -0.26351297515839,  0.03621163655877)
400
401 &csch
402 ( 2, 3):( -0.27254866146294, -0.04030057885689)
403 (-2, 3):(  0.27254866146294, -0.04030057885689)
404 (-2,-3):(  0.27254866146294,  0.04030057885689)
405 ( 2,-3):( -0.27254866146294,  0.04030057885689)
406
407 &coth
408 ( 2, 3):(  1.03574663776500,  0.01060478347034)
409 (-2, 3):( -1.03574663776500,  0.01060478347034)
410 (-2,-3):( -1.03574663776500, -0.01060478347034)
411 ( 2,-3):(  1.03574663776500, -0.01060478347034)
412
413 &asinh
414 ( 2, 3):(  1.96863792579310,  0.96465850440760)
415 (-2, 3):( -1.96863792579310,  0.96465850440761)
416 (-2,-3):( -1.96863792579310, -0.96465850440761)
417 ( 2,-3):(  1.96863792579310, -0.96465850440760)
418
419 &acosh
420 ( 2, 3):(  1.98338702991654,  1.00014354247380)
421 (-2, 3):( -1.98338702991653, -2.14144911111600)
422 (-2,-3):( -1.98338702991653,  2.14144911111600)
423 ( 2,-3):(  1.98338702991654, -1.00014354247380)
424
425 &atanh
426 ( 2, 3):(  0.14694666622553,  1.33897252229449)
427 (-2, 3):( -0.14694666622553,  1.33897252229449)
428 (-2,-3):( -0.14694666622553, -1.33897252229449)
429 ( 2,-3):(  0.14694666622553, -1.33897252229449)
430
431 &asech
432 ( 2, 3):(  0.23133469857397, -1.42041072246703)
433 (-2, 3):( -0.23133469857397,  1.72118193112276)
434 (-2,-3):( -0.23133469857397, -1.72118193112276)
435 ( 2,-3):(  0.23133469857397,  1.42041072246703)
436
437 &acsch
438 ( 2, 3):(  0.15735549884499, -0.22996290237721)
439 (-2, 3):( -0.15735549884499, -0.22996290237721)
440 (-2,-3):( -0.15735549884499,  0.22996290237721)
441 ( 2,-3):(  0.15735549884499,  0.22996290237721)
442
443 &acoth
444 ( 2, 3):(  0.14694666622553, -0.23182380450040)
445 (-2, 3):( -0.14694666622553, -0.23182380450040)
446 (-2,-3):( -0.14694666622553,  0.23182380450040)
447 ( 2,-3):(  0.14694666622553,  0.23182380450040)
448
449 # eof