Geometric algebra

From Rosetta Code
Geometric algebra is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.

Geometric algebra is an other name for Clifford algebras and it's basically an algebra containing a vector space and obeying the following axioms:

The product operation in such algebra is called the geometric product. Elements are called multivectors, while multivectors in are just called vectors.

There are a few simple examples of geometric algebras. A trivial one for instance is simply , where . The complex numbers also form a geometric algebra, where the vector space is the one-dimensional space of all purely imaginary numbers. An other example is the space of quaternions, where the vector space is the three-dimensional space of all linear combinations of .

The purpose of this task is to implement a geometric algebra with a vector space of dimension n of at least five, but for extra-credit you can implement a version with n arbitrary large. Using a dimension five is useful as it is the dimension required for the so-called conformal model which will be the subject of a derived task.

To ensure the unicity of the solution (that is, up to some isomorphism), we will also restrict ourselves to the so-called euclidean case, where the square of a non-zero vector is positive:

.

You can of course, for extra credit, implement the general case. This would require the definition of a parameter for the signature of the metric.

In order to show that your solution uses a vector space of dimension at least five, you will create a function n -> e(n) such that the vectors e(0), e(1), e(2), e(3), e(4) are linearly independent. To do so you will make them orthonormal with the following scalar product:

The fact that this so-called inner product defines a scalar product is a consequence of the fourth axiom. To see it one just needs to notice the relation:

Once you'll have shown that your vector space is at least of dimension five, you will show that the axioms are satisfied. For this purpose you will pick three random multivectors a, b and c, along with a random vector .

Producing a random vector is easy. Just use a pseudo-random generation function rand and create a vector:

Producing a random multivector is slightly more involved. It is known that when the dimension of is n, then the dimension of the algebra (seen as a vector space with its natural scalar multiplication) is 2n. This means that for n=5 there is a basis of 25 = 32 basis multivectors from which any multivector can be written as a linear combination. Create such a basis along with a function producting a random multivector:

To summarize, to solve this task you will:

  • define the inner product of two vectors : .
  • define the function e
  • verify the orthonormality for i, j in .
  • create a function returning a random multivector
  • create a function returning a random vector
  • verify the axioms for three rarndom multivectors a, b, c and a random vector x.


Optionally, you will repeat the last step a large number of times, in order to increase confidence in the result.

EchoLisp[edit]

We build a CGA based upon a generating quadratic form in R^n. The implementation is general enough, that is ei*ei = +/- 1 , and not restricted to 1. The 5 dimension limit comes from the use of 32 bits numbers to generate all permutations 101... , but this could be improved. The multi-vector multiplication is based on a multiplication table 2^n * 2^n , generated once for all.

 
(define e-bits (build-vector 32 (lambda(i) (arithmetic-shift 1 i)))) ;; 1,2,4,..
(define (e-index i) ;; index of ei in native vector
(if (zero? i) 0 (arithmetic-shift 1 (1- i))))
 
(define DIM 0) ;; 2^N
(define N 0)
(define MultTable null) ;; multiplication table eijk * el.. = exyz..
(define SignTable null) ;; sign of products
(define Signature null) ;; input quadratic form
 
;; return "eijk"
(define( e-print E sign )
(string-append
(cond ((= sign 1) " ") ((= sign -1) "- ") (else ""))
(if (zero? E) "1"
(for/string ((i N))
#:continue (zero? (bitwise-and E (vector-ref e-bits i)))
(string-append "e" (1+ i))))))
 
;; returns a string a *e1 + b*e2 + .. z*eijk + ..
(define (multi-print V (x))
(for/string ((i DIM))
(set! x (vector-ref V i))
#:continue (zero? x)
(string-append " " (if (> x 0) "+" "") x "*" (e-print i 0))))
 
 
;; generates the multiplication table e_i e__k . * e_j e_l ..==> e_u e_v ...
;; E_I and E_J are sets of indices >=1 , increasing order, represented by a 32 bits number
 
