Skip to main content

Complex

Struct Complex 

Source
pub struct Complex<T> {
    pub re: T,
    pub im: T,
}
Expand description

A complex number stored in rectangular form.

Fields§

§re: T

The real component.

§im: T

The imaginary component.

Implementations§

Source§

impl<T> Complex<T>

Source

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

Creates a complex number from real and imaginary parts.

Examples found in repository?
examples/basic_usage.rs (line 4)
3fn main() {
4    let value = Complex::new(3.0_f64, 4.0_f64);
5    let mirrored = value.conjugate();
6    let lifted = Complex::from(Imaginary::new(4.0_f64));
7    let (magnitude, argument) = value.to_polar();
8
9    assert_eq!(value + Complex::one(), Complex::new(4.0, 4.0));
10    assert_eq!(mirrored, Complex::new(3.0, -4.0));
11    assert_eq!(lifted, Complex::new(0.0, 4.0));
12    assert!((magnitude - 5.0).abs() <= 1.0e-10);
13    assert!((argument - value.argument()).abs() <= 1.0e-10);
14}
Source

pub const fn real(&self) -> &T

Returns the real component.

Source

pub const fn imaginary(&self) -> &T

Returns the imaginary component.

Source§

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

Source

pub fn from_real(re: T) -> Self

Creates a real-only complex value.

Source

pub fn from_imaginary(im: T) -> Self

Creates an imaginary-only complex value.

Source§

impl<T> Complex<T>
where T: Default + From<u8>,

Source

pub fn zero() -> Self

Returns 0 + 0i.

Source

pub fn one() -> Self

Returns 1 + 0i.

Examples found in repository?
examples/basic_usage.rs (line 9)
3fn main() {
4    let value = Complex::new(3.0_f64, 4.0_f64);
5    let mirrored = value.conjugate();
6    let lifted = Complex::from(Imaginary::new(4.0_f64));
7    let (magnitude, argument) = value.to_polar();
8
9    assert_eq!(value + Complex::one(), Complex::new(4.0, 4.0));
10    assert_eq!(mirrored, Complex::new(3.0, -4.0));
11    assert_eq!(lifted, Complex::new(0.0, 4.0));
12    assert!((magnitude - 5.0).abs() <= 1.0e-10);
13    assert!((argument - value.argument()).abs() <= 1.0e-10);
14}
Source

pub fn i() -> Self

Returns 0 + 1i.

Source§

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

Source

pub fn conjugate(&self) -> Self

Returns the complex conjugate.

Examples found in repository?
examples/basic_usage.rs (line 5)
3fn main() {
4    let value = Complex::new(3.0_f64, 4.0_f64);
5    let mirrored = value.conjugate();
6    let lifted = Complex::from(Imaginary::new(4.0_f64));
7    let (magnitude, argument) = value.to_polar();
8
9    assert_eq!(value + Complex::one(), Complex::new(4.0, 4.0));
10    assert_eq!(mirrored, Complex::new(3.0, -4.0));
11    assert_eq!(lifted, Complex::new(0.0, 4.0));
12    assert!((magnitude - 5.0).abs() <= 1.0e-10);
13    assert!((argument - value.argument()).abs() <= 1.0e-10);
14}
Source§

impl<T> Complex<T>
where T: Copy + Add<Output = T> + Mul<Output = T>,

Source

pub fn magnitude_squared(&self) -> T

Returns the squared magnitude, re^2 + im^2.

Source§

impl Complex<f32>

Source

pub fn magnitude(&self) -> f32

Returns the magnitude, sqrt(re^2 + im^2).

Source

pub fn argument(&self) -> f32

Returns the argument in radians.

Source

pub fn from_polar(magnitude: f32, argument: f32) -> Self

Builds a complex value from polar coordinates.

Source

pub fn to_polar(&self) -> (f32, f32)

Returns (magnitude, argument) in polar form.

Source§

impl Complex<f64>

Source

pub fn magnitude(&self) -> f64

Returns the magnitude, sqrt(re^2 + im^2).

Source

pub fn argument(&self) -> f64

Returns the argument in radians.

Examples found in repository?
examples/basic_usage.rs (line 13)
3fn main() {
4    let value = Complex::new(3.0_f64, 4.0_f64);
5    let mirrored = value.conjugate();
6    let lifted = Complex::from(Imaginary::new(4.0_f64));
7    let (magnitude, argument) = value.to_polar();
8
9    assert_eq!(value + Complex::one(), Complex::new(4.0, 4.0));
10    assert_eq!(mirrored, Complex::new(3.0, -4.0));
11    assert_eq!(lifted, Complex::new(0.0, 4.0));
12    assert!((magnitude - 5.0).abs() <= 1.0e-10);
13    assert!((argument - value.argument()).abs() <= 1.0e-10);
14}
Source

pub fn from_polar(magnitude: f64, argument: f64) -> Self

Builds a complex value from polar coordinates.

Source

pub fn to_polar(&self) -> (f64, f64)

Returns (magnitude, argument) in polar form.

Examples found in repository?
examples/basic_usage.rs (line 7)
3fn main() {
4    let value = Complex::new(3.0_f64, 4.0_f64);
5    let mirrored = value.conjugate();
6    let lifted = Complex::from(Imaginary::new(4.0_f64));
7    let (magnitude, argument) = value.to_polar();
8
9    assert_eq!(value + Complex::one(), Complex::new(4.0, 4.0));
10    assert_eq!(mirrored, Complex::new(3.0, -4.0));
11    assert_eq!(lifted, Complex::new(0.0, 4.0));
12    assert!((magnitude - 5.0).abs() <= 1.0e-10);
13    assert!((argument - value.argument()).abs() <= 1.0e-10);
14}

Trait Implementations§

Source§

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

Source§

type Output = Complex<T>

The resulting type after applying the + operator.
Source§

fn add(self, rhs: Self) -> Self::Output

Performs the + operation. Read more
Source§

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

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: 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() -> Complex<T>

Returns the “default value” for a type. Read more
Source§

impl<T> Display for Complex<T>
where T: Copy + Default + Display + Neg<Output = T> + PartialEq + PartialOrd,

Source§

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

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

impl<T> Div for Complex<T>
where T: Copy + Add<Output = T> + Sub<Output = T> + Mul<Output = T> + Div<Output = T>,

Source§

type Output = Complex<T>

The resulting type after applying the / operator.
Source§

fn div(self, rhs: Self) -> Self::Output

Performs the / operation. Read more
Source§

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

Source§

fn from(value: Imaginary<T>) -> Self

Converts to this type from the input type.
Source§

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

Source§

fn from(value: T) -> Self

Converts to this type from the input type.
Source§

impl<T> Mul for Complex<T>
where T: Copy + Add<Output = T> + Sub<Output = T> + Mul<Output = T>,

Source§

type Output = Complex<T>

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: Self) -> Self::Output

Performs the * operation. Read more
Source§

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

Source§

type Output = Complex<T>

The resulting type after applying the - operator.
Source§

fn neg(self) -> Self::Output

Performs the unary - operation. Read more
Source§

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

Source§

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

Tests for self and other values to be equal, and is used by ==.
1.0.0 (const: unstable) · Source§

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

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

Source§

type Output = Complex<T>

The resulting type after applying the - operator.
Source§

fn sub(self, rhs: Self) -> Self::Output

Performs the - operation. Read more
Source§

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

Source§

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

Source§

impl<T> StructuralPartialEq for Complex<T>

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)
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> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

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

Source§

type Error = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.