Jump to content

Toom–Cook multiplication

From Wikipedia, the free encyclopedia

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 computational 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 nine multiplications to five, and runs in Θ(nlog⁡(5)/log⁡(3))≈Θ(n1.46). In general, Toom-k runs in Θ(c(k)ne), where e=log⁡(2k−1)/log⁡(k), ne is the time spent on sub-multiplications, and c is the time spent on additions and multiplication by small constants (Knuth, p. 296). The Karatsuba algorithm is equivalent to Toom-2, where the number is split into two smaller ones. It reduces four multiplications to three and so operates at Θ(nlog⁡(3)/log⁡(2))≈Θ(n1.58).

Although the exponent e can be set arbitrarily close to 1 by increasing k, the constant term in the function grows very rapidly.[1][2] The growth rate for mixed-level Toom–Cook schemes was still an open research problem in 2005.[3] An implementation described by Donald Knuth achieves the time complexity Θ(n22log⁡nlog⁡n).[4]

Because of 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 (with complexity Θ(nlog⁡nlog⁡log⁡n)) becomes practical.

Toom first described this algorithm in 1963, and Cook published an improved (asymptotically equivalent) algorithm in his PhD thesis in 1966.[5]

Details

[edit]

Toom–Cook multiplication is a fast integer multiplication algorithm that reduces the number of large multiplication operations by splitting integers into smaller components and treating those components as polynomial coefficients.

The algorithm consists of five main steps:

  1. Splitting
  2. Evaluation
  3. Pointwise multiplication
  4. Interpolation
  5. Recomposition

This section describes Toom-3 multiplication, a particular form of Toom–Cook multiplication in which each integer is divided into three parts.

Splitting

[edit]

Let m and n be two integers that we want to multiply.

Choose a splitting base B, typically a power of the underlying number base. Express both integers as:

m=m2B2+m1B+m0
n=n2B2+n1B+n0

where mi and ni represent the individual chunks of the integers.

For example, an integer can be divided into three chunks:

m=123456789012
m=123456B2+78901B+2

The chunks are then treated as coefficients of two quadratic polynomials:

p(x)=m2x2+m1x+m0
q(x)=n2x2+n1x+n0

These polynomials have the property:

p(B)=m
q(B)=n

Therefore, the original multiplication can be expressed as:

mn=p(B)q(B)

Evaluation

[edit]

The product of two quadratic polynomials is a polynomial of degree four. Therefore, five evaluation points are sufficient to reconstruct the resulting polynomial.

Toom-3 uses the following evaluation points:

x∈{0,1,−1,−2,∞}

The two polynomials are evaluated at each point.

For p(x):

p(0)=m0p(1)=m0+m1+m2p(−1)=m0−m1+m2p(−2)=m0−2m1+4m2p(∞)=m2

Similarly, for q(x):

q(0)=n0q(1)=n0+n1+n2q(−1)=n0−n1+n2q(−2)=n0−2n1+4n2q(∞)=n2

Evaluation at infinity refers to the leading coefficient of the polynomial.

Pointwise multiplication

[edit]

Once the polynomial evaluations have been calculated, multiply the corresponding values together.

Define the resulting products as:

r(0)=p(0)q(0)r(1)=p(1)q(1)r(−1)=p(−1)q(−1)r(−2)=p(−2)q(−2)r(∞)=p(∞)q(∞)

Each multiplication involves smaller integers than the original operands.

For sufficiently large integers, these multiplications can themselves be performed recursively using Toom-Cook.

Interpolation

[edit]

After pointwise multiplication, five values of the resulting polynomial are known.

The product polynomial has the form:

r(x)=r4x4+r3x3+r2x2+r1x+r0

The objective of interpolation is to recover the five unknown coefficients.

First, the constant and leading coefficients are obtained directly:

r0=r(0)
r4=r(∞)

Next, calculate the following intermediate values:

s1=r(1)−r0−r4
s−1=r(−1)−r0−r4

Then calculate:

r2=s1+s−12
t=s1−s−12

Next:

u=r(−2)−r0−16r42

The remaining coefficients are:

r3=u−2r2−t3
r1=t−r3

All five coefficients of the product polynomial have now been recovered.

Recomposition

[edit]

The final step is to substitute the original splitting base B into the reconstructed polynomial.

Recall that:

m=p(B)
n=q(B)

Therefore, the final product is:

mn=r(B)

Expanding this expression gives:

mn=r4B4+r3B3+r2B2+r1B+r0

This reconstructs the original integer product.

Computational Complexity

[edit]

Toom-3 divides each operand into three approximately equal parts and performs five recursive multiplications.

Its recurrence relation is:

T(n)=5T(n/3)+O(n)

Using the Master Theorem:

T(n)=O(nlog35)

Since:

log35≈1.465

the asymptotic time complexity is:

T(n)=O(nlog35)=O(n1.465)

For comparison:

