Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
164 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
;; Example 3.59 | ||
|
||
#lang racket | ||
|
||
(require racket/stream) | ||
|
||
;; accumulate stream n times | ||
|
||
(define (fold-stream stream n) | ||
(if (<= n 0) | ||
0 | ||
(+ (stream-first stream) | ||
(fold-stream (stream-rest stream) (- n 1))))) | ||
|
||
(define (integrate-series stream) | ||
(define (iter str counter) | ||
(stream-cons (* (/ 1. counter) (stream-first str)) | ||
(iter (stream-rest str) (+ counter 1)))) | ||
(iter stream 1)) | ||
|
||
(define exp-series | ||
(stream-cons 1 (integrate-series exp-series))) | ||
|
||
(fold-stream exp-series 100) | ||
|
||
(define cosine-series | ||
(stream-cons 1 (stream-map (lambda (x) (- x)) | ||
(integrate-series sine-series)))) | ||
|
||
(define sine-series | ||
(stream-cons 0 (integrate-series cosine-series))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
;; Example 3.60 | ||
|
||
#lang racket | ||
|
||
(require racket/stream) | ||
|
||
;; scale a stream | ||
|
||
(define (scale-stream stream factor) | ||
(stream-map (lambda (x) (* x factor)) stream)) | ||
|
||
;; require for add streams | ||
|
||
(define (my-stream-map proc . argstreams) | ||
(if (stream-empty? (car argstreams)) | ||
empty-stream | ||
(stream-cons | ||
(apply proc (map stream-first argstreams)) | ||
(apply my-stream-map | ||
(cons proc (map stream-rest argstreams)))))) | ||
|
||
(define (add-streams s1 s2) | ||
(my-stream-map + s1 s2)) | ||
|
||
;; from 3.59 | ||
|
||
(define (integrate-series stream) | ||
(define (iter str counter) | ||
(stream-cons (* (/ 1. counter) (stream-first str)) | ||
(iter (stream-rest str) (+ counter 1)))) | ||
(iter stream 1)) | ||
|
||
(define cosine-series | ||
(stream-cons 1 (stream-map (lambda (x) (- x)) | ||
(integrate-series sine-series)))) | ||
|
||
(define sine-series | ||
(stream-cons 0 (integrate-series cosine-series))) | ||
|
||
;; 3.60 | ||
|
||
(define (mul-series s1 s2) | ||
(stream-cons (* (stream-first s1) (stream-first s2)) | ||
(add-streams (scale-stream (stream-rest s2) (stream-first s1)) | ||
(mul-series (stream-rest s1) s2)))) | ||
|
||
(define S (add-streams (mul-series sine-series sine-series) (mul-series cosine-series cosine-series))) | ||
|
||
(stream-ref S 0) | ||
(stream-ref S 1) | ||
(stream-ref S 2) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
;; Example 3.61 | ||
|
||
#lang racket | ||
|
||
;; scale a stream | ||
|
||
(define (scale-stream stream factor) | ||
(stream-map (lambda (x) (* x factor)) stream)) | ||
|
||
;; require for add streams | ||
|
||
(define (my-stream-map proc . argstreams) | ||
(if (stream-empty? (car argstreams)) | ||
empty-stream | ||
(stream-cons | ||
(apply proc (map stream-first argstreams)) | ||
(apply my-stream-map | ||
(cons proc (map stream-rest argstreams)))))) | ||
|
||
(define (add-streams s1 s2) | ||
(my-stream-map + s1 s2)) | ||
|
||
;; from 3.59 | ||
|
||
(define (integrate-series stream) | ||
(define (iter str counter) | ||
(stream-cons (* (/ 1. counter) (stream-first str)) | ||
(iter (stream-rest str) (+ counter 1)))) | ||
(iter stream 1)) | ||
|
||
(define cosine-series | ||
(stream-cons 1 (stream-map (lambda (x) (- x)) | ||
(integrate-series sine-series)))) | ||
|
||
(define sine-series | ||
(stream-cons 0 (integrate-series cosine-series))) | ||
|
||
;; from 3.60 | ||
|
||
(define (mul-series s1 s2) | ||
(stream-cons (* (stream-first s1) (stream-first s2)) | ||
(add-streams (scale-stream (stream-rest s2) (stream-first s1)) | ||
(mul-series (stream-rest s1) s2)))) | ||
|
||
;; 3.61 | ||
|
||
(define (invert-unit-series S) | ||
(define res (stream-cons 1 (stream-map (λ (x) (- x)) | ||
(mul-series (stream-rest S) res)))) | ||
res) | ||
|
||
;; test | ||
|
||
(define (fold-stream stream n) | ||
(if (<= n 0) | ||
0 | ||
(+ (stream-first stream) | ||
(fold-stream (stream-rest stream) (- n 1))))) | ||
|
||
(define exp-series | ||
(stream-cons 1 (integrate-series exp-series))) | ||
|
||
(define one-div-e (invert-unit-series exp-series)) | ||
|
||
(fold-stream one-div-e 10) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
;; Example 3.62 | ||
|
||
#lang racket | ||
|
||
(require racket/stream) | ||
|
||
;; without testing | ||
|
||
(define (div-series S1 S2) | ||
(let ((i2 (stream-first S2))) | ||
(if (= 0 i2) | ||
(error "Division by zero") | ||
(scale-stream (mul-series S1 | ||
(invert-unit-series (scale-stream S2 (/ 1. i2)))) | ||
(/ 1. i2))))) | ||
|
||
;; tg = sin/cos |