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
//! # Geometric Algebra
//!
//! This module provides tools for computations in Clifford and Geometric Algebra.
use std::collections::BTreeMap;
use std::ops::Add;
use std::ops::Mul;
use std::ops::Sub;
use num_bigint::BigInt;
use num_traits::One;
use num_traits::Zero;
use serde::Deserialize;
use serde::Serialize;
use crate::symbolic::core::Expr;
use crate::symbolic::simplify_dag::simplify;
/// Represents a multivector in a Clifford algebra.
///
/// The basis blades are represented by a bitmask. E.g., in 3D:
/// 001 (1) -> e1, 010 (2) -> e2, 100 (4) -> e3
/// 011 (3) -> e12, 101 (5) -> e13, 110 (6) -> e23
/// 111 (7) -> e123 (pseudoscalar)
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct Multivector {
/// A map from the basis blade bitmask to its coefficient.
pub terms: BTreeMap<u32, Expr>,
/// The signature of the algebra, e.g., (p, q, r) for (`e_i^2` = +1, `e_j^2` = -1, `e_k^2` = 0)
pub signature: (u32, u32, u32),
}
impl Multivector {
/// Creates a new, empty multivector for a given algebra signature.
///
/// # Arguments
/// * `signature` - A tuple `(p, q, r)` defining the metric of the algebra, where:
/// - `p` is the number of basis vectors that square to +1.
/// - `q` is the number of basis vectors that square to -1.
/// - `r` is the number of basis vectors that square to 0.
#[must_use]
pub const fn new(signature: (u32, u32, u32)) -> Self {
Self {
terms: BTreeMap::new(),
signature,
}
}
/// Creates a new multivector representing a scalar value.
///
/// A scalar is a grade-0 element of the algebra.
///
/// # Arguments
/// * `signature` - The signature of the algebra `(p, q, r)`.
/// * `value` - The scalar value as an `Expr`.
///
/// # Returns
/// A `Multivector` with a single term for the scalar part (grade 0).
#[must_use]
pub fn scalar(
signature: (u32, u32, u32),
value: Expr,
) -> Self {
let mut terms = BTreeMap::new();
terms.insert(0, value);
Self { terms, signature }
}
/// Creates a new multivector representing a vector (grade-1 element).
///
/// # Arguments
/// * `signature` - The signature of the algebra `(p, q, r)`.
/// * `components` - A vector of coefficients for each basis vector.
///
/// # Returns
/// A `Multivector` representing the vector.
#[must_use]
pub fn vector(
signature: (u32, u32, u32),
components: Vec<Expr>,
) -> Self {
let mut terms = BTreeMap::new();
for (i, coeff) in components.into_iter().enumerate() {
terms.insert(1 << i, coeff);
}
Self { terms, signature }
}
/// Computes the geometric product of this multivector with another.
///
/// The geometric product is the fundamental product of geometric algebra, combining
/// the properties of the inner and outer products. It is associative and distributive
/// but not generally commutative.
///
/// The product of two basis blades `e_A` and `e_B` is computed by considering
/// commutation rules (swaps) and contractions based on the algebra's metric signature.
///
/// # Arguments
/// * `other` - The `Multivector` to multiply with.
///
/// # Returns
/// A new `Multivector` representing the geometric product.
#[must_use]
pub fn geometric_product(
&self,
other: &Self,
) -> Self {
let mut result = Self::new(self.signature);
for (blade1, coeff1) in &self.terms {
for (blade2, coeff2) in &other.terms {
let (sign, metric_scalar, result_blade) = self.blade_product(*blade1, *blade2);
let new_coeff = simplify(&Expr::new_mul(coeff1.clone(), coeff2.clone()));
let signed_coeff = simplify(&Expr::new_mul(Expr::Constant(sign), new_coeff));
let final_coeff = simplify(&Expr::new_mul(signed_coeff, metric_scalar));
if let Some(existing_coeff) = result.terms.get_mut(&result_blade) {
*existing_coeff = simplify(&Expr::new_add(existing_coeff.clone(), final_coeff));
} else {
result.terms.insert(result_blade, final_coeff);
}
}
}
result.prune_zeros();
result
}
/// Helper to prune terms with zero coefficients.
fn prune_zeros(&mut self) {
self.terms.retain(|_, coeff| {
match coeff {
| Expr::Constant(c) => c.abs() > f64::EPSILON,
| Expr::BigInt(b) => !b.is_zero(),
| Expr::Rational(r) => !r.is_zero(),
| Expr::Dag(node) => {
node.to_expr().map_or(true, |expr| {
match expr {
| Expr::Constant(c) => c.abs() > f64::EPSILON,
| Expr::BigInt(b) => !b.is_zero(),
| Expr::Rational(r) => !r.is_zero(),
| _ => true,
}
})
},
| _ => true, // Keep symbolic terms
}
});
}
/// Helper to compute the product of two basis blades.
/// Returns (sign, `metric_scalar`, `resulting_blade`)
pub(crate) fn blade_product(
&self,
b1: u32,
b2: u32,
) -> (f64, Expr, u32) {
let b1_mut = b1;
let mut sign = 1.0;
for i in 0..32 {
if (b2 >> i) & 1 == 1 {
let swaps = (b1_mut >> (i + 1)).count_ones();
if !swaps.is_multiple_of(2) {
sign *= -1.0;
}
}
}
let common_blades = b1 & b2;
let mut metric_scalar = Expr::BigInt(BigInt::one());
for i in 0..32 {
if (common_blades >> i) & 1 == 1 {
let (p, q, _r) = self.signature;
let metric = if i < p {
1i64
} else if i < p + q {
-1i64
} else {
0i64
};
metric_scalar = simplify(&Expr::new_mul(
metric_scalar,
Expr::BigInt(BigInt::from(metric)),
));
}
}
(sign, metric_scalar, b1 ^ b2)
}
/// Extracts all terms of a specific grade from the multivector.
///
/// A multivector is a sum of blades of different grades (scalars are grade 0,
/// vectors are grade 1, bivectors are grade 2, etc.). This function filters
/// the multivector to keep only the terms corresponding to the desired grade.
///
/// # Arguments
/// * `grade` - The grade to project onto (e.g., 0 for scalar, 1 for vector).
///
/// # Returns
/// A new `Multivector` containing only the terms of the specified grade.
#[must_use]
pub fn grade_projection(
&self,
grade: u32,
) -> Self {
let mut result = Self::new(self.signature);
for (blade, coeff) in &self.terms {
if blade.count_ones() == grade {
result.terms.insert(*blade, coeff.clone());
}
}
result
}
/// Computes the outer (or wedge) product of this multivector with another.
///
/// The outer product `A ∧ B` produces a new blade representing the subspace
/// spanned by the subspaces of A and B. It is grade-increasing: `grade(A ∧ B) = grade(A) + grade(B)`.
/// It is defined in terms of the geometric product as the grade-sum part:
/// `A ∧ B = <A B>_{r+s}` where `r=grade(A)` and `s=grade(B)`.
///
/// # Arguments
/// * `other` - The `Multivector` to compute the outer product with.
///
/// # Returns
/// A new `Multivector` representing the outer product.
#[must_use]
pub fn outer_product(
&self,
other: &Self,
) -> Self {
let mut result = Self::new(self.signature);
for r in 0..=self.signature.0 + self.signature.1 {
for s in 0..=other.signature.0 + other.signature.1 {
if r + s > self.signature.0 + self.signature.1 {
continue;
}
let term = self
.grade_projection(r)
.geometric_product(&other.grade_projection(s));
result = result + term.grade_projection(r + s);
}
}
result.prune_zeros();
result
}
/// Computes the inner (or left contraction) product of this multivector with another.
///
/// The inner product `A . B` is a grade-decreasing operation. It is defined in terms
/// of the geometric product as the grade-difference part:
/// `A . B = <A B>_{s-r}` where `r=grade(A)` and `s=grade(B)`.
///
/// # Arguments
/// * `other` - The `Multivector` to compute the inner product with.
///
/// # Returns
/// A new `Multivector` representing the inner product.
#[must_use]
pub fn inner_product(
&self,
other: &Self,
) -> Self {
let mut result = Self::new(self.signature);
for r in 0..=self.signature.0 + self.signature.1 {
for s in 0..=other.signature.0 + other.signature.1 {
if s < r {
continue;
}
let term = self
.grade_projection(r)
.geometric_product(&other.grade_projection(s));
result = result + term.grade_projection(s - r);
}
}
result.prune_zeros();
result
}
/// Computes the reverse of the multivector.
///
/// The reverse operation is found by reversing the order of the vectors in each basis blade.
/// This results in a sign change for any blade `B` depending on its grade `k`:
/// `reverse(B) = (-1)^(k*(k-1)/2) * B`.
///
/// # Returns
/// A new `Multivector` representing the reversed multivector.
#[must_use]
pub fn reverse(&self) -> Self {
let mut result = Self::new(self.signature);
for (blade, coeff) in &self.terms {
let grade = i64::from(blade.count_ones());
let sign = if (grade * (grade - 1) / 2) % 2 == 0 {
1i64
} else {
-1i64
};
result.terms.insert(
*blade,
simplify(&Expr::new_mul(
Expr::BigInt(BigInt::from(sign)),
coeff.clone(),
)),
);
}
result
}
/// Computes the magnitude (norm) of the multivector.
///
/// The magnitude is defined as `sqrt(M * reverse(M))` where the result
/// should be a scalar.
///
/// # Returns
/// An `Expr` representing the magnitude.
#[must_use]
pub fn magnitude(&self) -> Expr {
let product = self.geometric_product(&self.reverse());
let scalar_part = product.grade_projection(0);
scalar_part.terms.get(&0).map_or_else(
|| Expr::Constant(0.0),
|scalar_coeff| simplify(&Expr::new_sqrt(scalar_coeff.clone())),
)
}
/// Computes the dual of the multivector with respect to the pseudoscalar.
///
/// The dual is defined as `M * I^(-1)` where `I` is the pseudoscalar.
///
/// # Returns
/// A new `Multivector` representing the dual.
#[must_use]
pub fn dual(&self) -> Self {
let dimension = self.signature.0 + self.signature.1 + self.signature.2;
let pseudoscalar_blade = (1 << dimension) - 1;
// Create pseudoscalar multivector
let mut pseudoscalar = Self::new(self.signature);
pseudoscalar
.terms
.insert(pseudoscalar_blade, Expr::Constant(1.0));
// Compute dual as M * I^(-1)
// For simplicity, we use M * I (which works for many cases)
self.geometric_product(&pseudoscalar)
}
/// Normalizes the multivector to unit magnitude.
///
/// # Returns
/// A new `Multivector` with unit magnitude.
#[must_use]
pub fn normalize(&self) -> Self {
let mag = self.magnitude();
let inv_mag = Expr::new_div(Expr::Constant(1.0), mag);
self.clone() * inv_mag
}
}
impl Add for Multivector {
type Output = Self;
fn add(
self,
rhs: Self,
) -> Self {
let mut result = self;
for (blade, coeff) in rhs.terms {
if let Some(existing_coeff) = result.terms.get_mut(&blade) {
*existing_coeff = simplify(&Expr::new_add(existing_coeff.clone(), coeff));
} else {
result.terms.insert(blade, coeff);
}
}
result.prune_zeros();
result
}
}
impl Sub for Multivector {
type Output = Self;
fn sub(
self,
rhs: Self,
) -> Self {
let mut result = self;
for (blade, coeff) in rhs.terms {
if let Some(existing_coeff) = result.terms.get_mut(&blade) {
*existing_coeff = simplify(&Expr::new_sub(existing_coeff.clone(), coeff));
} else {
result.terms.insert(blade, simplify(&Expr::new_neg(coeff)));
}
}
result.prune_zeros();
result
}
}
impl Mul<Expr> for Multivector {
type Output = Self;
fn mul(
self,
scalar: Expr,
) -> Self {
let mut result = self;
for coeff in result.terms.values_mut() {
*coeff = simplify(&Expr::new_mul(coeff.clone(), scalar.clone()));
}
result.prune_zeros();
result
}
}