Geometric algebra: Difference between revisions
→{{header|Perl 6}}: modernize
(→{{header|Perl 6}}: rephrasing + minor fix in the verification code) |
(→{{header|Perl 6}}: modernize) |
||
Line 505:
=={{header|Perl 6}}==
Here we write a simplified version of the [https://github.com/grondilu/clifford 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.
<lang perl6>unit class MultiVector;
subset UIntHash of MixHash where .keys.all ~~ UInt;
has
method narrow { $!blades.keys.any > 0 ?? self !! ($!blades{0} // 0) }
}▼
multi method
multi method new(Str $ where /^^e(\d+)$$/) { self.new: (1 +< (2*$0)).MixHash }
my sub order(UInt:D $i is copy, UInt:D $j) is cached {▼
multi method new(Str $ where /^^ē(\d+)$$/) { self.new: (1 +< (2*$0 + 1)).MixHash }
my $n = 0;▼
repeat {▼
$i +>= 1;▼
our @ē is export = map { MultiVector.new: "ē$_" }, ^Inf;
$n += [+] ($i +& $j).base(2).comb;▼
} until $i == 0;▼
return $n +& 1 ?? -1 !! 1;▼
(state @)[$i][$j] //= do {
}▼
}
multi infix:<+>(MultiVector $A, MultiVector $B) returns MultiVector is export {
▲ }
▲ return MultiVector.new: :%blades;
}
multi infix:<+>(Real $s, MultiVector $
}
multi infix:<+>(MultiVector $A, Real $s) returns MultiVector is export { $s + $A }
MultiVector.new: $A.blades.pairs.map({Pair.new: .key, $s*.value}).MixHash
▲}
multi infix:<*>(MultiVector $A, MultiVector $B) returns MultiVector is export {
|grep +*, (
|(1, -1) xx * Z*
($a.key +& $b.key).polymod(2 xx *)
)
}
}.MixHash
}
multi infix:<**>(MultiVector $ , 0) returns MultiVector is export { MultiVector.new }
Line 559 ⟶ 565:
multi infix:<**>(MultiVector $A, UInt $n) returns MultiVector is export { $A * ($A ** ($n div 2)) ** 2 }
▲multi infix:<*>(MultiVector $, 0) returns MultiVector is export { MultiVector.new }
▲multi infix:<*>(MultiVector $A, 1) returns MultiVector is export { $A }
▲multi infix:<*>(MultiVector $A, Real $s) returns MultiVector is export {
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) }
Line 589 ⟶ 590:
for ^5 X ^5 -> ($i, $j) {
my $s = $i == $j ?? 1 !! 0;
ok @e
}
sub random {
[+] map {
MultiVector.new:
:blades(
}, ^32;
}
Line 604 ⟶ 605:
ok ($a + $b)*$c == $a*$c + $b*$c, 'right distributivity';
my @coeff = (.5 - rand) xx 5;
my $v = [+] @coeff Z*
ok ($v**2).narrow ~~ Real, 'contraction';</
|