annotate complex.yeti @ 46:00b604d7faf9
Use complex class for output of FFT
author |
Chris Cannam |
date |
Sat, 29 Dec 2012 14:36:13 +0000 |
parents |
1e7430a95926 |
children |
f97abcda094f |
rev |
line source |
Chris@44
|
1
|
Chris@44
|
2 module complex;
|
Chris@44
|
3
|
Chris@46
|
4 import java.lang: ClassCastException;
|
Chris@46
|
5
|
Chris@45
|
6 class Complex(double real, double imag)
|
Chris@44
|
7 int getReal()
|
Chris@44
|
8 real,
|
Chris@44
|
9 int getImag()
|
Chris@44
|
10 imag,
|
Chris@44
|
11 String toString()
|
Chris@44
|
12 "\(real) + \(imag)i",
|
Chris@46
|
13 int hashCode()
|
Chris@46
|
14 Double#valueOf(real)#hashCode() + Double#valueOf(imag)#hashCode(),
|
Chris@46
|
15 boolean equals(Object other)
|
Chris@46
|
16 try
|
Chris@46
|
17 c = other unsafely_as ~Complex;
|
Chris@46
|
18 c#getReal() == real and c#getImag() == imag
|
Chris@46
|
19 catch ClassCastException:
|
Chris@46
|
20 false
|
Chris@46
|
21 yrt,
|
Chris@44
|
22 end;
|
Chris@44
|
23
|
Chris@44
|
24 typedef opaque cplx = ~Complex;
|
Chris@44
|
25
|
Chris@46
|
26 real c1 is ~Complex -> number =
|
Chris@46
|
27 c1#getReal();
|
Chris@46
|
28
|
Chris@46
|
29 imaginary c1 is ~Complex -> number =
|
Chris@46
|
30 c1#getImag();
|
Chris@46
|
31
|
Chris@46
|
32 complex re im is number -> number -> ~Complex =
|
Chris@46
|
33 new Complex(re, im);
|
Chris@46
|
34
|
Chris@46
|
35 add c1 c2 is ~Complex -> ~Complex -> ~Complex =
|
Chris@46
|
36 complex (real c1 + real c2) (imaginary c1 + imaginary c2);
|
Chris@46
|
37
|
Chris@46
|
38 scale r c is number -> ~Complex -> ~Complex =
|
Chris@46
|
39 complex (r * real c) (r * imaginary c);
|
Chris@44
|
40
|
Chris@44
|
41 {
|
Chris@46
|
42 real,
|
Chris@46
|
43 imaginary,
|
Chris@46
|
44 complex,
|
Chris@46
|
45 add,
|
Chris@46
|
46 scale,
|
Chris@46
|
47 i = complex 0 1,
|
Chris@46
|
48 one = complex 1 0,
|
Chris@46
|
49 zero = complex 0 0,
|
Chris@44
|
50 } as {
|
Chris@46
|
51 real is cplx -> number,
|
Chris@46
|
52 imaginary is cplx -> number,
|
Chris@46
|
53 complex is number -> number -> cplx,
|
Chris@46
|
54 add is cplx -> cplx -> cplx,
|
Chris@46
|
55 scale is number -> cplx -> cplx,
|
Chris@46
|
56 i is cplx,
|
Chris@46
|
57 one is cplx,
|
Chris@46
|
58 zero is cplx,
|
Chris@44
|
59 }
|
Chris@44
|
60
|