For many calculation algorithms (division of "BigInts", division of polynomials, division of power series, ...) it is desirable to compute efficiently the (multiplicative) inverse of an element b of some ring R. Here's a fairly general method, that works in many real problems. Like the Babylonian square root algorithm, it can be derived from the Newton-Raphson method; also like it, you don't have to use the iteration.
We assume we have some norm |.|:R→Q+ (or sometimes to the real numbers), i.e. just a function satisfying
Now, if you've glanced at the Newton-Raphson method, you'll know that it requires division. How can we use an algorithm which requires division in order to implement division? We'll arrange matters so all divisions in the algorithm cancel out, leaving us with just multiplications and additions.
Let b∈R; we shall compute 1/b. We decide to solve the equation 1/x-b=0. Dumbly applying the rules for deriving the Newton-Raphson iteration (after all, derivatives might not mean anything in our ring R!), we see:
So our desired iteration will be
xn - f(xn)/f'(xn) =
Why does it work?
We shall prove that this iteration converges (and rapidly, at a quadratic pace!). So we can ignore the fact that we used the Newton-Raphson method (with somewhat shaky justification) to derive our iteration -- we shall just prove ``it works''.
When can we guarantee convergence?
So we're guaranteed quadratic convergence if |x0
Let R=Q((Z)) be the Laurent field of formal power series with rational coefficients. A good choice of "norm" is
v(p(Z)) = the lowest power of Z appearing in p(Z)
|p(Z)| = 2-v(p(Z))
"Convergence" in this norm means agreement of increasing numbers of low-order terms.
We show how to compute the inverse of a power series of norm 1. For general p(Z), just note that we can express the inverse based on the inverse of such a power series:
1/p(Z) = Zv(p(Z))*1/(Z-v(p(Z))p(Z)).
And if v(p(Z))=1, it turns out that a good initial guess is p(0), the coefficient of the 1's term in p(Z).
Let us compute the inverse of Z2+1. The first few iterations:
- a0(Z) = 1 (the coefficient of the 1's term).
- a1(Z) = 2a0(Z)-(Z2+1)a0(Z)2 =
- a2(Z) =
Indeed, every iteration doubles the number of correct terms! (The correct answer is 1-Z2
Polynomials mod ZN
A more practical setting is used when dividing polynomials. We want to find the multiplicative inverse of a polynomial b(Z) modulo ZN (for more information, see the full algorithm under polynomial division). This exists (when working over a field; for polynomials over a ring we require b(0) invertible...) iff b(0)≠0.
We proceed exactly as in the power series case. Except that now there cannot be norms smaller than 2-N. So we may conclude our iteration after log2(N) iterations (rounded up), knowing we hold the precise answer.
Integers mod 2N
Suppose you're trying to divide huuuge numbers. Computer arithmetic is usually performed modulo 2N, so we shall do so here, too. The 2-adic norm will be our norm; we can invert any odd number.
For instance, to invert 13 modulo 2^16:
- a0=1 (≥1 bit of accuracy)
- a1=-11≡65525 (mod 2^16)=11111111111101012 (≥2 bits of accuracy; in fact, 4 bits!)
- a2=-1595≡63941 (mod 2^16)=11111001110001012
- a3≡20165=1001110110001012 is the correct answer.
Some square matrices have an inverse. We can compute it rapidly. Suppose we want to invert a matrix of real or complex numbers B. If the matrix norm |I-B-1|<1, then by the argument above the algorithm, started from A0=I, will converge.
But many ("most"!) nonsingular matrices B are not of this form. Still, this is the basis for a very strong method of inverting a matrix.