There are a few options:
>>> sp.symbols('b_0:10')
(b_0, b_1, b_2, b_3, b_4, b_5, b_6, b_7, b_8, b_9)
or, using a formatted string,
>>> n = 10
>>> sp.symbols('b_0:{}'.format(n))
(b_0, b_1, b_2, b_3, b_4, b_5, b_6, b_7, b_8, b_9)
These return a tuple of symbols. There are more formatting options: see symbols
docs.
There is also a function to generate a NumPy array of symbols:
>>> sp.symarray('b', 10)
array([b_0, b_1, b_2, b_3, b_4, b_5, b_6, b_7, b_8, b_9], dtype=object)
All of these examples are meant to be assigned to something. For example, b = sp.symbols('b_0:10')
assigns the tuple of symbols to b, so they can be accessed as b[0], b[1]
, etc. SymPy symbols are not accessed by the string representing them, such as "b_0" or "b_1"
.
Finally, there are Indexed objects in case you need an array of symbols of undetermined size: Indexed objects are created on the fly as you use A[n]
notation with A
being an IndexedBase.