Log in Page Discussion History Go to the site toolbox

Exercise 3.60

From BluWiki

Section 3.5 Exercises

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 sin2x + cos2x = 1, using the series from exercise 3.59.

Let stream s1 correspond to the series <math>\sum_{i=0}^{\infty} a_i \cdot x^i</math>, and stream s2 correspond to the series <math>\sum_{i=0}^{\infty} b_i \cdot x^i</math>.

This definition of mul-series uses the decomposition <math>

 \left( \sum_{i=0}^{\infty} a_i \cdot x^i \right) \cdot
 \left( \sum_{i=0}^{\infty} b_i \cdot x^i \right) =
 a_0 \cdot b_0 +
 a_0 \cdot \left( \sum_{i=1}^{\infty} b_i \cdot x^i \right) +
 \left( \sum_{i=1}^{\infty} a_i \cdot x^i \right) \cdot
 \left( \sum_{i=0}^{\infty} b_i \cdot x^i \right)

</math>

(define (add-series . args)
  (apply stream-map (cons + args)))
(define (mul-series s1 s2)
  (let ((first1 (stream-car s1))
       ((first2 (stream-car s2))
        (rest1  (stream-cdr s1))
        (rest2  (stream-cdr s2)))
    (cons-stream (* first1 first2)
                 (add-series (scale-stream rest2 first1)
                             (mul-series rest1 s2)))))
(define (square-series s) (mul-series s s))
(define better-be-one (add-series (square-series sine-series)
                                  (square-series cosine-series)))
(display-stream-part better-be-one 0 6)
1
0
0
0
0
0
0


This is a slightly different definition of mul-series that I find more intuitive.

(define (mul-series s1 s2)
  (let ((first1 (stream-car s1))
        (first2 (stream-car s2))
        (rest1  (stream-cdr s1))
        (rest2  (stream-cdr s2)))
    (cons-stream (* first1 first2)
                 (add-series (scale-stream rest2 first1)
                             (scale-stream rest1 first2)
                             (cons-stream 0 (mul-series rest1 rest2))))))

Site Toolbox:

Personal tools
GNU Free Documentation License 1.2
This page was last modified on 22 October 2007, at 04:41.
Disclaimers - About BluWiki