TExcelTAD

Fields

Gaussian elimination only ever adds, subtracts, multiplies and divides by non-zero numbers. Any number system where those four operations behave the usual way is called a field, and all of linear algebra works over it. Besides the familiar rationals, reals and complex numbers, that includes the finite fields used in cryptography and error-correcting codes.

The definition

A field is a set FF with two operations, ++ and ⋅\cdot, such that for all a,b,ca, b, c in FF:

Property Addition Multiplication
associative (a+b)+c=a+(b+c)(a + b) + c = a + (b + c) (a⋅b)⋅c=a⋅(b⋅c)(a \cdot b) \cdot c = a \cdot (b \cdot c)
commutative a+b=b+aa + b = b + a a⋅b=b⋅aa \cdot b = b \cdot a
identity a+0=aa + 0 = a a⋅1=aa \cdot 1 = a, and 1≠01 \ne 0
inverses every aa has −a-a with a+(−a)=0a + (-a) = 0 every a≠0a \ne 0 has a−1a^{-1} with a⋅a−1=1a \cdot a^{-1} = 1
distributive a⋅(b+c)=a⋅b+a⋅ca \cdot (b + c) = a \cdot b + a \cdot c

The last line in the inverses row is the important one: in a field you can divide by anything except 0.

Examples and non-examples

Set Field? Why
Q\mathbb{Q} (fractions) yes the inverse of pq≠0\frac{p}{q} \ne 0 is qp\frac{q}{p}
R\mathbb{R} (real numbers) yes
C\mathbb{C} (complex numbers) yes (a+bi)−1=a−bia2+b2(a + bi)^{-1} = \frac{a - bi}{a^2 + b^2}
Z\mathbb{Z} (integers) no 2 has no integer inverse: 2⋅x=12 \cdot x = 1 has no integer solution
GF(p)={0,1,…,p−1}\mathrm{GF}(p) = \{0, 1, \ldots, p - 1\} mod a prime pp yes see below
Z6=integers mod 6\mathbb{Z}_6 = \text{integers mod } 6 no 2⋅3=0 mod 62 \cdot 3 = 0 \bmod 6, and 2 has no inverse
floating-point numbers not exactly (a+b)+c≠a+(b+c)(a + b) + c \ne a + (b + c) can happen because of rounding

Finite fields GF(p)\mathrm{GF}(p)

For a prime pp, the integers modulo pp, with addition and multiplication followed by taking the remainder, form a field, written GF(p)\mathrm{GF}(p) or Zp\mathbb{Z}_p. (Modular arithmetic itself is introduced in Modular Arithmetic.) Why is every non-zero aa invertible? Because gcd⁡(a,p)=1\gcd(a, p) = 1 when pp is prime, so by Bézout there are integers u,vu, v with u⋅a+v⋅p=1u \cdot a + v \cdot p = 1, and then u⋅a≡1(modp)u \cdot a \equiv 1 \pmod{p}. Python computes this inverse with pow(a, -1, p):

python · try it yourself
p = 7
print("inverses mod 7:")
for a in range(1, p):
    inv = pow(a, -1, p)
    print(f"  {a} * {inv} = {a * inv} ≡ {a * inv % p}")

print("2 - 5 mod 7 =", (2 - 5) % p)
print("3 / 5 mod 7 =", 3 * pow(5, -1, p) % p)
try:
    pow(2, -1, 6)
except ValueError as e:
    print("mod 6:", e)
inverses mod 7:
  1 * 1 = 1 ≡ 1
  2 * 4 = 8 ≡ 1
  3 * 5 = 15 ≡ 1
  4 * 2 = 8 ≡ 1
  5 * 3 = 15 ≡ 1
  6 * 6 = 36 ≡ 1
2 - 5 mod 7 = 4
3 / 5 mod 7 = 2
mod 6: base is not invertible for the given modulus

Check: 5⋅2=10≡3(mod7)5 \cdot 2 = 10 \equiv 3 \pmod{7}, so "3/5=23 / 5 = 2" is right in GF(7)\mathrm{GF}(7).

Gaussian elimination over GF(p)\mathrm{GF}(p)

The elimination algorithm needs nothing but field operations, so it runs unchanged mod pp. Only division is replaced by multiplication with the modular inverse:

python · try it yourself
def rref_mod(M, p):
    M = [[a % p for a in row] for row in M]
    rows, cols, r = len(M), len(M[0]), 0
    for c in range(cols):
        piv = next((i for i in range(r, rows) if M[i][c]), None)
        if piv is None:
            continue
        M[r], M[piv] = M[piv], M[r]
        inv = pow(M[r][c], -1, p)
        M[r] = [a * inv % p for a in M[r]]
        for i in range(rows):
            if i != r and M[i][c]:
                f = M[i][c]
                M[i] = [(a - f * b) % p for a, b in zip(M[i], M[r])]
        r += 1
        if r == rows:
            break
    return M

