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: TReal part.
im: TImaginary part.
Implementations§
Source§impl<T: RealFull> Complex<T>
impl<T: RealFull> Complex<T>
Sourcepub fn new(re: T, im: T) -> Complex<T>
pub fn new(re: T, im: T) -> Complex<T>
Create a complex number from its real and imaginary parts.
Sourcepub fn is_close(&self, other: &Complex<T>, rel_tol: T, abs_tol: T) -> bool
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|).
Sourcepub fn sqrt(&self) -> Complex<T>
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);Sourcepub fn exp(&self) -> Complex<T>
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);Sourcepub fn pow(&self, p: Complex<T>) -> Complex<T>
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.
Sourcepub fn acosh(&self) -> Complex<T>
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.
Sourcepub fn atanh(&self) -> Complex<T>
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.
Sourcepub fn recip(&self) -> Complex<T>
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).
Sourcepub fn is_infinite(&self) -> bool
pub fn is_infinite(&self) -> bool
Returns true if either component is infinite.