Algorithm Time complexity
Schoolbook multiplication O(n2)
Karatsuba multiplication O(nlog23)
Toom-3 multiplication O(nlog35)

Summary

[edit]

Toom-3 multiplication reduces the number of recursive multiplication operations by representing integers as polynomials and using polynomial interpolation.

The five main stages are:

  1. Splitting the integers into three parts.
  2. Evaluating the corresponding polynomials at five points.
  3. Multiplying the evaluated values.
  4. Interpolating the resulting polynomial coefficients.
  5. Reconstructing the original integer product.

The main advantage of Toom-Cook multiplication is that it exchanges expensive multiplication operations for comparatively inexpensive additions, subtractions and divisions by small constants.

Asymmetric splitting

[edit]

The interpolation step depends on the degree of the product polynomial, which is the sum of the degrees of the factor polynomials, but not on the individual degrees. While basic Toom-3 divides each factor into 3 parts (quadratic polynomials), if the factors differ in size, it can be beneficial to split one into 2 parts (a linear polynomial) and the other into 4 parts (a cubic) with coefficients of more balanced size. Then the same interpolation step can produce a quartic product polynomial

For Toom-3, this is the only interesting alternative split (the degenerate case of splitting either factor into only 1 part offers no time saving), but higher-degree Toom-Cook allows additional possibilities.

In addition to the equal-split cases, it's possible to have half-integer cases which are always asymmetric; the total number of pieces is odd, and the degree of the product polynomial is even. For example, Toom-2.5 splits one factor into 2 parts and the other into 3, while Toom-3.5 can split the factors as 2+5 or 3+4.

Interpolation matrices for various k

[edit]

Here we give common interpolation matrices for a few different common small values of km and kn.

Toom-1

[edit]

Applying formally the definition, we may consider Toom-1 (km = kn = 1). This does not yield a multiplication algorithm, but a recursive algorithm that never halts, as it trivially reduces each input instance to a recursive call with the same instance. The algorithm requires 1 evaluation point, whose value is irrelevant, as it is used only to "evaluate" constant polynomials. Thus, the interpolation matrix is the identity matrix:

(1)−1=(1).

Toom-1.5

[edit]

Toom-1.5 (km = 2, kn = 1) is still degenerate: it recursively reduces one input by halving its size, but leaves the other input unchanged, hence we can make it into a multiplication algorithm only if we supply a 1 × n multiplication algorithm as a base case (whereas the true Toom–Cook algorithm reduces to constant-size base cases). It requires 2 evaluation points, here chosen to be 0 and ∞. Its interpolation matrix is then the identity matrix:

(1001)−1=(1001).

The algorithm is essentially equivalent to a form of long multiplication: both coefficients of one factor are multiplied by the sole coefficient of the other factor.

Toom-2

[edit]

Toom-2 (km = 2, kn = 2) requires 3 evaluation points, here chosen to be 0, 1, and ∞. It is the same as Karatsuba multiplication, with an interpolation matrix of:

(100111001)−1=(100−11−1001).

Toom-2.5

[edit]

Toom-2.5 (km = 3, kn = 2) requires 4 evaluation points, here chosen to be 0, 1, −1, and ∞. It then has an interpolation matrix of:

(100011111−11−10001)−1=(1000012−12−1−1121200001).

Notes

[edit]
  1. ↑ Knuth, p. 296
  2. ↑ Crandall & Pomerance, p. 474
  3. ↑ Crandall & Pomerance, p. 536
  4. ↑ Knuth, p. 302
  5. ↑ Positive Results, chapter III of Stephen A. Cook: On the Minimum Computation Time of Functions.

References

[edit]
  • D. Knuth. The Art of Computer Programming, Volume 2. Third Edition, Addison-Wesley, 1997. Section 4.3.3.A: Digital methods, pg.294.
  • R. Crandall & C. Pomerance. Prime Numbers – A Computational Perspective. Second Edition, Springer, 2005. Section 9.5.1: Karatsuba and Toom–Cook methods, pg.473.
  • Bodrato, Marco (2007). "Towards optimal Toom–Cook multiplication for univariate and multivariate polynomials in characteristic 2 and 0". In Carlet, Claude; Sunar, Berk (eds.). Arithmetic of Finite Fields, First International Workshop, WAIFI 2007, Madrid, Spain, June 21–22, 2007, Proceedings. Lecture Notes in Computer Science. Vol. 4547. Springer. pp. 116–133. doi:10.1007/978-3-540-73074-3_10. ISBN 978-3-540-73073-6.
  • Bodrato, Marco (August 8, 2011). "Optimal Toom-Cook Polynomial Multiplication / Toom Cook convolution, implementation for polynomials". Retrieved 22 September 2023.
[edit]
  • Toom–Cook 3-way multiplication from GMP documentation: "Toom 3-Way Multiplication". GNU MP multiple precision arithmetic library (version 6.3.0) manual. Free Software Foundation, Inc. 30 July 2023 [Copyright 1991, 1993-2016, 2018-2020].