# x + 2y + 3z = 1,  4x + 5y + 6z = 0,  2x + y + z = 3   over GF(7)
aug = [[1, 2, 3, 1], [4, 5, 6, 0], [2, 1, 1, 3]]
R = rref_mod(aug, 7)
for row in R:
    print(row)
x = [row[-1] for row in R]
print("check:", [sum(a * v for a, v in zip(row[:3], x)) % 7 for row in aug])
[1, 0, 0, 1]
[0, 1, 0, 3]
[0, 0, 1, 5]
check: [1, 0, 3]

The solution (1,3,5)(1, 3, 5) satisfies all three equations modulo 7 (for example 4⋅1+5⋅3+6⋅5=49≡04 \cdot 1 + 5 \cdot 3 + 6 \cdot 5 = 49 \equiv 0). Over Q\mathbb{Q} the same system has the fractional solution (103,−263,5)(\frac{10}{3}, -\frac{26}{3}, 5), which is a different object: in R\mathbb{R}, x=1x = 1 does not work. The field matters.

Note: The same coefficients can behave differently in different fields. A matrix can be invertible over Q\mathbb{Q} but singular mod pp, when pp divides its determinant. You will compute with matrices mod pp in Matrix Arithmetic Modulo p.

Complex numbers

Over C\mathbb{C} everything works too, and SymPy handles the arithmetic with I (the imaginary unit ii, i2=−1i^2 = -1). Complex fields become essential for eigenvalues, see Complex Eigenvalues and Eigenvectors.

python · try it yourself
from sympy import Matrix, I, simplify

A = Matrix([[1, I], [2, 1 + I]])
b = Matrix([1, 0])
x = A.solve(b)
print([simplify(v) for v in x])
print("check:", [simplify(v) for v in A * x])
[I, -1 - I]
check: [1, 0]

In software

  • GF(2)\mathrm{GF}(2), just {0,1}\{0, 1\} with XOR as addition and AND as multiplication, is the field of bits. Parity checks, CRCs, Hamming codes and QR-code error correction are linear algebra over GF(2)\mathrm{GF}(2) (error-correcting codes).
  • AES encryption works in GF(28)\mathrm{GF}(2^8), a field with 256 elements (built from polynomials, not integers mod 256). Elliptic-curve cryptography uses huge prime fields GF(p)\mathrm{GF}(p).
  • Floating point is not a field: addition is not associative, which is why numeric linear algebra worries about rounding and pivoting.

Exercises

Exercise 1: Field or not?

Which are fields? (a) the even integers, (b) the integers mod 11, (c) the integers mod 9, (d) the positive rationals.

Answer

Only (b).

Solution

(a) has no 1 and no inverses. (b) 11 is prime. (c) 3⋅3=9≡03 \cdot 3 = 9 \equiv 0, so 3 has no inverse. (d) no 0 and no additive inverses.

Exercise 2: Inverses mod 11

Find 3−13^{-1} and 7−17^{-1} in GF(11)\mathrm{GF}(11), then compute 7/37/3 in GF(11)\mathrm{GF}(11).

Answer

3−1=43^{-1} = 4, 7−1=87^{-1} = 8, 7/3=67/3 = 6.

Solution
3⋅4=12≡1→3−1=47⋅8=56=55+1≡1→7−1=87/3=7⋅4=28≡6check: 3⋅6=18≡7✓\begin{aligned} 3 \cdot 4 &= 12 \equiv 1 && \to 3^{-1} = 4 \\ 7 \cdot 8 &= 56 = 55 + 1 \equiv 1 && \to 7^{-1} = 8 \\ 7/3 &= 7 \cdot 4 = 28 \equiv 6 && \text{check: } 3 \cdot 6 = 18 \equiv 7 \checkmark \end{aligned}
python · try it yourself
print(pow(3, -1, 11), pow(7, -1, 11), 7 * pow(3, -1, 11) % 11)
4 8 6

Exercise 3: A 2×22 \times 2 system in GF(5)\mathrm{GF}(5)

Solve x+2y=3x + 2y = 3, 3x+4y=13x + 4y = 1 over GF(5)\mathrm{GF}(5).

Answer

x=0x = 0, y=4y = 4.

