Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Efficient calculation of polynomial coefficients from its roots

I have the roots of a monic polynomial, i.e.

p(x) = (x-x_1)*...*(x-x_n)

and I need the coefficients a_n, ..., a_0 from

p(x) = x^n + a_{n-1}x^{n-1} + ... + a_0.

Does anybody know a computationally efficient way of doing this? If anybody knows a C/C++ implementation, this would actually be the best. (I already had a look at GSL but it did not provide a function.)

Of course, I know how to to it mathematically. I know, that the coefficient a_i is the sum of all products of the subsets with n-i elements. But if I would do it the dumb way, this means to iterate across all subsets, I would need

sum^{n-1}_{k=1} ( k choose n) * (k-1)

multiplications and

sum^n_{k=0} ( k choose n) - n

additions. Hence both terms grow with O(n!), which is too much computation to transform a list of n root into a list of n coefficients. I believe there must be some intelligent way to reuse most of the intermediate results, but I do not find one.

like image 264
user2690527 Avatar asked Jan 20 '14 14:01

user2690527


People also ask

What is the relation between roots and coefficients of a polynomial equation?

The relationship between roots and coefficients is, In a quadratic equation the sum of the roots is equal to the negative of the coefficient of second term divided by the coefficient of first term. while the product of roots is equal to the third term divided by the first term.

What is the coefficients of a polynomial?

Polynomial coefficients are the numbers that come before a term. Terms usually have a number and a variable (e.g. 2 x 2 2x^2 2x2, where 2 is the number, and x is the variable). The number portion is the coefficient.


1 Answers

You can do this in O(n^2) very easily if you incrementally build your polynomial. Let's define:

p_k(x) = (x-x_1)*...*(x-x_k)

That is p_k(x) is the multiplication of the first k (x-x_i) of p(x). We have:

p_1(x) = x-x_1

In other words the array of coefficients (a) would be (indices start from 0 and from left):

-x_1 1

Now assume we have the array of coefficients for p_k(x):

a_0 a_1 a_2 ... a_k

(side note: a_k is 1). Now we want to calculate p_k+1(x), which is (note that k+1 is the index, and there is no summation by 1):

p_k+1(x) = p_k(x)*(x-x_k+1)
=> p_k+1(x) = x*p_k(x) - x_k+1*p_k(x)

Translating this to the array of coefficients, it means that the new coefficients are the previous ones shifted to the right (x*p_k(x)) minus the k+1th root multiplied by the same coefficients (x_k+1*p_k(x)):

           0   a_0 a_1 a_2 ... a_k-1 a_k
- x_k+1 * (a_0 a_1 a_2 a_3 ... a_k)
-----------------------------------------
-x_k+1*a_0 (a_0-x_k+1*a_1) (a_1-x_k+1*a_2) (a_2-x_k+1*a_3) ... (a_k-x_k+1*a_k-1) a_k

(side note: and that is how a_k stays 1) There is your algorithm. Start from p_1(x) (or even p_0(x) = 1) and incrementally build the array of coefficients by the above formula for each root of the polynomial.

like image 57
Shahbaz Avatar answered Sep 29 '22 23:09

Shahbaz