Skip to main content

Complex

Struct Complex 

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

Complex number re + im·i over any real type T (defaults to f64).

Fields§

§re: T

Real part.

§im: T

Imaginary part.

Implementations§

Source§

impl<T: RealFull> Complex<T>

Source

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

Create a complex number from its real and imaginary parts.

Source

pub fn real(r: T) -> Complex<T>

The real number r as a complex number.

Source

pub fn i() -> Complex<T>

The imaginary unit i (0 + 1i).

Source

pub fn zero() -> Complex<T>

Zero constant (0 + 0i).

Source

pub fn one() -> Complex<T>

One constant (1 + 0i).

Source

pub fn polar(r: T, theta: T) -> Complex<T>

From polar form r·e^(iθ) (equiv. cmath.rect(r, θ)).

Source

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

Complex conjugate re − im·i.

Source

pub fn norm_sq(&self) -> T

Squared modulus re² + im².

Source

pub fn norm(&self) -> T

Modulus |z| = √(re² + im²).

Source

pub fn arg(&self) -> T

Principal argument in (-π, π] (equiv. cmath.phase(z)).

Source

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

(r, θ) such that self == r·e^(iθ) (equiv. cmath.polar(z)).

Source

pub fn phase(&self) -> T

Alias for arg, matching numpy.angle/cmath.phase.

Source

pub fn rect(r: T, phi: T) -> Complex<T>

Build from polar form, matching cmath.rect(r, φ).

Source

pub fn is_close(&self, other: &Complex<T>, rel_tol: T, abs_tol: T) -> bool

cmath.isclose equivalent: true if |a − b| ≤ abs_tol or |a − b| ≤ rel_tol · max(|a|, |b|).

Source

pub fn is_zero(&self) -> bool

Returns true if both components are exactly zero.

Source

pub fn sqrt(&self) -> Complex<T>

Principal square root.

Uses the algebraically stable formula (Numerical Recipes §5.4) instead of the polar form, avoiding the two-square-root + atan2 round trip and keeping full precision for z near the real axis.

The branch cut is the negative real axis: sqrt(-4 + 0⁻i) = -2i.

use mathverse_complex::Complex;
let s: Complex = Complex::new(-4.0, 0.0).sqrt();
assert!((s.im - 2.0).abs() < 1e-12 && s.re.abs() < 1e-12);
Source

pub fn exp(&self) -> Complex<T>

e^self.

use mathverse_complex::Complex;
let e = Complex::new(0.0, core::f64::consts::PI).exp();
assert!((e.re + 1.0).abs() < 1e-12 && e.im.abs() < 1e-12);
Source

pub fn ln(&self) -> Complex<T>

Principal natural logarithm.

Source

pub fn pow(&self, p: Complex<T>) -> Complex<T>

self^p = e^(p·ln self), principal branch.

For 0^p the standard limiting convention is used: 0 when Re(p) > 0, +∞ when Re(p) < 0, and 1 for p = 0 (and purely imaginary p), matching the combinatorial 0^0 = 1 identity and numpy’s complex power.

Source

pub fn powf(&self, p: T) -> Complex<T>

self^p for a real exponent.

Source

pub fn sin(&self) -> Complex<T>

Sine: sin(re)·cosh(im) + i·cos(re)·sinh(im).

Source

pub fn cos(&self) -> Complex<T>

Cosine: cos(re)·cosh(im) − i·sin(re)·sinh(im).

Source

pub fn tan(&self) -> Complex<T>

Tangent: sin(z) / cos(z).

Source

pub fn sinh(&self) -> Complex<T>

Hyperbolic sine.

Source

pub fn cosh(&self) -> Complex<T>

Hyperbolic cosine.

Source

pub fn tanh(&self) -> Complex<T>

Hyperbolic tangent.

Source

pub fn asinh(&self) -> Complex<T>

Inverse hyperbolic sine.

Source

pub fn acosh(&self) -> Complex<T>

Inverse hyperbolic cosine.

acosh(z) = ln(z + sqrt(z² − 1)) with branch cut (-∞, 1]. The single-square-root form avoids the double sqrt round trip of the equivalent sqrt(z+1)·sqrt(z-1) formulation and matches numpy’s np.arccosh. Result satisfies Re(acosh(z)) ≥ 0.

Source

pub fn atanh(&self) -> Complex<T>

Inverse hyperbolic tangent.

atanh(z) = ½·ln((1+z)/(1−z)) with branch cuts (-∞, -1] and [1, ∞). On the real axis: for real x > 1 the principal value has Im = +π/2; for x < -1, Im = -π/2 (matches cmath.atanh). The sign follows the sign of the imaginary part of the input, so atanh(x ± 0i) sits on the matching side of the cut.

Source

pub fn asin(&self) -> Complex<T>

Inverse sine.

Source

pub fn acos(&self) -> Complex<T>

Inverse cosine.

Source

pub fn atan(&self) -> Complex<T>

Inverse tangent.

Source

pub fn log10(&self) -> Complex<T>

Base-10 logarithm.

Source

pub fn log2(&self) -> Complex<T>

Base-2 logarithm.

Source

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

Reciprocal (1/self).

Uses Smith’s algorithm: scale by the larger component before the squaring, so re² + im² cannot overflow (e.g. (1e200+0i).recip() is 1e-200, not NaN).

Source

pub fn is_nan(&self) -> bool

