Struct Complex

Source
pub struct Complex { /* private fields */ }
Expand description

Describes a complex number in both cartesian and polar form.

Implementations§

Source§

impl Complex

Source

pub fn new_cartesian(re: f64, im: f64) -> Self

Creates a complex number from its cartesian parts.

§Examples
use complex_stuff::Complex;

let complex = Complex::new_cartesian(5.0, 3.0);
Source

pub fn new_polar(mag: f64, ang: f64) -> Self

Creates a complex number from its polar parts.

§Examples
use std::f64::consts::PI;
use complex_stuff::Complex;

let complex = Complex::new_polar(5.0, PI / 2.0);
Source

pub fn new_real(re: f64) -> Self

Creates a complex number just from its real cartesian part leaving the imaginary part 0.

§Examples
use complex_stuff::Complex;

let complex = Complex::new_real(5.0);

assert_eq!(5.0, complex.re());
assert_eq!(0.0, complex.im());
Source

pub fn new_imaginary(im: f64) -> Self

Creates a complex number just from its imaginary cartesian part leaving the real part 0.

§Examples
use complex_stuff::Complex;

let complex = Complex::new_imaginary(3.0);

assert_eq!(0.0, complex.re());
assert_eq!(3.0, complex.im());
Source

pub fn zero() -> Self

Creates a complex number representing the value 0.

§Examples
use complex_stuff::Complex;

let complex = Complex::zero();

assert_eq!(0.0, complex.re());
assert_eq!(0.0, complex.im());
Source

pub fn one() -> Self

Creates a complex number representing the value 1.

§Examples
use complex_stuff::Complex;

let complex = Complex::one();

assert_eq!(1.0, complex.re());
assert_eq!(0.0, complex.im());
Source

pub fn i() -> Self

Creates a complex number representing the value i.

§Examples
use complex_stuff::Complex;

let complex = Complex::i();

assert_eq!(0.0, complex.re());
assert_eq!(1.0, complex.im());
Source

pub fn e() -> Self

Creates a complex number representing the value e.

§Examples
use std::f64::consts::E;
use complex_stuff::Complex;

let complex = Complex::e();

assert_eq!(E, complex.re());
assert_eq!(0.0, complex.im());
Source§

impl Complex

Source

pub fn cartesian(&self) -> ComplexCartesian

Returns the complex number in cartesian form.

§Examples
use complex_stuff::Complex;

let complex = Complex::new_cartesian(5.0, 3.0);

let cartesian = complex.cartesian();

assert_eq!(5.0, cartesian.re);
assert_eq!(3.0, cartesian.im);
Source

pub fn polar(&self) -> ComplexPolar

Returns the complex number in polar form.

§Examples
use std::f64::consts::PI;
use complex_stuff::Complex;

let complex = Complex::new_polar(5.0, PI / 2.0);

let polar = complex.polar();

assert_eq!(5.0, polar.mag);
assert_eq!(PI / 2.0, polar.ang);
Source

pub fn re(&self) -> f64

Returns just the real part of the complex numbers cartesian form.

§Examples
use complex_stuff::Complex;

let complex = Complex::new_cartesian(5.0, 3.0);

let real = complex.re();

assert_eq!(5.0, real);
Source

pub fn im(&self) -> f64

§Examples
use complex_stuff::Complex;

let complex = Complex::new_cartesian(5.0, 3.0);

let imaginary = complex.im();

assert_eq!(3.0, imaginary);

Returns just the imaginary part of the complex numbers cartesian form.

Source

pub fn mag(&self) -> f64

Returns just the magnitude of the complex numbers polar form.

§Examples
use std::f64::consts::PI;
use complex_stuff::Complex;

let complex = Complex::new_polar(5.0, PI / 2.0);

let magnitude = complex.mag();

assert_eq!(5.0, magnitude);
Source

pub fn ang(&self) -> f64

Returns just the angle of the complex numbers polar form.

§Examples
use std::f64::consts::PI;
use complex_stuff::Complex;

let complex = Complex::new_polar(5.0, PI / 2.0);

let angle = complex.ang();

assert_eq!(PI / 2.0, angle);
Source§

impl Complex

Source

pub fn opposite(self) -> Self

Returns the negation of the complex number. Same as using the unary negation operator -.

§Examples
use complex_stuff::Complex;

let complex = Complex::new_cartesian(5.0, 3.0);

let opposite = complex.opposite();

assert_eq!(-5.0, opposite.re());
assert_eq!(-3.0, opposite.im());
Source

pub fn reciprocal(self) -> Option<Self>

