### How to define a polynomial function over a nonprime finite field

What is the correct way to construct polynomial functions over nonprime finite fields?

Here is the code I have come up with.

```
p = 3
q = 3
K = GF(p**q, 'c')
PR = PolynomialRing(K, 'x')
x = PR.gen()
# return the trace polynomial for Tr_{F_{ground}/F_{ground^exp}}
def get_trace_poly(ground, exp):
P = sum([x**((ground**i)) for i in range(0,exp)])
return P
# P = x^9 + x^3 + x
P = get_trace_poly(p,q)
e = [P(i) for i in range(0,p**q)]
print(e)
```

Why is it treating P as a polynomial GF(3)->GF(3) instead of GF(27)->GF(27)?

Also, is there something wrong with my understanding of the trace function? It seems to be true that the trace of GF(27) over GF(3) is supposed to be Tr(x)= x^9 + x^3 + x, but then Tr(1)=3 which isn't an element of GF(3).