scheme - SICP exercise 3.62 -
i've been finished exercise 3.59, 3.60, 3.61 , stuck @ 3.62, turn sicp-solutions@scheme wiki community help.
exercise 3.59 , 3.60 tested sine
, cosine
case, not 3.61 or 3.62.
the 3.62 code provided on community produces all-zero tangent
series on condition of correct sine
, cosine
series:
> (display-stream-until sine-series 7) 0 1 0 -1/6 0 1/120 0 > (display-stream-until cosine-series 7) 1 0 -1/2 0 1/24 0 -1/720 > (display-stream-until tangent-series 7) 0 0 0 0 0 0 0
at first think code wrote in previous exercises might wrong, check solutions of 3.59/3.60/3.61 on community , turn out produce same result.
i try copying code of 3.59/3.60/3.61 ide(drracket) , still tangent
series printed zero.
here code:
(define (mul-series s1 s2) (cons-stream (* (stream-car s1) (stream-car s2)) (add-streams (mul-streams (stream-cdr s1) (stream-cdr s2)) (mul-series s1 s2)))) (define (reciprocal-series s) (cons-stream 1 (scale-stream (mul-series (stream-cdr s) (reciprocal-series s)) -1))) (define (div-series s1 s2) (let ((c (stream-car s2))) (if (= c 0) (error "constant term of s2 can't 0!") (scale-stream (mul-series s1 (reciprocal-series (scale-stream s2 (/ 1 c)))) (/ 1 c))))) (define tangent-series (div-series sine-series cosine-series))
and requiring part: (too long highlight body)
(require (only-in "mylib.rkt" cons-stream stream-car stream-cdr add-streams mul-streams scale-stream stream-map ones integers sine-series cosine-series stream-ref display-stream-until))
could tell me what's wrong code or might possible bugs hide?
by way, how guys verify code on sicp?
i've found several blog/github repository provide code without (enough) testing case(s).
i have hard time dealing such mathematics questions bare code.
the problem mul-series
.
product of series [a0 + a1x + ...] , series [b0 + b1x + ...] is
[a0b0 + (a0b1 + a1b0)x + ...]
mul-series
produces
(cons-stream a0b0 (add-streams (mul-streams (cdr s1) (cdr s2)) (mul-series s1 s2)))
= (cons-stream a0b0 (add-streams [a1b1, ...] [a0b0, ...]))
= [a0b0, a1b1 + a0b0, ...]
you can check mul-series
using identity sin(x)2 + cos(x)2 = 1 (as suggested in exercise 3.60).
other simple test cases try are
[2] * [0 + x] = [0 + 2x]
[1 + x] * [1 + x] = [ 1 + 2x + 1x2]
with appropriate helper functions, tests this:
> (show-series (add-streams (mul-series sine-series sine-series) (mul-series cosine-series cosine-series))) 1 + 0*x + 0*x^2 + ... > (show-series (mul-series (make-series '(2)) (make-series '(0 1)))) 0 + 2*x + ... > (show-series (mul-series (make-series '(1 1)) (make-series '(1 1)))) 1 + 2*x + 1*x^2 + ...
here helper functions i've defined:
(define zeroes (cons-stream 0 zeroes)) ;; make-series takes list of numbers , returns infinite stream ;; numbers followed 0's (define (make-series coeffs) (if (null? coeffs) zeroes (cons-stream (car coeffs) (make-series (cdr coeffs))))) ;; show-stream prints first 10 items in stream (define (show-stream stream) (begin (show-stream-helper stream 10))) (define (show-stream-helper stream n) (if (= 0 n) (begin (display "...") (newline)) (begin (display (stream-car stream)) (display ", ") (show-stream-helper (stream-cdr stream) (- n 1))))) ;; show-series show-stream, uses format a0 + a1*x + ... (define (show-series stream) (show-series-helper stream 0 10)) (define (show-series-helper stream n m) (if (> n m) (begin (display "...") (newline)) (begin (show-monomial (stream-car stream) n) (display " + ") (show-series-helper (stream-cdr stream) (+ 1 n) m)))) ;; show-monomial displays a*x^n (define (show-monomial coeff pow) (cond ((= 0 pow) (display coeff)) ((= 1 pow) (display coeff) (display "*x")) (else (display coeff) (display "*x^") (display pow))))
Comments
Post a Comment