Debasish Kanhar
Debasish Kanhar

Reputation: 1173

Sympy solver to solve under-determined set of equations

Okay I basically want to solve a set of under-determined equations. I've around 289 variables and 288 equations.

I followed the following link to create my solver program to solver the under-determined equations.

Since I've 289 variables and nearly as many equations, manually writing the equations wasn't possible, I introduced a loop which saves equations and Sym variables in arrays and returns that which is passed to solve() function.

Code :

def getEqn(A, B):
    for i in range(len(A)):
        A[i] = Symbol('A['+str(i)+']')

    equations = [None]*(len(predictions)-1)
    for i in range(len(equations)-1):
        equations[i] = Eq(A[i]-A[i+1], B[i])

    return equations, A
def solver(predictions):
    lenPredictions = len(predictions)
    A = [None]*lenPredictions
    for i in range(lenPredictions):
        A[i] = Symbol('A['+str(i)+']')
    equations, variables = getEqn(A, predictions)
    for i in range(lenPredictions-1):
        res = solve(equations, variables)
    return res
def main():
    res = solver(predictions)

When I try running the following code, I get following error: enter image description here

Note : The whole program is running fine without any error. Its only these following functions which is throwing error. I'm completely new to Python & Sympy also. Any guidance would be really helpful as I'm unable to know where I'm missing something.

Upvotes: 0

Views: 381

Answers (1)

jcfollower
jcfollower

Reputation: 3158

in getEqn() you have ...

equations = [None]*(len(predictions)-1)

and then ...

for i in range(len(equations)-1):
    equations[i] = Eq(A[i]-A[i+1], B[i])

That means your last equation won't be getting a value but will still be None, because of your -1 in the range.

I think you want ...

equations = [None]*(len(predictions))
for i in range(len(equations)):

Upvotes: 1

Related Questions