SageMath minicourse

A few weeks ago, at the 2019 CMS Summer Meeting in Regina, I gave a mini-course on the casual use of SageMath, for research and study in discrete math. As an example, I constructed a strongly regular graph; this used incidence structures, projective spaces, hyperovals, and finite fields.

The slides for the talk are  here: guo-cms. There is an accompanying .sage file, which can be executed from the sage command line with runfile, or copied in line by line, into a sage worksheet. This file is downloadable from my Google Drive here. Since it seems difficult to download or upload a .sage file, since it is executable, I’ve also included the contents below:

# CMS file for SRG, T2*O demonstration
def number_common_nbrs(G,u,v):
	return len(Set(G.neighbors(u)).intersection(Set(G.neighbors(v))))

def get_incidence_matrix(pts,blk):
	M = Matrix(len(pts), len(blk))
	for it in range(len(pts)):
		for ce in range(len(blk)):
			if pts[it] in blk[ce]:
				M[it,ce] = 1
	return M

# returns point graph of an incidence structure
def pointsGraph(T):
	P = T.ground_set()
	return Graph([range(len(P)), lambda i,j: i!=j and areAdjacent(T,P[i],P[j]) ])

# returns true if 2 pts in incidence structure T lie on the same block
def areAdjacent(S,i,j):
	adj = false
	for k in S.blocks():
		if i in k and j in k:
			adj = true
	return adj

# Here is a strongly regular graph; you can use number_common_nbrs(G,u,v)
# on the pairs of vertices of srg to verfiy that it is strongly regular.

# Here is a generalized quadrangle;
# pts is a list of points
# blks is a list that has lists consisting of points incident to each block
# One can check that for every p in pts and b in blks such that p is not in b
# there exists a unique b' in blks such that p is in b' and b' intersects b.

pts = [0, 1, 2, 3, 4, 5, 6, 7]
blks = [[0, 1], [0, 2], [0, 5], [0, 7], [1, 3], [1, 4], [1, 6], [2, 3], [2, 4], [2, 6], [3, 5], [3, 7], [4, 5], [4, 7], [5, 6], [6, 7]]
gq = [pts, blks]

# Now we will construct the GQ from T_2^*(O) of order q = 4.
# first we construct the field of order 4
F = GF(4,'a')
# Now we will construct the projective space PG(3,q);
# the points, lines, and planes will be 1-dim, 2-dim, and 3-dim, resp., subspaces of
# a 4-dimensional vector space
# for now, it is only necessary to have the 4-dim vector space
V = VectorSpace(F,4)
# We will pick a specific plane in this vector space, in which we will place the hyperoval
W = V.subspace_with_basis([[,0,0,0],[0,,0,0],[0,0,,0]])
# Now we construct the hyperconic hyperoval in W
ov = []
elts = F.list()
for i in elts:

# Now we get the points of the GQ, which are points of V, not in W.
ptslist =[]
for i in V.subspaces(1):
	if not i.is_subspace(W):

# Now we get the points of the GQ, which are 2-dmin subspaces of V, which intersect a point of the hyperoval. .
lns =[]
for i in V.subspaces(2):
	if not i.is_subspace(W):
		for k in ov:
			if k.is_subspace(i):

# Now we will make the incidence structure
pts = range(len(ptslist))
blcks = []
for i in lns:
	b =[]
	for j in pts:
		if ptslist[j].is_subspace(i):
GQ = IncidenceStructure(pts, blcks)

# We get the points graph of GQ; vertices are pts and adjacent if they are in the same
# block. The code for this is above.
t2o = pointsGraph(GQ)

# Below is a list of graphs constructed from Latin Squares of order 7.
lsq = []

Here are some useful websites:

Sage installation

Strongly regular graphs:  Ted Spence’s catalogue of strongly regular graphs

From Brendan McKay:  nauty, combinatorial data

house of graphs