The.Algorithm.Design.Manual.Springer-Verlag.1998
The.Algorithm.Design.Manual.Springer-Verlag.1998 The.Algorithm.Design.Manual.Springer-Verlag.1998
Arbitrary-Precision Arithmetic addition: (A - (-B)) = (A+B). The tricky part of subtraction is performing the borrow. This can be simplified by always subtracting from the number with the larger absolute value and adjusting the signs afterwards, so we can be certain there will always be something to borrow from. ● Multiplication - The simplest method of repeated addition will take exponential time on large integers, so stay away. The digit-by-digit schoolhouse method is reasonable to program and will work much better, presumably well enough for your application. On very large integers, Karatsuba's divide-and-conquer algorithm (cited in the notes) wins. Dan Grayson, author of Mathematica's arbitrary-precision arithmetic, found that the switch-over happened at well under 100 digits. Even faster on very large integers is an algorithm based on Fourier transforms. A discussion of such algorithms appears in Section . ● Division - Repeated subtraction will take exponential time, so the easiest reasonable algorithm to use is the long-division method you hated in school. This is a far more complicated algorithm than needed for the other operations, requiring arbitrary-precision multiplication and subtraction as subroutines, as well as trial and error to determine the correct digit to use at each position of the quotient. In fact, integer division can be reduced to integer multiplication, although in a nontrivial way, so if you are implementing asymptotically fast multiplication, you can reuse that effort in long division. See the references below for details. ● Exponentiation - We can compute in the obvious manner using b-1 multiplications, but a better way is to exploit the fact that . By repeatedly squaring the results of our partial product, we can escape using multiplications, a big win when b is large. See Section for a discussion of this algorithm. High- but not arbitrary-precision arithmetic can be conveniently performed using the Chinese remainder theorem and modular arithmetic. The Chinese remainder theorem states that an integer between 1 and is uniquely determined by its set of residues mod , where each are relatively prime integers. Addition, subtraction, and multiplication (but not division) can be supported using such residue systems, with the advantage that large integers can be manipulated without complicated data structures. Many of these algorithms for computations on long integers can be directly applied to computations on polynomials. See the references for more details. A particularly useful algorithm is Horner's rule for fast polynomial evaluation. When is blindly evaluated term by term, multiplications will be performed. Much better is observing that , the evaluation of which uses only a linear number of operations. Implementations: All major commercial computer algebra systems incorporate high-precision arithmetic, including Maple, Mathematica, Axiom, and Macsyma. If you have access to one of these, this is your best option for a quick, nonembedded application. The rest of this section focuses on source code available for embedded applications. file:///E|/BOOK/BOOK4/NODE144.HTM (3 of 5) [19/1/2003 1:30:30]
Arbitrary-Precision Arithmetic PARI, developed by Henri Cohen and his colleagues in France, is a system capable of handling complex number-theoretic problems on integers with up to 300,000 decimal digits, as well as reals, rationals, complex numbers, polynomials, and matrices. It is probably the most powerful free software available for number theory. Written mainly in C (with assembly-language code for speed-critical routines), it includes more than 200 special predefined mathematical functions. PARI can be used as a library, but it possesses also a powerful calculator mode that gives instant access to all the types and functions. The main advantage of PARI is its speed. On a Unix platform, it runs between 5 to 100 times faster than Maple or Mathematica, depending on the applications. PARI is available for PC, Amiga, Macintosh, and most Unix platforms by anonymous ftp at ftp://megrez.ceremab.u-bordeaux.fr/pub/pari/. Algorithm 693 [Smi91] of the Collected Algorithms of the ACM is a Fortran implementation of floating- point, multiple-precision arithmetic. See Section . An implementation of arbitrary-precision integer and rational arithmetic in C++ is embedded in LEDA (see Section ), including GCD, square roots, and logarithms as well as the basic four operations. Sparc assembler code is used for certain time-critical functions. Implementations in C of a high-precision calculator with all four elementary operations appear in [BR95]. The authors use base 10 for arithmetic and arrays of digits to represent long integers, with short integers as array indices, thus limiting computations to 32,768 digits. The code for these algorithms is printed in the text and available on disk for a modest fee. Bare bones implementations in C of high-precision multiplication and in Pascal of such special functions as logarithm and arctangent appear in [GBY91]. See Section for further details. Sedgewick [Sed92] provides a bare bones implementation of polynomial arithmetic in C++. See Section for details. Notes: Knuth [Knu81] is the primary reference on algorithms for all basic arithmetic operations, including implementations of them in the MIX assembly language. Bach and Shallit [BS96] provide a more recent treatment of computational number theory. Expositions on the -time divide-and-conquer algorithm for multiplication [KO63] include [AHU74, Man89]. An FFT-based algorithm multiplies two n-bit numbers in time and is due to Schönhage and Strassen [SS71]. Expositions include [AHU74, Knu81]. The reduction between integer division and multiplication is presented in [AHU74, Knu81]. Good expositions of algorithms for modular arithmetic and the Chinese remainder theorem include [AHU74, CLR90]. A good exposition of circuit-level algorithms for elementary arithmetic algorithms is [CLR90]. file:///E|/BOOK/BOOK4/NODE144.HTM (4 of 5) [19/1/2003 1:30:30]
- Page 359 and 360: Suffix Trees and Arrays [GBY91]. Se
- Page 361 and 362: Graph Data Structures algorithms).
- Page 363 and 364: Graph Data Structures including the
- Page 365 and 366: Set Data Structures Next: Kd-Trees
- Page 367 and 368: Set Data Structures parent pointers
- Page 369 and 370: Kd-Trees Next: Numerical Problems U
- Page 371 and 372: Kd-Trees about p. Say we are lookin
- Page 373 and 374: Numerical Problems Next: Solving Li
- Page 375 and 376: Numerical Problems Mon Jun 2 23:33:
- Page 377 and 378: Solving Linear Equations algorithm
- Page 379 and 380: Solving Linear Equations Matrix inv
- Page 381 and 382: Bandwidth Reduction images near eac
- Page 383 and 384: Matrix Multiplication Next: Determi
- Page 385 and 386: Matrix Multiplication The linear al
- Page 387 and 388: Determinants and Permanents Next: C
- Page 389 and 390: Determinants and Permanents exposit
- Page 391 and 392: Constrained and Unconstrained Optim
- Page 393 and 394: Constrained and Unconstrained Optim
- Page 395 and 396: Linear Programming variable assignm
- Page 397 and 398: Linear Programming The book [MW93]
- Page 399 and 400: Random Number Generation Next: Fact
- Page 401 and 402: Random Number Generation is largely
- Page 403 and 404: Random Number Generation Related Pr
- Page 405 and 406: Factoring and Primality Testing The
- Page 407 and 408: Factoring and Primality Testing Alg
- Page 409: Arbitrary-Precision Arithmetic If y
- Page 413 and 414: Knapsack Problem Next: Discrete Fou
- Page 415 and 416: Knapsack Problem is a subset of S'
- Page 417 and 418: Discrete Fourier Transform Next: Co
- Page 419 and 420: Discrete Fourier Transform an algor
- Page 421 and 422: Combinatorial Problems Next: Sortin
- Page 423 and 424: Sorting Next: Searching Up: Combina
- Page 425 and 426: Sorting The simplest approach to ex
- Page 427 and 428: Sorting operations, implying an sor
- Page 429 and 430: Searching large performance improve
- Page 431 and 432: Searching Notes: Mehlhorn and Tsaka
- Page 433 and 434: Median and Selection followed by fi
- Page 435 and 436: Generating Permutations Next: Gener
- Page 437 and 438: Generating Permutations The rank/un
- Page 439 and 440: Generating Permutations generating
- Page 441 and 442: Generating Subsets look right when
- Page 443 and 444: Generating Subsets above for detail
- Page 445 and 446: Generating Partitions Although the
- Page 447 and 448: Generating Partitions Two related c
- Page 449 and 450: Generating Graphs generate: ● Do
- Page 451 and 452: Generating Graphs Combinatorica [Sk
- Page 453 and 454: Calendrical Calculations Next: Job
- Page 455 and 456: Calendrical Calculations Gregorian,
- Page 457 and 458: Job Scheduling ● To assign a set
- Page 459 and 460: Job Scheduling shop scheduling incl
Arbitrary-Precision Arithmetic<br />
addition: (A - (-B)) = (A+B). <strong>The</strong> tricky part of subtraction is performing the borrow. This can be<br />
simplified by always subtracting from the number with the larger absolute value and adjusting the<br />
signs afterwards, so we can be certain there will always be something to borrow from.<br />
● Multiplication - <strong>The</strong> simplest method of repeated addition will take exponential time on large<br />
integers, so stay away. <strong>The</strong> digit-by-digit schoolhouse method is reasonable to program and will<br />
work much better, presumably well enough for your application. On very large integers,<br />
Karatsuba's divide-and-conquer algorithm (cited in the notes) wins. Dan Grayson, author<br />
of Mathematica's arbitrary-precision arithmetic, found that the switch-over happened at well under<br />
100 digits. Even faster on very large integers is an algorithm based on Fourier transforms. A<br />
discussion of such algorithms appears in Section .<br />
● Division - Repeated subtraction will take exponential time, so the easiest reasonable algorithm to<br />
use is the long-division method you hated in school. This is a far more complicated algorithm<br />
than needed for the other operations, requiring arbitrary-precision multiplication and subtraction as<br />
subroutines, as well as trial and error to determine the correct digit to use at each position of the<br />
quotient.<br />
In fact, integer division can be reduced to integer multiplication, although in a nontrivial way, so if<br />
you are implementing asymptotically fast multiplication, you can reuse that effort in long division.<br />
See the references below for details.<br />
● Exponentiation - We can compute in the obvious manner using b-1 multiplications, but a better<br />
way is to exploit the fact that . By repeatedly squaring the results of our partial<br />
product, we can escape using multiplications, a big win when b is large. See Section<br />
for a discussion of this algorithm.<br />
High- but not arbitrary-precision arithmetic can be conveniently performed using the Chinese remainder<br />
theorem and modular arithmetic. <strong>The</strong> Chinese remainder theorem states that an integer between 1 and<br />
is uniquely determined by its set of residues mod , where each are relatively prime<br />
integers. Addition, subtraction, and multiplication (but not division) can be supported using such residue<br />
systems, with the advantage that large integers can be manipulated without complicated data structures.<br />
Many of these algorithms for computations on long integers can be directly applied to computations on<br />
polynomials. See the references for more details. A particularly useful algorithm is Horner's rule for fast<br />
polynomial evaluation. When is blindly evaluated term by term, multiplications<br />
will be performed. Much better is observing that , the evaluation of which uses only a linear number of<br />
operations.<br />
Implementations: All major commercial computer algebra systems incorporate high-precision<br />
arithmetic, including Maple, Mathematica, Axiom, and Macsyma. If you have access to one of these,<br />
this is your best option for a quick, nonembedded application. <strong>The</strong> rest of this section focuses on source<br />
code available for embedded applications.<br />
file:///E|/BOOK/BOOK4/NODE144.HTM (3 of 5) [19/1/2003 1:30:30]