Here is some code that I think will do the trick. sage: def subexpressions(e): ....: res = [] ....: if not hasattr(e, '_operands'): ....: return [] ....: for op in e._operands: ....: res.append(op) ....: res += subexpressions(op) ....: return res ....: sage: e = exp(x)+1 sage: f = exp(x) sage: subexpressions(e) [e^x, exp, x, 1] sage: f in subexpressions(e) True
--Mike On 10/1/07, Ondrej Certik <[EMAIL PROTECTED]> wrote: > > Hi, > > I was curious, so I started to port the algorithm. Now I have this problem: > > sage: e = exp(x)+1 > sage: f = exp(x) > > I need to check, that f is a subexpression of e. In SymPy, I do: > > e.subs(f, Symbol("x", dummy=True))==e > > i.e. I substitute f for some dummy variable and compare if the > expression has changed. If it did, f is a subexpression of e. In SAGE > however, is there a way how to substitute the whole exp(x) in the > expression (exp(x)+1) by something else? > > Thanks, > Ondrej > > > > --~--~---------~--~----~------------~-------~--~----~ To post to this group, send email to sage-devel@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/sage-devel URLs: http://sage.scipy.org/sage/ and http://modular.math.washington.edu/sage/ -~----------~----~----~----~------~----~------~--~---