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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
use core::{
    ops::{
        Neg, Add, Sub, Mul, Div, Rem,
        AddAssign, SubAssign, MulAssign, DivAssign,
    },
    marker::PhantomData,
};
use num_traits::{Zero, One, Float, Inv, Num};
use super::traits::{Conj, Dot, NormSqr, Norm, NormL1, Algebra};


/// Cayley–Dickson construction, a basic building block.
///
/// Structure takes two type parameters:
/// + The first one, `T`: a scalar type the algebra is built over.
/// + The second one, `U`: is a type of two components of the construction: `re` and `im`.
#[derive(Clone, Copy, PartialEq)]
pub struct Construct<T, U> {
    re: U,
    im: U,
    ph: PhantomData<T>,
}

impl<T, U> Construct<T, U> {
    /// Create from real and imaginary parts.
    pub fn new(re: U, im: U) -> Self {
        Self { re, im, ph: PhantomData }
    }
    /// Split by real and imaginary parts.
    pub fn split(self) -> (U, U) {
        (self.re, self.im)
    }

    pub fn re_ref(&self) -> &U {
        &self.re
    }
    pub fn im_ref(&self) -> &U {
        &self.im
    }
    pub fn re_mut(&mut self) -> &mut U {
        &mut self.re
    }
    pub fn im_mut(&mut self) -> &mut U {
        &mut self.im
    }
}
impl<T, U> Construct<T, U> where U: Clone {
    pub fn re(&self) -> U {
        self.re.clone()
    }
    pub fn im(&self) -> U {
        self.im.clone()
    }
}

impl<T, U> Conj for Construct<T, U> where U: Conj + Neg<Output=U> {
    fn conj(self) -> Self {
        Self::new(self.re.conj(), -self.im)
    }
}

impl<T, U> NormSqr for Construct<T, U> where T: Add<Output=T>, U: NormSqr<Output=T> {
    type Output = T;
    fn norm_sqr(self) -> T {
        self.re.norm_sqr() + self.im.norm_sqr()
    }
}
impl<T, U> Norm for Construct<T, U> where T: Float, Self: NormSqr<Output=T> {
    type Output = T;
    fn norm(self) -> T {
        self.norm_sqr().sqrt()
    }
}
impl<T, U> NormL1 for Construct<T, U> where T: Add<Output=T>, U: NormL1<Output=T> {
    type Output = T;
    fn norm_l1(self) -> T {
        self.re.norm_l1() + self.im.norm_l1()
    }
}

impl<T, U> Neg for Construct<T, U> where U: Neg<Output=U> {
    type Output = Self;
    fn neg(self) -> Self {
        Self::new(-self.re, -self.im)
    }
}

impl<T, U> Add for Construct<T, U> where U: Add<Output=U> {
    type Output = Self;
    fn add(self, other: Self) -> Self::Output {
        Self::new(self.re + other.re, self.im + other.im)
    }
}
impl<T, U> Sub for Construct<T, U> where U: Sub<Output=U> {
    type Output = Self;
    fn sub(self, other: Self) -> Self::Output {
        Self::new(self.re - other.re, self.im - other.im)
    }
}
impl<T, U> Add<T> for Construct<T, U> where U: Add<T, Output=U> {
    type Output = Self;
    fn add(self, other: T) -> Self::Output {
        Self::new(self.re + other, self.im)
    }
}
impl<T, U> Sub<T> for Construct<T, U> where U: Sub<T, Output=U> {
    type Output = Self;
    fn sub(self, other: T) -> Self::Output {
        Self::new(self.re - other, self.im)
    }
}

impl<T, U> Mul<T> for Construct<T, U> where T: Clone, U: Mul<T, Output=U> {
    type Output = Self;
    fn mul(self, other: T) -> Self::Output {
        Self::new(self.re * other.clone(), self.im * other)
    }
}
impl<T, U> Div<T> for Construct<T, U> where T: Clone, U: Div<T, Output=U> {
    type Output = Self;
    fn div(self, other: T) -> Self::Output {
        Self::new(self.re / other.clone(), self.im / other)
    }
}
impl<T, U> Mul for Construct<T, U> where U: Clone + Conj + Mul<Output=U> + Add<Output=U> + Sub<Output=U> {
    type Output = Self;
    fn mul(self, other: Self) -> Self::Output {
        Self::new(
            self.re() * other.re() - other.im().conj() * self.im(),
            other.im() * self.re() + self.im() * other.re().conj(),
        )
    }
}
impl<T, U> Inv for Construct<T, U> where Self: Clone + Conj + NormSqr<Output=T> + Div<T, Output=Self> {
    type Output = Self;
    fn inv(self) -> Self {
        self.clone().conj() / self.norm_sqr()
    }
}
impl<T, U> Div for Construct<T, U> where Self: Inv<Output=Self> + Mul<Output=Self> {
    type Output = Self;
    fn div(self, other: Self) -> Self::Output {
        self * other.inv()
    }
}

