root/qd-bessel.lisp @ 8c5195a87137fd61952a175a5dae676c70b480ef

Revision 8c5195a87137fd61952a175a5dae676c70b480ef, 14.4 KB (checked in by Raymond Toy <toy.raymond@…>, 2 years ago)

Make big-n a defvar so we can change it easily.

  • Property mode set to 100644
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;;
113;; Also note that when v is an integer of the form (2*m+1)/2, then
114;;   r[2*k+1](-2*%i*v) = r[2*k+1](-%i*(2*m+1))
115;;                     = -%i*(2*m+1)*product(-(2*m+1)^2+(2*j-1)^2, j, 1, k)
116;; so the product is zero when k >= m and the series I(p, q) is
117;; finite.
118(defun exp-arc-i (p q)
119  (let* ((sqrt2 (sqrt (float 2 (realpart p))))
120         (exp/p/sqrt2 (/ (exp (- p)) p sqrt2))
121         (v (* #c(0 -2) q))
122         (v2 (expt v 2))
123         (eps (epsilon (realpart p))))
124    (when *debug-exparc*
125      (format t "sqrt2 = ~S~%" sqrt2)
126      (format t "exp/p/sqrt2 = ~S~%" exp/p/sqrt2))
127    (do* ((k 0 (1+ k))
128          (bk (/ (incomplete-gamma 1/2 p)
129                 2 sqrt2 (sqrt p))
130              (- (/ (* bk (- k 1/2)) 2 p)
131                 (/ exp/p/sqrt2 (ash 1 (+ k 1)))))
132          ;; ratio[k] = r[2*k+1](v)/(2*k)!.
133          ;; r[1] = v and r[2*k+1](v) = r[2*k-1](v)*(v^2 + (2*k-1)^2)
134          ;; ratio[0] = v
135          ;; and ratio[k] = r[2*k-1](v)*(v^2+(2*k-1)^2) / ((2*k-2)! * (2*k-1) * 2*k)
136          ;;              = ratio[k]*(v^2+(2*k-1)^2)/((2*k-1) * 2 * k)
137          (ratio v
138                 (* ratio (/ (+ v2 (expt (1- (* 2 k)) 2))
139                             (* 2 k (1- (* 2 k))))))
140          (term (* ratio bk)
141                (* ratio bk))
142          (sum term (+ sum term)))
143         ((< (abs term) (* (abs sum) eps))
144          (* sum #c(0 2) (/ (exp p) q)))
145      (when *debug-exparc*
146        (format t "k      = ~D~%" k)
147        (format t " bk    = ~S~%" bk)
148        (format t " ratio = ~S~%" ratio)
149        (format t " term  = ~S~%" term)
150        (format t " sum   - ~S~%" sum)))))
151
152(defun exp-arc-i-2 (p q)
153  (let* ((v (* #c(0 -2) q))
154         (v2 (expt v 2))
155         (eps (epsilon (realpart p))))
156    (do* ((k 0 (1+ k))
157          (bk (bk 0 p)
158              (bk k p))
159          ;; Compute g[k](p)/(2*k)!, not r[2*k+1](p)/(2*k)!
160          (ratio 1
161                 (* ratio (/ (+ v2 (expt (1- (* 2 k)) 2))
162                             (* 2 k (1- (* 2 k))))))
163          (term (* ratio bk)
164                (* ratio bk))
165          (sum term (+ sum term)))
166         ((< (abs term) (* (abs sum) eps))
167          (when *debug-exparc*
168            (format t "Final k= ~D~%" k)
169            (format t " bk    = ~S~%" bk)
170            (format t " ratio = ~S~%" ratio)
171            (format t " term  = ~S~%" term)
172            (format t " sum   - ~S~%" sum))
173          (* sum 4 (exp p)))
174      (when *debug-exparc*
175        (format t "k      = ~D~%" k)
176        (format t " bk    = ~S~%" bk)
177        (format t " ratio = ~S~%" ratio)
178        (format t " term  = ~S~%" term)
179        (format t " sum   - ~S~%" sum)))))
180
181
182;; Not really just for Bessel J for integer orders, but in that case,
183;; this is all that's needed to compute Bessel J.  For other values,
184;; this is just part of the computation needed.
185;;
186;; Compute
187;;
188;;  1/(2*%pi) * (exp(-%i*v*%pi/2) * I(%i*z, v) + exp(%i*v*%pi/2) * I(-%i*z, v))
189(defun integer-bessel-j-exp-arc (v z)
190  (let* ((iz (* #c(0 1) z))
191         (i+ (exp-arc-i-2 iz v)))
192    (cond ((and (= v (ftruncate v)) (realp z))
193           ;; We can simplify the result
194           (let ((c (exp (* v (float-pi i+) #c(0 -1/2)))))
195             (/ (+ (* c i+)
196                   (* (conjugate c) (conjugate i+)))
197                (float-pi i+)
198                2)))
199          (t
200           (let ((i- (exp-arc-i-2 (- iz ) v)))
201             (/ (+ (* (exp (* v (float-pi i+) #c(0 -1/2)))
202                      i+)
203                   (* (exp (* v (float-pi i+) #c(0 1/2)))
204                      i-))
205                (float-pi i+)
206                2))))))
207
208;; alpha[n](z) = integrate(exp(-z*s)*s^n, s, 0, 1/2)
209;; beta[n](z)  = integrate(exp(-z*s)*s^n, s, -1/2, 1/2)
210;;
211;; The recurrence in [2] is
212;;
213;; alpha[n](z) = - exp(-z/2)/2^n/z + n/z*alpha[n-1](z)
214;; beta[n]z)   = ((-1)^n*exp(z/2)-exp(-z/2))/2^n/z + n/z*beta[n-1](z)
215;;
216;; We also note that
217;;
218;; alpha[n](z) = G(n+1,z/2)/z^(n+1)
219;; beta[n](z)  = G(n+1,z/2)/z^(n+1) - G(n+1,-z/2)/z^(n+1)
220
221(defun alpha (n z)
222  (let ((n (float n (realpart z))))
223    (/ (incomplete-gamma (1+ n) (/ z 2))
224       (expt z (1+ n)))))
225
226(defun beta (n z)
227  (let ((n (float n (realpart z))))
228    (/ (- (incomplete-gamma (1+ n) (/ z 2))
229          (incomplete-gamma (1+ n) (/ z -2)))
230       (expt z (1+ n)))))
231
232;; a[0](k,v) := (k+sqrt(k^2+1))^(-v);
233;; a[1](k,v) := -v*a[0](k,v)/sqrt(k^2+1);
234;; 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));
235
236;; Convert this to iteration instead of using this quick-and-dirty
237;; memoization?
238(let ((hash (make-hash-table :test 'equal)))
239  (defun an-clrhash ()
240    (clrhash hash))
241  (defun an-dump-hash ()
242    (maphash #'(lambda (k v)
243                 (format t "~S -> ~S~%" k v))
244             hash))
245  (defun an (n k v)
246    (or (gethash (list n k v) hash)
247        (let ((result
248                (cond ((= n 0)
249                       (expt (+ k (sqrt (float (1+ (* k k)) (realpart v)))) (- v)))
250                      ((= n 1)
251                       (- (/ (* v (an 0 k v))
252                             (sqrt (float (1+ (* k k)) (realpart v))))))
253                      (t
254                       (/ (- (* (- (* v v) (expt (- n 2) 2)) (an (- n 2) k v))
255                             (* k (- n 1) (+ n n -3) (an (- n 1) k v)))
256                          (+ 1 (* k k))
257                          (- n 1)
258                          n)))))
259          (setf (gethash (list n k v) hash) result)
260          result))))
261
262;; SUM-AN computes the series
263;;
264;; sum(exp(-k*z)*a[n](k,v), k, 1, N)
265;;
266#+nil
267(defun sum-an (big-n n v z)
268  (let ((sum 0))
269    (loop for k from 1 upto big-n
270          do
271             (incf sum (* (exp (- (* k z)))
272                          (an n k v))))
273    sum))
274
275;; Like above, but we just stop when the terms no longer contribute to
276;; the sum.
277(defun sum-an (big-n n v z)
278  (let ((eps (epsilon (realpart z))))
279    (do* ((k 1 (+ 1 k))
280          (term (* (exp (- (* k z)))
281                   (an n k v))
282                (* (exp (- (* k z)))
283                   (an n k v)))
284          (sum term (+ sum term)))
285         ((or (<= (abs term) (* eps (abs sum)))
286              (> k big-n))
287          sum))))
288
289;; SUM-AB computes the series
290;;
291;; sum(alpha[n](z)*a[n](0,v) + beta[n](z)*sum_an(N, n, v, z), n, 0, inf)
292(defun sum-ab (big-n v z)
293  (let ((eps (epsilon (realpart z))))
294    (an-clrhash)
295    (do* ((n 0 (+ 1 n))
296          (term (+ (* (alpha n z) (an n 0 v))
297                   (* (beta n z) (sum-an big-n n v z)))
298                (+ (* (alpha n z) (an n 0 v))
299                   (* (beta n z) (sum-an big-n n v z))))
300          (sum term (+ sum term)))
301         ((<= (abs term) (* eps (abs sum)))
302          sum)
303      (when nil
304        (format t "n = ~D~%" n)
305        (format t " term = ~S~%" term)
306        (format t " sum  = ~S~%" sum)))))
307
308;; Convert to iteration instead of this quick-and-dirty memoization?
309(let ((hash (make-hash-table :test 'equal)))
310  (defun %big-a-clrhash ()
311    (clrhash hash))
312  (defun %big-a-dump-hash ()
313    (maphash #'(lambda (k v)
314                 (format t "~S -> ~S~%" k v))
315             hash))
316  (defun %big-a (n v)
317    (or (gethash (list n v) hash)
318        (let ((result
319                (cond ((zerop n)
320                       (expt 2 (- v)))
321                      (t
322                       (* (%big-a (- n 1) v)
323                          (/ (* (+ v n n -2) (+ v n n -1))
324                             (* 4 n (+ n v))))))))
325          (setf (gethash (list n v) hash) result)
326          result))))
327
328;; Computes A[n](v) =
329;; (-1)^n*v*2^(-v)*pochhammer(v+n+1,n-1)/(2^(2*n)*n!)  If v is a
330;; negative integer -m, use A[n](-m) = (-1)^(m+1)*A[n-m](m) for n >=
331;; m.
332(defun big-a (n v)
333  (let ((m (ftruncate v)))
334    (cond ((and (= m v) (minusp m))
335           (if (< n m)
336               (%big-a n v)
337               (let ((result (%big-a (+ n m) (- v))))
338                 (if (oddp (truncate m))
339                     result
340                     (- result)))))
341          (t
342           (%big-a n v)))))
343
344;; I[n](t, z, v) = exp(-t*z)/t^(2*n+v-1) *
345;;                  integrate(exp(-t*z*s)*(1+s)^(-2*n-v), s, 0, inf)
346;;
347;; Use the substitution u=1+s to get a new integral
348;;
349;;   integrate(exp(-t*z*s)*(1+s)^(-2*n-v), s, 0, inf)
350;;     = exp(t*z) * integrate(u^(-v-2*n)*exp(-t*u*z), u, 1, inf)
351;;     = exp(t*z)*t^(v+2*n-1)*z^(v+2*n-1)*incomplete_gamma_tail(1-v-2*n,t*z)
352;;
353;; Thus,
354;;
355;;   I[n](t, z, v) = z^(v+2*n-1)*incomplete_gamma_tail(1-v-2*n,t*z)
356;;
357(defun big-i (n theta z v)
358  (let* ((a (- 1 v n n)))
359    (* (expt z (- a))
360       (incomplete-gamma-tail a (* theta z)))))
361
362(defun sum-big-ia (big-n v z)
363  (let ((big-n-1/2 (+ big-n 1/2))
364        (eps (epsilon z)))
365    (do* ((n 0 (1+ n))
366          (term (* (big-a 0 v)
367                   (big-i 0 big-n-1/2 z v))
368                (* (big-a n v)
369                   (big-i n big-n-1/2 z v)))
370          (sum term (+ sum term)))
371         ((<= (abs term) (* eps (abs sum)))
372          sum)
373      #+nil
374      (progn
375        (format t "n = ~D~%" n)
376        (format t " term = ~S~%" term)
377        (format t " sum  = ~S~%" sum)))))
378
379;; Series for bessel J:
380;;
381;; (z/2)^v*sum((-1)^k/Gamma(k+v+1)/k!*(z^2//4)^k, k, 0, inf)
382(defun s-bessel-j (v z)
383  (with-floating-point-contagion (v z)
384    (let ((z2/4 (* z z 1/4))
385          (eps (epsilon z)))
386      (do* ((k 0 (+ 1 k))
387            (f (gamma (+ v 1))
388               (* k (+ v k)))
389            (term (/ f)
390                  (/ (* (- term) z2/4) f))
391            (sum term (+ sum term)))
392           ((<= (abs term) (* eps (abs sum)))
393            (* sum (expt (* z 1/2) v)))
394        #+nil
395        (progn
396          (format t "k = ~D~%" k)
397          (format t " f    = ~S~%" f)
398          (format t " term = ~S~%" term)
399          (format t " sum  = ~S~%" sum))))))
400
401;;
402;; TODO:
403;;  o For |z| <= 1 use the series.
404;;  o Currently accuracy is not good for large z and half-integer
405;;    order.
406;;  o For real v and z, return a real number instead of complex.
407;;  o Handle the case of Re(z) < 0. (The formulas are for Re(z) > 0:
408;;    bessel_j(v,z*exp(m*%pi*%i)) = exp(m*v*%pi*%i)*bessel_j(v, z)
409;;  o The paper suggests using
410;;      bessel_i(v,z) = exp(-v*%pi*%i/2)*bessel_j(v, %i*z)
411;;    when Im(z) >> Re(z)
412;;
413(defvar *big-n* 100)
414(defun bessel-j (v z)
415  (let ((vv (ftruncate v)))
416    ;; Clear the caches for now.
417    (an-clrhash)
418    (%big-a-clrhash)
419    (cond ((and (= vv v) (realp z))
420           ;; v is an integer and z is real
421           (integer-bessel-j-exp-arc v z))
422          (t
423           ;; Need to fine-tune the value of big-n.
424           (let ((big-n *big-n*)
425                 (vpi (* v (float-pi (realpart z)))))
426             (+ (integer-bessel-j-exp-arc v z)
427                (if (= vv v)
428                    0
429                    (* z
430                       (/ (sin vpi) vpi)
431                       (+ (/ -1 z)
432                          (sum-ab big-n v z)
433                          (sum-big-ia big-n v z))))))))))
434
435;; Bessel Y
436;;
437;; bessel_y(v, z) = 1/(2*%pi*%i)*(exp(-%i*v*%pi/2)*I(%i*v,z) - exp(%i*v*%pi/2)*I(-%i*z, v))
438;;                   + z/v/%pi*((1-cos(v*%pi)/z) + S(N,z,v)*cos(v*%pi)-S(N,z,-v))
439;;
440;; where
441;;
442;;   S(N,z,v) = sum(alpha[n](z)*a[n](0,v) + beta[n](z)*sum(exp(-k*z)*a[n](k,v),k,1,N),n,0,inf)
443;;               + sum(A[n](v)*I[n](N+1/2,z,v),n,0,inf)
444;;
445(defun bessel-y (v z)
446  (flet ((ipart (v z)
447           (let* ((iz (* #c(0 1) z))
448                  (c+ (exp (* v (float-pi z) 1/2)))
449                  (c- (exp (* v (float-pi z) -1/2)))
450                  (i+ (exp-arc-i-2 iz v))
451                  (i- (exp-arc-i-2 (- iz) v)))
452             (/ (- (* c- i+) (* c+ i-))
453                (* #c(0 2) (float-pi z)))))
454         (s (big-n z v)
455           (+ (sum-ab big-n v z)
456              (sum-big-ia big-n v z))))
457    (let* ((big-n 100)
458           (vpi (* v (float-pi z)))
459           (c (cos vpi)))
460      (+ (ipart v z)
461         (* (/ z vpi)
462            (+ (/ (- 1 c)
463                  z)
464               (* c
465                  (s big-n z v))
466               (- (s big-n z (- v)))))))))
467           
468 
469
470(defun paris-series (v z n)
471  (labels ((pochhammer (a k)
472             (/ (gamma (+ a k))
473                (gamma a)))
474           (a (v k)
475             (* (/ (pochhammer (+ 1/2 v) k)
476                   (gamma (float (1+ k) z)))
477                (pochhammer (- 1/2 v) k))))
478    (* (loop for k from 0 below n
479             sum (* (/ (a v k)
480                       (expt (* 2 z) k))
481                    (/ (cf-incomplete-gamma (+ k v 1/2) (* 2 z))
482                       (gamma (+ k v 1/2)))))
483       (/ (exp z)
484          (sqrt (* 2 (float-pi z) z))))))
485
Note: See TracBrowser for help on using the browser.