Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@masak
Last active December 15, 2015 13:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save masak/5266081 to your computer and use it in GitHub Desktop.
Save masak/5266081 to your computer and use it in GitHub Desktop.
Groups of order four
$ nom Z4-and-Z2_X_Z2.pl
Z4 0 1 2 3
====================
0 0 1 2 3
1 1 2 3 0
2 2 3 0 1
3 3 0 1 2
Z2 X Z2 0 1 2 3
====================
0 0 1 2 3
1 1 0 3 2
2 2 3 0 1
3 3 2 1 0
class Group {
has Str $.name;
has Int $.elems;
has &.rule;
method map(&xform) {
(^$.elems).map(&xform);
}
method times(Int $l, Int $r) {
&.rule.($l, $r);
}
}
sub print-multiplication-table(Group $group) {
for ^$group.elems -> $elem {
constant margin = "%8s";
constant column-width = 5;
constant column = "%{column-width}s";
FIRST say $group.name.fmt(margin), $group.map({ .fmt(column) }).join;
FIRST say "".fmt(margin), ('=' x column-width) x $group.elems;
say $elem.fmt(margin), $group.map({ $group.times($elem, $_).fmt(column) }).join;
}
say "";
}
sub cyclic-group(Int $elems) {
Group.new(:$elems, :name("Z$elems"), :rule( -> $l, $r { ($r + $l) % $elems } ));
}
sub infix:<⨯>(Group $left, Group $right) {
my $elems = $left.elems * $right.elems;
sub l($e) { $e div $right.elems };
sub r($e) { $e % $right.elems };
sub compose($l, $r) { $l * $right.elems + $r }
my &rule = -> $l, $r {
compose( $left.times( l($l), l($r) ), $right.times( r($l), r($r) ) );
};
Group.new( :$elems, :&rule, :name("$left.name() X $right.name()") );
}
my Group $Z4 = cyclic-group(4);
my Group $Z2_X_Z2 = cyclic-group(2) ⨯ cyclic-group(2);
print-multiplication-table $Z4;
print-multiplication-table $Z2_X_Z2;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment