TExcelTAD
Home › Selected Topics: FFT, Strings, Parallelism & NP-Completeness › Coefficient and Point-Value Representations

Coefficient and Point-Value Representations

Multiplying two polynomials of degree nn the schoolbook way takes Θ(n2)\Theta(n^2) coefficient products. The fast Fourier transform brings that down to Θ(nlg⁡n)\Theta(n \lg n), 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 nn is

A(x)=∑j=0n−1ajxj,A(x) = \sum_{j=0}^{n-1} a_j x^j ,

stored as the vector a=(a0,a1,…,an−1)a = (a_0, a_1, \ldots, a_{n-1}). (Any degree below nn is allowed, so a polynomial of degree-bound nn also has every larger degree-bound.) Three operations:

  • Evaluation at a point x0x_0 by Horner's rule, A(x0)=a0+x0(a1+x0(a2+⋯+x0an−1))A(x_0) = a_0 + x_0(a_1 + x_0(a_2 + \cdots + x_0 a_{n-1})): n−1n - 1 multiplications and additions, Θ(n)\Theta(n).
  • Addition coefficient by coefficient: Θ(n)\Theta(n).
  • Multiplication: C(x)=A(x)B(x)C(x) = A(x)B(x) has degree-bound 2n−12n - 1 and coefficients
ck=∑j=0kaj bk−j,c_k = \sum_{j=0}^{k} a_j\, b_{k-j},

the convolution c=a⊗bc = a \otimes b. Computed directly it costs n2n^2 coefficient products.

python · try it yourself
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 AA is a set of nn pairs

{(x0,y0),(x1,y1),…,(xn−1,yn−1)},yk=A(xk),\{(x_0, y_0), (x_1, y_1), \ldots, (x_{n-1}, y_{n-1})\}, \qquad y_k = A(x_k),

with distinct xkx_k. It determines AA uniquely: the coefficients satisfy the linear system Va=yVa = y with the Vandermonde matrix Vkj=xkjV_{kj} = x_k^j, whose determinant ∏j<k(xk−xj)\prod_{j < k}(x_k - x_j) 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, Θ(n)\Theta(n).
  • Multiplication: multiply the values, Θ(n)\Theta(n), but the product has degree-bound 2n−12n - 1, so both factors must first be given at 2n−12n - 1 (or more) points. Extended point-value forms at 2n2n points do the job.
  • Evaluation at a new point is the expensive one: it amounts to interpolating first.

Lagrange's formula interpolates directly:

A(x)=∑k=0n−1yk∏j≠kx−xjxk−xj,A(x) = \sum_{k=0}^{n-1} y_k \prod_{j \ne k} \frac{x - x_j}{x_k - x_j},

and it can be organized to compute all nn coefficients in Θ(n2)\Theta(n^2) time. The program multiplies AA and BB through point values: evaluate both at 7 points, multiply the values, and interpolate the product exactly with fractions.

python · try it yourself
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 (2n−12n - 1 points times Θ(n)\Theta(n) each) and the interpolation cost Θ(n2)\Theta(n^2), so nothing is gained yet.

The plan

text
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 C

The 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 2n2n complex roots of unity, numbers whose powers repeat in a very structured way, makes both directions take Θ(nlg⁡n)\Theta(n \lg n) 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.

cpp · try it yourself
#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 x=10x = 10 the polynomial product is ordinary integer multiplication of the digit strings (with digits allowed to be negative or above 9 until carries are propagated): C(10)=A(10) B(10)C(10) = A(10)\,B(10). 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 nn use?

  1. nn
  2. 2n−12n - 1
  3. n2n^2
  4. nlg⁡nn \lg n

Why must AA and BB be evaluated at 2n−12n - 1 points, not nn, before multiplying values?

  1. To avoid rounding errors
  2. Because the product has degree-bound 2n−12n - 1, and a polynomial is determined only by that many values
  3. Because roots of unity come in pairs
  4. Because interpolation needs an odd number of points

Which operation is cheap in point-value form but costs Θ(n2)\Theta(n^2) directly in coefficient form?

  1. Addition
  2. Evaluation at a new point
  3. Multiplication
  4. Differentiation

Why does a set of nn point-value pairs with distinct xkx_k determine a polynomial of degree-bound nn uniquely?

  1. Because the Vandermonde matrix of distinct points is invertible
  2. Because polynomials are continuous
  3. By Horner's rule
  4. Because the points are roots of unity