# How to write the given program in sage

I am new to sage. I have learned the basics of sage programming from various online notes.

I am interested to apply sage in linear algebra and graph theory.

My problem is I want to draw a graph as follows:

Vertices : Subspaces of a vector space of dimension $n$.

Edges: $V_1V_2\iff V_1 \subset V_2$ or $V_2\subset V_1$.

Please don't give me the solution.

Please help me how to write it step by step or give some resources from where I can find this.

edit retag close merge delete

Sort by » oldest newest most voted

This stackoverflow answer by John Palmieri has some very relevant information. In particular, from the first code block in John Palmieri's answer we easily see (1) how to define in SageMath a vector space $\mathbf{F}^n$ over $\mathbf{F}=GF(p^k)$:

 p = 3
k = 2
K = GF(p^k, 'a')
n = 3
V = K^n


and (2) how to get lists of subspaces of various dimensions, e.g.:

 m = 1
Vm = list(V.subspaces(m))


After running this code, V will be a SageMath object representing a vector space of dimension 3 over the finite field $GF(3^2)$, and Vm will be a list of the subspaces of V of dimension m=1 (with 91 elements).

For reference, please see this documentation page.

Now let us compute a list of all subspaces of V, as this will correspond to your set of vertices. You might do this by writing a list comprehension involving the dimension m in V.subspaces(m), e.g.

 subspacesbydimension = [list(V.subspaces(m) for m in range(n+1)]


You can check that this is a list of 4 lists where the lengths of each of the sublists are 1, 91, 91, and 1. To make a list of vertices, we flatten this list:

 vertices = flatten(subspacesbydimension)


You now want to generate a list of pairs of adjacent vertices. We can follow John Palmieri's second code block (except that we use the function .is_subspace() to check adjacency and we make a few changes so that it works):

 d = {}
for x,X in enumerate(vertices):
L = []
for y,Y in enumerate(vertices):
if x != y and (X.is_subspace(Y) or Y.is_subspace(X)):
L.append(y)
d[x] = L


Now generate the graph:

 G = Graph(d)


Hopefully that's more or less what you're after.

more

Note that if you work on the reals, your graph will have uncountably many vertices, and Sage is not able to handle that. So, the first thing could be to precise on which field are your vector spaces defined.

more