1 | initial version |
Try ex1.operands(), ex1.operator() , ex1.op
var('a b c x')
ex1 = a*x^2 + b/x - c
ex1.operands()
2 | No.2 Revision |
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]
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)
4 | No.4 Revision |
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)