1use crate::num::imp::libm::complex::*;
2use crate::ops::{Add, Div, Mul, Neg, Sub};
3
4#[derive(#[automatically_derived]#[unstable(feature = "complex_numbers", issue =
"154023")]
impl<T: crate::clone::Clone> crate::clone::Clone for Complex<T> {
#[inline]
#[rustc_trivial_field_reads]
fn clone(&self) -> Self {
Complex {
re: crate::clone::Clone::clone(&self.re),
im: crate::clone::Clone::clone(&self.im),
}
}
}Clone, #[automatically_derived]#[unstable(feature = "complex_numbers", issue =
"154023")]
impl<T: crate::marker::Copy> crate::marker::Copy for Complex<T> { }Copy, #[automatically_derived]#[unstable(feature = "complex_numbers", issue =
"154023")]
impl<T: crate::fmt::Debug> crate::fmt::Debug for Complex<T> {
#[inline]
#[rustc_trivial_field_reads]
fn fmt(&self, f: &mut crate::fmt::Formatter) -> crate::fmt::Result {
crate::fmt::Formatter::debug_struct_field2_finish(f, "Complex re im",
&self.re, &&self.im)
}
}Debug, #[automatically_derived]#[unstable(feature = "complex_numbers", issue =
"154023")]
impl<T: crate::cmp::PartialEq> crate::marker::StructuralPartialEq for
Complex<T> {
}
#[automatically_derived]#[unstable(feature = "complex_numbers", issue =
"154023")]
impl<T: crate::cmp::PartialEq> crate::cmp::PartialEq for Complex<T> {
#[inline]
fn eq(&self, other: &Self) -> bool {
self.re == other.re && self.im == other.im
}
}PartialEq, #[automatically_derived]#[unstable(feature = "complex_numbers", issue =
"154023")]
impl<T: crate::cmp::Eq> crate::cmp::Eq for Complex<T> {
#[inline]
#[doc(hidden)]
#[coverage(off)]
fn assert_fields_are_eq(&self) { let _: crate::cmp::AssertParamIsEq<T>; }
}Eq)]
76#[unstable(feature = "complex_numbers", issue = "154023")]
77#[repr(C)]
78#[lang = "complex"]
79pub struct Complex<T> {
80 pub re: T,
82 pub im: T,
84}
85
86#[unstable(feature = "complex_numbers", issue = "154023")]
87impl<T> Complex<T> {
88 #[must_use]
90 pub const fn new(re: T, im: T) -> Complex<T> {
91 Complex { re, im }
92 }
93}
94
95#[unstable(feature = "complex_numbers", issue = "154023")]
96impl<T: Default> Default for Complex<T> {
97 fn default() -> Self {
98 Self { re: Default::default(), im: Default::default() }
99 }
100}
101
102#[unstable(feature = "complex_numbers", issue = "154023")]
103impl<T> Complex<T>
104where
105 T: Neg<Output = T>,
106{
107 #[must_use]
121 pub fn conjugate(self) -> Self {
122 Complex { re: self.re, im: -self.im }
123 }
124}
125
126#[unstable(feature = "complex_numbers", issue = "154023")]
127impl<T: Neg> Neg for Complex<T> {
128 type Output = Complex<T::Output>;
129
130 fn neg(self) -> Self::Output {
143 Complex::new(-self.re, -self.im)
144 }
145}
146
147#[unstable(feature = "complex_numbers", issue = "154023")]
148impl<T: Add> Add<Self> for Complex<T> {
149 type Output = Complex<T::Output>;
150
151 fn add(self, rhs: Self) -> Self::Output {
161 Complex::new(self.re + rhs.re, self.im + rhs.im)
162 }
163}
164
165#[unstable(feature = "complex_numbers", issue = "154023")]
166impl<T: Add<Output = T>> Add<T> for Complex<T> {
167 type Output = Complex<T::Output>;
168
169 fn add(self, rhs: T) -> Self::Output {
179 Complex::new(self.re + rhs, self.im)
180 }
181}
182
183#[unstable(feature = "complex_numbers", issue = "154023")]
184impl<T: Sub> Sub<Self> for Complex<T> {
185 type Output = Complex<T::Output>;
186
187 fn sub(self, rhs: Self) -> Self::Output {
197 Complex::new(self.re - rhs.re, self.im - rhs.im)
198 }
199}
200
201#[unstable(feature = "complex_numbers", issue = "154023")]
202impl<T: Sub<Output = T>> Sub<T> for Complex<T> {
203 type Output = Complex<T::Output>;
204
205 fn sub(self, rhs: T) -> Self::Output {
215 Complex::new(self.re - rhs, self.im)
216 }
217}
218
219macro_rules! impl_complex_mul_div {
220 ($ty:ty, $mul:ident, $div:ident) => {
221 #[unstable(feature = "complex_numbers", issue = "154023")]
222 impl Mul for Complex<$ty> {
223 type Output = Self;
224
225 #[inline]
226 fn mul(self, rhs: Self) -> Self::Output {
227 let Complex { re: a, im: b } = self;
228 let Complex { re: c, im: d } = rhs;
229
230 let ac = a * c;
231 let bd = b * d;
232 let ad = a * d;
233 let bc = b * c;
234
235 let z = Complex::new(ac - bd, ad + bc);
236
237 if z.re.is_nan() && z.im.is_nan() {
245 crate::hint::cold_path();
246 $mul(a, b, c, d)
247 } else {
248 z
249 }
250 }
251 }
252
253 #[unstable(feature = "complex_numbers", issue = "154023")]
254 impl Div for Complex<$ty> {
255 type Output = Self;
256
257 #[inline]
258 fn div(self, rhs: Self) -> Self::Output {
259 $div(self.re, self.im, rhs.re, rhs.im)
260 }
261 }
262 };
263}
264
265#[unstable(feature = "complex_numbers", issue = "154023")]
impl Mul for Complex<f16> {
type Output = Self;
#[inline]
fn mul(self, rhs: Self) -> Self::Output {
let Complex { re: a, im: b } = self;
let Complex { re: c, im: d } = rhs;
let ac = a * c;
let bd = b * d;
let ad = a * d;
let bc = b * c;
let z = Complex::new(ac - bd, ad + bc);
if z.re.is_nan() && z.im.is_nan() {
crate::hint::cold_path();
__rust_mulhc3(a, b, c, d)
} else { z }
}
}
#[unstable(feature = "complex_numbers", issue = "154023")]
impl Div for Complex<f16> {
type Output = Self;
#[inline]
fn div(self, rhs: Self) -> Self::Output {
__rust_divhc3(self.re, self.im, rhs.re, rhs.im)
}
}impl_complex_mul_div!(f16, __rust_mulhc3, __rust_divhc3);
266#[unstable(feature = "complex_numbers", issue = "154023")]
impl Mul for Complex<f32> {
type Output = Self;
#[inline]
fn mul(self, rhs: Self) -> Self::Output {
let Complex { re: a, im: b } = self;
let Complex { re: c, im: d } = rhs;
let ac = a * c;
let bd = b * d;
let ad = a * d;
let bc = b * c;
let z = Complex::new(ac - bd, ad + bc);
if z.re.is_nan() && z.im.is_nan() {
crate::hint::cold_path();
__mulsc3(a, b, c, d)
} else { z }
}
}
#[unstable(feature = "complex_numbers", issue = "154023")]
impl Div for Complex<f32> {
type Output = Self;
#[inline]
fn div(self, rhs: Self) -> Self::Output {
__divsc3(self.re, self.im, rhs.re, rhs.im)
}
}impl_complex_mul_div!(f32, __mulsc3, __divsc3);
267#[unstable(feature = "complex_numbers", issue = "154023")]
impl Mul for Complex<f64> {
type Output = Self;
#[inline]
fn mul(self, rhs: Self) -> Self::Output {
let Complex { re: a, im: b } = self;
let Complex { re: c, im: d } = rhs;
let ac = a * c;
let bd = b * d;
let ad = a * d;
let bc = b * c;
let z = Complex::new(ac - bd, ad + bc);
if z.re.is_nan() && z.im.is_nan() {
crate::hint::cold_path();
__muldc3(a, b, c, d)
} else { z }
}
}
#[unstable(feature = "complex_numbers", issue = "154023")]
impl Div for Complex<f64> {
type Output = Self;
#[inline]
fn div(self, rhs: Self) -> Self::Output {
__divdc3(self.re, self.im, rhs.re, rhs.im)
}
}impl_complex_mul_div!(f64, __muldc3, __divdc3);
268#[unstable(feature = "complex_numbers", issue = "154023")]
impl Mul for Complex<f128> {
type Output = Self;
#[inline]
fn mul(self, rhs: Self) -> Self::Output {
let Complex { re: a, im: b } = self;
let Complex { re: c, im: d } = rhs;
let ac = a * c;
let bd = b * d;
let ad = a * d;
let bc = b * c;
let z = Complex::new(ac - bd, ad + bc);
if z.re.is_nan() && z.im.is_nan() {
crate::hint::cold_path();
__rust_multc3(a, b, c, d)
} else { z }
}
}
#[unstable(feature = "complex_numbers", issue = "154023")]
impl Div for Complex<f128> {
type Output = Self;
#[inline]
fn div(self, rhs: Self) -> Self::Output {
__rust_divtc3(self.re, self.im, rhs.re, rhs.im)
}
}impl_complex_mul_div!(f128, __rust_multc3, __rust_divtc3);