I'm trying to implement a gaussian algorithm that would work over Galois Field(3). I've already successfully implemented the algorithm over GF(2) but GF(3) seems a bit more tricky. My main problem is : when the value of the pivot line i've chosen is 2 (pl = 2) , how can i eliminate a 2 in the column ? My first idea would be to add pl/2 to the 2, but in GF(3), i'm not sure 2/2 = 1.
-
1In what language are you implementing this? Can you show some relevant code? See [mcve] – jtbandes Apr 17 '16 at 17:53
2 Answers
2/2 == 1 always, because 1 is the neutral element of multiplication.
In a finite field, though, it's not sure that 2 is the only divisor of 2 that leads to 1.
typically, just use multiplication instead of division to reach a 1; much easier!

- 34,677
- 4
- 53
- 94
-
If 2/2 ==1, i guess 0/2 in GF(3) is 0, but what would be 1/2 over GF(3) ? – FreeRide Apr 17 '16 at 18:01
-
-
-
also, obviously, 0/x == 0 for all x \in GF(2) \ \{0\} , because x*0 == 0 – Marcus Müller Apr 17 '16 at 18:05
-
If it's not defined, how can i eliminate a 2 if my pivot is 2 ? In gf(3), i'd need to add 1 to 2 in order to eliminate it. But if i divide 2 by 2, i have to divide the whole line by 2,which can contains 0 and 1, and i don't know what 0/2 and 1/2 are. Am i getting everything wrong ? – FreeRide Apr 17 '16 at 18:06
-
Ok i think i'm beginning to understad. So you're saying i have to multiply by 1/2 instead of dividing by 2 ? – FreeRide Apr 17 '16 at 18:18
-
-
I thought i was but i'm not sure anymore, GF(3) only contains 0,1,2 right ? So is it right that 1/2 = 0 ? – FreeRide Apr 17 '16 at 18:36
-
*sigh* no, you don't understand GF(3); GF(3) is a finite field, which means it's a *ring* w.r.t. multiplication. 1/2 = 2, because 2*2 = 1 in GF(3). Remember that division is the inverse of multiplication! You're pretty badly prepared for doing matrix operations on GF(3) if you're not aware of this. Please go and read some basic algebra. – Marcus Müller Apr 17 '16 at 18:39
-
Ok thanks a lot. I think i'm going to read some algebra indeed, but to sum up, in GF(3) : 0/2 = 0, 1/2 = 2 and 2/2 = 1 right ? – FreeRide Apr 17 '16 at 19:09
To eliminate the pivot a = 2
in the field GF(3)
, you need to find the multiplicative inverse of 2 in the Galois field, such that a * a_inv = 1
in GF(3)
. So to divide by a
, you will instead multiply by a_inv
(mod 3). It happens to be that a_inv = 2
, but not for the reason that 2/2 = 1
(that's incorrect). For example, in GF(5)
the inverse of 2 is 3, not 2.
The way to find the inverse of an element in a prime field GF(p)
is to use the Extended Euclidean Algorithm. The algorithm finds integers d, s, t
such that d = a*s + b*t = gcd(a, b)
. For prime fields GF(p)
to find the inverse of the element a
, you compute 1 = a*s + p*t = gcd(a, p)
and a_inv = s
. The GCD is always 1 because p
is prime.
I wrote a Python package galois that extends NumPy arrays over Galois fields. It also includes functions for computing the extended Euclidean algorithm, among many other things. Here are examples using galois
.
Manually find the inverse of 2 in GF(3)
.
In [1]: import galois
In [2]: a, p = 2, 3
In [3]: d, s, t = galois.egcd(a, p); d, s, t
Out[3]: (1, -1, 1)
In [4]: a_inv = s % p; a_inv
Out[4]: 2
In [5]: a * a_inv % p
Out[5]: 1
Manually find the inverse of 2 in GF(5)
.
In [6]: a, p = 2, 5
In [7]: d, s, t = galois.egcd(a, p); d, s, t
Out[7]: (1, -2, 1)
In [8]: a_inv = s % p; a_inv
Out[8]: 3
In [9]: a * a_inv % p
Out[9]: 1
Or you can use galois
to create the field GF(5)
and then compute the inverses directly, as the library intends.
In [10]: GF = galois.GF(5)
In [11]: GF(2)**-1
Out[11]: GF(3, order=5)
Linear algebra is also supported, including row reduction (Gaussian elimination), as you mentioned, and matrix inverses.
In [19]: import numpy as np
In [20]: GF = galois.GF(3)
In [21]: A = GF.Random((4,4)); A
Out[21]:
GF([[2, 2, 2, 2],
[0, 2, 1, 1],
[0, 1, 0, 1],
[2, 1, 2, 0]], order=3)
In [22]: A.row_reduce()
Out[22]:
GF([[1, 0, 0, 0],
[0, 1, 0, 0],
[0, 0, 1, 0],
[0, 0, 0, 1]], order=3)
In [23]: A_inv = np.linalg.inv(A); A_inv
Out[23]:
GF([[1, 2, 2, 1],
[2, 0, 2, 1],
[1, 1, 0, 2],
[1, 0, 2, 2]], order=3)
In [24]: A @ A_inv
Out[24]:
GF([[1, 0, 0, 0],
[0, 1, 0, 0],
[0, 0, 1, 0],
[0, 0, 0, 1]], order=3)

- 360
- 1
- 10