Matroid Computation with Sage: Extensions

In this post I want to continue exploring the new-found capabilities of Sage with respect to matroid computation. It follows up on this post; Gordon Royle started a different sequence of posts on sage-matroids here. Following a comment by Jason Grout, I decided to experiment with the Sage Cell Server. The result? All computations in this post can be carried out live, by clicking the “Evaluate” button, and you can edit them to play with them! How awesome is that? A word of warning: the cells in each section of this post are linked together, so if you didn’t evaluate a previous cell, the ones below it might not work. For technical reasons, we use # signs on otherwise empty lines. These symbols (and anything following) are comments, and don’t do anything.

The topic for today is matroid extensions. Continue reading

Matroid Computation with Sage

October 7, 2013, marked a day that I have long been looking forward to: the release of Sage 5.12. This is the first version of Sage to support matroid theory!

This is the culmination of 2 1/2 years of work by Rudi Pendavingh and myself, with further help from Michael Welsh and Gordon Royle. In this post I’m going to demonstrate why you should be as excited as we are.

What is Sage?

From the Sage homepage:

Sage is a free open-source mathematics software system licensed under the GPL. It combines the power of many existing open-source packages into a common Python-based interface.
Mission: Creating a viable free open source alternative to Magma, Maple, Mathematica and Matlab.

There are several ways to interact with Sage:

  • Download and install it on your own computerThere are plenty of installation instructions available.
  • Use Sage online, through the Sage Notebook (runs version 5.11, without the matroids, and is down at the time of writing).
  • Use Sage online, through the SageMathCloud .

The third option is the fastest way to get started. To get an idea what Sage is capable of, look at the considerable documentation on the Sage website (including a tour, a tutorial, and much more).

Matroids in Sage

Matroid computation is not new. I have a list of previous efforts on these slides, and currently the most widely used software is Petr Hliněný’s MACEK. It is very powerful when it can already do what you want, but its functionality has its bounds, and it is very hard to teach it new tricks. Moreover, the original author no longer supports it.

To avoid ending up with the same fate, we wanted to tie our efforts in with other open-source software. GAP and Sage were the obvious candidates, with Sage winning out because 1) it includes GAP, and 2) Sage appeared to have a more suitable programming language.

Sage gives us a big developer community, regular updates that are tested on many different systems, a bug tracker, a question-and-answer site (be sure to add the “matroid” tag to your questions), a support newsgroup, and did I mention extensive documentation? Like the Matroid Theory part of the Reference Manual.

Some examples

The best way to get to work with the software is to “get your hands dirty”: install it, run it, and experiment, with the Reference Manual at hand. To get you going, I include a few examples of the capabilities below. Lines starting with “sage: ” or “….:” are input, and other lines are output. Anything from a # symbol to the end of the line is a comment. Let’s get started with a theorem:

Theorem. The Fano matroid, $F_7$, is a splitter for the class of matroids with no $U_{2,5}$-, $U_{3,5}$-, and $F_7^*$-minors.

Proof. By Seymour’s Splitter Theorem, we only need to inspect the single-element extensions and coextensions. And since $F_7$ is 3-connected, a single-element extension is 3-connected if and only if it is simple, and a single-element coextension is 3-connected if and only if it is cosimple.

sage: F7 = matroids.named_matroids.Fano()
sage: U25 = matroids.Uniform(2,5)
sage: U35 = matroids.Uniform(3,5)
sage: F7d = F7.dual()
sage: S = [U25, U35, F7d]
sage: Ext = F7.extensions()
sage: print [M for M in Ext if M.is_simple() and not 
....:            any(M.has_minor(N) for N in S)]
[]
sage: CoExt = F7.coextensions()
sage: print [M for M in CoExt if M.is_cosimple() and not
....:            any(M.has_minor(N) for N in S)]
[]

The two lines containing “[ ]” are output. They show that the lists we just constructed are empty, and the result is proved. $\square$

What happened here? In the first three lines, we assigned easy names to several built-in matroids. To get to the built-in matroids, you can type “matroids.” in Sage (note the dot), and hit the TAB key. Likewise, typing “matroids.named_matroids.” and hitting TAB will bring up the list of named matroids. For help, you can always type the name of an object or function followed by “?”, and hit the TAB key (note: in all these examples, do not hit ENTER at any point). For instance, typing

F7?

will print this section of the reference manual.

Each matroid has many methods associated with it. For instance, in the fourth line of our example, we compute the dual of $F_7$. And in the sixth line we compute the single-element extensions. Similar to before, to get a list of all methods you can type

F7.<TAB>

and to get help for any method, type

F7.contract?<TAB>

