Geometric algebra: Difference between revisions

Content added Content deleted
(→‎{{header|Perl 6}}: rephrasing + minor fix in the verification code)
Line 578: Line 578:
}</lang>
}</lang>


And here is the code implementing and verifying quaternions:
And here is the code for verifying the solution:


<lang perl6>use MultiVector;
<lang perl6>use MultiVector;
use Test;
use Test;

plan 29;
plan 29;

sub infix:<cdot>($x, $y) { ($x*$y + $y*$x)/2 }
sub infix:<cdot>($x, $y) { ($x*$y + $y*$x)/2 }
my @e = map &e, ^5;

for ^5 X ^5 -> ($i, $j) {
for ^5 X ^5 -> ($i, $j) {
my $s = $i == $j ?? 1 !! 0;
my $s = $i == $j ?? 1 !! 0;
ok @e[$i] cdot @e[$j] == $s, "e$i cdot e$j = $s";
ok e($i) cdot e($j) == $s, "e$i cdot e$j = $s";
}
}
sub random {
sub random {
Line 596: Line 595:
MultiVector.new:
MultiVector.new:
:blades(my Real %{UInt} = $_ => rand.round(.01))
:blades(my Real %{UInt} = $_ => rand.round(.01))
}, (^32).pick(5);
}, ^32;
}
}

my ($a, $b, $c) = random() xx 3;
my ($a, $b, $c) = random() xx 3;

ok ($a*$b)*$c == $a*($b*$c), 'associativity';
ok ($a*$b)*$c == $a*($b*$c), 'associativity';
ok $a*($b + $c) == $a*$b + $a*$c, 'left distributivity';
ok $a*($b + $c) == $a*$b + $a*$c, 'left distributivity';
ok ($a + $b)*$c == $a*$c + $b*$c, 'right distributivity';
ok ($a + $b)*$c == $a*$c + $b*$c, 'right distributivity';
my @coeff = (.5 - rand) xx 4;
my @coeff = (.5 - rand) xx 5;
my $v = [+] @coeff Z* map &e, ^4;
my $v = [+] @coeff Z* map &e, ^5;
ok ($v**2).narrow ~~ Real, 'contraction';</lang>
ok ($v**2).narrow ~~ Real, 'contraction';</lang>
{{out}}
{{out}}