Debian Codegraph @

src/test/01-Basic/173-complex-polar.ck

raw
1 // adapted from examples/type/type_analysis.ck
2
3 // print complex
4 <<< #(1,1) >>>;
5 // print polar
6 <<< %(1,2*pi) >>>;
7
8 // cast int to complex
9 <<< 1 $ complex >>>;
10 // cast int to polar
11 <<< 2 $ polar >>>;
12 // cast float to complex
13 <<< 3.0 $ complex >>>;
14 // cast float to polar
15 <<< 4.0 $ polar >>>;
16
17 // cast complex to polar
18 <<< "cast:", #(10,10) $ polar >>>;
19 // cast polar to complex
20 <<< "cast:", %(1,pi/2) $ complex >>>;
21 // cast to and to
22 <<< "cast:", #(5,5) $ polar $ complex >>>;
23
24 // store in variable
25 #(1,2.0) => complex c;
26 // print
27 <<< c >>>;
28 // store in variable
29 %(3.0,4) => polar p;
30 // print
31 <<< p >>>;
32
33 // add
34 <<< #(1,1) + #(1,1) >>>;
35 <<< %(1,0) + 3*%(1,1) >>>;
36 // minus
37 <<< #(1,1) - #(.5,-1) >>>;
38 <<< %(1,1) - %(.5,0) >>>;
39 // times
40 <<< #(1,2) * #(3,4) >>>;
41 <<< %(2,.25*pi) * %(2,.35*pi) >>>;
42 // divide
43 <<< #(2,2) / #(1,1) >>>;
44 <<< %(5,.25*pi) / %(4,.5*pi) >>>;
45
46 // add assign
47 #(1,1) => complex foo;
48 #(2,3) +=> foo; <<< foo >>>;
49 #(2,3) -=> foo; <<< foo >>>;
50 #(0,5) *=> foo; <<< foo >>>;
51 #(0,5) /=> foo; <<< foo >>>;
52 %(2,pi) => polar bar;
53 %(3,pi) +=> bar; <<< bar >>>;
54 %(-1,0) -=> bar; <<< bar >>>;
55 %(2,pi) *=> bar; <<< bar >>>;
56 %(2,pi) /=> bar; <<< bar >>>;
57
58 // constant
59 <<< "Math.j:", Math.j >>>;
60
61 // math
62 <<< Math.re( #(1,2) ) >>>;
63 <<< Math.im( #(1,2) ) >>>;
64 <<< Math.mag( %(2,pi) ) >>>;
65 <<< Math.phase( %(2,pi) ) >>>;
66
67 // array
68 complex arr[10];
69 <<< arr.size() >>>;
70 <<< arr[0] >>>;
71 #(10, 11) => arr[1];
72 <<< arr[1] >>>;
73
74 polar brr[10];
75 <<< brr.size() >>>;
76 <<< brr[0] >>>;
77 %(5,pi) => brr[1];
78 <<< brr[1] >>>;
79 <<< Math.mag( brr[1] ) >>>;
80
81 // components
82 <<< #(1,2).re, #(1,2).im >>>;
83 <<< %(3,pi).mag, %(3,pi).phase >>>;
84 <<< foo.re, foo.im >>>;
85 5 => foo.re => foo.im;
86 <<< foo, foo.re, foo.im >>>;
87
88 // arrays
89 [ #(1,2), #(3,4), #(5,6) ] @=> complex cs[];
90 polar ps[cs.size()];
91 // convert
92 Math.rtop( cs, ps );
93 <<< ps[0], ps[1], ps[2] >>>;
94 Math.ptor( ps, cs );
95 <<< cs[0], cs[1], cs[2] >>>;