7 # just to make use happy
10 '+' => sub { my($x1,$y1,$x2,$y2) = (@{$_[0]},@{$_[1]});
11 bless [ $x1+$x2, $y1+$y2];
14 '-' => sub { my($x1,$y1,$x2,$y2) = (@{$_[0]},@{$_[1]});
15 bless [ $x1-$x2, $y1-$y2];
18 '*' => sub { my($x1,$y1,$x2,$y2) = (@{$_[0]},@{$_[1]});
19 bless [ $x1*$x2-$y1*$y2,$x1*$y2+$x2*$y1];
22 '/' => sub { my($x1,$y1,$x2,$y2) = (@{$_[0]},@{$_[1]});
23 my $q = $x2*$x2+$y2*$y2;
24 bless [($x1*$x2+$y1*$y2)/$q, ($y1*$x2-$y2*$x1)/$q];
27 'neg' => sub { my($x,$y) = @{$_[0]}; bless [ -$x, -$y];
30 '~' => sub { my($x,$y) = @{$_[0]}; bless [ $x, -$y];
33 'abs' => sub { my($x,$y) = @{$_[0]}; sqrt $x*$x+$y*$y;
36 'cos' => sub { my($x,$y) = @{$_[0]};
37 my ($ab,$c,$s) = (exp $y, cos $x, sin $x);
38 my $abr = 1/(2*$ab); $ab /= 2;
39 bless [ ($abr+$ab)*$c, ($abr-$ab)*$s];
42 'sin' => sub { my($x,$y) = @{$_[0]};
43 my ($ab,$c,$s) = (exp $y, cos $x, sin $x);
44 my $abr = 1/(2*$ab); $ab /= 2;
45 bless [ (-$abr-$ab)*$s, ($abr-$ab)*$c];
48 'exp' => sub { my($x,$y) = @{$_[0]};
49 my ($ab,$c,$s) = (exp $x, cos $y, sin $y);
50 bless [ $ab*$c, $ab*$s ];
54 my($zr,$zi) = @{$_[0]};
56 my $c = new Math::Complex (0,0);
57 if (($zr == 0) && ($zi == 0)) {
58 # nothing, $c already set
65 $w = sqrt($x) * sqrt(0.5*(1.0+sqrt(1.0+$r*$r)));
69 $w = sqrt($y) * sqrt($y) * sqrt(0.5*($r+sqrt(1.0+$r*$r)));
72 @$c = ($w, $zi/(2 * $w) );
75 $c->[1] = ($zi >= 0) ? $w : -$w;
76 $c->[0] = $zi/(2.0* $c->[1]);
102 my($x,$y) = @{$_[0]};
107 my($x,$y) = @{$_[0]};
111 if ($y == 1) {$im = 'i';}
112 elsif ($y == -1){$im = '-i';}
113 elsif ($y) {$im = "${y}i"; }
115 local $_ = $re.'+'.$im;
119 $_ = 0 if ($_ eq '');
128 Math::Complex - complex numbers package
133 $i = new Math::Complex;
137 Complex numbers declared as
139 $i = Math::Complex->new(1,1);
141 can be manipulated with overloaded math operators. The operators
143 + - * / neg ~ abs cos sin exp sqrt
145 are supported as well as
157 sqrt() should return two roots, but only returns one.
161 Dave Nadler, Tom Christiansen, Tim Bunce, Larry Wall.