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 with two operations, and , such that for all in :
| Property | Addition | Multiplication |
|---|---|---|
| associative | ||
| commutative | ||
| identity | , and | |
| inverses | every has with | every has with |
| distributive |
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 |
|---|---|---|
| (fractions) | yes | the inverse of is |
| (real numbers) | yes | |
| (complex numbers) | yes | |
| (integers) | no | 2 has no integer inverse: has no integer solution |
| mod a prime | yes | see below |
| no | , and 2 has no inverse | |
| floating-point numbers | not exactly | can happen because of rounding |
Finite fields
For a prime , the integers modulo , with addition and multiplication followed by taking the remainder, form a field, written or . (Modular arithmetic itself is introduced in Modular Arithmetic.) Why is every non-zero invertible? Because when is prime, so by Bézout there are integers with , and then . Python computes this inverse with pow(a, -1, p):
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: , so "" is right in .
Gaussian elimination over
The elimination algorithm needs nothing but field operations, so it runs unchanged mod . Only division is replaced by multiplication with the modular inverse:
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 satisfies all three equations modulo 7 (for example ). Over the same system has the fractional solution , which is a different object: in , does not work. The field matters.
Note: The same coefficients can behave differently in different fields. A matrix can be invertible over but singular mod , when divides its determinant. You will compute with matrices mod in Matrix Arithmetic Modulo p.
Complex numbers
Over everything works too, and SymPy handles the arithmetic with I (the imaginary unit , ). Complex fields become essential for eigenvalues, see Complex Eigenvalues and Eigenvectors.
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
- , just 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 (error-correcting codes).
- AES encryption works in , a field with 256 elements (built from polynomials, not integers mod 256). Elliptic-curve cryptography uses huge prime fields .
- 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) , so 3 has no inverse. (d) no 0 and no additive inverses.
Exercise 2: Inverses mod 11
Find and in , then compute in .
Answer
, , .
Solution
print(pow(3, -1, 11), pow(7, -1, 11), 7 * pow(3, -1, 11) % 11)4 8 6
Exercise 3: A system in
Solve , over .
Answer
, .
Solution
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
Over , , has exactly one solution. How many solutions does it have over ?
Answer
Five: , , , , .
Solution
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 to (code)
The lesson's system has the rational solution . Reduce each fraction to . What do you get, and why does it match the solution?
Answer
, the solution.
Solution
Reduction mod 7 respects , , 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.
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 not a field?
- They are infinite
- Most integers have no multiplicative inverse in
- Addition is not commutative
- They contain negative numbers
What is in ?
- 4
- 2
- 3
- 5
For which is the set of integers mod a field?
- Every
- Even
- Prime
- Powers of 2