sicp-ex-3.62



<< Previous exercise (3.61) | Index | Next exercise (3.63) >>


cyzx

A simple solution, that produces the right tangent power series coefficients when displayed

 ;nums is numerator, dems is denominator 
  
 (define (div-series nums dems) 
   (mul-series nums 
               (invert-series dems))) 
  
 (define tangent-series (div-series sine-series cosine-series)) 

Source for the correct tangent power series: https://socratic.org/questions/what-is-the-taylor-series-expansion-for-the-tangent-function-tanx


meteorgan

  
  
 (define (div-series s1 s2) 
   (let ((c (stream-car s2))) 
     (if (= c 0) 
         (error "constant term of s2 can't be 0!") 
         (scale-stream 
          (mul-series s1 (reciprocal-series 
                          (scale-stream s2 (/ 1 c)))) 
          (/ 1 c))))) 
  
 (define tane-series (div-series sine-series cosine-series)) 

I'm pretty sure the tan series won't work like that because half the coefficients for cos are zero.



gravitykey

  
  
 ; according 3-61 
 ; S·X = 1 
 ; X = 1 / S 
 ; another S1 
 ; S1 / S  =  S1 · X 
  
 ;use code in 3-61 
 (load "3-61.scm") 
 ;"daoshu" means reciprocal 
  
 (define (div-series s1 s2) 
   (cond ((eq? 0 (stream-car s2)) 
          (error "constant term of s2 can't be 0!")) 
         (else (mul-series s1 (daoshu s2))))) 
  

tests

 (define sine-series 
   (cons-stream 0 (integrate-series cosine-series))) 
 (define cosine-series 
   (cons-stream 1 (integrate-series 
                   (stream-map (lambda (x) (* x -1)) 
                               sine-series)))) 
 (define (add-streams s1 s2) (stream-map + s1 s2)) 
  
 (define (integrate-series stm) 
   (define (inner s a) 
     (cons-stream (* (/ 1 a) (stream-car s)) 
                  (inner (stream-cdr s) (+ a 1)))) 
   (inner stm 1)) 
  
 (define tan-series (div-series sine-series cosine-series)) 
 (stream-head tan-series 10) 
  
 ;Value 13: (0 1 0 1/3 0 2/15 0 17/315 0 62/2835) 
 ;It's correct. 

Sphinxsky

  
  
 (define (div-series stream1 stream2) 
   (let ((s2-car (stream-car stream2))) 
     (if (= s2-car 0) 
         (error "Denominator constant term cannot be 0 !-- DIV-SERIES" s2-car) 
         (let ((reciprocal-s2-car (/ 1 s2-car))) 
           (mul-series 
            (scale-stream stream1 reciprocal-s2-car) 
            (reciprocal-series (scale-stream stream2 
                                             reciprocal-s2-car))))))) 
  
 (define tangent-series (div-series sine-series cosine-series))