This repository has been archived by the owner on Jan 30, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
exp_integral.py
1561 lines (1151 loc) · 44.5 KB
/
exp_integral.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
r"""
Exponential Integrals
AUTHORS:
- Benjamin Jones (2011-06-12)
This module provides easy access to many exponential integral
special functions. It utilizes Maxima's `special functions package`_ and
the `mpmath library`_.
REFERENCES:
- [AS]_ Abramowitz and Stegun: *Handbook of Mathematical Functions*
- Wikipedia Entry: http://en.wikipedia.org/wiki/Exponential_integral
- Online Encyclopedia of Special Function: http://algo.inria.fr/esf/index.html
- NIST Digital Library of Mathematical Functions: http://dlmf.nist.gov/
- Maxima `special functions package`_
- `mpmath library`_
.. [AS] 'Handbook of Mathematical Functions', Milton Abramowitz and Irene
A. Stegun, National Bureau of Standards Applied Mathematics Series, 55.
See also http://www.math.sfu.ca/~cbm/aands/.
.. _`special functions package`: http://maxima.sourceforge.net/docs/manual/en/maxima_15.html
.. _`mpmath library`: http://code.google.com/p/mpmath/
AUTHORS:
- Benjamin Jones
Implementations of the classes ``Function_exp_integral_*``.
- David Joyner and William Stein
Authors of the code which was moved from special.py and trans.py.
Implementation of :meth:`exp_int` (from sage/functions/special.py).
Implementation of :meth:`exponential_integral_1` (from
sage/functions/transcendental.py).
"""
#*****************************************************************************
# Copyright (C) 2011 Benjamin Jones <benjaminfjones@gmail.com>
#
# Distributed under the terms of the GNU General Public License (GPL)
#
# This code is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# The full text of the GPL is available at:
#
# http://www.gnu.org/licenses/
#*****************************************************************************
import sage.interfaces.all
from sage.misc.sage_eval import sage_eval
from sage.symbolic.function import BuiltinFunction, is_inexact
from sage.calculus.calculus import maxima
from sage.symbolic.expression import Expression
from sage.structure.parent import Parent
from sage.structure.coerce import parent
from sage.libs.mpmath import utils as mpmath_utils
mpmath_utils_call = mpmath_utils.call # eliminate some overhead in _evalf_
from sage.rings.rational_field import RationalField
from sage.rings.real_mpfr import RealField
from sage.rings.complex_field import ComplexField
from sage.rings.all import ZZ, QQ, RR, RDF
from sage.functions.log import exp, log
from sage.functions.trig import sin, cos
from sage.functions.hyperbolic import sinh, cosh
import math
class Function_exp_integral_e(BuiltinFunction):
r"""
The generalized complex exponential integral `E_n(z)` defined by
.. math::
\operatorname{E_n}(z) = \int_1^{\infty} \frac{e^{-z t}}{t^n} \; dt
for complex numbers `n` and `z`, see [AS]_ 5.1.4.
The special case where `n = 1` is denoted in Sage by
``exp_integral_e1``.
EXAMPLES:
Numerical evaluation is handled using mpmath::
sage: N(exp_integral_e(1,1))
0.219383934395520
sage: exp_integral_e(1, RealField(100)(1))
0.21938393439552027367716377546
We can compare this to PARI's evaluation of
:meth:`exponential_integral_1`::
sage: N(exponential_integral_1(1))
0.219383934395520
We can verify one case of [AS]_ 5.1.45, i.e.
`E_n(z) = z^{n-1}\Gamma(1-n,z)`::
sage: N(exp_integral_e(2, 3+I))
0.00354575823814662 - 0.00973200528288687*I
sage: N((3+I)*gamma(-1, 3+I))
0.00354575823814662 - 0.00973200528288687*I
Maxima returns the following improper integral as a multiple of
``exp_integral_e(1,1)``::
sage: uu = integral(e^(-x)*log(x+1),x,0,oo)
sage: uu
e*exp_integral_e(1, 1)
sage: uu.n(digits=30)
0.596347362323194074341078499369
Symbolic derivatives and integrals are handled by Sage and Maxima::
sage: x = var('x')
sage: f = exp_integral_e(2,x)
sage: f.diff(x)
-exp_integral_e(1, x)
sage: f.integrate(x)
-exp_integral_e(3, x)
sage: f = exp_integral_e(-1,x)
sage: f.integrate(x)
Ei(-x) - gamma(-1, x)
Some special values of ``exp_integral_e`` can be simplified.
[AS]_ 5.1.23::
sage: exp_integral_e(0,x)
e^(-x)/x
[AS]_ 5.1.24::
sage: exp_integral_e(6,0)
1/5
sage: nn = var('nn')
sage: assume(nn > 1)
sage: f = exp_integral_e(nn,0)
sage: f.simplify()
1/(nn - 1)
ALGORITHM:
Numerical evaluation is handled using mpmath, but symbolics are handled
by Sage and Maxima.
"""
def __init__(self):
"""
See the docstring for :meth:`Function_exp_integral_e`.
EXAMPLES::
sage: exp_integral_e(1,0)
exp_integral_e(1, 0)
"""
BuiltinFunction.__init__(self, "exp_integral_e", nargs=2,
latex_name=r'exp_integral_e',
conversions=dict(maxima='expintegral_e'))
def _eval_(self, n, z):
"""
EXAMPLES::
sage: exp_integral_e(1.0, x)
exp_integral_e(1.00000000000000, x)
sage: exp_integral_e(x, 1.0)
exp_integral_e(x, 1.00000000000000)
sage: exp_integral_e(1.0, 1.0)
0.219383934395520
"""
if not isinstance(n, Expression) and not isinstance(z, Expression) and \
(is_inexact(n) or is_inexact(z)):
coercion_model = sage.structure.element.get_coercion_model()
n, z = coercion_model.canonical_coercion(n, z)
return self._evalf_(n, z, parent(n))
z_zero = False
# special case: z == 0 and n > 1
if isinstance(z, Expression):
if z.is_trivial_zero():
z_zero = True # for later
if n > 1:
return 1/(n-1)
else:
if not z:
z_zero = True
if n > 1:
return 1/(n-1)
# special case: n == 0
if isinstance(n, Expression):
if n.is_trivial_zero():
if z_zero:
return None
else:
return exp(-z)/z
else:
if not n:
if z_zero:
return None
else:
return exp(-z)/z
return None # leaves the expression unevaluated
def _evalf_(self, n, z, parent=None, algorithm=None):
"""
EXAMPLES::
sage: N(exp_integral_e(1, 1+I))
0.000281624451981418 - 0.179324535039359*I
sage: exp_integral_e(1, RealField(100)(1))
0.21938393439552027367716377546
"""
import mpmath
return mpmath_utils.call(mpmath.expint, n, z, parent=parent)
def _derivative_(self, n, z, diff_param=None):
"""
If `n` is an integer strictly larger than 0, then the derivative of
`E_n(z)` with respect to `z` is
`-E_{n-1}(z)`. See [AS]_ 5.1.26.
EXAMPLES::
sage: x = var('x')
sage: f = exp_integral_e(2,x)
sage: f.diff(x)
-exp_integral_e(1, x)
sage: f = exp_integral_e(2,sqrt(x))
sage: f.diff(x)
-1/2*exp_integral_e(1, sqrt(x))/sqrt(x)
"""
if n in ZZ and n > 0:
return -1*exp_integral_e(n-1,z)
else:
raise NotImplementedError("The derivative of this function is only implemented for n = 1, 2, 3, ...")
exp_integral_e = Function_exp_integral_e()
class Function_exp_integral_e1(BuiltinFunction):
r"""
The generalized complex exponential integral `E_1(z)` defined by
.. math::
\operatorname{E_1}(z) = \int_z^\infty \frac{e^{-t}}{t}\; dt
see [AS]_ 5.1.4.
EXAMPLES:
Numerical evaluation is handled using mpmath::
sage: N(exp_integral_e1(1))
0.219383934395520
sage: exp_integral_e1(RealField(100)(1))
0.21938393439552027367716377546
We can compare this to PARI's evaluation of
:meth:`exponential_integral_1`::
sage: N(exp_integral_e1(2.0))
0.0489005107080611
sage: N(exponential_integral_1(2.0))
0.0489005107080611
Symbolic derivatives and integrals are handled by Sage and Maxima::
sage: x = var('x')
sage: f = exp_integral_e1(x)
sage: f.diff(x)
-e^(-x)/x
sage: f.integrate(x)
-exp_integral_e(2, x)
ALGORITHM:
Numerical evaluation is handled using mpmath, but symbolics are handled
by Sage and Maxima.
"""
def __init__(self):
"""
See the docstring for :class:`Function_exp_integral_e1`.
EXAMPLES::
sage: exp_integral_e1(1)
exp_integral_e1(1)
"""
BuiltinFunction.__init__(self, "exp_integral_e1", nargs=1,
latex_name=r'exp_integral_e1',
conversions=dict(maxima='expintegral_e1'))
def _eval_(self, z):
"""
EXAMPLES::
sage: exp_integral_e1(x)
exp_integral_e1(x)
sage: exp_integral_e1(1.0)
0.219383934395520
"""
if not isinstance(z, Expression) and is_inexact(z):
return self._evalf_(z, parent(z))
return None # leaves the expression unevaluated
def _evalf_(self, z, parent=None, algorithm=None):
"""
EXAMPLES::
sage: N(exp_integral_e1(1+I))
0.000281624451981418 - 0.179324535039359*I
sage: exp_integral_e1(RealField(200)(0.5))
0.55977359477616081174679593931508523522684689031635351524829
"""
import mpmath
return mpmath_utils_call(mpmath.e1, z, parent=parent)
def _derivative_(self, z, diff_param=None):
"""
The derivative of `E_1(z)` is `-e^{-z}/z`. See [AS], 5.1.26.
EXAMPLES::
sage: x = var('x')
sage: f = exp_integral_e1(x)
sage: f.diff(x)
-e^(-x)/x
sage: f = exp_integral_e1(x^2)
sage: f.diff(x)
-2*e^(-x^2)/x
"""
return -exp(-z)/z
exp_integral_e1 = Function_exp_integral_e1()
class Function_log_integral(BuiltinFunction):
r"""
The logarithmic integral `\operatorname{li}(z)` defined by
.. math::
\operatorname{li}(x) = \int_0^z \frac{dt}{\ln(t)} = \operatorname{Ei}(\ln(x))
for x > 1 and by analytic continuation for complex arguments z (see [AS]_ 5.1.3).
EXAMPLES:
Numerical evaluation for real and complex arguments is handled using mpmath::
sage: N(log_integral(3))
2.16358859466719
sage: N(log_integral(3), digits=30)
2.16358859466719197287692236735
sage: log_integral(ComplexField(100)(3+I))
2.2879892769816826157078450911 + 0.87232935488528370139883806779*I
sage: log_integral(0)
0
Symbolic derivatives and integrals are handled by Sage and Maxima::
sage: x = var('x')
sage: f = log_integral(x)
sage: f.diff(x)
1/log(x)
sage: f.integrate(x)
x*log_integral(x) - Ei(2*log(x))
Here is a test from the mpmath documentation. There are
1,925,320,391,606,803,968,923 many prime numbers less than 1e23. The
value of ``log_integral(1e23)`` is very close to this::
sage: log_integral(1e23)
1.92532039161405e21
ALGORITHM:
Numerical evaluation is handled using mpmath, but symbolics are handled
by Sage and Maxima.
REFERENCES:
- http://en.wikipedia.org/wiki/Logarithmic_integral_function
- mpmath documentation: `logarithmic-integral`_
.. _`logarithmic-integral`: http://mpmath.googlecode.com/svn/trunk/doc/build/functions/expintegrals.html#logarithmic-integral
"""
def __init__(self):
"""
See the docstring for ``Function_log_integral``.
EXAMPLES::
sage: log_integral(3)
log_integral(3)
"""
BuiltinFunction.__init__(self, "log_integral", nargs=1,
latex_name=r'log_integral',
conversions=dict(maxima='expintegral_li'))
def _eval_(self, z):
"""
EXAMPLES::
sage: z = var('z')
sage: log_integral(z)
log_integral(z)
sage: log_integral(3.0)
2.16358859466719
sage: log_integral(0)
0
"""
if isinstance(z, Expression):
if z.is_trivial_zero(): # special case: z = 0
return z
else:
if is_inexact(z):
return self._evalf_(z, parent(z))
elif not z:
return z
return None # leaves the expression unevaluated
def _evalf_(self, z, parent=None, algorithm=None):
"""
EXAMPLES::
sage: N(log_integral(1e6))
78627.5491594622
sage: log_integral(RealField(200)(1e6))
78627.549159462181919862910747947261161321874382421767074759
"""
import mpmath
return mpmath_utils_call(mpmath.li, z, parent=parent)
def _derivative_(self, z, diff_param=None):
r"""
The derivative of `\operatorname{li}(z) is `1/log(z)`.
EXAMPLES::
sage: x = var('x')
sage: f = log_integral(x)
sage: f.diff(x)
1/log(x)
sage: f = log_integral(x^2)
sage: f.diff(x)
2*x/log(x^2)
"""
return 1/log(z)
li = log_integral = Function_log_integral()
class Function_log_integral_offset(BuiltinFunction):
r"""
The offset logarithmic integral, or Eulerian logarithmic integral,
`\operatorname{Li}(x)` is defined by
.. math::
\operatorname{Li}(x) = \int_2^x \frac{dt}{ln(t)} =
\operatorname{li}(x)-\operatorname{li}(2)
for `x \ge 2`.
The offset logarithmic integral should also not be confused with the
polylogarithm (also denoted by `\operatorname{Li}(x)` ), which is
implemented as :class:`sage.functions.log.Function_polylog`.
`\operatorname{Li}(x)` is identical to `\operatorname{li}(x)` except that
the lower limit of integration is `2` rather than `0` to avoid the
singularity at `x = 1` of
.. math::
\frac{1}{ln(t)}
See :class:`Function_log_integral` for details of `\operatorname{li}(x)`.
Thus `\operatorname{Li}(x)` can also be represented by
.. math::
\operatorname{Li}(x) = \operatorname{li}(x)-\operatorname{li}(2)
So we have::
sage: li(4.5)-li(2.0)-Li(4.5)
0.000000000000000
`\operatorname{Li}(x)` is extended to complex arguments `z`
by analytic continuation (see [AS]_ 5.1.3)::
sage: Li(6.6+5.4*I)
3.97032201503632 + 2.62311237593572*I
The function `\operatorname{Li}` is an approximation for the number of
primes up to `x`. In fact, the famous Riemann Hypothesis is
.. math::
|\pi(x) - \operatorname{Li}(x)| \leq \sqrt{x} \log(x).
For "small" `x`, `\operatorname{Li}(x)` is always slightly bigger
than `\pi(x)`. However it is a theorem that there are very
large values of `x` (e.g., around `10^{316}`), such that
`\exists x: \pi(x) > \operatorname{Li}(x)`. See "A new bound for the
smallest x with `\pi(x) > \operatorname{li}(x)`",
Bays and Hudson, Mathematics of Computation, 69 (2000) 1285-1296.
Here is a test from the mpmath documentation.
There are 1,925,320,391,606,803,968,923 prime numbers less than 1e23.
The value of ``log_integral_offset(1e23)`` is very close to this::
sage: log_integral_offset(1e23)
1.92532039161405e21
EXAMPLES:
Numerical evaluation for real and complex arguments is handled using mpmath::
sage: N(log_integral_offset(3))
1.11842481454970
sage: N(log_integral_offset(3), digits=30)
1.11842481454969918803233347815
sage: log_integral_offset(ComplexField(100)(3+I))
1.2428254968641898308632562019 + 0.87232935488528370139883806779*I
sage: log_integral_offset(2)
0
sage: for n in range(1,7):
... print '%-10s%-10s%-20s'%(10^n, prime_pi(10^n), N(Li(10^n)))
10 4 5.12043572466980
100 25 29.0809778039621
1000 168 176.564494210035
10000 1229 1245.09205211927
100000 9592 9628.76383727068
1000000 78498 78626.5039956821
Symbolic derivatives are handled by Sage and integration by Maxima::
sage: x = var('x')
sage: f = log_integral_offset(x)
sage: f.diff(x)
1/log(x)
sage: f.integrate(x)
-x*log_integral(2) + x*log_integral(x) - Ei(2*log(x))
sage: Li(x).integrate(x,2.0,4.5)
-2.5*log_integral(2) + 5.79932114741
sage: N(f.integrate(x,2.0,3.0))
0.601621785860587
Note: Definite integration returns a part symbolic and part
numerical result. This is because when Li(x) is evaluated it is
passed as li(x)-li(2).
ALGORITHM:
Numerical evaluation is handled using mpmath, but symbolics are handled
by Sage and Maxima.
REFERENCES:
- http://en.wikipedia.org/wiki/Logarithmic_integral_function
- mpmath documentation: `logarithmic-integral`_
.. _`logarithmic-integral`: http://mpmath.googlecode.com/svn/trunk/doc/build/functions/expintegrals.html#logarithmic-integral
"""
def __init__(self):
"""
See the docstring for ``Function_log_integral-offset``.
EXAMPLES::
sage: log_integral_offset(3)
log_integral(3) - log_integral(2)
"""
BuiltinFunction.__init__(self, "log_integral_offset", nargs=1,
latex_name=r'log_integral_offset')
def _eval_(self,z):
"""
EXAMPLES::
sage: z = var('z')
sage: log_integral_offset(z)
-log_integral(2) + log_integral(z)
sage: log_integral_offset(3.0)
1.11842481454970
sage: log_integral_offset(2)
0
"""
if not isinstance(z,Expression) and is_inexact(z):
return self._evalf_(z,parent(z))
if z==2:
import sage.symbolic.ring
return sage.symbolic.ring.SR(0)
return li(z)-li(2)
# If we return:(li(z)-li(2)) we get correct symbolic integration.
# But on definite integration it returns x.xxxx-li(2).
def _evalf_(self, z, parent=None, algorithm=None):
"""
EXAMPLES::
sage: N(log_integral_offset(1e6))
78626.5039956821
sage: log_integral_offset(RealField(200)(1e6))
78626.503995682064427078066159058066548185351766843615873183
sage: li(4.5)-li(2.0)-Li(4.5)
0.000000000000000
"""
import mpmath
return mpmath_utils_call(mpmath.li, z, offset=True, parent=parent)
def _derivative_(self, z, diff_param=None):
"""
The derivative of `\operatorname{Li}(z) is `1/log(z)`.
EXAMPLES::
sage: x = var('x')
sage: f = log_integral_offset(x)
sage: f.diff(x)
1/log(x)
sage: f = log_integral_offset(x^2)
sage: f.diff(x)
2*x/log(x^2)
"""
return 1/log(z)
Li = log_integral_offset = Function_log_integral_offset()
class Function_sin_integral(BuiltinFunction):
r"""
The trigonometric integral `\operatorname{Si}(z)` defined by
.. math::
\operatorname{Si}(z) = \int_0^z \frac{\sin(t)}{t}\; dt,
see [AS]_ 5.2.1.
EXAMPLES:
Numerical evaluation for real and complex arguments is handled using mpmath::
sage: sin_integral(0)
0
sage: sin_integral(0.0)
0.000000000000000
sage: sin_integral(3.0)
1.84865252799947
sage: N(sin_integral(3), digits=30)
1.84865252799946825639773025111
sage: sin_integral(ComplexField(100)(3+I))
2.0277151656451253616038525998 + 0.015210926166954211913653130271*I
The alias `Si` can be used instead of `sin_integral`::
sage: Si(3.0)
1.84865252799947
The limit of `\operatorname{Si}(z)` as `z \to \infty` is `\pi/2`::
sage: N(sin_integral(1e23))
1.57079632679490
sage: N(pi/2)
1.57079632679490
At 200 bits of precision `\operatorname{Si}(10^{23})` agrees with `\pi/2` up to
`10^{-24}`::
sage: sin_integral(RealField(200)(1e23))
1.5707963267948966192313288218697837425815368604836679189519
sage: N(pi/2, prec=200)
1.5707963267948966192313216916397514420985846996875529104875
The exponential sine integral is analytic everywhere::
sage: sin_integral(-1.0)
-0.946083070367183
sage: sin_integral(-2.0)
-1.60541297680269
sage: sin_integral(-1e23)
-1.57079632679490
Symbolic derivatives and integrals are handled by Sage and Maxima::
sage: x = var('x')
sage: f = sin_integral(x)
sage: f.diff(x)
sin(x)/x
sage: f.integrate(x)
x*sin_integral(x) + cos(x)
sage: integrate(sin(x)/x, x)
-1/2*I*Ei(I*x) + 1/2*I*Ei(-I*x)
Compare values of the functions `\operatorname{Si}(x)` and
`f(x) = (1/2)i \cdot \operatorname{Ei}(-ix) - (1/2)i \cdot
\operatorname{Ei}(ix) - \pi/2`, which are both anti-derivatives of
`\sin(x)/x`, at some random positive real numbers::
sage: f(x) = 1/2*I*Ei(-I*x) - 1/2*I*Ei(I*x) - pi/2
sage: g(x) = sin_integral(x)
sage: R = [ abs(RDF.random_element()) for i in range(100) ]
sage: all(abs(f(x) - g(x)) < 1e-10 for x in R)
True
The Nielsen spiral is the parametric plot of (Si(t), Ci(t))::
sage: x=var('x')
sage: f(x) = sin_integral(x)
sage: g(x) = cos_integral(x)
sage: P = parametric_plot([f, g], (x, 0.5 ,20))
sage: show(P, frame=True, axes=False)
ALGORITHM:
Numerical evaluation is handled using mpmath, but symbolics are handled
by Sage and Maxima.
REFERENCES:
- http://en.wikipedia.org/wiki/Trigonometric_integral
- mpmath documentation: `si`_
.. _`si`: http://mpmath.googlecode.com/svn/trunk/doc/build/functions/expintegrals.html#si
"""
def __init__(self):
"""
See the docstring for ``Function_sin_integral``.
EXAMPLES::
sage: sin_integral(1)
sin_integral(1)
"""
BuiltinFunction.__init__(self, "sin_integral", nargs=1,
latex_name=r'\operatorname{Si}',
conversions=dict(maxima='expintegral_si'))
def _eval_(self, z):
"""
EXAMPLES::
sage: z = var('z')
sage: sin_integral(z)
sin_integral(z)
sage: sin_integral(3.0)
1.84865252799947
sage: sin_integral(0)
0
"""
if not isinstance(z, Expression) and is_inexact(z):
return self._evalf_(z, parent(z))
# special case: z = 0
if isinstance(z, Expression):
if z.is_trivial_zero():
return z
else:
if not z:
return z
return None # leaves the expression unevaluated
def _evalf_(self, z, parent=None, algorithm=None):
"""
EXAMPLES:
The limit `\operatorname{Si}(z)` as `z \to \infty` is `\pi/2`::
sage: N(sin_integral(1e23) - pi/2)
0.000000000000000
At 200 bits of precision `\operatorname{Si}(10^{23})` agrees with `\pi/2` up to
`10^{-24}`::
sage: sin_integral(RealField(200)(1e23))
1.5707963267948966192313288218697837425815368604836679189519
sage: N(pi/2, prec=200)
1.5707963267948966192313216916397514420985846996875529104875
The exponential sine integral is analytic everywhere, even on the
negative real axis::
sage: sin_integral(-1.0)
-0.946083070367183
sage: sin_integral(-2.0)
-1.60541297680269
sage: sin_integral(-1e23)
-1.57079632679490
"""
import mpmath
return mpmath_utils_call(mpmath.si, z, parent=parent)
def _derivative_(self, z, diff_param=None):
r"""
The derivative of `\operatorname{Si}(z)` is `\sin(z)/z` if `z`
is not zero. The derivative at `z = 0` is `1` (but this
exception is not currently implemented).
EXAMPLES::
sage: x = var('x')
sage: f = sin_integral(x)
sage: f.diff(x)
sin(x)/x
sage: f = sin_integral(x^2)
sage: f.diff(x)
2*sin(x^2)/x
"""
return sin(z)/z
Si = sin_integral = Function_sin_integral()
class Function_cos_integral(BuiltinFunction):
r"""
The trigonometric integral `\operatorname{Ci}(z)` defined by
.. math::
\operatorname{Ci}(z) = \gamma + \log(z) + \int_0^z \frac{\cos(t)-1}{t}\; dt,
where `\gamma` is the Euler gamma constant (``euler_gamma`` in Sage),
see [AS]_ 5.2.1.
EXAMPLES:
Numerical evaluation for real and complex arguments is handled using mpmath::
sage: cos_integral(3.0)
0.119629786008000
The alias `Ci` can be used instead of `cos_integral`::
sage: Ci(3.0)
0.119629786008000
Compare ``cos_integral(3.0)`` to the definition of the value using
numerical integration::
sage: N(euler_gamma + log(3.0) + integrate((cos(x)-1)/x, x, 0, 3.0) - cos_integral(3.0)) < 1e-14
True
Arbitrary precision and complex arguments are handled::
sage: N(cos_integral(3), digits=30)
0.119629786008000327626472281177
sage: cos_integral(ComplexField(100)(3+I))
0.078134230477495714401983633057 - 0.37814733904787920181190368789*I
The limit `\operatorname{Ci}(z)` as `z \to \infty` is zero::
sage: N(cos_integral(1e23))
-3.24053937643003e-24
Symbolic derivatives and integrals are handled by Sage and Maxima::
sage: x = var('x')
sage: f = cos_integral(x)
sage: f.diff(x)
cos(x)/x
sage: f.integrate(x)
x*cos_integral(x) - sin(x)
The Nielsen spiral is the parametric plot of (Si(t), Ci(t))::
sage: t=var('t')
sage: f(t) = sin_integral(t)
sage: g(t) = cos_integral(t)
sage: P = parametric_plot([f, g], (t, 0.5 ,20))
sage: show(P, frame=True, axes=False)
ALGORITHM:
Numerical evaluation is handled using mpmath, but symbolics are handled
by Sage and Maxima.
REFERENCES:
- http://en.wikipedia.org/wiki/Trigonometric_integral
- mpmath documentation: `ci`_
.. _`ci`: http://mpmath.googlecode.com/svn/trunk/doc/build/functions/expintegrals.html#ci
"""
def __init__(self):
"""
See the docstring for :class:`Function_cos_integral`.
EXAMPLES::
sage: cos_integral(1)
cos_integral(1)
"""
BuiltinFunction.__init__(self, "cos_integral", nargs=1,
latex_name=r'\operatorname{Ci}',
conversions=dict(maxima='expintegral_ci'))
def _eval_(self, z):
"""
EXAMPLES::
sage: z = var('z')
sage: cos_integral(z)
cos_integral(z)
sage: cos_integral(3.0)
0.119629786008000
sage: cos_integral(0)
cos_integral(0)
sage: N(cos_integral(0))
-infinity
"""
if not isinstance(z, Expression) and is_inexact(z):
return self._evalf_(z, parent(z))
return None # leaves the expression unevaluated
def _evalf_(self, z, parent=None, algorithm=None):
"""
EXAMPLES::
sage: N(cos_integral(1e23)) < 1e-20
True
sage: N(cos_integral(1e-10), digits=30)
-22.4486352650389235918737540487
sage: cos_integral(ComplexField(100)(I))
0.83786694098020824089467857943 + 1.5707963267948966192313216916*I
"""
import mpmath
return mpmath_utils_call(mpmath.ci, z, parent=parent)
def _derivative_(self, z, diff_param=None):
r"""
The derivative of `\operatorname{Ci}(z)` is `\cos(z)/z` if `z` is not zero.
EXAMPLES::
sage: x = var('x')
sage: f = cos_integral(x)
sage: f.diff(x)
cos(x)/x