1 ;;; -*- Mode: Lisp; Package: Maxima; Syntax: Common-Lisp; Base: 10 -*- ;;;;
2 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3 ;;; The data in this file contains enhancements. ;;;;;
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 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
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
))
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
53 ((not (integerp s
)) ())
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
))
64 ;; li[s](-1) = (2^(1-s)-1)*zeta(s)
65 (mul (add -
1 (inv (expt 2 (- s
1))))
66 (take '(%zeta
) s
))))))
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
)))
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))))
85 (to (bigfloat::li-s-simp s
(bigfloat:to a
)))))))
86 (eqtest (subfunmakes '$li
(ncons s
) (ncons a
))
89 ;; Expand the Polylogarithm li[s](z) for a negative integer parameter s.
90 (defun lisimp-negative-integer (s z
)
92 (mul (inv (power (sub 1 z
) (+ n
1)))
93 (let ((index1 (gensumindex))
97 (let ((index2 (gensumindex)))
99 (mul (power -
1 (add index2
1))
100 (take '(%binomial
) (+ n
1) (sub index2
1))
101 (power (add 1 (sub index1 index2
)) n
))
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))))
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
132 (sub (div (power '$%pi
2)
134 (mul '$%pi
'$%i
(ftake '%log
2))))
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))))))
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
180 (declare (ignore subl
))
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
190 (setq npw
(/ (float (car pw
)) (float (cdr pw
))))
192 l
(cons '((0 .
1) 0 .
1)
195 (if (> e npw
) (return l
)
198 .
,(prep1 (m^ e
(m- subl
)))))))
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
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
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
)))
230 (prog1 bn
+1 (setq bn
+1 (+ (aref chebarr i
)
231 (- (* 2.0 x bn
+1) bn
+2))))))))
233 (defun cheby-prime (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.
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/)
255 (* 0.5 (log (- 1 x
)) (log (/ (- 1 x
) (* x x
))))
256 (- (/ (cl:expt
(float pi
) 2) 6))))
260 (/ (cl:expt
(float pi
) 2) 6))
262 ;; li[2](x) = -li[2](1/x)-log(-x)^2/2-%pi^2/6
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
290 :element-type
'flonum
))
293 (* x
(cheby-prime (/ (1+ (* x
4)) 3) *li2
*)))
296 (* x
(cheby-prime (/ (1+ (* 4 x
)) 3) *li3
*)))
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
)
310 (do ((*k
* lo
(1+ *k
*)))
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)
329 ((and ($integerp n
) (>= n
0))
331 ((= n
0) `((%log_gamma
) ,x
))
332 (t `((mqapply) (($psi array
) ((mplus) -
1 ,n
)) ,x
))))
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
))
343 (eqtest (psisimp1 s a
) expr
)))
345 ;; This gets pretty hairy now.
347 (defun psisimp1 (s a
)
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)
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
)) ())
367 (let* ((int ($entier a
)) ; reduction to fractional values
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
*)
378 (let ((p (cadr a
)) (q (caddr a
)))
380 ((or (> p $maxpsifracnum
)
381 (> q $maxpsifracdenom
) (bignump p
) (bignump q
)) ())
384 (m+ (m* -
2 '((%log
) 2)) (m- '$%gamma
)))
386 (m+ (m* '((rat simp
) -
1 2)
387 (m^t
3 '((rat simp
) -
1 2)) '$%pi
)
388 (m* '((rat simp
) -
3 2) '((%log
) 3))
391 (m+ (m* '((rat simp
) -
1 2) '$%pi
)
392 (m* -
3 '((%log
) 2)) (m- '$%gamma
)))
394 (m- (m+ (m* '((rat simp
) 3 2) '((%log
) 3))
396 (m* '((rat simp
) 1 2) '$%pi
397 (m^t
3 '((rat simp
) 1 2)))
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))
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))
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
*)
418 (m+t
(msum f
1 (1- (truncate q
2)))
419 (let ((*k
* (truncate q
2)))
420 (declare (special *k
*))
423 ('((rat simp
) 1 2)))))
424 (m-t (m+ (m* '$%pi
'((rat simp
) 1 2)
425 `((%cot
) ((mtimes simp
) ,a $%pi
)))
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))
435 (m+t
(psisimp1 s
(m- 1 a
))
437 ($diff
`((%cot
) ,(m* '$%pi
'$z
)) '$z s
)))
439 (declare (special $z
))
441 ((ratgreaterp a $maxpsinegint
) ;;; Reflection Formula
444 (m+t
(m+t
(psisimp1 s
(m- a
))
446 ($diff
`((%cot
) ,(m* '$%pi
'$z
)) '$z s
)))
448 (declare (special $z
))
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
))))
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
))))
478 `(((1 .
1) .
,(prep1 '((mtimes) -
1 $%gamma
)))))
480 (cons '((-1 .
1) -
1 .
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
)
494 .
,(rctimes (rcplygam e sf-sign subl sf-last
)
495 (prep1 ($zeta
(+ (1+ subl
) e
))))))))
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))
503 (cons (* sf-sign sf-last
) 1)
505 (quot (* sf-last
(+ subl
(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
)
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
)))
519 (if (atom func
) `((%gamma
) ,(rcdisrep c
))
520 `((mqapply) ,func
,(rcdisrep c
)))
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
))
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
)
546 (defun plygam-const (a arg func
)
547 (let ((const (ps-lc* arg
)) (sub (cadr func
)))
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
))
555 (expand (m+t a
(- const
)) func
)
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
))
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 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
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:
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/
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
612 ((mexpt) $%e
((mtimes ) -
1 ((%lambert_w
) x
)))
613 ((mexpt) ((mplus) 1 ((%lambert_w
) x
)) -
1)))
616 ;;; Integral of lambert_w
617 ;;; integrate(W(x),x) := x*(W(x)^2-W(x)+1)/W(x)
623 ((mexpt) ((%lambert_w
) x
) 2)
624 ((mtimes) -
1 ((%lambert_w
) x
))
626 ((mexpt) ((%lambert_w
) x
) -
1)))
629 (defun simp-lambertw (x yy z
)
630 (declare (ignore yy
))
632 (setq x
(simpcheck (cadr x
) z
))
633 (cond ((equal x
0) 0)
635 ((zerop1 x
) ($bfloat
0)) ;bfloat case
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)))
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;"
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
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
666 (branch-point (/ -
1 %e-val
))) ; branch point z=-1/%e
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
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
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
))))
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).
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
)))))))
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
710 (defun init-lambert-w-minus1 (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
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)
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) - ---------------------------
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
))))))
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
788 ;; if k=-1, z very close to -1/%e and imag(z)>=0, use power series
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
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
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
)))
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)
821 ;; z, w bigfloat: numbers
822 (defun check-lambert-w-k (k w z
)
823 (let ((tolerance 1.0e-6))
826 ;; k=-1 branch with z and w real.
827 ((and (= k -
1) (realp z
) (minusp z
) (>= z
(/ -
1 (%e z
))))
830 (< (abs (+ w
(log w
) (- (log z
)))) tolerance
))
834 ; i k = (W_k(z) + ln W_k(z) - ln(z)) / 2 pi
836 (setq ik
(/ (+ w
(log w
) (- (log z
))) (* 2 (%pi z
))))
837 (if (and (< (realpart ik
) tolerance
)
838 (< (abs (- k
(imagpart ik
))) tolerance
))
842 (maxima::merror
"Lambert W iteration converged to wrong branch"))))
846 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
847 ;;; Generalized Lambert W function
848 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
851 ;;; Derivative of lambert_w
852 (defprop %generalized_lambert_w
856 ((mexpt) $%e
((mtimes ) -
1 ((%generalized_lambert_w
) k x
)))
857 ((mexpt) ((mplus) 1 ((%generalized_lambert_w
) k x
)) -
1)))
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
868 ((mexpt) ((%generalized_lambert_w
) k x
) 2)
869 ((mtimes) -
1 ((%generalized_lambert_w
) k x
))
871 ((mexpt) ((%generalized_lambert_w
) k x
) -
1)))
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
)
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
)))
891 (to (bigfloat::lambert-w-k k-int
(bigfloat:to
($float x
))))
892 (to (bigfloat::lambert-w-k k-int
(bigfloat:to x
)))))
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
)))
899 (to (bigfloat::lambert-w-k k-int
(bigfloat:to x
))))
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.
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);
937 (let ((sum (- (log (- (log x
))))))
939 (loop for k from
1 below n
942 (to (maxima::$zeta
(maxima::to
(float j
(realpart x
)))))))))
943 (let* ((eps (epsilon x
))
949 (term (* (/ top bot
) (to (zfun (- n k
))))
950 (* (/ top bot
) (to (zfun (- n k
))))))
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)
960 ;; If |x| < series-threshold, the series is used.
961 (let ((series-threshold 0.8))
965 (maxima::$zeta
(maxima::to
(float 3 x
))))
967 ;; li[3](-1) = -(1-2^(1-3))*li[3](1)
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
))))))
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
)))
990 (result (- (li3numer (/ x
))
992 (+ (* lg lg
) (* dpi dpi
))))))
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
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.
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
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
))
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
))
1034 (incf term
(* 0.125 x x
))
1037 (p1 (* p x
) (* p1 x
))
1038 (h (/ p1
(* k k k
)) (/ p1
(* k k k
)))
1040 ((<= (abs (/ h s
)) threshold
)
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))
1052 ;; %pi^2/6. This follows from the series.
1053 (/ (expt (%pi z
) 2) 6))
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))
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
))
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))
1083 ;; (+ (li2numer (/ 1-z))
1085 ;; (- (* (log (- z))
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
)))
1094 (term z
(* term
(/ (* z k k
)
1096 (sum z
(+ term sum
)))
1097 ((<= (abs (/ term sum
)) eps
)
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
)))
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
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)
1127 (let ((sum (- (log (- (log z
))))))
1129 (loop for k from
1 below s
1132 (to (maxima::$zeta
(maxima::to
(float j
(realpart z
)))))))))
1133 (let* ((eps (epsilon z
))
1135 (logx^
2 (* logx logx
))
1139 ;; Compute sum(Z(s-j)*log(z)^j/j!, j = 1, s)
1141 (zf (zfun (- s k
)) (zfun (- s k
)))
1142 (term (* (/ top bot
) zf
)
1143 (* (/ top bot
) zf
)))
1145 (when *debug-li-eval
*
1146 (format t
"~3d: ~A / ~A * ~A => ~A~%" k top bot zf 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~%"
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
))
1173 (when *debug-li-eval
*
1174 (format t
"~3d: ~A / ~A = ~A~%" k top bot 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/)
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);
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
)))
1214 (half-s (floor s
2))
1215 (neg-1 (float -
1 (realpart z
)))
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
)
1223 (* (li-s-simp (* 2 (- half-s r
)) neg-1
)
1227 (when *debug-li-eval
*
1228 (format t
"r = ~4d: ~A / ~A, ~A; ~A~%" r top bot term sum
)))
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
)
1234 (* (li-s-simp (* 2 (- half-s r
)) neg-1
)
1238 (when *debug-li-eval
*
1239 (format t
"r = ~4d: ~A / ~A, ~A; ~A~%" r top bot term sum
))))
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))
1250 (maxima::to
(to 0.0)))
1252 (maxima::$zeta
(maxima::to
(float s z
))))
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
)))
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
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
))))
1290 (* -
1 a
(* (/ (+ (* 3 k
) 5)
1298 (maxima::to
(/ sum
2))))))