easy_complex/
ops.rs

1//! Generates all the operations needed
2
3macro_rules! add_op {
4	($float:ty, Complex) => {
5		impl<A> common::ops::Add<A> for Complex<$float>
6			where A: Into<Complex<$float>>
7		{
8			type Output = Complex<$float>;
9
10			fn add(self, other: A) -> Complex<$float> {
11				let c: Complex<$float> = other.into();
12				Self {
13					real: self.real + c.real,
14					imag: self.imag + c.imag,
15				}
16			}
17		}
18	};
19
20	($float:ty, EComplex) => {
21		impl<A> common::ops::Add<A> for EComplex<$float>
22			where A: Into<Complex<$float>>
23		{
24			type Output = Self;
25
26			fn add(self, other: A) -> Self {
27				let c: Complex<$float> = other.into();
28				let tmp: Complex<$float> = self.into();
29
30				let new: Complex<$float> = tmp + c;
31
32				new.into()
33			}
34		}
35	};
36}
37
38macro_rules! sub_op {
39	($float:ty, Complex) => (
40		impl<A> common::ops::Sub<A> for Complex<$float>
41			where A: Into<Complex<$float>>
42		{
43			type Output = Complex<$float>;
44
45			fn sub(self, other: A) -> Complex<$float> {
46				let c: Complex<$float> = other.into();
47				Self {
48					real: self.real - c.real,
49					imag: self.imag - c.imag,
50				}
51			}
52		}
53	);
54
55	($float:ty, EComplex) => {
56		impl<A> common::ops::Sub<A> for EComplex<$float>
57			where A: Into<Complex<$float>>
58		{
59			type Output = Self;
60
61			fn sub(self, other: A) -> Self {
62				let c: Complex<$float> = other.into();
63
64				let tmp: Complex<$float> = self.into();
65
66				let new: Complex<$float> = tmp + c;
67
68				new.into()
69			}
70		}
71	};
72}
73
74macro_rules! mul_op {
75	($float:ty, Complex) => (
76		impl<A> common::ops::Mul<A> for Complex<$float>
77			where A: Into<Complex<$float>>
78		{
79			type Output = Complex<$float>;
80
81			fn mul(self, other: A) -> Complex<$float> {
82				let c: Complex<$float> = other.into();
83				Self {
84					real: self.real * c.real - self.imag * c.imag,
85					imag: self.imag * c.real + self.real * c.imag,
86				}
87			}
88		}
89	);
90
91	($float:ty, EComplex) => {
92		impl<A> common::ops::Mul<A> for EComplex<$float>
93			where A: Into<EComplex<$float>>
94		{
95			type Output = Self;
96
97			fn mul(self, other: A) -> Self {
98				let c: Self = other.into();
99
100				Self {
101					module: self.module * c.module,
102					arg: self.arg + c.arg,
103				}
104			}
105		}
106	};
107}
108
109macro_rules! div_op {
110	($float:ty, Complex) => (
111		impl<A> common::ops::Div<A> for Complex<$float>
112			where A: Into<Complex<$float>>
113		{
114			type Output = Complex<$float>;
115
116			fn div(self, other: A) -> Complex<$float> {
117				let c: Complex<$float> = other.into();
118				Self {
119					real: (self.real*c.real + self.imag*c.imag) / (c.real*c.real + c.imag*c.imag),
120					imag: (self.imag*c.real - self.real*c.imag) / (c.real*c.real + c.imag*c.imag),
121				}
122			}
123		}
124	);
125
126	($float:ty, EComplex) => {
127		impl<A> common::ops::Div<A> for EComplex<$float>
128			where A: Into<EComplex<$float>>
129		{
130			type Output = Self;
131
132			fn div(self, other: A) -> Self {
133				let c: Self = other.into();
134
135				Self {
136					module: self.module / c.module,
137					arg: self.arg - c.arg,
138				}
139			}
140		}
141	};
142}
143
144macro_rules! neg_op {
145	($float:ty, Complex) => (
146		impl common::ops::Neg for Complex<$float> {
147			type Output = Complex<$float>;
148
149			fn neg(self) -> Complex<$float> {
150				Self {
151					real: -self.real,
152					imag: -self.imag,
153				}
154			}
155		}
156	);
157
158	($float:ty, EComplex) => {
159		impl common::ops::Neg for EComplex<$float> {
160			type Output = EComplex<$float>;
161
162			fn neg(self) -> EComplex<$float> {
163				let mut narg: $float = self.arg;
164
165				if self.arg.is_sign_positive() {
166					narg -= <$float>::from(common::f32::consts::PI)
167				} else {
168					narg += <$float>::from(common::f32::consts::PI)
169				};
170			
171				Self {
172					module: self.module,
173					arg: narg,
174				}
175			}
176		}
177	};
178}
179
180
181macro_rules! gen_ops {
182	( $float:ty, $ctype:ident) => (
183		add_op!($float, $ctype);
184		sub_op!($float, $ctype);
185		mul_op!($float, $ctype);
186		div_op!($float, $ctype);
187		neg_op!($float, $ctype);
188	);
189}