Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 37 additions & 2 deletions library/core/src/num/complex.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::ops::{Add, Sub};
use crate::ops::{Add, Neg, Sub};

/// A complex number.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
Expand All @@ -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>;
Expand Down
31 changes: 31 additions & 0 deletions library/coretests/tests/num/complex.rs
Original file line number Diff line number Diff line change
@@ -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);
Expand Down Expand Up @@ -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),);
}
Loading