Returns true if either component is NaN.

Source

pub fn is_infinite(&self) -> bool

Returns true if either component is infinite.

Source

pub fn is_finite(&self) -> bool

Returns true if both components are finite.

Source

pub fn signum(&self) -> Complex<T>

Signum (sign) function: same direction, unit magnitude. Returns zero if self is zero.

Source

pub fn cbrt(&self) -> Complex<T>

Cube root.

Source

pub fn root(&self, n: T) -> Complex<T>

Nth root (principal branch).

Trait Implementations§

Source§

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

Source§

type Output = Complex<T>

The resulting type after applying the + operator.
Source§

fn add(self, o: Complex<T>) -> Complex<T>

Performs the + operation. Read more
Source§

impl<'a, 'b, T: RealFull> Add<&'b Complex<T>> for &'a Complex<T>

Source§

type Output = Complex<T>

The resulting type after applying the + operator.
Source§

fn add(self, o: &'b Complex<T>) -> Complex<T>

Performs the + operation. Read more
Source§

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

Source§

type Output = Complex<T>

The resulting type after applying the + operator.
Source§

fn add(self, o: T) -> Complex<T>

Performs the + operation. Read more
Source§

impl<'a, T: RealFull> Add<T> for &'a Complex<T>

Source§

type Output = Complex<T>

The resulting type after applying the + operator.
Source§

fn add(self, o: T) -> Complex<T>

Performs the + operation. Read more
Source§

impl<T: Clone + RealFull> 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)

Performs copy-assignment from source. Read more
Source§

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

Source§

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

Source§

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

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

impl<T: RealFull + Display> Display for Complex<T>

Source§

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

Formats as a+bi (e.g. 3+4i, 1-2i), honoring precision like {:.6} for the component digits.

Source§

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

Source§

type Output = Complex<T>

The resulting type after applying the / operator.
Source§

fn div(self, o: Complex<T>) -> Complex<T>

Performs the / operation. Read more
Source§

impl<'a, 'b, T: RealFull> Div<&'b Complex<T>> for &'a Complex<T>

Source§

type Output = Complex<T>

The resulting type after applying the / operator.
Source§

fn div(self, o: &'b Complex<T>) -> Complex<T>

Performs the / operation. Read more
Source§

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

Source§

type Output = Complex<T>

The resulting type after applying the / operator.
Source§

fn div(self, o: T) -> Complex<T>

Performs the / operation. Read more
Source§

impl<'a, T: RealFull> Div<T> for &'a Complex<T>

Source§

type Output = Complex<T>

The resulting type after applying the / operator.
Source§

fn div(self, o: T) -> Complex<T>

Performs the / operation. Read more
Source§

impl<T: RealFull> From<(T, T)> for Complex<T>

Source§

fn from((re, im): (T, T)) -> Complex<T>

Converts to this type from the input type.
Source§

impl<T: RealFull> From<T> for Complex<T>

Source§

fn from(r: T) -> Complex<T>

Converts to this type from the input type.
Source§

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

Source§

type Output = Complex<T>

The resulting type after applying the * operator.
Source§

fn mul(self, o: Complex<T>) -> Complex<T>

Performs the * operation. Read more
Source§

impl<'a, 'b, T: RealFull> Mul<&'b Complex<T>> for &'a Complex<T>

Source§

type Output = Complex<T>

The resulting type after applying the * operator.
Source§

fn mul(self, o: &'b Complex<T>) -> Complex<T>

Performs the * operation. Read more
Source§

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

Source§

type Output = Complex<T>

The resulting type after applying the * operator.
Source§

fn mul(self, o: T) -> Complex<T>

Performs the * operation. Read more
Source§

impl<'a, T: RealFull> Mul<T> for &'a Complex<T>

Source§

type Output = Complex<T>

The resulting type after applying the * operator.
Source§

fn mul(self, o: T) -> Complex<T>

Performs the * operation. Read more
Source§

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

Source§

type Output = Complex<T>

The resulting type after applying the - operator.
Source§

fn neg(self) -> Complex<T>

Performs the unary - operation. Read more
Source§

impl<'a, T: RealFull> Neg for &'a Complex<T>

Source§

type Output = Complex<T>

The resulting type after applying the - operator.
Source§

fn neg(self) -> Complex<T>

Performs the unary - operation. Read more
Source§

impl<T: PartialEq + RealFull> 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 + RealFull> StructuralPartialEq for Complex<T>

Source§

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

Source§

type Output = Complex<T>

The resulting type after applying the - operator.
Source§

fn sub(self, o: Complex<T>) -> Complex<T>

Performs the - operation. Read more
Source§

impl<'a, 'b, T: RealFull> Sub<&'b Complex<T>> for &'a Complex<T>

Source§

type Output = Complex<T>

The resulting type after applying the - operator.
Source§

fn sub(self, o: &'b Complex<T>) -> Complex<T>

Performs the - operation. Read more
Source§

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

Source§

type Output = Complex<T>

The resulting type after applying the - operator.
Source§

fn sub(self, o: T) -> Complex<T>

Performs the - operation. Read more
Source§

impl<'a, T: RealFull> Sub<T> for &'a Complex<T>

Source§

type Output = Complex<T>

The resulting type after applying the - operator.
Source§

fn sub(self, o: T) -> Complex<T>

Performs the - operation. Read more

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 = !

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.