Click here to Skip to main content
15,889,216 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.
Posted
Comments
Richard MacCutchan 3-Aug-18 4:14am    
The problem is nothing to do with Python. You need first to work out the correct algorithm for the problem. Once you have that written down you can convert the steps of the algorithm into code.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900