Last active
December 15, 2015 13:18
-
-
Save masak/5266081 to your computer and use it in GitHub Desktop.
Groups of order four
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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