Calculating the (7, 4) Hamming code
The following program illustrates matrix multiplication with PDL, as well as transposing, modulo a number, and retrieving the values from a pdl as a Perl array.
#!/home/ben/software/install/bin/perl use warnings; use strict; use PDL; my $g = pdl [ [1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1], [1, 1, 1, 0], [0, 1, 1, 1], [1, 0, 1, 1], ]; for (0..15) { my @r; # Make the array of bits for my $x (0..3) { $r[3 - $x] = (int ($_ / (1 << $x))) % 2; } my $s = pdl (\@r); # Multiply by $g. my $t = $g x transpose ($s); # Modulo all values by 2. $t = $t % 2; # Get the bits out again. my @bits = $t->list (); # Print. print join ('', @bits), "\n"; }
0000000 0001011 0010111 0011100 0100110 0101101 0110001 0111010 1000101 1001110 1010010 1011001 1100011 1101000 1110100 1111111
Copyright © Ben Bullock 2009-2024. All
rights reserved.
For comments, questions, and corrections, please email
Ben Bullock
(benkasminbullock@gmail.com) or use the discussion group at Google Groups.
/
Privacy /
Disclaimer