Coefficient and Point-Value Representations
Multiplying two polynomials of degree the schoolbook way takes coefficient products. The fast Fourier transform brings that down to , and with it the multiplication of huge integers, convolution in signal processing and image filtering, and the inner loop of many string and counting algorithms. The idea starts with a change of representation. A polynomial can be stored by its coefficients or by its values at enough points, and each representation makes a different operation cheap. This lesson compares the two; the next four build the fast conversion between them.
Coefficient form
A polynomial of degree-bound is
stored as the vector . (Any degree below is allowed, so a polynomial of degree-bound also has every larger degree-bound.) Three operations:
- Evaluation at a point by Horner's rule, : multiplications and additions, .
- Addition coefficient by coefficient: .
- Multiplication: has degree-bound and coefficients
the convolution . Computed directly it costs coefficient products.
def horner(a, x):
result, mults = 0, 0
for coef in reversed(a):
result = result * x + coef
mults += 1
return result, mults - 1 # the first multiplication is by 0
def convolve(a, b):
c, mults = [0] * (len(a) + len(b) - 1), 0
for i, ai in enumerate(a):
for j, bj in enumerate(b):
c[i + j] += ai * bj
mults += 1
return c, mults
A = [5, -2, 0, 3] # 5 - 2x + 3x^3
B = [1, 4, -1, 2] # 1 + 4x - x^2 + 2x^3
print("A(2) =", horner(A, 2)[0], "with", horner(A, 2)[1], "multiplications")
C, m = convolve(A, B)
print("A * B coefficients:", C, "with", m, "multiplications")
print("check at x = 2:", horner(C, 2)[0], "=", horner(A, 2)[0] * horner(B, 2)[0])A(2) = 25 with 3 multiplications
A * B coefficients: [5, 18, -13, 15, 8, -3, 6] with 16 multiplications
check at x = 2: 525 = 525
Point-value form
A point-value representation of is a set of pairs
with distinct . It determines uniquely: the coefficients satisfy the linear system with the Vandermonde matrix , whose determinant is nonzero for distinct points. Recovering the coefficients from values is called interpolation.
In this form the operations change cost:
- Addition: add the values at the same points, .
- Multiplication: multiply the values, , but the product has degree-bound , so both factors must first be given at (or more) points. Extended point-value forms at points do the job.
- Evaluation at a new point is the expensive one: it amounts to interpolating first.
Lagrange's formula interpolates directly:
and it can be organized to compute all coefficients in time. The program multiplies and through point values: evaluate both at 7 points, multiply the values, and interpolate the product exactly with fractions.
from fractions import Fraction
def horner(a, x):
r = 0
for coef in reversed(a):
r = r * x + coef
return r
def poly_mul_linear(p, x0): # p(x) * (x - x0)
out = [0] * (len(p) + 1)
for i, c in enumerate(p):
out[i + 1] += c
out[i] -= c * x0
return out
def interpolate(xs, ys):
"""Lagrange interpolation, exact; returns the coefficients"""
n = len(xs)
coeffs = [Fraction(0)] * n
for k in range(n):
basis, denom = [Fraction(1)], Fraction(1)
for j in range(n):
if j != k:
basis = poly_mul_linear(basis, xs[j])
denom *= xs[k] - xs[j]
for i in range(n):
coeffs[i] += ys[k] * basis[i] / denom
return coeffs
A = [5, -2, 0, 3]
B = [1, 4, -1, 2]
xs = [-3, -2, -1, 0, 1, 2, 3] # 2n - 1 = 7 distinct points
ya = [horner(A, x) for x in xs]
yb = [horner(B, x) for x in xs]
yc = [p * q for p, q in zip(ya, yb)] # pointwise product: 7 multiplications
print("values of A:", ya)
print("values of B:", yb)
print("values of C:", yc)
print("interpolated C:", [int(c) for c in interpolate(xs, yc)])values of A: [-70, -15, 4, 5, 6, 25, 80]
values of B: [-74, -27, -6, 1, 6, 21, 58]
values of C: [5180, 405, -24, 5, 36, 525, 4640]
interpolated C: [5, 18, -13, 15, 8, -3, 6]
The product's coefficients match the convolution above. The middle step cost only 7 multiplications, but the two evaluations ( points times each) and the interpolation cost , so nothing is gained yet.
The plan
coefficients a, b --evaluate at 2n points--> values of A, B
| |
convolution, Theta(n^2) pointwise product, Theta(n)
v v
coefficients c <--------interpolate-------- values of CThe detour pays off only if evaluation and interpolation are fast. At arbitrary points they are not, but the points are ours to choose. Choosing the complex roots of unity, numbers whose powers repeat in a very structured way, makes both directions take time: evaluation is the discrete Fourier transform, computed by the FFT, and interpolation is the inverse transform, computed by the same algorithm. The next lesson, Roots of Unity and the DFT, introduces those points.
In C++
Horner evaluation and direct convolution in C++, with the same polynomials, counting multiplications.
#include <cstdio>
#include <vector>
using Poly = std::vector<long long>;
long long horner(const Poly& a, long long x) {
long long r = 0;
for (auto it = a.rbegin(); it != a.rend(); ++it) r = r * x + *it;
return r;
}
Poly convolve(const Poly& a, const Poly& b, long long& mults) {
Poly c(a.size() + b.size() - 1, 0);
for (size_t i = 0; i < a.size(); i++)
for (size_t j = 0; j < b.size(); j++) { c[i + j] += a[i] * b[j]; mults++; }
return c;
}
int main() {
Poly A = {5, -2, 0, 3}, B = {1, 4, -1, 2};
long long mults = 0;
Poly C = convolve(A, B, mults);
std::printf("C =");
for (long long c : C) std::printf(" %lld", c);
std::printf(" (%lld multiplications)\n", mults);
for (long long x : {-1LL, 2LL, 10LL})
std::printf("x = %3lld: C(x) = %lld, A(x) B(x) = %lld\n", x, horner(C, x), horner(A, x) * horner(B, x));
}C = 5 18 -13 15 8 -3 6 (16 multiplications)
x = -1: C(x) = -24, A(x) B(x) = -24
x = 2: C(x) = 525, A(x) B(x) = 525
x = 10: C(x) = 5793885, A(x) B(x) = 5793885
At the polynomial product is ordinary integer multiplication of the digit strings (with digits allowed to be negative or above 9 until carries are propagated): . That is how the FFT multiplies large integers, in The Inverse FFT and Fast Polynomial Multiplication.
Test yourself
How many coefficient multiplications does the direct convolution of two polynomials of degree-bound use?
Why must and be evaluated at points, not , before multiplying values?
- To avoid rounding errors
- Because the product has degree-bound , and a polynomial is determined only by that many values
- Because roots of unity come in pairs
- Because interpolation needs an odd number of points
Which operation is cheap in point-value form but costs directly in coefficient form?
- Addition
- Evaluation at a new point
- Multiplication
- Differentiation
Why does a set of point-value pairs with distinct determine a polynomial of degree-bound uniquely?
- Because the Vandermonde matrix of distinct points is invertible
- Because polynomials are continuous
- By Horner's rule
- Because the points are roots of unity