How to create symbol polynomial from given array with sympy?

Question:

I am trying to calculate some polynomials given an input numerator and denominator polynomials as coefficient arrays.

How can I create my polynomials from these arrays?

E.g:

Inputs:

den= [2,3,4]
num= [1,3]

Output:
(s+3)/(s^2+3*s+4)

I need to use symbols because I will further need to divide the results by other polynomials and perform further polynomial computations.

P.S Is sympy suitable for this? I would usually solve things like this in matlab but I want to expand my knowledge.

Asked By: Bercovici Adrian

||

Answers:

I think what you want is (s+3)/(2*s^2+3*s+4), there is a typo in your original expression. And in Python, ^ is not power, power is **.

You just need a ordinary Python list comprehension:

from sympy import poly
from sympy.abc import s

den_ = sum(co*s**i for i, co in enumerate(reversed(den)))
num_ = sum(co*s**i for i, co in enumerate(reversed(num)))
res = num_/den_
Answered By: llllllllll

You can do the following:

den = [2, 3, 4]
num = [1, 3]
x = symbols('x')
Poly(num, x)/Poly(den, x)

This creates Poly objects for the numerator and denominator (not just expressions). The coefficients are listed from the highest power of x.

Note that the result of division is an ordinary expression, since there is no RationalFunction type in SymPy. If you want to apply the tools from the polys module to the numerator and denominator, keep them separate as a tuple.

Answered By: user6655984

Use the classmethod Poly.from_list. The terms of the list are the coefficients in descending order of a polynomial of degree equal to len(list)-1

from sympy import Symbol, Poly

# coefs
num= [1,3]
den= [2,3,4]

# symbol
s = Symbol('s')

# polynomial
n = Poly.from_list(num, x)
d = Poly.from_list(den, x)

# expression
e = n/d
Answered By: cards
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.