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 , we can represent this series by its finite or infinite sequence of coefficients . 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:
- Correctly implement the functions specified below
- Resolve all linter warnings
- Follow the coding, bad practice and testing guidelines
- Design full-coverage unit-tests for the functions they implemented
- See the testing guidelines on coverage for more details
Resourcesβ
- Formal power series and a discussion on manipulating polynomials as sequence of coefficients
- Polynomial long multiplication
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 and and returns the stream of coefficients for the sum . For example, given and , the result will be
prodSeries
β
Write a function prodSeries
that takes two streams of coefficients for the series and and returns the stream of coefficients for the product . Review polynomial multiplication. For example, given and , the result will be
Hint: Write one of the series as , where is another series. Then use distributivity to multiply (do not rewrite ) and map all operations to streams (how can you represent multiplying with ?). 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 , and returns a stream of coefficients for the derivative . For example, given the result will be
coeff
β
Write a function coeff
that takes a stream of coefficients for the series and a natural number , and returns the array of coefficients of , up to and including that of order (degree) . 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 , and a natural number , and returns a closure. When called with a real number , this closure will return the sum of the values of all terms of up to and including the term of degree .
applySeries
β
Write a function applySeries
that takes a function f
and a value v
and returns the stream representing the infinite series where , and , for all .
expSeries
β
Write a function expSeries
with no arguments that returns the Taylor series for , i.e., the coefficients are You may use applySeries
with an appropriate closure.
recurSeries
β
Write a function recurSeries
, taking two arrays, coef
and init
, assumed of equal positive length , with coef
= . The function should return the infinite stream of values given by a k-order recurrence: the first values are given by init: ; the following values are given by the relation for all .
Hint: Derive the formula for when and .