Adding tests for complex hyperbolic functions.
[Math-GSL.git] / lib / Math / GSL / Complex / Test.pm
blob41a124b176ba42bb68051797674975b6aae08bf5
1 package Math::GSL::Complex::Test;
2 use base q{Test::Class};
3 use Test::More;
4 use Math::GSL::Complex qw/:all/;
5 use Math::GSL qw/:all/;
6 use Data::Dumper;
7 use strict;
9 sub make_fixture : Test(setup) {
10 my $self = shift;
11 $self->{gsl_complex} = Math::GSL::Complex::gsl_complex->new;
14 sub teardown : Test(teardown) {
17 sub GSL_COMPLEX_NEW : Test {
18 my $self = shift;
19 my $x = $self->{gsl_complex};
20 isa_ok( $x, 'Math::GSL::Complex' );
23 sub GSL_COMPLEX_RECT : Tests {
24 my $x = gsl_complex_rect(5,3);
25 ok_similar( [ gsl_parts($x) ], [ 5, 3 ], 'gsl_complex_rect' );
28 sub GSL_COMPLEX_POLAR : Tests {
29 my $x = gsl_complex_polar(1.0,0.1);
30 my ($r,$theta) = ( gsl_complex_abs($x), gsl_complex_arg($x) );
32 print "x=$r * e^(i $theta)*I\n" if $ENV{DEBUG};
33 ok( is_similar($r,1.0), 'gsl_complex_polar r, res=' .($r-1.) . "\n");
34 ok( is_similar($theta,0.1), 'gsl_complex_polar theta, res=' .($theta-0.1) . "\n");
37 sub GSL_COMPLEX_ABS : Tests {
38 my $x = gsl_complex_rect(5,3);
39 my $abs = gsl_complex_abs($x);
40 ok ( is_similar($abs, sqrt(34)), 'gsl_complex_abs');
43 sub GSL_COMPLEX_ABS2 : Tests {
44 my $x = gsl_complex_rect(5,3);
45 my $abs = gsl_complex_abs2($x);
46 ok( is_similar($abs, 34), 'gsl_complex_abs2' );
49 sub GSL_COMPLEX_LOGABS : Tests {
50 my $x = gsl_complex_rect(5,3);
52 my $abs = gsl_complex_logabs($x);
53 ok ( is_similar($abs, log(sqrt(34)) ), 'gsl_complex_logabs' );
56 sub GSL_COMPLEX_ADD : Tests {
57 my $x = gsl_complex_rect(5,3);
58 my $y = gsl_complex_rect(1,2);
60 my $z = gsl_complex_add($x, $y);
61 ok_similar( [ gsl_parts($z) ], [ 6, 5 ], 'gsl_complex_add' );
64 sub GSL_COMPLEX_SUB : Tests {
65 my $x = gsl_complex_rect(5,3);
66 my $y = gsl_complex_rect(1,2);
68 my $z = gsl_complex_sub($x, $y);
69 ok_similar( [ gsl_parts($z) ], [ 4, 1 ], 'gsl_complex_sub' );
72 sub GSL_COMPLEX_MUL : Tests {
73 my $x = gsl_complex_rect(5,3);
74 my $y = gsl_complex_rect(1,2);
76 my $z = gsl_complex_mul($x, $y);
77 ok_similar( [ gsl_parts($z) ], [ -1, 13 ], 'gsl_complex_mul' );
80 sub GSL_COMPLEX_DIV : Tests {
81 my $x = gsl_complex_rect(4,5);
82 my $y = gsl_complex_rect(2,1);
84 my $z = gsl_complex_div($x, $y);
85 ok_similar( [ gsl_parts($z) ], [ 13/5, 6/5 ], 'gsl_complex_div' );
87 sub GSL_COMPLEX_EQ : Tests {
88 my $x = gsl_complex_rect(4,5);
89 my $y = gsl_complex_rect(2,1);
90 ok(! gsl_complex_eq($x,$y), 'gsl_complex_eq' );
91 $y = gsl_complex_rect(4,5);
92 ok( gsl_complex_eq($x,$y), 'gsl_complex_eq' );
94 sub GSL_COMPLEX_ADD_REAL : Tests {
95 my $x = gsl_complex_rect(5,3);
97 my $z = gsl_complex_add_real($x, 1);
98 ok( gsl_real($z) == 6, 'gsl_complex_add_real');
101 sub GSL_COMPLEX_SUB_REAL : Tests {
102 my $x = gsl_complex_rect(5,3);
104 my $z = gsl_complex_sub_real($x, 1);
105 ok( gsl_real($z) == 4, 'gsl_complex_sub_real');
108 sub GSL_COMPLEX_MUL_REAL : Tests {
109 my $x = gsl_complex_rect(5,3);
111 my $z = gsl_complex_mul_real($x, 2);
112 ok( gsl_real($z) == 10, 'gsl_complex_mul_real');
115 sub GSL_COMPLEX_DIV_REAL : Tests {
116 my $x = gsl_complex_rect(6,3);
118 my $z = gsl_complex_div_real($x, 2);
119 ok( gsl_real($z) == 3, 'gsl_complex_div_real');
122 sub GSL_COMPLEX_ADD_IMAG : Tests {
123 my $x = gsl_complex_rect(6,3);
125 my $z = gsl_complex_add_imag($x, 2);
126 ok( gsl_imag($z) == 5, 'gsl_complex_add_imag');
129 sub GSL_COMPLEX_SUB_IMAG : Tests {
130 my $x = gsl_complex_rect(6,3);
132 my $z = gsl_complex_sub_imag($x, 2);
133 ok( gsl_imag($z) == 1, 'gsl_complex_sub_imag');
136 sub GSL_COMPLEX_MUL_IMAG : Tests {
137 my $x = gsl_complex_rect(6,3);
139 my $z = gsl_complex_mul_imag($x, 2);
140 ok_similar( [ gsl_parts($z) ], [ -6, 12 ], 'gsl_complex_mul_imag' );
143 sub GSL_COMPLEX_DIV_IMAG : Tests {
144 my $x = gsl_complex_rect(6,4);
146 my $z = gsl_complex_div_imag($x, 2);
147 ok_similar( [ gsl_parts($z) ], [ 2, -3 ], 'gsl_complex_div_imag' );
150 sub GSL_COMPLEX_CONJUGATE : Tests {
151 my $x = gsl_complex_rect(6,4);
153 my $z = gsl_complex_conjugate($x);
154 ok_similar( [ gsl_parts($z) ], [6, -4], 'gsl_complex_conjugate');
158 sub GSL_COMPLEX_NEGATIVE : Tests {
159 my $x = gsl_complex_rect(6,4);
161 my $z = gsl_complex_negative($x);
162 ok_similar( [ gsl_parts($z) ], [-6, -4], 'gsl_complex_negative');
165 sub GSL_COMPLEX_SQRT : Tests {
166 my $x = gsl_complex_rect(-7,24);
168 my $z = gsl_complex_sqrt($x);
169 ok_similar( [ gsl_parts($z) ], [ 3 , 4 ] ,'gsl_complex_sqrt' );
172 sub GSL_COMPLEX_SQRT_REAL : Tests {
173 my $x = gsl_complex_rect(9,4);
175 my $z = gsl_complex_sqrt_real(-4);
176 ok( gsl_imag($z) == 2, 'gsl_complex_sqrt_real');
179 sub GSL_COMPLEX_POW : Tests {
180 my $x = gsl_complex_rect(0,1);
181 my $y = gsl_complex_rect(2,0);
182 my $z = gsl_complex_pow($x, $y);
184 ok_similar( [ gsl_parts($z) ], [-1, 0 ], 'gsl_complex_pow' );
186 $x = gsl_complex_rect(3,4);
187 $z = gsl_complex_pow($x, $y);
189 ok_similar( [gsl_parts($z) ], [ 3**2 - 4**2, 2*3*4 ], 'gsl_complex_pow' );
192 sub GSL_COMPLEX_SET_REAL : Tests {
193 my $x = gsl_complex_rect(3,4);
194 gsl_set_real($x, 5);
195 ok_similar( [ gsl_parts($x) ], [ 5, 4 ], 'gsl_complex_set_real');
198 sub GSL_COMPLEX_SET_IMAG : Tests {
199 my $x = gsl_complex_rect(3,4);
200 gsl_set_imag($x, 5);
201 ok_similar( [ gsl_parts($x) ], [ 3, 5 ], 'gsl_complex_set_imag');
204 sub GSL_COMPLEX_SET_COMPLEX : Tests {
205 my $x = gsl_complex_rect(3,4);
206 gsl_set_complex($x, 5, 3);
207 ok_similar( [ gsl_parts($x) ], [ 5, 3 ], 'gsl_complex_set_complex');
210 sub GSL_COMPLEX_SIN : Tests {
211 my $x = gsl_complex_rect(3,2);
212 my $z = gsl_complex_sin($x);
214 ok_similar( [ gsl_parts($z) ],
215 [ 0.53092108624852, -3.59056458998578 ],
216 'gsl_complex_sin'
220 sub GSL_COMPLEX_SINH : Tests {
221 my $x = gsl_complex_rect(0,0);
222 my $z = gsl_complex_sinh($x);
224 ok_similar( [ gsl_parts($z) ],
225 [ 0.0, 0.0 ],
226 'gsl_complex_sinh'
229 sub GSL_COMPLEX_COS : Tests {
230 my $x = gsl_complex_rect(3,2);
231 my $z = gsl_complex_cos($x);
232 ok_similar( [ gsl_parts($z) ],
233 [ -3.72454550491532, -0.511822569987385],
234 'gsl_complex_cos'
238 sub GSL_COMPLEX_COSH : Tests {
239 my $x = gsl_complex_rect(0,0);
240 my $z = gsl_complex_cosh($x);
241 ok_similar( [ gsl_parts($z) ],
242 [ 1, 0.0 ],
243 'gsl_complex_cosh'
246 sub GSL_COMPLEX_TAN : Tests {
247 my $x = gsl_complex_rect(3,2);
248 my $z = gsl_complex_tan($x);
249 ok_similar( [ gsl_parts($z) ],
250 [ -0.0098843750383225, 0.965385879022133 ],
251 'gsl_complex_tan'
255 sub GSL_COMPLEX_TANH : Tests {
256 my $x = gsl_complex_rect(0,0);
257 my $z = gsl_complex_tan($x);
258 ok_similar( [ gsl_parts($z) ],
259 [ 0.0, 0.0 ],
260 'gsl_complex_tanh'
263 sub GSL_COMPLEX_SEC : Tests {
264 my $x = gsl_complex_rect(3,2);
265 my $z = gsl_complex_sec($x);
266 ok_similar( [ gsl_parts($z) ],
267 [ -0.263512975158389, 0.0362116365587685 ],
268 'gsl_complex_sec'
272 sub GSL_COMPLEX_SECH : Tests {
273 my $x = gsl_complex_rect(1,0);
274 my $z = gsl_complex_sech($x);
275 ok_similar( [ gsl_parts($z) ],
276 [ 2/(exp(1)+exp(-1)), 0.0 ],
277 'gsl_complex_sech'
281 sub GSL_COMPLEX_CSC : Tests {
282 my $x = gsl_complex_rect(3,2);
283 my $z = gsl_complex_csc($x);
284 ok_similar( [ gsl_parts($z) ],
285 [ 0.0403005788568915, 0.27254866146294],
286 'gsl_complex_csc'
290 sub GSL_COMPLEX_CSCH : Tests {
291 my $x = gsl_complex_rect(0,1);
292 my $z = gsl_complex_csch($x);
293 ok_similar( [ gsl_parts($z) ],
294 [ 0.0 , -1/sin(1)],
295 'gsl_complex_csch'
299 sub GSL_COMPLEX_COT : Tests {
300 my $x = gsl_complex_rect(3,2);
301 my $z = gsl_complex_cot($x);
302 ok_similar( [ gsl_parts($z) ],
303 [ -0.0106047834703371, -1.035746637765],
304 'gsl_complex_cot'
308 sub GSL_COMPLEX_COTH : Tests {
309 my $x = gsl_complex_rect(0,1);
310 my $z = gsl_complex_coth($x);
311 ok_similar( [ gsl_parts($z) ],
312 [ 0.0, -cos(1)/sin(1) ],
313 'gsl_complex_coth'