Skip to main content

Complex

Struct Complex 

Source
#[repr(C)]
pub struct Complex<T> { pub re: T, pub im: T, }
๐Ÿ”ฌThis is a nightly-only experimental API. (complex_numbers #154023)
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: T
๐Ÿ”ฌThis is a nightly-only experimental API. (complex_numbers #154023)

The real component.

ยงim: T
๐Ÿ”ฌThis is a nightly-only experimental API. (complex_numbers #154023)

The imaginary component.

Implementationsยง

Sourceยง

impl<T> Complex<T>

Source

pub const fn new(re: T, im: T) -> Complex<T>

๐Ÿ”ฌThis is a nightly-only experimental API. (complex_numbers #154023)

Create a new complex number from a real and imaginary component.

Sourceยง

impl<T> Complex<T>
where T: Neg<Output = T>,

Source

pub fn conjugate(self) -> Complex<T>

๐Ÿ”ฌThis is a nightly-only experimental API. (complex_numbers #154023)

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,

Sourceยง

fn add(self, rhs: Complex<T>) -> <Complex<T> as Add>::Output

Complex addition operates on each component separately.

#![feature(complex_numbers)]

use std::num::Complex;

assert_eq!(Complex::new(2.0, 3.0) + Complex::new(5.0, 9.0), Complex::new(7.0, 12.0));
Sourceยง

type Output = Complex<<T as Add>::Output>

The resulting type after applying the + operator.
Sourceยง

impl<T> Add<T> for Complex<T>
where T: Add<Output = T>,

Sourceยง

fn add(self, rhs: T) -> <Complex<T> as Add<T>>::Output

Scalar addition operates only on the real component.

#![feature(complex_numbers)]

use std::num::Complex;

assert_eq!(Complex::new(2.0, 3.0) + 10.0, Complex::new(12.0, 3.0));
Sourceยง

type Output = Complex<<T as Add>::Output>

The resulting type after applying the + operator.
Sourceยง

impl<T> Clone for Complex<T>
where T: Clone,

Sourceยง

fn clone(&self) -> Complex<T>

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) ยท Sourceยง

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Sourceยง

impl<T> Copy for Complex<T>
where T: Copy,

Sourceยง

impl<T> Debug for Complex<T>
where T: Debug,

Sourceยง

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
Sourceยง

impl<T> Default for Complex<T>
where T: Default,

Sourceยง

fn default() -> Complex<T>

Returns the โ€œdefault valueโ€ for a type. Read more
Sourceยง

impl Div for Complex<f16>

Sourceยง

type Output = Complex<f16>

The resulting type after applying the / operator.
Sourceยง

fn div(self, rhs: Complex<f16>) -> <Complex<f16> as Div>::Output

Performs the / operation. Read more
Sourceยง

impl Div for Complex<f32>

Sourceยง

type Output = Complex<f32>

The resulting type after applying the / operator.
Sourceยง

fn div(self, rhs: Complex<f32>) -> <Complex<f32> as Div>::Output

Performs the / operation. Read more
Sourceยง

impl Div for Complex<f64>

Sourceยง

type Output = Complex<f64>

The resulting type after applying the / operator.
Sourceยง

fn div(self, rhs: Complex<f64>) -> <Complex<f64> as Div>::Output

Performs the / operation. Read more
Sourceยง

impl Div for Complex<f128>

Sourceยง

type Output = Complex<f128>

The resulting type after applying the / operator.
Sourceยง

fn div(self, rhs: Complex<f128>) -> <Complex<f128> as Div>::Output

Performs the / operation. Read more
Sourceยง

impl<T> Eq for Complex<T>
where T: Eq,

Sourceยง

impl Mul for Complex<f16>

Sourceยง

type Output = Complex<f16>

The resulting type after applying the * operator.
Sourceยง

fn mul(self, rhs: Complex<f16>) -> <Complex<f16> as Mul>::Output

Performs the * operation. Read more
Sourceยง

impl Mul for Complex<f32>

Sourceยง

type Output = Complex<f32>

The resulting type after applying the * operator.
Sourceยง

fn mul(self, rhs: Complex<f32>) -> <Complex<f32> as Mul>::Output

Performs the * operation. Read more
Sourceยง

impl Mul for Complex<f64>

Sourceยง

type Output = Complex<f64>

The resulting type after applying the * operator.
Sourceยง

fn mul(self, rhs: Complex<f64>) -> <Complex<f64> as Mul>::Output

Performs the * operation. Read more
Sourceยง

impl Mul for Complex<f128>

Sourceยง

type Output = Complex<f128>

The resulting type after applying the * operator.
Sourceยง

fn mul(self, rhs: Complex<f128>) -> <Complex<f128> as Mul>::Output

Performs the * operation. Read more
Sourceยง

impl<T> Neg for Complex<T>
where T: Neg,

Sourceยง

fn neg(self) -> <Complex<T> as Neg>::Output

Negate a complex number.

The negation of a + bi is -a - bi: both components are negated. Geometrically this is a rotation of 180ยฐ.

#![feature(complex_numbers)]

use std::num::Complex;

assert_eq!(-Complex::new(3.0, 2.0), Complex::new(-3.0, -2.0));
Sourceยง

type Output = Complex<<T as Neg>::Output>

The resulting type after applying the - operator.
Sourceยง

impl<T> PartialEq for Complex<T>
where T: PartialEq,

Sourceยง

fn eq(&self, other: &Complex<T>) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) ยท Sourceยง

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Sourceยง

impl<T> StructuralPartialEq for Complex<T>
where T: PartialEq,

Sourceยง

impl<T> Sub for Complex<T>
where T: Sub,

Sourceยง

fn sub(self, rhs: Complex<T>) -> <Complex<T> as Sub>::Output

Complex subtraction operates on each component separately.

#![feature(complex_numbers)]

use std::num::Complex;

assert_eq!(Complex::new(2.0, 3.0) - Complex::new(5.0, 9.0), Complex::new(-3.0, -6.0));
Sourceยง

type Output = Complex<<T as Sub>::Output>

The resulting type after applying the - operator.
Sourceยง

impl<T> Sub<T> for Complex<T>
where T: Sub<Output = T>,

Sourceยง

fn sub(self, rhs: T) -> <Complex<T> as Sub<T>>::Output

Scalar subtraction operates only on the real component.

#![feature(complex_numbers)]

use std::num::Complex;

assert_eq!(Complex::new(2.0, 3.0) - 10.0, Complex::new(-8.0, 3.0));
Sourceยง

type Output = Complex<<T as Sub>::Output>

The resulting type after applying the - operator.

Auto Trait Implementationsยง

ยง

impl<T> Forget for Complex<T>

ยง

impl<T> Freeze for Complex<T>
where T: Freeze,

ยง

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> Any for T
where T: 'static + ?Sized,

Sourceยง

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Sourceยง

impl<T> Borrow<T> for T
where T: ?Sized,

Sourceยง

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Sourceยง

impl<T> BorrowMut<T> for T
where T: ?Sized,

Sourceยง

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Sourceยง

impl<T> CloneToUninit for T
where T: Clone,

Sourceยง

unsafe fn clone_to_uninit(&self, dest: *mut u8)

๐Ÿ”ฌThis is a nightly-only experimental API. (clone_to_uninit #126799)
Performs copy-assignment from self to dest. Read more
Sourceยง

impl<T> From<T> for T

Sourceยง

fn from(t: T) -> T

Returns the argument unchanged.

Sourceยง

impl<T, U> Into<U> for T
where U: From<T>,

Sourceยง

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Sourceยง

impl<T> ToOwned for T
where T: Clone,

Sourceยง

type Owned = T

The resulting type after obtaining ownership.
Sourceยง

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Sourceยง

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Sourceยง

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Sourceยง

type Error = !

The type returned in the event of a conversion error.
Sourceยง

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Sourceยง

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Sourceยง

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Sourceยง

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.