OOP initial commit with new traces.
[Faustine.git] / dsp_files / mrfaustlib / complex.lib
1 import ( "math.lib" );
2 square = _ <: *;
3
4 // Complex in cartesian coordinates
5 carte_cplx = _, _: (_, 1 : vectorize), (_, 1 : vectorize) : #;
6
7 // Cartesian complex : basic operations
8 ccplx_real = _, 0 : [];
9 ccplx_imag = _, 1 : [];
10 ccplx_conj = _ <: ccplx_real, ccplx_imag : _, (0, _ : -) : carte_cplx;
11 ccplx_modul2 = _ <: ccplx_real, ccplx_imag : square, square : +;
12 ccplx_modul = ccplx_modul2 : sqrt;
13
14 //real_not_zero = _ <: (ccplx_imag, ccplx_real : / : atan);
15 //real_compare = _ <: (ccplx_real, 0 : >), (ccplx_real, 0 : <) : + <: _, (-(1), 0 : <);
16 //real_zero = _ <: (ccplx_imag, 0 : > : *(PI) : /(2)), (ccplx_imag, 0 : < : _, (0, 1 : -) : * : *(PI) : /(2)) : +;
17 //ccplx_angle = _ <: (real_not_zero, real_compare, real_zero : *, * : +), (ccplx_real, 0 : < : _, PI : *) : +;
18 ccplx_angle = _ <: ccplx_imag, ccplx_real : atan2;
19
20 ccplx_div_real = _, _ <: ccplx_real, _, ccplx_imag, _ : /, / : carte_cplx;
21
22 // cartesian complex : add, sub, mul and div
23 ccplx_add = +;
24 ccplx_sub = -;
25
26 ccplx_mul = _, _ <: ccplx_real, ccplx_real, ccplx_imag, ccplx_imag,ccplx_real, ccplx_imag, ccplx_imag, ccplx_real : *, *, *, * : -, + : carte_cplx;
27
28 ccplx_div = _, _ <: ccplx_real, ccplx_real, ccplx_imag, ccplx_imag, ccplx_imag, ccplx_real, ccplx_real, ccplx_imag, !, ccplx_real, !, ccplx_imag : *, *, *, *, square, square : +, -, + : carte_cplx, _ : ccplx_div_real;
29 //ccplx_div = _, _ <: _, ccplx_conj, !, _ : ccplx_mul, ccplx_modul2 : ccplx_div_real;
30
31 // Complex in polar coordinates
32 polar_cplx = _, _: (_, 1 : vectorize), (_, 1 : vectorize) : #;
33 pcplx_modul = _, 0 : [];
34 pcplx_angle = _, 1 : [];
35 pcplx_real = _ <: pcplx_modul, (pcplx_angle : cos) : *;
36 pcplx_imag = _ <: pcplx_modul, (pcplx_angle : sin) : *;
37
38 // conversion between cartesian complex and polar complex
39 sca_abs = _ <: _, (>(0) : *(2) : -(1)) : *;
40 sca2pcplx = _ <: sca_abs, (<(0) : *(PI)) : polar_cplx;
41 polar2carte = _ <: pcplx_real, pcplx_imag : carte_cplx;
42 carte2polar = _ <: ccplx_modul, ccplx_angle : polar_cplx;
43
44 // Polar complex : add, sub, mul and div
45 pcplx_add = _, _ : polar2carte, polar2carte : ccplx_add : carte2polar;
46 pcplx_sub = _, _ : polar2carte, polar2carte : ccplx_sub : carte2polar;
47 pcplx_mul = _, _ <: (pcplx_modul, pcplx_modul : *), (pcplx_angle, pcplx_angle : +) : polar_cplx;
48
49 pcplx_div = _, _ <: (pcplx_modul, pcplx_modul : /), (pcplx_angle, pcplx_angle : -) : polar_cplx;
50
51 //functions used in FFT
52 real_pcplx_mul = _, ( _ <: pcplx_modul, pcplx_angle) : *, _ : polar_cplx;
53 pcplx_moduls(n) = par(i, n, pcplx_modul);
54
55