-
-
Notifications
You must be signed in to change notification settings - Fork 18
/
interpolate.cljc
311 lines (250 loc) · 11.5 KB
/
interpolate.cljc
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
#_"SPDX-License-Identifier: GPL-3.0"
(ns emmy.rational-function.interpolate
"This namespace contains a discussion of rational function interpolation, and
different methods for fitting rational functions to `N` points and evaluating
them at some value `x`."
(:require [emmy.algebra.fold :as af]
[emmy.generic :as g]
[emmy.polynomial.interpolate :as pi]
[taoensso.timbre :as log]))
;; ## Rational Function Interpolation
;;
;; This namespace contains implementations of rational function interpolation
;; methods. The [ALGLib](https://www.alglib.net/interpolation/rational.php) user
;; guide has a nice page on [rational function
;; interpolation](https://www.alglib.net/interpolation/rational.php), which
;; suggests that the Bulirsch-Stoer method, included here, is NOT great, and
;; that there are better methods. We'd love implementations of the others if you
;; agree!
;;
;; The main method in this package is an incremental version of the
;; Bulirsch-Stoer algorithm.
;;
;; Just like with polynomial interpolation, let's start with a straightforward
;; implementation of the non-incremental recursive algorithm.
(defn bulirsch-stoer-recursive
"Returns the value of `P(x)`, where `P` is rational function fit (using the
Bulirsch-Stoer algorithm, of similar style to Neville's algorithm described in
[[emmy.numerical.interpolate.polynomial]]) to every point in the supplied
sequence `points`.
`points`: is a sequence of pairs of the form `[x (f x)]`.
\"The Bulirsch-Stoer algorithm produces the so-called diagonal rational
function, with the degrees of numerator and denominator equal (if m is even)
or with the degree of the denominator larger by one if m is odd.\" ~ Press,
Numerical Recipes, p105
The implementation follows [Equation 3.2.3 on on page 105 of
Press](http://phys.uri.edu/nigh/NumRec/bookfpdf/f3-2.pdf).
References:
- Stoer & Bulirsch, ['Introduction to Numerical Analysis'](https://www.amazon.com/Introduction-Numerical-Analysis-Applied-Mathematics/dp/144193006X)
- [PDF of the same reference](http://www.math.uni.wroc.pl/~olech/metnum2/Podreczniki/(eBook)%20Introduction%20to%20Numerical%20Analysis%20-%20J.Stoer,R.Bulirsch.pdf)
- Press's Numerical Recipes (p105), [Section 3.2](http://phys.uri.edu/nigh/NumRec/bookfpdf/f3-2.pdf)"
[points x]
(letfn [(evaluate [points x]
(cond (empty? points) 0
(= 1 (count points))
(let [[[_ y]] points]
y)
:else
(let [l-branch (pop points)
r-branch (subvec points 1)
center (pop r-branch)
[xl] (first points)
[xr] (peek points)
rl (evaluate l-branch x)
rr (evaluate r-branch x)
rc (evaluate center x)
p (g/- rr rl)
q (-> (g// (g/- x xl)
(g/- x xr))
(g/* (g/- 1 (g// p (g/- rr rc))))
(g/- 1))]
(g/+ rr (g// p q)))))]
(let [point-array (vec points)]
(evaluate point-array x))))
;; We can be a bit more clever, if we reuse the idea of the "tableau" described
;; in the polynomial namespace.
(defn bs-prepare [[x fx]]
[x x 0 fx])
(defn bs-merge [x]
(fn [[xl _ _ rl] [_ xr rc rr]]
(let [p (- rr rl)
q (-> (/ (- x xl)
(- x xr))
(* (- 1 (/ p (- rr rc))))
(- 1))]
[xl xr rl (+ rr (/ p q))])))
(defn- bs-present [row]
(map peek row))
(defn bulirsch-stoer
"Takes
- a (potentially lazy) sequence of `points` of the form `[x (f x)]` and
- a point `x` to interpolate
and generates a lazy sequence of approximations of `P(x)`. Each entry in the
return sequence incorporates one more point from `points` into the `P(x)`
estimate.
`P(x)` is rational function fit (using the Bulirsch-Stoer algorithm, of
similar style to Neville's algorithm described
in [[emmy.numerical.interpolate.polynomial]]) to every point in the
supplied sequence `points`.
\"The Bulirsch-Stoer algorithm produces the so-called diagonal rational
function, with the degrees of numerator and denominator equal (if m is even)
or with the degree of the denominator larger by one if m is odd.\" ~ Press,
Numerical Recipes, p105
The implementation follows [Equation 3.2.3 on on page 105 of
Press](http://phys.uri.edu/nigh/NumRec/bookfpdf/f3-2.pdf).
### Column
If you supply an integer for the third (optional) `column` argument,
`bulirsch-stoer` will return that /column/ offset the interpolation tableau
instead of the first row. This will give you a sequence of nth-order
polynomial approximations taken between point `i` and the next `n` points.
As a reminder, this is the shape of the tableau:
```
p0 p01 p012 p0123 p01234
p1 p12 p123 p1234 .
p2 p23 p234 . .
p3 p34 . . .
p4 . . . .
```
So supplying a `column` of `1` gives a sequence of 2-point approximations
between pairs of points; `2` gives 3-point approximations between successive
triplets, etc.
References:
- Stoer & Bulirsch, ['Introduction to Numerical Analysis'](https://www.amazon.com/Introduction-Numerical-Analysis-Applied-Mathematics/dp/144193006X)
- [PDF of the same reference](http://www.math.uni.wroc.pl/~olech/metnum2/Podreczniki/(eBook)%20Introduction%20to%20Numerical%20Analysis%20-%20J.Stoer,R.Bulirsch.pdf)
- Press's Numerical Recipes (p105), [Section 3.2](http://phys.uri.edu/nigh/NumRec/bookfpdf/f3-2.pdf)"
([points x] (bulirsch-stoer points x nil))
([points x column]
(let [merge (bs-merge x)
tableau (pi/tableau-fn bs-prepare merge points)]
(bs-present
(if column
(nth tableau column)
(pi/first-terms tableau))))))
;; ## Incremental Bulirsch-Stoer
;;
;; Press, in [Numerical Recipes section
;; 3.2](http://phys.uri.edu/nigh/NumRec/bookfpdf/f3-2.pdf), describes a
;; modification to the Bulirsch-Stoer that lets you track the differences from
;; the left and left-up entries in the tableau, just like the modified Neville
;; method in `polynomial.cljc`. the algorithm is implemented below.
(defn- mbs-prepare
"Processes an initial point `[x (f x)]` into the required state:
```
[x_l, x_r, C, D]
```
The recursion starts with $C = D = f(x)$."
[[x fx]] [x x fx fx])
(defn- mbs-merge
"Implements the recursion rules described in Press's Numerical Recipes, [section
3.2](http://phys.uri.edu/nigh/NumRec/bookfpdf/f3-2.pdf) to generate x_l, x_r,
C and D for a tableau node, given the usual left and left-up tableau entries.
This merge function ALSO includes a 'zero denominator fix used by Bulirsch and
Stoer and Henon', in the words of Sussman from `rational.scm` in the scmutils
package.
If the denominator is 0, we pass along `C` from the up-left node and `d` from
the previous entry in the row. Otherwise, we use the algorithm to calculate.
TODO understand why this works, or where it helps!"
[x]
(fn [[xl _ _ dl] [_ xr cr _]]
(let [c-d (- cr dl)
d*ratio (-> (/ (- x xl)
(- x xr))
(* dl))
den (- d*ratio cr)]
(if (zero? den)
(do (log/info "zero denominator!")
[xl xr cr dl])
(let [cnum (* d*ratio c-d)
dnum (* cr c-d)]
[xl xr (/ cnum den) (/ dnum den)])))))
(defn modified-bulirsch-stoer
"Similar to [[bulirsch-stoer]] (the interface is identical) but slightly more
efficient. Internally this builds up its estimates by tracking the delta from
the previous estimate.
This non-obvious change lets us swap an addition in for a division,
making the algorithm slightly more efficient.
See [[bulirsch-stoer]] for usage information, and info about the required
structure of the arguments.
References:
- Press's Numerical Recipes (p105), [Section 3.2](http://phys.uri.edu/nigh/NumRec/bookfpdf/f3-2.pdf)"
[points x]
(pi/mn-present
(pi/first-terms
(pi/tableau-fn mbs-prepare
(mbs-merge x)
points))))
;; ## Rational Interpolation as a Fold
;;
;; Just like in `polynomial.cljc`, we can write rational interpolation in the
;; style of a functional fold:
(defn bulirsch-stoer-fold
"Given some point `x`, returns a fold that accumulates rows of a rational
function interpolation tableau providing successively better estimates (at the
value `x`) of a rational function interpolated to all seen points.
The 2-arity aggregation step takes:
- `previous-row`: previous row of an interpolation tableau
- a new point of the form `[x_new (f x_new)]`
Returns a function that accepts:
- `previous-row`: previous row of an interpolation tableau
- a new point of the form `[x (f x)]`
and returns the next row of the tableau using the algorithm described in
[[bulirsch-stoer]]."
[x]
(pi/tableau-fold-fn bs-prepare
(bs-merge x)
(fn [row]
(peek (last row)))))
(defn modified-bulirsch-stoer-fold
"Given some point `x`, returns a fold that accumulates rows of a rational
function interpolation tableau providing successively better estimates (at the
value `x`) of a rational function interpolated to all seen points.
The 2-arity aggregation step takes:
- `previous-row`: previous row of an interpolation tableau
- a new point of the form `[x_new (f x_new)]`
Returns a function that accepts:
- `previous-row`: previous row of an interpolation tableau
- a new point of the form `[x (f x)]`
and returns the next row of the tableau using the algorithm described in
[[modified-bulirsch-stoer]]."
[x]
(pi/tableau-fold-fn mbs-prepare
(mbs-merge x)
pi/mn-present-final))
(defn bulirsch-stoer-sum
"Returns a function that consumes an entire sequence `xs` of points of the form
`[x_i, f(x_i)]` and returns the best approximation of `x` using a rational
function fitted to all points in `xs` using the algorithm described
in [[modified-bulirsch-stoer]].
Faster than, but equivalent to, `(last ([[bulirsch-stoer]] xs x))`"
[x]
(af/fold->sum-fn
(bulirsch-stoer-fold x)))
(defn bulirsch-stoer-scan
"Returns a function that consumes an entire sequence `xs` of points of the form
`[x_i, f(x_i)]` and returns a lazy sequence of successive approximations of
`x` using rational functions fitted to the first point, then the first and
second points, etc. using the algorithm described
in [[modified-bulirsch-stoer]].
Equivalent to `([[bulirsch-stoer]] xs x)`."
[x]
(af/fold->scan-fn
(bulirsch-stoer-fold x)))
(defn modified-bulirsch-stoer-sum
"Returns a function that consumes an entire sequence `xs` of points of the form
`[x_i, f(x_i)]` and returns the best approximation of `x` using a rational
function fitted to all points in `xs` using the algorithm described
in [[modified-bulirsch-stoer]].
Faster than, but equivalent to, `(last ([[modified-bulirsch-stoer]] xs x))`"
[x]
(af/fold->sum-fn
(modified-bulirsch-stoer-fold x)))
(defn modified-bulirsch-stoer-scan
"Returns a function that consumes an entire sequence `xs` of points of the form
`[x_i, f(x_i)]` and returns a lazy sequence of successive approximations of
`x` using rational functions fitted to the first point, then the first and
second points, etc. using the algorithm described
in [[modified-bulirsch-stoer]].
Equivalent to `([[modified-bulirsch-stoer]] xs x)`."
[x]
(af/fold->scan-fn
(modified-bulirsch-stoer-fold x)))