The problem is that you're comparing A's operator with a "string", so
it'll evaluate to False.

Maybe what you're trying is something like this:

if A.operator() == operator.add:

I don't know if it's the right way to do what you want, but should work.

On Sun, May 15, 2011 at 3:28 PM, MathLynx <[email protected]> wrote:
> 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 <[email protected]> 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 [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
> URL: http://www.sagemath.org
>

-- 
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
URL: http://www.sagemath.org

Reply via email to