abels_complex/complex/
polar.rs

1pub type ComplexPolar32 = ComplexPolar<f32>;
2pub type ComplexPolar64 = ComplexPolar<f64>;
3use crate::traits::Number;
4use core::fmt;
5
6use super::Complex as Rectangular;
7use core::ops::*;
8
9/// Creates a complex number in polar form.
10#[inline(always)]
11#[must_use]
12pub const fn complex_polar<FT>(abs: FT, arg: FT) -> ComplexPolar<FT> {
13    ComplexPolar::new(abs, arg)
14}
15
16/// A complex number in polar form.
17#[derive(Clone, Copy, PartialEq, Debug, Default)]
18#[repr(C)]
19pub struct ComplexPolar<FT> {
20    pub abs: FT,
21    pub arg: FT,
22}
23
24impl<FT> ComplexPolar<FT> {
25    /// Creates a complex number.
26    pub const fn new(abs: FT, arg: FT) -> Self {
27        Self { abs, arg }
28    }
29}
30
31impl<FT: Number> ComplexPolar<FT> {
32    pub const ZERO: Self = Self::new(FT::ZERO, FT::ZERO);
33    pub const ONE: Self = Self::new(FT::ONE, FT::ZERO);
34
35    /// Computes the conjugate.                        
36    pub fn conjugate(self) -> Self {
37        Self::new(self.abs, -self.arg)
38    }
39
40    /// Computes the real component.
41    pub fn re(self) -> FT {
42        self.abs * self.arg.cos()
43    }
44
45    /// Computes the imaginary component.
46    pub fn im(self) -> FT {
47        self.abs * self.arg.sin()
48    }
49
50    /// Computes the squared absolute value.
51    pub fn abs_sq(self) -> FT {
52        self.abs * self.abs
53    }
54
55    /// Computes the reciprocal.
56    pub fn recip(self) -> Self {
57        Self::new(self.abs.recip(), -self.arg)
58    }
59
60    /// Computes the principle square root.
61    pub fn sqrt(self) -> Self {
62        let two = FT::ONE + FT::ONE;
63        Self::new(self.abs.sqrt(), self.arg / two)
64    }
65
66    /// Convert to rectangular form.
67    pub fn to_rectangular(self) -> Rectangular<FT> {
68        let (sin, cos) = self.arg.sin_cos();
69        Rectangular::new(cos, sin) * self.abs
70    }
71
72    /// Computes `e^self` where `e` is the base of the natural logarithm.
73    pub fn exp(self) -> Self {
74        self.to_rectangular().exp()
75    }
76
77    /// Computes the principle natural logarithm.
78    pub fn ln(self) -> Rectangular<FT> {
79        Rectangular::new(self.abs.ln(), self.arg)
80    }
81
82    /// Computes the principle logarithm in base 2.
83    pub fn log2(self) -> Rectangular<FT> {
84        self.ln() / FT::LN_2()
85    }
86
87    /// Computes the principle logarithm in base 10.
88    pub fn log10(self) -> Rectangular<FT> {
89        self.ln() / FT::LN_10()
90    }
91
92    /// Raises `self` to a floating point power.
93    pub fn powf(self, x: FT) -> Self {
94        if x < FT::ZERO && self.abs == FT::ZERO {
95            return Self::ZERO;
96        }
97        Self::new(self.abs.powf(x), self.arg * x)
98    }
99
100    /// Raises `self` to an integer power.
101    pub fn powi(self, n: i32) -> Self {
102        if n < 0 && self.abs == FT::ZERO {
103            return Self::ZERO;
104        }
105        Self::new(self.abs.powi(n), self.arg * FT::from_i32(n))
106    }
107
108    /// Normalizes the absolute value and the argument into the range `[0, ∞)` and `(-π, +π]` respectively.
109    pub fn normalize(mut self) -> Self {
110        self.arg = self.arg.rem_euclid(&FT::TAU());
111        if self.abs < FT::ZERO {
112            self.abs = -self.abs;
113            if self.arg <= FT::ZERO {
114                self.arg += FT::PI();
115            } else {
116                self.arg -= FT::PI();
117            }
118        } else {
119            if self.arg > FT::PI() {
120                self.arg -= FT::TAU();
121            } else if self.arg <= -FT::PI() {
122                self.arg += FT::TAU();
123            }
124        }
125        self
126    }
127}
128
129impl<FT: Number> Mul for ComplexPolar<FT> {
130    type Output = Self;
131    fn mul(mut self, other: Self) -> Self {
132        self *= other;
133        self
134    }
135}
136
137impl<FT: Number> Mul<FT> for ComplexPolar<FT> {
138    type Output = Self;
139    fn mul(mut self, re: FT) -> Self::Output {
140        self *= re;
141        self
142    }
143}
144
145impl<FT: Number> MulAssign for ComplexPolar<FT> {
146    fn mul_assign(&mut self, other: Self) {
147        self.abs *= other.abs;
148        self.arg += other.arg;
149    }
150}
151
152impl<FT: Number> MulAssign<FT> for ComplexPolar<FT> {
153    fn mul_assign(&mut self, re: FT) {
154        self.abs *= re;
155    }
156}
157
158impl<FT: Number> Div for ComplexPolar<FT> {
159    type Output = Self;
160    fn div(mut self, other: Self) -> Self {
161        self /= other;
162        self
163    }
164}
165
166impl<FT: Number> Div<FT> for ComplexPolar<FT> {
167    type Output = Self;
168    fn div(mut self, re: FT) -> Self {
169        self /= re;
170        self
171    }
172}
173
174impl<FT: Number> DivAssign for ComplexPolar<FT> {
175    fn div_assign(&mut self, other: Self) {
176        *self *= other.recip();
177    }
178}
179
180impl<FT: Number> DivAssign<FT> for ComplexPolar<FT> {
181    fn div_assign(&mut self, re: FT) {
182        self.abs /= re;
183    }
184}
185
186impl<FT: Number> Neg for ComplexPolar<FT> {
187    type Output = Self;
188    fn neg(mut self) -> Self {
189        self.abs = -self.abs;
190        self
191    }
192}
193
194impl<FT: Number + fmt::Display> fmt::Display for ComplexPolar<FT> {
195    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
196        fn fmt_x<FT: fmt::Display>(f: &mut fmt::Formatter, x: FT) -> fmt::Result {
197            if let Some(p) = f.precision() {
198                write!(f, "{x:.*}", p)
199            } else {
200                write!(f, "{x}")
201            }
202        }
203        let pi_radians = self.arg / FT::PI();
204        fmt_x(f, self.abs)?;
205        if pi_radians == FT::ZERO || self.abs == FT::ZERO {
206            Ok(())
207        } else if pi_radians == FT::ONE {
208            write!(f, "e^iπ")
209        } else {
210            write!(f, "e^")?;
211            fmt_x(f, pi_radians)?;
212            write!(f, "iπ")
213        }
214    }
215}
216
217impl<FT: Number> From<FT> for ComplexPolar<FT> {
218    fn from(value: FT) -> Self {
219        Self::new(value, FT::ZERO)
220    }
221}
222
223#[cfg(feature = "approx")]
224use approx::{AbsDiffEq, RelativeEq, UlpsEq};
225
226#[cfg(feature = "approx")]
227impl<FT: AbsDiffEq + Copy> AbsDiffEq for ComplexPolar<FT>
228where
229    <FT as AbsDiffEq>::Epsilon: Copy,
230{
231    type Epsilon = <FT as AbsDiffEq>::Epsilon;
232    fn default_epsilon() -> Self::Epsilon {
233        FT::default_epsilon()
234    }
235    fn abs_diff_eq(&self, other: &Self, epsilon: Self::Epsilon) -> bool {
236        FT::abs_diff_eq(&self.abs, &other.abs, epsilon)
237            && FT::abs_diff_eq(&self.arg, &other.arg, epsilon)
238    }
239}
240
241#[cfg(feature = "approx")]
242impl<FT: RelativeEq + Copy> RelativeEq for ComplexPolar<FT>
243where
244    <FT as AbsDiffEq>::Epsilon: Copy,
245{
246    fn default_max_relative() -> Self::Epsilon {
247        FT::default_max_relative()
248    }
249    fn relative_eq(
250        &self,
251        other: &Self,
252        epsilon: Self::Epsilon,
253        max_relative: Self::Epsilon,
254    ) -> bool {
255        FT::relative_eq(&self.abs, &other.abs, epsilon, max_relative)
256            && FT::relative_eq(&self.arg, &other.arg, epsilon, max_relative)
257    }
258}
259
260#[cfg(feature = "approx")]
261impl<FT: UlpsEq + Copy> UlpsEq for ComplexPolar<FT>
262where
263    <FT as AbsDiffEq>::Epsilon: Copy,
264{
265    fn default_max_ulps() -> u32 {
266        FT::default_max_ulps()
267    }
268    fn ulps_eq(&self, other: &Self, epsilon: Self::Epsilon, max_ulps: u32) -> bool {
269        FT::ulps_eq(&self.abs, &other.abs, epsilon, max_ulps)
270            && FT::ulps_eq(&self.arg, &other.arg, epsilon, max_ulps)
271    }
272}