Ask Your Question

Revision history [back]

fidelbc's answer is better than this one (because it's much faster), but here's more of a brute force technique, since it iterates through all of the elements of the matrix space M:

sage: M = MatrixSpace(GF(5), 3, 2)
sage: S = [GF(5)(_) for _ in 2,4]  # S = {2,4}
sage: [a for a in M if set(a.list()).issubset(S)]

Note the speed difference:

sage: timeit('[a for a in M if set(a.list()).issubset(S)]')
5 loops, best of 3: 5.43 s per loop

versus

sage: from itertools import product
sage: m = []
sage: timeit('for entries in product(S, repeat=6): m.append(M(entries))')
125 loops, best of 3: 1.91 ms per loop