1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
//! Axial vectors.

use std::fmt;

use approx;
use derive_builder::Builder;
use nalgebra::Vector3;
use ndarray_linalg::types::Lapack;
use num_complex::{Complex, ComplexFloat};
use num_traits::float::{Float, FloatConst};

#[cfg(test)]
#[path = "axialvector_tests.rs"]
mod axialvector_tests;

#[path = "axialvector_analysis.rs"]
pub mod axialvector_analysis;
#[path = "axialvector_transformation.rs"]
mod axialvector_transformation;

/// Enumerated type to handle the two possible time parities.
#[derive(Debug, PartialEq, Eq, Clone)]
pub enum TimeParity {
    /// Variant for time-even.
    Even,

    /// Variant for time-odd.
    Odd,
}

// -------
// Display
// -------
impl fmt::Display for TimeParity {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            TimeParity::Even => write!(f, "time-even"),
            TimeParity::Odd => write!(f, "time-odd"),
        }
    }
}

// ==================
// Struct definitions
// ==================

/// Structure to manage axial vectors in three dimensions.
#[derive(Builder, Clone)]
pub struct AxialVector3<T>
where
    T: ComplexFloat + Lapack,
{
    /// The $`(x, y, z)`$ components of this axial vector.
    components: Vector3<T>,

    /// The time parity of this axial vector.
    time_parity: TimeParity,

    /// The threshold for comparing determinants.
    threshold: <T as ComplexFloat>::Real,
}

impl<T> AxialVector3<T>
where
    T: ComplexFloat + Clone + Lapack,
{
    /// Returns a builder to construct a new [`AxialVector3`].
    pub fn builder() -> AxialVector3Builder<T> {
        AxialVector3Builder::default()
    }

    /// Returns a shared reference to the components of the axial vector.
    pub fn components(&self) -> &Vector3<T> {
        &self.components
    }

    /// Returns a shared reference to the time parity.
    pub fn time_parity(&self) -> &TimeParity {
        &self.time_parity
    }

    /// Returns the threshold with which axial vectors are compared.
    pub fn threshold(&self) -> <T as ComplexFloat>::Real {
        self.threshold
    }
}

// =====================
// Trait implementations
// =====================

// ----
// From
// ----
impl<T> From<AxialVector3<T>> for AxialVector3<Complex<T>>
where
    T: Float + FloatConst + Lapack,
    Complex<T>: Lapack,
{
    fn from(value: AxialVector3<T>) -> Self {
        AxialVector3::<Complex<T>>::builder()
            .components(value.components.map(Complex::from))
            .time_parity(value.time_parity.clone())
            .threshold(value.threshold)
            .build()
            .expect("Unable to construct a complex axial vector in three dimensions.")
    }
}

// ---------
// PartialEq
// ---------
impl<T> PartialEq for AxialVector3<T>
where
    T: ComplexFloat<Real = f64> + Lapack,
{
    fn eq(&self, other: &Self) -> bool {
        let thresh = (self.threshold * other.threshold).sqrt();
        let components_eq = approx::relative_eq!(
            (&self.components - &other.components)
                .map(|x| ComplexFloat::abs(x).powi(2))
                .sum()
                .sqrt(),
            0.0,
            epsilon = thresh,
            max_relative = thresh,
        );
        self.time_parity == other.time_parity && components_eq
    }
}

// --
// Eq
// --
impl<T> Eq for AxialVector3<T> where T: ComplexFloat<Real = f64> + Lapack {}

// -----
// Debug
// -----
impl<T> fmt::Debug for AxialVector3<T>
where
    T: fmt::Debug + ComplexFloat + Lapack,
{
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "AxialVector3({})({:+.7}, {:+.7}, {:+.7})",
            self.time_parity, self.components[0], self.components[1], self.components[2],
        )
    }
}

// -------
// Display
// -------
impl<T> fmt::Display for AxialVector3<T>
where
    T: fmt::Display + ComplexFloat + Lapack,
{
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "AxialVector3({})({:+.3}, {:+.3}, {:+.3})",
            self.time_parity, self.components[0], self.components[1], self.components[2],
        )
    }
}