1 package Math::GSL::Complex::Test;
2 use base q{Test::Class};
3 use Test::More tests => 60;
4 use Math::GSL::Complex qw/:all/;
5 use Math::GSL::Test qw/:all/;
6 use Math::GSL::Const qw/:all/;
7 use Math::GSL qw/:all/;
11 sub make_fixture : Test(setup) {
13 $self->{gsl_complex} = Math::GSL::Complex::gsl_complex->new;
16 sub teardown : Test(teardown) {
19 sub GSL_COMPLEX_NEW : Tests {
21 my $x = $self->{gsl_complex};
22 isa_ok( $x, 'Math::GSL::Complex::gsl_complex' );
23 isa_ok( Math::GSL::Complex->new(0,0), 'Math::GSL::Complex' );
26 sub GSL_COMPLEX_RECT : Tests {
27 my $x = gsl_complex_rect(5,3);
28 ok_similar( [ gsl_parts($x) ], [ 5, 3 ], 'gsl_complex_rect' );
31 sub GSL_COMPLEX_POLAR : Tests {
32 my $x = gsl_complex_polar(1.0,0.1);
33 my ($r,$theta) = ( gsl_complex_abs($x), gsl_complex_arg($x) );
35 print "x=$r * e^(i $theta)*I\n" if $ENV{DEBUG};
36 ok( is_similar($r,1.0), 'gsl_complex_polar r, res=' .($r-1.) . "\n");
37 ok( is_similar($theta,0.1), 'gsl_complex_polar theta, res=' .($theta-0.1) . "\n");
40 sub GSL_COMPLEX_ABS : Tests {
41 my $x = gsl_complex_rect(5,3);
42 my $abs = gsl_complex_abs($x);
43 ok ( is_similar($abs, sqrt(34)), 'gsl_complex_abs');
46 sub GSL_COMPLEX_ABS2 : Tests {
47 my $x = gsl_complex_rect(5,3);
48 my $abs = gsl_complex_abs2($x);
49 ok( is_similar($abs, 34), 'gsl_complex_abs2' );
52 sub GSL_COMPLEX_LOGABS : Tests {
53 my $x = gsl_complex_rect(5,3);
55 my $abs = gsl_complex_logabs($x);
56 ok ( is_similar($abs, log(sqrt(34)) ), 'gsl_complex_logabs' );
59 sub GSL_COMPLEX_LOG : Tests {
60 my $x = gsl_complex_rect($M_E,0);
61 $x = gsl_complex_log($x);
62 ok_similar([ gsl_parts($x) ] ,
67 sub GSL_COMPLEX_LOG10 : Tests {
68 my $x = gsl_complex_rect(10,0);
69 $x = gsl_complex_log10($x);
70 ok_similar([ gsl_parts($x) ] ,
76 sub GSL_COMPLEX_LOG_B : Tests {
77 my $z = gsl_complex_rect(5.0,0);
78 my $base = gsl_complex_rect(5.0,0);
79 $z = gsl_complex_log_b($z, $base);
80 ok_similar([ gsl_parts($z) ] ,
86 sub GSL_COMPLEX_EXP : Tests {
87 my $z = gsl_complex_rect(2, 5);
88 $z = gsl_complex_exp($z);
89 ok_similar( [ gsl_parts($z) ],
90 [ $M_E**2*cos(5), $M_E**2*sin(5) ]
94 sub GSL_COMPLEX_EXP_EULERS_IDENTITY : Tests { # e^(i pi) + 1 = 0
95 my $z = gsl_complex_rect(0, $M_PI);
96 $z = gsl_complex_exp($z);
97 $z = gsl_complex_add_real($z, 1);
98 ok_similar( [ gsl_parts($z) ],
103 sub GSL_COMPLEX_ADD : Tests {
104 my $x = gsl_complex_rect(5,3);
105 my $y = gsl_complex_rect(1,2);
107 my $z = gsl_complex_add($x, $y);
108 ok_similar( [ gsl_parts($z) ], [ 6, 5 ], 'gsl_complex_add' );
111 sub GSL_COMPLEX_SUB : Tests {
112 my $x = gsl_complex_rect(5,3);
113 my $y = gsl_complex_rect(1,2);
115 my $z = gsl_complex_sub($x, $y);
116 ok_similar( [ gsl_parts($z) ], [ 4, 1 ], 'gsl_complex_sub' );
119 sub GSL_COMPLEX_MUL : Tests {
120 my $x = gsl_complex_rect(5,3);
121 my $y = gsl_complex_rect(1,2);
123 my $z = gsl_complex_mul($x, $y);
124 ok_similar( [ gsl_parts($z) ], [ -1, 13 ], 'gsl_complex_mul' );
127 sub GSL_COMPLEX_DIV : Tests {
128 my $x = gsl_complex_rect(4,5);
129 my $y = gsl_complex_rect(2,1);
131 my $z = gsl_complex_div($x, $y);
132 ok_similar( [ gsl_parts($z) ], [ 13/5, 6/5 ], 'gsl_complex_div' );
134 sub GSL_COMPLEX_EQ : Tests {
135 my $x = gsl_complex_rect(4,5);
136 my $y = gsl_complex_rect(2,1);
137 ok(! gsl_complex_eq($x,$y), 'gsl_complex_eq' );
138 $y = gsl_complex_rect(4,5);
139 ok( gsl_complex_eq($x,$y), 'gsl_complex_eq' );
141 sub GSL_COMPLEX_ADD_REAL : Tests {
142 my $x = gsl_complex_rect(5,3);
144 my $z = gsl_complex_add_real($x, 1);
145 ok( gsl_real($z) == 6, 'gsl_complex_add_real');
148 sub GSL_COMPLEX_SUB_REAL : Tests {
149 my $x = gsl_complex_rect(5,3);
151 my $z = gsl_complex_sub_real($x, 1);
152 ok( gsl_real($z) == 4, 'gsl_complex_sub_real');
155 sub GSL_COMPLEX_MUL_REAL : Tests {
156 my $x = gsl_complex_rect(5,3);
158 my $z = gsl_complex_mul_real($x, 2);
159 ok( gsl_real($z) == 10, 'gsl_complex_mul_real');
162 sub GSL_COMPLEX_DIV_REAL : Tests {
163 my $x = gsl_complex_rect(6,3);
165 my $z = gsl_complex_div_real($x, 2);
166 ok( gsl_real($z) == 3, 'gsl_complex_div_real');
169 sub GSL_COMPLEX_ADD_IMAG : Tests {
170 my $x = gsl_complex_rect(6,3);
172 my $z = gsl_complex_add_imag($x, 2);
173 ok( gsl_imag($z) == 5, 'gsl_complex_add_imag');
176 sub GSL_COMPLEX_SUB_IMAG : Tests {
177 my $x = gsl_complex_rect(6,3);
179 my $z = gsl_complex_sub_imag($x, 2);
180 ok( gsl_imag($z) == 1, 'gsl_complex_sub_imag');
183 sub GSL_COMPLEX_MUL_IMAG : Tests {
184 my $x = gsl_complex_rect(6,3);
186 my $z = gsl_complex_mul_imag($x, 2);
187 ok_similar( [ gsl_parts($z) ], [ -6, 12 ], 'gsl_complex_mul_imag' );
190 sub GSL_COMPLEX_DIV_IMAG : Tests {
191 my $x = gsl_complex_rect(6,4);
193 my $z = gsl_complex_div_imag($x, 2);
194 ok_similar( [ gsl_parts($z) ], [ 2, -3 ], 'gsl_complex_div_imag' );
197 sub GSL_COMPLEX_CONJUGATE : Tests {
198 my $x = gsl_complex_rect(6,4);
200 my $z = gsl_complex_conjugate($x);
201 ok_similar( [ gsl_parts($z) ], [6, -4], 'gsl_complex_conjugate');
205 sub GSL_COMPLEX_NEGATIVE : Tests {
206 my $x = gsl_complex_rect(6,4);
208 my $z = gsl_complex_negative($x);
209 ok_similar( [ gsl_parts($z) ], [-6, -4], 'gsl_complex_negative');
212 sub GSL_COMPLEX_SQRT : Tests {
213 my $x = gsl_complex_rect(-7,24);
215 my $z = gsl_complex_sqrt($x);
216 ok_similar( [ gsl_parts($z) ], [ 3 , 4 ] ,'gsl_complex_sqrt' );
219 sub GSL_COMPLEX_SQRT_REAL : Tests {
220 my $x = gsl_complex_rect(9,4);
222 my $z = gsl_complex_sqrt_real(-4);
223 ok( gsl_imag($z) == 2, 'gsl_complex_sqrt_real');
226 sub GSL_COMPLEX_POW : Tests {
227 my $x = gsl_complex_rect(0,1);
228 my $y = gsl_complex_rect(2,0);
229 my $z = gsl_complex_pow($x, $y);
231 ok_similar( [ gsl_parts($z) ], [-1, 0 ], 'gsl_complex_pow' );
233 $x = gsl_complex_rect(3,4);
234 $z = gsl_complex_pow($x, $y);
236 ok_similar( [gsl_parts($z) ], [ 3**2 - 4**2, 2*3*4 ], 'gsl_complex_pow' );
239 sub GSL_COMPLEX_SET_REAL : Tests {
240 my $x = gsl_complex_rect(3,4);
242 ok_similar( [ gsl_parts($x) ], [ 5, 4 ], 'gsl_complex_set_real');
245 sub GSL_COMPLEX_SET_IMAG : Tests {
246 my $x = gsl_complex_rect(3,4);
248 ok_similar( [ gsl_parts($x) ], [ 3, 5 ], 'gsl_complex_set_imag');
251 sub GSL_COMPLEX_SET_COMPLEX : Tests {
252 my $x = gsl_complex_rect(3,4);
253 gsl_set_complex($x, 5, 3);
254 ok_similar( [ gsl_parts($x) ], [ 5, 3 ], 'gsl_complex_set_complex');
257 sub GSL_COMPLEX_SIN : Tests {
258 my $x = gsl_complex_rect(3,2);
259 my $z = gsl_complex_sin($x);
261 ok_similar( [ gsl_parts($z) ],
262 [ 0.53092108624852, -3.59056458998578 ],
267 sub GSL_COMPLEX_ARCSIN : Tests {
268 my $x = gsl_complex_rect(1,0);
269 my $z = gsl_complex_arcsin($x);
271 ok_similar( [ gsl_parts($z) ],
276 sub GSL_COMPLEX_SINH : Tests {
277 my $x = gsl_complex_rect(0,0);
278 my $z = gsl_complex_sinh($x);
280 ok_similar( [ gsl_parts($z) ],
286 sub GSL_COMPLEX_ARCSINH : Tests {
287 my $x = gsl_complex_rect(0,0);
288 my $z = gsl_complex_arcsinh($x);
290 ok_similar( [ gsl_parts($z) ],
292 'gsl_complex_arcsinh'
295 sub GSL_COMPLEX_COS : Tests {
296 my $x = gsl_complex_rect(3,2);
297 my $z = gsl_complex_cos($x);
298 ok_similar( [ gsl_parts($z) ],
299 [ -3.72454550491532, -0.511822569987385],
304 sub GSL_COMPLEX_ARCCOS : Tests {
305 my $x = gsl_complex_rect(0,0);
306 my $z = gsl_complex_arccos($x);
307 ok_similar( [ gsl_parts($z)],
312 sub GSL_COMPLEX_COSH : Tests {
313 my $x = gsl_complex_rect(0,0);
314 my $z = gsl_complex_cosh($x);
315 ok_similar( [ gsl_parts($z) ],
321 sub GSL_COMPLEX_ARCCOSH : Tests {
322 my $x = gsl_complex_rect(1,0);
323 my $z = gsl_complex_arccosh($x);
324 ok_similar( [ gsl_parts($z) ],
326 'gsl_complex_arccosh'
330 sub GSL_COMPLEX_TAN : Tests {
331 my $x = gsl_complex_rect(3,2);
332 my $z = gsl_complex_tan($x);
333 ok_similar( [ gsl_parts($z) ],
334 [ -0.0098843750383225, 0.965385879022133 ],
339 sub GSL_COMPLEX_ARCTAN : Tests {
340 my $x = gsl_complex_rect(1,0);
341 my $z = gsl_complex_arctan($x);
342 ok_similar( [ gsl_parts($z) ],
348 sub GSL_COMPLEX_TANH : Tests {
349 my $x = gsl_complex_rect(0,0);
350 my $z = gsl_complex_tan($x);
351 ok_similar( [ gsl_parts($z) ],
357 sub GSL_COMPLEX_ARCTANH : Tests {
358 my $x = gsl_complex_rect(0,0);
359 my $z = gsl_complex_arctan($x);
360 ok_similar( [ gsl_parts($z) ],
362 'gsl_complex_arctanh'
365 sub GSL_COMPLEX_SEC : Tests {
366 my $x = gsl_complex_rect(3,2);
367 my $z = gsl_complex_sec($x);
368 ok_similar( [ gsl_parts($z) ],
369 [ -0.263512975158389, 0.0362116365587685 ],
374 sub GSL_COMPLEX_ARCSEC : Tests {
375 my $x = gsl_complex_rect(0,0);
376 my $z = gsl_complex_arcsec($x);
377 ok_similar( [ gsl_parts($z) ],
382 sub GSL_COMPLEX_SECH : Tests {
383 my $x = gsl_complex_rect(1,0);
384 my $z = gsl_complex_sech($x);
385 ok_similar( [ gsl_parts($z) ],
386 [ 2/(exp(1)+exp(-1)), 0.0 ],
391 sub GSL_COMPLEX_ARCSECH : Tests {
392 my $x = gsl_complex_rect(2/(exp(1)+exp(-1)),0);
393 my $z = gsl_complex_arcsech($x);
394 ok_similar( [ gsl_parts($z) ],
396 'gsl_complex_arcsech'
400 sub GSL_COMPLEX_CSC : Tests {
401 my $x = gsl_complex_rect(3,2);
402 my $z = gsl_complex_csc($x);
403 ok_similar( [ gsl_parts($z) ],
404 [ 0.0403005788568915, 0.27254866146294],
409 sub GSL_COMPLEX_ARCCSC : Tests {
410 my $x = gsl_complex_rect(0,0);
411 my $z = gsl_complex_arccsc($x);
412 ok_similar( [ gsl_parts($z)],
418 sub GSL_COMPLEX_CSCH : Tests {
419 my $x = gsl_complex_rect(0,1);
420 my $z = gsl_complex_csch($x);
421 ok_similar( [ gsl_parts($z) ],
427 sub GSL_COMPLEX_ARCCSCH : Tests {
428 my $x = gsl_complex_rect(0,0);
429 my $z = gsl_complex_arccsch($x);
430 ok_similar( [ gsl_parts($z) ],
432 'gsl_complex_arccsch'
436 sub GSL_COMPLEX_COT : Tests {
437 my $x = gsl_complex_rect(3,2);
438 my $z = gsl_complex_cot($x);
439 ok_similar( [ gsl_parts($z) ],
440 [ -0.0106047834703371, -1.035746637765],
445 sub GSL_COMPLEX_ARCCOT : Tests {
446 my $x = gsl_complex_rect(1,0);
447 my $z = gsl_complex_arccot($x);
448 ok_similar( [ gsl_parts($z) ],
454 sub GSL_COMPLEX_COTH : Tests {
455 my $x = gsl_complex_rect(0,1);
456 my $z = gsl_complex_coth($x);
457 ok_similar( [ gsl_parts($z) ],
458 [ 0.0, -cos(1)/sin(1) ],
463 sub GSL_COMPLEX_ARCCOTH : Tests {
464 my $x = gsl_complex_rect(0,0);
465 my $z = gsl_complex_arccoth($x);
466 ok_similar( [ gsl_parts($z) ],
468 'gsl_complex_arccoth'
472 Test::Class->runtests;