Easy! The remaining lines show off some aspects of the programming language underlying Sage, Python. It has a very pleasant, mathematical syntax, with excellent support for working with lists and sets of objects.

Let’s look at some more examples. At this point, there is no built-in method to compute the lattice of flats of a matroid. But it is very straightforward to implement this (I spent more time hunting through the documentation on Sage’s posets than on writing the code):

sage: def lattice_of_flats(M):
....:     F = [] 
....:     for i in range(M.rank()+1): 
....:         F.extend([frozenset(X) for X in M.flats(i)]) 
....:     P = Poset((F, lambda x, y: x <= y)) 
....:     return P
....:
sage: lattice_of_flats(F7).plot()

This will yield the following picture (which could be improved, of course):

Lattice of Flats of $F_7$

The first line of this code indicates that we are defining a function with one input, $M$. We use that “M.flats(i)” returns the set of all flats of a matroid $M$ rank $i$. By calling this method for each $i$, doing a little conversion on the output, and gluing the resulting list onto the end of $F$, we make $F$ equal to the list of all flats of $M$. Then we create a poset with a custom partial order, using that Python’s built-in “frozenset” type uses set inclusion as the interpretation of its “<=” operator. The “lambda” notation means we have an inline function taking two inputs, $x$ and $y$. This is another standard Python feature.

Finally, let’s verify for one example that the weight enumerator of a linear code is an evaluation of the Tutte polynomial:

For linear codes, the Weight Enumerator is an evaluation of the Tutte Polynomial of the corresponding matroid. Example:

sage: C = HammingCode(3, GF(3))
sage: x, y = var('x,y')
sage: p = C.weight_enumerator('xy')

sage: M = matroids.PG(2,3).dual() 
sage: t = M.tutte_polynomial(x,y) 
sage: u = t.substitute({x: 3*y/(x-y)+1, 
....:                   y: (x-y)/y+1   }) * y^3 * (x-y)^10 
sage: q = u.full_simplify()

sage: p - q
0

This example also shows Sage’s capabilities for symbolic math. The two variables $x$, $y$ are declared as symbols, and we can use them in polynomials and other functions.

Unfortunately, it also shows that Sage, because it is so big and has so many developers, does not always have a consistent interface. The syntax for the weight enumerator differs from that of the Tutte polynomial.

In conclusion

I didn’t show off the powerful isomorphism tests, the additional capabilities that linear matroids bring to the table, or how to define your own matroids. The latter is pretty easy. In most cases, just use the “Matroid” function, which accepts a variety of inputs:

sage: G = graphs.PetersenGraph()
sage: M = Matroid(G)
sage: M
Regular matroid of rank 9 on 15 elements with 2000 bases

I invite you to give Sage, and its matroid functionality in particular, a try! Make sure to let us know what you accomplish with the code, and what you would like to see added. As for me, together with Carolyn, Deb, and Dillon, I hope to finish a first research paper that uses Sage computations in the next few weeks.

edited on October 15 to reflect that SageMathCloud runs Sage 5.12 now.

Partial Fields, I

Partial fields give a more flexible theory of matroid representation than mere fields. Rather than committing to a field, a partial field allows you to capture several representations at once. They were first introduced by Semple and Whittle [SW96]. The archetypical example is the following theorem by Tutte:

Theorem 1. The following statements are equivalent for a matroid $M$:

  1. $M$ is representable over both $\text{GF}(2)$ and $\text{GF}(3)$;
  2. $M$ is representable over $\text{GF}(2)$ and some field of characteristic other than $2$;
  3. $M$ is representable over $\mathbb{R}$ by a totally unimodular matrix;
  4. $M$ is representable over every field.

Matroid representations over partial fields generalize totally unimodular matrices, as we will see below. We know that regular matroids (i.e. the matroids from Theorem 1) possess many interesting properties. Let $\mathcal{M}$ be the class of regular matroids.

  1. The excluded minors for $\mathcal{M}$ are $U_{2,4}$, $F_7$, and $F_7^*$.
  2. Every regular matroid is uniquely representable over every field (up to row operations and column scaling).
  3. All regular matroids can be obtained from graphic matroids and $R_{10}$ by dualizing and 1-, 2-, 3-sums (this is Seymour’s Decomposition Theorem).
  4. A simple, regular matroid of rank $r$ has at most ${r+1 \choose 2}$ elements, with equality for the graphic matroids of complete graphs.
  5. If $A$ is a totally unimodular matrix of full row rank representing $M$, then $\det(A A^T)$ equals the number of bases of $M$.

For every partial field $\mathbb{P}$, one may ask if analogues of these properties exist for the class of $\mathbb{P}$-representable matroids. This is an area of research with a number of interesting results and many unsolved problems.

