1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
|
#
# Perl5 Package for complex numbers
#
# 1994 by David Nadler
# Coding know-how provided by Tom Christiansen, Tim Bunce, and Larry Wall
# sqrt() added by Tom Christiansen; beware should have two roots,
# but only returns one. (use wantarray?)
#
#
# The functions "Re", "Im", and "arg" are provided.
# "~" is used as the conjugation operator and "abs" is overloaded.
#
# Transcendental functions overloaded: so far only sin, cos, and exp.
#
package Math::Complex;
require Exporter;
@ISA = ('Exporter');
# just to make use happy
%OVERLOAD= (
'+' => sub { my($x1,$y1,$x2,$y2) = (@{$_[0]},@{$_[1]});
bless [ $x1+$x2, $y1+$y2];
},
'-' => sub { my($x1,$y1,$x2,$y2) = (@{$_[0]},@{$_[1]});
bless [ $x1-$x2, $y1-$y2];
},
'*' => sub { my($x1,$y1,$x2,$y2) = (@{$_[0]},@{$_[1]});
bless [ $x1*$x2-$y1*$y2,$x1*$y2+$x2*$y1];
},
'/' => sub { my($x1,$y1,$x2,$y2) = (@{$_[0]},@{$_[1]});
my $q = $x2*$x2+$y2*$y2;
bless [($x1*$x2+$y1*$y2)/$q, ($y1*$x2-$y2*$x1)/$q];
},
'neg' => sub { my($x,$y) = @{$_[0]}; bless [ -$x, -$y];
},
'~' => sub { my($x,$y) = @{$_[0]}; bless [ $x, -$y];
},
'abs' => sub { my($x,$y) = @{$_[0]}; sqrt $x*$x+$y*$y;
},
'cos' => sub { my($x,$y) = @{$_[0]};
my ($ab,$c,$s) = (exp $y, cos $x, sin $x);
my $abr = 1/(2*$ab); $ab /= 2;
bless [ ($abr+$ab)*$c, ($abr-$ab)*$s];
},
'sin' => sub { my($x,$y) = @{$_[0]};
my ($ab,$c,$s) = (exp $y, cos $x, sin $x);
my $abr = 1/(2*$ab); $ab /= 2;
bless [ (-$abr-$ab)*$s, ($abr-$ab)*$c];
},
'exp' => sub { my($x,$y) = @{$_[0]};
my ($ab,$c,$s) = (exp $x, cos $y, sin $y);
bless [ $ab*$c, $ab*$s ];
},
'sqrt' => sub {
my($zr,$zi) = @{$_[0]};
my ($x, $y, $r, $w);
my $c = new Math::Complex (0,0);
if (($zr == 0) && ($zi == 0)) {
# nothing, $c already set
}
else {
$x = abs($zr);
$y = abs($zi);
if ($x >= $y) {
$r = $y/$x;
$w = sqrt($x) * sqrt(0.5*(1.0+sqrt(1.0+$r*$r)));
}
else {
$r = $x/$y;
$w = sqrt($y) * sqrt($y) * sqrt(0.5*($r+sqrt(1.0+$r*$r)));
}
if ( $zr >= 0) {
@$c = ($w, $zi/(2 * $w) );
}
else {
$c->[1] = ($zi >= 0) ? $w : -$w;
$c->[0] = $zi/(2.0* $c->[1]);
}
}
return $c;
},
qw("" stringify)
);
sub new {
shift;
my @C = @_;
bless \@C;
}
sub Re {
my($x,$y) = @{$_[0]};
$x;
}
sub Im {
my($x,$y) = @{$_[0]};
$y;
}
sub arg {
my($x,$y) = @{$_[0]};
atan2($y,$x);
}
sub stringify {
my($x,$y) = @{$_[0]};
my($re,$im);
$re = $x if ($x);
if ($y == 1) {$im = 'i';}
elsif ($y == -1){$im = '-i';}
elsif ($y) {$im = "${y}i"; }
local $_ = $re.'+'.$im;
s/\+-/-/;
s/^\+//;
s/[\+-]$//;
$_ = 0 if ($_ eq '');
return $_;
}
|