#[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));