Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to find the polynomial form coefficients of x with the presence of exp(1/x)

Consider the following expression

from sympy import *
a,b,c,x=symbols('a,b,c,x',real=True)
expr=a+(a+(a+b)*x+c*(a+b)*x**2 )*exp(a*x/c)

I wanted to simplify this expression such that it’s in the "almost" polynomial form, i.e.

a+a*exp(a*x/c) + (a+b)*exp(a*x/c)*x + c*(a+b)*exp(a*x/c)*x**2

where one can extract the "coefficients" of the terms of x**n.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

a+a*exp(a*x/c)
(a+b)*exp(a*x/c)
c*(a+b)*exp(a*x/c)

Though it’s not a trivial task, I suspect that there might be a simpler built in function, but did not find one.

expr.series(x)

returned a series expansion, which expanded the exp(a*x/c) with respect to x and complicated the things.

from sympy import poly
poly(expr,x)

also failed, because

PolynomialError: exp(a*x/c) contains an element of the set of generators.

I also tried

expr.rewrite(x)

which did not do anything.

How to find the polynomial form coefficients of x in this types of expression? Is there a built in function for it?

>Solution :

Try creating a list of the polynomial forms by manually collecting terms with the same power of x using the expand() and collect() functions:

from sympy import symbols, exp, collect, expand

a, b, c, x = symbols('a b c x', real=True)
expr = a + (a + (a + b) * x + c * (a + b) * x**2) * exp(a * x / c)

# Expand the expression to ensure the multiplication is distributed across the terms.
expr_expanded = expand(expr)

# Collect the terms with the same power of x.
expr_collected = collect(expr_expanded, x, evaluate=False)

for power, coeff in expr_collected.items():
    print(f'x^{power}: {coeff}')

Output:

x^x: a*exp(a*x/c) + b*exp(a*x/c)
x^x**2: a*c*exp(a*x/c) + b*c*exp(a*x/c)
x^1: a*exp(a*x/c) + a
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading