rust-lang/rust · #162865

Complex conjugate, negation and default

folkertdev · merged Sep 17, 20262 files · 68 + / 2
library/core/src/num/complex.rs37 + / 2
@@ -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<T> { impl<T> Complex<T> {     /// Create a new complex number from a real and imaginary component.     #[must_use]-    pub fn new(re: T, im: T) -> Complex<T> {+    pub const fn new(re: T, im: T) -> Complex<T> {         Complex { re, im }     } } +#[unstable(feature = "complex_numbers", issue = "154023")]+impl<T: Default> Default for Complex<T> {+    fn default() -> Self {+        Self { re: Default::default(), im: Default::default() }+    }+}++#[unstable(feature = "complex_numbers", issue = "154023")]+impl<T> Complex<T>+where+    T: Neg<Output = T>,+{+    /// 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<T: Neg> Neg for Complex<T> {+    type Output = Complex<T::Output>;++    /// 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<T: Add> Add<Self> for Complex<T> {     type Output = Complex<T::Output>;
library/coretests/tests/num/complex.rs31 + / 0
@@ -1,5 +1,16 @@ use core::num::{Complex, Wrapping}; +#[test]+fn complex_default() {+    assert_eq!(Complex::<i32>::default(), Complex::new(0, 0));+    assert_eq!(Complex::<f32>::default(), Complex::new(0.0, 0.0));++    // The default is the additive unit.+    let a = Complex::new(1, 2);+    assert_eq!(a + Complex::<i32>::default(), a);+    assert_eq!(Complex::<i32>::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),);+}