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