In this post I will limit myself to introducing matroid representation over partial fields, linking them to the definition of totally unimodular matrices, and giving some examples. In my next post I will discuss Whittle’s characterization [Whi95, 97] of the representations of ternary matroids.

1. Definitions

The definition of a partial field is straightforward:

Definition 2. partial field is a pair $\mathbb{P} = (R, G)$, where $R$ is a commutative ring, and $G$ is a subgroup of the invertible elements of $R$ such that $-1 \in G$.

I will introduce matroid representations over partial fields through chain groups. For fields, this corresponds to the row space of the representation matrix. The advantage is a very clean basic theory, plus an extension to noncommutative rings at no extra cost. Incidentally, this is how Tutte himself thought about regular matroids.

Definition 3. Let $R$ be a ring, and $E$ a finite set. A chain group is a subset $C \subseteq R^E$ such that, for $c, d \in C$ and $r \in R$,

  1. $0 \in C$;
  2. $c+d \in C$;
  3. $rc \in C$.

The elements of a chain group are called chains.

Definition 4. The support of a chain $c\in C$ is
$$\| c \| := \{e \in E : c_e \neq 0\}.$$

A nonzero chain with inclusionwise minimal support is called elementary.

Definition 5. Let $G \subseteq R$. A chain $c \in R^E$ is $G$-primitive if $c \in (G\cup\{0\})^E$.

Definition 6. Let $\mathbb{P} = (R, G)$ be a partial field, and $E$ a finite set. A chain group $C \subseteq R^E$ is a $\mathbb{P}$-chain group if, for every elementary chain $c$ there exists an $r\in R$ such that $c = rg$ for some $G$-primitive chain $g$.

I realize this was a fairly long string of definitions, but the payoff is that we can very quickly define matroid representations:

Theorem 7. Let $\mathbb{P}$ be a partial field, and $C$ a $\mathbb{P}$-chain group. Define
$$\mathcal{C}^* := \{ \|c\| : c \in C, \text{ elementary}\}.$$
Then $\mathcal{C}^*$ is the collection of cocircuits of a matroid, $M(C)$.

Proof. We verify the (co)circuit axioms.

  1. $\emptyset\not\in \mathcal{C}^*$.
    An elementary chain was defined to be nonzero.
  2. If $C, D \in \mathcal{C}^*$ and $C \subseteq D$ then $C = D$.
    The supports of elementary chains are inclusionwise minimal.
  3. If $C, D \in \mathcal{C}^*$ are distinct, and $e \in C \cap D$ then there is a member $F \in \mathcal{C}^*$ with $F \subseteq (C\cup D) – \{e\}$.
    Let $c, d$ be chains with $\|c\| = C$ and $\|d\| = D$. Since these chains are elementary, we may assume they are $G$-primitive, i.e. every nonzero entry is invertible. Now write
    $$g := c_e^{-1} c – d_e^{-1} d.$$
    Then $g$ is nonzero, and therefore there is an elementary chain $f$ whose support is contained in that of $g$. We simply take $F = \|f\|$. $\square$

Definition 8. We say a matroid $M$ is $\mathbb{P}$-representable if there exists a $\mathbb{P}$-chain group $C$ such that $M = M(C)$.

One thing to note is that everything above holds even if we don’t assume $R$ to be commutative. This theory of skew partial fields was explored in [PZ13]. For the remainder of this post we’ll stick to the commutative case, though.

2. Restrictions on determinants

If you’ve seen totally unimodular matrices defined, you’ve probably seen a definition involving determinants. This also works for partial fields:

Definition 9. Let $\mathbb{P} = (R, G)$ be a partial field. A $\mathbb{P}$-matrix is a matrix $A$ with entries in $R$ such that each square submatrix has a determinant in $G \cup \{0\}$.

We denote by $A[X]$ the submatrix of $A$ with columns indexed by $X$.

Theorem 10. Let $\mathbb{P} = (R, G)$ be a partial field, and $A$ a $\mathbb{P}$-matrix with $r$ rows and columns indexed by $E$. Define
$$\mathcal{B}_A := \{ X \subseteq E : |X| = r \text{ and } \det(A[X]) \neq 0\}.$$
If this set is nonempty, then $\mathcal{B}_A$ is the set of bases of a matroid, $M[A]$.

Proof. We use a trick from commutative algebra. Let $I$ be a maximal ideal of $R$. Such an ideal is guaranteed to exist (assuming the axiom of choice). Then $\mathbb{F} := R/I$ is a field. Consider the ring homomorphism $\phi: R \to \mathbb{F}$ sending $r$ to $r + I$. Since this is a homomorphism, it commutes with addition and multiplication. Hence, if we apply it to the entries of $A$, we preserve the nonzero determinants. Since we have a matroid after applying $\phi$, we must have had a matroid before. $\square$

