【问题标题】:Solving a system of linear equations over the field F(2) with python用python求解域F(2)上的线性方程组
【发布时间】:2020-09-23 04:05:54
【问题描述】:

有没有一种方法可以使用 python 在 F2 域(即加法和乘法模 2 - 二进制域)上求解线性方程组? 我一直在尝试寻找一个有用的包,但没有找到任何东西......

谢谢,

【问题讨论】:

标签: python numpy linear-algebra finite-field


【解决方案1】:

我创建了一个 Python 包 galois,它在有限域上扩展了 NumPy 数组。它还支持np.linalg中的NumPy线性代数例程。

这是一个在GF(2) 中为x 求解线性系统Ax = b 的示例。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-13
    • 2020-04-05
    相关资源
    最近更新 更多