Marcel Golay discovered two “perfect” error-correcting codes: one binary and one ternary. These two codes stick out in the classification of perfect codes [1].

The ternary code is a linear code over *GF*(3), the field with three elements. You can encode a list of 5 base-three digits by multiplying the list as a row vector by the following generator matrix on the right:

Here the arithmetic is carried out in *GF*(3): every multiplication and addition is carried out mod 3.

Suppose we want to write this up in Python. How can you tell NumPy that you want to do arithmetic mod 3? I don’t believe you can directly. However, you could just multiply your row vector by the matrix using ordinary arithmetic and reducing the result mod 3 at the end. This gives the same result as if all intermediate operations had been carried out mod 3.

For example, suppose we wanted to encode the list [1, 0, 1, 2, 2].

import numpy as np G = np.array([ [1, 0, 0, 0, 0, 1, 1, 1, 2, 2, 0], [0, 1, 0, 0, 0, 1, 1, 2, 1, 0, 2], [0, 0, 1, 0, 0, 1, 2, 1, 0, 1, 2], [0, 0, 0, 1, 0, 1, 2, 0, 1, 2, 1], [0, 0, 0, 0, 1, 1, 0, 2, 2, 1, 1], ]) v = np.array( [1, 0, 1, 2, 2] ) print ((v@G)%3)

The vector-matrix product `v@G`

contains integers that are larger than 3:

[1 0 1 2 2 6 7 6 8 9 6]

But when we reduce everything mod 3 we get

[1 0 1 2 2 0 1 0 2 0 0]

This doesn’t mean that linear algebra over a finite field is trivial, though it was trivial in this case.

The same trick would work if we were to work modulo any prime. So the analogous trick would work mod 7, for example.

However, the trick would not work for the field with 8 elements, for example, because arithmetic in this field is **not** simply arithmetic mod 8. (You can read why here.) So our trick works for *GF*(*p*) for any prime *p*, but not in *GF*(*p*^{k}) for any *k* > 1.

## Related posts

[1] From “Sphere Packings, Lattices and Groups” by J. H. Conway and N. J. A. Sloane:

Perfect codes were essentially classified by Tietäväinen and van Lint. The list is as follows.

(i) Certain trivial codes …

(ii) Hamming codes …

(iii) Nonlinear codes with the same parameters as Hamming codes …

(iv) The binary [23, 12, 7] binary Golay codeC_{23}and the ternary [11, 6, 5] Golay codeC_{11}.