Permutations Tutorial for Math 4660
ubuntu2204
Permutations Tutorial for Math 4660
Permutations
This worksheet is a beginner's tutorial to how to work with permutations in SageMath. This worksheets assumes no prior knowledge of coding in Python or Sage, but seeks to demonstrate primarily by example and does not provide lengthy explanations of concepts. If you are confused or want more detail about a specific concept, feel free to ask questions or to Google the concept.
Some reminders:
Remember that you run code in a "cell" by hitting "Shift" and "Enter" while your cursor is in the shell.
If you put the
#character on a line, everything after on that line is ignored. We call this a comment and there are helpful comments placed throughout this worksheet in code boxes.There are coding exercises throughout this worksheet denoted with bold Exercise at the beginning. You can complete the exercises in a cell below the exercise.
All permutations are given in one-line notation.
Finally, run this code to make sure permutation composition agrees with your class. We want for permutations .
You can create permutation objects using one-line notation.
Exercise: Create a permutation tau that sends 1 to 3, 2 to 1, 3 to 2, and fixes 4,5,6,7. Then, check that it works correctly.
You can multiply and invert permutations!
Exercise: What is the inverse permutation to tau above? Write it down and then check your answer!
If/Else Statements
In programs, we can control the logic flow using "if/else" statements. Let's try an example. Remember that a permutation is called an involution if .
Exercise: Check if sigma2 defined below is an involution or not using an "if/else" statment. Also, pay attention to how indentation is used in the above example.
Also, notice that in Sage, you check if two things are equal using a double equals sign, ==
It is annoying to rewrite the same blocks of code over and over again, so instead we can create functions to reuse code. Functions work like this silly example.
Exercise: Write a function is_perm_inv to check whether or not a permutation is an involution. Test it on sigma and sigma2.
List Comprehensions
In Python, there is a tool called a "list comprehension" that allows you to apply a function to every element in a list. So, if you have some function f and some list L, then [f(a) for a in L] will apply the function to every list element. Let's look at an example
Definition: We say a permutation has an inversion if and .
Definition: the length of a permutation is the number of pairs such that and , ie the number of inversions has.
Then, given a length and a size , we may wonder how many permutations of size have length .
Lists are an essential data structure in Python, so it is important to know how to manipulate them.
Exercise: Using your is_perm_inv function, create a list using a list comprehension that tells you which permutations of 3 are inversions and which ones are not, similar to the list above.
We can also combine list comprehensions and if statements like so
This notation is meant to read like an English sentence. That is, this list contains every permutation in Permutations(3) if the length of the permutation is 1. So, the if condition "filters" your list comprehension and only contains the items that satisfy the condition.
Exercise: Write a function called involutions(n) that returns a list of all permutations of that are involutions. Check your result on permutations of and make sure it agrees with what you know mathematically.
Definition: The Lehmer code of a permutation on is a list where is the number of such that .
In SageMath, you can compute the Lehmer code of a permutation. Furthermore, SageMath provides documentation of built-in functions. If you want to know more about a function, you can type the name of the function followed by a question mark, like in this example below.
You can also Google "sage Lehmer code" to quickly get a website version of this help page. It is very useful to learn how to read and search the SageMath reference manual so you can figure out how to use functions in Sage.
Definition: We say a permutation has a descent at position if .
For example, has descents at and .
Exercise: Modify the list comprehension below to create a list that shows all permutations of that have exactly 1 descent and their Lehmer codes. Do you notice anything interesting about the Lehmer codes of these permutations?
You can also view exactly where the descents of a permutation are like so
In Python, lists are indexed starting at 0. So, this output says the permutation has descents at positions 2 and 4. To see what is happening more concretely, look at the list example below.
So, when we work with lists in Python and Sage, we need to get used to count starting with 0 instead of with 1.
Exercise Examine and run the code below. While this code contains concepts we have not covered yet, see if you can understand what it does. Compare its output to the permutations in the previous exercise.
For loops
In Python, you can iterate over items in a list in the order they appear using a for loop. The code in the last exercise used a for loop to print information about each permutation of that had exactly 1 descent.
For loops can be useful for doing complex operations for each element of a list. For example, we can print out a multiplication table of permutations (although it may not be easy to read).
Final remarks
In this tutorial, we covered the basics of how to use permutations in SageMath, if/else, list comprehensions, and for loops. However, we have only just scratched the surface! There is much more to learn.
To learn more about the various programming structures in Python, check the control flow section of the Python tutorial
To learn more about what Sage can do with permutations, visit the documentation for the Permutation class