re.scm 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. #| -*-Scheme-*-
  2. Copyright (C) 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994,
  3. 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
  4. 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013 Massachusetts
  5. Institute of Technology
  6. This file is part of MIT/GNU Scheme.
  7. MIT/GNU Scheme is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2 of the License, or (at
  10. your option) any later version.
  11. MIT/GNU Scheme is distributed in the hope that it will be useful, but
  12. WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. General Public License for more details.
  15. You should have received a copy of the GNU General Public License
  16. along with MIT/GNU Scheme; if not, write to the Free Software
  17. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301,
  18. USA.
  19. |#
  20. ;;;; Richardson Extrapolation of a sequence, represented as a stream.
  21. (declare (usual-integrations))
  22. ;;; Assumption: The sequence is the values of a function at successive
  23. ;;; reciprocal powers of 2. The function is assumed to be analytic in
  24. ;;; a region around h=0 with a power-series expansion. The first
  25. ;;; non-constant term of the series has power p and successive terms
  26. ;;; are separated by powers of q.
  27. (define (make-zeno-sequence R h)
  28. (cons-stream (R h) (make-zeno-sequence R (/ h 2))))
  29. (define (accelerate-zeno-sequence seq p)
  30. (let* ((2^p (expt 2 p)) (2^p-1 (- 2^p 1)))
  31. (map-streams (lambda (Rh Rh/2)
  32. (/ (- (* 2^p Rh/2) Rh)
  33. 2^p-1))
  34. seq
  35. (tail seq))))
  36. (define (make-zeno-tableau seq p q)
  37. (define (sequences seq order)
  38. (cons-stream seq
  39. (sequences (accelerate-zeno-sequence seq order)
  40. (+ order q))))
  41. (sequences seq p))
  42. (define (first-terms-of-zeno-tableau tableau)
  43. (map-stream head tableau))
  44. (define (richardson-sequence seq p q)
  45. (first-terms-of-zeno-tableau (make-zeno-tableau seq p q)))
  46. (define (stream-limit s tolerance . optionals)
  47. (let ((M (default-lookup 'MAXTERMS -1 optionals))
  48. (ok? (default-lookup 'CONVERGENCE-TEST close-enuf? optionals)))
  49. (let loop ((s s) (count 2))
  50. ;;(write-line 'main-loop)
  51. (let* ((h1 (head s)) (t (tail s)) (h2 (head t)))
  52. ;;(write-line (list count h1 h2))
  53. (cond ((ok? h1 h2 tolerance) h2)
  54. ((and (positive? M) (>= count M))
  55. ((default-lookup 'IFFAIL
  56. (lambda args
  57. (error "STREAM-LIMIT not reached" M))
  58. optionals)
  59. 'stream-limit
  60. 'not-converged
  61. M
  62. h1
  63. h2))
  64. (else (loop t (+ count 1))))))))
  65. (define (richardson-limit f start-h ord inc tolerance . opts)
  66. (apply stream-limit
  67. (richardson-sequence (make-zeno-sequence f start-h)
  68. ord
  69. inc)
  70. tolerance
  71. opts))
  72. #|
  73. ;;; Archimedean computation of Pi.
  74. (define (refine-by-doubling s)
  75. (/ s (sqrt (+ 2 (sqrt (- 4 (square s)))))))
  76. (define (stream-of-iterates next value)
  77. (cons-stream value (stream-of-iterates next (next value))))
  78. (define side-lengths
  79. (stream-of-iterates refine-by-doubling (sqrt 2)))
  80. (define side-numbers
  81. (stream-of-iterates (lambda (n) (* 2 n)) 4))
  82. (define (semi-perimeter length-of-side number-of-sides)
  83. (* (/ number-of-sides 2) length-of-side))
  84. (define archimedean-pi-sequence
  85. (map-streams semi-perimeter side-lengths side-numbers))
  86. (print-stream archimedean-pi-sequence 24)
  87. 2.8284271247461903
  88. 3.0614674589207183
  89. 3.1214451522580524
  90. 3.1365484905459393
  91. 3.140331156954753
  92. 3.141277250932773
  93. 3.1415138011443013
  94. 3.141572940367092
  95. 3.14158772527716
  96. 3.1415914215112
  97. 3.141592345570118
  98. 3.141592576584873
  99. 3.1415926343385636
  100. 3.141592648776986
  101. 3.1415926523865916
  102. 3.1415926532889933
  103. 3.1415926535145937
  104. 3.141592653570994
  105. 3.141592653585094
  106. 3.141592653588619
  107. 3.1415926535895
  108. 3.1415926535897203
  109. 3.1415926535897754
  110. 3.141592653589789
  111. ;Value: ...
  112. |#
  113. #|
  114. (print-stream (richardson-sequence archimedean-pi-sequence 2 2) 7)
  115. 2.8284271247461903
  116. 3.1391475703122276
  117. 3.1415903931299374
  118. 3.141592653286045
  119. 3.1415926535897865
  120. 3.141592653589793
  121. 3.1415926535897936
  122. ;Value: ...
  123. (guess-ord-and-inc archimedean-pi-sequence)
  124. ;Value: (2. 2.)
  125. |#
  126. (define ord-estimate-stream
  127. (let ((log2 (log 2)))
  128. (lambda (s)
  129. (let* ((s0 (head s)) (st (tail s))
  130. (s1 (head st)) (s2 (head (tail st))))
  131. (cons-stream (/ (log (abs (/ (- s0 s1) (- s1 s2))))
  132. log2)
  133. (ord-estimate-stream st))))))
  134. (define (guess-integer-convergent s)
  135. (let* ((s0 (head s)) (st (tail s))
  136. (s1 (head st)) (s2 (head (tail st))))
  137. (let* ((N (round s0))
  138. (e2 (magnitude (- s2 N)))
  139. (e1 (magnitude (- s1 N)))
  140. (e0 (magnitude (- s0 N))))
  141. (if (and (<= e2 e1) (<= e1 e0))
  142. N
  143. (guess-integer-convergent st)))))
  144. (define (guess-ord-inc-etc s) ;tries to return { ord inc inc inc ... }
  145. (let ((psequence (ord-estimate-stream s)))
  146. (cons-stream (guess-integer-convergent psequence)
  147. (guess-ord-inc-etc psequence))))
  148. (define (guess-ord-and-inc s)
  149. (let ((g (guess-ord-inc-etc s)))
  150. ;; ... and if we make it this far ...
  151. (list (head g) (head (tail g)))))
  152. ;;; For example, we can make a Richardson extrapolation to get the
  153. ;;; first derivative of a function to a required tolerance.
  154. (define richardson-derivative
  155. (let ((log2 (log 2.)))
  156. (define* (rderiv f tolerance #:optional plausible-h)
  157. (define (the-richardson-derivative x)
  158. (let ((h
  159. (if (default-object? plausible-h)
  160. (if (zero? x)
  161. 0.1
  162. (* 0.5 (magnitude x)))
  163. plausible-h)))
  164. (let* ((delta (- (f (+ x h)) (f (- x h))))
  165. (roundoff (* *machine-epsilon*
  166. (+ 1 (floor (magnitude (/ (f x)
  167. (if (zero? delta)
  168. 1
  169. delta)))))))
  170. (n (floor (/ (log (/ tolerance roundoff)) log2))))
  171. (richardson-limit (lambda (dx)
  172. (/ (- (f (+ x dx))
  173. (f (- x dx)))
  174. (* 2 dx)))
  175. h
  176. 2
  177. 2
  178. tolerance
  179. 'MAXTERMS (+ n 1)))))
  180. the-richardson-derivative)
  181. rderiv))
  182. (define richardson-second-derivative
  183. (let ((log4 (log 4)))
  184. (lambda (f tolerance)
  185. (lambda (x)
  186. (let* ((plausible-h (* 0.5 (magnitude x)))
  187. (h (if (zero? plausible-h) 0.1 plausible-h))
  188. (2fx (* 2 (f x)))
  189. (delta (+ (f (+ x h h)) (- 2fx) (f (- x h h))))
  190. (roundoff (* *machine-epsilon*
  191. (+ 1 (floor (magnitude (/ 2fx
  192. (if (zero? delta)
  193. 1
  194. delta)))))))
  195. (n (floor (/ (log (/ tolerance roundoff)) log4))))
  196. (richardson-limit (lambda (h)
  197. (/ (+ (f (+ x h h))
  198. (* -2 (f x))
  199. (f (- x h h)))
  200. (* 4 h h)))
  201. h
  202. 2
  203. 2
  204. tolerance
  205. 'MAXTERMS (+ n 1)))))))
  206. ;;; Romberg integration is the result of a Richardson extrapolation
  207. ;;; of the trapezoid method.
  208. (define (romberg-quadrature f a b tolerance)
  209. (define (trapezoid-sums f a b)
  210. (define (next-S S n)
  211. (let* ((h (/ (- b a) 2 n))
  212. (fx (lambda (i) (f (+ a (* (+ i i -1) h))))))
  213. (+ (/ S 2) (* h (sigma fx 1 n)))))
  214. (define (S-and-n-stream S n)
  215. (cons-stream (list S n)
  216. (S-and-n-stream (next-S S n) (* n 2))))
  217. (let* ((h (- b a))
  218. (S (* (/ h 2) (+ (f a) (f b)))))
  219. (map-stream car (S-and-n-stream S 1))))
  220. (stream-limit
  221. (richardson-sequence (trapezoid-sums f a b)
  222. 2
  223. 2)
  224. tolerance))
  225. ;;; Given a sequence (stream) of abscissas and ordinates for a function,
  226. ;;; the following procedure constructs a sequence of constant terms of
  227. ;;; polynomials that interpolate the successive initial segments of the
  228. ;;; given sequences.
  229. ;;; This is done by incrementally constructing a Neville-like tableau
  230. ;;; for the interpolating polynomials, specialized for argument 0.
  231. (define polynomial-extrapolation
  232. (letrec ((pt-lp
  233. (lambda (xs ys xstate ystate)
  234. (let ((x (head xs)))
  235. (letrec ((poly-lp
  236. (lambda (y xstate ystate)
  237. (if (null? ystate)
  238. (list y)
  239. (cons y
  240. (poly-lp (/ (- (* y (car xstate))
  241. (* x (car ystate)))
  242. (- (car xstate) x))
  243. (cdr xstate)
  244. (cdr ystate)))))))
  245. (let ((new (poly-lp (head ys) xstate ystate)))
  246. (cons-stream (car (last-pair new))
  247. (pt-lp (tail xs)
  248. (tail ys)
  249. (cons x xstate)
  250. new))))))))
  251. (lambda (abscissas ordinates)
  252. (pt-lp abscissas ordinates '() '()))))
  253. ;;; Given a sequence (stream) of abscissas and ordinates for a function,
  254. ;;; the following procedure constructs a sequence of constant terms of
  255. ;;; rational functions that interpolate the successive initial segments
  256. ;;; of the given sequences.
  257. ;;; This is done by incrementally constructing a Bulirsch-Stoer tableau
  258. ;;; for the interpolating rational functions, specialized for argument 0.
  259. (define rational-extrapolation
  260. (letrec ((pt-lp
  261. (lambda (xs ys xstate ystate)
  262. (let ((x (head xs)))
  263. (letrec ((rat-lp
  264. (lambda (y xstate ystate z)
  265. (if (null? ystate)
  266. (list y)
  267. (let ((u (* (/ (car xstate) x)
  268. (- (car ystate) z))))
  269. (cons y
  270. (rat-lp
  271. (/ (+ (* y (- u (car ystate)))
  272. (* z (car ystate)))
  273. (- u (- y z)))
  274. (cdr xstate)
  275. (cdr ystate)
  276. (car ystate))))))))
  277. (let ((new (rat-lp (head ys) xstate ystate 0)))
  278. (cons-stream (car (last-pair new))
  279. (pt-lp (tail xs)
  280. (tail ys)
  281. (cons x xstate)
  282. new))))))))
  283. (lambda (abscissas ordinates)
  284. (pt-lp abscissas ordinates '() '()))))