Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemath
GitHub Repository: sagemath/sagelib
Path: blob/master/sage/functions/special.py
4057 views
1
r"""
2
Special Functions
3
4
AUTHORS:
5
6
- David Joyner (2006-13-06): initial version
7
8
- David Joyner (2006-30-10): bug fixes to pari wrappers of Bessel
9
functions, hypergeometric_U
10
11
- William Stein (2008-02): Impose some sanity checks.
12
13
- David Joyner (2008-04-23): addition of elliptic integrals
14
15
This module provides easy access to many of Maxima and PARI's
16
special functions.
17
18
Maxima's special functions package (which includes spherical
19
harmonic functions, spherical Bessel functions (of the 1st and 2nd
20
kind), and spherical Hankel functions (of the 1st and 2nd kind))
21
was written by Barton Willis of the University of Nebraska at
22
Kearney. It is released under the terms of the General Public
23
License (GPL).
24
25
Support for elliptic functions and integrals was written by Raymond
26
Toy. It is placed under the terms of the General Public License
27
(GPL) that governs the distribution of Maxima.
28
29
The (usual) Bessel functions and Airy functions are part of the
30
standard Maxima package. Some Bessel functions also are implemented
31
in PARI. (Caution: The PARI versions are sometimes different than
32
the Maxima version.) For example, the J-Bessel function
33
`J_\nu (z)` can be computed using either Maxima or PARI,
34
depending on an optional variable you pass to bessel_J.
35
36
Next, we summarize some of the properties of the functions
37
implemented here.
38
39
40
- Bessel functions, first defined by the Swiss mathematician
41
Daniel Bernoulli and named after Friedrich Bessel, are canonical
42
solutions y(x) of Bessel's differential equation:
43
44
45
.. math::
46
47
x^2 \frac{d^2 y}{dx^2} + x \frac{dy}{dx} + (x^2 - \alpha^2)y = 0,
48
49
50
for an arbitrary real number `\alpha` (the order).
51
52
- Another important formulation of the two linearly independent
53
solutions to Bessel's equation are the Hankel functions
54
`H_\alpha^{(1)}(x)` and `H_\alpha^{(2)}(x)`,
55
defined by:
56
57
58
.. math::
59
60
H_\alpha^{(1)}(x) = J_\alpha(x) + i Y_\alpha(x)
61
62
63
64
.. math::
65
66
H_\alpha^{(2)}(x) = J_\alpha(x) - i Y_\alpha(x)
67
68
69
where `i` is the imaginary unit (and `J_*` and
70
`Y_*` are the usual J- and Y-Bessel functions). These
71
linear combinations are also known as Bessel functions of the third
72
kind; they are two linearly independent solutions of Bessel's
73
differential equation. They are named for Hermann Hankel.
74
75
- Airy function The function `Ai(x)` and the related
76
function `Bi(x)`, which is also called an Airy function,
77
are solutions to the differential equation
78
79
80
.. math::
81
82
y'' - xy = 0,
83
84
known as the Airy equation. They belong to the class of 'Bessel functions of
85
fractional order'. The initial conditions
86
`Ai(0) = (\Gamma(2/3)3^{2/3})^{-1}`,
87
`Ai'(0) = -(\Gamma(1/3)3^{1/3})^{-1}` define
88
`Ai(x)`. The initial conditions
89
`Bi(0) = 3^{1/2}Ai(0)`, `Bi'(0) = -3^{1/2}Ai'(0)`
90
define `Bi(x)`.
91
92
They are named after the British astronomer George Biddell Airy.
93
94
- Spherical harmonics: Laplace's equation in spherical coordinates
95
is:
96
97
.. math::
98
99
{\frac{1}{r^2}}{\frac{\partial}{\partial r}} \left(r^2 {\frac{\partial f}{\partial r}}\right) + {\frac{1}{r^2}\sin\theta}{\frac{\partial}{\partial \theta}} \left(\sin\theta {\frac{\partial f}{\partial \theta}}\right) + {\frac{1}{r^2\sin^2\theta}}{\frac{\partial^2 f}{\partial \varphi^2}} = 0.
100
101
102
Note that the spherical coordinates `\theta` and
103
`\varphi` are defined here as follows: `\theta` is
104
the colatitude or polar angle, ranging from
105
`0\leq\theta\leq\pi` and `\varphi` the azimuth or
106
longitude, ranging from `0\leq\varphi<2\pi`.
107
108
The general solution which remains finite towards infinity is a
109
linear combination of functions of the form
110
111
.. math::
112
113
r^{-1-\ell} \cos (m \varphi) P_\ell^m (\cos{\theta} )
114
115
116
and
117
118
.. math::
119
120
r^{-1-\ell} \sin (m \varphi) P_\ell^m (\cos{\theta} )
121
122
123
where `P_\ell^m` are the associated Legendre polynomials,
124
and with integer parameters `\ell \ge 0` and `m`
125
from `0` to `\ell`. Put in another way, the
126
solutions with integer parameters `\ell \ge 0` and
127
`- \ell\leq m\leq \ell`, can be written as linear
128
combinations of:
129
130
.. math::
131
132
U_{\ell,m}(r,\theta , \varphi ) = r^{-1-\ell} Y_\ell^m( \theta , \varphi )
133
134
135
where the functions `Y` are the spherical harmonic
136
functions with parameters `\ell`, `m`, which can be
137
written as:
138
139
.. math::
140
141
Y_\ell^m( \theta , \varphi ) = \sqrt{{\frac{(2\ell+1)}{4\pi}}{\frac{(\ell-m)!}{(\ell+m)!}}} \cdot e^{i m \varphi } \cdot P_\ell^m ( \cos{\theta} ) .
142
143
144
145
The spherical harmonics obey the normalisation condition
146
147
148
.. math::
149
150
\int_{\theta=0}^\pi\int_{\varphi=0}^{2\pi} Y_\ell^mY_{\ell'}^{m'*}\,d\Omega =\delta_{\ell\ell'}\delta_{mm'}\quad\quad d\Omega =\sin\theta\,d\varphi\,d\theta .
151
152
153
154
- When solving for separable solutions of Laplace's equation in
155
spherical coordinates, the radial equation has the form:
156
157
.. math::
158
159
x^2 \frac{d^2 y}{dx^2} + 2x \frac{dy}{dx} + [x^2 - n(n+1)]y = 0.
160
161
162
The spherical Bessel functions `j_n` and `y_n`,
163
are two linearly independent solutions to this equation. They are
164
related to the ordinary Bessel functions `J_n` and
165
`Y_n` by:
166
167
.. math::
168
169
j_n(x) = \sqrt{\frac{\pi}{2x}} J_{n+1/2}(x),
170
171
172
173
.. math::
174
175
y_n(x) = \sqrt{\frac{\pi}{2x}} Y_{n+1/2}(x) = (-1)^{n+1} \sqrt{\frac{\pi}{2x}} J_{-n-1/2}(x).
176
177
178
179
- For `x>0`, the confluent hypergeometric function
180
`y = U(a,b,x)` is defined to be the solution to Kummer's
181
differential equation
182
183
184
.. math::
185
186
xy'' + (b-x)y' - ay = 0,
187
188
which satisfies `U(a,b,x) \sim x^{-a}`, as
189
`x\rightarrow \infty`. (There is a linearly independent
190
solution, called Kummer's function `M(a,b,x)`, which is not
191
implemented.)
192
193
- Jacobi elliptic functions can be thought of as generalizations
194
of both ordinary and hyperbolic trig functions. There are twelve
195
Jacobian elliptic functions. Each of the twelve corresponds to an
196
arrow drawn from one corner of a rectangle to another.
197
198
::
199
200
n ------------------- d
201
| |
202
| |
203
| |
204
s ------------------- c
205
206
Each of the corners of the rectangle are labeled, by convention, s,
207
c, d and n. The rectangle is understood to be lying on the complex
208
plane, so that s is at the origin, c is on the real axis, and n is
209
on the imaginary axis. The twelve Jacobian elliptic functions are
210
then pq(x), where p and q are one of the letters s,c,d,n.
211
212
The Jacobian elliptic functions are then the unique
213
doubly-periodic, meromorphic functions satisfying the following
214
three properties:
215
216
217
#. There is a simple zero at the corner p, and a simple pole at the
218
corner q.
219
220
#. The step from p to q is equal to half the period of the function
221
pq(x); that is, the function pq(x) is periodic in the direction pq,
222
with the period being twice the distance from p to q. Also, pq(x)
223
is also periodic in the other two directions as well, with a period
224
such that the distance from p to one of the other corners is a
225
quarter period.
226
227
#. If the function pq(x) is expanded in terms of x at one of the
228
corners, the leading term in the expansion has a coefficient of 1.
229
In other words, the leading term of the expansion of pq(x) at the
230
corner p is x; the leading term of the expansion at the corner q is
231
1/x, and the leading term of an expansion at the other two corners
232
is 1.
233
234
235
We can write
236
237
.. math::
238
239
pq(x)=\frac{pr(x)}{qr(x)}
240
241
242
where `p`, `q`, and `r` are any of the
243
letters `s`, `c`, `d`, `n`, with
244
the understanding that `ss=cc=dd=nn=1`.
245
246
Let
247
248
.. math::
249
250
u=\int_0^\phi \frac{d\theta} {\sqrt {1-m \sin^2 \theta}}
251
252
253
Then the *Jacobi elliptic function* `sn(u)` is given by
254
255
.. math::
256
257
{sn}\; u = \sin \phi
258
259
and `cn(u)` is given by
260
261
.. math::
262
263
{cn}\; u = \cos \phi
264
265
and
266
267
.. math::
268
269
{dn}\; u = \sqrt {1-m\sin^2 \phi}.
270
271
To emphasize the dependence on `m`, one can write
272
`sn(u,m)` for example (and similarly for `cn` and
273
`dn`). This is the notation used below.
274
275
For a given `k` with `0 < k < 1` they therefore are
276
solutions to the following nonlinear ordinary differential
277
equations:
278
279
280
- `\mathrm{sn}\,(x;k)` solves the differential equations
281
282
.. math::
283
284
\frac{\mathrm{d}^2 y}{\mathrm{d}x^2} + (1+k^2) y - 2 k^2 y^3 = 0,
285
286
and
287
288
.. math::
289
290
\left(\frac{\mathrm{d} y}{\mathrm{d}x}\right)^2 = (1-y^2) (1-k^2 y^2).
291
292
- `\mathrm{cn}\,(x;k)` solves the differential equations
293
294
295
.. math::
296
297
\frac{\mathrm{d}^2 y}{\mathrm{d}x^2} + (1-2k^2) y + 2 k^2 y^3 = 0,
298
299
and `\left(\frac{\mathrm{d} y}{\mathrm{d}x}\right)^2 = (1-y^2) (1-k^2 + k^2 y^2)`.
300
301
- `\mathrm{dn}\,(x;k)` solves the differential equations
302
303
.. math::
304
305
\frac{\mathrm{d}^2 y}{\mathrm{d}x^2} - (2 - k^2) y + 2 y^3 = 0,
306
307
and `\left(\frac{\mathrm{d} y}{\mathrm{d}x}\right)^2= y^2 (1 - k^2 - y^2)`.
308
309
If `K(m)` denotes the complete elliptic integral of the
310
first kind (denoted ``elliptic_kc``), the elliptic functions
311
`sn (x,m)` and `cn (x,m)` have real periods
312
`4K(m)`, whereas `dn (x,m)` has a period
313
`2K(m)`. The limit `m\rightarrow 0` gives
314
`K(0) = \pi/2` and trigonometric functions:
315
`sn(x, 0) = \sin x`, `cn(x, 0) = \cos x`,
316
`dn(x, 0) = 1`. The limit `m \rightarrow 1` gives
317
`K(1) \rightarrow \infty` and hyperbolic functions:
318
`sn(x, 1) = \tanh x`,
319
`cn(x, 1) = \mbox{\rm sech} x`,
320
`dn(x, 1) = \mbox{\rm sech} x`.
321
322
- The incomplete elliptic integrals (of the first kind, etc.) are:
323
324
.. math::
325
326
\begin{array}{c} \displaystyle\int_0^\phi \frac{1}{\sqrt{1 - m\sin(x)^2}}\, dx,\\ \displaystyle\int_0^\phi \sqrt{1 - m\sin(x)^2}\, dx,\\ \displaystyle\int_0^\phi \frac{\sqrt{1-mt^2}}{\sqrt(1 - t^2)}\, dx,\\ \displaystyle\int_0^\phi \frac{1}{\sqrt{1 - m\sin(x)^2\sqrt{1 - n\sin(x)^2}}}\, dx, \end{array}
327
328
and the complete ones are obtained by taking `\phi =\pi/2`.
329
330
331
REFERENCES:
332
333
- Abramowitz and Stegun: Handbook of Mathematical Functions,
334
http://www.math.sfu.ca/~cbm/aands/
335
336
- http://en.wikipedia.org/wiki/Bessel_function
337
338
- http://en.wikipedia.org/wiki/Airy_function
339
340
- http://en.wikipedia.org/wiki/Spherical_harmonics
341
342
- http://en.wikipedia.org/wiki/Helmholtz_equation
343
344
- http://en.wikipedia.org/wiki/Jacobi's_elliptic_functions
345
346
- A. Khare, U. Sukhatme, 'Cyclic Identities Involving
347
Jacobi Elliptic Functions', Math ArXiv, math-ph/0201004
348
349
- Online Encyclopedia of Special Function
350
http://algo.inria.fr/esf/index.html
351
352
TODO: Resolve weird bug in commented out code in hypergeometric_U
353
below.
354
355
AUTHORS:
356
357
- David Joyner and William Stein
358
359
Added 16-02-2008 (wdj): optional calls to scipy and replace all
360
'#random' by '...' (both at the request of William Stein)
361
362
.. warning::
363
364
SciPy's versions are poorly documented and seem less
365
accurate than the Maxima and PARI versions.
366
"""
367
368
#*****************************************************************************
369
# Copyright (C) 2006 William Stein <[email protected]>
370
# 2006 David Joyner <[email protected]>
371
#
372
# Distributed under the terms of the GNU General Public License (GPL)
373
#
374
# This code is distributed in the hope that it will be useful,
375
# but WITHOUT ANY WARRANTY; without even the implied warranty of
376
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
377
# General Public License for more details.
378
#
379
# The full text of the GPL is available at:
380
#
381
# http://www.gnu.org/licenses/
382
#*****************************************************************************
383
384
from sage.plot.plot import plot
385
from sage.rings.real_mpfr import RealField
386
from sage.rings.complex_field import ComplexField
387
from sage.misc.sage_eval import sage_eval
388
from sage.rings.all import ZZ, RR, RDF
389
from sage.functions.other import real, imag, log_gamma
390
from sage.symbolic.function import BuiltinFunction
391
from sage.calculus.calculus import maxima
392
393
_done = False
394
def _init():
395
"""
396
Internal function which checks if Maxima has loaded the
397
"orthopoly" package. All functions using this in this
398
file should call this function first.
399
400
TEST:
401
402
The global starts ``False``::
403
404
sage: sage.functions.special._done
405
False
406
407
Then after using one of these functions, it changes::
408
409
sage: from sage.functions.special import airy_ai
410
sage: airy_ai(1.0)
411
0.135292416313
412
sage: sage.functions.special._done
413
True
414
"""
415
global _done
416
if _done:
417
return
418
maxima.eval('load("orthopoly");')
419
maxima.eval('orthopoly_returns_intervals:false;')
420
_done = True
421
422
def meval(x):
423
"""
424
Returns ``x`` evaluated in Maxima, then returned to Sage.
425
This is used to evaluate several of these special functions.
426
427
TEST::
428
429
sage: from sage.functions.special import airy_ai
430
sage: airy_bi(1.0)
431
1.20742359495
432
"""
433
return maxima(x).sage()
434
435
436
class MaximaFunction(BuiltinFunction):
437
"""
438
EXAMPLES::
439
440
sage: from sage.functions.special import MaximaFunction
441
sage: f = MaximaFunction("jacobi_sn")
442
sage: f(1,1)
443
tanh(1)
444
sage: f(1/2,1/2).n()
445
0.470750473655657
446
"""
447
def __init__(self, name, nargs=2, conversions={}):
448
"""
449
EXAMPLES::
450
451
sage: from sage.functions.special import MaximaFunction
452
sage: f = MaximaFunction("jacobi_sn")
453
sage: f(1,1)
454
tanh(1)
455
sage: f(1/2,1/2).n()
456
0.470750473655657
457
"""
458
c = dict(maxima=name)
459
c.update(conversions)
460
BuiltinFunction.__init__(self, name=name, nargs=nargs,
461
conversions=c)
462
463
def _maxima_init_evaled_(self, *args):
464
"""
465
Returns a string which represents this function evaluated at
466
*args* in Maxima.
467
468
EXAMPLES::
469
470
sage: from sage.functions.special import MaximaFunction
471
sage: f = MaximaFunction("jacobi_sn")
472
sage: f._maxima_init_evaled_(1/2, 1/2)
473
'jacobi_sn(1/2, 1/2)'
474
"""
475
args_maxima = []
476
for a in args:
477
if isinstance(a, str):
478
args_maxima.append(a)
479
elif hasattr(a, '_maxima_init_'):
480
args_maxima.append(a._maxima_init_())
481
else:
482
args_maxima.append(str(a))
483
return "%s(%s)"%(self.name(), ', '.join(args_maxima))
484
485
def _evalf_(self, *args, **kwds):
486
"""
487
Returns a numerical approximation of this function using
488
Maxima. Currently, this is limited to 53 bits of precision.
489
490
EXAMPLES::
491
492
sage: from sage.functions.special import MaximaFunction
493
sage: f = MaximaFunction("jacobi_sn")
494
sage: f(1/2,1/2)
495
jacobi_sn(1/2, 1/2)
496
sage: f(1/2,1/2).n()
497
0.470750473655657
498
499
TESTS::
500
501
sage: f(1/2,1/2).n(150)
502
Traceback (most recent call last):
503
...
504
NotImplementedError: jacobi_sn not implemented for precision > 53
505
"""
506
parent = kwds['parent']
507
if hasattr(parent, 'prec') and parent.prec() > 53:
508
raise NotImplementedError, "%s not implemented for precision > 53"%self.name()
509
_init()
510
return parent(maxima("%s, numer"%self._maxima_init_evaled_(*args)))
511
512
def _eval_(self, *args):
513
"""
514
Returns a string which represents this function evaluated at
515
*args* in Maxima.
516
517
EXAMPLES::
518
519
sage: from sage.functions.special import MaximaFunction
520
sage: f = MaximaFunction("jacobi_sn")
521
sage: f(1,1)
522
tanh(1)
523
524
sage: f._eval_(1,1)
525
tanh(1)
526
527
Here arccoth doesn't have 1 in its domain, so we just hold the expression:
528
529
sage: elliptic_e(arccoth(1), x^2*e)
530
elliptic_e(arccoth(1), x^2*e)
531
"""
532
_init()
533
try:
534
s = maxima(self._maxima_init_evaled_(*args))
535
except TypeError:
536
return None
537
if self.name() in repr(s):
538
return None
539
else:
540
return s.sage()
541
542
from sage.misc.cachefunc import cached_function
543
544
@cached_function
545
def maxima_function(name):
546
"""
547
Returns a function which is evaluated both symbolically and
548
numerically via Maxima. In particular, it returns an instance
549
of :class:`MaximaFunction`.
550
551
.. note::
552
553
This function is cached so that duplicate copies of the same
554
function are not created.
555
556
EXAMPLES::
557
558
sage: n(bessel_J(3,10,"maxima"))
559
0.0583793793051...
560
sage: spherical_hankel2(2,i)
561
-e
562
"""
563
# The superclass of MaximaFunction, BuiltinFunction, assumes that there
564
# will be only one symbolic function with the same name and class.
565
# We create a new class for each Maxima function wrapped.
566
class NewMaximaFunction(MaximaFunction):
567
def __init__(self):
568
"""
569
Constructs an object that wraps a Maxima function.
570
571
TESTS::
572
573
sage: n(bessel_J(3,10,"maxima"))
574
0.0583793793051...
575
sage: spherical_hankel2(2,x)
576
(-I*x^2 - 3*x + 3*I)*e^(-I*x)/x^3
577
"""
578
MaximaFunction.__init__(self, name)
579
580
return NewMaximaFunction()
581
582
583
def airy_ai(x):
584
r"""
585
The function `Ai(x)` and the related function `Bi(x)`,
586
which is also called an *Airy function*, are
587
solutions to the differential equation
588
589
.. math::
590
591
y'' - xy = 0,
592
593
known as the *Airy equation*. The initial conditions
594
`Ai(0) = (\Gamma(2/3)3^{2/3})^{-1}`,
595
`Ai'(0) = -(\Gamma(1/3)3^{1/3})^{-1}` define `Ai(x)`.
596
The initial conditions `Bi(0) = 3^{1/2}Ai(0)`,
597
`Bi'(0) = -3^{1/2}Ai'(0)` define `Bi(x)`.
598
599
They are named after the British astronomer George Biddell Airy.
600
They belong to the class of "Bessel functions of fractional order".
601
602
EXAMPLES::
603
604
sage: airy_ai(1.0) # last few digits are random
605
0.135292416312881400
606
sage: airy_bi(1.0) # last few digits are random
607
1.20742359495287099
608
609
REFERENCE:
610
611
- Abramowitz and Stegun: Handbook of Mathematical Functions,
612
http://www.math.sfu.ca/~cbm/aands/
613
614
- http://en.wikipedia.org/wiki/Airy_function
615
"""
616
_init()
617
return RDF(meval("airy_ai(%s)"%RDF(x)))
618
619
def airy_bi(x):
620
r"""
621
The function `Ai(x)` and the related function `Bi(x)`,
622
which is also called an *Airy function*, are
623
solutions to the differential equation
624
625
.. math::
626
627
y'' - xy = 0,
628
629
known as the *Airy equation*. The initial conditions
630
`Ai(0) = (\Gamma(2/3)3^{2/3})^{-1}`,
631
`Ai'(0) = -(\Gamma(1/3)3^{1/3})^{-1}` define `Ai(x)`.
632
The initial conditions `Bi(0) = 3^{1/2}Ai(0)`,
633
`Bi'(0) = -3^{1/2}Ai'(0)` define `Bi(x)`.
634
635
They are named after the British astronomer George Biddell Airy.
636
They belong to the class of "Bessel functions of fractional order".
637
638
EXAMPLES::
639
640
sage: airy_ai(1) # last few digits are random
641
0.135292416312881400
642
sage: airy_bi(1) # last few digits are random
643
1.20742359495287099
644
645
REFERENCE:
646
647
- Abramowitz and Stegun: Handbook of Mathematical Functions,
648
http://www.math.sfu.ca/~cbm/aands/
649
650
- http://en.wikipedia.org/wiki/Airy_function
651
"""
652
_init()
653
return RDF(meval("airy_bi(%s)"%RDF(x)))
654
655
656
def bessel_I(nu,z,algorithm = "pari",prec=53):
657
r"""
658
Implements the "I-Bessel function", or "modified Bessel function,
659
1st kind", with index (or "order") nu and argument z.
660
661
INPUT:
662
663
664
- ``nu`` - a real (or complex, for pari) number
665
666
- ``z`` - a real (positive) algorithm - "pari" or
667
"maxima" or "scipy" prec - real precision (for PARI only)
668
669
670
DEFINITION::
671
672
Maxima:
673
inf
674
==== - nu - 2 k nu + 2 k
675
\ 2 z
676
> -------------------
677
/ k! Gamma(nu + k + 1)
678
====
679
k = 0
680
681
PARI:
682
683
inf
684
==== - 2 k 2 k
685
\ 2 z Gamma(nu + 1)
686
> -----------------------
687
/ k! Gamma(nu + k + 1)
688
====
689
k = 0
690
691
692
693
Sometimes ``bessel_I(nu,z)`` is denoted
694
``I_nu(z)`` in the literature.
695
696
.. warning::
697
698
In Maxima (the manual says) i0 is deprecated but
699
``bessel_i(0,*)`` is broken. (Was fixed in recent CVS patch
700
though.)
701
702
EXAMPLES::
703
704
sage: bessel_I(1,1,"pari",500)
705
0.565159103992485027207696027609863307328899621621092009480294489479255640964371134092664997766814410064677886055526302676857637684917179812041131208121
706
sage: bessel_I(1,1)
707
0.565159103992485
708
sage: bessel_I(2,1.1,"maxima")
709
0.16708949925104...
710
sage: bessel_I(0,1.1,"maxima")
711
1.32616018371265...
712
sage: bessel_I(0,1,"maxima")
713
1.2660658777520...
714
sage: bessel_I(1,1,"scipy")
715
0.565159103992...
716
717
Check whether the return value is real whenever the argument is real (#10251)::
718
719
sage: bessel_I(5, 1.5, algorithm='scipy') in RR
720
True
721
722
"""
723
if algorithm=="pari":
724
from sage.libs.pari.all import pari
725
try:
726
R = RealField(prec)
727
nu = R(nu)
728
z = R(z)
729
except TypeError:
730
C = ComplexField(prec)
731
nu = C(nu)
732
z = C(z)
733
K = C
734
K = z.parent()
735
return K(pari(nu).besseli(z, precision=prec))
736
elif algorithm=="scipy":
737
if prec != 53:
738
raise ValueError, "for the scipy algorithm the precision must be 53"
739
import scipy.special
740
ans = str(scipy.special.iv(float(nu),complex(real(z),imag(z))))
741
ans = ans.replace("(","")
742
ans = ans.replace(")","")
743
ans = ans.replace("j","*I")
744
ans = sage_eval(ans)
745
return real(ans) if z in RR else ans # Return real value when arg is real
746
elif algorithm == "maxima":
747
if prec != 53:
748
raise ValueError, "for the maxima algorithm the precision must be 53"
749
return sage_eval(maxima.eval("bessel_i(%s,%s)"%(float(nu),float(z))))
750
else:
751
raise ValueError, "unknown algorithm '%s'"%algorithm
752
753
def bessel_J(nu,z,algorithm="pari",prec=53):
754
r"""
755
Return value of the "J-Bessel function", or "Bessel function, 1st
756
kind", with index (or "order") nu and argument z.
757
758
::
759
760
Defn:
761
Maxima:
762
inf
763
==== - nu - 2 k nu + 2 k
764
\ (-1)^k 2 z
765
> -------------------------
766
/ k! Gamma(nu + k + 1)
767
====
768
k = 0
769
770
PARI:
771
772
inf
773
==== - 2k 2k
774
\ (-1)^k 2 z Gamma(nu + 1)
775
> ----------------------------
776
/ k! Gamma(nu + k + 1)
777
====
778
k = 0
779
780
781
Sometimes bessel_J(nu,z) is denoted J_nu(z) in the literature.
782
783
.. warning::
784
785
Inaccurate for small values of z.
786
787
EXAMPLES::
788
789
sage: bessel_J(2,1.1)
790
0.136564153956658
791
sage: bessel_J(0,1.1)
792
0.719622018527511
793
sage: bessel_J(0,1)
794
0.765197686557967
795
sage: bessel_J(0,0)
796
1.00000000000000
797
sage: bessel_J(0.1,0.1)
798
0.777264368097005
799
800
We check consistency of PARI and Maxima::
801
802
sage: n(bessel_J(3,10,"maxima"))
803
0.0583793793051...
804
sage: n(bessel_J(3,10,"pari"))
805
0.0583793793051868
806
sage: bessel_J(3,10,"scipy")
807
0.0583793793052...
808
809
Check whether the return value is real whenever the argument is real (#10251)::
810
sage: bessel_J(5, 1.5, algorithm='scipy') in RR
811
True
812
"""
813
814
if algorithm=="pari":
815
from sage.libs.pari.all import pari
816
try:
817
R = RealField(prec)
818
nu = R(nu)
819
z = R(z)
820
except TypeError:
821
C = ComplexField(prec)
822
nu = C(nu)
823
z = C(z)
824
K = C
825
if nu == 0:
826
nu = ZZ(0)
827
K = z.parent()
828
return K(pari(nu).besselj(z, precision=prec))
829
elif algorithm=="scipy":
830
if prec != 53:
831
raise ValueError, "for the scipy algorithm the precision must be 53"
832
import scipy.special
833
ans = str(scipy.special.jv(float(nu),complex(real(z),imag(z))))
834
ans = ans.replace("(","")
835
ans = ans.replace(")","")
836
ans = ans.replace("j","*I")
837
ans = sage_eval(ans)
838
return real(ans) if z in RR else ans
839
elif algorithm == "maxima":
840
if prec != 53:
841
raise ValueError, "for the maxima algorithm the precision must be 53"
842
return maxima_function("bessel_j")(nu, z)
843
else:
844
raise ValueError, "unknown algorithm '%s'"%algorithm
845
846
def bessel_K(nu,z,algorithm="pari",prec=53):
847
r"""
848
Implements the "K-Bessel function", or "modified Bessel function,
849
2nd kind", with index (or "order") nu and argument z. Defn::
850
851
pi*(bessel_I(-nu, z) - bessel_I(nu, z))
852
----------------------------------------
853
2*sin(pi*nu)
854
855
856
if nu is not an integer and by taking a limit otherwise.
857
858
Sometimes bessel_K(nu,z) is denoted K_nu(z) in the literature. In
859
PARI, nu can be complex and z must be real and positive.
860
861
EXAMPLES::
862
863
sage: bessel_K(3,2,"scipy")
864
0.64738539094...
865
sage: bessel_K(3,2)
866
0.64738539094...
867
sage: bessel_K(1,1)
868
0.60190723019...
869
sage: bessel_K(1,1,"pari",10)
870
0.60
871
sage: bessel_K(1,1,"pari",100)
872
0.60190723019723457473754000154
873
874
TESTS::
875
876
sage: bessel_K(2,1.1, algorithm="maxima")
877
Traceback (most recent call last):
878
...
879
NotImplementedError: The K-Bessel function is only implemented for the pari and scipy algorithms
880
881
Check whether the return value is real whenever the argument is real (#10251)::
882
883
sage: bessel_K(5, 1.5, algorithm='scipy') in RR
884
True
885
886
"""
887
if algorithm=="scipy":
888
if prec != 53:
889
raise ValueError, "for the scipy algorithm the precision must be 53"
890
import scipy.special
891
ans = str(scipy.special.kv(float(nu),float(z)))
892
ans = ans.replace("(","")
893
ans = ans.replace(")","")
894
ans = ans.replace("j","*I")
895
ans = sage_eval(ans)
896
return real(ans) if z in RR else ans
897
elif algorithm == 'pari':
898
from sage.libs.pari.all import pari
899
try:
900
R = RealField(prec)
901
nu = R(nu)
902
z = R(z)
903
except TypeError:
904
C = ComplexField(prec)
905
nu = C(nu)
906
z = C(z)
907
K = C
908
K = z.parent()
909
return K(pari(nu).besselk(z, precision=prec))
910
elif algorithm == 'maxima':
911
raise NotImplementedError, "The K-Bessel function is only implemented for the pari and scipy algorithms"
912
else:
913
raise ValueError, "unknown algorithm '%s'"%algorithm
914
915
916
def bessel_Y(nu,z,algorithm="maxima", prec=53):
917
r"""
918
Implements the "Y-Bessel function", or "Bessel function of the 2nd
919
kind", with index (or "order") nu and argument z.
920
921
.. note::
922
923
Currently only prec=53 is supported.
924
925
Defn::
926
927
cos(pi n)*bessel_J(nu, z) - bessel_J(-nu, z)
928
-------------------------------------------------
929
sin(nu*pi)
930
931
if nu is not an integer and by taking a limit otherwise.
932
933
Sometimes bessel_Y(n,z) is denoted Y_n(z) in the literature.
934
935
This is computed using Maxima by default.
936
937
EXAMPLES::
938
939
sage: bessel_Y(2,1.1,"scipy")
940
-1.4314714939...
941
sage: bessel_Y(2,1.1)
942
-1.4314714939590...
943
sage: bessel_Y(3.001,2.1)
944
-1.0299574976424...
945
946
TESTS::
947
948
sage: bessel_Y(2,1.1, algorithm="pari")
949
Traceback (most recent call last):
950
...
951
NotImplementedError: The Y-Bessel function is only implemented for the maxima and scipy algorithms
952
"""
953
if algorithm=="scipy":
954
if prec != 53:
955
raise ValueError, "for the scipy algorithm the precision must be 53"
956
import scipy.special
957
ans = str(scipy.special.yv(float(nu),complex(real(z),imag(z))))
958
ans = ans.replace("(","")
959
ans = ans.replace(")","")
960
ans = ans.replace("j","*I")
961
ans = sage_eval(ans)
962
return real(ans) if z in RR else ans
963
elif algorithm == "maxima":
964
if prec != 53:
965
raise ValueError, "for the maxima algorithm the precision must be 53"
966
return RR(maxima.eval("bessel_y(%s,%s)"%(float(nu),float(z))))
967
elif algorithm == "pari":
968
raise NotImplementedError, "The Y-Bessel function is only implemented for the maxima and scipy algorithms"
969
else:
970
raise ValueError, "unknown algorithm '%s'"%algorithm
971
972
class Bessel():
973
"""
974
A class implementing the I, J, K, and Y Bessel functions.
975
976
EXAMPLES::
977
978
sage: g = Bessel(2); g
979
J_{2}
980
sage: print g
981
J-Bessel function of order 2
982
sage: g.plot(0,10)
983
984
::
985
986
sage: Bessel(2, typ='I')(pi)
987
2.61849485263445
988
sage: Bessel(2, typ='J')(pi)
989
0.485433932631509
990
sage: Bessel(2, typ='K')(pi)
991
0.0510986902537926
992
sage: Bessel(2, typ='Y')(pi)
993
-0.0999007139289404
994
"""
995
def __init__(self, nu, typ = "J", algorithm = None, prec = 53):
996
"""
997
Initializes new instance of the Bessel class.
998
999
INPUT:
1000
1001
- ``typ`` -- (default: J) the type of Bessel function: 'I', 'J', 'K'
1002
or 'Y'.
1003
1004
- ``algorithm`` -- (default: maxima for type Y, pari for other types)
1005
algorithm to use to compute the Bessel function: 'pari', 'maxima' or
1006
'scipy'. Note that type K is not implemented in Maxima and type Y
1007
is not implemented in PARI.
1008
1009
- ``prec`` -- (default: 53) precision in bits of the Bessel function.
1010
Only supported for the PARI algorithm.
1011
1012
EXAMPLES::
1013
1014
sage: g = Bessel(2); g
1015
J_{2}
1016
sage: Bessel(1,'I')
1017
I_{1}
1018
sage: Bessel(6, prec=120)(pi)
1019
0.014545966982505560573660369604001804
1020
sage: Bessel(6, algorithm="pari")(pi)
1021
0.0145459669825056
1022
1023
For the Bessel J-function, Maxima returns a symbolic result. For
1024
types I and Y, we always get a numeric result::
1025
1026
sage: b = Bessel(6, algorithm="maxima")(pi); b
1027
bessel_j(6, pi)
1028
sage: b.n(53)
1029
0.0145459669825056
1030
sage: Bessel(6, typ='I', algorithm="maxima")(pi)
1031
0.0294619840059568
1032
sage: Bessel(6, typ='Y', algorithm="maxima")(pi)
1033
-4.33932818939038
1034
1035
SciPy usually gives less precise results::
1036
1037
sage: Bessel(6, algorithm="scipy")(pi)
1038
0.0145459669825000...
1039
1040
TESTS::
1041
1042
sage: Bessel(1,'Z')
1043
Traceback (most recent call last):
1044
...
1045
ValueError: typ must be one of I, J, K, Y
1046
"""
1047
if not (typ in ['I', 'J', 'K', 'Y']):
1048
raise ValueError, "typ must be one of I, J, K, Y"
1049
1050
# Did the user ask for the default algorithm?
1051
if algorithm is None:
1052
if typ == 'Y':
1053
algorithm = 'maxima'
1054
else:
1055
algorithm = 'pari'
1056
1057
self._system = algorithm
1058
self._order = nu
1059
self._type = typ
1060
prec = int(prec)
1061
if prec < 0:
1062
raise ValueError, "prec must be a positive integer"
1063
self._prec = int(prec)
1064
1065
def __str__(self):
1066
"""
1067
Returns a string representation of this Bessel object.
1068
1069
TEST::
1070
1071
sage: a = Bessel(1,'I')
1072
sage: str(a)
1073
'I-Bessel function of order 1'
1074
"""
1075
return self.type()+"-Bessel function of order "+str(self.order())
1076
1077
def __repr__(self):
1078
"""
1079
Returns a string representation of this Bessel object.
1080
1081
TESTS::
1082
1083
sage: Bessel(1,'I')
1084
I_{1}
1085
"""
1086
return self.type()+"_{"+str(self.order())+"}"
1087
1088
def type(self):
1089
"""
1090
Returns the type of this Bessel object.
1091
1092
TEST::
1093
1094
sage: a = Bessel(3,'K')
1095
sage: a.type()
1096
'K'
1097
"""
1098
return self._type
1099
1100
def prec(self):
1101
"""
1102
Returns the precision (in number of bits) used to represent this
1103
Bessel function.
1104
1105
TESTS::
1106
1107
sage: a = Bessel(3,'K')
1108
sage: a.prec()
1109
53
1110
sage: B = Bessel(20,prec=100); B
1111
J_{20}
1112
sage: B.prec()
1113
100
1114
"""
1115
return self._prec
1116
1117
def order(self):
1118
"""
1119
Returns the order of this Bessel function.
1120
1121
TEST::
1122
1123
sage: a = Bessel(3,'K')
1124
sage: a.order()
1125
3
1126
"""
1127
return self._order
1128
1129
def system(self):
1130
"""
1131
Returns the package used, e.g. Maxima, PARI, or SciPy, to compute with
1132
this Bessel function.
1133
1134
TESTS::
1135
1136
sage: Bessel(20,algorithm='maxima').system()
1137
'maxima'
1138
sage: Bessel(20,prec=100).system()
1139
'pari'
1140
"""
1141
return self._system
1142
1143
def __call__(self,z):
1144
"""
1145
Implements evaluation of all the Bessel functions directly
1146
from the Bessel class. This essentially allows a Bessel object to
1147
behave like a function that can be invoked.
1148
1149
TESTS::
1150
1151
sage: Bessel(3,'K')(5.0)
1152
0.00829176841523093
1153
sage: Bessel(20,algorithm='maxima')(5.0)
1154
2.77033005213e-11
1155
sage: Bessel(20,prec=100)(5.0101010101010101)
1156
2.8809188227195382093062257967e-11
1157
sage: B = Bessel(2,'Y',algorithm='scipy',prec=50)
1158
sage: B(2.0)
1159
Traceback (most recent call last):
1160
...
1161
ValueError: for the scipy algorithm the precision must be 53
1162
"""
1163
nu = self.order()
1164
t = self.type()
1165
s = self.system()
1166
p = self.prec()
1167
if t == "I":
1168
return bessel_I(nu,z,algorithm=s,prec=p)
1169
if t == "J":
1170
return bessel_J(nu,z,algorithm=s,prec=p)
1171
if t == "K":
1172
return bessel_K(nu,z,algorithm=s,prec=p)
1173
if t == "Y":
1174
return bessel_Y(nu,z,algorithm=s,prec=p)
1175
1176
def plot(self,a,b):
1177
"""
1178
Enables easy plotting of all the Bessel functions directly
1179
from the Bessel class.
1180
1181
TESTS::
1182
1183
sage: plot(Bessel(2),3,4)
1184
sage: Bessel(2).plot(3,4)
1185
sage: P = Bessel(2,'I').plot(1,5)
1186
sage: P += Bessel(2,'J').plot(1,5)
1187
sage: P += Bessel(2,'K').plot(1,5)
1188
sage: P += Bessel(2,'Y').plot(1,5)
1189
sage: show(P)
1190
"""
1191
nu = self.order()
1192
s = self.system()
1193
t = self.type()
1194
if t == "I":
1195
f = lambda z: bessel_I(nu,z,s)
1196
P = plot(f,a,b)
1197
if t == "J":
1198
f = lambda z: bessel_J(nu,z,s)
1199
P = plot(f,a,b)
1200
if t == "K":
1201
f = lambda z: bessel_K(nu,z,s)
1202
P = plot(f,a,b)
1203
if t == "Y":
1204
f = lambda z: bessel_Y(nu,z,s)
1205
P = plot(f,a,b)
1206
return P
1207
1208
def hypergeometric_U(alpha,beta,x,algorithm="pari",prec=53):
1209
r"""
1210
Default is a wrap of PARI's hyperu(alpha,beta,x) function.
1211
Optionally, algorithm = "scipy" can be used.
1212
1213
The confluent hypergeometric function `y = U(a,b,x)` is
1214
defined to be the solution to Kummer's differential equation
1215
1216
.. math::
1217
1218
xy'' + (b-x)y' - ay = 0.
1219
1220
This satisfies `U(a,b,x) \sim x^{-a}`, as
1221
`x\rightarrow \infty`, and is sometimes denoted
1222
``x^{-a}2_F_0(a,1+a-b,-1/x)``. This is not the same as Kummer's
1223
`M`-hypergeometric function, denoted sometimes as
1224
``_1F_1(alpha,beta,x)``, though it satisfies the same DE that
1225
`U` does.
1226
1227
.. warning::
1228
1229
In the literature, both are called "Kummer confluent
1230
hypergeometric" functions.
1231
1232
EXAMPLES::
1233
1234
sage: hypergeometric_U(1,1,1,"scipy")
1235
0.596347362323...
1236
sage: hypergeometric_U(1,1,1)
1237
0.59634736232319...
1238
sage: hypergeometric_U(1,1,1,"pari",70)
1239
0.59634736232319407434...
1240
"""
1241
if algorithm=="scipy":
1242
if prec != 53:
1243
raise ValueError, "for the scipy algorithm the precision must be 53"
1244
import scipy.special
1245
ans = str(scipy.special.hyperu(float(alpha),float(beta),float(x)))
1246
ans = ans.replace("(","")
1247
ans = ans.replace(")","")
1248
ans = ans.replace("j","*I")
1249
return sage_eval(ans)
1250
elif algorithm=='pari':
1251
from sage.libs.pari.all import pari
1252
R = RealField(prec)
1253
return R(pari(R(alpha)).hyperu(R(beta), R(x), precision=prec))
1254
else:
1255
raise ValueError, "unknown algorithm '%s'"%algorithm
1256
1257
def spherical_bessel_J(n, var, algorithm="maxima"):
1258
r"""
1259
Returns the spherical Bessel function of the first kind for
1260
integers n >= 1.
1261
1262
Reference: AS 10.1.8 page 437 and AS 10.1.15 page 439.
1263
1264
EXAMPLES::
1265
1266
sage: spherical_bessel_J(2,x)
1267
((3/x^2 - 1)*sin(x) - 3*cos(x)/x)/x
1268
sage: spherical_bessel_J(1, 5.2, algorithm='scipy')
1269
-0.12277149950007...
1270
sage: spherical_bessel_J(1, 3, algorithm='scipy')
1271
0.345677499762355...
1272
"""
1273
if algorithm=="scipy":
1274
from scipy.special.specfun import sphj
1275
return sphj(int(n), float(var))[1][-1]
1276
elif algorithm == 'maxima':
1277
_init()
1278
return meval("spherical_bessel_j(%s,%s)"%(ZZ(n),var))
1279
else:
1280
raise ValueError, "unknown algorithm '%s'"%algorithm
1281
1282
def spherical_bessel_Y(n,var, algorithm="maxima"):
1283
r"""
1284
Returns the spherical Bessel function of the second kind for
1285
integers n -1.
1286
1287
Reference: AS 10.1.9 page 437 and AS 10.1.15 page 439.
1288
1289
EXAMPLES::
1290
1291
sage: x = PolynomialRing(QQ, 'x').gen()
1292
sage: spherical_bessel_Y(2,x)
1293
-((3/x^2 - 1)*cos(x) + 3*sin(x)/x)/x
1294
"""
1295
if algorithm=="scipy":
1296
import scipy.special
1297
ans = str(scipy.special.sph_yn(int(n),float(var)))
1298
ans = ans.replace("(","")
1299
ans = ans.replace(")","")
1300
ans = ans.replace("j","*I")
1301
return sage_eval(ans)
1302
elif algorithm == 'maxima':
1303
_init()
1304
return meval("spherical_bessel_y(%s,%s)"%(ZZ(n),var))
1305
else:
1306
raise ValueError, "unknown algorithm '%s'"%algorithm
1307
1308
def spherical_hankel1(n, var):
1309
r"""
1310
Returns the spherical Hankel function of the first kind for
1311
integers `n > -1`, written as a string. Reference: AS
1312
10.1.36 page 439.
1313
1314
EXAMPLES::
1315
1316
sage: spherical_hankel1(2, x)
1317
(I*x^2 - 3*x - 3*I)*e^(I*x)/x^3
1318
"""
1319
return maxima_function("spherical_hankel1")(ZZ(n), var)
1320
1321
def spherical_hankel2(n,x):
1322
r"""
1323
Returns the spherical Hankel function of the second kind for
1324
integers `n > -1`, written as a string. Reference: AS 10.1.17 page
1325
439.
1326
1327
EXAMPLES::
1328
1329
sage: spherical_hankel2(2, x)
1330
(-I*x^2 - 3*x + 3*I)*e^(-I*x)/x^3
1331
1332
Here I = sqrt(-1).
1333
"""
1334
return maxima_function("spherical_hankel2")(ZZ(n), x)
1335
1336
def spherical_harmonic(m,n,x,y):
1337
r"""
1338
Returns the spherical Harmonic function of the second kind for
1339
integers `n > -1`, `|m|\leq n`. Reference:
1340
Merzbacher 9.64.
1341
1342
EXAMPLES::
1343
1344
sage: x,y = var('x,y')
1345
sage: spherical_harmonic(3,2,x,y)
1346
15/4*sqrt(7/30)*e^(2*I*y)*sin(x)^2*cos(x)/sqrt(pi)
1347
sage: spherical_harmonic(3,2,1,2)
1348
15/4*sqrt(7/30)*e^(4*I)*sin(1)^2*cos(1)/sqrt(pi)
1349
"""
1350
_init()
1351
return meval("spherical_harmonic(%s,%s,%s,%s)"%(ZZ(m),ZZ(n),x,y))
1352
1353
####### elliptic functions and integrals
1354
1355
def elliptic_j(z):
1356
r"""
1357
Returns the elliptic modular `j`-function evaluated at `z`.
1358
1359
INPUT:
1360
1361
- ``z`` (complex) -- a complex number with positive imaginary part.
1362
1363
OUTPUT:
1364
1365
(complex) The value of `j(z)`.
1366
1367
ALGORITHM:
1368
1369
Calls the ``pari`` function ``ellj()``.
1370
1371
AUTHOR:
1372
1373
John Cremona
1374
1375
EXAMPLES::
1376
1377
sage: elliptic_j(CC(i))
1378
1728.00000000000
1379
sage: elliptic_j(sqrt(-2.0))
1380
8000.00000000000
1381
sage: z = ComplexField(100)(1,sqrt(11))/2
1382
sage: elliptic_j(z)
1383
-32768.000...
1384
sage: elliptic_j(z).real().round()
1385
-32768
1386
1387
"""
1388
CC = z.parent()
1389
from sage.rings.complex_field import is_ComplexField
1390
if not is_ComplexField(CC):
1391
CC = ComplexField()
1392
try:
1393
z = CC(z)
1394
except ValueError:
1395
raise ValueError, "elliptic_j only defined for complex arguments."
1396
from sage.libs.all import pari
1397
return CC(pari(z).ellj())
1398
1399
def jacobi(sym,x,m):
1400
r"""
1401
Here sym = "pq", where p,q in c,d,n,s. This returns the value of
1402
the Jacobi function pq(x,m), as described in the documentation for
1403
Sage's "special" module. There are a total of 12 functions
1404
described by this.
1405
1406
EXAMPLES::
1407
1408
sage: jacobi("sn",1,1)
1409
tanh(1)
1410
sage: jacobi("cd",1,1/2)
1411
jacobi_cd(1, 1/2)
1412
sage: RDF(jacobi("cd",1,1/2))
1413
0.724009721659
1414
sage: RDF(jacobi("cn",1,1/2)); RDF(jacobi("dn",1,1/2)); RDF(jacobi("cn",1,1/2)/jacobi("dn",1,1/2))
1415
0.595976567672
1416
0.823161001632
1417
0.724009721659
1418
sage: jsn = jacobi("sn",x,1)
1419
sage: P = plot(jsn,0,1)
1420
1421
To view this, type P.show().
1422
"""
1423
return maxima_function("jacobi_%s"%sym)(x,m)
1424
1425
def inverse_jacobi(sym,x,m):
1426
"""
1427
Here sym = "pq", where p,q in c,d,n,s. This returns the value of
1428
the inverse Jacobi function `pq^{-1}(x,m)`. There are a
1429
total of 12 functions described by this.
1430
1431
EXAMPLES::
1432
1433
sage: jacobi("sn",1/2,1/2)
1434
jacobi_sn(1/2, 1/2)
1435
sage: float(jacobi("sn",1/2,1/2))
1436
0.4707504736556572
1437
sage: float(inverse_jacobi("sn",0.47,1/2))
1438
0.4990982313222196
1439
sage: float(inverse_jacobi("sn",0.4707504,0.5))
1440
0.4999999114665548
1441
sage: P = plot(inverse_jacobi('sn', x, 0.5), 0, 1, plot_points=20)
1442
1443
Now to view this, just type show(P).
1444
"""
1445
return maxima_function("inverse_jacobi_%s"%sym)(x,m)
1446
1447
#### elliptic integrals
1448
1449
class EllipticE(MaximaFunction):
1450
r"""
1451
This returns the value of the "incomplete elliptic integral of the
1452
second kind,"
1453
1454
.. math::
1455
1456
\int_0^\phi \sqrt{1 - m\sin(x)^2}\, dx,
1457
1458
i.e., ``integrate(sqrt(1 - m*sin(x)^2), x, 0, phi)``. Taking `\phi
1459
= \pi/2` gives ``elliptic_ec``.
1460
1461
EXAMPLES::
1462
1463
sage: z = var("z")
1464
sage: # this is still wrong: must be abs(sin(z)) + 2*round(z/pi)
1465
sage: elliptic_e(z, 1)
1466
sin(z) + 2*round(z/pi)
1467
sage: elliptic_e(z, 0)
1468
z
1469
sage: elliptic_e(0.5, 0.1)
1470
0.498011394499
1471
1472
sage: loads(dumps(elliptic_e))
1473
elliptic_e
1474
"""
1475
def __init__(self):
1476
"""
1477
EXAMPLES::
1478
1479
sage: elliptic_e(0.5, 0.1)
1480
0.498011394499
1481
"""
1482
MaximaFunction.__init__(self, "elliptic_e")
1483
1484
elliptic_e = EllipticE()
1485
1486
class EllipticEC(MaximaFunction):
1487
"""
1488
This returns the value of the "complete elliptic integral of the
1489
second kind,"
1490
1491
.. math::
1492
1493
\int_0^{\pi/2} \sqrt{1 - m\sin(x)^2}\, dx.
1494
1495
EXAMPLES::
1496
1497
sage: elliptic_ec(0.1)
1498
1.5307576369
1499
sage: elliptic_ec(x).diff()
1500
1/2*(elliptic_ec(x) - elliptic_kc(x))/x
1501
1502
sage: loads(dumps(elliptic_ec))
1503
elliptic_ec
1504
"""
1505
def __init__(self):
1506
"""
1507
EXAMPLES::
1508
1509
sage: elliptic_ec(0.1)
1510
1.5307576369
1511
"""
1512
MaximaFunction.__init__(self, "elliptic_ec", nargs=1)
1513
1514
def _derivative_(self, *args, **kwds):
1515
"""
1516
EXAMPLES::
1517
1518
sage: elliptic_ec(x).diff()
1519
1/2*(elliptic_ec(x) - elliptic_kc(x))/x
1520
"""
1521
diff_param = kwds['diff_param']
1522
assert diff_param == 0
1523
x = args[diff_param]
1524
return (elliptic_ec(x) - elliptic_kc(x))/(2*x)
1525
1526
elliptic_ec = EllipticEC()
1527
1528
class EllipticEU(MaximaFunction):
1529
r"""
1530
This returns the value of the "incomplete elliptic integral of the
1531
second kind,"
1532
1533
.. math::
1534
1535
\int_0^u \mathrm{dn}(x,m)^2\, dx = \int_0^\tau
1536
{\sqrt{1-m x^2}\over\sqrt{1-x^2}}\, dx.
1537
1538
where `\tau = \mathrm{sn}(u, m)`.
1539
1540
EXAMPLES::
1541
1542
sage: elliptic_eu (0.5, 0.1)
1543
0.496054551287
1544
"""
1545
def __init__(self):
1546
r"""
1547
EXAMPLES::
1548
1549
sage: elliptic_eu (0.5, 0.1)
1550
0.496054551287
1551
"""
1552
MaximaFunction.__init__(self, "elliptic_eu")
1553
1554
elliptic_eu = EllipticEU()
1555
1556
class EllipticF(MaximaFunction):
1557
r"""
1558
This returns the value of the "incomplete elliptic integral of the
1559
first kind,"
1560
1561
.. math::
1562
1563
\int_0^\phi \frac{dx}{\sqrt{1 - m\sin(x)^2}},
1564
1565
i.e., ``integrate(1/sqrt(1 - m*sin(x)^2), x, 0, phi)``. Taking
1566
`\phi = \pi/2` gives ``elliptic_kc``.
1567
1568
EXAMPLES::
1569
1570
sage: z = var("z")
1571
sage: elliptic_f (z, 0)
1572
z
1573
sage: elliptic_f (z, 1)
1574
log(tan(1/4*pi + 1/2*z))
1575
sage: elliptic_f (0.2, 0.1)
1576
0.200132506748
1577
"""
1578
def __init__(self):
1579
r"""
1580
EXAMPLES::
1581
1582
sage: elliptic_f (0.2, 0.1)
1583
0.200132506748
1584
"""
1585
MaximaFunction.__init__(self, "elliptic_f")
1586
1587
elliptic_f = EllipticF()
1588
1589
class EllipticKC(MaximaFunction):
1590
r"""
1591
This returns the value of the "complete elliptic integral of the
1592
first kind,"
1593
1594
.. math::
1595
1596
\int_0^{\pi/2} \frac{dx}{\sqrt{1 - m\sin(x)^2}}.
1597
1598
EXAMPLES::
1599
1600
sage: elliptic_kc(0.5)
1601
1.8540746773
1602
sage: elliptic_f(RR(pi/2), 0.5)
1603
1.8540746773
1604
"""
1605
def __init__(self):
1606
r"""
1607
EXAMPLES::
1608
1609
sage: elliptic_kc(0.5)
1610
1.8540746773
1611
sage: elliptic_f(RR(pi/2), 0.5)
1612
1.8540746773
1613
"""
1614
MaximaFunction.__init__(self, "elliptic_kc", nargs=1)
1615
1616
elliptic_kc = EllipticKC()
1617
1618
class EllipticPi(MaximaFunction):
1619
r"""
1620
This returns the value of the "incomplete elliptic integral of the
1621
third kind,"
1622
1623
.. math::
1624
1625
\text{elliptic\_pi}(n, t, m) = \int_0^t \frac{dx}{(1 - n \sin(x)^2)
1626
\sqrt{1 - m \sin(x)^2}}.
1627
1628
INPUT:
1629
1630
- ``n`` -- a real number, called the "characteristic"
1631
1632
- ``t`` -- a real number, called the "amplitude"
1633
1634
- ``m`` -- a real number, called the "parameter"
1635
1636
EXAMPLES::
1637
1638
sage: N(elliptic_pi(1, pi/4, 1))
1639
1.14779357469632
1640
1641
Compare the value computed by Maxima to the definition as a definite integral
1642
(using GSL)::
1643
1644
sage: elliptic_pi(0.1, 0.2, 0.3)
1645
0.200665068221
1646
sage: numerical_integral(1/(1-0.1*sin(x)^2)/sqrt(1-0.3*sin(x)^2), 0.0, 0.2)
1647
(0.2006650682209791, 2.227829789769088e-15)
1648
1649
ALGORITHM:
1650
1651
Numerical evaluation and symbolic manipulation are provided by `Maxima`_.
1652
1653
REFERENCES:
1654
1655
- Abramowitz and Stegun: Handbook of Mathematical Functions, section 17.7
1656
http://www.math.sfu.ca/~cbm/aands/
1657
- Elliptic Functions in `Maxima`_
1658
1659
.. _`Maxima`: http://maxima.sourceforge.net/docs/manual/en/maxima_16.html#SEC91
1660
"""
1661
def __init__(self):
1662
r"""
1663
EXAMPLES::
1664
1665
sage: elliptic_pi(0.1, 0.2, 0.3)
1666
0.200665068221
1667
"""
1668
MaximaFunction.__init__(self, "elliptic_pi", nargs=3)
1669
1670
elliptic_pi = EllipticPi()
1671
1672
1673
def lngamma(t):
1674
r"""
1675
This method is deprecated, please use
1676
:meth:`~sage.functions.other.log_gamma` instead.
1677
1678
See the :meth:`~sage.functions.other.log_gamma` function for '
1679
documentation and examples.
1680
1681
EXAMPLES::
1682
1683
sage: lngamma(RR(6))
1684
doctest:...: DeprecationWarning: The method lngamma() is deprecated. Use log_gamma() instead.
1685
4.78749174278205
1686
"""
1687
from sage.misc.misc import deprecation
1688
deprecation("The method lngamma() is deprecated. Use log_gamma() instead.")
1689
return log_gamma(t)
1690
1691
def exp_int(t):
1692
r"""
1693
The exponential integral `\int_t^\infty e^{-x}/x dx` (t
1694
belongs to RR). This function is deprecated - please use
1695
``Ei`` or ``exponential_integral_1`` as needed instead.
1696
1697
EXAMPLES::
1698
1699
sage: exp_int(6)
1700
doctest:...: DeprecationWarning: The method exp_int() is deprecated. Use -Ei(-x) or exponential_integral_1(x) as needed instead.
1701
0.000360082452162659
1702
"""
1703
from sage.misc.misc import deprecation
1704
deprecation("The method exp_int() is deprecated. Use -Ei(-x) or exponential_integral_1(x) as needed instead.")
1705
try:
1706
return t.eint1()
1707
except AttributeError:
1708
from sage.libs.pari.all import pari
1709
try:
1710
return pari(t).eint1()
1711
except:
1712
raise NotImplementedError
1713
1714
def error_fcn(t):
1715
r"""
1716
The complementary error function
1717
`\frac{2}{\sqrt{\pi}}\int_t^\infty e^{-x^2} dx` (t belongs
1718
to RR). This function is currently always
1719
evaluated immediately.
1720
1721
EXAMPLES::
1722
1723
sage: error_fcn(6)
1724
2.15197367124989e-17
1725
sage: error_fcn(RealField(100)(1/2))
1726
0.47950012218695346231725334611
1727
1728
Note this is literally equal to `1 - erf(t)`::
1729
1730
sage: 1 - error_fcn(0.5)
1731
0.520499877813047
1732
sage: erf(0.5)
1733
0.520499877813047
1734
"""
1735
try:
1736
return t.erfc()
1737
except AttributeError:
1738
from sage.rings.real_mpfr import RR
1739
try:
1740
return RR(t).erfc()
1741
except:
1742
raise NotImplementedError
1743
1744
1745
1746
1747