I think Round as a shortcut for putting parentheses around something
is not implemented in the LaTeX printer. I've never seen that before,
but if it is common, we can add it. Otherwise, if you need custom
LaTeX in your symbol names you should just use it directly in the
symbol name.

Aaron Meurer

On Fri, Nov 6, 2020 at 1:15 PM Thomas Ligon <[email protected]> wrote:
>
> Thanks again for the very valuable help. I think I am getting very close to a 
> good solution, but I must still have a bug in there.
> First, just a small piece of background: I am using some symbols like
> Round1 = symbols('\\(1\\)')
> Round1Bar = symbols('\\Bar{\\(1\\)}')
> In other words, a "1" in parentheses and the same thing with a bar over it, 
> plus some using square brackets. These were defined and used in two papers I 
> am referring to, published in 1854 and 1896, both by famous mathematicians. 
> One version of my code used something like E_1 instead, but this time I chose 
> to stick to the old symbols, even though they are not typical today, since 
> they could be confused with normal parentheses.
> Now, I have working code that has a hard-coded limit on the number of symbols 
> and I need to upgrade it so that it can handle an undetermined (perhaps 
> large) number of symbols. When creating expressions, I am using functions 
> like this one:
>  def getRoundjBar (j):
>     strJ = str(abs(j))
>     if j < 0:
>         strJ = 'm' + strJ
>     strSym = 'Round' + strJ + 'Bar'
>     return parse_expr(strSym)
> and here is some test code showing it in action:
> ret1 = getRoundjBar(1)
> print(ret1)
> print(latex(ret1))
> I can see that it is important for me to understand the type of the variable 
> and, fortunately, the "Locals" window in my debugger shows that ret1 contains 
> the value Round1Bar and has type Symbol. Good, that makes almost everything 
> work well, but
> print(latex(ret1))
> produces
> \bar{Round1}
> where it should produce
> \\bar{\(1\)}
>
> On Wednesday, November 4, 2020 at 9:46:03 PM UTC+1 [email protected] wrote:
>>
>> You can't use exec() to do a return statement for a function that is
>> not also part of the string. What you want is eval(), which evaluates
>> an expression and returns it, like
>>
>> return eval(string)
>>
>> However, you should consider just using sympify() or parse_expr() if
>> you are evaluating strings to SymPy expressions, as they are designed
>> for that and will give you things like automatically defining the
>> symbols.
>>
>> Actually, for what you've shown, I don't see the need to use exec() or
>> eval() at all. You can just create the symbols from the strings, like
>> Symbol('a' + str(j)). Symbols are completely defined by name, so if
>> two symbols have the same name, they will be equal.
>>
>> Aaron Meurer
>>
>> On Wed, Nov 4, 2020 at 2:24 AM Thomas Ligon <[email protected]> wrote:
>> >
>> > Now I see a solution: forget the function and create the expression exp 
>> > using string manipulation and exec, something like
>> > strExp = 'complex string'
>> > strSym = 'exp = ' + strExp
>> > exec(strSym)
>> >
>> > On Wednesday, November 4, 2020 at 9:51:35 AM UTC+1 Thomas Ligon wrote:
>> >>
>> >> 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/00d00ac2-7b0c-4ac1-b81a-dcc9f7956572n%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/24d5bbe0-8388-4564-ad2f-10fa6412c94bn%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/CAKgW%3D6Lm3QwUDgZhZhq69TY-X8cNPW9VBHNL7U1a7JozRcMczA%40mail.gmail.com.

Reply via email to