#### ch 1 sage: True and True True sage: True and False False sage: False and True False sage: False and False False sage: def implies(p,q): return not(p and not(q)) sage: implies(True,True) True sage: implies(True,False) False sage: implies(False,True) True sage: implies(False,False) True sage: S1 = Set([1,ZZ,"Zeus"]) sage: S2 = Set([S1,1,"Hera"]) sage: S1; S2 {1, 'Zeus', Integer Ring} {{1, 'Zeus', Integer Ring}, 1, 'Hera'} sage: S2.cardinality() 3 sage: S1.difference(S2) {'Zeus', Integer Ring} sage: S2.difference(S1) {{1, 'Zeus', Integer Ring}, 'Hera'} sage: "Zeus" in S1 True sage: S2.symmetric_difference(S1) {{1, 'Zeus', Integer Ring}, 'Hera', 'Zeus', Integer Ring} sage: S1.symmetric_difference(S2) {{1, 'Zeus', Integer Ring}, 'Hera', 'Zeus', Integer Ring} sage: S1.intersection(S2) {1} sage: S1.union(S2) {{1, 'Zeus', Integer Ring}, 1, 'Hera', 'Zeus', Integer Ring} ### ch 2 sage: euler_phi(3) 2 sage: pi = lambda x: pari(x).primepi() sage: pi(4) 2 sage: f1 = lambda x: sqrt(x) sage: f1(3) sqrt(3) sage: f1(4) 2 sage: f2 = lambda x: RR(sqrt(x)) sage: f2(3) 1.73205080756888 sage: f2(4) 2.00000000000000 sage: f = x^4 - 16 sage: f.roots() [(2*I, 1), (-2, 1), (-2*I, 1), (2, 1)] sage: list(cartesian_product_iterator([[1,2], ['a','b']])) [(1, 'a'), (1, 'b'), (2, 'a'), (2, 'b')] sage: A = matrix(3,3,[1,2,3,4,5,6,7,8,9]) sage: (-1)*A [-1 -2 -3] [-4 -5 -6] [-7 -8 -9] sage: v = vector([1,1,1]) sage: A*v (6, 15, 24) sage: x = var("x"); y = var("y"); z = var("z") sage: w = vector([x,y,z]) sage: A*w ( 3 z + 2 y + x, 6 z + 5 y + 4 x, 9 z + 8 y + 7 x) sage: A = matrix(3,3,[1,2,3,4,5,6,1,0,0]) sage: det(A) -3 sage: A^(-1) [ 0 0 1] [ -2 1 -2] [ 5/3 -2/3 1] sage: B = matrix(4,3,[1,2,3,4,5,6,7,8,9,1,1,1]) sage: B*A [12 12 15] [30 33 42] [48 54 69] [ 6 7 9] sage: A = matrix(3,3,[1,2,3,4,5,6,7,8,9]) sage: det(A) 0 sage: a = var("a"); b = var("b"); c = var("c"); d = var("d") sage: B = matrix(2,2,[a,b,c,d]) sage: det(B) a*d - b*c sage: C0 = [x for x in range(-10,10) if x%3 == 0]; print C0 [-9, -6, -3, 0, 3, 6, 9] sage: C1 = [x for x in range(-10,10) if x%3 == 1]; print C1 [-8, -5, -2, 1, 4, 7] sage: C2 = [x for x in range(-10,10) if x%3 == 2]; print C2 [-10, -7, -4, -1, 2, 5, 8] sage: expand((x+y)^(5)) y^5 + 5*x*y^4 + 10*x^2*y^3 + 10*x^3*y^2 + 5*x^4*y + x^5 sage: binomial(5,2) 10 sage: terms = ["x","x","y","y","y"] sage: number_of_permutations(terms) 10 sage: permutations(terms) ['xxyyy', 'xyxyy', 'xyyxy', 'xyyyx', 'yxxyy', 'yxyxy', 'yxyyx', 'yyxxy', 'yyxyx', 'yyyxx'] sage: terms = [1,2,3,4,5] sage: number_of_permutations(terms) 120 sage: factorial(5) 120 ### ch 3 def swap(g): N = parent(g).degree() return sum([len([i2 for i2 in range(i1+1,N+1) if g(i2) Permutation Group with generators [(1,2), (3,4,5)], Homomorphism : Cyclic group of order 3 as a permutation group --> Permutation Group with generators [(1,2), (3,4,5)], Homomorphism : Permutation Group with generators [(1,2), (3,4,5)] --> Cyclic group of order 2 as a permutation group, Homomorphism : Permutation Group with generators [(1,2), (3,4,5)] --> Cyclic group of order 3 as a permutation group) sage: G[0].order() 6 sage: C3 = CyclicPermutationGroup(3) sage: S8 = SymmetricGroup(8) sage: W = C3._gap_().WreathProduct(S8._gap_()) sage: W.Order() 264539520 sage: W.Order() == 3^8*factorial(8) True ### ch 10 sage: p = lambda n: \ prod([(t^(j+1)-1)/(t-1) for j in range(1,n+1)]) sage: p(2) t^3 + 2*t^2 + 2*t + 1 sage: G = SymmetricGroup(3) sage: p = sum([t^swap(g) for g in G]) sage: p t^3 + 2*t^2 + 2*t + 1 sage: rubik = CubeGroup() sage: b = rubik.B() sage: d = rubik.D() sage: f = rubik.F() sage: l = rubik.L() sage: r = rubik.R() sage: u = rubik.U() sage: r*u*r^(-1) (1,19,24,6)(2,21,7,4)(8,30,17,9)(10,34,28,18)(11,35,25,43) sage: G = PermutationGroup([b,d,f,l,r,u]) sage: g = G(str(r*u*r^(-1))) sage: g.word_problem([b,d,f,l,r,u]) x5*x6*x5^-1 [['(25,27,32,30)(26,29,31,28)(3,38,43,19)(5,36,45,21) *(8,33,48,24)', 1], ['(1,3,8,6)(2,5,7,4)(9,33,25,17)(10,34,26,18) *(11,35,27,19)', 1], ['(25,27,32,30)(26,29,31,28)(3,38,43,19)(5,36,45,21) *(8,33,48,24)', -1]] '(25,27,32,30)(26,29,31,28)(3,38,43,19)(5,36,45,21) *(8,33,48,24)(1,3,8,6)(2,5,7,4)(9,33,25,17)(10,34,26,18) *(11,35,27,19) (25,27,32,30)(26,29,31,28)(3,38,43,19) *(5,36,45,21)(8,33,48,24)^-1' sage: rubik = CubeGroup() sage: state = rubik.faces("R*U") sage: rubik.solve(state) 'R*U' sage: state = rubik.faces("R*U*R^(-1)") sage: state {'back': [[33, 28, 25], [36, 0, 37], [38, 39, 40]], 'down': [[41, 42, 11], [44, 0, 45], [46, 47, 48]], 'front': [[9, 10, 24], [20, 0, 7], [22, 23, 6]], 'left': [[8, 34, 35], [12, 0, 13], [14, 15, 16]], 'right': [[43, 26, 27], [18, 0, 29], [17, 31, 32]], 'up': [[19, 21, 3], [2, 0, 5], [1, 4, 30]]} sage: rubik.solve(state) 'R*U*R^-1' ## ch 11 sage: u2 = '(1,3)(2,4)'; f2 = '(1,8)(4,5)'; d2 = '(5,7)(6,8)' sage: b2 = '(3,6)(2,7)'; r2 = '(2,5)(1,6)'; l2 = '(4,7)(3,8)' sage: H = PermutationGroup([u2,f2,d2,b2,r2,l2]) sage: H.order() 96 sage: H.conjugacy_classes_representatives() [(), (5,7)(6,8), (3,6,8)(4,5,7), (2,4)(5,7), (2,4,7,5)(3,6), (1,3)(6,8), (1,3,6,8)(4,7), (1,3)(2,4)(5,7)(6,8), (1,3,6,8)(2,4,5,7), (1,3)(2,5)(4,7)(6,8)] sage: P = PolynomialRing(GF(5),"x") sage: x = P.gen() sage: K. = GF(5^2, name='a', modulus=x^2 - 2 ) sage: K Finite Field in a of size 5^2 sage: L = GF(25,"b") sage: L Finite Field in b of size 5^2 sage: rubik = CubeGroup() sage: f = rubik.F() sage: u = rubik.U() sage: T = PermutationGroup([f,u]) sage: T.order() 73483200 sage: P = PGL(2,5) sage: sT = PermutationGroup(['(1,8,5,3)','(4,3,5,6)']) sage: sT.order() 120 sage: sT.is_isomorphic(P) True sage: rT = PermutationGroup(['(1,6,9,5)','(1,4,3,2)']) sage: rT.is_isomorphic(S7) True ########## ch 14: sage: r = lambda t: 11-t sage: s = lambda t: min(2*t,23-2*t) sage: R = [str((i+1,r(i)+1)) for i in range(12)] sage: S = [str((i+1,s(i)+1)) for i in range(12)] sage: print R ['(1, 12)', '(2, 11)', '(3, 10)', '(4, 9)', '(5, 8)', '(6, 7)', '(7, 6)', '(8, 5)', '(9, 4)', '(10, 3)', '(11, 2)', '(12, 1)'] sage: print S ['(1, 1)', '(2, 3)', '(3, 5)', '(4, 7)', '(5, 9)', '(6, 11)', '(7, 12)', '(8, 10)', '(9, 8)', '(10, 6)', '(11, 4)', '(12, 2)'] sage: g1 = G('(1, 12)(2, 11)(3, 10)(4, 9)(5, 8)(6, 7)') sage: g2 = G('(2, 3, 5, 9, 8, 10, 6, 11, 4, 7, 12)') sage: M = PermutationGroup([g1,g2]) sage: M.order() 95040 sage: MathieuGroup(12).order() 95040 sage: MathieuGroup(12).is_isomorphic(M) True sage: G = PGL(2,13) sage: G._gap_().LargestMovedPoint() 14 sage: G._gap_().SmallerDegreePermutationRepresentation() IdentityMapping( Group([ (3,14,13,12,11,10,9,8,7,6,5,4), (1,2,9)(3,8,10)(4,5,12)(6,13,14) ]) ) sage: Set([x^2 for x in GF(23)]) {0, 1, 2, 3, 4, 6, 8, 9, 12, 13, 16, 18} sage: MathieuGroup(12).order() 95040 sage: MathieuGroup(24).order() 244823040 sage: C = HammingCode(3,GF(2)) sage: C Linear code of length 7, dimension 4 over Finite Field of size 2 sage: C.gen_mat() [1 1 1 0 0 0 0] [1 0 0 1 1 0 0] [0 1 0 1 0 1 0] [1 1 0 1 0 0 1] sage: C.check_mat() [0 1 1 1 1 0 0] [1 0 1 1 0 1 0] [1 1 0 1 0 0 1] sage: C = TernaryGolayCode(); C Linear code of length 11, dimension 6 over Finite Field of size 3 sage: C = BinaryGolayCode(); C Linear code of length 23, dimension 12 over Finite Field of size 2 sage: C = ExtendedBinaryGolayCode(); C Linear code of length 24, dimension 12 over Finite Field of size 2 sage: print C.weight_distribution() [1, 0, 0, 0, 0, 0, 0, 0, 759, 0, 0, 0, 2576, 0, 0, 0, 759, 0, 0, 0, 0, 0, 0, 0, 1] sage: c = C.random(); c (0, 0, 1, 0, 1, 0, 0, 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1) sage: hamming_weight(C.random()) 12 sage: c in C True sage: d = c.nonzero_positions(); d [2, 4, 8, 9, 11, 14, 15, 17, 19, 21, 22, 23] sage: C = ExtendedBinaryGolayCode() The weight 8 codewords of C form a t-(v,k,lambda) design, where t = 5 , v = 24 , k = 8 , lambda = 1 There are 759 blocks of this design. The weight 12 codewords of C form a t-(v,k,lambda) design, where t = 5 , v = 24 , k = 12 , lambda = 48 There are 2576 blocks of this design. The weight 16 codewords of C form a t-(v,k,lambda) design, where t = 5 , v = 24 , k = 16 , lambda = 78 There are 759 blocks of this design. The weight 24 codewords of C form a t-(v,k,lambda) design, where t = 5 , v = 24 , k = 24 , lambda = 1 There are 1 blocks of this design. The weight 8 codewords of C* form a t-(v,k,lambda) design, where t = 5 , v = 24 , k = 8 , lambda = 1 There are 759 blocks of this design. The weight 12 codewords of C* form a t-(v,k,lambda) design, where t = 5 , v = 24 , k = 12 , lambda = 48 There are 2576 blocks of this design. The weight 16 codewords of C* form a t-(v,k,lambda) design, where t = 5 , v = 24 , k = 16 , lambda = 78 There are 759 blocks of this design. ['weights from C: ', [8, 12, 16, 24], 'designs from C: ', [[5, (24, 8, 1)], [5, (24, 12, 48)], [5, (24, 16, 78)], [5, (24, 24, 1)]], 'weights from C*: ', [8, 12, 16], 'designs from C*: ', [[5, (24, 8, 1)], [5, (24, 12, 48)], [5, (24, 16, 78)]]] sage: MS = MatrixSpace(QQ,12,12) sage: A = MS(sage_eval(gap.eval("HadamardMat( 12 )"))) sage: A [ 1 1 1 1 1 1 1 1 1 1 1 1] [ 1 -1 1 -1 1 1 1 -1 -1 -1 1 -1] [ 1 -1 -1 1 -1 1 1 1 -1 -1 -1 1] [ 1 1 -1 -1 1 -1 1 1 1 -1 -1 -1] [ 1 -1 1 -1 -1 1 -1 1 1 1 -1 -1] [ 1 -1 -1 1 -1 -1 1 -1 1 1 1 -1] [ 1 -1 -1 -1 1 -1 -1 1 -1 1 1 1] [ 1 1 -1 -1 -1 1 -1 -1 1 -1 1 1] [ 1 1 1 -1 -1 -1 1 -1 -1 1 -1 1] [ 1 1 1 1 -1 -1 -1 1 -1 -1 1 -1] [ 1 -1 1 1 1 -1 -1 -1 1 -1 -1 1] [ 1 1 -1 1 1 1 -1 -1 -1 1 -1 -1]