Skip to main content

abels_complex/complex/
rectangular.rs

1use core::ops::*;
2pub type Complex32 = Complex<f32>;
3pub type Complex64 = Complex<f64>;
4use crate::traits::Number;
5
6use super::ComplexPolar as Polar;
7
8/// Creates a complex number in rectangular form.
9#[inline(always)]
10#[must_use]
11pub const fn complex<FT>(re: FT, im: FT) -> Complex<FT> {
12    Complex::new(re, im)
13}
14
15/// A complex number in rectangular form.
16#[derive(Clone, Copy, PartialEq, Debug, Default)]
17#[repr(C)]
18pub struct Complex<FT> {
19    pub re: FT,
20    pub im: FT,
21}
22impl<FT> Complex<FT> {
23    /// Creates a complex number.
24    pub const fn new(re: FT, im: FT) -> Self {
25        Self { re, im }
26    }
27}
28
29impl<FT: Number> Complex<FT> {
30    pub const ZERO: Self = Self::new(FT::ZERO, FT::ZERO);
31    pub const ONE: Self = Self::new(FT::ONE, FT::ZERO);
32    pub const I: Self = Self::new(FT::ZERO, FT::ONE);
33
34    /// Computes the conjugate.
35    pub fn conjugate(self) -> Self {
36        Self::new(self.re, -self.im)
37    }
38
39    /// Computes the absolute value.
40    pub fn abs(self) -> FT {
41        self.abs_sq().sqrt()
42    }
43
44    pub fn square(mut self) -> Self {
45        let two = FT::ONE + FT::ONE;
46        let re = self.re * self.re - self.im * self.im;
47        self.im = self.re * self.im * two;
48        self.re = re;
49        self
50    }
51
52    /// Computes the squared absolute value.
53    ///
54    /// This is faster than `abs()` as it avoids a square root operation.
55    pub fn abs_sq(self) -> FT {
56        self.re * self.re + self.im * self.im
57    }
58
59    /// Computes the argument in the range `(-π, +π]`.
60    pub fn arg(self) -> FT {
61        self.im.atan2(self.re)
62    }
63
64    /// Computes the reciprocal.
65    pub fn recip(self) -> Self {
66        self.conjugate() / self.abs_sq()
67    }
68
69    /// Convert to polar form.
70    pub fn to_polar(self) -> Polar<FT> {
71        Polar::new(self.abs(), self.arg())
72    }
73
74    /// Computes `e^self` where `e` is the base of the natural logarithm.
75    pub fn exp(self) -> Polar<FT> {
76        Polar::new(self.re.exp(), self.im)
77    }
78
79    /// Computes `2^self`.
80    pub fn exp2(self) -> Polar<FT> {
81        Polar::new(self.re.exp2(), self.im * FT::LN_2())
82    }
83
84    /// Computes the principle natural logarithm.
85    pub fn ln(self) -> Self {
86        self.to_polar().ln()
87    }
88
89    /// Computes the principle logarithm in base 2.
90    pub fn log2(self) -> Self {
91        self.ln() / FT::LN_2()
92    }
93
94    /// Computes the principle logarithm in base 10.
95    pub fn log10(self) -> Self {
96        self.ln() / FT::LN_10()
97    }
98
99    /// Raises `self` to an integer power.
100    pub fn powi(self, n: i32) -> Polar<FT> {
101        self.to_polar().powi(n)
102    }
103
104    /// Raises `self` to a floating point power.
105    pub fn powf(self, x: FT) -> Polar<FT> {
106        self.to_polar().powf(x)
107    }
108
109    /// Computes the principle square root.
110    pub fn sqrt(self) -> Self {
111        let two = FT::ONE + FT::ONE;
112        let abs = self.abs();
113        Self::new(
114            ((abs + self.re) / two).sqrt(),
115            ((abs - self.re) / two).sqrt().copysign(self.im),
116        )
117    }
118
119    /// Computes the euclidian distance between two points.
120    pub fn distance(self, other: Self) -> FT {
121        (self - other).abs()
122    }
123
124    /// Computes the squared euclidian distance between two points.
125    pub fn distance_squared(self, other: Self) -> FT {
126        (self - other).abs_sq()
127    }
128
129    /// Computes the linear interpolation between two points based on the value `t`.
130    pub fn lerp(self, other: Self, t: FT) -> Self {
131        self + (other - self) * t
132    }
133}
134
135impl<FT: Number> Add for Complex<FT> {
136    type Output = Self;
137    fn add(self, other: Self) -> Self::Output {
138        Complex::new(self.re + other.re, self.im + other.im)
139    }
140}
141
142impl<FT: Number> Add<FT> for Complex<FT> {
143    type Output = Self;
144    fn add(self, re: FT) -> Self::Output {
145        Complex::new(self.re + re, self.im)
146    }
147}
148
149impl<FT: Number> AddAssign for Complex<FT> {
150    fn add_assign(&mut self, other: Self) {
151        self.re += other.re;
152        self.im += other.im;
153    }
154}
155
156impl<FT: Number> AddAssign<FT> for Complex<FT> {
157    fn add_assign(&mut self, re: FT) {
158        self.re += re;
159    }
160}
161
162impl<FT: Number> Sub for Complex<FT> {
163    type Output = Self;
164    fn sub(self, other: Self) -> Self::Output {
165        Complex::new(self.re - other.re, self.im - other.im)
166    }
167}
168
169impl<FT: Number> Sub<FT> for Complex<FT> {
170    type Output = Self;
171    fn sub(self, re: FT) -> Self::Output {
172        Complex::new(self.re - re, self.im)
173    }
174}
175
176impl<FT: Number> SubAssign for Complex<FT> {
177    fn sub_assign(&mut self, other: Self) {
178        self.re -= other.re;
179        self.im -= other.im;
180    }
181}
182
183impl<FT: Number> SubAssign<FT> for Complex<FT> {
184    fn sub_assign(&mut self, re: FT) {
185        self.re -= re;
186    }
187}
188
189impl<FT: Number> Mul for Complex<FT> {
190    type Output = Self;
191    fn mul(mut self, other: Self) -> Self {
192        self *= other;
193        self
194    }
195}
196
197impl<FT: Number> Mul<FT> for Complex<FT> {
198    type Output = Self;
199    fn mul(self, re: FT) -> Self {
200        Complex::new(self.re * re, self.im * re)
201    }
202}
203
204impl<FT: Number> MulAssign for Complex<FT> {
205    fn mul_assign(&mut self, other: Self) {
206        let re = self.re * other.re - self.im * other.im;
207        self.im = self.re * other.im + self.im * other.re;
208        self.re = re;
209    }
210}
211
212impl<FT: Number> MulAssign<FT> for Complex<FT> {
213    fn mul_assign(&mut self, re: FT) {
214        self.re *= re;
215        self.im *= re;
216    }
217}
218
219impl<FT: Number> Div for Complex<FT> {
220    type Output = Self;
221    fn div(self, other: Self) -> Self::Output {
222        self * other.recip()
223    }
224}
225
226impl<FT: Number> Div<FT> for Complex<FT> {
227    type Output = Self;
228    fn div(self, re: FT) -> Self::Output {
229        Complex::new(self.re / re, self.im / re)
230    }
231}
232
233impl<FT: Number> DivAssign for Complex<FT> {
234    fn div_assign(&mut self, other: Self) {
235        *self = *self / other;
236    }
237}
238
239impl<FT: Number> DivAssign<FT> for Complex<FT> {
240    fn div_assign(&mut self, re: FT) {
241        self.re /= re;
242        self.im /= re;
243    }
244}
245
246impl<FT: Number> Neg for Complex<FT> {
247    type Output = Self;
248    fn neg(self) -> Self::Output {
249        Self::new(-self.re, -self.im)
250    }
251}
252
253impl<FT: Number> From<FT> for Complex<FT> {
254    fn from(value: FT) -> Self {
255        Self::new(value, FT::ZERO)
256    }
257}
258
259#[cfg(feature = "approx")]
260use approx::{AbsDiffEq, RelativeEq, UlpsEq};
261
262#[cfg(feature = "approx")]
263impl<FT: AbsDiffEq + Copy> AbsDiffEq for Complex<FT>
264where
265    <FT as AbsDiffEq>::Epsilon: Copy,
266{
267    type Epsilon = <FT as AbsDiffEq>::Epsilon;
268    fn default_epsilon() -> Self::Epsilon {
269        FT::default_epsilon()
270    }
271    fn abs_diff_eq(&self, other: &Self, epsilon: Self::Epsilon) -> bool {
272        FT::abs_diff_eq(&self.re, &other.re, epsilon)
273            && FT::abs_diff_eq(&self.im, &other.im, epsilon)
274    }
275}
276
277#[cfg(feature = "approx")]
278impl<FT: RelativeEq + Copy> RelativeEq for Complex<FT>
279where
280    <FT as AbsDiffEq>::Epsilon: Copy,
281{
282    fn default_max_relative() -> Self::Epsilon {
283        FT::default_max_relative()
284    }
285    fn relative_eq(
286        &self,
287        other: &Self,
288        epsilon: Self::Epsilon,
289        max_relative: Self::Epsilon,
290    ) -> bool {
291        FT::relative_eq(&self.re, &other.re, epsilon, max_relative)
292            && FT::relative_eq(&self.im, &other.im, epsilon, max_relative)
293    }
294}
295
296#[cfg(feature = "approx")]
297impl<FT: UlpsEq + Copy> UlpsEq for Complex<FT>
298where
299    <FT as AbsDiffEq>::Epsilon: Copy,
300{
301    fn default_max_ulps() -> u32 {
302        FT::default_max_ulps()
303    }
304    fn ulps_eq(&self, other: &Self, epsilon: Self::Epsilon, max_ulps: u32) -> bool {
305        FT::ulps_eq(&self.re, &other.re, epsilon, max_ulps)
306            && FT::ulps_eq(&self.im, &other.im, epsilon, max_ulps)
307    }
308}