source: qd-bessel.lisp @ c7fc98

Last change on this file since c7fc98 was c7fc98, checked in by Raymond Toy <toy.raymond@…>, 3 years ago

Fix typo in %big-a. Just use incomplete-gamma-tail in big-i.

  • Property mode set to 100644
File size: 10.8 KB
Line 
1;;;; -*- Mode: lisp -*-
2;;;;
3;;;; Copyright (c) 2011 Raymond Toy
4;;;; Permission is hereby granted, free of charge, to any person
5;;;; obtaining a copy of this software and associated documentation
6;;;; files (the "Software"), to deal in the Software without
7;;;; restriction, including without limitation the rights to use,
8;;;; copy, modify, merge, publish, distribute, sublicense, and/or sell
9;;;; copies of the Software, and to permit persons to whom the
10;;;; Software is furnished to do so, subject to the following
11;;;; conditions:
12;;;;
13;;;; The above copyright notice and this permission notice shall be
14;;;; included in all copies or substantial portions of the Software.
15;;;;
16;;;; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17;;;; EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
18;;;; OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19;;;; NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
20;;;; HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
21;;;; WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22;;;; FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23;;;; OTHER DEALINGS IN THE SOFTWARE.
24
25(in-package #:oct)
26
27;;; References:
28;;;
29;;; [1] Borwein, Borwein, Crandall, "Effective Laguerre Asymptotics",
30;;; http://people.reed.edu/~crandall/papers/Laguerre-f.pdf
31;;;
32;;; [2] Borwein, Borwein, Chan, "The Evaluation of Bessel Functions
33;;; via Exp-Arc Integrals", http://web.cs.dal.ca/~jborwein/bessel.pdf
34;;;
35
36(defvar *debug-exparc* nil)
37
38;; B[k](p) = 1/2^(k+3/2)*integrate(exp(-p*u)*u^(k-1/2),u,0,1)
39;;         = 1/2^(k+3/2)/p^(k+1/2)*integrate(t^(k-1/2)*exp(-t),t,0,p)
40;;         = 1/2^(k+3/2)/p^(k+1/2) * g(k+1/2, p)
41;;
42;; where G(a,z) is the lower incomplete gamma function.
43;;
44;; There is the continued fraction expansion for G(a,z) (see
45;; cf-incomplete-gamma in qd-gamma.lisp):
46;;
47;;  G(a,z) = z^a*exp(-z)/ CF
48;;
49;; So
50;;
51;;  B[k](p) = 1/2^(k+3/2)/p^(k+1/2)*p^(k+1/2)*exp(-p)/CF
52;;          = exp(-p)/2^(k+3/2)/CF
53;;
54;;
55;; Note also that [2] gives a recurrence relationship for B[k](p) in
56;; eq (2.6), but there is an error there.  The correct relationship is
57;;
58;;  B[k](p) = -exp(-p)/(p*sqrt(2)*2^(k+1)) + (k-1/2)*B[k-1](p)/(2*p)
59;;
60;; The paper is missing the division by p in the term containing
61;; B[k-1](p).  This is easily derived from the recurrence relationship
62;; for the (lower) incomplete gamma function.
63;;
64;; Note too that as k increases, the recurrence appears to be unstable
65;; and B[k](p) begins to increase even though it is strictly bounded.
66;; (This is also easy to see from the integral.)  Hence, we do not use
67;; the recursion.  However, it might be stable for use with
68;; double-float precision; this has not been tested.
69;;
70(defun bk (k p)
71  (/ (exp (- p))
72     (* (sqrt (float 2 (realpart p))) (ash 1 (+ k 1)))
73     (let ((a (float (+ k 1/2) (realpart p))))
74       (lentz #'(lambda (n)
75                  (+ n a))
76              #'(lambda (n)
77                  (if (evenp n)
78                      (* (ash n -1) p)
79                      (- (* (+ a (ash n -1)) p))))))))
80
81;; exp-arc I function, as given in the Laguerre paper
82;;
83;; I(p, q) = 4*exp(p) * sum(g[k](-2*%i*q)/(2*k)!*B[k](p), k, 0, inf)
84;;
85;; where g[k](p) = product(p^2+(2*j-1)^2, j, 1, k) and B[k](p) as above.
86;;
87;; For computation, note that g[k](p) = g[k-1](p) * (p^2 + (2*k-1)^2)
88;; and (2*k)! = (2*k-2)! * (2*k-1) * (2*k).  Then, let
89;;
90;;  R[k](p) = g[k](p)/(2*k)!
91;;
92;; Then
93;;
94;;  R[k](p) = g[k](p)/(2*k)!
95;;          = g[k-1](p)/(2*k-2)! * (p^2 + (2*k-1)^2)/((2*k-1)*(2*k)
96;;          = R[k-1](p) * (p^2 + (2*k-1)^2)/((2*k-1)*(2*k)
97;;
98;; In the exp-arc paper, the function is defined (equivalently) as
99;;
100;; I(p, q) = 2*%i*exp(p)/q * sum(r[2*k+1](-2*%i*q)/(2*k)!*B[k](p), k, 0, inf)
101;;
102;; where r[2*k+1](p) = p*product(p^2 + (2*j-1)^2, j, 1, k)
103;;
104;; Let's note some properties of I(p, q).
105;;
106;; I(-%i*z, v) = 2*%i*exp(-%i*z)/q * sum(r[2*k+1](-2*%i*v)/(2*k)!*B[k](-%i*z))
107;;
108;; Note thate B[k](-%i*z) = 1/2^(k+3/2)*integrate(exp(%i*z*u)*u^(k-1/2),u,0,1)
109;;                        = conj(B[k](%i*z).
110;;
111;; Hence I(-%i*z, v) = conj(I(%i*z, v)) when both z and v are real.
112(defun exp-arc-i (p q)
113  (let* ((sqrt2 (sqrt (float 2 (realpart p))))
114         (exp/p/sqrt2 (/ (exp (- p)) p sqrt2))
115         (v (* #c(0 -2) q))
116         (v2 (expt v 2))
117         (eps (epsilon (realpart p))))
118    (when *debug-exparc*
119      (format t "sqrt2 = ~S~%" sqrt2)
120      (format t "exp/p/sqrt2 = ~S~%" exp/p/sqrt2))
121    (do* ((k 0 (1+ k))
122          (bk (/ (incomplete-gamma 1/2 p)
123                 2 sqrt2 (sqrt p))
124              (- (/ (* bk (- k 1/2)) 2 p)
125                 (/ exp/p/sqrt2 (ash 1 (+ k 1)))))
126          ;; ratio[k] = r[2*k+1](v)/(2*k)!.
127          ;; r[1] = v and r[2*k+1](v) = r[2*k-1](v)*(v^2 + (2*k-1)^2)
128          ;; ratio[0] = v
129          ;; and ratio[k] = r[2*k-1](v)*(v^2+(2*k-1)^2) / ((2*k-2)! * (2*k-1) * 2*k)
130          ;;              = ratio[k]*(v^2+(2*k-1)^2)/((2*k-1) * 2 * k)
131          (ratio v
132                 (* ratio (/ (+ v2 (expt (1- (* 2 k)) 2))
133                             (* 2 k (1- (* 2 k))))))
134          (term (* ratio bk)
135                (* ratio bk))
136          (sum term (+ sum term)))
137         ((< (abs term) (* (abs sum) eps))
138          (* sum #c(0 2) (/ (exp p) q)))
139      (when *debug-exparc*
140        (format t "k      = ~D~%" k)
141        (format t " bk    = ~S~%" bk)
142        (format t " ratio = ~S~%" ratio)
143        (format t " term  = ~S~%" term)
144        (format t " sum   - ~S~%" sum)))))
145
146(defun exp-arc-i-2 (p q)
147  (let* ((sqrt2 (sqrt (float 2 (realpart p))))
148         (exp/p/sqrt2 (/ (exp (- p)) p sqrt2))
149         (v (* #c(0 -2) q))
150         (v2 (expt v 2))
151         (eps (epsilon (realpart p))))
152    (when *debug-exparc*
153      (format t "sqrt2 = ~S~%" sqrt2)
154      (format t "exp/p/sqrt2 = ~S~%" exp/p/sqrt2))
155    (do* ((k 0 (1+ k))
156          (bk (bk 0 p)
157              (bk k p))
158          (ratio v
159                 (* ratio (/ (+ v2 (expt (1- (* 2 k)) 2))
160                             (* 2 k (1- (* 2 k))))))
161          (term (* ratio bk)
162                (* ratio bk))
163          (sum term (+ sum term)))
164         ((< (abs term) (* (abs sum) eps))
165          (* sum #c(0 2) (/ (exp p) q)))
166      (when *debug-exparc*
167        (format t "k      = ~D~%" k)
168        (format t " bk    = ~S~%" bk)
169        (format t " ratio = ~S~%" ratio)
170        (format t " term  = ~S~%" term)
171        (format t " sum   - ~S~%" sum)))))
172
173
174;; This currently only works for v an integer.
175;;
176(defun integer-bessel-j-exp-arc (v z)
177  (let* ((iz (* #c(0 1) z))
178         (i+ (exp-arc-i-2 iz v))
179         (i- (exp-arc-i-2 (- iz ) v)))
180    (/ (+ (* (cis (* v (float-pi i+) -1/2))
181             i+)
182          (* (cis (* v (float-pi i+) 1/2))
183             i-))
184       (float-pi i+)
185       2)))
186
187;; alpha[n](z) = integrate(exp(-z*s)*s^n, s, 0, 1/2)
188;; beta[n](z)  = integrate(exp(-z*s)*s^n, s, -1/2, 1/2)
189;;
190;; The recurrence in [2] is
191;;
192;; alpha[n](z) = - exp(-z/2)/2^n/z + n/z*alpha[n-1](z)
193;; beta[n]z)   = ((-1)^n*exp(z/2)-exp(-z/2))/2^n/z + n/z*beta[n-1](z)
194;;
195;; We also note that
196;;
197;; alpha[n](z) = G(n+1,z/2)/z^(n+1)
198;; beta[n](z)  = G(n+1,z/2)/z^(n+1) - G(n+1,-z/2)/z^(n+1)
199
200(defun alpha (n z)
201  (let ((n (float n (realpart z))))
202    (/ (cf-incomplete-gamma (1+ n) (/ z 2))
203       (expt z (1+ n)))))
204
205(defun beta (n z)
206  (let ((n (float n (realpart z))))
207    (/ (- (cf-incomplete-gamma (1+ n) (/ z 2))
208          (cf-incomplete-gamma (1+ n) (/ z -2)))
209       (expt z (1+ n)))))
210
211;; a[0](k,v) := (k+sqrt(k^2+1))^(-v);
212;; a[1](k,v) := -v*a[0](k,v)/sqrt(k^2+1);
213;; a[n](k,v) := 1/(k^2+1)/(n-1)/n*((v^2-(n-2)^2)*a[n-2](k,v)-k*(n-1)*(2*n-3)*a[n-1](k,v));
214
215;; Convert this to iteration instead of using this quick-and-dirty
216;; memoization?
217(let ((hash (make-hash-table :test 'equal)))
218  (defun an-clrhash ()
219    (clrhash hash))
220  (defun an-dump-hash ()
221    (maphash #'(lambda (k v)
222                 (format t "~S -> ~S~%" k v))
223             hash))
224  (defun an (n k v)
225    (or (gethash (list n k v) hash)
226        (let ((result
227                (cond ((= n 0)
228                       (expt (+ k (sqrt (float (1+ (* k k)) (realpart v)))) (- v)))
229                      ((= n 1)
230                       (- (/ (* v (an 0 k v))
231                             (sqrt (float (1+ (* k k)) (realpart v))))))
232                      (t
233                       (/ (- (* (- (* v v) (expt (- n 2) 2)) (an (- n 2) k v))
234                             (* k (- n 1) (+ n n -3) (an (- n 1) k v)))
235                          (+ 1 (* k k))
236                          (- n 1)
237                          n)))))
238          (setf (gethash (list n k v) hash) result)
239          result))))
240
241;; SUM-AN computes the series
242;;
243;; sum(exp(-k*z)*a[n](k,v), k, 1, N)
244;;
245(defun sum-an (big-n n v z)
246  (let ((sum 0))
247    (loop for k from 1 upto big-n
248          do
249             (incf sum (* (exp (- (* k z)))
250                          (an n k v))))
251    sum))
252
253;; SUM-AB computes the series
254;;
255;; sum(alpha[n](z)*a[n](0,v) + beta[n](z)*sum_an(N, n, v, z), n, 0, inf)
256(defun sum-ab (big-n v z)
257  (let ((eps (epsilon (realpart z))))
258    (an-clrhash)
259    (do* ((n 0 (+ 1 n))
260          (term (+ (* (alpha n z) (an n 0 v))
261                   (* (beta n z) (sum-an big-n n v z)))
262                (+ (* (alpha n z) (an n 0 v))
263                   (* (beta n z) (sum-an big-n n v z))))
264          (sum term (+ sum term)))
265         ((<= (abs term) (* eps (abs sum)))
266          sum)
267      (when nil
268        (format t "n = ~D~%" n)
269        (format t " term = ~S~%" term)
270        (format t " sum  = ~S~%" sum)))))
271
272;; Convert to iteration instead of this quick-and-dirty memoization?
273(let ((hash (make-hash-table :test 'equal)))
274  (defun %big-a-clrhash ()
275    (clrhash hash))
276  (defun %big-a-dump-hash ()
277    (maphash #'(lambda (k v)
278                 (format t "~S -> ~S~%" k v))
279             hash))
280  (defun %big-a (n v)
281    (or (gethash (list n v) hash)
282        (let ((result
283                (cond ((zerop n)
284                       (expt 2 (- v)))
285                      (t
286                       (* (%big-a (- n 1) v)
287                          (/ (* (+ v n n -2) (+ v n n -1))
288                             (* 4 n (+ n v))))))))
289          (setf (gethash (list n v) hash) result)
290          result))))
291
292;; Computes A[n](v) =
293;; (-1)^n*v*2^(-v)*pochhammer(v+n+1,n-1)/(2^(2*n)*n!)  If v is a
294;; negative integer -m, use A[n](-m) = (-1)^(m+1)*A[n-m](m) for n >=
295;; m.
296(defun big-a (n v)
297  (let ((m (ftruncate v)))
298    (cond ((and (= m v) (minusp m))
299           (if (< n m)
300               (%big-a n v)
301               (let ((result (%big-a (+ n m) (- v))))
302                 (if (oddp (truncate m))
303                     result
304                     (- result)))))
305          (t
306           (%big-a n v)))))
307
308;; I[n](t, z, v) = exp(-t*z)/t^(2*n+v-1) *
309;;                  integrate(exp(-t*z*s)*(1+s)^(-2*n-v), s, 0, inf)
310;;
311;; Use the substitution u=1+s to get a new integral
312;;
313;;   integrate(exp(-t*z*s)*(1+s)^(-2*n-v), s, 0, inf)
314;;     = exp(t*z) * integrate(u^(-v-2*n)*exp(-t*u*z), u, 1, inf)
315;;     = exp(t*z)*t^(v+2*n-1)*z^(v+2*n-1)*incomplete_gamma_tail(1-v-2*n,t*z)
316;;
317;; Thus,
318;;
319;;   I[n](t, z, v) = z^(v+2*n-1)*incomplete_gamma_tail(1-v-2*n,t*z)
320;;
321(defun big-i (n theta z v)
322  (let* ((a (- 1 v n n)))
323    (* (expt z (- a))
324       (incomplete-gamma-tail a (* theta z)))))
325
326(defun sum-big-ia (big-n v z)
327  )
328
329(defun bessel-j (v z)
330  (let ((vv (ftruncate v)))
331    (cond ((= vv v)
332           ;; v is an integer
333           (integer-bessel-j-exp-arc v z))
334          (t
335           (let ((big-n 100)
336                 (vpi (* v (float-pi (realpart z)))))
337             (+ (integer-bessel-j-exp-arc v z)
338                (* z
339                   (/ (sin vpi) vpi)
340                   (+ (/ -1 z)
341                      (sum-ab big-n v z)))))))))
342
343(defun paris-series (v z n)
344  (labels ((pochhammer (a k)
345             (/ (gamma (+ a k))
346                (gamma a)))
347           (a (v k)
348             (* (/ (pochhammer (+ 1/2 v) k)
349                   (gamma (float (1+ k) z)))
350                (pochhammer (- 1/2 v) k))))
351    (* (loop for k from 0 below n
352             sum (* (/ (a v k)
353                       (expt (* 2 z) k))
354                    (/ (cf-incomplete-gamma (+ k v 1/2) (* 2 z))
355                       (gamma (+ k v 1/2)))))
356       (/ (exp z)
357          (sqrt (* 2 (float-pi z) z))))))
358
Note: See TracBrowser for help on using the repository browser.