#[repr(C)]pub struct Complex<T> {
pub re: T,
pub im: T,
}complex_numbers)Expand description
A complex number, represented in rectangular form.
ยงBasic operations
A Complex<T> supports addition, subtraction, and negation when T does. The conjugation
requires that T supports negation.
#![feature(complex_numbers)]
use std::num::Complex;
let x = Complex { re: 5.0, im: 9.0 };
let y = Complex::new(2.0, 3.0);
// Scalar addition and subtraction operate on the real component.
assert_eq!(x + 1.0, Complex::new(6.0, 9.0));
assert_eq!(x - 1.0, Complex::new(4.0, 9.0));
// Complex addition and subtraction operate on each component separately.
assert_eq!(x + y, Complex::new(7.0, 12.0));
assert_eq!(x - y, Complex::new(3.0, 6.0));
// Negation occurs on both components.
// Geometrically this is a rotation by 180 degrees.
assert_eq!(-x, Complex::new(-5.0, -9.0));
// The conjugate negates only the imaginary component.
// Geometrically this is a reflection across the real axis.
assert_eq!(x.conjugate(), Complex::new(5.0, -9.0));ยงComplex multiplication and division of floating points
Multiplication and division follow the textbook formulas, except that they attempt to correct for overflow of intermediate steps of the calculation.
This behavior mirrors the C standard, although it does not specify exactly when the output should be corrected. Hence, the exact output may vary from platform to platform.
#![feature(complex_numbers)]
use std::num::Complex;
assert_eq!(Complex::new(1.0, 2.0) * Complex::new(3.0, 4.0), Complex::new(-5.0, 10.0));
assert_eq!(Complex::new(2.0, 11.0) / Complex::new(2.0, 1.0), Complex::new(3.0, 4.0));
// The textbook algorithm would return NaN + NaNi for these inputs.
assert_eq!(
Complex::new(1.0, 0.0) * Complex::new(f64::INFINITY, f64::INFINITY),
Complex::new(f64::INFINITY, f64::INFINITY)
);
assert_eq!(
Complex::new(1.0, 1.0) / Complex::new(f64::INFINITY, 0.0),
Complex::new(0.0, 0.0)
);ยงFFI compatibility
The following types are guaranteed to fully match the platformโs _Complex in terms of layout and ABI:
For any other Complex<T> ABI compatibility is not guaranteed.
Fieldsยง
ยงre: Tcomplex_numbers)The real component.
im: Tcomplex_numbers)The imaginary component.
Implementationsยง
Sourceยงimpl<T> Complex<T>where
T: Neg<Output = T>,
impl<T> Complex<T>where
T: Neg<Output = T>,
Sourcepub fn conjugate(self) -> Complex<T>
๐ฌThis is a nightly-only experimental API. (complex_numbers)
pub fn conjugate(self) -> Complex<T>
complex_numbers)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.
#![feature(complex_numbers)]
use std::num::Complex;
assert_eq!(Complex::new(3.0, 2.0).conjugate(), Complex::new(3.0, -2.0));
assert_eq!(Complex::new(-3.0, -2.0).conjugate(), Complex::new(-3.0, 2.0));Trait Implementationsยง
Sourceยงimpl<T> Add for Complex<T>where
T: Add,
impl<T> Add for Complex<T>where
T: Add,
Sourceยงimpl<T> Add<T> for Complex<T>where
T: Add<Output = T>,
impl<T> Add<T> for Complex<T>where
T: Add<Output = T>,
impl<T> Copy for Complex<T>where
T: Copy,
impl<T> Eq for Complex<T>where
T: Eq,
Sourceยงimpl<T> Neg for Complex<T>where
T: Neg,
impl<T> Neg for Complex<T>where
T: Neg,
impl<T> StructuralPartialEq for Complex<T>where
T: PartialEq,
Sourceยงimpl<T> Sub for Complex<T>where
T: Sub,
impl<T> Sub for Complex<T>where
T: Sub,
Auto Trait Implementationsยง
impl<T> Forget for Complex<T>
impl<T> Freeze for Complex<T>where
T: Freeze,
impl<T> Move for Complex<T>
impl<T> RefUnwindSafe for Complex<T>where
T: RefUnwindSafe,
impl<T> Send for Complex<T>where
T: Send,
impl<T> Sync for Complex<T>where
T: Sync,
impl<T> Unpin for Complex<T>where
T: Unpin,
impl<T> UnsafeUnpin for Complex<T>where
T: UnsafeUnpin,
impl<T> UnwindSafe for Complex<T>where
T: UnwindSafe,
Blanket Implementationsยง
Sourceยงimpl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Sourceยงfn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Sourceยงimpl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
ยงimpl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
ยงfn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
ยงimpl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
ยงfn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.