TITLE: Singular Procedure: Vanishing polynomial function (A Singular Introduction to Commutative Algebra)
QUESTION [6 upvotes]: I have recently started studying the computer algebra system Singular with help of the book „A Singular Introduction to Commutative Algebra“ by Gert-Martin Greuel & Gerhard Pfister. 
Since a few days now I‘m stuck on Exercise 1.1.13.
„Write a SINGULAR procedure, depending on two integers $p,d$, with $p$ prime, which returns all polynomials in $\mathbb{F}_p[X]$ of degree $d$ such that the corresponding polynomial function vanishes. Use the procedure to display all $f \in (\mathbb{Z}/5 \mathbb{Z})[X]$ of degree $\leq$ 6 such that $\tilde{f}=0$“ (Annotation: $\tilde{f}$ is the polynomial function).
If I had to write that function in Python/ C++, I would recursively define all polynomials of degree $d-p$ in $\mathbb{F}_p[X]$ and then multiply all of them with $X^p-X$. Unfortunately that algorithm did not work in Singular as expected. Since this is one of the first exercises of the book I think that there has to be any clue working with Singular functionalities to solve that exercise in a more convenient way. Working through the libraries and Singular functionalities in the book I can‘t find any suitable procedure or functionality that would be helpfull in this case. Either my algorithm is wrong or Singular is requiring a different solution.
Now I‘m requesting help from someone with Singular experience or anyone that studied the book mentioned above.

REPLY [2 votes]: Before anything else, beware that the desired procedure itself demands exorbitant amounts of runtime: For given values $p$ and $d$, if $d\geq p $ the output consists of $p^{d-p+1}$ polynomials over $\Bbb{F}_p$, requiring a total of
$$(d+1)p^{d-p+1},$$
elements of $\Bbb{F}_p$ to represent. As the size of the desired output is exponential in $d$, certainly the runtime will at least exponential.

As for an effective algorithm; as you note a polynomial with coefficients in $\Bbb{F}_p$ vanishes on $\Bbb{F}_p$ if and only if it is divisible by $X^p-X$. Polynomial long division shows that mod $X^p-X$ we have $X^i\equiv X^{i+(p-1)}$ for all $i\geq 1$, and so for an arbitrary polynomial $f=\sum_{i=0}^d c_iX^i\in\Bbb{F}_p[X]$
we have
$$f\equiv c_0+\sum_{i=1}^{p-1}\left(\sum_{j\geq0}c_{i+(p-1)j}\right)X^i\pmod{X^p-X}.$$
This shows that $f$ vanishes if and only if $c_0=0$ and $\sum_{j\geq0}c_{i+(p-1)j}=0$ for all $i\in\{1,\ldots,p-1\}$.
In particular, if $d\leq p-1$ then this shows that $f\equiv0$, so there are no such polynomials for $d<p$, except perhaps $d=0$ if you consider the zero polynomial to have degree $0$. If $d\geq p$ then the constraints on the coefficients are equivalent to
$$c_0=0\qquad\text{ and }\qquad c_i=-\sum_{j\geq1}c_{i+(p-1)j}\quad\text{ for all }\quad i\in\{1,\ldots,p-1\}.$$
In other words, the first $p$ coefficients of $f$ are uniquely determined by the remaining coefficients of $f$, and there are no constraints on the remaining coefficients of $f$. So every choice of coefficients $c_p,c_{p+1},\ldots,c_d\in\Bbb{F}_p$ (with $c_d\neq0$) yields a unique polynomail of degree $d$ that vanishes on $\Bbb{F}_p$. This yields the following algorithm:

For $c_p,c_{p+1},\ldots,c_{d-1}$ in $\Bbb{F}_p$ and $c_d\in\Bbb{F}_p^{\times}$:
$\qquad$For $i$ in $\{1,\ldots,p-1\}$
$\qquad\qquad$Set $c_i:=-\sum_{j\geq1}c_{i+(p-1)j}$.
$\qquad$Print $f:=\sum_{i=1}^dc_iX^i$.

This requires $d+2-2p$ additions per polynomial, so the runtime is a linear in the output.

Another way to reach the same construction is as follows:

Take an arbitrary polynomial $f\in\Bbb{F}_p[X]$ of degree $d$.
Compute the unique $g\in\Bbb{F}_p[X]$ with $\deg g<p$ such that $g\equiv f\pmod{X^p-X}$.
Output $f-g$.

The computation in step $2$ is a matter of polynomial long division. Then
$$f=(X^p-X)h+g,$$
which shows that $f-g$ vanishes on $\Bbb{F}_p$. Of course every polynomial of degree $d$ that vanishes on $\Bbb{F}_p$ arises in this way, because if $f\in\Bbb{F}_p[X]$ vanishes on $\Bbb{F}_p$ then $g\equiv0$.