Reputation: 587
Is there a way that I can solve a system of linear equations over the field F2(i.e addition and multiplication modulo 2 - the binary field) using python? I've been trying to search for a useful package for a while but hadn't come up with anything...
Thanks,
Upvotes: 8
Views: 2233
Reputation: 580
I created a Python package galois that extends NumPy arrays over finite fields. It also supports NumPy linear algebra routines in np.linalg
.
Here is an example solving a linear system Ax = b
for x
in GF(2)
.
In [1]: import numpy as np
In [2]: import galois
In [3]: GF = galois.GF(2)
In [4]: A = GF.Random((4,4)); A
Out[4]:
GF([[0, 1, 0, 0],
[0, 0, 1, 1],
[0, 0, 0, 1],
[1, 1, 0, 0]], order=2)
In [5]: x_truth = GF([1,0,1,1]); x_truth
Out[5]: GF([1, 0, 1, 1], order=2)
In [6]: b = A @ x_truth; b
Out[6]: GF([0, 0, 1, 1], order=2)
# Solve Ax = b for x
In [7]: x = np.linalg.solve(A, b); x
Out[7]: GF([1, 0, 1, 1], order=2)
# Verify that x is x_truth
In [8]: np.array_equal(x, x_truth)
Out[8]: True
Upvotes: 2