Skip to main content

Complex

Struct Complex 

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

A complex number with real and imaginary parts of type T.

§Examples

let z = Complex::new(3.0_f64, 4.0);
assert_eq!(z.norm(), 5.0);

Fields§

§re: T

Real part.

§im: T

Imaginary part.

Implementations§

Source§

impl<T: Float> Complex<T>

Source

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

Create a new complex number from real and imaginary parts.

§Examples
let z = Complex::new(1.0_f64, 2.0);
assert_eq!(z.re, 1.0);
assert_eq!(z.im, 2.0);
Source

pub fn from_real(re: T) -> Self

Create a complex number from a real value (imaginary part is zero).

§Examples
let z = Complex::from_real(3.0_f64);
assert_eq!(z.im, 0.0);
Source

pub fn from_polar(r: T, theta: T) -> Self

Create a complex number from polar form: r * (cos(theta) + i*sin(theta)).

§Examples
let z = Complex::from_polar(1.0_f64, 0.0_f64);
assert!((z.re - 1.0).abs() < 1e-15);
assert!(z.im.abs() < 1e-15);
Source

pub fn conj(self) -> Self

Complex conjugate: flips the sign of the imaginary part.

§Examples
let z = Complex::new(3.0_f64, 4.0);
let c = z.conj();
assert_eq!(c.re, 3.0);
assert_eq!(c.im, -4.0);
Source

pub fn norm_sqr(self) -> T

Squared modulus: re² + im². Avoids the sqrt in norm.

§Examples
let z = Complex::new(3.0_f64, 4.0);
assert_eq!(z.norm_sqr(), 25.0);
Source

pub fn norm(self) -> T

Modulus (absolute value): sqrt(re² + im²).

Source

pub fn arg(self) -> T

Phase angle (argument): atan2(im, re).

§Examples
let z = Complex::new(0.0_f64, 1.0); // i
let angle = z.arg();
assert!((angle - std::f64::consts::FRAC_PI_2).abs() < 1e-10);
Source

pub fn exp(self) -> Self

Complex exponential: e^(a+bi) = e^a * (cos b + i sin b).

§Examples
// e^0 = 1
let z = Complex::new(0.0_f64, 0.0);
let e = z.exp();
assert!((e.re - 1.0).abs() < 1e-15);
assert!(e.im.abs() < 1e-15);
Source

pub fn ln(self) -> Self

Complex natural logarithm: ln|z| + i*arg(z).

§Examples
// ln(1) = 0
let z = Complex::new(1.0_f64, 0.0);
let l = z.ln();
assert!(l.re.abs() < 1e-15);
assert!(l.im.abs() < 1e-15);
Source

pub fn sqrt(self) -> Self

Complex square root.

Uses the principal branch: sqrt(r) * (cos(theta/2) + i*sin(theta/2)).

Source

pub fn pow(self, n: T) -> Self

Complex power: z^n = e^(n * ln(z)).

§Examples
// (1+0i)^5 = 1
let z = Complex::new(1.0_f64, 0.0);
let p = z.pow(5.0_f64);
assert!((p.re - 1.0).abs() < 1e-10);
assert!(p.im.abs() < 1e-10);
Source

pub fn is_finite(self) -> bool

Returns true if both real and imaginary parts are finite.

§Examples
assert!(Complex::new(1.0_f64, 2.0).is_finite());
assert!(!Complex::new(f64::INFINITY, 0.0).is_finite());
Source

pub fn is_nan(self) -> bool

Returns true if either part is NaN.

§Examples
assert!(!Complex::new(1.0_f64, 2.0).is_nan());
assert!(Complex::new(f64::NAN, 0.0).is_nan());
Source§

impl<T: Float> Complex<T>

Source

pub fn i() -> Self

The imaginary unit i.

§Examples
let i = Complex::<f64>::i();
assert_eq!(i.re, 0.0);
assert_eq!(i.im, 1.0);

Trait Implementations§

Source§

impl<T: Float> Add<T> for Complex<T>

Source§

type Output = Complex<T>

The resulting type after applying the + operator.
Source§

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

Performs the + operation. Read more
Source§

impl<T: Float> Add for Complex<T>

Source§

type Output = Complex<T>

The resulting type after applying the + operator.
Source§

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

Performs the + operation. Read more
Source§

impl<T: Float> AddAssign for Complex<T>

Source§

fn add_assign(&mut self, rhs: Self)

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 · 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: Float + Display> Display for Complex<T>

Source§

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

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

impl<T: Float> Div<T> for Complex<T>

Source§

type Output = Complex<T>

The resulting type after applying the / operator.
Source§

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

Performs the / operation. Read more
Source§

impl<T: Float> Div for Complex<T>

Source§

type Output = Complex<T>

The resulting type after applying the / operator.
Source§

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

Performs the / operation. Read more
Source§

impl<T: Float> DivAssign for Complex<T>

Source§

fn div_assign(&mut self, rhs: Self)

Performs the /= operation. Read more
Source§

impl<T: Float> Mul<T> for Complex<T>

Source§

type Output = Complex<T>

The resulting type after applying the * operator.
Source§

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

Performs the * operation. Read more
Source§

impl<T: Float> Mul for Complex<T>

Source§

type Output = Complex<T>

The resulting type after applying the * operator.
Source§

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

Performs the * operation. Read more
Source§

impl<T: Float> MulAssign for Complex<T>

Source§

fn mul_assign(&mut self, rhs: Self)

Performs the *= operation. Read more
Source§

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

Source§

type Output = Complex<T>

The resulting type after applying the - operator.
Source§

fn neg(self) -> Self

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 · 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: Float> PartialOrd for Complex<T>

Source§

fn partial_cmp(&self, other: &Self) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

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

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

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

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

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

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

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

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl<T: Float> Scalar for Complex<T>

Source§

fn zero() -> Self

The additive identity (0).
Source§

fn one() -> Self

The multiplicative identity (1).
Source§

fn from_usize(v: usize) -> Self

Convert from usize (used for index / shape arithmetic).
Source§

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

Source§

type Output = Complex<T>

The resulting type after applying the - operator.
Source§

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

Performs the - operation. Read more
Source§

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

Source§

type Output = Complex<T>

The resulting type after applying the - operator.
Source§

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

Performs the - operation. Read more
Source§

impl<T: Float> SubAssign for Complex<T>

Source§

fn sub_assign(&mut self, rhs: Self)

Performs the -= operation. Read more
Source§

impl<T: Float> Sum for Complex<T>

Source§

fn sum<I: Iterator<Item = Self>>(iter: I) -> Self

Takes an iterator and generates Self from the elements by “summing up” the items.
Source§

impl<T: Copy> Copy 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.