On Sep 15, 6:24 pm, Stefan Boettner <[email protected]> wrote: > Hello, > > I'm trying to parse symbolic expressions, but got stuck very quickly. > > If I say: > (x^2).operator() > I get: > <built-in function pow> > > If I say: > pow > I also get: > <built-in function pow> > > But if I say: > (x^2).operator()==pow > I get: > False > > How do I properly test if the topmost operation of an expression is a > power, product, sum, whatever?
The animal seems to be called: sage.symbolic.expression.operator.pow but if you don't want to bother figuring something like that out, you can just cache it from a model equation: pow_as_a_symbolic_operator = (x^2).operator() and whenever you have a symbolic expression you expect is an exponentiation, you can do expr.operator() == pow_as_a_symbolic_operator Mike Hansen has authored a file in the sage library "devel/sage/sage/ symbolic/expression_conversions.py" that seems to make it easier to traverse an expression tree. You may be able to save yourself some work by inheriting from the classes he provides there and only implement what needs to be done on the leaves. I ran into that file by accident myself. It doesn't seem to be imported by default and I have no experience using it. Be aware: expressions that do not have an operator can be a SymbolicVariable or the method ".pyobject()" unpacks it: compare SR(x).pyobject() and SR(pi).pyobject(). I am not aware of other possibilities. Nils --~--~---------~--~----~------------~-------~--~----~ To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/sage-support URLs: http://www.sagemath.org -~----------~----~----~----~------~----~------~--~---
