如何在Python中使用$ \\\\\\\\\\\\\\\\\\\\ [英] How to solving a system of linear equations over $\mathbf{Z}_p$ in Python
问题描述
让我们考虑一个线性方程组:
$$
a_0 + a_1 + a_2 + a_3 = 5 >
a_0 + 2a_1 + 4a_2 + 8a_3 = 14
a_0 + 3a_1 + 9a_2 + 27a_3 = 33
a_0 + 4a_1 + 16a_2 + 64a_3 = 5
$$
我想在整数模$ p $上解决上面的方程组,其中$ p $是素数。
我试图通过使用下面的代码在python中解决这个方程组,但它不符合我的目的。这是代码:
a = np.array([1,1,1,1],[1,2,4,8],[1,3 ,9,27],[1,4,16,64]])
b = np.array([5,14,33,68])
x = np .linalg.solve(a,b)
print(x)
答案错误。我和Sage解决了。它给出正确的答案:这是代码:
R = IntegerModRing(19)
M =矩阵(R,[[1, 1,1,1],[1,2,4,8],[1,3,9,27],[1,4,16,64]])
b = vector(R ,[5,14,33,68])
M.solve_right(b)
答案:(0,5,18, 1)
如何用Python做同样的事情?
我尝试了什么:
我试图通过使用以下代码在python中解决这个方程组,但它没有为我的目的服务。这是代码:
a = np.array([1,1,1,1],[1,2,4,8],[1,3 ,9,27],[1,4,16,64]])
b = np.array([5,14,33,68])
x = np .linalg.solve(a,b)
print(x)
答案错误。
Let us consider a system of linear equations :
$$
a_0+a_1+a_2+a_3=5
a_0+2a_1+4a_2+8a_3=14
a_0+3a_1+9a_2+27a_3=33
a_0+4a_1+16a_2+64a_3=5
$$
I want to solve this above system of equations over the integer modulo $p$, where $p$ is a prime number.
I have tried to solve this system of equations in python by using the following code, but it does not serve my purpose. Here is the code :
a = np.array([1,1,1,1],[1,2,4,8],[1,3,9,27],[1,4,16,64]])
b = np.array([5,14,33,68])
x = np.linalg.solve(a, b)
print(x)
It gives wrong answer. I have solved with Sage. It gives right answer : here is the code :
R = IntegerModRing(19)
M = Matrix(R, [[1,1,1,1],[1,2,4,8],[1,3,9,27],[1,4,16,64]])
b = vector(R, [5,14,33,68])
M.solve_right(b)
Answer : (0, 5, 18, 1)
How can I do the same in Python?
What I have tried:
I have tried to solve this system of equations in python by using the following code, but it does not serve my purpose. Here is the code :
a = np.array([1,1,1,1],[1,2,4,8],[1,3,9,27],[1,4,16,64]])
b = np.array([5,14,33,68])
x = np.linalg.solve(a, b)
print(x)
It gives wrong answer.
推荐答案
a_0 + a_1 + a_2 + a_3 = 5
a_0 + 2a_1 + 4a_2 + 8a_3 = 14
a_0 + 3a_1 + 9a_2 + 27a_3 = 33
a_0 + 4a_1 + 16a_2 + 64a_3 = 5
a_0+a_1+a_2+a_3=5
a_0+2a_1+4a_2+8a_3=14
a_0+3a_1+9a_2+27a_3=33
a_0+4a_1+16a_2+64a_3=5
我想在整数模上解决上面的方程组
I want to solve this above system of equations over the integer modulo
p
这篇关于如何在Python中使用$ \\\\\\\\\\\\\\\\\\\\的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!