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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
pub mod epga1d;
pub mod ppga1d;
pub mod hpga1d;
pub mod epga2d;
pub mod ppga2d;
pub mod hpga2d;
pub mod epga3d;
pub mod ppga3d;
pub mod hpga3d;
pub mod simd;
pub mod polynomial;

impl Zero for f32 {
    fn zero() -> Self {
        0.0
    }
}

impl One for f32 {
    fn one() -> Self {
        1.0
    }
}

impl Automorphism for f32 {
    type Output = f32;

    fn automorphism(self) -> f32 {
        self
    }
}

impl Reversal for f32 {
    type Output = f32;

    fn reversal(self) -> f32 {
        self
    }
}

impl Conjugation for f32 {
    type Output = f32;

    fn conjugation(self) -> f32 {
        self
    }
}

impl GeometricProduct<f32> for f32 {
    type Output = f32;

    fn geometric_product(self, other: f32) -> f32 {
        self * other
    }
}

impl OuterProduct<f32> for f32 {
    type Output = f32;

    fn outer_product(self, other: f32) -> f32 {
        self * other
    }
}

impl InnerProduct<f32> for f32 {
    type Output = f32;

    fn inner_product(self, other: f32) -> f32 {
        self * other
    }
}

impl LeftContraction<f32> for f32 {
    type Output = f32;

    fn left_contraction(self, other: f32) -> f32 {
        self * other
    }
}

impl RightContraction<f32> for f32 {
    type Output = f32;

    fn right_contraction(self, other: f32) -> f32 {
        self * other
    }
}

impl ScalarProduct<f32> for f32 {
    type Output = f32;

    fn scalar_product(self, other: f32) -> f32 {
        self * other
    }
}

impl SquaredMagnitude for f32 {
    type Output = f32;

    fn squared_magnitude(self) -> f32 {
        self.scalar_product(self.reversal())
    }
}

impl Magnitude for f32 {
    type Output = f32;

    fn magnitude(self) -> f32 {
        self.abs()
    }
}

impl Signum for f32 {
    type Output = f32;

    fn signum(self) -> f32 {
        f32::signum(self)
    }
}

impl Inverse for f32 {
    type Output = f32;

    fn inverse(self) -> f32 {
        1.0 / self
    }
}

impl GeometricQuotient<f32> for f32 {
    type Output = f32;

    fn geometric_quotient(self, other: f32) -> f32 {
        self.geometric_product(other.inverse())
    }
}

impl Transformation<f32> for f32 {
    type Output = f32;

    fn transformation(self, other: f32) -> f32 {
        self.geometric_product(other)
            .geometric_product(self.reversal())
    }
}

impl epga1d::ComplexNumber {
    pub fn real(self) -> f32 {
        self[0]
    }

    pub fn imaginary(self) -> f32 {
        self[1]
    }

    pub fn from_polar(magnitude: f32, argument: f32) -> Self {
        Self::new(magnitude * argument.cos(), magnitude * argument.sin())
    }

    pub fn arg(self) -> f32 {
        self.imaginary().atan2(self.real())
    }
}

impl Exp for epga1d::ComplexNumber {
    type Output = Self;

    fn exp(self) -> Self {
        Self::from_polar(self[0].exp(), self[1])
    }
}

impl Ln for epga1d::ComplexNumber {
    type Output = Self;

    fn ln(self) -> Self {
        Self::new(self.magnitude().ln(), self.arg())
    }
}

impl Powf for epga1d::ComplexNumber {
    type Output = Self;

    fn powf(self, exponent: f32) -> Self {
        Self::from_polar(self.magnitude().powf(exponent), self.arg() * exponent)
    }
}

impl Exp for ppga2d::IdealPoint {
    type Output = ppga2d::Translator;

    fn exp(self) -> ppga2d::Translator {
        ppga2d::Translator::new(1.0, self[0], self[1])
    }
}

