True. But the conditional sage: A=x+x^2 sage: if A.operator()=="<built-in function add>": sage: print "Y" sage: else: sage: print "N"
fails to give "Y". How can I patch this up? On May 15, 1:57 am, Jason Grout <jason-s...@creativetrax.com> wrote: > On 5/14/11 8:32 PM, MathLynx wrote: > > > How can I use Sage to determine if an expression is a sum - i.e. if > > the last operation performed is either addition or subtraction? > > > Thus, I would like to identify "a*(b+c)" as a product but "a*b+a*c" as > > a sum. > > > It would be nice to also identify the number of summands in an > > addition - or multiplicands in a product - i.e. "a*b+a*c" should be a > > sum with two summands, as opposed to "a*b+a*c+a*d" having three > > summands. > > You can use operands() and operator() to do these things: > > sage: var('a,b,c,d') > (a, b, c, d) > sage: f=a*b+a*c+a*d > sage: f.operands() > [a*b, a*c, a*d] > sage: f.operator() > <built-in function add> > sage: g=a*(b+c) > sage: g.operands() > [b + c, a] > sage: g.operator() > <built-in function mul> > > Jason -- To post to this group, send email to sage-support@googlegroups.com To unsubscribe from this group, send email to sage-support+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/sage-support URL: http://www.sagemath.org