More documentation and comment out debugging print.
[maxima.git] / src / hypergeometric.lisp
blob09cef1bd94e68a3044fdf737ca7a975663bb3145
1 ;; Copyright 2009,2021 by Barton Willis
3 ;; This is free software; you can redistribute it and/or
4 ;; modify it under the terms of the GNU General Public License,
5 ;; http://www.gnu.org/copyleft/gpl.html.
7 ;; This software has NO WARRANTY, not even the implied warranty of
8 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10 (in-package :maxima)
12 ;; mea culpa---for numerical evaluation of the hypergeometric functions, the
13 ;; method uses a running error. When the error is too large, the value of fpprec
14 ;; is increased and the evaluation is redone with the larger value of fpprec.
15 ;; The option variable max_fpprec is the largest value for fpprec Maxima will try.
17 (defmvar $max_fpprec 1000)
19 (setf (get '$max_fpprec 'assign)
20 #'(lambda (unused b)
21 (declare (ignore unused))
22 (if (not (and (atom b) (integerp b)))
23 (progn
24 (mtell "The value of `max_fpprec' must be an integer.~%")
25 'munbindp))))
27 (defmvar $expand_hypergeometric nil)
29 (setf (get '$expand_hypergeometric 'assign)
30 #'(lambda (unused b)
31 (declare (ignore unused))
32 (if (not (or (eq b nil) (eq b t)))
33 (progn
34 (mtell "The value of `expand_hypergeometric' must be either true or false.~%")
35 'munbindp))))
37 ;; If the length of l is n, return true; otherwise signal wna-err = (wrong number of arguments, by the way).
39 (defun argument-length-check (l n)
40 (if (and (consp l) (consp (first l)) (equal n (length (margs l)))) t (wna-err (caar l))))
42 ;; When multiple_value_return is nil, multiple_values(e1,e2,...) --> e1; otherwise
43 ;; multiple_values(e1,e2,...) --> multiple_values(e1,e2,...).
45 (setf (get '$multiple_values 'operators) 'simp-multiple-values)
47 (defmvar $multiple_value_return nil)
49 (defun simp-multiple-values (e yy z)
50 (declare (ignore yy))
51 (if $multiple_value_return
52 `(($multiple_values simp) ,@(mapcar #'(lambda (s) (simpcheck s z)) (cdr e)))
53 (simpcheck (cadr e) z)))
55 ;; Detect undefined and polynomial cases.
57 (defun classify-hypergeometric (a b x)
58 (let ((ah nil) (bh nil))
60 ;; Let bh = the least member of b that is a negative integer. If there is
61 ;; no such member, set bh = nil.
63 (dolist (bk b)
64 (if (and (integerp bk) (<= bk 0) (or (eq bh nil) (< bk bh))) (setq bh bk)))
66 ;; Let ah = the greatest member of a that is a negative integer. If there is
67 ;; no such member, set ah = nil.
69 (dolist (ak a)
70 (if (and (integerp ak) (<= ak 0) (or (eq ah nil) (> ak ah))) (setq ah ak)))
72 ;; Undefined when either (1) ah is nil and bh is non-nil or (2) ah and bh are
73 ;; non-nil and ah >= bh, and each member of a and b are numbers. (We don't
74 ;; want hypergeometric([a],[-3],x) to be undefined, do we?). I suppose this
75 ;; function could look for declared integers...
77 (cond ((and (every '$numberp a)
78 (every '$numberp b)
79 (or (and (not ah) bh) (and ah bh (>= bh ah)))) 'undefined)
81 ((or ah (zerop1 ($ratdisrep x))
82 (and ($taylorp x) (eql 0 ($second ($first ($taylorinfo x))))
83 (integerp ($third ($first ($taylorinfo x))))))
84 'polynomial)
86 (t 'nonpolynomial))))
88 ;; The function simpcheck changes taylor polynomials to general form--that messes
89 ;; it harder to taylorize hypergeometrics (things like hypergeometric([5],[], taylor(x,x,0,3)) -->
90 ;; a taylor polynomial. So use tsimpcheck: if e is a taylor polynomial, simplify; otherwise, simpcheck.
92 (defun tsimpcheck (e z)
93 (if (or ($taylorp e) ($ratp e)) (simplifya e z) (simpcheck e z)))
95 ;; We don't want realpart and imagpart to think that hypergeometric functions are
96 ;; real valued. So declare hypergeometric to be complex.
98 (eval-when
99 (:load-toplevel :execute)
100 (let (($context '$global) (context '$global))
101 (meval '(($declare) %hypergeometric $complex))))
103 (setf (get '%hypergeometric 'conjugate-function) 'conjugate-hypergeometric)
105 ;; hypergeometric(a,b,x) is entire (commutes with conjugate) when length(a) < length(b) + 1. Also
106 ;; hypergeometric(a,b,x) is analytic inside the unit disk. Outside the unit disk, we need to be careful;
107 ;; for now, conjugate gives a nounform in this case. I suppose we could check for declared negative integer
108 ;; parameter in the list a...I'll wait for a user to request this feature :)
110 (defun conjugate-hypergeometric (l)
111 (let ((a (first l)) (b (second l)) (x (third l)))
112 (cond ((or (< ($length a) (+ 1 ($length b))) (eq t (mgrp 1 (take '(mabs) x))))
113 (take '(%hypergeometric) (take '($conjugate) a) (take '($conjugate) b) (take '($conjugate) x)))
115 (list (list '$conjugate 'simp) (take '(%hypergeometric) a b x))))))
117 (defun lenient-complex-p (e)
118 (and ($freeof '$infinity '$und '$ind '$inf '$minf '$false '$true t nil e) ;; what else?
119 (not (mbagp e))
120 (not ($featurep e '$nonscalar))
121 (not (mrelationp e))
122 (not ($member e $arrays))))
124 ;;(defprop $hypergeometric simp-hypergeometric operators)
126 ;; Do noncontroversial simplifications on the hypergeometric function. A user that
127 ;; wants additional simplifications can use $hypergeometric_simp. The simplifications are
129 ;; (a) hypergeometric([], [], x) --> exp(x),
131 ;; (b) hypergeometric([a], [], x) --> 1 / (1 - x)^a,
133 ;; (c) hypergeometric([a1,...], [b1, ...], 0) --> 1,
135 ;; (d) hypergeometric([-n,...], [b1, ...], x) --> polynomial.
137 ;; (d) sort and delete common parameters; for example
138 ;; hypergeometric([p,b,a],[c,b],x) --> hypergeometric([a,p],[c],x).
140 ;; (e) hypergeometric([0,a1, ... ], [b1, ...], x) --> 1.
142 ;; Why does this code do (take '(mlist) a) instead of (cons '(mlist) a)? Because
143 ;; (cons '(mlist) a) messes up tellsimp rules. Say tellsimp([a], a]). Then
144 ;; (take (mlist) a) --> a, but (cons '(mlist) a) --> ((mlist) a). And that's not correct.
146 (def-simplifier (hypergeometric :simpcheck :custom) (a b x)
147 (let ((l nil) (a-len) (b-len)
148 (hg-type nil) (dig) (return-type) ($domain '$complex))
150 (unless (and($listp a) ($listp b))
151 (mtell "warning: The first two arguments to 'hypergeometric' must be lists.~%")
152 (return-from simp-%hypergeometric (give-up)))
154 (setq a (mapcar #'(lambda (s) (tsimpcheck s %%simpflag)) (margs a))
155 b (mapcar #'(lambda (s) (tsimpcheck s %%simpflag)) (margs b))
156 x (tsimpcheck x %%simpflag))
158 ;; Delete common members of a and b. This code is taken from hyp.lisp.
160 (setq l (zl-intersection a b))
161 (setq a (del l a)
162 b (del l b))
164 ;; Check for undefined cases
165 (setq hg-type (classify-hypergeometric a b x))
167 (setq a-len (length a))
168 (setq b-len (length b))
170 ;; Sort a and b and reconvert to Maxima lists.
172 (setq a (sort a '$orderlessp))
173 (setq b (sort b '$orderlessp))
174 (setq a (simplify (cons '(mlist) a)))
175 (setq b (simplify (cons '(mlist) b)))
177 ;; If constantp(x), apply rectform to x. For now, multiplication and division
178 ;; of complex numbers doesn't always return a number in rectangular form. Let's
179 ;; apply rectform to constants.
181 (when ($constantp x)
182 (setq x ($rectform x)))
183 (cond
185 ;; Catch undefined cases and return a nounform.
186 ((or (eq hg-type 'undefined)
187 (member-if #'(lambda(s) (not (lenient-complex-p s))) (cdr a))
188 (member-if #'(lambda(s) (not (lenient-complex-p s))) (cdr b))
189 (not (lenient-complex-p x)))
190 (give-up))
192 ;; pFq([a1,...,ap], [b1,...,bq], 0) --> 1 + 0
193 ((zerop1 x)
194 (add 1 x))
196 ;; pFq([0,a1,...,ap], [b1,...,bq], x) --> 1
197 ((member-if 'zerop1 (margs a))
200 ;; Do hypergeometric([],[],x) --> exp(x). All numerical evaluation is funneled through
201 ;; the same entry point; the function hypergeometric-0f0 doesn't do numerical evaluation.
202 ((and (= 0 a-len) (= 0 b-len)
203 (hypergeometric-0f0 x)))
205 ;; Do hypergeometric([a],[],x) --> 1 / (1-x)^a.
206 ((and (= a-len 1) (= 0 b-len)
207 (hypergeometric-1f0 (second a) x)))
209 ;; Try reflection identity for 1F1.
210 ((and (= a-len 1) (= b-len 1)
211 (hypergeometric-1f1 (second a) (second b) x hg-type)))
213 ;; For 2F1, value at 1--nothing else.
214 ((and (= a-len 2) (= b-len 1)
215 (hypergeometric-2f1 (second a) (third a) (second b) x)))
217 ;; Try numerical evaluation; return nil on failure. This should handle IEEE float,
218 ;; IEEE complex float, bigfloat, and complex big float cases.
219 ((and (setq return-type (use-float-hypergeometric-numerical-eval (margs a) (margs b) x))
220 (setq dig (ceiling (* (if (eq return-type 'float) (float-digits 1.0) fpprec)
221 #.(/ (log 2) (log 10)))))
222 (hypergeometric-float-eval (margs a) (margs b) x dig return-type)))
224 ;; Try rational number numerical evaluation; return nil on failure. This should handle
225 ;; rational and complex rational numerical evaluation.
226 ((use-rational-hypergeometric-numerical-eval (margs a) (margs b) x)
227 (rational-hypergeometric-numerical-eval (margs a) (margs b) x))
229 ;; Handle all other polynomial cases; this includes the case that
230 ;; x is a Taylor polynomial centered at zero.
231 ((hypergeometric-poly-case (margs a) (margs b) x))
233 ;; Return a nounform.
235 (give-up)))))
237 ;; When x isn't a float, do 0F0([],[],x) --> exp(x).
238 (defun hypergeometric-0f0 (x)
239 (if (use-float-hypergeometric-numerical-eval nil nil x) nil (take '(mexpt) '$%e x)))
241 ;; When a or x aren't floats, do 1F0([a],[],x) --> 1/(1-x)^a.
242 (defun hypergeometric-1f0 (a x)
243 (cond ((use-float-hypergeometric-numerical-eval (list a) nil x) nil)
244 ((onep x)
245 (if (eq t (mgrp 0 a)) 0 nil))
246 (t (div 1 (take '(mexpt) (sub 1 x) a)))))
248 ;; Apply the Kummer reflection identity when b-a is a negative integer and we know that
249 ;; the hypergeometric function is not already known to be a polynomial (that is a is not a
250 ;; negative integer) or when (great (neg x) x); otherwise, return nil. This function
251 ;; doesn't do floating point evaluation.
253 (defun hypergeometric-1f1 (a b x hg-type)
254 (cond ((use-float-hypergeometric-numerical-eval (list a) (list b) x) nil)
255 ((or (and (not (eq hg-type 'polynomial)) (great (neg x) x))
256 (and (not (eq hg-type 'polynomial)) (integerp (sub b a)) (< (sub b a) 0)))
257 (mul (take '(mexpt) '$%e x)
258 (take '(%hypergeometric) (take '(mlist) (sub b a)) (take '(mlist) b) (neg x))))
259 (t nil)))
261 ;; Convert x to a float; if float fails (say overflow), convert to a
262 ;; big float.
263 (defun safe-float (x)
264 (handler-case
265 ($float x)
266 (error ()
267 ($bfloat x))))
269 ;; Coerce x to the number type of z. The Maxima function safe_float returns a bigfloat when
270 ;; conversion to a float fails (overflow, for example).
271 (defun number-coerce (x z)
272 (cond ((complex-number-p z '$bfloatp)
273 ($bfloat x))
274 ((complex-number-p z 'floatp)
275 (safe-float x))
276 (t x)))
278 ;; 2F1(a,b;c, x) --> gamma(c) gamma(c - a - b) / (gamma(c-a) gamma (c-b))
279 ;; (Chu-Vandermonde identity, A & S 15.1.20) provided real_part(c-a-b) > 0 and c # 0,-1,-2, ...
280 ;; The c = 0, -1, -2, ... case should be caught previously. If we wanted to be super careful, we'd
281 ;; demand explicitly that c isn't a negative integer.
283 (defun hypergeometric-2f1 (a b c x)
284 (let ((z))
285 (setq z (sub c (add a b)))
286 (cond ((and (onep1 x) (complex-number-p z '$numberp) (eq t (mgrp ($realpart z) 0)))
287 (number-coerce
288 (div
289 (mul (take '(%gamma) c) (take '(%gamma) z))
290 (mul (take '(%gamma) (sub c a)) (take '(%gamma) (sub c b))))
292 (t nil))))
296 ;; For numerical evaluation of a general hypergeometric function, there aren't many
297 ;; alternatives to power series summation.
299 ;; Pursuant to well-established Maxima coding practices :), bigfloat
300 ;; functions receive bigfloat arguments and return bigfloat values.
302 (in-package #:bigfloat)
304 ;(import 'maxima::while) ;; <--- broken Why?
306 (defmacro while (cond &rest body)
307 `(do ()
308 ((not ,cond))
309 ,@body))
311 (defun 0f0-numeric (x)
312 (exp x))
314 (defun 1f0-numeric (a x)
315 (/ 1 (expt (- 1 x) a)))
317 ;; This is DLMF: http://dlmf.nist.gov/15.15#E1 with zo = 1/2. Also here is Maxima code that
318 ;; sums the first n+1 terms of the sum. The CL function 2f1-numeric-alt uses a running
319 ;; error and it sums until three consecutive partial sums have a modified relative difference
320 ;; that is bounded by the machine epsilon.
323 ff(a,b,c,x,n) := block([f, f0 : 1, f1 : 1- 2 * b / c,s : 1,k : 1, cf : a / (1-2/x), z],
324 b : c - 2 * b,
325 z : 1 - 2 / x,
326 while k <= n do (
327 s : s + cf * f1,
328 cf : cf * (a + k) / ((k + 1) * z),
329 f : (k * f0 + b * f1)/(k+c),
330 f0 : f1,
331 f1 : f,
332 k : k + 1),
333 s / (1-x/2)^a)$
336 (defun 2f1-numeric-alt (a b c x)
337 (let ((f) (f0 1) (f1 (- 1 (/ (* 2 b) c))) (s 1) (ds 1) (k 1) (cf (/ a (- 1 (/ 2 x)))) (z) (se 0)
338 (eps (epsilon x)) (done 0))
339 (setq b (- c (* 2 b)))
340 (setq z (- 1 (/ 2 x)))
341 (while (< done 3)
342 (setq ds (* cf f1))
343 (setq s (+ s ds))
344 (setq done (if (< (abs ds) (* eps (max 1 (abs s)))) (+ done 1) 0))
345 (setq se (+ se (abs s) (abs ds)))
346 (setq cf (/ (* cf (+ a k)) (* (+ 1 k) z)))
347 (setq f (/ (+ (* k f0) (* b f1)) (+ k c)))
348 (setq f0 f1)
349 (setq f1 f)
350 (setq k (+ k 1)))
351 (values (/ s (expt (- 1 (/ x 2)) a)) (* se (epsilon x)))))
353 ;; hypergeometric([ma,mb],[mc],mx); prefix m means Maxima expression.
355 (defun 2f1-numeric (ma mb mc mx digits)
356 (let* ((region) (f) (ff) (er) (local-fpprec digits) (eps) (mma) (mmb) (mmc) (mmx)
357 (x (bigfloat::to mx))
358 (d (list (list "none" (abs x)) ;; region I, inside unit disk
359 (list "15.3.4" (if (= x 1) nil (abs (/ x (- x 1)))))
360 (list "15.3.6" (abs (- 1 x)))
361 (list "15.3.7" (if (zerop x) nil (abs (/ 1 x))))
362 (list "15.3.8" (if (= x 1) nil (abs (/ 1 (- 1 x)))))
363 (list "15.3.9" (if (zerop x) nil (abs (- 1 (/ 1 x))))))))
365 (setq d (delete-if #'(lambda(s) (null (second s))) d))
366 ;; Sort d from least to greatest magnitude.
367 ;;(print `(d = ,d))
368 (setq d (sort d #'(lambda (a b) (< (second a) (second b)))))
369 (setq region (first (first d)))
370 ;;(print `(region = ,region))
371 ;;(print `(d = ,(second (first d))))
373 (cond
374 ;; When x = 0, return 1.
375 ((zerop x) 1)
377 ;; Use the alternative numerical method when |x| > 0.9; this happens when x is near exp(+/- %i %pi / 3).
379 ((> (second (first d)) 0.9)
380 (setq eps (epsilon (bigfloat::to mx)))
381 (setq er 1)
382 (setq f 1)
384 (while (> (abs er) (* eps (max (abs f) 1)))
385 (maxima::bind-fpprec local-fpprec
386 (setq mma (maxima::nfloat ma `((maxima::mlist)) local-fpprec maxima::$max_fpprec))
387 (setq mmb (maxima::nfloat mb `((maxima::mlist)) local-fpprec maxima::$max_fpprec))
388 (setq mmc (maxima::nfloat mc `((maxima::mlist)) local-fpprec maxima::$max_fpprec))
389 (setq mmx (maxima::nfloat mx `((maxima::mlist)) local-fpprec maxima::$max_fpprec))
390 (multiple-value-setq (f er)
391 (2f1-numeric-alt
392 (bigfloat::to mma) (bigfloat::to mmb) (bigfloat:to mmc) (bigfloat::to mmx)))
393 (setq local-fpprec (* 2 local-fpprec))))
395 (values f er))
396 ;; ma or mb negative integers--that causes trouble for most of the A&S 15.3.4--15.3.9
397 ;; transformations--let's quickly dispatch hypergeometric-float-eval; also dispatch
398 ;; hypergeometric-float-eval when the transformation is "none" (with adjust-parameters
399 ;; is false!
401 ((or (equal region "none") (and (integerp ma) (<= ma 0)) (and (integerp mb) (<= mb 0))
402 (< (abs x) 0.5))
403 (hypergeometric-float-eval (list ma mb) (list mc) mx digits nil))
405 ;; The case of a,b, and c integers causes trouble; let's dispatch hgfred on it.
406 ((and (integerp ma) (integerp mb) (integerp mc))
407 (setq f (maxima::$hgfred (maxima::take '(maxima::mlist) ma mb)
408 (maxima::take '(maxima::mlist) mc) 'maxima::z))
409 (setq f (maxima::$horner f 'maxima::z))
410 (let ((d))
411 (multiple-value-setq (f d)
412 (maxima::nfloat f `((maxima::mlist) ((maxima::mequal) maxima::z ,mx)) digits maxima::$max_fpprec))
413 (values (bigfloat f) (bigfloat d))))
416 (let ((maxima::$multiple_value_return t))
417 ;; It's really important to use the verb for here because
418 ;; that's what abramowitz_id is looking for.
419 (setq ff `((maxima::%hypergeometric maxima::simp)
420 ((maxima::mlist maxima::simp) ,ma ,mb)
421 ((maxima::mlist maxima::simp) ,mc) maxima::z))
422 #+nil
423 (progn
424 (format t "ff = ~A~%" ff)
425 (format t "d = ~A~%" d)
426 (format t "region = ~A~%" region))
427 (setq f nil)
428 (while d
429 (setq f (if (equal region "none")
430 `((maxima::multiple_values) ,ff t)
431 (maxima::mfuncall 'maxima::$abramowitz_id ff region)))
432 #+nil
433 (format t "f = ~A~%" f)
434 (if (maxima::$second f)
435 (setq d nil f (maxima::$first f)) (setq region (first (pop d)))))
437 #+nil
438 (progn
439 (maxima::displa f)
440 (maxima::displa `((maxima::mequal) maxima::z ,mx)))
441 (setq f (multiple-value-list
442 (maxima::nfloat f `((maxima::mlist) ((maxima::mequal) maxima::z ,mx))
443 digits maxima::$max_fpprec)))
444 #+nil
445 (progn
446 (format t "f = ~A~%" f)
447 (format t "first f = ~A~%" (first f))
448 (format t "sec f = ~A~%" (second f)))
449 (values (bigfloat::to (first f)) (bigfloat::to (second f))))))))
451 ;; Let a = (a1, a2, ..., am) and b = (b1, b2, ..., bn). Approximate sum(c(k) x^k / k!,k,1,inf),
452 ;; where c(k + 1) / c(k) = (a1 + k) (a2 + k) ... (am + k) / (b1 + k) (b2 + k) ... (bn + k).
454 (defun hypergeometric-by-series (a b x)
455 ;; es = running error for e and ez running error for z.
457 (let ((s 0) (s0 1) (k 0) (z 1) (es 0) (ez 1) (n) (p) (q) (stop 20000) (dig))
458 (setq n (* 2 (+ (length a) (length b) 1)))
459 (while (and (< k stop) (/= s s0)) ;; (not (= s s0)))
460 (setq s s0)
461 (setq p (reduce #'* (mapcar #'(lambda (s) (+ s k)) a))) ;; p adds and p-1 multiplications
462 (setq q (reduce #'* (mapcar #'(lambda (s) (+ s k)) b))) ;; q adds and q-1 multiplications
463 (incf k)
464 (setq z (* z (/ (* p x) (* q k))))
465 ;;(setq ez (+ (* n (abs z)) ez))
466 (setq ez (+ (* (abs (/ (* x p) (* q k))) ez) (* (abs z) n)))
467 (setq s0 (+ s z))
468 (setq es (+ es ez (abs s0))))
470 ;;(print `(k = ,k))
471 (if (>= k stop) (values nil nil)
472 (progn
473 ;; estimate number of correct digits:
475 (setq dig (floor
477 (- (log (max (abs s) (epsilon x))) (log (* es (epsilon x))))
478 #.(/ (log 2) (log 10)))))
480 ;;(print "-----------")
481 ;;(maxima::displa `((maxima::mequal) k ,k))
482 ;;(maxima::displa `((maxima::mequal) xxx ,(maxima::to (epsilon x))))
483 ;;(maxima::displa `((maxima::mequal) es ,(maxima::$float (maxima::to es))))
484 ;;(maxima::displa `((maxima::mequal) s ,(maxima::$float (maxima::to s))))
485 ;;(maxima::displa `((maxima::mequal) dig ,(maxima::$float (maxima::to dig))))
486 (values s dig)))))
488 (defun hypergeometric-poly-case (a b x)
489 (let ((z 1) (s 1) (k 0) (p) (q))
490 (while (not (zerop z))
491 (setq p (reduce #'* (mapcar #'(lambda (s) (+ s k)) a)))
492 (setq q (reduce #'* (mapcar #'(lambda (s) (+ s k)) b)))
493 (incf k)
494 (setq z (/ (* p x z) (* q k)))
495 (setq s (+ z s)))
498 ;; This function numerically evaluates pFq([a1,...,ap], [b1,....bq], x), where all the arguments
499 ;; are Maxima expressions, not bigfloat objects.
501 (defun hypergeometric-float-eval (ma mb mx digits &optional (adjust-params t))
502 (let ((a-len (length ma)) (b-len (length mb)) (f nil) (local-fpprec maxima::$fpprec) (d) (a) (b) (x))
504 ;(maxima::displa `((maxima::mlist) ,@ma))
505 ;(maxima::displa `((maxima::mlist) ,@mb))
506 ;(maxima::displa `((maxima::mlist) ,mx))
507 (setq a (mapcar #'bigfloat::to ma))
508 (setq b (mapcar #'bigfloat::to mb))
509 (setq x (bigfloat::to mx))
511 ;; Special case 0f0, 1f0, 2f1 for |x| > 1, and pfq for |x| > 1 and p >= q + 1.
512 ;; For a general hypergeometric, I don't know how to analytically continue, so in the last case,
513 ;; return false.
515 ;; In the general case, sum the hypergeometric series using a running error, recursing
516 ;; on local-fpprec; bailout when local-fpprec exceeds 1000.
518 (cond ((and (eql a-len 0) (eql b-len 0)) ;; special case 0f0
519 (values (0f0-numeric x) digits))
521 ((and (eql a-len 1) (eql b-len 0)) ;; special case 1f0
522 (values (1f0-numeric (first a) x) digits))
524 ((and (eql a-len 1) (integerp (first a)) (< (first a) 0) (eql b-len 1)) ;; special case 1f1
525 (maxima::bind-fpprec local-fpprec
526 (multiple-value-setq (f d) (1f1-downward-recursion (first a) (first b) x)))
527 (values f d))
529 ;; Optionally do Kummer transformation--when is the Kummer transformation advantageous?
530 ;; I think the sum is ill-conditioned when realpart(x) < 0. Since x is a float, realpart
531 ;; should be quick.
533 ;; The adjust-params argument should prevent an infinite loop (transform --> untransform ...)
534 ;; In this case, an infinite loop shouldn't happen even without the adjust-param scheme.
537 ((and adjust-params
538 (eql a-len 1)
539 (eql b-len 1)
540 (< (realpart x) 0))
541 (let ((f) (d))
542 (multiple-value-setq (f d) (hypergeometric-float-eval
543 (list (maxima::sub (car mb) (car ma)))
544 mb (maxima::neg mx) digits nil))
545 (values (* (exp x) f) d)))
547 ;; analytic continuation for 2f1;
548 ((and (eql a-len 2) (eql b-len 1) adjust-params)
549 (2f1-numeric (car ma) (cadr ma) (car mb) mx digits))
551 ((or (< a-len (+ b-len 1)) (in-unit-circle-p x) (eq 'maxima::polynomial
552 (maxima::classify-hypergeometric ma mb mx)))
554 ;; recurse on local-fpprec; bailout when local-fpprec exceeds $max_fpprec.
556 (while (and (or (null f) (< d digits)) (< local-fpprec maxima::$max_fpprec))
557 (maxima::bind-fpprec local-fpprec
558 (multiple-value-setq (f d) (hypergeometric-by-series a b x))
559 (setq a (mapcar #'(lambda (s) (bigfloat::to (maxima::$bfloat s))) ma))
560 (setq b (mapcar #'(lambda (s) (bigfloat::to (maxima::$bfloat s))) mb))
561 (setq x (bigfloat::to (maxima::$bfloat mx)))
562 ;(print "----------")
563 ;(print `(fpprec = ,local-fpprec))
564 ;(print `(d = ,d))
565 ;(print `(digits = ,digits))
566 ;(incf local-fpprec (+ (- digits d) 10))))
567 (setq local-fpprec (* 2 local-fpprec))))
569 (if (>= local-fpprec maxima::$max_fpprec)
570 (progn
571 (maxima::mtell "Exceeded maximum allowed fpprec.~%")
572 (values nil nil))
573 (values f d))))))
575 (defun in-unit-circle-p (x)
576 (< (abs x) 1))
578 ;; Compute f11(a,b,x) using downward recursion (A&S 13.4.1). The first argument must be a negative integer:
580 ;; f <-- (k * fo + (2 * k + x) * fm1)/(b-k)
583 ;; I think this is faster than the power series summation--it might be useful for orthogonal polynomials.
584 (defun 1f1-downward-recursion (a b x)
585 (let ((fo 1) (fm1 (- 1 (/ x b))) (f) (k -1) (efo 0) (efm1 0) (ef 0))
586 (declare (type fixnum k))
587 (setq k -1)
588 (cond ((eql a 0) (values fo 0))
589 ((eql a -1) (values fm1 0))
591 (setq x (- x b))
592 (while (>= k a)
593 (setq f (/ (- (* k fo) (* (+ (* 2 k) x) fm1)) (- b k)))
594 (setq ef
596 (/ (+ (* k efo)
597 (* (abs (+ (* 2 k) x))) (+ efm1 (* 2 fm1))
598 (* k fo))
599 (abs (- b k)))
600 (* 3 (abs f))))
602 (setq fo fm1)
603 (setq efo efm1)
604 (setq fm1 f)
605 (setq efm1 ef)
606 (decf k))
607 (values fo efo)))))
609 (in-package :maxima)
611 (defun float-or-bigfloat-p (x)
612 (or (floatp x) ($bfloatp x)))
614 ;; Return true iff it is possible to evaluate hypergeometric(a,b,x) using (exact)
615 ;; rational arithmetic. Thus (1) x and every member of a and b (Common Lisp lists) must be
616 ;; a $ratnump and (2) some member of a must be an explicit negative integer. When $numer
617 ;; is true, never do exact rational evaluation? (Likely when $numer is true, we'll never
618 ;; get here anyway?)
620 (defun use-rational-hypergeometric-numerical-eval (a b x)
621 (and (not $numer)
622 (complex-number-p x '$ratnump)
623 (every #'(lambda (s) (complex-number-p s '$ratnump)) a)
624 (every #'(lambda (s) (complex-number-p s '$ratnump)) b)
625 (some #'(lambda (s) (and (integerp s) (< s 0))) a)))
627 ;; Evaluate hypergeometric(a,b,x) using (exact) rational arithmetic. Here a
628 ;; and b are Common Lisp lists. Don't call this function without first
629 ;; checking that use-rational-hypergeometric-numerical-eval returns true.
630 ;; These are all polynomial cases, so we don't need any analytic continuations.
632 (defun rational-hypergeometric-numerical-eval (a b x)
633 (setq a (mapcar #'(lambda (s) (bigfloat::to s)) a))
634 (setq b (mapcar #'(lambda (s) (bigfloat::to s)) b))
635 (setq x (bigfloat::to x))
636 (maxima::to (bigfloat::hypergeometric-poly-case a b x)))
638 ;; Return float if hypergeometric(a,b,x) should evaluate to a double float (real or
639 ;; complex; return bigfloat if it should evaluate to a bigfloat (real or complex); otherwise
640 ;; return false.
642 (defun use-float-hypergeometric-numerical-eval (a b x)
644 ;; float, complex float, bigfloat, and complex bigfloat; this is a great deal of
645 ;; stuff to check. When $numer is true, everybody must be a $numberp for numerical
646 ;; evaluation; when numer is false, everybody must be a $numberp and somebody must
647 ;;be a float.
649 (if (and (every #'(lambda (s) (complex-number-p s '$numberp)) a)
650 (every #'(lambda (s) (complex-number-p s '$numberp)) b)
651 (complex-number-p x '$numberp)
653 $numer
654 (not (every #'(lambda (s) (complex-number-p s '$ratnump)) a))
655 (not (every #'(lambda (s) (complex-number-p s '$ratnump)) b))
656 (not (complex-number-p x '$ratnump))))
658 ;; When everybody is a float or rational, the return type is float; otherwise bigfloat.
659 (if (and
660 (every #'(lambda (s) (complex-number-p s 'float-or-rational-p)) a)
661 (every #'(lambda (s) (complex-number-p s 'float-or-rational-p)) b)
662 (complex-number-p x 'float-or-rational-p))
663 'float 'bigfloat) nil))
665 ;; Evaluate pFq(a,b,x) using floating point arithmetic. Coerce the returned value
666 ;; to the type described by return-type.
668 ;; When there is a double float overflow, ignore-errors should return nil. After that, we'll
669 ;; try again with a bigfloat.
671 (defun hypergeometric-float-eval (a b z digits return-type)
672 (let ((d) (x))
673 (multiple-value-setq (x d) (ignore-errors (bigfloat::hypergeometric-float-eval a b z digits)))
675 (cond ((and (null x) (eq return-type 'float))
676 (number-coerce
677 (hypergeometric-float-eval (mapcar '$bfloat a)
678 (mapcar '$bfloat b)
679 ($bfloat z)
680 digits
681 'bigfloat) 1.0))
683 ((or (null x) (null d)) nil)
685 ((eq return-type 'float)
686 ($float (maxima::to x)))
688 ((eq return-type 'bigfloat)
689 ($bfloat (maxima::to x)))
691 ;; Unused hypergeometric-float-eval doesn't return rational
692 ;; ((eq return-type 'rational)
693 ;; ($rationalize (maxima::to x)))
695 ;; This should not happen.
696 (t (maxima::to x)))))
698 (defun hypergeometric-poly-case (a b x)
699 (let ((n nil) (z 1) (s 1) (p) (q) (cf 1))
701 ;; Determine how many terms to sum
702 (cond ((and ($taylorp x) (eql 0 ($second ($first ($taylorinfo x))))
703 (integerp ($third ($first ($taylorinfo x)))))
704 (setq n ($third ($first ($taylorinfo x)))))
706 ((some #'(lambda (s) (and (integerp s) (<= s 0))) a)
707 (dolist (ak a)
708 (if (and (integerp ak) (< ak 0)) (setq n (if (null n) ak (max n ak)))))
709 (setq n (- n)))
710 (t (setq n nil)))
712 (if ($ratp x) (setq s ($rat 1) z ($rat 1)))
714 ;; Expand to a polynomial when n is an integer and either
715 ;; (1) x and each member of a and b are complex numbers,
716 ;; (2) n < $expop or $expand_hypergeometric
717 ;; (3) x is a CRE expression.
719 (if (and (integerp n) (or (and (complex-number-p x '$numberp)
720 (every #'(lambda (s) (complex-number-p s '$numberp)) a)
721 (every #'(lambda (s) (complex-number-p s '$numberp)) b))
722 (or $expand_hypergeometric (< n $expop))
723 ($ratp x)))
724 (dotimes (k n s)
725 (setq p (reduce #'mul (mapcar #'(lambda (s) (add s k)) a)))
726 (setq q (reduce #'mul (mapcar #'(lambda (s) (add s k)) b)))
728 ;; sigh..Maxima should (I think) return a rectangular form for
729 ;; complex number multiplication and division. But it doesn't. If
730 ;; that changes, delete the next two lines.
732 (setq cf (mul cf (div p (mul q (+ k 1)))))
733 (if ($constantp cf) (setq cf ($rectform cf)))
734 (setq z (mul z x))
735 (setq s (add s (mul cf z))))
737 nil)))
739 (defun diff-hypergeometric (a b z x)
740 (cond ((and ($freeof x a) ($freeof x b))
741 (setq a (margs a))
742 (setq b (margs b))
743 (let ((p (reduce #'mul a))
744 (q (reduce #'mul b)))
745 (setq a (simplify (cons '(mlist) (mapcar #'(lambda (s) (add 1 s)) a))))
746 (setq b (simplify (cons '(mlist) (mapcar #'(lambda (s) (add 1 s)) b))))
747 (mul ($diff z x) p (div 1 q) (take '(%hypergeometric) a b z))))
748 (t (merror "Maxima does not know the derivatives of the hypergeometric functions with respect to the parameters"))))
751 ;; TeX hypergeometric([a],[b,c],x) as $$F\left( \begin{bmatrix}a\\b\;\,c\end{bmatrix} ,x\right)$$
752 ;; For no good reason, I'm not so fond of pFq notation. Some newer references don't use
753 ;; the pFq notation.
755 (defprop %hypergeometric tex-hypergeometric tex)
757 (defun tex-hypergeometric (x l r)
758 (let ((p) (q) (wide-space ",\\;"))
759 (setq p (tex-list (margs (cadr x)) nil nil wide-space))
760 (setq q (tex-list (margs (caddr x)) nil nil wide-space))
761 (setq p `(,@l "F\\left( \\left. \\begin{array}{c}" ,@p "\\\\" ,@q "\\end{array} \\right |,"))
762 (tex (fourth x) p `("\\right)" ,@r) 'mparen 'mparen)))
764 ;; Integral of hypergeometric(a,b,z)
766 ;; Integrals and Series: Volume 3, More Special Functions
767 ;; Prudnikov, A. P., Brychkov, Yu A., Gould, G. G., Marichev, O.I.
769 ;; /
770 ;; [
771 ;; I pFq((a_p);(b_q);c z) dz
772 ;; ]
773 ;; /
775 ;; = z (p+1)F(q+1)((a_p),1;(b_q),2;c z) 1.16.1.2
777 ;; product((b_q - 1))
778 ;; = ------------------ pFq((a_p)-1; (b_q)-1; c z) 1.16.1.3
779 ;; product((a_p - 1))
781 (defun hyp-integral-3 (a b z)
782 "Integral of hypergeometric(a,b,z) wrt z"
783 (let* (($listarith t)
784 (a-1 (add a -1))
785 (b-1 (add b -1))
786 (prod_b-1 (reduce #'mul (margs b-1)))
787 (prod_a-1 (reduce #'mul (margs a-1))))
788 (if (eql prod_a-1 0)
789 (mul z (take '(%hypergeometric) (append a '(1)) (append b '(2)) z))
790 (mul prod_b-1 (inv prod_a-1) (take '(%hypergeometric) a-1 b-1 z)))))
792 (putprop '%hypergeometric `((a b z) nil nil ,'hyp-integral-3) 'integral)