impl Ln for ppga2d::Translator {
    type Output = ppga2d::IdealPoint;

    fn ln(self) -> ppga2d::IdealPoint {
        let result: ppga2d::IdealPoint = self.into();
        result * (1.0 / self[0])
    }
}

impl Powf for ppga2d::Translator {
    type Output = Self;

    fn powf(self, exponent: f32) -> Self {
        (self.ln() * exponent).exp()
    }
}

impl Exp for ppga2d::Point {
    type Output = ppga2d::Motor;

    fn exp(self) -> ppga2d::Motor {
        let det = self[0] * self[0];
        if det <= 0.0 {
            return ppga2d::Motor::new(1.0, 0.0, self[1], self[2]);
        }
        let a = det.sqrt();
        let c = a.cos();
        let s = a.sin() / a;
        let g0 = simd::Simd32x3::from(s) * self.group0();
        ppga2d::Motor::new(c, g0[0], g0[1], g0[2])
    }
}

impl Ln for ppga2d::Motor {
    type Output = ppga2d::Point;

    fn ln(self) -> ppga2d::Point {
        let det = 1.0 - self[0] * self[0];
        if det <= 0.0 {
            return ppga2d::Point::new(0.0, self[2], self[3]);
        }
        let a = 1.0 / det;
        let b = self[0].acos() * a.sqrt();
        let g0 = simd::Simd32x4::from(b) * self.group0();
        ppga2d::Point::new(g0[1], g0[2], g0[3])
    }
}

impl Powf for ppga2d::Motor {
    type Output = Self;

    fn powf(self, exponent: f32) -> Self {
        (self.ln() * exponent).exp()
    }
}

impl Exp for ppga3d::IdealPoint {
    type Output = ppga3d::Translator;

    fn exp(self) -> ppga3d::Translator {
        ppga3d::Translator::new(1.0, self[0], self[1], self[2])
    }
}

impl Ln for ppga3d::Translator {
    type Output = ppga3d::IdealPoint;

    fn ln(self) -> ppga3d::IdealPoint {
        let result: ppga3d::IdealPoint = self.into();
        result * (1.0 / self[0])
    }
}

impl Powf for ppga3d::Translator {
    type Output = Self;

    fn powf(self, exponent: f32) -> Self {
        (self.ln() * exponent).exp()
    }
}

impl Exp for ppga3d::Line {
    type Output = ppga3d::Motor;

    fn exp(self) -> ppga3d::Motor {
        let det = self[3] * self[3] + self[4] * self[4] + self[5] * self[5];
        if det <= 0.0 {
            return ppga3d::Motor::new(1.0, 0.0, 0.0, 0.0, 0.0, self[0], self[1], self[2]);
        }
        let a = det.sqrt();
        let c = a.cos();
        let s = a.sin() / a;
        let m = self[0] * self[3] + self[1] * self[4] + self[2] * self[5];
        let t = m / det * (c - s);
        let g0 = simd::Simd32x3::from(s) * self.group1();
        let g1 = simd::Simd32x3::from(s) * self.group0() + simd::Simd32x3::from(t) * self.group1();
        ppga3d::Motor::new(c, g0[0], g0[1], g0[2], s * m, g1[0], g1[1], g1[2])
    }
}

impl Ln for ppga3d::Motor {
    type Output = ppga3d::Line;

    fn ln(self) -> ppga3d::Line {
        let det = 1.0 - self[0] * self[0];
        if det <= 0.0 {
            return ppga3d::Line::new(self[5], self[6], self[7], 0.0, 0.0, 0.0);
        }
        let a = 1.0 / det;
        let b = self[0].acos() * a.sqrt();
        let c = a * self[4] * (1.0 - self[0] * b);
        let g0 = simd::Simd32x4::from(b) * self.group1() + simd::Simd32x4::from(c) * self.group0();
        let g1 = simd::Simd32x4::from(b) * self.group0();
        ppga3d::Line::new(g0[1], g0[2], g0[3], g1[1], g1[2], g1[3])
    }
}

