SICP Exercise 3.60 mul-series
Exercise 3.60. With power series represented as streams of coefficients as in exercise 3.59, adding series is implemented by add-streams. Complete the definition of the following procedure for multiplying series:
(define (mul-series s1 s2)
(cons-stream <??> (add-streams <??> <??>)))
You can test your procedure by verifying that sin2 x + cos2 x = 1, using the series from exercise 3.59.
SOLUTION
Logic used in mul-series:
(a0 + a1x + a2x^2 + a3x^3 + ...) * (b0 + b1x + b2x^2 + b3x^3 + ...) is:
(a0 * b0) +
{a0 * (b1x + b2x^2 + b3x^3 + ...)} +
{b0 * (a1x + a2x^2 + a3x^3 + ...)} +
{(a1x + a2x^2 + a3x^3 + ...) * (b1x + b2x^2 + b3x^3 + ...)}
The coefficients after multiplying the two series above will be:
Variable | Coefficient
-------------------------------
x^0 | a0*b0
x^1 | a0*b1 + a1*b0
x^2 | a0*b2 + a1*b1 + a2*b0
x^3 | a0*b3 + a1*b2 + a2*b1 + a3*b0
x^4 | a0*b4 + a1*b3 + a2*b2 + a3*b1 + a4*b0
and so on
The code and tests are here.
(define (mul-series s1 s2)
(cons-stream <??> (add-streams <??> <??>)))
You can test your procedure by verifying that sin2 x + cos2 x = 1, using the series from exercise 3.59.
SOLUTION
Logic used in mul-series:
(a0 + a1x + a2x^2 + a3x^3 + ...) * (b0 + b1x + b2x^2 + b3x^3 + ...) is:
(a0 * b0) +
{a0 * (b1x + b2x^2 + b3x^3 + ...)} +
{b0 * (a1x + a2x^2 + a3x^3 + ...)} +
{(a1x + a2x^2 + a3x^3 + ...) * (b1x + b2x^2 + b3x^3 + ...)}
The coefficients after multiplying the two series above will be:
Variable | Coefficient
-------------------------------
x^0 | a0*b0
x^1 | a0*b1 + a1*b0
x^2 | a0*b2 + a1*b1 + a2*b0
x^3 | a0*b3 + a1*b2 + a2*b1 + a3*b0
x^4 | a0*b4 + a1*b3 + a2*b2 + a3*b1 + a4*b0
and so on
The code and tests are here.
Comments
Post a Comment