On Sat, Sep 5, 2015 at 1:35 PM, Francesco Bonazzi <[email protected]> wrote: > Hi, > > in Mathematica one can access the expression tree by the [[ ]] operator: > > > In[1]:= e = x*y + z > > Out[1]= x y + z > > In[2]:= e[[1]] > > Out[2]= x y > > In[3]:= e[[1, 1]] > > Out[3]= x > > > Now one could change the x for a q: > > > In[4]:= e[[1, 1]] = q > > Out[4]= q > > In[5]:= e > > Out[5]= q y + z > > > This works by replacing a part in the expression tree (position [[1, 1]]), > there is no pattern matching. > > I was wondering, is there something like that in SymPy? > > Maybe, instead of accessing e.args[0].args[0] (Mathematica's arrays are > 1-starting), have some > > e.<some method>(0, 0) > > and > > new_e = e.<some other method>(<new value>, 0, 0) > > Is there some easy way to avoid writing a traversal function?
I don't think you need traversal. You just need to rebuild the expression from the bottom up with the remaining args (remember that expr == expr.func(*expr.args)). It would probably be useful to have some utilities functions for dealing with tuples like (1, 0) meaning expr.args[1].args[0]. Aaron Meurer > > -- > 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 post to this group, send email to [email protected]. > 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/8fc479d2-8131-4c01-9ad3-5618c52588e9%40googlegroups.com. > For more options, visit https://groups.google.com/d/optout. -- 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 post to this group, send email to [email protected]. 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/CAKgW%3D6LUH8JnZtZbPF1wbRtmJh2EUH9PNKXpBxuXuG%2B8g4CnrA%40mail.gmail.com. For more options, visit https://groups.google.com/d/optout.
