# Iterate over all homogeneous polynomials of a certain degree

I'd like to iterate over all cubic equations in four variables over a (small) finite field and count their rational points. What is the easiest way to iterate over all homogeneous polynomials of a certain degree?

edit retag close merge delete

Sort by » oldest newest most voted Define a polynomial ring and the desired degree of your homogeneous polynomials:

R.<x,y,z,w> = PolynomialRing(GF(2))
degree = 3


A monomial should be a product of degree many variables (where some can coincide):

from itertools import combinations_with_replacement
monomials = [prod(c) for c in combinations_with_replacement(R.gens(), degree)]


Choose coefficients in all possible ways:

from itertools import product
polynomials = [sum(c*m for (c,m) in zip(coeffs, monomials)) for coeffs in product(R.base_ring(), repeat=len(monomials))]


Note not all of these give meaningfully different equations, e.g. $F=0$ is equivalent to $cF=0$ for $c\neq 0$, and many are related by coordinate changes.

You can define $\mathbb{P}^3$ as

P3 = ProjectiveSpace(3, R.base_ring(), names=R.gens())


so you can do e.g.

sage: P3.subscheme(polynomials).point_set()
Set of rational points of Closed subscheme of Projective Space of dimension 3 over Finite Field of size 2 defined by:
y^2 + y*z + z*w

more