Struct lib_rapid::math::complex::ComplexNumber
source · pub struct ComplexNumber<T> {
pub real: T,
pub complex: T,
}
Expand description
The generic Complex number type.
Examples
use lib_rapid::math::complex::ComplexNumber;
use lib_rapid::math::general::delta;
let mut c1 = ComplexNumber::new(4.0, 2.8);
let mut c2 = ComplexNumber::new(2.0, 3.8);
let mut complex = c1.clone();
let mut expected;
// Addition
expected = ComplexNumber::new(6.0, 6.6);
assert_eq!(c1 + c2, expected);
complex += c2;
assert_eq!(complex, expected);
// Subtraction
expected = ComplexNumber::new(-2.0, 1.0);
complex = c2.clone();
complex -= c1;
assert_eq!(c2 - c1, expected);
assert_eq!(complex, expected);
// Multiplication
expected = ComplexNumber::new(-2.64, 20.8);
complex = c1.clone();
complex *= c2;
assert!( delta( (c1 * c2).real, expected.real ) < 1e-10);
assert!( delta( (c1 * c2).complex, expected.complex ) < 1e-10);
assert!( delta( complex.real, expected.real ) < 1e-10);
assert!( delta( complex.complex, expected.complex ) < 1e-10);
// Multiplication
c1 = ComplexNumber::new(2.0, 2.0);
c2 = ComplexNumber::new(4.0, 3.0);
expected = ComplexNumber::new(0.56, 0.08);
complex = c1.clone();
complex /= c2;
assert!( delta( (c1 / c2).real, expected.real ) < 1e-10);
assert!( delta( (c1 / c2).complex, expected.complex ) < 1e-10);
assert!( delta( complex.real, expected.real ) < 1e-10);
assert!( delta( complex.complex, expected.complex ) < 1e-10);
// Convert from real to complex
assert_eq!(ComplexNumber::from(5.0), ComplexNumber::new(5.0, 0.0));
Fields§
§real: T
§complex: T
Implementations§
source§impl<T: Neg<Output = T> + Div<Output = T> + Mul<Output = T> + Sub<Output = T> + Add<Output = T> + Copy + NumTools<T> + From<u8>> ComplexNumber<T>
impl<T: Neg<Output = T> + Div<Output = T> + Mul<Output = T> + Sub<Output = T> + Add<Output = T> + Copy + NumTools<T> + From<u8>> ComplexNumber<T>
sourcepub fn new(real: T, complex: T) -> ComplexNumber<T>
pub fn new(real: T, complex: T) -> ComplexNumber<T>
Create a new Complex number of the form a + bi
with a,b ∈ ℝ
.
Arguments
real: T
- The real part of a complex number.complex: T
- The real part of a complex number.
Returns
A ComplexNumber<T>
.
Examples
use lib_rapid::math::complex::ComplexNumber;
let c = ComplexNumber::new(2, -4);
assert_eq!(c.to_string(), "2 - 4i".to_owned());
sourcepub fn new_unitc() -> ComplexNumber<T>
pub fn new_unitc() -> ComplexNumber<T>
sourcepub fn recip(&self) -> ComplexNumber<T>
pub fn recip(&self) -> ComplexNumber<T>
sourcepub fn complex_conjugate(&self) -> ComplexNumber<T>
pub fn complex_conjugate(&self) -> ComplexNumber<T>
sourcepub fn powi(&self, pow: isize) -> ComplexNumber<T>
pub fn powi(&self, pow: isize) -> ComplexNumber<T>
sourcepub fn exp_f64(&self) -> ComplexNumber<f64>
pub fn exp_f64(&self) -> ComplexNumber<f64>
Trait Implementations§
source§impl<T: Add<Output = T>> Add for ComplexNumber<T>
impl<T: Add<Output = T>> Add for ComplexNumber<T>
source§impl<T: Add<Output = T> + Copy> AddAssign for ComplexNumber<T>
impl<T: Add<Output = T> + Copy> AddAssign for ComplexNumber<T>
source§fn add_assign(&mut self, rhs: Self)
fn add_assign(&mut self, rhs: Self)
Performs the
+=
operation. Read moresource§impl<T: Clone> Clone for ComplexNumber<T>
impl<T: Clone> Clone for ComplexNumber<T>
source§fn clone(&self) -> ComplexNumber<T>
fn clone(&self) -> ComplexNumber<T>
Returns a copy of the value. Read more
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source
. Read moresource§impl<T: Debug> Debug for ComplexNumber<T>
impl<T: Debug> Debug for ComplexNumber<T>
source§impl<T: Display + Neg<Output = T> + From<u8> + PartialOrd + Copy> Display for ComplexNumber<T>
impl<T: Display + Neg<Output = T> + From<u8> + PartialOrd + Copy> Display for ComplexNumber<T>
source§impl<T: Mul<Output = T> + Sub<Output = T> + Add<Output = T> + Div<Output = T> + NumTools<T> + Copy> Div for ComplexNumber<T>
impl<T: Mul<Output = T> + Sub<Output = T> + Add<Output = T> + Div<Output = T> + NumTools<T> + Copy> Div for ComplexNumber<T>
source§impl<T: Mul<Output = T> + Sub<Output = T> + Add<Output = T> + Div<Output = T> + NumTools<T> + Copy> DivAssign for ComplexNumber<T>
impl<T: Mul<Output = T> + Sub<Output = T> + Add<Output = T> + Div<Output = T> + NumTools<T> + Copy> DivAssign for ComplexNumber<T>
source§fn div_assign(&mut self, rhs: Self)
fn div_assign(&mut self, rhs: Self)
Performs the
/=
operation. Read moresource§impl<T: Mul<Output = T> + Sub<Output = T> + Add<Output = T> + Copy> MulAssign for ComplexNumber<T>
impl<T: Mul<Output = T> + Sub<Output = T> + Add<Output = T> + Copy> MulAssign for ComplexNumber<T>
source§fn mul_assign(&mut self, rhs: Self)
fn mul_assign(&mut self, rhs: Self)
Performs the
*=
operation. Read moresource§impl<T: PartialEq> PartialEq for ComplexNumber<T>
impl<T: PartialEq> PartialEq for ComplexNumber<T>
source§fn eq(&self, other: &ComplexNumber<T>) -> bool
fn eq(&self, other: &ComplexNumber<T>) -> bool
This method tests for
self
and other
values to be equal, and is used
by ==
.source§impl<T: PartialEq + PartialOrd> PartialOrd for ComplexNumber<T>
impl<T: PartialEq + PartialOrd> PartialOrd for ComplexNumber<T>
This PartialOrd implementation uses Lexicographical ordering. This means:
- Ordered by real part if a₁ ≠ a₂.
- Ordered by imaginary part if a₁ = a₂.
use lib_rapid::math::complex::ComplexNumber;
assert!(ComplexNumber::new(2, 3) < ComplexNumber::new(3, 4));
assert!(ComplexNumber::new(2, 1) > ComplexNumber::new(2, 0));
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
This method tests less than or equal to (for
self
and other
) and is used by the <=
operator. Read moresource§impl<T: Sub<Output = T>> Sub for ComplexNumber<T>
impl<T: Sub<Output = T>> Sub for ComplexNumber<T>
source§impl<T: Sub<Output = T> + Copy> SubAssign for ComplexNumber<T>
impl<T: Sub<Output = T> + Copy> SubAssign for ComplexNumber<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 ComplexNumber<T>
impl<T: Eq> Eq for ComplexNumber<T>
impl<T> StructuralEq for ComplexNumber<T>
impl<T> StructuralPartialEq for ComplexNumber<T>
Auto Trait Implementations§
impl<T> RefUnwindSafe for ComplexNumber<T>where
T: RefUnwindSafe,
impl<T> Send for ComplexNumber<T>where
T: Send,
impl<T> Sync for ComplexNumber<T>where
T: Sync,
impl<T> Unpin for ComplexNumber<T>where
T: Unpin,
impl<T> UnwindSafe for ComplexNumber<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
source§impl<T> NumTools<T> for T
impl<T> NumTools<T> for T
source§fn is_in_range(&self, start: T, end: T) -> bool
fn is_in_range(&self, start: T, end: T) -> bool
source§fn is_in_range_exclusive(&self, start: T, end: T) -> bool
fn is_in_range_exclusive(&self, start: T, end: T) -> bool
source§fn map_to(&self, start1: T, end1: T, start2: T, end2: T) -> T
fn map_to(&self, start1: T, end1: T, start2: T, end2: T) -> T
Maps a given number of a range onto another range. Read more
source§fn is_negative(&self) -> bool
fn is_negative(&self) -> bool
Determines whether a number is negative. Only implemented for readability. Read more