Skip to main content

Streams and Series

  • Please download the homework here

Overview​

In this assignment, you will work with streams to evaluate formal power series. Consider the series 𝑠(π‘₯)=π‘Ž0+π‘Ž1π‘₯+π‘Ž2π‘₯2+…𝑠(π‘₯) = π‘Ž_0 + π‘Ž_1π‘₯ + π‘Ž_2π‘₯^2 + \ldots, we can represent this series by its finite or infinite sequence of coefficients (a0,a1,a2,...)(a_0, a_1, a_2, ...). We will view this sequence as a stream.

Learning Objectives​

  • Practice writing and reasoning about streams
  • Practice writing and reasoning with recursion

Student Expectations​

Students will be graded on their ability to:

Resources​

Testing​

You must write tests for all your functions, following the principles used so far.

It may not be obvious how to implement some of the functions. Try writing some tests first.

Programming Tasks​

For all functions below, use streams. A series is represented by a nonempty stream, which may be finite or infinite. Compute only as much of the result stream as needed.

addSeries​

Write a function addSeries that takes two streams of coefficients for the series s(x)s(x) and t(x)t(x) and returns the stream of coefficients for the sum s(x)+t(x)s(x) + t(x). For example, given 1+2x+3x2+…1+2x+3x^2+\ldots and 2+6x+9x2+…2+6x+9x^2+\ldots, the result will be 3+8x+12x2+…3+8x+12x^2+\ldots

prodSeries​

Write a function prodSeries that takes two streams of coefficients for the series s(x)s(x) and t(x)t(x) and returns the stream of coefficients for the product s(x)β‹…t(x)s(x) β‹… t(x). Review polynomial multiplication. For example, given 1+2x+3x2+…1+2x+3x^2+\ldots and 2+6x+9x2+…2+6x+9x^2+\ldots, the result will be 2+10x+27x2+…2+10x+27x^2+\ldots

Hint: Write one of the series as 𝑠(π‘₯)=π‘Ž0+π‘₯𝑠1(π‘₯)𝑠(π‘₯) = π‘Ž_0 + π‘₯ 𝑠_1(π‘₯), where s1(x)s_1(x) is another series. Then use distributivity to multiply s(x)β‹…t(x)s(x) β‹… t(x) (do not rewrite t(x)t(x)) and map all operations to streams (how can you represent multiplying with xx?). Delay the recursive computation of the result’s tail until needed.

derivSeries​

Write a function derivSeries that takes a stream of coefficients for the series s(x)s(x), and returns a stream of coefficients for the derivative sβ€²(x)s^\prime(x). For example, given 1+2x+3x2+…1+2x+3x^2+\ldots the result will be 2+6x+…2+6x+\ldots

coeff​

Write a function coeff that takes a stream of coefficients for the series s(x)s(x) and a natural number nn, and returns the array of coefficients of s(x)s(x), up to and including that of order (degree) nn. If the stream has fewer coefficients, return as many as there are.

evalSeries​

Write a function evalSeries that takes a stream of coefficients for the series s(x)s(x), and a natural number nn, and returns a closure. When called with a real number xx, this closure will return the sum of the values of all terms of s(x)s(x) up to and including the term of degree nn.

applySeries​

Write a function applySeries that takes a function f and a value v and returns the stream representing the infinite series s(x)s(x) where a0=va_0 = v, and ak+1=f(ak)a_{k + 1}= f(a_k), for all kβ‰₯0k β‰₯ 0.

expSeries​

Write a function expSeries with no arguments that returns the Taylor series for exe^x, i.e., the coefficients are ak=1/k!a_k = 1/k! You may use applySeries with an appropriate closure.

recurSeries​

Write a function recurSeries, taking two arrays, coef and init, assumed of equal positive length kk, with coef = [c0,c1,…,ckβˆ’1][c_0, c_1, \ldots, c_{k-1}]. The function should return the infinite stream of values aia_i given by a k-order recurrence: the first kk values are given by init: [a0,a1,…,akβˆ’1][a_0, a_1, \ldots, a_{k-1}]; the following values are given by the relation an+k=c0an+c1an+1+…+ckβˆ’1an+kβˆ’1a_{n+k} = c_0 a_n + c_1 a_{n+1} + \ldots + c_{k-1} a_{n+k-1} for all nβ‰₯0n β‰₯ 0.

Hint: Derive the formula for an+ka_{n + k} when n=0n = 0 and n=1n = 1.