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
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
//! # Numerical Geometric Algebra (3D)
//!
//! This module provides a `Multivector3D` struct for numerical computations
//! in 3D Geometric Algebra (`G_3`). It implements the geometric product and
//! standard arithmetic operations for multivectors with `f64` components.
use std::ops::Add;
use std::ops::Neg;
use std::ops::Sub;
use serde::Deserialize;
use serde::Serialize;
/// Represents a multivector in 3D Geometric Algebra (`G_3`).
/// Components are: 1 (scalar), e1, e2, e3 (vectors), e12, e23, e31 (bivectors), e123 (pseudoscalar)
#[derive(Debug, Clone, Copy, PartialEq, Default, Serialize, Deserialize)]
pub struct Multivector3D {
/// Scalar component.
pub s: f64,
/// Vector e1 component.
pub v1: f64,
/// Vector e2 component.
pub v2: f64,
/// Vector e3 component.
pub v3: f64,
/// Bivector e12 component.
pub b12: f64,
/// Bivector e23 component.
pub b23: f64,
/// Bivector e31 component.
pub b31: f64,
/// Pseudoscalar e123 component.
pub pss: f64,
}
impl Add for Multivector3D {
type Output = Self;
/// Performs multivector addition.
///
/// Addition is performed component-wise.
fn add(
self,
rhs: Self,
) -> Self {
Self {
s: self.s + rhs.s,
v1: self.v1 + rhs.v1,
v2: self.v2 + rhs.v2,
v3: self.v3 + rhs.v3,
b12: self.b12 + rhs.b12,
b23: self.b23 + rhs.b23,
b31: self.b31 + rhs.b31,
pss: self.pss + rhs.pss,
}
}
}
impl Sub for Multivector3D {
type Output = Self;
/// Performs multivector subtraction.
///
/// Subtraction is performed component-wise.
fn sub(
self,
rhs: Self,
) -> Self {
Self {
s: self.s - rhs.s,
v1: self.v1 - rhs.v1,
v2: self.v2 - rhs.v2,
v3: self.v3 - rhs.v3,
b12: self.b12 - rhs.b12,
b23: self.b23 - rhs.b23,
b31: self.b31 - rhs.b31,
pss: self.pss - rhs.pss,
}
}
}
impl Neg for Multivector3D {
type Output = Self;
/// Performs multivector negation.
///
/// Negation is performed component-wise.
fn neg(self) -> Self {
Self {
s: -self.s,
v1: -self.v1,
v2: -self.v2,
v3: -self.v3,
b12: -self.b12,
b23: -self.b23,
b31: -self.b31,
pss: -self.pss,
}
}
}
/// Implements the geometric product for `Multivector3D`.
///
/// The geometric product is the fundamental product in geometric algebra.
/// It combines the inner (dot) and outer (wedge) products.
/// This implementation uses the full multiplication table for `G_3`,
/// based on `e_i*e_j = -e_j*e_i` for `i != j` and `e_i*e_i = 1`.
impl Multivector3D {
/// Creates a new `Multivector3D` with all components.
#[allow(clippy::too_many_arguments)]
#[must_use]
pub const fn new(
s: f64,
v1: f64,
v2: f64,
v3: f64,
b12: f64,
b23: f64,
b31: f64,
pss: f64,
) -> Self {
Self {
s,
v1,
v2,
v3,
b12,
b23,
b31,
pss,
}
}
/// Returns the reverse of the multivector.
///
/// The reverse operation reverses the order of products of basis vectors.
/// For G3: s, v1, v2, v3 are unchanged; b12, b23, b31, pss are negated.
/// Wait, `reverse(e_i` `e_j`) = `e_j` `e_i` = -`e_i` `e_j`. So bivectors are negated.
/// `reverse(e_1` `e_2` `e_3`) = `e_3` `e_2` `e_1` = -`e_1` `e_2` `e_3`. So pseudoscalar is negated.
#[must_use]
pub fn reverse(self) -> Self {
Self {
s: self.s,
v1: self.v1,
v2: self.v2,
v3: self.v3,
b12: -self.b12,
b23: -self.b23,
b31: -self.b31,
pss: -self.pss,
}
}
/// Returns the Clifford conjugate of the multivector.
///
/// Conjugation combines reversal and grade involution.
#[must_use]
pub fn conjugate(self) -> Self {
Self {
s: self.s,
v1: -self.v1,
v2: -self.v2,
v3: -self.v3,
b12: -self.b12,
b23: -self.b23,
b31: -self.b31,
pss: self.pss,
}
}
/// Returns the squared norm of the multivector (A * reverse(A))_s.
#[must_use]
pub fn norm_sq(self) -> f64 {
self.pss.mul_add(
self.pss,
self.b31.mul_add(
self.b31,
self.b23.mul_add(
self.b23,
self.b12.mul_add(
self.b12,
self.v3.mul_add(
self.v3,
self.v2
.mul_add(self.v2, self.s.mul_add(self.s, self.v1 * self.v1)),
),
),
),
),
)
}
/// Returns the norm of the multivector.
#[must_use]
pub fn norm(self) -> f64 {
self.norm_sq().sqrt()
}
/// Returns the inverse of the multivector, if it exists.
///
/// For simple multivectors (like vectors or blades), this is `A_rev` / |A|^2.
#[must_use]
pub fn inv(self) -> Option<Self> {
let n2 = self.norm_sq();
if n2.abs() < f64::EPSILON {
None
} else {
let rev = self.reverse();
Some(Self {
s: rev.s / n2,
v1: rev.v1 / n2,
v2: rev.v2 / n2,
v3: rev.v3 / n2,
b12: rev.b12 / n2,
b23: rev.b23 / n2,
b31: rev.b31 / n2,
pss: rev.pss / n2,
})
}
}
/// Performs the outer (wedge) product.
#[must_use]
pub fn wedge(
self,
rhs: Self,
) -> Self {
// The wedge product is the grade-increasing part of the geometric product.
// A ^ B = sum_{r,s} <<a>_r <b>_s>_{r+s}
Self {
s: self.s * rhs.s,
v1: self.s.mul_add(rhs.v1, self.v1 * rhs.s),
v2: self.s.mul_add(rhs.v2, self.v2 * rhs.s),
v3: self.s.mul_add(rhs.v3, self.v3 * rhs.s),
b12: self.v2.mul_add(
-rhs.v1,
self.v1
.mul_add(rhs.v2, self.s.mul_add(rhs.b12, self.b12 * rhs.s)),
),
b23: self.v3.mul_add(
-rhs.v2,
self.v2
.mul_add(rhs.v3, self.s.mul_add(rhs.b23, self.b23 * rhs.s)),
),
b31: self.v1.mul_add(
-rhs.v3,
self.v3
.mul_add(rhs.v1, self.s.mul_add(rhs.b31, self.b31 * rhs.s)),
),
pss: self.b31.mul_add(
rhs.v2,
self.b23.mul_add(
rhs.v1,
self.b12.mul_add(
rhs.v3,
self.v3.mul_add(
rhs.b12,
self.v2.mul_add(
rhs.b31,
self.v1
.mul_add(rhs.b23, self.s.mul_add(rhs.pss, self.pss * rhs.s)),
),
),
),
),
),
}
}
/// Performs the inner (dot) product.
#[must_use]
pub fn dot(
self,
rhs: Self,
) -> Self {
// The inner product is the grade-decreasing part of the geometric product.
// A . B = sum_{r,s} <<a>_r <b>_s>_{|r-s|}
Self {
s: (-self.pss).mul_add(
rhs.pss,
(-self.b31).mul_add(
rhs.b31,
(-self.b23).mul_add(
rhs.b23,
self.b12.mul_add(
-rhs.b12,
self.v3.mul_add(
rhs.v3,
self.v2
.mul_add(rhs.v2, self.s.mul_add(rhs.s, self.v1 * rhs.v1)),
),
),
),
),
),
v1: (-self.pss).mul_add(
rhs.b23,
(-self.b23).mul_add(
rhs.pss,
(-self.b31).mul_add(
rhs.v3,
self.b12.mul_add(
rhs.v2,
self.v3.mul_add(
rhs.b31,
self.v2
.mul_add(-rhs.b12, self.s.mul_add(rhs.v1, self.v1 * rhs.s)),
),
),
),
),
),
v2: (-self.pss).mul_add(
rhs.b31,
(-self.b31).mul_add(
rhs.pss,
self.b23.mul_add(
rhs.v3,
(-self.b12).mul_add(
rhs.v1,
self.v3.mul_add(
-rhs.b23,
self.v2
.mul_add(rhs.s, self.s.mul_add(rhs.v2, self.v1 * rhs.b12)),
),
),
),
),
),
v3: (-self.pss).mul_add(
rhs.b12,
(-self.b12).mul_add(
rhs.pss,
self.b31.mul_add(
rhs.v1,
(-self.b23).mul_add(
rhs.v2,
self.v3.mul_add(
rhs.s,
self.v2
.mul_add(rhs.b23, self.s.mul_add(rhs.v3, -(self.v1 * rhs.b31))),
),
),
),
),
),
b12: self.b31.mul_add(
rhs.b23,
self.b23
.mul_add(-rhs.b31, self.s.mul_add(rhs.b12, self.b12 * rhs.s)),
),
b23: self.b31.mul_add(
-rhs.b12,
self.b12
.mul_add(rhs.b31, self.s.mul_add(rhs.b23, self.b23 * rhs.s)),
),
b31: self.b23.mul_add(
rhs.b12,
self.b12
.mul_add(-rhs.b23, self.s.mul_add(rhs.b31, self.b31 * rhs.s)),
),
pss: self.s.mul_add(rhs.pss, self.pss * rhs.s),
}
}
}
impl std::ops::Mul for Multivector3D {
type Output = Self;
fn mul(
self,
rhs: Self,
) -> Self::Output {
Self {
s: (-self.pss).mul_add(
rhs.pss,
(-self.b31).mul_add(
rhs.b31,
(-self.b23).mul_add(
rhs.b23,
self.b12.mul_add(
-rhs.b12,
self.v3.mul_add(
rhs.v3,
self.v2
.mul_add(rhs.v2, self.s.mul_add(rhs.s, self.v1 * rhs.v1)),
),
),
),
),
),
v1: (-self.pss).mul_add(
rhs.b23,
(-self.b23).mul_add(
rhs.pss,
(-self.b31).mul_add(
rhs.v3,
self.b12.mul_add(
rhs.v2,
self.v3.mul_add(
rhs.b31,
self.v2
.mul_add(-rhs.b12, self.s.mul_add(rhs.v1, self.v1 * rhs.s)),
),
),
),
),
),
v2: (-self.pss).mul_add(
rhs.b31,
(-self.b31).mul_add(
rhs.pss,
self.b23.mul_add(
rhs.v3,
(-self.b12).mul_add(
rhs.v1,
self.v3.mul_add(
-rhs.b23,
self.v1
.mul_add(rhs.b12, self.s.mul_add(rhs.v2, self.v2 * rhs.s)),
),
),
),
),
),
v3: (-self.pss).mul_add(
rhs.b12,
(-self.b12).mul_add(
rhs.pss,
self.b31.mul_add(
rhs.v1,
(-self.b23).mul_add(
rhs.v2,
self.v2.mul_add(
rhs.b23,
self.v1
.mul_add(-rhs.b31, self.s.mul_add(rhs.v3, self.v3 * rhs.s)),
),
),
),
),
),
b12: self.b31.mul_add(
rhs.b23,
(-self.b23).mul_add(
rhs.b31,
self.pss.mul_add(
rhs.v3,
self.v3.mul_add(
rhs.pss,
self.v2.mul_add(
-rhs.v1,
self.v1
.mul_add(rhs.v2, self.s.mul_add(rhs.b12, self.b12 * rhs.s)),
),
),
),
),
),
b23: self.b12.mul_add(
rhs.b31,
(-self.b31).mul_add(
rhs.b12,
self.pss.mul_add(
rhs.v1,
self.v1.mul_add(
rhs.pss,
self.v3.mul_add(
-rhs.v2,
self.v2
.mul_add(rhs.v3, self.s.mul_add(rhs.b23, self.b23 * rhs.s)),
),
),
),
),
),
b31: self.b23.mul_add(
rhs.b12,
(-self.b12).mul_add(
rhs.b23,
self.pss.mul_add(
rhs.v2,
self.v2.mul_add(
rhs.pss,
self.v1.mul_add(
-rhs.v3,
self.v3
.mul_add(rhs.v1, self.s.mul_add(rhs.b31, self.b31 * rhs.s)),
),
),
),
),
),
pss: self.b31.mul_add(
rhs.v2,
self.b23.mul_add(
rhs.v1,
self.b12.mul_add(
rhs.v3,
self.v3.mul_add(
rhs.b12,
self.v2.mul_add(
rhs.b31,
self.v1
.mul_add(rhs.b23, self.s.mul_add(rhs.pss, self.pss * rhs.s)),
),
),
),
),
),
}
}
}