1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670
|
#############################################################################
##
#A codeops.gi GUAVA Reinald Baart
#A &Jasper Cramwinckel
#A &Erik Roijackers
## &David Joyner
##
## All the code operations
##
#H @(#)$Id: codeops.gi,v 1.11 2004/09/29 03:49:17 gap Exp $
##
## changes 2003-2004 to MinimumDistance by David Joyner, Aron Foster
## bug in MinimumDistance corrected 9-29-2004 by wdj
## moved Decode and PermutationDecode to decoders.gi on 10-2004
## added HasGeneratorMat to GeneratorMat function 11-2-2004
## another bug in MinimumDistance (discovered by Jason McGowan)
## corrected 11-9-2004 by wdj
## slight changes to MinimumWeightWords (11-26-2005)
## wdj (9-14-2007): bug fix to MinimumDistance
## added MinimumDistanceCodeword
## 20 Dec 07 14:24 (CJ) added IsDoublyEvenCode, IsSinglyEvenCode
## and IsEvenCode functions
##
Revision.("guava/lib/codeops_gi") :=
"@(#)$Id: codeops.gi,v 1.11 2004/09/29 03:49:17 gap Exp $";
#############################################################################
##
#F WordLength( <C> ) . . . . . . . . . . . . length of the codewords of <C>
##
InstallOtherMethod(WordLength, "generic code", true, [IsCode], 0,
function(C)
return WordLength(AsSSortedList(C)[1]) ;
end);
# This comment from GAP3 version.
#In a linear code, the wordlength must always be included
#because the wordlength cannot always be calculated (NullCode)
#
#InstallOtherMethod(WordLength, "method for linear codes", true,
# [IsLinearCode], 0,
#function(C)
# if HasGeneratorMat(C) then
# return Length( GeneratorMat(C)[1] );
# else
# return Length( CheckMat(C)[1] );
# fi;
#end);
#############################################################################
##
#F IsLinearCode( <C> ) . . . . . . . . . . . . . . . checks if <C> is linear
##
## If so, the record fields will be adjusted to the linear type
##
InstallMethod(IsLinearCode, "method for unrestricted codes", true, [IsCode], 0,
function(C)
local gen, k, F, q;
F := LeftActingDomain(C);
q := Size(F);
k := LogInt(Size(C),q);
# first the trivial cases:
if ( HasWeightDistribution(C) and
HasInnerDistribution(C)
and (WeightDistribution(C) <> InnerDistribution(C)) )
or ( q^k <> Size(C) )
or (not NullWord(WordLength(C), F) in C) then
return false; #is cool
else
gen:=BaseMat(VectorCodeword(AsSSortedList(C)));
if Length(gen) <> k then
return false; # is cool as ice
else
SetFilterObj(C, IsLinearCodeRep);
SetGeneratorMat(C, gen);
if Length(gen) = 0 then # special case for Nullcode
SetGeneratorsOfLeftModule(C, [AsSSortedList(C)[1]]);
else
SetGeneratorsOfLeftModule(C, AsList(Codeword(gen,F)));
fi;
if HasInnerDistribution(C) then
SetWeightDistribution(C, InnerDistribution(C));
fi;
return true;
fi;
fi;
end);
InstallOtherMethod(IsLinearCode, "method for generic object", true,
[IsObject], 0,
function(obj)
return IsCode(obj) and IsLinearCode(obj);
end);
#############################################################################
##
#F IsFinite( <C> ) . . . . . . . . . . . . . . . . . . . . . . . . . . . .
##
##
InstallTrueMethod(IsFinite, IsCode);
#############################################################################
##
#F Dimension( <C> ) . . . . . . . . . . . . . . . . . . . . . . . . . . .
##
##
InstallOtherMethod(Dimension, "method for unrestricted codes", true,
[IsCode], 0,
function(C)
if IsLinearCode(C) then
return Dimension(C);
else
Error("dimension is only defined for linear codes");
fi;
end);
InstallOtherMethod(Dimension, "method for cyclic codes", true,
[IsCyclicCode], 0,
function(C)
if HasGeneratorPol(C) then
return WordLength(C) - DegreeOfLaurentPolynomial(
GeneratorPol(C));
else
return DegreeOfLaurentPolynomial(CheckPol(C));
fi;
end);
#############################################################################
##
#F Size( <C> ) . . . . . . . . . . . returns the number of codewords of <C>
##
##
InstallMethod(Size, "method for unrestricted codes", true, [IsCode], 0,
function(C)
return Length(AsSSortedList(C));
end);
#############################################################################
##
#F AsSSortedList( <C> ) . . . . . . . . returns the list of codewords of <C>
## AsList( <C> )
##
## Codes created with ElementsCode must have AsSSortedList set.
## Linear codes use the vector space / FLM method to calculate.
## AsList defaults to AsSSortedList.
InstallMethod(AsList, "method for unrestricted codes", true, [IsCode], 0,
function(C)
return AsSSortedList(C);
end);
#############################################################################
##
#F Redundancy( <C> ) . . . . . . . . . . . . . . . . . . . . . . . . . . .
##
##
InstallMethod(Redundancy, "method for unrestricted codes", true, [IsCode], 0,
function(C)
if IsLinearCode(C) then
return Redundancy(C);
else
Error("redundancy is only defined for linear codes");
fi;
end);
InstallMethod(Redundancy, "method for linear codes", true, [IsLinearCode], 0,
function(C)
return WordLength(C) - Dimension(C);
end);
#############################################################################
##
#F GeneratorMat(C) . . . . . finds the generator matrix belonging to code C
##
## Pre: C should contain a generator or check matrix
##
InstallMethod(GeneratorMat, "method for unrestricted code", true, [IsCode], 0,
function(C)
if IsLinearCode(C) then
return GeneratorMat(C);
else
Error("non-linear codes don't have a generator matrix");
fi;
end);
InstallMethod(GeneratorMat, "method for linear code", true, [IsLinearCode], 0,
function(C)
local G;
if HasGeneratorMat(C) then return C!.GeneratorMat; fi;
if not HasCheckMat( C ) then
return List( BasisVectors( Basis( C ) ), x -> VectorCodeword( x ) );
fi;
if CheckMat(C) = [] then
G := IdentityMat(Dimension(C), LeftActingDomain(C));
elif IsInStandardForm(CheckMat(C), false) then
G := TransposedMat(Concatenation(IdentityMat(
Dimension(C), LeftActingDomain(C) ),
List(-CheckMat(C), x->x{[1..Dimension(C) ]})));
else
G := NullspaceMat(TransposedMat(CheckMat(C)));
fi;
return ShallowCopy(G);
end);
InstallMethod(GeneratorMat, "method for cyclic code", true, [IsCyclicCode], 0,
function(C)
local F, G, p, n, i, R, zero, coeffs;
if HasGeneratorMat(C) then return C!.GeneratorMat; fi;
#To be inspected:
#if HasCheckMat(C) and IsInStandardForm(CheckMat(C), false) then
# G := TransposedMat(Concatenation(IdentityMat(Dimension(C),
# LeftActingDomain(C)),
# List(-CheckMat(C), x->x{[1..Dimension(C)]})));
#else
F := LeftActingDomain(C);
p := GeneratorPol(C);
n := WordLength(C);
G := [];
zero := Zero(F);
coeffs := CoefficientsOfLaurentPolynomial(p);
coeffs := ShiftedCoeffs(coeffs[1], coeffs[2]);
for i in [1..Dimension(C)] do
R := NullVector(i-1, F);
Append(R, coeffs);
Append(R, NullVector(n-Length(R), F));
G[i] := R;
od;
#fi;
return ShallowCopy(G);
end);
#############################################################################
##
#F CheckMat( <C> ) . . . . . . . finds the check matrix belonging to code C
##
## Pre: <C> should be a linear code
##
InstallMethod(CheckMat, "method for unrestricted codes", true, [IsCode], 0,
function(C)
if IsLinearCode(C) then
return CheckMat(C);
else
Error("non-linear codes don't have a check matrix");
fi;
end);
InstallMethod(CheckMat, "method for linear code", true, [IsLinearCode], 0,
function(C)
local H;
if GeneratorMat(C) = [] then
H := IdentityMat(WordLength(C), LeftActingDomain(C));
elif IsInStandardForm(GeneratorMat(C), true) then
H := TransposedMat(Concatenation(List(-GeneratorMat(C),
x-> x{[Dimension(C)+1 .. WordLength(C) ]}),
IdentityMat(Redundancy(C), LeftActingDomain(C) )));
else
H := NullspaceMat(TransposedMat(GeneratorMat(C)));
fi;
return ShallowCopy(H);
end);
InstallMethod(CheckMat, "method for cyclic code", true, [IsCyclicCode], 0,
function(C)
local F, H, p, n, i, R, zero, coeffs;
#if HasGeneratorMat(C) and IsInStandardForm(GeneratorMat(C), true) then
# H := TransposedMat(Concatenation(List(-GeneratorMat(C), x->
# x{[Dimension(C)+1..WordLength(C)]}),
# IdentityMat(Redundancy(C), LeftActingDomain(C))));
#else
F := LeftActingDomain(C);
H := [];
p := CheckPol(C);
p := Indeterminate(F)^Dimension(C)*Value(p,Indeterminate(F)^-1);
n := WordLength(C);
zero := Zero(F);
coeffs := CoefficientsOfLaurentPolynomial(p);
coeffs := ShiftedCoeffs(coeffs[1], coeffs[2]);
for i in [1..Redundancy(C)] do
R := NullVector(i-1, F);
Append(R, coeffs);
Append(R, NullVector(n-Length(R), F));
H[i] := R;
od;
#fi;
return ShallowCopy(H);
end);
#############################################################################
##
#F IsCyclicCode( <C> ) . . . . . . . . . . . . . . . . . . . . . . . . . .
##
InstallOtherMethod(IsCyclicCode, "method for unrestricted codes",
true, [IsCode], 0,
function(C)
if IsLinearCode(C) then
return IsCyclicCode(C);
else
return false;
fi;
end);
InstallMethod(IsCyclicCode, "method for linear codes", true, [IsLinearCode], 0,
function(C)
local C1, F, L, Gp;
F := LeftActingDomain(C);
L := List(GeneratorMat(C),
g->LaurentPolynomialByCoefficients(
ElementsFamily(FamilyObj(F)),One(F)*g, 0 ));
Add(L, Indeterminate(F)^WordLength(C) - One(F));
Gp := Gcd(L);
if Redundancy(C) = DegreeOfLaurentPolynomial(Gp) then
SetGeneratorPol(C, Gp);
return true;
else
return false; #so the code is not cyclic
fi;
end);
InstallOtherMethod(IsCyclicCode, "method for generic objects", true,
[IsObject], 0,
function(obj)
return IsCode(obj) and IsLinearCode(obj) and IsCyclicCode(obj);
end);
#############################################################################
##
#F GeneratorPol( <C> ) . . . . . . . . returns the generator polynomial of C
##
## Pre: C must have a generator or check polynomial
##
InstallMethod(GeneratorPol, "method for unrestricted codes", true, [IsCode], 0,
function(C)
if IsCyclicCode(C) then
return GeneratorPol(C);
else
Error("generator polynomial is only defined for cyclic codes");
fi;
end);
InstallMethod(GeneratorPol, "method for cyclic codes", true, [IsCyclicCode], 0,
function(C)
local F, n;
F := LeftActingDomain(C);
n := WordLength(C);
return EuclideanQuotient(One(F)*(Indeterminate(F)^n-1),CheckPol(C));
end);
#############################################################################
##
#F CheckPol( <C> ) . . . . . . . . returns the parity check polynomial of C
##
## Pre: C must have a generator or check polynomial
##
InstallMethod(CheckPol, "method for unrestricted codes", true, [IsCode], 0,
function(C)
if IsCyclicCode(C) then
return CheckPol(C);
else
Error("generator polynomial is only defined for cyclic codes");
fi;
end);
InstallMethod(CheckPol, "method for cyclic codes", true, [IsCyclicCode], 0,
function(C)
local F, n;
F := LeftActingDomain(C);
n := WordLength(C);
return EuclideanQuotient((Indeterminate(F)^n-One(F)),GeneratorPol(C));
end);
#############################################################################
##
#F MinimumDistanceCodeword( <C> [, <w>] ) . . . . determines a codeword
## having minimum distance to w
## (w= zero vector is the default)
##
##wdj,9-14-2007
InstallMethod(MinimumDistanceCodeword, "attribute method for linear codes", true,
[IsLinearCode], 0,
function(C)
local k, i, j, G, F, zero, AClosestVec, minwt, num, n, closestvec;
F := LeftActingDomain(C);
n := WordLength(C);
zero := Zero(F)*NullVector(n);
G := GeneratorMat(C);
minwt:=n;
closestvec := zero;
for i in [1..Length(G)] do
AClosestVec:=AClosestVectorCombinationsMatFFEVecFFE(G, F, zero, i, 1);
if WeightVecFFE(AClosestVec)<minwt then
minwt := WeightVecFFE(AClosestVec);
closestvec := AClosestVec;
fi;
od;
return(closestvec);
end);
InstallOtherMethod(MinimumDistance, "linear code, word", true,
[IsLinearCode, IsCodeword], 0,
function(C, word)
local k, i, j, G, F, zero, AClosestVec, minwt, num, n, closestvec;
F := LeftActingDomain(C);
n := WordLength(C);
zero := Zero(F)*NullVector(n);
G := GeneratorMat(C);
minwt:=n;
closestvec := word;
for i in [1..Length(G)] do
AClosestVec:=AClosestVectorCombinationsMatFFEVecFFE(G, F, word, i, 1);
if WeightVecFFE(AClosestVec)<minwt then
minwt := WeightVecFFE(AClosestVec);
closestvec := AClosestVec;
fi;
od;
return(closestvec);
end);
#############################################################################
##
#F MinimumDistance( <C> [, <w>] ) . . . . determines the minimum distance
##
## MinimumDistance( <C> ) determines the minimum distance of <C>
## MinimumDistance( <C>, <w> ) determines the minimum distance to a word <w>
##
InstallMethod(MinimumDistance, "attribute method for unrestricted codes", true,
[IsCode], 0,
function(C)
local W, El, F, n, zero, d, DD, w;
#Print("unrestricted code\n");
if IsBound(C!.upperBoundMinimumDistance) and
IsBound(C!.lowerBoundMinimumDistance) and
C!.upperBoundMinimumDistance = C!.lowerBoundMinimumDistance then
return C!.lowerBoundMinimumDistance;
elif IsCyclicCode(C) or IsLinearCode(C) then
return MinimumDistance(C);
fi;
W := VectorCodeword(AsSSortedList(C));
El := W; # so not a copy!
F := LeftActingDomain(C);
n := WordLength(C);
zero := Zero(F);
d := n;
DD := NullVector(n+1);
for w in W do
DD := DD + DistancesDistributionVecFFEsVecFFE(El, w);
od;
d := PositionProperty([2..n+1], i->DD[i] <> 0);
C!.lowerBoundMinimumDistance := d;
C!.upperBoundMinimumDistance := d;
return d;
end);
InstallMethod(MinimumDistance, "attribute method for linear codes", true,
[IsLinearCode], 0,
function(C)
local k, i, j, G, F, zero, AClosestVec, minwt, num, n;
if IsBound(C!.upperBoundMinimumDistance) and
IsBound(C!.lowerBoundMinimumDistance) and
C!.upperBoundMinimumDistance = C!.lowerBoundMinimumDistance then
return C!.lowerBoundMinimumDistance;
fi;
F := LeftActingDomain(C);
n := WordLength(C);
zero := Zero(F)*NullVector(n);
G := GeneratorMat(C);
minwt:=n;
for i in [1..Length(G)] do
AClosestVec:=AClosestVectorCombinationsMatFFEVecFFE(G, F, zero, i, 1);
if WeightVecFFE(AClosestVec)<minwt then
minwt := WeightVecFFE(AClosestVec);
fi;
od;
C!.lowerBoundMinimumDistance := minwt;
C!.upperBoundMinimumDistance := minwt;
return(minwt);
end);
InstallMethod(MinimumDistance, "attribute method for cyclic code", true,
[IsCyclicCode], 0,
function(C)
local md;
#Print("cyclic code\n");
if IsBound(C!.lowerBoundMinimumDistance) and
IsBound(C!.upperBoundMinimumDistance) and
C!.lowerBoundMinimumDistance = C!.upperBoundMinimumDistance then
return C!.lowerBoundMinimumDistance;
else
md := MinimumDistance( PuncturedCode( C ) ) + 1;
C!.lowerBoundMinimumDistance := md;
C!.upperBoundMinimumDistance := md;
return md;
fi;
end);
## Should be a better way to set up the Other methods, given
## how much overlap there is with attribute methods. For now, though,
## this works.
InstallOtherMethod(MinimumDistance, "unrestricted code, word", true,
[IsCode, IsCodeword], 0,
function(C, word)
local W, El, F, n, zero, d, w, DD;
#Print("unrestricted code, vector\n");
if IsLinearCode(C) then
return MinimumDistance(C, word);
fi;
if word in C then
return 0;
fi;
W := [VectorCodeword(Codeword(word, C))];
El := VectorCodeword(AsSSortedList(C));
F := LeftActingDomain(C);
n := WordLength(C);
zero := Zero(F);
d := n;
DD := NullVector(n+1);
for w in W do
DD := DD + DistancesDistributionVecFFEsVecFFE(El, w);
od;
d := PositionProperty([2..n+1], i->DD[i] <> 0);
return d;
end);
InstallOtherMethod(MinimumDistance, "linear code, word", true,
[IsLinearCode, IsCodeword], 0,
function(C, word)
local Mat, n, k, zero, UP, G, W, multiple, weight,
ThisGIsDone, #is true as the latest matrix is converted
Icount, #number of corrected generatormatrices
i, #first rownumber which could be added to I
l, #columnnumber which could be used
IdentityColumns,
j, CurW, UMD, w, q, tmp, F;
#Print("linear code, vector\n");
k := Dimension(C);
n := WordLength(C);
zero := Zero(LeftActingDomain(C));
q := Size(LeftActingDomain(C));
F := LeftActingDomain(C);
w := VectorCodeword(word);
if w in C then
return 0;
elif k = 0 then
return Weight(Codeword(w));
elif HasSyndromeTable(C) then
j := 1;
w := VectorCodeword( Syndrome(C, w) );
for i in [ 0 .. k - 1 ] do
if w[ k - i ] <> zero then
j := j + q^i * ( LogFFE( w[ k - i ] ) + 1 );
fi;
od;
return Weight(SyndromeTable(C)[j][1]);
fi;
UMD := Weight(Codeword(w));
#this must be so, because the kernel function
#can not find this distance
CurW := 0;
Mat := ShallowCopy(GeneratorMat(C));
i := 1;
## The next lines could go etwas faster for cyclic codes by weighting the
## generator polynomial, but a copy of this function must be made in the
## CycCodeOps, which makes it harder to make changes.
if q = 2 then
multiple := 2;
repeat
weight := 0;
for j in Mat[i] do
if not j = zero then
weight := weight + 1;
fi;
od;
multiple := Gcd( multiple, weight );
i := i + 1;
until multiple = 1 or i > k;
else
multiple := 1;
fi;
# we now know that the weight of all the elements are a multiple of multiple
UP := List([1..n], i->false); #which columns are already used
G := [];
W := [];
Icount := 0;
repeat
ThisGIsDone := false;
i := 1; # i is the row of the identitymatrix it
l := 1; # is trying to make
IdentityColumns := [];
while not ThisGIsDone and (l <= n) do
if not UP[l] then # try this column if it is not already used
j := i;
while (j <= k) and (Mat[j][l] = zero) do
j := j + 1; # go down in the matrix until a nonzero
od; # entry is found
if j <= k then
if j > i then
tmp := Mat[i];
Mat[i] := Mat[j];
Mat[j] := tmp;
fi;
Mat[i] := Mat[i]/Mat[i][l];
for j in Concatenation([1..i-1], [i+1..k]) do
if Mat[j][l] <> zero then
Mat[j] := Mat[j] - Mat[j][l]*Mat[i];
fi;
od;
UP[l] := true;
Add(IdentityColumns, l);
i := i + 1;
ThisGIsDone := ( i > k );
fi;
fi;
l := l + 1;
od;
if ThisGIsDone then
Icount := Icount + 1;
Add( G, Mat{[1..k]}{Difference([1..n],IdentityColumns)} );
w := w-w{IdentityColumns}*Mat;
Add(W,w{Difference([1..n], IdentityColumns)} );
UMD := Minimum( UMD, WeightCodeword( Codeword( w ) ) );
## G_i is generator matrix i
## W_i has zeros in IdentityColumns,
## but has same distance to code because
## only a codeword is added
fi;
until not ThisGIsDone or ( Icount = Int( n / k ) );
while CurW <= ( UMD - multiple ) / Icount do
i := 0;
repeat
i := i + 1;
UMD := Minimum( UMD, DistanceVecFFE( W[i],
AClosestVectorCombinationsMatFFEVecFFE(
G[i], F, W[i], CurW, CurW*(Icount-1) )
) + CurW );
until (i = Length(G)) or (UMD = CurW*Icount);
CurW := CurW + 1;
od;
return UMD;
end);
InstallMethod(MinimumDistanceLeon, "attribute method for linear codes", true,
[IsLinearCode], 0,
function(C)
local majority,G0, Gp, Gpt, Gt, L, k, i, j, dimMat, Grstr, J, d1, arrayd1, Combo, rows, row, rowSum, G, F, zero, AClosestVec, s, p, num;
G0 := GeneratorMat(C);
if (IsInStandardForm(G0)=false) then
G := List(G0,ShallowCopy);
PutStandardForm(G);
fi;
F:=LeftActingDomain(C);
if F<>GF(2) then Print("Code must be binary. Quitting. \n"); return(0); fi;
p:=5; #these seem to be optimal values
num:=8; #these seem to be optimal values
dimMat := DimensionsMat(G);
s := dimMat[2]-dimMat[1];
arrayd1:=[];
for k in [1..num] do
##Permute the columns randomly
Gt := TransposedMat(G);
Gp := NullMat(dimMat[2],dimMat[1]);
L := SymmetricGroup(dimMat[2]);
L := Random(L);
L:=List([1..dimMat[2]],i->OnPoints(i,L));
for i in [1..dimMat[2]] do
Gp[i] := Gt[L[i]];
od;
Gp := TransposedMat(Gp);
Gp := ShallowCopy(Gp);
##Use gaussian elimination on the new matrix
TriangulizeMat(Gp);
##generate the restricted code (I|Z) from Gp=(I|Z|B)
Gpt := TransposedMat(Gp);
Grstr := NullMat(s,dimMat[1]);
for i in [dimMat[1]+1..dimMat[1]+s] do
Grstr[i-dimMat[1]] := Gpt[i];
od;
Grstr := TransposedMat(Grstr);
zero := Zero(F)*Grstr[1];
##search for all rows of weight p
J := []; #col number of codewords to compute the length of
for i in [1..p] do
AClosestVec:=AClosestVectorCombinationsMatFFEVecFFE(Grstr, F, zero, i, 1);
if WeightVecFFE(AClosestVec) > 0 then
Add(J, [AClosestVec,i]);
fi;
od;
d1:=dimMat[2];
for rows in J do
d1:=Minimum(WeightVecFFE(rows[1])+rows[2],d1);
od;
arrayd1[k]:=d1;
od;
if AbsoluteValue(Sum(arrayd1)/Length(arrayd1)-Int(Sum(arrayd1)/Length(arrayd1)))<1/2 then
majority:=Int(Sum(arrayd1)/Length(arrayd1));
else
majority:=Int(Sum(arrayd1)/Length(arrayd1))+1;
fi;
return(majority);
end);
#############################################################################
##
#F LowerBoundMinimumDistance( arg ) . . . . . . . . . . . . . . . . . . .
##
##LR - Is there a better way to handle HasMD case, without reset?
InstallMethod(LowerBoundMinimumDistance, "method for unrestricted codes",
true, [IsCode], 0,
function(C)
if HasMinimumDistance(C) then
C!.lowerBoundMinimumDistance := MinimumDistance(C);
elif not IsBound(C!.lowerBoundMinimumDistance) then
if Size(C) = 1 then
C!.lowerBoundMinimumDistance := WordLength(C);
else
C!.lowerBoundMinimumDistance := 1;
fi;
fi;
return C!.lowerBoundMinimumDistance;
end);
InstallMethod(LowerBoundMinimumDistance, "method for linear code", true,
[IsLinearCode], 0,
function(C)
if HasMinimumDistance(C) then
C!.lowerBoundMinimumDistance := MinimumDistance(C);
elif not IsBound(C!.lowerBoundMinimumDistance) then
if Dimension(C) = 0 then
C!.lowerBoundMinimumDistance := WordLength(C);
elif Dimension(C) = 1 then
C!.lowerBoundMinimumDistance:= Weight(Codeword(GeneratorMat(C)[1]));
else
C!.lowerBoundMinimumDistance := 1;
fi;
fi;
return C!.lowerBoundMinimumDistance;
end);
InstallMethod(LowerBoundMinimumDistance, "method for cyclic codes", true,
[IsCyclicCode], 0,
function(C)
if HasMinimumDistance(C) then
C!.lowerBoundMinimumDistance := MinimumDistance(C);
elif not IsBound(C!.lowerBoundMinimumDistance) then
if Dimension(C) = 0 then
C!.lowerBoundMinimumDistance := WordLength(C);
elif Dimension(C) = 1 then
C!.lowerBoundMinimumDistance := Weight(Codeword(GeneratorPol(C)));
else
C!.lowerBoundMinimumDistance := 1;
fi;
fi;
return C!.lowerBoundMinimumDistance;
end);
InstallOtherMethod(LowerBoundMinimumDistance, "n, k, q", true,
[IsInt, IsInt, IsInt], 0,
function(n, k, q)
local r;
r := BoundsMinimumDistance(n, k, q, true);
return r.lowerBound;
end);
InstallOtherMethod(LowerBoundMinimumDistance, "n, k", true,
[IsInt, IsInt], 0,
function(n, k)
local r;
r := BoundsMinimumDistance(n, k, 2, true);
return r.lowerBound;
end);
InstallOtherMethod(LowerBoundMinimumDistance, "n, k, F", true,
[IsInt, IsInt, IsField], 0,
function(n, k, F)
local r;
r := BoundsMinimumDistance(n, k, Size(F), true);
return r.lowerBound;
end);
#############################################################################
##
#F UpperBoundMinimumDistance( arg ) . . . . . . . . . . . . . . . . . . .
##
##LR - is there a better way to handle HasMD case, without reset?
InstallMethod(UpperBoundMinimumDistance, "method for unrestricted codes",
true, [IsCode], 0,
function(C)
if HasMinimumDistance(C) then
C!.upperBoundMinimumDistance := MinimumDistance(C);
elif not IsBound(C!.upperBoundMinimumDistance) then
C!.upperBoundMinimumDistance := WordLength(C);
fi;
return C!.upperBoundMinimumDistance;
end);
InstallMethod(UpperBoundMinimumDistance, "method for linear codes", true,
[IsLinearCode], 0,
function(C)
local ubmd;
if HasMinimumDistance(C) then
C!.upperBoundMinimumDistance := MinimumDistance(C);
else
if not IsBound(C!.upperBoundMinimumDistance) then
ubmd := WordLength(C);
else
ubmd := C!.upperBoundMinimumDistance;
fi;
if MinimumWeightOfGenerators(C) < ubmd then
ubmd := MinimumWeightOfGenerators(C);
fi;
if UpperBoundOptimalMinimumDistance(C) < ubmd then
ubmd := UpperBoundOptimalMinimumDistance(C);
fi;
C!.upperBoundMinimumDistance := ubmd;
fi;
return C!.upperBoundMinimumDistance;
end);
InstallOtherMethod(UpperBoundMinimumDistance, "n, k, q", true,
[IsInt, IsInt, IsInt], 0,
function(n, k, q)
local r;
r := BoundsMinimumDistance(n, k, q, false);
return r.upperBound;
end);
InstallOtherMethod(UpperBoundMinimumDistance, "n,k", true,
[IsInt, IsInt], 0,
function(n, k)
local r;
r := BoundsMinimumDistance(n, k, 2, false);
return r.upperBound;
end);
InstallOtherMethod(UpperBoundMinimumDistance, "n,k,F", true,
[IsInt, IsInt, IsField], 0,
function(n, k, F)
local r;
r := BoundsMinimumDistance(n, k, Size(F), false);
return r.upperBound;
end);
#############################################################################
##
#F UpperBoundOptimalMinimumDistance( arg ) . . . . . . . . . . . . . . . .
##
## UpperBoundMinimumDistance of optimal code with given parameters
##
InstallMethod(UpperBoundOptimalMinimumDistance, "method for unrestricted code",
true, [IsCode], 0,
function(C)
local r;
r := BoundsMinimumDistance(WordLength(C), Dimension(C),
Size(LeftActingDomain(C)), false);
return r.upperBound;
end);
#############################################################################
##
#F MinimumWeightOfGenerators( arg ) . . . . . . . . . . . . . . . . . . . .
##
##
InstallMethod(MinimumWeightOfGenerators, "linear codes", true,
[IsLinearCode], 0,
function(C)
local zero, mwg, sum, element, row;
zero := Zero(LeftActingDomain(C));
mwg := WordLength(C);
if Dimension(C) > 0 then
# minimumWeightOfGenerators for null codes is n
for row in GeneratorMat(C) do
sum := 0;
for element in row do
if element <> zero then
sum := sum + 1;
fi;
od;
if sum < mwg then
mwg := sum;
fi;
od;
fi;
return mwg;
end);
InstallMethod(MinimumWeightOfGenerators, "method for cyclic codes", true,
[IsCyclicCode], 0,
function(C)
if Dimension(C) > 0 then
# minimumWeightOfGenerators of null codes is n
return Weight(Codeword(GeneratorPol(C)));
else
return WordLength(C);
fi;
end);
#############################################################################
##
#F MinimumWeightWords( <C> ) . . . returns the code words of minimum weight
##
InstallMethod(MinimumWeightWords, "method for unrestricted code", true,
[IsCode], 0,
function(C)
local curmin, res, e, w, zerovec, d;
if IsLinearCode(C) then
return MinimumWeightWords(C);
fi;
curmin := WordLength(C);
if not HasWeightDistribution(C) then
res := [];
for e in AsSSortedList(C) do
w := Weight(e);
if w < curmin and w <> 0 then
# New minimum weight found
curmin := w;
res := [ e ];
elif w = curmin then
Add(res, e);
fi;
od;
return res;
else
# Find the minimum weight
d := MinimumDistance(C);
return Filtered(AsSSortedList(C), e -> Weight(e) = d);
fi;
end);
InstallMethod(MinimumWeightWords, "method for linear code", true,
[IsLinearCode], 0,
function(C)
local d, G, res, vector, count, i, t, k, q, M, zerovec;
d := MinimumDistance(C); # Equal to minimum weight
G := GeneratorMat(C);
k := Dimension(C);
q := Size(LeftActingDomain(C));
M := Size(C);
res := [];
vector := NullVector(WordLength(C), LeftActingDomain(C));
zerovec := ShallowCopy(vector);
count := 1;
while count < M do
# Calculate next word in the code
i := k;
t := count;
while t mod q = 0 do
t := t / q;
i := i - 1;
od;
vector := vector + G[i];
if DistanceVecFFE(vector, zerovec) = d then
# This word has minimum weight
Add(res, Codeword(vector));
fi;
count := count + 1;
od;
return res;
end);
#############################################################################
##
#F WeightDistribution( <C> ) . . . returns the weight distribution of a code
##
InstallMethod(WeightDistribution, "method for unrestricted code", true,
[IsCode], 0,
function (C)
local El, nl, newwd;
if IsLinearCode(C) then
return WeightDistribution(C);
fi;
El := VectorCodeword(AsSSortedList(C));
nl := VectorCodeword(NullWord(C));
newwd := DistancesDistributionVecFFEsVecFFE(El, nl);
return newwd;
end);
InstallMethod(WeightDistribution, "method for linear code", true,
[IsLinearCode], 0,
function(C)
local G, nl, k, n, q, F, wd, newwd, oldrow, newrow, i, j;
n := WordLength(C);
k := Dimension(C);
q := Size(LeftActingDomain(C));
F := LeftActingDomain(C);
nl := VectorCodeword(NullWord(C));
if k = 0 then
G := NullVector(n+1);
G[1] := 1;
newwd := G;
elif k = n then
newwd := List([0..n], i->Binomial(n, i));
elif k <= Int(n/2) then
G := ShallowCopy(GeneratorMat(C));
newwd := DistancesDistributionMatFFEVecFFE(G, F, nl);
else
G := ShallowCopy(CheckMat(C));
wd := DistancesDistributionMatFFEVecFFE(G, F, nl);
newwd := [Sum(wd)];
oldrow := List([1..n+1], i->1);
newrow := [];
for i in [2..n+1] do
newrow[1] := Binomial(n, i-1) * (q-1)^(i-1);
for j in [2..n+1] do
newrow[j] := newrow[j-1] - (q-1) * oldrow[j] - oldrow[j-1];
od;
newwd[i] := newrow * wd;
oldrow := ShallowCopy(newrow);
od;
newwd:= newwd / (q ^ Redundancy(C));
fi;
return newwd;
end);
#############################################################################
##
#F InnerDistribution( <C> ) . . . . . . the inner distribution of the code
##
## The average distance distribution of distances between all codewords
##
InstallMethod(InnerDistribution, "method for unrestricted code", true,
[IsCode], 0,
function (C)
local ID, c, El;
El := VectorCodeword(AsSSortedList(C));
ID := List([1..WordLength(C)+1], i->0);
for c in El do
ID := ID + DistancesDistributionVecFFEsVecFFE(El, c);
od;
return ID/Size(C);
end);
InstallMethod(InnerDistribution, "method for linear codes", true,
[IsLinearCode], 0,
function (C)
return WeightDistribution(C);
end);
#############################################################################
##
#F OuterDistribution( <C> ) . . . . . . . . . . . . . . . . . . . . . . .
##
## the number of codewords on a distance i from all elements of GF(q)^n
##
InstallOtherMethod(OuterDistribution, "method for unrestricted code", true,
[IsCode], 0,
function (C)
local gen, q, n, F, zero, Els, res, vector, t, large, count, dd;
if IsLinearCode(C) then
return OuterDistribution(C);
fi;
q := Size(LeftActingDomain(C));
n := WordLength(C);
F := LeftActingDomain(C);
zero := Zero(F);
Els := VectorCodeword(AsSSortedList(C));
res := [[NullWord(C),WeightDistribution(C)]];
vector := NullVector(n, F);
t := n;
gen := Z(q);
large := One(F);
for count in [2..q^n] do
t := n;
while vector[t] = large do
vector[t] := zero;
t := t-1;
od;
if vector[t] = zero then
vector[t] := gen;
else
vector[t] := vector[t] * gen;
fi;
dd := DistancesDistributionVecFFEsVecFFE(Els, vector);
Add(res, [Codeword(vector), dd]);
od;
return res;
end);
InstallMethod(OuterDistribution, "method for linear codes", true,
[IsLinearCode], 0,
function(C)
local STentry, dtw, E, res, i;
E := AsSSortedList(C);
res := [];
for STentry in List(SyndromeTable(C), i -> i[1]) do
dtw := DistancesDistribution(C, STentry);
for i in E do
Add(res, [VectorCodeword(STentry) + i, dtw]);
od;
od;
return res;
end);
#############################################################################
##
#F InformationWord( Code, c ) . . . "decodes" a codeword c in C to the
## information "message" word m, so m*C=c
InstallMethod(InformationWord, "code, codeword", true, [IsCode, IsCodeword], 1,
function(C, c)
local m;
if not(c in C) then return "ERROR: codeword must belong to code"; fi;
if not(IsLinearCode(C)) then return "ERROR: code must be linear"; fi;
m := SolutionMat(List(GeneratorMat(C),List), VectorCodeword(c));
return Codeword(m);
end);
#############################################################################
##
#F IsSelfDualCode( <C> ) . . . . . . . . . determines whether C is self dual
##
## i.o.w. each codeword is orthogonal to all codewords (including itself)
##
InstallMethod(IsSelfDualCode, "method for unrestricted code", true,
[IsCode], 0,
function(C)
if IsCyclicCode(C) or IsLinearCode(C) then
return IsSelfDualCode(C);
else
return false;
fi;
end);
InstallMethod(IsSelfDualCode, "method for linear code", true,
[IsLinearCode], 0,
function(C)
if IsCyclicCode(C) then
return IsSelfDualCode(C);
elif Redundancy(C) <> Dimension(C) then
return false; #so the code is not self dual
else
return (GeneratorMat(C)*TransposedMat(GeneratorMat(C)) =
NullMat(Dimension(C),Dimension(C),LeftActingDomain(C)));
fi;
end);
InstallMethod(IsSelfDualCode, "method for cyclic codes", true,
[IsCyclicCode], 0,
function(C)
local r;
if Redundancy(C) <> Dimension(C) then
return false; #so the code is not self dual
else
r := ReciprocalPolynomial(GeneratorPol(C),Redundancy(C));
r := r/LeadingCoefficient(r);
return CheckPol(C) = r;
fi;
end);
#############################################################################
##
#F CodewordVector( <l>, <C> )
##
## only valid if C is linear!
##
InstallOtherMethod(CodewordVector,"vector and unrestricted code", true,
[IsList, IsCode], 0,
function(l, C)
if IsLinearCode(C) then
return CodewordVector(l,C);
else
Error("<r> is a non-linear code");# encoding not possible
fi;
end);
InstallOtherMethod(CodewordVector,"vector and linear code", true,
[IsList, IsLinearCode], 0,
function(l, C)
local s, i, k;
if IsCyclicCode(C) then
return CodewordVector(l,C);
else
l := VectorCodeword(Codeword(l, Dimension(C), LeftActingDomain(C)));
if GeneratorMat(C) = [] then
return NullMat(Length(l), WordLength(C), LeftActingDomain(C));
else
return Codeword(l*GeneratorMat(C), C);
fi;
fi;
end);
InstallOtherMethod(CodewordVector, "<list of codewords|vector>,cyclic code",
true, [IsList, IsCyclicCode], 0,
function(l, C)
local F, p;
F := LeftActingDomain(C);
l := Codeword(l, Dimension(C), F);
if IsList(l) and not IsCodeword(l) then
return List(l, i->CodewordVector(i,C));
else
return Codeword(PolyCodeword(l) * GeneratorPol(C), C);
fi;
end);
InstallOtherMethod(CodewordVector, "method for poly and cyclic code", true,
[IsUnivariatePolynomial, IsCyclicCode], 0,
function(p, C)
local F, w;
F := LeftActingDomain(C);
w := Codeword(p, Dimension(C), F);
return Codeword(PolyCodeword(w) * GeneratorPol(C), C);
end);
InstallOtherMethod(\*, "list with code", true, [IsList, IsCode], 0,
CodewordVector);
InstallOtherMethod(\*, "poly with code", true,
[IsUnivariatePolynomial, IsCode], 0, CodewordVector);
InstallOtherMethod(\*, "method for two codes", true, [IsCode, IsCode], 0,
function(C1, C2)
return DirectProductCode(C1, C2);
end);
#############################################################################
##
#F \+( <l>, <C> ) . . . . . . . . . . . . . . . . . . . . . . . . . . . .
##
##
InstallOtherMethod(\+, "method for codeword+code", true,
[IsCodeword, IsCode], 0,
function(w, C)
return CosetCode(C, w);
end);
InstallOtherMethod(\+, "method for code+codeword", true,
[IsCode, IsCodeword], 0,
function(C, w)
return CosetCode(C, w);
end);
InstallOtherMethod(\+, "method for two codes", true, [IsCode, IsCode], 0,
function(C1, C2)
return DirectSumCode(C1, C2);
end);
#############################################################################
##
#F \in( <l>, <C> ) . . . . . . true if the vector is an element of the code
##
##
InstallMethod(\in, "method for codeword in unrestricted code", true,
[IsCodeword, IsCode], 0,
function(w, C)
if WordLength(w) <> WordLength(C) then
return false;
else
return w in AsSSortedList(C);
fi;
end);
InstallMethod(\in, "method for list of codewords in unrestricted code", true,
[IsList, IsCode], 0,
function(l, C)
return ForAll(l, w->w in C);
end);
InstallMethod(\in, "method for unrestricted code in unrestricted code", true,
[IsCode, IsCode], 0,
function(C1, C2)
local l;
l := ShallowCopy(AsSSortedList(C1));
return ForAll(l, w->w in C2);
end);
InstallMethod(\in, "method for codeword in linear code", true,
[IsCodeword, IsLinearCode], 0,
function(w, C)
if WordLength(w) <> WordLength(C) then
return false;
elif GeneratorMat(C) = [] then
return w = 0*w;
elif CheckMat(C) = [] then #Code is WholeSpace, just check field.
return ForAll(VectorCodeword(w), x->x in LeftActingDomain(C));
else
return CheckMat(C)*w = 0*w;
fi;
end);
InstallMethod(\in, "method for linear code in linear code", true,
[IsLinearCode, IsLinearCode], 0,
function(C1, C2)
local l;
l := ShallowCopy(GeneratorMat(C1));
return ForAll(l, w-> Codeword(w) in C2);
end);
InstallMethod(\in, "method for codeword in cyclic code", true,
[IsCodeword, IsCyclicCode], 0,
function(w, C)
return PolyCodeword(w) mod GeneratorPol(C) =
0 * Indeterminate(LeftActingDomain(C));
end);
InstallMethod(\in, "method for cyclic code in cyclic code", true,
[IsCyclicCode, IsCyclicCode], 0,
function(C1, C2)
return GeneratorPol(C1) mod GeneratorPol(C2) =
0 * Indeterminate(LeftActingDomain(C2));
end);
#############################################################################
##
#F \=( <C1>, <C2> ) . . . . . tests if Set(AsList(C1))=Set(AsList(C2))
##
## Post: returns a boolean
##
InstallMethod(\=, "method for unrestricted code = unrestricted code", true,
[IsCode, IsCode], 0,
function(C1, C2)
local field, fields;
if (IsLinearCode(C1) and IsLinearCode(C2)) or
(IsCyclicCode(C1) and IsCyclicCode(C2)) then
return C1 = C2;
elif IsLinearCode(C1) or IsLinearCode(C2) then
return false; ##one is linear, the other is not, so not equal
fi;
if Set(AsSSortedList(C1)) = Set(AsSSortedList(C2)) then
fields := [WeightDistribution, InnerDistribution,
IsLinearCode, IsPerfectCode,
IsSelfDualCode, OuterDistribution, IsCyclicCode,
AutomorphismGroup, MinimumDistance, CoveringRadius];
for field in fields do
if not Tester(field)(C1) then
if Tester(field)(C2) then
Setter(field)(C1, field(C2));
fi;
else
if not Tester(field)(C2) then
Setter(field)(C2, field(C1));
fi;
fi;
od;
if not IsBound(C1!.boundsCoveringRadius) then
if IsBound(C2!.boundsCoveringRadius) then
C1!.boundsCoveringRadius := C2!.boundsCoveringRadius;
fi;
else
if not IsBound(C2!.boundsCoveringRadius) then
C2!.boundsCoveringRadius := C1!.boundsCoveringRadius;
fi;
fi;
C1!.lowerBoundMinimumDistance := Maximum(LowerBoundMinimumDistance(C1),
LowerBoundMinimumDistance(C2));
C2!.lowerBoundMinimumDistance := C1!.lowerBoundMinimumDistance;
C1!.upperBoundMinimumDistance := Minimum(UpperBoundMinimumDistance(C1),
UpperBoundMinimumDistance(C2));
C2!.upperBoundMinimumDistance := C1!.upperBoundMinimumDistance;
return true;
else
return false; #so C1 is not equal to C2
fi;
end);
InstallMethod(\=, "method for linear code = linear code", true,
[IsLinearCode, IsLinearCode], 0,
function(C1, C2)
local field, fields;
if IsCyclicCode(C1) and IsCyclicCode(C2) then
return C1 = C2;
elif IsCyclicCode(C1) or IsCyclicCode(C2) then
return false; ##one is cyclic, the other is not, so not equal.
fi;
if BaseMat(GeneratorMat(C1))=BaseMat(GeneratorMat(C2)) then
fields := [WeightDistribution, InnerDistribution,
IsLinearCode, IsPerfectCode,
IsSelfDualCode, OuterDistribution, IsCyclicCode,
AutomorphismGroup, MinimumDistance, CoveringRadius];
for field in fields do
if not Tester(field)(C1) then
if Tester(field)(C2) then
Setter(field)(C1, field(C2));
fi;
else
if not Tester(field)(C2) then
Setter(field)(C2, field(C1));
fi;
fi;
od;
if not IsBound(C1!.boundsCoveringRadius) then
if IsBound(C2!.boundsCoveringRadius) then
C1!.boundsCoveringRadius := C2!.boundsCoveringRadius;
fi;
else
if not IsBound(C2!.boundsCoveringRadius) then
C2!.boundsCoveringRadius := C1!.boundsCoveringRadius;
fi;
fi;
C1!.lowerBoundMinimumDistance := Maximum(LowerBoundMinimumDistance(C1),
LowerBoundMinimumDistance(C2));
C2!.lowerBoundMinimumDistance := C1!.lowerBoundMinimumDistance;
C1!.upperBoundMinimumDistance := Minimum(UpperBoundMinimumDistance(C1),
UpperBoundMinimumDistance(C2));
C2!.upperBoundMinimumDistance := C1!.upperBoundMinimumDistance;
return true;
else
return false; #so l is not equal to r
fi;
end);
InstallMethod(\=, "method for cyclic code = cyclic code", true,
[IsCyclicCode, IsCyclicCode], 0,
function(C1, C2)
local field, fields, bmdl, bmdr;
if GeneratorPol(C1) = GeneratorPol(C2) then
fields := [WeightDistribution, InnerDistribution,
IsLinearCode, IsPerfectCode,
IsSelfDualCode, OuterDistribution, IsCyclicCode,
AutomorphismGroup, MinimumDistance, CoveringRadius,
RootsOfCode];
for field in fields do
if not Tester(field)(C1) then
if Tester(field)(C2) then
Setter(field)(C1, field(C2));
fi;
else
if not Tester(field)(C2) then
Setter(field)(C2, field(C1));
fi;
fi;
od;
if not IsBound(C1!.boundsCoveringRadius) then
if IsBound(C2!.boundsCoveringRadius) then
C1!.boundsCoveringRadius := C2!.boundsCoveringRadius;
fi;
else
if not IsBound(C2!.boundsCoveringRadius) then
C2!.boundsCoveringRadius := C1!.boundsCoveringRadius;
fi;
fi;
C1!.lowerBoundMinimumDistance := Maximum(LowerBoundMinimumDistance(C1),
LowerBoundMinimumDistance(C2));
C2!.lowerBoundMinimumDistance := C1!.lowerBoundMinimumDistance;
C1!.upperBoundMinimumDistance := Minimum(UpperBoundMinimumDistance(C1),
UpperBoundMinimumDistance(C2));
C2!.upperBoundMinimumDistance := C1!.upperBoundMinimumDistance;
return true;
else
return false; #so l is not equal to r
fi;
end);
#############################################################################
##
#F SyndromeTable ( <C> ) . . . . . . . . . . . . . . . a Syndrome table of C
##
InstallMethod(SyndromeTable, "method for unrestricted code", true,
[IsCode], 0,
function(C)
if IsLinearCode(C) then
return SyndromeTable(C);
else
Error("the syndrome table is not defined for non-linear codes");
fi;
end);
InstallMethod(SyndromeTable, "method for linear code", true,
[IsLinearCode], 0,
function(C)
local H, L, F;
H := CheckMat(C);
if H = [] then
return [];
fi;
F := LeftActingDomain(C);
L := CosetLeadersMatFFE(List(H,List), F);
H := TransposedMat(H);
return Codeword(List(L, l-> [l, l*H]), F);
end);
#############################################################################
##
#F StandardArray( <C> ) . . . . . . . . . . . . a standard array for code C
##
## Post: returns a 3D-matrix. The first row contains all the codewords of C.
## The other rows contain the cosets, preceded by their coset leaders.
##
InstallMethod(StandardArray, "method for unrestricted code", true,
[IsCode], 0,
function(C)
if IsLinearCode(C) then
return StandardArray(C);
else
Error("a standard array is not defined for non-linear codes");
fi;
end);
InstallMethod(StandardArray, "method for linear code", true, [IsLinearCode], 0,
function(C)
local Els;
Els := AsSSortedList(C);
if CheckMat(C) = [] then
return [Els];
fi;
return List(Set(CosetLeadersMatFFE(CheckMat(C), LeftActingDomain(C))),
row -> List(Els, column -> row + column));
end);
#############################################################################
##
#F AutomorphismGroup( <C> ) . . . . . . . . the automorphism group of code
##
## The automorphism group is the largest permutation group of degree n such
## that for each permutation in the group C' = C
##
InstallOtherMethod(AutomorphismGroup, "method for unrestricted codes", true,
[IsCode], 0,
function(C)
local path;
path := DirectoriesPackagePrograms( "guava" );
if ForAny( ["desauto", "leonconv", "wtdist"],
f -> Filename( path, f ) = fail ) then
Print("desauto not loaded ... switching to PermutationGroup ...\n");
return PermutationGroup(C);
fi;
if Size(LeftActingDomain(C)) > 2 then
Print("This command calculates automorphism groups for binary codes only\n");
Print("... automatically switching to PermutationGroup ...\n");
return PermutationGroup(C);
elif IsLinearCode(C) then
return AutomorphismGroup(C);
else
return MatrixAutomorphisms(VectorCodeword(AsSSortedList(C)),
[], Group( () ));
fi;
end);
InstallOtherMethod(AutomorphismGroup, "method for linear codes", true,
[IsLinearCode], 0,
function(C)
local incode, inV, outgroup, infile,Ccalc,path;
path := DirectoriesPackagePrograms( "guava" );
if ForAny( ["desauto", "leonconv", "wtdist"],
f -> Filename( path, f ) = fail ) then
Print("desauto not loaded ... switching to PermutationGroup ...\n");
return PermutationGroup(C);
fi;
if Size(LeftActingDomain(C)) > 2 then
Print("This command calculates automorphism groups for binary codes only\n");
Print("... automatically switching to PermutationGroup ...\n");
return PermutationGroup(C);
fi;
incode := TmpName(); PrintTo( incode, "\n" );
inV := TmpName(); PrintTo( inV, "\n" );
outgroup := TmpName(); PrintTo( outgroup, "\n" );
infile := TmpName(); PrintTo( infile, "\n" );
# Calculate with dual code if it is smaller:
if Dimension(C) > QuoInt(WordLength(C), 2) then
Ccalc := DualCode(C);
else
Ccalc := ShallowCopy(C);
fi;
GuavaToLeon(Ccalc, incode);
Exec(Filename(DirectoriesPackagePrograms("guava"), "wtdist"),
Concatenation("-q ",incode,"::code ",
String(MinimumDistance(Ccalc))," ",inV,"::code"));
Exec(Filename(DirectoriesPackagePrograms("guava"), "desauto"),
Concatenation("-code -q ",
incode,"::code ",inV,"::code ",outgroup));
Exec(Filename(DirectoriesPackagePrograms("guava"), "leonconv"),
Concatenation("-a ",outgroup," ", infile));
Read(infile);
RemoveFiles(incode,inV,outgroup,infile);
return GUAVA_TEMP_VAR;
end);
## If the new partition backtrack algorithms are implemented, the previous
## function can be replaced by the next:
#InstallOtherMethod(AutomorphismGroup, "method for linear code", true,
# [IsLinearCode], 0,
#function(C)
# local Ccalc, InvSet;
# if Dimension(C) > QuoInt(WordLength(C), 2) then
# Ccalc := DualCode(C);
# else
# Ccalc := ShallowCopy(C);
# fi;
# InvSet := VectorCodeword(MinimumWeightWords(Ccalc));
# return AutomorphismGroupBinaryLinearCode(Ccalc, InvSet);
#end);
#############################################################################
##
## PermutationGroup( <C> ) . . . . . . PermutationGroup of non-binary code
##
## confusing name?
InstallMethod(PermutationGroup, "attribute method for linear codes", true,
[IsLinearCode], 0,
function(C)
local G0, Gell, G1, G2, Gt, L, k, i, j, G, F, A, aut, n, Sn, ell;
Print("\n To be deprecated. Please use PermutationAutomorphismGroup.\n");
F:=LeftActingDomain(C);
G1 := GeneratorMat(C);
G := List(G1,ShallowCopy);
k:=DimensionsMat(G)[1];
n:=DimensionsMat(G)[2];
TriangulizeMat(G);
Gt := TransposedMat(G);
Sn := SymmetricGroup(n);
A:=[];
for ell in Sn do
G2:= NullMat(n,k);
for j in [1..n] do
G2[j]:=Gt[OnPoints(j,ell)];
od; # j
Gell := TransposedMat(G2);
G0 := List(Gell,ShallowCopy);
TriangulizeMat(G0);
if G = G0 then Add(A, ell); fi;
od; # ell
if Length(A)>0 then
aut := Group(A);
else aut:=Group(());
fi;
return(aut);
end);
#############################################################################
##
## PermutationAutomorphismGroup( <C> ) . . Permutation automorphism
## group of linear (possibly non-binary) code
##
##
InstallMethod(PermutationAutomorphismGroup, "attribute method for linear codes", true,
[IsLinearCode], 0,
function(C)
local G0, Gell, G1, G2, Gt, L, k, i, j, G, F, A, aut, n, Sn, ell;
F:=LeftActingDomain(C);
G1 := GeneratorMat(C);
G := List(G1,ShallowCopy);
k:=DimensionsMat(G)[1];
n:=DimensionsMat(G)[2];
TriangulizeMat(G);
Gt := TransposedMat(G);
Sn := SymmetricGroup(n);
A:=[];
for ell in Sn do
G2:= NullMat(n,k);
for j in [1..n] do
G2[j]:=Gt[OnPoints(j,ell)];
od; # j
Gell := TransposedMat(G2);
G0 := List(Gell,ShallowCopy);
TriangulizeMat(G0);
if G = G0 then Add(A, ell); fi;
od; # ell
if Length(A)>0 then
aut := Group(A);
else aut:=Group(());
fi;
return(aut);
end);
#############################################################################
##
#F IsSelfOrthogonalCode( <C> ) . . . . . . . . . . . . . . . . . . . . . .
##
InstallTrueMethod(IsSelfOrthogonalCode, IsSelfDualCode);
InstallMethod(IsSelfOrthogonalCode, "method for unrestricted code", true,
[IsCode], 0,
function(C)
local El, M, zero, i, j, IsSO;
if IsLinearCode(C) then
return IsSelfOrthogonalCode(C);
fi;
El := AsSSortedList(C);
M := Size(C);
zero := Zero(LeftActingDomain(C));
i := 1; IsSO := true;
while (i <= M-1) and IsSO do
j := i+1;
while (j <= M) and (El[i]*El[j] = zero) do
j := j + 1;
od;
if j <= M then
IsSO := false;
fi;
i := i + 1;
od;
return IsSO;
end);
InstallMethod(IsSelfOrthogonalCode, "method for linear code", true,
[IsLinearCode], 0,
function(C)
local G, k;
G := GeneratorMat(C);
k := Dimension(C);
return G*TransposedMat(G) = NullMat(k,k,LeftActingDomain(C));
end);
#############################################################################
##
#F IsDoublyEvenCode( <C> ) . . . . . . . . . . . . . . . . . . . . . . . .
##
## Return true if and only if the code C is a binary linear code which has
## all codewords of weight divisible by 4 only.
##
## If a binary linear code is self-orthogonal and the weight of each row
## in its generator matrix is divisibly by 4, the code is doubly-even
## (see Theorem 1.4.8 in W. C. Huffman and V. Pless, "Fundamentals of
## error-correcting codes", Cambridge Univ. Press, 2003.)
##
InstallMethod(IsDoublyEvenCode, "method for binary linear code", true,
[IsLinearCode], 0,
function(C)
local G, i;
if LeftActingDomain(C)<>GF(2) then
Error("Code must be binary\n");
fi;
G:=GeneratorMat(C);
for i in [1..Size(G)] do;
if Weight(Codeword(G[i])) mod 4 <> 0 then
return false;
fi;
od;
return IsSelfOrthogonalCode(C);
end);
#############################################################################
##
#F IsSinglyEvenCode( <C> ) . . . . . . . . . . . . . . . . . . . . . . . .
##
## Return true if and only if the code C is a self-orthogonal binary linear
## code which is not doubly-even.
##
InstallMethod(IsSinglyEvenCode, "method for binary linear code", true,
[IsLinearCode], 0,
function(C)
if LeftActingDomain(C)<>GF(2) then
Error("Code must be binary\n");
fi;
return (IsSelfOrthogonalCode(C)) and (not IsDoublyEvenCode(C));
end);
#############################################################################
##
#F IsEvenCode( <C> ) . . . . . . . . . . . . . . . . . . . . . . . . . . . .
##
## Return true if and only if the code C is a binary linear code which has
## even weight codewords--regardless whether or not it is self-orgthogonal.
##
InstallMethod(IsEvenCode, "method for binary linear code", true,
[IsLinearCode], 0,
function(C)
if LeftActingDomain(C)<>GF(2) then
Error("Code must be binary\n");
fi;
return (C = EvenWeightSubcode(C));
end);
#############################################################################
##
#F CodeIsomorphism( <C1>, <C2> ) . . the permutation that translates C1 into
#F C2 if C1 and C2 are equivalent, or false otherwise
##
InstallMethod(CodeIsomorphism, "method for two unrestricted codes", true,
[IsCode, IsCode], 0,
function(C1, C2)
local tp, field;
if WordLength(C1) <> WordLength(C2) or Size(C1) <> Size(C2)
or MinimumDistance(C1) <> MinimumDistance(C2)
or LeftActingDomain(C1) <> LeftActingDomain(C2) then
return false; #I think this is what we want (see IsEquivalentCode)
elif C1=C2 then
return ();
elif IsLinearCode(C1) and IsLinearCode(C2) then
return CodeIsomorphism(C1, C2 );
fi;
tp := TransformingPermutations(VectorCodeword(AsSSortedList(C1)),
VectorCodeword(AsSSortedList(C2)));
if tp <> false then
for field in [WeightDistribution, InnerDistribution,
IsPerfectCode, IsSelfDualCode] do
if not Tester(field)(C1) then
if Tester(field)(C2) then
Setter(field)(C1, field(C2));
fi;
else
if not Tester(field)(C2) then
Setter(field)(C2, field(C1));
fi;
fi;
od;
if not IsBound(C1!.boundsCoveringRadius) then
if IsBound(C2!.boundsCoveringRadius) then
C1!.boundsCoveringRadius := C2!.boundsCoveringRadius;
fi;
else
if not IsBound(C2!.boundsCoveringRadius) then
C2!.boundsCoveringRadius := C1!.boundsCoveringRadius;
fi;
fi;
C1!.lowerBoundMinimumDistance := Maximum(LowerBoundMinimumDistance(C1),
LowerBoundMinimumDistance(C2));
C2!.lowerBoundMinimumDistance := LowerBoundMinimumDistance(C1);
C1!.upperBoundMinimumDistance := Minimum(UpperBoundMinimumDistance(C1),
UpperBoundMinimumDistance(C2));
C2!.upperBoundMinimumDistance := UpperBoundMinimumDistance(C1);
return tp.columns;
else
return false; #yes, this is right
fi;
end);
InstallMethod(CodeIsomorphism, "method for two linear codes", true,
[IsLinearCode, IsLinearCode], 0,
function(C1, C2)
local code1,code2,cwcode1,cwcode2,output,infile, field;
if WordLength(C1) <> WordLength(C2) or Size(C1) <> Size(C2)
or MinimumDistance(C1) <> MinimumDistance(C2)
or LeftActingDomain(C1) <> LeftActingDomain(C2) then
return false; #I think this is what we want (see IsEquivalentCode)
elif C1=C2 then
return ();
elif LeftActingDomain(C1) <> GF(2) then
Error("GUAVA can only calculate equivalence over GF(2)");
fi;
code1 := TmpName(); PrintTo( code1, "\n" );
code2 := TmpName(); PrintTo( code2, "\n" );
cwcode1 := TmpName(); PrintTo( cwcode1, "\n" );
cwcode2 := TmpName(); PrintTo( cwcode2, "\n" );
output := TmpName(); PrintTo( output, "\n" );
infile := TmpName(); PrintTo( infile, "\n" );
GuavaToLeon(C1, code1);
GuavaToLeon(C2, code2);
Exec(Filename(DirectoriesPackagePrograms("guava"), "wtdist"),
Concatenation("-q ",code1,"::code ",
String(MinimumDistance(C1))," ",cwcode1,"::code"));
Exec(Filename(DirectoriesPackagePrograms("guava"), "wtdist"),
Concatenation("-q ",code2,"::code ",
String(MinimumDistance(C2))," ",cwcode2,"::code"));
Exec(Filename(DirectoriesPackagePrograms("guava"), "desauto"),
Concatenation("-iso -code -q ",
code1,"::code ",code2,"::code ",cwcode1,"::code ",
cwcode2,"::code ",output));
Exec(Filename(DirectoriesPackagePrograms("guava"), "leonconv"),
Concatenation("-e ",output," ",
infile));
Read(infile);
RemoveFiles(code1,code2,cwcode1,cwcode2,output,infile);
if not IsPerm(GUAVA_TEMP_VAR) then
return false; #it is good that false is returned
else
for field in [WeightDistribution,
IsPerfectCode,
IsSelfDualCode] do
if not Tester(field)(C1) then
if Tester(field)(C2) then
Setter(field)(C1, field(C2));
fi;
else
if not Tester(field)(C2) then
Setter(field)(C2, field(C1));
fi;
fi;
od;
if not IsBound(C1!.boundsCoveringRadius) then
if IsBound(C2!.boundsCoveringRadius) then
C1!.boundsCoveringRadius := C2!.boundsCoveringRadius;
fi;
else
if not IsBound(C2!.boundsCoveringRadius) then
C2!.boundsCoveringRadius := C1!.boundsCoveringRadius;
fi;
fi;
C1!.lowerBoundMinimumDistance := Maximum(LowerBoundMinimumDistance(C1),
LowerBoundMinimumDistance(C2));
C2!.lowerBoundMinimumDistance := LowerBoundMinimumDistance(C1);
C1!.upperBoundMinimumDistance := Minimum(UpperBoundMinimumDistance(C1),
UpperBoundMinimumDistance(C2));
C2!.upperBoundMinimumDistance := UpperBoundMinimumDistance(C1);
return GUAVA_TEMP_VAR;
fi;
end);
## If the new partition backtrack algorithms are implemented, the previous
## function can be replaced by the next:
#InstallMethod(CodeIsomorphism, "method for linear codes", true,
# [IsLinearCode, IsLinearCode], 0,
#function (C1, C2)
# local field, InvSet1, InvSet2, P;
# if WordLength(C1) <> WordLength(C2) or Size(C1) <> Size(C2)
# or MinimumDistance(C1) <> MinimumDistance(C2)
# or LeftActingDomain(C1) <> LeftActingDomain(C2) then
# return false; #I think this is what we want (see IsEquivalentCode)
# elif C1=C2 then
# return ();
# elif LeftActingDomain(C1) <> GF(2) then
# Error("GUAVA can only calculate equivalence over GF(2)");
# fi;
# InvSet1 := VectorCodeword(MinimumWeightWords(C1));
# InvSet2 := VectorCodeword(MinimumWeightWords(C2));
# P := AutomorphismGroupBinaryLinearCode(C1, InvSet1, C2, InvSet2);
# if not IsPerm(P) then
# return false; #it is good that false is returned
# else
# for field in [WeightDistribution,
# IsPerfectCode,
# IsSelfDualCode] do
# if not Tester(field)(C1) then
# if Tester(field)(C2) then
# Setter(field)(C1, field(C2));
# fi;
# else
# if not Tester(field)(C2) then
# Setter(field)(C2, field(C1));
# fi;
# fi;
# od;
#
# if not IsBound(C1!.boundsCoveringRadius) then
# if IsBound(C2!.boundsCoveringRadius) then
# C1!.boundsCoveringRadius := C2!.boundsCoveringRadius;
# fi;
# else
# if not IsBound(C2!.boundsCoveringRadius) then
# C2!.boundsCoveringRadius := C1!.boundsCoveringRadius;
# fi;
# fi;
# C1!.lowerBoundMinimumDistance := Maximum(LowerBoundMinimumDistance(C1),
# LowerBoundMinimumDistance(C2));
# C2!.lowerBoundMinimumDistance := LowerBoundMinimumDistance(C1);
# C1!.upperBoundMinimumDistance := Minimum(UpperBoundMinimumDistance(C1),
# UpperBoundMinimumDistance(C2));
# C2!.upperBoundMinimumDistance := UpperBoundMinimumDistance(C1);
# return P;
# fi;
#end);
#############################################################################
##
#F IsEquivalent( <C1>, <C2> ) . . . . . . true if C1 and C2 are equivalent
##
## that is if there exists a permutation that transforms C1 into C2.
## If returnperm is true, this permutation (if it exists) is returned;
## else the function only returns true or false. Has a global dispatcher.
##
InstallMethod(IsEquivalent, "method for unrestricted codes", true,
[IsCode, IsCode], 0,
function (C1, C2 )
return not IsBool( CodeIsomorphism( C1, C2 ) );
end);
#############################################################################
##
#F RootsOfCode( <C> ) . . . . the roots of the generator polynomial of <C>
##
## It finds the roots by trying all elements of the extension field
##
InstallMethod(RootsOfCode, "method for unrestricted code", true,
[IsCode], 0,
function(C)
if IsCyclicCode(C) then
return RootsOfCode(C);
else
Error("the roots of a code are only defined for cyclic codes");
fi;
end);
InstallMethod(RootsOfCode, "method for cyclic code", true, [IsCyclicCode], 0,
function(C)
local a, roots, zero, i, t, G;
G := GeneratorPol(C);
a := PrimitiveUnityRoot(Size(LeftActingDomain(C)), WordLength(C));
roots := [];
zero := 0*a;
t := a^0;
for i in [0..WordLength(C)-1] do
if Value(G, t) = zero then
Add(roots, t);
fi;
t := t * a;
od;
return(Set(roots));
end);
#############################################################################
##
#F DistancesDistribution( <C>, <w> ) . . . distribution of distances from a
#F word w to all codewords of C
##
InstallMethod(DistancesDistribution,
"method for unrestricted code and codeword",
true, [IsCode, IsCodeword], 0,
function(C, w)
local El;
if IsLinearCode(C) then
return DistancesDistribution(C,w);
fi;
El := VectorCodeword(AsSSortedList(C));
w := VectorCodeword(w);
return DistancesDistributionVecFFEsVecFFE(El,w);
end);
InstallMethod(DistancesDistribution, "method for linear code and codeword",
true, [IsLinearCode, IsCodeword], 0,
function(C, w)
local G;
G := ShallowCopy(GeneratorMat(C));
w := VectorCodeword(w);
return DistancesDistributionMatFFEVecFFE(G, LeftActingDomain(C), w);
end);
#############################################################################
##
#F Syndrome( <C>, <c> ) . . . . . . . the syndrome of word <c> in code <C>
##
InstallMethod(Syndrome, "method for unrestricted code and codeword", true,
[IsCode, IsCodeword], 0,
function(C, c)
if not IsLinearCode(C) then
Error("argument must be a linear code");
else
return Syndrome(C,c);
fi;
end);
InstallMethod(Syndrome, "method for linear code and codeword", true,
[IsLinearCode, IsCodeword], 0,
function(C, c)
if CheckMat(C) = [] then
return [Zero(LeftActingDomain(C))];
else
return CheckMat(C) * Codeword(c,C);
fi;
end);
#############################################################################
##
#F CodewordNr( <C>, <i> ) . . . . . . . . . . . . . . . . . elements(C)[i]
##
InstallMethod(CodewordNr, "method for unrestricted code and position list",
true, [IsCode, IsList], 0,
function(C, l)
local returnlist;
if IsLinearCode(C) then
return CodewordNr(C,l);
fi;
l := Set(l);
returnlist := (Length(l) > 1);
if (l[1] < 1) or (l[Length(l)] > Size(C)) then
Error("range: 1..", String(Size(C)));
fi;
if returnlist then
return AsSSortedList(C){l};
else
return Flat(AsSSortedList(C){l})[1];
fi;
end);
DoCodewordNr:=function(C, l)
local index, source, i, result, q, returnlist, F, kmin;
if IsList(l) then
l := Set(l);
returnlist:=true;
else
l:=[l];
returnlist:=false;
fi;
if (l[1] < 1) or (l[Length(l)] > Size(C)) then
Error("range: 1..", String(Size(C)));
fi;
if HasAsSSortedList(C) then
if returnlist then
return AsSSortedList(C){l};
else
return AsSSortedList(C)[l[1]];
fi;
else
result := [];
q := Size(LeftActingDomain(C));
F := LeftActingDomain(C);
kmin := Dimension(C) - 1;
for index in l do
source := [];
i := index-1;
while i >= 1 do
Add(source, i mod q);
i := Int(i / q);
od;
for i in [Length(source)..kmin] do
Add(source, 0);
od;
Add(result, CodewordVector(Reversed(source),C));
od;
if returnlist then
return result;
else
return result[1];
fi;
fi;
end;
InstallOtherMethod(CodewordNr, "method for unrestricted code and int position",
true, [IsCode, IsInt], 0,DoCodewordNr);
InstallMethod(CodewordNr, "method for linear code and position list", true,
[IsLinearCode, IsList], 0, DoCodewordNr);
#############################################################################
##
#F String( <C> ) . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
##
##
InstallMethod(String, "method for code", true, [IsCode], 0,
function(C)
return CodeDescription(C);
end);
#############################################################################
##
#F CodeDescription( <C> ) . . . . . . . . . . . . . . . . . . . . . . . . .
##
##
InstallMethod(CodeDescription, "method for an unrestricted code",
true, [IsCode], 0,
function(C)
local n, x, lbmd, ubmd, line;
line := "a";
if Int(WordLength(C)/(10^LogInt(WordLength(C),10))) = 8
or WordLength( C ) = 11 or WordLength( C ) = 18 then
Append(line, "n");
fi;
Append(line,Concatenation(" (",String(WordLength(C)),",",
String(Size(C)),","));
lbmd := String( LowerBoundMinimumDistance(C));
ubmd := String( UpperBoundMinimumDistance(C));
if lbmd = ubmd then
Append(line, lbmd );
else
Append( line, Concatenation( lbmd, "..", ubmd ) );
fi;
Append( line, ")" );
# Call to BoundsCoveringRadius checks HasCoveringRadius first.
# No need to repeat here. Similar for LBMD, UBMD, above.
if Length( BoundsCoveringRadius( C ) ) = 1 then
SetCoveringRadius(C, BoundsCoveringRadius(C)[1]);
Append( line, String( BoundsCoveringRadius(C)[ 1 ] ) );
else
Append( line, Concatenation(
String( BoundsCoveringRadius(C)[ 1 ] ),
"..",
String( BoundsCoveringRadius(C)[
Length( BoundsCoveringRadius(C) ) ] ) ) );
fi;
Append( line, " " );
if not IsBound( C!.name ) then
C!.name := "unknown unrestricted code";
fi;
Append( line, C!.name );
if not IsBound( C!.history ) then
Append(line,Concatenation(" over GF(", String(Size(LeftActingDomain(C))),")"));
fi;
IsString( line );
return line;
end);
InstallMethod(CodeDescription, "method for linear code",
true, [IsLinearCode], 0,
function(C)
local lbmd, ubmd, line;
line := "a linear [";
line := Concatenation( line, String(WordLength(C)), ",",
String(Dimension(C)), "," );
lbmd := String( LowerBoundMinimumDistance(C) );
ubmd := String( UpperBoundMinimumDistance(C) );
if lbmd = ubmd then
Append(line, lbmd );
else
Append(line,Concatenation( lbmd, "..", ubmd ) );
fi;
Append( line, "]" );
if Length( BoundsCoveringRadius( C ) ) = 1 then
Append( line, String( BoundsCoveringRadius(C)[ 1 ] ) );
else
Append( line, Concatenation(
String( BoundsCoveringRadius(C)[ 1 ] ),
"..",
String( Maximum( BoundsCoveringRadius(C) ) ) ) );
fi;
Append( line, " " );
if not IsBound( C!.name ) then
C!.name := "unknown linear code";
fi;
Append( line, C!.name );
if not IsBound( C!.history ) then
Append(line,Concatenation(" over GF(", String(Size(LeftActingDomain(C))),")"));
fi;
IsString( line );
return line;
end);
InstallMethod(CodeDescription, "method for cyclic codes",
true, [IsCyclicCode], 0,
function(C)
local n, x, lbmd, ubmd, line;
line:="a cyclic [";
Append( line, Concatenation( String(WordLength(C)), ",",
String(Dimension(C)), "," ));
lbmd := String( LowerBoundMinimumDistance(C) );
ubmd := String( UpperBoundMinimumDistance(C) );
if lbmd = ubmd then
Append(line, lbmd );
else
Append(line,Concatenation( lbmd, "..", ubmd ) );
fi;
Append( line, "]" );
if Length( BoundsCoveringRadius( C ) ) = 1 then
Append( line, String( BoundsCoveringRadius(C)[ 1 ] ) );
else
Append( line, Concatenation(
String( BoundsCoveringRadius(C)[ 1 ] ),
"..",
String( BoundsCoveringRadius(C)[
Length( BoundsCoveringRadius(C) ) ] ) ) );
fi;
Append( line, " " );
if not IsBound( C!.name ) then
C!.name := "unknown cyclic code";
fi;
Append( line, C!.name );
if not IsBound( C!.history ) then
Append(line,Concatenation(" over GF(", String(Size(LeftActingDomain(C))),")"));
fi;
IsString( line );
return line;
end);
#############################################################################
##
#F Print( <C> ) . . . . . . . . . . . . . prints short information about C
##
##
InstallMethod(PrintObj, "method for codes", true, [IsCode], 0,
function(C)
if HasCoveringRadius(C) and HasMinimumDistance(C) then
Print(String(C)); # sets for future use
else
Print(CodeDescription(C)); # allows to change as bmd, bcr updated
fi;
end);
InstallMethod(PrintObj, "method for linear code", true,
[IsFreeLeftModule and IsLinearCodeRep], 0,
function(C)
if HasCoveringRadius(C) and HasMinimumDistance(C) then
Print(String(C)); #sets for future use
else
Print(CodeDescription(C)); # allows to change as bcr, bmd updated
fi;
end);
InstallMethod(ViewObj, "method for codes", true, [IsCode], 0,
function(C)
if HasCoveringRadius(C) and HasMinimumDistance(C) then
Print(String(C)); # sets for future use
else
Print(CodeDescription(C)); # allows to change as bcr, bmd updated
fi;
end);
InstallMethod(ViewObj, "method for linear code", true,
[IsFreeLeftModule and IsLinearCodeRep], 0,
function(C)
local line;
if HasCoveringRadius(C) and HasMinimumDistance(C) then
Print(String(C)); #sets for future use
elif (IsBound(C!.name) and C!.name="random linear code") then
line := Concatenation( "a [", String(WordLength(C)), ",",String(Dimension(C)), "," );
Append(line,Concatenation( "?] randomly generated code over GF(",String(Size(LeftActingDomain(C))),")") );
Print(line);
elif (IsBound(C!.name) and C!.name="code defined by generator matrix, NC") then
line := Concatenation( "a [", String(WordLength(C)), ",",String(Dimension(C)), "," );
Append(line,Concatenation( "?] randomly generated code over GF(",String(Size(LeftActingDomain(C))),")") );
Print(line);
else
Print(CodeDescription(C)); # allows to change as bcr, bmd updated
fi;
end);
#############################################################################
##
#F Display( <C> ) . . . . . . . . . . . . prints the history of the code C
##
##
InstallMethod(Display, "method for codes", true, [IsCode], 0,
function(C)
local d;
for d in History(C) do
Print(d,"\n");
od;
end);
#############################################################################
##
#F Save( <filename>, <C>, <var-name> ) . . . . . writes the code C to a file
##
## with variable name var-name. It can be read back by calling
## Read (filename); the code then has the name var-name.
## All fields of the code record are stored except for the operations field
## and, in case of a linear or cyclic code, the elements.
## Pre: filename is accessible for writing
##
InstallMethod(Save, "method for unrestricted code", true,
[IsString, IsCode, IsString], 0,
function(filename, C, codename)
local fld, attr_list, attr;
PrintTo(filename, "\n# GUAVA code #\n");
##LR - need proper code creation statement here!
AppendTo(filename, codename, " := rec(\n");
attr_list := [CheckMat, CheckPol, CodeDensity, CodeNorm,
CoordinateNorm, CoveringRadius, DesignedDistance,
Dimension, GeneratorMat, GeneratorPol,
GeneratorsOfLeftModule,
InnerDistribution, IsAffineCode, IsAlmostAffineCode,
IsCyclicCode, IsGriesmerCode, IsLinearCode,
IsMDSCode, IsNormalCode, IsPerfectCode,
IsSelfComplementaryCode, IsSelfDualCode,
IsSelfOrthogonalCode,
LeftActingDomain, MinimumDistance,
MinimumWeightOfGenerators, MinimumWeightWords,
OuterDistribution, Redundancy,
RootsOfCode, SpecialCoveringRadius, SpecialDecoder,
StandardArray, SyndromeTable,
UpperBoundOptimalMinimumDistance,
WeightDistribution, WordLength ];
for attr in attr_list do
if Tester(attr)(C) then
AppendTo(filename, "Set", NameFunction(attr), "(", codename, ", ",
attr(C), ");\n");
fi;
od;
for fld in ["boundsCoveringRadius", "lowerBoundMinimumDistance",
"upperBoundMinimumDistance"] do
if IsBound(C!.(fld)) then
AppendTo(filename, codename, "!.", fld, ":=", C!.(fld), ";\n" );
fi;
od;
if (not HasIsLinearCode(C)) or (not IsLinearCode(C)) then
AppendTo(filename,
"SetAsSSortedList(", codename, ", ", "Codeword(",
VectorCodeword(AsSSortedList(C)),");\n");
fi;
AppendTo(filename, "C!.name := \"", C!.name,"\";\n");
end);
#############################################################################
##
#F History( <C> ) . . . . . . . . . . . . . . . shows the history of a code
##
InstallMethod(History, "method for codes", true, [IsCode], 0,
function(C)
local s;
if not IsBound(C!.history) then
return [CodeDescription(C)];
else
s := String(Concatenation(CodeDescription(C), " of"));
return Concatenation( [s], C!.history);
fi;
end);
######################################################################################
##
#F MinimumDistanceRandom( <C>, <num>, <s> )
##
## This is a simpler version than Leon's method, which does not put G in st form.
## (this works welland is in some cases faster than the st form one)
## Input: C is a linear code
## num is an integer >0 which represents the number of iterations
## s is an integer between 1 and n which represents the columns considered
## in the algorithm.
## Output: an integer >= min dist(C), and hopefully equal to it!
## a codework of that weight
##
## Algorithm: randomly permute the columns of the gen mat G of C
## by a permutation rho - call new mat Gp
## break Gp into (A,B), where A is kxs and B is kx(n-s)
## compute code C_A generated by rows of A
## find min weight codeword c_A of C_A and w_A=wt(c_A)
## using AClosestVectorCombinationsMatFFEVecFFECoords
## extend c_A to a corresponding codeword c_p in C_Gp
## return c=rho^(-1)(c_p) and wt=wt(c_p)=wt(c)
##
InstallMethod(MinimumDistanceRandom, "attribute method for linear codes", true,
[IsLinearCode,IsInt,IsInt], 0,
function(C,num,s)
local A,HasZeroRow,majority,G0, Gp, Gpt, Gt, L, k, n, i, j, m, dimMat, J, d1,M,
arrayd1, Combo, rows, row, rowSum, G, F, zero, AClosestVec, B, ZZ, p, numrow0,
bigwtrow,bigwtvec, x, d, v, ds, vecs,rho,perms,g,newv,pos,rowcombos,v1,v2;
# returns the estimated distance, and corresponding vector of that weight
Print("\n This is a probabilistic algorithm which may return the wrong answer.\n");
G0 := GeneratorMat(C);
p:=5; #this seems to be an optimal value
# it's the max number of rows used in Z to find a small
# codewd in C_Z
G := List(G0,ShallowCopy);
F:=LeftActingDomain(C);
dimMat := DimensionsMat(G);
n:=dimMat[2];
k:=dimMat[1];
if n=k then
C!.lowerBoundMinimumDistance := 1;
C!.upperBoundMinimumDistance := 1;
return 1;
fi; # added 11-2004
if s > n-1 then
Print("Resetting s to ",n-2," ... \n");
s:=n-k;
fi;
arrayd1:=[n];
numrow0:=0; # initialize
perms:=[]; # initialize
for m in [1..num] do
##Permute the columns of C randomly
Gt := TransposedMat(G);
Gp := NullMat(n,k);
L := SymmetricGroup(n);
rho := Random(L);
L:=List([1..n],i->OnPoints(i,rho));
for i in [1..n] do
Gp[i] := Gt[L[i]];
od;
Gp := TransposedMat(Gp);
Gp := List(Gp,ShallowCopy);
##generate the matrix A from Gp=(A|B)
Gpt := TransposedMat(Gp);
A := NullMat(s,k);
for i in [1..s] do
A[i] := Gpt[i];
od;
A := TransposedMat(A);
##generate the matrix B from Gp=(A|B)
Gpt := TransposedMat(Gp);
B := NullMat(n-s,k);
for i in [s+1..n] do
B[i-s] := Gpt[i];
od;
B := TransposedMat(B);
zero := Zero(F)*A[1];
if (s<n-k and A=Zero(F)*A) then
Error("This method fails for these parameters. Try increasing s.\n");
fi;
if (s=n and A=Zero(F)*A) then
return 1;
fi;
## search for all rows of weight p
## J is the list of all triples representing the codeword in C which
## corresponds to AClosestVec[1].
J := []; #col number of codewords to compute the length of
for i in [1..p] do
AClosestVec:=AClosestVectorCombinationsMatFFEVecFFECoords(A, F, zero, i, 1);
### AClosestVec[1]=ZZ*AClosestVec[2]...
v1:=AClosestVec[2]*Gp;
v2:=Permuted(v1,(rho)^(-1));
Add(J,[WeightVecFFE(v2),Codeword(v2,n,F),AClosestVec[2]]);
od;
ds:=List(J,x->x[1]);
vecs:=List(J,x->x[2]);
rowcombos:=List(J,x->x[3]);
d:=Minimum(ds);
i:=Position(ds,d);
arrayd1[m]:=[d,vecs[i],rowcombos[i]];
perms[m]:=rho;
od; ## m
ds:=List(arrayd1,x->x[1]);
vecs:=List(arrayd1,x->x[2]);
rowcombos:=List(arrayd1,x->x[3]);
d:=MostCommonInList(ds);
pos:=Position(ds,d);
v:=vecs[pos];
L:=List([1..n],i->OnPoints(i,perms[pos]^(-1)));
newv:=Codeword(List(L,i->v[L[i]]));
return([d,newv]);
end);
############################################################################
#F MinimumWeight( <C> )
##
## This function calls an external C program to compute the minimum Hamming
## weight of a linear code over GF(2) and GF(3). The external program is
## "minimum-weight"
##
## Author: CJ, Tjhai
##
InstallMethod(MinimumWeight, "attribute method for linear codes", true,
[IsLinearCode], 0,
function(C)
## Since this function calls an external program, we need to write the code
## into a generator matrix format recognised by this external program
local r, c, q, k, n, G, path, param, tmpFile, tmpOutFile;
path := DirectoriesPackagePrograms( "guava" );
if ForAny( ["minimum-weight"], f->Filename( path, f ) = fail ) then
Print("minimum-weight is not loaded ... switching to MinimumDistance ...\n");
return MinimumDistance(C);
fi;
if Size(LeftActingDomain(C)) > 3 then
Print("Code must either be binary or ternary. Quitting. \n"); return(0);
fi;
G := GeneratorMat(C);; G := ShallowCopy(G); TriangulizeMat(G);
k := DimensionsMat(G)[1];
n := DimensionsMat(G)[2];
path := DirectoriesPackagePrograms( "guava" );;
tmpFile := TmpName(); tmpOutFile := TmpName();
PrintTo(tmpFile, k, " ", n, " ", Size(LeftActingDomain(C)), "\n");
for r in [1..k] do;
for c in [1..n] do;
AppendTo(tmpFile, IntFFE(G[r][c]), " ");
od;
AppendTo(tmpFile, "\n");
od;
# Build the parameters required for the external program
param := "";
param := Concatenation(param, " --out ", tmpOutFile);
if IsCyclicCode(C) then
param := Concatenation(param, " --cyclic");
fi;
if IsBound(C!.lowerBoundMinimumDistance) then
param := Concatenation(param, " --lower-bound ", String(C!.lowerBoundMinimumDistance));
fi;
if LeftActingDomain(C) = GF(2) then
if IsSelfOrthogonalCode(C) then
param := Concatenation(param, " --mod 4");
elif IsEvenCode(C) then
param := Concatenation(param, " --mod 1");
fi;
elif LeftActingDomain(C) = GF(3) then
if IsSelfOrthogonalCode(C) then
param := Concatenation(param, " --mod 5");
fi;
fi;
## Now call the external program
Exec(Filename(path, "minimum-weight"), Concatenation(" ", param, " ", tmpFile));
Read(tmpOutFile);
RemoveFiles(tmpFile, tmpOutFile);
C!.lowerBoundMinimumDistance := GUAVA_TEMP_VAR;
C!.upperBoundMinimumDistance := GUAVA_TEMP_VAR;
return GUAVA_TEMP_VAR;
end);
|