Skip to content

Commit 5d9d4c3

Browse files
Merge pull request #376 from marvin-hansen/main
feat(deep_causality_num): Added new Quaternion type accompanied by a comprehensive suite.
2 parents d61ba78 + d7ec364 commit 5d9d4c3

39 files changed

+2811
-0
lines changed

deep_causality_num/README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ Custom numerical traits with default implementation for the [DeepCausality proje
3333

3434
This crate provides a reduced custom implementation of the `rust-num` main traits with the following properties:
3535

36+
### Implemented Types
37+
38+
* `Complex`
39+
* `Quaternion`
40+
3641
* Zero external dependencies
3742
* Zero unsafe
3843
* Minimal macros (only used for testing)
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
//! This module defines common type aliases used throughout the crate.
2+
3+
pub type Vector3<F> = [F; 3];
4+
pub type Matrix3<F> = [[F; 3]; 3];

deep_causality_num/src/complex/mod.rs

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,13 @@ mod num_cast;
2121
mod part_ord;
2222
mod 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`.
2431
pub trait ComplexNumber<F>: Sized
2532
where
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)]
60159
pub struct Complex<F>
61160
where

deep_causality_num/src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
1+
mod alias;
12
mod cast;
23
mod complex;
34
pub mod float;
45
mod float_option;
56
mod identity;
67
pub mod num;
78
mod ops;
9+
mod quaternion;
810
pub mod utils_tests;
911

12+
pub use crate::alias::{Matrix3, Vector3};
1013
pub use crate::cast::as_primitive::AsPrimitive;
1114
pub use crate::cast::as_scalar::float_as_scalar_impl::FloatAsScalar;
1215
pub use crate::cast::as_scalar::int_as_scalar_impl::IntAsScalar;
@@ -21,3 +24,4 @@ pub use crate::identity::one::{ConstOne, One};
2124
pub use crate::identity::zero::{ConstZero, Zero};
2225
pub use crate::num::Num;
2326
pub use crate::ops::num_ops::*;
27+
pub use crate::quaternion::Quaternion;

0 commit comments

Comments
 (0)