Solution
R2←R2−3R1:(4−6)y=1−9→−2y=−8→3y≡2(mod5)3−1=2 in GF(5)→y=2⋅2=4x=3−2y=3−8=−5≡0Check: 0+2⋅4=8≡3✓3⋅0+4⋅4=16≡1✓\begin{aligned} &R_2 \leftarrow R_2 - 3R_1\text{:} \quad (4 - 6)y = 1 - 9 \to -2y = -8 \to 3y \equiv 2 \pmod{5} \\ &3^{-1} = 2 \text{ in } \mathrm{GF}(5) \to y = 2 \cdot 2 = 4 \\ &x = 3 - 2y = 3 - 8 = -5 \equiv 0 \\ &\text{Check: } 0 + 2 \cdot 4 = 8 \equiv 3 \checkmark \qquad 3 \cdot 0 + 4 \cdot 4 = 16 \equiv 1 \checkmark \end{aligned}
python · try it yourself
print([(x, y) for x in range(5) for y in range(5)
       if (x + 2 * y) % 5 == 3 and (3 * x + 4 * y) % 5 == 1])
[(0, 4)]

Exercise 4: Singular only mod pp

Over Q\mathbb{Q}, x+2y=3x + 2y = 3, 3x+y=43x + y = 4 has exactly one solution. How many solutions does it have over GF(5)\mathrm{GF}(5)?

Answer

Five: (0,4)(0, 4), (1,1)(1, 1), (2,3)(2, 3), (3,0)(3, 0), (4,2)(4, 2).

Solution
det⁡=1⋅1−3⋅2=−5≡0(mod5)→not invertible in GF(5)R2←R2−3R1:(1−6)y=4−9→−5y=−5→0=0(mod5)So y=t is free and x=3−2t: one solution for each of the 5 values of t.(Over Q: −5y=−5 gives y=1, x=1.)\begin{aligned} &\det = 1 \cdot 1 - 3 \cdot 2 = -5 \equiv 0 \pmod{5} && \to \text{not invertible in } \mathrm{GF}(5) \\ &R_2 \leftarrow R_2 - 3R_1\text{:} \quad (1 - 6)y = 4 - 9 \to -5y = -5 \to 0 = 0 \pmod{5} \\ &\text{So } y = t \text{ is free and } x = 3 - 2t\text{: one solution for each of the } 5 \text{ values of } t. \\ &(\text{Over } \mathbb{Q}\text{: } -5y = -5 \text{ gives } y = 1,\ x = 1.) \end{aligned}
python · try it yourself
print([(x, y) for x in range(5) for y in range(5)
       if (x + 2 * y) % 5 == 3 and (3 * x + y) % 5 == 4])
[(0, 4), (1, 1), (2, 3), (3, 0), (4, 2)]

Exercise 5: From Q\mathbb{Q} to GF(7)\mathrm{GF}(7) (code)

The lesson's system has the rational solution (103,−263,5)(\frac{10}{3}, -\frac{26}{3}, 5). Reduce each fraction a/ba/b to a⋅b−1 mod 7a \cdot b^{-1} \bmod 7. What do you get, and why does it match the GF(7)\mathrm{GF}(7) solution?

Answer

(1,3,5)(1, 3, 5), the GF(7)\mathrm{GF}(7) solution.

Solution
103→10⋅5=50≡1(3−1=5 mod 7)−263→−26⋅5=−130≡35→5\begin{aligned} \frac{10}{3} &\to 10 \cdot 5 = 50 \equiv 1 && (3^{-1} = 5 \bmod 7) \\ -\frac{26}{3} &\to -26 \cdot 5 = -130 \equiv 3 \\ 5 &\to 5 \end{aligned}

Reduction mod 7 respects ++, −-, ⋅\cdot and division by numbers not divisible by 7. Elimination only divided by such numbers here (the determinant is −3, not a multiple of 7), so every step of the rational computation maps to a valid step mod 7.

python · try it yourself
from fractions import Fraction as F
sol = [F(10, 3), F(-26, 3), F(5)]
print([v.numerator * pow(v.denominator, -1, 7) % 7 for v in sol])
[1, 3, 5]

Test yourself

Why are the integers Z\mathbb{Z} not a field?

  1. They are infinite
  2. Most integers have no multiplicative inverse in Z\mathbb{Z}
  3. Addition is not commutative
  4. They contain negative numbers

What is 4−14^{-1} in GF(7)\mathrm{GF}(7)?

  1. 4
  2. 2
  3. 3
  4. 5

For which nn is the set of integers mod nn a field?

  1. Every n≥2n \ge 2
  2. Even nn
  3. Prime nn
  4. Powers of 2