Squinting at Power Series
Total Page:16
File Type:pdf, Size:1020Kb
Squinting at Power Series M. Douglas McIlroy AT&T Bell Laboratories Murray Hill, New Jersey 07974 ABSTRACT Data streams are an ideal vehicle for handling power series. Stream implementations can be read off directly from simple recursive equations that de®ne operations such as multiplication, substitution, exponentiation, and reversion of series. The bookkeeping that bedevils these algorithms when they are expressed in traditional languages is completely hidden when they are expressed in stream terms. Communicating processes are the key to the simplicity of the algorithms. Working versions are presented in newsqueak, the language of Pike's ``squint'' system; their effectiveness depends critically on the stream protocol. Series and streams Power series are natural objects for stream processing. Pertinent computations are neatly describable by recursive equations. CSP (communicating sequential process) languages1, 2 are good vehicles for implementation. This paper develops the algorithmic ideas and reduces them to practice in a working CSP language,3 not coincidentally illustrating the utility of concurrent programming notions in untangling the logic of some intricate computations on sequences. This elegant approach to power series computation, ®rst demonstrated but never published by Kahn and MacQueen as an application of their data stream system, is still little known.4 Power series are represented as streams of coef®cients, the exponents being given implicitly by ordinal position. (This is an in®nite analogue of the familiar representation of a polynomial as an array of coef®cients.) Thus the power series for the exponential function ∞ 1 1 1 e x = Σ x n / n! = 1 + x + __ x 2 + __ x 3 + ___ x 4 + . n = 0 2 6 24 - 2 - is represented as a stream of rationals: 1 1 1 1 , 1 , __ , __ , ___ , . 2 6 24 Throughout the paper power series will be denoted by symbols such as F, with the functional form F(x) being used when the variable must be made explicit. Subscripts denote individual coef®cients: ∞ i F(x) = Σ F i x . i = 0 The corresponding stream will be referred to by the same symbol in program font: F. In programs the names of power series variables contain capital letters; scalars are all lower case. When new power series are calculated from old, the input series will always be called F and G. In this section all programs are written in pseudocode; later they will be translated into the language ``newsqueak'' to be run in the ``squint'' system.* The methods are strictly formal; analytic interpretation of results depends on the convergence of the power series in question. Thus assertions such as x i = 1/( 1 − x) are understood to hold Σ i only where the series converge. Addition. The sum, S = F + G, is easy. Since S i = F i + G i, all we need is a looping process, which gets pairs of coef®cients from the two inputs and puts their sum on the output S. The following pseudocode suf®ces. # calculate power series S = F + G loop forever let f = get(F), g = get(G) put(f+g, S) Multiplication by a term. Simple termwise operations on single power series, such as multiplication by a constant, integration, or differentiation are equally easy to program and are left to the reader's imagination. Multiplication of a power series by x to yield P = x F involves a one-element delay: * The name of the language reveals descent from a mousier ancestor intended for programming a terminal with multiple asynchronous windows. The name ``squint'' suggests ``squeak interpreter.'' - 3 - # calculate power series P = x*F put(0, P) loop forever put(get(F), P) Multiplication. Calculation of the product, P = F G, is more challenging. By equating coef®cients in ∞ ∞ ∞ n i j Σ P n x = Σ F i x Σ G j x , n = 0 i = 0 j = 0 we obtain the familiar convolution for terms of the product, n P n = Σ F i G n − i , (1) i = 0 a most unpromising formula to program directly. To calculate the nth coef®cient we must have stored up the ®rst n terms of both inputs, thus sacri®cing most bene®ts of the stream representation. It will be best to look in another direction, guided by the adage: When iteration gets sticky, try recursion. We treat streams recursively, as we do lists, by handling the leading term and recurring on the tail. This means rewriting a power series as a ®rst term plus x times another power series: _ F = F 0 + x F. _ The tail F is a whole power series beginning with a constant term; it is ripe for recursion. Writing the multiplication P = F G recursively, we obtain _ __ _ _ __ 2 P 0 + x P = F 0 G 0 + x (F 0 G + G 0 F) + x F G. Equate coef®cients to ®nd the ®rst term of the product, P 0 = F 0 G 0 , leaving for the tail, _ __ _ _ __ P = F 0 G + G 0 F + x F G, (2) a sum of a constant F times a power series, another constant G times another power 0 _0 __ series, and x times a third power series, which is itself a product F G. We already know - 4 - how to multiply power series by a constant and how to add power series. We also know how to multiply power series by x; recursion does the rest. In the following pseudocode, f*G denotes an auxiliary stream for the product f G, and so on. Each auxiliary stream will be computed by another process. Thus f G will be computed by a process that multiplies a power series by a constant. When two auxiliary streams share an input stream, both see every element of the input. # calculate power series P = F*G let f = get(F), __ g = get(G) # F and G now "contain" Fbar and G put(f*g, P) # F 0 G 0 let fG = f*G, gF = g*F, xFG = x*(F*G) # here is the recursion loop forever put(get(fG) + get(gF) + get(xFG), P) The convolution (1) has been hidden completely; all the necessary bookkeeping and storage management is embodied in the topology and contents of the auxiliary streams. Streams fan out to feed multiple consumers. For instance G enters into both f*G and F*G. The topology rami®es as the recursion progresses; more auxiliary streams appear at every stage, making the recursive picture of Figure 1. To write the program, though, we need not think about the topology, for the program is a direct image of the mathematics (2), and the mathematics is simple. Substitution. Substitution, or composition, of power series, de®ned by S = F(G), may be done similarly, using multiplication as a subprocess. The recursive formulation is _ __ _ __ S 0 + x S = F 0 + (G 0 + x G) F(G 0 + x G). Equate coef®cients to ®nd the constant term, _ . S 0 = F 0 + G 0 ( ®rst term of F(G) ). Induction shows that in general S 0 is an in®nite sum. ∞ i S 0 = Σ F i G0 . i = 0 To keep the problem ®nite, we further stipulate that G 0 = 0. Now - 5 - F G ×G 0 ×F 0 _ __ F×G × x + F×G Figure 1. Data ¯ow for multiplication. The outer box represents the process for the tail of F G, equation (2). The similar inner box does not receive the ®rst terms of F or G, and its output does not enter into the ®rst two terms of the product F G. The newsqueak implementation uses a separate process for each box, and one or more processes for each stream splitting. Viewed as a three-dimensional scene receding into the distance, the picture shows a simple repeating structure connected on top by F and G buses. The buses grow, extending one level further with the receipt of each input term. A daisy chain of output connections leads from back to front. _ __ _ S = S 0 + x S = F 0 + x G F(G) , (3) from which pseudocode can be read off. - 6 - # calculate power series S = F(G) put(get(F), S) # F 0 _ let FG = F(G) # the recursion, F(G) let g = get(G) # discard the first term (must be 0) __ _ let GF = G*FG # G F(G) loop forever put(get(GF), S) A subtlety: the G in let FG = F(G) stands for the whole power series, while the G in __ let GF = G*FG stands only for the tail G. In the same way, the meaning of F changes between get(F) and let FG = F(G). Exponentiation. We wish to compute X = e F. From the recursive representation, _ X = e F 0 e x F , (4) we see that X = e F 0 . ( 1 + terms in x). To make the constant term of X rational, we further require that F 0 = 0. Hence X 0 = 1. Unfortunately equation (4) does not lead to an algorithm. Formal substitution yields _ X(x) = 1 . X(x X). (5) _ Since the ®rst term of X(x) depends on the ®rst term of X(x X), we are stuck in an in®nite regress. By induction we may conclude that the ®rst term is an in®nite product of ones, but what are the higher terms? We can ``cheat,'' and substitute F in the known power series for the exponential. It is more satisfying, though, to build from nothing and let the program ``discover'' the exponential for itself.