Another option here is numbered_symbols(), which returns a generator
which produces any number of numbered symbols. It's also useful in
conjunction with the take() function:
>>> gen = numbered_symbols('a')
>>> n = 10
>>> take(gen, n)
[a0, a1, a2, a3, a4, a5, a6, a7, a8, a9]
In general, if you want to create symbols in complicated ways that
doesn't match the convenience methods of symbols(), you may be better
of generating them manually using the Symbol constructor and a list
comprehension or an explicit for loop:
>>> [Symbol("a%d" % i) for i in range(n)]
[a0, a1, a2, a3, a4, a5, a6, a7, a8, a9]
Aaron Meurer
On Thu, Feb 11, 2021 at 5:22 AM Oscar Benjamin
<[email protected]> wrote:
>
> On Thu, 11 Feb 2021 at 11:19, Thomas Ligon <[email protected]> wrote:
> >
> > "The solution to this in Python is to use lists or tuples or some other
> > container rather than raw variables. For example:
> >
> > x = symbols('x:10')"
> >
> > Based on that, and
> > Core — SymPy 1.7.1 documentation
> >
> > a = symbols('a:2*maxIter+1')
> >
> > The result is
> >
> > (a0*maxIter+1, a1*maxIter+1)
> >
> > The problem is that symbols does not support variables after the colon,
> > defeating the whole purpose of using indexed symbols to begin with.
>
> You can use the features of the Python language to generate the string
> as input to the symbols function:
>
> >>> num_symbols = 10
>
> This is using %-formatting of strings:
>
> >>> 'x:%d' % num_symbols
> 'x:10'
> >>> symbols('x:%d' % num_symbols)
> (x0, x1, x2, x3, x4, x5, x6, x7, x8, x9)
>
> This is using f-strings:
>
> >>> f'x:{num_symbols}'
> 'x:10'
> >>> symbols(f'x:{num_symbols}')
> (x0, x1, x2, x3, x4, x5, x6, x7, x8, x9)
>
> > I am trying to use indexed symbols.
>
> Note that "indexed symbols" means something different in sympy e.g.:
>
> >>> from sympy import IndexedBase
> >>> x = IndexedBase('x')
> >>> x
> x
> >>> x[0]
> x[0]
>
> IndexedBase represents a mathematical quantity that can be subscripted
> but in a symbolic way. For example if I have some data points that I
> refer to as x_1, x_2, etc. and I want to refer to x_i then I can use
> IndexedBase and index it with a symbol.
>
> Oscar
>
> --
> You received this message because you are subscribed to the Google Groups
> "sympy" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/sympy/CAHVvXxRCL%3D2cQKoGTJjUos6onXEWAujitgDwZaY3Ohhn2ONmvA%40mail.gmail.com.
--
You received this message because you are subscribed to the Google Groups
"sympy" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/sympy/CAKgW%3D6Kg5zDtkpvn8uEcTYZAFOj2-LFgwjg9xww-OEfHBcZnOA%40mail.gmail.com.