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: TReal part.
im: TImaginary part.
Implementations§
Source§impl<T: Float> Complex<T>
impl<T: Float> Complex<T>
Sourcepub fn new(re: T, im: T) -> Self
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);Sourcepub fn from_real(re: T) -> Self
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);Sourcepub fn from_polar(r: T, theta: T) -> Self
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);Sourcepub fn conj(self) -> Self
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);Sourcepub fn arg(self) -> T
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);Sourcepub fn exp(self) -> Self
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);Sourcepub fn ln(self) -> Self
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);Sourcepub fn sqrt(self) -> Self
pub fn sqrt(self) -> Self
Complex square root.
Uses the principal branch: sqrt(r) * (cos(theta/2) + i*sin(theta/2)).
Sourcepub fn pow(self, n: T) -> Self
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);Trait Implementations§
Source§impl<T: Float> AddAssign for Complex<T>
impl<T: Float> AddAssign for Complex<T>
Source§fn add_assign(&mut self, rhs: Self)
fn add_assign(&mut self, rhs: Self)
Performs the
+= operation. Read moreSource§impl<T: Float> DivAssign for Complex<T>
impl<T: Float> DivAssign for Complex<T>
Source§fn div_assign(&mut self, rhs: Self)
fn div_assign(&mut self, rhs: Self)
Performs the
/= operation. Read moreSource§impl<T: Float> MulAssign for Complex<T>
impl<T: Float> MulAssign for Complex<T>
Source§fn mul_assign(&mut self, rhs: Self)
fn mul_assign(&mut self, rhs: Self)
Performs the
*= operation. Read moreSource§impl<T: Float> PartialOrd for Complex<T>
impl<T: Float> PartialOrd for Complex<T>
Source§impl<T: Float> SubAssign for Complex<T>
impl<T: Float> SubAssign for Complex<T>
Source§fn sub_assign(&mut self, rhs: Self)
fn sub_assign(&mut self, rhs: Self)
Performs the
-= operation. Read moreimpl<T: Copy> Copy for Complex<T>
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more