(define (make-mult-table (verbose #f) (result) (swaps) (ej))
(when verbose (writeln 'N= N 'DIM= DIM 'Q= Signature))
(for* ((E_I (in-range 1 DIM))(E_J (in-range 1 DIM)))
(set! result E_I)
(set! swaps 0)
(for ((j DIM)) ; each bit# in E_J
(set! ej (vector-ref e-bits j))
#:continue (zero? (bitwise-and ej E_J))
 
(for((s (in-range (1- N) j -1))) ;; count swaps
(when (!zero? (bitwise-and E_I (vector-ref e-bits s)))
(set! swaps (1+ swaps))))
 
(if (zero? (bitwise-and E_I ej)) ;; e_i * e_j
(set! result (bitwise-ior result ej))
(begin ;; else e_i * e_i
(set! result (bitwise-xor result ej))
(when (= -1 (vector-ref Signature ej)) (set! swaps (1+ swaps)))
))) ;; j loop
 
(when verbose (writeln (e-print E_I 0) '* (e-print E_J 0)
'= (e-print result (if (even? swaps) 1 -1))))
 
(matrix-set! MultTable E_I E_J result)
(matrix-set! SignTable E_I E_J (if (even? swaps) 1 -1))
))
 
;; multivector operations
;; addition is standard vector addition
;; multiplication a b -> c
(define (multi-mult a b)
(define c (make-vector DIM 0))
(for* ((i DIM) (j DIM))
#:continue (zero? (vector-ref a i))
#:continue (zero? (vector-ref b j))
(vector-set! c
(array-ref MultTable i j)
(+
(* (array-ref SignTable i j) (vector-ref a i) (vector-ref b j))
(vector-ref c (array-ref MultTable i j)))))
c)
 
;; pretty print a • b or a • b • c
(define ( • a b (c #f))
(multi-print
(if c (multi-mult a (multi-mult b c)) (multi-mult a b))))
 
 
;; (Eij i j) -> return multi-vector eiej 0 <= i <= n
(define (Eij i j (coeff 1))
(define Eij (make-vector DIM))
(vector-set! Eij (array-ref MultTable (e-index i) (e-index j)) coeff)
Eij)
 
 
;; Reference : https://en.wikipedia.org/wiki/Clifford_algebra#Real_numbers
 
;; (make-cga m p [verbose]) => Algebra A(m p)
;; Input : a quadratic form Q(x) = x1*x1 + + xm*xm - xm+1*xm+1 - xm+p*xm+p
;; n = m + p = dimension of vector space R^n
;; generates an algebra A(m p) of dimension DIM = 2^n
;; Ex : A(n 0) = use R^n dot product as quadratic form : ei*ei = 1
;; Ex : A (0 1) = Complex , e1*e1 = -1 ; A(0 2) => quaternions ei*ei = -1
;;
;; Implementation
;; limitation n <= 5
;; multivectors of A(m p) will be mapped on Vectors V of dimension 2^n
;; V[0] is the scalar part of a multivector.
;; Blade of vectors of R^n :  :V[2^(i-1)] = 1 , 0 elsewhere , i in [1 ..n]
 
(define (make-cga m p (verbose #f))
(string-delimiter "")
(set! N (+ m p))
(set! DIM (expt 2 N))
(set! MultTable (build-array DIM DIM (lambda(i j) (cond ((zero? i) j)((zero? j) i)(else 0)))))
(set! SignTable (make-array DIM DIM 1))
(set! Signature (make-vector DIM 1)) ;; Q polynomial
(for ((j (in-range m N))) (vector-set! Signature (vector-ref e-bits j) -1))
 
(make-mult-table verbose) DIM )
 
 
 
Output:
 
;; we use indices (1 ... n) in conformity with the Wikipedia reference
 
;; dimension 2
;; (1 + e1e2) • (e1 -2e2) = -e1 -3e2
(make-cga 2 0)
(define u #(1 0 0 1))
(define v #(0 1 -2 0))
 
(multi-print u)+1*1 +1*e1e2
(multi-print v)+1*e1 -2*e2
(• u v)-1*e1 -3*e2
 
;; task
(make-cga 5 0)
(define X #(0 1 -1 0 2 0 0 0 -3 0 0 0 0 0 0 0 -2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ))
(multi-print X)+1*e1 -1*e2 +2*e3 -3*e4 -2*e5
(• X X)+19*1
 
; with another polynomial
(make-cga 0 5)
Signature → #( 1 -1 -1 1 -1 1 1 1 -1 1 1 1 1 1 1 1 -1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1)
(• X X)-19*1
 
;
(make-cga 4 0)
(define i (Eij 1 2))
(define j (Eij 2 3))
(define k (Eij 1 3))
 
(multi-print i)+1*e1e2
(multi-print j)+1*e2e3
(multi-print k)]+1*e1e3
(• i i)-1*1
(• j j)-1*1
(• k k)-1*1
(• i j k)-1*1
 
(define I (Eij 2 3))
😖️ error: define : cannot redefine : I (used in Complex) ;; use II instead
 
(define II (Eij 2 3))+1*e2e3
(define J (Eij 3 4))+1*e3e4
(define K (Eij 2 4))+1*e2e4
 
(• II II)-1*1
(• J J)-1*1
(• K K)-1*1
(• II J K)-1*1
 
 

Multiplication table for A(3 0)

N= 3 DIM= 8 Q= #( 1 1 1 1 1 1 1 1)
e1 * e1 = 1
e1 * e2 = e1e2
e1 * e1e2 = e2
e1 * e3 = e1e3
e1 * e1e3 = e3
e1 * e2e3 = e1e2e3
e1 * e1e2e3 = e2e3
e2 * e1 = - e1e2
e2 * e2 = 1
e2 * e1e2 = - e1
e2 * e3 = e2e3
e2 * e1e3 = - e1e2e3
e2 * e2e3 = e3
e2 * e1e2e3 = - e1e3
e1e2 * e1 = - e2
e1e2 * e2 = e1
e1e2 * e1e2 = - 1
e1e2 * e3 = e1e2e3
e1e2 * e1e3 = - e2e3
e1e2 * e2e3 = e1e3
e1e2 * e1e2e3 = - e3
e3 * e1 = - e1e3
e3 * e2 = - e2e3
e3 * e1e2 = e1e2e3
e3 * e3 = 1
e3 * e1e3 = - e1
e3 * e2e3 = - e2
e3 * e1e2e3 = e1e2
e1e3 * e1 = - e3
e1e3 * e2 = - e1e2e3
e1e3 * e1e2 = e2e3
e1e3 * e3 = e1
e1e3 * e1e3 = - 1
e1e3 * e2e3 = - e1e2
e1e3 * e1e2e3 = e2
e2e3 * e1 = e1e2e3
e2e3 * e2 = - e3
e2e3 * e1e2 = - e1e3
e2e3 * e3 = e2
e2e3 * e1e3 = e1e2
e2e3 * e2e3 = - 1
e2e3 * e1e2e3 = - e1
e1e2e3 * e1 = e2e3
e1e2e3 * e2 = - e1e3
e1e2e3 * e1e2 = - e3
e1e2e3 * e3 = e1e2
e1e2e3 * e1e3 = e2
e1e2e3 * e2e3 = - e1
e1e2e3 * e1e2e3 = - 1

J[edit]

Sparse arrays give better performance for this task, than dense arrays, and support relatively arbitrary dimensions, but can be a bit quirky because current implementations of J do not support some features for sparse arrays. We add multivectors x and y using x + y. Also, the first element of one of these multivectors represents the "real valued" or "scalar component" of the multivector.

Implementation:

NB. indices are signed machine integers
vzero=: 1 $.2^31+IF64*32
odim=. 2^.#vzero
 
ndx01=:1 :0
:
NB. indexed update of numeric rank 1 sparse y
NB. creating rank 2 sparse result
NB. using scalar values from x and scalar inds from m
NB. where x, m are rank 0 or 1
NB. (this works around a spurious error in sparse handling)
n=. #x,.m
x ((i.n),&.> m)} n#,:y
)
 
NB. specify that all axes are sparse, for better display
clean=: (2;i.@#@$) $. ]
 
gmul=:4 :0"1
xj=. ,4$.x
yj=. ,4$.y
if. 0= xj *&# yj do. vzero return. end.
b=. (-##:>./0,xj,yj)&{."1@#:
xb=. b xj
yb=. b yj
rj=. ,#.xb~:"1/yb
s=. ,_1^ ~:/"1 yb *"1/ 0,.}:"1 ~:/\"1 xb
vzero (~.rj)}~ rj +//. s*,(xj{x)*/yj{y
)
 
gdot=: (gmul + gmul~) % 2:
 
obasis=:1 (2^i.odim)ndx01 vzero
e=: {&obasis

Explanation:

We work with sparse vectors of length 2147483647 on 32 bit machines and of length 9223372036854775807 on 64 bit machines. These are the largest representable vector lengths in current J implementations. J allows negative indices and J uses an index value corresponding to the length of the list to indicate value not found when searching. Thus, current J implementations use signed machine integers for performance and correctness reasons and these are the largest vector lengths we can use in J.

Except, for the purpose of this task, we must pretend that these are "multivectors" instead of vectors - task vectors have a length of log2 the multivector length. So technically speaking, we can only represent 30 element (or less) vectors on 32 bit J and 62 element (or less) vectors on 64 bit J. (Actually, we can represent 31 element vectors on 32 bit J and 63 bit vectors on 64 bit J, but there are hypothetical operations involving the last element which perhaps would be hindered by the fact that the multivector length is not 2147483648 or 9223372036854775808 -- but fortunately, none of this is actually relevant.)

Since these multivectors are silly large, and almost all zeros, we use a sparse representation and only concern ourselves with non-zero values, and their indices.

For addition on multivectors we use J's + (and "multivectors" includes "task vectors").

For "geometric multiplication" (whose relationship to geometry seems obscure, at best) we define something analogous to an inner product. More specifically, we look at the base 2 representation of the argument indices to find the result index and to find whether we negate the product of those two values.

For each pair of values in the left and right argument we use the base 2 representation of the indices to determine what to do with the product of the corresponding two non-zero values:

  • The result index for that product is the bit-wise exclusive-or of the two indices (the corresponding bit in the result index is 1 when the corresponding bits in the argument indices are different, 0 otherwise).
  • We negate the product depending on the bits of the left and right argument index. For each 1 bit of the right argument index, we count the number of more significant bits in the left argument index. If this total is odd, we negate the product (but not if it's even). Note that we don't actually have to form a sum to do this - exclusive-or is an adequate replacement for sum if we only need to know whether it's odd or even.

Where we have more than one non-zero value pair contributing to the same product index we sum those values. (Of course, this would apply to value pairs whose result is zero, but since typically there's something approaching 85070591730234615847396907784232501249 of those, we don't actually compute all those zeros...)

Task examples:

   NB. test arbitrary vector being real (and having the specified result)
clean gmul~ +/ (e 0 1 2 3 4) gmul 1 _1 2 3 _2 (0 ndx01) vzero
019
 
NB. required orthogonality
clean gdot&e&>/~i.4
0 0 01
1 1 01
2 2 01
3 3 01
 
NB. i j k
i=: 0 gmul&e 1
j=: 1 gmul&e 2
k=: 0 gmul&e 2
 
i gmul i
0_1
j gmul j
0_1
k gmul k
0_1
i gmul j gmul k
0_1
 
NB. I J K
I=: 1 gmul&e 2
J=: 2 gmul&e 3
K=: 1 gmul&e 3
 
I gmul I
0_1
J gmul J
0_1
K gmul K
0_1
I gmul J gmul K
0_1
K-J
101
12_1
I gmul J+K
101
12_1

Note that sparse arrays display as indices | value for elements which are not the default element (0).

So, for example in the part where we check for orthogonality, we are forming a 5 by 5 by 9223372036854775807 array. The first two dimensions correspond to arguments to e and the final dimension is the multivector dimension. A 0 in the multivector dimension means that that's the "real" or "scalar" part of the multivector. And, since the pair of dimensions for the e arguments whose "dot" product are 1 are identical, we know we have an identity matrix.

JavaScript[edit]

var GA = function () {
function e(n) {
var result = [];
result[1 << n] = 1;
return result;
}
function cdot(a, b) { return mul([0.5], add(mul(a, b), mul(b, a))) }
function neg(x) { return mul([-1], x) }
function bitCount(i) {
// Note that unsigned shifting (>>>) is not required.
i = i - ((i >> 1) & 0x55555555);
i = (i & 0x33333333) + ((i >> 2) & 0x33333333);
i = (i + (i >> 4)) & 0x0F0F0F0F;
i = i + (i >> 8);
i = i + (i >> 16);
return i & 0x0000003F;
}
function reorderingSign(a, b) {
a >>= 1;
var sum = 0;
while (a != 0) {
sum += bitCount(a & b);
a >>= 1;
}
return (sum & 1) == 0 ? 1 : -1;
}
function add(a, b) {
var result = a.slice(0);
for (var i in b) {
if (result[i]) {
result[i] += b[i];
} else {
result[i] = b[i];
}
}
return result;
}
function mul(a, b)
{
var result = [];
for (var i in a) {
if (a[i]) {
for (var j in b) {
if (b[j]) {
var s = reorderingSign(i, j) * a[i] * b[j];
// if (i == 1 && j == 1) { s *= -1 } // e0*e0 == -1
var k = i ^ j;
if (result[k]) {
result[k] += s;
} else {
result[k] = s;
}
}
}
}
}
return result;
}
return {
e : e,
cdot : cdot,
neg : neg,
add : add,
mul : mul
};
}();

And then, from the console:

var e = GA.e, cdot = GA.cdot;
 
for (var i = 0; i < 5; i++) {
for (var j = 0; j < 5; j++) {
if (i < j) {
if (cdot(e(i), e(j))[0]) { console.log("unexpected non-nul scalar product"); }
} else if (i === j) {
if (!cdot(e(i), e(j))[0]) { console.log("unexpected nul scalar product"); }
}
}
}
 
function randomVector() {
var result = [];
for (var i = 0; i < 5; i++) { result = GA.add( result, GA.mul([Math.random()], e(i))); }
return result;
}
function randomMultiVector() {
var result = [];
for (var i = 0; i < 32; i++) { result[i] = Math.random(); }
return result;
}
 
var a = randomMultiVector(), b = randomMultiVector(), c = randomMultiVector();
var x = randomVector();
 
// (ab)c == a(bc)
console.log(GA.mul(GA.mul(a, b), c));
console.log(GA.mul(a, GA.mul(b, c)));
 
// a(b + c) == ab + ac
console.log(GA.mul(a, GA.add(b, c)));
console.log(GA.add(GA.mul(a,b), GA.mul(a, c)));
 
// (a + b)c == ac + bc
console.log(GA.mul(GA.add(a, b), c));
console.log(GA.add(GA.mul(a,c), GA.mul(b, c)));
 
// x² is real
console.log(GA.mul(x, x));
Output:
[-7.834854130554672, -10.179405417124476, 5.696414143584243, -1.4014556169803851, 12.334288331422336, 11.690738709598888, -0.4279888274147221, 6.226618790084965, -10.904144874917206, -5.46919448234424, -5.647472225071031, -2.9801969751721744, -8.284532508545746, -3.3280413654836494, -2.2182526412098493, 0.4191036292473347, 3.0485450100607103, -0.20619687045226742, 2.1369938048939527, 3.730913391951158, 10.929856967963905, 8.301187183717643, -4.874133827873075, 0.7918650606624789, -8.520661635525103, -7.732342981599732, -6.494750491582618, -2.458749173402162, 3.573788336699224, 2.784339193089742, -1.6479372032388944, -0.35120747879544256]
[-7.83485413055467, -10.179405417124475, 5.696414143584248, -1.4014556169803827, 12.334288331422337, 11.690738709598893, -0.4279888274147213, 6.226618790084964, -10.90414487491721, -5.46919448234424, -5.647472225071032, -2.9801969751721726, -8.284532508545746, -3.3280413654836507, -2.218252641209847, 0.41910362924733874, 3.048545010060707, -0.20619687045226748, 2.136993804893955, 3.7309133919511575, 10.929856967963904, 8.301187183717648, -4.8741338278730755, 0.7918650606624811, -8.520661635525107, -7.732342981599734, -6.494750491582625, -2.45874917340216, 3.5737883366992262, 2.7843391930897443, -1.6479372032388935, -0.351207478795442]
[-4.5157935996060425, -3.9762419076273514, -2.653425845411889, -1.2899302330562412, 6.161562884801266, 3.664812215240675, -0.4471521091019873, 2.39303455739218, -1.6486347268701103, 1.156714478904937, 4.5859158357958965, 6.879356425817299, 1.3341425863947358, 5.641350122882839, 6.378155334673649, 6.466962714879142, -3.645688408496504, -1.9659188980662032, 1.3062519818876646, 1.7973392350972788, 2.4770203476100843, 1.258017836002405, 1.3794942194985413, 3.993871627961031, -3.3620439843097127, -0.4228490927003264, 0.27245046364398495, 3.813642689561589, 2.6785051915908604, 5.409359105713415, 2.9578168177883555, 4.425426168284635]
[-4.515793599606042, -3.976241907627351, -2.653425845411889, -1.2899302330562417, 6.161562884801263, 3.664812215240676, -0.44715210910198766, 2.393034557392179, -1.6486347268701103, 1.156714478904937, 4.585915835795897, 6.8793564258172974, 1.3341425863947352, 5.641350122882839, 6.378155334673649, 6.466962714879143, -3.645688408496502, -1.9659188980662032, 1.3062519818876661, 1.7973392350972783, 2.4770203476100843, 1.258017836002407, 1.379494219498544, 3.99387162796103, -3.3620439843097127, -0.42284909270032545, 0.2724504636439853, 3.8136426895615894, 2.67850519159086, 5.409359105713415, 2.9578168177883555, 4.425426168284636]
[-5.8903316026132755, -6.619647679486295, -1.8140191326116537, -2.519531799741982, 6.604158362571294, 6.352401943423508, 0.9412086471616096, 3.719341486246096, -2.209111542028446, 1.9980997124233557, 5.717878641652222, 7.351597777237362, -2.9037939632499974, 1.497897713658653, 6.811544238648882, 5.861907187665564, -3.2638975880372363, -2.2659714695119115, 1.227221599808634, 0.8343365341022846, 2.72461491531054, 2.728833585944902, 2.226404227376565, 3.888097816250177, 0.35867175462798684, 2.3965356477571302, -1.7151608532791172, 1.403673323043394, -2.1441532262277607, 2.5435142440445646, 2.00110597707534, 1.9825972651495558]
[-5.8903316026132755, -6.6196476794862935, -1.8140191326116533, -2.5195317997419817, 6.604158362571292, 6.352401943423505, 0.9412086471616091, 3.719341486246094, -2.209111542028446, 1.9980997124233555, 5.71787864165222, 7.351597777237364, -2.9037939632499974, 1.4978977136586529, 6.81154423864888, 5.861907187665565, -3.263897588037235, -2.2659714695119124, 1.2272215998086353, 0.8343365341022843, 2.72461491531054, 2.7288335859449018, 2.226404227376565, 3.8880978162501783, 0.3586717546279864, 2.396535647757131, -1.715160853279117, 1.4036733230433938, -2.1441532262277603, 2.543514244044564, 2.0011059770753405, 1.9825972651495565]
[3.193752260485546, 3: 0, 5: 0, 6: 0, 9: 0, 10: 0, 12: 0, 17: 0, 18: 0, 20: 0, 24: 0]

Perl 6[edit]

Here we write a simplified version of the Clifford module. It is very general as it is of infinite dimension and also contains an anti-euclidean basis @ē in addition to the euclidean basis @e.

unit class MultiVector;
subset UIntHash of MixHash where .keys.all ~~ UInt;
has UIntHash $.blades;
method narrow { $!blades.keys.any > 0 ?? self !! ($!blades{0} // 0) }
 
multi method new(Real $x) returns MultiVector { self.new: (0 => $x).MixHash }
multi method new(UIntHash $blades) returns MultiVector { self.new: :$blades }
 
multi method new(Str $ where /^^e(\d+)$$/) { self.new: (1 +< (2*$0)).MixHash }
multi method new(Str $ where /^^ē(\d+)$$/) { self.new: (1 +< (2*$0 + 1)).MixHash }
 
our @e is export = map { MultiVector.new: "e$_" }, ^Inf;
our @ē is export = map { MultiVector.new: "ē$_" }, ^Inf;
 
my sub order(UInt:D $i is copy, UInt:D $j) {
(state %){$i}{$j} //= do {
my $n = 0;
repeat {
$i +>= 1;
$n += [+] ($i +& $j).polymod(2 xx *);
} until $i == 0;
$n +& 1 ?? -1 !! 1;
}
}
 
multi infix:<+>(MultiVector $A, MultiVector $B) returns MultiVector is export {
return MultiVector.new: ($A.blades.pairs, |$B.blades.pairs).MixHash;
}
multi infix:<+>(Real $s, MultiVector $B) returns MultiVector is export {
return MultiVector.new: (0 => $s, |$B.blades.pairs).MixHash;
}
multi infix:<+>(MultiVector $A, Real $s) returns MultiVector is export { $s + $A }
 
multi infix:<*>(MultiVector $, 0) is export { 0 }
multi infix:<*>(MultiVector $A, 1) returns MultiVector is export { $A }
multi infix:<*>(MultiVector $A, Real $s) returns MultiVector is export {
MultiVector.new: $A.blades.pairs.map({Pair.new: .key, $s*.value}).MixHash
}
multi infix:<*>(MultiVector $A, MultiVector $B) returns MultiVector is export {
MultiVector.new: do for $A.blades -> $a {
|do for $B.blades -> $b {
($a.key +^ $b.key) => [*]
$a.value, $b.value,
order($a.key, $b.key),
|grep +*, (
|(1, -1) xx * Z*
($a.key +& $b.key).polymod(2 xx *)
)
}
}.MixHash
}
multi infix:<**>(MultiVector $ , 0) returns MultiVector is export { MultiVector.new }
multi infix:<**>(MultiVector $A, 1) returns MultiVector is export { $A }
multi infix:<**>(MultiVector $A, 2) returns MultiVector is export { $A * $A }
multi infix:<**>(MultiVector $A, UInt $n where $n %% 2) returns MultiVector is export { ($A ** ($n div 2)) ** 2 }
multi infix:<**>(MultiVector $A, UInt $n) returns MultiVector is export { $A * ($A ** ($n div 2)) ** 2 }
 
multi infix:<*>(Real $s, MultiVector $A) returns MultiVector is export { $A * $s }
multi infix:</>(MultiVector $A, Real $s) returns MultiVector is export { $A * (1/$s) }
multi prefix:<->(MultiVector $A) returns MultiVector is export { return -1 * $A }
multi infix:<->(MultiVector $A, MultiVector $B) returns MultiVector is export { $A + -$B }
multi infix:<->(MultiVector $A, Real $s) returns MultiVector is export { $A + -$s }
multi infix:<->(Real $s, MultiVector $A) returns MultiVector is export { $s + -$A }
 
multi infix:<==>(MultiVector $A, MultiVector $B) returns Bool is export { $A - $B == 0 }
multi infix:<==>(Real $x, MultiVector $A) returns Bool is export { $A == $x }
multi infix:<==>(MultiVector $A, Real $x) returns Bool is export {
my $narrowed = $A.narrow;
$narrowed ~~ Real and $narrowed == $x;
}

And here is the code for verifying the solution:

use MultiVector;
use Test;
 
plan 29;
 
sub infix:<cdot>($x, $y) { ($x*$y + $y*$x)/2 }
 
for ^5 X ^5 -> ($i, $j) {
my $s = $i == $j ?? 1 !! 0;
ok @e[$i] cdot @e[$j] == $s, "e$i cdot e$j = $s";
}
sub random {
[+] map {
MultiVector.new:
:blades(($_ => rand.round(.01)).MixHash)
}, ^32;
}
 
my ($a, $b, $c) = random() xx 3;
 
ok ($a*$b)*$c == $a*($b*$c), 'associativity';
ok $a*($b + $c) == $a*$b + $a*$c, 'left distributivity';
ok ($a + $b)*$c == $a*$c + $b*$c, 'right distributivity';
my @coeff = (.5 - rand) xx 5;
my $v = [+] @coeff Z* @e[^5];
ok ($v**2).narrow ~~ Real, 'contraction';</pre>