Add note that the lapack package needs to loaded to get the functions.
[maxima.git] / src / specfn.lisp
blob620bdd29da05e0d6598e707c69baef9594fc7427
1 ;;; -*- Mode: Lisp; Package: Maxima; Syntax: Common-Lisp; Base: 10 -*- ;;;;
2 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3 ;;; The data in this file contains enhancements. ;;;;;
4 ;;; ;;;;;
5 ;;; Copyright (c) 1984,1987 by William Schelter,University of Texas ;;;;;
6 ;;; All rights reserved ;;;;;
7 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
8 ;;; (c) Copyright 1980 Massachusetts Institute of Technology ;;;
9 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
11 (in-package :maxima)
13 (macsyma-module specfn)
15 ;;*********************************************************************
16 ;;**************** ******************
17 ;;**************** Macsyma Special Function Routines ******************
18 ;;**************** ******************
19 ;;*********************************************************************
21 (load-macsyma-macros rzmac)
22 (load-macsyma-macros mhayat)
24 (defmacro mnumericalp (arg)
25 `(or (floatp ,arg) (and (or $numer $float) (integerp ,arg))))
27 ;; subtitle polylogarithm routines
29 ;; li has mirror symmetry:
31 ;; li[s](conjugate(z)) = conjugate(li[s](z))
33 ;; for z not on the negative real line.
35 ;; See http://functions.wolfram.com/10.08.04.0002.01
36 (defprop %li t commutes-with-conjugate)
38 (declare-top (special tlist))
40 (defun lisimp (expr vestigial z)
41 (declare (ignore vestigial))
42 (let ((s (simpcheck (car (subfunsubs expr)) z))
43 ($zerobern t)
44 (a))
45 (subargcheck expr 1 1 '$li)
46 (setq a (simpcheck (car (subfunargs expr)) z))
47 (or (cond ((zerop1 a) a)
48 ((and (mnump s) (ratgreaterp s 1) (eql a 1))
49 ;; li[s](a) = zeta(s) if s > 1 and if a = 1. We
50 ;; simplify this only if s is a rational number and a
51 ;; is the integer 1.
52 (ftake '%zeta s))
53 ((not (integerp s)) ())
54 ((= s 1)
55 (if (onep1 a)
56 (simp-domain-error
57 (intl:gettext "li: li[~:M](~:M) is undefined.") s a)
58 (neg (take '(%log) (sub 1 a)))))
59 ((= s 0) (div a (sub 1 a)))
60 ((< s 0) (lisimp-negative-integer s a))
61 ((and (integerp a) (> s 1)
62 (cond ((= a 1) (take '(%zeta) s))
63 ((= a -1)
64 ;; li[s](-1) = (2^(1-s)-1)*zeta(s)
65 (mul (add -1 (inv (expt 2 (- s 1))))
66 (take '(%zeta) s))))))
67 ((= s 2) (li2simp a))
68 ((= s 3) (li3simp a))
69 ((or (complex-float-numerical-eval-p a)
70 (complex-bigfloat-numerical-eval-p a))
71 (cond ((bigfloat:= 1 (bigfloat:to a))
72 ;; li[s](1) -> zeta(s)
73 (let ((result ($zeta s)))
74 (if (floatp a)
75 ($float result)
76 ($bfloat result))))
77 ((bigfloat:= -1 (bigfloat:to a))
78 ;; li[s](-1) = (2^(1-s)-1)*zeta(s)
79 (let ((result (mul (add -1 (inv (expt 2 (- s 1))))
80 (take '(%zeta) s))))
81 (if (floatp a)
82 ($float result)
83 ($bfloat result))))
84 ((integerp s)
85 (to (bigfloat::li-s-simp s (bigfloat:to a)))))))
86 (eqtest (subfunmakes '$li (ncons s) (ncons a))
87 expr))))
89 ;; Expand the Polylogarithm li[s](z) for a negative integer parameter s.
90 (defun lisimp-negative-integer (s z)
91 (let ((n (- s)))
92 (mul (inv (power (sub 1 z) (+ n 1)))
93 (let ((index1 (gensumindex))
94 ($simpsum t))
95 (dosum
96 (mul (power z index1)
97 (let ((index2 (gensumindex)))
98 (dosum
99 (mul (power -1 (add index2 1))
100 (take '(%binomial) (+ n 1) (sub index2 1))
101 (power (add 1 (sub index1 index2)) n))
102 index2 1 index1 t)))
103 index1 1 n t)))))
105 (defun li2simp (arg)
106 (cond ((mnumericalp arg)
107 ;; When arg is a float or rational, use the original li2numer
108 ;; using Spences function.
109 (li2numer (float arg)))
110 ((complex-float-numerical-eval-p arg)
111 ;; For complex args that should should result in float
112 ;; answers, use bigfloat::li2numer.
113 (to (bigfloat::li2numer (bigfloat:to ($rectform ($float arg))))))
114 ((or (bigfloat-numerical-eval-p arg)
115 (complex-bigfloat-numerical-eval-p arg))
116 (to (bigfloat::li2numer (bigfloat:to ($rectform ($bfloat arg))))))
117 ((alike1 arg '((rat) 1 2))
118 ;; li[2](1/2) = zeta(2)/2-log(2)^2/2
119 ;; = %pi^2/12-log(2)^2/2
120 (add (div (take '(%zeta) 2) 2)
121 (mul '((rat simp) -1 2)
122 (power (take '(%log) 2) 2))))
123 ((alike1 arg 2)
124 ;; li[2](2) = %pi^2/4 - %i*%pi*log(2)
126 ;; See http://functions.wolfram.com/10.07.03.0007.01. But
127 ;; this also follows from https://dlmf.nist.gov/25.12.E4:
129 ;; li[2](z) + li[2](1/z) = -%pi^2/6-log(-z)^2/2
131 ;; with z = 2.
132 (sub (div (power '$%pi 2)
134 (mul '$%pi '$%i (ftake '%log 2))))
135 ((alike1 arg '$%i)
136 ;; li[2](%i) = %i*%catalan - %pi^2/48
138 ;; See http://functions.wolfram.com/10.07.03.0008.01
139 (sub (mul '$%i '$%catalan)
140 (mul '$%pi '$%pi (div 1 48))))
141 ((alike1 arg (neg '$%i))
142 ;; li[2](-%i) = -%i*%catalan - %pi^2/48
144 ;; See http://functions.wolfram.com/10.07.03.0009.01, but
145 ;; this follows from the mirror symmetry: li[2](conjugate(z))
146 ;; = conjugate(li[2](z)), which holds when z is not on the
147 ;; negative real line.
148 (sub (mul -1 '$%i '$%catalan)
149 (mul '$%pi '$%pi (div 1 48))))
150 ((alike1 arg (sub 1 '$%i))
151 ;; li[2](1 - %i) = %pi^2/16 - %i*%catalan - %pi*%i*log(2)/4
153 ;; See http://functions.wolfram.com/10.07.03.0010.01
154 (sub (div (power '$%pi 2) 16)
155 (add (mul '$%i '$%catalan)
156 (mul '$%pi '$%i (div 1 4) (ftake '%log 2)))))
157 ((alike1 arg (add 1 '$%i))
158 ;; li[2](1 + %i) = %pi^2/16 + %i*%catalan + %pi*%i*log(2)/4
160 ;; See http://functions.wolfram.com/10.07.03.0011.01, but
161 ;; this also follows from mirror symmetry.
162 (add (div (power '$%pi 2) 16)
163 (mul '$%i '$%catalan)
164 (mul '$%pi '$%i (div 1 4) (ftake '%log 2))))))
166 (defun li3simp (arg)
167 (cond ((or (float-numerical-eval-p arg)
168 (complex-float-numerical-eval-p arg))
169 (to (bigfloat::li3numer (bigfloat:to ($rectform ($float arg))))))
170 ((or (bigfloat-numerical-eval-p arg)
171 (complex-bigfloat-numerical-eval-p arg))
172 (to (bigfloat::li3numer (bigfloat:to ($rectform ($bfloat arg))))))
173 ((alike1 arg '((rat) 1 2))
174 (add (mul '((rat simp) 7 8) (take '(%zeta) 3))
175 (mul (div (take '(%zeta) 2) -2) (take '(%log) 2))
176 (mul '((rat simp) 1 6) (power (take '(%log) 2) 3))))))
178 ;; exponent in first term of taylor expansion of $li is one
179 (defun li-ord (subl)
180 (declare (ignore subl))
181 (ncons (rcone)))
183 ;; taylor expansion of $li is its definition:
184 ;; x + x^2/2^s + x^3/3^s + ...
185 (defun exp$li-fun (pw subl l) ; l is a irrelevant here
186 (setq subl (car subl)) ; subl is subscript of li
187 (prog ((e 0) ; e is exponent of current term
188 npw) ; npw is exponent of last term needed
189 (declare (fixnum e))
190 (setq npw (/ (float (car pw)) (float (cdr pw))))
191 (setq
192 l (cons '((0 . 1) 0 . 1)
193 nil))
194 a (setq e (1+ e))
195 (if (> e npw) (return l)
196 (rplacd (last l)
197 `(((,e . 1)
198 . ,(prep1 (m^ e (m- subl)))))))
199 (go a)))
202 ;; computes first pw terms of asymptotic expansion of $li[s](z)
204 ;; pw should be < (1/2)*s or gamma term is undefined
206 ;; Wood, D.C. (June 1992). The Computation of Polylogarithms. Technical Report 15-92
207 ;; University of Kent Computing Laboratory.
208 ;; http://www.cs.kent.ac.uk/pubs/1992/110
209 ;; equation 11.1
210 (defun li-asymptotic-expansion (pw s z)
211 (m+l (loop for k from 0 to pw collect
212 (if (and ($integerp k)
213 (mgqp 0 (m+ s 1 (m* -2 k) )))
214 0 ;; gamma in denominator below is infinite, this term is 0
215 (m* (m^ -1 k)
216 (m- 1 (m^ 2 (m- 1 (m* 2 k))))
217 (m^ (m* 2 '$%pi) (m* 2 k))
218 (m// (ftake '%bern (m* 2 k))
219 `((mfactorial) ,(m* 2 k)))
220 (m// (m^ `((%log) ,(m- z)) (m- s (m* 2 k)))
221 ($gamma (m+ s 1 (m* -2 k)))))))))
223 ;; Numerical evaluation for Chebyschev expansions of the first kind
225 (defun cheby (x chebarr)
226 (let ((bn+2 0.0) (bn+1 0.0))
227 (do ((i (floor (aref chebarr 0)) (1- i)))
228 ((< i 1) (- bn+1 (* bn+2 x)))
229 (setq bn+2
230 (prog1 bn+1 (setq bn+1 (+ (aref chebarr i)
231 (- (* 2.0 x bn+1) bn+2))))))))
233 (defun cheby-prime (x chebarr)
234 (- (cheby x chebarr)
235 (* (aref chebarr 1) 0.5)))
237 ;; These should really be calculated with minimax rational approximations.
238 ;; Someone has done LI[2] already, and this should be updated; I haven't
239 ;; seen any results for LI[3] yet.
241 (defun li2numer (y)
242 ;; Spence's function can be used to compute li[2] for 0 <= x <= 1.
243 ;; To compute the rest, we need the following identities:
245 ;; li[2](x) = -li[2](1/x)-log(-x)^2/2-%pi^2/6
246 ;; li[2](x) = li[2](1/(1-x)) + log(1-x)*log((1-x)/x^2)/2 - %pi^2/6
248 ;; The first tells us how to compute li[2] for x > 1. The result is complex.
249 ;; For x < 0, the second can be used, and the result is real.
251 ;; (See http://functions.wolfram.com/ZetaFunctionsandPolylogarithms/PolyLog2/17/01/01/)
252 (labels ((li2 (x)
253 (cond ((< x 0)
254 (+ (li2 (/ (- 1 x)))
255 (* 0.5 (log (- 1 x)) (log (/ (- 1 x) (* x x))))
256 (- (/ (cl:expt (float pi) 2) 6))))
257 ((< x 1)
258 (slatec:dspenc x))
259 ((= x 1)
260 (/ (cl:expt (float pi) 2) 6))
262 ;; li[2](x) = -li[2](1/x)-log(-x)^2/2-%pi^2/6
263 (- (+ (li2 (/ x))
264 (/ (cl:expt (cl:log (- x)) 2) 2)
265 (/ (cl:expt (float pi) 2) 6)))))))
266 (complexify (li2 y))))
269 (defvar *li2* (make-array 15. :initial-contents '(14.0 1.93506430 .166073033 2.48793229e-2
270 4.68636196e-3 1.0016275e-3 2.32002196e-4
271 5.68178227e-5 1.44963006e-5 3.81632946e-6
272 1.02990426e-6 2.83575385e-7 7.9387055e-8
273 2.2536705e-8 6.474338e-9)
274 :element-type 'flonum))
277 (defvar *li3* (make-array 15. :initial-contents '(14.0 1.95841721 8.51881315e-2 8.55985222e-3
278 1.21177214e-3 2.07227685e-4 3.99695869e-5
279 8.38064066e-6 1.86848945e-6 4.36660867e-7
280 1.05917334e-7 2.6478920e-8 6.787e-9
281 1.776536e-9 4.73417e-10)
282 :element-type 'flonum))
284 (defvar *s12* (make-array 18. :initial-contents '(17.0 1.90361778 .431311318 .100022507
285 2.44241560e-2 6.22512464e-3 1.64078831e-3
286 4.44079203e-4 1.22774942e-4 3.45398128e-5
287 9.85869565e-6 2.84856995e-6 8.31708473e-7
288 2.45039499e-7 7.2764962e-8 2.1758023e-8 6.546158e-9
289 1.980328e-9)
290 :element-type 'flonum))
292 (defun chebyli2 (x)
293 (* x (cheby-prime (/ (1+ (* x 4)) 3) *li2*)))
295 (defun chebyli3 (x)
296 (* x (cheby-prime (/ (1+ (* 4 x)) 3) *li3*)))
298 (defun chebys12 (x)
299 (* (/ (expt x 2) 4)
300 (cheby-prime (/ (1+ (* 4 x)) 3) *s12*)))
302 ;; subtitle polygamma routines
304 ;; gross efficiency hack, exp is a function of *k*, *k* should be mbind'ed
306 (defun msum (exp lo hi)
307 (if (< hi lo)
309 (let ((sum 0))
310 (do ((*k* lo (1+ *k*)))
311 ((> *k* hi) sum)
312 (declare (special *k*))
313 (setq sum (add2 sum (meval exp)))))))
316 (defun pole-err (exp)
317 (cond (errorsw (throw 'errorsw t))
318 (t (merror (intl:gettext "Pole encountered in: ~M") exp))))
321 (defprop $psi psisimp specsimp)
323 ;; Integral of psi function psi[n](x)
324 (putprop '$psi
325 `((n x)
327 ,(lambda (n x)
328 (cond
329 ((and ($integerp n) (>= n 0))
330 (cond
331 ((= n 0) `((%log_gamma) ,x))
332 (t `((mqapply) (($psi array) ((mplus) -1 ,n)) ,x))))
333 (t nil))))
334 'integral)
336 (defun psisimp (expr a z)
337 (let ((s (simpcheck (car (subfunsubs expr)) z)))
338 (subargcheck expr 1 1 '$psi)
339 (setq a (simpcheck (car (subfunargs expr)) z))
340 (and (setq z (integer-representation-p a))
341 (< z 1)
342 (pole-err expr))
343 (eqtest (psisimp1 s a) expr)))
345 ;; This gets pretty hairy now.
347 (defun psisimp1 (s a)
348 (let ((*k*))
349 (declare (special *k*))
351 (and (integerp s) (>= s 0) (mnumericalp a)
352 (let (($float2bf t)) ($float (mfuncall '$bfpsi s a 18))))
353 (and (integerp s) (>= s 0) ($bfloatp a)
354 (mfuncall '$bfpsi s a $fpprec))
355 (and (not $numer) (not $float) (integerp s) (> s -1)
356 (cond
357 ((integerp a)
358 (and (not (> a $maxpsiposint)) ; integer values
359 (m*t (expt -1 s) (factorial s)
360 (m- (msum (inv (m^t '*k* (1+ s))) 1 (1- a))
361 (cond ((zerop s) '$%gamma)
362 (($zeta (1+ s))))))))
363 ((or (not (ratnump a)) (ratgreaterp a $maxpsiposint)) ())
364 ((ratgreaterp a 0)
365 (cond
366 ((ratgreaterp a 1)
367 (let* ((int ($entier a)) ; reduction to fractional values
368 (frac (m-t a int)))
369 (m+t
370 (psisimp1 s frac)
371 (if (> int $maxpsiposint)
372 (subfunmakes '$psi (ncons s) (ncons int))
373 (m*t (expt -1 s) (factorial s)
374 (msum (m^t (m+t (m-t a int) '*k*)
375 (1- (- s)))
376 0 (1- int)))))))
377 ((= s 0)
378 (let ((p (cadr a)) (q (caddr a)))
379 (cond
380 ((or (> p $maxpsifracnum)
381 (> q $maxpsifracdenom) (bignump p) (bignump q)) ())
382 ((and (= p 1)
383 (cond ((= q 2)
384 (m+ (m* -2 '((%log) 2)) (m- '$%gamma)))
385 ((= q 3)
386 (m+ (m* '((rat simp) -1 2)
387 (m^t 3 '((rat simp) -1 2)) '$%pi)
388 (m* '((rat simp) -3 2) '((%log) 3))
389 (m- '$%gamma)))
390 ((= q 4)
391 (m+ (m* '((rat simp) -1 2) '$%pi)
392 (m* -3 '((%log) 2)) (m- '$%gamma)))
393 ((= q 6)
394 (m- (m+ (m* '((rat simp) 3 2) '((%log) 3))
395 (m* 2 '((%log) 2))
396 (m* '((rat simp) 1 2) '$%pi
397 (m^t 3 '((rat simp) 1 2)))
398 '$%gamma))))))
399 ((and (= p 2) (= q 3))
400 (m+ (m* '((rat simp) 1 2)
401 (m^t 3 '((rat simp) -1 2)) '$%pi)
402 (m* '((rat simp) -3 2) '((%log) 3))
403 (m- '$%gamma)))
404 ((and (= p 3) (= q 4))
405 (m+ (m* '((rat simp) 1 2) '$%pi)
406 (m* -3 '((%log) 2)) (m- '$%gamma)))
407 ((and (= p 5) (= q 6))
408 (m- (m* '((rat simp) 1 2) '$%pi
409 (m^t 3 '((rat simp) 1 2)))
410 (m+ (m* '((rat simp) 3 2) '((%log) 3))
411 (m* 2 '((%log) 2))
412 '$%gamma)))
413 ;; Gauss's Formula
414 ((let ((f (m* `((%cos) ,(m* 2 a '$%pi '*k*))
415 `((%log) ,(m-t 2 (m* 2 `((%cos)
416 ,(m//t (m* 2 '$%pi '*k*)
417 q))))))))
418 (m+t (msum f 1 (1- (truncate q 2)))
419 (let ((*k* (truncate q 2)))
420 (declare (special *k*))
421 (m*t (meval f)
422 (cond ((oddp q) 1)
423 ('((rat simp) 1 2)))))
424 (m-t (m+ (m* '$%pi '((rat simp) 1 2)
425 `((%cot) ((mtimes simp) ,a $%pi)))
426 `((%log) ,q)
427 '$%gamma))))))))
428 ((alike1 a '((rat) 1 2))
429 (m*t (expt -1 (1+ s)) (factorial s)
430 (1- (expt 2 (1+ s))) (simplify ($zeta (1+ s)))))
431 ((and (ratgreaterp a '((rat) 1 2))
432 (ratgreaterp 1 a))
433 (m*t
434 (expt -1 s)
435 (m+t (psisimp1 s (m- 1 a))
436 (let ((dif (m* '$%pi
437 ($diff `((%cot) ,(m* '$%pi '$z)) '$z s)))
438 ($z (m-t a)))
439 (declare (special $z))
440 (meval dif)))))))
441 ((ratgreaterp a $maxpsinegint) ;;; Reflection Formula
442 (m*t
443 (expt -1 s)
444 (m+t (m+t (psisimp1 s (m- a))
445 (let ((dif (m* '$%pi
446 ($diff `((%cot) ,(m* '$%pi '$z)) '$z s)))
447 ($z (m-t a)))
448 (declare (special $z))
449 (meval dif)))
450 (m*t (factorial s) (m^t (m-t a) (1- (- s)))))))))
451 (subfunmakes '$psi (ncons s) (ncons a)))))
454 ;; subtitle polygamma tayloring routines
456 ;; These routines are specially coded to be as fast as possible given the
457 ;; current $TAYLOR; too bad they have to be so ugly.
459 (declare-top (special var))
461 (defun expgam-fun (pw temp)
462 (setq temp (get-datum (get-key-var (car var))))
463 (let-pw temp pw
464 (pstimes
465 (let-pw temp (e1+ pw)
466 (psexpt-fn (getexp-fun '(($psi) -1) var (e1+ pw))))
467 (make-ps var (ncons pw) '(((-1 . 1) 1 . 1))))))
469 (defun expplygam-funs (pw subl l) ; l is a irrelevant here
470 (setq subl (car subl))
471 (if (or (not (integerp subl)) (< subl -1))
472 (tay-err "Unable to expand at a subscript in")
473 (prog ((e 0) (sf-sign 0) npw sf-last)
474 (declare (fixnum e) (fixnum sf-sign))
475 (setq npw (/ (float (car pw)) (float (cdr pw))))
476 (setq
477 l (cond ((= subl -1)
478 `(((1 . 1) . ,(prep1 '((mtimes) -1 $%gamma)))))
479 ((= subl 0)
480 (cons '((-1 . 1) -1 . 1)
481 (if (> 0.0 npw) ()
482 `(((0 . 1)
483 . ,(prep1 '((mtimes) -1 $%gamma)))))))
484 (t (setq sf-last (factorial subl))
485 `(((,(- (1+ subl)) . 1)
486 ,(* (expt -1 (1+ subl))
487 (factorial subl)) . 1))))
488 e (if (< subl 1) (- subl) -1)
489 sf-sign (if (< subl 1) -1 (expt -1 subl)))
490 a (setq e (1+ e) sf-sign (- sf-sign))
491 (if (> e npw) (return l)
492 (rplacd (last l)
493 `(((,e . 1)
494 . ,(rctimes (rcplygam e sf-sign subl sf-last)
495 (prep1 ($zeta (+ (1+ subl) e))))))))
496 (go a))))
498 (defun rcplygam (k sf-sign subl sf-last)
499 (declare (fixnum k) )
500 (cond ((= subl -1) (cons sf-sign k))
501 ((= subl 0) (cons sf-sign 1))
502 (t (prog1
503 (cons (* sf-sign sf-last) 1)
504 (setq sf-last
505 (quot (* sf-last (+ subl (1+ k)))
506 (1+ k)))))))
508 (defun plygam-ord (subl)
509 (if (equal (car subl) -1) (ncons (rcone))
510 `((,(m- (m1+ (car subl))) . 1))))
512 (defun plygam-pole (a c func)
513 (if (rcmintegerp c)
514 (let ((ps (get-lexp (m- a (rcdisrep c)) () t)))
515 (rplacd (cddr ps) (cons `((0 . 1) . ,c) (cdddr ps)))
516 (if (atom func) (gam-const a ps func)
517 (plygam-const a ps func)))
518 (prep1 (simplifya
519 (if (atom func) `((%gamma) ,(rcdisrep c))
520 `((mqapply) ,func ,(rcdisrep c)))
521 () ))))
523 (defun gam-const (a arg func)
524 (let ((const (ps-lc* arg)) (arg-c))
525 (cond ((not (rcintegerp const))
526 (taylor2 (diff-expand `((%gamma) ,a) tlist)))
528 (setq const (car const))
529 (if (pscoefp arg) (setq arg-c (get-lexp (m+t a (- const)) (rcone) (signp le const))))
530 (if (and arg-c (not (psp arg-c)))
531 (taylor2 (simplify `((%gamma) ,const)))
532 (let ((datum (get-datum (get-key-var (gvar (or arg-c arg)))))
533 (ord (if arg-c (le (terms arg-c)) (le (n-term (terms arg))))))
534 (setq func (current-trunc datum))
535 (if (> const 0)
536 (pstimes (let-pw datum (e- func ord) (expand (m+t a (- const)) '%gamma))
537 (let-pw datum (e+ func ord)
538 (tsprsum (m+t a (m-t '%%taylor-index%%))
539 `(%%taylor-index%% 1 ,const) '%product)))
540 (pstimes (expand (m+t a (- const)) '%gamma)
541 (let-pw datum (e+ func ord)
542 (psexpt (tsprsum (m+t a '%%taylor-index%%)
543 `(%%taylor-index%% 0 ,(- (1+ const))) '%product)
544 (rcmone)))))))))))
546 (defun plygam-const (a arg func)
547 (let ((const (ps-lc* arg)) (sub (cadr func)))
548 (cond
549 ((or (not (integerp sub)) (< sub -1))
550 (tay-err "Unable to expand at a subscript in"))
551 ((not (rcintegerp const))
552 (taylor2 (diff-expand `((mqapply) ,func ,a) tlist)))
553 (t (setq const (car const))
554 (psplus
555 (expand (m+t a (- const)) func)
556 (if (> const 0)
557 (pstimes
558 (cons (* (expt -1 sub) (factorial sub)) 1)
559 (tsprsum `((mexpt) ,(m+t a (m-t '%%taylor-index%%)) ,(- (1+ sub)))
560 `(%%taylor-index%% 1 ,const) '%sum))
561 (pstimes
562 (cons (* (expt -1 (1+ sub)) (factorial sub)) 1)
563 (tsprsum `((mexpt) ,(m+t a '%%taylor-index%%) ,(- (1+ sub)))
564 `(%%taylor-index%% 0 ,(- (1+ const))) '%sum))))))))
566 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
567 ;;; Lambert W function
568 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
570 ;; References
572 ;; Corless, R. M., Gonnet, D. E. G., Jeffrey, D. J., Knuth, D. E. (1996).
573 ;; "On the Lambert W function". Advances in Computational Mathematics 5:
574 ;; pp 329-359
576 ;; http://www.apmaths.uwo.ca/~djeffrey/Offprints/W-adv-cm.pdf.
577 ;; or http://www.apmaths.uwo.ca/~rcorless/frames/PAPERS/LambertW/
579 ;; D. J. Jeffrey, D. E. G. Hare, R. M. Corless
580 ;; Unwinding the branches of the Lambert W function
581 ;; The Mathematical Scientist, 21, pp 1-7, (1996)
582 ;; http://www.apmaths.uwo.ca/~djeffrey/Offprints/wbranch.pdf
584 ;; Winitzki, S. Uniform Approximations for Transcendental Functions.
585 ;; In Part 1 of Computational Science and its Applications - ICCSA 2003,
586 ;; Lecture Notes in Computer Science, Vol. 2667, Springer-Verlag,
587 ;; Berlin, 2003, 780-789. DOI 10.1007/3-540-44839-X_82
588 ;; http://homepages.physik.uni-muenchen.de/~Winitzki/papers/
590 ;; Darko Veberic,
591 ;; Having Fun with Lambert W(x) Function
592 ;; arXiv:1003.1628v1, March 2010, http://arxiv.org/abs/1003.1628
594 ;; See also http://en.wikipedia.org/wiki/Lambert's_W_function
596 (defmfun $lambert_w (z)
597 (ftake* '%lambert_w z))
599 ;;; Set properties to give full support to the parser and display
600 (defprop $lambert_w %lambert_w alias)
601 (defprop $lambert_w %lambert_w verb)
602 (defprop %lambert_w $lambert_w reversealias)
603 (defprop %lambert_w $lambert_w noun)
605 ;;; lambert_w is a simplifying function
606 (defprop %lambert_w simp-lambertw operators)
608 ;;; Derivative of lambert_w
609 (defprop %lambert_w
610 ((x)
611 ((mtimes)
612 ((mexpt) $%e ((mtimes ) -1 ((%lambert_w) x)))
613 ((mexpt) ((mplus) 1 ((%lambert_w) x)) -1)))
614 grad)
616 ;;; Integral of lambert_w
617 ;;; integrate(W(x),x) := x*(W(x)^2-W(x)+1)/W(x)
618 (defprop %lambert_w
619 ((x)
620 ((mtimes)
622 ((mplus)
623 ((mexpt) ((%lambert_w) x) 2)
624 ((mtimes) -1 ((%lambert_w) x))
626 ((mexpt) ((%lambert_w) x) -1)))
627 integral)
629 (defun simp-lambertw (x yy z)
630 (declare (ignore yy))
631 (oneargcheck x)
632 (setq x (simpcheck (cadr x) z))
633 (cond ((equal x 0) 0)
634 ((equal x 0.0) 0.0)
635 ((zerop1 x) ($bfloat 0)) ;bfloat case
636 ((alike1 x '$%e)
637 ;; W(%e) = 1
639 ((alike1 x '((mtimes simp) ((rat simp) -1 2) ((%log simp) 2)))
640 ;; W(-log(2)/2) = -log(2)
641 '((mtimes simp) -1 ((%log simp) 2)))
642 ((alike1 x '((mtimes simp) -1 ((mexpt simp) $%e -1)))
643 ;; W(-1/e) = -1
645 ((alike1 x '((mtimes) ((rat) -1 2) $%pi))
646 ;; W(-%pi/2) = %i*%pi/2
647 '((mtimes simp) ((rat simp) 1 2) $%i $%pi))
648 ;; numerical evaluation
649 ((complex-float-numerical-eval-p x)
650 ;; x may be an integer. eg "lambert_w(1),numer;"
651 (if (integerp x)
652 (to (bigfloat::lambert-w-k 0 (bigfloat:to ($float x))))
653 (to (bigfloat::lambert-w-k 0 (bigfloat:to x)))))
654 ((complex-bigfloat-numerical-eval-p x)
655 (to (bigfloat::lambert-w-k 0 (bigfloat:to x))))
656 (t (list '(%lambert_w simp) x))))
658 ;; An approximation of the k-branch of generalized Lambert W function
659 ;; k integer
660 ;; z real or complex lisp float
661 ;; Used as initial guess for Halley's iteration.
662 ;; When W(z) is real, ensure that guess is real.
663 (defun init-lambert-w-k (k z)
664 (let ( ; parameters for k = +/- 1 near branch point z=-1/%e
665 (branch-eps 0.2e0)
666 (branch-point (/ -1 %e-val))) ; branch point z=-1/%e
667 (cond
668 ; For principal branch k=0, use expression by Winitzki
669 ((= k 0) (init-lambert-w-0 z))
670 ; For k=1 branch, near branch point z=-1/%e with im(z) < 0
671 ((and (= k 1)
672 (< (imagpart z) 0)
673 (< (abs (- branch-point z)) branch-eps))
674 (bigfloat::lambert-branch-approx z))
675 ; For k=-1 branch, z real with -1/%e < z < 0
676 ; W(z) is real in this range
677 ((and (= k -1) (realp z) (> z branch-point) (< z 0))
678 (init-lambert-w-minus1 z))
679 ; For k=-1 branch, near branch point z=-1/%e with im(z) >= 0
680 ((and (= k -1)
681 (>= (imagpart z) 0)
682 (< (abs (- branch-point z)) branch-eps))
683 (bigfloat::lambert-branch-approx z))
684 ; Default to asymptotic expansion Corless et al (4.20)
685 ; W_k(z) = log(z) + 2.pi.i.k - log(log(z)+2.pi.i.k)
686 (t (let ((two-pi-i-k (complex 0.0e0 (* 2 pi k))))
687 (+ (log z)
688 two-pi-i-k
689 (* -1 (log (+ (log z) two-pi-i-k )))))))))
691 ;; Complex value of the principal branch of Lambert's W function in
692 ;; the entire complex plane with relative error less than 1%, given
693 ;; standard branch cuts for sqrt(z) and log(z).
694 ;; Winitzki (2003)
695 (defun init-lambert-w-0 (z)
696 (let ((a 2.344e0) (b 0.8842e0) (c 0.9294e0) (d 0.5106e0) (e -1.213e0)
697 (y (sqrt (+ (* 2 %e-val z ) 2)) ) ) ; y=sqrt(2*%e*z+2)
698 ; w = (2*log(1+B*y)-log(1+C*log(1+D*y))+E)/(1+1/(2*log(1+B*y)+2*A)
700 (+ (* 2 (log (+ 1 (* b y))))
701 (* -1 (log (+ 1 (* c (log (+ 1 (* d y)))))))
703 (+ 1
704 (/ 1 (+ (* 2 (log (+ 1 (* b y)))) (* 2 a)))))))
706 ;; Approximate k=-1 branch of Lambert's W function over -1/e < z < 0.
707 ;; W(z) is real, so we ensure the starting guess for Halley iteration
708 ;; is also real.
709 ;; Veberic (2010)
710 (defun init-lambert-w-minus1 (z)
711 (cond
712 ((not (realp z))
713 (merror "z not real in init-lambert-w-minus1"))
714 ((or (< z (/ -1 %e-val)) (plusp z))
715 (merror "z outside range of approximation in init-lambert-w-minus1"))
716 ;; In the region where W(z) is real
717 ;; -1/e < z < C, use power series about branch point -1/e ~ -0.36787
718 ;; C = -0.3 seems a reasonable crossover point
719 ((< z -0.3)
720 (bigfloat::lambert-branch-approx z))
721 ;; otherwise C <= z < 0, use iteration W(z) ~ ln(-z)-ln(-W(z))
722 ;; nine iterations are sufficient over -0.3 <= z < 0
723 (t (let* ((ln-z (log (- z))) (maxiter 9) (w ln-z))
724 (dotimes (k maxiter w)
725 (setq w (- ln-z (log (- w)))))))))
727 (in-package #:bigfloat)
729 ;; Approximate Lambert W(k,z) for k=1 and k=-1 near branch point z=-1/%e
730 ;; using power series in y=-sqrt(2*%e*z+2)
731 ;; for im(z) < 0, approximates k=1 branch
732 ;; for im(z) >= 0, approximates k=-1 branch
734 ;; Corless et al (1996) (4.22)
735 ;; Veberic (2010)
737 ;; z is a real or complex bigfloat:
738 (defun lambert-branch-approx (z)
739 (let ((y (- (sqrt (+ (* 2 (%e z) z ) 2)))) ; y=-sqrt(2*%e*z+2)
740 (b0 -1) (b1 1) (b2 -1/3) (b3 11/72))
741 (+ b0 (* y (+ b1 (* y (+ b2 (* b3 y))))))))
743 ;; Algorithm based in part on Corless et al (1996).
745 ;; It is Halley's iteration applied to w*exp(w).
748 ;; w[j] exp(w[j]) - z
749 ;; w[j+1] = w[j] - -------------------------------------------------
750 ;; (w[j]+2)(w[j] exp(w[j]) -z)
751 ;; exp(w[j])(w[j]+1) - ---------------------------
752 ;; 2 w[j] + 2
754 ;; The algorithm has cubic convergence. Once convergence begins, the
755 ;; number of digits correct at step k is roughly 3 times the number
756 ;; which were correct at step k-1.
758 ;; Convergence can stall using convergence test abs(w[j+1]-w[j]) < prec,
759 ;; as happens for generalized_lambert_w(-1,z) near branch point z = -1/%e
760 ;; Therefore also stop iterating if abs(w[j]*exp(w[j]) - z) << abs(z)
761 (defun lambert-w-k (k z &key (maxiter 50))
762 (let ((w (init-lambert-w-k k z)) we w1e delta (prec (* 4 (epsilon z))))
763 (dotimes (i maxiter (maxima::merror "lambert-w-k did not converge"))
764 (setq we (* w (exp w)))
765 (when (<= (abs (- z we)) (* 4 (epsilon z) (abs z))) (return))
766 (setq w1e (* (1+ w) (exp w)))
767 (setq delta (/ (- we z)
768 (- w1e (/ (* (+ w 2) (- we z)) (+ 2 (* 2 w))))))
769 (decf w delta)
770 (when (<= (abs (/ delta w)) prec) (return)))
771 ;; Check iteration converged to correct branch
772 (check-lambert-w-k k w z)
775 (defmethod init-lambert-w-k ((k integer) (z number))
776 (maxima::init-lambert-w-k k z))
778 (defmethod init-lambert-w-k ((k integer) (z bigfloat))
779 (bfloat-init-lambert-w-k k z))
781 (defmethod init-lambert-w-k ((k integer) (z complex-bigfloat))
782 (bfloat-init-lambert-w-k k z))
784 (defun bfloat-init-lambert-w-k (k z)
785 "Approximate generalized_lambert_w(k,z) for bigfloat: z as initial guess"
786 (let ((branch-point -0.36787944117144)) ; branch point -1/%e
787 (cond
788 ;; if k=-1, z very close to -1/%e and imag(z)>=0, use power series
789 ((and (= k -1)
790 (or (zerop (imagpart z))
791 (plusp (imagpart z)))
792 (< (abs (- z branch-point)) 1e-10))
793 (lambert-branch-approx z))
794 ;; if k=1, z very close to -1/%e and imag(z)<0, use power series
795 ((and (= k 1)
796 (minusp (imagpart z))
797 (< (abs (- z branch-point)) 1e-10))
798 (lambert-branch-approx z))
799 ;; Initialize using float value if z is representable as a float
800 ((< (abs z) 1.0e100)
801 (if (complexp z)
802 (bigfloat (lambert-w-k k (cl:complex (float (realpart z) 1.0)
803 (float (imagpart z) 1.0))))
804 (bigfloat (lambert-w-k k (float z 1.0)))))
805 ;; For large z, use Corless et al (4.20)
806 ;; W_k(z) ~ log(z) + 2.pi.i.k - log(log(z)+2.pi.i.k)
808 (let ((log-z (log z)))
809 (if (= k 0)
810 (- log-z (log log-z))
811 (let* ((i (make-instance 'complex-bigfloat :imag (intofp 1)))
812 (two-pi-i-k (* 2 (%pi z) i k)))
813 (- (+ log-z two-pi-i-k)
814 (log (+ log-z two-pi-i-k))))))))))
816 ;; Check Lambert W iteration converged to the correct branch
817 ;; W_k(z) + ln W_k(z) = ln z, for k = -1 and z in [-1/e,0)
818 ;; = ln z + 2 pi i k, otherwise
819 ;; See Jeffrey, Hare, Corless (1996), eq (12)
820 ;; k integer
821 ;; z, w bigfloat: numbers
822 (defun check-lambert-w-k (k w z)
823 (let ((tolerance 1.0e-6))
825 (cond
826 ;; k=-1 branch with z and w real.
827 ((and (= k -1) (realp z) (minusp z) (>= z (/ -1 (%e z))))
828 (if (and (realp w)
829 (<= w -1)
830 (< (abs (+ w (log w) (- (log z)))) tolerance))
832 nil))
834 ; i k = (W_k(z) + ln W_k(z) - ln(z)) / 2 pi
835 (let (ik)
836 (setq ik (/ (+ w (log w) (- (log z))) (* 2 (%pi z))))
837 (if (and (< (realpart ik) tolerance)
838 (< (abs (- k (imagpart ik))) tolerance))
840 nil))))
842 (maxima::merror "Lambert W iteration converged to wrong branch"))))
844 (in-package :maxima)
846 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
847 ;;; Generalized Lambert W function
848 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
851 ;;; Derivative of lambert_w
852 (defprop %generalized_lambert_w
853 ((k x)
855 ((mtimes)
856 ((mexpt) $%e ((mtimes ) -1 ((%generalized_lambert_w) k x)))
857 ((mexpt) ((mplus) 1 ((%generalized_lambert_w) k x)) -1)))
858 grad)
860 ;;; Integral of lambert_w
861 ;;; integrate(W(k,x),x) := x*(W(k,x)^2-W(k,x)+1)/W(k,x)
862 (defprop %generalized_lambert_w
863 ((k x)
865 ((mtimes)
867 ((mplus)
868 ((mexpt) ((%generalized_lambert_w) k x) 2)
869 ((mtimes) -1 ((%generalized_lambert_w) k x))
871 ((mexpt) ((%generalized_lambert_w) k x) -1)))
872 integral)
874 (def-simplifier generalized_lambert_w (k x)
875 (flet ((test-for-integer (arg)
876 ;; The ARG must be some kind of number acceptable to
877 ;; BIGFLOAT:TO. If ARG is numerically equal to an integer,
878 ;; return the integer value. Otherwise, return NIL.
879 (let* ((new-arg (bigfloat:to arg))
880 (arg-truncate (bigfloat:truncate new-arg)))
881 (when (bigfloat:= arg-truncate new-arg)
882 arg-truncate))))
883 (cond
884 ;; Numerical evaluation for real or complex x
885 ((complex-float-numerical-eval-p k x)
886 ;; x may be an integer. eg "generalized_lambert_w(0,1),numer;"
887 ;; Also, we can only evaluate this if k is equal to an integer.
888 (let ((k-int (test-for-integer k)))
889 (cond (k-int
890 (if (integerp x)
891 (to (bigfloat::lambert-w-k k-int (bigfloat:to ($float x))))
892 (to (bigfloat::lambert-w-k k-int (bigfloat:to x)))))
894 (give-up)))))
895 ;; Numerical evaluation for real or complex bigfloat x
896 ((complex-bigfloat-numerical-eval-p k x)
897 (let ((k-int (test-for-integer k)))
898 (cond (k-int
899 (to (bigfloat::lambert-w-k k-int (bigfloat:to x))))
901 (give-up)))))
903 (give-up)))))
905 (in-package "BIGFLOAT")
907 (defvar *debug-li-eval* nil)
909 (defun li-using-powers-of-log (n x)
910 ;; When x is on or near the unit circle the other
911 ;; approaches don't work. Use the expansion in powers of
912 ;; log(z) (from cephes cpolylog)
914 ;; li[s](z) = sum(Z(s-j)*(log(z))^j/j!, j = 0, inf)
916 ;; where Z(j) = zeta(j) for j != 1. For j = 1:
918 ;; Z(1) = -log(-log(z)) + sum(1/k, k, 1, s - 1)
921 ;; This is similar to
922 ;; http://functions.wolfram.com/10.08.06.0024.01, but that
923 ;; identity is clearly undefined if v is a positive
924 ;; integer because zeta(1) is undefined.
926 ;; Thus,
928 ;; li[3](z) = Z(3) + Z(2)*log(z) + Z(1)*log(z)^2/2!
929 ;; + Z(0)*log(z)^3/3! + sum(Z(-k)*log(z)^(k+4)/(k+4)!,k,1,inf);
931 ;; But Z(-k) = zeta(-k) is 0 if k is even. So
933 ;; li[3](z) = Z(3) + Z(2)*log(z) + Z(1)*log(z)^2/2!
934 ;; + Z(0)*log(z)^3/3! + sum(Z(-(2*k+1))*log(z)^(2*k+4)/(2*k+4)!,k,0,inf);
935 (flet ((zfun (j)
936 (cond ((= j 1)
937 (let ((sum (- (log (- (log x))))))
938 (+ sum
939 (loop for k from 1 below n
940 sum (/ k)))))
942 (to (maxima::$zeta (maxima::to (float j (realpart x)))))))))
943 (let* ((eps (epsilon x))
944 (logx (log x))
945 (sum 0))
946 (do* ((k 0 (1+ k))
947 (top 1 (* top logx))
948 (bot 1 (* bot k))
949 (term (* (/ top bot) (to (zfun (- n k))))
950 (* (/ top bot) (to (zfun (- n k))))))
951 ((and (> k 4)
952 (oddp (- n k)) ;; even terms are all zero
953 (<= (abs term) (* (abs sum) eps))))
954 ;;(format t "~3d: ~A / ~A = ~A~%" k top bot term)
955 (incf sum term))
956 sum)))
959 (defun li3numer (x)
960 ;; If |x| < series-threshold, the series is used.
961 (let ((series-threshold 0.8))
962 (cond ((zerop x)
963 0.0)
964 ((= x 1)
965 (maxima::$zeta (maxima::to (float 3 x))))
966 ((= x -1)
967 ;; li[3](-1) = -(1-2^(1-3))*li[3](1)
968 ;; = -3/4*zeta(3)
970 ;; From the formula
972 ;; li[s](-1) = (2^(1-s)-1)*zeta(s)
974 ;; (See http://functions.wolfram.com/10.08.03.0003.01)
975 (* -3/4 (to (maxima::$zeta (maxima::to (float 3 x))))))
976 ((> (abs x) 1)
977 ;; For z not in the interval (0, 1) and for integral n, we
978 ;; have the identity:
980 ;; li[n](z) = -log(-z)^n/n! + (-1)^(n-1)*li[n](1/z)
981 ;; + 2 * sum(li[2*r](-1)/(n-2*r)!*log(-z)^(n-2*r), r, 1, floor(n/2))
983 ;; (See http://functions.wolfram.com/ZetaFunctionsandPolylogarithms/PolyLog/17/02/01/01/0008/)
985 ;; In particular for n = 3:
987 ;; li[3](z) = li[3](1/z) - log(-z)/6*(log(-z)^2+%pi^2)
988 (let* ((lg (log (- x)))
989 (dpi (%pi x))
990 (result (- (li3numer (/ x))
991 (* (/ lg 6)
992 (+ (* lg lg) (* dpi dpi))))))
993 result))
994 ((> (abs x) series-threshold)
995 (let ((result (li-using-powers-of-log 3 x)))
996 ;; For real x, li-using-power-of-log can return a complex
997 ;; number with a tiny imaginary part. Get rid of that
998 ;; when x is real.
999 (if (realp x)
1000 (realpart result)
1001 result)))
1002 ;; Don't use the identity below because the identity seems
1003 ;; to be incorrect. For example, for x = -0.862 it returns
1004 ;; a complex value with an imaginary part that is not close
1005 ;; to zero as expected.
1006 #+nil
1007 ((> (abs x) series-threshold)
1008 ;; The series converges too slowly so use the identity:
1010 ;; li[3](-x/(1-x)) + li[3](1-x) + li[3](x)
1011 ;; = li[3](1) + %pi^2/6*log(1-x) - 1/2*log(x)*(log(1-x))^2 + 1/6*(log(1-x))^3
1013 ;; Or
1015 ;; li[3](x) = li[3](1) + %pi^2/6*log(1-x) - 1/2*log(x)*(log(1-x))^2 + 1/6*(log(1-x))^3
1016 ;; - li[3](-x/(1-x)) - li[3](1-x)
1018 ;; (See http://functions.wolfram.com/10.08.17.0048.01)
1019 (let* ((dpi (%pi x))
1020 (u (log x))
1021 (s (/ (* u u u) 6))
1022 (xc (- 1 x)))
1023 (decf s (* 0.5 u u (log xc)))
1024 (incf s (/ (* dpi dpi u) 6))
1025 (decf s (li3numer (- (/ xc x))))
1026 (decf s (li3numer xc))
1027 (incf s (li3numer (float 1 x)))))
1029 ;; Sum the power series. threshold determines when the
1030 ;; summation has converted.
1031 (let* ((threshold (epsilon x))
1032 (p (* x x x))
1033 (term (/ p 27)))
1034 (incf term (* 0.125 x x))
1035 (incf term x)
1036 (do* ((k 4 (1+ k))
1037 (p1 (* p x) (* p1 x))
1038 (h (/ p1 (* k k k)) (/ p1 (* k k k)))
1039 (s h (+ s h)))
1040 ((<= (abs (/ h s)) threshold)
1041 (+ s term))))))))
1043 (defun li2numer (z)
1044 ;; The series threshold to above sqrt(1/2) because li[2](%i) needs
1045 ;; the value of li[2](1/2-%i/2), and the magnitude of the argument
1046 ;; is sqrt(1/2) = 0.707. If the threshold is below this, we get
1047 ;; into an infinite recursion oscillating between the two args.
1048 (let ((series-threshold .75))
1049 (cond ((zerop z)
1051 ((= z 1)
1052 ;; %pi^2/6. This follows from the series.
1053 (/ (expt (%pi z) 2) 6))
1054 ((= z -1)
1055 ;; -%pi^2/12. From the formula
1057 ;; li[s](-1) = (2^(1-s)-1)*zeta(s)
1059 ;; (See http://functions.wolfram.com/10.08.03.0003.01)
1060 (/ (expt (%pi z) 2) -12))
1061 ((> (abs z) 1)
1062 ;; Use
1063 ;; li[2](z) = -li[2](1/z) - 1/2*log(-z)^2 - %pi^2/6,
1065 ;; valid for all z not in the intervale (0, 1).
1067 ;; (See http://functions.wolfram.com/10.08.17.0013.01)
1068 (- (+ (li2numer (/ z))
1069 (* 0.5 (expt (log (- z)) 2))
1070 (/ (expt (%pi z) 2) 6))))
1071 ;; this converges faster when close to unit circle
1072 ((> (abs z) series-threshold)
1073 (li-using-powers-of-log 2 z))
1074 ;; no longer in use
1075 ;;(> (abs z) series-threshold)
1076 ;; For 0.5 <= |z|, where the series would not converge very quickly, use
1078 ;; li[2](z) = li[2](1/(1-z)) + 1/2*log(1-z)^2 - log(-z)*log(1-z) - %pi^2/6
1080 ;; (See http://functions.wolfram.com/10.08.17.0016.01)
1081 ;;(let* ((1-z (- 1 z))
1082 ;; (ln (log 1-z)))
1083 ;; (+ (li2numer (/ 1-z))
1084 ;; (* 0.5 ln ln)
1085 ;; (- (* (log (- z))
1086 ;; ln))
1087 ;; (- (/ (expt (%pi z) 2) 6)))))
1089 ;; Series evaluation:
1091 ;; li[2](z) = sum(z^k/k^2, k, 1, inf);
1092 (let ((eps (epsilon z)))
1093 (do* ((k 0 (1+ k))
1094 (term z (* term (/ (* z k k)
1095 (expt (1+ k) 2))))
1096 (sum z (+ term sum)))
1097 ((<= (abs (/ term sum)) eps)
1098 sum)))))))
1100 (defun polylog-power-series (s z)
1101 ;; Series evaluation:
1103 ;; li[s](z) = sum(z^k/k^s, k, 1, inf);
1104 (let ((eps (epsilon z)))
1105 (do* ((k 1 (1+ k))
1106 (term z (* term z (expt (/ (- k 1) k) s)))
1107 (sum z (+ term sum)))
1108 ((<= (abs (/ term sum)) eps)
1109 ;; Return the value and the number of terms used, for
1110 ;; debugging and for helping in determining the series
1111 ;; threshold.
1112 (values sum k)))))
1114 (defun polylog-log-series (s z)
1115 ;; When x is on or near the unit circle the other
1116 ;; approaches don't work. Use the expansion in powers of
1117 ;; log(z) (from cephes cpolylog)
1119 ;; li[s](z) = sum(Z(s-j)*(log(z))^j/j!, j = 0, inf)
1121 ;; where Z(j) = zeta(j) for j != 1. For j = 1:
1123 ;; Z(1) = -log(-log(z)) + sum(1/k, k, 1, s - 1)
1124 (flet ((zfun (j)
1125 ;; Compute Z(j)
1126 (cond ((= j 1)
1127 (let ((sum (- (log (- (log z))))))
1128 (+ sum
1129 (loop for k from 1 below s
1130 sum (/ k)))))
1132 (to (maxima::$zeta (maxima::to (float j (realpart z)))))))))
1133 (let* ((eps (epsilon z))
1134 (logx (log z))
1135 (logx^2 (* logx logx))
1136 (top logx)
1137 (bot 1)
1138 (sum (zfun s)))
1139 ;; Compute sum(Z(s-j)*log(z)^j/j!, j = 1, s)
1140 (do* ((k 1 (1+ k))
1141 (zf (zfun (- s k)) (zfun (- s k)))
1142 (term (* (/ top bot) zf)
1143 (* (/ top bot) zf)))
1144 ((> k s))
1145 (when *debug-li-eval*
1146 (format t "~3d: ~A / ~A * ~A => ~A~%" k top bot zf term))
1147 (incf sum term)
1148 (setf bot (* bot (1+ k)))
1149 (setf top (* top logx)))
1151 (when *debug-li-eval*
1152 (format t "s = ~A, sum = ~S top, bot = ~S ~S~%"
1153 s sum top bot))
1154 ;; Compute the sum for j = s+1 and up. But since
1155 ;; zeta(-k) is 0 for k even, we only every other term.
1156 (do* ((k (+ s 1) (+ k 2))
1157 (zf (zfun (- s k)) (zfun (- s k)))
1158 (term (* (/ top bot) zf)
1159 (* (/ top bot) zf)))
1160 ((<= (abs term) (* (abs sum) eps))
1161 ;; Return the result and the number of terms used for
1162 ;; helping in determining the series threshold and the
1163 ;; log-series threshold.
1165 ;; Note that if z is real and less than 0, li[s](z) is
1166 ;; real. The series can return a tiny complex value in
1167 ;; this case, so we want to clear that out before
1168 ;; returning the answer.
1169 (values (if (and (realp z) (minusp z))
1170 (realpart sum)
1171 sum)
1173 (when *debug-li-eval*
1174 (format t "~3d: ~A / ~A = ~A~%" k top bot term))
1175 (incf sum term)
1176 (setf bot (* bot (+ k 1) (+ k 2)))
1177 (setf top (* top logx^2))))))
1179 (defun polylog-inversion-formula (s z)
1180 ;; For z not in the interval (0, 1) and for integral n, we
1181 ;; have the identity:
1183 ;; li[n](z) = -log(-z)^n/n! + (-1)^(n-1)*li[n](1/z)
1184 ;; + 2 * sum(li[2*r](-1)/(n-2*r)!*log(-z)^(n-2*r), r, 1, floor(n/2))
1186 ;; (See http://functions.wolfram.com/ZetaFunctionsandPolylogarithms/PolyLog/17/02/01/01/0008/)
1188 ;; Or
1190 ;; li[n](z) = -log(-z)^n/n! + (-1)^(n-1)*li[n](1/z)
1191 ;; + 2 * sum(li[2*m-2*r](-1)/(n-2*m+2*r)!*log(-z)^(n-2*m+2*r), r, 0, m - 1)
1193 ;; where m = floor(n/2). Thus, n-2*m = 0 if n is even and 1 if n is odd.
1195 ;; For n = 2*m, we have
1197 ;; li[2*m](z) = -log(-z)^(2*m)/(2*m)! - li[2*m](1/z)
1198 ;; + 2 * sum(li[2*r](-1)/(2*m-2*r)!*log(-z)^(2*m-2*r), r, 1, m)
1199 ;; = -log(-z)^(2*m)/(2*m)! - li[2*m](1/z)
1200 ;; + 2 * sum((li[2*m-2*r](-1)*log(-z)^(2*r+1))/(2*r+1)!,r,0,m-1);
1202 ;; For n = 2*m+1, we have
1204 ;; li[2*m+1](z) = -log(-z)^(2*m+1)/(2*m+1)! + li[2*m+1](1/z)
1205 ;; + 2 * sum(li[2*r](-1)/(2*m-2*r + 1)!*log(-z)^(2*m-2*r + 1), r, 1, m)
1206 ;; = -log(-z)^(2*m+1)/(2*m+1)! + li[2*m+1](1/z)
1207 ;; + 2 * sum((li[2*m-2*r](-1)*log(-z)^(2*r+1))/(2*r+1)!,r,0,m-1);
1208 ;; Thus,
1210 ;; li[n](z) = -log(-z)^n/n! + (-1)^(n-1)*li[n](1/z)
1211 ;; + 2 * sum((li[2*m-2*r](-1)*log(-z)^(2*r+1))/(2*r+1)!,r,0,floor(n/2)-1);
1212 (let* ((lgz (log (- z)))
1213 (lgz^2 (* lgz lgz))
1214 (half-s (floor s 2))
1215 (neg-1 (float -1 (realpart z)))
1216 (sum 0))
1217 (if (evenp s)
1218 (do* ((r 0 (1+ r))
1219 (top (if (oddp s) lgz 1) (* top lgz^2))
1220 (bot 1 (* bot (+ r r -1) (+ r r)))
1221 (term (* (li-s-simp (* 2 (- half-s r)) neg-1)
1222 (/ top bot))
1223 (* (li-s-simp (* 2 (- half-s r)) neg-1)
1224 (/ top bot))))
1225 ((>= r half-s))
1226 (incf sum term)
1227 (when *debug-li-eval*
1228 (format t "r = ~4d: ~A / ~A, ~A; ~A~%" r top bot term sum)))
1229 (do* ((r 0 (1+ r))
1230 (top (if (oddp s) lgz 1) (* top lgz^2))
1231 (bot 1 (* bot (+ r r) (+ r r 1)))
1232 (term (* (li-s-simp (* 2 (- half-s r)) neg-1)
1233 (/ top bot))
1234 (* (li-s-simp (* 2 (- half-s r)) neg-1)
1235 (/ top bot))))
1236 ((>= r half-s))
1237 (incf sum term)
1238 (when *debug-li-eval*
1239 (format t "r = ~4d: ~A / ~A, ~A; ~A~%" r top bot term sum))))
1240 (+ (+ sum sum)
1241 (- (/ (expt lgz s)
1242 (maxima::take '(maxima::mfactorial) s)))
1243 (* (expt -1 (- s 1))
1244 (li-s-simp s (/ z))))))
1246 (defun li-s-simp (s z)
1247 (let ((series-threshold 0.5)
1248 (log-series-threshold 2))
1249 (cond ((zerop z)
1250 (maxima::to (to 0.0)))
1251 ((= z 1)
1252 (maxima::$zeta (maxima::to (float s z))))
1253 ((= z -1)
1254 (- (* (- 1 (expt 2 (- 1 s)))
1255 (to (li-s-simp s (- z))))))
1256 ((<= (abs z) series-threshold)
1257 (values (polylog-power-series s z)))
1258 ((<= (abs z) log-series-threshold)
1259 (values (polylog-log-series s z)))
1260 ((> (abs z) 1.5)
1261 (polylog-inversion-formula s z)))))
1263 ;;; Computation of Catalan's constant
1264 (in-package #:bigfloat)
1266 ;; catalan = 1/2*sum(a[n], k, 0, inf)
1268 ;; a[n] = (-8)^k*(3*k+2)/((2*k+1)^3*binomial(2*k,k)^3)
1270 ;; This is the first of the other quickly converging series from
1271 ;; https://en.wikipedia.org/wiki/Catalan%27s_constant
1273 ;; There are other quickly converging series given in the Wikipedia
1274 ;; article, that might work better, but this one has relatively simple
1275 ;; form and is an alternating series so it's easy to know when to
1276 ;; stop.
1278 ;; This is an alternating series, so we can stop when the computed
1279 ;; term is below our desired accuracy.
1281 ;; The ratio between successive terms is
1283 ;; a[n+1]/a[n] = -(3*k+5)/(3*k+2)*((k+1)/(2*k+3))^3
1286 (defun comp-catalan (prec)
1287 (let* ((limit (expt 2 (- prec))))
1288 (do ((k 0 (+ k 1))
1289 (a (bigfloat 2)
1290 (* -1 a (* (/ (+ (* 3 k) 5)
1291 (+ (* 3 k) 2))
1292 (expt (/ (+ k 1)
1293 (+ (* 2 k) 3))
1294 3))))
1295 (sum (bigfloat 0)
1296 (+ sum a)))
1297 ((< (abs a) limit)
1298 (maxima::to (/ sum 2))))))