@@ -21,6 +21,13 @@ mod num_cast;
2121mod part_ord;
2222mod to_primitive;
2323
24+ /// A trait for types that represent complex numbers.
25+ ///
26+ /// This trait defines the fundamental operations and properties expected of a complex number,
27+ /// such as accessing its real and imaginary parts, computing its magnitude and argument,
28+ /// and calculating its conjugate.
29+ ///
30+ /// Implementors of this trait are expected to provide these operations for a generic float type `F`.
2431pub trait ComplexNumber < F > : Sized
2532where
2633 F : Float ,
@@ -37,25 +44,117 @@ where
3744 + Clone ,
3845{
3946 /// Returns the real part of the complex number.
47+ ///
48+ /// # Examples
49+ ///
50+ /// ```
51+ /// use deep_causality_num::{Complex, ComplexNumber};
52+ ///
53+ /// let c = Complex::new(3.0, 4.0);
54+ /// assert_eq!(c.re(), 3.0);
55+ /// ```
4056 fn re ( & self ) -> F ;
4157
4258 /// Returns the imaginary part of the complex number.
59+ ///
60+ /// # Examples
61+ ///
62+ /// ```
63+ /// use deep_causality_num::{Complex, ComplexNumber};
64+ ///
65+ /// let c = Complex::new(3.0, 4.0);
66+ /// assert_eq!(c.im(), 4.0);
67+ /// ```
4368 fn im ( & self ) -> F ;
4469
4570 /// Computes the squared norm (magnitude squared) of the complex number.
71+ ///
72+ /// For a complex number `z = a + bi`, the squared norm is `a^2 + b^2`.
73+ /// This method avoids the square root operation, making it more efficient
74+ /// when only relative magnitudes are needed.
75+ ///
76+ /// # Examples
77+ ///
78+ /// ```
79+ /// use deep_causality_num::{Complex, ComplexNumber};
80+ ///
81+ /// let c = Complex::new(3.0, 4.0);
82+ /// assert_eq!(c.norm_sqr(), 25.0);
83+ /// ```
4684 fn norm_sqr ( & self ) -> F ;
4785
4886 /// Computes the norm (magnitude or absolute value) of the complex number.
87+ ///
88+ /// For a complex number `z = a + bi`, the norm is `sqrt(a^2 + b^2)`.
89+ ///
90+ /// # Examples
91+ ///
92+ /// ```
93+ /// use deep_causality_num::{Complex, ComplexNumber};
94+ ///
95+ /// let c = Complex::new(3.0, 4.0);
96+ /// assert_eq!(c.norm(), 5.0);
97+ /// ```
4998 fn norm ( & self ) -> F ;
5099
51100 /// Computes the argument (phase angle) of the complex number.
101+ ///
102+ /// The argument is the angle `theta` such that `z = r * (cos(theta) + i * sin(theta))`, where `r` is the norm.
103+ /// It is typically in the range `(-PI, PI]`.
104+ ///
105+ /// # Examples
106+ ///
107+ /// ```
108+ /// use deep_causality_num::{Complex, ComplexNumber};
109+ /// use std::f64::consts::FRAC_PI_4;
110+ ///
111+ /// let c = Complex::new(1.0, 1.0);
112+ /// assert!((c.arg() - FRAC_PI_4).abs() < 1e-9);
113+ /// ```
52114 fn arg ( & self ) -> F ;
53115
54116 /// Computes the complex conjugate of the complex number.
117+ ///
118+ /// For a complex number `z = a + bi`, the conjugate is `a - bi`.
119+ ///
120+ /// # Examples
121+ ///
122+ /// ```
123+ /// use deep_causality_num::{Complex, ComplexNumber};
124+ ///
125+ /// let c = Complex::new(3.0, 4.0);
126+ /// let conj_c = c.conj();
127+ /// assert_eq!(conj_c.re(), 3.0);
128+ /// assert_eq!(conj_c.im(), -4.0);
129+ /// ```
55130 fn conj ( & self ) -> Self ;
56131}
57132
58133/// Represents a complex number with real and imaginary parts.
134+ ///
135+ /// A complex number is a number that can be expressed in the form `a + bi`,
136+ /// where `a` and `b` are real numbers, and `i` is the imaginary unit,
137+ /// satisfying the equation `i^2 = -1`.
138+ ///
139+ /// The `Complex` struct is generic over a float type `F`, allowing it to work
140+ /// with different floating-point precisions (e.g., `f32` or `f64`).
141+ ///
142+ /// # Fields
143+ ///
144+ /// * `re`: The real part of the complex number.
145+ /// * `im`: The imaginary part of the complex number.
146+ ///
147+ /// # Examples
148+ ///
149+ /// ```
150+ /// use deep_causality_num::Complex;
151+ ///
152+ /// let c1 = Complex::new(1.0, 2.0); // Represents 1.0 + 2.0i
153+ /// let c2 = Complex { re: 3.0, im: -1.0 }; // Represents 3.0 - 1.0i
154+ ///
155+ /// assert_eq!(c1.re, 1.0);
156+ /// assert_eq!(c2.im, -1.0);
157+ /// ```
59158#[ derive( Copy , Clone , PartialEq , Default ) ]
60159pub struct Complex < F >
61160where
0 commit comments