Ticket #9055: trac_9055-rebased.patch
File trac_9055-rebased.patch, 36.8 KB (added by , 11 years ago) |
---|
-
doc/en/constructions/linear_codes.rst
# HG changeset patch # User Charlie Turner <charlotteturner@gmail.com> # Date 1274885896 -3600 # Node ID 3f5073b62286b9690484b5e30efbae4a36aa065c # Parent 190a1ae4b5835c94dd114984fb429bf866c233b6 Trac 9055: moving and cleaning enumeration functions for points on schemes * * * trac 9055: coding thry fixes to docstrings - wdj diff -r 190a1ae4b583 -r 3f5073b62286 doc/en/constructions/linear_codes.rst
a b 28 28 sage: C.minimum_distance() 29 29 3 30 30 sage: C.gen_mat() 31 [1 0 0 0 0 0 0 0 2 0 0 2 1]32 [0 1 0 0 0 0 0 0 2 0 0 2 0]33 [0 0 1 0 0 0 0 0 2 0 0 22]34 [0 0 0 1 0 0 0 0 2 0 0 1 0]35 [0 0 0 0 1 0 0 0 2 0 01 2]36 [0 0 0 0 0 1 0 0 2 0 0 1 1]37 [0 0 0 0 0 0 1 0 2 0 0 0 2]38 [0 0 0 0 0 0 0 1 2 0 0 01]39 [0 0 0 0 0 0 0 0 0 1 0 2 2]40 [0 0 0 0 0 0 0 0 0 0 1 21]31 [1 0 0 0 0 0 0 0 0 0 1 2 0] 32 [0 1 0 0 0 0 0 0 0 0 0 1 2] 33 [0 0 1 0 0 0 0 0 0 0 1 0 2] 34 [0 0 0 1 0 0 0 0 0 0 1 1 1] 35 [0 0 0 0 1 0 0 0 0 0 1 1 2] 36 [0 0 0 0 0 1 0 0 0 0 2 0 2] 37 [0 0 0 0 0 0 1 0 0 0 1 2 1] 38 [0 0 0 0 0 0 0 1 0 0 2 1 1] 39 [0 0 0 0 0 0 0 0 1 0 2 2 0] 40 [0 0 0 0 0 0 0 0 0 1 0 1 1] 41 41 42 42 .. index:: 43 43 pair: codes; Golay … … 80 80 Linear code of length 7, dimension 4 over Finite Field of size 2 81 81 Linear code of length 7, dimension 3 over Finite Field of size 2 82 82 sage: C.gen_mat() 83 [1 0 0 1 0 1 0]84 [0 1 0 1 0 11]85 [0 0 1 1 0 0 1]86 [0 0 0 01 1 1]83 [1 0 0 0 0 1 1] 84 [0 1 0 0 1 0 1] 85 [0 0 1 0 1 1 0] 86 [0 0 0 1 1 1 1] 87 87 sage: C.check_mat() 88 [1 0 0 11 0 1]89 [0 1 0 10 1 1]90 [0 0 1 1 1 1 0]88 [1 0 1 0 1 0 1] 89 [0 1 1 0 0 1 1] 90 [0 0 0 1 1 1 1] 91 91 sage: C.dual_code() 92 92 Linear code of length 7, dimension 3 over Finite Field of size 2 93 93 sage: C = HammingCode(3,GF(4,'a')) … … 107 107 sage: F = GF(2); a = F.gen() 108 108 sage: v1 = [a,a,F(0),a,a,F(0),a] 109 109 sage: C.decode(v1) 110 (1, 0, 0, 1, 1, 0, 1)110 (1, 1, 0, 1, 0, 0, 1) 111 111 sage: v2 = matrix([[a,a,F(0),a,a,F(0),a]]) 112 112 sage: C.decode(v2) 113 (1, 0, 0, 1, 1, 0, 1)113 (1, 1, 0, 1, 0, 0, 1) 114 114 sage: v3 = vector([a,a,F(0),a,a,F(0),a]) 115 115 sage: c = C.decode(v3); c 116 (1, 0, 0, 1, 1, 0, 1)116 (1, 1, 0, 1, 0, 0, 1) 117 117 118 118 To plot the (histogram of) the weight distribution of a code, one 119 119 can use the matplotlib package included with Sage: -
doc/en/reference/schemes.rst
diff -r 190a1ae4b583 -r 3f5073b62286 doc/en/reference/schemes.rst
a b 27 27 sage/schemes/generic/morphism 28 28 sage/schemes/generic/divisor 29 29 30 sage/schemes/generic/rational_point -
sage/coding/code_constructions.py
diff -r 190a1ae4b583 -r 3f5073b62286 sage/coding/code_constructions.py
a b 997 997 998 998 sage: C = HammingCode(3,GF(2)) 999 999 sage: H = C.check_mat(); H 1000 [1 0 0 11 0 1]1001 [0 1 0 10 1 1]1002 [0 0 1 1 1 1 0]1000 [1 0 1 0 1 0 1] 1001 [0 1 1 0 0 1 1] 1002 [0 0 0 1 1 1 1] 1003 1003 sage: LinearCodeFromCheckMatrix(H) == C 1004 1004 True 1005 1005 sage: C = HammingCode(2,GF(3)) 1006 1006 sage: H = C.check_mat(); H 1007 [1 0 2 2]1008 [0 1 2 1]1007 [1 0 1 1] 1008 [0 1 1 2] 1009 1009 sage: LinearCodeFromCheckMatrix(H) == C 1010 1010 True 1011 1011 sage: C = RandomLinearCode(10,5,GF(4,"a")) -
sage/coding/decoder.py
diff -r 190a1ae4b583 -r 3f5073b62286 sage/coding/decoder.py
a b 37 37 sage: v = V([0, 2, 0, 1]) 38 38 sage: from sage.coding.decoder import syndrome 39 39 sage: syndrome(C, v) 40 [(2, 0, 0, 0), 41 (0, 2, 0, 1), 42 (0, 0, 2, 2), 43 (0, 1, 1, 0), 44 (1, 2, 2, 0), 45 (1, 0, 1, 1), 46 (1, 1, 0, 2), 47 (2, 2, 1, 2), 48 (2, 1, 2, 1)] 40 [(0, 0, 1, 0), (0, 2, 0, 1), (2, 0, 0, 2), (1, 1, 0, 0), (2, 2, 2, 0), (1, 0, 2, 1), (0, 1, 2, 2), (1, 2, 1, 2), (2, 1, 1, 1)] 49 41 50 42 """ 51 43 V = C.ambient_space() … … 69 61 sage: v = V([0, 2, 0, 1]) 70 62 sage: from sage.coding.decoder import coset_leader 71 63 sage: coset_leader(C, v) 72 (( 2, 0, 0, 0), 1)64 ((0, 0, 1, 0), 1) 73 65 sage: coset_leader(C, v)[0]-v in C 74 66 True 75 67 … … 105 97 False 106 98 sage: from sage.coding.decoder import decode 107 99 sage: c = decode(C, v);c 108 ( 1, 2, 0, 1)100 (0, 2, 2, 1) 109 101 sage: c in C 110 102 True 111 103 sage: c = decode(C, v, algorithm="nearest neighbor");c 112 ( 1, 2, 0, 1)104 (0, 2, 2, 1) 113 105 sage: C = HammingCode(3,GF(3)); C 114 106 Linear code of length 13, dimension 10 over Finite Field of size 3 115 107 sage: V = VectorSpace(GF(3), 13) -
sage/coding/linear_code.py
diff -r 190a1ae4b583 -r 3f5073b62286 sage/coding/linear_code.py
a b 1 # -*- coding: utf-8 -*- 1 2 r""" 2 3 Linear Codes 3 4 … … 253 254 sage: f = open(file_loc); print f.read() 254 255 LIBRARY code; 255 256 code=seq(2,4,7,seq( 256 1,0,0, 1,0,1,0,257 0,1,0, 1,0,1,1,258 0,0,1, 1,0,0,1,259 0,0,0, 0,1,1,1257 1,0,0,0,0,1,1, 258 0,1,0,0,1,0,1, 259 0,0,1,0,1,1,0, 260 0,0,0,1,1,1,1 260 261 )); 261 262 FINISH; 262 263 sage: f.close() … … 727 728 728 729 sage: C = HammingCode(3,GF(2)) 729 730 sage: G = C.automorphism_group_binary_code(); G 730 Permutation Group with generators [(3,4)(5,6), (3,5)(4,6), (2,3)(5,7), (1,2)(5,6)]731 Permutation Group with generators [(4,5)(6,7), (4,6)(5,7), (2,3)(6,7), (2,4)(3,5), (1,2)(5,6)] 731 732 sage: G.order() 732 733 168 733 734 """ … … 755 756 756 757 sage: C = HammingCode(3,GF(2)) 757 758 sage: [list(c) for c in C if hamming_weight(c) < 4] 758 [[0, 0, 0, 0, 0, 0, 0], [1, 0, 0, 1, 0, 1, 0], [1, 1, 0, 0, 0, 0, 1], [0, 0, 1, 1, 0, 0, 1], [0, 1, 1, 0, 0, 1, 0], [0, 0, 0, 0, 1, 1, 1], [0, 1, 0, 1, 1, 0, 0], [1, 0, 1, 0, 1, 0, 0]]759 [[0, 0, 0, 0, 0, 0, 0], [1, 0, 0, 0, 0, 1, 1], [0, 1, 0, 0, 1, 0, 1], [0, 0, 1, 0, 1, 1, 0], [1, 1, 1, 0, 0, 0, 0], [1, 0, 0, 1, 1, 0, 0], [0, 1, 0, 1, 0, 1, 0], [0, 0, 1, 1, 0, 0, 1]] 759 760 """ 760 761 n = self.length() 761 762 k = self.dimension() … … 1063 1064 Linear code of length 7, dimension 4 over Finite Field of size 2 1064 1065 Linear code of length 7, dimension 3 over Finite Field of size 2 1065 1066 sage: C.gen_mat() 1066 [1 0 0 1 0 1 0]1067 [0 1 0 1 0 11]1068 [0 0 1 1 0 0 1]1069 [0 0 0 01 1 1]1067 [1 0 0 0 0 1 1] 1068 [0 1 0 0 1 0 1] 1069 [0 0 1 0 1 1 0] 1070 [0 0 0 1 1 1 1] 1070 1071 sage: C.check_mat() 1071 [1 0 0 11 0 1]1072 [0 1 0 10 1 1]1073 [0 0 1 1 1 1 0]1072 [1 0 1 0 1 0 1] 1073 [0 1 1 0 0 1 1] 1074 [0 0 0 1 1 1 1] 1074 1075 sage: Cperp.check_mat() 1075 [1 0 0 1 0 1 0]1076 [0 1 0 1 0 11]1077 [0 0 1 1 0 0 1]1078 [0 0 0 01 1 1]1076 [1 0 0 0 0 1 1] 1077 [0 1 0 0 1 0 1] 1078 [0 0 1 0 1 1 0] 1079 [0 0 0 1 1 1 1] 1079 1080 sage: Cperp.gen_mat() 1080 [1 0 0 11 0 1]1081 [0 1 0 10 1 1]1082 [0 0 1 1 1 1 0]1081 [1 0 1 0 1 0 1] 1082 [0 1 1 0 0 1 1] 1083 [0 0 0 1 1 1 1] 1083 1084 """ 1084 1085 Cperp = self.dual_code() 1085 1086 return Cperp.gen_mat() … … 1142 1143 sage: F = GF(2); a = F.gen() 1143 1144 sage: v1 = [a,a,F(0),a,a,F(0),a] 1144 1145 sage: C.decode(v1) 1145 (1, 0, 0, 1, 1, 0, 1)1146 (1, 1, 0, 1, 0, 0, 1) 1146 1147 sage: C.decode(v1,algorithm="nearest neighbor") 1147 (1, 0, 0, 1, 1, 0, 1)1148 (1, 1, 0, 1, 0, 0, 1) 1148 1149 sage: C.decode(v1,algorithm="guava") # requires optional GAP package Guava 1149 (1, 0, 0, 1, 1, 0, 1)1150 (1, 1, 0, 1, 0, 0, 1) 1150 1151 sage: v2 = matrix([[a,a,F(0),a,a,F(0),a]]) 1151 1152 sage: C.decode(v2) 1152 (1, 0, 0, 1, 1, 0, 1)1153 (1, 1, 0, 1, 0, 0, 1) 1153 1154 sage: v3 = vector([a,a,F(0),a,a,F(0),a]) 1154 1155 sage: c = C.decode(v3); c 1155 (1, 0, 0, 1, 1, 0, 1)1156 (1, 1, 0, 1, 0, 0, 1) 1156 1157 sage: c in C 1157 1158 True 1158 1159 sage: C = HammingCode(2,GF(5)) 1159 1160 sage: v = vector(GF(5),[1,0,0,2,1,0]) 1160 1161 sage: C.decode(v) 1161 ( 2, 0, 0, 2, 1, 0)1162 (1, 0, 0, 2, 2, 0) 1162 1163 sage: F = GF(4,"a") 1163 1164 sage: C = HammingCode(2,F) 1164 1165 sage: v = vector(F, [1,0,0,a,1]) 1165 1166 sage: C.decode(v) 1166 (1, 0, 0, 1, 1)1167 (1, 0, 1, 1, 1) 1167 1168 sage: C.decode(v, algorithm="nearest neighbor") 1168 (1, 0, 0, 1, 1)1169 (1, 0, 1, 1, 1) 1169 1170 sage: C.decode(v, algorithm="guava") # requires optional GAP package Guava 1170 (1, 0, 0, 1, 1)1171 (1, 0, 1, 1, 1) 1171 1172 1172 1173 Does not work for very long codes since the syndrome table grows too 1173 1174 large. … … 1375 1376 sage: C; Cc 1376 1377 Linear code of length 21, dimension 18 over Finite Field in a of size 2^2 1377 1378 Linear code of length 21, dimension 20 over Finite Field in a of size 2^2 1378 sage: c = C.basis()[ 1]1379 sage: c = C.basis()[2] 1379 1380 sage: V = VectorSpace(GF(4,'a'),21) 1380 1381 sage: c2 = V([x^2 for x in c.list()]) 1381 1382 sage: c2 in C … … 1411 1412 1412 1413 sage: C1 = HammingCode(3,GF(2)) 1413 1414 sage: C1.gen_mat() 1414 [1 0 0 1 0 1 0]1415 [0 1 0 1 0 11]1416 [0 0 1 1 0 0 1]1417 [0 0 0 01 1 1]1415 [1 0 0 0 0 1 1] 1416 [0 1 0 0 1 0 1] 1417 [0 0 1 0 1 1 0] 1418 [0 0 0 1 1 1 1] 1418 1419 sage: C2 = HammingCode(2,GF(4,"a")) 1419 1420 sage: C2.gen_mat() 1420 [ 1 0 0 1 1]1421 [ 0 1 0 1 a +1]1422 [ 0 0 1 1 a]1421 [ 1 0 0 a + 1 a] 1422 [ 0 1 0 1 1] 1423 [ 0 0 1 a a + 1] 1423 1424 """ 1424 1425 return self.__gen_mat 1425 1426 … … 1451 1452 1452 1453 sage: C = HammingCode(3,GF(2)) 1453 1454 sage: C.gens() 1454 [(1, 0, 0, 1, 0, 1, 0), (0, 1, 0, 1, 0, 1, 1), (0, 0, 1, 1, 0, 0, 1), (0, 0, 0, 0, 1, 1, 1)]1455 [(1, 0, 0, 0, 0, 1, 1), (0, 1, 0, 0, 1, 0, 1), (0, 0, 1, 0, 1, 1, 0), (0, 0, 0, 1, 1, 1, 1)] 1455 1456 """ 1456 1457 return self.__gens 1457 1458 … … 1557 1558 sage: C1.is_permutation_equivalent(C2) 1558 1559 True 1559 1560 sage: C1.is_permutation_equivalent(C2,algorithm="verbose") 1560 (True, ( 4,6,5,7))1561 (True, (3,4)(5,7,6)) 1561 1562 sage: C1 = RandomLinearCode(10,5,GF(2)) 1562 1563 sage: C2 = RandomLinearCode(10,5,GF(3)) 1563 1564 sage: C1.is_permutation_equivalent(C2) … … 1695 1696 sage: C = HammingCode(3,GF(2)) 1696 1697 sage: Clist = C.list() 1697 1698 sage: Clist[5]; Clist[5] in C 1698 (1, 0, 1, 0, 0, 1, 1)1699 (1, 0, 1, 0, 1, 0, 1) 1699 1700 True 1700 1701 """ 1701 1702 return self.gen_mat().row_space().list() … … 1872 1873 sage: C = HammingCode(2,GF(3)); C 1873 1874 Linear code of length 4, dimension 2 over Finite Field of size 3 1874 1875 sage: C.permutation_automorphism_group(algorithm="partition") 1875 Permutation Group with generators [(1, 2,3)]1876 Permutation Group with generators [(1,3,4)] 1876 1877 sage: C = HammingCode(2,GF(4,"z")); C 1877 1878 Linear code of length 5, dimension 3 over Finite Field in z of size 2^2 1878 1879 sage: C.permutation_automorphism_group(algorithm="partition") 1879 Permutation Group with generators [(1, 2)(3,4), (1,3)(2,4)]1880 Permutation Group with generators [(1,3)(4,5), (1,4)(3,5)] 1880 1881 sage: C.permutation_automorphism_group(algorithm="gap") # requires optional GAP package Guava 1881 Permutation Group with generators [(1, 2)(3,4), (1,3)(2,4)]1882 Permutation Group with generators [(1,3)(4,5), (1,4)(3,5)] 1882 1883 sage: C = TernaryGolayCode() 1883 1884 sage: C.permutation_automorphism_group(algorithm="gap") # requires optional GAP package Guava 1884 1885 Permutation Group with generators [(3,4)(5,7)(6,9)(8,11), (3,5,8)(4,11,7)(6,9,10), (2,3)(4,6)(5,8)(7,10), (1,2)(4,11)(5,8)(9,10)] … … 1965 1966 1966 1967 sage: C = HammingCode(3,GF(2)) 1967 1968 sage: G = C.automorphism_group_binary_code(); G 1968 Permutation Group with generators [( 3,4)(5,6), (3,5)(4,6), (2,3)(5,7), (1,2)(5,6)]1969 sage: g = G("(2,3)( 5,7)")1969 Permutation Group with generators [(4,5)(6,7), (4,6)(5,7), (2,3)(6,7), (2,4)(3,5), (1,2)(5,6)] 1970 sage: g = G("(2,3)(6,7)") 1970 1971 sage: Cg = C.permuted_code(g) 1971 1972 sage: Cg 1972 1973 Linear code of length 7, dimension 4 over Finite Field of size 2 … … 2039 2040 EXAMPLES:: 2040 2041 2041 2042 sage: C = HammingCode(3,GF(4,'a')) 2042 sage: Cc = C.galois_closure(GF(2)) 2043 sage: c = C.gen_mat()[1] 2044 sage: V = VectorSpace(GF(4,'a'),21) 2045 sage: c2 = V([x^2 for x in c.list()]) 2046 sage: c2 in C 2047 False 2048 sage: c2 in Cc 2049 True 2050 2051 :: 2052 2053 sage: C.random_element() 2043 sage: C.random_element() # random test 2054 2044 (1, 0, 0, a + 1, 1, a, a, a + 1, a + 1, 1, 1, 0, a + 1, a, 0, a, a, 0, a, a, 1) 2055 2045 2056 2046 Passes extra positional or keyword arguments through:: 2057 2047 2058 sage: C.random_element(prob=.5, distribution='1/n') 2048 sage: C.random_element(prob=.5, distribution='1/n') # random test 2059 2049 (1, 0, a, 0, 0, 0, 0, a + 1, 0, 0, 0, 0, 0, 0, 0, 0, a + 1, a + 1, 1, 0, 0) 2060 2050 """ 2061 2051 V = self.ambient_space() … … 2078 2068 2079 2069 sage: C = HammingCode(3,GF(2)) 2080 2070 sage: C.gen_mat() 2081 [1 0 0 1 0 1 0]2082 [0 1 0 1 0 11]2083 [0 0 1 1 0 0 1]2084 [0 0 0 01 1 1]2071 [1 0 0 0 0 1 1] 2072 [0 1 0 0 1 0 1] 2073 [0 0 1 0 1 1 0] 2074 [0 0 0 1 1 1 1] 2085 2075 sage: C.redundancy_matrix() 2086 [1 1 0]2087 [1 11]2088 [1 0 1]2089 [0 1 1]2076 [0 1 1] 2077 [1 0 1] 2078 [1 1 0] 2079 [1 1 1] 2090 2080 sage: C.standard_form()[0].gen_mat() 2091 [1 0 0 0 1 1 0]2092 [0 1 0 0 1 11]2093 [0 0 1 0 1 0 1]2094 [0 0 0 1 01 1]2081 [1 0 0 0 0 1 1] 2082 [0 1 0 0 1 0 1] 2083 [0 0 1 0 1 1 0] 2084 [0 0 0 1 1 1 1] 2095 2085 sage: C = HammingCode(2,GF(3)) 2096 2086 sage: C.gen_mat() 2097 [1 0 2 2]2098 [0 1 2 1]2087 [1 0 1 1] 2088 [0 1 1 2] 2099 2089 sage: C.redundancy_matrix() 2100 [ 2 2]2101 [ 2 1]2090 [1 1] 2091 [1 2] 2102 2092 """ 2103 2093 n = C.length() 2104 2094 k = C.dimension() … … 2406 2396 2407 2397 sage: C = HammingCode(3,GF(2)) 2408 2398 sage: C.gen_mat() 2409 [1 0 0 1 0 1 0]2410 [0 1 0 1 0 11]2411 [0 0 1 1 0 0 1]2412 [0 0 0 01 1 1]2399 [1 0 0 0 0 1 1] 2400 [0 1 0 0 1 0 1] 2401 [0 0 1 0 1 1 0] 2402 [0 0 0 1 1 1 1] 2413 2403 sage: Cs,p = C.standard_form() 2414 2404 sage: p 2415 (4,5) 2416 sage: Cs 2417 Linear code of length 7, dimension 4 over Finite Field of size 2 2418 sage: Cs.gen_mat() 2419 [1 0 0 0 1 1 0] 2420 [0 1 0 0 1 1 1] 2421 [0 0 1 0 1 0 1] 2422 [0 0 0 1 0 1 1] 2405 () 2423 2406 sage: MS = MatrixSpace(GF(3),3,7) 2424 2407 sage: G = MS([[1,0,0,0,1,1,0],[0,1,0,1,0,1,0],[0,0,0,0,0,0,1]]) 2425 2408 sage: C = LinearCode(G) 2426 sage: G; C.standard_form()[0].gen_mat() 2427 [1 0 0 0 1 1 0] 2428 [0 1 0 1 0 1 0] 2429 [0 0 0 0 0 0 1] 2430 [1 0 0 0 1 1 0] 2431 [0 1 0 1 0 1 0] 2432 [0 0 1 0 0 0 0] 2433 sage: C.standard_form()[1] 2409 sage: Cs, p = C.standard_form() 2410 sage: p 2434 2411 (3,7) 2412 sage: Cs.gen_mat() 2413 [1 0 0 0 1 1 0] 2414 [0 1 0 1 0 1 0] 2415 [0 0 1 0 0 0 0] 2435 2416 """ 2436 2417 from sage.coding.code_constructions import permutation_action as perm_action 2437 2418 mat = self.gen_mat() -
sage/schemes/generic/algebraic_scheme.py
diff -r 190a1ae4b583 -r 3f5073b62286 sage/schemes/generic/algebraic_scheme.py
a b 935 935 936 936 sage: E = EllipticCurve('37a') 937 937 sage: E.rational_points(bound=8) 938 [(0 : 0 : 1), 939 (1 : 0 : 1), 940 (-1 : 0 : 1), 941 (0 : -1 : 1), 942 (1 : -1 : 1), 943 (-1 : -1 : 1), 944 (2 : 2 : 1), 945 (2 : -3 : 1), 946 (1/4 : -3/8 : 1), 947 (1/4 : -5/8 : 1), 948 (0 : 1 : 0)] 938 [(-1 : -1 : 1), (-1 : 0 : 1), (0 : -1 : 1), (0 : 0 : 1), (0 : 1 : 0), (1/4 : -5/8 : 1), (1/4 : -3/8 : 1), (1 : -1 : 1), (1 : 0 : 1), (2 : -3 : 1), (2 : 2 : 1)] 949 939 950 940 For a small finite field, the complete set of points can be 951 941 enumerated. … … 965 955 sage: P.<x> = PolynomialRing(FiniteField(7)) 966 956 sage: C = HyperellipticCurve(x^8+x+1) 967 957 sage: C.rational_points() 968 [( 2 : 0 : 1), (4 : 0 : 1), (0 : 1 : 1), (6 : 1 : 1), (0 : 6 : 1), (6 : 6 : 1), (0 : 1 : 0)]969 958 [(0 : 1 : 0), (0 : 1 : 1), (0 : 6 : 1), (2 : 0 : 1), (4 : 0 : 1), (6 : 1 : 1), (6 : 6 : 1)] 959 970 960 TODO: 971 961 972 962 1. The above algorithms enumerate all projective points and -
sage/schemes/generic/homset.py
diff -r 190a1ae4b583 -r 3f5073b62286 sage/schemes/generic/homset.py
a b 6 6 from sage.rings.arith import gcd 7 7 Z = sage.rings.integer_ring.ZZ 8 8 9 # Some naive point enumeration routines for default.10 # AUTHOR: David R. Kohel <kohel@maths.usyd.edu.au>11 12 def enum_projective_rational_field(X,B):13 n = X.codomain().ambient_space().ngens()14 Q = [ k+1 for k in range(B) ]15 R = [ 0 ] + [ s*k for k in Q for s in [1,-1] ]16 pts = []17 i = int(n-1)18 while not i < 0:19 P = [ 0 for _ in range(n) ]; P[i] = 120 try:21 pts.append(X(P))22 except:23 pass24 iters = [ iter(R) for _ in range(i) ]25 [ iters[j].next() for j in range(i) ]26 j = 027 while j < i:28 try:29 aj = ZZ(iters[j].next())30 P[j] = aj31 for ai in Q:32 P[i] = ai33 if gcd(P) == 1:34 try:35 pts.append(X(P))36 except:37 pass38 j = 039 except StopIteration:40 iters[j] = iter(R) # reset41 P[j] = 042 P[j] = iters[j].next() # reset P[j] to 0 and increment43 j += 144 i -= 145 return pts46 47 def enum_affine_rational_field(X,B):48 n = X.codomain().ambient_space().ngens()49 if X.value_ring() is ZZ:50 Q = [ 1 ]51 else: # rational field52 Q = [ k+1 for k in range(B) ]53 R = [ 0 ] + [ s*k for k in range(1,B+1) for s in [1,-1] ]54 pts = []55 P = [ 0 for _ in range(n) ]56 m = ZZ(0)57 try:58 pts.append(X(P))59 except:60 pass61 iters = [ iter(R) for _ in range(n) ]62 [ iters[j].next() for j in range(n) ]63 i = 064 while i < n:65 try:66 a = ZZ(iters[i].next())67 m = m.gcd(a)68 P[i] = a69 for b in Q:70 if m.gcd(b) == 1:71 try:72 pts.append(X([ num/b for num in P ]))73 except:74 pass75 i = 076 m = ZZ(0)77 except StopIteration:78 iters[i] = iter(R) # reset79 P[i] = iters[i].next() # reset P[i] to 0 and increment80 i += 181 return pts82 83 def enum_projective_finite_field(X):84 n = X.codomain().ambient_space().ngens()85 R = X.value_ring()86 pts = []87 i = int(n-1)88 while not i < 0:89 P = [ 0 for _ in range(n) ]; P[i] = 190 try:91 pts.append(X(P))92 except:93 pass94 # define some iterators and increment them:95 iters = [ iter(R) for _ in range(i) ]96 [ iters[j].next() for j in range(i) ]97 j = 098 while j < i:99 try:100 P[j] = iters[j].next()101 try:102 pts.append(X(P))103 except:104 pass105 j = 0106 except StopIteration:107 iters[j] = iter(R) # reset iterator at j108 P[j] = iters[j].next() # reset P[j] to 0 and increment109 j += 1110 i -= 1111 return pts112 113 def enum_affine_finite_field(X):114 n = X.codomain().ambient_space().ngens()115 R = X.value_ring()116 pts = []117 zero = R(0)118 P = [ zero for _ in range(n) ]119 pts.append(X(P))120 iters = [ iter(R) for _ in range(n) ]121 for x in iters: x.next() # put at zero122 i = 0123 while i < n:124 try:125 P[i] = iters[i].next()126 try:127 pts.append(X(P))128 except:129 pass130 i = 0131 except StopIteration:132 iters[i] = iter(R) # reset133 iters[i].next() # put at zero134 P[i] = zero135 i += 1136 return pts137 138 9 #***************************************************************************** 139 10 # Copyright (C) 2006 William Stein <wstein@gmail.com> 140 11 # Distributed under the terms of the GNU General Public License (GPL) … … 290 161 if is_RationalField(R) or R == Z: 291 162 if not B > 0: 292 163 raise TypeError, "A positive bound B (= %s) must be specified."%B 164 from sage.schemes.generic.rational_point import enum_affine_rational_field 293 165 return enum_affine_rational_field(self,B) 294 166 elif is_FiniteField(R): 167 from sage.schemes.generic.rational_point import enum_affine_finite_field 295 168 return enum_affine_finite_field(self) 296 169 else: 297 170 raise TypeError, "Unable to enumerate points over %s."%R … … 311 184 return morphism.SchemeMorphism_projective_coordinates_field(self, v) 312 185 313 186 def points(self, B=0): 187 from sage.schemes.generic.rational_point import enum_projective_rational_field 188 from sage.schemes.generic.rational_point import enum_projective_finite_field 314 189 try: 315 190 R = self.value_ring() 316 191 except TypeError: … … 341 216 if R == Z: 342 217 if not B > 0: 343 218 raise TypeError, "A positive bound B (= %s) must be specified."%B 219 from sage.schemes.generic.rational_points import enum_projective_rational_field 344 220 return enum_projective_rational_field(self,B) 345 221 else: 346 222 raise TypeError, "Unable to enumerate points over %s."%R -
new file sage/schemes/generic/rational_point.py
diff -r 190a1ae4b583 -r 3f5073b62286 sage/schemes/generic/rational_point.py
- + 1 r""" 2 Enumeration of rational points on affine and projective schemes 3 4 Naive algorithms for enumerating rational points over `\QQ` or finite fields over for general schemes. Warning: Incorrect results and infinite loops may occur if using the wrong function.(For instance using an affine function for a projective scheme or finite field function for a scheme defined over an infinite field). 5 6 EXAMPLES: 7 8 Projective, over `\QQ`:: 9 10 sage: from sage.schemes.generic.rational_point import enum_projective_rational_field 11 sage: P.<X,Y,Z> = ProjectiveSpace(2,QQ) 12 sage: C = P.subscheme([X+Y-Z]) 13 sage: enum_projective_rational_field(C,3) 14 [(-2 : 3 : 1), (-1 : 1 : 0), (-1 : 2 : 1), (-1/2 : 3/2 : 1), (0 : 1 : 1), 15 (1/3 : 2/3 : 1), (1/2 : 1/2 : 1), (2/3 : 1/3 : 1), (1 : 0 : 1), 16 (3/2 : -1/2 : 1), (2 : -1 : 1), (3 : -2 : 1)] 17 18 Affine, over `\QQ`:: 19 20 sage: from sage.schemes.generic.rational_point import enum_affine_rational_field 21 sage: A.<x,y,z> = AffineSpace(3,QQ) 22 sage: S = A.subscheme([2*x-3*y]) 23 sage: enum_affine_rational_field(S,2) 24 [(0, 0, -2), (0, 0, -1), (0, 0, -1/2), (0, 0, 0), (0, 0, 1/2), (0, 0, 1), 25 (0, 0, 2)] 26 27 Projective over a finite field:: 28 29 sage: from sage.schemes.generic.rational_point import enum_projective_finite_field 30 sage: E = EllipticCurve('72').change_ring(GF(19)) 31 sage: enum_projective_finite_field(E) 32 [(0 : 1 : 0), (1 : 0 : 1), (3 : 0 : 1), (4 : 9 : 1), (4 : 10 : 1), (6 : 6 : 1), 33 (6 : 13 : 1), (7 : 6 : 1), (7 : 13 : 1), (9 : 4 : 1), (9 : 15 : 1), (12 : 8 : 1), 34 (12 : 11 : 1), (13 : 8 : 1), (13 : 11 : 1), (14 : 3 : 1), (14 : 16 : 1), 35 (15 : 0 : 1), (16 : 9 : 1), (16 : 10 : 1), (17 : 7 : 1), (17 : 12 : 1), (18 : 9 : 1), 36 (18 : 10 : 1)] 37 38 Affine over a finite field:: 39 40 sage: from sage.schemes.generic.rational_point import enum_affine_finite_field 41 sage: A.<w,x,y,z> = AffineSpace(4,GF(2)) 42 sage: enum_affine_finite_field(A(GF(2))) 43 [(0, 0, 0, 0), (0, 0, 0, 1), (0, 0, 1, 0), (0, 0, 1, 1), (0, 1, 0, 0), (0, 1, 0, 1), 44 (0, 1, 1, 0), (0, 1, 1, 1), (1, 0, 0, 0), (1, 0, 0, 1), (1, 0, 1, 0), (1, 0, 1, 1), 45 (1, 1, 0, 0), (1, 1, 0, 1), (1, 1, 1, 0), (1, 1, 1, 1)] 46 47 AUTHORS: 48 49 Originally by David R. Kohel <kohel@maths.usyd.edu.au> 50 51 Improvements to clarity and documentation made by John Cremona and Charlie Turner <charlotteturner@gmail.com> (06-2010) 52 """ 53 #******************************************************************************* 54 # Copyright (C) 2010 William Stein, David Kohel, John Cremona, Charlie Turner 55 # Distributed under the terms of the GNU General Public License (GPL) 56 # http://www.gnu.org/licenses/ 57 #******************************************************************************* 58 from sage.rings.arith import gcd 59 from sage.rings.all import QQ, ZZ 60 from sage.misc.all import srange, cartesian_product_iterator 61 from sage.schemes.all import is_Scheme 62 63 def enum_projective_rational_field(X,B): 64 """ 65 Enumerates projective, rational points on scheme X of height up to bound B. 66 67 INPUT: 68 69 - ``X`` - a scheme or set of abstract rational points of a scheme. 70 - ``B`` - a positive integer bound 71 72 OUTPUT: 73 74 - a list containing the projective points of X of height up to B, sorted. 75 76 EXAMPLES:: 77 78 sage: P.<X,Y,Z> = ProjectiveSpace(2,QQ) 79 sage: C = P.subscheme([X+Y-Z]) 80 sage: from sage.schemes.generic.rational_point import enum_projective_rational_field 81 sage: enum_projective_rational_field(C(QQ),6) 82 [(-5 : 6 : 1), (-4 : 5 : 1), (-3 : 4 : 1), (-2 : 3 : 1), (-3/2 : 5/2 : 1), 83 (-1 : 1 : 0), (-1 : 2 : 1), (-2/3 : 5/3 : 1), (-1/2 : 3/2 : 1), (-1/3 : 4/3 : 1), 84 (-1/4 : 5/4 : 1), (-1/5 : 6/5 : 1), (0 : 1 : 1), (1/6 : 5/6 : 1), (1/5 : 4/5 : 1), 85 (1/4 : 3/4 : 1), (1/3 : 2/3 : 1), (2/5 : 3/5 : 1), (1/2 : 1/2 : 1), (3/5 : 2/5 : 1), 86 (2/3 : 1/3 : 1), (3/4 : 1/4 : 1), (4/5 : 1/5 : 1), (5/6 : 1/6 : 1), (1 : 0 : 1), 87 (6/5 : -1/5 : 1), (5/4 : -1/4 : 1), (4/3 : -1/3 : 1), (3/2 : -1/2 : 1), 88 (5/3 : -2/3 : 1), (2 : -1 : 1), (5/2 : -3/2 : 1), (3 : -2 : 1), (4 : -3 : 1), 89 (5 : -4 : 1), (6 : -5 : 1)] 90 sage: enum_projective_rational_field(C,6) == enum_projective_rational_field(C(QQ),6) 91 True 92 93 :: 94 95 sage: P3.<W,X,Y,Z> = ProjectiveSpace(3,QQ) 96 sage: enum_projective_rational_field(P3,1) 97 [(-1 : -1 : -1 : 1), (-1 : -1 : 0 : 1), (-1 : -1 : 1 : 0), (-1 : -1 : 1 : 1), 98 (-1 : 0 : -1 : 1), (-1 : 0 : 0 : 1), (-1 : 0 : 1 : 0), (-1 : 0 : 1 : 1), 99 (-1 : 1 : -1 : 1), (-1 : 1 : 0 : 0), (-1 : 1 : 0 : 1), (-1 : 1 : 1 : 0), 100 (-1 : 1 : 1 : 1), (0 : -1 : -1 : 1), (0 : -1 : 0 : 1), (0 : -1 : 1 : 0), 101 (0 : -1 : 1 : 1), (0 : 0 : -1 : 1), (0 : 0 : 0 : 1), (0 : 0 : 1 : 0), 102 (0 : 0 : 1 : 1), (0 : 1 : -1 : 1), (0 : 1 : 0 : 0), (0 : 1 : 0 : 1), 103 (0 : 1 : 1 : 0), (0 : 1 : 1 : 1), (1 : -1 : -1 : 1), (1 : -1 : 0 : 1), 104 (1 : -1 : 1 : 0), (1 : -1 : 1 : 1), (1 : 0 : -1 : 1), (1 : 0 : 0 : 0), 105 (1 : 0 : 0 : 1), (1 : 0 : 1 : 0), (1 : 0 : 1 : 1), (1 : 1 : -1 : 1), 106 (1 : 1 : 0 : 0), (1 : 1 : 0 : 1), (1 : 1 : 1 : 0), (1 : 1 : 1 : 1)] 107 108 ALGORITHM: 109 110 We just check all possible projective points in correct dimension 111 of projective space to see if they lie on X. 112 113 AUTHORS: 114 115 John Cremona and Charlie Turner (06-2010) 116 """ 117 if is_Scheme(X): 118 X = X(X.base_ring()) 119 n = X.codomain().ambient_space().ngens()-1 120 zero=tuple([0 for _ in range(n+1)]) 121 # pts = [X(c) for c in cartesian_product_iterator([srange(-B,B+1) for _ in range(n+1)]) if gcd(c)==1 and c>zero] 122 pts =[] 123 for c in cartesian_product_iterator([srange(-B,B+1) for _ in range(n+1)]): 124 if gcd(c)==1 and c>zero: 125 try: 126 pts.append(X(c)) 127 except: 128 pass 129 pts.sort() 130 return pts 131 132 def enum_affine_rational_field(X,B): 133 """ 134 Enumerates affine rational points on scheme X (defined over `\QQ`) up to bound B. 135 136 INPUT: 137 138 - ``X`` - a scheme or set of abstract rational points of a scheme 139 - ``B`` - a positive integer bound 140 141 OUTPUT: 142 143 - a list containing the affine points of X of height up to B, sorted. 144 145 EXAMPLES:: 146 147 sage: A.<x,y,z> = AffineSpace(3,QQ) 148 sage: from sage.schemes.generic.rational_point import enum_affine_rational_field 149 sage: enum_affine_rational_field(A(QQ),1) 150 [(-1, -1, -1), (-1, -1, 0), (-1, -1, 1), (-1, 0, -1), (-1, 0, 0), (-1, 0, 1), 151 (-1, 1, -1), (-1, 1, 0), (-1, 1, 1), (0, -1, -1), (0, -1, 0), (0, -1, 1), 152 (0, 0, -1), (0, 0, 0), (0, 0, 1), (0, 1, -1), (0, 1, 0), (0, 1, 1), (1, -1, -1), 153 (1, -1, 0), (1, -1, 1), (1, 0, -1), (1, 0, 0), (1, 0, 1), (1, 1, -1), (1, 1, 0), 154 (1, 1, 1)] 155 156 157 :: 158 159 sage: A.<w,x,y,z> = AffineSpace(4,QQ) 160 sage: S = A.subscheme([x^2-y*z+3,w^3+z+y^2]) 161 sage: enum_affine_rational_field(S(QQ),2) 162 [] 163 sage: enum_affine_rational_field(S(QQ),3) 164 [(-2, 0, -3, -1)] 165 166 :: 167 168 sage: A.<x,y> = AffineSpace(2,QQ) 169 sage: C = Curve(x^2+y-x) 170 sage: enum_affine_rational_field(C,10) 171 [(-2, -6), (-1, -2), (0, 0), (1, 0), (2, -2), (3, -6)] 172 173 174 AUTHOR: 175 176 David R. Kohel <kohel@maths.usyd.edu.au> 177 (small adjustments by Charlie Turner 06-2010) 178 """ 179 if is_Scheme(X): 180 X = X(X.base_ring()) 181 n = X.codomain().ambient_space().ngens() 182 if X.value_ring() is ZZ: 183 Q = [ 1 ] 184 else: # rational field 185 Q = [ k+1 for k in range(B) ] 186 R = [ 0 ] + [ s*k for k in range(1,B+1) for s in [1,-1] ] 187 pts = [] 188 P = [ 0 for _ in range(n) ] 189 m = ZZ(0) 190 try: 191 pts.append(X(P)) 192 except: 193 pass 194 iters = [ iter(R) for _ in range(n) ] 195 [ iters[j].next() for j in range(n) ] 196 i = 0 197 while i < n: 198 try: 199 a = ZZ(iters[i].next()) 200 m = m.gcd(a) 201 P[i] = a 202 for b in Q: 203 if m.gcd(b) == 1: 204 try: 205 pts.append(X([ num/b for num in P ])) 206 except: 207 pass 208 i = 0 209 m = ZZ(0) 210 except StopIteration: 211 iters[i] = iter(R) # reset 212 P[i] = iters[i].next() # reset P[i] to 0 and increment 213 i += 1 214 pts.sort() 215 return pts 216 217 def enum_projective_finite_field(X): 218 """ 219 Enumerates projective points on scheme X defined over a finite field 220 221 INPUT: 222 223 - ``X`` - a scheme defined over a finite field or set of abstract rational points of such a scheme 224 225 OUTPUT: 226 227 - a list containing the projective points of X over the finite field, sorted 228 229 EXAMPLES:: 230 231 sage: F = GF(53) 232 sage: P.<X,Y,Z> = ProjectiveSpace(2,F) 233 sage: from sage.schemes.generic.rational_point import enum_projective_finite_field 234 sage: len(enum_projective_finite_field(P(F))) 235 2863 236 sage: 53^2+53+1 237 2863 238 239 :: 240 241 sage: F = GF(9,'a') 242 sage: P.<X,Y,Z> = ProjectiveSpace(2,F) 243 sage: C = Curve(X^3-Y^3+Z^2*Y) 244 sage: enum_projective_finite_field(C(F)) 245 [(0 : 0 : 1), (0 : 1 : 1), (0 : 2 : 1), (1 : 1 : 0), (a + 1 : 2*a : 1), 246 (a + 1 : 2*a + 1 : 1), (a + 1 : 2*a + 2 : 1), (2*a + 2 : a : 1), 247 (2*a + 2 : a + 1 : 1), (2*a + 2 : a + 2 : 1)] 248 249 :: 250 251 sage: F = GF(5) 252 sage: P2F.<X,Y,Z> = ProjectiveSpace(2,F) 253 sage: enum_projective_finite_field(P2F) 254 [(0 : 0 : 1), (0 : 1 : 0), (0 : 1 : 1), (0 : 2 : 1), (0 : 3 : 1), (0 : 4 : 1), 255 (1 : 0 : 0), (1 : 0 : 1), (1 : 1 : 0), (1 : 1 : 1), (1 : 2 : 1), (1 : 3 : 1), 256 (1 : 4 : 1), (2 : 0 : 1), (2 : 1 : 0), (2 : 1 : 1), (2 : 2 : 1), (2 : 3 : 1), 257 (2 : 4 : 1), (3 : 0 : 1), (3 : 1 : 0), (3 : 1 : 1), (3 : 2 : 1), (3 : 3 : 1), 258 (3 : 4 : 1), (4 : 0 : 1), (4 : 1 : 0), (4 : 1 : 1), (4 : 2 : 1), (4 : 3 : 1), 259 (4 : 4 : 1)] 260 261 ALGORITHM: 262 263 Checks all points in projective space to see if they lie on X. 264 265 NOTE: 266 267 Warning:if X given as input is defined over an infinite field then this code will not finish! 268 269 AUTHORS: John Cremona and Charlie Turner (06-2010) 270 """ 271 if is_Scheme(X): 272 X = X(X.base_ring()) 273 n = X.codomain().ambient_space().ngens()-1 274 F = X.value_ring() 275 pts = [] 276 for k in range(n+1): 277 for c in cartesian_product_iterator([F for _ in range(k)]): 278 try: 279 pts.append(X(list(c)+[1]+[0]*(n-k))) 280 except: 281 pass 282 pts.sort() 283 return pts 284 285 def enum_affine_finite_field(X): 286 """ 287 Enumerates affine points on scheme X defined over a finite field 288 289 INPUT: 290 291 - ``X`` - a scheme defined over a finite field or set of abstract rational points of such a scheme 292 293 OUTPUT: 294 295 - a list containing the affine points of X over the finite field, sorted 296 297 EXAMPLES:: 298 299 sage: F = GF(7) 300 sage: A.<w,x,y,z> = AffineSpace(4,F) 301 sage: C = A.subscheme([w^2+x+4,y*z*x-6,z*y+w*x]) 302 sage: from sage.schemes.generic.rational_point import enum_affine_finite_field 303 sage: enum_affine_finite_field(C(F)) 304 [] 305 sage: C = A.subscheme([w^2+x+4,y*z*x-6]) 306 sage: enum_affine_finite_field(C(F)) 307 [(0, 3, 1, 2), (0, 3, 2, 1), (0, 3, 3, 3), (0, 3, 4, 4), (0, 3, 5, 6), 308 (0, 3, 6, 5), (1, 2, 1, 3), (1, 2, 2, 5), (1, 2, 3, 1), (1, 2, 4, 6), 309 (1, 2, 5, 2), (1, 2, 6, 4), (2, 6, 1, 1), (2, 6, 2, 4), (2, 6, 3, 5), 310 (2, 6, 4, 2), (2, 6, 5, 3), (2, 6, 6, 6), (3, 1, 1, 6), (3, 1, 2, 3), 311 (3, 1, 3, 2), (3, 1, 4, 5), (3, 1, 5, 4), (3, 1, 6, 1), (4, 1, 1, 6), 312 (4, 1, 2, 3), (4, 1, 3, 2), (4, 1, 4, 5), (4, 1, 5, 4), (4, 1, 6, 1), 313 (5, 6, 1, 1), (5, 6, 2, 4), (5, 6, 3, 5), (5, 6, 4, 2), (5, 6, 5, 3), 314 (5, 6, 6, 6), (6, 2, 1, 3), (6, 2, 2, 5), (6, 2, 3, 1), (6, 2, 4, 6), 315 (6, 2, 5, 2), (6, 2, 6, 4)] 316 317 :: 318 319 sage: A.<x,y,z> = AffineSpace(3,GF(3)) 320 sage: S = A.subscheme(x+y) 321 sage: enum_affine_finite_field(S) 322 [(0, 0, 0), (0, 0, 1), (0, 0, 2), (1, 2, 0), (1, 2, 1), (1, 2, 2), 323 (2, 1, 0), (2, 1, 1), (2, 1, 2)] 324 325 ALGORITHM: 326 327 Checks all points in affine space to see if they lie on X. 328 329 NOTE: 330 331 Warning:if X given as input is defined over an infinite field then this code will not finish! 332 333 AUTHORS: 334 335 John Cremona and Charlie Turner (06-2010) 336 337 """ 338 if is_Scheme(X): 339 X = X(X.base_ring()) 340 n = X.codomain().ambient_space().ngens() 341 F = X.value_ring() 342 pts = [] 343 for c in cartesian_product_iterator([F]*n): 344 try: 345 pts.append(X(c)) 346 except: 347 pass 348 pts.sort() 349 return pts