
Mathematics and Algorithms for Computer Algebra Part 1 c 1992 Dr Francis J. Wright – CBPF, Rio de Janeiro July 9, 2003 3: Integer and rational arithmetic I now want to apply the basic notions of computational representations and abstract algebra that I have developed so far to concrete algorithms, and briefly to consider their complexity. As I explained earlier, underlying all of computer algebra are the integers, so I will begin by considering algorithms for performing arithmetic on integers of unlimited magnitude, which I will call long integers. It is then a very small step to extend this to arbitrary rational numbers. I will not consider any approximate representations, such as rounded representations of the reals, because they are not fundamental to CA and they lie more within the domain of numerical analysis. Given algorithms to manipulate long integers it is a small extension to manipulate rounded real representations of unlimited magnitude and precision. I will not explicitly consider complex numbers because they are a trivial extension of the real numbers, and similarly arithmetic on the integers mod m, which as we have seen is very important, is essentially a trivial extension of integer arithmetic. The definitive presentation of the material discussed in this set of notes is in Chapter 4 “Arithmetic” in Vol. 2 “Seminumerical Algorithms” of the clas- sic book The Art of Computer Programming by Donald E. Knuth (Addison- Wesley, 1981), which henceforth I will refer to simply as “Knuth”, and most of it is in section 4.3.1 “Multiple-precision arithmetic: The classical algo- rithms”. 1 Representation of long integers We have already seen how to do this in practice, by choosing a base B that conveniently fits into a word of memory, and then linking a sufficient 1 number of words together, either as a linked list or an array. There is a question about exactly what the base should be, to which we will return later. Any integer can be represented by its magnitude together with its sign. I will assume that the sign, which requires only one bit of information, is stored somewhere and can be accessed as required, and I will focus on the representation of non-negative integers. The manipulation of long integers essentially just formalizes the conventional techniques for performing integer arithmetic by hand using “long multiplication”, “long division”, etc. In order to understand the algorithms it is often useful to imagine the base B to be 10, even though the real intention is that the base will be more like 105 or 1010. The mathematics of this representation requires the lexicographic order- ing once again, but now in a purely numerical context. Each digit (compo- nent) of a long (unlimited magnitude) integer is itself a (small) integer, so a (long) integer is represented by a set of digits (small integers), but this set has an order corresponding to the positional representation used when writing integers conventionally. Hence we need to formalize the ordering on such sets of digits. 1.1 Lexicographic order Let E1,...,Er be ordered sets (e.g. each {0, 1, 2,...,B − 1}), and define the relation < on the Cartesian product set E = E1 × · · · × Er of r-tuples of the form x = (x1, . , xr) ∈ E by x < y if xi < yi for the first index i such that xi 6= yi. This is called the lexicographic order on E. For example, taking B = 10 as in conventional decimal representation, and r = 5 to allow integers with up to 5 digits, this ordering would give (1, 2, 3, ?, ?) < (1, 2, 4, ?, ?), where ? represents any digit, or expressed more conventionally and in words: 123 hundred and anything is less than 124 hundred and anything. [Note that if each Ei is an ordered set of variable identifiers together with 1 ordered so that xi > 1 ∀i and (x1, x2, . , xr) denotes the product (x1 × x2 × · · · × xr) then E is the set of all monomials of total degree at most r, and this lexicographic ordering is exactly the same as that which I described less formally in the first set of notes.] 2 The set E together with the lexicographic order relation is called the lexicographic product of the sets Ei. If each Ei is totally ordered then so is E. 1.2 Existence and uniqueness of the representation Theorem 1 Let B be an integer > 1, and for k a strictly positive integer let k B be the lexicographic product of k copies of the set B = {0, 1,...,B − 1}. k k Then the map fk : B → {0, 1,...,B − 1} defined by k−1 X k−i−1 fk :(d0, d1, . , dk−1) 7→ diB i=0 is an isomorphism. Proof is by induction on k. 2 This shows how to go from a representation to the integer that it rep- resents. To go the other way we need the observation that for any integer a > 0 there is a smallest integer k > 0 such that a < Bk. Then according to the above theorem a has a unique representation of the form a = (d0, d1, . , dk−1)B, (d0 6= 0). The (small) integers di are called the digits of a in base B; the base is normally only indicated (as a subscript) when necessary to avoid ambiguity. Finally, the following bounds will be useful later. Proposition 2 If the positive integer a admits the following representation in base B: k X i a = aiB , with ak 6= 0, i=0 then k k k+1 B ≤ akB ≤ a ≤ B − 1. [Beware of the change of notation here!] Proof The only non-trivial inequality is the last one, which follows from the observation that k k+1 ai ≤ B − 1 ⇒ a ≤ (B − 1)(1 + B + ··· + B ) = B − 1. 2 3 1.3 Number bases The most commonly used bases are 10, giving the decimal or denary sys- tem, 2 giving the binary system, 8 giving the octal system and 16 giving the hexadecimal system. Modern computers normally use binary internally, al- though because binary representation generates rather long strings of digits it is common for computer programmers to use hexadecimal or octal nota- tion. The smallest addressable amount of memory is usually 8 bits, called a byte, and one hexadecimal digits corresponds to 4 bits, which is sometimes called a nibble (or nybble) (because “nibble” means a small “bite” in the more conventional context of eating food). Therefore one byte is conve- niently represented by two hexadecimal digits. However, we only have con- ventional symbols for 10 digits, whereas hexadecimal representation needs 16 digits, so these are represented as {0, 1, 2,..., 9, a, b, . , f}. Octal repre- sentation has the advantage that it requires only (the first 8) conventional digit symbols. (In fact, many computers can also work in decimal internally using a representation called binary coded decimal (BCD), in which nybbles are used to store only decimal digits. If not much arithmetic is performed then BCD avoids the input-output overhead of converting from decimal to binary and back again, and may therefore be more efficient, but there is no floating- point analogue of BCD and it is used in commerce rather than in science.) When a representation requires more memory than is available it is said to overflow, which can easily happen in the conventional representation of numbers using a fixed number of words. In our flexible multi-word repre- sentation it would correspond to completely running out of memory, which is certainly possible. However, it is important to avoid conventional over- flow during arithmetic calculations with multi-word integers. Addition (and hence subtraction) of two integers in any base can produce at most one extra digit, and so a choice of base that leaves at least one (small) digit position free in each word of the representation is necessary. Thus if the word size of a computer is n bits the base should use at most n − 1 of them, and so we require B ≤ 2n−1. This implies a maximum digit size of 2n−1 − 1, hence we can add two maximal digits to get 2n − 2 which is less than the maximum (small) integer that can be stored in an n-bit word, namely 2n − 1. This choice of base is the most storage-efficient among reasonable choices, but it causes difficulty in multiplication, because the product of two (n−1)- bit digits can have up to 2n − 2 bits! Using only half of the available bits to avoid this problem does not make efficient use of memory, so the solution is 4 to take care when designing the multiplication algorithm, to which we will return below. 1.4 Converting between representation and long integer To convert from a representation a = (a0, a1, . , ak−1) to the integer that it represents is simply a matter of evaluating the sum k−1 X k−i−1 a = aiB . i=0 Note that this is the same as the standard numerical task of evaluating a (univariate) polynomial, and the efficient way to do it is exactly the same. For example, to numerically evaluate the polynomial 3 2 a0x + a1x + a2x + a3 one writes it in Horner’s nested form as ((a0x + a1)x + a2)x + a3, which (generally) involves the minimum number of multiplications, and (as always) evaluate from the inside of the nest of parentheses outwards. In the general case, this evaluation scheme is expressed by the following algorithm. I will write algorithms in an algorithmic pseudo-language that is similar to Pascal and REDUCE, and not unlike Maple, C, etc.
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages33 Page
-
File Size-