Note that there are some explanatory texts on larger screens.

plurals
  1. POSymPy - Arbitrary number of Symbols
    text
    copied!<p>I am coding a function that solves an arbitrary number of simultaneous equations. The number of equations is set by one of the parameters of the function and each equation is built from a number of symbols - as many symbols as there are equations. This means that I can't simply hardcode the equations, or even the symbols needed to put together the equations; the function needs to be able to handle any number of equations. So, my question is, how do I produce a list of symbols?</p> <p>I have one possible solution, but my gut tells me that it's not going to be very efficient. Please let me know if there is a better way of doing this. </p> <p>I'm new to SymPy and am still feeling my way about. As far as I can see, Symbols need to be defined with a string. Therefore, I can produce a series strings via appending an incrementing number to a letter (say 't0', 't1', etc), add them to a list and then create the symbols using those strings as parameters. Those symbols would themselves be stored in a list and would be used to produce the equations. </p> <pre><code>def solveEquations(numEquations): symbolNameList = [] symbolList = [] equationList = [] for i in range(numEquations): name = 't' + str(i) symbolNameList.append(name) symbolList.append(Symbol(name)) for i in range(numEquations): equation = 0 for sym in symbolList: equation += sym ** i # Or whatever structure the equation needs equationList.append(equation) #Then go on to solve the equations... </code></pre> <p>Is this the best way of doing this, or is there a more efficient approach?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload