0

I am trying to figure out an efficient way for solving systems of equations in Sympy "automatically". Let me exemplify, this is a standard approach to formulating the code

n = 3  
y = sp.symbols('y1:{}'.format(n + 1))
TempDict1=(sp.solve([Dem_s[0],Dem_s[1]],(y[0],y[1])))

I want to make it iterable, so the system of equations update itself with regards to how many equations and variables exists in the lists of Dem_s and y respectively.

I have tried the following:

Templist=[]
for i in range(n-1):
    Templist.append(y[i])

TempDict1={}
for i in range(n-1):
    TempDict1=sp.solve([Dem_s[i]], (Templist))

However, this will not solve it. Do you have any suggestions?

Thanks in advance.

1 Answers1

0

The first snippet would be better expressed as

n = 3  
y = sp.symbols('y1:{}'.format(n + 1))
TempDict1 = sp.solve(Dem_s, y)

If you have a tuple of variables y, there is no need to unpack and repack it with (y[0], y[1]). Passing y should do exactly what you want.

You didn't show how Dem_s is formed, but as long as it's a list or tuple of SymPy expressions, the above will work.