220
220
from sage .rings .infinity import (infinity , minus_infinity ,
221
221
unsigned_infinity )
222
222
from sage .structure .richcmp import richcmp_method , op_EQ , op_GE , op_LE
223
+ from sage .symbolic .expression import register_symbol , init_pynac_I
224
+ from sage .symbolic .expression import E
223
225
224
226
constants_table = {}
225
227
constants_name_table = {}
226
228
constants_name_table [repr (infinity )] = infinity
227
229
constants_name_table [repr (unsigned_infinity )] = unsigned_infinity
228
230
constants_name_table [repr (minus_infinity )] = minus_infinity
229
231
230
- from sage .symbolic .expression import register_symbol , init_pynac_I
231
-
232
232
I = init_pynac_I ()
233
233
234
- register_symbol (infinity , {'maxima' :'inf' }, 0 )
235
- register_symbol (minus_infinity , {'maxima' :'minf' }, 0 )
236
- register_symbol (unsigned_infinity , {'maxima' :'infinity' }, 0 )
237
- register_symbol (I , {'mathematica' :'I' }, 0 )
238
- register_symbol (True , {'giac' :'true' ,
239
- 'mathematica' :'True' ,
240
- 'maxima' :'true' }, 0 )
241
- register_symbol (False , {'giac' :'false' ,
242
- 'mathematica' :'False' ,
243
- 'maxima' :'false' }, 0 )
234
+ register_symbol (infinity , {'maxima' : 'inf' }, 0 )
235
+ register_symbol (minus_infinity , {'maxima' : 'minf' }, 0 )
236
+ register_symbol (unsigned_infinity , {'maxima' : 'infinity' }, 0 )
237
+ register_symbol (I , {'mathematica' : 'I' }, 0 )
238
+ register_symbol (True , {'giac' : 'true' ,
239
+ 'mathematica' : 'True' ,
240
+ 'maxima' : 'true' }, 0 )
241
+ register_symbol (False , {'giac' : 'false' ,
242
+ 'mathematica' : 'False' ,
243
+ 'maxima' : 'false' }, 0 )
244
244
245
245
246
246
def unpickle_Constant (class_name , name , conversions , latex , mathml , domain ):
@@ -271,6 +271,7 @@ def unpickle_Constant(class_name, name, conversions, latex, mathml, domain):
271
271
cls = globals ()[class_name ]
272
272
return cls (name = name )
273
273
274
+
274
275
@richcmp_method
275
276
class Constant ():
276
277
def __init__ (self , name , conversions = None , latex = None , mathml = "" ,
@@ -290,8 +291,8 @@ def __init__(self, name, conversions=None, latex=None, mathml="",
290
291
self ._domain = domain
291
292
292
293
for system , value in self ._conversions .items ():
293
- setattr (self , "_%s_" % system , partial (self ._generic_interface , value ))
294
- setattr (self , "_%s_init_" % system , partial (self ._generic_interface_init , value ))
294
+ setattr (self , "_%s_" % system , partial (self ._generic_interface , value ))
295
+ setattr (self , "_%s_init_" % system , partial (self ._generic_interface_init , value ))
295
296
296
297
from .expression import PynacConstant
297
298
self ._pynac = PynacConstant (self ._name , self ._latex , self ._domain )
@@ -510,7 +511,7 @@ def _interface_(self, I):
510
511
pass
511
512
512
513
try :
513
- return getattr (self , "_%s_" % (I .name ()))(I )
514
+ return getattr (self , "_%s_" % (I .name ()))(I )
514
515
except AttributeError :
515
516
pass
516
517
@@ -529,7 +530,7 @@ def _gap_(self, gap):
529
530
sage: gap(p)
530
531
p
531
532
"""
532
- return gap ('"%s"' % self )
533
+ return gap ('"%s"' % self )
533
534
534
535
def _singular_ (self , singular ):
535
536
"""
@@ -546,7 +547,7 @@ def _singular_(self, singular):
546
547
sage: singular(p)
547
548
p
548
549
"""
549
- return singular ('"%s"' % self )
550
+ return singular ('"%s"' % self )
550
551
551
552
552
553
class Pi (Constant ):
@@ -598,7 +599,7 @@ def _real_double_(self, R):
598
599
599
600
def _sympy_ (self ):
600
601
"""
601
- Converts pi to sympy pi.
602
+ Convert pi to sympy pi.
602
603
603
604
EXAMPLES::
604
605
@@ -609,6 +610,7 @@ def _sympy_(self):
609
610
import sympy
610
611
return sympy .pi
611
612
613
+
612
614
pi = Pi ().expression ()
613
615
614
616
"""
@@ -687,12 +689,12 @@ def _sympy_(self):
687
689
# The base of the natural logarithm, e, is not a constant in GiNaC/Sage. It is
688
690
# represented by exp(1). A dummy class to make this work with arithmetic and
689
691
# coercion is implemented in the module sage.symbolic.expression for speed.
690
- from sage .symbolic .expression import E
691
692
e = E ()
692
693
693
694
# Allow for backtranslation to this symbol from Mathematica (#29833).
694
695
register_symbol (e , {'mathematica' : 'E' })
695
696
697
+
696
698
class NotANumber (Constant ):
697
699
"""
698
700
Not a Number
@@ -704,7 +706,7 @@ def __init__(self, name="NaN"):
704
706
sage: loads(dumps(NaN))
705
707
NaN
706
708
"""
707
- conversions = dict (matlab = 'NaN' )
709
+ conversions = dict (matlab = 'NaN' )
708
710
Constant .__init__ (self , name , conversions = conversions )
709
711
710
712
def __float__ (self ):
@@ -716,7 +718,7 @@ def __float__(self):
716
718
"""
717
719
return float ('nan' )
718
720
719
- def _mpfr_ (self ,R ):
721
+ def _mpfr_ (self , R ):
720
722
"""
721
723
EXAMPLES::
722
724
@@ -725,7 +727,7 @@ def _mpfr_(self,R):
725
727
sage: type(_)
726
728
<class 'sage.rings.real_mpfr.RealNumber'>
727
729
"""
728
- return R ('NaN' ) # ??? nan in mpfr: void mpfr_set_nan (mpfr_t x)
730
+ return R ('NaN' ) # ??? nan in mpfr: void mpfr_set_nan (mpfr_t x)
729
731
730
732
def _real_double_ (self , R ):
731
733
"""
@@ -751,8 +753,10 @@ def _sympy_(self):
751
753
import sympy
752
754
return sympy .nan
753
755
756
+
754
757
NaN = NotANumber ().expression ()
755
758
759
+
756
760
class GoldenRatio (Constant ):
757
761
"""
758
762
The number (1+sqrt(5))/2
@@ -806,7 +810,7 @@ def __float__(self):
806
810
sage: golden_ratio.__float__()
807
811
1.618033988749895
808
812
"""
809
- return float ( 0.5 ) * ( float ( 1.0 ) + math .sqrt (float ( 5.0 )) )
813
+ return 0.5 + math .sqrt (1.25 )
810
814
811
815
def _real_double_ (self , R ):
812
816
"""
@@ -817,7 +821,7 @@ def _real_double_(self, R):
817
821
"""
818
822
return R ('1.61803398874989484820458' )
819
823
820
- def _mpfr_ (self ,R ):
824
+ def _mpfr_ (self , R ):
821
825
"""
822
826
EXAMPLES::
823
827
@@ -826,7 +830,7 @@ def _mpfr_(self,R):
826
830
sage: RealField(100)(golden_ratio)
827
831
1.6180339887498948482045868344
828
832
"""
829
- return (R (1 )+ R (5 ).sqrt ())/ R (2 )
833
+ return (R (1 ) + R (5 ).sqrt ()) / R (2 )
830
834
831
835
def _algebraic_ (self , field ):
832
836
"""
@@ -853,8 +857,10 @@ def _sympy_(self):
853
857
import sympy
854
858
return sympy .GoldenRatio
855
859
860
+
856
861
golden_ratio = GoldenRatio ().expression ()
857
862
863
+
858
864
class Log2 (Constant ):
859
865
"""
860
866
The natural logarithm of the real number 2.
@@ -918,7 +924,7 @@ def _real_double_(self, R):
918
924
"""
919
925
return R .log2 ()
920
926
921
- def _mpfr_ (self ,R ):
927
+ def _mpfr_ (self , R ):
922
928
"""
923
929
EXAMPLES::
924
930
@@ -929,8 +935,10 @@ def _mpfr_(self,R):
929
935
"""
930
936
return R .log2 ()
931
937
938
+
932
939
log2 = Log2 ().expression ()
933
940
941
+
934
942
class EulerGamma (Constant ):
935
943
"""
936
944
The limiting difference between the harmonic series and the natural
@@ -964,7 +972,7 @@ def __init__(self, name='euler_gamma'):
964
972
Constant .__init__ (self , name , conversions = conversions ,
965
973
latex = r'\gamma' , domain = 'positive' )
966
974
967
- def _mpfr_ (self ,R ):
975
+ def _mpfr_ (self , R ):
968
976
"""
969
977
EXAMPLES::
970
978
@@ -1006,8 +1014,10 @@ def _sympy_(self):
1006
1014
import sympy
1007
1015
return sympy .EulerGamma
1008
1016
1017
+
1009
1018
euler_gamma = EulerGamma ().expression ()
1010
1019
1020
+
1011
1021
class Catalan (Constant ):
1012
1022
"""
1013
1023
A number appearing in combinatorics defined as the Dirichlet beta
@@ -1025,7 +1035,7 @@ def __init__(self, name='catalan'):
1025
1035
sage: loads(dumps(catalan))
1026
1036
catalan
1027
1037
"""
1028
- #kash: R is default prec
1038
+ # kash: R is default prec
1029
1039
conversions = dict (mathematica = 'Catalan' , kash = 'Catalan(R)' ,
1030
1040
maple = 'Catalan' , maxima = 'catalan' ,
1031
1041
pynac = 'Catalan' )
@@ -1074,8 +1084,10 @@ def _sympy_(self):
1074
1084
import sympy
1075
1085
return sympy .Catalan
1076
1086
1087
+
1077
1088
catalan = Catalan ().expression ()
1078
1089
1090
+
1079
1091
class Khinchin (Constant ):
1080
1092
"""
1081
1093
The geometric mean of the continued fraction expansion of any
@@ -1126,8 +1138,10 @@ def __float__(self):
1126
1138
"""
1127
1139
return 2.6854520010653064453097148355
1128
1140
1141
+
1129
1142
khinchin = Khinchin ().expression ()
1130
1143
1144
+
1131
1145
class TwinPrime (Constant ):
1132
1146
r"""
1133
1147
The Twin Primes constant is defined as
@@ -1173,6 +1187,7 @@ def __float__(self):
1173
1187
"""
1174
1188
return 0.66016181584686957392781211001
1175
1189
1190
+
1176
1191
twinprime = TwinPrime ().expression ()
1177
1192
1178
1193
@@ -1222,6 +1237,7 @@ def __float__(self):
1222
1237
"""
1223
1238
return 0.26149721284764278375542683861
1224
1239
1240
+
1225
1241
mertens = Mertens ().expression ()
1226
1242
1227
1243
0 commit comments