Fast Exponentiation by Repeated Squaring
In questa pagina 5
Let be a domain and an associative operator, , with identity (). Examples: with ; matrices with the row-by-column product, . need not be commutative.
Definition (power). for ; for (by associativity the two forms agree, also for non-commutative ).
Cost model: per application of , for the rest.
Definition-based algorithm
STUPID_POWER(x, n)
if n = 0 then return e
p <- x
for i <- 1 to n - 1 do
p <- x ∘ p
return p: linear in the exponent. We want better.
Substructure
Property. With : if then ; if then .
Proof. and , grouping the factors by associativity; no other hypothesis on .
Given , one application of if is even (), two if odd (). Algorithms of this kind are called squaring algorithms.
Recursive algorithm POWER
POWER(x, n)
if n = 0 then return e { BASE }
if n = 1 then return x
k <- floor(n / 2) { DIVIDE: no work }
y <- POWER(x, k) { RECURSE }
if EVEN(n) then return y ∘ y { CONQUER }
else return y ∘ y ∘ xThe base case is superfluous but avoids two useless applications (). Correctness follows from the substructure property.
Analysis
Conquer costs at most , Divide :
For every generated instance is odd, so is paid at every level (the bound is attained). The recurrence has , , : threshold , Case 2 of the Master TheoremThe Master Theorem for T(n) = aT(n/b) + w(n) compares w(n) with the threshold n^(log_b a). Case 1 (w polynomially smaller): Theta(n^(log_b a)); case 2 (same order): Theta(n^(log_b a) log n); case 3 (polynomially larger and regular): Theta(w(n)). Full proof from the levels of the tree, extended version with floors and ceilings, a table of applications, and the misuse when no case applies (w = n log n).Master Theorem →, so (same recurrence as binary search): an exponential improvement over STUPID_POWER.
Exact count. Let be the number of bits of . For : Proof (induction on ). : . For even : , with and , so . For odd : with and : .
That is squarings (one per level) plus multiplications by . On the minimum is () and the maximum (). For the general formula with , gives .
Example (). : POWER(x,3) (2 operations), POWER(x,6) (1), POWER(x,13) (2). Total , against for STUPID_POWER.
Cost-model remark. Unit cost is reasonable for sums and products of integers or reals, not for operators whose results are larger than the operands (set union, polynomial product, integers with huge numbers of digits): there the analysis needs an adequate cost model.
Iterative algorithms
With in binary, , associativity gives , and .
POWER2(x, n)
if n = 0 then return e
* n = b_k b_{k-1} ... b_0 *
p <- e { accumulates the powers x^(2^i) with b_i = 1 }
s <- x { at the start of iteration i: s = x^(2^i) }
for i <- 0 to k do
if b_i = 1 then p <- p ∘ s
s <- s ∘ s
return pLiteral count: squarings and products: , .
The bits are not needed: use the parity test and integer division with :
POWER3(x, n)
p <- e; s <- x; m <- n
for i <- 0 to k do
if NOT EVEN(m) then p <- p ∘ s
s <- s ∘ s
m <- floor(m / 2)
return pPOWER3 performs the same operations as POWER, bottom-up: only the Conquer phases, skipping the top-down generation of the tree.
| after the step | after the step | ||
|---|---|---|---|
| 0 | 1 | ||
| 1 | 0 | ||
| 2 | 1 | ||
| 3 | 1 | (useless) |
Precise count. Literally, POWER3 performs applications (for : versus ): the first with produces nothing new, and the last squaring computes an unused power. With two optimizations (start from at the first bit, do not square after the last bit) the counts coincide.
Example with matrices: Fibonacci numbers
With the product of matrices and : (induction, ). For : , so . products of matrices give (compare with recursion and memoization in dynamic programming). Caution: has bits, so scalar operations are not unit cost for large . For general matrices, needs products, each scalar operations (Matrix Multiplication - Iterative and Recursive Block ProductCost model on n x n matrices (scalar operations, size n or N = n^2); sum in n^2 operations (optimal); iterative row-by-column product with exact cost 2n^3 - n^2; the fractal property of the block product proved on indices; recursive algorithm RMUL with T(n) = 8T(n/2) + n^2, Theta(n^3) and exact solution 2n^3 - n^2: it performs the same operations as the iterative one.Matrix Multiplication - Iterative and Recursive Block Product →) or fewer with Strassen's AlgorithmStrassen computes the n x n product with 7 recursive calls instead of 8, using 10 matrix additions in Divide and 8 in Conquer: T(n) = 7T(n/2) + (9/2)n^2, Theta(n^(log2 7)) with exact solution 7n^(log2 7) - 6n^2. Its leading constant 7 is more than three times that of the iterative algorithm (2), so Strassen beats it only from n = 655 (from 1024 for powers of two).Strassen's Algorithm →; for structured (circulant) matrices the product is cheaper (Supercirculant and Circulant MatricesSupercirculant matrices (alternating recursive quadrants) have only n distinct values: sum in n operations, product in 4n log2 n + n with the trick (A1+A2)(B1+B2) and (A1-A2)(B1-B2) instead of 4 calls (Theta(n^2)); their eigenvalues are the Hadamard transform of the first row (FHT, n log2 n). Circulant matrices are polynomials in the rotation matrix R, their product is the cyclic convolution of the first rows, and the Fourier matrix diagonalizes them: product in O(n log n) with the FFT.Supercirculant and Circulant Matrices →). The same scheme applied to polynomials, with convolution as : Notable Transforms and Polynomial ExponentiationSome vector families have a known or cheaper DFT than the general FFT: the constant vector (a,...,a) has DFT (na,0,...,0), the vector (a,0,...,0) has DFT (a,...,a); an (n,k)-sparse vector has a DFT made of k copies of the DFT_{n/k} of its components at multiples of k, and SPARSE_FFT costs 2 (n/k) log2(n/k) with recurrence T(n,k) = T(n/2,k/2). For the power p(x)^k of a polynomial with n coefficients: either repeated squaring with LIN_CONV, O(nk log(nk)), or evaluate p with the FFT on h = 2^ceil(log2((n-1)k+1)) points, raise each value to the k, and invert, Theta(nk log(nk)).Notable Transforms and Polynomial Exponentiation →.
Versione ripasso
- Setting: associative with identity (non-commutative allowed, e.g. matrix product with ); , . Cost = number of applications of .
STUPID_POWER: , then times : .- Substructure: ; ( even), ( odd). Proof: , grouped by associativity. From : application (even) or (odd).
POWER: base (), (, avoids two useless applications); Divide ;POWER; Conquer or .- Recurrence: , . , , , threshold : Case 2, (Master TheoremThe Master Theorem for T(n) = aT(n/b) + w(n) compares w(n) with the threshold n^(log_b a). Case 1 (w polynomially smaller): Theta(n^(log_b a)); case 2 (same order): Theta(n^(log_b a) log n); case 3 (polynomially larger and regular): Theta(w(n)). Full proof from the levels of the tree, extended version with floors and ceilings, a table of applications, and the misuse when no case applies (w = n log n).Master Theorem →); binary-search recurrence. The bound per level is attained by (all odd).
- Exact: , = number of bits.
- Induction: base gives .
- Even: , , .
- Odd: , .
- So squarings and multiplications by . Minimum (), maximum ().
- : , operations (against ); : .
- Iterative: , , .
POWER2: , ; for : if then ; then .- Literal cost .
POWER3: bits viaEVENand , initially.- : ; .
- The literal code does more applications than
POWER( at the first bit and the last useless squaring); with at the first bit and no last squaring they coincide.
- Fibonacci: , , with products ( gives ) if scalars are unit cost.
- Common mistakes: unit cost with growing operands; omitting the base case ; counting instead of bits; thinking commutativity is needed (associativity suffices).