complexible/complex_numbers/z.rs
1use super::*;
2
3/// Represents a complex number in Cartesian form.
4///
5/// # Fields
6///
7/// * `real` - The real part of the complex number.
8/// * `imaginary` - The imaginary part of the complex number.
9#[derive(Debug)]
10pub struct CartesianComplexNumber {
11 pub real: f64,
12 pub imaginary: f64,
13}
14impl CartesianComplexNumber {
15 /// Creates a new `CartesianComplexNumber` with the given real and imaginary parts.
16 ///
17 /// # Arguments
18 ///
19 /// * `real` - The real part of the complex number.
20 /// * `imaginary` - The imaginary part of the complex number.
21 ///
22 /// # Example
23 ///
24 /// ```
25 /// use complexible::complex_numbers::z::CartesianComplexNumber;
26 ///
27 /// let complex = CartesianComplexNumber::new(1.0, 2.0);
28 /// assert_eq!(complex.real, 1.0);
29 /// assert_eq!(complex.imaginary, 2.0);
30 /// ```
31 pub fn new(real: f64, imaginary: f64) -> CartesianComplexNumber {
32 CartesianComplexNumber { real, imaginary }
33 }
34 /// Converts the `CartesianComplexNumber` to polar form.
35 ///
36 /// # Example
37 ///
38 /// ```
39 /// use complexible::complex_numbers::z::CartesianComplexNumber;
40 ///
41 /// let complex = CartesianComplexNumber { real: 1.0, imaginary: 1.0 };
42 /// let polar = complex.to_polar();
43 /// assert_eq!(polar.magnitude, 1.4142135623730951);
44 /// assert_eq!(polar.angle.d.value, 45.0);
45 /// ```
46 pub fn to_polar(&self) -> PolarComplexNumber {
47 let magnitude = (self.real.powi(2) + self.imaginary.powi(2)).sqrt();
48 let angle = Angle::from_radians(self.imaginary.atan2(self.real));
49 PolarComplexNumber { magnitude, angle }
50 }
51}
52
53
54/// Represents a complex number in polar form.
55///
56/// # Fields
57///
58/// * `magnitude` - The magnitude of the complex number.
59/// * `angle` - The angle of the complex number, represented as an `Angle` struct.
60#[derive(Debug)]
61pub struct PolarComplexNumber {
62 pub magnitude: f64,
63 pub angle: Angle,
64}
65impl PolarComplexNumber {
66 /// Creates a new `PolarComplexNumber` with the given magnitude and angle.
67 ///
68 /// # Arguments
69 ///
70 /// * `magnitude` - The magnitude of the complex number.
71 /// * `angle` - The angle of the complex number, represented as an `Angle` struct.
72 ///
73 /// # Example
74 ///
75 /// ```
76 /// use complexible::complex_numbers::{ * , z::*};
77 ///
78 /// let angle = Angle::from_degrees(45.0);
79 /// let complex = PolarComplexNumber::new(1.0, angle);
80 /// assert_eq!(complex.magnitude, 1.0);
81 /// assert_eq!(complex.angle.d.value, 45.0);
82 /// ```
83 pub fn new(magnitude: f64, angle: Angle) -> PolarComplexNumber {
84 PolarComplexNumber { magnitude, angle }
85 }
86
87 /// Converts the `PolarComplexNumber` to Cartesian form.
88 ///
89 /// # Example
90 ///
91 /// ```
92 /// use complexible::complex_numbers::{ * , z::*};
93 ///
94 /// let angle = Angle::from_degrees(45.0);
95 /// let polar = PolarComplexNumber { magnitude: 1.0, angle };
96 /// let cartesian = polar.to_cartesian();
97 /// assert_eq!(cartesian.real, 0.7071067811865476);
98 /// assert_eq!(cartesian.imaginary, 0.7071067811865476 );
99 /// ```
100 pub fn to_cartesian(&self) -> CartesianComplexNumber {
101 let r = self.angle.r.value;
102 let real = self.magnitude * r.cos();
103 let imaginary = self.magnitude * r.sin();
104 CartesianComplexNumber { real, imaginary }
105 }
106}
107