SymPy - Arbitrary number of Symbols
The symbols
function can be used to easily generate lists of symbols
In [1]: symbols('a0:3')Out[1]: (a₀, a₁, a₂)In [2]: numEquations = 15In [3]: symbols('a0:%d'%numEquations)Out[3]: (a₀, a₁, a₂, a₃, a₄, a₅, a₆, a₇, a₈, a₉, a₁₀, a₁₁, a₁₂, a₁₃, a₁₄)
numbered_symbols("t")
will return a generator that generates t0
, t1
, t2
, etc. You can use the start
parameter to choose a different starting value. And if you want to use dummy variables, use numbered_symbols("t", cls=Dummy)
.
Don't know if add any more useful information to the topic, but I use the following method to create a list of symbolic variables:
x = [sympy.symbols('x%d' % i) for i in range(3)]
And then I can use it normally in an equation:
eq = x[0]**2 + x[1]*2 + x[2]print(sympy.diff(eq,x[0]))>>> 2*x0