1 ;;; -*- Mode: Lisp; Package: Maxima; Syntax: Common-Lisp; Base: 10-*- ;;;;
2 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3 ;;; The data in this file contains enhancements. ;;;;;
5 ;;; Copyright (c) 1984,1987 by William Schelter,University of Texas ;;;;;
6 ;;; All rights reserved ;;;;;
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)
18 ;;; Jacobian elliptic functions and elliptic integrals.
22 ;;; [1] Abramowitz and Stegun
23 ;;; [2] Lawden, Elliptic Functions and Applications, Springer-Verlag, 1989
24 ;;; [3] Whittaker & Watson, A Course of Modern Analysis
26 ;;; We use the definitions from Abramowitz and Stegun where our
27 ;;; sn(u,m) is sn(u|m). That is, the second arg is the parameter,
28 ;;; instead of the modulus k or modular angle alpha.
30 ;;; Note that m = k^2 and k = sin(alpha).
34 ;; Routines for computing the basic elliptic functions sn, cn, and dn.
37 ;; A&S gives several methods for computing elliptic functions
38 ;; including the AGM method (16.4) and ascending and descending Landen
39 ;; transformations (16.12 and 16.14). The latter are actually quite
40 ;; fast, only requiring simple arithmetic and square roots for the
41 ;; transformation until the last step. The AGM requires evaluation of
42 ;; several trigonometric functions at each stage.
44 ;; However, the Landen transformations appear to have some round-off
45 ;; issues. For example, using the ascending transform to compute cn,
46 ;; cn(100,.7) > 1e10. This is clearly not right since |cn| <= 1.
49 (in-package #:bigfloat
)
51 (declaim (inline descending-transform ascending-transform
))
53 (defun ascending-transform (u m
)
56 ;; Take care in computing this transform. For the case where
57 ;; m is complex, we should compute sqrt(mu1) first as
58 ;; (1-sqrt(m))/(1+sqrt(m)), and then square this to get mu1.
59 ;; If not, we may choose the wrong branch when computing
61 (let* ((root-m (sqrt m
))
63 (expt (1+ root-m
) 2)))
64 (root-mu1 (/ (- 1 root-m
) (+ 1 root-m
)))
65 (v (/ u
(1+ root-mu1
))))
66 (values v mu root-mu1
)))
68 (defun descending-transform (u m
)
69 ;; Note: Don't calculate mu first, as given in 16.12.1. We
70 ;; should calculate sqrt(mu) = (1-sqrt(m1)/(1+sqrt(m1)), and
71 ;; then compute mu = sqrt(mu)^2. If we calculate mu first,
72 ;; sqrt(mu) loses information when m or m1 is complex.
73 (let* ((root-m1 (sqrt (- 1 m
)))
74 (root-mu (/ (- 1 root-m1
) (+ 1 root-m1
)))
75 (mu (* root-mu root-mu
))
76 (v (/ u
(1+ root-mu
))))
77 (values v mu root-mu
)))
80 ;; This appears to work quite well for both real and complex values
82 (defun elliptic-sn-descending (u m
)
86 ((< (abs m
) (epsilon u
))
90 (multiple-value-bind (v mu root-mu
)
91 (descending-transform u m
)
92 (let* ((new-sn (elliptic-sn-descending v mu
)))
93 (/ (* (1+ root-mu
) new-sn
)
94 (1+ (* root-mu new-sn new-sn
))))))))
96 ;; AGM scale. See A&S 17.6
100 ;; 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.
102 ;; We stop when abs(c[n]) <= 10*eps
104 ;; A list of (n a[n] b[n] c[n]) is returned.
105 (defun agm-scale (a b c
)
107 while
(> (abs c
) (* 10 (epsilon c
)))
108 collect
(list n a b c
)
109 do
(psetf a
(/ (+ a b
) 2)
113 ;; WARNING: This seems to have accuracy problems when u is complex. I
114 ;; (rtoy) do not know why. For example (jacobi-agm #c(1e0 1e0) .7e0)
117 ;; #C(1.134045970915582 0.3522523454566013)
118 ;; #C(0.57149659007575 -0.6989899153338323)
119 ;; #C(0.6229715431044184 -0.4488635962149656)
121 ;; But the actual value of sn(1+%i, .7) is .3522523469224946 %i +
122 ;; 1.134045971912365. We've lost about 7 digits of accuracy!
123 (defun jacobi-agm (u m
)
126 ;; Compute the AGM scale with a = 1, b = sqrt(1-m), c = sqrt(m).
128 ;; Then phi[N] = 2^N*a[N]*u and compute phi[n] from
130 ;; sin(2*phi[n-1] - phi[n]) = c[n]/a[n]*sin(phi[n])
134 ;; sn(u|m) = sin(phi[0]), cn(u|m) = cos(phi[0])
135 ;; dn(u|m) = cos(phi[0])/cos(phi[1]-phi[0])
137 ;; Returns the three values sn, cn, dn.
138 (let* ((agm-data (nreverse (rest (agm-scale 1 (sqrt (- 1 m
)) (sqrt m
)))))
139 (phi (destructuring-bind (n a b c
)
141 (declare (ignore b c
))
144 (dolist (agm agm-data
)
145 (destructuring-bind (n a b c
)
147 (declare (ignore n b
))
149 phi
(/ (+ phi
(asin (* (/ c a
) (sin phi
)))) 2))))
150 (values (sin phi
) (cos phi
) (/ (cos phi
) (cos (- phi1 phi
))))))
154 ;; jacobi_sn(u,0) = sin(u). Should we use A&S 16.13.1 if m
157 ;; sn(u,m) = sin(u) - 1/4*m(u-sin(u)*cos(u))*cos(u)
160 ;; jacobi_sn(u,1) = tanh(u). Should we use A&S 16.15.1 if m
161 ;; is close enough to 1?
163 ;; sn(u,m) = tanh(u) + 1/4*(1-m)*(sinh(u)*cosh(u)-u)*sech(u)^2
166 ;; Use the ascending Landen transformation to compute sn.
167 (let ((s (elliptic-sn-descending u m
)))
168 (if (and (realp u
) (realp m
))
174 ;; jacobi_dn(u,0) = 1. Should we use A&S 16.13.3 for small m?
176 ;; dn(u,m) = 1 - 1/2*m*sin(u)^2
179 ;; jacobi_dn(u,1) = sech(u). Should we use A&S 16.15.3 if m
180 ;; is close enough to 1?
182 ;; dn(u,m) = sech(u) + 1/4*(1-m)*(sinh(u)*cosh(u)+u)*tanh(u)*sech(u)
185 ;; Use the Gauss transformation from
186 ;; http://functions.wolfram.com/09.29.16.0013.01:
189 ;; dn((1+sqrt(m))*z, 4*sqrt(m)/(1+sqrt(m))^2)
190 ;; = (1-sqrt(m)*sn(z, m)^2)/(1+sqrt(m)*sn(z,m)^2)
194 ;; dn(y, mu) = (1-sqrt(m)*sn(z, m)^2)/(1+sqrt(m)*sn(z,m)^2)
196 ;; where z = y/(1+sqrt(m)) and mu=4*sqrt(m)/(1+sqrt(m))^2.
198 ;; Solve for m, and we get
200 ;; sqrt(m) = -(mu+2*sqrt(1-mu)-2)/mu or (-mu+2*sqrt(1-mu)+2)/mu.
202 ;; I don't think it matters which sqrt we use, so I (rtoy)
203 ;; arbitrarily choose the first one above.
205 ;; Note that (1-sqrt(1-mu))/(1+sqrt(1-mu)) is the same as
206 ;; -(mu+2*sqrt(1-mu)-2)/mu. Also, the former is more
207 ;; accurate for small mu.
208 (let* ((root (let ((root-1-m (sqrt (- 1 m
))))
212 (s (elliptic-sn-descending z
(* root root
)))
219 ;; jacobi_cn(u,0) = cos(u). Should we use A&S 16.13.2 for
222 ;; cn(u,m) = cos(u) + 1/4*m*(u-sin(u)*cos(u))*sin(u)
225 ;; jacobi_cn(u,1) = sech(u). Should we use A&S 16.15.3 if m
226 ;; is close enough to 1?
228 ;; cn(u,m) = sech(u) - 1/4*(1-m)*(sinh(u)*cosh(u)-u)*tanh(u)*sech(u)
231 ;; Use the ascending Landen transformation, A&S 16.14.3.
232 (multiple-value-bind (v mu root-mu1
)
233 (ascending-transform u m
)
235 (* (/ (+ 1 root-mu1
) mu
)
236 (/ (- (* d d
) root-mu1
)
241 ;; Tell maxima what the derivatives are.
243 ;; Lawden says the derivative wrt to k but that's not what we want.
245 ;; Here's the derivation we used, based on how Lawden get's his results.
249 ;; diff(sn(u,m),m) = s
250 ;; diff(cn(u,m),m) = p
251 ;; diff(dn(u,m),m) = q
253 ;; From the derivatives of sn, cn, dn wrt to u, we have
255 ;; diff(sn(u,m),u) = cn(u)*dn(u)
256 ;; diff(cn(u,m),u) = -cn(u)*dn(u)
257 ;; diff(dn(u,m),u) = -m*sn(u)*cn(u)
260 ;; Differentiate these wrt to m:
262 ;; diff(s,u) = p*dn + cn*q
263 ;; diff(p,u) = -p*dn - q*dn
264 ;; diff(q,u) = -sn*cn - m*s*cn - m*sn*q
268 ;; sn(u)^2 + cn(u)^2 = 1
269 ;; dn(u)^2 + m*sn(u)^2 = 1
271 ;; Differentiate these wrt to m:
274 ;; 2*dn*q + sn^2 + 2*m*sn*s = 0
279 ;; q = -m*s*sn/dn - sn^2/dn/2
282 ;; diff(s,u) = -s*sn*dn/cn - m*s*sn*cn/dn - sn^2*cn/dn/2
286 ;; diff(s,u) + s*(sn*dn/cn + m*sn*cn/dn) = -1/2*sn^2*cn/dn
288 ;; diff(s,u) + s*sn/cn/dn*(dn^2 + m*cn^2) = -1/2*sn^2*cn/dn
290 ;; Multiply through by the integrating factor 1/cn/dn:
292 ;; diff(s/cn/dn, u) = -1/2*sn^2/dn^2 = -1/2*sd^2.
294 ;; Integrate this to get
296 ;; s/cn/dn = C + -1/2*int sd^2
298 ;; It can be shown that C is zero.
300 ;; We know that (by differentiating this expression)
302 ;; int dn^2 = (1-m)*u+m*sn*cd + m*(1-m)*int sd^2
306 ;; int sd^2 = 1/m/(1-m)*int dn^2 - u/m - sn*cd/(1-m)
310 ;; s/cn/dn = u/(2*m) + sn*cd/(2*(1-m)) - 1/2/m/(1-m)*int dn^2
314 ;; s = 1/(2*m)*u*cn*dn + 1/(2*(1-m))*sn*cn^2 - 1/2/(m*(1-m))*cn*dn*E(u)
316 ;; where E(u) = int dn^2 = elliptic_e(am(u)) = elliptic_e(asin(sn(u)))
318 ;; This is our desired result:
320 ;; s = 1/(2*m)*cn*dn*[u - elliptic_e(asin(sn(u)),m)/(1-m)] + sn*cn^2/2/(1-m)
323 ;; Since diff(cn(u,m),m) = p = -s*sn/cn, we have
325 ;; p = -1/(2*m)*sn*dn[u - elliptic_e(asin(sn(u)),m)/(1-m)] - sn^2*cn/2/(1-m)
327 ;; diff(dn(u,m),m) = q = -m*s*sn/dn - sn^2/dn/2
329 ;; q = -1/2*sn*cn*[u-elliptic_e(asin(sn),m)/(1-m)] - m*sn^2*cn^2/dn/2/(1-m)
333 ;; = -1/2*sn*cn*[u-elliptic_e(asin(sn),m)/(1-m)] + dn*sn^2/2/(m-1)
337 ((mtimes) ((%jacobi_cn
) u m
) ((%jacobi_dn
) u m
))
339 ((mtimes simp
) ((rat simp
) 1 2)
340 ((mexpt simp
) ((mplus simp
) 1 ((mtimes simp
) -
1 m
)) -
1)
341 ((mexpt simp
) ((%jacobi_cn simp
) u m
) 2) ((%jacobi_sn simp
) u m
))
342 ((mtimes simp
) ((rat simp
) 1 2) ((mexpt simp
) m -
1)
343 ((%jacobi_cn simp
) u m
) ((%jacobi_dn simp
) u m
)
345 ((mtimes simp
) -
1 ((mexpt simp
) ((mplus simp
) 1 ((mtimes simp
) -
1 m
)) -
1)
346 ((%elliptic_e simp
) ((%asin simp
) ((%jacobi_sn simp
) u m
)) m
))))))
351 ((mtimes simp
) -
1 ((%jacobi_sn simp
) u m
) ((%jacobi_dn simp
) u m
))
353 ((mtimes simp
) ((rat simp
) -
1 2)
354 ((mexpt simp
) ((mplus simp
) 1 ((mtimes simp
) -
1 m
)) -
1)
355 ((%jacobi_cn simp
) u m
) ((mexpt simp
) ((%jacobi_sn simp
) u m
) 2))
356 ((mtimes simp
) ((rat simp
) -
1 2) ((mexpt simp
) m -
1)
357 ((%jacobi_dn simp
) u m
) ((%jacobi_sn simp
) u m
)
359 ((mtimes simp
) -
1 ((mexpt simp
) ((mplus simp
) 1 ((mtimes simp
) -
1 m
)) -
1)
360 ((%elliptic_e simp
) ((%asin simp
) ((%jacobi_sn simp
) u m
)) m
))))))
365 ((mtimes) -
1 m
((%jacobi_sn
) u m
) ((%jacobi_cn
) u m
))
367 ((mtimes simp
) ((rat simp
) -
1 2)
368 ((mexpt simp
) ((mplus simp
) 1 ((mtimes simp
) -
1 m
)) -
1)
369 ((%jacobi_dn simp
) u m
) ((mexpt simp
) ((%jacobi_sn simp
) u m
) 2))
370 ((mtimes simp
) ((rat simp
) -
1 2) ((%jacobi_cn simp
) u m
)
371 ((%jacobi_sn simp
) u m
)
374 ((mexpt simp
) ((mplus simp
) 1 ((mtimes simp
) -
1 m
)) -
1)
375 ((%elliptic_e simp
) ((%asin simp
) ((%jacobi_sn simp
) u m
)) m
))))))
378 ;; The inverse elliptic functions.
380 ;; F(phi|m) = asn(sin(phi),m)
382 ;; so asn(u,m) = F(asin(u)|m)
383 (defprop %inverse_jacobi_sn
386 ;; inverse_jacobi_sn(x) = integrate(1/sqrt(1-t^2)/sqrt(1-m*t^2),t,0,x)
387 ;; -> 1/sqrt(1-x^2)/sqrt(1-m*x^2)
389 ((mexpt simp
) ((mplus simp
) 1 ((mtimes simp
) -
1 ((mexpt simp
) x
2)))
391 ((mexpt simp
) ((mplus simp
) 1 ((mtimes simp
) -
1 m
((mexpt simp
) x
2)))
393 ;; diff(F(asin(u)|m),m)
394 ((mtimes simp
) ((mexpt simp
) ((mplus simp
) 1 ((mtimes simp
) -
1 m
)) -
1)
397 ((mexpt simp
) ((mplus simp
) 1 ((mtimes simp
) -
1 ((mexpt simp
) x
2)))
399 ((mexpt simp
) ((mplus simp
) 1 ((mtimes simp
) -
1 m
((mexpt simp
) x
2)))
401 ((mtimes simp
) ((mexpt simp
) m -
1)
402 ((mplus simp
) ((%elliptic_e simp
) ((%asin simp
) x
) m
)
403 ((mtimes simp
) -
1 ((mplus simp
) 1 ((mtimes simp
) -
1 m
))
404 ((%elliptic_f simp
) ((%asin simp
) x
) m
)))))))
407 ;; Let u = inverse_jacobi_cn(x). Then jacobi_cn(u) = x or
408 ;; sqrt(1-jacobi_sn(u)^2) = x. Or
410 ;; jacobi_sn(u) = sqrt(1-x^2)
412 ;; So u = inverse_jacobi_sn(sqrt(1-x^2),m) = inverse_jacob_cn(x,m)
414 (defprop %inverse_jacobi_cn
416 ;; Whittaker and Watson, 22.121
417 ;; inverse_jacobi_cn(u,m) = integrate(1/sqrt(1-t^2)/sqrt(1-m+m*t^2), t, u, 1)
418 ;; -> -1/sqrt(1-x^2)/sqrt(1-m+m*x^2)
420 ((mexpt simp
) ((mplus simp
) 1 ((mtimes simp
) -
1 ((mexpt simp
) x
2)))
423 ((mplus simp
) 1 ((mtimes simp
) -
1 m
)
424 ((mtimes simp
) m
((mexpt simp
) x
2)))
426 ((mtimes simp
) ((mexpt simp
) ((mplus simp
) 1 ((mtimes simp
) -
1 m
)) -
1)
431 ((mtimes simp
) -
1 m
((mplus simp
) 1 ((mtimes simp
) -
1 ((mexpt simp
) x
2)))))
433 ((mexpt simp
) ((mplus simp
) 1 ((mtimes simp
) -
1 ((mexpt simp
) x
2))) ((rat simp
) 1 2))
435 ((mtimes simp
) ((mexpt simp
) m -
1)
439 ((mexpt simp
) ((mplus simp
) 1 ((mtimes simp
) -
1 ((mexpt simp
) x
2))) ((rat simp
) 1 2)))
441 ((mtimes simp
) -
1 ((mplus simp
) 1 ((mtimes simp
) -
1 m
))
444 ((mexpt simp
) ((mplus simp
) 1 ((mtimes simp
) -
1 ((mexpt simp
) x
2))) ((rat simp
) 1 2)))
448 ;; Let u = inverse_jacobi_dn(x). Then
450 ;; jacobi_dn(u) = x or
452 ;; x^2 = jacobi_dn(u)^2 = 1 - m*jacobi_sn(u)^2
454 ;; so jacobi_sn(u) = sqrt(1-x^2)/sqrt(m)
456 ;; or u = inverse_jacobi_sn(sqrt(1-x^2)/sqrt(m))
457 (defprop %inverse_jacobi_dn
459 ;; Whittaker and Watson, 22.121
460 ;; inverse_jacobi_dn(u,m) = integrate(1/sqrt(1-t^2)/sqrt(t^2-(1-m)), t, u, 1)
461 ;; -> -1/sqrt(1-x^2)/sqrt(x^2+m-1)
463 ((mexpt simp
) ((mplus simp
) 1 ((mtimes simp
) -
1 ((mexpt simp
) x
2)))
465 ((mexpt simp
) ((mplus simp
) -
1 m
((mexpt simp
) x
2)) ((rat simp
) -
1 2)))
467 ((mtimes simp
) ((rat simp
) -
1 2) ((mexpt simp
) m
((rat simp
) -
3 2))
470 ((mtimes simp
) -
1 ((mexpt simp
) m -
1)
471 ((mplus simp
) 1 ((mtimes simp
) -
1 ((mexpt simp
) x
2)))))
473 ((mexpt simp
) ((mplus simp
) 1 ((mtimes simp
) -
1 ((mexpt simp
) x
2)))
475 ((mexpt simp
) ((mabs simp
) x
) -
1))
476 ((mtimes simp
) ((mexpt simp
) ((mplus simp
) 1 ((mtimes simp
) -
1 m
)) -
1)
478 ((mtimes simp
) -
1 ((mexpt simp
) m
((rat simp
) -
1 2))
481 ((mtimes simp
) -
1 ((mexpt simp
) m -
1)
482 ((mplus simp
) 1 ((mtimes simp
) -
1 ((mexpt simp
) x
2)))))
484 ((mexpt simp
) ((mplus simp
) 1 ((mtimes simp
) -
1 ((mexpt simp
) x
2)))
486 ((mexpt simp
) ((mabs simp
) x
) -
1))
487 ((mtimes simp
) ((mexpt simp
) m -
1)
491 ((mtimes simp
) ((mexpt simp
) m
((rat simp
) -
1 2))
492 ((mexpt simp
) ((mplus simp
) 1
493 ((mtimes simp
) -
1 ((mexpt simp
) x
2)))
496 ((mtimes simp
) -
1 ((mplus simp
) 1 ((mtimes simp
) -
1 m
))
499 ((mtimes simp
) ((mexpt simp
) m
((rat simp
) -
1 2))
500 ((mexpt simp
) ((mplus simp
) 1
501 ((mtimes simp
) -
1 ((mexpt simp
) x
2)))
507 ;; Possible forms of a complex number:
511 ;; ((mplus simp) 2.3 ((mtimes simp) 2.3 $%i))
512 ;; ((mplus simp) 2.3 $%i))
513 ;; ((mtimes simp) 2.3 $%i)
517 ;; Is argument u a complex number with real and imagpart satisfying predicate ntypep?
518 (defun complex-number-p (u &optional
(ntypep 'numberp
))
520 (labels ((a1 (x) (cadr x
))
523 (N (x) (funcall ntypep x
)) ; N
524 (i (x) (and (eq x
'$%i
) (N 1))) ; %i
525 (N+i
(x) (and (null (a3+ x
)) ; mplus test is precondition
527 (or (and (i (a2 x
)) (setq I
1) t
)
528 (and (mtimesp (a2 x
)) (N*i
(a2 x
))))))
529 (N*i
(x) (and (null (a3+ x
)) ; mtimes test is precondition
532 (declare (inline a1 a2 a3
+ N i N
+i N
*i
))
533 (cond ((N u
) (values t u
0)) ;2.3
534 ((atom u
) (if (i u
) (values t
0 1))) ;%i
535 ((mplusp u
) (if (N+i u
) (values t R I
))) ;N+%i, N+N*%i
536 ((mtimesp u
) (if (N*i u
) (values t R I
))) ;N*%i
539 (defun complexify (x)
540 ;; Convert a Lisp number to a maxima number
542 ((complexp x
) (add (realpart x
) (mul '$%i
(imagpart x
))))
543 (t (merror (intl:gettext
"COMPLEXIFY: argument must be a Lisp real or complex number.~%COMPLEXIFY: found: ~:M") x
))))
545 (defun kc-arg (exp m
)
546 ;; Replace elliptic_kc(m) in the expression with sym. Check to see
547 ;; if the resulting expression is linear in sym and the constant
548 ;; term is zero. If so, return the coefficient of sym, i.e, the
549 ;; coefficient of elliptic_kc(m).
550 (let* ((sym (gensym))
551 (arg (maxima-substitute sym
`((%elliptic_kc
) ,m
) exp
)))
552 (if (and (not (equalp arg exp
))
554 (zerop1 (coefficient arg sym
0)))
555 (coefficient arg sym
1)
558 (defun kc-arg2 (exp m
)
559 ;; Replace elliptic_kc(m) in the expression with sym. Check to see
560 ;; if the resulting expression is linear in sym and the constant
561 ;; term is zero. If so, return the coefficient of sym, i.e, the
562 ;; coefficient of elliptic_kc(m), and the constant term. Otherwise,
564 (let* ((sym (gensym))
565 (arg (maxima-substitute sym
`((%elliptic_kc
) ,m
) exp
)))
566 (if (and (not (equalp arg exp
))
568 (list (coefficient arg sym
1)
569 (coefficient arg sym
0))
572 ;; Tell maxima how to simplify the functions
574 (def-simplifier jacobi_sn
(u m
)
577 ((float-numerical-eval-p u m
)
578 (to (bigfloat::sn
(bigfloat:to
($float u
)) (bigfloat:to
($float m
)))))
579 ((setf args
(complex-float-numerical-eval-p u m
))
580 (destructuring-bind (u m
)
582 (to (bigfloat::sn
(bigfloat:to
($float u
)) (bigfloat:to
($float m
))))))
583 ((bigfloat-numerical-eval-p u m
)
584 (to (bigfloat::sn
(bigfloat:to
($bfloat u
)) (bigfloat:to
($bfloat m
)))))
585 ((setf args
(complex-bigfloat-numerical-eval-p u m
))
586 (destructuring-bind (u m
)
588 (to (bigfloat::sn
(bigfloat:to
($bfloat u
)) (bigfloat:to
($bfloat m
))))))
598 ((and $trigsign
(mminusp* u
))
599 (neg (ftake* '%jacobi_sn
(neg u
) m
)))
602 (member (caar u
) '(%inverse_jacobi_sn
614 (alike1 (third u
) m
))
615 (let ((inv-arg (second u
)))
618 ;; jacobi_sn(inverse_jacobi_sn(u,m), m) = u
621 ;; inverse_jacobi_ns(u,m) = inverse_jacobi_sn(1/u,m)
624 ;; sn(x)^2 + cn(x)^2 = 1 so sn(x) = sqrt(1-cn(x)^2)
625 (power (sub 1 (mul inv-arg inv-arg
)) 1//2))
627 ;; inverse_jacobi_nc(u) = inverse_jacobi_cn(1/u)
628 (ftake '%jacobi_sn
(ftake '%inverse_jacobi_cn
(div 1 inv-arg
) m
)
631 ;; dn(x)^2 + m*sn(x)^2 = 1 so
632 ;; sn(x) = 1/sqrt(m)*sqrt(1-dn(x)^2)
633 (mul (div 1 (power m
1//2))
634 (power (sub 1 (mul inv-arg inv-arg
)) 1//2)))
636 ;; inverse_jacobi_nd(u) = inverse_jacobi_dn(1/u)
637 (ftake '%jacobi_sn
(ftake '%inverse_jacobi_dn
(div 1 inv-arg
) m
)
640 ;; See below for inverse_jacobi_sc.
641 (div inv-arg
(power (add 1 (mul inv-arg inv-arg
)) 1//2)))
643 ;; inverse_jacobi_cs(u) = inverse_jacobi_sc(1/u)
644 (ftake '%jacobi_sn
(ftake '%inverse_jacobi_sc
(div 1 inv-arg
) m
)
647 ;; See below for inverse_jacobi_sd
648 (div inv-arg
(power (add 1 (mul m
(mul inv-arg inv-arg
))) 1//2)))
650 ;; inverse_jacobi_ds(u) = inverse_jacobi_sd(1/u)
651 (ftake '%jacobi_sn
(ftake '%inverse_jacobi_sd
(div 1 inv-arg
) m
)
655 (div (power (sub 1 (mul inv-arg inv-arg
)) 1//2)
656 (power (sub 1 (mul m
(mul inv-arg inv-arg
))) 1//2)))
658 (ftake '%jacobi_sn
(ftake '%inverse_jacobi_cd
(div 1 inv-arg
) m
) m
)))))
659 ;; A&S 16.20.1 (Jacobi's Imaginary transformation)
660 ((and $%iargs
(multiplep u
'$%i
))
662 (ftake* '%jacobi_sc
(coeff u
'$%i
1) (add 1 (neg m
)))))
663 ((setq coef
(kc-arg2 u m
))
667 (destructuring-bind (lin const
)
669 (cond ((integerp lin
)
672 ;; sn(4*m*K + u) = sn(u), sn(0) = 0
675 (ftake '%jacobi_sn const m
)))
677 ;; sn(4*m*K + K + u) = sn(K+u) = cd(u)
681 (ftake '%jacobi_cd const m
)))
683 ;; sn(4*m*K+2*K + u) = sn(2*K+u) = -sn(u)
687 (neg (ftake '%jacobi_sn const m
))))
689 ;; sn(4*m*K+3*K+u) = sn(2*K + K + u) = -sn(K+u) = -cd(u)
693 (neg (ftake '%jacobi_cd const m
))))))
694 ((and (alike1 lin
1//2)
698 ;; sn(1/2*K) = 1/sqrt(1+sqrt(1-m))
700 (power (add 1 (power (sub 1 m
) 1//2))
702 ((and (alike1 lin
3//2)
706 ;; sn(1/2*K + K) = cd(1/2*K,m)
707 (ftake '%jacobi_cd
(mul 1//2
708 (ftake '%elliptic_kc m
))
716 (def-simplifier jacobi_cn
(u m
)
719 ((float-numerical-eval-p u m
)
720 (to (bigfloat::cn
(bigfloat:to
($float u
)) (bigfloat:to
($float m
)))))
721 ((setf args
(complex-float-numerical-eval-p u m
))
722 (destructuring-bind (u m
)
724 (to (bigfloat::cn
(bigfloat:to
($float u
)) (bigfloat:to
($float m
))))))
725 ((bigfloat-numerical-eval-p u m
)
726 (to (bigfloat::cn
(bigfloat:to
($bfloat u
)) (bigfloat:to
($bfloat m
)))))
727 ((setf args
(complex-bigfloat-numerical-eval-p u m
))
728 (destructuring-bind (u m
)
730 (to (bigfloat::cn
(bigfloat:to
($bfloat u
)) (bigfloat:to
($bfloat m
))))))
740 ((and $trigsign
(mminusp* u
))
741 (ftake* '%jacobi_cn
(neg u
) m
))
744 (member (caar u
) '(%inverse_jacobi_sn
756 (alike1 (third u
) m
))
757 (cond ((eq (caar u
) '%inverse_jacobi_cn
)
760 ;; I'm lazy. Use cn(x) = sqrt(1-sn(x)^2). Hope
762 (power (sub 1 (power (ftake '%jacobi_sn u
(third u
)) 2))
764 ;; A&S 16.20.2 (Jacobi's Imaginary transformation)
765 ((and $%iargs
(multiplep u
'$%i
))
766 (ftake* '%jacobi_nc
(coeff u
'$%i
1) (add 1 (neg m
))))
767 ((setq coef
(kc-arg2 u m
))
771 (destructuring-bind (lin const
)
773 (cond ((integerp lin
)
776 ;; cn(4*m*K + u) = cn(u),
780 (ftake '%jacobi_cn const m
)))
782 ;; cn(4*m*K + K + u) = cn(K+u) = -sqrt(m1)*sd(u)
786 (neg (mul (power (sub 1 m
) 1//2)
787 (ftake '%jacobi_sd const m
)))))
789 ;; cn(4*m*K + 2*K + u) = cn(2*K+u) = -cn(u)
793 (neg (ftake '%jacobi_cn const m
))))
795 ;; cn(4*m*K + 3*K + u) = cn(2*K + K + u) =
796 ;; -cn(K+u) = sqrt(m1)*sd(u)
801 (mul (power (sub 1 m
) 1//2)
802 (ftake '%jacobi_sd const m
))))))
803 ((and (alike1 lin
1//2)
806 ;; cn(1/2*K) = (1-m)^(1/4)/sqrt(1+sqrt(1-m))
807 (mul (power (sub 1 m
) (div 1 4))
817 (def-simplifier jacobi_dn
(u m
)
820 ((float-numerical-eval-p u m
)
821 (to (bigfloat::dn
(bigfloat:to
($float u
)) (bigfloat:to
($float m
)))))
822 ((setf args
(complex-float-numerical-eval-p u m
))
823 (destructuring-bind (u m
)
825 (to (bigfloat::dn
(bigfloat:to
($float u
)) (bigfloat:to
($float m
))))))
826 ((bigfloat-numerical-eval-p u m
)
827 (to (bigfloat::dn
(bigfloat:to
($bfloat u
)) (bigfloat:to
($bfloat m
)))))
828 ((setf args
(complex-bigfloat-numerical-eval-p u m
))
829 (destructuring-bind (u m
)
831 (to (bigfloat::dn
(bigfloat:to
($bfloat u
)) (bigfloat:to
($bfloat m
))))))
841 ((and $trigsign
(mminusp* u
))
842 (ftake* '%jacobi_dn
(neg u
) m
))
845 (member (caar u
) '(%inverse_jacobi_sn
857 (alike1 (third u
) m
))
858 (cond ((eq (caar u
) '%inverse_jacobi_dn
)
859 ;; jacobi_dn(inverse_jacobi_dn(u,m), m) = u
862 ;; Express in terms of sn:
863 ;; dn(x) = sqrt(1-m*sn(x)^2)
865 (power (ftake '%jacobi_sn u m
) 2)))
867 ((zerop1 ($ratsimp
(sub u
(power (sub 1 m
) 1//2))))
869 ;; dn(sqrt(1-m),m) = K(m)
870 (ftake '%elliptic_kc m
))
871 ;; A&S 16.20.2 (Jacobi's Imaginary transformation)
872 ((and $%iargs
(multiplep u
'$%i
))
873 (ftake* '%jacobi_dc
(coeff u
'$%i
1)
875 ((setq coef
(kc-arg2 u m
))
878 ;; dn(m*K+u) has period 2K
880 (destructuring-bind (lin const
)
882 (cond ((integerp lin
)
885 ;; dn(2*m*K + u) = dn(u)
889 ;; dn(4*m*K+2*K + u) = dn(2*K+u) = dn(u)
890 (ftake '%jacobi_dn const m
)))
892 ;; dn(2*m*K + K + u) = dn(K + u) = sqrt(1-m)*nd(u)
895 (power (sub 1 m
) 1//2)
896 (mul (power (sub 1 m
) 1//2)
897 (ftake '%jacobi_nd const m
))))))
898 ((and (alike1 lin
1//2)
901 ;; dn(1/2*K) = (1-m)^(1/4)
908 ;; Should we simplify the inverse elliptic functions into the
909 ;; appropriate incomplete elliptic integral? I think we should leave
910 ;; it, but perhaps allow some way to do that transformation if
913 (def-simplifier inverse_jacobi_sn
(u m
)
915 ;; To numerically evaluate inverse_jacobi_sn (asn), use
917 ;; asn(x,m) = F(asin(x),m)
919 ;; But F(phi,m) = sin(phi)*rf(cos(phi)^2, 1-m*sin(phi)^2,1). Thus
921 ;; asn(x,m) = F(asin(x),m)
922 ;; = x*rf(1-x^2,1-m*x^2,1)
924 ;; I (rtoy) am not 100% about the first identity above for all
925 ;; complex values of x and m, but tests seem to indicate that it
926 ;; produces the correct value as verified by verifying
927 ;; jacobi_sn(inverse_jacobi_sn(x,m),m) = x.
928 (cond ((float-numerical-eval-p u m
)
929 (let ((uu (bigfloat:to
($float u
)))
930 (mm (bigfloat:to
($float m
))))
933 (bigfloat::bf-rf
(bigfloat:to
(- 1 (* uu uu
)))
934 (bigfloat:to
(- 1 (* mm uu uu
)))
936 ((setf args
(complex-float-numerical-eval-p u m
))
937 (destructuring-bind (u m
)
939 (let ((uu (bigfloat:to
($float u
)))
940 (mm (bigfloat:to
($float m
))))
941 (complexify (* uu
(bigfloat::bf-rf
(- 1 (* uu uu
))
944 ((bigfloat-numerical-eval-p u m
)
945 (let ((uu (bigfloat:to u
))
946 (mm (bigfloat:to m
)))
948 (bigfloat::bf-rf
(bigfloat:-
1 (bigfloat:* uu uu
))
949 (bigfloat:-
1 (bigfloat:* mm uu uu
))
951 ((setf args
(complex-bigfloat-numerical-eval-p u m
))
952 (destructuring-bind (u m
)
954 (let ((uu (bigfloat:to u
))
955 (mm (bigfloat:to m
)))
957 (bigfloat::bf-rf
(bigfloat:-
1 (bigfloat:* uu uu
))
958 (bigfloat:-
1 (bigfloat:* mm uu uu
))
964 ;; asn(1,m) = elliptic_kc(m)
965 (ftake '%elliptic_kc m
))
966 ((and (numberp u
) (onep1 (- u
)))
967 ;; asn(-1,m) = -elliptic_kc(m)
968 (mul -
1 (ftake '%elliptic_kc m
)))
970 ;; asn(x,0) = F(asin(x),0) = asin(x)
973 ;; asn(x,1) = F(asin(x),1) = log(tan(pi/4+asin(x)/2))
974 (ftake '%elliptic_f
(ftake '%asin u
) 1))
975 ((and (eq $triginverses
'$all
)
977 (eq (caar u
) '%jacobi_sn
)
978 (alike1 (third u
) m
))
979 ;; inverse_jacobi_sn(sn(u)) = u
985 (def-simplifier inverse_jacobi_cn
(u m
)
987 (cond ((float-numerical-eval-p u m
)
988 ;; Numerically evaluate acn
990 ;; acn(x,m) = F(acos(x),m)
991 (to (elliptic-f (cl:acos
($float u
)) ($float m
))))
992 ((setf args
(complex-float-numerical-eval-p u m
))
993 (destructuring-bind (u m
)
995 (to (elliptic-f (cl:acos
(bigfloat:to
($float u
)))
996 (bigfloat:to
($float m
))))))
997 ((bigfloat-numerical-eval-p u m
)
998 (to (bigfloat::bf-elliptic-f
(bigfloat:acos
(bigfloat:to u
))
1000 ((setf args
(complex-bigfloat-numerical-eval-p u m
))
1001 (destructuring-bind (u m
)
1003 (to (bigfloat::bf-elliptic-f
(bigfloat:acos
(bigfloat:to u
))
1006 ;; asn(x,0) = F(acos(x),0) = acos(x)
1007 (ftake '%elliptic_f
(ftake '%acos u
) 0))
1009 ;; asn(x,1) = F(asin(x),1) = log(tan(pi/2+asin(x)/2))
1010 (ftake '%elliptic_f
(ftake '%acos u
) 1))
1012 (ftake '%elliptic_kc m
))
1015 ((and (eq $triginverses
'$all
)
1017 (eq (caar u
) '%jacobi_cn
)
1018 (alike1 (third u
) m
))
1019 ;; inverse_jacobi_cn(cn(u)) = u
1025 (def-simplifier inverse_jacobi_dn
(u m
)
1027 (cond ((float-numerical-eval-p u m
)
1028 (to (bigfloat::bf-inverse-jacobi-dn
(bigfloat:to
(float u
))
1029 (bigfloat:to
(float m
)))))
1030 ((setf args
(complex-float-numerical-eval-p u m
))
1031 (destructuring-bind (u m
)
1033 (let ((uu (bigfloat:to
($float u
)))
1034 (mm (bigfloat:to
($float m
))))
1035 (to (bigfloat::bf-inverse-jacobi-dn uu mm
)))))
1036 ((bigfloat-numerical-eval-p u m
)
1037 (let ((uu (bigfloat:to u
))
1038 (mm (bigfloat:to m
)))
1039 (to (bigfloat::bf-inverse-jacobi-dn uu mm
))))
1040 ((setf args
(complex-bigfloat-numerical-eval-p u m
))
1041 (destructuring-bind (u m
)
1043 (to (bigfloat::bf-inverse-jacobi-dn
(bigfloat:to u
) (bigfloat:to m
)))))
1045 ;; x = dn(u,1) = sech(u). so u = asech(x)
1048 ;; jacobi_dn(0,m) = 1
1050 ((zerop1 ($ratsimp
(sub u
(power (sub 1 m
) 1//2))))
1051 ;; jacobi_dn(K(m),m) = sqrt(1-m) so
1052 ;; inverse_jacobi_dn(sqrt(1-m),m) = K(m)
1053 (ftake '%elliptic_kc m
))
1054 ((and (eq $triginverses
'$all
)
1056 (eq (caar u
) '%jacobi_dn
)
1057 (alike1 (third u
) m
))
1058 ;; inverse_jacobi_dn(dn(u)) = u
1064 ;;;; Elliptic integrals
1066 (let ((errtol (expt (* 4 +flonum-epsilon
+) 1/6))
1070 (declare (type flonum errtol c1 c2 c3
))
1072 "Compute Carlson's incomplete or complete elliptic integral of the
1078 RF(x, y, z) = I ----------------------------------- dt
1079 ] SQRT(x + t) SQRT(y + t) SQRT(z + t)
1083 x, y, and z may be complex.
1085 (declare (number x y z
))
1086 (let ((x (coerce x
'(complex flonum
)))
1087 (y (coerce y
'(complex flonum
)))
1088 (z (coerce z
'(complex flonum
))))
1089 (declare (type (complex flonum
) x y z
))
1091 (let* ((mu (/ (+ x y z
) 3))
1092 (x-dev (- 2 (/ (+ mu x
) mu
)))
1093 (y-dev (- 2 (/ (+ mu y
) mu
)))
1094 (z-dev (- 2 (/ (+ mu z
) mu
))))
1095 (when (< (max (abs x-dev
) (abs y-dev
) (abs z-dev
)) errtol
)
1096 (let ((e2 (- (* x-dev y-dev
) (* z-dev z-dev
)))
1097 (e3 (* x-dev y-dev z-dev
)))
1104 (let* ((x-root (sqrt x
))
1107 (lam (+ (* x-root
(+ y-root z-root
)) (* y-root z-root
))))
1108 (setf x
(* (+ x lam
) 1/4))
1109 (setf y
(* (+ y lam
) 1/4))
1110 (setf z
(* (+ z lam
) 1/4))))))))
1112 ;; Elliptic integral of the first kind (Legendre's form):
1118 ;; I ------------------- ds
1120 ;; / SQRT(1 - m SIN (s))
1123 (defun elliptic-f (phi-arg m-arg
)
1124 (flet ((base (phi-arg m-arg
)
1125 (cond ((and (realp m-arg
) (realp phi-arg
))
1126 (let ((phi (float phi-arg
))
1131 ;; F(phi|m) = 1/sqrt(m)*F(theta|1/m)
1133 ;; with sin(theta) = sqrt(m)*sin(phi)
1134 (/ (elliptic-f (cl:asin
(* (sqrt m
) (sin phi
))) (/ m
))
1142 (- (/ (elliptic-f (float (/ pi
2)) m
/m
+1)
1144 (/ (elliptic-f (- (float (/ pi
2)) phi
) m
/m
+1)
1152 1 ;; F(phi,1) = log(sec(phi)+tan(phi))
1153 ;; = log(tan(pi/4+pi/2))
1154 (log (cl:tan
(+ (/ phi
2) (float (/ pi
4))))))
1156 (- (elliptic-f (- phi
) m
)))
1159 (multiple-value-bind (s phi-rem
)
1160 (truncate phi
(float pi
))
1161 (+ (* 2 s
(elliptic-k m
))
1162 (elliptic-f phi-rem m
))))
1164 (let ((sin-phi (sin phi
))
1168 (bigfloat::bf-rf
(* cos-phi cos-phi
)
1169 (* (- 1 (* k sin-phi
))
1170 (+ 1 (* k sin-phi
)))
1173 (+ (* 2 (elliptic-k m
))
1174 (elliptic-f (- phi
(float pi
)) m
)))
1176 (error "Shouldn't happen! Unhandled case in elliptic-f: ~S ~S~%"
1179 (let ((phi (coerce phi-arg
'(complex flonum
)))
1180 (m (coerce m-arg
'(complex flonum
))))
1181 (let ((sin-phi (sin phi
))
1185 (crf (* cos-phi cos-phi
)
1186 (* (- 1 (* k sin-phi
))
1187 (+ 1 (* k sin-phi
)))
1189 ;; Elliptic F is quasi-periodic wrt to z:
1191 ;; F(z|m) = F(z - pi*round(Re(z)/pi)|m) + 2*round(Re(z)/pi)*K(m)
1192 (let ((period (round (realpart phi-arg
) pi
)))
1193 (+ (base (- phi-arg
(* pi period
)) m-arg
)
1197 (bigfloat:to
(elliptic-k m-arg
))))))))
1199 ;; Complete elliptic integral of the first kind
1200 (defun elliptic-k (m)
1208 (- (/ (elliptic-k m
/m
+1)
1210 (/ (elliptic-f 0.0 m
/m
+1)
1217 (intl:gettext
"elliptic_kc: elliptic_kc(1) is undefined.")))
1219 (bigfloat::bf-rf
0.0 (- 1 m
)
1222 (bigfloat::bf-rf
0.0 (- 1 m
)
1225 ;; Elliptic integral of the second kind (Legendre's form):
1231 ;; I SQRT(1 - m SIN (s)) ds
1236 (defun elliptic-e (phi m
)
1237 (declare (type flonum phi m
))
1238 (flet ((base (phi m
)
1246 (let* ((sin-phi (sin phi
))
1249 (y (* (- 1 (* k sin-phi
))
1250 (+ 1 (* k sin-phi
)))))
1252 (bigfloat::bf-rf
(* cos-phi cos-phi
) y
1.0))
1255 (bigfloat::bf-rd
(* cos-phi cos-phi
) y
1.0)))))))))
1256 ;; Elliptic E is quasi-periodic wrt to phi:
1258 ;; E(z|m) = E(z - %pi*round(Re(z)/%pi)|m) + 2*round(Re(z)/%pi)*E(m)
1259 (let ((period (round (realpart phi
) pi
)))
1260 (+ (base (- phi
(* pi period
)) m
)
1261 (* 2 period
(elliptic-ec m
))))))
1264 (defun elliptic-ec (m)
1265 (declare (type flonum m
))
1274 (to (- (bigfloat::bf-rf
0.0 y
1.0)
1276 (bigfloat::bf-rd
0.0 y
1.0))))))))
1279 ;; Define the elliptic integrals for maxima
1281 ;; We use the definitions given in A&S 17.2.6 and 17.2.8. In particular:
1286 ;; F(phi|m) = I ------------------- ds
1288 ;; / SQRT(1 - m SIN (s))
1296 ;; E(phi|m) = I SQRT(1 - m SIN (s)) ds
1301 ;; That is, we do not use the modular angle, alpha, as the second arg;
1302 ;; the parameter m = sin(alpha)^2 is used.
1306 ;; The derivative of F(phi|m) wrt to phi is easy. The derivative wrt
1307 ;; to m is harder. Here is a derivation. Hope I got it right.
1309 ;; diff(integrate(1/sqrt(1-m*sin(x)^2),x,0,phi), m);
1314 ;; I ------------------ dx
1316 ;; / (1 - m SIN (x))
1318 ;; --------------------------
1322 ;; Now use the following relationship that is easily verified:
1325 ;; (1 - m) SIN (x) COS (x) COS(x) SIN(x)
1326 ;; ------------------- = ------------------- - DIFF(-------------------, x)
1328 ;; SQRT(1 - m SIN (x)) SQRT(1 - m SIN (x)) SQRT(1 - m SIN (x))
1331 ;; Now integrate this to get:
1337 ;; (1 - m) I ------------------- dx =
1339 ;; / SQRT(1 - m SIN (x))
1346 ;; + I ------------------- dx
1348 ;; / SQRT(1 - m SIN (x))
1350 ;; COS(PHI) SIN(PHI)
1351 ;; - ---------------------
1353 ;; SQRT(1 - m SIN (PHI))
1355 ;; Use the fact that cos(x)^2 = 1 - sin(x)^2 to show that this
1356 ;; integral on the RHS is:
1359 ;; (1 - m) elliptic_F(PHI, m) + elliptic_E(PHI, m)
1360 ;; -------------------------------------------
1362 ;; So, finally, we have
1367 ;; 2 -- (elliptic_F(PHI, m)) =
1370 ;; elliptic_E(PHI, m) - (1 - m) elliptic_F(PHI, m) COS(PHI) SIN(PHI)
1371 ;; ---------------------------------------------- - ---------------------
1373 ;; SQRT(1 - m SIN (PHI))
1374 ;; ----------------------------------------------------------------------
1377 (defprop %elliptic_f
1380 ;; 1/sqrt(1-m*sin(phi)^2)
1382 ((mplus simp
) 1 ((mtimes simp
) -
1 m
((mexpt simp
) ((%sin simp
) phi
) 2)))
1385 ((mtimes simp
) ((rat simp
) 1 2)
1386 ((mexpt simp
) ((mplus simp
) 1 ((mtimes simp
) -
1 m
)) -
1)
1388 ((mtimes simp
) ((mexpt simp
) m -
1)
1389 ((mplus simp
) ((%elliptic_e simp
) phi m
)
1390 ((mtimes simp
) -
1 ((mplus simp
) 1 ((mtimes simp
) -
1 m
))
1391 ((%elliptic_f simp
) phi m
))))
1392 ((mtimes simp
) -
1 ((%cos simp
) phi
) ((%sin simp
) phi
)
1395 ((mtimes simp
) -
1 m
((mexpt simp
) ((%sin simp
) phi
) 2)))
1396 ((rat simp
) -
1 2))))))
1400 ;; The derivative of E(phi|m) wrt to m is much simpler to derive than F(phi|m).
1402 ;; Take the derivative of the definition to get
1407 ;; I ------------------- dx
1409 ;; / SQRT(1 - m SIN (x))
1411 ;; - ---------------------------
1414 ;; It is easy to see that
1419 ;; elliptic_F(PHI, m) - m I ------------------- dx = elliptic_E(PHI, m)
1421 ;; / SQRT(1 - m SIN (x))
1424 ;; So we finally have
1426 ;; d elliptic_E(PHI, m) - elliptic_F(PHI, m)
1427 ;; -- (elliptic_E(PHI, m)) = ---------------------------------------
1430 (defprop %elliptic_e
1432 ;; sqrt(1-m*sin(phi)^2)
1434 ((mplus simp
) 1 ((mtimes simp
) -
1 m
((mexpt simp
) ((%sin simp
) phi
) 2)))
1437 ((mtimes simp
) ((rat simp
) 1 2) ((mexpt simp
) m -
1)
1438 ((mplus simp
) ((%elliptic_e simp
) phi m
)
1439 ((mtimes simp
) -
1 ((%elliptic_f simp
) phi m
)))))
1442 (def-simplifier elliptic_f
(phi m
)
1444 (cond ((float-numerical-eval-p phi m
)
1445 ;; Numerically evaluate it
1446 (to (elliptic-f ($float phi
) ($float m
))))
1447 ((setf args
(complex-float-numerical-eval-p phi m
))
1448 (destructuring-bind (phi m
)
1450 (to (elliptic-f (bigfloat:to
($float phi
))
1451 (bigfloat:to
($float m
))))))
1452 ((bigfloat-numerical-eval-p phi m
)
1453 (to (bigfloat::bf-elliptic-f
(bigfloat:to
($bfloat phi
))
1454 (bigfloat:to
($bfloat m
)))))
1455 ((setf args
(complex-bigfloat-numerical-eval-p phi m
))
1456 (destructuring-bind (phi m
)
1458 (to (bigfloat::bf-elliptic-f
(bigfloat:to
($bfloat phi
))
1459 (bigfloat:to
($bfloat m
))))))
1466 ;; A&S 17.4.21. Let's pick the log tan form. But this
1467 ;; isn't right if we know that abs(phi) > %pi/2, where
1468 ;; elliptic_f is undefined (or infinity).
1469 (cond ((not (eq '$pos
(csign (sub ($abs phi
) (div '$%pi
2)))))
1472 (add (mul '$%pi
(div 1 4))
1475 (merror (intl:gettext
"elliptic_f: elliptic_f(~:M, ~:M) is undefined.")
1477 ((alike1 phi
(div '$%pi
2))
1478 ;; Complete elliptic integral
1479 (ftake '%elliptic_kc m
))
1484 (def-simplifier elliptic_e
(phi m
)
1486 (cond ((float-numerical-eval-p phi m
)
1487 ;; Numerically evaluate it
1488 (elliptic-e ($float phi
) ($float m
)))
1489 ((complex-float-numerical-eval-p phi m
)
1490 (complexify (bigfloat::bf-elliptic-e
(complex ($float
($realpart phi
)) ($float
($imagpart phi
)))
1491 (complex ($float
($realpart m
)) ($float
($imagpart m
))))))
1492 ((bigfloat-numerical-eval-p phi m
)
1493 (to (bigfloat::bf-elliptic-e
(bigfloat:to
($bfloat phi
))
1494 (bigfloat:to
($bfloat m
)))))
1495 ((setf args
(complex-bigfloat-numerical-eval-p phi m
))
1496 (destructuring-bind (phi m
)
1498 (to (bigfloat::bf-elliptic-e
(bigfloat:to
($bfloat phi
))
1499 (bigfloat:to
($bfloat m
))))))
1506 ;; A&S 17.4.25, but handle periodicity:
1507 ;; elliptic_e(x,m) = elliptic_e(x-%pi*round(x/%pi), m)
1508 ;; + 2*round(x/%pi)*elliptic_ec(m)
1512 ;; elliptic_e(x,1) = sin(x-%pi*round(x/%pi)) + 2*round(x/%pi)*elliptic_ec(m)
1514 (let ((mult-pi (ftake '%round
(div phi
'$%pi
))))
1515 (add (ftake '%sin
(sub phi
1520 (ftake '%elliptic_ec m
))))))
1521 ((alike1 phi
(div '$%pi
2))
1522 ;; Complete elliptic integral
1523 (ftake '%elliptic_ec m
))
1524 ((and ($numberp phi
)
1525 (let ((r ($round
(div phi
'$%pi
))))
1528 ;; Handle the case where phi is a number where we can apply
1529 ;; the periodicity property without blowing up the
1531 (add (ftake '%elliptic_e
1534 (ftake '%round
(div phi
'$%pi
))))
1537 (mul (ftake '%round
(div phi
'$%pi
))
1538 (ftake '%elliptic_ec m
)))))
1543 ;; Complete elliptic integrals
1545 ;; elliptic_kc(m) = elliptic_f(%pi/2, m)
1547 ;; elliptic_ec(m) = elliptic_e(%pi/2, m)
1550 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1552 ;;; We support a simplim%function. The function is looked up in simplimit and
1553 ;;; handles specific values of the function.
1555 (defprop %elliptic_kc simplim%elliptic_kc simplim%function
)
1557 (defun simplim%elliptic_kc
(expr var val
)
1558 ;; Look for the limit of the argument
1559 (let ((m (limit (cadr expr
) var val
'think
)))
1561 ;; For an argument 1 return $infinity.
1564 ;; All other cases are handled by the simplifier of the function.
1565 (simplify (list '(%elliptic_kc
) m
))))))
1567 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1569 (def-simplifier elliptic_kc
(m)
1572 ;; elliptic_kc(1) is complex infinity. Maxima can not handle
1573 ;; infinities correctly, throw a Maxima error.
1575 (intl:gettext
"elliptic_kc: elliptic_kc(~:M) is undefined.")
1577 ((float-numerical-eval-p m
)
1578 ;; Numerically evaluate it
1579 (to (elliptic-k ($float m
))))
1580 ((complex-float-numerical-eval-p m
)
1581 (complexify (bigfloat::bf-elliptic-k
(complex ($float
($realpart m
)) ($float
($imagpart m
))))))
1582 ((setf args
(complex-bigfloat-numerical-eval-p m
))
1583 (destructuring-bind (m)
1585 (to (bigfloat::bf-elliptic-k
(bigfloat:to
($bfloat m
))))))
1589 ;; http://functions.wolfram.com/EllipticIntegrals/EllipticK/03/01/
1591 ;; elliptic_kc(1/2) = 8*%pi^(3/2)/gamma(-1/4)^2
1592 (div (mul 8 (power '$%pi
(div 3 2)))
1593 (power (gm (div -
1 4)) 2)))
1595 ;; elliptic_kc(-1) = gamma(1/4)^2/(4*sqrt(2*%pi))
1596 (div (power (gm (div 1 4)) 2)
1597 (mul 4 (power (mul 2 '$%pi
) 1//2))))
1598 ((alike1 m
(add 17 (mul -
12 (power 2 1//2))))
1599 ;; elliptic_kc(17-12*sqrt(2)) = 2*(2+sqrt(2))*%pi^(3/2)/gamma(-1/4)^2
1600 (div (mul 2 (mul (add 2 (power 2 1//2))
1601 (power '$%pi
(div 3 2))))
1602 (power (gm (div -
1 4)) 2)))
1603 ((or (alike1 m
(div (add 2 (power 3 1//2))
1605 (alike1 m
(add (div (power 3 1//2)
1608 ;; elliptic_kc((sqrt(3)+2)/4) = sqrt(%pi)*gamma(1/3)/gamma(5/6).
1610 ;; First evaluate this integral, where y = sqrt(1+t^3).
1612 ;; integrate(1/y,t,-1,inf) = integrate(1/y,t,-1,0) + integrate(1/y,t,0,inf).
1614 ;; The second integral, maxima gives beta(1/6,1/3)/3.
1616 ;; For the first, we can use the change of variable x=-u^(1/3) to get
1618 ;; integrate(1/sqrt(1-u)/u^(2/3),u,0,1)
1620 ;; which is a beta integral that maxima can evaluate to
1621 ;; beta(1/3,1/2)/3. Then we see the value of the initial
1624 ;; beta(1/6,1/3)/3 + beta(1/3,1/2)/3
1626 ;; (Thanks to Guilherme Namen for this derivation on the mailing list, 2023-03-09.)
1628 ;; We can simplify this expression by converting to gamma functions:
1630 ;; beta(1/6,1/3)/3 + beta(1/3,1/2)/3 =
1631 ;; (gamma(1/3)*(gamma(1/6)*gamma(5/6)+%pi))/(3*sqrt(%pi)*gamma(5/6));
1633 ;; Using the reflection formula gamma(1-z)*gamma(z) =
1634 ;; %pi/sin(%pi*z), we can write gamma(1/6)*gamma(5/6) =
1635 ;; %pi/sin(%pi*1/6) = 2*%pi. Finally, we have
1637 ;; sqrt(%pi)*gamma(1/3)/gamma(5/6);
1639 ;; All that remains is to show that integrate(1/y,t) can be
1640 ;; written as an inverse_jacobi_cn function with modulus
1643 ;; First apply the substitution
1645 ;; s = (t+sqrt(3)+1)/(t-sqrt(3)+1). We then have the integral
1647 ;; C*integrate(1/sqrt(s^2-1)/sqrt(s^2+4*sqrt(3)+7),s)
1649 ;; where C is some constant. From A&S 14.4.49, we can see
1650 ;; this integral is the inverse_jacobi_nc function with
1651 ;; modulus of (4*sqrt(3)+7)/(4*sqrt(3)+7+1) =
1653 (div (mul (power '$%pi
1//2)
1654 (ftake '%gamma
(div 1 3)))
1655 (ftake '%gamma
(div 5 6))))
1656 ($hypergeometric_representation
1657 ;; See http://functions.wolfram.com/08.02.26.0001.01
1659 ;; elliptic_kc(z) = %pi/2*%f[2,1]([1/2,1/2],[1], z)
1662 (ftake '%hypergeometric
1663 (make-mlist 1//2 1//2)
1670 (defprop %elliptic_kc
1675 ((mplus) ((%elliptic_ec
) m
)
1678 ((mplus) 1 ((mtimes) -
1 m
))))
1679 ((mexpt) ((mplus) 1 ((mtimes) -
1 m
)) -
1)
1683 (def-simplifier elliptic_ec
(m)
1685 (cond ((float-numerical-eval-p m
)
1686 ;; Numerically evaluate it
1687 (elliptic-ec ($float m
)))
1688 ((setf args
(complex-float-numerical-eval-p m
))
1689 (destructuring-bind (m)
1691 (complexify (bigfloat::bf-elliptic-ec
(bigfloat:to
($float m
))))))
1692 ((setf args
(complex-bigfloat-numerical-eval-p m
))
1693 (destructuring-bind (m)
1695 (to (bigfloat::bf-elliptic-ec
(bigfloat:to
($bfloat m
))))))
1696 ;; Some special cases we know about.
1702 ;; elliptic_ec(1/2). Use the identity
1704 ;; elliptic_ec(z)*elliptic_kc(1-z) - elliptic_kc(z)*elliptic_kc(1-z)
1705 ;; + elliptic_ec(1-z)*elliptic_kc(z) = %pi/2;
1707 ;; Let z = 1/2 to get
1709 ;; %pi^(3/2)*'elliptic_ec(1/2)/gamma(3/4)^2-%pi^3/(4*gamma(3/4)^4) = %pi/2
1711 ;; since we know that elliptic_kc(1/2) = %pi^(3/2)/(2*gamma(3/4)^2). Hence
1714 ;; = (2*%pi*gamma(3/4)^4+%pi^3)/(4*%pi^(3/2)*gamma(3/4)^2)
1715 ;; = gamma(3/4)^2/(2*sqrt(%pi))+%pi^(3/2)/(4*gamma(3/4)^2)
1717 (add (div (power (ftake '%gamma
(div 3 4)) 2)
1718 (mul 2 (power '$%pi
1//2)))
1719 (div (power '$%pi
(div 3 2))
1720 (mul 4 (power (ftake '%gamma
(div 3 4)) 2)))))
1722 ;; elliptic_ec(-1). Use the identity
1723 ;; http://functions.wolfram.com/08.01.17.0002.01
1726 ;; elliptic_ec(z) = sqrt(1 - z)*elliptic_ec(z/(z-1))
1728 ;; Let z = -1 to get
1730 ;; elliptic_ec(-1) = sqrt(2)*elliptic_ec(1/2)
1732 ;; Should we expand out elliptic_ec(1/2) using the above result?
1734 (ftake '%elliptic_ec
1//2)))
1735 ($hypergeometric_representation
1736 ;; See http://functions.wolfram.com/08.01.26.0001.01
1738 ;; elliptic_ec(z) = %pi/2*%f[2,1]([-1/2,1/2],[1], z)
1741 (ftake '%hypergeometric
1742 (make-mlist -
1//2 1//2)
1749 (defprop %elliptic_ec
1751 ((mtimes) ((rat) 1 2)
1752 ((mplus) ((%elliptic_ec
) m
)
1753 ((mtimes) -
1 ((%elliptic_kc
)
1759 ;; Elliptic integral of the third kind:
1766 ;; PI(n;phi|m) = I ----------------------------------- ds
1768 ;; / SQRT(1 - m SIN (s)) (1 - n SIN (s))
1771 ;; As with E and F, we do not use the modular angle alpha but the
1772 ;; parameter m = sin(alpha)^2.
1774 (def-simplifier elliptic_pi
(n phi m
)
1777 ((float-numerical-eval-p n phi m
)
1778 ;; Numerically evaluate it
1779 (elliptic-pi ($float n
) ($float phi
) ($float m
)))
1780 ((setf args
(complex-float-numerical-eval-p n phi m
))
1781 (destructuring-bind (n phi m
)
1783 (elliptic-pi (bigfloat:to
($float n
))
1784 (bigfloat:to
($float phi
))
1785 (bigfloat:to
($float m
)))))
1786 ((bigfloat-numerical-eval-p n phi m
)
1787 (to (bigfloat::bf-elliptic-pi
(bigfloat:to n
)
1790 ((setq args
(complex-bigfloat-numerical-eval-p n phi m
))
1791 (destructuring-bind (n phi m
)
1793 (to (bigfloat::bf-elliptic-pi
(bigfloat:to n
)
1797 (ftake '%elliptic_f phi m
))
1799 ;; 3 cases depending on n < 1, n > 1, or n = 1.
1800 (let ((s (asksign (add -
1 n
))))
1803 (div (ftake '%atanh
(mul (power (add n -
1) 1//2)
1805 (power (add n -
1) 1//2)))
1807 (div (ftake '%atan
(mul (power (sub 1 n
) 1//2)
1809 (power (sub 1 n
) 1//2)))
1811 (ftake '%tan phi
)))))
1816 ;; Complete elliptic-pi. That is phi = %pi/2. Then
1818 ;; = Rf(0, 1-m,1) + Rj(0,1-m,1-n)*n/3;
1819 (defun elliptic-pi-complete (n m
)
1820 (to (bigfloat:+ (bigfloat::bf-rf
0 (- 1 m
) 1)
1821 (bigfloat:* 1/3 n
(bigfloat::bf-rj
0 (- 1 m
) 1 (- 1 n
))))))
1823 ;; To compute elliptic_pi for all z, we use the property
1824 ;; (http://functions.wolfram.com/08.06.16.0002.01)
1826 ;; elliptic_pi(n, z + %pi*k, m)
1827 ;; = 2*k*elliptic_pi(n, %pi/2, m) + elliptic_pi(n, z, m)
1829 ;; So we are left with computing the integral for 0 <= z < %pi. Using
1830 ;; Carlson's formulation produces the wrong values for %pi/2 < z <
1831 ;; %pi. How to do that?
1835 ;; I(a,b) = integrate(1/(1-n*sin(x)^2)/sqrt(1 - m*sin(x)^2), x, a, b)
1837 ;; That is, I(a,b) is the integral for the elliptic_pi function but
1838 ;; with a lower limit of a and an upper limit of b.
1840 ;; Then, we want to compute I(0, z), with %pi <= z < %pi. Let w = z +
1841 ;; %pi/2, 0 <= w < %pi/2. Then
1843 ;; I(0, w+%pi/2) = I(0, %pi/2) + I(%pi/2, w+%pi/2)
1845 ;; To evaluate I(%pi/2, w+%pi/2), use a change of variables:
1847 ;; changevar('integrate(1/(1-n*sin(x)^2)/sqrt(1 - m*sin(x)^2), x, %pi/2, w + %pi/2),
1850 ;; = integrate(-1/(sqrt(1-m*sin(u)^2)*(1-n*sin(u)^2)),u,%pi/2-w,%pi/2)
1851 ;; = I(%pi/2-w,%pi/2)
1852 ;; = I(0,%pi/2) - I(0,%pi/2-w)
1856 ;; I(0,%pi/2+w) = 2*I(0,%pi/2) - I(0,%pi/2-w)
1858 ;; This allows us to compute the general result with 0 <= z < %pi
1860 ;; I(0, k*%pi + z) = 2*k*I(0,%pi/2) + I(0,z);
1862 ;; If 0 <= z < %pi/2, then the we are done. If %pi/2 <= z < %pi, let
1863 ;; z = w+%pi/2. Then
1865 ;; I(0,z) = 2*I(0,%pi/2) - I(0,%pi/2-w)
1867 ;; Or, since w = z-%pi/2:
1869 ;; I(0,z) = 2*I(0,%pi/2) - I(0,%pi-z)
1871 (defun elliptic-pi (n phi m
)
1872 ;; elliptic_pi(n, -phi, m) = -elliptic_pi(n, phi, m). That is, it
1873 ;; is an odd function of phi.
1874 (when (minusp (realpart phi
))
1875 (return-from elliptic-pi
(- (elliptic-pi n
(- phi
) m
))))
1877 ;; Note: Carlson's DRJ has n defined as the negative of the n given
1879 (flet ((base (n phi m
)
1880 ;; elliptic_pi(n,phi,m) =
1881 ;; sin(phi)*Rf(cos(phi)^2, 1-m*sin(phi)^2, 1)
1882 ;; - (-n / 3) * sin(phi)^3
1883 ;; * Rj(cos(phi)^2, 1-m*sin(phi)^2, 1, 1-n*sin(phi)^2)
1888 (k2sin (* (- 1 (* k sin-phi
))
1889 (+ 1 (* k sin-phi
)))))
1890 (- (* sin-phi
(bigfloat::bf-rf
(expt cos-phi
2) k2sin
1.0))
1891 (* (/ nn
3) (expt sin-phi
3)
1892 (bigfloat::bf-rj
(expt cos-phi
2) k2sin
1.0
1893 (- 1 (* n
(expt sin-phi
2)))))))))
1894 ;; FIXME: Reducing the arg by pi has significant round-off.
1895 ;; Consider doing something better.
1896 (let* ((cycles (round (realpart phi
) pi
))
1897 (rem (- phi
(* cycles pi
))))
1898 (let ((complete (elliptic-pi-complete n m
)))
1899 (to (+ (* 2 cycles complete
)
1900 (base n rem m
)))))))
1902 ;;; Deriviatives from functions.wolfram.com
1903 ;;; http://functions.wolfram.com/EllipticIntegrals/EllipticPi3/20/
1904 (defprop %elliptic_pi
1906 ;Derivative wrt first argument
1907 ((mtimes) ((rat) 1 2)
1908 ((mexpt) ((mplus) m
((mtimes) -
1 n
)) -
1)
1909 ((mexpt) ((mplus) -
1 n
) -
1)
1911 ((mtimes) ((mexpt) n -
1)
1912 ((mplus) ((mtimes) -
1 m
) ((mexpt) n
2))
1913 ((%elliptic_pi
) n z m
))
1915 ((mtimes) ((mplus) m
((mtimes) -
1 n
)) ((mexpt) n -
1)
1916 ((%elliptic_f
) z m
))
1917 ((mtimes) ((rat) -
1 2) n
1919 ((mplus) 1 ((mtimes) -
1 m
((mexpt) ((%sin
) z
) 2)))
1922 ((mplus) 1 ((mtimes) -
1 n
((mexpt) ((%sin
) z
) 2)))
1924 ((%sin
) ((mtimes) 2 z
)))))
1925 ;derivative wrt second argument
1928 ((mplus) 1 ((mtimes) -
1 m
((mexpt) ((%sin
) z
) 2)))
1931 ((mplus) 1 ((mtimes) -
1 n
((mexpt) ((%sin
) z
) 2))) -
1))
1932 ;Derivative wrt third argument
1933 ((mtimes) ((rat) 1 2)
1934 ((mexpt) ((mplus) ((mtimes) -
1 m
) n
) -
1)
1935 ((mplus) ((%elliptic_pi
) n z m
)
1936 ((mtimes) ((mexpt) ((mplus) -
1 m
) -
1)
1937 ((%elliptic_e
) z m
))
1938 ((mtimes) ((rat) -
1 2) ((mexpt) ((mplus) -
1 m
) -
1) m
1940 ((mplus) 1 ((mtimes) -
1 m
((mexpt) ((%sin
) z
) 2)))
1942 ((%sin
) ((mtimes) 2 z
))))))
1945 (in-package #:bigfloat
)
1946 ;; Translation of Jim FitzSimons' bigfloat implementation of elliptic
1947 ;; integrals from http://www.getnet.com/~cherry/elliptbf3.mac.
1949 ;; The algorithms are based on B.C. Carlson's "Numerical Computation
1950 ;; of Real or Complex Elliptic Integrals". These are updated to the
1951 ;; algorithms in Journal of Computational and Applied Mathematics 118
1952 ;; (2000) 71-85 "Reduction Theorems for Elliptic Integrands with the
1953 ;; Square Root of two quadritic factors"
1956 ;; NOTE: Despite the names indicating these are for bigfloat numbers,
1957 ;; the algorithms and routines are generic and will work with floats
1960 (defun bferrtol (&rest args
)
1961 ;; Compute error tolerance as sqrt(2^(-fpprec)). Not sure this is
1962 ;; quite right, but it makes the routines more accurate as fpprec
1964 (sqrt (reduce #'min
(mapcar #'(lambda (x)
1965 (if (rationalp (realpart x
))
1966 maxima
::+flonum-epsilon
+
1970 ;; rc(x,y) = integrate(1/2*(t+x)^(-1/2)/(t+y), t, 0, inf)
1972 ;; log(x) = (x-1)*rc(((1+x)/2)^2, x), x > 0
1973 ;; asin(x) = x * rc(1-x^2, 1), |x|<= 1
1974 ;; acos(x) = sqrt(1-x^2)*rc(x^2,1), 0 <= x <=1
1975 ;; atan(x) = x * rc(1,1+x^2)
1976 ;; asinh(x) = x * rc(1+x^2,1)
1977 ;; acosh(x) = sqrt(x^2-1) * rc(x^2,1), x >= 1
1978 ;; atanh(x) = x * rc(1,1-x^2), |x|<=1
1982 xn z w a an pwr4 n epslon lambda sn s
)
1983 (cond ((and (zerop (imagpart yn
))
1984 (minusp (realpart yn
)))
1988 (setf w
(sqrt (/ x xn
))))
1993 (setf a
(/ (+ xn yn yn
) 3))
1994 (setf epslon
(/ (abs (- a xn
)) (bferrtol x y
)))
1998 (loop while
(> (* epslon pwr4
) (abs an
))
2000 (setf pwr4
(/ pwr4
4))
2001 (setf lambda
(+ (* 2 (sqrt xn
) (sqrt yn
)) yn
))
2002 (setf an
(/ (+ an lambda
) 4))
2003 (setf xn
(/ (+ xn lambda
) 4))
2004 (setf yn
(/ (+ yn lambda
) 4))
2006 ;; c2=3/10,c3=1/7,c4=3/8,c5=9/22,c6=159/208,c7=9/8
2007 (setf sn
(/ (* pwr4
(- z a
)) an
))
2008 (setf s
(* sn sn
(+ 3/10
2013 (* sn
9/8))))))))))))
2019 ;; See https://dlmf.nist.gov/19.16.E5:
2021 ;; rd(x,y,z) = integrate(3/2/sqrt(t+x)/sqrt(t+y)/sqrt(t+z)/(t+z), t, 0, inf)
2024 ;; E(K) = rf(0, 1-K^2, 1) - (K^2/3)*rd(0,1-K^2,1)
2026 ;; B = integrate(s^2/sqrt(1-s^4), s, 0 ,1)
2027 ;; = beta(3/4,1/2)/4
2028 ;; = sqrt(%pi)*gamma(3/4)/gamma(1/4)
2031 (defun bf-rd (x y z
)
2035 (a (/ (+ xn yn
(* 3 zn
)) 5))
2036 (epslon (/ (max (abs (- a xn
))
2044 xnroot ynroot znroot lam
)
2045 (loop while
(> (* power4 epslon
) (abs an
))
2047 (setf xnroot
(sqrt xn
))
2048 (setf ynroot
(sqrt yn
))
2049 (setf znroot
(sqrt zn
))
2050 (setf lam
(+ (* xnroot ynroot
)
2053 (setf sigma
(+ sigma
(/ power4
2054 (* znroot
(+ zn lam
)))))
2055 (setf power4
(* power4
1/4))
2056 (setf xn
(* (+ xn lam
) 1/4))
2057 (setf yn
(* (+ yn lam
) 1/4))
2058 (setf zn
(* (+ zn lam
) 1/4))
2059 (setf an
(* (+ an lam
) 1/4))
2061 ;; c1=-3/14,c2=1/6,c3=9/88,c4=9/22,c5=-3/22,c6=-9/52,c7=3/26
2062 (let* ((xndev (/ (* (- a x
) power4
) an
))
2063 (yndev (/ (* (- a y
) power4
) an
))
2064 (zndev (- (* (+ xndev yndev
) 1/3)))
2065 (ee2 (- (* xndev yndev
) (* 6 zndev zndev
)))
2066 (ee3 (* (- (* 3 xndev yndev
)
2069 (ee4 (* 3 (- (* xndev yndev
) (* zndev zndev
)) zndev zndev
))
2070 (ee5 (* xndev yndev zndev zndev zndev
))
2078 (* -
1/16 ee2 ee2 ee2
)
2081 (* 45/272 ee2 ee2 ee3
)
2082 (* -
9/68 (+ (* ee2 ee5
) (* ee3 ee4
))))))
2087 ;; See https://dlmf.nist.gov/19.16.E1
2089 ;; rf(x,y,z) = 1/2*integrate(1/(sqrt(t+x)*sqrt(t+y)*sqrt(t+z)), t, 0, inf);
2092 (defun bf-rf (x y z
)
2096 (a (/ (+ xn yn zn
) 3))
2097 (epslon (/ (max (abs (- a xn
))
2104 xnroot ynroot znroot lam
)
2105 (loop while
(> (* power4 epslon
) (abs an
))
2107 (setf xnroot
(sqrt xn
))
2108 (setf ynroot
(sqrt yn
))
2109 (setf znroot
(sqrt zn
))
2110 (setf lam
(+ (* xnroot ynroot
)
2113 (setf power4
(* power4
1/4))
2114 (setf xn
(* (+ xn lam
) 1/4))
2115 (setf yn
(* (+ yn lam
) 1/4))
2116 (setf zn
(* (+ zn lam
) 1/4))
2117 (setf an
(* (+ an lam
) 1/4))
2119 ;; c1=-3/14,c2=1/6,c3=9/88,c4=9/22,c5=-3/22,c6=-9/52,c7=3/26
2120 (let* ((xndev (/ (* (- a x
) power4
) an
))
2121 (yndev (/ (* (- a y
) power4
) an
))
2122 (zndev (- (+ xndev yndev
)))
2123 (ee2 (- (* xndev yndev
) (* 6 zndev zndev
)))
2124 (ee3 (* xndev yndev zndev
))
2129 (* -
3/44 ee2 ee3
))))
2132 (defun bf-rj1 (x y z p
)
2143 (a (/ (+ xn yn zn pn pn
) 5))
2144 (epslon (/ (max (abs (- a xn
))
2148 (bferrtol x y z p
)))
2150 xnroot ynroot znroot pnroot lam dn
)
2151 (loop while
(> (* power4 epslon
) (abs an
))
2153 (setf xnroot
(sqrt xn
))
2154 (setf ynroot
(sqrt yn
))
2155 (setf znroot
(sqrt zn
))
2156 (setf pnroot
(sqrt pn
))
2157 (setf lam
(+ (* xnroot ynroot
)
2160 (setf dn
(* (+ pnroot xnroot
)
2163 (setf sigma
(+ sigma
2165 (bf-rc 1 (+ 1 (/ en
(* dn dn
)))))
2167 (setf power4
(* power4
1/4))
2169 (setf xn
(* (+ xn lam
) 1/4))
2170 (setf yn
(* (+ yn lam
) 1/4))
2171 (setf zn
(* (+ zn lam
) 1/4))
2172 (setf pn
(* (+ pn lam
) 1/4))
2173 (setf an
(* (+ an lam
) 1/4))
2175 (let* ((xndev (/ (* (- a x
) power4
) an
))
2176 (yndev (/ (* (- a y
) power4
) an
))
2177 (zndev (/ (* (- a z
) power4
) an
))
2178 (pndev (* -
0.5 (+ xndev yndev zndev
)))
2179 (ee2 (+ (* xndev yndev
)
2182 (* -
3 pndev pndev
)))
2183 (ee3 (+ (* xndev yndev zndev
)
2185 (* 4 pndev pndev pndev
)))
2186 (ee4 (* (+ (* 2 xndev yndev zndev
)
2188 (* 3 pndev pndev pndev
))
2190 (ee5 (* xndev yndev zndev pndev pndev
))
2198 (* -
1/16 ee2 ee2 ee2
)
2201 (* 45/272 ee2 ee2 ee3
)
2202 (* -
9/68 (+ (* ee2 ee5
) (* ee3 ee4
))))))
2205 (sqrt (* an an an
)))))))
2207 (defun bf-rj (x y z p
)
2212 (cond ((and (and (zerop (imagpart xn
)) (>= (realpart xn
) 0))
2213 (and (zerop (imagpart yn
)) (>= (realpart yn
) 0))
2214 (and (zerop (imagpart zn
)) (>= (realpart zn
) 0))
2215 (and (zerop (imagpart qn
)) (> (realpart qn
) 0)))
2216 (destructuring-bind (xn yn zn
)
2217 (sort (list xn yn zn
) #'<)
2218 (let* ((pn (+ yn
(* (- zn yn
) (/ (- yn xn
) (+ yn qn
)))))
2219 (s (- (* (- pn yn
) (bf-rj1 xn yn zn pn
))
2220 (* 3 (bf-rf xn yn zn
)))))
2221 (setf s
(+ s
(* 3 (sqrt (/ (* xn yn zn
)
2222 (+ (* xn zn
) (* pn qn
))))
2223 (bf-rc (+ (* xn zn
) (* pn qn
)) (* pn qn
)))))
2226 (bf-rj1 x y z p
)))))
2228 (defun bf-rg (x y z
)
2230 (+ (* z
(bf-rf x y z
))
2235 (sqrt (/ (* x y
) z
)))))
2237 ;; elliptic_f(phi,m) = sin(phi)*rf(cos(phi)^2, 1-m*sin(phi)^2,1)
2238 (defun bf-elliptic-f (phi m
)
2239 (flet ((base (phi m
)
2241 ;; F(z|1) = log(tan(z/2+%pi/4))
2242 (log (tan (+ (/ phi
2) (/ (%pi phi
) 4)))))
2246 (* s
(bf-rf (* c c
) (- 1 (* m s s
)) 1)))))))
2247 ;; Handle periodicity (see elliptic-f)
2248 (let* ((bfpi (%pi phi
))
2249 (period (round (realpart phi
) bfpi
)))
2250 (+ (base (- phi
(* bfpi period
)) m
)
2253 (* 2 period
(bf-elliptic-k m
)))))))
2255 ;; elliptic_kc(k) = rf(0, 1-k^2,1)
2258 ;; elliptic_kc(m) = rf(0, 1-m,1)
2260 (defun bf-elliptic-k (m)
2262 (if (maxima::$bfloatp m
)
2263 (maxima::$bfloat
(maxima::div
'maxima
::$%pi
2))
2264 (float (/ pi
2) 1e0
)))
2267 (intl:gettext
"elliptic_kc: elliptic_kc(1) is undefined.")))
2269 (bf-rf 0 (- 1 m
) 1))))
2271 ;; elliptic_e(phi, k) = sin(phi)*rf(cos(phi)^2,1-k^2*sin(phi)^2,1)
2272 ;; - (k^2/3)*sin(phi)^3*rd(cos(phi)^2, 1-k^2*sin(phi)^2,1)
2276 ;; elliptic_e(phi, m) = sin(phi)*rf(cos(phi)^2,1-m*sin(phi)^2,1)
2277 ;; - (m/3)*sin(phi)^3*rd(cos(phi)^2, 1-m*sin(phi)^2,1)
2279 (defun bf-elliptic-e (phi m
)
2280 (flet ((base (phi m
)
2281 (let* ((s (sin phi
))
2284 (s2 (- 1 (* m s s
))))
2285 (- (* s
(bf-rf c2 s2
1))
2286 (* (/ m
3) (* s s s
) (bf-rd c2 s2
1))))))
2287 ;; Elliptic E is quasi-periodic wrt to phi:
2289 ;; E(z|m) = E(z - %pi*round(Re(z)/%pi)|m) + 2*round(Re(z)/%pi)*E(m)
2290 (let* ((bfpi (%pi phi
))
2291 (period (round (realpart phi
) bfpi
)))
2292 (+ (base (- phi
(* bfpi period
)) m
)
2293 (* 2 period
(bf-elliptic-ec m
))))))
2296 ;; elliptic_ec(k) = rf(0,1-k^2,1) - (k^2/3)*rd(0,1-k^2,1);
2299 ;; elliptic_ec(m) = rf(0,1-m,1) - (m/3)*rd(0,1-m,1);
2301 (defun bf-elliptic-ec (m)
2303 (if (typep m
'bigfloat
)
2304 (bigfloat (maxima::$bfloat
(maxima::div
'maxima
::$%pi
2)))
2305 (float (/ pi
2) 1e0
)))
2307 (if (typep m
'bigfloat
)
2313 (* m
1/3 (bf-rd 0 m1
1)))))))
2315 (defun bf-elliptic-pi-complete (n m
)
2316 (+ (bf-rf 0 (- 1 m
) 1)
2317 (* 1/3 n
(bf-rj 0 (- 1 m
) 1 (- 1 n
)))))
2319 (defun bf-elliptic-pi (n phi m
)
2320 ;; Note: Carlson's DRJ has n defined as the negative of the n given
2322 (flet ((base (n phi m
)
2327 (k2sin (* (- 1 (* k sin-phi
))
2328 (+ 1 (* k sin-phi
)))))
2329 (- (* sin-phi
(bf-rf (expt cos-phi
2) k2sin
1.0))
2330 (* (/ nn
3) (expt sin-phi
3)
2331 (bf-rj (expt cos-phi
2) k2sin
1.0
2332 (- 1 (* n
(expt sin-phi
2)))))))))
2333 ;; FIXME: Reducing the arg by pi has significant round-off.
2334 ;; Consider doing something better.
2335 (let* ((bf-pi (%pi
(realpart phi
)))
2336 (cycles (round (realpart phi
) bf-pi
))
2337 (rem (- phi
(* cycles bf-pi
))))
2338 (let ((complete (bf-elliptic-pi-complete n m
)))
2339 (+ (* 2 cycles complete
)
2342 ;; Compute inverse_jacobi_sn, for float or bigfloat args.
2343 (defun bf-inverse-jacobi-sn (u m
)
2344 (* u
(bf-rf (- 1 (* u u
))
2348 ;; Compute inverse_jacobi_dn. We use the following identity
2349 ;; from Gradshteyn & Ryzhik, 8.153.6
2351 ;; w = dn(z|m) = cn(sqrt(m)*z, 1/m)
2353 ;; Solve for z to get
2355 ;; z = inverse_jacobi_dn(w,m)
2356 ;; = 1/sqrt(m) * inverse_jacobi_cn(w, 1/m)
2357 (defun bf-inverse-jacobi-dn (w m
)
2361 ;; jacobi_dn(x,1) = sech(x) so the inverse is asech(x)
2362 (maxima::take
'(maxima::%asech
) (maxima::to w
)))
2364 ;; We should do something better to make sure that things
2365 ;; that should be real are real.
2366 (/ (to (maxima::take
'(maxima::%inverse_jacobi_cn
)
2368 (maxima::to
(/ m
))))
2371 (in-package :maxima
)
2373 ;; Define Carlson's elliptic integrals.
2375 (def-simplifier carlson_rc
(x y
)
2378 (flet ((floatify (z)
2379 ;; If z is a complex rational, convert to a
2380 ;; complex double-float. Otherwise, leave it as
2381 ;; is. If we don't do this, %i is handled as
2382 ;; #c(0 1), which makes bf-rc use single-float
2383 ;; arithmetic instead of the desired
2385 (if (and (complexp z
) (rationalp (realpart z
)))
2386 (complex (float (realpart z
))
2387 (float (imagpart z
)))
2389 (to (bigfloat::bf-rc
(floatify (bigfloat:to x
))
2390 (floatify (bigfloat:to y
)))))))
2391 ;; See comments from bf-rc
2392 (cond ((float-numerical-eval-p x y
)
2393 (calc ($float x
) ($float y
)))
2394 ((bigfloat-numerical-eval-p x y
)
2395 (calc ($bfloat x
) ($bfloat y
)))
2396 ((setf args
(complex-float-numerical-eval-p x y
))
2397 (destructuring-bind (x y
)
2399 (calc ($float x
) ($float y
))))
2400 ((setf args
(complex-bigfloat-numerical-eval-p x y
))
2401 (destructuring-bind (x y
)
2403 (calc ($bfloat x
) ($bfloat y
))))
2409 (alike1 y
(div 1 4)))
2414 ;; rc(2,1) = 1/2*integrate(1/sqrt(t+2)/(t+1), t, 0, inf)
2415 ;; = (log(sqrt(2)+1)-log(sqrt(2)-1))/2
2416 ;; ratsimp(logcontract(%)),algebraic:
2417 ;; = -log(3-2^(3/2))/2
2418 ;; = -log(sqrt(3-2^(3/2)))
2419 ;; = -log(sqrt(2)-1)
2420 ;; = log(1/(sqrt(2)-1))
2421 ;; ratsimp(%),algebraic;
2423 (ftake '%log
(add 1 (power 2 1//2))))
2424 ((and (alike x
'$%i
)
2425 (alike y
(add 1 '$%i
)))
2426 ;; rc(%i, %i+1) = 1/2*integrate(1/sqrt(t+%i)/(t+%i+1), t, 0, inf)
2427 ;; = %pi/2-atan((-1)^(1/4))
2428 ;; ratsimp(logcontract(ratsimp(rectform(%o42)))),algebraic;
2429 ;; = (%i*log(3-2^(3/2))+%pi)/4
2430 ;; = (%i*log(3-2^(3/2)))/4+%pi/4
2431 ;; = %i*log(sqrt(3-2^(3/2)))/2+%pi/4
2433 ;; = %pi/4 + %i*log(sqrt(2)-1)/2
2437 (ftake '%log
(sub (power 2 1//2) 1)))))
2440 ;; rc(0,%i) = 1/2*integrate(1/(sqrt(t)*(t+%i)), t, 0, inf)
2441 ;; = -((sqrt(2)*%i-sqrt(2))*%pi)/4
2442 ;; = ((1-%i)*%pi)/2^(3/2)
2443 (div (mul (sub 1 '$%i
)
2447 (eq ($sign
($realpart x
)) '$pos
))
2448 ;; carlson_rc(x,x) = 1/2*integrate(1/sqrt(t+x)/(t+x), t, 0, inf)
2451 ((and (alike1 x
(power (div (add 1 y
) 2) 2))
2452 (eq ($sign
($realpart y
)) '$pos
))
2453 ;; Rc(((1+x)/2)^2,x) = log(x)/(x-1) for x > 0.
2455 ;; This is done by looking at Rc(x,y) and seeing if
2456 ;; ((1+y)/2)^2 is the same as x.
2457 (div (ftake '%log y
)
2462 (def-simplifier carlson_rd
(x y z
)
2464 (flet ((calc (x y z
)
2465 (to (bigfloat::bf-rd
(bigfloat:to x
)
2468 ;; See https://dlmf.nist.gov/19.20.E18
2469 (cond ((and (eql x
1)
2476 ;; Rd(x,x,x) = x^(-3/2)
2477 (power x
(div -
3 2)))
2480 ;; Rd(0,y,y) = 3/4*%pi*y^(-3/2)
2483 (power y
(div -
3 2))))
2485 ;; Rd(x,y,y) = 3/(2*(y-x))*(Rc(x, y) - sqrt(x)/y)
2486 (mul (div 3 (mul 2 (sub y x
)))
2487 (sub (ftake '%carlson_rc x y
)
2491 ;; Rd(x,x,z) = 3/(z-x)*(Rc(z,x) - 1/sqrt(z))
2492 (mul (div 3 (sub z x
))
2493 (sub (ftake '%carlson_rc z x
)
2494 (div 1 (power z
1//2)))))
2500 ;; Rd(0,2,1) = 3*(gamma(3/4)^2)/sqrt(2*%pi)
2501 ;; See https://dlmf.nist.gov/19.20.E22.
2503 ;; But that's the same as
2504 ;; 3*sqrt(%pi)*gamma(3/4)/gamma(1/4). We can see this by
2505 ;; taking the ratio to get
2506 ;; gamma(1/4)*gamma(3/4)/sqrt(2)*%pi. But
2507 ;; gamma(1/4)*gamma(3/4) = beta(1/4,3/4) = sqrt(2)*%pi.
2508 ;; Hence, the ratio is 1.
2510 ;; Note also that Rd(x,y,z) = Rd(y,x,z)
2513 (div (ftake '%gamma
(div 3 4))
2514 (ftake '%gamma
(div 1 4)))))
2515 ((and (or (eql x
0) (eql y
0))
2517 ;; 1/3*m*Rd(0,1-m,1) = K(m) - E(m).
2518 ;; See https://dlmf.nist.gov/19.25.E1
2520 ;; Thus, Rd(0,y,1) = 3/(1-y)*(K(1-y) - E(1-y))
2522 ;; Note that Rd(x,y,z) = Rd(y,x,z).
2523 (let ((m (sub 1 y
)))
2525 (sub (ftake '%elliptic_kc m
)
2526 (ftake '%elliptic_ec m
)))))
2531 ;; 1/3*m*(1-m)*Rd(0,1,1-m) = E(m) - (1-m)*K(m)
2532 ;; See https://dlmf.nist.gov/19.25.E1
2535 ;; Rd(0,1,z) = 3/(z*(1-z))*(E(1-z) - z*K(1-z))
2536 ;; Recall that Rd(x,y,z) = Rd(y,x,z).
2537 (mul (div 3 (mul z
(sub 1 z
)))
2538 (sub (ftake '%elliptic_ec
(sub 1 z
))
2540 (ftake '%elliptic_kc
(sub 1 z
))))))
2541 ((float-numerical-eval-p x y z
)
2542 (calc ($float x
) ($float y
) ($float z
)))
2543 ((bigfloat-numerical-eval-p x y z
)
2544 (calc ($bfloat x
) ($bfloat y
) ($bfloat z
)))
2545 ((setf args
(complex-float-numerical-eval-p x y z
))
2546 (destructuring-bind (x y z
)
2548 (calc ($float x
) ($float y
) ($float z
))))
2549 ((setf args
(complex-bigfloat-numerical-eval-p x y z
))
2550 (destructuring-bind (x y z
)
2552 (calc ($bfloat x
) ($bfloat y
) ($bfloat z
))))
2556 (def-simplifier carlson_rf
(x y z
)
2558 (flet ((calc (x y z
)
2559 (to (bigfloat::bf-rf
(bigfloat:to x
)
2562 ;; See https://dlmf.nist.gov/19.20.i
2563 (cond ((and (alike1 x y
)
2565 ;; Rf(x,x,x) = x^(-1/2)
2569 ;; Rf(0,y,y) = 1/2*%pi*y^(-1/2)
2573 (ftake '%carlson_rc x y
))
2574 ((some #'(lambda (args)
2575 (destructuring-bind (x y z
)
2586 ;; Rf(0,1,2) = (gamma(1/4))^2/(4*sqrt(2*%pi))
2588 ;; And Rf is symmetric in all the args, so check every
2589 ;; permutation too. This could probably be simplified
2590 ;; without consing all the lists, but I'm lazy.
2591 (div (power (ftake '%gamma
(div 1 4)) 2)
2592 (mul 4 (power (mul 2 '$%pi
) 1//2))))
2593 ((some #'(lambda (args)
2594 (destructuring-bind (x y z
)
2596 (and (alike1 x
'$%i
)
2597 (alike1 y
(mul -
1 '$%i
))
2606 ;; = 1/2*integrate(1/sqrt(t^2+1)/sqrt(t),t,0,inf)
2607 ;; = beta(1/4,1/4)/4;
2609 ;; = gamma(1/4)^2/(4*sqrt(%pi))
2611 ;; Rf is symmetric, so check all the permutations too.
2612 (div (power (ftake '%gamma
(div 1 4)) 2)
2613 (mul 4 (power '$%pi
1//2))))
2615 (some #'(lambda (args)
2616 (destructuring-bind (x y z
)
2618 ;; Check that x = 0 and z = 1, and
2629 ;; Rf(0,1-m,1) = elliptic_kc(m).
2630 ;; See https://dlmf.nist.gov/19.25.E1
2631 (ftake '%elliptic_kc
(sub 1 args
)))
2632 ((some #'(lambda (args)
2633 (destructuring-bind (x y z
)
2635 (and (alike1 x
'$%i
)
2636 (alike1 y
(mul -
1 '$%i
))
2645 ;; = 1/2*integrate(1/sqrt(t^2+1)/sqrt(t),t,0,inf)
2646 ;; = beta(1/4,1/4)/4;
2648 ;; = gamma(1/4)^2/(4*sqrt(%pi))
2650 ;; Rf is symmetric, so check all the permutations too.
2651 (div (power (ftake '%gamma
(div 1 4)) 2)
2652 (mul 4 (power '$%pi
1//2))))
2653 ((float-numerical-eval-p x y z
)
2654 (calc ($float x
) ($float y
) ($float z
)))
2655 ((bigfloat-numerical-eval-p x y z
)
2656 (calc ($bfloat x
) ($bfloat y
) ($bfloat z
)))
2657 ((setf args
(complex-float-numerical-eval-p x y z
))
2658 (destructuring-bind (x y z
)
2660 (calc ($float x
) ($float y
) ($float z
))))
2661 ((setf args
(complex-bigfloat-numerical-eval-p x y z
))
2662 (destructuring-bind (x y z
)
2664 (calc ($bfloat x
) ($bfloat y
) ($bfloat z
))))
2668 (def-simplifier carlson_rj
(x y z p
)
2670 (flet ((calc (x y z p
)
2671 (to (bigfloat::bf-rj
(bigfloat:to x
)
2675 ;; See https://dlmf.nist.gov/19.20.iii
2676 (cond ((and (alike1 x y
)
2679 ;; Rj(x,x,x,x) = x^(-3/2)
2680 (power x
(div -
3 2)))
2682 ;; Rj(x,y,z,z) = Rd(x,y,z)
2683 (ftake '%carlson_rd x y z
))
2686 ;; Rj(0,y,y,p) = 3*%pi/(2*(y*sqrt(p)+p*sqrt(y)))
2689 (add (mul y
(power p
1//2))
2690 (mul p
(power y
1//2))))))
2692 ;; Rj(x,y,y,p) = 3/(p-y)*(Rc(x,y) - Rc(x,p))
2693 (mul (div 3 (sub p y
))
2694 (sub (ftake '%carlson_rc x y
)
2695 (ftake '%carlson_rc x p
))))
2698 ;; Rj(x,y,y,y) = Rd(x,y,y)
2699 (ftake '%carlson_rd x y y
))
2700 ((float-numerical-eval-p x y z p
)
2701 (calc ($float x
) ($float y
) ($float z
) ($float p
)))
2702 ((bigfloat-numerical-eval-p x y z p
)
2703 (calc ($bfloat x
) ($bfloat y
) ($bfloat z
) ($bfloat p
)))
2704 ((setf args
(complex-float-numerical-eval-p x y z p
))
2705 (destructuring-bind (x y z p
)
2707 (calc ($float x
) ($float y
) ($float z
) ($float p
))))
2708 ((setf args
(complex-bigfloat-numerical-eval-p x y z p
))
2709 (destructuring-bind (x y z p
)
2711 (calc ($bfloat x
) ($bfloat y
) ($bfloat z
) ($bfloat p
))))
2715 ;;; Other Jacobian elliptic functions
2717 ;; jacobi_ns(u,m) = 1/jacobi_sn(u,m)
2721 ((mtimes) -
1 ((%jacobi_cn
) u m
) ((%jacobi_dn
) u m
)
2722 ((mexpt) ((%jacobi_sn
) u m
) -
2))
2724 ((mtimes) -
1 ((mexpt) ((%jacobi_sn
) u m
) -
2)
2726 ((mtimes) ((rat) 1 2)
2727 ((mexpt) ((mplus) 1 ((mtimes) -
1 m
)) -
1)
2728 ((mexpt) ((%jacobi_cn
) u m
) 2)
2730 ((mtimes) ((rat) 1 2) ((mexpt) m -
1)
2731 ((%jacobi_cn
) u m
) ((%jacobi_dn
) u m
)
2734 ((mexpt) ((mplus) 1 ((mtimes) -
1 m
)) -
1)
2735 ((%elliptic_e
) ((%asin
) ((%jacobi_sn
) u m
))
2739 (def-simplifier jacobi_ns
(u m
)
2742 ((float-numerical-eval-p u m
)
2743 (to (bigfloat:/ (bigfloat::sn
(bigfloat:to
($float u
))
2744 (bigfloat:to
($float m
))))))
2745 ((setf args
(complex-float-numerical-eval-p u m
))
2746 (destructuring-bind (u m
)
2748 (to (bigfloat:/ (bigfloat::sn
(bigfloat:to
($float u
))
2749 (bigfloat:to
($float m
)))))))
2750 ((bigfloat-numerical-eval-p u m
)
2751 (let ((uu (bigfloat:to
($bfloat u
)))
2752 (mm (bigfloat:to
($bfloat m
))))
2753 (to (bigfloat:/ (bigfloat::sn uu mm
)))))
2754 ((setf args
(complex-bigfloat-numerical-eval-p u m
))
2755 (destructuring-bind (u m
)
2757 (let ((uu (bigfloat:to
($bfloat u
)))
2758 (mm (bigfloat:to
($bfloat m
))))
2759 (to (bigfloat:/ (bigfloat::sn uu mm
))))))
2767 (dbz-err1 'jacobi_ns
))
2768 ((and $trigsign
(mminusp* u
))
2770 (neg (ftake* '%jacobi_ns
(neg u
) m
)))
2773 (member (caar u
) '(%inverse_jacobi_sn
2784 %inverse_jacobi_dc
))
2785 (alike1 (third u
) m
))
2786 (cond ((eq (caar u
) '%inverse_jacobi_ns
)
2789 ;; Express in terms of sn:
2791 (div 1 (ftake '%jacobi_sn u m
)))))
2792 ;; A&S 16.20 (Jacobi's Imaginary transformation)
2793 ((and $%iargs
(multiplep u
'$%i
))
2794 ;; ns(i*u) = 1/sn(i*u) = -i/sc(u,m1) = -i*cs(u,m1)
2796 (ftake* '%jacobi_cs
(coeff u
'$%i
1) (add 1 (neg m
))))))
2797 ((setq coef
(kc-arg2 u m
))
2800 ;; ns(m*K+u) = 1/sn(m*K+u)
2802 (destructuring-bind (lin const
)
2804 (cond ((integerp lin
)
2807 ;; ns(4*m*K+u) = ns(u)
2810 (dbz-err1 'jacobi_ns
)
2811 (ftake '%jacobi_ns const m
)))
2813 ;; ns(4*m*K + K + u) = ns(K+u) = dc(u)
2817 (ftake '%jacobi_dc const m
)))
2819 ;; ns(4*m*K+2*K + u) = ns(2*K+u) = -ns(u)
2820 ;; ns(2*K) = infinity
2822 (dbz-err1 'jacobi_ns
)
2823 (neg (ftake '%jacobi_ns const m
))))
2825 ;; ns(4*m*K+3*K+u) = ns(2*K + K + u) = -ns(K+u) = -dc(u)
2829 (neg (ftake '%jacobi_dc const m
))))))
2830 ((and (alike1 lin
1//2)
2832 (div 1 (ftake '%jacobi_sn u m
)))
2839 ;; jacobi_nc(u,m) = 1/jacobi_cn(u,m)
2843 ((mtimes) ((mexpt) ((%jacobi_cn
) u m
) -
2)
2844 ((%jacobi_dn
) u m
) ((%jacobi_sn
) u m
))
2846 ((mtimes) -
1 ((mexpt) ((%jacobi_cn
) u m
) -
2)
2848 ((mtimes) ((rat) -
1 2)
2849 ((mexpt) ((mplus) 1 ((mtimes) -
1 m
)) -
1)
2850 ((%jacobi_cn
) u m
) ((mexpt) ((%jacobi_sn
) u m
) 2))
2851 ((mtimes) ((rat) -
1 2) ((mexpt) m -
1)
2852 ((%jacobi_dn
) u m
) ((%jacobi_sn
) u m
)
2854 ((mtimes) -
1 ((mexpt) ((mplus) 1 ((mtimes) -
1 m
)) -
1)
2855 ((%elliptic_e
) ((%asin
) ((%jacobi_sn
) u m
)) m
)))))))
2858 (def-simplifier jacobi_nc
(u m
)
2861 ((float-numerical-eval-p u m
)
2862 (to (bigfloat:/ (bigfloat::cn
(bigfloat:to
($float u
))
2863 (bigfloat:to
($float m
))))))
2864 ((setf args
(complex-float-numerical-eval-p u m
))
2865 (destructuring-bind (u m
)
2867 (to (bigfloat:/ (bigfloat::cn
(bigfloat:to
($float u
))
2868 (bigfloat:to
($float m
)))))))
2869 ((bigfloat-numerical-eval-p u m
)
2870 (let ((uu (bigfloat:to
($bfloat u
)))
2871 (mm (bigfloat:to
($bfloat m
))))
2872 (to (bigfloat:/ (bigfloat::cn uu mm
)))))
2873 ((setf args
(complex-bigfloat-numerical-eval-p u m
))
2874 (destructuring-bind (u m
)
2876 (let ((uu (bigfloat:to
($bfloat u
)))
2877 (mm (bigfloat:to
($bfloat m
))))
2878 (to (bigfloat:/ (bigfloat::cn uu mm
))))))
2887 ((and $trigsign
(mminusp* u
))
2889 (ftake* '%jacobi_nc
(neg u
) m
))
2892 (member (caar u
) '(%inverse_jacobi_sn
2903 %inverse_jacobi_dc
))
2904 (alike1 (third u
) m
))
2905 (cond ((eq (caar u
) '%inverse_jacobi_nc
)
2908 ;; Express in terms of cn:
2910 (div 1 (ftake '%jacobi_cn u m
)))))
2911 ;; A&S 16.20 (Jacobi's Imaginary transformation)
2912 ((and $%iargs
(multiplep u
'$%i
))
2913 ;; nc(i*u) = 1/cn(i*u) = 1/nc(u,1-m) = cn(u,1-m)
2914 (ftake* '%jacobi_cn
(coeff u
'$%i
1) (add 1 (neg m
))))
2915 ((setq coef
(kc-arg2 u m
))
2920 (destructuring-bind (lin const
)
2922 (cond ((integerp lin
)
2925 ;; nc(4*m*K+u) = nc(u)
2929 (ftake '%jacobi_nc const m
)))
2931 ;; nc(4*m*K+K+u) = nc(K+u) = -ds(u)/sqrt(1-m)
2934 (dbz-err1 'jacobi_nc
)
2935 (neg (div (ftake '%jacobi_ds const m
)
2936 (power (sub 1 m
) 1//2)))))
2938 ;; nc(4*m*K+2*K+u) = nc(2*K+u) = -nc(u)
2942 (neg (ftake '%jacobi_nc const m
))))
2944 ;; nc(4*m*K+3*K+u) = nc(3*K+u) = nc(2*K+K+u) =
2945 ;; -nc(K+u) = ds(u)/sqrt(1-m)
2947 ;; nc(3*K) = infinity
2949 (dbz-err1 'jacobi_nc
)
2950 (div (ftake '%jacobi_ds const m
)
2951 (power (sub 1 m
) 1//2))))))
2952 ((and (alike1 1//2 lin
)
2954 (div 1 (ftake '%jacobi_cn u m
)))
2961 ;; jacobi_nd(u,m) = 1/jacobi_dn(u,m)
2965 ((mtimes) m
((%jacobi_cn
) u m
)
2966 ((mexpt) ((%jacobi_dn
) u m
) -
2) ((%jacobi_sn
) u m
))
2968 ((mtimes) -
1 ((mexpt) ((%jacobi_dn
) u m
) -
2)
2970 ((mtimes) ((rat) -
1 2)
2971 ((mexpt) ((mplus) 1 ((mtimes) -
1 m
)) -
1)
2973 ((mexpt) ((%jacobi_sn
) u m
) 2))
2974 ((mtimes) ((rat) -
1 2) ((%jacobi_cn
) u m
)
2978 ((mexpt) ((mplus) 1 ((mtimes) -
1 m
)) -
1)
2979 ((%elliptic_e
) ((%asin
) ((%jacobi_sn
) u m
))
2983 (def-simplifier jacobi_nd
(u m
)
2986 ((float-numerical-eval-p u m
)
2987 (to (bigfloat:/ (bigfloat::dn
(bigfloat:to
($float u
))
2988 (bigfloat:to
($float m
))))))
2989 ((setf args
(complex-float-numerical-eval-p u m
))
2990 (destructuring-bind (u m
)
2992 (to (bigfloat:/ (bigfloat::dn
(bigfloat:to
($float u
))
2993 (bigfloat:to
($float m
)))))))
2994 ((bigfloat-numerical-eval-p u m
)
2995 (let ((uu (bigfloat:to
($bfloat u
)))
2996 (mm (bigfloat:to
($bfloat m
))))
2997 (to (bigfloat:/ (bigfloat::dn uu mm
)))))
2998 ((setf args
(complex-bigfloat-numerical-eval-p u m
))
2999 (destructuring-bind (u m
)
3001 (let ((uu (bigfloat:to
($bfloat u
)))
3002 (mm (bigfloat:to
($bfloat m
))))
3003 (to (bigfloat:/ (bigfloat::dn uu mm
))))))
3012 ((and $trigsign
(mminusp* u
))
3014 (ftake* '%jacobi_nd
(neg u
) m
))
3017 (member (caar u
) '(%inverse_jacobi_sn
3028 %inverse_jacobi_dc
))
3029 (alike1 (third u
) m
))
3030 (cond ((eq (caar u
) '%inverse_jacobi_nd
)
3033 ;; Express in terms of dn:
3035 (div 1 (ftake '%jacobi_dn u m
)))))
3036 ;; A&S 16.20 (Jacobi's Imaginary transformation)
3037 ((and $%iargs
(multiplep u
'$%i
))
3038 ;; nd(i*u) = 1/dn(i*u) = 1/dc(u,1-m) = cd(u,1-m)
3039 (ftake* '%jacobi_cd
(coeff u
'$%i
1) (add 1 (neg m
))))
3040 ((setq coef
(kc-arg2 u m
))
3043 (destructuring-bind (lin const
)
3045 (cond ((integerp lin
)
3049 ;; nd(2*m*K+u) = nd(u)
3053 (ftake '%jacobi_nd const m
)))
3055 ;; nd(2*m*K+K+u) = nd(K+u) = dn(u)/sqrt(1-m)
3056 ;; nd(K) = 1/sqrt(1-m)
3058 (power (sub 1 m
) -
1//2)
3059 (div (ftake '%jacobi_nd const m
)
3060 (power (sub 1 m
) 1//2))))))
3067 ;; jacobi_sc(u,m) = jacobi_sn/jacobi_cn
3071 ((mtimes) ((mexpt) ((%jacobi_cn
) u m
) -
2)
3075 ((mtimes) ((mexpt) ((%jacobi_cn
) u m
) -
1)
3077 ((mtimes) ((rat) 1 2)
3078 ((mexpt) ((mplus) 1 ((mtimes) -
1 m
)) -
1)
3079 ((mexpt) ((%jacobi_cn
) u m
) 2)
3081 ((mtimes) ((rat) 1 2) ((mexpt) m -
1)
3082 ((%jacobi_cn
) u m
) ((%jacobi_dn
) u m
)
3085 ((mexpt) ((mplus) 1 ((mtimes) -
1 m
)) -
1)
3086 ((%elliptic_e
) ((%asin
) ((%jacobi_sn
) u m
))
3088 ((mtimes) -
1 ((mexpt) ((%jacobi_cn
) u m
) -
2)
3091 ((mtimes) ((rat) -
1 2)
3092 ((mexpt) ((mplus) 1 ((mtimes) -
1 m
)) -
1)
3094 ((mexpt) ((%jacobi_sn
) u m
) 2))
3095 ((mtimes) ((rat) -
1 2) ((mexpt) m -
1)
3096 ((%jacobi_dn
) u m
) ((%jacobi_sn
) u m
)
3099 ((mexpt) ((mplus) 1 ((mtimes) -
1 m
)) -
1)
3100 ((%elliptic_e
) ((%asin
) ((%jacobi_sn
) u m
))
3104 (def-simplifier jacobi_sc
(u m
)
3107 ((float-numerical-eval-p u m
)
3108 (let ((fu (bigfloat:to
($float u
)))
3109 (fm (bigfloat:to
($float m
))))
3110 (to (bigfloat:/ (bigfloat::sn fu fm
) (bigfloat::cn fu fm
)))))
3111 ((setf args
(complex-float-numerical-eval-p u m
))
3112 (destructuring-bind (u m
)
3114 (let ((fu (bigfloat:to
($float u
)))
3115 (fm (bigfloat:to
($float m
))))
3116 (to (bigfloat:/ (bigfloat::sn fu fm
) (bigfloat::cn fu fm
))))))
3117 ((bigfloat-numerical-eval-p u m
)
3118 (let ((uu (bigfloat:to
($bfloat u
)))
3119 (mm (bigfloat:to
($bfloat m
))))
3120 (to (bigfloat:/ (bigfloat::sn uu mm
)
3121 (bigfloat::cn uu mm
)))))
3122 ((setf args
(complex-bigfloat-numerical-eval-p u m
))
3123 (destructuring-bind (u m
)
3125 (let ((uu (bigfloat:to
($bfloat u
)))
3126 (mm (bigfloat:to
($bfloat m
))))
3127 (to (bigfloat:/ (bigfloat::sn uu mm
)
3128 (bigfloat::cn uu mm
))))))
3137 ((and $trigsign
(mminusp* u
))
3139 (neg (ftake* '%jacobi_sc
(neg u
) m
)))
3142 (member (caar u
) '(%inverse_jacobi_sn
3153 %inverse_jacobi_dc
))
3154 (alike1 (third u
) m
))
3155 (cond ((eq (caar u
) '%inverse_jacobi_sc
)
3158 ;; Express in terms of sn and cn
3159 ;; sc(x) = sn(x)/cn(x)
3160 (div (ftake '%jacobi_sn u m
)
3161 (ftake '%jacobi_cn u m
)))))
3162 ;; A&S 16.20 (Jacobi's Imaginary transformation)
3163 ((and $%iargs
(multiplep u
'$%i
))
3164 ;; sc(i*u) = sn(i*u)/cn(i*u) = i*sc(u,m1)/nc(u,m1) = i*sn(u,m1)
3166 (ftake* '%jacobi_sn
(coeff u
'$%i
1) (add 1 (neg m
)))))
3167 ((setq coef
(kc-arg2 u m
))
3169 ;; sc(2*m*K+u) = sc(u)
3170 (destructuring-bind (lin const
)
3172 (cond ((integerp lin
)
3175 ;; sc(2*m*K+ u) = sc(u)
3179 (ftake '%jacobi_sc const m
)))
3181 ;; sc(2*m*K + K + u) = sc(K+u)= - cs(u)/sqrt(1-m)
3184 (dbz-err1 'jacobi_sc
)
3186 (div (ftake* '%jacobi_cs const m
)
3187 (power (sub 1 m
) 1//2)))))))
3188 ((and (alike1 lin
1//2)
3190 ;; From A&S 16.3.3 and 16.5.2:
3191 ;; sc(1/2*K) = 1/(1-m)^(1/4)
3192 (power (sub 1 m
) (div -
1 4)))
3199 ;; jacobi_sd(u,m) = jacobi_sn/jacobi_dn
3203 ((mtimes) ((%jacobi_cn
) u m
)
3204 ((mexpt) ((%jacobi_dn
) u m
) -
2))
3207 ((mtimes) ((mexpt) ((%jacobi_dn
) u m
) -
1)
3209 ((mtimes) ((rat) 1 2)
3210 ((mexpt) ((mplus) 1 ((mtimes) -
1 m
)) -
1)
3211 ((mexpt) ((%jacobi_cn
) u m
) 2)
3213 ((mtimes) ((rat) 1 2) ((mexpt) m -
1)
3214 ((%jacobi_cn
) u m
) ((%jacobi_dn
) u m
)
3217 ((mexpt) ((mplus) 1 ((mtimes) -
1 m
)) -
1)
3218 ((%elliptic_e
) ((%asin
) ((%jacobi_sn
) u m
))
3220 ((mtimes) -
1 ((mexpt) ((%jacobi_dn
) u m
) -
2)
3223 ((mtimes) ((rat) -
1 2)
3224 ((mexpt) ((mplus) 1 ((mtimes) -
1 m
)) -
1)
3226 ((mexpt) ((%jacobi_sn
) u m
) 2))
3227 ((mtimes) ((rat) -
1 2) ((%jacobi_cn
) u m
)
3231 ((mexpt) ((mplus) 1 ((mtimes) -
1 m
)) -
1)
3232 ((%elliptic_e
) ((%asin
) ((%jacobi_sn
) u m
))
3236 (def-simplifier jacobi_sd
(u m
)
3239 ((float-numerical-eval-p u m
)
3240 (let ((fu (bigfloat:to
($float u
)))
3241 (fm (bigfloat:to
($float m
))))
3242 (to (bigfloat:/ (bigfloat::sn fu fm
) (bigfloat::dn fu fm
)))))
3243 ((setf args
(complex-float-numerical-eval-p u m
))
3244 (destructuring-bind (u m
)
3246 (let ((fu (bigfloat:to
($float u
)))
3247 (fm (bigfloat:to
($float m
))))
3248 (to (bigfloat:/ (bigfloat::sn fu fm
) (bigfloat::dn fu fm
))))))
3249 ((bigfloat-numerical-eval-p u m
)
3250 (let ((uu (bigfloat:to
($bfloat u
)))
3251 (mm (bigfloat:to
($bfloat m
))))
3252 (to (bigfloat:/ (bigfloat::sn uu mm
)
3253 (bigfloat::dn uu mm
)))))
3254 ((setf args
(complex-bigfloat-numerical-eval-p u m
))
3255 (destructuring-bind (u m
)
3257 (let ((uu (bigfloat:to
($bfloat u
)))
3258 (mm (bigfloat:to
($bfloat m
))))
3259 (to (bigfloat:/ (bigfloat::sn uu mm
)
3260 (bigfloat::dn uu mm
))))))
3269 ((and $trigsign
(mminusp* u
))
3271 (neg (ftake* '%jacobi_sd
(neg u
) m
)))
3274 (member (caar u
) '(%inverse_jacobi_sn
3285 %inverse_jacobi_dc
))
3286 (alike1 (third u
) m
))
3287 (cond ((eq (caar u
) '%inverse_jacobi_sd
)
3290 ;; Express in terms of sn and dn
3291 (div (ftake '%jacobi_sn u m
)
3292 (ftake '%jacobi_dn u m
)))))
3293 ;; A&S 16.20 (Jacobi's Imaginary transformation)
3294 ((and $%iargs
(multiplep u
'$%i
))
3295 ;; sd(i*u) = sn(i*u)/dn(i*u) = i*sc(u,m1)/dc(u,m1) = i*sd(u,m1)
3297 (ftake* '%jacobi_sd
(coeff u
'$%i
1) (add 1 (neg m
)))))
3298 ((setq coef
(kc-arg2 u m
))
3300 ;; sd(4*m*K+u) = sd(u)
3301 (destructuring-bind (lin const
)
3303 (cond ((integerp lin
)
3306 ;; sd(4*m*K+u) = sd(u)
3310 (ftake '%jacobi_sd const m
)))
3312 ;; sd(4*m*K+K+u) = sd(K+u) = cn(u)/sqrt(1-m)
3313 ;; sd(K) = 1/sqrt(m1)
3315 (power (sub 1 m
) 1//2)
3316 (div (ftake '%jacobi_cn const m
)
3317 (power (sub 1 m
) 1//2))))
3319 ;; sd(4*m*K+2*K+u) = sd(2*K+u) = -sd(u)
3323 (neg (ftake '%jacobi_sd const m
))))
3325 ;; sd(4*m*K+3*K+u) = sd(3*K+u) = sd(2*K+K+u) =
3326 ;; -sd(K+u) = -cn(u)/sqrt(1-m)
3327 ;; sd(3*K) = -1/sqrt(m1)
3329 (neg (power (sub 1 m
) -
1//2))
3330 (neg (div (ftake '%jacobi_cn const m
)
3331 (power (sub 1 m
) 1//2)))))))
3332 ((and (alike1 lin
1//2)
3334 ;; jacobi_sn/jacobi_dn
3335 (div (ftake '%jacobi_sn
3337 (ftake '%elliptic_kc m
))
3341 (ftake '%elliptic_kc m
))
3349 ;; jacobi_cs(u,m) = jacobi_cn/jacobi_sn
3353 ((mtimes) -
1 ((%jacobi_dn
) u m
)
3354 ((mexpt) ((%jacobi_sn
) u m
) -
2))
3357 ((mtimes) -
1 ((%jacobi_cn
) u m
)
3358 ((mexpt) ((%jacobi_sn
) u m
) -
2)
3360 ((mtimes) ((rat) 1 2)
3361 ((mexpt) ((mplus) 1 ((mtimes) -
1 m
)) -
1)
3362 ((mexpt) ((%jacobi_cn
) u m
) 2)
3364 ((mtimes) ((rat) 1 2) ((mexpt) m -
1)
3365 ((%jacobi_cn
) u m
) ((%jacobi_dn
) u m
)
3368 ((mexpt) ((mplus) 1 ((mtimes) -
1 m
)) -
1)
3369 ((%elliptic_e
) ((%asin
) ((%jacobi_sn
) u m
))
3371 ((mtimes) ((mexpt) ((%jacobi_sn
) u m
) -
1)
3373 ((mtimes) ((rat) -
1 2)
3374 ((mexpt) ((mplus) 1 ((mtimes) -
1 m
)) -
1)
3376 ((mexpt) ((%jacobi_sn
) u m
) 2))
3377 ((mtimes) ((rat) -
1 2) ((mexpt) m -
1)
3378 ((%jacobi_dn
) u m
) ((%jacobi_sn
) u m
)
3381 ((mexpt) ((mplus) 1 ((mtimes) -
1 m
)) -
1)
3382 ((%elliptic_e
) ((%asin
) ((%jacobi_sn
) u m
))
3386 (def-simplifier jacobi_cs
(u m
)
3389 ((float-numerical-eval-p u m
)
3390 (let ((fu (bigfloat:to
($float u
)))
3391 (fm (bigfloat:to
($float m
))))
3392 (to (bigfloat:/ (bigfloat::cn fu fm
) (bigfloat::sn fu fm
)))))
3393 ((setf args
(complex-float-numerical-eval-p u m
))
3394 (destructuring-bind (u m
)
3396 (let ((fu (bigfloat:to
($float u
)))
3397 (fm (bigfloat:to
($float m
))))
3398 (to (bigfloat:/ (bigfloat::cn fu fm
) (bigfloat::sn fu fm
))))))
3399 ((bigfloat-numerical-eval-p u m
)
3400 (let ((uu (bigfloat:to
($bfloat u
)))
3401 (mm (bigfloat:to
($bfloat m
))))
3402 (to (bigfloat:/ (bigfloat::cn uu mm
)
3403 (bigfloat::sn uu mm
)))))
3404 ((setf args
(complex-bigfloat-numerical-eval-p u m
))
3405 (destructuring-bind (u m
)
3407 (let ((uu (bigfloat:to
($bfloat u
)))
3408 (mm (bigfloat:to
($bfloat m
))))
3409 (to (bigfloat:/ (bigfloat::cn uu mm
)
3410 (bigfloat::sn uu mm
))))))
3418 (dbz-err1 'jacobi_cs
))
3419 ((and $trigsign
(mminusp* u
))
3421 (neg (ftake* '%jacobi_cs
(neg u
) m
)))
3424 (member (caar u
) '(%inverse_jacobi_sn
3435 %inverse_jacobi_dc
))
3436 (alike1 (third u
) m
))
3437 (cond ((eq (caar u
) '%inverse_jacobi_cs
)
3440 ;; Express in terms of cn an sn
3441 (div (ftake '%jacobi_cn u m
)
3442 (ftake '%jacobi_sn u m
)))))
3443 ;; A&S 16.20 (Jacobi's Imaginary transformation)
3444 ((and $%iargs
(multiplep u
'$%i
))
3445 ;; cs(i*u) = cn(i*u)/sn(i*u) = -i*nc(u,m1)/sc(u,m1) = -i*ns(u,m1)
3447 (ftake* '%jacobi_ns
(coeff u
'$%i
1) (add 1 (neg m
))))))
3448 ((setq coef
(kc-arg2 u m
))
3451 ;; cs(2*m*K + u) = cs(u)
3452 (destructuring-bind (lin const
)
3454 (cond ((integerp lin
)
3457 ;; cs(2*m*K + u) = cs(u)
3460 (dbz-err1 'jacobi_cs
)
3461 (ftake '%jacobi_cs const m
)))
3463 ;; cs(K+u) = -sqrt(1-m)*sc(u)
3467 (neg (mul (power (sub 1 m
) 1//2)
3468 (ftake '%jacobi_sc const m
)))))))
3469 ((and (alike1 lin
1//2)
3473 (ftake '%jacobi_sc
(mul 1//2
3474 (ftake '%elliptic_kc m
))
3482 ;; jacobi_cd(u,m) = jacobi_cn/jacobi_dn
3486 ((mtimes) ((mplus) -
1 m
)
3487 ((mexpt) ((%jacobi_dn
) u m
) -
2)
3491 ((mtimes) -
1 ((%jacobi_cn
) u m
)
3492 ((mexpt) ((%jacobi_dn
) u m
) -
2)
3494 ((mtimes) ((rat) -
1 2)
3495 ((mexpt) ((mplus) 1 ((mtimes) -
1 m
)) -
1)
3497 ((mexpt) ((%jacobi_sn
) u m
) 2))
3498 ((mtimes) ((rat) -
1 2) ((%jacobi_cn
) u m
)
3502 ((mexpt) ((mplus) 1 ((mtimes) -
1 m
)) -
1)
3503 ((%elliptic_e
) ((%asin
) ((%jacobi_sn
) u m
))
3505 ((mtimes) ((mexpt) ((%jacobi_dn
) u m
) -
1)
3507 ((mtimes) ((rat) -
1 2)
3508 ((mexpt) ((mplus) 1 ((mtimes) -
1 m
)) -
1)
3510 ((mexpt) ((%jacobi_sn
) u m
) 2))
3511 ((mtimes) ((rat) -
1 2) ((mexpt) m -
1)
3512 ((%jacobi_dn
) u m
) ((%jacobi_sn
) u m
)
3515 ((mexpt) ((mplus) 1 ((mtimes) -
1 m
)) -
1)
3516 ((%elliptic_e
) ((%asin
) ((%jacobi_sn
) u m
))
3520 (def-simplifier jacobi_cd
(u m
)
3523 ((float-numerical-eval-p u m
)
3524 (let ((fu (bigfloat:to
($float u
)))
3525 (fm (bigfloat:to
($float m
))))
3526 (to (bigfloat:/ (bigfloat::cn fu fm
) (bigfloat::dn fu fm
)))))
3527 ((setf args
(complex-float-numerical-eval-p u m
))
3528 (destructuring-bind (u m
)
3530 (let ((fu (bigfloat:to
($float u
)))
3531 (fm (bigfloat:to
($float m
))))
3532 (to (bigfloat:/ (bigfloat::cn fu fm
) (bigfloat::dn fu fm
))))))
3533 ((bigfloat-numerical-eval-p u m
)
3534 (let ((uu (bigfloat:to
($bfloat u
)))
3535 (mm (bigfloat:to
($bfloat m
))))
3536 (to (bigfloat:/ (bigfloat::cn uu mm
) (bigfloat::dn uu mm
)))))
3537 ((setf args
(complex-bigfloat-numerical-eval-p u m
))
3538 (destructuring-bind (u m
)
3540 (let ((uu (bigfloat:to
($bfloat u
)))
3541 (mm (bigfloat:to
($bfloat m
))))
3542 (to (bigfloat:/ (bigfloat::cn uu mm
) (bigfloat::dn uu mm
))))))
3551 ((and $trigsign
(mminusp* u
))
3553 (ftake* '%jacobi_cd
(neg u
) m
))
3556 (member (caar u
) '(%inverse_jacobi_sn
3567 %inverse_jacobi_dc
))
3568 (alike1 (third u
) m
))
3569 (cond ((eq (caar u
) '%inverse_jacobi_cd
)
3572 ;; Express in terms of cn and dn
3573 (div (ftake '%jacobi_cn u m
)
3574 (ftake '%jacobi_dn u m
)))))
3575 ;; A&S 16.20 (Jacobi's Imaginary transformation)
3576 ((and $%iargs
(multiplep u
'$%i
))
3577 ;; cd(i*u) = cn(i*u)/dn(i*u) = nc(u,m1)/dc(u,m1) = nd(u,m1)
3578 (ftake* '%jacobi_nd
(coeff u
'$%i
1) (add 1 (neg m
))))
3579 ((setf coef
(kc-arg2 u m
))
3582 (destructuring-bind (lin const
)
3584 (cond ((integerp lin
)
3587 ;; cd(4*m*K + u) = cd(u)
3591 (ftake '%jacobi_cd const m
)))
3593 ;; cd(4*m*K + K + u) = cd(K+u) = -sn(u)
3597 (neg (ftake '%jacobi_sn const m
))))
3599 ;; cd(4*m*K + 2*K + u) = cd(2*K+u) = -cd(u)
3603 (neg (ftake '%jacobi_cd const m
))))
3605 ;; cd(4*m*K + 3*K + u) = cd(2*K + K + u) =
3610 (ftake '%jacobi_sn const m
)))))
3611 ((and (alike1 lin
1//2)
3613 ;; jacobi_cn/jacobi_dn
3614 (div (ftake '%jacobi_cn
3616 (ftake '%elliptic_kc m
))
3620 (ftake '%elliptic_kc m
))
3629 ;; jacobi_ds(u,m) = jacobi_dn/jacobi_sn
3633 ((mtimes) -
1 ((%jacobi_cn
) u m
)
3634 ((mexpt) ((%jacobi_sn
) u m
) -
2))
3637 ((mtimes) -
1 ((%jacobi_dn
) u m
)
3638 ((mexpt) ((%jacobi_sn
) u m
) -
2)
3640 ((mtimes) ((rat) 1 2)
3641 ((mexpt) ((mplus) 1 ((mtimes) -
1 m
)) -
1)
3642 ((mexpt) ((%jacobi_cn
) u m
) 2)
3644 ((mtimes) ((rat) 1 2) ((mexpt) m -
1)
3645 ((%jacobi_cn
) u m
) ((%jacobi_dn
) u m
)
3648 ((mexpt) ((mplus) 1 ((mtimes) -
1 m
)) -
1)
3649 ((%elliptic_e
) ((%asin
) ((%jacobi_sn
) u m
))
3651 ((mtimes) ((mexpt) ((%jacobi_sn
) u m
) -
1)
3653 ((mtimes) ((rat) -
1 2)
3654 ((mexpt) ((mplus) 1 ((mtimes) -
1 m
)) -
1)
3656 ((mexpt) ((%jacobi_sn
) u m
) 2))
3657 ((mtimes) ((rat) -
1 2) ((%jacobi_cn
) u m
)
3661 ((mexpt) ((mplus) 1 ((mtimes) -
1 m
)) -
1)
3662 ((%elliptic_e
) ((%asin
) ((%jacobi_sn
) u m
))
3666 (def-simplifier jacobi_ds
(u m
)
3669 ((float-numerical-eval-p u m
)
3670 (let ((fu (bigfloat:to
($float u
)))
3671 (fm (bigfloat:to
($float m
))))
3672 (to (bigfloat:/ (bigfloat::dn fu fm
) (bigfloat::sn fu fm
)))))
3673 ((setf args
(complex-float-numerical-eval-p u m
))
3674 (destructuring-bind (u m
)
3676 (let ((fu (bigfloat:to
($float u
)))
3677 (fm (bigfloat:to
($float m
))))
3678 (to (bigfloat:/ (bigfloat::dn fu fm
) (bigfloat::sn fu fm
))))))
3679 ((bigfloat-numerical-eval-p u m
)
3680 (let ((uu (bigfloat:to
($bfloat u
)))
3681 (mm (bigfloat:to
($bfloat m
))))
3682 (to (bigfloat:/ (bigfloat::dn uu mm
)
3683 (bigfloat::sn uu mm
)))))
3684 ((setf args
(complex-bigfloat-numerical-eval-p u m
))
3685 (destructuring-bind (u m
)
3687 (let ((uu (bigfloat:to
($bfloat u
)))
3688 (mm (bigfloat:to
($bfloat m
))))
3689 (to (bigfloat:/ (bigfloat::dn uu mm
)
3690 (bigfloat::sn uu mm
))))))
3698 (dbz-err1 'jacobi_ds
))
3699 ((and $trigsign
(mminusp* u
))
3700 (neg (ftake* '%jacobi_ds
(neg u
) m
)))
3703 (member (caar u
) '(%inverse_jacobi_sn
3714 %inverse_jacobi_dc
))
3715 (alike1 (third u
) m
))
3716 (cond ((eq (caar u
) '%inverse_jacobi_ds
)
3719 ;; Express in terms of dn and sn
3720 (div (ftake '%jacobi_dn u m
)
3721 (ftake '%jacobi_sn u m
)))))
3722 ;; A&S 16.20 (Jacobi's Imaginary transformation)
3723 ((and $%iargs
(multiplep u
'$%i
))
3724 ;; ds(i*u) = dn(i*u)/sn(i*u) = -i*dc(u,m1)/sc(u,m1) = -i*ds(u,m1)
3726 (ftake* '%jacobi_ds
(coeff u
'$%i
1) (add 1 (neg m
))))))
3727 ((setf coef
(kc-arg2 u m
))
3729 (destructuring-bind (lin const
)
3731 (cond ((integerp lin
)
3734 ;; ds(4*m*K + u) = ds(u)
3737 (dbz-err1 'jacobi_ds
)
3738 (ftake '%jacobi_ds const m
)))
3740 ;; ds(4*m*K + K + u) = ds(K+u) = sqrt(1-m)*nc(u)
3741 ;; ds(K) = sqrt(1-m)
3743 (power (sub 1 m
) 1//2)
3744 (mul (power (sub 1 m
) 1//2)
3745 (ftake '%jacobi_nc const m
))))
3747 ;; ds(4*m*K + 2*K + u) = ds(2*K+u) = -ds(u)
3750 (dbz-err1 'jacobi_ds
)
3751 (neg (ftake '%jacobi_ds const m
))))
3753 ;; ds(4*m*K + 3*K + u) = ds(2*K + K + u) =
3754 ;; -ds(K+u) = -sqrt(1-m)*nc(u)
3755 ;; ds(3*K) = -sqrt(1-m)
3757 (neg (power (sub 1 m
) 1//2))
3758 (neg (mul (power (sub 1 m
) 1//2)
3759 (ftake '%jacobi_nc u m
)))))))
3760 ((and (alike1 lin
1//2)
3762 ;; jacobi_dn/jacobi_sn
3765 (mul 1//2 (ftake '%elliptic_kc m
))
3768 (mul 1//2 (ftake '%elliptic_kc m
))
3777 ;; jacobi_dc(u,m) = jacobi_dn/jacobi_cn
3781 ((mtimes) ((mplus) 1 ((mtimes) -
1 m
))
3782 ((mexpt) ((%jacobi_cn
) u m
) -
2)
3786 ((mtimes) ((mexpt) ((%jacobi_cn
) u m
) -
1)
3788 ((mtimes) ((rat) -
1 2)
3789 ((mexpt) ((mplus) 1 ((mtimes) -
1 m
)) -
1)
3791 ((mexpt) ((%jacobi_sn
) u m
) 2))
3792 ((mtimes) ((rat) -
1 2) ((%jacobi_cn
) u m
)
3796 ((mexpt) ((mplus) 1 ((mtimes) -
1 m
)) -
1)
3797 ((%elliptic_e
) ((%asin
) ((%jacobi_sn
) u m
))
3799 ((mtimes) -
1 ((mexpt) ((%jacobi_cn
) u m
) -
2)
3802 ((mtimes) ((rat) -
1 2)
3803 ((mexpt) ((mplus) 1 ((mtimes) -
1 m
)) -
1)
3805 ((mexpt) ((%jacobi_sn
) u m
) 2))
3806 ((mtimes) ((rat) -
1 2) ((mexpt) m -
1)
3807 ((%jacobi_dn
) u m
) ((%jacobi_sn
) u m
)
3810 ((mexpt) ((mplus) 1 ((mtimes) -
1 m
)) -
1)
3811 ((%elliptic_e
) ((%asin
) ((%jacobi_sn
) u m
))
3815 (def-simplifier jacobi_dc
(u m
)
3818 ((float-numerical-eval-p u m
)
3819 (let ((fu (bigfloat:to
($float u
)))
3820 (fm (bigfloat:to
($float m
))))
3821 (to (bigfloat:/ (bigfloat::dn fu fm
) (bigfloat::cn fu fm
)))))
3822 ((setf args
(complex-float-numerical-eval-p u m
))
3823 (destructuring-bind (u m
)
3825 (let ((fu (bigfloat:to
($float u
)))
3826 (fm (bigfloat:to
($float m
))))
3827 (to (bigfloat:/ (bigfloat::dn fu fm
) (bigfloat::cn fu fm
))))))
3828 ((bigfloat-numerical-eval-p u m
)
3829 (let ((uu (bigfloat:to
($bfloat u
)))
3830 (mm (bigfloat:to
($bfloat m
))))
3831 (to (bigfloat:/ (bigfloat::dn uu mm
)
3832 (bigfloat::cn uu mm
)))))
3833 ((setf args
(complex-bigfloat-numerical-eval-p u m
))
3834 (destructuring-bind (u m
)
3836 (let ((uu (bigfloat:to
($bfloat u
)))
3837 (mm (bigfloat:to
($bfloat m
))))
3838 (to (bigfloat:/ (bigfloat::dn uu mm
)
3839 (bigfloat::cn uu mm
))))))
3848 ((and $trigsign
(mminusp* u
))
3849 (ftake* '%jacobi_dc
(neg u
) m
))
3852 (member (caar u
) '(%inverse_jacobi_sn
3863 %inverse_jacobi_dc
))
3864 (alike1 (third u
) m
))
3865 (cond ((eq (caar u
) '%inverse_jacobi_dc
)
3868 ;; Express in terms of dn and cn
3869 (div (ftake '%jacobi_dn u m
)
3870 (ftake '%jacobi_cn u m
)))))
3871 ;; A&S 16.20 (Jacobi's Imaginary transformation)
3872 ((and $%iargs
(multiplep u
'$%i
))
3873 ;; dc(i*u) = dn(i*u)/cn(i*u) = dc(u,m1)/nc(u,m1) = dn(u,m1)
3874 (ftake* '%jacobi_dn
(coeff u
'$%i
1) (add 1 (neg m
))))
3875 ((setf coef
(kc-arg2 u m
))
3877 (destructuring-bind (lin const
)
3879 (cond ((integerp lin
)
3882 ;; dc(4*m*K + u) = dc(u)
3886 (ftake '%jacobi_dc const m
)))
3888 ;; dc(4*m*K + K + u) = dc(K+u) = -ns(u)
3891 (dbz-err1 'jacobi_dc
)
3892 (neg (ftake '%jacobi_ns const m
))))
3894 ;; dc(4*m*K + 2*K + u) = dc(2*K+u) = -dc(u)
3898 (neg (ftake '%jacobi_dc const m
))))
3900 ;; dc(4*m*K + 3*K + u) = dc(2*K + K + u) =
3902 ;; dc(3*K) = ns(0) = inf
3904 (dbz-err1 'jacobi_dc
)
3905 (ftake '%jacobi_dc const m
)))))
3906 ((and (alike1 lin
1//2)
3908 ;; jacobi_dn/jacobi_cn
3911 (mul 1//2 (ftake '%elliptic_kc m
))
3914 (mul 1//2 (ftake '%elliptic_kc m
))
3923 ;;; Other inverse Jacobian functions
3925 ;; inverse_jacobi_ns(x)
3927 ;; Let u = inverse_jacobi_ns(x). Then jacobi_ns(u) = x or
3928 ;; 1/jacobi_sn(u) = x or
3930 ;; jacobi_sn(u) = 1/x
3932 ;; so u = inverse_jacobi_sn(1/x)
3933 (defprop %inverse_jacobi_ns
3935 ;; Whittaker and Watson, example in 22.122
3936 ;; inverse_jacobi_ns(u,m) = integrate(1/sqrt(t^2-1)/sqrt(t^2-m), t, u, inf)
3937 ;; -> -1/sqrt(x^2-1)/sqrt(x^2-m)
3939 ((mexpt) ((mplus) -
1 ((mexpt) x
2)) ((rat) -
1 2))
3941 ((mplus) ((mtimes simp ratsimp
) -
1 m
) ((mexpt) x
2))
3944 ; ((%derivative) ((%inverse_jacobi_ns) x m) m 1)
3948 (def-simplifier inverse_jacobi_ns
(u m
)
3951 ((float-numerical-eval-p u m
)
3952 ;; Numerically evaluate asn
3954 ;; ans(x,m) = asn(1/x,m) = F(asin(1/x),m)
3955 (to (elliptic-f (cl:asin
(/ ($float u
))) ($float m
))))
3956 ((complex-float-numerical-eval-p u m
)
3957 (to (elliptic-f (cl:asin
(/ (complex ($realpart
($float u
)) ($imagpart
($float u
)))))
3958 (complex ($realpart
($float m
)) ($imagpart
($float m
))))))
3959 ((bigfloat-numerical-eval-p u m
)
3960 (to (bigfloat::bf-elliptic-f
(bigfloat:asin
(bigfloat:/ (bigfloat:to
($bfloat u
))))
3961 (bigfloat:to
($bfloat m
)))))
3962 ((setf args
(complex-bigfloat-numerical-eval-p u m
))
3963 (destructuring-bind (u m
)
3965 (to (bigfloat::bf-elliptic-f
(bigfloat:asin
(bigfloat:/ (bigfloat:to
($bfloat u
))))
3966 (bigfloat:to
($bfloat m
))))))
3968 ;; ans(x,0) = F(asin(1/x),0) = asin(1/x)
3969 (ftake '%elliptic_f
(ftake '%asin
(div 1 u
)) 0))
3971 ;; ans(x,1) = F(asin(1/x),1) = log(tan(pi/2+asin(1/x)/2))
3972 (ftake '%elliptic_f
(ftake '%asin
(div 1 u
)) 1))
3974 (ftake '%elliptic_kc m
))
3976 (neg (ftake '%elliptic_kc m
)))
3977 ((and (eq $triginverses
'$all
)
3979 (eq (caar u
) '%jacobi_ns
)
3980 (alike1 (third u
) m
))
3981 ;; inverse_jacobi_ns(ns(u)) = u
3987 ;; inverse_jacobi_nc(x)
3989 ;; Let u = inverse_jacobi_nc(x). Then jacobi_nc(u) = x or
3990 ;; 1/jacobi_cn(u) = x or
3992 ;; jacobi_cn(u) = 1/x
3994 ;; so u = inverse_jacobi_cn(1/x)
3995 (defprop %inverse_jacobi_nc
3997 ;; Whittaker and Watson, example in 22.122
3998 ;; inverse_jacobi_nc(u,m) = integrate(1/sqrt(t^2-1)/sqrt((1-m)*t^2+m), t, 1, u)
3999 ;; -> 1/sqrt(x^2-1)/sqrt((1-m)*x^2+m)
4001 ((mexpt) ((mplus) -
1 ((mexpt) x
2)) ((rat) -
1 2))
4004 ((mtimes) -
1 ((mplus) -
1 m
) ((mexpt) x
2)))
4007 ; ((%derivative) ((%inverse_jacobi_nc) x m) m 1)
4011 (def-simplifier inverse_jacobi_nc
(u m
)
4012 (cond ((or (float-numerical-eval-p u m
)
4013 (complex-float-numerical-eval-p u m
)
4014 (bigfloat-numerical-eval-p u m
)
4015 (complex-bigfloat-numerical-eval-p u m
))
4017 (ftake '%inverse_jacobi_cn
($rectform
(div 1 u
)) m
))
4021 (mul 2 (ftake '%elliptic_kc m
)))
4022 ((and (eq $triginverses
'$all
)
4024 (eq (caar u
) '%jacobi_nc
)
4025 (alike1 (third u
) m
))
4026 ;; inverse_jacobi_nc(nc(u)) = u
4032 ;; inverse_jacobi_nd(x)
4034 ;; Let u = inverse_jacobi_nd(x). Then jacobi_nd(u) = x or
4035 ;; 1/jacobi_dn(u) = x or
4037 ;; jacobi_dn(u) = 1/x
4039 ;; so u = inverse_jacobi_dn(1/x)
4040 (defprop %inverse_jacobi_nd
4042 ;; Whittaker and Watson, example in 22.122
4043 ;; inverse_jacobi_nd(u,m) = integrate(1/sqrt(t^2-1)/sqrt(1-(1-m)*t^2), t, 1, u)
4044 ;; -> 1/sqrt(u^2-1)/sqrt(1-(1-m)*t^2)
4046 ((mexpt) ((mplus) -
1 ((mexpt simp ratsimp
) x
2))
4050 ((mtimes) ((mplus) -
1 m
) ((mexpt simp ratsimp
) x
2)))
4053 ; ((%derivative) ((%inverse_jacobi_nd) x m) m 1)
4057 (def-simplifier inverse_jacobi_nd
(u m
)
4058 (cond ((or (float-numerical-eval-p u m
)
4059 (complex-float-numerical-eval-p u m
)
4060 (bigfloat-numerical-eval-p u m
)
4061 (complex-bigfloat-numerical-eval-p u m
))
4062 (ftake '%inverse_jacobi_dn
($rectform
(div 1 u
)) m
))
4065 ((onep1 ($ratsimp
(mul (power (sub 1 m
) 1//2) u
)))
4066 ;; jacobi_nd(1/sqrt(1-m),m) = K(m). This follows from
4067 ;; jacobi_dn(sqrt(1-m),m) = K(m).
4068 (ftake '%elliptic_kc m
))
4069 ((and (eq $triginverses
'$all
)
4071 (eq (caar u
) '%jacobi_nd
)
4072 (alike1 (third u
) m
))
4073 ;; inverse_jacobi_nd(nd(u)) = u
4079 ;; inverse_jacobi_sc(x)
4081 ;; Let u = inverse_jacobi_sc(x). Then jacobi_sc(u) = x or
4082 ;; x = jacobi_sn(u)/jacobi_cn(u)
4089 ;; sn^2 = x^2/(1+x^2)
4091 ;; sn(u) = x/sqrt(1+x^2)
4093 ;; u = inverse_sn(x/sqrt(1+x^2))
4095 (defprop %inverse_jacobi_sc
4097 ;; Whittaker and Watson, example in 22.122
4098 ;; inverse_jacobi_sc(u,m) = integrate(1/sqrt(1+t^2)/sqrt(1+(1-m)*t^2), t, 0, u)
4099 ;; -> 1/sqrt(1+x^2)/sqrt(1+(1-m)*x^2)
4101 ((mexpt) ((mplus) 1 ((mexpt) x
2))
4105 ((mtimes) -
1 ((mplus) -
1 m
) ((mexpt) x
2)))
4108 ; ((%derivative) ((%inverse_jacobi_sc) x m) m 1)
4112 (def-simplifier inverse_jacobi_sc
(u m
)
4113 (cond ((or (float-numerical-eval-p u m
)
4114 (complex-float-numerical-eval-p u m
)
4115 (bigfloat-numerical-eval-p u m
)
4116 (complex-bigfloat-numerical-eval-p u m
))
4117 (ftake '%inverse_jacobi_sn
4118 ($rectform
(div u
(power (add 1 (mul u u
)) 1//2)))
4121 ;; jacobi_sc(0,m) = 0
4123 ((and (eq $triginverses
'$all
)
4125 (eq (caar u
) '%jacobi_sc
)
4126 (alike1 (third u
) m
))
4127 ;; inverse_jacobi_sc(sc(u)) = u
4133 ;; inverse_jacobi_sd(x)
4135 ;; Let u = inverse_jacobi_sd(x). Then jacobi_sd(u) = x or
4136 ;; x = jacobi_sn(u)/jacobi_dn(u)
4139 ;; = sn^2/(1-m*sn^2)
4143 ;; sn^2 = x^2/(1+m*x^2)
4145 ;; sn(u) = x/sqrt(1+m*x^2)
4147 ;; u = inverse_sn(x/sqrt(1+m*x^2))
4149 (defprop %inverse_jacobi_sd
4151 ;; Whittaker and Watson, example in 22.122
4152 ;; inverse_jacobi_sd(u,m) = integrate(1/sqrt(1-(1-m)*t^2)/sqrt(1+m*t^2), t, 0, u)
4153 ;; -> 1/sqrt(1-(1-m)*x^2)/sqrt(1+m*x^2)
4156 ((mplus) 1 ((mtimes) ((mplus) -
1 m
) ((mexpt) x
2)))
4158 ((mexpt) ((mplus) 1 ((mtimes) m
((mexpt) x
2)))
4161 ; ((%derivative) ((%inverse_jacobi_sd) x m) m 1)
4165 (def-simplifier inverse_jacobi_sd
(u m
)
4166 (cond ((or (float-numerical-eval-p u m
)
4167 (complex-float-numerical-eval-p u m
)
4168 (bigfloat-numerical-eval-p u m
)
4169 (complex-bigfloat-numerical-eval-p u m
))
4170 (ftake '%inverse_jacobi_sn
4171 ($rectform
(div u
(power (add 1 (mul m
(mul u u
))) 1//2)))
4175 ((eql 0 ($ratsimp
(sub u
(div 1 (power (sub 1 m
) 1//2)))))
4176 ;; inverse_jacobi_sd(1/sqrt(1-m), m) = elliptic_kc(m)
4178 ;; We can see this from inverse_jacobi_sd(x,m) =
4179 ;; inverse_jacobi_sn(x/sqrt(1+m*x^2), m). So
4180 ;; inverse_jacobi_sd(1/sqrt(1-m),m) = inverse_jacobi_sn(1,m)
4181 (ftake '%elliptic_kc m
))
4182 ((and (eq $triginverses
'$all
)
4184 (eq (caar u
) '%jacobi_sd
)
4185 (alike1 (third u
) m
))
4186 ;; inverse_jacobi_sd(sd(u)) = u
4192 ;; inverse_jacobi_cs(x)
4194 ;; Let u = inverse_jacobi_cs(x). Then jacobi_cs(u) = x or
4195 ;; 1/x = 1/jacobi_cs(u) = jacobi_sc(u)
4197 ;; u = inverse_sc(1/x)
4199 (defprop %inverse_jacobi_cs
4201 ;; Whittaker and Watson, example in 22.122
4202 ;; inverse_jacobi_cs(u,m) = integrate(1/sqrt(t^2+1)/sqrt(t^2+(1-m)), t, u, inf)
4203 ;; -> -1/sqrt(x^2+1)/sqrt(x^2+(1-m))
4205 ((mexpt) ((mplus) 1 ((mexpt simp ratsimp
) x
2))
4208 ((mtimes simp ratsimp
) -
1 m
)
4209 ((mexpt simp ratsimp
) x
2))
4212 ; ((%derivative) ((%inverse_jacobi_cs) x m) m 1)
4216 (def-simplifier inverse_jacobi_cs
(u m
)
4217 (cond ((or (float-numerical-eval-p u m
)
4218 (complex-float-numerical-eval-p u m
)
4219 (bigfloat-numerical-eval-p u m
)
4220 (complex-bigfloat-numerical-eval-p u m
))
4221 (ftake '%inverse_jacobi_sc
($rectform
(div 1 u
)) m
))
4223 (ftake '%elliptic_kc m
))
4228 ;; inverse_jacobi_cd(x)
4230 ;; Let u = inverse_jacobi_cd(x). Then jacobi_cd(u) = x or
4231 ;; x = jacobi_cn(u)/jacobi_dn(u)
4234 ;; = (1-sn^2)/(1-m*sn^2)
4238 ;; sn^2 = (1-x^2)/(1-m*x^2)
4240 ;; sn(u) = sqrt(1-x^2)/sqrt(1-m*x^2)
4242 ;; u = inverse_sn(sqrt(1-x^2)/sqrt(1-m*x^2))
4244 (defprop %inverse_jacobi_cd
4246 ;; Whittaker and Watson, example in 22.122
4247 ;; inverse_jacobi_cd(u,m) = integrate(1/sqrt(1-t^2)/sqrt(1-m*t^2), t, u, 1)
4248 ;; -> -1/sqrt(1-x^2)/sqrt(1-m*x^2)
4251 ((mplus) 1 ((mtimes) -
1 ((mexpt) x
2)))
4254 ((mplus) 1 ((mtimes) -
1 m
((mexpt) x
2)))
4257 ; ((%derivative) ((%inverse_jacobi_cd) x m) m 1)
4261 (def-simplifier inverse_jacobi_cd
(u m
)
4262 (cond ((or (complex-float-numerical-eval-p u m
)
4263 (complex-bigfloat-numerical-eval-p u m
))
4265 (ftake '%inverse_jacobi_sn
4266 ($rectform
(div (power (mul (sub 1 u
) (add 1 u
)) 1//2)
4267 (power (sub 1 (mul m
(mul u u
))) 1//2)))
4272 (ftake '%elliptic_kc m
))
4273 ((and (eq $triginverses
'$all
)
4275 (eq (caar u
) '%jacobi_cd
)
4276 (alike1 (third u
) m
))
4277 ;; inverse_jacobi_cd(cd(u)) = u
4283 ;; inverse_jacobi_ds(x)
4285 ;; Let u = inverse_jacobi_ds(x). Then jacobi_ds(u) = x or
4286 ;; 1/x = 1/jacobi_ds(u) = jacobi_sd(u)
4288 ;; u = inverse_sd(1/x)
4290 (defprop %inverse_jacobi_ds
4292 ;; Whittaker and Watson, example in 22.122
4293 ;; inverse_jacobi_ds(u,m) = integrate(1/sqrt(t^2-(1-m))/sqrt(t^2+m), t, u, inf)
4294 ;; -> -1/sqrt(x^2-(1-m))/sqrt(x^2+m)
4297 ((mplus) -
1 m
((mexpt simp ratsimp
) x
2))
4300 ((mplus) m
((mexpt simp ratsimp
) x
2))
4303 ; ((%derivative) ((%inverse_jacobi_ds) x m) m 1)
4307 (def-simplifier inverse_jacobi_ds
(u m
)
4308 (cond ((or (float-numerical-eval-p u m
)
4309 (complex-float-numerical-eval-p u m
)
4310 (bigfloat-numerical-eval-p u m
)
4311 (complex-bigfloat-numerical-eval-p u m
))
4312 (ftake '%inverse_jacobi_sd
($rectform
(div 1 u
)) m
))
4313 ((and $trigsign
(mminusp* u
))
4314 (neg (ftake* '%inverse_jacobi_ds
(neg u
) m
)))
4315 ((eql 0 ($ratsimp
(sub u
(power (sub 1 m
) 1//2))))
4316 ;; inverse_jacobi_ds(sqrt(1-m),m) = elliptic_kc(m)
4318 ;; Since inverse_jacobi_ds(sqrt(1-m), m) =
4319 ;; inverse_jacobi_sd(1/sqrt(1-m),m). And we know from
4320 ;; above that this is elliptic_kc(m)
4321 (ftake '%elliptic_kc m
))
4322 ((and (eq $triginverses
'$all
)
4324 (eq (caar u
) '%jacobi_ds
)
4325 (alike1 (third u
) m
))
4326 ;; inverse_jacobi_ds(ds(u)) = u
4333 ;; inverse_jacobi_dc(x)
4335 ;; Let u = inverse_jacobi_dc(x). Then jacobi_dc(u) = x or
4336 ;; 1/x = 1/jacobi_dc(u) = jacobi_cd(u)
4338 ;; u = inverse_cd(1/x)
4340 (defprop %inverse_jacobi_dc
4342 ;; Note: Whittaker and Watson, example in 22.122 says
4343 ;; inverse_jacobi_dc(u,m) = integrate(1/sqrt(t^2-1)/sqrt(t^2-m),
4344 ;; t, u, 1) but that seems wrong. A&S 17.4.47 says
4345 ;; integrate(1/sqrt(t^2-1)/sqrt(t^2-m), t, a, u) =
4346 ;; inverse_jacobi_cd(x,m). Lawden 3.2.8 says the same.
4347 ;; functions.wolfram.com says the derivative is
4348 ;; 1/sqrt(t^2-1)/sqrt(t^2-m).
4351 ((mplus) -
1 ((mexpt simp ratsimp
) x
2))
4355 ((mtimes simp ratsimp
) -
1 m
)
4356 ((mexpt simp ratsimp
) x
2))
4359 ; ((%derivative) ((%inverse_jacobi_dc) x m) m 1)
4363 (def-simplifier inverse_jacobi_dc
(u m
)
4364 (cond ((or (complex-float-numerical-eval-p u m
)
4365 (complex-bigfloat-numerical-eval-p u m
))
4366 (ftake '%inverse_jacobi_cd
($rectform
(div 1 u
)) m
))
4369 ((and (eq $triginverses
'$all
)
4371 (eq (caar u
) '%jacobi_dc
)
4372 (alike1 (third u
) m
))
4373 ;; inverse_jacobi_dc(dc(u)) = u
4379 ;; Convert an inverse Jacobian function into the equivalent elliptic
4382 ;; See A&S 17.4.41-17.4.52.
4383 (defun make-elliptic-f (e)
4386 ((member (caar e
) '(%inverse_jacobi_sc %inverse_jacobi_cs
4387 %inverse_jacobi_nd %inverse_jacobi_dn
4388 %inverse_jacobi_sn %inverse_jacobi_cd
4389 %inverse_jacobi_dc %inverse_jacobi_ns
4390 %inverse_jacobi_nc %inverse_jacobi_ds
4391 %inverse_jacobi_sd %inverse_jacobi_cn
))
4392 ;; We have some inverse Jacobi function. Convert it to the F form.
4393 (destructuring-bind ((fn &rest ops
) u m
)
4395 (declare (ignore ops
))
4399 (ftake '%elliptic_f
(ftake '%atan u
) m
))
4402 (ftake '%elliptic_f
(ftake '%atan
(div 1 u
)) m
))
4407 (mul (power m -
1//2)
4409 (power (add -
1 (mul u u
))
4417 (power (sub 1 (power u
2)) 1//2)))
4421 (ftake '%elliptic_f
(ftake '%asin u
) m
))
4426 (power (mul (sub 1 (mul u u
))
4427 (sub 1 (mul m u u
)))
4434 (power (mul (sub (mul u u
) 1)
4440 (ftake '%elliptic_f
(ftake '%asin
(div 1 u
)) m
))
4443 (ftake '%elliptic_f
(ftake '%acos
(div 1 u
)) m
))
4448 (power (add m
(mul u u
))
4456 (power (add 1 (mul m u u
))
4461 (ftake '%elliptic_f
(ftake '%acos u
) m
)))))
4463 (recur-apply #'make-elliptic-f e
))))
4465 (defmfun $make_elliptic_f
(e)
4468 (simplify (make-elliptic-f e
))))
4470 (defun make-elliptic-e (e)
4472 ((eq (caar e
) '$elliptic_eu
)
4473 (destructuring-bind ((ffun &rest ops
) u m
) e
4474 (declare (ignore ffun ops
))
4475 (ftake '%elliptic_e
(ftake '%asin
(ftake '%jacobi_sn u m
)) m
)))
4477 (recur-apply #'make-elliptic-e e
))))
4479 (defmfun $make_elliptic_e
(e)
4482 (simplify (make-elliptic-e e
))))
4485 ;; Eu(u,m) = integrate(jacobi_dn(v,m)^2,v,0,u)
4486 ;; = integrate(sqrt((1-m*t^2)/(1-t^2)),t,0,jacobi_sn(u,m))
4488 ;; Eu(u,m) = E(am(u),m)
4490 ;; where E(u,m) is elliptic-e above.
4493 ;; Lawden gives the following relationships
4495 ;; E(u+v) = E(u) + E(v) - m*sn(u)*sn(v)*sn(u+v)
4496 ;; E(u,0) = u, E(u,1) = tanh u
4498 ;; E(i*u,k) = i*sc(u,k')*dn(u,k') - i*E(u,k') + i*u
4500 ;; E(2*i*K') = 2*i*(K'-E')
4502 ;; E(u + 2*i*K') = E(u) + 2*i*(K' - E')
4504 ;; E(u+K) = E(u) + E - k^2*sn(u)*cd(u)
4505 (defun elliptic-eu (u m
)
4507 ;; E(u + 2*n*K) = E(u) + 2*n*E
4508 (let ((ell-k (to (elliptic-k m
)))
4509 (ell-e (elliptic-ec m
)))
4510 (multiple-value-bind (n u-rem
)
4511 (floor u
(* 2 ell-k
))
4514 (cond ((>= u-rem ell-k
)
4515 ;; 0 <= u-rem < K so
4516 ;; E(u + K) = E(u) + E - m*sn(u)*cd(u)
4517 (let ((u-k (- u ell-k
)))
4518 (- (+ (elliptic-e (cl:asin
(bigfloat::sn u-k m
)) m
)
4520 (/ (* m
(bigfloat::sn u-k m
) (bigfloat::cn u-k m
))
4521 (bigfloat::dn u-k m
)))))
4523 (elliptic-e (cl:asin
(bigfloat::sn u m
)) m
)))))))
4527 ;; E(u+i*v, m) = E(u,m) -i*E(v,m') + i*v + i*sc(v,m')*dn(v,m')
4528 ;; -i*m*sn(u,m)*sc(v,m')*sn(u+i*v,m)
4530 (let ((u-r (realpart u
))
4533 (+ (elliptic-eu u-r m
)
4536 (/ (* (bigfloat::sn u-i m1
) (bigfloat::dn u-i m1
))
4537 (bigfloat::cn u-i m1
)))
4538 (+ (elliptic-eu u-i m1
)
4539 (/ (* m
(bigfloat::sn u-r m
) (bigfloat::sn u-i m1
) (bigfloat::sn u m
))
4540 (bigfloat::cn u-i m1
))))))))))
4542 (defprop $elliptic_eu
4544 ((mexpt) ((%jacobi_dn
) u m
) 2)
4549 (def-simplifier elliptic_eu
(u m
)
4551 ;; as it stands, ELLIPTIC-EU can't handle bigfloats or complex bigfloats,
4552 ;; so handle only floats and complex floats here.
4553 ((float-numerical-eval-p u m
)
4554 (elliptic-eu ($float u
) ($float m
)))
4555 ((complex-float-numerical-eval-p u m
)
4556 (let ((u-r ($realpart u
))
4559 (complexify (elliptic-eu (complex u-r u-i
) m
))))
4564 (def-simplifier jacobi_am
(u m
)
4566 ;; as it stands, BIGFLOAT::SN can't handle bigfloats or complex bigfloats,
4567 ;; so handle only floats and complex floats here.
4568 ((float-numerical-eval-p u m
)
4569 (cl:asin
(bigfloat::sn
($float u
) ($float m
))))
4570 ((complex-float-numerical-eval-p u m
)
4571 (let ((u-r ($realpart
($float u
)))
4572 (u-i ($imagpart
($float u
)))
4574 (complexify (cl:asin
(bigfloat::sn
(complex u-r u-i
) m
)))))
4579 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
4580 ;; Integrals. At present with respect to first argument only.
4581 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
4583 ;; A&S 16.24.1: integrate(jacobi_sn(u,m),u)
4584 ;; = log(jacobi_dn(u,m)-sqrt(m)*jacobi_cn(u,m))/sqrt(m)
4587 ((mtimes simp
) ((mexpt simp
) m
((rat simp
) -
1 2))
4590 ((mtimes simp
) -
1 ((mexpt simp
) m
((rat simp
) 1 2))
4591 ((%jacobi_cn simp
) u m
))
4592 ((%jacobi_dn simp
) u m
))))
4596 ;; A&S 16.24.2: integrate(jacobi_cn(u,m),u) = acos(jacobi_dn(u,m))/sqrt(m)
4599 ((mtimes simp
) ((mexpt simp
) m
((rat simp
) -
1 2))
4600 ((%acos simp
) ((%jacobi_dn simp
) u m
)))
4604 ;; A&S 16.24.3: integrate(jacobi_dn(u,m),u) = asin(jacobi_sn(u,m))
4607 ((%asin simp
) ((%jacobi_sn simp
) u m
))
4611 ;; A&S 16.24.4: integrate(jacobi_cd(u,m),u)
4612 ;; = log(jacobi_nd(u,m)+sqrt(m)*jacobi_sd(u,m))/sqrt(m)
4615 ((mtimes simp
) ((mexpt simp
) m
((rat simp
) -
1 2))
4617 ((mplus simp
) ((%jacobi_nd simp
) u m
)
4618 ((mtimes simp
) ((mexpt simp
) m
((rat simp
) 1 2))
4619 ((%jacobi_sd simp
) u m
)))))
4623 ;; integrate(jacobi_sd(u,m),u)
4625 ;; A&S 16.24.5 gives
4626 ;; asin(-sqrt(m)*jacobi_cd(u,m))/sqrt(m*m_1), where m + m_1 = 1
4627 ;; but this does not pass some simple tests.
4629 ;; functions.wolfram.com 09.35.21.001.01 gives
4630 ;; -asin(sqrt(m)*jacobi_cd(u,m))*sqrt(1-m*jacobi_cd(u,m)^2)*jacobi_dn(u,m)/((1-m)*sqrt(m))
4631 ;; and this does pass.
4635 ((mexpt simp
) ((mplus simp
) 1 ((mtimes simp
) -
1 m
)) -
1)
4636 ((mexpt simp
) m
((rat simp
) -
1 2))
4639 ((mtimes simp
) -
1 $m
((mexpt simp
) ((%jacobi_cd simp
) u m
) 2)))
4641 ((%jacobi_dn simp
) u m
)
4643 ((mtimes simp
) ((mexpt simp
) m
((rat simp
) 1 2))
4644 ((%jacobi_cd simp
) u m
))))
4648 ;; integrate(jacobi_nd(u,m),u)
4650 ;; A&S 16.24.6 gives
4651 ;; acos(jacobi_cd(u,m))/sqrt(m_1), where m + m_1 = 1
4652 ;; but this does not pass some simple tests.
4654 ;; functions.wolfram.com 09.32.21.0001.01 gives
4655 ;; sqrt(1-jacobi_cd(u,m)^2)*acos(jacobi_cd(u,m))/((1-m)*jacobi_sd(u,m))
4656 ;; and this does pass.
4659 ((mtimes simp
) ((mexpt simp
) ((mplus simp
) 1 ((mtimes simp
) -
1 m
)) -
1)
4662 ((mtimes simp
) -
1 ((mexpt simp
) ((%jacobi_cd simp
) u m
) 2)))
4664 ((mexpt simp
) ((%jacobi_sd simp
) u m
) -
1)
4665 ((%acos simp
) ((%jacobi_cd simp
) u m
)))
4669 ;; A&S 16.24.7: integrate(jacobi_dc(u,m),u) = log(jacobi_nc(u,m)+jacobi_sc(u,m))
4672 ((%log simp
) ((mplus simp
) ((%jacobi_nc simp
) u m
) ((%jacobi_sc simp
) u m
)))
4676 ;; A&S 16.24.8: integrate(jacobi_nc(u,m),u)
4677 ;; = log(jacobi_dc(u,m)+sqrt(m_1)*jacobi_sc(u,m))/sqrt(m_1), where m + m_1 = 1
4681 ((mexpt simp
) ((mplus simp
) 1 ((mtimes simp
) -
1 m
))
4684 ((mplus simp
) ((%jacobi_dc simp
) u m
)
4686 ((mexpt simp
) ((mplus simp
) 1 ((mtimes simp
) -
1 m
))
4688 ((%jacobi_sc simp
) u m
)))))
4692 ;; A&S 16.24.9: integrate(jacobi_sc(u,m),u)
4693 ;; = log(jacobi_dc(u,m)+sqrt(m_1)*jacobi_nc(u,m))/sqrt(m_1), where m + m_1 = 1
4697 ((mexpt simp
) ((mplus simp
) 1 ((mtimes simp
) -
1 m
))
4700 ((mplus simp
) ((%jacobi_dc simp
) u m
)
4702 ((mexpt simp
) ((mplus simp
) 1 ((mtimes simp
) -
1 m
))
4704 ((%jacobi_nc simp
) u m
)))))
4708 ;; A&S 16.24.10: integrate(jacobi_ns(u,m),u)
4709 ;; = log(jacobi_ds(u,m)-jacobi_cs(u,m))
4713 ((mplus simp
) ((mtimes simp
) -
1 ((%jacobi_cs simp
) u m
))
4714 ((%jacobi_ds simp
) u m
)))
4718 ;; integrate(jacobi_ds(u,m),u)
4720 ;; A&S 16.24.11 gives
4721 ;; log(jacobi_ds(u,m)-jacobi_cs(u,m))
4722 ;; but this does not pass some simple tests.
4724 ;; functions.wolfram.com 09.30.21.0001.01 gives
4725 ;; log((1-jacobi_cn(u,m))/jacobi_sn(u,m))
4731 ((mplus simp
) 1 ((mtimes simp
) -
1 ((%jacobi_cn simp
) u m
)))
4732 ((mexpt simp
) ((%jacobi_sn simp
) u m
) -
1)))
4736 ;; A&S 16.24.12: integrate(jacobi_cs(u,m),u) = log(jacobi_ns(u,m)-jacobi_ds(u,m))
4740 ((mplus simp
) ((mtimes simp
) -
1 ((%jacobi_ds simp
) u m
))
4741 ((%jacobi_ns simp
) u m
)))
4745 ;; functions.wolfram.com 09.48.21.0001.01
4746 ;; integrate(inverse_jacobi_sn(u,m),u) =
4747 ;; inverse_jacobi_sn(u,m)*u
4748 ;; - log( jacobi_dn(inverse_jacobi_sn(u,m),m)
4749 ;; -sqrt(m)*jacobi_cn(inverse_jacobi_sn(u,m),m)) / sqrt(m)
4750 (defprop %inverse_jacobi_sn
4752 ((mplus simp
) ((mtimes simp
) u
((%inverse_jacobi_sn simp
) u m
))
4753 ((mtimes simp
) -
1 ((mexpt simp
) m
((rat simp
) -
1 2))
4756 ((mtimes simp
) -
1 ((mexpt simp
) m
((rat simp
) 1 2))
4757 ((%jacobi_cn simp
) ((%inverse_jacobi_sn simp
) u m
) m
))
4758 ((%jacobi_dn simp
) ((%inverse_jacobi_sn simp
) u m
) m
)))))
4762 ;; functions.wolfram.com 09.38.21.0001.01
4763 ;; integrate(inverse_jacobi_cn(u,m),u) =
4764 ;; u*inverse_jacobi_cn(u,m)
4765 ;; -%i*log(%i*jacobi_dn(inverse_jacobi_cn(u,m),m)/sqrt(m)
4766 ;; -jacobi_sn(inverse_jacobi_cn(u,m),m))
4768 (defprop %inverse_jacobi_cn
4770 ((mplus simp
) ((mtimes simp
) u
((%inverse_jacobi_cn simp
) u m
))
4771 ((mtimes simp
) -
1 $%i
((mexpt simp
) m
((rat simp
) -
1 2))
4774 ((mtimes simp
) $%i
((mexpt simp
) m
((rat simp
) -
1 2))
4775 ((%jacobi_dn simp
) ((%inverse_jacobi_cn simp
) u m
) m
))
4777 ((%jacobi_sn simp
) ((%inverse_jacobi_cn simp
) u m
) m
))))))
4781 ;; functions.wolfram.com 09.41.21.0001.01
4782 ;; integrate(inverse_jacobi_dn(u,m),u) =
4783 ;; u*inverse_jacobi_dn(u,m)
4784 ;; - %i*log(%i*jacobi_cn(inverse_jacobi_dn(u,m),m)
4785 ;; +jacobi_sn(inverse_jacobi_dn(u,m),m))
4786 (defprop %inverse_jacobi_dn
4788 ((mplus simp
) ((mtimes simp
) u
((%inverse_jacobi_dn simp
) u m
))
4789 ((mtimes simp
) -
1 $%i
4793 ((%jacobi_cn simp
) ((%inverse_jacobi_dn simp
) u m
) m
))
4794 ((%jacobi_sn simp
) ((%inverse_jacobi_dn simp
) u m
) m
)))))
4799 ;; Real and imaginary part for Jacobi elliptic functions.
4800 (defprop %jacobi_sn risplit-sn-cn-dn risplit-function
)
4801 (defprop %jacobi_cn risplit-sn-cn-dn risplit-function
)
4802 (defprop %jacobi_dn risplit-sn-cn-dn risplit-function
)
4804 (defun risplit-sn-cn-dn (expr)
4805 (let* ((arg (second expr
))
4806 (param (third expr
)))
4807 ;; We only split on the argument, not the order
4808 (destructuring-bind (arg-r . arg-i
)
4812 (cons (take (first expr
) arg-r param
)
4815 (let* ((s (ftake '%jacobi_sn arg-r param
))
4816 (c (ftake '%jacobi_cn arg-r param
))
4817 (d (ftake '%jacobi_dn arg-r param
))
4818 (s1 (ftake '%jacobi_sn arg-i
(sub 1 param
)))
4819 (c1 (ftake '%jacobi_cn arg-i
(sub 1 param
)))
4820 (d1 (ftake '%jacobi_dn arg-i
(sub 1 param
)))
4821 (den (add (mul c1 c1
)
4825 ;; Let s = jacobi_sn(x,m)
4826 ;; c = jacobi_cn(x,m)
4827 ;; d = jacobi_dn(x,m)
4828 ;; s1 = jacobi_sn(y,1-m)
4829 ;; c1 = jacobi_cn(y,1-m)
4830 ;; d1 = jacobi_dn(y,1-m)
4834 ;; jacobi_sn(x+%i*y,m) =
4836 ;; s*d1 + %i*c*d*s1*c1
4837 ;; -------------------
4840 (cons (div (mul s d1
) den
)
4841 (div (mul c
(mul d
(mul s1 c1
)))
4848 ;; c*c1 - %i*s*d*s1*d1
4849 ;; -------------------
4851 (cons (div (mul c c1
) den
)
4853 (mul s
(mul d
(mul s1 d1
))))
4860 ;; d*c1*d1 - %i*m*s*c*s1
4861 ;; ---------------------
4863 (cons (div (mul d
(mul c1 d1
))
4865 (div (mul -
1 (mul param
(mul s
(mul c s1
))))
4869 ;; Jacobi amplitude function.
4871 (in-package #:bigfloat
)
4873 ;; Arithmetic-Geometric Mean algorithm for real or complex numbers.
4874 ;; See https://dlmf.nist.gov/22.20.ii.
4875 (let ((an (make-array 100 :fill-pointer
0))
4876 (bn (make-array 100 :fill-pointer
0))
4877 (cn (make-array 100 :fill-pointer
0)))
4878 ;; Instead of allocating these array anew each time, we'll reuse
4879 ;; them and allow them to grow as needed.
4880 (defun agm (a0 b0 c0 tol
)
4881 "Arithmetic-Geometric Mean algorithm for real or complex a0, b0, c0.
4882 Algorithm continues until |c[n]| <= tol."
4884 ;; DLMF (https://dlmf.nist.gov/22.20.ii) says for any real or
4885 ;; complex a0 and b0, b0/a0 must not be real and negative. Let's
4887 (let ((q (/ b0 a0
)))
4888 (when (and (= (imagpart q
) 0)
4889 (minusp (realpart q
)))
4890 (error "Invalid arguments for AGM: ~A ~A~%" a0 b0
)))
4891 (let ((nd (max (* 2 (ceiling (log (- (log tol
2))))) 8)))
4892 ;; DLMF (https://dlmf.nist.gov/22.20.ii) says that |c[n]| <=
4893 ;; C*2^(-2^n), for some constant C. Solve C*2^(-2^n) = tol to
4894 ;; get n = log(log(C/tol)/log(2))/log(2). Arbitrarily assume C
4895 ;; is one to get n = log(-(log(tol)/log(2)))/log(2). Thus, the
4896 ;; approximate number of term needed is n =
4897 ;; 1.44*log(-(1.44*log(tol))). Round to 2*log(-log2(tol)).
4898 (setf (fill-pointer an
) 0
4900 (fill-pointer cn
) 0)
4901 (vector-push-extend a0 an
)
4902 (vector-push-extend b0 bn
)
4903 (vector-push-extend c0 cn
)
4906 ((or (<= (abs (aref cn k
)) tol
)
4909 (error "Failed to converge")
4910 (values k an bn cn
)))
4911 (vector-push-extend (/ (+ (aref an k
) (aref bn k
)) 2) an
)
4912 ;; DLMF (https://dlmf.nist.gov/22.20.ii) has conditions on how
4913 ;; to choose the square root depending on the phase of a[n-1]
4914 ;; and b[n-1]. We don't check for that here.
4915 (vector-push-extend (sqrt (* (aref an k
) (aref bn k
))) bn
)
4916 (vector-push-extend (/ (- (aref an k
) (aref bn k
)) 2) cn
)))))
4918 (defun jacobi-am-agm (u m tol
)
4919 "Evaluate the jacobi_am function from real u and m with |m| <= 1. This
4920 uses the AGM method until a tolerance of TOL is reached for the
4922 (multiple-value-bind (n an bn cn
)
4923 (agm 1 (sqrt (- 1 m
)) (sqrt m
) tol
)
4924 (declare (ignore bn
))
4925 ;; See DLMF (https://dlmf.nist.gov/22.20.ii) for the algorithm.
4926 (let ((phi (* u
(aref an n
) (expt 2 n
))))
4927 (loop for k from n downto
1
4929 (setf phi
(/ (+ phi
(asin (* (/ (aref cn k
)
4935 ;; For real z and real m with |m| < 1, this appears to be less
4936 ;; accurate than using AGM. For am(3,0.7), we get 2.1263556865337185,
4937 ;; but AGM gives 2.126355671062825, which matches what Wolfram gives.
4939 (defun am-q-series (z m limit
)
4940 (let* ((K (bf-elliptic-k m
))
4941 (K-prime (bf-elliptic-k (- 1 m
)))
4942 (2-arg (/ (* (%pi z
) z
)
4944 (q (exp (- (* (%pi z
)
4946 (format t
"K = ~A~%" K
)
4947 (format t
"K-prime = ~A~%" K-prime
)
4948 (format t
"2-arg = ~A~%" 2-arg
)
4949 (format t
"q = ~A~%" q
)
4952 (term (* (sin (* n
2-arg
))
4954 (* n
(1+ (expt q
(* 2 n
))))))
4955 (* (sin (* n
2-arg
))
4957 (* n
(1+ (expt q
(* 2 n
)))))))
4960 ((<= (abs term
) limit
)
4965 (format t
"~4d: term = ~A~%" n term
))))
4967 ;; Compute Jacobi am for real or complex values of U and M. The args
4968 ;; must be floats or bigfloat::bigfloats. TOL is the tolerance used
4969 ;; by the AGM algorithm. It is ignored if the AGM algorithm is not
4971 (defun bf-jacobi-am (u m tol
)
4972 (cond ((and (realp u
) (realp m
) (<= (abs m
) 1))
4973 ;; The case of real u and m with |m| <= 1. We can use AGM to
4974 ;; compute the result.
4975 (jacobi-am-agm (to u
)
4979 ;; Otherwise, use the formula am(u,m) = asin(jacobi_sn(u,m)).
4980 ;; (See DLMF https://dlmf.nist.gov/22.16.E1). This appears
4981 ;; to be what functions.wolfram.com is using in this case.
4982 (asin (sn (to u
) (to m
))))))
4984 (in-package :maxima
)
4985 (def-simplifier jacobi_am
(u m
)
4995 ;; am(u,1) = 2*atan(exp(u))-%pi/2
4996 (sub (mul 2 (ftake '%atan
(ftake '%exp u
)))
4998 ((float-numerical-eval-p u m
)
4999 ;; For |m| <= 1, we want to use AGM. But for |m| > 1, we want
5000 ;; to use am(z,m) = asin(jacobi_sn(z,m)).
5001 (to (bigfloat::bf-jacobi-am
($float u
)
5003 double-float-epsilon
)))
5004 ((setf args
(complex-float-numerical-eval-p u m
))
5005 (destructuring-bind (u m
)
5007 (to (bigfloat::bf-jacobi-am
($float u
)
5009 double-float-epsilon
))))
5010 ((bigfloat-numerical-eval-p u m
)
5011 (to (bigfloat::bf-jacobi-am
(bigfloat:to
($bfloat u
))
5012 (bigfloat:to
($bfloat m
))
5013 (expt 2 (- fpprec
)))))
5014 ((setf args
(complex-bigfloat-numerical-eval-p u m
))
5015 (destructuring-bind (u m
)
5017 (to (bigfloat::bf-jacobi-am
(bigfloat:to
($bfloat u
))
5018 (bigfloat:to
($bfloat m
))
5019 (expt 2 (- fpprec
))))))