pub struct Complex {
pub real: f32,
pub imag: f32,
}
Expand description
A struct representing a complex number with f32
real and imaginary components.
Fields§
§real: f32
§imag: f32
Implementations§
source§impl Complex
impl Complex
sourcepub fn new(real: f32, imag: f32) -> Self
pub fn new(real: f32, imag: f32) -> Self
Creates a new Complex
number given a real and imaginary part.
§Arguments
real
- The real component of the complex number.imag
- The imaginary component of the complex number.
§Returns
A new Complex
number with the specified real and imaginary components.
§Example
use matrix::Complex;
let complex_num = Complex::new(3.0, 4.0);
assert_eq!(complex_num.real, 3.0);
assert_eq!(complex_num.imag, 4.0);
sourcepub fn conjugate(self) -> Self
pub fn conjugate(self) -> Self
Returns the conjugate of the complex number.
The conjugate of a complex number a + bi
is given by a - bi
.
§Returns
A new Complex
number with the same real component and a negated imaginary component.
§Example
use matrix::Complex;
let complex_num = Complex::new(3.0, 4.0);
let conjugate = complex_num.conjugate();
assert_eq!(conjugate, Complex::new(3.0, -4.0));
sourcepub fn magnitude(&self) -> f32
pub fn magnitude(&self) -> f32
Computes the magnitude (or modulus) of the complex number.
The magnitude of a complex number ( z = a + bi ) is calculated as:
where a
is the real part and b
is the imaginary part of the complex number.
§Returns
A f64
value representing the magnitude of the complex number.
§Example
use matrix::Complex;
let complex_num = Complex::new(3.0, 4.0);
assert_eq!(complex_num.magnitude(), 5.0);
Trait Implementations§
source§impl Add for Complex
impl Add for Complex
source§fn add(self, other: Self) -> Self::Output
fn add(self, other: Self) -> Self::Output
Adds two Complex
numbers.
Given two complex numbers a + bi
and c + di
, the sum is:
§Arguments
self
- The firstComplex
number.other
- The secondComplex
number to add.
§Returns
A new Complex
number which is the sum of self
and other
.
§Example
use matrix::Complex;
let a = Complex::new(3.0, 4.0);
let b = Complex::new(1.0, 2.0);
assert_eq!(a + b, Complex::new(4.0, 6.0));
source§impl AddAssign for Complex
impl AddAssign for Complex
source§fn add_assign(&mut self, other: Self)
fn add_assign(&mut self, other: Self)
Adds another Complex
number to self
.
source§impl Div for Complex
impl Div for Complex
source§fn div(self, other: Self) -> Self::Output
fn div(self, other: Self) -> Self::Output
Divides one Complex
number by another.
Given two complex numbers a + bi
and c + di
, the quotient is:
§Arguments
self
- TheComplex
number to be divided.other
- TheComplex
number to divide by.
§Returns
A new Complex
number which is the quotient of self
and other
.
§Example
use matrix::Complex;
let a = Complex::new(3.0, 2.0);
let b = Complex::new(1.0, -1.0);
let result = a / b;
assert!((result.real - 0.5).abs() < 1e-10);
assert!((result.imag - 2.5).abs() < 1e-10);
source§impl DivAssign for Complex
impl DivAssign for Complex
source§fn div_assign(&mut self, other: Self)
fn div_assign(&mut self, other: Self)
Divides self
by another Complex
number.
source§impl Mul for Complex
impl Mul for Complex
source§fn mul(self, other: Self) -> Self::Output
fn mul(self, other: Self) -> Self::Output
Multiplies two Complex
numbers.
Given two complex numbers a + bi
and c + di
, the product is:
§Arguments
self
- The firstComplex
number.other
- The secondComplex
number to multiply.
§Returns
A new Complex
number which is the product of self
and other
.
§Example
use matrix::Complex;
let a = Complex::new(3.0, 4.0);
let b = Complex::new(1.0, 2.0);
assert_eq!(a * b, Complex::new(-5.0, 10.0));
source§impl MulAssign for Complex
impl MulAssign for Complex
source§fn mul_assign(&mut self, other: Self)
fn mul_assign(&mut self, other: Self)
Multiplies self
by another Complex
number.
source§impl PartialEq for Complex
impl PartialEq for Complex
source§impl PartialOrd for Complex
impl PartialOrd for Complex
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read moresource§impl Scalar for Complex
impl Scalar for Complex
source§impl Sub for Complex
impl Sub for Complex
source§fn sub(self, other: Self) -> Self::Output
fn sub(self, other: Self) -> Self::Output
Subtracts one Complex
number from another.
Given two complex numbers a + bi
and c + di
, the difference is:
§Arguments
self
- TheComplex
number to be subtracted from.other
- TheComplex
number to subtract.
§Returns
A new Complex
number which is the difference of self
and other
.
§Example
use matrix::Complex;
let a = Complex::new(3.0, 4.0);
let b = Complex::new(1.0, 2.0);
assert_eq!(a - b, Complex::new(2.0, 2.0));
source§impl SubAssign for Complex
impl SubAssign for Complex
source§fn sub_assign(&mut self, other: Self)
fn sub_assign(&mut self, other: Self)
Subtracts another Complex
number from self
.
impl Copy for Complex
impl StructuralPartialEq for Complex
Auto Trait Implementations§
impl Freeze for Complex
impl RefUnwindSafe for Complex
impl Send for Complex
impl Sync for Complex
impl Unpin for Complex
impl UnwindSafe for Complex
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
source§impl<T> CloneToUninit for Twhere
T: Copy,
impl<T> CloneToUninit for Twhere
T: Copy,
source§unsafe fn clone_to_uninit(&self, dst: *mut T)
unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
source§default unsafe fn clone_to_uninit(&self, dst: *mut T)
default unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)