Thanks!
The automatic subscript is just a minor irritation, and your response is 
helpful.
I have included a new test program that hopefully no longer oversimplifies 
the situation. Currently, I have code (2000 lines) for investigating a very 
complex series, and it runs to a maximum of 3 terms of the series, 
requiring index values up to 6. I showed the results to the mathematics 
professor who has mentored my work and he immediately says that it is 
valuable and I should publish a version that can go up to a very large 
number of terms. In order to do that, I need to produce a fully automated 
version with a variable number of terms and indices.
So, for variable j, I want to use a_j in a formula.
The expression exp on line 20 does that, where I use get_a(1) to represent 
aj for variable j, where j happens to be 1.
In line 5, the return is part of a function. In line 8, I expect return in 
exec ('return...') to be a part of a function.

maxIndex = 2
import sympy
from sympy import symbols, latex
def get_a1():
    return a1
def get_a2():
    strSym = 'return a2'
    exec(strSym)
def get_a (j):
    strSym = 'return a' + str(j)
    exec(strSym)
lam = symbols('\\lambda')
for ind in range(1,maxIndex+1):
    strSym = 'a' + str(ind) + ' = symbols(\'a_' + str(ind) + '\')'
    exec(strSym)
#a1 = symbols('a_1')
#a2 = symbols('a_2')
print(latex(get_a1()))
#print(latex(get_a2())) # causes 'return' outside function (<string>, line 
1)
exp = 3*get_a(1) + 5*get_a(2)*lam**2 # causes 'return' outside function 
(<string>, line 1)
print(latex(exp))
print('end testSym')

On Tuesday, November 3, 2020 at 11:29:00 PM UTC+1 [email protected] wrote:

> I'm unclear what you're trying to achieve with the functions. exec()
> takes a string of Python code and executes it. The entire string must
> be valid Python code by itself, so exec('return x') fails because a
> bare "return x" is not valid Python. "return" must be inside a
> function definition to be valid. But even defSym1 doesn't do anything
> useful beyond just returning Sym1, so there's no point to having it
> instead of just "Sym1" directly.
>
> The LaTeX version of Sym1 contains _ because SymPy automatically
> assumes that symbol names ending in numbers are subscripted, so it
> renders it as a Sym_1 in LaTeX. If you don't want the 1 to be
> subscripted, you can use something like Symbol("{Sym1}").
>
> Aaron Meurer
>
> On Tue, Nov 3, 2020 at 9:42 AM Thomas Ligon <[email protected]> wrote:
> >
> > The following code is not working as I expected.
> > Why does Sym1 contain an underline (_{1})?
> > Why does return fail (unhandled exception) in line 7?
> >
> > Code:
> > import sympy
> > from sympy import symbols, latex
> > def defSym1():
> > return Sym1
> > def defSym2():
> > strSym = 'return Sym2'
> > exec(strSym)
> > Sym1 = symbols('Sym1')
> > Sym2 = symbols('Sym2')
> > print(latex(defSym1()))
> > print(latex(defSym2()))
> > print('end testSym')
> >
> > Output:
> > Sym_{1}
> > 'return' outside function (<string>, line 1)
> >
> >
> >
> >
> > --
> > 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/4a1df466-d3a3-4f7b-8a14-5e5a906f2662n%40googlegroups.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/baf74b5f-142f-4ea8-b997-ae9f58f139b4n%40googlegroups.com.

Reply via email to