1 ;;; -*- Mode: Lisp; Package: Maxima; Syntax: Common-Lisp; Base: 10-*- ;;;;
2 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3 ;;; The data in this file contains enhancments. ;;;;;
5 ;;; Copyright (c) 1984,1987 by William Schelter,University of Texas ;;;;;
6 ;;; All rights reserved ;;;;;
8 ;;; Copyright (c) 2001 by Raymond Toy. Replaced everything and added ;;;;;
9 ;;; support for symbolic manipulation of all 12 Jacobian elliptic ;;;;;
10 ;;; functions and the complete and incomplete elliptic integral ;;;;;
11 ;;; of the first, second and third kinds. ;;;;;
12 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
15 ;;(macsyma-module ellipt)
17 (defvar 3//2 '((rat simp
) 3 2))
18 (defvar 1//2 '((rat simp
) 1 2))
19 (defvar -
1//2 '((rat simp
) -
1 2))
22 ;;; Jacobian elliptic functions and elliptic integrals.
26 ;;; [1] Abramowitz and Stegun
27 ;;; [2] Lawden, Elliptic Functions and Applications, Springer-Verlag, 1989
28 ;;; [3] Whittaker & Watson, A Course of Modern Analysis
30 ;;; We use the definitions from Abramowitz and Stegun where our
31 ;;; sn(u,m) is sn(u|m). That is, the second arg is the parameter,
32 ;;; instead of the modulus k or modular angle alpha.
34 ;;; Note that m = k^2 and k = sin(alpha).
38 ;; Routines for computing the basic elliptic functions sn, cn, and dn.
41 ;; A&S gives several methods for computing elliptic functions
42 ;; including the AGM method (16.4) and ascending and descending Landen
43 ;; transformations (16.12 and 16.14). The latter are actually quite
44 ;; fast, only requiring simple arithmetic and square roots for the
45 ;; transformation until the last step. The AGM requires evaluation of
46 ;; several trigonometric functions at each stage.
48 ;; However, the Landen transformations appear to have some round-off
49 ;; issues. For example, using the ascending transform to compute cn,
50 ;; cn(100,.7) > 1e10. This is clearly not right since |cn| <= 1.
53 (in-package #-gcl
#:bigfloat
#+gcl
"BIGFLOAT")
55 (declaim (inline descending-transform ascending-transform
))
57 (defun ascending-transform (u m
)
60 ;; Take care in computing this transform. For the case where
61 ;; m is complex, we should compute sqrt(mu1) first as
62 ;; (1-sqrt(m))/(1+sqrt(m)), and then square this to get mu1.
63 ;; If not, we may choose the wrong branch when computing
65 (let* ((root-m (sqrt m
))
67 (expt (1+ root-m
) 2)))
68 (root-mu1 (/ (- 1 root-m
) (+ 1 root-m
)))
69 (v (/ u
(1+ root-mu1
))))
70 (values v mu root-mu1
)))
72 (defun descending-transform (u m
)
73 ;; Note: Don't calculate mu first, as given in 16.12.1. We
74 ;; should calculate sqrt(mu) = (1-sqrt(m1)/(1+sqrt(m1)), and
75 ;; then compute mu = sqrt(mu)^2. If we calculate mu first,
76 ;; sqrt(mu) loses information when m or m1 is complex.
77 (let* ((root-m1 (sqrt (- 1 m
)))
78 (root-mu (/ (- 1 root-m1
) (+ 1 root-m1
)))
79 (mu (* root-mu root-mu
))
80 (v (/ u
(1+ root-mu
))))
81 (values v mu root-mu
)))
84 ;; This appears to work quite well for both real and complex values
86 (defun elliptic-sn-descending (u m
)
90 ((< (abs m
) (epsilon u
))
94 (multiple-value-bind (v mu root-mu
)
95 (descending-transform u m
)
96 (let* ((new-sn (elliptic-sn-descending v mu
)))
97 (/ (* (1+ root-mu
) new-sn
)
98 (1+ (* root-mu new-sn new-sn
))))))))
100 ;; AGM scale. See A&S 17.6
104 ;; a[n] = (a[n-1]+b[n-1])/2, b[n] = sqrt(a[n-1]*b[n-1]), c[n] = (a[n-1]-b[n-1])/2.
106 ;; We stop when abs(c[n]) <= 10*eps
108 ;; A list of (n a[n] b[n] c[n]) is returned.
109 (defun agm-scale (a b c
)
111 while
(> (abs c
) (* 10 (epsilon c
)))
112 collect
(list n a b c
)
113 do
(psetf a
(/ (+ a b
) 2)
117 ;; WARNING: This seems to have accuracy problems when u is complex. I
118 ;; (rtoy) do not know why. For example (jacobi-agm #c(1e0 1e0) .7e0)
121 ;; #C(1.134045970915582 0.3522523454566013)
122 ;; #C(0.57149659007575 -0.6989899153338323)
123 ;; #C(0.6229715431044184 -0.4488635962149656)
125 ;; But the actual value of sn(1+%i, .7) is .3522523469224946 %i +
126 ;; 1.134045971912365. We've lost about 7 digits of accuracy!
127 (defun jacobi-agm (u m
)
130 ;; Compute the AGM scale with a = 1, b = sqrt(1-m), c = sqrt(m).
132 ;; Then phi[N] = 2^N*a[N]*u and compute phi[n] from
134 ;; sin(2*phi[n-1] - phi[n]) = c[n]/a[n]*sin(phi[n])
138 ;; sn(u|m) = sin(phi[0]), cn(u|m) = cos(phi[0])
139 ;; dn(u|m) = cos(phi[0])/cos(phi[1]-phi[0])
141 ;; Returns the three values sn, cn, dn.
142 (let* ((agm-data (nreverse (rest (agm-scale 1 (sqrt (- 1 m
)) (sqrt m
)))))
143 (phi (destructuring-bind (n a b c
)
145 (declare (ignore b c
))
148 (dolist (agm agm-data
)
149 (destructuring-bind (n a b c
)
151 (declare (ignore n b
))
153 phi
(/ (+ phi
(asin (* (/ c a
) (sin phi
)))) 2))))
154 (values (sin phi
) (cos phi
) (/ (cos phi
) (cos (- phi1 phi
))))))
158 ;; jacobi_sn(u,0) = sin(u). Should we use A&S 16.13.1 if m
161 ;; sn(u,m) = sin(u) - 1/4*m(u-sin(u)*cos(u))*cos(u)
164 ;; jacobi_sn(u,1) = tanh(u). Should we use A&S 16.15.1 if m
165 ;; is close enough to 1?
167 ;; sn(u,m) = tanh(u) + 1/4*(1-m)*(sinh(u)*cosh(u)-u)*sech(u)^2
170 ;; Use the ascending Landen transformation to compute sn.
171 (let ((s (elliptic-sn-descending u m
)))
172 (if (and (realp u
) (realp m
))
178 ;; jacobi_dn(u,0) = 1. Should we use A&S 16.13.3 for small m?
180 ;; dn(u,m) = 1 - 1/2*m*sin(u)^2
183 ;; jacobi_dn(u,1) = sech(u). Should we use A&S 16.15.3 if m
184 ;; is close enough to 1?
186 ;; dn(u,m) = sech(u) + 1/4*(1-m)*(sinh(u)*cosh(u)+u)*tanh(u)*sech(u)
189 ;; Use the Gauss transformation from
190 ;; http://functions.wolfram.com/09.29.16.0013.01:
193 ;; dn((1+sqrt(m))*z, 4*sqrt(m)/(1+sqrt(m))^2)
194 ;; = (1-sqrt(m)*sn(z, m)^2)/(1+sqrt(m)*sn(z,m)^2)
198 ;; dn(y, mu) = (1-sqrt(m)*sn(z, m)^2)/(1+sqrt(m)*sn(z,m)^2)
200 ;; where z = y/(1+sqrt(m)) and mu=4*sqrt(m)/(1+sqrt(m))^2.
202 ;; Solve for m, and we get
204 ;; sqrt(m) = -(mu+2*sqrt(1-mu)-2)/mu or (-mu+2*sqrt(1-mu)+2)/mu.
206 ;; I don't think it matters which sqrt we use, so I (rtoy)
207 ;; arbitrarily choose the first one above.
209 ;; Note that (1-sqrt(1-mu))/(1+sqrt(1-mu)) is the same as
210 ;; -(mu+2*sqrt(1-mu)-2)/mu. Also, the former is more
211 ;; accurate for small mu.
212 (let* ((root (let ((root-1-m (sqrt (- 1 m
))))
216 (s (elliptic-sn-descending z
(* root root
)))
223 ;; jacobi_cn(u,0) = cos(u). Should we use A&S 16.13.2 for
226 ;; cn(u,m) = cos(u) + 1/4*m*(u-sin(u)*cos(u))*sin(u)
229 ;; jacobi_cn(u,1) = sech(u). Should we use A&S 16.15.3 if m
230 ;; is close enough to 1?
232 ;; cn(u,m) = sech(u) - 1/4*(1-m)*(sinh(u)*cosh(u)-u)*tanh(u)*sech(u)
235 ;; Use the ascending Landen transformation, A&S 16.14.3.
236 (multiple-value-bind (v mu root-mu1
)
237 (ascending-transform u m
)
239 (* (/ (+ 1 root-mu1
) mu
)
240 (/ (- (* d d
) root-mu1
)
245 ;; Tell maxima what the derivatives are.
247 ;; Lawden says the derivative wrt to k but that's not what we want.
249 ;; Here's the derivation we used, based on how Lawden get's his results.
253 ;; diff(sn(u,m),m) = s
254 ;; diff(cn(u,m),m) = p
255 ;; diff(dn(u,m),m) = q
257 ;; From the derivatives of sn, cn, dn wrt to u, we have
259 ;; diff(sn(u,m),u) = cn(u)*dn(u)
260 ;; diff(cn(u,m),u) = -cn(u)*dn(u)
261 ;; diff(dn(u,m),u) = -m*sn(u)*cn(u)
264 ;; Differentiate these wrt to m:
266 ;; diff(s,u) = p*dn + cn*q
267 ;; diff(p,u) = -p*dn - q*dn
268 ;; diff(q,u) = -sn*cn - m*s*cn - m*sn*q
272 ;; sn(u)^2 + cn(u)^2 = 1
273 ;; dn(u)^2 + m*sn(u)^2 = 1
275 ;; Differentiate these wrt to m:
278 ;; 2*dn*q + sn^2 + 2*m*sn*s = 0
283 ;; q = -m*s*sn/dn - sn^2/dn/2
286 ;; diff(s,u) = -s*sn*dn/cn - m*s*sn*cn/dn - sn^2*cn/dn/2
290 ;; diff(s,u) + s*(sn*dn/cn + m*sn*cn/dn) = -1/2*sn^2*cn/dn
292 ;; diff(s,u) + s*sn/cn/dn*(dn^2 + m*cn^2) = -1/2*sn^2*cn/dn
294 ;; Multiply through by the integrating factor 1/cn/dn:
296 ;; diff(s/cn/dn, u) = -1/2*sn^2/dn^2 = -1/2*sd^2.
298 ;; Interate this to get
300 ;; s/cn/dn = C + -1/2*int sd^2
302 ;; It can be shown that C is zero.
304 ;; We know that (by differentiating this expression)
306 ;; int dn^2 = (1-m)*u+m*sn*cd + m*(1-m)*int sd^2
310 ;; int sd^2 = 1/m/(1-m)*int dn^2 - u/m - sn*cd/(1-m)
314 ;; s/cn/dn = u/(2*m) + sn*cd/(2*(1-m)) - 1/2/m/(1-m)*int dn^2
318 ;; s = 1/(2*m)*u*cn*dn + 1/(2*(1-m))*sn*cn^2 - 1/2/(m*(1-m))*cn*dn*E(u)
320 ;; where E(u) = int dn^2 = elliptic_e(am(u)) = elliptic_e(asin(sn(u)))
322 ;; This is our desired result:
324 ;; s = 1/(2*m)*cn*dn*[u - elliptic_e(asin(sn(u)),m)/(1-m)] + sn*cn^2/2/(1-m)
327 ;; Since diff(cn(u,m),m) = p = -s*sn/cn, we have
329 ;; p = -1/(2*m)*sn*dn[u - elliptic_e(asin(sn(u)),m)/(1-m)] - sn^2*cn/2/(1-m)
331 ;; diff(dn(u,m),m) = q = -m*s*sn/dn - sn^2/dn/2
333 ;; q = -1/2*sn*cn*[u-elliptic_e(asin(sn),m)/(1-m)] - m*sn^2*cn^2/dn/2/(1-m)
337 ;; = -1/2*sn*cn*[u-elliptic_e(asin(sn),m)/(1-m)] + dn*sn^2/2/(m-1)
341 ((mtimes) ((%jacobi_cn
) u m
) ((%jacobi_dn
) u m
))
343 ((mtimes simp
) ((rat simp
) 1 2)
344 ((mexpt simp
) ((mplus simp
) 1 ((mtimes simp
) -
1 m
)) -
1)
345 ((mexpt simp
) ((%jacobi_cn simp
) u m
) 2) ((%jacobi_sn simp
) u m
))
346 ((mtimes simp
) ((rat simp
) 1 2) ((mexpt simp
) m -
1)
347 ((%jacobi_cn simp
) u m
) ((%jacobi_dn simp
) u m
)
349 ((mtimes simp
) -
1 ((mexpt simp
) ((mplus simp
) 1 ((mtimes simp
) -
1 m
)) -
1)
350 ((%elliptic_e simp
) ((%asin simp
) ((%jacobi_sn simp
) u m
)) m
))))))
355 ((mtimes simp
) -
1 ((%jacobi_sn simp
) u m
) ((%jacobi_dn simp
) u m
))
357 ((mtimes simp
) ((rat simp
) -
1 2)
358 ((mexpt simp
) ((mplus simp
) 1 ((mtimes simp
) -
1 m
)) -
1)
359 ((%jacobi_cn simp
) u m
) ((mexpt simp
) ((%jacobi_sn simp
) u m
) 2))
360 ((mtimes simp
) ((rat simp
) -
1 2) ((mexpt simp
) m -
1)
361 ((%jacobi_dn simp
) u m
) ((%jacobi_sn simp
) u m
)
363 ((mtimes simp
) -
1 ((mexpt simp
) ((mplus simp
) 1 ((mtimes simp
) -
1 m
)) -
1)
364 ((%elliptic_e simp
) ((%asin simp
) ((%jacobi_sn simp
) u m
)) m
))))))
369 ((mtimes) -
1 m
((%jacobi_sn
) u m
) ((%jacobi_cn
) u m
))
371 ((mtimes simp
) ((rat simp
) -
1 2)
372 ((mexpt simp
) ((mplus simp
) 1 ((mtimes simp
) -
1 m
)) -
1)
373 ((%jacobi_dn simp
) u m
) ((mexpt simp
) ((%jacobi_sn simp
) u m
) 2))
374 ((mtimes simp
) ((rat simp
) -
1 2) ((%jacobi_cn simp
) u m
)
375 ((%jacobi_sn simp
) u m
)
378 ((mexpt simp
) ((mplus simp
) 1 ((mtimes simp
) -
1 m
)) -
1)
379 ((%elliptic_e simp
) ((%asin simp
) ((%jacobi_sn simp
) u m
)) m
))))))
382 ;; The inverse elliptic functions.
384 ;; F(phi|m) = asn(sin(phi),m)
386 ;; so asn(u,m) = F(asin(u)|m)
387 (defprop %inverse_jacobi_sn
390 ;; inverse_jacobi_sn(x) = integrate(1/sqrt(1-t^2)/sqrt(1-m*t^2),t,0,x)
391 ;; -> 1/sqrt(1-x^2)/sqrt(1-m*x^2)
393 ((mexpt simp
) ((mplus simp
) 1 ((mtimes simp
) -
1 ((mexpt simp
) x
2)))
395 ((mexpt simp
) ((mplus simp
) 1 ((mtimes simp
) -
1 m
((mexpt simp
) x
2)))
397 ;; diff(F(asin(u)|m),m)
398 ((mtimes simp
) ((mexpt simp
) ((mplus simp
) 1 ((mtimes simp
) -
1 m
)) -
1)
401 ((mexpt simp
) ((mplus simp
) 1 ((mtimes simp
) -
1 ((mexpt simp
) x
2)))
403 ((mexpt simp
) ((mplus simp
) 1 ((mtimes simp
) -
1 m
((mexpt simp
) x
2)))
405 ((mtimes simp
) ((mexpt simp
) m -
1)
406 ((mplus simp
) ((%elliptic_e simp
) ((%asin simp
) x
) m
)
407 ((mtimes simp
) -
1 ((mplus simp
) 1 ((mtimes simp
) -
1 m
))
408 ((%elliptic_f simp
) ((%asin simp
) x
) m
)))))))
411 ;; Let u = inverse_jacobi_cn(x). Then jacobi_cn(u) = x or
412 ;; sqrt(1-jacobi_sn(u)^2) = x. Or
414 ;; jacobi_sn(u) = sqrt(1-x^2)
416 ;; So u = inverse_jacobi_sn(sqrt(1-x^2),m) = inverse_jacob_cn(x,m)
418 (defprop %inverse_jacobi_cn
420 ;; Whittaker and Watson, 22.121
421 ;; inverse_jacobi_cn(u,m) = integrate(1/sqrt(1-t^2)/sqrt(1-m+m*t^2), t, u, 1)
422 ;; -> -1/sqrt(1-x^2)/sqrt(1-m+m*x^2)
424 ((mexpt simp
) ((mplus simp
) 1 ((mtimes simp
) -
1 ((mexpt simp
) x
2)))
427 ((mplus simp
) 1 ((mtimes simp
) -
1 m
)
428 ((mtimes simp
) m
((mexpt simp
) x
2)))
430 ((mtimes simp
) ((mexpt simp
) ((mplus simp
) 1 ((mtimes simp
) -
1 m
)) -
1)
435 ((mtimes simp
) -
1 m
((mplus simp
) 1 ((mtimes simp
) -
1 ((mexpt simp
) x
2)))))
437 ((mexpt simp
) ((mplus simp
) 1 ((mtimes simp
) -
1 ((mexpt simp
) x
2))) ((rat simp
) 1 2))
439 ((mtimes simp
) ((mexpt simp
) m -
1)
443 ((mexpt simp
) ((mplus simp
) 1 ((mtimes simp
) -
1 ((mexpt simp
) x
2))) ((rat simp
) 1 2)))
445 ((mtimes simp
) -
1 ((mplus simp
) 1 ((mtimes simp
) -
1 m
))
448 ((mexpt simp
) ((mplus simp
) 1 ((mtimes simp
) -
1 ((mexpt simp
) x
2))) ((rat simp
) 1 2)))
452 ;; Let u = inverse_jacobi_dn(x). Then
454 ;; jacobi_dn(u) = x or
456 ;; x^2 = jacobi_dn(u)^2 = 1 - m*jacobi_sn(u)^2
458 ;; so jacobi_sn(u) = sqrt(1-x^2)/sqrt(m)
460 ;; or u = inverse_jacobi_sn(sqrt(1-x^2)/sqrt(m))
461 (defprop %inverse_jacobi_dn
463 ;; Whittaker and Watson, 22.121
464 ;; inverse_jacobi_dn(u,m) = integrate(1/sqrt(1-t^2)/sqrt(t^2-(1-m)), t, u, 1)
465 ;; -> -1/sqrt(1-x^2)/sqrt(x^2+m-1)
467 ((mexpt simp
) ((mplus simp
) 1 ((mtimes simp
) -
1 ((mexpt simp
) x
2)))
469 ((mexpt simp
) ((mplus simp
) -
1 m
((mexpt simp
) x
2)) ((rat simp
) -
1 2)))
471 ((mtimes simp
) ((rat simp
) -
1 2) ((mexpt simp
) m
((rat simp
) -
3 2))
474 ((mtimes simp
) -
1 ((mexpt simp
) m -
1)
475 ((mplus simp
) 1 ((mtimes simp
) -
1 ((mexpt simp
) x
2)))))
477 ((mexpt simp
) ((mplus simp
) 1 ((mtimes simp
) -
1 ((mexpt simp
) x
2)))
479 ((mexpt simp
) ((mabs simp
) x
) -
1))
480 ((mtimes simp
) ((mexpt simp
) ((mplus simp
) 1 ((mtimes simp
) -
1 m
)) -
1)
482 ((mtimes simp
) -
1 ((mexpt simp
) m
((rat simp
) -
1 2))
485 ((mtimes simp
) -
1 ((mexpt simp
) m -
1)
486 ((mplus simp
) 1 ((mtimes simp
) -
1 ((mexpt simp
) x
2)))))
488 ((mexpt simp
) ((mplus simp
) 1 ((mtimes simp
) -
1 ((mexpt simp
) x
2)))
490 ((mexpt simp
) ((mabs simp
) x
) -
1))
491 ((mtimes simp
) ((mexpt simp
) m -
1)
495 ((mtimes simp
) ((mexpt simp
) m
((rat simp
) -
1 2))
496 ((mexpt simp
) ((mplus simp
) 1
497 ((mtimes simp
) -
1 ((mexpt simp
) x
2)))
500 ((mtimes simp
) -
1 ((mplus simp
) 1 ((mtimes simp
) -
1 m
))
503 ((mtimes simp
) ((mexpt simp
) m
((rat simp
) -
1 2))
504 ((mexpt simp
) ((mplus simp
) 1
505 ((mtimes simp
) -
1 ((mexpt simp
) x
2)))
511 ;; Possible forms of a complex number:
515 ;; ((mplus simp) 2.3 ((mtimes simp) 2.3 $%i))
516 ;; ((mplus simp) 2.3 $%i))
517 ;; ((mtimes simp) 2.3 $%i)
521 ;; Is argument u a complex number with real and imagpart satisfying predicate ntypep?
522 (defun complex-number-p (u &optional
(ntypep 'numberp
))
524 (labels ((a1 (x) (cadr x
))
527 (N (x) (funcall ntypep x
)) ; N
528 (i (x) (and (eq x
'$%i
) (N 1))) ; %i
529 (N+i
(x) (and (null (a3+ x
)) ; mplus test is precondition
531 (or (and (i (a2 x
)) (setq I
1) t
)
532 (and (mtimesp (a2 x
)) (N*i
(a2 x
))))))
533 (N*i
(x) (and (null (a3+ x
)) ; mtimes test is precondition
536 (declare (inline a1 a2 a3
+ N i N
+i N
*i
))
537 (cond ((N u
) (values t u
0)) ;2.3
538 ((atom u
) (if (i u
) (values t
0 1))) ;%i
539 ((mplusp u
) (if (N+i u
) (values t R I
))) ;N+%i, N+N*%i
540 ((mtimesp u
) (if (N*i u
) (values t R I
))) ;N*%i
543 (defun complexify (x)
544 ;; Convert a Lisp number to a maxima number
546 ((complexp x
) (add (realpart x
) (mul '$%i
(imagpart x
))))
547 (t (merror (intl:gettext
"COMPLEXIFY: argument must be a Lisp real or complex number.~%COMPLEXIFY: found: ~:M") x
))))
549 (defun kc-arg (exp m
)
550 ;; Replace elliptic_kc(m) in the expression with sym. Check to see
551 ;; if the resulting expression is linear in sym and the constant
552 ;; term is zero. If so, return the coefficient of sym, i.e, the
553 ;; coefficient of elliptic_kc(m).
554 (let* ((sym (gensym))
555 (arg (maxima-substitute sym
`((%elliptic_kc
) ,m
) exp
)))
556 (if (and (not (equalp arg exp
))
558 (zerop1 (coefficient arg sym
0)))
559 (coefficient arg sym
1)
562 (defun kc-arg2 (exp m
)
563 ;; Replace elliptic_kc(m) in the expression with sym. Check to see
564 ;; if the resulting expression is linear in sym and the constant
565 ;; term is zero. If so, return the coefficient of sym, i.e, the
566 ;; coefficient of elliptic_kc(m), and the constant term. Otherwise,
568 (let* ((sym (gensym))
569 (arg (maxima-substitute sym
`((%elliptic_kc
) ,m
) exp
)))
570 (if (and (not (equalp arg exp
))
572 (list (coefficient arg sym
1)
573 (coefficient arg sym
0))
576 ;; Tell maxima how to simplify the functions
578 (def-simplifier jacobi_sn
(u m
)
581 ((float-numerical-eval-p u m
)
582 (to (bigfloat::sn
(bigfloat:to
($float u
)) (bigfloat:to
($float m
)))))
583 ((setf args
(complex-float-numerical-eval-p u m
))
584 (destructuring-bind (u m
)
586 (to (bigfloat::sn
(bigfloat:to
($float u
)) (bigfloat:to
($float m
))))))
587 ((bigfloat-numerical-eval-p u m
)
588 (to (bigfloat::sn
(bigfloat:to
($bfloat u
)) (bigfloat:to
($bfloat m
)))))
589 ((setf args
(complex-bigfloat-numerical-eval-p u m
))
590 (destructuring-bind (u m
)
592 (to (bigfloat::sn
(bigfloat:to
($bfloat u
)) (bigfloat:to
($bfloat m
))))))
602 ((and $trigsign
(mminusp* u
))
603 (neg (ftake* '%jacobi_sn
(neg u
) m
)))
606 (member (caar u
) '(%inverse_jacobi_sn
618 (alike1 (third u
) m
))
619 (let ((inv-arg (second u
)))
622 ;; jacobi_sn(inverse_jacobi_sn(u,m), m) = u
625 ;; inverse_jacobi_ns(u,m) = inverse_jacobi_sn(1/u,m)
628 ;; sn(x)^2 + cn(x)^2 = 1 so sn(x) = sqrt(1-cn(x)^2)
629 (power (sub 1 (mul inv-arg inv-arg
)) 1//2))
631 ;; inverse_jacobi_nc(u) = inverse_jacobi_cn(1/u)
632 (ftake '%jacobi_sn
(ftake '%inverse_jacobi_cn
(div 1 inv-arg
) m
)
635 ;; dn(x)^2 + m*sn(x)^2 = 1 so
636 ;; sn(x) = 1/sqrt(m)*sqrt(1-dn(x)^2)
637 (mul (div 1 (power m
1//2))
638 (power (sub 1 (mul inv-arg inv-arg
)) 1//2)))
640 ;; inverse_jacobi_nd(u) = inverse_jacobi_dn(1/u)
641 (ftake '%jacobi_sn
(ftake '%inverse_jacobi_dn
(div 1 inv-arg
) m
)
644 ;; See below for inverse_jacobi_sc.
645 (div inv-arg
(power (add 1 (mul inv-arg inv-arg
)) 1//2)))
647 ;; inverse_jacobi_cs(u) = inverse_jacobi_sc(1/u)
648 (ftake '%jacobi_sn
(ftake '%inverse_jacobi_sc
(div 1 inv-arg
) m
)
651 ;; See below for inverse_jacobi_sd
652 (div inv-arg
(power (add 1 (mul m
(mul inv-arg inv-arg
))) 1//2)))
654 ;; inverse_jacobi_ds(u) = inverse_jacobi_sd(1/u)
655 (ftake '%jacobi_sn
(ftake '%inverse_jacobi_sd
(div 1 inv-arg
) m
)
659 (div (power (sub 1 (mul inv-arg inv-arg
)) 1//2)
660 (power (sub 1 (mul m
(mul inv-arg inv-arg
))) 1//2)))
662 (ftake '%jacobi_sn
(ftake '%inverse_jacobi_cd
(div 1 inv-arg
) m
) m
)))))
663 ;; A&S 16.20.1 (Jacobi's Imaginary transformation)
664 ((and $%iargs
(multiplep u
'$%i
))
666 (ftake* '%jacobi_sc
(coeff u
'$%i
1) (add 1 (neg m
)))))
667 ((setq coef
(kc-arg2 u m
))
671 (destructuring-bind (lin const
)
673 (cond ((integerp lin
)
676 ;; sn(4*m*K + u) = sn(u), sn(0) = 0
679 (ftake '%jacobi_sn const m
)))
681 ;; sn(4*m*K + K + u) = sn(K+u) = cd(u)
685 (ftake '%jacobi_cd const m
)))
687 ;; sn(4*m*K+2*K + u) = sn(2*K+u) = -sn(u)
691 (neg (ftake '%jacobi_sn const m
))))
693 ;; sn(4*m*K+3*K+u) = sn(2*K + K + u) = -sn(K+u) = -cd(u)
697 (neg (ftake '%jacobi_cd const m
))))))
698 ((and (alike1 lin
1//2)
702 ;; sn(1/2*K) = 1/sqrt(1+sqrt(1-m))
704 (power (add 1 (power (sub 1 m
) 1//2))
706 ((and (alike1 lin
3//2)
710 ;; sn(1/2*K + K) = cd(1/2*K,m)
711 (ftake '%jacobi_cd
(mul 1//2
712 (ftake '%elliptic_kc m
))
720 (def-simplifier jacobi_cn
(u m
)
723 ((float-numerical-eval-p u m
)
724 (to (bigfloat::cn
(bigfloat:to
($float u
)) (bigfloat:to
($float m
)))))
725 ((setf args
(complex-float-numerical-eval-p u m
))
726 (destructuring-bind (u m
)
728 (to (bigfloat::cn
(bigfloat:to
($float u
)) (bigfloat:to
($float m
))))))
729 ((bigfloat-numerical-eval-p u m
)
730 (to (bigfloat::cn
(bigfloat:to
($bfloat u
)) (bigfloat:to
($bfloat m
)))))
731 ((setf args
(complex-bigfloat-numerical-eval-p u m
))
732 (destructuring-bind (u m
)
734 (to (bigfloat::cn
(bigfloat:to
($bfloat u
)) (bigfloat:to
($bfloat m
))))))
744 ((and $trigsign
(mminusp* u
))
745 (ftake* '%jacobi_cn
(neg u
) m
))
748 (member (caar u
) '(%inverse_jacobi_sn
760 (alike1 (third u
) m
))
761 (cond ((eq (caar u
) '%inverse_jacobi_cn
)
764 ;; I'm lazy. Use cn(x) = sqrt(1-sn(x)^2). Hope
766 (power (sub 1 (power (ftake '%jacobi_sn u
(third u
)) 2))
768 ;; A&S 16.20.2 (Jacobi's Imaginary transformation)
769 ((and $%iargs
(multiplep u
'$%i
))
770 (ftake* '%jacobi_nc
(coeff u
'$%i
1) (add 1 (neg m
))))
771 ((setq coef
(kc-arg2 u m
))
775 (destructuring-bind (lin const
)
777 (cond ((integerp lin
)
780 ;; cn(4*m*K + u) = cn(u),
784 (ftake '%jacobi_cn const m
)))
786 ;; cn(4*m*K + K + u) = cn(K+u) = -sqrt(m1)*sd(u)
790 (neg (mul (power (sub 1 m
) 1//2)
791 (ftake '%jacobi_sd const m
)))))
793 ;; cn(4*m*K + 2*K + u) = cn(2*K+u) = -cn(u)
797 (neg (ftake '%jacobi_cn const m
))))
799 ;; cn(4*m*K + 3*K + u) = cn(2*K + K + u) =
800 ;; -cn(K+u) = sqrt(m1)*sd(u)
805 (mul (power (sub 1 m
) 1//2)
806 (ftake '%jacobi_sd const m
))))))
807 ((and (alike1 lin
1//2)
810 ;; cn(1/2*K) = (1-m)^(1/4)/sqrt(1+sqrt(1-m))
811 (mul (power (sub 1 m
) (div 1 4))
821 (def-simplifier jacobi_dn
(u m
)
824 ((float-numerical-eval-p u m
)
825 (to (bigfloat::dn
(bigfloat:to
($float u
)) (bigfloat:to
($float m
)))))
826 ((setf args
(complex-float-numerical-eval-p u m
))
827 (destructuring-bind (u m
)
829 (to (bigfloat::dn
(bigfloat:to
($float u
)) (bigfloat:to
($float m
))))))
830 ((bigfloat-numerical-eval-p u m
)
831 (to (bigfloat::dn
(bigfloat:to
($bfloat u
)) (bigfloat:to
($bfloat m
)))))
832 ((setf args
(complex-bigfloat-numerical-eval-p u m
))
833 (destructuring-bind (u m
)
835 (to (bigfloat::dn
(bigfloat:to
($bfloat u
)) (bigfloat:to
($bfloat m
))))))
845 ((and $trigsign
(mminusp* u
))
846 (ftake* '%jacobi_dn
(neg u
) m
))
849 (member (caar u
) '(%inverse_jacobi_sn
861 (alike1 (third u
) m
))
862 (cond ((eq (caar u
) '%inverse_jacobi_dn
)
863 ;; jacobi_dn(inverse_jacobi_dn(u,m), m) = u
866 ;; Express in terms of sn:
867 ;; dn(x) = sqrt(1-m*sn(x)^2)
869 (power (ftake '%jacobi_sn u m
) 2)))
871 ((zerop1 ($ratsimp
(sub u
(power (sub 1 m
) 1//2))))
873 ;; dn(sqrt(1-m),m) = K(m)
874 (ftake '%elliptic_kc m
))
875 ;; A&S 16.20.2 (Jacobi's Imaginary transformation)
876 ((and $%iargs
(multiplep u
'$%i
))
877 (ftake* '%jacobi_dc
(coeff u
'$%i
1)
879 ((setq coef
(kc-arg2 u m
))
882 ;; dn(m*K+u) has period 2K
884 (destructuring-bind (lin const
)
886 (cond ((integerp lin
)
889 ;; dn(2*m*K + u) = dn(u)
893 ;; dn(4*m*K+2*K + u) = dn(2*K+u) = dn(u)
894 (ftake '%jacobi_dn const m
)))
896 ;; dn(2*m*K + K + u) = dn(K + u) = sqrt(1-m)*nd(u)
899 (power (sub 1 m
) 1//2)
900 (mul (power (sub 1 m
) 1//2)
901 (ftake '%jacobi_nd const m
))))))
902 ((and (alike1 lin
1//2)
905 ;; dn(1/2*K) = (1-m)^(1/4)
912 ;; Should we simplify the inverse elliptic functions into the
913 ;; appropriate incomplete elliptic integral? I think we should leave
914 ;; it, but perhaps allow some way to do that transformation if
917 (def-simplifier inverse_jacobi_sn
(u m
)
919 ;; To numerically evaluate inverse_jacobi_sn (asn), use
921 ;; asn(x,m) = F(asin(x),m)
923 ;; But F(phi,m) = sin(phi)*rf(cos(phi)^2, 1-m*sin(phi)^2,1). Thus
925 ;; asn(x,m) = F(asin(x),m)
926 ;; = x*rf(1-x^2,1-m*x^2,1)
928 ;; I (rtoy) am not 100% about the first identity above for all
929 ;; complex values of x and m, but tests seem to indicate that it
930 ;; produces the correct value as verified by verifying
931 ;; jacobi_sn(inverse_jacobi_sn(x,m),m) = x.
932 (cond ((float-numerical-eval-p u m
)
933 (let ((uu (bigfloat:to
($float u
)))
934 (mm (bigfloat:to
($float m
))))
937 (bigfloat::bf-rf
(bigfloat:to
(- 1 (* uu uu
)))
938 (bigfloat:to
(- 1 (* mm uu uu
)))
940 ((setf args
(complex-float-numerical-eval-p u m
))
941 (destructuring-bind (u m
)
943 (let ((uu (bigfloat:to
($float u
)))
944 (mm (bigfloat:to
($float m
))))
945 (complexify (* uu
(bigfloat::bf-rf
(- 1 (* uu uu
))
948 ((bigfloat-numerical-eval-p u m
)
949 (let ((uu (bigfloat:to u
))
950 (mm (bigfloat:to m
)))
952 (bigfloat::bf-rf
(bigfloat:-
1 (bigfloat:* uu uu
))
953 (bigfloat:-
1 (bigfloat:* mm uu uu
))
955 ((setf args
(complex-bigfloat-numerical-eval-p u m
))
956 (destructuring-bind (u m
)
958 (let ((uu (bigfloat:to u
))
959 (mm (bigfloat:to m
)))
961 (bigfloat::bf-rf
(bigfloat:-
1 (bigfloat:* uu uu
))
962 (bigfloat:-
1 (bigfloat:* mm uu uu
))
968 ;; asn(1,m) = elliptic_kc(m)
969 (ftake '%elliptic_kc m
))
970 ((and (numberp u
) (onep1 (- u
)))
971 ;; asn(-1,m) = -elliptic_kc(m)
972 (mul -
1 (ftake '%elliptic_kc m
)))
974 ;; asn(x,0) = F(asin(x),0) = asin(x)
977 ;; asn(x,1) = F(asin(x),1) = log(tan(pi/4+asin(x)/2))
978 (ftake '%elliptic_f
(ftake '%asin u
) 1))
979 ((and (eq $triginverses
'$all
)
981 (eq (caar u
) '%jacobi_sn
)
982 (alike1 (third u
) m
))
983 ;; inverse_jacobi_sn(sn(u)) = u
989 (def-simplifier inverse_jacobi_cn
(u m
)
991 (cond ((float-numerical-eval-p u m
)
992 ;; Numerically evaluate acn
994 ;; acn(x,m) = F(acos(x),m)
995 (to (elliptic-f (cl:acos
($float u
)) ($float m
))))
996 ((setf args
(complex-float-numerical-eval-p u m
))
997 (destructuring-bind (u m
)
999 (to (elliptic-f (cl:acos
(bigfloat:to
($float u
)))
1000 (bigfloat:to
($float m
))))))
1001 ((bigfloat-numerical-eval-p u m
)
1002 (to (bigfloat::bf-elliptic-f
(bigfloat:acos
(bigfloat:to u
))
1004 ((setf args
(complex-bigfloat-numerical-eval-p u m
))
1005 (destructuring-bind (u m
)
1007 (to (bigfloat::bf-elliptic-f
(bigfloat:acos
(bigfloat:to u
))
1010 ;; asn(x,0) = F(acos(x),0) = acos(x)
1011 (ftake '%elliptic_f
(ftake '%acos u
) 0))
1013 ;; asn(x,1) = F(asin(x),1) = log(tan(pi/2+asin(x)/2))
1014 (ftake '%elliptic_f
(ftake '%acos u
) 1))
1016 (ftake '%elliptic_kc m
))
1019 ((and (eq $triginverses
'$all
)
1021 (eq (caar u
) '%jacobi_cn
)
1022 (alike1 (third u
) m
))
1023 ;; inverse_jacobi_cn(cn(u)) = u
1029 (def-simplifier inverse_jacobi_dn
(u m
)
1031 (cond ((float-numerical-eval-p u m
)
1032 (to (bigfloat::bf-inverse-jacobi-dn
(bigfloat:to
(float u
))
1033 (bigfloat:to
(float m
)))))
1034 ((setf args
(complex-float-numerical-eval-p u m
))
1035 (destructuring-bind (u m
)
1037 (let ((uu (bigfloat:to
($float u
)))
1038 (mm (bigfloat:to
($float m
))))
1039 (to (bigfloat::bf-inverse-jacobi-dn uu mm
)))))
1040 ((bigfloat-numerical-eval-p u m
)
1041 (let ((uu (bigfloat:to u
))
1042 (mm (bigfloat:to m
)))
1043 (to (bigfloat::bf-inverse-jacobi-dn uu mm
))))
1044 ((setf args
(complex-bigfloat-numerical-eval-p u m
))
1045 (destructuring-bind (u m
)
1047 (to (bigfloat::bf-inverse-jacobi-dn
(bigfloat:to u
) (bigfloat:to m
)))))
1049 ;; x = dn(u,1) = sech(u). so u = asech(x)
1052 ;; jacobi_dn(0,m) = 1
1054 ((zerop1 ($ratsimp
(sub u
(power (sub 1 m
) 1//2))))
1055 ;; jacobi_dn(K(m),m) = sqrt(1-m) so
1056 ;; inverse_jacobi_dn(sqrt(1-m),m) = K(m)
1057 (ftake '%elliptic_kc m
))
1058 ((and (eq $triginverses
'$all
)
1060 (eq (caar u
) '%jacobi_dn
)
1061 (alike1 (third u
) m
))
1062 ;; inverse_jacobi_dn(dn(u)) = u
1068 ;;;; Elliptic integrals
1070 (let ((errtol (expt (* 4 flonum-epsilon
) 1/6))
1074 (declare (type flonum errtol c1 c2 c3
))
1076 "Compute Carlson's incomplete or complete elliptic integral of the
1082 RF(x, y, z) = I ----------------------------------- dt
1083 ] SQRT(x + t) SQRT(y + t) SQRT(z + t)
1087 x, y, and z may be complex.
1089 (declare (number x y z
))
1090 (let ((x (coerce x
'(complex flonum
)))
1091 (y (coerce y
'(complex flonum
)))
1092 (z (coerce z
'(complex flonum
))))
1093 (declare (type (complex flonum
) x y z
))
1095 (let* ((mu (/ (+ x y z
) 3))
1096 (x-dev (- 2 (/ (+ mu x
) mu
)))
1097 (y-dev (- 2 (/ (+ mu y
) mu
)))
1098 (z-dev (- 2 (/ (+ mu z
) mu
))))
1099 (when (< (max (abs x-dev
) (abs y-dev
) (abs z-dev
)) errtol
)
1100 (let ((e2 (- (* x-dev y-dev
) (* z-dev z-dev
)))
1101 (e3 (* x-dev y-dev z-dev
)))
1108 (let* ((x-root (sqrt x
))
1111 (lam (+ (* x-root
(+ y-root z-root
)) (* y-root z-root
))))
1112 (setf x
(* (+ x lam
) 1/4))
1113 (setf y
(* (+ y lam
) 1/4))
1114 (setf z
(* (+ z lam
) 1/4))))))))
1116 ;; Elliptic integral of the first kind (Legendre's form):
1122 ;; I ------------------- ds
1124 ;; / SQRT(1 - m SIN (s))
1127 (defun elliptic-f (phi-arg m-arg
)
1128 (flet ((base (phi-arg m-arg
)
1129 (cond ((and (realp m-arg
) (realp phi-arg
))
1130 (let ((phi (float phi-arg
))
1135 ;; F(phi|m) = 1/sqrt(m)*F(theta|1/m)
1137 ;; with sin(theta) = sqrt(m)*sin(phi)
1138 (/ (elliptic-f (cl:asin
(* (sqrt m
) (sin phi
))) (/ m
))
1146 (- (/ (elliptic-f (float (/ pi
2)) m
/m
+1)
1148 (/ (elliptic-f (- (float (/ pi
2)) phi
) m
/m
+1)
1156 1 ;; F(phi,1) = log(sec(phi)+tan(phi))
1157 ;; = log(tan(pi/4+pi/2))
1158 (log (cl:tan
(+ (/ phi
2) (float (/ pi
4))))))
1160 (- (elliptic-f (- phi
) m
)))
1163 (multiple-value-bind (s phi-rem
)
1164 (truncate phi
(float pi
))
1165 (+ (* 2 s
(elliptic-k m
))
1166 (elliptic-f phi-rem m
))))
1168 (let ((sin-phi (sin phi
))
1172 (bigfloat::bf-rf
(* cos-phi cos-phi
)
1173 (* (- 1 (* k sin-phi
))
1174 (+ 1 (* k sin-phi
)))
1177 (+ (* 2 (elliptic-k m
))
1178 (elliptic-f (- phi
(float pi
)) m
)))
1180 (error "Shouldn't happen! Unhandled case in elliptic-f: ~S ~S~%"
1183 (let ((phi (coerce phi-arg
'(complex flonum
)))
1184 (m (coerce m-arg
'(complex flonum
))))
1185 (let ((sin-phi (sin phi
))
1189 (crf (* cos-phi cos-phi
)
1190 (* (- 1 (* k sin-phi
))
1191 (+ 1 (* k sin-phi
)))
1193 ;; Elliptic F is quasi-periodic wrt to z:
1195 ;; F(z|m) = F(z - pi*round(Re(z)/pi)|m) + 2*round(Re(z)/pi)*K(m)
1196 (let ((period (round (realpart phi-arg
) pi
)))
1197 (+ (base (- phi-arg
(* pi period
)) m-arg
)
1201 (bigfloat:to
(elliptic-k m-arg
))))))))
1203 ;; Complete elliptic integral of the first kind
1204 (defun elliptic-k (m)
1212 (- (/ (elliptic-k m
/m
+1)
1214 (/ (elliptic-f 0.0 m
/m
+1)
1221 (intl:gettext
"elliptic_kc: elliptic_kc(1) is undefined.")))
1223 (bigfloat::bf-rf
0.0 (- 1 m
)
1226 (bigfloat::bf-rf
0.0 (- 1 m
)
1229 ;; Elliptic integral of the second kind (Legendre's form):
1235 ;; I SQRT(1 - m SIN (s)) ds
1240 (defun elliptic-e (phi m
)
1241 (declare (type flonum phi m
))
1242 (flet ((base (phi m
)
1250 (let* ((sin-phi (sin phi
))
1253 (y (* (- 1 (* k sin-phi
))
1254 (+ 1 (* k sin-phi
)))))
1256 (bigfloat::bf-rf
(* cos-phi cos-phi
) y
1.0))
1259 (bigfloat::bf-rd
(* cos-phi cos-phi
) y
1.0)))))))))
1260 ;; Elliptic E is quasi-periodic wrt to phi:
1262 ;; E(z|m) = E(z - %pi*round(Re(z)/%pi)|m) + 2*round(Re(z)/%pi)*E(m)
1263 (let ((period (round (realpart phi
) pi
)))
1264 (+ (base (- phi
(* pi period
)) m
)
1265 (* 2 period
(elliptic-ec m
))))))
1268 (defun elliptic-ec (m)
1269 (declare (type flonum m
))
1278 (to (- (bigfloat::bf-rf
0.0 y
1.0)
1280 (bigfloat::bf-rd
0.0 y
1.0))))))))
1283 ;; Define the elliptic integrals for maxima
1285 ;; We use the definitions given in A&S 17.2.6 and 17.2.8. In particular:
1290 ;; F(phi|m) = I ------------------- ds
1292 ;; / SQRT(1 - m SIN (s))
1300 ;; E(phi|m) = I SQRT(1 - m SIN (s)) ds
1305 ;; That is, we do not use the modular angle, alpha, as the second arg;
1306 ;; the parameter m = sin(alpha)^2 is used.
1310 ;; The derivative of F(phi|m) wrt to phi is easy. The derivative wrt
1311 ;; to m is harder. Here is a derivation. Hope I got it right.
1313 ;; diff(integrate(1/sqrt(1-m*sin(x)^2),x,0,phi), m);
1318 ;; I ------------------ dx
1320 ;; / (1 - m SIN (x))
1322 ;; --------------------------
1326 ;; Now use the following relationship that is easily verified:
1329 ;; (1 - m) SIN (x) COS (x) COS(x) SIN(x)
1330 ;; ------------------- = ------------------- - DIFF(-------------------, x)
1332 ;; SQRT(1 - m SIN (x)) SQRT(1 - m SIN (x)) SQRT(1 - m SIN (x))
1335 ;; Now integrate this to get:
1341 ;; (1 - m) I ------------------- dx =
1343 ;; / SQRT(1 - m SIN (x))
1350 ;; + I ------------------- dx
1352 ;; / SQRT(1 - m SIN (x))
1354 ;; COS(PHI) SIN(PHI)
1355 ;; - ---------------------
1357 ;; SQRT(1 - m SIN (PHI))
1359 ;; Use the fact that cos(x)^2 = 1 - sin(x)^2 to show that this
1360 ;; integral on the RHS is:
1363 ;; (1 - m) elliptic_F(PHI, m) + elliptic_E(PHI, m)
1364 ;; -------------------------------------------
1366 ;; So, finally, we have
1371 ;; 2 -- (elliptic_F(PHI, m)) =
1374 ;; elliptic_E(PHI, m) - (1 - m) elliptic_F(PHI, m) COS(PHI) SIN(PHI)
1375 ;; ---------------------------------------------- - ---------------------
1377 ;; SQRT(1 - m SIN (PHI))
1378 ;; ----------------------------------------------------------------------
1381 (defprop %elliptic_f
1384 ;; 1/sqrt(1-m*sin(phi)^2)
1386 ((mplus simp
) 1 ((mtimes simp
) -
1 m
((mexpt simp
) ((%sin simp
) phi
) 2)))
1389 ((mtimes simp
) ((rat simp
) 1 2)
1390 ((mexpt simp
) ((mplus simp
) 1 ((mtimes simp
) -
1 m
)) -
1)
1392 ((mtimes simp
) ((mexpt simp
) m -
1)
1393 ((mplus simp
) ((%elliptic_e simp
) phi m
)
1394 ((mtimes simp
) -
1 ((mplus simp
) 1 ((mtimes simp
) -
1 m
))
1395 ((%elliptic_f simp
) phi m
))))
1396 ((mtimes simp
) -
1 ((%cos simp
) phi
) ((%sin simp
) phi
)
1399 ((mtimes simp
) -
1 m
((mexpt simp
) ((%sin simp
) phi
) 2)))
1400 ((rat simp
) -
1 2))))))
1404 ;; The derivative of E(phi|m) wrt to m is much simpler to derive than F(phi|m).
1406 ;; Take the derivative of the definition to get
1411 ;; I ------------------- dx
1413 ;; / SQRT(1 - m SIN (x))
1415 ;; - ---------------------------
1418 ;; It is easy to see that
1423 ;; elliptic_F(PHI, m) - m I ------------------- dx = elliptic_E(PHI, m)
1425 ;; / SQRT(1 - m SIN (x))
1428 ;; So we finally have
1430 ;; d elliptic_E(PHI, m) - elliptic_F(PHI, m)
1431 ;; -- (elliptic_E(PHI, m)) = ---------------------------------------
1434 (defprop %elliptic_e
1436 ;; sqrt(1-m*sin(phi)^2)
1438 ((mplus simp
) 1 ((mtimes simp
) -
1 m
((mexpt simp
) ((%sin simp
) phi
) 2)))
1441 ((mtimes simp
) ((rat simp
) 1 2) ((mexpt simp
) m -
1)
1442 ((mplus simp
) ((%elliptic_e simp
) phi m
)
1443 ((mtimes simp
) -
1 ((%elliptic_f simp
) phi m
)))))
1446 (def-simplifier elliptic_f
(phi m
)
1448 (cond ((float-numerical-eval-p phi m
)
1449 ;; Numerically evaluate it
1450 (to (elliptic-f ($float phi
) ($float m
))))
1451 ((setf args
(complex-float-numerical-eval-p phi m
))
1452 (destructuring-bind (phi m
)
1454 (to (elliptic-f (bigfloat:to
($float phi
))
1455 (bigfloat:to
($float m
))))))
1456 ((bigfloat-numerical-eval-p phi m
)
1457 (to (bigfloat::bf-elliptic-f
(bigfloat:to
($bfloat phi
))
1458 (bigfloat:to
($bfloat m
)))))
1459 ((setf args
(complex-bigfloat-numerical-eval-p phi m
))
1460 (destructuring-bind (phi m
)
1462 (to (bigfloat::bf-elliptic-f
(bigfloat:to
($bfloat phi
))
1463 (bigfloat:to
($bfloat m
))))))
1470 ;; A&S 17.4.21. Let's pick the log tan form. But this
1471 ;; isn't right if we know that abs(phi) > %pi/2, where
1472 ;; elliptic_f is undefined (or infinity).
1473 (cond ((not (eq '$pos
(csign (sub ($abs phi
) (div '$%pi
2)))))
1476 (add (mul '$%pi
(div 1 4))
1479 (merror (intl:gettext
"elliptic_f: elliptic_f(~:M, ~:M) is undefined.")
1481 ((alike1 phi
'((mtimes) ((rat) 1 2) $%pi
))
1482 ;; Complete elliptic integral
1483 (ftake '%elliptic_kc m
))
1488 (def-simplifier elliptic_e
(phi m
)
1490 (cond ((float-numerical-eval-p phi m
)
1491 ;; Numerically evaluate it
1492 (elliptic-e ($float phi
) ($float m
)))
1493 ((complex-float-numerical-eval-p phi m
)
1494 (complexify (bigfloat::bf-elliptic-e
(complex ($float
($realpart phi
)) ($float
($imagpart phi
)))
1495 (complex ($float
($realpart m
)) ($float
($imagpart m
))))))
1496 ((bigfloat-numerical-eval-p phi m
)
1497 (to (bigfloat::bf-elliptic-e
(bigfloat:to
($bfloat phi
))
1498 (bigfloat:to
($bfloat m
)))))
1499 ((setf args
(complex-bigfloat-numerical-eval-p phi m
))
1500 (destructuring-bind (phi m
)
1502 (to (bigfloat::bf-elliptic-e
(bigfloat:to
($bfloat phi
))
1503 (bigfloat:to
($bfloat m
))))))
1510 ;; A&S 17.4.25, but handle periodicity:
1511 ;; elliptic_e(x,m) = elliptic_e(x-%pi*round(x/%pi), m)
1512 ;; + 2*round(x/%pi)*elliptic_ec(m)
1516 ;; elliptic_e(x,1) = sin(x-%pi*round(x/%pi)) + 2*round(x/%pi)*elliptic_ec(m)
1518 (let ((mult-pi (ftake '%round
(div phi
'$%pi
))))
1519 (add (ftake '%sin
(sub phi
1524 (ftake '%elliptic_ec m
))))))
1525 ((alike1 phi
'((mtimes) ((rat) 1 2) $%pi
))
1526 ;; Complete elliptic integral
1527 (ftake '%elliptic_ec m
))
1528 ((and ($numberp phi
)
1529 (let ((r ($round
(div phi
'$%pi
))))
1532 ;; Handle the case where phi is a number where we can apply
1533 ;; the periodicity property without blowing up the
1535 (add (ftake '%elliptic_e
1538 (ftake '%round
(div phi
'$%pi
))))
1541 (mul (ftake '%round
(div phi
'$%pi
))
1542 (ftake '%elliptic_ec m
)))))
1547 ;; Complete elliptic integrals
1549 ;; elliptic_kc(m) = elliptic_f(%pi/2, m)
1551 ;; elliptic_ec(m) = elliptic_e(%pi/2, m)
1554 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1556 ;;; We support a simplim%function. The function is looked up in simplimit and
1557 ;;; handles specific values of the function.
1559 (defprop %elliptic_kc simplim%elliptic_kc simplim%function
)
1561 (defun simplim%elliptic_kc
(expr var val
)
1562 ;; Look for the limit of the argument
1563 (let ((m (limit (cadr expr
) var val
'think
)))
1565 ;; For an argument 1 return $infinity.
1568 ;; All other cases are handled by the simplifier of the function.
1569 (simplify (list '(%elliptic_kc
) m
))))))
1571 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1573 (def-simplifier elliptic_kc
(m)
1576 ;; elliptic_kc(1) is complex infinity. Maxima can not handle
1577 ;; infinities correctly, throw a Maxima error.
1579 (intl:gettext
"elliptic_kc: elliptic_kc(~:M) is undefined.")
1581 ((float-numerical-eval-p m
)
1582 ;; Numerically evaluate it
1583 (to (elliptic-k ($float m
))))
1584 ((complex-float-numerical-eval-p m
)
1585 (complexify (bigfloat::bf-elliptic-k
(complex ($float
($realpart m
)) ($float
($imagpart m
))))))
1586 ((setf args
(complex-bigfloat-numerical-eval-p m
))
1587 (destructuring-bind (m)
1589 (to (bigfloat::bf-elliptic-k
(bigfloat:to
($bfloat m
))))))
1591 '((mtimes) ((rat) 1 2) $%pi
))
1593 ;; http://functions.wolfram.com/EllipticIntegrals/EllipticK/03/01/
1595 ;; elliptic_kc(1/2) = 8*%pi^(3/2)/gamma(-1/4)^2
1596 (div (mul 8 (power '$%pi
(div 3 2)))
1597 (power (gm (div -
1 4)) 2)))
1599 ;; elliptic_kc(-1) = gamma(1/4)^2/(4*sqrt(2*%pi))
1600 (div (power (gm (div 1 4)) 2)
1601 (mul 4 (power (mul 2 '$%pi
) 1//2))))
1602 ((alike1 m
(add 17 (mul -
12 (power 2 1//2))))
1603 ;; elliptic_kc(17-12*sqrt(2)) = 2*(2+sqrt(2))*%pi^(3/2)/gamma(-1/4)^2
1604 (div (mul 2 (mul (add 2 (power 2 1//2))
1605 (power '$%pi
(div 3 2))))
1606 (power (gm (div -
1 4)) 2)))
1611 (defprop %elliptic_kc
1616 ((mplus) ((%elliptic_ec
) m
)
1619 ((mplus) 1 ((mtimes) -
1 m
))))
1620 ((mexpt) ((mplus) 1 ((mtimes) -
1 m
)) -
1)
1624 (def-simplifier elliptic_ec
(m)
1626 (cond ((float-numerical-eval-p m
)
1627 ;; Numerically evaluate it
1628 (elliptic-ec ($float m
)))
1629 ((setf args
(complex-float-numerical-eval-p m
))
1630 (destructuring-bind (m)
1632 (complexify (bigfloat::bf-elliptic-ec
(bigfloat:to
($float m
))))))
1633 ((setf args
(complex-bigfloat-numerical-eval-p m
))
1634 (destructuring-bind (m)
1636 (to (bigfloat::bf-elliptic-ec
(bigfloat:to
($bfloat m
))))))
1637 ;; Some special cases we know about.
1639 '((mtimes) ((rat) 1 2) $%pi
))
1643 ;; elliptic_ec(1/2). Use the identity
1645 ;; elliptic_ec(z)*elliptic_kc(1-z) - elliptic_kc(z)*elliptic_kc(1-z)
1646 ;; + elliptic_ec(1-z)*elliptic_kc(z) = %pi/2;
1648 ;; Let z = 1/2 to get
1650 ;; %pi^(3/2)*'elliptic_ec(1/2)/gamma(3/4)^2-%pi^3/(4*gamma(3/4)^4) = %pi/2
1652 ;; since we know that elliptic_kc(1/2) = %pi^(3/2)/(2*gamma(3/4)^2). Hence
1655 ;; = (2*%pi*gamma(3/4)^4+%pi^3)/(4*%pi^(3/2)*gamma(3/4)^2)
1656 ;; = gamma(3/4)^2/(2*sqrt(%pi))+%pi^(3/2)/(4*gamma(3/4)^2)
1658 (add (div (power (ftake '%gamma
(div 3 4)) 2)
1659 (mul 2 (power '$%pi
1//2)))
1660 (div (power '$%pi
(div 3 2))
1661 (mul 4 (power (ftake '%gamma
(div 3 4)) 2)))))
1663 ;; elliptic_ec(-1). Use the identity
1664 ;; http://functions.wolfram.com/08.01.17.0002.01
1667 ;; elliptic_ec(z) = sqrt(1 - z)*elliptic_ec(z/(z-1))
1669 ;; Let z = -1 to get
1671 ;; elliptic_ec(-1) = sqrt(2)*elliptic_ec(1/2)
1673 ;; Should we expand out elliptic_ec(1/2) using the above result?
1675 (ftake '%elliptic_ec
1//2)))
1680 (defprop %elliptic_ec
1682 ((mtimes) ((rat) 1 2)
1683 ((mplus) ((%elliptic_ec
) m
)
1684 ((mtimes) -
1 ((%elliptic_kc
)
1690 ;; Elliptic integral of the third kind:
1697 ;; PI(n;phi|m) = I ----------------------------------- ds
1699 ;; / SQRT(1 - m SIN (s)) (1 - n SIN (s))
1702 ;; As with E and F, we do not use the modular angle alpha but the
1703 ;; parameter m = sin(alpha)^2.
1705 (def-simplifier elliptic_pi
(n phi m
)
1708 ((float-numerical-eval-p n phi m
)
1709 ;; Numerically evaluate it
1710 (elliptic-pi ($float n
) ($float phi
) ($float m
)))
1711 ((setf args
(complex-float-numerical-eval-p n phi m
))
1712 (destructuring-bind (n phi m
)
1714 (elliptic-pi (bigfloat:to
($float n
))
1715 (bigfloat:to
($float phi
))
1716 (bigfloat:to
($float m
)))))
1717 ((bigfloat-numerical-eval-p n phi m
)
1718 (to (bigfloat::bf-elliptic-pi
(bigfloat:to n
)
1721 ((setq args
(complex-bigfloat-numerical-eval-p n phi m
))
1722 (destructuring-bind (n phi m
)
1724 (to (bigfloat::bf-elliptic-pi
(bigfloat:to n
)
1728 (ftake '%elliptic_f phi m
))
1730 ;; 3 cases depending on n < 1, n > 1, or n = 1.
1731 (let ((s (asksign (add -
1 n
))))
1734 (div (ftake '%atanh
(mul (power (add n -
1) 1//2)
1736 (power (add n -
1) 1//2)))
1738 (div (ftake '%atan
(mul (power (sub 1 n
) 1//2)
1740 (power (sub 1 n
) 1//2)))
1742 (ftake '%tan phi
)))))
1747 ;; Complete elliptic-pi. That is phi = %pi/2. Then
1749 ;; = Rf(0, 1-m,1) + Rj(0,1-m,1-n)*n/3;
1750 (defun elliptic-pi-complete (n m
)
1751 (to (bigfloat:+ (bigfloat::bf-rf
0 (- 1 m
) 1)
1752 (bigfloat:* 1/3 n
(bigfloat::bf-rj
0 (- 1 m
) 1 (- 1 n
))))))
1754 ;; To compute elliptic_pi for all z, we use the property
1755 ;; (http://functions.wolfram.com/08.06.16.0002.01)
1757 ;; elliptic_pi(n, z + %pi*k, m)
1758 ;; = 2*k*elliptic_pi(n, %pi/2, m) + elliptic_pi(n, z, m)
1760 ;; So we are left with computing the integral for 0 <= z < %pi. Using
1761 ;; Carlson's formulation produces the wrong values for %pi/2 < z <
1762 ;; %pi. How to do that?
1766 ;; I(a,b) = integrate(1/(1-n*sin(x)^2)/sqrt(1 - m*sin(x)^2), x, a, b)
1768 ;; That is, I(a,b) is the integral for the elliptic_pi function but
1769 ;; with a lower limit of a and an upper limit of b.
1771 ;; Then, we want to compute I(0, z), with %pi <= z < %pi. Let w = z +
1772 ;; %pi/2, 0 <= w < %pi/2. Then
1774 ;; I(0, w+%pi/2) = I(0, %pi/2) + I(%pi/2, w+%pi/2)
1776 ;; To evaluate I(%pi/2, w+%pi/2), use a change of variables:
1778 ;; changevar('integrate(1/(1-n*sin(x)^2)/sqrt(1 - m*sin(x)^2), x, %pi/2, w + %pi/2),
1781 ;; = integrate(-1/(sqrt(1-m*sin(u)^2)*(1-n*sin(u)^2)),u,%pi/2-w,%pi/2)
1782 ;; = I(%pi/2-w,%pi/2)
1783 ;; = I(0,%pi/2) - I(0,%pi/2-w)
1787 ;; I(0,%pi/2+w) = 2*I(0,%pi/2) - I(0,%pi/2-w)
1789 ;; This allows us to compute the general result with 0 <= z < %pi
1791 ;; I(0, k*%pi + z) = 2*k*I(0,%pi/2) + I(0,z);
1793 ;; If 0 <= z < %pi/2, then the we are done. If %pi/2 <= z < %pi, let
1794 ;; z = w+%pi/2. Then
1796 ;; I(0,z) = 2*I(0,%pi/2) - I(0,%pi/2-w)
1798 ;; Or, since w = z-%pi/2:
1800 ;; I(0,z) = 2*I(0,%pi/2) - I(0,%pi-z)
1802 (defun elliptic-pi (n phi m
)
1803 ;; elliptic_pi(n, -phi, m) = -elliptic_pi(n, phi, m). That is, it
1804 ;; is an odd function of phi.
1805 (when (minusp (realpart phi
))
1806 (return-from elliptic-pi
(- (elliptic-pi n
(- phi
) m
))))
1808 ;; Note: Carlson's DRJ has n defined as the negative of the n given
1810 (flet ((base (n phi m
)
1811 ;; elliptic_pi(n,phi,m) =
1812 ;; sin(phi)*Rf(cos(phi)^2, 1-m*sin(phi)^2, 1)
1813 ;; - (-n / 3) * sin(phi)^3
1814 ;; * Rj(cos(phi)^2, 1-m*sin(phi)^2, 1, 1-n*sin(phi)^2)
1819 (k2sin (* (- 1 (* k sin-phi
))
1820 (+ 1 (* k sin-phi
)))))
1821 (- (* sin-phi
(bigfloat::bf-rf
(expt cos-phi
2) k2sin
1.0))
1822 (* (/ nn
3) (expt sin-phi
3)
1823 (bigfloat::bf-rj
(expt cos-phi
2) k2sin
1.0
1824 (- 1 (* n
(expt sin-phi
2)))))))))
1825 ;; FIXME: Reducing the arg by pi has significant round-off.
1826 ;; Consider doing something better.
1827 (let* ((cycles (round (realpart phi
) pi
))
1828 (rem (- phi
(* cycles pi
))))
1829 (let ((complete (elliptic-pi-complete n m
)))
1830 (to (+ (* 2 cycles complete
)
1831 (base n rem m
)))))))
1833 ;;; Deriviatives from functions.wolfram.com
1834 ;;; http://functions.wolfram.com/EllipticIntegrals/EllipticPi3/20/
1835 (defprop %elliptic_pi
1837 ;Derivative wrt first argument
1838 ((mtimes) ((rat) 1 2)
1839 ((mexpt) ((mplus) m
((mtimes) -
1 n
)) -
1)
1840 ((mexpt) ((mplus) -
1 n
) -
1)
1842 ((mtimes) ((mexpt) n -
1)
1843 ((mplus) ((mtimes) -
1 m
) ((mexpt) n
2))
1844 ((%elliptic_pi
) n z m
))
1846 ((mtimes) ((mplus) m
((mtimes) -
1 n
)) ((mexpt) n -
1)
1847 ((%elliptic_f
) z m
))
1848 ((mtimes) ((rat) -
1 2) n
1850 ((mplus) 1 ((mtimes) -
1 m
((mexpt) ((%sin
) z
) 2)))
1853 ((mplus) 1 ((mtimes) -
1 n
((mexpt) ((%sin
) z
) 2)))
1855 ((%sin
) ((mtimes) 2 z
)))))
1856 ;derivative wrt second argument
1859 ((mplus) 1 ((mtimes) -
1 m
((mexpt) ((%sin
) z
) 2)))
1862 ((mplus) 1 ((mtimes) -
1 n
((mexpt) ((%sin
) z
) 2))) -
1))
1863 ;Derivative wrt third argument
1864 ((mtimes) ((rat) 1 2)
1865 ((mexpt) ((mplus) ((mtimes) -
1 m
) n
) -
1)
1866 ((mplus) ((%elliptic_pi
) n z m
)
1867 ((mtimes) ((mexpt) ((mplus) -
1 m
) -
1)
1868 ((%elliptic_e
) z m
))
1869 ((mtimes) ((rat) -
1 2) ((mexpt) ((mplus) -
1 m
) -
1) m
1871 ((mplus) 1 ((mtimes) -
1 m
((mexpt) ((%sin
) z
) 2)))
1873 ((%sin
) ((mtimes) 2 z
))))))
1876 (in-package #-gcl
#:bigfloat
#+gcl
"BIGFLOAT")
1877 ;; Translation of Jim FitzSimons' bigfloat implementation of elliptic
1878 ;; integrals from http://www.getnet.com/~cherry/elliptbf3.mac.
1880 ;; The algorithms are based on B.C. Carlson's "Numerical Computation
1881 ;; of Real or Complex Elliptic Integrals". These are updated to the
1882 ;; algorithms in Journal of Computational and Applied Mathematics 118
1883 ;; (2000) 71-85 "Reduction Theorems for Elliptic Integrands with the
1884 ;; Square Root of two quadritic factors"
1887 ;; NOTE: Despite the names indicating these are for bigfloat numbers,
1888 ;; the algorithms and routines are generic and will work with floats
1891 (defun bferrtol (&rest args
)
1892 ;; Compute error tolerance as sqrt(2^(-fpprec)). Not sure this is
1893 ;; quite right, but it makes the routines more accurate as fpprec
1895 (sqrt (reduce #'min
(mapcar #'(lambda (x)
1896 (if (rationalp (realpart x
))
1897 maxima
::flonum-epsilon
1901 ;; rc(x,y) = integrate(1/2*(t+x)^(-1/2)/(t+y), t, 0, inf)
1903 ;; log(x) = (x-1)*rc(((1+x)/2)^2, x), x > 0
1904 ;; asin(x) = x * rc(1-x^2, 1), |x|<= 1
1905 ;; acos(x) = sqrt(1-x^2)*rc(x^2,1), 0 <= x <=1
1906 ;; atan(x) = x * rc(1,1+x^2)
1907 ;; asinh(x) = x * rc(1+x^2,1)
1908 ;; acosh(x) = sqrt(x^2-1) * rc(x^2,1), x >= 1
1909 ;; atanh(x) = x * rc(1,1-x^2), |x|<=1
1913 xn z w a an pwr4 n epslon lambda sn s
)
1914 (cond ((and (zerop (imagpart yn
))
1915 (minusp (realpart yn
)))
1919 (setf w
(sqrt (/ x xn
))))
1924 (setf a
(/ (+ xn yn yn
) 3))
1925 (setf epslon
(/ (abs (- a xn
)) (bferrtol x y
)))
1929 (loop while
(> (* epslon pwr4
) (abs an
))
1931 (setf pwr4
(/ pwr4
4))
1932 (setf lambda
(+ (* 2 (sqrt xn
) (sqrt yn
)) yn
))
1933 (setf an
(/ (+ an lambda
) 4))
1934 (setf xn
(/ (+ xn lambda
) 4))
1935 (setf yn
(/ (+ yn lambda
) 4))
1937 ;; c2=3/10,c3=1/7,c4=3/8,c5=9/22,c6=159/208,c7=9/8
1938 (setf sn
(/ (* pwr4
(- z a
)) an
))
1939 (setf s
(* sn sn
(+ 3/10
1944 (* sn
9/8))))))))))))
1950 ;; See https://dlmf.nist.gov/19.16.E5:
1952 ;; rd(x,y,z) = integrate(3/2/sqrt(t+x)/sqrt(t+y)/sqrt(t+z)/(t+z), t, 0, inf)
1955 ;; E(K) = rf(0, 1-K^2, 1) - (K^2/3)*rd(0,1-K^2,1)
1957 ;; B = integrate(s^2/sqrt(1-s^4), s, 0 ,1)
1958 ;; = beta(3/4,1/2)/4
1959 ;; = sqrt(%pi)*gamma(3/4)/gamma(1/4)
1962 (defun bf-rd (x y z
)
1966 (a (/ (+ xn yn
(* 3 zn
)) 5))
1967 (epslon (/ (max (abs (- a xn
))
1975 xnroot ynroot znroot lam
)
1976 (loop while
(> (* power4 epslon
) (abs an
))
1978 (setf xnroot
(sqrt xn
))
1979 (setf ynroot
(sqrt yn
))
1980 (setf znroot
(sqrt zn
))
1981 (setf lam
(+ (* xnroot ynroot
)
1984 (setf sigma
(+ sigma
(/ power4
1985 (* znroot
(+ zn lam
)))))
1986 (setf power4
(* power4
1/4))
1987 (setf xn
(* (+ xn lam
) 1/4))
1988 (setf yn
(* (+ yn lam
) 1/4))
1989 (setf zn
(* (+ zn lam
) 1/4))
1990 (setf an
(* (+ an lam
) 1/4))
1992 ;; c1=-3/14,c2=1/6,c3=9/88,c4=9/22,c5=-3/22,c6=-9/52,c7=3/26
1993 (let* ((xndev (/ (* (- a x
) power4
) an
))
1994 (yndev (/ (* (- a y
) power4
) an
))
1995 (zndev (- (* (+ xndev yndev
) 1/3)))
1996 (ee2 (- (* xndev yndev
) (* 6 zndev zndev
)))
1997 (ee3 (* (- (* 3 xndev yndev
)
2000 (ee4 (* 3 (- (* xndev yndev
) (* zndev zndev
)) zndev zndev
))
2001 (ee5 (* xndev yndev zndev zndev zndev
))
2009 (* -
1/16 ee2 ee2 ee2
)
2012 (* 45/272 ee2 ee2 ee3
)
2013 (* -
9/68 (+ (* ee2 ee5
) (* ee3 ee4
))))))
2018 ;; See https://dlmf.nist.gov/19.16.E1
2020 ;; rf(x,y,z) = 1/2*integrate(1/(sqrt(t+x)*sqrt(t+y)*sqrt(t+z)), t, 0, inf);
2023 (defun bf-rf (x y z
)
2027 (a (/ (+ xn yn zn
) 3))
2028 (epslon (/ (max (abs (- a xn
))
2035 xnroot ynroot znroot lam
)
2036 (loop while
(> (* power4 epslon
) (abs an
))
2038 (setf xnroot
(sqrt xn
))
2039 (setf ynroot
(sqrt yn
))
2040 (setf znroot
(sqrt zn
))
2041 (setf lam
(+ (* xnroot ynroot
)
2044 (setf power4
(* power4
1/4))
2045 (setf xn
(* (+ xn lam
) 1/4))
2046 (setf yn
(* (+ yn lam
) 1/4))
2047 (setf zn
(* (+ zn lam
) 1/4))
2048 (setf an
(* (+ an lam
) 1/4))
2050 ;; c1=-3/14,c2=1/6,c3=9/88,c4=9/22,c5=-3/22,c6=-9/52,c7=3/26
2051 (let* ((xndev (/ (* (- a x
) power4
) an
))
2052 (yndev (/ (* (- a y
) power4
) an
))
2053 (zndev (- (+ xndev yndev
)))
2054 (ee2 (- (* xndev yndev
) (* 6 zndev zndev
)))
2055 (ee3 (* xndev yndev zndev
))
2060 (* -
3/44 ee2 ee3
))))
2063 (defun bf-rj1 (x y z p
)
2074 (a (/ (+ xn yn zn pn pn
) 5))
2075 (epslon (/ (max (abs (- a xn
))
2079 (bferrtol x y z p
)))
2081 xnroot ynroot znroot pnroot lam dn
)
2082 (loop while
(> (* power4 epslon
) (abs an
))
2084 (setf xnroot
(sqrt xn
))
2085 (setf ynroot
(sqrt yn
))
2086 (setf znroot
(sqrt zn
))
2087 (setf pnroot
(sqrt pn
))
2088 (setf lam
(+ (* xnroot ynroot
)
2091 (setf dn
(* (+ pnroot xnroot
)
2094 (setf sigma
(+ sigma
2096 (bf-rc 1 (+ 1 (/ en
(* dn dn
)))))
2098 (setf power4
(* power4
1/4))
2100 (setf xn
(* (+ xn lam
) 1/4))
2101 (setf yn
(* (+ yn lam
) 1/4))
2102 (setf zn
(* (+ zn lam
) 1/4))
2103 (setf pn
(* (+ pn lam
) 1/4))
2104 (setf an
(* (+ an lam
) 1/4))
2106 (let* ((xndev (/ (* (- a x
) power4
) an
))
2107 (yndev (/ (* (- a y
) power4
) an
))
2108 (zndev (/ (* (- a z
) power4
) an
))
2109 (pndev (* -
0.5 (+ xndev yndev zndev
)))
2110 (ee2 (+ (* xndev yndev
)
2113 (* -
3 pndev pndev
)))
2114 (ee3 (+ (* xndev yndev zndev
)
2116 (* 4 pndev pndev pndev
)))
2117 (ee4 (* (+ (* 2 xndev yndev zndev
)
2119 (* 3 pndev pndev pndev
))
2121 (ee5 (* xndev yndev zndev pndev pndev
))
2129 (* -
1/16 ee2 ee2 ee2
)
2132 (* 45/272 ee2 ee2 ee3
)
2133 (* -
9/68 (+ (* ee2 ee5
) (* ee3 ee4
))))))
2136 (sqrt (* an an an
)))))))
2138 (defun bf-rj (x y z p
)
2143 (cond ((and (and (zerop (imagpart xn
)) (>= (realpart xn
) 0))
2144 (and (zerop (imagpart yn
)) (>= (realpart yn
) 0))
2145 (and (zerop (imagpart zn
)) (>= (realpart zn
) 0))
2146 (and (zerop (imagpart qn
)) (> (realpart qn
) 0)))
2147 (destructuring-bind (xn yn zn
)
2148 (sort (list xn yn zn
) #'<)
2149 (let* ((pn (+ yn
(* (- zn yn
) (/ (- yn xn
) (+ yn qn
)))))
2150 (s (- (* (- pn yn
) (bf-rj1 xn yn zn pn
))
2151 (* 3 (bf-rf xn yn zn
)))))
2152 (setf s
(+ s
(* 3 (sqrt (/ (* xn yn zn
)
2153 (+ (* xn zn
) (* pn qn
))))
2154 (bf-rc (+ (* xn zn
) (* pn qn
)) (* pn qn
)))))
2157 (bf-rj1 x y z p
)))))
2159 (defun bf-rg (x y z
)
2161 (+ (* z
(bf-rf x y z
))
2166 (sqrt (/ (* x y
) z
)))))
2168 ;; elliptic_f(phi,m) = sin(phi)*rf(cos(phi)^2, 1-m*sin(phi)^2,1)
2169 (defun bf-elliptic-f (phi m
)
2170 (flet ((base (phi m
)
2172 ;; F(z|1) = log(tan(z/2+%pi/4))
2173 (log (tan (+ (/ phi
2) (/ (%pi phi
) 4)))))
2177 (* s
(bf-rf (* c c
) (- 1 (* m s s
)) 1)))))))
2178 ;; Handle periodicity (see elliptic-f)
2179 (let* ((bfpi (%pi phi
))
2180 (period (round (realpart phi
) bfpi
)))
2181 (+ (base (- phi
(* bfpi period
)) m
)
2184 (* 2 period
(bf-elliptic-k m
)))))))
2186 ;; elliptic_kc(k) = rf(0, 1-k^2,1)
2189 ;; elliptic_kc(m) = rf(0, 1-m,1)
2191 (defun bf-elliptic-k (m)
2193 (if (maxima::$bfloatp m
)
2194 (maxima::$bfloat
(maxima::div
'maxima
::$%pi
2))
2195 (float (/ pi
2) 1e0
)))
2198 (intl:gettext
"elliptic_kc: elliptic_kc(1) is undefined.")))
2200 (bf-rf 0 (- 1 m
) 1))))
2202 ;; elliptic_e(phi, k) = sin(phi)*rf(cos(phi)^2,1-k^2*sin(phi)^2,1)
2203 ;; - (k^2/3)*sin(phi)^3*rd(cos(phi)^2, 1-k^2*sin(phi)^2,1)
2207 ;; elliptic_e(phi, m) = sin(phi)*rf(cos(phi)^2,1-m*sin(phi)^2,1)
2208 ;; - (m/3)*sin(phi)^3*rd(cos(phi)^2, 1-m*sin(phi)^2,1)
2210 (defun bf-elliptic-e (phi m
)
2211 (flet ((base (phi m
)
2212 (let* ((s (sin phi
))
2215 (s2 (- 1 (* m s s
))))
2216 (- (* s
(bf-rf c2 s2
1))
2217 (* (/ m
3) (* s s s
) (bf-rd c2 s2
1))))))
2218 ;; Elliptic E is quasi-periodic wrt to phi:
2220 ;; E(z|m) = E(z - %pi*round(Re(z)/%pi)|m) + 2*round(Re(z)/%pi)*E(m)
2221 (let* ((bfpi (%pi phi
))
2222 (period (round (realpart phi
) bfpi
)))
2223 (+ (base (- phi
(* bfpi period
)) m
)
2224 (* 2 period
(bf-elliptic-ec m
))))))
2227 ;; elliptic_ec(k) = rf(0,1-k^2,1) - (k^2/3)*rd(0,1-k^2,1);
2230 ;; elliptic_ec(m) = rf(0,1-m,1) - (m/3)*rd(0,1-m,1);
2232 (defun bf-elliptic-ec (m)
2234 (if (typep m
'bigfloat
)
2235 (bigfloat (maxima::$bfloat
(maxima::div
'maxima
::$%pi
2)))
2236 (float (/ pi
2) 1e0
)))
2238 (if (typep m
'bigfloat
)
2244 (* m
1/3 (bf-rd 0 m1
1)))))))
2246 (defun bf-elliptic-pi-complete (n m
)
2247 (+ (bf-rf 0 (- 1 m
) 1)
2248 (* 1/3 n
(bf-rj 0 (- 1 m
) 1 (- 1 n
)))))
2250 (defun bf-elliptic-pi (n phi m
)
2251 ;; Note: Carlson's DRJ has n defined as the negative of the n given
2253 (flet ((base (n phi m
)
2258 (k2sin (* (- 1 (* k sin-phi
))
2259 (+ 1 (* k sin-phi
)))))
2260 (- (* sin-phi
(bf-rf (expt cos-phi
2) k2sin
1.0))
2261 (* (/ nn
3) (expt sin-phi
3)
2262 (bf-rj (expt cos-phi
2) k2sin
1.0
2263 (- 1 (* n
(expt sin-phi
2)))))))))
2264 ;; FIXME: Reducing the arg by pi has significant round-off.
2265 ;; Consider doing something better.
2266 (let* ((bf-pi (%pi
(realpart phi
)))
2267 (cycles (round (realpart phi
) bf-pi
))
2268 (rem (- phi
(* cycles bf-pi
))))
2269 (let ((complete (bf-elliptic-pi-complete n m
)))
2270 (+ (* 2 cycles complete
)
2273 ;; Compute inverse_jacobi_sn, for float or bigfloat args.
2274 (defun bf-inverse-jacobi-sn (u m
)
2275 (* u
(bf-rf (- 1 (* u u
))
2279 ;; Compute inverse_jacobi_dn. We use the following identity
2280 ;; from Gradshteyn & Ryzhik, 8.153.6
2282 ;; w = dn(z|m) = cn(sqrt(m)*z, 1/m)
2284 ;; Solve for z to get
2286 ;; z = inverse_jacobi_dn(w,m)
2287 ;; = 1/sqrt(m) * inverse_jacobi_cn(w, 1/m)
2288 (defun bf-inverse-jacobi-dn (w m
)
2292 ;; jacobi_dn(x,1) = sech(x) so the inverse is asech(x)
2293 (maxima::take
'(maxima::%asech
) (maxima::to w
)))
2295 ;; We should do something better to make sure that things
2296 ;; that should be real are real.
2297 (/ (to (maxima::take
'(maxima::%inverse_jacobi_cn
)
2299 (maxima::to
(/ m
))))
2302 (in-package :maxima
)
2304 ;; Define Carlson's elliptic integrals.
2306 (def-simplifier carlson_rc
(x y
)
2309 (flet ((floatify (z)
2310 ;; If z is a complex rational, convert to a
2311 ;; complex double-float. Otherwise, leave it as
2312 ;; is. If we don't do this, %i is handled as
2313 ;; #c(0 1), which makes bf-rc use single-float
2314 ;; arithmetic instead of the desired
2316 (if (and (complexp z
) (rationalp (realpart z
)))
2317 (complex (float (realpart z
))
2318 (float (imagpart z
)))
2320 (to (bigfloat::bf-rc
(floatify (bigfloat:to x
))
2321 (floatify (bigfloat:to y
)))))))
2322 ;; See comments from bf-rc
2323 (cond ((float-numerical-eval-p x y
)
2324 (calc ($float x
) ($float y
)))
2325 ((bigfloat-numerical-eval-p x y
)
2326 (calc ($bfloat x
) ($bfloat y
)))
2327 ((setf args
(complex-float-numerical-eval-p x y
))
2328 (destructuring-bind (x y
)
2330 (calc ($float x
) ($float y
))))
2331 ((setf args
(complex-bigfloat-numerical-eval-p x y
))
2332 (destructuring-bind (x y
)
2334 (calc ($bfloat x
) ($bfloat y
))))
2340 (alike1 y
(div 1 4)))
2345 ;; rc(2,1) = 1/2*integrate(1/sqrt(t+2)/(t+1), t, 0, inf)
2346 ;; = (log(sqrt(2)+1)-log(sqrt(2)-1))/2
2347 ;; ratsimp(logcontract(%)),algebraic:
2348 ;; = -log(3-2^(3/2))/2
2349 ;; = -log(sqrt(3-2^(3/2)))
2350 ;; = -log(sqrt(2)-1)
2351 ;; = log(1/(sqrt(2)-1))
2352 ;; ratsimp(%),algebraic;
2354 (ftake '%log
(add 1 (power 2 1//2))))
2355 ((and (alike x
'$%i
)
2356 (alike y
(add 1 '$%i
)))
2357 ;; rc(%i, %i+1) = 1/2*integrate(1/sqrt(t+%i)/(t+%i+1), t, 0, inf)
2358 ;; = %pi/2-atan((-1)^(1/4))
2359 ;; ratsimp(logcontract(ratsimp(rectform(%o42)))),algebraic;
2360 ;; = (%i*log(3-2^(3/2))+%pi)/4
2361 ;; = (%i*log(3-2^(3/2)))/4+%pi/4
2362 ;; = %i*log(sqrt(3-2^(3/2)))/2+%pi/4
2364 ;; = %pi/4 + %i*log(sqrt(2)-1)/2
2368 (ftake '%log
(sub (power 2 1//2) 1)))))
2371 ;; rc(0,%i) = 1/2*integrate(1/(sqrt(t)*(t+%i)), t, 0, inf)
2372 ;; = -((sqrt(2)*%i-sqrt(2))*%pi)/4
2373 ;; = ((1-%i)*%pi)/2^(3/2)
2374 (div (mul (sub 1 '$%i
)
2378 (eq ($sign
($realpart x
)) '$pos
))
2379 ;; carlson_rc(x,x) = 1/2*integrate(1/sqrt(t+x)/(t+x), t, 0, inf)
2382 ((and (alike1 x
(power (div (add 1 y
) 2) 2))
2383 (eq ($sign
($realpart y
)) '$pos
))
2384 ;; Rc(((1+x)/2)^2,x) = log(x)/(x-1) for x > 0.
2386 ;; This is done by looking at Rc(x,y) and seeing if
2387 ;; ((1+y)/2)^2 is the same as x.
2388 (div (ftake '%log y
)
2393 (def-simplifier carlson_rd
(x y z
)
2395 (flet ((calc (x y z
)
2396 (to (bigfloat::bf-rd
(bigfloat:to x
)
2399 ;; See https://dlmf.nist.gov/19.20.E18
2400 (cond ((and (eql x
1)
2407 ;; Rd(x,x,x) = x^(-3/2)
2408 (power x
(div -
3 2)))
2411 ;; Rd(0,y,y) = 3/4*%pi*y^(-3/2)
2414 (power y
(div -
3 2))))
2416 ;; Rd(x,y,y) = 3/(2*(y-x))*(Rc(x, y) - sqrt(x)/y)
2417 (mul (div 3 (mul 2 (sub y x
)))
2418 (sub (ftake '%carlson_rc x y
)
2422 ;; Rd(x,x,z) = 3/(z-x)*(Rc(z,x) - 1/sqrt(z))
2423 (mul (div 3 (sub z x
))
2424 (sub (ftake '%carlson_rc z x
)
2425 (div 1 (power z
1//2)))))
2431 ;; Rd(0,2,1) = 3*(gamma(3/4)^2)/sqrt(2*%pi)
2432 ;; See https://dlmf.nist.gov/19.20.E22.
2434 ;; But that's the same as
2435 ;; 3*sqrt(%pi)*gamma(3/4)/gamma(1/4). We can see this by
2436 ;; taking the ratio to get
2437 ;; gamma(1/4)*gamma(3/4)/sqrt(2)*%pi. But
2438 ;; gamma(1/4)*gamma(3/4) = beta(1/4,3/4) = sqrt(2)*%pi.
2439 ;; Hence, the ratio is 1.
2441 ;; Note also that Rd(x,y,z) = Rd(y,x,z)
2444 (div (ftake '%gamma
(div 3 4))
2445 (ftake '%gamma
(div 1 4)))))
2446 ((and (or (eql x
0) (eql y
0))
2448 ;; 1/3*m*Rd(0,1-m,1) = K(m) - E(m).
2449 ;; See https://dlmf.nist.gov/19.25.E1
2451 ;; Thus, Rd(0,y,1) = 3/(1-y)*(K(1-y) - E(1-y))
2453 ;; Note that Rd(x,y,z) = Rd(y,x,z).
2454 (let ((m (sub 1 y
)))
2456 (sub (ftake '%elliptic_kc m
)
2457 (ftake '%elliptic_ec m
)))))
2462 ;; 1/3*m*(1-m)*Rd(0,1,1-m) = E(m) - (1-m)*K(m)
2463 ;; See https://dlmf.nist.gov/19.25.E1
2466 ;; Rd(0,1,z) = 3/(z*(1-z))*(E(1-z) - z*K(1-z))
2467 ;; Recall that Rd(x,y,z) = Rd(y,x,z).
2468 (mul (div 3 (mul z
(sub 1 z
)))
2469 (sub (ftake '%elliptic_ec
(sub 1 z
))
2471 (ftake '%elliptic_kc
(sub 1 z
))))))
2472 ((float-numerical-eval-p x y z
)
2473 (calc ($float x
) ($float y
) ($float z
)))
2474 ((bigfloat-numerical-eval-p x y z
)
2475 (calc ($bfloat x
) ($bfloat y
) ($bfloat z
)))
2476 ((setf args
(complex-float-numerical-eval-p x y z
))
2477 (destructuring-bind (x y z
)
2479 (calc ($float x
) ($float y
) ($float z
))))
2480 ((setf args
(complex-bigfloat-numerical-eval-p x y z
))
2481 (destructuring-bind (x y z
)
2483 (calc ($bfloat x
) ($bfloat y
) ($bfloat z
))))
2487 (def-simplifier carlson_rf
(x y z
)
2489 (flet ((calc (x y z
)
2490 (to (bigfloat::bf-rf
(bigfloat:to x
)
2493 ;; See https://dlmf.nist.gov/19.20.i
2494 (cond ((and (alike1 x y
)
2496 ;; Rf(x,x,x) = x^(-1/2)
2500 ;; Rf(0,y,y) = 1/2*%pi*y^(-1/2)
2504 (ftake '%carlson_rc x y
))
2505 ((some #'(lambda (args)
2506 (destructuring-bind (x y z
)
2517 ;; Rf(0,1,2) = (gamma(1/4))^2/(4*sqrt(2*%pi))
2519 ;; And Rf is symmetric in all the args, so check every
2520 ;; permutation too. This could probably be simplified
2521 ;; without consing all the lists, but I'm lazy.
2522 (div (power (ftake '%gamma
(div 1 4)) 2)
2523 (mul 4 (power (mul 2 '$%pi
) 1//2))))
2524 ((some #'(lambda (args)
2525 (destructuring-bind (x y z
)
2527 (and (alike1 x
'$%i
)
2528 (alike1 y
(mul -
1 '$%i
))
2537 ;; = 1/2*integrate(1/sqrt(t^2+1)/sqrt(t),t,0,inf)
2538 ;; = beta(1/4,1/4)/4;
2540 ;; = gamma(1/4)^2/(4*sqrt(%pi))
2542 ;; Rf is symmetric, so check all the permutations too.
2543 (div (power (ftake '%gamma
(div 1 4)) 2)
2544 (mul 4 (power '$%pi
1//2))))
2546 (some #'(lambda (args)
2547 (destructuring-bind (x y z
)
2549 ;; Check that x = 0 and z = 1, and
2560 ;; Rf(0,1-m,1) = elliptic_kc(m).
2561 ;; See https://dlmf.nist.gov/19.25.E1
2562 (ftake '%elliptic_kc
(sub 1 args
)))
2563 ((some #'(lambda (args)
2564 (destructuring-bind (x y z
)
2566 (and (alike1 x
'$%i
)
2567 (alike1 y
(mul -
1 '$%i
))
2576 ;; = 1/2*integrate(1/sqrt(t^2+1)/sqrt(t),t,0,inf)
2577 ;; = beta(1/4,1/4)/4;
2579 ;; = gamma(1/4)^2/(4*sqrt(%pi))
2581 ;; Rf is symmetric, so check all the permutations too.
2582 (div (power (ftake '%gamma
(div 1 4)) 2)
2583 (mul 4 (power '$%pi
1//2))))
2584 ((float-numerical-eval-p x y z
)
2585 (calc ($float x
) ($float y
) ($float z
)))
2586 ((bigfloat-numerical-eval-p x y z
)
2587 (calc ($bfloat x
) ($bfloat y
) ($bfloat z
)))
2588 ((setf args
(complex-float-numerical-eval-p x y z
))
2589 (destructuring-bind (x y z
)
2591 (calc ($float x
) ($float y
) ($float z
))))
2592 ((setf args
(complex-bigfloat-numerical-eval-p x y z
))
2593 (destructuring-bind (x y z
)
2595 (calc ($bfloat x
) ($bfloat y
) ($bfloat z
))))
2599 (def-simplifier carlson_rj
(x y z p
)
2601 (flet ((calc (x y z p
)
2602 (to (bigfloat::bf-rj
(bigfloat:to x
)
2606 ;; See https://dlmf.nist.gov/19.20.iii
2607 (cond ((and (alike1 x y
)
2610 ;; Rj(x,x,x,x) = x^(-3/2)
2611 (power x
(div -
3 2)))
2613 ;; Rj(x,y,z,z) = Rd(x,y,z)
2614 (ftake '%carlson_rd x y z
))
2617 ;; Rj(0,y,y,p) = 3*%pi/(2*(y*sqrt(p)+p*sqrt(y)))
2620 (add (mul y
(power p
1//2))
2621 (mul p
(power y
1//2))))))
2623 ;; Rj(x,y,y,p) = 3/(p-y)*(Rc(x,y) - Rc(x,p))
2624 (mul (div 3 (sub p y
))
2625 (sub (ftake '%carlson_rc x y
)
2626 (ftake '%carlson_rc x p
))))
2629 ;; Rj(x,y,y,y) = Rd(x,y,y)
2630 (ftake '%carlson_rd x y y
))
2631 ((float-numerical-eval-p x y z p
)
2632 (calc ($float x
) ($float y
) ($float z
) ($float p
)))
2633 ((bigfloat-numerical-eval-p x y z p
)
2634 (calc ($bfloat x
) ($bfloat y
) ($bfloat z
) ($bfloat p
)))
2635 ((setf args
(complex-float-numerical-eval-p x y z p
))
2636 (destructuring-bind (x y z p
)
2638 (calc ($float x
) ($float y
) ($float z
) ($float p
))))
2639 ((setf args
(complex-bigfloat-numerical-eval-p x y z p
))
2640 (destructuring-bind (x y z p
)
2642 (calc ($bfloat x
) ($bfloat y
) ($bfloat z
) ($bfloat p
))))
2646 ;;; Other Jacobian elliptic functions
2648 ;; jacobi_ns(u,m) = 1/jacobi_sn(u,m)
2652 ((mtimes) -
1 ((%jacobi_cn
) u m
) ((%jacobi_dn
) u m
)
2653 ((mexpt) ((%jacobi_sn
) u m
) -
2))
2655 ((mtimes) -
1 ((mexpt) ((%jacobi_sn
) u m
) -
2)
2657 ((mtimes) ((rat) 1 2)
2658 ((mexpt) ((mplus) 1 ((mtimes) -
1 m
)) -
1)
2659 ((mexpt) ((%jacobi_cn
) u m
) 2)
2661 ((mtimes) ((rat) 1 2) ((mexpt) m -
1)
2662 ((%jacobi_cn
) u m
) ((%jacobi_dn
) u m
)
2665 ((mexpt) ((mplus) 1 ((mtimes) -
1 m
)) -
1)
2666 ((%elliptic_e
) ((%asin
) ((%jacobi_sn
) u m
))
2670 (def-simplifier jacobi_ns
(u m
)
2673 ((float-numerical-eval-p u m
)
2674 (to (bigfloat:/ (bigfloat::sn
(bigfloat:to
($float u
))
2675 (bigfloat:to
($float m
))))))
2676 ((setf args
(complex-float-numerical-eval-p u m
))
2677 (destructuring-bind (u m
)
2679 (to (bigfloat:/ (bigfloat::sn
(bigfloat:to
($float u
))
2680 (bigfloat:to
($float m
)))))))
2681 ((bigfloat-numerical-eval-p u m
)
2682 (let ((uu (bigfloat:to
($bfloat u
)))
2683 (mm (bigfloat:to
($bfloat m
))))
2684 (to (bigfloat:/ (bigfloat::sn uu mm
)))))
2685 ((setf args
(complex-bigfloat-numerical-eval-p u m
))
2686 (destructuring-bind (u m
)
2688 (let ((uu (bigfloat:to
($bfloat u
)))
2689 (mm (bigfloat:to
($bfloat m
))))
2690 (to (bigfloat:/ (bigfloat::sn uu mm
))))))
2698 (dbz-err1 'jacobi_ns
))
2699 ((and $trigsign
(mminusp* u
))
2701 (neg (ftake* '%jacobi_ns
(neg u
) m
)))
2704 (member (caar u
) '(%inverse_jacobi_sn
2715 %inverse_jacobi_dc
))
2716 (alike1 (third u
) m
))
2717 (cond ((eq (caar u
) '%inverse_jacobi_ns
)
2720 ;; Express in terms of sn:
2722 (div 1 (ftake '%jacobi_sn u m
)))))
2723 ;; A&S 16.20 (Jacobi's Imaginary transformation)
2724 ((and $%iargs
(multiplep u
'$%i
))
2725 ;; ns(i*u) = 1/sn(i*u) = -i/sc(u,m1) = -i*cs(u,m1)
2727 (ftake* '%jacobi_cs
(coeff u
'$%i
1) (add 1 (neg m
))))))
2728 ((setq coef
(kc-arg2 u m
))
2731 ;; ns(m*K+u) = 1/sn(m*K+u)
2733 (destructuring-bind (lin const
)
2735 (cond ((integerp lin
)
2738 ;; ns(4*m*K+u) = ns(u)
2741 (dbz-err1 'jacobi_ns
)
2742 (ftake '%jacobi_ns const m
)))
2744 ;; ns(4*m*K + K + u) = ns(K+u) = dc(u)
2748 (ftake '%jacobi_dc const m
)))
2750 ;; ns(4*m*K+2*K + u) = ns(2*K+u) = -ns(u)
2751 ;; ns(2*K) = infinity
2753 (dbz-err1 'jacobi_ns
)
2754 (neg (ftake '%jacobi_ns const m
))))
2756 ;; ns(4*m*K+3*K+u) = ns(2*K + K + u) = -ns(K+u) = -dc(u)
2760 (neg (ftake '%jacobi_dc const m
))))))
2761 ((and (alike1 lin
1//2)
2763 (div 1 (ftake '%jacobi_sn u m
)))
2770 ;; jacobi_nc(u,m) = 1/jacobi_cn(u,m)
2774 ((mtimes) ((mexpt) ((%jacobi_cn
) u m
) -
2)
2775 ((%jacobi_dn
) u m
) ((%jacobi_sn
) u m
))
2777 ((mtimes) -
1 ((mexpt) ((%jacobi_cn
) u m
) -
2)
2779 ((mtimes) ((rat) -
1 2)
2780 ((mexpt) ((mplus) 1 ((mtimes) -
1 m
)) -
1)
2781 ((%jacobi_cn
) u m
) ((mexpt) ((%jacobi_sn
) u m
) 2))
2782 ((mtimes) ((rat) -
1 2) ((mexpt) m -
1)
2783 ((%jacobi_dn
) u m
) ((%jacobi_sn
) u m
)
2785 ((mtimes) -
1 ((mexpt) ((mplus) 1 ((mtimes) -
1 m
)) -
1)
2786 ((%elliptic_e
) ((%asin
) ((%jacobi_sn
) u m
)) m
)))))))
2789 (def-simplifier jacobi_nc
(u m
)
2792 ((float-numerical-eval-p u m
)
2793 (to (bigfloat:/ (bigfloat::cn
(bigfloat:to
($float u
))
2794 (bigfloat:to
($float m
))))))
2795 ((setf args
(complex-float-numerical-eval-p u m
))
2796 (destructuring-bind (u m
)
2798 (to (bigfloat:/ (bigfloat::cn
(bigfloat:to
($float u
))
2799 (bigfloat:to
($float m
)))))))
2800 ((bigfloat-numerical-eval-p u m
)
2801 (let ((uu (bigfloat:to
($bfloat u
)))
2802 (mm (bigfloat:to
($bfloat m
))))
2803 (to (bigfloat:/ (bigfloat::cn uu mm
)))))
2804 ((setf args
(complex-bigfloat-numerical-eval-p u m
))
2805 (destructuring-bind (u m
)
2807 (let ((uu (bigfloat:to
($bfloat u
)))
2808 (mm (bigfloat:to
($bfloat m
))))
2809 (to (bigfloat:/ (bigfloat::cn uu mm
))))))
2818 ((and $trigsign
(mminusp* u
))
2820 (ftake* '%jacobi_nc
(neg u
) m
))
2823 (member (caar u
) '(%inverse_jacobi_sn
2834 %inverse_jacobi_dc
))
2835 (alike1 (third u
) m
))
2836 (cond ((eq (caar u
) '%inverse_jacobi_nc
)
2839 ;; Express in terms of cn:
2841 (div 1 (ftake '%jacobi_cn u m
)))))
2842 ;; A&S 16.20 (Jacobi's Imaginary transformation)
2843 ((and $%iargs
(multiplep u
'$%i
))
2844 ;; nc(i*u) = 1/cn(i*u) = 1/nc(u,1-m) = cn(u,1-m)
2845 (ftake* '%jacobi_cn
(coeff u
'$%i
1) (add 1 (neg m
))))
2846 ((setq coef
(kc-arg2 u m
))
2851 (destructuring-bind (lin const
)
2853 (cond ((integerp lin
)
2856 ;; nc(4*m*K+u) = nc(u)
2860 (ftake '%jacobi_nc const m
)))
2862 ;; nc(4*m*K+K+u) = nc(K+u) = -ds(u)/sqrt(1-m)
2865 (dbz-err1 'jacobi_nc
)
2866 (neg (div (ftake '%jacobi_ds const m
)
2867 (power (sub 1 m
) 1//2)))))
2869 ;; nc(4*m*K+2*K+u) = nc(2*K+u) = -nc(u)
2873 (neg (ftake '%jacobi_nc const m
))))
2875 ;; nc(4*m*K+3*K+u) = nc(3*K+u) = nc(2*K+K+u) =
2876 ;; -nc(K+u) = ds(u)/sqrt(1-m)
2878 ;; nc(3*K) = infinity
2880 (dbz-err1 'jacobi_nc
)
2881 (div (ftake '%jacobi_ds const m
)
2882 (power (sub 1 m
) 1//2))))))
2883 ((and (alike1 1//2 lin
)
2885 (div 1 (ftake '%jacobi_cn u m
)))
2892 ;; jacobi_nd(u,m) = 1/jacobi_dn(u,m)
2896 ((mtimes) m
((%jacobi_cn
) u m
)
2897 ((mexpt) ((%jacobi_dn
) u m
) -
2) ((%jacobi_sn
) u m
))
2899 ((mtimes) -
1 ((mexpt) ((%jacobi_dn
) u m
) -
2)
2901 ((mtimes) ((rat) -
1 2)
2902 ((mexpt) ((mplus) 1 ((mtimes) -
1 m
)) -
1)
2904 ((mexpt) ((%jacobi_sn
) u m
) 2))
2905 ((mtimes) ((rat) -
1 2) ((%jacobi_cn
) u m
)
2909 ((mexpt) ((mplus) 1 ((mtimes) -
1 m
)) -
1)
2910 ((%elliptic_e
) ((%asin
) ((%jacobi_sn
) u m
))
2914 (def-simplifier jacobi_nd
(u m
)
2917 ((float-numerical-eval-p u m
)
2918 (to (bigfloat:/ (bigfloat::dn
(bigfloat:to
($float u
))
2919 (bigfloat:to
($float m
))))))
2920 ((setf args
(complex-float-numerical-eval-p u m
))
2921 (destructuring-bind (u m
)
2923 (to (bigfloat:/ (bigfloat::dn
(bigfloat:to
($float u
))
2924 (bigfloat:to
($float m
)))))))
2925 ((bigfloat-numerical-eval-p u m
)
2926 (let ((uu (bigfloat:to
($bfloat u
)))
2927 (mm (bigfloat:to
($bfloat m
))))
2928 (to (bigfloat:/ (bigfloat::dn uu mm
)))))
2929 ((setf args
(complex-bigfloat-numerical-eval-p u m
))
2930 (destructuring-bind (u m
)
2932 (let ((uu (bigfloat:to
($bfloat u
)))
2933 (mm (bigfloat:to
($bfloat m
))))
2934 (to (bigfloat:/ (bigfloat::dn uu mm
))))))
2943 ((and $trigsign
(mminusp* u
))
2945 (ftake* '%jacobi_nd
(neg u
) m
))
2948 (member (caar u
) '(%inverse_jacobi_sn
2959 %inverse_jacobi_dc
))
2960 (alike1 (third u
) m
))
2961 (cond ((eq (caar u
) '%inverse_jacobi_nd
)
2964 ;; Express in terms of dn:
2966 (div 1 (ftake '%jacobi_dn u m
)))))
2967 ;; A&S 16.20 (Jacobi's Imaginary transformation)
2968 ((and $%iargs
(multiplep u
'$%i
))
2969 ;; nd(i*u) = 1/dn(i*u) = 1/dc(u,1-m) = cd(u,1-m)
2970 (ftake* '%jacobi_cd
(coeff u
'$%i
1) (add 1 (neg m
))))
2971 ((setq coef
(kc-arg2 u m
))
2974 (destructuring-bind (lin const
)
2976 (cond ((integerp lin
)
2980 ;; nd(2*m*K+u) = nd(u)
2984 (ftake '%jacobi_nd const m
)))
2986 ;; nd(2*m*K+K+u) = nd(K+u) = dn(u)/sqrt(1-m)
2987 ;; nd(K) = 1/sqrt(1-m)
2989 (power (sub 1 m
) -
1//2)
2990 (div (ftake '%jacobi_nd const m
)
2991 (power (sub 1 m
) 1//2))))))
2998 ;; jacobi_sc(u,m) = jacobi_sn/jacobi_cn
3002 ((mtimes) ((mexpt) ((%jacobi_cn
) u m
) -
2)
3006 ((mtimes) ((mexpt) ((%jacobi_cn
) u m
) -
1)
3008 ((mtimes) ((rat) 1 2)
3009 ((mexpt) ((mplus) 1 ((mtimes) -
1 m
)) -
1)
3010 ((mexpt) ((%jacobi_cn
) u m
) 2)
3012 ((mtimes) ((rat) 1 2) ((mexpt) m -
1)
3013 ((%jacobi_cn
) u m
) ((%jacobi_dn
) u m
)
3016 ((mexpt) ((mplus) 1 ((mtimes) -
1 m
)) -
1)
3017 ((%elliptic_e
) ((%asin
) ((%jacobi_sn
) u m
))
3019 ((mtimes) -
1 ((mexpt) ((%jacobi_cn
) u m
) -
2)
3022 ((mtimes) ((rat) -
1 2)
3023 ((mexpt) ((mplus) 1 ((mtimes) -
1 m
)) -
1)
3025 ((mexpt) ((%jacobi_sn
) u m
) 2))
3026 ((mtimes) ((rat) -
1 2) ((mexpt) m -
1)
3027 ((%jacobi_dn
) u m
) ((%jacobi_sn
) u m
)
3030 ((mexpt) ((mplus) 1 ((mtimes) -
1 m
)) -
1)
3031 ((%elliptic_e
) ((%asin
) ((%jacobi_sn
) u m
))
3035 (def-simplifier jacobi_sc
(u m
)
3038 ((float-numerical-eval-p u m
)
3039 (let ((fu (bigfloat:to
($float u
)))
3040 (fm (bigfloat:to
($float m
))))
3041 (to (bigfloat:/ (bigfloat::sn fu fm
) (bigfloat::cn fu fm
)))))
3042 ((setf args
(complex-float-numerical-eval-p u m
))
3043 (destructuring-bind (u m
)
3045 (let ((fu (bigfloat:to
($float u
)))
3046 (fm (bigfloat:to
($float m
))))
3047 (to (bigfloat:/ (bigfloat::sn fu fm
) (bigfloat::cn fu fm
))))))
3048 ((bigfloat-numerical-eval-p u m
)
3049 (let ((uu (bigfloat:to
($bfloat u
)))
3050 (mm (bigfloat:to
($bfloat m
))))
3051 (to (bigfloat:/ (bigfloat::sn uu mm
)
3052 (bigfloat::cn uu mm
)))))
3053 ((setf args
(complex-bigfloat-numerical-eval-p u m
))
3054 (destructuring-bind (u m
)
3056 (let ((uu (bigfloat:to
($bfloat u
)))
3057 (mm (bigfloat:to
($bfloat m
))))
3058 (to (bigfloat:/ (bigfloat::sn uu mm
)
3059 (bigfloat::cn uu mm
))))))
3068 ((and $trigsign
(mminusp* u
))
3070 (neg (ftake* '%jacobi_sc
(neg u
) m
)))
3073 (member (caar u
) '(%inverse_jacobi_sn
3084 %inverse_jacobi_dc
))
3085 (alike1 (third u
) m
))
3086 (cond ((eq (caar u
) '%inverse_jacobi_sc
)
3089 ;; Express in terms of sn and cn
3090 ;; sc(x) = sn(x)/cn(x)
3091 (div (ftake '%jacobi_sn u m
)
3092 (ftake '%jacobi_cn u m
)))))
3093 ;; A&S 16.20 (Jacobi's Imaginary transformation)
3094 ((and $%iargs
(multiplep u
'$%i
))
3095 ;; sc(i*u) = sn(i*u)/cn(i*u) = i*sc(u,m1)/nc(u,m1) = i*sn(u,m1)
3097 (ftake* '%jacobi_sn
(coeff u
'$%i
1) (add 1 (neg m
)))))
3098 ((setq coef
(kc-arg2 u m
))
3100 ;; sc(2*m*K+u) = sc(u)
3101 (destructuring-bind (lin const
)
3103 (cond ((integerp lin
)
3106 ;; sc(2*m*K+ u) = sc(u)
3110 (ftake '%jacobi_sc const m
)))
3112 ;; sc(2*m*K + K + u) = sc(K+u)= - cs(u)/sqrt(1-m)
3115 (dbz-err1 'jacobi_sc
)
3117 (div (ftake* '%jacobi_cs const m
)
3118 (power (sub 1 m
) 1//2)))))))
3119 ((and (alike1 lin
1//2)
3121 ;; From A&S 16.3.3 and 16.5.2:
3122 ;; sc(1/2*K) = 1/(1-m)^(1/4)
3123 (power (sub 1 m
) (div -
1 4)))
3130 ;; jacobi_sd(u,m) = jacobi_sn/jacobi_dn
3134 ((mtimes) ((%jacobi_cn
) u m
)
3135 ((mexpt) ((%jacobi_dn
) u m
) -
2))
3138 ((mtimes) ((mexpt) ((%jacobi_dn
) u m
) -
1)
3140 ((mtimes) ((rat) 1 2)
3141 ((mexpt) ((mplus) 1 ((mtimes) -
1 m
)) -
1)
3142 ((mexpt) ((%jacobi_cn
) u m
) 2)
3144 ((mtimes) ((rat) 1 2) ((mexpt) m -
1)
3145 ((%jacobi_cn
) u m
) ((%jacobi_dn
) u m
)
3148 ((mexpt) ((mplus) 1 ((mtimes) -
1 m
)) -
1)
3149 ((%elliptic_e
) ((%asin
) ((%jacobi_sn
) u m
))
3151 ((mtimes) -
1 ((mexpt) ((%jacobi_dn
) u m
) -
2)
3154 ((mtimes) ((rat) -
1 2)
3155 ((mexpt) ((mplus) 1 ((mtimes) -
1 m
)) -
1)
3157 ((mexpt) ((%jacobi_sn
) u m
) 2))
3158 ((mtimes) ((rat) -
1 2) ((%jacobi_cn
) u m
)
3162 ((mexpt) ((mplus) 1 ((mtimes) -
1 m
)) -
1)
3163 ((%elliptic_e
) ((%asin
) ((%jacobi_sn
) u m
))
3167 (def-simplifier jacobi_sd
(u m
)
3170 ((float-numerical-eval-p u m
)
3171 (let ((fu (bigfloat:to
($float u
)))
3172 (fm (bigfloat:to
($float m
))))
3173 (to (bigfloat:/ (bigfloat::sn fu fm
) (bigfloat::dn fu fm
)))))
3174 ((setf args
(complex-float-numerical-eval-p u m
))
3175 (destructuring-bind (u m
)
3177 (let ((fu (bigfloat:to
($float u
)))
3178 (fm (bigfloat:to
($float m
))))
3179 (to (bigfloat:/ (bigfloat::sn fu fm
) (bigfloat::dn fu fm
))))))
3180 ((bigfloat-numerical-eval-p u m
)
3181 (let ((uu (bigfloat:to
($bfloat u
)))
3182 (mm (bigfloat:to
($bfloat m
))))
3183 (to (bigfloat:/ (bigfloat::sn uu mm
)
3184 (bigfloat::dn uu mm
)))))
3185 ((setf args
(complex-bigfloat-numerical-eval-p u m
))
3186 (destructuring-bind (u m
)
3188 (let ((uu (bigfloat:to
($bfloat u
)))
3189 (mm (bigfloat:to
($bfloat m
))))
3190 (to (bigfloat:/ (bigfloat::sn uu mm
)
3191 (bigfloat::dn uu mm
))))))
3200 ((and $trigsign
(mminusp* u
))
3202 (neg (ftake* '%jacobi_sd
(neg u
) m
)))
3205 (member (caar u
) '(%inverse_jacobi_sn
3216 %inverse_jacobi_dc
))
3217 (alike1 (third u
) m
))
3218 (cond ((eq (caar u
) '%inverse_jacobi_sd
)
3221 ;; Express in terms of sn and dn
3222 (div (ftake '%jacobi_sn u m
)
3223 (ftake '%jacobi_dn u m
)))))
3224 ;; A&S 16.20 (Jacobi's Imaginary transformation)
3225 ((and $%iargs
(multiplep u
'$%i
))
3226 ;; sd(i*u) = sn(i*u)/dn(i*u) = i*sc(u,m1)/dc(u,m1) = i*sd(u,m1)
3228 (ftake* '%jacobi_sd
(coeff u
'$%i
1) (add 1 (neg m
)))))
3229 ((setq coef
(kc-arg2 u m
))
3231 ;; sd(4*m*K+u) = sd(u)
3232 (destructuring-bind (lin const
)
3234 (cond ((integerp lin
)
3237 ;; sd(4*m*K+u) = sd(u)
3241 (ftake '%jacobi_sd const m
)))
3243 ;; sd(4*m*K+K+u) = sd(K+u) = cn(u)/sqrt(1-m)
3244 ;; sd(K) = 1/sqrt(m1)
3246 (power (sub 1 m
) 1//2)
3247 (div (ftake '%jacobi_cn const m
)
3248 (power (sub 1 m
) 1//2))))
3250 ;; sd(4*m*K+2*K+u) = sd(2*K+u) = -sd(u)
3254 (neg (ftake '%jacobi_sd const m
))))
3256 ;; sd(4*m*K+3*K+u) = sd(3*K+u) = sd(2*K+K+u) =
3257 ;; -sd(K+u) = -cn(u)/sqrt(1-m)
3258 ;; sd(3*K) = -1/sqrt(m1)
3260 (neg (power (sub 1 m
) -
1//2))
3261 (neg (div (ftake '%jacobi_cn const m
)
3262 (power (sub 1 m
) 1//2)))))))
3263 ((and (alike1 lin
1//2)
3265 ;; jacobi_sn/jacobi_dn
3266 (div (ftake '%jacobi_sn
3268 (ftake '%elliptic_kc m
))
3272 (ftake '%elliptic_kc m
))
3280 ;; jacobi_cs(u,m) = jacobi_cn/jacobi_sn
3284 ((mtimes) -
1 ((%jacobi_dn
) u m
)
3285 ((mexpt) ((%jacobi_sn
) u m
) -
2))
3288 ((mtimes) -
1 ((%jacobi_cn
) u m
)
3289 ((mexpt) ((%jacobi_sn
) u m
) -
2)
3291 ((mtimes) ((rat) 1 2)
3292 ((mexpt) ((mplus) 1 ((mtimes) -
1 m
)) -
1)
3293 ((mexpt) ((%jacobi_cn
) u m
) 2)
3295 ((mtimes) ((rat) 1 2) ((mexpt) m -
1)
3296 ((%jacobi_cn
) u m
) ((%jacobi_dn
) u m
)
3299 ((mexpt) ((mplus) 1 ((mtimes) -
1 m
)) -
1)
3300 ((%elliptic_e
) ((%asin
) ((%jacobi_sn
) u m
))
3302 ((mtimes) ((mexpt) ((%jacobi_sn
) u m
) -
1)
3304 ((mtimes) ((rat) -
1 2)
3305 ((mexpt) ((mplus) 1 ((mtimes) -
1 m
)) -
1)
3307 ((mexpt) ((%jacobi_sn
) u m
) 2))
3308 ((mtimes) ((rat) -
1 2) ((mexpt) m -
1)
3309 ((%jacobi_dn
) u m
) ((%jacobi_sn
) u m
)
3312 ((mexpt) ((mplus) 1 ((mtimes) -
1 m
)) -
1)
3313 ((%elliptic_e
) ((%asin
) ((%jacobi_sn
) u m
))
3317 (def-simplifier jacobi_cs
(u m
)
3320 ((float-numerical-eval-p u m
)
3321 (let ((fu (bigfloat:to
($float u
)))
3322 (fm (bigfloat:to
($float m
))))
3323 (to (bigfloat:/ (bigfloat::cn fu fm
) (bigfloat::sn fu fm
)))))
3324 ((setf args
(complex-float-numerical-eval-p u m
))
3325 (destructuring-bind (u m
)
3327 (let ((fu (bigfloat:to
($float u
)))
3328 (fm (bigfloat:to
($float m
))))
3329 (to (bigfloat:/ (bigfloat::cn fu fm
) (bigfloat::sn fu fm
))))))
3330 ((bigfloat-numerical-eval-p u m
)
3331 (let ((uu (bigfloat:to
($bfloat u
)))
3332 (mm (bigfloat:to
($bfloat m
))))
3333 (to (bigfloat:/ (bigfloat::cn uu mm
)
3334 (bigfloat::sn uu mm
)))))
3335 ((setf args
(complex-bigfloat-numerical-eval-p u m
))
3336 (destructuring-bind (u m
)
3338 (let ((uu (bigfloat:to
($bfloat u
)))
3339 (mm (bigfloat:to
($bfloat m
))))
3340 (to (bigfloat:/ (bigfloat::cn uu mm
)
3341 (bigfloat::sn uu mm
))))))
3349 (dbz-err1 'jacobi_cs
))
3350 ((and $trigsign
(mminusp* u
))
3352 (neg (ftake* '%jacobi_cs
(neg u
) m
)))
3355 (member (caar u
) '(%inverse_jacobi_sn
3366 %inverse_jacobi_dc
))
3367 (alike1 (third u
) m
))
3368 (cond ((eq (caar u
) '%inverse_jacobi_cs
)
3371 ;; Express in terms of cn an sn
3372 (div (ftake '%jacobi_cn u m
)
3373 (ftake '%jacobi_sn u m
)))))
3374 ;; A&S 16.20 (Jacobi's Imaginary transformation)
3375 ((and $%iargs
(multiplep u
'$%i
))
3376 ;; cs(i*u) = cn(i*u)/sn(i*u) = -i*nc(u,m1)/sc(u,m1) = -i*ns(u,m1)
3378 (ftake* '%jacobi_ns
(coeff u
'$%i
1) (add 1 (neg m
))))))
3379 ((setq coef
(kc-arg2 u m
))
3382 ;; cs(2*m*K + u) = cs(u)
3383 (destructuring-bind (lin const
)
3385 (cond ((integerp lin
)
3388 ;; cs(2*m*K + u) = cs(u)
3391 (dbz-err1 'jacobi_cs
)
3392 (ftake '%jacobi_cs const m
)))
3394 ;; cs(K+u) = -sqrt(1-m)*sc(u)
3398 (neg (mul (power (sub 1 m
) 1//2)
3399 (ftake '%jacobi_sc const m
)))))))
3400 ((and (alike1 lin
1//2)
3404 (ftake '%jacobi_sc
(mul 1//2
3405 (ftake '%elliptic_kc m
))
3413 ;; jacobi_cd(u,m) = jacobi_cn/jacobi_dn
3417 ((mtimes) ((mplus) -
1 m
)
3418 ((mexpt) ((%jacobi_dn
) u m
) -
2)
3422 ((mtimes) -
1 ((%jacobi_cn
) u m
)
3423 ((mexpt) ((%jacobi_dn
) u m
) -
2)
3425 ((mtimes) ((rat) -
1 2)
3426 ((mexpt) ((mplus) 1 ((mtimes) -
1 m
)) -
1)
3428 ((mexpt) ((%jacobi_sn
) u m
) 2))
3429 ((mtimes) ((rat) -
1 2) ((%jacobi_cn
) u m
)
3433 ((mexpt) ((mplus) 1 ((mtimes) -
1 m
)) -
1)
3434 ((%elliptic_e
) ((%asin
) ((%jacobi_sn
) u m
))
3436 ((mtimes) ((mexpt) ((%jacobi_dn
) u m
) -
1)
3438 ((mtimes) ((rat) -
1 2)
3439 ((mexpt) ((mplus) 1 ((mtimes) -
1 m
)) -
1)
3441 ((mexpt) ((%jacobi_sn
) u m
) 2))
3442 ((mtimes) ((rat) -
1 2) ((mexpt) m -
1)
3443 ((%jacobi_dn
) u m
) ((%jacobi_sn
) u m
)
3446 ((mexpt) ((mplus) 1 ((mtimes) -
1 m
)) -
1)
3447 ((%elliptic_e
) ((%asin
) ((%jacobi_sn
) u m
))
3451 (def-simplifier jacobi_cd
(u m
)
3454 ((float-numerical-eval-p u m
)
3455 (let ((fu (bigfloat:to
($float u
)))
3456 (fm (bigfloat:to
($float m
))))
3457 (to (bigfloat:/ (bigfloat::cn fu fm
) (bigfloat::dn fu fm
)))))
3458 ((setf args
(complex-float-numerical-eval-p u m
))
3459 (destructuring-bind (u m
)
3461 (let ((fu (bigfloat:to
($float u
)))
3462 (fm (bigfloat:to
($float m
))))
3463 (to (bigfloat:/ (bigfloat::cn fu fm
) (bigfloat::dn fu fm
))))))
3464 ((bigfloat-numerical-eval-p u m
)
3465 (let ((uu (bigfloat:to
($bfloat u
)))
3466 (mm (bigfloat:to
($bfloat m
))))
3467 (to (bigfloat:/ (bigfloat::cn uu mm
) (bigfloat::dn uu mm
)))))
3468 ((setf args
(complex-bigfloat-numerical-eval-p u m
))
3469 (destructuring-bind (u m
)
3471 (let ((uu (bigfloat:to
($bfloat u
)))
3472 (mm (bigfloat:to
($bfloat m
))))
3473 (to (bigfloat:/ (bigfloat::cn uu mm
) (bigfloat::dn uu mm
))))))
3482 ((and $trigsign
(mminusp* u
))
3484 (ftake* '%jacobi_cd
(neg u
) m
))
3487 (member (caar u
) '(%inverse_jacobi_sn
3498 %inverse_jacobi_dc
))
3499 (alike1 (third u
) m
))
3500 (cond ((eq (caar u
) '%inverse_jacobi_cd
)
3503 ;; Express in terms of cn and dn
3504 (div (ftake '%jacobi_cn u m
)
3505 (ftake '%jacobi_dn u m
)))))
3506 ;; A&S 16.20 (Jacobi's Imaginary transformation)
3507 ((and $%iargs
(multiplep u
'$%i
))
3508 ;; cd(i*u) = cn(i*u)/dn(i*u) = nc(u,m1)/dc(u,m1) = nd(u,m1)
3509 (ftake* '%jacobi_nd
(coeff u
'$%i
1) (add 1 (neg m
))))
3510 ((setf coef
(kc-arg2 u m
))
3513 (destructuring-bind (lin const
)
3515 (cond ((integerp lin
)
3518 ;; cd(4*m*K + u) = cd(u)
3522 (ftake '%jacobi_cd const m
)))
3524 ;; cd(4*m*K + K + u) = cd(K+u) = -sn(u)
3528 (neg (ftake '%jacobi_sn const m
))))
3530 ;; cd(4*m*K + 2*K + u) = cd(2*K+u) = -cd(u)
3534 (neg (ftake '%jacobi_cd const m
))))
3536 ;; cd(4*m*K + 3*K + u) = cd(2*K + K + u) =
3541 (ftake '%jacobi_sn const m
)))))
3542 ((and (alike1 lin
1//2)
3544 ;; jacobi_cn/jacobi_dn
3545 (div (ftake '%jacobi_cn
3547 (ftake '%elliptic_kc m
))
3551 (ftake '%elliptic_kc m
))
3560 ;; jacobi_ds(u,m) = jacobi_dn/jacobi_sn
3564 ((mtimes) -
1 ((%jacobi_cn
) u m
)
3565 ((mexpt) ((%jacobi_sn
) u m
) -
2))
3568 ((mtimes) -
1 ((%jacobi_dn
) u m
)
3569 ((mexpt) ((%jacobi_sn
) u m
) -
2)
3571 ((mtimes) ((rat) 1 2)
3572 ((mexpt) ((mplus) 1 ((mtimes) -
1 m
)) -
1)
3573 ((mexpt) ((%jacobi_cn
) u m
) 2)
3575 ((mtimes) ((rat) 1 2) ((mexpt) m -
1)
3576 ((%jacobi_cn
) u m
) ((%jacobi_dn
) u m
)
3579 ((mexpt) ((mplus) 1 ((mtimes) -
1 m
)) -
1)
3580 ((%elliptic_e
) ((%asin
) ((%jacobi_sn
) u m
))
3582 ((mtimes) ((mexpt) ((%jacobi_sn
) u m
) -
1)
3584 ((mtimes) ((rat) -
1 2)
3585 ((mexpt) ((mplus) 1 ((mtimes) -
1 m
)) -
1)
3587 ((mexpt) ((%jacobi_sn
) u m
) 2))
3588 ((mtimes) ((rat) -
1 2) ((%jacobi_cn
) u m
)
3592 ((mexpt) ((mplus) 1 ((mtimes) -
1 m
)) -
1)
3593 ((%elliptic_e
) ((%asin
) ((%jacobi_sn
) u m
))
3597 (def-simplifier jacobi_ds
(u m
)
3600 ((float-numerical-eval-p u m
)
3601 (let ((fu (bigfloat:to
($float u
)))
3602 (fm (bigfloat:to
($float m
))))
3603 (to (bigfloat:/ (bigfloat::dn fu fm
) (bigfloat::sn fu fm
)))))
3604 ((setf args
(complex-float-numerical-eval-p u m
))
3605 (destructuring-bind (u m
)
3607 (let ((fu (bigfloat:to
($float u
)))
3608 (fm (bigfloat:to
($float m
))))
3609 (to (bigfloat:/ (bigfloat::dn fu fm
) (bigfloat::sn fu fm
))))))
3610 ((bigfloat-numerical-eval-p u m
)
3611 (let ((uu (bigfloat:to
($bfloat u
)))
3612 (mm (bigfloat:to
($bfloat m
))))
3613 (to (bigfloat:/ (bigfloat::dn uu mm
)
3614 (bigfloat::sn uu mm
)))))
3615 ((setf args
(complex-bigfloat-numerical-eval-p u m
))
3616 (destructuring-bind (u m
)
3618 (let ((uu (bigfloat:to
($bfloat u
)))
3619 (mm (bigfloat:to
($bfloat m
))))
3620 (to (bigfloat:/ (bigfloat::dn uu mm
)
3621 (bigfloat::sn uu mm
))))))
3629 (dbz-err1 'jacobi_ds
))
3630 ((and $trigsign
(mminusp* u
))
3631 (neg (ftake* '%jacobi_ds
(neg u
) m
)))
3634 (member (caar u
) '(%inverse_jacobi_sn
3645 %inverse_jacobi_dc
))
3646 (alike1 (third u
) m
))
3647 (cond ((eq (caar u
) '%inverse_jacobi_ds
)
3650 ;; Express in terms of dn and sn
3651 (div (ftake '%jacobi_dn u m
)
3652 (ftake '%jacobi_sn u m
)))))
3653 ;; A&S 16.20 (Jacobi's Imaginary transformation)
3654 ((and $%iargs
(multiplep u
'$%i
))
3655 ;; ds(i*u) = dn(i*u)/sn(i*u) = -i*dc(u,m1)/sc(u,m1) = -i*ds(u,m1)
3657 (ftake* '%jacobi_ds
(coeff u
'$%i
1) (add 1 (neg m
))))))
3658 ((setf coef
(kc-arg2 u m
))
3660 (destructuring-bind (lin const
)
3662 (cond ((integerp lin
)
3665 ;; ds(4*m*K + u) = ds(u)
3668 (dbz-err1 'jacobi_ds
)
3669 (ftake '%jacobi_ds const m
)))
3671 ;; ds(4*m*K + K + u) = ds(K+u) = sqrt(1-m)*nc(u)
3672 ;; ds(K) = sqrt(1-m)
3674 (power (sub 1 m
) 1//2)
3675 (mul (power (sub 1 m
) 1//2)
3676 (ftake '%jacobi_nc const m
))))
3678 ;; ds(4*m*K + 2*K + u) = ds(2*K+u) = -ds(u)
3681 (dbz-err1 'jacobi_ds
)
3682 (neg (ftake '%jacobi_ds const m
))))
3684 ;; ds(4*m*K + 3*K + u) = ds(2*K + K + u) =
3685 ;; -ds(K+u) = -sqrt(1-m)*nc(u)
3686 ;; ds(3*K) = -sqrt(1-m)
3688 (neg (power (sub 1 m
) 1//2))
3689 (neg (mul (power (sub 1 m
) 1//2)
3690 (ftake '%jacobi_nc u m
)))))))
3691 ((and (alike1 lin
1//2)
3693 ;; jacobi_dn/jacobi_sn
3696 (mul 1//2 (ftake '%elliptic_kc m
))
3699 (mul 1//2 (ftake '%elliptic_kc m
))
3708 ;; jacobi_dc(u,m) = jacobi_dn/jacobi_cn
3712 ((mtimes) ((mplus) 1 ((mtimes) -
1 m
))
3713 ((mexpt) ((%jacobi_cn
) u m
) -
2)
3717 ((mtimes) ((mexpt) ((%jacobi_cn
) u m
) -
1)
3719 ((mtimes) ((rat) -
1 2)
3720 ((mexpt) ((mplus) 1 ((mtimes) -
1 m
)) -
1)
3722 ((mexpt) ((%jacobi_sn
) u m
) 2))
3723 ((mtimes) ((rat) -
1 2) ((%jacobi_cn
) u m
)
3727 ((mexpt) ((mplus) 1 ((mtimes) -
1 m
)) -
1)
3728 ((%elliptic_e
) ((%asin
) ((%jacobi_sn
) u m
))
3730 ((mtimes) -
1 ((mexpt) ((%jacobi_cn
) u m
) -
2)
3733 ((mtimes) ((rat) -
1 2)
3734 ((mexpt) ((mplus) 1 ((mtimes) -
1 m
)) -
1)
3736 ((mexpt) ((%jacobi_sn
) u m
) 2))
3737 ((mtimes) ((rat) -
1 2) ((mexpt) m -
1)
3738 ((%jacobi_dn
) u m
) ((%jacobi_sn
) u m
)
3741 ((mexpt) ((mplus) 1 ((mtimes) -
1 m
)) -
1)
3742 ((%elliptic_e
) ((%asin
) ((%jacobi_sn
) u m
))
3746 (def-simplifier jacobi_dc
(u m
)
3749 ((float-numerical-eval-p u m
)
3750 (let ((fu (bigfloat:to
($float u
)))
3751 (fm (bigfloat:to
($float m
))))
3752 (to (bigfloat:/ (bigfloat::dn fu fm
) (bigfloat::cn fu fm
)))))
3753 ((setf args
(complex-float-numerical-eval-p u m
))
3754 (destructuring-bind (u m
)
3756 (let ((fu (bigfloat:to
($float u
)))
3757 (fm (bigfloat:to
($float m
))))
3758 (to (bigfloat:/ (bigfloat::dn fu fm
) (bigfloat::cn fu fm
))))))
3759 ((bigfloat-numerical-eval-p u m
)
3760 (let ((uu (bigfloat:to
($bfloat u
)))
3761 (mm (bigfloat:to
($bfloat m
))))
3762 (to (bigfloat:/ (bigfloat::dn uu mm
)
3763 (bigfloat::cn uu mm
)))))
3764 ((setf args
(complex-bigfloat-numerical-eval-p u m
))
3765 (destructuring-bind (u m
)
3767 (let ((uu (bigfloat:to
($bfloat u
)))
3768 (mm (bigfloat:to
($bfloat m
))))
3769 (to (bigfloat:/ (bigfloat::dn uu mm
)
3770 (bigfloat::cn uu mm
))))))
3779 ((and $trigsign
(mminusp* u
))
3780 (ftake* '%jacobi_dc
(neg u
) m
))
3783 (member (caar u
) '(%inverse_jacobi_sn
3794 %inverse_jacobi_dc
))
3795 (alike1 (third u
) m
))
3796 (cond ((eq (caar u
) '%inverse_jacobi_dc
)
3799 ;; Express in terms of dn and cn
3800 (div (ftake '%jacobi_dn u m
)
3801 (ftake '%jacobi_cn u m
)))))
3802 ;; A&S 16.20 (Jacobi's Imaginary transformation)
3803 ((and $%iargs
(multiplep u
'$%i
))
3804 ;; dc(i*u) = dn(i*u)/cn(i*u) = dc(u,m1)/nc(u,m1) = dn(u,m1)
3805 (ftake* '%jacobi_dn
(coeff u
'$%i
1) (add 1 (neg m
))))
3806 ((setf coef
(kc-arg2 u m
))
3808 (destructuring-bind (lin const
)
3810 (cond ((integerp lin
)
3813 ;; dc(4*m*K + u) = dc(u)
3817 (ftake '%jacobi_dc const m
)))
3819 ;; dc(4*m*K + K + u) = dc(K+u) = -ns(u)
3822 (dbz-err1 'jacobi_dc
)
3823 (neg (ftake '%jacobi_ns const m
))))
3825 ;; dc(4*m*K + 2*K + u) = dc(2*K+u) = -dc(u)
3829 (neg (ftake '%jacobi_dc const m
))))
3831 ;; dc(4*m*K + 3*K + u) = dc(2*K + K + u) =
3833 ;; dc(3*K) = ns(0) = inf
3835 (dbz-err1 'jacobi_dc
)
3836 (ftake '%jacobi_dc const m
)))))
3837 ((and (alike1 lin
1//2)
3839 ;; jacobi_dn/jacobi_cn
3842 (mul 1//2 (ftake '%elliptic_kc m
))
3845 (mul 1//2 (ftake '%elliptic_kc m
))
3854 ;;; Other inverse Jacobian functions
3856 ;; inverse_jacobi_ns(x)
3858 ;; Let u = inverse_jacobi_ns(x). Then jacobi_ns(u) = x or
3859 ;; 1/jacobi_sn(u) = x or
3861 ;; jacobi_sn(u) = 1/x
3863 ;; so u = inverse_jacobi_sn(1/x)
3864 (defprop %inverse_jacobi_ns
3866 ;; Whittaker and Watson, example in 22.122
3867 ;; inverse_jacobi_ns(u,m) = integrate(1/sqrt(t^2-1)/sqrt(t^2-m), t, u, inf)
3868 ;; -> -1/sqrt(x^2-1)/sqrt(x^2-m)
3870 ((mexpt) ((mplus) -
1 ((mexpt) x
2)) ((rat) -
1 2))
3872 ((mplus) ((mtimes simp ratsimp
) -
1 m
) ((mexpt) x
2))
3875 ; ((%derivative) ((%inverse_jacobi_ns) x m) m 1)
3879 (def-simplifier inverse_jacobi_ns
(u m
)
3882 ((float-numerical-eval-p u m
)
3883 ;; Numerically evaluate asn
3885 ;; ans(x,m) = asn(1/x,m) = F(asin(1/x),m)
3886 (to (elliptic-f (cl:asin
(/ ($float u
))) ($float m
))))
3887 ((complex-float-numerical-eval-p u m
)
3888 (to (elliptic-f (cl:asin
(/ (complex ($realpart
($float u
)) ($imagpart
($float u
)))))
3889 (complex ($realpart
($float m
)) ($imagpart
($float m
))))))
3890 ((bigfloat-numerical-eval-p u m
)
3891 (to (bigfloat::bf-elliptic-f
(bigfloat:asin
(bigfloat:/ (bigfloat:to
($bfloat u
))))
3892 (bigfloat:to
($bfloat m
)))))
3893 ((setf args
(complex-bigfloat-numerical-eval-p u m
))
3894 (destructuring-bind (u m
)
3896 (to (bigfloat::bf-elliptic-f
(bigfloat:asin
(bigfloat:/ (bigfloat:to
($bfloat u
))))
3897 (bigfloat:to
($bfloat m
))))))
3899 ;; ans(x,0) = F(asin(1/x),0) = asin(1/x)
3900 (ftake '%elliptic_f
(ftake '%asin
(div 1 u
)) 0))
3902 ;; ans(x,1) = F(asin(1/x),1) = log(tan(pi/2+asin(1/x)/2))
3903 (ftake '%elliptic_f
(ftake '%asin
(div 1 u
)) 1))
3905 (ftake '%elliptic_kc m
))
3907 (neg (ftake '%elliptic_kc m
)))
3908 ((and (eq $triginverses
'$all
)
3910 (eq (caar u
) '%jacobi_ns
)
3911 (alike1 (third u
) m
))
3912 ;; inverse_jacobi_ns(ns(u)) = u
3918 ;; inverse_jacobi_nc(x)
3920 ;; Let u = inverse_jacobi_nc(x). Then jacobi_nc(u) = x or
3921 ;; 1/jacobi_cn(u) = x or
3923 ;; jacobi_cn(u) = 1/x
3925 ;; so u = inverse_jacobi_cn(1/x)
3926 (defprop %inverse_jacobi_nc
3928 ;; Whittaker and Watson, example in 22.122
3929 ;; inverse_jacobi_nc(u,m) = integrate(1/sqrt(t^2-1)/sqrt((1-m)*t^2+m), t, 1, u)
3930 ;; -> 1/sqrt(x^2-1)/sqrt((1-m)*x^2+m)
3932 ((mexpt) ((mplus) -
1 ((mexpt) x
2)) ((rat) -
1 2))
3935 ((mtimes) -
1 ((mplus) -
1 m
) ((mexpt) x
2)))
3938 ; ((%derivative) ((%inverse_jacobi_nc) x m) m 1)
3942 (def-simplifier inverse_jacobi_nc
(u m
)
3943 (cond ((or (float-numerical-eval-p u m
)
3944 (complex-float-numerical-eval-p u m
)
3945 (bigfloat-numerical-eval-p u m
)
3946 (complex-bigfloat-numerical-eval-p u m
))
3948 (ftake '%inverse_jacobi_cn
($rectform
(div 1 u
)) m
))
3952 (mul 2 (ftake '%elliptic_kc m
)))
3953 ((and (eq $triginverses
'$all
)
3955 (eq (caar u
) '%jacobi_nc
)
3956 (alike1 (third u
) m
))
3957 ;; inverse_jacobi_nc(nc(u)) = u
3963 ;; inverse_jacobi_nd(x)
3965 ;; Let u = inverse_jacobi_nd(x). Then jacobi_nd(u) = x or
3966 ;; 1/jacobi_dn(u) = x or
3968 ;; jacobi_dn(u) = 1/x
3970 ;; so u = inverse_jacobi_dn(1/x)
3971 (defprop %inverse_jacobi_nd
3973 ;; Whittaker and Watson, example in 22.122
3974 ;; inverse_jacobi_nd(u,m) = integrate(1/sqrt(t^2-1)/sqrt(1-(1-m)*t^2), t, 1, u)
3975 ;; -> 1/sqrt(u^2-1)/sqrt(1-(1-m)*t^2)
3977 ((mexpt) ((mplus) -
1 ((mexpt simp ratsimp
) x
2))
3981 ((mtimes) ((mplus) -
1 m
) ((mexpt simp ratsimp
) x
2)))
3984 ; ((%derivative) ((%inverse_jacobi_nd) x m) m 1)
3988 (def-simplifier inverse_jacobi_nd
(u m
)
3989 (cond ((or (float-numerical-eval-p u m
)
3990 (complex-float-numerical-eval-p u m
)
3991 (bigfloat-numerical-eval-p u m
)
3992 (complex-bigfloat-numerical-eval-p u m
))
3993 (ftake '%inverse_jacobi_dn
($rectform
(div 1 u
)) m
))
3996 ((onep1 ($ratsimp
(mul (power (sub 1 m
) 1//2) u
)))
3997 ;; jacobi_nd(1/sqrt(1-m),m) = K(m). This follows from
3998 ;; jacobi_dn(sqrt(1-m),m) = K(m).
3999 (ftake '%elliptic_kc m
))
4000 ((and (eq $triginverses
'$all
)
4002 (eq (caar u
) '%jacobi_nd
)
4003 (alike1 (third u
) m
))
4004 ;; inverse_jacobi_nd(nd(u)) = u
4010 ;; inverse_jacobi_sc(x)
4012 ;; Let u = inverse_jacobi_sc(x). Then jacobi_sc(u) = x or
4013 ;; x = jacobi_sn(u)/jacobi_cn(u)
4020 ;; sn^2 = x^2/(1+x^2)
4022 ;; sn(u) = x/sqrt(1+x^2)
4024 ;; u = inverse_sn(x/sqrt(1+x^2))
4026 (defprop %inverse_jacobi_sc
4028 ;; Whittaker and Watson, example in 22.122
4029 ;; inverse_jacobi_sc(u,m) = integrate(1/sqrt(1+t^2)/sqrt(1+(1-m)*t^2), t, 0, u)
4030 ;; -> 1/sqrt(1+x^2)/sqrt(1+(1-m)*x^2)
4032 ((mexpt) ((mplus) 1 ((mexpt) x
2))
4036 ((mtimes) -
1 ((mplus) -
1 m
) ((mexpt) x
2)))
4039 ; ((%derivative) ((%inverse_jacobi_sc) x m) m 1)
4043 (def-simplifier inverse_jacobi_sc
(u m
)
4044 (cond ((or (float-numerical-eval-p u m
)
4045 (complex-float-numerical-eval-p u m
)
4046 (bigfloat-numerical-eval-p u m
)
4047 (complex-bigfloat-numerical-eval-p u m
))
4048 (ftake '%inverse_jacobi_sn
4049 ($rectform
(div u
(power (add 1 (mul u u
)) 1//2)))
4052 ;; jacobi_sc(0,m) = 0
4054 ((and (eq $triginverses
'$all
)
4056 (eq (caar u
) '%jacobi_sc
)
4057 (alike1 (third u
) m
))
4058 ;; inverse_jacobi_sc(sc(u)) = u
4064 ;; inverse_jacobi_sd(x)
4066 ;; Let u = inverse_jacobi_sd(x). Then jacobi_sd(u) = x or
4067 ;; x = jacobi_sn(u)/jacobi_dn(u)
4070 ;; = sn^2/(1-m*sn^2)
4074 ;; sn^2 = x^2/(1+m*x^2)
4076 ;; sn(u) = x/sqrt(1+m*x^2)
4078 ;; u = inverse_sn(x/sqrt(1+m*x^2))
4080 (defprop %inverse_jacobi_sd
4082 ;; Whittaker and Watson, example in 22.122
4083 ;; inverse_jacobi_sd(u,m) = integrate(1/sqrt(1-(1-m)*t^2)/sqrt(1+m*t^2), t, 0, u)
4084 ;; -> 1/sqrt(1-(1-m)*x^2)/sqrt(1+m*x^2)
4087 ((mplus) 1 ((mtimes) ((mplus) -
1 m
) ((mexpt) x
2)))
4089 ((mexpt) ((mplus) 1 ((mtimes) m
((mexpt) x
2)))
4092 ; ((%derivative) ((%inverse_jacobi_sd) x m) m 1)
4096 (def-simplifier inverse_jacobi_sd
(u m
)
4097 (cond ((or (float-numerical-eval-p u m
)
4098 (complex-float-numerical-eval-p u m
)
4099 (bigfloat-numerical-eval-p u m
)
4100 (complex-bigfloat-numerical-eval-p u m
))
4101 (ftake '%inverse_jacobi_sn
4102 ($rectform
(div u
(power (add 1 (mul m
(mul u u
))) 1//2)))
4106 ((eql 0 ($ratsimp
(sub u
(div 1 (power (sub 1 m
) 1//2)))))
4107 ;; inverse_jacobi_sd(1/sqrt(1-m), m) = elliptic_kc(m)
4109 ;; We can see this from inverse_jacobi_sd(x,m) =
4110 ;; inverse_jacobi_sn(x/sqrt(1+m*x^2), m). So
4111 ;; inverse_jacobi_sd(1/sqrt(1-m),m) = inverse_jacobi_sn(1,m)
4112 (ftake '%elliptic_kc m
))
4113 ((and (eq $triginverses
'$all
)
4115 (eq (caar u
) '%jacobi_sd
)
4116 (alike1 (third u
) m
))
4117 ;; inverse_jacobi_sd(sd(u)) = u
4123 ;; inverse_jacobi_cs(x)
4125 ;; Let u = inverse_jacobi_cs(x). Then jacobi_cs(u) = x or
4126 ;; 1/x = 1/jacobi_cs(u) = jacobi_sc(u)
4128 ;; u = inverse_sc(1/x)
4130 (defprop %inverse_jacobi_cs
4132 ;; Whittaker and Watson, example in 22.122
4133 ;; inverse_jacobi_cs(u,m) = integrate(1/sqrt(t^2+1)/sqrt(t^2+(1-m)), t, u, inf)
4134 ;; -> -1/sqrt(x^2+1)/sqrt(x^2+(1-m))
4136 ((mexpt) ((mplus) 1 ((mexpt simp ratsimp
) x
2))
4139 ((mtimes simp ratsimp
) -
1 m
)
4140 ((mexpt simp ratsimp
) x
2))
4143 ; ((%derivative) ((%inverse_jacobi_cs) x m) m 1)
4147 (def-simplifier inverse_jacobi_cs
(u m
)
4148 (cond ((or (float-numerical-eval-p u m
)
4149 (complex-float-numerical-eval-p u m
)
4150 (bigfloat-numerical-eval-p u m
)
4151 (complex-bigfloat-numerical-eval-p u m
))
4152 (ftake '%inverse_jacobi_sc
($rectform
(div 1 u
)) m
))
4154 (ftake '%elliptic_kc m
))
4159 ;; inverse_jacobi_cd(x)
4161 ;; Let u = inverse_jacobi_cd(x). Then jacobi_cd(u) = x or
4162 ;; x = jacobi_cn(u)/jacobi_dn(u)
4165 ;; = (1-sn^2)/(1-m*sn^2)
4169 ;; sn^2 = (1-x^2)/(1-m*x^2)
4171 ;; sn(u) = sqrt(1-x^2)/sqrt(1-m*x^2)
4173 ;; u = inverse_sn(sqrt(1-x^2)/sqrt(1-m*x^2))
4175 (defprop %inverse_jacobi_cd
4177 ;; Whittaker and Watson, example in 22.122
4178 ;; inverse_jacobi_cd(u,m) = integrate(1/sqrt(1-t^2)/sqrt(1-m*t^2), t, u, 1)
4179 ;; -> -1/sqrt(1-x^2)/sqrt(1-m*x^2)
4182 ((mplus) 1 ((mtimes) -
1 ((mexpt) x
2)))
4185 ((mplus) 1 ((mtimes) -
1 m
((mexpt) x
2)))
4188 ; ((%derivative) ((%inverse_jacobi_cd) x m) m 1)
4192 (def-simplifier inverse_jacobi_cd
(u m
)
4193 (cond ((or (complex-float-numerical-eval-p u m
)
4194 (complex-bigfloat-numerical-eval-p u m
))
4196 (ftake '%inverse_jacobi_sn
4197 ($rectform
(div (power (mul (sub 1 u
) (add 1 u
)) 1//2)
4198 (power (sub 1 (mul m
(mul u u
))) 1//2)))
4203 (ftake '%elliptic_kc m
))
4204 ((and (eq $triginverses
'$all
)
4206 (eq (caar u
) '%jacobi_cd
)
4207 (alike1 (third u
) m
))
4208 ;; inverse_jacobi_cd(cd(u)) = u
4214 ;; inverse_jacobi_ds(x)
4216 ;; Let u = inverse_jacobi_ds(x). Then jacobi_ds(u) = x or
4217 ;; 1/x = 1/jacobi_ds(u) = jacobi_sd(u)
4219 ;; u = inverse_sd(1/x)
4221 (defprop %inverse_jacobi_ds
4223 ;; Whittaker and Watson, example in 22.122
4224 ;; inverse_jacobi_ds(u,m) = integrate(1/sqrt(t^2-(1-m))/sqrt(t^2+m), t, u, inf)
4225 ;; -> -1/sqrt(x^2-(1-m))/sqrt(x^2+m)
4228 ((mplus) -
1 m
((mexpt simp ratsimp
) x
2))
4231 ((mplus) m
((mexpt simp ratsimp
) x
2))
4234 ; ((%derivative) ((%inverse_jacobi_ds) x m) m 1)
4238 (def-simplifier inverse_jacobi_ds
(u m
)
4239 (cond ((or (float-numerical-eval-p u m
)
4240 (complex-float-numerical-eval-p u m
)
4241 (bigfloat-numerical-eval-p u m
)
4242 (complex-bigfloat-numerical-eval-p u m
))
4243 (ftake '%inverse_jacobi_sd
($rectform
(div 1 u
)) m
))
4244 ((and $trigsign
(mminusp* u
))
4245 (neg (ftake* '%inverse_jacobi_ds
(neg u
) m
)))
4246 ((eql 0 ($ratsimp
(sub u
(power (sub 1 m
) 1//2))))
4247 ;; inverse_jacobi_ds(sqrt(1-m),m) = elliptic_kc(m)
4249 ;; Since inverse_jacobi_ds(sqrt(1-m), m) =
4250 ;; inverse_jacobi_sd(1/sqrt(1-m),m). And we know from
4251 ;; above that this is elliptic_kc(m)
4252 (ftake '%elliptic_kc m
))
4253 ((and (eq $triginverses
'$all
)
4255 (eq (caar u
) '%jacobi_ds
)
4256 (alike1 (third u
) m
))
4257 ;; inverse_jacobi_ds(ds(u)) = u
4264 ;; inverse_jacobi_dc(x)
4266 ;; Let u = inverse_jacobi_dc(x). Then jacobi_dc(u) = x or
4267 ;; 1/x = 1/jacobi_dc(u) = jacobi_cd(u)
4269 ;; u = inverse_cd(1/x)
4271 (defprop %inverse_jacobi_dc
4273 ;; Note: Whittaker and Watson, example in 22.122 says
4274 ;; inverse_jacobi_dc(u,m) = integrate(1/sqrt(t^2-1)/sqrt(t^2-m),
4275 ;; t, u, 1) but that seems wrong. A&S 17.4.47 says
4276 ;; integrate(1/sqrt(t^2-1)/sqrt(t^2-m), t, a, u) =
4277 ;; inverse_jacobi_cd(x,m). Lawden 3.2.8 says the same.
4278 ;; functions.wolfram.com says the derivative is
4279 ;; 1/sqrt(t^2-1)/sqrt(t^2-m).
4282 ((mplus) -
1 ((mexpt simp ratsimp
) x
2))
4286 ((mtimes simp ratsimp
) -
1 m
)
4287 ((mexpt simp ratsimp
) x
2))
4290 ; ((%derivative) ((%inverse_jacobi_dc) x m) m 1)
4294 (def-simplifier inverse_jacobi_dc
(u m
)
4295 (cond ((or (complex-float-numerical-eval-p u m
)
4296 (complex-bigfloat-numerical-eval-p u m
))
4297 (ftake '%inverse_jacobi_cd
($rectform
(div 1 u
)) m
))
4300 ((and (eq $triginverses
'$all
)
4302 (eq (caar u
) '%jacobi_dc
)
4303 (alike1 (third u
) m
))
4304 ;; inverse_jacobi_dc(dc(u)) = u
4310 ;; Convert an inverse Jacobian function into the equivalent elliptic
4313 ;; See A&S 17.4.41-17.4.52.
4314 (defun make-elliptic-f (e)
4317 ((member (caar e
) '(%inverse_jacobi_sc %inverse_jacobi_cs
4318 %inverse_jacobi_nd %inverse_jacobi_dn
4319 %inverse_jacobi_sn %inverse_jacobi_cd
4320 %inverse_jacobi_dc %inverse_jacobi_ns
4321 %inverse_jacobi_nc %inverse_jacobi_ds
4322 %inverse_jacobi_sd %inverse_jacobi_cn
))
4323 ;; We have some inverse Jacobi function. Convert it to the F form.
4324 (destructuring-bind ((fn &rest ops
) u m
)
4326 (declare (ignore ops
))
4330 (ftake '%elliptic_f
(ftake '%atan u
) m
))
4333 (ftake '%elliptic_f
(ftake '%atan
(div 1 u
)) m
))
4338 (mul (power m -
1//2)
4340 (power (add -
1 (mul u u
))
4348 (power (sub 1 (power u
2)) 1//2)))
4352 (ftake '%elliptic_f
(ftake '%asin u
) m
))
4357 (power (mul (sub 1 (mul u u
))
4358 (sub 1 (mul m u u
)))
4365 (power (mul (sub (mul u u
) 1)
4371 (ftake '%elliptic_f
(ftake '%asin
(div 1 u
)) m
))
4374 (ftake '%elliptic_f
(ftake '%acos
(div 1 u
)) m
))
4379 (power (add m
(mul u u
))
4387 (power (add 1 (mul m u u
))
4392 (ftake '%elliptic_f
(ftake '%acos u
) m
)))))
4394 (recur-apply #'make-elliptic-f e
))))
4396 (defmfun $make_elliptic_f
(e)
4399 (simplify (make-elliptic-f e
))))
4401 (defun make-elliptic-e (e)
4403 ((eq (caar e
) '$elliptic_eu
)
4404 (destructuring-bind ((ffun &rest ops
) u m
) e
4405 (declare (ignore ffun ops
))
4406 (ftake '%elliptic_e
(ftake '%asin
(ftake '%jacobi_sn u m
)) m
)))
4408 (recur-apply #'make-elliptic-e e
))))
4410 (defmfun $make_elliptic_e
(e)
4413 (simplify (make-elliptic-e e
))))
4416 ;; Eu(u,m) = integrate(jacobi_dn(v,m)^2,v,0,u)
4417 ;; = integrate(sqrt((1-m*t^2)/(1-t^2)),t,0,jacobi_sn(u,m))
4419 ;; Eu(u,m) = E(am(u),m)
4421 ;; where E(u,m) is elliptic-e above.
4424 ;; Lawden gives the following relationships
4426 ;; E(u+v) = E(u) + E(v) - m*sn(u)*sn(v)*sn(u+v)
4427 ;; E(u,0) = u, E(u,1) = tanh u
4429 ;; E(i*u,k) = i*sc(u,k')*dn(u,k') - i*E(u,k') + i*u
4431 ;; E(2*i*K') = 2*i*(K'-E')
4433 ;; E(u + 2*i*K') = E(u) + 2*i*(K' - E')
4435 ;; E(u+K) = E(u) + E - k^2*sn(u)*cd(u)
4436 (defun elliptic-eu (u m
)
4438 ;; E(u + 2*n*K) = E(u) + 2*n*E
4439 (let ((ell-k (to (elliptic-k m
)))
4440 (ell-e (elliptic-ec m
)))
4441 (multiple-value-bind (n u-rem
)
4442 (floor u
(* 2 ell-k
))
4445 (cond ((>= u-rem ell-k
)
4446 ;; 0 <= u-rem < K so
4447 ;; E(u + K) = E(u) + E - m*sn(u)*cd(u)
4448 (let ((u-k (- u ell-k
)))
4449 (- (+ (elliptic-e (cl:asin
(bigfloat::sn u-k m
)) m
)
4451 (/ (* m
(bigfloat::sn u-k m
) (bigfloat::cn u-k m
))
4452 (bigfloat::dn u-k m
)))))
4454 (elliptic-e (cl:asin
(bigfloat::sn u m
)) m
)))))))
4458 ;; E(u+i*v, m) = E(u,m) -i*E(v,m') + i*v + i*sc(v,m')*dn(v,m')
4459 ;; -i*m*sn(u,m)*sc(v,m')*sn(u+i*v,m)
4461 (let ((u-r (realpart u
))
4464 (+ (elliptic-eu u-r m
)
4467 (/ (* (bigfloat::sn u-i m1
) (bigfloat::dn u-i m1
))
4468 (bigfloat::cn u-i m1
)))
4469 (+ (elliptic-eu u-i m1
)
4470 (/ (* m
(bigfloat::sn u-r m
) (bigfloat::sn u-i m1
) (bigfloat::sn u m
))
4471 (bigfloat::cn u-i m1
))))))))))
4473 (defprop $elliptic_eu
4475 ((mexpt) ((%jacobi_dn
) u m
) 2)
4480 (def-simplifier elliptic_eu
(u m
)
4482 ;; as it stands, ELLIPTIC-EU can't handle bigfloats or complex bigfloats,
4483 ;; so handle only floats and complex floats here.
4484 ((float-numerical-eval-p u m
)
4485 (elliptic-eu ($float u
) ($float m
)))
4486 ((complex-float-numerical-eval-p u m
)
4487 (let ((u-r ($realpart u
))
4490 (complexify (elliptic-eu (complex u-r u-i
) m
))))
4494 (def-simplifier jacobi_am
(u m
)
4496 ;; as it stands, BIGFLOAT::SN can't handle bigfloats or complex bigfloats,
4497 ;; so handle only floats and complex floats here.
4498 ((float-numerical-eval-p u m
)
4499 (cl:asin
(bigfloat::sn
($float u
) ($float m
))))
4500 ((complex-float-numerical-eval-p u m
)
4501 (let ((u-r ($realpart
($float u
)))
4502 (u-i ($imagpart
($float u
)))
4504 (complexify (cl:asin
(bigfloat::sn
(complex u-r u-i
) m
)))))
4509 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
4510 ;; Integrals. At present with respect to first argument only.
4511 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
4513 ;; A&S 16.24.1: integrate(jacobi_sn(u,m),u)
4514 ;; = log(jacobi_dn(u,m)-sqrt(m)*jacobi_cn(u,m))/sqrt(m)
4517 ((mtimes simp
) ((mexpt simp
) m
((rat simp
) -
1 2))
4520 ((mtimes simp
) -
1 ((mexpt simp
) m
((rat simp
) 1 2))
4521 ((%jacobi_cn simp
) u m
))
4522 ((%jacobi_dn simp
) u m
))))
4526 ;; A&S 16.24.2: integrate(jacobi_cn(u,m),u) = acos(jacobi_dn(u,m))/sqrt(m)
4529 ((mtimes simp
) ((mexpt simp
) m
((rat simp
) -
1 2))
4530 ((%acos simp
) ((%jacobi_dn simp
) u m
)))
4534 ;; A&S 16.24.3: integrate(jacobi_dn(u,m),u) = asin(jacobi_sn(u,m))
4537 ((%asin simp
) ((%jacobi_sn simp
) u m
))
4541 ;; A&S 16.24.4: integrate(jacobi_cd(u,m),u)
4542 ;; = log(jacobi_nd(u,m)+sqrt(m)*jacobi_sd(u,m))/sqrt(m)
4545 ((mtimes simp
) ((mexpt simp
) m
((rat simp
) -
1 2))
4547 ((mplus simp
) ((%jacobi_nd simp
) u m
)
4548 ((mtimes simp
) ((mexpt simp
) m
((rat simp
) 1 2))
4549 ((%jacobi_sd simp
) u m
)))))
4553 ;; integrate(jacobi_sd(u,m),u)
4555 ;; A&S 16.24.5 gives
4556 ;; asin(-sqrt(m)*jacobi_cd(u,m))/sqrt(m*m_1), where m + m_1 = 1
4557 ;; but this does not pass some simple tests.
4559 ;; functions.wolfram.com 09.35.21.001.01 gives
4560 ;; -asin(sqrt(m)*jacobi_cd(u,m))*sqrt(1-m*jacobi_cd(u,m)^2)*jacobi_dn(u,m)/((1-m)*sqrt(m))
4561 ;; and this does pass.
4565 ((mexpt simp
) ((mplus simp
) 1 ((mtimes simp
) -
1 m
)) -
1)
4566 ((mexpt simp
) m
((rat simp
) -
1 2))
4569 ((mtimes simp
) -
1 $m
((mexpt simp
) ((%jacobi_cd simp
) u m
) 2)))
4571 ((%jacobi_dn simp
) u m
)
4573 ((mtimes simp
) ((mexpt simp
) m
((rat simp
) 1 2))
4574 ((%jacobi_cd simp
) u m
))))
4578 ;; integrate(jacobi_nd(u,m),u)
4580 ;; A&S 16.24.6 gives
4581 ;; acos(jacobi_cd(u,m))/sqrt(m_1), where m + m_1 = 1
4582 ;; but this does not pass some simple tests.
4584 ;; functions.wolfram.com 09.32.21.0001.01 gives
4585 ;; sqrt(1-jacobi_cd(u,m)^2)*acos(jacobi_cd(u,m))/((1-m)*jacobi_sd(u,m))
4586 ;; and this does pass.
4589 ((mtimes simp
) ((mexpt simp
) ((mplus simp
) 1 ((mtimes simp
) -
1 m
)) -
1)
4592 ((mtimes simp
) -
1 ((mexpt simp
) ((%jacobi_cd simp
) u m
) 2)))
4594 ((mexpt simp
) ((%jacobi_sd simp
) u m
) -
1)
4595 ((%acos simp
) ((%jacobi_cd simp
) u m
)))
4599 ;; A&S 16.24.7: integrate(jacobi_dc(u,m),u) = log(jacobi_nc(u,m)+jacobi_sc(u,m))
4602 ((%log simp
) ((mplus simp
) ((%jacobi_nc simp
) u m
) ((%jacobi_sc simp
) u m
)))
4606 ;; A&S 16.24.8: integrate(jacobi_nc(u,m),u)
4607 ;; = log(jacobi_dc(u,m)+sqrt(m_1)*jacobi_sc(u,m))/sqrt(m_1), where m + m_1 = 1
4611 ((mexpt simp
) ((mplus simp
) 1 ((mtimes simp
) -
1 m
))
4614 ((mplus simp
) ((%jacobi_dc simp
) u m
)
4616 ((mexpt simp
) ((mplus simp
) 1 ((mtimes simp
) -
1 m
))
4618 ((%jacobi_sc simp
) u m
)))))
4622 ;; A&S 16.24.9: integrate(jacobi_sc(u,m),u)
4623 ;; = log(jacobi_dc(u,m)+sqrt(m_1)*jacobi_nc(u,m))/sqrt(m_1), where m + m_1 = 1
4627 ((mexpt simp
) ((mplus simp
) 1 ((mtimes simp
) -
1 m
))
4630 ((mplus simp
) ((%jacobi_dc simp
) u m
)
4632 ((mexpt simp
) ((mplus simp
) 1 ((mtimes simp
) -
1 m
))
4634 ((%jacobi_nc simp
) u m
)))))
4638 ;; A&S 16.24.10: integrate(jacobi_ns(u,m),u)
4639 ;; = log(jacobi_ds(u,m)-jacobi_cs(u,m))
4643 ((mplus simp
) ((mtimes simp
) -
1 ((%jacobi_cs simp
) u m
))
4644 ((%jacobi_ds simp
) u m
)))
4648 ;; integrate(jacobi_ds(u,m),u)
4650 ;; A&S 16.24.11 gives
4651 ;; log(jacobi_ds(u,m)-jacobi_cs(u,m))
4652 ;; but this does not pass some simple tests.
4654 ;; functions.wolfram.com 09.30.21.0001.01 gives
4655 ;; log((1-jacobi_cn(u,m))/jacobi_sn(u,m))
4661 ((mplus simp
) 1 ((mtimes simp
) -
1 ((%jacobi_cn simp
) u m
)))
4662 ((mexpt simp
) ((%jacobi_sn simp
) u m
) -
1)))
4666 ;; A&S 16.24.12: integrate(jacobi_cs(u,m),u) = log(jacobi_ns(u,m)-jacobi_ds(u,m))
4670 ((mplus simp
) ((mtimes simp
) -
1 ((%jacobi_ds simp
) u m
))
4671 ((%jacobi_ns simp
) u m
)))
4675 ;; functions.wolfram.com 09.48.21.0001.01
4676 ;; integrate(inverse_jacobi_sn(u,m),u) =
4677 ;; inverse_jacobi_sn(u,m)*u
4678 ;; - log( jacobi_dn(inverse_jacobi_sn(u,m),m)
4679 ;; -sqrt(m)*jacobi_cn(inverse_jacobi_sn(u,m),m)) / sqrt(m)
4680 (defprop %inverse_jacobi_sn
4682 ((mplus simp
) ((mtimes simp
) u
((%inverse_jacobi_sn simp
) u m
))
4683 ((mtimes simp
) -
1 ((mexpt simp
) m
((rat simp
) -
1 2))
4686 ((mtimes simp
) -
1 ((mexpt simp
) m
((rat simp
) 1 2))
4687 ((%jacobi_cn simp
) ((%inverse_jacobi_sn simp
) u m
) m
))
4688 ((%jacobi_dn simp
) ((%inverse_jacobi_sn simp
) u m
) m
)))))
4692 ;; functions.wolfram.com 09.38.21.0001.01
4693 ;; integrate(inverse_jacobi_cn(u,m),u) =
4694 ;; u*inverse_jacobi_cn(u,m)
4695 ;; -%i*log(%i*jacobi_dn(inverse_jacobi_cn(u,m),m)/sqrt(m)
4696 ;; -jacobi_sn(inverse_jacobi_cn(u,m),m))
4698 (defprop %inverse_jacobi_cn
4700 ((mplus simp
) ((mtimes simp
) u
((%inverse_jacobi_cn simp
) u m
))
4701 ((mtimes simp
) -
1 $%i
((mexpt simp
) m
((rat simp
) -
1 2))
4704 ((mtimes simp
) $%i
((mexpt simp
) m
((rat simp
) -
1 2))
4705 ((%jacobi_dn simp
) ((%inverse_jacobi_cn simp
) u m
) m
))
4707 ((%jacobi_sn simp
) ((%inverse_jacobi_cn simp
) u m
) m
))))))
4711 ;; functions.wolfram.com 09.41.21.0001.01
4712 ;; integrate(inverse_jacobi_dn(u,m),u) =
4713 ;; u*inverse_jacobi_dn(u,m)
4714 ;; - %i*log(%i*jacobi_cn(inverse_jacobi_dn(u,m),m)
4715 ;; +jacobi_sn(inverse_jacobi_dn(u,m),m))
4716 (defprop %inverse_jacobi_dn
4718 ((mplus simp
) ((mtimes simp
) u
((%inverse_jacobi_dn simp
) u m
))
4719 ((mtimes simp
) -
1 $%i
4723 ((%jacobi_cn simp
) ((%inverse_jacobi_dn simp
) u m
) m
))
4724 ((%jacobi_sn simp
) ((%inverse_jacobi_dn simp
) u m
) m
)))))
4729 ;; Real and imaginary part for Jacobi elliptic functions.
4730 (defprop %jacobi_sn risplit-sn-cn-dn risplit-function
)
4731 (defprop %jacobi_cn risplit-sn-cn-dn risplit-function
)
4732 (defprop %jacobi_dn risplit-sn-cn-dn risplit-function
)
4734 (defun risplit-sn-cn-dn (expr)
4735 (let* ((arg (second expr
))
4736 (param (third expr
)))
4737 ;; We only split on the argument, not the order
4738 (destructuring-bind (arg-r . arg-i
)
4742 (cons (take (first expr
) arg-r param
)
4745 (let* ((s (ftake '%jacobi_sn arg-r param
))
4746 (c (ftake '%jacobi_cn arg-r param
))
4747 (d (ftake '%jacobi_dn arg-r param
))
4748 (s1 (ftake '%jacobi_sn arg-i
(sub 1 param
)))
4749 (c1 (ftake '%jacobi_cn arg-i
(sub 1 param
)))
4750 (d1 (ftake '%jacobi_dn arg-i
(sub 1 param
)))
4751 (den (add (mul c1 c1
)
4755 ;; Let s = jacobi_sn(x,m)
4756 ;; c = jacobi_cn(x,m)
4757 ;; d = jacobi_dn(x,m)
4758 ;; s1 = jacobi_sn(y,1-m)
4759 ;; c1 = jacobi_cn(y,1-m)
4760 ;; d1 = jacobi_dn(y,1-m)
4764 ;; jacobi_sn(x+%i*y,m) =
4766 ;; s*d1 + %i*c*d*s1*c1
4767 ;; -------------------
4770 (cons (div (mul s d1
) den
)
4771 (div (mul c
(mul d
(mul s1 c1
)))
4778 ;; c*c1 - %i*s*d*s1*d1
4779 ;; -------------------
4781 (cons (div (mul c c1
) den
)
4783 (mul s
(mul d
(mul s1 d1
))))
4790 ;; d*c1*d1 - %i*m*s*c*s1
4791 ;; ---------------------
4793 (cons (div (mul d
(mul c1 d1
))
4795 (div (mul -
1 (mul param
(mul s
(mul c s1
))))