Toom–Cook multiplication
Toom–Cook, sometimes known as Toom-3, named after Andrei Toom, who introduced the new algorithm with its low complexity, and Stephen Cook, who cleaned the description of it, is a multiplication algorithm for large integers.
Given two large integers, a and b, Toom–Cook splits up a and b into k smaller parts each of length l, and performs operations on the parts. As k grows, one may combine many of the multiplication sub-operations, thus reducing the overall complexity of the algorithm. The multiplication sub-operations can then be computed recursively using Toom–Cook multiplication again, and so on. Although the terms "Toom-3" and "Toom–Cook" are sometimes incorrectly used interchangeably, Toom-3 is only a single instance of the Toom–Cook algorithm, where k = 3.
Toom-3 reduces 9 multiplications to 5, and runs in Θ/log ≈ Θ. In general, Toom-k runs in, where, ne is the time spent on sub-multiplications, and c is the time spent on additions and multiplication by small constants. The Karatsuba algorithm is a special case of Toom–Cook, where the number is split into two smaller ones. It reduces 4 multiplications to 3 and so operates at Θ/log ≈ Θ. Ordinary long multiplication is equivalent to Toom-1, with complexity Θ.
Although the exponent e can be set arbitrarily close to 1 by increasing k, the function c unfortunately grows very rapidly. The growth rate for mixed-level Toom–Cook schemes was still an open research problem in 2005. An implementation described by Donald Knuth achieves the time complexity.
Due to its overhead, Toom–Cook is slower than long multiplication with small numbers, and it is therefore typically used for intermediate-size multiplications, before the asymptotically faster Schönhage–Strassen algorithm becomes practical.
Toom first described this algorithm in 1963, and Cook published an improved algorithm in his PhD thesis in 1966.
Details
This section discusses exactly how to perform Toom-k for any given value of k, and is a simplification of a description of Toom–Cook polynomial multiplication described by Marco Bodrato. The algorithm has five main steps:In a typical large integer implementation, each integer is represented as a sequence of digits in positional notation, with the base or radix set to some value b; for this example we use b = 10000, so that each digit corresponds to a group of four decimal digits. Say the two integers being multiplied are:
These are much smaller than would normally be processed with Toom–Cook but they will serve to illustrate the algorithm.
Splitting
The first step is to select the base B = bi, such that the number of digits of both m and n in base B is at most k. A typical choice for i is given by:In our example we'll be doing Toom-3, so we choose. We then separate m and n into their base B digits mi, ni:
We then use these digits as coefficients in degree- polynomials p and q, with the property that p = m and q = n:
The purpose of defining these polynomials is that if we can compute their product, our answer will be.
In the case where the numbers being multiplied are of different sizes, it's useful to use different values of k for m and n, which we'll call km and kn. For example, the algorithm "Toom-2.5" refers to Toom–Cook with km = 3 and kn = 2. In this case the i in B = bi is typically chosen by:
Evaluation
The Toom–Cook approach to computing the polynomial product pq is a commonly used one. Note that a polynomial of degree d is uniquely determined by d + 1 points. The idea is to evaluate p and q at various points. Then multiply their values at these points to get points on the product polynomial. Finally interpolate to find its coefficients.Since, we will need points to determine the final result. Call this d. In the case of Toom-3, d = 5. The algorithm will work no matter what points are chosen, but in the interest of simplifying the algorithm it's better to choose small integer values like 0, 1, −1, and −2.
One unusual point value that is frequently used is infinity, written ∞ or 1/0. To "evaluate" a polynomial p at infinity actually means to take the limit of p/xdeg p as x goes to infinity. Consequently, p is always the value of its highest-degree coefficient.
In our Toom-3 example, we will use the points 0, 1, −1, −2, and ∞. These choices simplify evaluation, producing the formulas:
and analogously for q. In our example, the values we get are:
As shown, these values may be negative.
For the purpose of later explanation, it will be useful to view this evaluation process as a matrix-vector multiplication, where each row of the matrix contains powers of one of the evaluation points, and the vector contains the coefficients of the polynomial:
The dimensions of the matrix are d by km for p and d by kn for q. The row for infinity is always all zero except for a 1 in the last column.
Faster evaluation
Multipoint evaluation can be obtained faster than with the above formulas. The number of elementary operations can be reduced. The sequence given by Bodrato for Toom-3, executed here over the first operand of the running example is the following:This sequence requires five addition/subtraction operations, one less than the straightforward evaluation. Moreover the multiplication by 4 in the calculation of p was saved.
Pointwise multiplication
Unlike multiplying the polynomials p and q, multiplying the evaluated values p and q just involves multiplying integers — a smaller instance of the original problem. We recursively invoke our multiplication procedure to multiply each pair of evaluated points. In practical implementations, as the operands become smaller, the algorithm will switch to schoolbook long multiplication. Letting r be the product polynomial, in our example we have:As shown, these can also be negative. For large enough numbers, this is the most expensive step, the only step that is not linear in the sizes of m and n.
Interpolation
This is the most complex step, the reverse of the evaluation step: given our d points on the product polynomial r, we need to determine its coefficients. In other words, we want to solve this matrix equation for the vector on the right-hand side:This matrix is constructed the same way as the one in the evaluation step, except that it's d × d. We could solve this equation with a technique like Gaussian elimination, but this is too expensive. Instead, we use the fact that, provided the evaluation points were chosen suitably, this matrix is invertible, and so:
All that remains is to compute this matrix-vector product. Although the matrix contains fractions, the resulting coefficients will be integers — so this can all be done with integer arithmetic, just additions, subtractions, and multiplication/division by small constants. A difficult design challenge in Toom–Cook is to find an efficient sequence of operations to compute this product; one sequence given by Bodrato for Toom-3 is the following, executed here over the running example:
We now know our product polynomial r:
If we were using different km, kn, or evaluation points, the matrix and so our interpolation strategy would change; but it does not depend on the inputs and so can be hard-coded for any given set of parameters.
Recomposition
Finally, we evaluate r to obtain our final answer. This is straightforward since B is a power of b and so the multiplications by powers of B are all shifts by a whole number of digits in base b. In the running example b = 104 and B = b2 = 108.And this is in fact the product of 1234567890123456789012 and 987654321987654321098.
Interpolation matrices for various ''k''
Here we give common interpolation matrices for a few different common small values of km and kn.Toom-1
Toom-1 requires 1 evaluation point, here chosen to be 0. It degenerates to long multiplication, with an interpolation matrix of the identity matrix:Toom-1.5
Toom-1.5 requires 2 evaluation points, here chosen to be 0 and ∞. Its interpolation matrix is then the identity matrix:This also degenerates to long multiplication: both coefficients of one factor are multipled by the sole coefficient of the other factor.