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) -> Self

🔬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> Add for Complex<T>

Source§

fn add(self, rhs: Self) -> Self::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<Output = T>> Add<T> for Complex<T>

Source§

fn add(self, rhs: T) -> Self::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> Clone for Complex<T>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

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

Source§

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

Source§

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

Formats the value using the given formatter. Read more
Source§

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

Source§

fn default() -> Self

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: Self) -> Self::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: Self) -> Self::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: Self) -> Self::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: Self) -> Self::Output

Performs the / operation. Read more
Source§

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

Source§

impl Mul for Complex<f16>

Source§

type Output = Complex<f16>

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: Self) -> Self::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: Self) -> Self::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: Self) -> Self::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: Self) -> Self::Output

Performs the * operation. Read more
Source§

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

Source§

fn neg(self) -> Self::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> PartialEq for Complex<T>

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

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

Inequality operator !=. Read more
Source§

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

Source§

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

Source§

fn sub(self, rhs: Self) -> Self::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<Output = T>> Sub<T> for Complex<T>

Source§

fn sub(self, rhs: T) -> Self::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> 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, 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.