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>
where f64: From<T>,

source

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());
source

pub fn new_unitc() -> ComplexNumber<T>

Create a new Complex number with the values 0 + 1i.

Returns

A ComplexNumber<T>.

Examples
use lib_rapid::math::complex::ComplexNumber;
 
let c = ComplexNumber::new(0, 1);
assert_eq!(c, ComplexNumber::new_unitc());
source

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

Calculate the reciprocal of a complex number.

Returns

A ComplexNumber<T>.

Examples
use lib_rapid::math::complex::ComplexNumber;
 
let c = ComplexNumber::new(2.0, -4.0);
assert_eq!(c.recip(), ComplexNumber::new(0.1, 0.2));
source

pub fn complex_conjugate(&self) -> ComplexNumber<T>

Calculate the complex conjugage of a complex number.

Returns

A ComplexNumber<T>.

Examples
use lib_rapid::math::complex::ComplexNumber;
 
let c = ComplexNumber::new(2, -4);
assert_eq!(c.complex_conjugate(), ComplexNumber::new(2, 4));
source

pub fn abs_f64(&self) -> f64

Calculate the absolute value of a complex number.

Returns

A f64.

Examples
use lib_rapid::math::complex::ComplexNumber;
use lib_rapid::math::constants::SQRT5;
 
let c = ComplexNumber::new(2, -4);
assert!((2.0 * SQRT5 - c.abs_f64()).abs() < 1e-10);
source

pub fn abs_f32(&self) -> f32

Calculate the absolute value of a complex number.

Returns

A f32.

Examples
use lib_rapid::math::complex::ComplexNumber;
use lib_rapid::math::constants::SQRT5;
 
let c = ComplexNumber::new(2, -4);
assert!((2.0 * SQRT5 as f32 - c.abs_f32()).abs() < 1e-10);
source

pub fn powi(&self, pow: isize) -> ComplexNumber<T>

Raise self to a whole number power pow.

Arguments
  • pow: isize - the power.
Returns

A ComplexNumber<T>.

Examples
use lib_rapid::math::complex::ComplexNumber;
 
let c = ComplexNumber::new(2, -4);
assert_eq!(c.powi(2), ComplexNumber::new(-12, -16));
source

pub fn exp_f64(&self) -> ComplexNumber<f64>

Raise e to a imaginary number self.

Returns

A ComplexNumber<f64>.

Examples
use lib_rapid::math::complex::ComplexNumber;
use lib_rapid::math::constants::{PI, E};
 
let c = ComplexNumber::new(PI, PI);
assert!((c.exp_f64().real + E.powf(PI)).abs() < 1e-10);

Trait Implementations§

source§

impl<T: Add<Output = T>> Add for ComplexNumber<T>

§

type Output = ComplexNumber<T>

The resulting type after applying the + operator.
source§

fn add(self, rhs: Self) -> Self::Output

Performs the + operation. Read more
source§

impl<T: Add<Output = T> + Copy> AddAssign for ComplexNumber<T>

source§

fn add_assign(&mut self, rhs: Self)

Performs the += operation. Read more
source§

impl<T: Clone> Clone for ComplexNumber<T>

source§

fn clone(&self) -> ComplexNumber<T>

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl<T: Debug> Debug for ComplexNumber<T>

source§

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

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

impl<T: Display + Neg<Output = T> + From<u8> + PartialOrd + Copy> Display for ComplexNumber<T>

source§

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

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

impl<T: Mul<Output = T> + Sub<Output = T> + Add<Output = T> + Div<Output = T> + NumTools<T> + Copy> Div for ComplexNumber<T>

§

type Output = ComplexNumber<T>

The resulting type after applying the / operator.
source§

fn div(self, rhs: Self) -> Self::Output

Performs the / operation. Read more
source§

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)

Performs the /= operation. Read more
source§

impl<T: From<u8>> From<T> for ComplexNumber<T>

source§

fn from(_a: T) -> Self

Converts to this type from the input type.
source§

impl<T: Mul<Output = T> + Sub<Output = T> + Add<Output = T> + Copy> Mul for ComplexNumber<T>

§

type Output = ComplexNumber<T>

The resulting type after applying the * operator.
source§

fn mul(self, rhs: Self) -> Self::Output

Performs the * operation. Read more
source§

impl<T: Mul<Output = T> + Sub<Output = T> + Add<Output = T> + Copy> MulAssign for ComplexNumber<T>

source§

fn mul_assign(&mut self, rhs: Self)

Performs the *= operation. Read more
source§

impl<T: PartialEq> PartialEq for ComplexNumber<T>

source§

fn eq(&self, other: &ComplexNumber<T>) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

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));
source§

fn partial_cmp(&self, other: &Self) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · source§

fn lt(&self, other: &Rhs) -> bool

This method tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · source§

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 more
1.0.0 · source§

fn gt(&self, other: &Rhs) -> bool

This method tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · source§

fn ge(&self, other: &Rhs) -> bool

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more
source§

impl<T: Sub<Output = T>> Sub for ComplexNumber<T>

§

type Output = ComplexNumber<T>

The resulting type after applying the - operator.
source§

fn sub(self, rhs: Self) -> Self::Output

Performs the - operation. Read more
source§

impl<T: Sub<Output = T> + Copy> SubAssign for ComplexNumber<T>

source§

fn sub_assign(&mut self, rhs: Self)

Performs the -= operation. Read more
source§

impl<T: Copy> Copy for ComplexNumber<T>

source§

impl<T: Eq> Eq for ComplexNumber<T>

source§

impl<T> StructuralEq for ComplexNumber<T>

source§

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> 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> 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> NumTools<T> for T
where T: Add<Output = T> + PartialOrd + Div<Output = T> + Sub<Output = T> + Mul<Output = T> + From<u8> + Copy + SubAssign + AddAssign + MulAssign,

source§

fn is_in_range(&self, start: T, end: T) -> bool

Determine whether self is in the interval [start; end]. Read more
source§

fn is_in_range_exclusive(&self, start: T, end: T) -> bool

Determine whether self is in the interval (start; end). Read more
source§

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 inc(&mut self)

Increment a number by one. Read more
source§

fn inc_by(&mut self, n: T)

Increment a number by a specified value. Read more
source§

fn dec(&mut self)

Decrement a number by one. Read more
source§

fn dec_by(&mut self, n: T)

Decrement a number by a specified value. Read more
source§

fn square(&self) -> T

Square a number. Read more
source§

fn cube(&self) -> T

Cube a number. Read more
source§

fn recip(&self) -> T

Calculates the reciprocal ( 1 ÷ number ) of a given number. Read more
source§

fn pow(&self, power: usize) -> T

Calculates the nth power of a given number. Read more
source§

fn is_negative(&self) -> bool

Determines whether a number is negative. Only implemented for readability. Read more
source§

impl<T> ToOwned for T
where T: Clone,

§

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§

default 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>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

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.