Is there a simply way of solving large arrays of polynomial equations in Python?
I have to solve 16 equations that look like:
W1*(P1^0) + W2*(P2^0) + ... + W8*(P8^0) = 2/1
W1*(P1^1) + W2*(P2^1) + ... + W8*(P8^1) = 0
W1*(P1^2) + W2*(P2^2) + ... + W8*(P8^2) = 2/3
...
W1*(P1^14) + W2*(P2^14) + ... + W8*(P8^14) = 2/15
W1*(P1^15) + W2*(P2^15) + ... + W8*(P8^15) = 0
My idea was to divide this into 2 arrays, J (left part) and I (right side). With a for loop I managed to make an array of 2/(n+1) if n is an even number, with n starting at 0. For the left part I generated an array with indexing Wi and Pi.
I've tried several different solvers, but i'm not managing to succeed with any.
Also, I'm willing to learn a better way of solving this problem if there are any ideas.
from sympy import *
from sympy.solvers.solveset import linsolve
init_printing(use_unicode=True)
#definição dos
w1, w2, w3, w4, w5, w6, w7, w8 = symbols('w1:9')
p1, p2, p3, p4, p5, p6, p7, p8 = symbols('p1:9')
#vetor de coeficientes do polinómio I de n=15
I=[]
for i in range(16):
if i%2==0:
I.append(2/(i+1))
else:
I.append('0')
#vetor de coeficientes do polinómio J de n=15
J=[]
W = IndexedBase('W')
P = IndexedBase('P')
i = symbols('i', cls=Idx)
s=0
J=[]
for n in range(16):
for i in range(1,9):
s += W[i]*P[i]**n
J.append(s)
s=0
lst_PW = []
for i in range(1,9):
lst_PW.append(W[i])
lst_PW.append(P[i])
solve(J-I,lst_PW)