Returns the reciprocal of the complex number.

§Examples
use complex_stuff::Complex;

let complex = Complex::new_cartesian(5.0, 0.0);

let reciprocal = complex.reciprocal();
Source§

impl Complex

Source

pub fn ln(self) -> Option<Self>

Returns the natural logarithm of the complex number.

Returns None when the result is undefined or infinite.

§Examples
use complex_stuff::Complex;

let complex = Complex::new_cartesian(5.0, 3.0);

let result = complex.ln()?;
Source

pub fn log(self, other: Self) -> Option<Self>

Returns the logarithm to any other base of the complex number.

Returns None when the result is undefined or infinite.

§Examples
use complex_stuff::Complex;

let complex = Complex::new_cartesian(5.0, 3.0);
let base = Complex::new_cartesian(7.0, 10.0);

let result = complex.log(base)?;
Source§

impl Complex

Source

pub fn pow(self, other: Self) -> Option<Self>

Raises the complex number to any other complex number and returns the result.

Returns None when the result is undefined or infinite.

§Examples
use complex_stuff::Complex;

let complex = Complex::new_cartesian(5.0, 3.0);
let exponent = Complex::new_cartesian(2.0, 1.0);

let result = complex.pow(exponent)?;
Source

pub fn root(self, other: Self) -> Option<Self>

Retuns the nth root of the complex number with n being any other complex number.

Returns None when the result is undefined or infinite.

§Examples
use complex_stuff::Complex;

let complex = Complex::new_cartesian(5.0, 3.0);
let n = Complex::new_cartesian(2.0, 1.0);

let result = complex.root(n)?;
Source§

impl Complex

Source

pub fn sin(self) -> Option<Self>

Returns the sine of the complex number.

Returns None when the result is undefined or infinite.

§Examples
use complex_stuff::Complex;

let complex = Complex::new_cartesian(5.0, 3.0);

let result = complex.sin()?;
Source

pub fn cos(self) -> Option<Self>

Returns the cosine of the complex number.

Returns None when the result is undefined or infinite.

§Examples
use complex_stuff::Complex;

let complex = Complex::new_cartesian(5.0, 3.0);

let result = complex.cos()?;

Trait Implementations§

Source§

impl Add for Complex

Source§

fn add(self, other: Self) -> Self

Performs the + operation.

§Examples
use complex_stuff::Complex;

let c1 = Complex::new_cartesian(5.0, 3.0);
let c2 = Complex::new_cartesian(1.0, 2.0);

let sum = c1 + c2;

assert_eq!(6.0, sum.re());
assert_eq!(5.0, sum.im());
Source§

type Output = Complex

The resulting type after applying the + operator.
Source§

impl Clone for Complex

Source§

fn clone(&self) -> Complex

Returns a duplicate 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 Debug for Complex

Source§

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

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

impl Display for Complex

Source§

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

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

impl Div for Complex

Source§

fn div(self, other: Self) -> Option<Self>

Performs the / operation.

Returns None when the result is undefined or infinite.

§Examples
use complex_stuff::Complex;

let c1 = Complex::new_polar(5.0, 2.0);
let c2 = Complex::new_polar(2.0, 1.0);

let quotient = (c1 / c2)?;

assert_eq!(2.5, product.mag());
assert_eq!(1.0, product.ang());
Source§

type Output = Option<Complex>

The resulting type after applying the / operator.
Source§

impl Mul for Complex

Source§

fn mul(self, other: Self) -> Self

Performs the * operation.

§Examples
use complex_stuff::Complex;

let c1 = Complex::new_polar(5.0, 2.0);
let c2 = Complex::new_polar(2.0, 1.0);

let product = c1 * c2;

assert_eq!(10.0, product.mag());
assert_eq!(3.0, product.ang());
Source§

type Output = Complex

The resulting type after applying the * operator.
Source§

impl Neg for Complex

Source§

type Output = Complex

The resulting type after applying the - operator.
Source§

fn neg(self) -> Self

Performs the unary - operation. Read more
Source§

impl Sub for Complex

Source§

fn sub(self, other: Self) -> Self

Performs the - operation.

§Examples
use complex_stuff::Complex;

let c1 = Complex::new_cartesian(5.0, 3.0);
let c2 = Complex::new_cartesian(1.0, 2.0);

let diff = c1 - c2;

assert_eq!(4.0, diff.re());
assert_eq!(1.0, diff.im());
Source§

type Output = Complex

The resulting type after applying the - operator.
Source§

impl Copy for Complex

Auto Trait Implementations§

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

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§

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

Source§

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

Source§

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.