Geometric algebra: Difference between revisions

rewriting. Back to verifying axioms.
m (J: with all the contradictions in the verbiage, who knows what's better or not...)
(rewriting. Back to verifying axioms.)
Line 11:
</math>
 
The product operation in thissuch algebra is called the ''geometric product''. The elementsElements are called ''multivectors''., while Multivectorsmultivectors in <math>\mathcal{V}</math> are just called ''vectors''. If the dimension of <math>\mathcal{V}</math> is <math>n</math>, then the dimension of the algebra is <math>2^n</math>.
 
There are a few simple examples of geometric algebras. A trivial one for instance is simply <math>\R</math>, where <math>\mathcal{V} = \R</math>. 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 [[Quaternion type|quaternions]], where the vector space is the three-dimensional space of all linear combinations of <math>(i, j, k)</math>.
The purpose of this task is to implement such an algebra for an arbitrary large dimension <math>n</math>, but just take <math>n = 5</math> if that's easier to implement in your language (for instance if support of large integers is not easy). For this task we will also focus on simple versions of this algebra where <math>\forall\mathbf{x}\in\mathcal{V},\,\mathbf{x}\neq 0 \implies \mathbf{x}^2>0</math>, but you can also implement a general version for extra credit.
 
The purpose of this task is to implement a geometric algebra with a vector space <math>\mathcal{V}</math> 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 demonstrate your solution, you will use it to implement [[Quaternion type|quaternions]]:
* define the inner product of two vectors : <math>\mathbf{a}\cdot\mathbf{b} = (\mathbf{ab} + \mathbf{ba})/2</math>.
Since <math>\mathbf{ab} + \mathbf{ba} = (\mathbf{a} + \mathbf{b})^2 - \mathbf{a}^2 - \mathbf{b}^2</math>, the fourth axiom tells us that the inner product is a real number, and since it is also clearly symmetric and bilinear, it defines a [[wp:scalar product|scalar product]] on <math>\mathcal{V}</math>.
* define a function <math>n\mapsto e(n)</math> which allows for creation of an orthonormal basis of <math>\mathcal{V}</math>
<math>(\mathbf{e}_0,\mathbf{e}_1,\mathbf{e}_2,\ldots)</math>,
* verify that the square of an arbitrary vector is real. Use this example: <math>(\mathbf{e}_0 - \mathbf{e}_1 + 2\mathbf{e}_2 + 3\mathbf{e}_3 - 2\mathbf{e}_4)^2 = 19</math>
* verify the orthonormality <math>\mathbf{e}_i\cdot\mathbf{e}_j = \delta_{i,j}</math> for i, j in <math>\{0, 1, 2, 3\}</math>.
* define the following three constants:
:<math>\begin{array}{c}
i = \mathbf{e}_0\mathbf{e}_1\\
j = \mathbf{e}_1\mathbf{e}_2\\
k = \mathbf{e}_0\mathbf{e}_2
\end{array}</math>
* verify that <math>i^2 = j^2 = k^2 = ijk = -1</math>
* to verify that your implementation does not just reuse an ad-hoc implementation of quaternions, also define the following three constants:
:<math>\begin{array}{c}
I = \mathbf{e}_1\mathbf{e}_2\\
J = \mathbf{e}_2\mathbf{e}_3\\
K = \mathbf{e}_1\mathbf{e}_3
\end{array}</math>
* verify that <math>I^2 = J^2 = K^2 = IJK = -1</math>
* verify that <math>(K-J) = I(J+K)</math>
 
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:
 
<math>\forall\mathbf{x}\in\mathcal{V},\, \mathbf{x}\neq 0 \implies\mathbf{x}^2 > 0</math>.
 
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 <tt>n -> e(n)</tt> such that the vectors <tt>e(0), e(1), e(2), e(3), e(4)</tt> are linearly independent. To do so you will make them orthonormal with the following [[wp:scalar product|scalar product]]:
 
<math>\mathbf{x}\cdot\mathbf{y} = (\mathbf{x}\mathbf{y} + \mathbf{y}\mathbf{x})/2</math>
 
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:
 
<math>\mathbf{x}\mathbf{y} + \mathbf{y}\mathbf{x} = (\mathbf{x} + \mathbf{y})^2 - \mathbf{x}^2 - \mathbf{y}^2</math>
 
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 <math>\mathbf{x}</math>.
 
Producing a random vector is easy. Just use a pseudo-random generation function <tt>rand</tt> and create a vector:
 
<math>\mathrm{randomVector}() = \sum_{i=0}^4 \mathrm{rand}() \mathbf{e}_i</math>
 
Producing a random multivector is slightly more involved. It is known that when the dimension of <math>\mathcal{V}</math> is ''n'', then the dimension of the algebra (seen as a vector space with its natural scalar multiplication) is 2<sup>n</sup>. This means that for n=5 there is a basis of 2<sup>5</sup> = 32 basis multivectors from which any multivector can be written as a linear combination. Create such a basis <math>m_0, m_1,\ldots,m_{31}</math> along with a function producting a random multivector:
 
<math>\mathrm{randomMultivector}() = \sum_{i=0}^{31} \mathrm{rand}() m_i</math>
 
To summarize, to solve this task you will:
* define the inner product of two vectors : <math>\mathbf{x}\cdot\mathbf{y} = (\mathbf{xy} + \mathbf{yx})/2</math>.
* define the function <tt>e</tt>
* verify the orthonormality <math>\mathbf{e}_i\cdot\mathbf{e}_j = \delta_{i,j}</math> for i, j in <math>\{0, 1, 2, 3, 4\}</math>.
* 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.
 
=={{header|EchoLisp}}==
Line 561 ⟶ 573:
<lang perl6>use MultiVector;
use Test;
plan 13;
sub infix:<cdot>($a, $b) { ($a*$b + $b*$a) / 2 }
 
plan 29;
for ^4 X ^4 -> ($i, $j) {
next if $i > $j;
ok e($i) cdot e($j) == +($i == $j);
}
 
mysub infix:<cdot>($vx, = e(0$y) -{ e(1) + 2$x*e(2)$y + 3$y*e(3$x)/2 - 2*e(4);}
my @e = map &e, ^5;
 
for ^5 X ^5 -> ($i, $j) {
ok $v**2 == 19, 'v² = 19';
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(my Real %{UInt} = $_ => rand.round(.01))
}, (^32).pick(5);
}
 
my constant($a, i$b, $c) = erandom(0)*e(1) xx 3;
my constant j = e(1)*e(2);
my constant k = e(0)*e(2);
ok i**2 == j**2 == k**2 == i*j*k == -1, "i² = j² = k² = ijk = -1";
 
ok ($a*$b)*$c == $a*($b*$c), 'associativity';
my constant I = e(1)*e(2);
ok $a*($b + $c) == $a*$b + $a*$c, 'left distributivity';
my constant J = e(2)*e(3);
ok ($a + $b)*$c == $a*$c + $b*$c, 'right distributivity';
my constant K = e(1)*e(3);
my @coeff = (.5 - rand) xx 4;
my $v = [+] @coeff Z* map &e, ^4;
ok I**2 == J**2 == K**2 == I*J*K == -1, "I² = J² = K² = IJK = -1";</lang>
ok ($v**2).narrow ~~ Real, 'contraction';</lang>
{{out}}
<pre>1..1329
ok 1 - e0 cdot e0 = 1
ok 2 - e0 cdot e1 = 0
ok 3 - e0 cdot e2 = 0
ok 4 - e0 cdot e3 = 0
ok 5 - e0 cdot e4 = 0
ok 6 - e1 cdot e0 = 0
ok 7 - e1 cdot e1 = 1
ok 8 - e1 cdot e2 = 0
ok 9 - e1 cdot e3 = 0
ok 10 - e1 cdot e4 = 0
ok 11 - e2 cdot e0 = 190
ok 12 - e2 = j² = k² =cdot ijke1 = -10
ok 13 - e2 = J² = K² =cdot IJKe2 = -1</pre>
ok 14 - e2 cdot e3 = 0
ok 15 - e2 cdot e4 = 0
ok 16 - e3 cdot e0 = 0
ok 17 - e3 cdot e1 = 0
ok 18 - e3 cdot e2 = 0
ok 19 - e3 cdot e3 = 1
ok 20 - e3 cdot e4 = 0
ok 21 - e4 cdot e0 = 0
ok 22 - e4 cdot e1 = 0
ok 23 - e4 cdot e2 = 0
ok 24 - e4 cdot e3 = 0
ok 25 - e4 cdot e4 = 1
ok 26 - associativity
ok 27 - left distributivity
ok 28 - right distributivity
ok 29 - contraction</pre>
1,934

edits