A Brief Introduction to Combinatorics in Sage Math
This document aims to give a crash-course to certain combinatorial features in Sage. This document is meant to follow the notebook A Brief Introduction to Sage Math, available by clicking here.
To execute a piece of Sage code, click on the Input section of the corresponding code cell and hit Shift + Enter (only hitting Enter simply adds a new line). The reader should execute each statement as they work through the notebook, and is encouraged to modify the code and play around as they go. Note that skipping a cell may result in errors when later cells are executed (for instance, if one skips a code block defining a variable and later tries to run code calling that variable). There are a selection of short exercises throughout, and a few larger exercises in the final section. To add a new cell, click to the left of any cell and press the "a" key. To delete a cell, click to the left of a cell and press the "d" key. These (and other) tasks can also be accomplished through the menu bars at the top of the page.
Further information about the combinatorics capabilities of Sage can be found in the official documentation or Part 4 of the open textbook Computational Mathematics with SageMath.
1. Basic Combinatorial Objects and Operations
We begin by discussing a selection of combinatorial objects that can be represented and manipulated in Sage. The point is not to give an exhaustive list of all combinatorial objects in Sage; rather, we simply show that many objects of interest to combinatorialists have already been implemented. Hopefully, the user will be inspired to look further into the vast selection of available options (and maybe even add more).
Subsets and Combinations
Permutations
Additional details on permutations in Sage can be found here.
Integer Partitions
Additional details on integer partitions in Sage can be found here.
Set Partitions
Additional details on set partitions in Sage can be found here.
Alternating Sign Matrices
Additional details on ASMs in Sage can be found here.
Graph Theory
Additional details on Graph Theory in Sage can be found here.
2. Generating Functions and Combinatorial Species
Next, we examine methods for setting up and solving functional equations for the generating functions of combinatorial classes. Recall that the generating function of a sequence of real numbers is the (formal) power series Given a collection of objects of various sizes (for instance, the class of permutations on letters, or the number of binary trees on nodes) the generating function of the class is the formal series whose coefficient of is the number of objects in the collection with size .
Additional details on the Sage methods discussed in this section can be found here. Additional details on the generating functions constructed here can be found in Chapter One of Analytic Combinatorics by Flajolet and Sedgewick. Additional details on species theory can be found in these notes of Bergeron.
3.Rational and D-finite Generating Functions
Finally, we look at two classes of generating functions, and see how they provide data structures for combinatorial sequences.
C-Finite Sequences and Rational Functions
A sequence is C-finite over the rational numbers if it satisfies a linear recurrence of the form for some and constants . It is well known that a sequence of rational numbers is C-finite if and only if its generating function is the power series expansion of a rational function (ratio of polynomials with integer coefficients) at the origin.
P-Recursive Sequences and D-Finite Functions
Generalizing from C-finite sequences, a P-recursive sequence over the rational numbers satisfies a linear recurrence of the form for some and polynomials with . A power series (or differentiable function) is D-finite if it satisfies a linear differential equation of the form for some and polynomials with .
It is well known that a sequence of rational numbers is P-recursive if and only if its generating function is D-finite. Note that if the generating function does not converge at the origin, then the formal derivative is used.
Unlike C-finite sequences, in general D-finite functions cannot be represented explicitly. This means one often must manipulate P-recursive sequences implicitly using the recurrences they satisfy (or the differential equations for the corresponding generating functions).
Manipulating these expressions requires the ore_algebra package, which is not built-in to Sage. It can be downloaded HERE.
Those who have paid for accounts with internet access can install the package in CoCalc by opening a new terminal window and running the command "sage --pip install --user git+https://github.com/mkauers/ore_algebra.git"
Those with free CoCalc accounts can download the zip file from the link above, upload the file to CoCalc, unzip the file, create a new terminal window, navigate to the directory with the package, and then run the command "sage -pip install --user ./"
These capabilities can be combined in somewhat remarkable ways. For instance, consider the problem of enumerating the number of walks on the integer lattice which use the steps North, South, East, West, start at the origin and stay in the non-negative quadrant . The following function computes the number of such walks of a given length.
As a solution of the recurrence, the sequence counting the number of walks under consideration is a linear combination of these basis elements. Thus, (assuming the recurrence is actually satisfied) there exist such that the number of walks satisfies It can be shown that this asymptotic template implies is not C-finite (and, in fact, its generating function is not even algebraic, much less rational).
Using the theory of numeric analytic continuation, the ore_algebra package can compute and rigorously to arbitrary accuracy. Furthermore, the recurrence can be proven using a technique called the kernel method, commonly used in lattice path enumeration. Exact asymptotics of can be computed using the kernel method and the theory of analytic combinatorics in several variables (following a general approach) or using a careful analysis of binomial coefficients.
These topics go beyond the current discussion. They can be found in Section 2.4 (D-finite equations) and Chapter 6 (lattice path enumeration with symmetries) in An Invitation to Analytic Combinatorics: From One to Several Variables.