︠4f58aad8-aa03-4417-90a5-ffaec91c96das︠ ### Google ''permutation, Sage'' and you will get ### Permutations - SageMath Documentation ### http://doc.sagemath.org/html/en/reference/combinat/sage/combinat/permutation.html ︡bd29834a-cbbc-41fd-b74c-5020e9a415d3︡ ︠407baad3-1d24-44dd-835d-691c4a2d02c5︠ ### Permutations in Sage print Permutation([2, 1, 4, 5, 3]) print Permutation( [ (1,2), (3,4,5)] ) ︡89713a36-87c1-4b61-9c97-526fcfd4ed79︡ ︠67c41eda-50bf-4df3-ac1a-9d7a4a31491d︠ ### Use ? to find the possible ways of input ### Permutation is a function convert the input to a Permutation object Permutation? ︡3d735801-dac0-446d-a586-c877c0d9fe13︡ ︠9e597852-aa2e-4bc5-9a45-3d9a67a8c0e7︠ ### An class in Python defines a data type ### Elements in a class is called an object. type(1) ︡db808dc9-d545-4d3e-bdef-fee25bac5843︡ ︠eea2a0e4-d7a3-4947-baf5-aba3b494d3b4s︠ ### Permutation is a class ### each element created by Permutation(l) is a Permutation object p = Permutation( [ (1,2), (3,4,5)] ) type(p) ︡5ed903d3-6342-4c1a-90af-b9cbde7d7435︡ ︠81557aaa-0fbd-45d3-b4a3-b8ce7a126c7d︠ ### Each object is associated with lots of attributes ### list them by dir dir(p) ︡0162ed1b-6e17-497e-8a6d-6608eafc5a86︡ ︠8053c892-4ec1-458a-8ee1-6a7f2f3f137fs︠ ### Attributes are set up when the class was defined ### but you can set attribute by setattr setattr(Permutation,'self_introduction','Hello, I am a permutation on 1,...,n') p.self_introduction ︡53587bc3-8bf6-44cc-9427-3457ab74f286︡ ︠1c1bc840-2a8c-475d-868a-4381918571ees︠ ### An attribute that is a function is called a method p.size p.size() ︡ad0855f1-9d52-435a-a9da-c9a069c65c7d︡ ︠368180d7-68da-4429-a7c3-8ddac8825416s︠ ### A method always take self as its first argument def send(self,i): return self[i-1]; setattr(Permutation,'send',send) print 'p =',p for i in range(1,p.size()+1): print i,p.send(i) ︡ccbfcab2-7289-441a-92e2-ff20046a4229︡ ︠a5407ed1-2560-4f21-8471-3ca8ed377aef︠ ### Find associated attributes and functions by tab ### move the pointer after . and press tab p = Permutation( [ (1,2), (3,4,5)] ) p. ︡1d60ac25-5eda-4e0c-8ee2-f9df6f968502︡ ︠0b6c612a-fd73-44b3-a811-46d087df1a2ds︠ ### the computer needs to know what it is first ### in order to tell you the associated attributes reset() ### clean all variables ppp = Permutation([3,1,2]) ppp. ︡259d2d21-0fce-4995-b930-9a5a270ae829︡ ︠70faaff4-fe81-484d-a676-ca597442a7cbs︠ ### Use ? to read to documentation ### Use ?? to read the source code ppp.size? ︡7e2ca633-35ae-487c-acde-652422cbf8ea︡ ︠c69260cd-c7a8-468b-8ea0-7021b01a35d8︠ ### Exercise 1: ### Read the documentation of "length" and "inversions" and find their relations ppp.inversions? ppp.length? ︡367ff394-5e7a-42a6-92a2-7a8f76baf237︡ ︠4ce1b7c0-ed32-476f-8fb1-b6c5884faa64︠ ### Exercise 2: ### Find a function that return the inverse of a permutation. ppp.inv... ︡d6af7fda-14dd-4c39-95b4-5fa3a2f0bd92︡ ︠9ca8f82b-4488-4b92-931f-d09c16fc09ad︠ ### Exercise 3: ### Explore other functions: to_inversion_vector, action, fixed_points, sign, ... ppp.action... ︡b4dc7f99-2a1b-4593-bcbb-d5e74b5ed797︡ ︠9f20d4a5-6626-47fd-9308-d8da5f58503di︠ %md ## Project 1 Define a function `perm_to_inv(p)` that send a permutation `p` to its inversion table.\ `Permutation([a1,...,an])` will be sent to `[b1,...,bn]`\ where `bk` is the number of `a1,...,a{i-1}` that is greater than `ai=k`. Again, do not use the function `to_inversion_vector`. ︡1491331a-ff0e-4231-ae95-9c18acf6be1f︡{"done":true,"md":"\n## Project 1\n\nDefine a function `perm_to_inv(p)` that send a permutation `p` to its inversion table.\\\n`Permutation([a1,...,an])` will be sent to `[b1,...,bn]`\\\nwhere `bk` is the number of `a1,...,a{i-1}` that is greater than `ai=k`.\n\nAgain, do not use the function `to_inversion_vector`."} ︠49a8da51-288f-4181-b23f-c90972e0f3f6︠ def perm_to_inv(p): ### Your answers: inv_table = []; for k in range(1,p.size()+1): ind = p.index(k); inv_table.append(len([i for i in range(ind) if p[i] > k])); return inv_table; ### test for per in Permutations(4): print "per =", per print "Your answer:", perm_to_inv(per); print "True answer:", per.to_inversion_vector(); print "Correct?", perm_to_inv(per) == per.to_inversion_vector(); ︡1a5bb3c2-29e3-4a4e-82e5-d6352f6cc6a7︡ ︠170c53c5-c795-48f2-93b9-160ef1c4ff05i︠ %md ## Project 2 Define a function `inv_to_perm(t)` that send an inversion table `t` to its permutation. ︡0eb7d90f-1194-46f1-84f4-f874b498e416︡{"done":true,"md":"\n## Project 2\n\nDefine a function `inv_to_perm(t)` that send an inversion table `t` to its permutation."} ︠f3e2bcad-117e-419d-8908-be1eaeb091cess︠ def inv_to_perm(t): ### You answers: nn = len(t); perm = [nn+1] * nn; for k in range(1,nn+1): left = t[k-1]; counter = 0; for i in range(nn): if counter == left and perm[i] == nn+1: perm[i] = k; break; if perm[i] > k: counter += 1; return Permutation(perm); ### test ### your functions should have inv_to_perm(perm_to_inv(p)) == p for per in Permutations(4): print "per =", per #print perm_to_inv(per) print "Correct?", inv_to_perm(perm_to_inv(per)) == per; ︡f197e063-4306-4f65-8cb4-fbd10c4cf641︡ ︠faff3157-8342-4fae-83a2-26ca79cdcdfa︠