pub struct Complex<T> {
pub re: T,
pub im: T,
}Expand description
A complex number stored in rectangular form.
Fields§
§re: TThe real component.
im: TThe imaginary component.
Implementations§
Source§impl<T> Complex<T>
impl<T> Complex<T>
Sourcepub const fn new(re: T, im: T) -> Self
pub const fn new(re: T, im: T) -> Self
Creates a complex number from real and imaginary parts.
Examples found in repository?
examples/basic_usage.rs (line 4)
3fn main() {
4 let value = Complex::new(3.0_f64, 4.0_f64);
5 let mirrored = value.conjugate();
6 let lifted = Complex::from(Imaginary::new(4.0_f64));
7 let (magnitude, argument) = value.to_polar();
8
9 assert_eq!(value + Complex::one(), Complex::new(4.0, 4.0));
10 assert_eq!(mirrored, Complex::new(3.0, -4.0));
11 assert_eq!(lifted, Complex::new(0.0, 4.0));
12 assert!((magnitude - 5.0).abs() <= 1.0e-10);
13 assert!((argument - value.argument()).abs() <= 1.0e-10);
14}Source§impl<T> Complex<T>where
T: Default,
impl<T> Complex<T>where
T: Default,
Sourcepub fn from_imaginary(im: T) -> Self
pub fn from_imaginary(im: T) -> Self
Creates an imaginary-only complex value.
Source§impl<T> Complex<T>
impl<T> Complex<T>
Sourcepub fn one() -> Self
pub fn one() -> Self
Returns 1 + 0i.
Examples found in repository?
examples/basic_usage.rs (line 9)
3fn main() {
4 let value = Complex::new(3.0_f64, 4.0_f64);
5 let mirrored = value.conjugate();
6 let lifted = Complex::from(Imaginary::new(4.0_f64));
7 let (magnitude, argument) = value.to_polar();
8
9 assert_eq!(value + Complex::one(), Complex::new(4.0, 4.0));
10 assert_eq!(mirrored, Complex::new(3.0, -4.0));
11 assert_eq!(lifted, Complex::new(0.0, 4.0));
12 assert!((magnitude - 5.0).abs() <= 1.0e-10);
13 assert!((argument - value.argument()).abs() <= 1.0e-10);
14}Source§impl<T> Complex<T>
impl<T> Complex<T>
Sourcepub fn conjugate(&self) -> Self
pub fn conjugate(&self) -> Self
Returns the complex conjugate.
Examples found in repository?
examples/basic_usage.rs (line 5)
3fn main() {
4 let value = Complex::new(3.0_f64, 4.0_f64);
5 let mirrored = value.conjugate();
6 let lifted = Complex::from(Imaginary::new(4.0_f64));
7 let (magnitude, argument) = value.to_polar();
8
9 assert_eq!(value + Complex::one(), Complex::new(4.0, 4.0));
10 assert_eq!(mirrored, Complex::new(3.0, -4.0));
11 assert_eq!(lifted, Complex::new(0.0, 4.0));
12 assert!((magnitude - 5.0).abs() <= 1.0e-10);
13 assert!((argument - value.argument()).abs() <= 1.0e-10);
14}Source§impl<T> Complex<T>
impl<T> Complex<T>
Sourcepub fn magnitude_squared(&self) -> T
pub fn magnitude_squared(&self) -> T
Returns the squared magnitude, re^2 + im^2.
Source§impl Complex<f32>
impl Complex<f32>
Sourcepub fn from_polar(magnitude: f32, argument: f32) -> Self
pub fn from_polar(magnitude: f32, argument: f32) -> Self
Builds a complex value from polar coordinates.
Source§impl Complex<f64>
impl Complex<f64>
Sourcepub fn argument(&self) -> f64
pub fn argument(&self) -> f64
Returns the argument in radians.
Examples found in repository?
examples/basic_usage.rs (line 13)
3fn main() {
4 let value = Complex::new(3.0_f64, 4.0_f64);
5 let mirrored = value.conjugate();
6 let lifted = Complex::from(Imaginary::new(4.0_f64));
7 let (magnitude, argument) = value.to_polar();
8
9 assert_eq!(value + Complex::one(), Complex::new(4.0, 4.0));
10 assert_eq!(mirrored, Complex::new(3.0, -4.0));
11 assert_eq!(lifted, Complex::new(0.0, 4.0));
12 assert!((magnitude - 5.0).abs() <= 1.0e-10);
13 assert!((argument - value.argument()).abs() <= 1.0e-10);
14}Sourcepub fn from_polar(magnitude: f64, argument: f64) -> Self
pub fn from_polar(magnitude: f64, argument: f64) -> Self
Builds a complex value from polar coordinates.
Sourcepub fn to_polar(&self) -> (f64, f64)
pub fn to_polar(&self) -> (f64, f64)
Returns (magnitude, argument) in polar form.
Examples found in repository?
examples/basic_usage.rs (line 7)
3fn main() {
4 let value = Complex::new(3.0_f64, 4.0_f64);
5 let mirrored = value.conjugate();
6 let lifted = Complex::from(Imaginary::new(4.0_f64));
7 let (magnitude, argument) = value.to_polar();
8
9 assert_eq!(value + Complex::one(), Complex::new(4.0, 4.0));
10 assert_eq!(mirrored, Complex::new(3.0, -4.0));
11 assert_eq!(lifted, Complex::new(0.0, 4.0));
12 assert!((magnitude - 5.0).abs() <= 1.0e-10);
13 assert!((argument - value.argument()).abs() <= 1.0e-10);
14}Trait Implementations§
Source§impl<T: PartialEq> PartialEq for Complex<T>
impl<T: PartialEq> PartialEq for Complex<T>
impl<T: Copy> Copy for Complex<T>
impl<T: Eq> Eq for Complex<T>
impl<T> StructuralPartialEq for Complex<T>
Auto Trait Implementations§
impl<T> Freeze for Complex<T>where
T: Freeze,
impl<T> RefUnwindSafe for Complex<T>where
T: RefUnwindSafe,
impl<T> Send for Complex<T>where
T: Send,
impl<T> Sync for Complex<T>where
T: Sync,
impl<T> Unpin for Complex<T>where
T: Unpin,
impl<T> UnsafeUnpin for Complex<T>where
T: UnsafeUnpin,
impl<T> UnwindSafe for Complex<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