An algorithm for polynomial operations

An algorithm for polynomial operations1
By C. L. Hamblinf
POLYGEORGE is an interpretive program for the English Electric KDF9 computer, providing
facilities for calculations with polynomials in accordance with a new algorithm, which is economical, fast, and simple enough to be realized in hardware. The program and its algorithm are
described and some extensions are discussed.
Recent interest in the mechanization of operations in
polynomial algebra has led to the production of programs such as ALPAK, FORMAC, Formula ALGOL
(with associated translator), GRAD Assistant and PM.
(See the survey in Comm. ACM, Vol. 9, No. 8, August
1966; and Brown, Hyde and Tague, 1962-3, Williams,
1962, and Collins, 1966a.) These programs provide for
the handling of polynomials in one or several variables
and for operations on them such as addition, subtraction, multiplication, differentiation, substitution for
variables, and calculation of residues. The aim of these
programs has usually been simply to provide assistance
in the (laborious and error-prone) task of calculating
with algebraic expressions. More ambitious aims are
possible, however, since the economical mechanization
of certain systematic approaches to algebra, such as
that provided by Tarski's decision method (Tarski, 1948;
Seidenberg, 1954; Collins, 19666), could open up an
entirely new field to the computer.
List-processing techniques have generally commended
themselves in this field. They suffer from the inherent
disadvantage of relative slowness, mainly due to the
continual manipulation of non-germane next-item
addresses. On the other hand, to operate with detached
coefficients—in effect, with matrices—is extremely inefficient in the common case in which most of the elements
of a matrix are zero, and is suitable mainly for a theoretically restricted range of problems in which numbers
are rounded-off to fixed lengths. In the case of many
important applications the operations must be exact,
in the sense that no rounding-off of numbers occurs at
all.
In the algorithm to be described, polynomials are
represented as strings of symbols some of which are
letters and some numbers in a positional notation.
Discussion will at first be limited to the case in which
the numbers are integers.
descending powers of b, and so on. The algorithm to
be described is for the operatorless representation of a
polynomial in this form. Let us, as a first example,
consider a polynomial in a alone, with integer coefficients; such as
8a4 + 6a2 + 13a + 5.
If, for uniformity, we write a1 for a where it occurs
without another exponent, and write coefficients after
the powers of a instead of before, we get
a 4 .8 + a 2 . 6 + a 1 . 1 3 + 5 .
There is now no difficulty in dropping operators: using
a comma after each number to avoid having numbers
run together, we have
a4, 8, a2, 6, al, 13, 5.
The end of the polynomial is sensed by the occurrence
of a term which is a simple number. It follows that we
must always write such a constant term; if necessary,
that is, we must write a zero. The characteristic of a
complete polynomial is that the number of numbers in
it shall be one more than twice the number of letters:
incomplete polynomials have a surplus of letters.
This characteristic is, moreover, preserved if we
permit coefficients to be themselves polynomials. For
example, the polynomial
a3(4b2 + 3) + 6a + 1
becomes
a3, bl, 4, 3, al, 6, 1
where bl, 4, 3, represents the coefficient of a3. In
general, we may define a polynomial in standard form
recursively as
(polynomial) = (number)
/(letter) (number) (polynomial)
(polynomial)
Internal representation of polynomials
Within the POLYGEORGE program, polynomials
are handled in a standard form, which is preserved by
all operations. In the first place, assuming a particular
ordering a, b, c, . . . of the variables that may occur in
them, they are arranged as sums of terms in descending
powers of a, with coefficients as sums of terms in
A formula in this form would become a late-operator
Forward Polish formula (Hamblin, 1962) if each letter
were preceded by three operators + x f. It is not
difficult to find algorithms for inserting operators in
positions yielding other Polish or orthodox forms.
* The work described was carried out as part of a project supported by the Australian Research Grants Committee. The author
wishes to acknowledge the programming assistance of Mr. M. C. Newey.
t University of New South Wales, Kensington, N.S.W., Australia.
168
Polynomial operations
if found, check whether the number being calculated is
a coefficient (rather than an addend); and, if so, cancel
the zero and the preceding number and letter from the
output. To make possible the test for a numerical
coefficient it is necessary to count numbers and letters
in the output in accordance with a subsidiary algorithm:
this must allow for the fact that cancelling one term
may bring another number into coefficient position, but
does not raise any other question of difficulty.
As polynomials are actually written in the present
application there are the following further restrictions:
(1) the first polynomial on the right of the above
definition—that is, the "coefficient polynomial"—
must not be zero and must not contain the letter
of which it is a coefficient, or any alphabetically
earlier letter;
(2) the second or "addend" polynomial must not
contain any letter alphabetically earlier than the
letter in the term of which it is an addend, and
may contain this letter only to lower degree
(the "degree" of any occurrence of any letter is,
of course, given by the number following it);
and
(3) any number which immediately follows a letter
(i.e. any exponent) must be a positive integer.
Multiplication
It is convenient to think of polynomials as made up
of simple terms, each consisting of (possibly) a product
of exponentiated letters and (in any case) just one
numerical coefficient: thus, for example, the polynomial
a\3b3 + 8) + 3a26 + 9
contains the four simple terms 3a4b3, 8a4, 3a2b and 9.
In multiplying two polynomials we need, in one way
or another, to multiply every simple term of the first by
every simple term of the second, and add up the results.
There are, however, different ways in which the program
for doing this might be arranged.
The simplest program seems to arise from breaking
down one multiplicand into its simple terms and multiplying them successively into the other multiplicand,
accumulating the result using the addition algorithm
already described. The accumulated partial product
needs to be rewritten, however, with the possible exception of some initial terms, each time something is added
to it; and this not only makes the method a slow one
but involves relatively heavy demands on storage.
The alternative is, at each stage, to collect together
all those products of simple terms which contribute to
a given simple term in the answer. As the polynomials
are arranged, this involves scanning the one forwards
as the other is scanned backwards. For example, in
multiplying
Addition
The representation of a polynomial under these rules
is unique, and is subject to a particularly simple and
easily mechanizable addition algorithm.* First of all,
let us define precedence of polynomials as follows:
(a) a polynomial commencing with a letter takes
precedence over one commencing with a number;
(b) in the case of polynomials commencing with
different letters the one with alphabetically earlier
letter takes precedence;
(c) in the case of polynomials commencing with the
same letter, one with higher value of following
number takes precedence over one with lower;
and
(d) in other cases, precedence is equal.
Now in adding two polynomials they are first compared for precedence.
(1) If one takes precedence a complete term is transferred to output as the first complete term of the
sum.
(2) If precedence is equal and both commence with a
letter, the letter and following number must be
the same in both, and are transferred to output
as the next part of the sum; and the two coefficient polynomials are now compared for precedence preparatory to being added, the operation
proceeding recursively.
(3) Finally, if both are numbers, they are added and
the sum is transferred to output. The process
continues until both polynomials are exhausted;
the final operation will be the addition of the
final numbers of the two polynomials.
a\3b2 + 4) + ab + 9
by
9a4 + a\8b3 + 5b1) + 2a2b + 2b2 + 1
there are contributions to the simple term in a3b2
from the product of a3. 3b2 and 1, the product of a3.4
and 2b2, the product of ab and 2a2b, and so on. If we
start by finding the simple term of highest precedence
in the answer, this may be written as first term of the
answer and remain unaltered by what follows; we then
proceed to the simple term of next highest precedence,
and so on.
This procedure will be helped if one of the two multiplicands is first written in a special reverse standard
form, so that both multiplicands may always be scanned
forwards. The form that recommends itself is less neat
than the standard form already described, and depends
on the sensing of letter-precedences and on a special
symbol to indicate the end of the formula.
The sum of two polynomials added in this way will
itself be a polynomial in standard form, with one qualification: namely, that it may contain zero coefficients.
To preserve strict standard form it is necessary, when
adding numbers under (3), to check for zero result and,
* Provisional patent
169
Polynomial operations
It may be derived as follows. If we first of all put in
zero powers of letters in the constant terms of any
polynomials in which they are principal letters, we can
write terms in ascending degrees instead of descending.
Thus:
becomes
written as
4a3 + 3a + 2
2a° + 3al + 4
aO,2, al, 3, a3, 4, #
' #" is the special end-symbol.
where
Coefficients may be polynomials instead of mere
numbers, and arranged in the same form, an end of a
coefficient being sensed by the occurrence of an alphabetically earlier letter. Thus:
4a3 + a(5b2 + 7) + 2
aO, 2, al, bO, 7, 62, 5, al, 4, # .
becomes
One further elaboration is desirable. Since the first
term of any polynomial is always a zero-degree one, the
zero is in fact redundant except as a place marker; and
since we frequently want, in scanning through, to know
the degree of polynomial, the zero may be replaced by a
copy of the largest of the exponents of the relevant
letter. The above example now becomes
ai, 2, al, b2, 1, b2, 5, a.3, 4, #.
Here the underlining, which is put in only for the sake
of illustration, indicates numbers which have been
inserted in place of zero exponents and which give the
degrees of the polynomials concerned.
In multiplying, one multiplicand is first rewritten in
this form by a preliminary program. The main multiplication program is a complicated one in detail, and
need not be further described here, but follows closely
the principles outlined.
Machine representation of polynomials
In the current use of this polynomial algorithm in a
program for the English Electric KDF9 machine a
variable and its exponent each occupy 24 bits of a
48-bit machine word, and each number other than an
exponent is represented in binary in one or more machine
words, with the least-significant first. To facilitate
numerical multiplication only 39 bits of each 48-bit
word store significant bits of the number; and the last
word of each number also has a sign-bit and a "last
word" marker bit.
general, handled in a nesting-store during the running
of the program.)
Polynomials may be written into the program, or
read in by it, in a form which is an approximation to
ordinary notation. They are immediately converted to
the standard form described. They may be "stored"
by means of instructions "=A", "=B", etc. which, in
effect, tag the polynomials with the names "A", "B",
etc. so that they may be recalled under these names as
required. The operations provided are addition; subtraction; sign-change; multiplication; exact division
(with failure if not possible); a "residue" operation
which guarantees non-failure by multiplying the dividend
by a suitable power of the leading coefficient of the
divisor; integral exponentiation; partial differentiation
with respect to any variable; substitution of polynomials for one or more variables in a given polynomial;
discrimination on whether a given polynomial is a pure
number, whether it is determinably zero, non-zero,
positive, etc.; and various more trivial operations such
as determination of degree in a given variable or selection
of a coefficient. The operation of substitution may be
used to substitute pure numbers (integers) for all the
variables in a polynomial and hence to calculate a "value"
of it. As a subsidiary operation, provision is made for
calculating a value more directly, using the normal
floating arithmetic of the machine, given values of the
variables (not necessarily integral) in standard Usercode
floating representations.
Polynomial multiplication and division times
In the following table multiplication times are for
two polynomials in the same variables and of the same
total degree, all terms being present and having small
positive integer coefficients c, 1 < c < 7. (The size of
coefficients does not affect times so long as accumulated
products do not exceed word-length.) Division times
are for re-division of the calculated product by one of
the factors. The column giving the number of terms in
the factor-polynomials permits more meaningful comparison of figures for different numbers of variables.
NUMBER OF TOTAL NUMBER OF
VARIABLES DEGREE
TERMS
(EACH
(EACH
(EACH
FACTOR)
FACTOR)
FACTOR)
1
Operations available in POLYGEORGE
The POLYGEORGE program permits the writing of
programs for polynomial manipulations in an interpretive language which in some ways resembles the
Usercode of the KDF9 machine, and are carried out as
if polynomials were being handled in a push-down
store. (In fact, it is polynomial addresses that are, in
170
MULTIPLICATION RE-DIVISION
TIME
TIME
(SEC ± 1 SEC) (SEC ± 1 SEC)
20
40
60
80
21
41
61
81
2
5
10
17
6
40
125
—
5
10
15
21
66
136
2
15
59
2
26
131
2
3
4
21
56
126
3
18
94
2
21
118
Polynomial operations
the general case of rational coefficients by letting all
coefficients be quotients of integers, preserving uniqueness, if desired, by cancelling common factors from
numerator and denominator. A generalization of a
similar kind would deal with the "field" of quotients of
polynomials, rather than with the "ring" of polynomials.
The operation of finding the common factor of two
polynomials is, however, a relatively time-consuming
one, and in many cases it would be wasteful to attempt
to secure uniqueness of representation by this means.
Certain generalizations of another kind—namely, to
the use of complex numbers and vectors—are relatively
straightforward and may be introduced as applications
demand.
The wealth of applications awaiting these developments justifies some attention to methods of securing
the highest possible speed of operation. The provision
of some special hardware should not be ruled out. In
this connection the algorithm described might prove
especially suitable as a basis.
Possible extensions
The limitation of coefficients to integer values is
unimportant for many purposes, since the case of
arbitrary rational values may often be dealt with by
multiplying a polynomial through by the lowest common
denominator of its coefficients, leaving roots and other
relevant properties unchanged. That this would sometimes lead to rather large integer coefficients is not a
reflection on the method itself so much as on the class
of problem that is being dealt with, since it is only in
simple contexts that truncation or rounding-off can be
guaranteed not to lead to intolerable error.
Certain generalizations which at first seem to offer
useful facilities turn out, on examination, to involve
theoretical problems. One of these, for example, is the
introduction of non-integral powers; this immediately
destroys the uniqueness of any system of representation,
since it is not an elementary matter to decide whether,
for example, a given polynomial has a square root.
It would be a relatively simple matter to accommodate
References
W. S., HYDE, J. P., and TAGUE, B. A. (1962-3). "The ALPAK system for non-numerical algebra on a digital computer",
B.S.T.J., Vol. 42, pp. 2081-2119; Vol. 43, pp. 785-804; Vol. 53, pp. 1547-1562.
COLLINS, G. E. (1966C). "PM, A System for Polynomial Manipulation", Comm. ACM, Vol. 9, No. 8.
COLLINS, G. E. (19666). "Subresultants and Reduced Polynomial Remainder Sequences", IBM Research Paper No. RC-1595.
HAMBLIN, C. L. (1962). "Translation to and from Polish notation", The Computer Journal, Vol. 5, pp. 210-13.
SEIDENBERG, A. (1954). "A New Decision Method for Elementary Algebra", Ann. Math., Vol. 60, pp. 365-74.
TARSKI, A. (1948). A Decision Method for Elementary Algebra and Geometry, Rand Corporation; 2nd edn., Berkeley and Los
Angeles, 1951.
WILLIAMS, LELAND H. (1962). "Algebra of Polynomials in Several Variables for a Digital Computer", /. ACM, Vol. 9, pp. 29-40.
BROWN,
long as you are prepared to ignore a finite subset of the
domain), are here extended to logics by Michael Arbib.
Problems of formal linguistics, also inseparable from automata theory, are the subject of authoritative contributions by
Schiitzenberger and others from the Institut Blaise Pascal; it
is perhaps a pity these were left untranslated. In a long and
interesting paper, Bohm and Gross introduce their description language CUCH, being an amalgam of CUrry combinatory logic and CHurch lambda-calculus. CUCH is a
powerful language of wide potential application, as is also
the system of Generalized Normal Algorithms described by
Caracciolo di Forino.
Something has gone very wrong with pages 119 to 121 in
Caracciolo's otherwise lucid paper; at this price should the
reader have to work quite so hard to restore the original text ?
Automata Theory, edited by E. R. Caianiello, 1966; 342 pages.
(New York: Academic Press Inc., 112s.)
This volume consists of some thirty papers presented at a
NATO Summer School in 1964 at Ravello. Although it is
hardly, as its title might suggest, that systematic survey of a
broad field which we are still lacking, it undoubtedly contains
much that is worth the attention of the computer scientist,
whatever his line of country. Buchi and Rabin, for instance,
provide clear expositions of finite automata theory, mainly
from the abstract algebraic standpoint. McCulloch and
Harth speculate on the latest brain models based on "a more
realistic neuron". A few admirable pages by Martin Davis
clarify in simple terms what recursive function theory is
about and how it concerns automata. At the other end of
the road, some of the more bizarre propositions of that theory,
such as that any program may be indefinitely accelerated (as
M. BELL
171