Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Try ex1.operands(), ex1.operator() , ex1.op

var('a b c x')
ex1 = a*x^2 + b/x - c
ex1.operands()

Try ex1.operands(), ex1.operator() , ex1.op

var('a b c x')
ex1 = a*x^2 + b/x - c
ex1.operands()

     [a*x^2, -c, b/x]
click to hide/show revision 3
To show the parse tree, use a recursive function

Try ex1.operands(), ex1.operator() , ex1.op

var('a b c x')
ex1 = a*x^2 + b/x - c
ex1.operands()

     [a*x^2, -c, b/x]

To show the parse tree, let us use a recursive function:

def print_tree(expr,k=0):
    op = expr.operator()
    operands = expr.operands()
    if op:
        print k*' ---',op, operands
        k += 1
        for operand in operands:
            print_tree(operand,k)

print_tree(ex1)

Try ex1.operands(), ex1.operator() , ex1.op

var('a b c x')
ex1 = a*x^2 + b/x - c
ex1.operands()

     [a*x^2, -c, b/x]

UPDATE: To show the parse tree, let us use a recursive function:

def print_tree(expr,k=0):
    op = expr.operator()
    operands = expr.operands()
    if op:
        print k*' ---',op, operands
        k += 1
        for operand in operands:
            print_tree(operand,k)

print_tree(ex1)