-
Notifications
You must be signed in to change notification settings - Fork 0
/
3-59.scm
127 lines (89 loc) · 3.22 KB
/
3-59.scm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#lang scheme
(require "streams.scm")
(define (integrate-series s)
(div-streams s integers))
(define exp-series
(cons-stream 1 (integrate-series exp-series)))
(define cosine-series
(cons-stream 1 (stream-map - (integrate-series sine-series))))
(define sine-series
(cons-stream 0 (integrate-series cosine-series)))
(take exp-series 10)
(take cosine-series 10)
(take sine-series 10)
(define (mul-series s1 s2)
(cons-stream (* (stream-car s1) (stream-car s2))
(add-streams (scale-stream (stream-cdr s1) (stream-car s2))
(mul-series (stream-cdr s2) s1))))
(define (S Sr)
(cons-stream 1 Sr))
(define (invert-unit-series s)
(cons-stream 1 (scale-stream (mul-series s
(invert-unit-series s))
-1)))
;(take (invert-unit-series integers) 10)
(define (div-series num den)
(if (= (stream-car den) 0)
(error "zero division")
(mul-series num
(invert-unit-series den))))
(define tangent-series (div-series sine-series cosine-series))
(define (partial-sums s)
(add-streams s (cons-stream 0 (partial-sums s))))
(define (pi-summands n)
(cons-stream (/ 1.0 n)
(stream-map - (pi-summands (+ n 2)))))
(define pi-stream
(scale-stream (partial-sums (pi-summands 1)) 4))
;(take pi-stream 1000)
;(take (pi-summands 1) 10)
(define (square x)
(* x x ))
(define (euler-transform s)
(let ((s0 (stream-ref s 0))
(s1 (stream-ref s 1))
(s2 (stream-ref s 2)))
(cons-stream (- s2 (/ (square (- s2 s1))
(+ s0 (* -2 s1) s2)))
(euler-transform (stream-cdr s)))))
(stream-ref pi-stream 100)
(stream-ref (euler-transform pi-stream) 100)
(stream-ref (euler-transform (euler-transform pi-stream)) 100)
(stream-ref (euler-transform (euler-transform (euler-transform pi-stream))) 100)
(define (make-tableau transform s)
(cons-stream s (make-tableau transform (transform s))))
(define (accelerated-sequence transform s)
(stream-map stream-car (make-tableau transform s)))
(take (accelerated-sequence euler-transform pi-stream) 10)
(define (stream-limit s tolerance)
(let ((first (stream-car s))
(second (stream-car (stream-cdr s))))
(if (>= tolerance (abs (- first second)))
second
(stream-limit (stream-cdr s) tolerance))))
(stream-limit (accelerated-sequence euler-transform pi-stream) 0.001)
(define (ln2-summands n)
(cons-stream (/ 1.0 n)
(stream-map - (ln2-summands (+ n 1)))))
(define ln2-stream
(partial-sums (ln2-summands 1)))
(stream-ref ln2-stream 100)
(define (interleave s1 s2)
(if (stream-null? s1)
s2
(cons-stream (stream-car s1)
(interleave s2 (stream-cdr s1)))))
(take (interleave integers integers) 10)
(define (pairs s t)
(cons-stream
(list (stream-car s) (stream-car t))
(interleave
(stream-map (lambda (x)
(list (stream-car s) x))
(stream-cdr t))
(pairs (stream-cdr s) (stream-cdr t)))))
; 198 pairs precede (1, 100)
(stream-ref (pairs integers integers) 199)
; all integers 99 times precedes (99, 100)
; all integers 100 times precedes (100, 100)
(take (pairs integers integers) 10)