-
-
Notifications
You must be signed in to change notification settings - Fork 856
Expand file tree
/
Copy pathconstants.py
More file actions
1282 lines (1018 loc) · 32.9 KB
/
constants.py
File metadata and controls
1282 lines (1018 loc) · 32.9 KB
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"""
Mathematical constants
The following standard mathematical constants are defined in Sage,
along with support for coercing them into GAP, PARI/GP, KASH,
Maxima, Mathematica, Maple, Octave, and Singular::
sage: pi
pi
sage: e # base of the natural logarithm
e
sage: NaN # Not a number
NaN
sage: golden_ratio
golden_ratio
sage: log2 # natural logarithm of the real number 2
log2
sage: euler_gamma # Euler's gamma constant
euler_gamma
sage: catalan # the Catalan constant
catalan
sage: khinchin # Khinchin's constant
khinchin
sage: twinprime
twinprime
sage: mertens
mertens
Support for coercion into the various systems means that if, e.g.,
you want to create `\pi` in Maxima and Singular, you don't
have to figure out the special notation for each system. You just
type the following::
sage: maxima(pi)
%pi
sage: singular(pi)
pi
sage: gap(pi)
pi
sage: gp(pi)
3.1415926535897932384626433832795028842
sage: pari(pi)
3.14159265358979
sage: kash(pi) # optional - kash
3.14159265358979323846264338328
sage: mathematica(pi) # optional - mathematica
Pi
sage: pi._maple_init_()
'Pi'
sage: octave(pi) # optional - octave
3.14159
Arithmetic operations with constants also yield constants, which
can be coerced into other systems or evaluated.
::
sage: a = pi + e*4/5; a
pi + 4/5*e
sage: maxima(a)
%pi+(4*%e)/5
sage: RealField(15)(a) # 15 *bits* of precision
5.316
sage: gp(a)
5.3162181163570294267508733603616328824
sage: print(mathematica(a)) # optional - mathematica
4 E
--- + Pi
5
EXAMPLES: Decimal expansions of constants
We can obtain floating point approximations to each of these
constants by coercing into the real field with given precision. For
example, to 200 binary places we have the following::
sage: R = RealField(200); R
Real Field with 200 bits of precision
::
sage: R(pi)
3.1415926535897932384626433832795028841971693993751058209749
::
sage: R(e)
2.7182818284590452353602874713526624977572470936999595749670
::
sage: R(NaN)
NaN
::
sage: R(golden_ratio)
1.6180339887498948482045868343656381177203091798057628621354
::
sage: R(log2)
0.69314718055994530941723212145817656807550013436025525412068
::
sage: R(euler_gamma)
0.57721566490153286060651209008240243104215933593992359880577
::
sage: R(catalan)
0.91596559417721901505460351493238411077414937428167213426650
::
sage: R(khinchin)
2.6854520010653064453097148354817956938203822939944629530512
EXAMPLES: Arithmetic with constants
::
sage: f = I*(e+1); f
I*e + I
sage: f^2
(I*e + I)^2
sage: _.expand()
-e^2 - 2*e - 1
::
sage: pp = pi+pi; pp
2*pi
sage: R(pp)
6.2831853071795864769252867665590057683943387987502116419499
::
sage: s = (1 + e^pi); s
e^pi + 1
sage: R(s)
24.140692632779269005729086367948547380266106242600211993445
sage: R(s-1)
23.140692632779269005729086367948547380266106242600211993445
::
sage: l = (1-log2)/(1+log2); l
-(log2 - 1)/(log2 + 1)
sage: R(l)
0.18123221829928249948761381864650311423330609774776013488056
::
sage: pim = maxima(pi)
sage: maxima.eval('fpprec : 100')
'100'
sage: pim.bfloat()
3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117068b0
AUTHORS:
- Alex Clemesha (2006-01-15)
- William Stein
- Alex Clemesha, William Stein (2006-02-20): added new constants;
removed todos
- Didier Deshommes (2007-03-27): added constants from RQDF (deprecated)
TESTS:
Coercing the sum of a bunch of the constants to many different
floating point rings::
sage: a = pi + e + golden_ratio + log2 + euler_gamma + catalan + khinchin + twinprime + mertens; a
mertens + twinprime + khinchin + log2 + golden_ratio + catalan + euler_gamma + pi + e
sage: parent(a)
Symbolic Ring
sage: RR(a) # abs tol 1e-13
13.2713479401972
sage: RealField(212)(a)
13.2713479401972493100988191995758139408711068200030748178329712
sage: RealField(230)(a)
13.271347940197249310098819199575813940871106820003074817832971189555
sage: RDF(a) # abs tol 1e-13
13.271347940197249
sage: CC(a) # abs tol 1e-13
13.2713479401972
sage: CDF(a) # abs tol 1e-13
13.271347940197249
sage: ComplexField(230)(a)
13.271347940197249310098819199575813940871106820003074817832971189555
Check that :issue:`8237` is fixed::
sage: maxima('infinity').sage()
Infinity
sage: maxima('inf').sage()
+Infinity
sage: maxima('minf').sage()
-Infinity
"""
###############################################################################
# Sage: Open Source Mathematical Software
# Copyright (C) 2008 William Stein <wstein@gmail.com>
# Copyright (C) 2008-2010 Burcin Erocal <burcin@erocal.org>
# 2009 Mike Hansen <mhansen@gmail.com>
# Distributed under the terms of the GNU General Public License (GPL),
# version 2 or any later version. The full text of the GPL is available at:
# https://www.gnu.org/licenses/
###############################################################################
import math
from functools import partial
from sage.rings.infinity import infinity, minus_infinity, unsigned_infinity
from sage.structure.richcmp import op_EQ, op_GE, op_LE, richcmp_method
from sage.symbolic.expression import E, init_pynac_I, register_symbol
constants_table = {}
constants_name_table = {}
constants_name_table[repr(infinity)] = infinity
constants_name_table[repr(unsigned_infinity)] = unsigned_infinity
constants_name_table[repr(minus_infinity)] = minus_infinity
I = init_pynac_I()
register_symbol(infinity, {'maxima': 'inf'}, 0)
register_symbol(minus_infinity, {'maxima': 'minf'}, 0)
register_symbol(unsigned_infinity, {'maxima': 'infinity'}, 0)
register_symbol(I, {'mathematica': 'I'}, 0)
register_symbol(True, {'giac': 'true',
'mathematica': 'True',
'maxima': 'true'}, 0)
register_symbol(False, {'giac': 'false',
'mathematica': 'False',
'maxima': 'false'}, 0)
def unpickle_Constant(class_name, name, conversions, latex, mathml, domain):
"""
EXAMPLES::
sage: from sage.symbolic.constants import unpickle_Constant
sage: a = unpickle_Constant('Constant', 'a', {}, 'aa', '', 'positive')
sage: a.domain()
'positive'
sage: latex(a)
aa
Note that if the name already appears in the
``constants_name_table``, then that will be returned instead of
constructing a new object::
sage: pi = unpickle_Constant('Pi', 'pi', None, None, None, None)
sage: pi._maxima_init_()
'%pi'
"""
if name in constants_name_table:
return constants_name_table[name]
if class_name == "Constant":
return Constant(name, conversions=conversions, latex=latex,
mathml=mathml, domain=domain)
cls = globals()[class_name]
return cls(name=name)
@richcmp_method
class Constant:
def __init__(self, name, conversions=None, latex=None, mathml='',
domain='complex'):
"""
EXAMPLES::
sage: from sage.symbolic.constants import Constant
sage: p = Constant('p')
sage: loads(dumps(p))
p
"""
self._conversions = conversions if conversions is not None else {}
self._latex = latex if latex is not None else name
self._mathml = mathml
self._name = name
self._domain = domain
for system, value in self._conversions.items():
setattr(self, "_%s_" % system, partial(self._generic_interface, value))
setattr(self, "_%s_init_" % system, partial(self._generic_interface_init, value))
from sage.symbolic.expression import PynacConstant
self._pynac = PynacConstant(self._name, self._latex, self._domain)
self._serial = self._pynac.serial()
constants_table[self._serial] = self
constants_name_table[self._name] = self
register_symbol(self.expression(), self._conversions)
def __richcmp__(self, other, op):
"""
EXAMPLES::
sage: from sage.symbolic.constants import Constant
sage: p = Constant('p')
sage: s = Constant('s')
sage: p == p
True
sage: p == s
False
sage: p != s
True
"""
if self.__class__ == other.__class__ and self._name == other._name:
return op in [op_EQ, op_GE, op_LE]
return NotImplemented
def __reduce__(self):
"""
Add support for pickling constants.
EXAMPLES::
sage: from sage.symbolic.constants import Constant
sage: p = Constant('p')
sage: p.__reduce__()
(<function unpickle_Constant at 0x...>,
('Constant', 'p', {}, 'p', '', 'complex'))
sage: loads(dumps(p))
p
sage: pi.pyobject().__reduce__()
(<function unpickle_Constant at 0x...>,
('Pi',
'pi',
...,
'\\pi',
'<mi>π</mi>',
'positive'))
sage: loads(dumps(pi.pyobject()))
pi
"""
return (unpickle_Constant, (self.__class__.__name__, self._name,
self._conversions, self._latex,
self._mathml, self._domain))
def domain(self):
"""
Return the domain of this constant. This is either positive,
real, or complex, and is used by Pynac to make inferences
about expressions containing this constant.
EXAMPLES::
sage: p = pi.pyobject(); p
pi
sage: type(_)
<class 'sage.symbolic.constants.Pi'>
sage: p.domain()
'positive'
"""
return self._domain
def expression(self):
"""
Return an expression for this constant.
EXAMPLES::
sage: a = pi.pyobject()
sage: pi2 = a.expression()
sage: pi2
pi
sage: pi2 + 2
pi + 2
sage: pi - pi2
0
"""
return self._pynac.expression()
def _symbolic_(self, SR):
"""
Return an expression for this constant.
INPUT:
- ``SR`` -- a symbolic ring parent
EXAMPLES::
sage: SR(pi.pyobject())
pi
sage: pi.pyobject()._symbolic_(SR)
pi
sage: f(x,y) = 2
sage: f.parent()(pi.pyobject())
(x, y) |--> pi
"""
return SR(self.expression())
def name(self):
"""
Return the name of this constant.
EXAMPLES::
sage: from sage.symbolic.constants import Constant
sage: c = Constant('c')
sage: c.name()
'c'
"""
return self._name
def __repr__(self):
"""
EXAMPLES::
sage: from sage.symbolic.constants import Constant
sage: c = Constant('c')
sage: c
c
"""
return self._name
def _latex_(self):
r"""
EXAMPLES::
sage: from sage.symbolic.constants import Constant
sage: c = Constant('c', latex=r'\xi')
sage: latex(c)
\xi
"""
return self._latex
def _mathml_(self):
"""
EXAMPLES::
sage: from sage.symbolic.constants import Constant
sage: c = Constant('c', mathml=r'<mi>c</mi>')
sage: mathml(c)
<mi>c</mi>
"""
return self._mathml
def _generic_interface(self, value, I):
"""
This is a helper method used in defining the ``_X_`` methods
where ``X`` is the name of some interface.
EXAMPLES::
sage: from sage.symbolic.constants import Constant
sage: p = Constant('p', conversions=dict(maxima='%pi'))
sage: p._maxima_(maxima)
%pi
The above ``_maxima_`` is constructed like ``m`` below::
sage: from functools import partial
sage: m = partial(p._generic_interface, '%pi')
sage: m(maxima)
%pi
"""
return I(value)
def _generic_interface_init(self, value):
"""
This is a helper method used in defining the ``_X_init_`` methods
where ``X`` is the name of some interface.
EXAMPLES::
sage: from sage.symbolic.constants import Constant
sage: p = Constant('p', conversions=dict(maxima='%pi'))
sage: p._maxima_init_()
'%pi'
The above ``_maxima_init_`` is constructed like ``mi`` below::
sage: from functools import partial
sage: mi = partial(p._generic_interface_init, '%pi')
sage: mi()
'%pi'
"""
return value
def _interface_(self, I):
"""
EXAMPLES::
sage: from sage.symbolic.constants import Constant
sage: p = Constant('p', conversions=dict(maxima='%pi'))
sage: p._interface_(maxima)
%pi
"""
try:
s = self._conversions[I.name()]
return I(s)
except KeyError:
pass
try:
return getattr(self, "_%s_" % (I.name()))(I)
except AttributeError:
pass
raise NotImplementedError
def _gap_(self, gap):
"""
Return the constant as a string in GAP. Since GAP does not
have floating point numbers, we simply return the constant as
a string.
EXAMPLES::
sage: from sage.symbolic.constants import Constant
sage: p = Constant('p')
sage: gap(p)
p
"""
return gap('"%s"' % self)
def _singular_(self, singular):
"""
Return the constant as a string in Singular. Since Singular
does not always support floating point numbers, we simply
return the constant as a string. (Singular allows floating point
numbers if the current ring has floating point coefficients,
but not otherwise.)
EXAMPLES::
sage: from sage.symbolic.constants import Constant
sage: p = Constant('p')
sage: singular(p)
p
"""
return singular('"%s"' % self)
class Pi(Constant):
def __init__(self, name='pi'):
r"""
TESTS::
sage: pi._latex_()
'\\pi'
sage: latex(pi)
\pi
sage: mathml(pi)
<mi>π</mi>
"""
conversions = dict(axiom='%pi', fricas='%pi', maxima='%pi', giac='pi',
gp='Pi', kash='PI',
mathematica='Pi', matlab='pi', maple='Pi',
octave='pi', pari='Pi', pynac='Pi')
Constant.__init__(self, name, conversions=conversions,
latex=r"\pi", mathml="<mi>π</mi>",
domain='positive')
def __float__(self):
"""
EXAMPLES::
sage: float(pi)
3.141592653589793
"""
return math.pi
def _mpfr_(self, R):
"""
EXAMPLES::
sage: pi._mpfr_(RealField(100))
3.1415926535897932384626433833
"""
return R.pi()
def _real_double_(self, R):
"""
EXAMPLES::
sage: pi._real_double_(RDF)
3.141592653589793
"""
return R.pi()
def _sympy_(self):
"""
Convert pi to sympy pi.
EXAMPLES::
sage: import sympy # needs sympy
sage: sympy.pi == pi # indirect doctest # needs sympy
True
"""
import sympy
return sympy.pi
pi = Pi().expression()
"""
The formal square root of -1.
EXAMPLES::
sage: SR.I()
I
sage: SR.I()^2
-1
Note that conversions to real fields will give TypeErrors::
sage: float(SR.I())
Traceback (most recent call last):
...
TypeError: unable to simplify to float approximation
sage: gp(SR.I())
I
sage: RR(SR.I())
Traceback (most recent call last):
...
TypeError: unable to convert '1.00000000000000*I' to a real number
Expressions involving I that are real-valued can be converted to real fields::
sage: float(I*I)
-1.0
sage: RR(I*I)
-1.00000000000000
We can convert to complex fields::
sage: C = ComplexField(200); C
Complex Field with 200 bits of precision
sage: C(SR.I())
1.0000000000000000000000000000000000000000000000000000000000*I
sage: SR.I()._complex_mpfr_field_(ComplexField(53))
1.00000000000000*I
sage: SR.I()._complex_double_(CDF)
1.0*I
sage: CDF(SR.I())
1.0*I
sage: z = SR.I() + I; z
2*I
sage: C(z)
2.0000000000000000000000000000000000000000000000000000000000*I
sage: 1e8*SR.I()
1.00000000000000e8*I
sage: complex(SR.I())
1j
sage: QQbar(SR.I())
I
sage: abs(SR.I())
1
sage: SR.I().minpoly()
x^2 + 1
sage: maxima(2*SR.I())
2*%i
TESTS::
sage: repr(SR.I())
'I'
sage: latex(SR.I())
i
"""
# The base of the natural logarithm, e, is not a constant in GiNaC/Sage. It is
# represented by exp(1). A dummy class to make this work with arithmetic and
# coercion is implemented in the module sage.symbolic.expression for speed.
e = E()
# Allow for backtranslation to this symbol from Mathematica (#29833).
register_symbol(e, {'mathematica': 'E'})
class NotANumber(Constant):
"""
Not a Number
"""
def __init__(self, name="NaN"):
"""
EXAMPLES::
sage: loads(dumps(NaN))
NaN
"""
conversions = dict(matlab='NaN')
Constant.__init__(self, name, conversions=conversions)
def __float__(self):
"""
EXAMPLES::
sage: float(NaN)
nan
"""
return float('nan')
def _mpfr_(self, R):
"""
EXAMPLES::
sage: NaN._mpfr_(RealField(53))
NaN
sage: type(_)
<class 'sage.rings.real_mpfr.RealNumber'>
"""
return R('NaN') # ??? nan in mpfr: void mpfr_set_nan (mpfr_t x)
def _real_double_(self, R):
"""
EXAMPLES::
sage: RDF(NaN)
NaN
"""
return R.NaN()
def _sympy_(self):
"""
Convert ``NaN`` to SymPy NaN.
EXAMPLES::
sage: bool(NaN._sympy_()._sage_() == NaN) # needs sympy
True
sage: import sympy # needs sympy
sage: sympy.nan == NaN # this should be fixed # needs sympy
False
"""
import sympy
return sympy.nan
NaN = NotANumber().expression()
class GoldenRatio(Constant):
"""
The number (1+sqrt(5))/2.
EXAMPLES::
sage: gr = golden_ratio
sage: RR(gr)
1.61803398874989
sage: R = RealField(200)
sage: R(gr)
1.6180339887498948482045868343656381177203091798057628621354
sage: grm = maxima(golden_ratio);grm
(sqrt(5)+1)/2
sage: grm + grm
sqrt(5)+1
sage: float(grm + grm)
3.23606797749979
"""
def __init__(self, name='golden_ratio'):
"""
EXAMPLES::
sage: loads(dumps(golden_ratio))
golden_ratio
"""
conversions = dict(mathematica='(1+Sqrt[5])/2', gp='(1+sqrt(5))/2',
maple='(1+sqrt(5))/2', maxima='(1+sqrt(5))/2',
pari='(1+sqrt(5))/2', octave='(1+sqrt(5))/2',
kash='(1+Sqrt(5))/2', giac='(1+sqrt(5))/2')
Constant.__init__(self, name, conversions=conversions,
latex=r'\phi', domain='positive')
def minpoly(self, bits=None, degree=None, epsilon=0):
"""
EXAMPLES::
sage: golden_ratio.minpoly()
x^2 - x - 1
"""
from sage.rings.rational_field import QQ
x = QQ['x'].gen(0)
return x**2 - x - 1
def __float__(self):
"""
EXAMPLES::
sage: float(golden_ratio)
1.618033988749895
sage: golden_ratio.__float__()
1.618033988749895
"""
return 0.5 + math.sqrt(1.25)
def _real_double_(self, R):
"""
EXAMPLES::
sage: RDF(golden_ratio)
1.618033988749895
"""
return R('1.61803398874989484820458')
def _mpfr_(self, R):
"""
EXAMPLES::
sage: golden_ratio._mpfr_(RealField(100))
1.6180339887498948482045868344
sage: RealField(100)(golden_ratio)
1.6180339887498948482045868344
"""
return (R(1) + R(5).sqrt()) / R(2)
def _algebraic_(self, field):
"""
EXAMPLES::
sage: golden_ratio._algebraic_(QQbar)
1.618033988749895?
sage: QQbar(golden_ratio)
1.618033988749895?
"""
import sage.rings.qqbar
return field(sage.rings.qqbar.get_AA_golden_ratio())
def _sympy_(self):
"""
Convert ``golden_ratio`` to SymPy GoldenRatio.
EXAMPLES::
sage: import sympy # needs sympy
sage: sympy.GoldenRatio == golden_ratio # indirect doctest # needs sympy
True
"""
import sympy
return sympy.GoldenRatio
golden_ratio = GoldenRatio().expression()
class Log2(Constant):
"""
The natural logarithm of the real number 2.
EXAMPLES::
sage: log2
log2
sage: float(log2)
0.6931471805599453
sage: RR(log2)
0.693147180559945
sage: R = RealField(200); R
Real Field with 200 bits of precision
sage: R(log2)
0.69314718055994530941723212145817656807550013436025525412068
sage: l = (1-log2)/(1+log2); l
-(log2 - 1)/(log2 + 1)
sage: R(l)
0.18123221829928249948761381864650311423330609774776013488056
sage: maxima(log2)
log(2)
sage: maxima(log2).float()
0.6931471805599453
sage: gp(log2)
0.69314718055994530941723212145817656807
sage: RealField(150)(2).log()
0.69314718055994530941723212145817656807550013
sage: giac(log2) # optional - giac
ln(2)
"""
def __init__(self, name='log2'):
"""
EXAMPLES::
sage: loads(dumps(log2))
log2
"""
conversions = dict(mathematica='Log[2]', kash='Log(2)',
maple='log(2)', maxima='log(2)', gp='log(2)',
pari='log(2)', octave='log(2)', giac='log(2)')
Constant.__init__(self, name, conversions=conversions,
latex=r'\log(2)', domain='positive')
def __float__(self):
"""
EXAMPLES::
sage: float(log2)
0.6931471805599453
sage: log2.__float__()
0.6931471805599453
"""
return math.log(2)
def _real_double_(self, R):
"""
EXAMPLES::
sage: RDF(log2)
0.6931471805599453
"""
return R.log2()
def _mpfr_(self, R):
"""
EXAMPLES::
sage: RealField(100)(log2)
0.69314718055994530941723212146
sage: log2._mpfr_(RealField(100))
0.69314718055994530941723212146
"""
return R.log2()
log2 = Log2().expression()
class EulerGamma(Constant):
"""
The limiting difference between the harmonic series and the natural
logarithm.
EXAMPLES::
sage: R = RealField()
sage: R(euler_gamma)
0.577215664901533
sage: R = RealField(200); R
Real Field with 200 bits of precision
sage: R(euler_gamma)
0.57721566490153286060651209008240243104215933593992359880577
sage: eg = euler_gamma + euler_gamma; eg
2*euler_gamma
sage: R(eg)
1.1544313298030657212130241801648048620843186718798471976115
"""
def __init__(self, name='euler_gamma'):
"""
EXAMPLES::
sage: loads(dumps(euler_gamma))
euler_gamma
"""
conversions = dict(kash='EulerGamma(R)', maple='gamma',
mathematica='EulerGamma', pari='Euler',
maxima='%gamma', pynac='Euler', giac='euler_gamma',
fricas='-digamma(1)')
Constant.__init__(self, name, conversions=conversions,
latex=r'\gamma', domain='positive')
def _mpfr_(self, R):
"""
EXAMPLES::
sage: RealField(100)(euler_gamma)
0.57721566490153286060651209008
sage: euler_gamma._mpfr_(RealField(100))
0.57721566490153286060651209008
"""
return R.euler_constant()
def __float__(self):
"""
EXAMPLES::
sage: float(euler_gamma)
0.5772156649015329
"""
return 0.57721566490153286060651209008
def _real_double_(self, R):
"""
EXAMPLES::
sage: RDF(euler_gamma)
0.5772156649015329
"""
return R.euler_constant()
def _sympy_(self):
"""
Convert ``euler_gamma`` to SymPy EulerGamma.
EXAMPLES::