The link with the previous section is simple:

Theorem 11. Let $C$ be the chain group generated by the rows of a $\mathbb{P}$-matrix $A$. Then $C$ is a $\mathbb{P}$-chain group and $M(C) = M[A]$.

We also have a converse:

Theorem 12. Let $C$ be a $\mathbb{P}$-chain group, let $B$ be a basis of $M(C)$, and let $A$ be the matrix whose rows are $G$-primitive chains such that their supports form the $B$-fundamental cocircuits of $M(C)$. Then $A$ is a $\mathbb{P}$-matrix, and $M[A] = M(C)$.

This post is already getting long, so I will skip the proof and refer to [PZ13, Section 3.4] instead.

3. Examples

The key takeaway from the proof of Theorem 10 is the idea of a homomorphism:

Definition 13. Let $\mathbb{P}_1 = (R_1, G_1)$ and $\mathbb{P}_2 = (R_2, G_2)$ be partial fields, and let $\phi: R_1 \to R_2$ be a ring homomorphism such that $\phi(G_1) \subseteq G_2$. Then $\phi$ is a partial field homomorphism.

Theorem 14. If $\phi$ is as above, then every $\mathbb{P}_1$-representable matroid is also $\mathbb{P}_2$-representable.

We omit the proof, which is only a slight modification of the proof of Theorem 10. Note that we can identify a field $\mathbb{F}$ with the partial field $(\mathbb{F}, \mathbb{F}^*)$.

The regular partial field is $\mathbb{U}_0 = (\mathbb{Z}, \{-1, 1\})$. The $\mathbb{U}_0$-matrices are precisely the totally unimodular matrices. There is clearly a partial field homomorphism to any partial field, so regular matroids are, in fact, representable over every partial field too!

The dyadic partial field is $\mathbb{D} = (\mathbb{Z}[\frac{1}{2}], \langle -1, 2 \rangle)$. This means that we extended the ring of integers with all powers of $\frac{1}{2}$, and consider matrices where all subdeterminants are, in absolute value, a (positive or negative) power of $2$. There is a ring homomorphism to every field of characteristic other than 2.

The golden ratio partial field is $\mathbb{G} = (\mathbb{Z}[\tau], \langle -1, \tau\rangle)$, where $\tau$ is the golden ratio, i.e. the positive root of $x^2 – x – 1 = 0$. There is a homomorphism to a number of fields, notably $\text{GF}(4)$ and $\text{GF}(5)$. In fact, it can be shown that a matroid has a golden ratio representation if and only if it is representable over both $\text{GF}(4)$ and $\text{GF}(5)$.

The partial field $\mathbb{P}_4$ is defined as follows. Let $\alpha$ be an indeterminate, let $G$ be the subgroup of the units of $\mathbb{Q}(\alpha)$ generated by $\{-1, \alpha, \alpha-1, \alpha + 1, \alpha – 2\}$, and let $R$ be the smallest subring of $\mathbb{Q}(\alpha)$ containing $G$. There is a partial field homomorphism to every field with at least four elements (obtained by mapping $\alpha$ to any element other than $0, 1, -1, 2$). Let me end with a conjecture about this partial field:

Conjecture 15. A matroid is representable over all fields with at least four elements if and only if it is representable over $\mathbb{P}_4$.

Note that the partial fields for the corresponding statements for two and three elements are known: they are, respectively, the regular and near-regular partial fields.

For a bigger catalog of partial fields, including a list of homomorphisms between them, I refer to the appendix of my PhD Thesis, [vZ09].

References

[SW96] Charles Semple and Geoff Whittle. Partial fields and matroid representation. Adv. in Appl. Math., 17(2):184–208, 1996.

[PZ13] R.A. Pendavingh, S.H.M. van Zwam. Skew partial fields, multilinear representations of matroids, and a matrix tree theorem. Advances in Applied Mathematics, Vol. 50, Issue 1, pp. 201-227, 2013 (PDFarXivdoi).

[Whi95]  Geoff Whittle. A characterisation of the matroids representable over GF(3) and the rationals. J. Combin. Theory Ser. B, 65(2):222–261, 1995.

[Whi97]  Geoff Whittle. On matroids representable over GF(3) and other fields. Trans. Amer. Math. Soc., 349(2):579–603, 1997.

[vZ09] Stefan H. M. van Zwam. Partial Fields in Matroid Theory. PhD thesis, Technische Universiteit Eindhoven, 2009.