~ubuntu-branches/ubuntu/precise/rakudo/precise

« back to all changes in this revision

Viewing changes to src/setting/Complex.pm

  • Committer: Bazaar Package Importer
  • Author(s): Alessandro Ghedini
  • Date: 2011-05-17 11:31:09 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20110517113109-rmfir654u1axbpt4
Tags: 0.1~2011.04-1
* New upstream release (Closes: #601862, #585762, #577502)
* New maintainer
* Switch to 3.0 (quilt) format
* Update dependencies (Closes: #584498)
* Update debian/copyright to lastest DEP5 revision
* Do not generate/install perl6 manpage (now done by the build system)
* Enable tests
* Bump Standards-Version to 3.9.2 (no changes needed)
* Do not install extra LICENSE files and duplicated docs
* Remove debian/clean (no more needed)
* Add Vcs-* fields in debian/control
* Rewrite (short) description
* Update upstream copyright years
* Upload to unstable

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
class Complex {
2
 
    has $.re;
3
 
    has $.im;
4
 
 
5
 
    multi method new($re, $im) {
6
 
        self.bless(*, :$re, :$im);
7
 
    }
8
 
 
9
 
    multi method ACCEPTS(Complex $topic) {
10
 
        ($topic.re ~~ $.re) && ($topic.im ~~ $.im);
11
 
    }
12
 
    multi method ACCEPTS($topic) {
13
 
        ($topic.Num ~~ $.re) && ($.im == 0);
14
 
    }
15
 
 
16
 
    multi method abs() {
17
 
        ($!re * $!re + $!im * $!im).sqrt
18
 
    }
19
 
 
20
 
    multi method Complex() { self }
21
 
 
22
 
    multi method perl() {
23
 
        "Complex.new({$.re.perl}, {$.im.perl})";
24
 
    }
25
 
 
26
 
    multi method Str() {
27
 
        "$.re + {$.im}i";
28
 
    }
29
 
 
30
 
    multi method exp() {
31
 
        Complex.new($.re.Num.exp * $.im.Num.cos, $.re.Num.exp * $.im.Num.sin);
32
 
    }
33
 
 
34
 
    multi method sin($base = 'radians') {
35
 
        $.re.sin($base) * $.im.cosh($base) + ($.re.cos($base) * $.im.sinh($base))i;
36
 
    }
37
 
 
38
 
    multi method asin($base = 'radians') {
39
 
        (-1i * log((self)i + sqrt(1 - self * self)))!from-radians($base);
40
 
    }
41
 
 
42
 
    multi method cos($base = 'radians') {
43
 
        $.re.cos($base) * $.im.cosh($base) - ($.re.sin($base) * $.im.sinh($base))i;
44
 
    }
45
 
 
46
 
    multi method acos($base = 'radians') {
47
 
       (pi / 2)!from-radians($base) - self.asin($base);
48
 
    }
49
 
 
50
 
    multi method tan($base = 'radians') {
51
 
        self.sin($base) / self.cos($base);
52
 
    }
53
 
 
54
 
    multi method atan($base = 'radians') {
55
 
        ((log(1 - (self)i) - log(1 + (self)i))i / 2)!from-radians($base);
56
 
    }
57
 
 
58
 
    multi method sec($base = 'radians') {
59
 
        1 / self.cos($base);
60
 
    }
61
 
 
62
 
    multi method asec($base = 'radians') {
63
 
        (1 / self).acos($base);
64
 
    }
65
 
 
66
 
    multi method cosec($base = 'radians') {
67
 
        1 / self.sin($base);
68
 
    }
69
 
 
70
 
    multi method acosec($base = 'radians') {
71
 
        (1 / self).asin($base);
72
 
    }
73
 
 
74
 
    multi method cotan($base = 'radians') {
75
 
        self.cos($base) / self.sin($base);
76
 
    }
77
 
 
78
 
    multi method acotan($base = 'radians') {
79
 
        (1 / self).atan($base);
80
 
    }
81
 
 
82
 
    multi method sinh($base = 'radians') {
83
 
        -((1i * self).sin($base))i;
84
 
    }
85
 
 
86
 
    multi method asinh($base = 'radians') {
87
 
        (self + sqrt(1 + self * self)).log!from-radians($base);
88
 
    }
89
 
 
90
 
    multi method cosh($base = 'radians') {
91
 
        (1i * self).cos($base);
92
 
    }
93
 
 
94
 
    multi method acosh($base = 'radians') {
95
 
        (self + sqrt(self * self - 1)).log!from-radians($base);
96
 
    }
97
 
 
98
 
    multi method tanh($base = 'radians') {
99
 
        -((1i * self).tan($base))i;
100
 
    }
101
 
 
102
 
    multi method atanh($base = 'radians') {
103
 
        (((1 + self) / (1 - self)).log / 2)!from-radians($base);
104
 
    }
105
 
 
106
 
    multi method sech($base = 'radians') {
107
 
        1 / self.cosh($base);
108
 
    }
109
 
 
110
 
    multi method asech($base = 'radians') {
111
 
        (1 / self).acosh($base);
112
 
    }
113
 
 
114
 
    multi method cosech($base = 'radians') {
115
 
        1 / self.sinh($base);
116
 
    }
117
 
 
118
 
    multi method acosech($base = 'radians') {
119
 
        (1 / self).asinh($base);
120
 
    }
121
 
 
122
 
    multi method cotanh($base = 'radians') {
123
 
        1 / self.tanh($base);
124
 
    }
125
 
 
126
 
    multi method acotanh($base = 'radians') {
127
 
        (1 / self).atanh($base);
128
 
    }
129
 
 
130
 
    multi method log() {
131
 
        Q:PIR {
132
 
            $P0 = get_root_namespace ['parrot'; 'Complex' ]
133
 
            $P0 = get_class $P0
134
 
            $P0 = $P0.'new'()
135
 
            $N0 = self.'re'()
136
 
            $P0[0] = $N0
137
 
            $N1 = self.'im'()
138
 
            $P0[1] = $N1
139
 
            $P0 = $P0.'ln'()
140
 
            $N0 = $P0[0]
141
 
            $P2 = box $N0
142
 
            $N1 = $P0[1]
143
 
            $P3 = box $N1
144
 
            $P1 = get_hll_global 'Complex'
145
 
            $P1 = $P1.'new'($P2, $P3)
146
 
            %r  = $P1
147
 
        }
148
 
    }
149
 
 
150
 
    multi method log($base) {
151
 
        $.log / $base.log;
152
 
    }
153
 
 
154
 
    multi method log10() {
155
 
        $.log / 10.log;
156
 
    }
157
 
 
158
 
    multi method polar() {
159
 
        $.abs, atan2($.im, $.re);
160
 
    }
161
 
 
162
 
    multi method roots($n is copy) {
163
 
        my ($mag, $angle) = @.polar;
164
 
        return NaN  if $n < 1;
165
 
        return self if $n == 1;
166
 
        return NaN  if $!re|$!im ~~  Inf|NaN|-Inf;
167
 
        $n = $n.Int;
168
 
        $mag **= 1/$n;
169
 
        (^$n).map: { $mag.unpolar( ($angle + $_ * 2 * pi) / $n) };
170
 
    }
171
 
 
172
 
    multi method sign() {
173
 
        fail('Cannot take the sign() of a Complex number');
174
 
    }
175
 
 
176
 
    multi method sqrt() {
177
 
        Q:PIR {
178
 
            $P0 = get_root_namespace ['parrot'; 'Complex' ]
179
 
            $P0 = get_class $P0
180
 
            $P0 = $P0.'new'()
181
 
            $N0 = self.'re'()
182
 
            $P0[0] = $N0
183
 
            $N1 = self.'im'()
184
 
            $P0[1] = $N1
185
 
            $P0 = $P0.'sqrt'()
186
 
            $N0 = $P0[0]
187
 
            $P2 = box $N0
188
 
            $N1 = $P0[1]
189
 
            $P3 = box $N1
190
 
            $P1 = get_hll_global 'Complex'
191
 
            $P1 = $P1.'new'($P2, $P3)
192
 
            %r  = $P1
193
 
        }
194
 
    }
195
 
 
196
 
    multi method cosec($base = 'radians') {
197
 
        1.0 / self!to-radians($base).sin;
198
 
    }
199
 
 
200
 
    multi method cosech($base = 'radians') {
201
 
        1.0 / self!to-radians($base).sinh;
202
 
    }
203
 
 
204
 
    multi method acosec($base = 'radians') {
205
 
        (1.0 / self).asin!to-radians($base);
206
 
    }
207
 
 
208
 
    multi method cotan($base = 'radians') {
209
 
        1.0 / self!to-radians($base).tan;
210
 
    }
211
 
 
212
 
    multi method cotanh($base = 'radians') {
213
 
        1.0 / self!to-radians($base).tanh;
214
 
    }
215
 
 
216
 
    multi method acotan($base = 'radians') {
217
 
        (1.0 / self).atan!to-radians($base);
218
 
    }
219
 
 
220
 
    multi method acosech($base = 'radians') {
221
 
        (1.0 / self).asinh!to-radians($base);
222
 
    }
223
 
 
224
 
    multi method acotanh($base = 'radians') {
225
 
        (1.0 / self).atanh!to-radians($base);
226
 
    }
227
 
 
228
 
    multi method Num {
229
 
        if $!im == 0 {
230
 
            $!re;
231
 
        } else {
232
 
            fail "You can only coerce a Complex to Num if the imaginary part is zero"
233
 
        }
234
 
    }
235
 
}
236
 
 
237
 
multi sub abs(Complex $x) { $x.abs }
238
 
 
239
 
multi sub infix:<+>(Complex $a, Complex $b) {
240
 
    Complex.new($a.re + $b.re, $a.im + $b.im);
241
 
}
242
 
 
243
 
multi sub infix:<+>(Complex $a, $b) is default {
244
 
    Complex.new($a.re + $b, $a.im);
245
 
}
246
 
 
247
 
multi sub infix:<+>($a, Complex $b) {
248
 
    $b + $a;
249
 
}
250
 
 
251
 
multi sub infix:<->(Complex $a, $b) is default {
252
 
    $a + (-$b);
253
 
}
254
 
 
255
 
multi sub infix:<->($a, Complex $b) {
256
 
    $a + (-$b);
257
 
}
258
 
 
259
 
multi sub infix:<*>(Complex $a, Complex $b) {
260
 
    Complex.new($a.re * $b.re - $a.im * $b.im, $a.im * $b.re + $a.re * $b.im);
261
 
#    Complex.new($a.re * $a.re - $a.im * $b.im, $a.re * $b.im + $a.im * $b.re);
262
 
}
263
 
 
264
 
multi sub infix:<*>(Complex $a, $b) is default {
265
 
    Complex.new($a.re * $b, $a.im * $b);
266
 
 
267
 
}
268
 
 
269
 
multi sub infix:<*>($a, Complex $b) {
270
 
    Complex.new($a * $b.re, $a * $b.im);
271
 
}
272
 
 
273
 
multi sub infix:</>(Complex $a, Complex $b) {
274
 
    my $d = $b.re * $b.re + $b.im * $b.im;
275
 
    Complex.new(($a.re * $b.re + $a.im * $b.im) / $d,
276
 
                ($a.im * $b.re - $a.re * $b.im) / $d);
277
 
}
278
 
 
279
 
multi sub infix:</>(Complex $a, $b) is default {
280
 
    $a * (1/$b);
281
 
}
282
 
 
283
 
multi sub infix:</>($a, Complex $b) {
284
 
    Complex.new($a, 0) / $b;
285
 
}
286
 
 
287
 
multi sub postfix:<i>($x) {
288
 
    Complex.new(0, +$x);
289
 
}
290
 
 
291
 
multi sub postfix:<i>(Complex $z) {
292
 
    Complex.new(-$z.im, $z.re);
293
 
}
294
 
 
295
 
multi sub prefix:<->(Complex $a) {
296
 
    Complex.new(-$a.re, -$a.im);
297
 
}
298
 
 
299
 
multi sub infix:<**>(Complex $a, $b) is default {
300
 
    ($a.log * $b).exp;
301
 
}
302
 
 
303
 
multi sub infix:<**>($a, Complex $b) {
304
 
    ($a.log * $b).exp;
305
 
}
306
 
 
307
 
multi sub sign(Complex $x) { $x.sign }
308
 
 
309
 
multi sub sqrt(Complex $x) {
310
 
    $x.sqrt;
311
 
}
312
 
 
313
 
multi sub exp(Complex $x) {
314
 
    $x.exp()
315
 
}
316
 
 
317
 
# vim: ft=perl6