The following standard mathematical constants are defined in Sage, along with support for coercing them into GAP, GP/PARI, 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
sage: brun
brun
Support for coercion into the various systems means that if, e.g.,
you want to create 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.141592653589793238462643383 # 32-bit
3.1415926535897932384626433832795028842 # 64-bit
sage: pari(pi)
3.14159265358979
sage: kash(pi) # optional
3.14159265358979323846264338328
sage: mathematica(pi) # optional
Pi
sage: maple(pi) # optional
Pi
sage: octave(pi) # optional
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.316218116357029426750873360 # 32-bit
5.3162181163570294267508733603616328824 # 64-bit
sage: print mathematica(a) # optional
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 decimal 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()
-2*e - e^2 - 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:
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
pi + euler_gamma + catalan + golden_ratio + log2 + khinchin + twinprime + mertens + e
sage: parent(a)
Symbolic Ring
sage: RR(a)
13.2713479401972
sage: RealField(212)(a)
13.2713479401972493100988191995758139408711068200030748178329712
sage: RealField(230)(a)
13.271347940197249310098819199575813940871106820003074817832971189555
sage: CC(a)
13.2713479401972
sage: CDF(a)
13.2713479402
sage: ComplexField(230)(a)
13.271347940197249310098819199575813940871106820003074817832971189555
sage: RDF(a)
13.2713479402
Brun’s constant is the sum of reciprocals of odd twin primes.
It is not known to very high precision; calculating the number
using twin primes up to (Sebah 2002) gives the
number
.
EXAMPLES:
sage: float(brun)
...
NotImplementedError: brun is only available up to 41 bits
sage: R = RealField(41); R
Real Field with 41 bits of precision
sage: R(brun)
1.90216058310
EXAMPLES:
sage: loads(dumps(brun))
brun
A number appearing in combinatorics defined as the Dirichlet beta function evaluated at the number 2.
EXAMPLES:
sage: catalan^2 + mertens
mertens + catalan^2
EXAMPLES:
sage: float(catalan)
0.91596559417721901
EXAMPLES:
sage: loads(dumps(catalan))
catalan
EXAMPLES:
sage: RealField(100)(catalan)
0.91596559417721901505460351493
sage: catalan._mpfr_(RealField(100))
0.91596559417721901505460351493
EXAMPLES: We coerce to the real double field:
sage: RDF(catalan)
0.915965594177
Converts catalan to SymPy Catalan.
EXAMPLES:
sage: import sympy
sage: sympy.Catalan == catalan # indirect doctest
True
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
EXAMPLES:
sage: from sage.symbolic.constants import Constant
sage: p = Constant('p')
sage: loads(dumps(p))
p
Adds 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
EXAMPLES:
sage: from sage.symbolic.constants import Constant
sage: c = Constant('c')
sage: c
c
Returns 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
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
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'
EXAMPLES:
sage: from sage.symbolic.constants import Constant
sage: p = Constant('p', conversions=dict(maxima='%pi'))
sage: p._interface_(maxima)
%pi
EXAMPLES:
sage: from sage.symbolic.constants import Constant
sage: c = Constant('c', latex=r'\xi')
sage: latex(c)
\xi
EXAMPLES:
sage: from sage.symbolic.constants import Constant
sage: c = Constant('c', mathml=r'<mi>c</mi>')
sage: mathml(c)
<mi>c</mi>
Returns 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
Returns 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'
Returns 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
Returns the name of this constant.
EXAMPLES:
sage: from sage.symbolic.constants import Constant
sage: c = Constant('c')
sage: c.name()
'c'
EXAMPLES:
sage: float(e)
2.7182818284590451
sage: e.__float__()
2.7182818284590451
The base of the natural logarithm.
EXAMPLES:
sage: RR(e)
2.71828182845905
sage: R = RealField(200); R
Real Field with 200 bits of precision
sage: R(e)
2.7182818284590452353602874713526624977572470936999595749670
sage: em = 1 + e^(1-e); em
e^(-e + 1) + 1
sage: R(em)
1.1793740787340171819619895873183164984596816017589156131574
sage: maxima(e).float()
2.718281828459045
sage: t = mathematica(e) # optional
sage: t # optional
E
sage: float(t) # optional
2.7182818284590451
sage: loads(dumps(e))
e
EXAMPLES:
sage: e._mpfr_(RealField(100))
2.7182818284590452353602874714
EXAMPLES:
sage: e._real_double_(RDF)
2.71828182846
Converts e to sympy E.
EXAMPLES:
sage: import sympy
sage: sympy.E == e # indirect doctest
True
Note
For e, we don’t return a wrapper around a Pynac constant. Instead, we return exp(1) so that Pynac can perform appropiate.
EXAMPLES:
sage: e + 2
e + 2
sage: e.operator()
exp
sage: e.operands()
[1]
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
EXAMPLES:
sage: loads(dumps(euler_gamma))
euler_gamma
EXAMPLES:
sage: RealField(100)(euler_gamma)
0.57721566490153286060651209008
sage: euler_gamma._mpfr_(RealField(100))
0.57721566490153286060651209008
EXAMPLES:
sage: RDF(euler_gamma)
0.577215664902
Converts euler_gamma to SymPy EulerGamma.
EXAMPLES:
sage: import sympy
sage: sympy.EulerGamma == euler_gamma # indirect doctest
True
The Glaisher-Kinkelin constant .
EXAMPLES:
sage: float(glaisher)
1.2824271291006226
sage: glaisher.n(digits=60)
1.28242712910062263687534256886979172776768892732500119206374
sage: a = glaisher + 2
sage: a
glaisher + 2
sage: parent(a)
Symbolic Ring
EXAMPLES:
sage: loads(dumps(glaisher))
glaisher
EXAMPLES:
sage: glaisher._mpfr_(RealField(100))
1.2824271291006226368753425689
sage: RealField(100)(glaisher)
1.2824271291006226368753425689
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.2360679774997898
EXAMPLES:
sage: float(golden_ratio)
1.6180339887498949
sage: golden_ratio.__float__()
1.6180339887498949
EXAMPLES:
sage: loads(dumps(golden_ratio))
golden_ratio
EXAMPLES:
sage: golden_ratio._algebraic_(QQbar)
1.618033988749895?
sage: QQbar(golden_ratio)
1.618033988749895?
EXAMPLES:
sage: golden_ratio._mpfr_(RealField(100))
1.6180339887498948482045868344
sage: RealField(100)(golden_ratio)
1.6180339887498948482045868344
EXAMPLES:
sage: RDF(golden_ratio)
1.61803398875
Converts golden_ratio to SymPy GoldenRation.
EXAMPLES:
sage: import sympy
sage: sympy.GoldenRatio == golden_ratio # indirect doctest
True
EXAMPLES:
sage: golden_ratio.minpoly()
x^2 - x - 1
The formal square root of -1.
Warning
Note that calling pyobject() on I from within Sage does not return an instance of this class. Instead, it returns a wrapper around a number field element.
EXAMPLES:
sage: I
I
sage: I^2
-1
Note that conversions to real fields will give TypeErrors:
sage: float(I)
...
TypeError: can't convert complex to float; use abs(z)
sage: gp(I)
I
sage: RR(I)
...
TypeError: Unable to convert x (='1.00000000000000*I') to real number.
We can convert to complex fields:
sage: C = ComplexField(200); C
Complex Field with 200 bits of precision
sage: C(I)
1.0000000000000000000000000000000000000000000000000000000000*I
sage: I._complex_mpfr_field_(ComplexField(53))
1.00000000000000*I
sage: I._complex_double_(CDF)
1.0*I
sage: CDF(I)
1.0*I
sage: z = I + I; z
2*I
sage: C(z)
2.0000000000000000000000000000000000000000000000000000000000*I
sage: 1e8*I
1.00000000000000e8*I
sage: complex(I)
1j
sage: QQbar(I)
1*I
sage: abs(I)
1
sage: I.minpoly()
x^2 + 1
sage: maxima(2*I)
2*%i
TESTS:
sage: repr(I)
'I'
sage: latex(I)
I
Returns an Expression for I. If constant is True, then it returns a wrapper around a Pynac constant. If constant is False, then it returns a wrapper around a NumberFieldElement.
EXAMPLES:
sage: from sage.symbolic.constants import I_class
sage: a = I_class()
sage: I_constant = a.expression(constant=True)
sage: type(I_constant.pyobject())
<class 'sage.symbolic.constants.I_class'>
sage: I_nf = a.expression()
sage: type(I_nf.pyobject())
<type 'sage.rings.number_field.number_field_element_quadratic.NumberFieldElement_quadratic'>
The geometric mean of the continued fraction expansion of any (almost any) real number.
EXAMPLES:
sage: float(khinchin)
2.6854520010653062
sage: khinchin.n(digits=60)
2.68545200106530644530971483548179569382038229399446295305115
sage: m = mathematica(khinchin); m # optional
Khinchin
sage: m.N(200) # optional
2.68545200106530644530971483548179569382038229399446295305115234555721885953715200280114117493184769799515346590528809008289767771641096305179253348325966838185231542133211949962603932852204481940961807 # 32-bit
2.6854520010653064453097148354817956938203822939944629530511523455572188595371520028011411749318476979951534659052880900828976777164109630517925334832596683818523154213321194996260393285220448194096181 # 64-bit
EXAMPLES:
sage: loads(dumps(khinchin))
khinchin
EXAMPLES:
sage: khinchin._mpfr_(RealField(100))
2.6854520010653064453097148355
sage: RealField(100)(khinchin)
2.6854520010653064453097148355
EXAMPLES:
sage: from sage.symbolic.constants import LimitedPrecisionConstant
sage: a = LimitedPrecisionConstant('a', '1.234567891011121314').expression()
sage: float(a)
1.2345678910111213
A class for constants that are only known to a limited precision.
EXAMPLES:
sage: from sage.symbolic.constants import LimitedPrecisionConstant
sage: a = LimitedPrecisionConstant('a', '1.234567891011121314').expression(); a
a
sage: RDF(a)
1.23456789101
sage: RealField(200)(a)
...
NotImplementedError: a is only available up to 59 bits
EXAMPLES:
sage: RealField(41)(brun)
1.90216058310
sage: RealField(20000)(brun)
...
NotImplementedError: brun is only available up to 41 bits
EXAMPLES:
sage: from sage.symbolic.constants import LimitedPrecisionConstant
sage: a = LimitedPrecisionConstant('a', '1.234567891011121314').expression()
sage: RDF(a)
1.23456789101
The natural logarithm of the real number 2.
EXAMPLES:
sage: log2
log2
sage: float(log2)
0.69314718055994529
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()
.6931471805599453
sage: gp(log2)
0.6931471805599453094172321215 # 32-bit
0.69314718055994530941723212145817656807 # 64-bit
EXAMPLES:
sage: float(log2)
0.69314718055994529
sage: log2.__float__()
0.69314718055994529
EXAMPLES:
sage: loads(dumps(log2))
log2
EXAMPLES:
sage: RealField(100)(log2)
0.69314718055994530941723212146
sage: log2._mpfr_(RealField(100))
0.69314718055994530941723212146
EXAMPLES:
sage: RDF(log2)
0.69314718056
The Mertens constant is related to the Twin Primes constant and appears in Mertens’ second theorem.
EXAMPLES:
sage: float(mertens)
0.26149721284764277
sage: mertens.n(digits=60)
0.261497212847642783755426838608695859051566648261199206192064
EXAMPLES:
sage: loads(dumps(mertens))
mertens
EXAMPLES:
sage: mertens._mpfr_(RealField(100))
0.26149721284764278375542683861
sage: RealField(100)(mertens)
0.26149721284764278375542683861
Not a Number
EXAMPLES:
sage: loads(dumps(NaN))
NaN
EXAMPLES:
sage: NaN._mpfr_(RealField(53))
NaN
sage: type(_)
<type 'sage.rings.real_mpfr.RealNumber'>
EXAMPLES:
sage: RDF(NaN)
NaN
Converts NaN to SymPy NaN.
EXAMPLES:
sage: import sympy
sage: sympy.nan == NaN # indirect doctest
True
EXAMPLES:
sage: float(pi)
3.1415926535897931
TESTS:
sage: pi._latex_()
'\pi'
sage: latex(pi)
\pi
sage: mathml(pi)
<mi>π</mi>
EXAMPLES:
sage: pi._mpfr_(RealField(100))
3.1415926535897932384626433833
EXAMPLES:
sage: pi._real_double_(RDF)
3.14159265359
Converts pi to sympy pi.
EXAMPLES:
sage: import sympy
sage: sympy.pi == pi # indirect doctest
True
The Twin Primes constant is defined as
for primes
.
EXAMPLES:
sage: float(twinprime)
0.66016181584686962
sage: twinprime.n(digits=60)
0.660161815846869573927812110014555778432623360284733413319448
EXAMPLES:
sage: loads(dumps(twinprime))
twinprime
EXAMPLES:
sage: twinprime._mpfr_(RealField(100))
0.66016181584686957392781211001
sage: RealField(100)(twinprime)
0.66016181584686957392781211001
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'