Skip to content

Instantly share code, notes, and snippets.

@jcreedcmu
Created September 11, 2013 02:21
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 jcreedcmu/6518640 to your computer and use it in GitHub Desktop.
Save jcreedcmu/6518640 to your computer and use it in GitHub Desktop.
finite field visualization
#!/usr/bin/perl
my %log = ();
my %exp = ();
my $P = 1049;
my $Pm = $P - 1;
my $gen = 1;
my $pi = 3.1415926535;
sub discretelog {
$m = 1;
for (my $x = 0; $x < $P; $x++) {
# print "$m\n";
$log{$m} = $x;
$exp{$x} = $m;
$m = ($m * $gen) % $P;
if ($m == 1) {
$x++;
print STDERR "period: $x\n";
return $x == $Pm;
}
}
}
while (!discretelog() && $gen < $Pm) { $gen++; }
sub point {
my ($n) = @_;
my $x = 350 * cos(2 * $pi * $n / $Pm) + 400;
my $y = 350 * sin(2 * $pi * $n / $Pm) + 400;
return ($x, $y);
}
print '<svg xmlns="http://www.w3.org/2000/svg" version="1.1">';
for (my $x = 1; $x < $P / 2 - 1; $x++) {
my $out = $log{$exp{$x} + 1};
my $out2 = $log{$exp{$P - $x - 1} + 1};
my $diff = ($out - $out2) % ($P - 1);
print STDERR "$out $out2 $diff\n";
my ($x1, $y1) = point($out);
my ($x2, $y2) = point($out2);
my $green = int(256 * $x / ($P / 2 - 1));
print <<EOF
<line x1="$x1" y1="$y1" x2="$x2" y2="$y2"
style="stroke:rgb($green,$green,128);stroke-width:2"/>
EOF
}
for (my $theta = 0; $theta < $Pm; $theta++) {
my ($x, $y) = point($theta);
print <<EOF
<circle cx="$x" cy="$y" r="2" fill="rgb(40,40,40)"/>
EOF
}
print '</svg>';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment