Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

How can extract different terms from a symbolic expression?

I am trying to dissect an equation into its terms for statements such as "the first term in the denominator...". Optimally, I would like a list of terms, e.g.:

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

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

This used to be possible ages ago by just typing

ex1[0]

a*x^2

ex1[1]

b/x

etc.

Similarly, I would like to be able to extract the terms inside a log or exponent, nominator, denominator... Is there a structured way to do this?

How can extract different terms from a symbolic expression?

I am trying to dissect an equation into its terms for statements such as "the first term in the denominator...". Optimally, I would like a list of terms, e.g.:

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

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

This used to be possible ages ago by just typing

ex1[0]

a*x^2

ex1[1]

b/x

etc.

Similarly, I would like to be able to extract the terms inside a log or exponent, nominator, denominator... Is there a structured way to do this?

UPDATE: Following on from the first answer given below, I tried:

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

[a*x^2, -c, b/x] <built-in function="" add="">

Other examples:

var('a b c x')
ex1 = a*x^2*b*c
print ex1.operands()
print ex1.operator()

[a, b, c, x^2] <built-in function="" mul="">

var('a b c x')
ex1 = a+x^2+b+c
print ex1.operands()
print ex1.operator()

[x^2, a, b, c] <built-in function="" add="">

I could now iterate through each list and link the terms with the top operator given by ex1.operator() to recover the original equation. Does anyone know how to do this?

How can I use the outputs of .operands() and .operator() to recover the original equation?

Thanks again!