Puzzles, Maths and Algorithms: Polynomial Evaluation
Problem: Given a polynomial with degree bound of n=m^r. How do we evaluate the polynomial at n different points such that the polynomial can be recovered from these n points (i.e. the coefficients can be calculated back using the points).
Note: We do not want to recover the coefficients but cleverly choosing the points as that impacts the running time of the algorithm.
Let the polynomial be A(x) = a0 + a1 x + a2 x^2 + ... + a(n-1) x^(n-1).
A(x) has a degree bound of n. Its actual degree can be lower than n-1 if a(n-1) equals zero but that doesn't matter here.
Using Horner's rule, we can compute a polynomial in O(n) time by arranging the terms as follows:
A(x) = a0 + x (a1 + x (a2 + ... + x (a(n-2) + x a(n-1))))
So it will take O(n^2) time to compute polynomial at n different points. But we can use divide and conquer to do better than that i.e. in O(n lg n). We can create "m" different polynomials such that each term of ith polynomial has degree mod m = i.
=> A(x) = [ a0 + a(m) x^m + a(2m) x^2m ... ] +
[ a1 + a(m+1) x^(m+1) + a(2m+1) x^(2m+1) ... ] +
.
.
.
[ a(m-1) x^(m-1) + a(2m-1) x^(2m-1) + ... ]
=> A(x) = A[0](y) + x A[1](y) + ... + x^i A[i](y) + ... + x^(m-1) A[m-1](y)
where y = x^m, and
A[i](x) = a(i) + a(m+i) . x + ... + a(n-m+i) . x^(n/m-1)
Still the recurrence for evaluation each point is T(n) = m T(n/m) + O(m) = O(n). So evaluating polynomial at n points leads to O(n^2) which is not great.
Now we can choose the n points cleverly. Lets set w as principal nth root of unity.
=> w^n = 1
=> w = e^(2 . pi . i / n)
= cos (2 . pi /n) + i . sin(2 . pi / n)
The two interesting properties of w are
1. w^(n/2) = -1
2. [w^(kn/m + p)]^m = w^(kn + mp) = w^(mp)
So the n points on which the polynomial can be evaluated are w^0, w^1, ..., w^(n-1). Now we need to evaluate lower order polynomials of size n/m at only first m powers of w as afterwards it cycles again and we can reuse the computation. This comes from the observation that
A(w^p) = A[0](w^(mp)) + w^p A[1](w^(mp)) + ... + w^(m-1) A[m-1](w^(mp))
Also,
A(m^(kn/m+p)) = A[0](w^(mp)) + w^(kn/m+p) A[1](w^(mp)) + ... + [w^(kn/m+p)]^(m-1) A[m-1](w^(mp))
So A[0], ... A[m-1] are evaluated at p = { 0, 1, ..., m-1 }, and the computation is used for all the other n-m powers of w.
The general recurrence of this algorithm is:
T(n) = m T(n/m) + O(n)
=> T(n) = O(n lg n)
m = 2 works well in practice and is employed by Fast Fourier Transform in order to intrapolate the coefficients of the polynomial. This is generally used for multiplying two polynomials to get higher order polynomial. The steps are to first evaluate the lower order polynomials at n points and then use the multiplication of the point values to generate the coefficients of the higher order polynomial.
Also check Horner's Method for Polynomial Evaluation
Read full article from Puzzles, Maths and Algorithms: Polynomial Evaluation
Problem: Given a polynomial with degree bound of n=m^r. How do we evaluate the polynomial at n different points such that the polynomial can be recovered from these n points (i.e. the coefficients can be calculated back using the points).
Note: We do not want to recover the coefficients but cleverly choosing the points as that impacts the running time of the algorithm.
Let the polynomial be A(x) = a0 + a1 x + a2 x^2 + ... + a(n-1) x^(n-1).
A(x) has a degree bound of n. Its actual degree can be lower than n-1 if a(n-1) equals zero but that doesn't matter here.
Using Horner's rule, we can compute a polynomial in O(n) time by arranging the terms as follows:
A(x) = a0 + x (a1 + x (a2 + ... + x (a(n-2) + x a(n-1))))
So it will take O(n^2) time to compute polynomial at n different points. But we can use divide and conquer to do better than that i.e. in O(n lg n). We can create "m" different polynomials such that each term of ith polynomial has degree mod m = i.
=> A(x) = [ a0 + a(m) x^m + a(2m) x^2m ... ] +
[ a1 + a(m+1) x^(m+1) + a(2m+1) x^(2m+1) ... ] +
.
.
.
[ a(m-1) x^(m-1) + a(2m-1) x^(2m-1) + ... ]
=> A(x) = A[0](y) + x A[1](y) + ... + x^i A[i](y) + ... + x^(m-1) A[m-1](y)
where y = x^m, and
A[i](x) = a(i) + a(m+i) . x + ... + a(n-m+i) . x^(n/m-1)
Still the recurrence for evaluation each point is T(n) = m T(n/m) + O(m) = O(n). So evaluating polynomial at n points leads to O(n^2) which is not great.
Now we can choose the n points cleverly. Lets set w as principal nth root of unity.
=> w^n = 1
=> w = e^(2 . pi . i / n)
= cos (2 . pi /n) + i . sin(2 . pi / n)
The two interesting properties of w are
1. w^(n/2) = -1
2. [w^(kn/m + p)]^m = w^(kn + mp) = w^(mp)
So the n points on which the polynomial can be evaluated are w^0, w^1, ..., w^(n-1). Now we need to evaluate lower order polynomials of size n/m at only first m powers of w as afterwards it cycles again and we can reuse the computation. This comes from the observation that
A(w^p) = A[0](w^(mp)) + w^p A[1](w^(mp)) + ... + w^(m-1) A[m-1](w^(mp))
Also,
A(m^(kn/m+p)) = A[0](w^(mp)) + w^(kn/m+p) A[1](w^(mp)) + ... + [w^(kn/m+p)]^(m-1) A[m-1](w^(mp))
So A[0], ... A[m-1] are evaluated at p = { 0, 1, ..., m-1 }, and the computation is used for all the other n-m powers of w.
The general recurrence of this algorithm is:
T(n) = m T(n/m) + O(n)
=> T(n) = O(n lg n)
m = 2 works well in practice and is employed by Fast Fourier Transform in order to intrapolate the coefficients of the polynomial. This is generally used for multiplying two polynomials to get higher order polynomial. The steps are to first evaluate the lower order polynomials at n points and then use the multiplication of the point values to generate the coefficients of the higher order polynomial.
Also check Horner's Method for Polynomial Evaluation
Read full article from Puzzles, Maths and Algorithms: Polynomial Evaluation