On Sun, Apr 26, 2015 at 12:27 AM, Loïc Gouarin <loic.g...@gmail.com> wrote:
> Hi,
>
> I would like to parse a string expression like that
>
> from sympy.parsing.sympy_parser import parse_expr
> parse_expr("m[0][0]")
>
> But I have an error with 'Symbol' object does not support indexing.
>
> If I do
>
> sympy.Symbol("m[0][0]")
>
> everything is ok.
>
> I try to write my own transformation funtion that takes the "m[x][y]" and
> creates a Symbol and add it to the standard_transformations in parse_expr
> but the problem is still the same because the problem is in auto_symbol and
> it parses again my string 'm[x][y]'.
Does it work if you transform it literally to sympy.Symbol("m[0][0]")?
Le dimanche 26 avril 2015 21:03:02 UTC+2, Aaron Meurer a écrit :On Sun, Apr 26, 2015 at 12:27 AM, Loïc Gouarin <loic.g...@gmail.com> wrote:
> Hi,
>
> I would like to parse a string expression like that
>
> from sympy.parsing.sympy_parser import parse_expr
> parse_expr("m[0][0]")
>
> But I have an error with 'Symbol' object does not support indexing.
>
> If I do
>
> sympy.Symbol("m[0][0]")
>
> everything is ok.
>
> I try to write my own transformation funtion that takes the "m[x][y]" and
> creates a Symbol and add it to the standard_transformations in parse_expr
> but the problem is still the same because the problem is in auto_symbol and
> it parses again my string 'm[x][y]'.
Does it work if you transform it literally to sympy.Symbol("m[0][0]")?
Hi Aaron,your solution is almost what I want. There is no error in the parse_exp but I have in the result Symbol("m[x][y]") and not m[x][y]. I know thatit is the same but I parse again the result and I need m[x][y].Is there an easy way to have on the ouput m[x][y] and not Symbol("m[x][y]") ?
from sympy.parsing.sympy_parser import parse_expr, standard_transformations
def auto_moments(tokens, local_dict, global_dict):
result = []
i = 0
while(i < len(tokens)):
tokNum, tokVal = tokens[i]
if tokVal == 'm':
name = ''.join([val for n, val in tokens[i:i+7]])
result.append((tokNum, 'Symbol("{0}")'.format(name)))
i += 7
else:
result.append(tokens[i])
i += 1
return result
s = parse_expr("m[0][0]", transformations=(auto_moments,) + standard_transformations)
Symbol("m[0][0]")from sympy.parsing.sympy_parser import parse_expr, standard_transformations
def auto_moments(tokens, local_dict, global_dict):
result = []
i = 0
while(i < len(tokens)):
tokNum, tokVal = tokens[i]
if tokVal == 'm':
name = ''.join([val for n, val in tokens[i:i+7]])
result.extend([(1, 'Symbol'), (51, '('), (3, "'{0}'".format(name)), (51, ')')])
i += 7
else:
result.append(tokens[i])
i += 1
return result
s = parse_expr("m[0][0]", transformations=(auto_moments,) + standard_transformations)
print sm[0][0]