impl<T, U> Zero for Construct<T, U> where U: Zero {
    fn zero() -> Self {
        Self::new(U::zero(), U::zero())
    }
    fn is_zero(&self) -> bool {
        self.re.is_zero() && self.im.is_zero()
    }
}
impl<T, U> One for Construct<T, U> where U: Zero + One, Self: Mul<Output=Self> {
    fn one() -> Self {
        Self::new(U::one(), U::zero())
    }
}

impl<T, U> Construct<T, U> where Self: Clone + Norm<Output=T> + Div<T, Output=Self> {
    pub fn normalize(self) -> Self {
        self.clone() / self.norm()
    }
}

impl<T, U> Dot for Construct<T, U> where T: Add<Output=T>, U: Dot<Output=T> {
    type Output = T;
    fn dot(self, other: Self) -> T {
        let (l, r) = (self.split(), other.split());
        l.0.dot(r.0) + l.1.dot(r.1)
    }
}

/// Not implemented yet.
impl<T: Num + Algebra + Clone, U: Num + Algebra<T> + Clone> Rem for Construct<T, U> {
    type Output = Self;
    fn rem(self, _other: Self) -> Self::Output {
        unimplemented!()
    }
}
/// Not implemented yet.
impl<T: Num + Algebra + Clone, U: Num + Algebra<T> + Clone> Num for Construct<T, U> {
    type FromStrRadixErr = ();
    fn from_str_radix(_str: &str, _radix: u32) -> Result<Self, Self::FromStrRadixErr> {
        unimplemented!()
    }
}

impl<T, U> Algebra<T> for Construct<T, U> where T: Algebra + Clone, U: Algebra<T> + Clone {}

macro_rules! radd { ($T:ident) => (
    /// Workaround for reverse addition.
    impl<U> Add<Construct<$T, U>> for $T where Construct<$T, U>: Add<$T, Output=Construct<$T, U>> {
        type Output = Construct<$T, U>;
        fn add(self, other: Construct<$T, U>) -> Self::Output {
            other + self
        }
    }
) }
macro_rules! rsub { ($T:ident) => (
    /// Workaround for reverse subtraction.
    impl<U> Sub<Construct<$T, U>> for $T where Construct<$T, U>: Neg<Output=Construct<$T, U>> + Add<$T, Output=Construct<$T, U>> {
        type Output = Construct<$T, U>;
        fn sub(self, other: Construct<$T, U>) -> Self::Output {
            -other + self
        }
    }
) }
macro_rules! rmul { ($T:ident) => (
    /// Workaround for reverse multiplication.
    impl<U> Mul<Construct<$T, U>> for $T where Construct<$T, U>: Mul<$T, Output=Construct<$T, U>> {
        type Output = Construct<$T, U>;
        fn mul(self, other: Construct<$T, U>) -> Self::Output {
            other*self
        }
    }
) }
macro_rules! rdiv { ($T:ident) => (
    /// Workaround for reverse division.
    impl<U> Div<Construct<$T, U>> for $T where Construct<$T, U>: Inv<Output=Construct<$T, U>> + Mul<$T, Output=Construct<$T, U>> + Clone {
        type Output = Construct<$T, U>;
        fn div(self, other: Construct<$T, U>) -> Self::Output {
            other.inv()*self
        }
    }
) }
macro_rules! reverse { ($T:ident) => (
    radd!($T);
    rsub!($T);
    rmul!($T);
    rdiv!($T);
) }
reverse!(f32);
reverse!(f64);


impl<T, U> AddAssign for Construct<T, U> where U: AddAssign {
    fn add_assign(&mut self, other: Self) -> () {
        self.re += other.re;
        self.im += other.im;
    }
}
impl<T, U> SubAssign for Construct<T, U> where U: SubAssign {
    fn sub_assign(&mut self, other: Self) -> () {
        self.re -= other.re;
        self.im -= other.im;
    }
}
impl<T, U> AddAssign<T> for Construct<T, U> where U: AddAssign<T> {
    fn add_assign(&mut self, other: T) -> () {
        self.re += other;
    }
}
impl<T, U> SubAssign<T> for Construct<T, U> where U: SubAssign<T> {
    fn sub_assign(&mut self, other: T) -> () {
        self.re -= other;
    }
}
impl<T, U> MulAssign<T> for Construct<T, U> where Self: Clone + Mul<T, Output=Self> {
    fn mul_assign(&mut self, other: T) -> () {
        *self = self.clone() * other;
    }
}
impl<T, U> DivAssign<T> for Construct<T, U> where Self: Clone + Div<T, Output=Self> {
    fn div_assign(&mut self, other: T) -> () {
        *self = self.clone() / other;
    }
}
impl<T, U> MulAssign for Construct<T, U> where Self: Clone + Mul<Output=Self> {
    fn mul_assign(&mut self, other: Self) -> () {
        *self = self.clone() * other;
    }
}
impl<T, U> DivAssign for Construct<T, U> where Self: Clone + Div<Output=Self> {
    fn div_assign(&mut self, other: Self) -> () {
        *self = self.clone() / other;
    }
}