Add some basic letsimp tests based on bug #3950
[maxima.git] / share / hypergeometric / hypergeometric.lisp
blob1bb885693ae7edd7c8d9c940a4131a41b61554fd
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 (if (not (mget '$hypergeometric_simp 'mexpr)) ($load "hypergeometric_mac.mac"))
13 (if (not (mget '$abramowitz_id 'mexpr)) ($load "abramowitz_id.mac"))
14 (if (not (functionp 'simp-nfloat)) ($load "nfloat"))
16 ;; mea culpa---for numerical evaluation of the hypergeometric functions, the
17 ;; method uses a running error. When the error is too large, the value of fpprec
18 ;; is increased and the evaluation is redone with the larger value of fpprec.
19 ;; The option variable max_fpprec is the largest value for fpprec Maxima will try.
21 (defmvar $max_fpprec 1000)
23 (setf (get '$max_fpprec 'assign)
24 #'(lambda (a b)
25 (declare (ignore a))
26 (if (not (and (atom b) (integerp b)))
27 (progn
28 (mtell "The value of `max_fpprec' must be an integer.~%")
29 'munbindp))))
31 (defmvar $expand_hypergeometric nil)
33 (setf (get '$expand_hypergeometric 'assign)
34 #'(lambda (a b)
35 (declare (ignore a))
36 (if (not (or (eq b nil) (eq b t)))
37 (progn
38 (mtell "The value of `expand_hypergeometric' must be either true or false.~%")
39 'munbindp))))
41 ;; If the length of l is n, return true; otherwise signal wna-err = (wrong number of arguments, by the way).
43 (defun argument-length-check (l n)
44 (if (and (consp l) (consp (first l)) (equal n (length (margs l)))) t (wna-err (caar l))))
46 ;; When multiple_value_return is nil, multiple_values(e1,e2,...) --> e1; otherwise
47 ;; multiple_values(e1,e2,...) --> multiple_values(e1,e2,...).
49 (setf (get '$multiple_values 'operators) 'simp-multiple-values)
51 (defmvar $multiple_value_return nil)
53 (defun simp-multiple-values (e yy z)
54 (declare (ignore yy))
55 (if $multiple_value_return
56 `(($multiple_values simp) ,@(mapcar #'(lambda (s) (simpcheck s z)) (cdr e)))
57 (simpcheck (cadr e) z)))
59 ;; Detect undefined and polynomial cases.
61 (defun classify-hypergeometric (a b x)
62 (let ((ah nil) (bh nil))
64 ;; Let bh = the least member of b that is a negative integer. If there is
65 ;; no such member, set bh = nil.
67 (dolist (bk b)
68 (if (and (integerp bk) (<= bk 0) (or (eq bh nil) (< bk bh))) (setq bh bk)))
70 ;; Let ah = the greatest member of a that is a negative integer. If there is
71 ;; no such member, set ah = nil.
73 (dolist (ak a)
74 (if (and (integerp ak) (<= ak 0) (or (eq ah nil) (> ak ah))) (setq ah ak)))
76 ;; Undefined when either (1) ah is nil and bh is non-nil or (2) ah and bh are
77 ;; non-nil and ah >= bh, and each member of a and b are numbers. (We don't
78 ;; want hypergeometric([a],[-3],x) to be undefined, do we?). I suppose this
79 ;; function could look for declared integers...
81 (cond ((and (every '$numberp a)
82 (every '$numberp b)
83 (or (and (not ah) bh) (and ah bh (>= bh ah)))) 'undefined)
85 ((or ah (zerop1 ($ratdisrep x))
86 (and ($taylorp x) (eql 0 ($second ($first ($taylorinfo x))))
87 (integerp ($third ($first ($taylorinfo x))))))
88 'polynomial)
90 (t 'nonpolynomial))))
92 ;; The function simpcheck changes taylor polynomials to general form--that messes
93 ;; it harder to taylorize hypergeometrics (things like hypergeometric([5],[], taylor(x,x,0,3)) -->
94 ;; a taylor polynomial. So use tsimpcheck: if e is a taylor polynomial, simplify; otherwise, simpcheck.
96 (defun tsimpcheck (e z)
97 (if (or ($taylorp e) ($ratp e)) (simplifya e z) (simpcheck e z)))
99 ;; We don't want realpart and imagpart to think that hypergeometric functions are
100 ;; real valued. So declare hypergeometric to be complex.
102 (eval-when
103 #+gcl (load eval)
104 #-gcl (:load-toplevel :execute)
105 (let (($context '$global) (context '$global))
106 (meval '(($declare) $hypergeometric $complex))))
108 (setf (get '$hypergeometric 'conjugate-function) 'conjugate-hypergeometric)
110 ;; hypergeometric(a,b,x) is entire (commutes with conjugate) when length(a) < length(b) + 1. Also
111 ;; hypergeometric(a,b,x) is analytic inside the unit disk. Outside the unit disk, we need to be careful;
112 ;; for now, conjugate gives a nounform in this case. I suppose we could check for declared negative integer
113 ;; parameter in the list a...I'll wait for a user to request this feature :)
115 (defun conjugate-hypergeometric (l)
116 (let ((a (first l)) (b (second l)) (x (third l)))
117 (cond ((or (< ($length a) (+ 1 ($length b))) (eq t (mgrp 1 (take '(mabs) x))))
118 (take '($hypergeometric) (take '($conjugate) a) (take '($conjugate) b) (take '($conjugate) x)))
120 (list (list '$conjugate 'simp) (take '($hypergeometric) a b x))))))
122 (defun lenient-complex-p (e)
123 (and ($freeof '$infinity '$und '$ind '$inf '$minf '$false '$true t nil e) ;; what else?
124 (not (mbagp e))
125 (not ($featurep e '$nonscalar))
126 (not (mrelationp e))
127 (not ($member e $arrays))))
129 (defprop $hypergeometric simp-hypergeometric operators)
131 ;; Do noncontroversial simplifications on the hypergeometric function. A user that
132 ;; wants additional simplifications can use $hypergeometric_simp. The simplifications are
134 ;; (a) hypergeometric([], [], x) --> exp(x),
136 ;; (b) hypergeometric([a], [], x) --> 1 / (1 - x)^a,
138 ;; (c) hypergeometric([a1,...], [b1, ...], 0) --> 1,
140 ;; (d) hypergeometric([-n,...], [b1, ...], x) --> polynomial.
142 ;; (d) sort and delete common parameters; for example
143 ;; hypergeometric([p,b,a],[c,b],x) --> hypergeometric([a,p],[c],x).
145 ;; (e) hypergeometric([0,a1, ... ], [b1, ...], x) --> 1.
147 ;; Why does this code do (take '(mlist) a) instead of (cons '(mlist) a)? Because
148 ;; (cons '(mlist) a) messes up tellsimp rules. Say tellsimp([a], a]). Then
149 ;; (take (mlist) a) --> a, but (cons '(mlist) a) --> ((mlist) a). And that's not correct.
151 (defun simp-hypergeometric (e yy z)
152 (declare (ignore yy))
153 (argument-length-check e 3)
154 (let ((a (second e)) (b (third e)) (x (fourth e)) (l nil) (a-len) (b-len)
155 (hg-type nil) (dig) (return-type) ($domain '$complex))
157 (cond ((or (not ($listp a)) (not ($listp b)))
158 (mtell "warning: The first two arguments to 'hypergeometric' must be lists.~%")
159 `(($hypergeometric simp) ,(tsimpcheck a z) ,(tsimpcheck b z) ,(tsimpcheck x z)))
162 (setq a (mapcar #'(lambda (s) (tsimpcheck s z)) (margs a))
163 b (mapcar #'(lambda (s) (tsimpcheck s z)) (margs b))
164 x (tsimpcheck x z))
166 ;; Delete common members of a and b. This code is taken from hyp.lisp.
168 (setq l (zl-intersection a b))
169 (setq a (del l a)
170 b (del l b))
172 ;; Check for undefined cases
173 (setq hg-type (classify-hypergeometric a b x))
175 (setq a-len (length a))
176 (setq b-len (length b))
178 ;; Sort a and b and reconvert to Maxima lists.
180 (setq a (sort a '$orderlessp))
181 (setq b (sort b '$orderlessp))
182 (setq a (simplify (cons '(mlist) a)))
183 (setq b (simplify (cons '(mlist) b)))
185 ;; If constantp(x), apply rectform to x. For now, multiplication and division
186 ;; of complex numbers doesn't always return a number in rectangular form. Let's
187 ;; apply rectform to constants.
189 (if ($constantp x) (setq x ($rectform x)))
190 (cond
192 ;; Catch undefined cases and return a nounform.
193 ((or (eq hg-type 'undefined)
194 (member-if #'(lambda(s) (not (lenient-complex-p s))) (cdr a))
195 (member-if #'(lambda(s) (not (lenient-complex-p s))) (cdr b))
196 (not (lenient-complex-p x)))
197 `(($hypergeometric simp) ,a ,b ,x))
199 ;; pFq([a1,...,ap], [b1,...,bq], 0) --> 1 + 0
200 ((zerop1 x) (add 1 x))
202 ;; pFq([0,a1,...,ap], [b1,...,bq], x) --> 1
203 ((member-if 'zerop1 (margs a)) 1)
205 ;; Do hypergeometric([],[],x) --> exp(x). All numerical evaluation is funneled through
206 ;; the same entry point; the function hypergeometric-0f0 doesn't do numerical evaluation.
207 ((and (= 0 a-len) (= 0 b-len) (hypergeometric-0f0 x)))
209 ;; Do hypergeometric([a],[],x) --> 1 / (1-x)^a.
210 ((and (= a-len 1) (= 0 b-len) (hypergeometric-1f0 (second a) x)))
212 ;; Try reflection identity for 1F1.
213 ((and (= a-len 1) (= b-len 1) (hypergeometric-1f1 (second a) (second b) x hg-type)))
215 ;; For 2F1, value at 1--nothing else.
216 ((and (= a-len 2) (= b-len 1) (hypergeometric-2f1 (second a) (third a) (second b) x)))
218 ;; Try numerical evaluation; return nil on failure. This should handle IEEE float,
219 ;; IEEE complex float, bigfloat, and complex big float cases.
220 ((and (setq return-type (use-float-hypergeometric-numerical-eval (margs a) (margs b) x))
221 (setq dig (ceiling (* (if (eq return-type 'float) (float-digits 1.0) fpprec)
222 #.(/ (log 2) (log 10)))))
223 (hypergeometric-float-eval (margs a) (margs b) x dig return-type)))
225 ;; Try rational number numerical evaluation; return nil on failure. This should handle
226 ;; rational and complex rational numerical evaluation.
227 ((use-rational-hypergeometric-numerical-eval (margs a) (margs b) x)
228 (rational-hypergeometric-numerical-eval (margs a) (margs b) x))
230 ;; Handle all other polynomial cases; this includes the case that
231 ;; x is a Taylor polynomial centered at zero.
232 ((hypergeometric-poly-case (margs a) (margs b) x))
234 ;; Return a nounform.
235 (t `(($hypergeometric simp) ,a ,b ,x)))))))
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 ;; Coerce x to the number type of z. The Maxima function safe_float returns a bigfloat when
262 ;; conversion to a float fails (overflow, for example).
263 (defun number-coerce (x z)
264 (cond ((complex-number-p z '$bfloatp)
265 ($bfloat x))
266 ((complex-number-p z 'floatp)
267 (mfuncall '$safe_float x))
268 (t x)))
270 ;; 2F1(a,b;c, x) --> gamma(c) gamma(c - a - b) / (gamma(c-a) gamma (c-b))
271 ;; (Chu-Vandermonde identity, A & S 15.1.20) provided real_part(c-a-b) > 0 and c # 0,-1,-2, ...
272 ;; The c = 0, -1, -2, ... case should be caught previously. If we wanted to be super careful, we'd
273 ;; demand explicitly that c isn't a negative integer.
275 (defun hypergeometric-2f1 (a b c x)
276 (let ((z))
277 (setq z (sub c (add a b)))
278 (cond ((and (onep1 x) (complex-number-p z '$numberp) (eq t (mgrp ($realpart z) 0)))
279 (number-coerce
280 (div
281 (mul (take '(%gamma) c) (take '(%gamma) z))
282 (mul (take '(%gamma) (sub c a)) (take '(%gamma) (sub c b))))
284 (t nil))))
288 ;; For numerical evaluation of a general hypergeometric function, there aren't many
289 ;; alternatives to power series summation.
291 ;; Pursuant to well-established Maxima coding practices :), bigfloat
292 ;; functions receive bigfloat arguments and return bigfloat values.
294 (in-package #-gcl #:bigfloat #+gcl "BIGFLOAT")
296 ;(import 'maxima::while) ;; <--- broken Why?
298 (defmacro while (cond &rest body)
299 `(do ()
300 ((not ,cond))
301 ,@body))
303 (defun 0f0-numeric (x)
304 (exp x))
306 (defun 1f0-numeric (a x)
307 (/ 1 (expt (- 1 x) a)))
309 ;; This is DLMF: http://dlmf.nist.gov/15.15#E1 with zo = 1/2. Also here is Maxima code that
310 ;; sums the first n+1 terms of the sum. The CL function 2f1-numeric-alt uses a running
311 ;; error and it sums until three consecutive partial sums have a modified relative difference
312 ;; that is bounded by the machine epsilon.
315 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],
316 b : c - 2 * b,
317 z : 1 - 2 / x,
318 while k <= n do (
319 s : s + cf * f1,
320 cf : cf * (a + k) / ((k + 1) * z),
321 f : (k * f0 + b * f1)/(k+c),
322 f0 : f1,
323 f1 : f,
324 k : k + 1),
325 s / (1-x/2)^a)$
328 (defun 2f1-numeric-alt (a b c x)
329 (let ((f) (f0 1) (f1 (- 1 (/ (* 2 b) c))) (s 1) (ds 1) (k 1) (cf (/ a (- 1 (/ 2 x)))) (z) (se 0)
330 (eps (epsilon x)) (done 0))
331 (setq b (- c (* 2 b)))
332 (setq z (- 1 (/ 2 x)))
333 (while (< done 3)
334 (setq ds (* cf f1))
335 (setq s (+ s ds))
336 (setq done (if (< (abs ds) (* eps (max 1 (abs s)))) (+ done 1) 0))
337 (setq se (+ se (abs s) (abs ds)))
338 (setq cf (/ (* cf (+ a k)) (* (+ 1 k) z)))
339 (setq f (/ (+ (* k f0) (* b f1)) (+ k c)))
340 (setq f0 f1)
341 (setq f1 f)
342 (setq k (+ k 1)))
343 (values (/ s (expt (- 1 (/ x 2)) a)) (* se (epsilon x)))))
345 ;; hypergeometric([ma,mb],[mc],mx); prefix m means Maxima expression.
347 (defun 2f1-numeric (ma mb mc mx digits)
348 (let* ((region) (f) (ff) (er) (local-fpprec digits) (eps) (mma) (mmb) (mmc) (mmx)
349 (x (bigfloat::to mx))
350 (d (list (list "none" (abs x)) ;; region I, inside unit disk
351 (list "15.3.4" (if (= x 1) nil (abs (/ x (- x 1)))))
352 (list "15.3.6" (abs (- 1 x)))
353 (list "15.3.7" (if (zerop x) nil (abs (/ 1 x))))
354 (list "15.3.8" (if (= x 1) nil (abs (/ 1 (- 1 x)))))
355 (list "15.3.9" (if (zerop x) nil (abs (- 1 (/ 1 x))))))))
357 (setq d (delete-if #'(lambda(s) (null (second s))) d))
358 ;; Sort d from least to greatest magnitude.
359 ;;(print `(d = ,d))
360 (setq d (sort d #'(lambda (a b) (< (second a) (second b)))))
361 (setq region (first (first d)))
362 ;;(print `(region = ,region))
363 ;;(print `(d = ,(second (first d))))
365 (cond
366 ;; When x = 0, return 1.
367 ((zerop x) 1)
369 ;; Use the alternative numerical method when |x| > 0.9; this happens when x is near exp(+/- %i %pi / 3).
371 ((> (second (first d)) 0.9)
372 (setq eps (epsilon (bigfloat::to mx)))
373 (setq er 1)
374 (setq f 1)
376 (while (> (abs er) (* eps (max (abs f) 1)))
377 (maxima::bind-fpprec local-fpprec
378 (setq mma (maxima::nfloat ma `((maxima::mlist)) local-fpprec maxima::$max_fpprec))
379 (setq mmb (maxima::nfloat mb `((maxima::mlist)) local-fpprec maxima::$max_fpprec))
380 (setq mmc (maxima::nfloat mc `((maxima::mlist)) local-fpprec maxima::$max_fpprec))
381 (setq mmx (maxima::nfloat mx `((maxima::mlist)) local-fpprec maxima::$max_fpprec))
382 (multiple-value-setq (f er)
383 (2f1-numeric-alt
384 (bigfloat::to mma) (bigfloat::to mmb) (bigfloat:to mmc) (bigfloat::to mmx)))
385 (setq local-fpprec (* 2 local-fpprec))))
387 (values f er))
388 ;; ma or mb negative integers--that causes trouble for most of the A&S 15.3.4--15.3.9
389 ;; transformations--let's quickly dispatch hypergeometric-float-eval; also dispatch
390 ;; hypergeometric-float-eval when the tranformation is "none" (with adjust-parameters
391 ;; is false!
393 ((or (equal region "none") (and (integerp ma) (<= ma 0)) (and (integerp mb) (<= mb 0))
394 (< (abs x) 0.5))
395 (hypergeometric-float-eval (list ma mb) (list mc) mx digits nil))
397 ;; The case of a,b, and c integers causes trouble; let's dispatch hgfred on it.
398 ((and (integerp ma) (integerp mb) (integerp mc))
399 (setq f (maxima::$hgfred (maxima::take '(maxima::mlist) ma mb)
400 (maxima::take '(maxima::mlist) mc) 'maxima::z))
401 (setq f (maxima::$horner f 'maxima::z))
402 (let ((d))
403 (multiple-value-setq (f d)
404 (maxima::nfloat f `((maxima::mlist) ((maxima::mequal) maxima::z ,mx)) digits maxima::$max_fpprec))
405 (values (bigfloat f) (bigfloat d))))
408 (let ((maxima::$multiple_value_return t))
409 (setq ff `((maxima::$hypergeometric maxima::simp)
410 ((maxima::mlist maxima::simp) ,ma ,mb)
411 ((maxima::mlist maxima::simp) ,mc) maxima::z))
412 (setq f nil)
413 (while d
414 (setq f (if (equal region "none")
415 `((maxima::multiple_values) ,ff t)
416 (maxima::mfuncall 'maxima::$abramowitz_id ff region)))
417 (if (maxima::$second f)
418 (setq d nil f (maxima::$first f)) (setq region (first (pop d)))))
420 ;;(maxima::displa f)
421 ;;(maxima::displa `((maxima::mequal) maxima::z ,mx))
422 (setq f (multiple-value-list
423 (maxima::nfloat f `((maxima::mlist) ((maxima::mequal) maxima::z ,mx))
424 digits maxima::$max_fpprec)))
425 (values (bigfloat::to (first f)) (bigfloat::to (second f))))))))
427 ;; Let a = (a1, a2, ..., am) and b = (b1, b2, ..., bn). Approximate sum(c(k) x^k / k!,k,1,inf),
428 ;; where c(k + 1) / c(k) = (a1 + k) (a2 + k) ... (am + k) / (b1 + k) (b2 + k) ... (bn + k).
430 (defun hypergeometric-by-series (a b x)
431 ;; es = running error for e and ez running error for z.
433 (let ((s 0) (s0 1) (k 0) (z 1) (es 0) (ez 1) (n) (p) (q) (stop 20000) (dig))
434 (setq n (* 2 (+ (length a) (length b) 1)))
435 (while (and (< k stop) (/= s s0)) ;; (not (= s s0)))
436 (setq s s0)
437 (setq p (reduce #'* (mapcar #'(lambda (s) (+ s k)) a))) ;; p adds and p-1 multiplications
438 (setq q (reduce #'* (mapcar #'(lambda (s) (+ s k)) b))) ;; q adds and q-1 multiplications
439 (incf k)
440 (setq z (* z (/ (* p x) (* q k))))
441 ;;(setq ez (+ (* n (abs z)) ez))
442 (setq ez (+ (* (abs (/ (* x p) (* q k))) ez) (* (abs z) n)))
443 (setq s0 (+ s z))
444 (setq es (+ es ez (abs s0))))
446 ;;(print `(k = ,k))
447 (if (>= k stop) (values nil nil)
448 (progn
449 ;; estimate number of correct digits:
451 (setq dig (floor
453 (- (log (max (abs s) (epsilon x))) (log (* es (epsilon x))))
454 #.(/ (log 2) (log 10)))))
456 ;;(print "-----------")
457 ;;(maxima::displa `((maxima::mequal) k ,k))
458 ;;(maxima::displa `((maxima::mequal) xxx ,(maxima::to (epsilon x))))
459 ;;(maxima::displa `((maxima::mequal) es ,(maxima::$float (maxima::to es))))
460 ;;(maxima::displa `((maxima::mequal) s ,(maxima::$float (maxima::to s))))
461 ;;(maxima::displa `((maxima::mequal) dig ,(maxima::$float (maxima::to dig))))
462 (values s dig)))))
464 (defun hypergeometric-poly-case (a b x)
465 (let ((z 1) (s 1) (k 0) (p) (q))
466 (while (not (zerop z))
467 (setq p (reduce #'* (mapcar #'(lambda (s) (+ s k)) a)))
468 (setq q (reduce #'* (mapcar #'(lambda (s) (+ s k)) b)))
469 (incf k)
470 (setq z (/ (* p x z) (* q k)))
471 (setq s (+ z s)))
474 ;; This function numerically evaluates pFq([a1,...,ap], [b1,....bq], x), where all the arguments
475 ;; are Maxima expressions, not bigfloat objects.
477 (defun hypergeometric-float-eval (ma mb mx digits &optional (adjust-params t))
478 (let ((a-len (length ma)) (b-len (length mb)) (f nil) (local-fpprec maxima::$fpprec) (d) (a) (b) (x))
480 ;(maxima::displa `((maxima::mlist) ,@ma))
481 ;(maxima::displa `((maxima::mlist) ,@mb))
482 ;(maxima::displa `((maxima::mlist) ,mx))
483 (setq a (mapcar #'bigfloat::to ma))
484 (setq b (mapcar #'bigfloat::to mb))
485 (setq x (bigfloat::to mx))
487 ;; Special case 0f0, 1f0, 2f1 for |x| > 1, and pfq for |x| > 1 and p >= q + 1.
488 ;; For a general hypergeometric, I don't know how to analytically continue, so in the last case,
489 ;; return false.
491 ;; In the general case, sum the hypergeometric series using a running error, recursing
492 ;; on local-fpprec; bailout when local-fpprec exceeds 1000.
494 (cond ((and (eql a-len 0) (eql b-len 0)) ;; special case 0f0
495 (values (0f0-numeric x) digits))
497 ((and (eql a-len 1) (eql b-len 0)) ;; special case 1f0
498 (values (1f0-numeric (first a) x) digits))
500 ((and (eql a-len 1) (integerp (first a)) (< (first a) 0) (eql b-len 1)) ;; special case 1f1
501 (maxima::bind-fpprec local-fpprec
502 (multiple-value-setq (f d) (1f1-downward-recursion (first a) (first b) x)))
503 (values f d))
505 ;; Optionally do Kummer transformation--when is the Kummer transformation advantageous?
506 ;; I think the sum is ill-conditioned when realpart(x) < 0. Since x is a float, realpart
507 ;; should be quick.
509 ;; The adjust-params argument should prevent an infinite loop (transform --> untransform ...)
510 ;; In this case, an infinite loop shouldn't happen even without the adjust-param scheme.
513 ((and adjust-params
514 (eql a-len 1)
515 (eql b-len 1)
516 (< (realpart x) 0))
517 (let ((f) (d))
518 (multiple-value-setq (f d) (hypergeometric-float-eval
519 (list (maxima::sub (car mb) (car ma)))
520 mb (maxima::neg mx) digits nil))
521 (values (* (exp x) f) d)))
523 ;; analytic continuation for 2f1;
524 ((and (eql a-len 2) (eql b-len 1) adjust-params)
525 (2f1-numeric (car ma) (cadr ma) (car mb) mx digits))
527 ((or (< a-len (+ b-len 1)) (in-unit-circle-p x) (eq 'maxima::polynomial
528 (maxima::classify-hypergeometric ma mb mx)))
530 ;; recurse on local-fpprec; bailout when local-fpprec exceeds $max_fpprec.
532 (while (and (or (null f) (< d digits)) (< local-fpprec maxima::$max_fpprec))
533 (maxima::bind-fpprec local-fpprec
534 (multiple-value-setq (f d) (hypergeometric-by-series a b x))
535 (setq a (mapcar #'(lambda (s) (bigfloat::to (maxima::$bfloat s))) ma))
536 (setq b (mapcar #'(lambda (s) (bigfloat::to (maxima::$bfloat s))) mb))
537 (setq x (bigfloat::to (maxima::$bfloat mx)))
538 ;(print "----------")
539 ;(print `(fpprec = ,local-fpprec))
540 ;(print `(d = ,d))
541 ;(print `(digits = ,digits))
542 ;(incf local-fpprec (+ (- digits d) 10))))
543 (setq local-fpprec (* 2 local-fpprec))))
545 (if (>= local-fpprec maxima::$max_fpprec)
546 (progn
547 (maxima::mtell "Exceeded maximum allowed fpprec.~%")
548 (values nil nil))
549 (values f d))))))
551 (defun in-unit-circle-p (x)
552 (< (abs x) 1))
554 ;; Compute f11(a,b,x) using downward recursion (A&S 13.4.1). The first argument must be a negative integer:
556 ;; f <-- (k * fo + (2 * k + x) * fm1)/(b-k)
559 ;; I think this is faster than the power series summation--it might be useful for orthogonal polynomials.
560 (defun 1f1-downward-recursion (a b x)
561 (let ((fo 1) (fm1 (- 1 (/ x b))) (f) (k -1) (efo 0) (efm1 0) (ef 0))
562 (declare (type fixnum k))
563 (setq k -1)
564 (cond ((eql a 0) (values fo 0))
565 ((eql a -1) (values fm1 0))
567 (setq x (- x b))
568 (while (>= k a)
569 (setq f (/ (- (* k fo) (* (+ (* 2 k) x) fm1)) (- b k)))
570 (setq ef
572 (/ (+ (* k efo)
573 (* (abs (+ (* 2 k) x))) (+ efm1 (* 2 fm1))
574 (* k fo))
575 (abs (- b k)))
576 (* 3 (abs f))))
578 (setq fo fm1)
579 (setq efo efm1)
580 (setq fm1 f)
581 (setq efm1 ef)
582 (decf k))
583 (values fo efo)))))
585 (in-package :maxima)
587 (defun float-or-bigfloat-p (x)
588 (or (floatp x) ($bfloatp x)))
590 ;; Return true iff it is possible to evaluate hypergeometric(a,b,x) using (exact)
591 ;; rational arithmetic. Thus (1) x and every member of a and b (Common Lisp lists) must be
592 ;; a $ratnump and (2) some member of a must be an explicit negative integer. When $numer
593 ;; is true, never do exact rational evaluation? (Likely when $numer is true, we'll never
594 ;; get here anyway?)
596 (defun use-rational-hypergeometric-numerical-eval (a b x)
597 (and (not $numer)
598 (complex-number-p x '$ratnump)
599 (every #'(lambda (s) (complex-number-p s '$ratnump)) a)
600 (every #'(lambda (s) (complex-number-p s '$ratnump)) b)
601 (some #'(lambda (s) (and (integerp s) (< s 0))) a)))
603 ;; Evaluate hypergeometric(a,b,x) using (exact) rational arithmetic. Here a
604 ;; and b are Common Lisp lists. Don't call this function without first
605 ;; checking that use-rational-hypergeometric-numerical-eval returns true.
606 ;; These are all polynomial cases, so we don't need any analytic continuations.
608 (defun rational-hypergeometric-numerical-eval (a b x)
609 (setq a (mapcar #'(lambda (s) (bigfloat::to s)) a))
610 (setq b (mapcar #'(lambda (s) (bigfloat::to s)) b))
611 (setq x (bigfloat::to x))
612 (maxima::to (bigfloat::hypergeometric-poly-case a b x)))
614 ;; Return float if hypergeometric(a,b,x) should evaluate to a double float (real or
615 ;; complex; return bigfloat if it should evaluate to a bigfloat (real or complex); otherwise
616 ;; return false.
618 (defun use-float-hypergeometric-numerical-eval (a b x)
620 ;; float, complex float, bigfloat, and complex bigfloat; this is a great deal of
621 ;; stuff to check. When $numer is true, everybody must be a $numberp for numerical
622 ;; evaluation; when numer is false, everybody must be a $numberp and somebody must
623 ;;be a float.
625 (if (and (every #'(lambda (s) (complex-number-p s '$numberp)) a)
626 (every #'(lambda (s) (complex-number-p s '$numberp)) b)
627 (complex-number-p x '$numberp)
629 $numer
630 (not (every #'(lambda (s) (complex-number-p s '$ratnump)) a))
631 (not (every #'(lambda (s) (complex-number-p s '$ratnump)) b))
632 (not (complex-number-p x '$ratnump))))
634 ;; When everybody is a float or rational, the return type is float; otherwise bigfloat.
635 (if (and
636 (every #'(lambda (s) (complex-number-p s 'float-or-rational-p)) a)
637 (every #'(lambda (s) (complex-number-p s 'float-or-rational-p)) b)
638 (complex-number-p x 'float-or-rational-p))
639 'float 'bigfloat) nil))
641 ;; Evaluate pFq(a,b,x) using floating point arithmetic. Coerce the returned value
642 ;; to the type described by return-type.
644 ;; When there is a double float overflow, ignore-errors should return nil. After that, we'll
645 ;; try again with a bigfloat.
647 (defun hypergeometric-float-eval (a b z digits return-type)
648 (let ((d) (x))
649 (multiple-value-setq (x d) (ignore-errors (bigfloat::hypergeometric-float-eval a b z digits)))
651 (cond ((and (null x) (eq return-type 'float))
652 (number-coerce
653 (hypergeometric-float-eval (mapcar '$bfloat a)
654 (mapcar '$bfloat b)
655 ($bfloat z)
656 digits
657 'bigfloat) 1.0))
659 ((or (null x) (null d)) nil)
661 ((eq return-type 'float)
662 ($float (maxima::to x)))
664 ((eq return-type 'bigfloat)
665 ($bfloat (maxima::to x)))
667 ;; Unused hypergeometric-float-eval doesn't return rational
668 ;; ((eq return-type 'rational)
669 ;; ($rationalize (maxima::to x)))
671 ;; This should not happen.
672 (t (maxima::to x)))))
674 (defun hypergeometric-poly-case (a b x)
675 (let ((n nil) (z 1) (s 1) (p) (q) (cf 1))
677 ;; Determine how many terms to sum
678 (cond ((and ($taylorp x) (eql 0 ($second ($first ($taylorinfo x))))
679 (integerp ($third ($first ($taylorinfo x)))))
680 (setq n ($third ($first ($taylorinfo x)))))
682 ((some #'(lambda (s) (and (integerp s) (<= s 0))) a)
683 (dolist (ak a)
684 (if (and (integerp ak) (< ak 0)) (setq n (if (null n) ak (max n ak)))))
685 (setq n (- n)))
686 (t (setq n nil)))
688 (if ($ratp x) (setq s ($rat 1) z ($rat 1)))
690 ;; Expand to a polynomial when n is an integer and either
691 ;; (1) x and each member of a and b are complex numbers,
692 ;; (2) n < $expop or $expand_hypergeometric
693 ;; (3) x is a CRE expression.
695 (if (and (integerp n) (or (and (complex-number-p x '$numberp)
696 (every #'(lambda (s) (complex-number-p s '$numberp)) a)
697 (every #'(lambda (s) (complex-number-p s '$numberp)) b))
698 (or $expand_hypergeometric (< n $expop))
699 ($ratp x)))
700 (dotimes (k n s)
701 (setq p (reduce #'mul (mapcar #'(lambda (s) (add s k)) a)))
702 (setq q (reduce #'mul (mapcar #'(lambda (s) (add s k)) b)))
704 ;; sigh..Maxima should (I think) return a rectangular form for
705 ;; complex number multiplication and division. But it doesn't. If
706 ;; that changes, delete the next two lines.
708 (setq cf (mul cf (div p (mul q (+ k 1)))))
709 (if ($constantp cf) (setq cf ($rectform cf)))
710 (setq z (mul z x))
711 (setq s (add s (mul cf z))))
713 nil)))
715 (defun diff-hypergeometric (a b z x)
716 (cond ((and ($freeof x a) ($freeof x b))
717 (setq a (margs a))
718 (setq b (margs b))
719 (let ((p (reduce #'mul a))
720 (q (reduce #'mul b)))
721 (setq a (simplify (cons '(mlist) (mapcar #'(lambda (s) (add 1 s)) a))))
722 (setq b (simplify (cons '(mlist) (mapcar #'(lambda (s) (add 1 s)) b))))
723 (mul ($diff z x) p (div 1 q) (take '($hypergeometric) a b z))))
724 (t (merror "Maxima does not know the derivatives of the hypergeometric functions with respect to the parameters"))))
727 ;; TeX hypergeometric([a],[b,c],x) as $$F\left( \begin{bmatrix}a\\b\;\,c\end{bmatrix} ,x\right)$$
728 ;; For no good reason, I'm not so fond of pFq notation. Some newer references don't use
729 ;; the pFq notation.
731 (defprop $hypergeometric tex-hypergeometric tex)
733 (defun tex-hypergeometric (x l r)
734 (let ((p) (q) (wide-space ",\\;"))
735 (setq p (tex-list (margs (cadr x)) nil nil wide-space))
736 (setq q (tex-list (margs (caddr x)) nil nil wide-space))
737 (setq p `(,@l "F\\left( \\left. \\begin{array}{c}" ,@p "\\\\" ,@q "\\end{array} \\right |,"))
738 (tex (fourth x) p `("\\right)" ,@r) 'mparen 'mparen)))
740 ;; Integral of hypergeometric(a,b,z)
742 ;; Integrals and Series: Volume 3, More Special Functions
743 ;; Prudnikov, A. P., Brychkov, Yu A., Gould, G. G., Marichev, O.I.
745 ;; /
746 ;; [
747 ;; I pFq((a_p);(b_q);c z) dz
748 ;; ]
749 ;; /
751 ;; = z (p+1)F(q+1)((a_p),1;(b_q),2;c z) 1.16.1.2
753 ;; product((b_q - 1))
754 ;; = ------------------ pFq((a_p)-1; (b_q)-1; c z) 1.16.1.3
755 ;; product((a_p - 1))
757 (defun hyp-integral-3 (a b z)
758 "Integral of hypergeometric(a,b,z) wrt z"
759 (let* (($listarith t)
760 (a-1 (add a -1))
761 (b-1 (add b -1))
762 (prod_b-1 (reduce #'mul (margs b-1)))
763 (prod_a-1 (reduce #'mul (margs a-1))))
764 (if (eql prod_a-1 0)
765 (mul z (take '($hypergeometric) (append a '(1)) (append b '(2)) z))
766 (mul prod_b-1 (inv prod_a-1) (take '($hypergeometric) a-1 b-1 z)))))
768 (putprop '$hypergeometric `((a b z) nil nil ,#'hyp-integral-3) 'integral)