impl Powf for ppga3d::Motor {
    type Output = Self;

    fn powf(self, exponent: f32) -> Self {
        (self.ln() * exponent).exp()
    }
}

/// All elements set to `0.0`
pub trait Zero {
    fn zero() -> Self;
}

/// All elements set to `0.0`, except for the scalar, which is set to `1.0`
pub trait One {
    fn one() -> Self;
}

/// Element order reversed
pub trait Dual {
    type Output;
    fn dual(self) -> Self::Output;
}

/// Negates elements with `grade % 2 == 1`
///
/// Also called main involution
pub trait Automorphism {
    type Output;
    fn automorphism(self) -> Self::Output;
}

/// Negates elements with `grade % 4 >= 2`
///
/// Also called transpose
pub trait Reversal {
    type Output;
    fn reversal(self) -> Self::Output;
}

/// Negates elements with `(grade + 3) % 4 < 2`
pub trait Conjugation {
    type Output;
    fn conjugation(self) -> Self::Output;
}

/// General multi vector multiplication
pub trait GeometricProduct<T> {
    type Output;
    fn geometric_product(self, other: T) -> Self::Output;
}

/// General multi vector division
pub trait GeometricQuotient<T> {
    type Output;
    fn geometric_quotient(self, other: T) -> Self::Output;
}

/// Dual of the geometric product grade filtered by `t == r + s`
///
/// Also called join
pub trait RegressiveProduct<T> {
    type Output;
    fn regressive_product(self, other: T) -> Self::Output;
}

/// Geometric product grade filtered by `t == r + s`
///
/// Also called meet or exterior product
pub trait OuterProduct<T> {
    type Output;
    fn outer_product(self, other: T) -> Self::Output;
}

/// Geometric product grade filtered by `t == (r - s).abs()`
///
/// Also called fat dot product
pub trait InnerProduct<T> {
    type Output;
    fn inner_product(self, other: T) -> Self::Output;
}

/// Geometric product grade filtered by `t == s - r`
pub trait LeftContraction<T> {
    type Output;
    fn left_contraction(self, other: T) -> Self::Output;
}

/// Geometric product grade filtered by `t == r - s`
pub trait RightContraction<T> {
    type Output;
    fn right_contraction(self, other: T) -> Self::Output;
}

/// Geometric product grade filtered by `t == 0`
pub trait ScalarProduct<T> {
    type Output;
    fn scalar_product(self, other: T) -> Self::Output;
}

/// `self * other * self.reversion()`
///
/// Also called sandwich product
pub trait Transformation<T> {
    type Output;
    fn transformation(self, other: T) -> Self::Output;
}

/// Square of the magnitude
pub trait SquaredMagnitude {
    type Output;
    fn squared_magnitude(self) -> Self::Output;
}

/// Length as scalar
///
/// Also called amplitude, absolute value or norm
pub trait Magnitude {
    type Output;
    fn magnitude(self) -> Self::Output;
}

/// Direction without magnitude (set to scalar `-1.0` or `1.0`)
///
/// Also called sign or normalize
pub trait Signum {
    type Output;
    fn signum(self) -> Self::Output;
}

/// Raises a number to the scalar power of `-1.0`
pub trait Inverse {
    type Output;
    fn inverse(self) -> Self::Output;
}

/// The natural logarithm
pub trait Ln {
    type Output;
    fn ln(self) -> Self::Output;
}

/// The exponential function
pub trait Exp {
    type Output;
    fn exp(self) -> Self::Output;
}

/// Raises a number to an integer scalar power
pub trait Powi {
    type Output;
    fn powi(self, exponent: isize) -> Self::Output;
}

/// Raises a number to an floating point scalar power
pub trait Powf {
    type Output;
    fn powf(self, exponent: f32) -> Self::Output;
}