diff --git a/library/core/src/num/complex.rs b/library/core/src/num/complex.rs index 73b904d81fc53..66126c52fadad 100644 --- a/library/core/src/num/complex.rs +++ b/library/core/src/num/complex.rs @@ -1,4 +1,4 @@ -use crate::ops::{Add, Sub}; +use crate::ops::{Add, Neg, Sub}; /// A complex number. #[derive(Clone, Copy, Debug, PartialEq, Eq)] @@ -16,11 +16,46 @@ pub struct Complex { impl Complex { /// Create a new complex number from a real and imaginary component. #[must_use] - pub fn new(re: T, im: T) -> Complex { + pub const fn new(re: T, im: T) -> Complex { Complex { re, im } } } +#[unstable(feature = "complex_numbers", issue = "154023")] +impl Default for Complex { + fn default() -> Self { + Self { re: Default::default(), im: Default::default() } + } +} + +#[unstable(feature = "complex_numbers", issue = "154023")] +impl Complex +where + T: Neg, +{ + /// The complex conjugate of a complex number. + /// + /// The conjugate of `a + bi` is `a - bi`: the imaginary component is negated. + /// Geometrically, this is a reflection across the real axis. + #[must_use] + pub fn conjugate(self) -> Self { + Complex { re: self.re, im: -self.im } + } +} + +#[unstable(feature = "complex_numbers", issue = "154023")] +impl Neg for Complex { + type Output = Complex; + + /// Negate a complex number. + /// + /// The negation of `a + bi` is `-a - bi`: both components are negated. + /// Geometrically this is a rotation of 180°. + fn neg(self) -> Self::Output { + Complex::new(-self.re, -self.im) + } +} + #[unstable(feature = "complex_numbers", issue = "154023")] impl Add for Complex { type Output = Complex; diff --git a/library/coretests/tests/num/complex.rs b/library/coretests/tests/num/complex.rs index 4260c0a9a27a9..c22c5b9575b3d 100644 --- a/library/coretests/tests/num/complex.rs +++ b/library/coretests/tests/num/complex.rs @@ -1,5 +1,16 @@ use core::num::{Complex, Wrapping}; +#[test] +fn complex_default() { + assert_eq!(Complex::::default(), Complex::new(0, 0)); + assert_eq!(Complex::::default(), Complex::new(0.0, 0.0)); + + // The default is the additive unit. + let a = Complex::new(1, 2); + assert_eq!(a + Complex::::default(), a); + assert_eq!(Complex::::default() + a, a); +} + #[test] fn complex_addition() { let a = Complex::new(1, 2); @@ -42,3 +53,23 @@ fn complex_subtraction() { assert_eq!(a - b, Complex::new(a.re - b.re, a.im - b.im)); assert_eq!(a - 8.0, Complex::new(a.re - 8.0, a.im)); } + +#[test] +fn complex_conjugate() { + assert_eq!(Complex::new(1, 2).conjugate(), Complex::new(1, -2)); + assert_eq!(Complex::new(1, -2).conjugate(), Complex::new(1, 2)); + + assert_eq!(Complex::new(1.0, 2.0).conjugate(), Complex::new(1.0, -2.0)); + assert_eq!(Complex::new(1.0, -2.0).conjugate(), Complex::new(1.0, 2.0)); + assert_eq!(Complex::new(1.0, f32::INFINITY).conjugate(), Complex::new(1.0, f32::NEG_INFINITY)); +} + +#[test] +fn complex_negation() { + assert_eq!(-Complex::new(1, 2), Complex::new(-1, -2)); + assert_eq!(-Complex::new(1, -2), Complex::new(-1, 2)); + + assert_eq!(-Complex::new(1.0, 2.0), Complex::new(-1.0, -2.0)); + assert_eq!(-Complex::new(1.0, -2.0), Complex::new(-1.0, 2.0)); + assert_eq!(-Complex::new(1.0, f32::INFINITY), Complex::new(-1.0, f32::NEG_INFINITY),); +}