Skip to main content

Complex

Struct Complex 

Source
pub struct Complex { /* private fields */ }
Expand description

An exact complex number re + im·i, each part a Rational. Because the parts are exact, i·i == −1 and (1+i)(1−i) == 2 hold with no floating error, and the Gaussian rationals are closed under + − × ÷ (every nonzero value has an exact inverse). The magnitude √(re²+im²) is irrational in general — request it as an f64 view via Complex::abs_f64 rather than forcing the exact value inexact.

Complex numbers are NOT ordered, so there is deliberately no Ord/PartialOrd.

Implementations§

Source§

impl Complex

Source

pub fn new(re: Rational, im: Rational) -> Complex

Source

pub fn from_rational(re: Rational) -> Complex

A real number as re + 0i.

Source

pub fn from_i64(x: i64) -> Complex

Source

pub fn zero() -> Complex

Source

pub fn one() -> Complex

Source

pub fn i() -> Complex

The imaginary unit i (0 + 1i).

Source

pub fn re(&self) -> &Rational

Source

pub fn im(&self) -> &Rational

Source

pub fn is_zero(&self) -> bool

Source

pub fn is_real(&self) -> bool

True when the imaginary part is zero (the value is a plain real).

Source

pub fn conjugate(&self) -> Complex

re − im·i.

Source

pub fn norm_sqr(&self) -> Rational

re² + im² — the squared magnitude, exact (a nonnegative Rational).

Source

pub fn abs_f64(&self) -> f64

The magnitude √(re²+im²) as an f64 (lossy by nature — the exact value is generally irrational). The components stay exact; this is a view.

Source

pub fn negated(&self) -> Complex

Source

pub fn add(&self, other: &Complex) -> Complex

Source

pub fn sub(&self, other: &Complex) -> Complex

Source

pub fn mul(&self, other: &Complex) -> Complex

(a+bi)(c+di) = (ac − bd) + (ad + bc)i.

Source

pub fn recip(&self) -> Option<Complex>

1/self via the conjugate over the squared magnitude — None only for zero.

Source

pub fn div(&self, other: &Complex) -> Option<Complex>

self / otherNone when other == 0.

Source

pub fn pow(&self, exp: i32) -> Option<Complex>

self raised to an integer power. Negative exponents take the reciprocal first; None only for zero raised to a negative power. 0^0 == 1.

Source

pub fn parse(s: &str) -> Option<Complex>

Parse "3+4i", "3-4i", "4i", "-i", "i", or a bare real "3" /"1/2". Round-trips the Display form. None on malformed input.

Trait Implementations§

Source§

impl Clone for Complex

Source§

fn clone(&self) -> Complex

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 Debug for Complex

Source§

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

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

impl Display for Complex

Source§

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

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

impl From<Rational> for Complex

Source§

fn from(x: Rational) -> Self

Converts to this type from the input type.
Source§

impl From<i64> for Complex

Source§

fn from(x: i64) -> Self

Converts to this type from the input type.
Source§

impl Hash for Complex

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl PartialEq for Complex

Source§

fn eq(&self, other: &Complex) -> 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 Eq for Complex

Source§

impl StructuralPartialEq for Complex

Auto Trait Implementations§

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.