pub struct Complex { /* private fields */ }
Expand description
Describes a complex number in both cartesian and polar form.
Implementations§
Source§impl Complex
impl Complex
Sourcepub fn new_cartesian(re: f64, im: f64) -> Self
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);
Sourcepub fn new_polar(mag: f64, ang: f64) -> Self
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);
Sourcepub fn new_real(re: f64) -> Self
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());
Sourcepub fn new_imaginary(im: f64) -> Self
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());
Sourcepub fn zero() -> Self
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());
Sourcepub fn one() -> Self
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§impl Complex
impl Complex
Sourcepub fn cartesian(&self) -> ComplexCartesian
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);
Sourcepub fn polar(&self) -> ComplexPolar
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);
Sourcepub fn re(&self) -> f64
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);
Sourcepub fn im(&self) -> f64
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§impl Complex
impl Complex
Sourcepub fn opposite(self) -> Self
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());
Sourcepub fn reciprocal(self) -> Option<Self>
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
impl Complex
Source§impl Complex
impl Complex
Sourcepub fn pow(self, other: Self) -> Option<Self>
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)?;