Parse expression with indexing

123 views
Skip to first unread message

Loïc Gouarin

unread,
Apr 26, 2015, 2:59:37 PM4/26/15
to sy...@googlegroups.com
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]'.

Could you tell me how I can do that ?

Thanks,
Loic

Aaron Meurer

unread,
Apr 26, 2015, 3:03:02 PM4/26/15
to sy...@googlegroups.com
Does it work if you transform it literally to sympy.Symbol("m[0][0]")?

Aaron Meurer

>
> Could you tell me how I can do that ?
>
> Thanks,
> Loic
>
> --
> 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 sympy+un...@googlegroups.com.
> To post to this group, send email to sy...@googlegroups.com.
> Visit this group at http://groups.google.com/group/sympy.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/sympy/b23973b8-0d28-4bf4-b863-404af71458d9%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Loïc Gouarin

unread,
Apr 27, 2015, 2:59:38 AM4/27/15
to sy...@googlegroups.com


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]") ?

Thanks,
Loic 

Loïc Gouarin

unread,
Apr 28, 2015, 12:43:27 AM4/28/15
to sy...@googlegroups.com


Le lundi 27 avril 2015 08:59:38 UTC+2, Loïc Gouarin a écrit :


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]") ?

 
Ok I found my error. My transformation was incorrect. I wrote

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)



The result was 

Symbol("m[0][0]")

But if I write 

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 s

Now it is ok. I have

m[0][0]

Thanks,
Loic
Reply all
Reply to author
Forward
0 new messages