| 1 | """ |
|---|
| 2 | Mathematical constants |
|---|
| 3 | |
|---|
| 4 | The following standard mathematical constants are defined in Sage, |
|---|
| 5 | along with support for coercing them into GAP, GP/PARI, KASH, |
|---|
| 6 | Maxima, Mathematica, Maple, Octave, and Singular:: |
|---|
| 7 | |
|---|
| 8 | sage: pi |
|---|
| 9 | pi |
|---|
| 10 | sage: e # base of the natural logarithm |
|---|
| 11 | e |
|---|
| 12 | sage: NaN # Not a number |
|---|
| 13 | NaN |
|---|
| 14 | sage: golden_ratio |
|---|
| 15 | golden_ratio |
|---|
| 16 | sage: log2 # natural logarithm of the real number 2 |
|---|
| 17 | log2 |
|---|
| 18 | sage: euler_gamma # Euler's gamma constant |
|---|
| 19 | euler_gamma |
|---|
| 20 | sage: catalan # the Catalon constant |
|---|
| 21 | catalan |
|---|
| 22 | sage: khinchin # Khinchin's constant |
|---|
| 23 | khinchin |
|---|
| 24 | sage: twinprime |
|---|
| 25 | twinprime |
|---|
| 26 | sage: merten |
|---|
| 27 | merten |
|---|
| 28 | sage: brun |
|---|
| 29 | brun |
|---|
| 30 | |
|---|
| 31 | Support for coercion into the various systems means that if, e.g., |
|---|
| 32 | you want to create `\pi` in Maxima and Singular, you don't |
|---|
| 33 | have to figure out the special notation for each system. You just |
|---|
| 34 | type the following:: |
|---|
| 35 | |
|---|
| 36 | sage: maxima(pi) |
|---|
| 37 | %pi |
|---|
| 38 | sage: singular(pi) |
|---|
| 39 | pi |
|---|
| 40 | sage: gap(pi) |
|---|
| 41 | pi |
|---|
| 42 | sage: gp(pi) |
|---|
| 43 | 3.141592653589793238462643383 # 32-bit |
|---|
| 44 | 3.1415926535897932384626433832795028842 # 64-bit |
|---|
| 45 | sage: pari(pi) |
|---|
| 46 | 3.14159265358979 |
|---|
| 47 | sage: kash(pi) # optional |
|---|
| 48 | 3.14159265358979323846264338328 |
|---|
| 49 | sage: mathematica(pi) # optional |
|---|
| 50 | Pi |
|---|
| 51 | sage: maple(pi) # optional |
|---|
| 52 | Pi |
|---|
| 53 | sage: octave(pi) # optional |
|---|
| 54 | 3.14159 |
|---|
| 55 | |
|---|
| 56 | Arithmetic operations with constants also yield constants, which |
|---|
| 57 | can be coerced into other systems or evaluated. |
|---|
| 58 | |
|---|
| 59 | :: |
|---|
| 60 | |
|---|
| 61 | sage: a = pi + e*4/5; a |
|---|
| 62 | pi + 4/5*e |
|---|
| 63 | sage: maxima(a) |
|---|
| 64 | %pi+4*%e/5 |
|---|
| 65 | sage: RealField(15)(a) # 15 *bits* of precision |
|---|
| 66 | 5.316 |
|---|
| 67 | sage: gp(a) |
|---|
| 68 | 5.316218116357029426750873360 # 32-bit |
|---|
| 69 | 5.3162181163570294267508733603616328824 # 64-bit |
|---|
| 70 | sage: print mathematica(a) # optional |
|---|
| 71 | 4 E |
|---|
| 72 | --- + Pi |
|---|
| 73 | 5 |
|---|
| 74 | |
|---|
| 75 | EXAMPLES: Decimal expansions of constants |
|---|
| 76 | |
|---|
| 77 | We can obtain floating point approximations to each of these |
|---|
| 78 | constants by coercing into the real field with given precision. For |
|---|
| 79 | example, to 200 decimal places we have the following:: |
|---|
| 80 | |
|---|
| 81 | sage: R = RealField(200); R |
|---|
| 82 | Real Field with 200 bits of precision |
|---|
| 83 | |
|---|
| 84 | :: |
|---|
| 85 | |
|---|
| 86 | sage: R(pi) |
|---|
| 87 | 3.1415926535897932384626433832795028841971693993751058209749 |
|---|
| 88 | |
|---|
| 89 | :: |
|---|
| 90 | |
|---|
| 91 | sage: R(e) |
|---|
| 92 | 2.7182818284590452353602874713526624977572470936999595749670 |
|---|
| 93 | |
|---|
| 94 | :: |
|---|
| 95 | |
|---|
| 96 | sage: R(NaN) |
|---|
| 97 | NaN |
|---|
| 98 | |
|---|
| 99 | :: |
|---|
| 100 | |
|---|
| 101 | sage: R(golden_ratio) |
|---|
| 102 | 1.6180339887498948482045868343656381177203091798057628621354 |
|---|
| 103 | |
|---|
| 104 | :: |
|---|
| 105 | |
|---|
| 106 | sage: R(log2) |
|---|
| 107 | 0.69314718055994530941723212145817656807550013436025525412068 |
|---|
| 108 | |
|---|
| 109 | :: |
|---|
| 110 | |
|---|
| 111 | sage: R(euler_gamma) |
|---|
| 112 | 0.57721566490153286060651209008240243104215933593992359880577 |
|---|
| 113 | |
|---|
| 114 | :: |
|---|
| 115 | |
|---|
| 116 | sage: R(catalan) |
|---|
| 117 | 0.91596559417721901505460351493238411077414937428167213426650 |
|---|
| 118 | |
|---|
| 119 | :: |
|---|
| 120 | |
|---|
| 121 | sage: R(khinchin) |
|---|
| 122 | 2.6854520010653064453097148354817956938203822939944629530512 |
|---|
| 123 | |
|---|
| 124 | EXAMPLES: Arithmetic with constants |
|---|
| 125 | |
|---|
| 126 | :: |
|---|
| 127 | |
|---|
| 128 | sage: f = I*(e+1); f |
|---|
| 129 | I*e + I |
|---|
| 130 | sage: f^2 |
|---|
| 131 | (I*e + I)^2 |
|---|
| 132 | sage: _.expand() |
|---|
| 133 | -2*e - e^2 - 1 |
|---|
| 134 | |
|---|
| 135 | :: |
|---|
| 136 | |
|---|
| 137 | sage: pp = pi+pi; pp |
|---|
| 138 | 2*pi |
|---|
| 139 | sage: R(pp) |
|---|
| 140 | 6.2831853071795864769252867665590057683943387987502116419499 |
|---|
| 141 | |
|---|
| 142 | :: |
|---|
| 143 | |
|---|
| 144 | sage: s = (1 + e^pi); s |
|---|
| 145 | e^pi + 1 |
|---|
| 146 | sage: R(s) |
|---|
| 147 | 24.140692632779269005729086367948547380266106242600211993445 |
|---|
| 148 | sage: R(s-1) |
|---|
| 149 | 23.140692632779269005729086367948547380266106242600211993445 |
|---|
| 150 | |
|---|
| 151 | :: |
|---|
| 152 | |
|---|
| 153 | sage: l = (1-log2)/(1+log2); l |
|---|
| 154 | -(log2 - 1)/(log2 + 1) |
|---|
| 155 | sage: R(l) |
|---|
| 156 | 0.18123221829928249948761381864650311423330609774776013488056 |
|---|
| 157 | |
|---|
| 158 | :: |
|---|
| 159 | |
|---|
| 160 | sage: pim = maxima(pi) |
|---|
| 161 | sage: maxima.eval('fpprec : 100') |
|---|
| 162 | '100' |
|---|
| 163 | sage: pim.bfloat() |
|---|
| 164 | 3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117068b0 |
|---|
| 165 | |
|---|
| 166 | AUTHORS: |
|---|
| 167 | |
|---|
| 168 | - Alex Clemesha (2006-01-15) |
|---|
| 169 | |
|---|
| 170 | - William Stein |
|---|
| 171 | |
|---|
| 172 | - Alex Clemesha, William Stein (2006-02-20): added new constants; |
|---|
| 173 | removed todos |
|---|
| 174 | |
|---|
| 175 | - Didier Deshommes (2007-03-27): added constants from RQDF (deprecated) |
|---|
| 176 | |
|---|
| 177 | TESTS: |
|---|
| 178 | |
|---|
| 179 | Coercing the sum of a bunch of the constants to many different |
|---|
| 180 | floating point rings:: |
|---|
| 181 | |
|---|
| 182 | sage: a = pi + e + golden_ratio + log2 + euler_gamma + catalan + khinchin + twinprime + merten; a |
|---|
| 183 | pi + euler_gamma + catalan + golden_ratio + log2 + khinchin + twinprime + merten + e |
|---|
| 184 | sage: parent(a) |
|---|
| 185 | Symbolic Ring |
|---|
| 186 | sage: RR(a) |
|---|
| 187 | 13.2713479401972 |
|---|
| 188 | sage: RealField(212)(a) |
|---|
| 189 | 13.2713479401972493100988191995758139408711068200030748178329712 |
|---|
| 190 | sage: RealField(230)(a) |
|---|
| 191 | 13.271347940197249310098819199575813940871106820003074817832971189555 |
|---|
| 192 | sage: CC(a) |
|---|
| 193 | 13.2713479401972 |
|---|
| 194 | sage: CDF(a) |
|---|
| 195 | 13.2713479402 |
|---|
| 196 | sage: ComplexField(230)(a) |
|---|
| 197 | 13.271347940197249310098819199575813940871106820003074817832971189555 |
|---|
| 198 | sage: RDF(a) |
|---|
| 199 | 13.2713479402 |
|---|
| 200 | """ |
|---|
| 201 | ############################################################################### |
|---|
| 202 | # Sage: Open Source Mathematical Software |
|---|
| 203 | # Copyright (C) 2008 William Stein <wstein@gmail.com> |
|---|
| 204 | # Copyright (C) 2008 Burcin Erocal <burcin@erocal.org> |
|---|
| 205 | # 2009 Mike Hansen <mhansen@gmail.com> |
|---|
| 206 | # Distributed under the terms of the GNU General Public License (GPL), |
|---|
| 207 | # version 2 or any later version. The full text of the GPL is available at: |
|---|
| 208 | # http://www.gnu.org/licenses/ |
|---|
| 209 | ############################################################################### |
|---|
| 210 | import math |
|---|
| 211 | from functools import partial |
|---|
| 212 | from sage.rings.infinity import (infinity, minus_infinity, |
|---|
| 213 | unsigned_infinity) |
|---|
| 214 | |
|---|
| 215 | constants_table = {} |
|---|
| 216 | constants_name_table = {} |
|---|
| 217 | constants_name_table[repr(infinity)] = infinity |
|---|
| 218 | constants_name_table[repr(unsigned_infinity)] = unsigned_infinity |
|---|
| 219 | constants_name_table[repr(minus_infinity)] = minus_infinity |
|---|
| 220 | |
|---|
| 221 | def unpickle_Constant(class_name, name, conversions, latex, mathml, domain): |
|---|
| 222 | """ |
|---|
| 223 | EXAMPLES:: |
|---|
| 224 | |
|---|
| 225 | sage: from sage.symbolic.constants import unpickle_Constant |
|---|
| 226 | sage: a = unpickle_Constant('Constant', 'a', {}, 'aa', '', 'positive') |
|---|
| 227 | sage: a.domain() |
|---|
| 228 | 'positive' |
|---|
| 229 | sage: latex(a) |
|---|
| 230 | aa |
|---|
| 231 | |
|---|
| 232 | Note that if the name already appears in the |
|---|
| 233 | ``constants_name_table``, then that will be returned instead of |
|---|
| 234 | constructing a new object:: |
|---|
| 235 | |
|---|
| 236 | sage: pi = unpickle_Constant('Pi', 'pi', None, None, None, None) |
|---|
| 237 | sage: pi._maxima_init_() |
|---|
| 238 | '%pi' |
|---|
| 239 | """ |
|---|
| 240 | if name in constants_name_table: |
|---|
| 241 | return constants_name_table[name] |
|---|
| 242 | if class_name == "Constant": |
|---|
| 243 | return Constant(name, conversions=conversions, latex=latex, |
|---|
| 244 | mathml=mathml, domain=domain) |
|---|
| 245 | else: |
|---|
| 246 | cls = globals()[class_name] |
|---|
| 247 | return cls(name=name) |
|---|
| 248 | |
|---|
| 249 | class Constant(object): |
|---|
| 250 | def __init__(self, name, conversions=None, latex=None, mathml="", |
|---|
| 251 | domain='complex'): |
|---|
| 252 | """ |
|---|
| 253 | EXAMPLES:: |
|---|
| 254 | |
|---|
| 255 | sage: from sage.symbolic.constants import Constant |
|---|
| 256 | sage: p = Constant('p') |
|---|
| 257 | sage: loads(dumps(p)) |
|---|
| 258 | p |
|---|
| 259 | """ |
|---|
| 260 | self._conversions = conversions if conversions is not None else {} |
|---|
| 261 | self._latex = latex if latex is not None else name |
|---|
| 262 | self._mathml = mathml |
|---|
| 263 | self._name = name |
|---|
| 264 | self._domain = domain |
|---|
| 265 | |
|---|
| 266 | for system, value in self._conversions.items(): |
|---|
| 267 | setattr(self, "_%s_"%system, partial(self._generic_interface, value)) |
|---|
| 268 | setattr(self, "_%s_init_"%system, partial(self._generic_interface_init, value)) |
|---|
| 269 | |
|---|
| 270 | from sage.symbolic.constants_c import PynacConstant |
|---|
| 271 | self._pynac = PynacConstant(self._name, self._latex, self._domain) |
|---|
| 272 | self._serial = self._pynac.serial() |
|---|
| 273 | constants_table[self._serial] = self |
|---|
| 274 | constants_name_table[self._name] = self |
|---|
| 275 | |
|---|
| 276 | from sage.symbolic.pynac import register_symbol |
|---|
| 277 | register_symbol(self.expression(), self._conversions) |
|---|
| 278 | |
|---|
| 279 | def __reduce__(self): |
|---|
| 280 | """ |
|---|
| 281 | Adds support for pickling constants. |
|---|
| 282 | |
|---|
| 283 | EXAMPLES:: |
|---|
| 284 | |
|---|
| 285 | sage: from sage.symbolic.constants import Constant |
|---|
| 286 | sage: p = Constant('p') |
|---|
| 287 | sage: p.__reduce__() |
|---|
| 288 | (<function unpickle_Constant at 0x...>, |
|---|
| 289 | ('Constant', 'p', {}, 'p', '', 'complex')) |
|---|
| 290 | sage: loads(dumps(p)) |
|---|
| 291 | p |
|---|
| 292 | |
|---|
| 293 | sage: pi.pyobject().__reduce__() |
|---|
| 294 | (<function unpickle_Constant at 0x...>, |
|---|
| 295 | ('Pi', |
|---|
| 296 | 'pi', |
|---|
| 297 | ..., |
|---|
| 298 | '\\pi', |
|---|
| 299 | '<mi>π</mi>', |
|---|
| 300 | 'positive')) |
|---|
| 301 | sage: loads(dumps(pi.pyobject())) |
|---|
| 302 | pi |
|---|
| 303 | |
|---|
| 304 | """ |
|---|
| 305 | return (unpickle_Constant, (self.__class__.__name__, self._name, |
|---|
| 306 | self._conversions, self._latex, |
|---|
| 307 | self._mathml, self._domain)) |
|---|
| 308 | |
|---|
| 309 | |
|---|
| 310 | def domain(self): |
|---|
| 311 | """ |
|---|
| 312 | Returns the domain of this constant. This is either positive, |
|---|
| 313 | real, or complex, and is used by Pynac to make inferences |
|---|
| 314 | about expressions containing this constant. |
|---|
| 315 | |
|---|
| 316 | EXAMPLES:: |
|---|
| 317 | |
|---|
| 318 | sage: p = pi.pyobject(); p |
|---|
| 319 | pi |
|---|
| 320 | sage: type(_) |
|---|
| 321 | <class 'sage.symbolic.constants.Pi'> |
|---|
| 322 | sage: p.domain() |
|---|
| 323 | 'positive' |
|---|
| 324 | """ |
|---|
| 325 | return self._domain |
|---|
| 326 | |
|---|
| 327 | def expression(self): |
|---|
| 328 | """ |
|---|
| 329 | Returns an expression for this constant. |
|---|
| 330 | |
|---|
| 331 | EXAMPLES:: |
|---|
| 332 | |
|---|
| 333 | sage: a = pi.pyobject() |
|---|
| 334 | sage: pi2 = a.expression() |
|---|
| 335 | sage: pi2 |
|---|
| 336 | pi |
|---|
| 337 | sage: pi2 + 2 |
|---|
| 338 | pi + 2 |
|---|
| 339 | sage: pi - pi2 |
|---|
| 340 | 0 |
|---|
| 341 | """ |
|---|
| 342 | return self._pynac.expression() |
|---|
| 343 | |
|---|
| 344 | def name(self): |
|---|
| 345 | """ |
|---|
| 346 | Returns the name of this constant. |
|---|
| 347 | |
|---|
| 348 | EXAMPLES:: |
|---|
| 349 | |
|---|
| 350 | sage: from sage.symbolic.constants import Constant |
|---|
| 351 | sage: c = Constant('c') |
|---|
| 352 | sage: c.name() |
|---|
| 353 | 'c' |
|---|
| 354 | """ |
|---|
| 355 | return self._name |
|---|
| 356 | |
|---|
| 357 | def __repr__(self): |
|---|
| 358 | """ |
|---|
| 359 | EXAMPLES:: |
|---|
| 360 | |
|---|
| 361 | sage: from sage.symbolic.constants import Constant |
|---|
| 362 | sage: c = Constant('c') |
|---|
| 363 | sage: c |
|---|
| 364 | c |
|---|
| 365 | """ |
|---|
| 366 | return self._name |
|---|
| 367 | |
|---|
| 368 | def _latex_(self): |
|---|
| 369 | r""" |
|---|
| 370 | EXAMPLES:: |
|---|
| 371 | |
|---|
| 372 | sage: from sage.symbolic.constants import Constant |
|---|
| 373 | sage: c = Constant('c', latex=r'\xi') |
|---|
| 374 | sage: latex(c) |
|---|
| 375 | \xi |
|---|
| 376 | """ |
|---|
| 377 | return self._latex |
|---|
| 378 | |
|---|
| 379 | def _mathml_(self): |
|---|
| 380 | """ |
|---|
| 381 | EXAMPLES:: |
|---|
| 382 | |
|---|
| 383 | sage: from sage.symbolic.constants import Constant |
|---|
| 384 | sage: c = Constant('c', mathml=r'<mi>c</mi>') |
|---|
| 385 | sage: mathml(c) |
|---|
| 386 | <mi>c</mi> |
|---|
| 387 | """ |
|---|
| 388 | return self._mathml |
|---|
| 389 | |
|---|
| 390 | def _generic_interface(self, value, I): |
|---|
| 391 | """ |
|---|
| 392 | This is a helper method used in defining the ``_X_`` methods |
|---|
| 393 | where ``X`` is the name of some interface. |
|---|
| 394 | |
|---|
| 395 | EXAMPLES:: |
|---|
| 396 | |
|---|
| 397 | sage: from sage.symbolic.constants import Constant |
|---|
| 398 | sage: p = Constant('p', conversions=dict(maxima='%pi')) |
|---|
| 399 | sage: p._maxima_(maxima) |
|---|
| 400 | %pi |
|---|
| 401 | |
|---|
| 402 | The above ``_maxima_`` is constructed like ``m`` below:: |
|---|
| 403 | |
|---|
| 404 | sage: from functools import partial |
|---|
| 405 | sage: m = partial(p._generic_interface, '%pi') |
|---|
| 406 | sage: m(maxima) |
|---|
| 407 | %pi |
|---|
| 408 | |
|---|
| 409 | """ |
|---|
| 410 | return I(value) |
|---|
| 411 | |
|---|
| 412 | def _generic_interface_init(self, value): |
|---|
| 413 | """ |
|---|
| 414 | This is a helper method used in defining the ``_X_init_`` methods |
|---|
| 415 | where ``X`` is the name of some interface. |
|---|
| 416 | |
|---|
| 417 | EXAMPLES:: |
|---|
| 418 | |
|---|
| 419 | sage: from sage.symbolic.constants import Constant |
|---|
| 420 | sage: p = Constant('p', conversions=dict(maxima='%pi')) |
|---|
| 421 | sage: p._maxima_init_() |
|---|
| 422 | '%pi' |
|---|
| 423 | |
|---|
| 424 | The above ``_maxima_init_`` is constructed like ``mi`` below:: |
|---|
| 425 | |
|---|
| 426 | sage: from functools import partial |
|---|
| 427 | sage: mi = partial(p._generic_interface_init, '%pi') |
|---|
| 428 | sage: mi() |
|---|
| 429 | '%pi' |
|---|
| 430 | |
|---|
| 431 | """ |
|---|
| 432 | return value |
|---|
| 433 | |
|---|
| 434 | def _interface_(self, I): |
|---|
| 435 | """ |
|---|
| 436 | EXAMPLES:: |
|---|
| 437 | |
|---|
| 438 | sage: from sage.symbolic.constants import Constant |
|---|
| 439 | sage: p = Constant('p', conversions=dict(maxima='%pi')) |
|---|
| 440 | sage: p._interface_(maxima) |
|---|
| 441 | %pi |
|---|
| 442 | """ |
|---|
| 443 | try: |
|---|
| 444 | s = self._conversions[I.name()] |
|---|
| 445 | return I(s) |
|---|
| 446 | except KeyError: |
|---|
| 447 | pass |
|---|
| 448 | |
|---|
| 449 | try: |
|---|
| 450 | return getattr(self, "_%s_"%(I.name()))(I) |
|---|
| 451 | except AttributeError: |
|---|
| 452 | pass |
|---|
| 453 | |
|---|
| 454 | raise NotImplementedError |
|---|
| 455 | |
|---|
| 456 | def _gap_(self, gap): |
|---|
| 457 | """ |
|---|
| 458 | Returns the constant as a string in GAP. Since GAP does not |
|---|
| 459 | have floating point numbers, we simply return the constant as |
|---|
| 460 | a string. |
|---|
| 461 | |
|---|
| 462 | EXAMPLES:: |
|---|
| 463 | |
|---|
| 464 | sage: from sage.symbolic.constants import Constant |
|---|
| 465 | sage: p = Constant('p') |
|---|
| 466 | sage: gap(p) |
|---|
| 467 | p |
|---|
| 468 | """ |
|---|
| 469 | return gap('"%s"'%self) |
|---|
| 470 | |
|---|
| 471 | def _singular_(self, singular): |
|---|
| 472 | """ |
|---|
| 473 | Returns the constant as a string in Singular. Since Singular |
|---|
| 474 | does not have floating point numbers, we simply return the |
|---|
| 475 | constant as a string. |
|---|
| 476 | |
|---|
| 477 | EXAMPLES:: |
|---|
| 478 | |
|---|
| 479 | sage: from sage.symbolic.constants import Constant |
|---|
| 480 | sage: p = Constant('p') |
|---|
| 481 | sage: singular(p) |
|---|
| 482 | p |
|---|
| 483 | """ |
|---|
| 484 | return singular('"%s"'%self) |
|---|
| 485 | |
|---|
| 486 | |
|---|
| 487 | class Pi(Constant): |
|---|
| 488 | def __init__(self, name="pi"): |
|---|
| 489 | """ |
|---|
| 490 | TESTS:: |
|---|
| 491 | |
|---|
| 492 | sage: pi._latex_() |
|---|
| 493 | '\\pi' |
|---|
| 494 | sage: latex(pi) |
|---|
| 495 | \pi |
|---|
| 496 | sage: mathml(pi) |
|---|
| 497 | <mi>π</mi> |
|---|
| 498 | |
|---|
| 499 | """ |
|---|
| 500 | conversions = dict(axiom='%pi', maxima='%pi', gp='Pi', kash='PI', |
|---|
| 501 | mathematica='Pi', matlab='pi', maple='pi', |
|---|
| 502 | octave='pi', pari='Pi', pynac='Pi') |
|---|
| 503 | Constant.__init__(self, name, conversions=conversions, |
|---|
| 504 | latex=r"\pi", mathml="<mi>π</mi>", |
|---|
| 505 | domain='positive') |
|---|
| 506 | |
|---|
| 507 | def __float__(self): |
|---|
| 508 | """ |
|---|
| 509 | EXAMPLES:: |
|---|
| 510 | |
|---|
| 511 | sage: float(pi) |
|---|
| 512 | 3.1415926535897931 |
|---|
| 513 | """ |
|---|
| 514 | return math.pi |
|---|
| 515 | |
|---|
| 516 | def _mpfr_(self, R): |
|---|
| 517 | """ |
|---|
| 518 | EXAMPLES:: |
|---|
| 519 | |
|---|
| 520 | sage: pi._mpfr_(RealField(100)) |
|---|
| 521 | 3.1415926535897932384626433833 |
|---|
| 522 | """ |
|---|
| 523 | return R.pi() |
|---|
| 524 | |
|---|
| 525 | def _real_double_(self, R): |
|---|
| 526 | """ |
|---|
| 527 | EXAMPLES:: |
|---|
| 528 | |
|---|
| 529 | sage: pi._real_double_(RDF) |
|---|
| 530 | 3.14159265359 |
|---|
| 531 | """ |
|---|
| 532 | return R.pi() |
|---|
| 533 | |
|---|
| 534 | def _sympy_(self): |
|---|
| 535 | """ |
|---|
| 536 | Converts pi to sympy pi. |
|---|
| 537 | |
|---|
| 538 | EXAMPLES:: |
|---|
| 539 | |
|---|
| 540 | sage: import sympy |
|---|
| 541 | sage: sympy.pi == pi # indirect doctest |
|---|
| 542 | True |
|---|
| 543 | """ |
|---|
| 544 | import sympy |
|---|
| 545 | return sympy.pi |
|---|
| 546 | |
|---|
| 547 | pi = Pi().expression() |
|---|
| 548 | |
|---|
| 549 | class I_class(Constant): |
|---|
| 550 | def __init__(self, name="I"): |
|---|
| 551 | """ |
|---|
| 552 | The formal square root of -1. |
|---|
| 553 | |
|---|
| 554 | .. warning:: |
|---|
| 555 | |
|---|
| 556 | Note that calling :meth:`pyobject` on ``I`` from within |
|---|
| 557 | Sage does not return an instance of this class. Instead, |
|---|
| 558 | it returns a wrapper around a number field element. |
|---|
| 559 | |
|---|
| 560 | EXAMPLES:: |
|---|
| 561 | |
|---|
| 562 | sage: I |
|---|
| 563 | I |
|---|
| 564 | sage: I^2 |
|---|
| 565 | -1 |
|---|
| 566 | |
|---|
| 567 | Note that conversions to real fields will give TypeErrors:: |
|---|
| 568 | |
|---|
| 569 | sage: float(I) |
|---|
| 570 | Traceback (most recent call last): |
|---|
| 571 | ... |
|---|
| 572 | TypeError: can't convert complex to float; use abs(z) |
|---|
| 573 | sage: gp(I) |
|---|
| 574 | I |
|---|
| 575 | sage: RR(I) |
|---|
| 576 | Traceback (most recent call last): |
|---|
| 577 | ... |
|---|
| 578 | TypeError: Unable to convert x (='1.00000000000000*I') to real number. |
|---|
| 579 | |
|---|
| 580 | We can convert to complex fields:: |
|---|
| 581 | |
|---|
| 582 | sage: C = ComplexField(200); C |
|---|
| 583 | Complex Field with 200 bits of precision |
|---|
| 584 | sage: C(I) |
|---|
| 585 | 1.0000000000000000000000000000000000000000000000000000000000*I |
|---|
| 586 | sage: I._complex_mpfr_field_(ComplexField(53)) |
|---|
| 587 | 1.00000000000000*I |
|---|
| 588 | |
|---|
| 589 | sage: I._complex_double_(CDF) |
|---|
| 590 | 1.0*I |
|---|
| 591 | sage: CDF(I) |
|---|
| 592 | 1.0*I |
|---|
| 593 | |
|---|
| 594 | sage: z = I + I; z |
|---|
| 595 | 2*I |
|---|
| 596 | sage: C(z) |
|---|
| 597 | 2.0000000000000000000000000000000000000000000000000000000000*I |
|---|
| 598 | sage: 1e8*I |
|---|
| 599 | 1.00000000000000e8*I |
|---|
| 600 | |
|---|
| 601 | sage: complex(I) |
|---|
| 602 | 1j |
|---|
| 603 | |
|---|
| 604 | sage: QQbar(I) |
|---|
| 605 | 1*I |
|---|
| 606 | |
|---|
| 607 | sage: abs(I) |
|---|
| 608 | 1 |
|---|
| 609 | |
|---|
| 610 | sage: I.minpoly() |
|---|
| 611 | x^2 + 1 |
|---|
| 612 | sage: maxima(2*I) |
|---|
| 613 | 2*%i |
|---|
| 614 | |
|---|
| 615 | TESTS:: |
|---|
| 616 | |
|---|
| 617 | sage: repr(I) |
|---|
| 618 | 'I' |
|---|
| 619 | sage: latex(I) |
|---|
| 620 | I |
|---|
| 621 | """ |
|---|
| 622 | conversions = dict(axiom='%i', maxima='%i', gp='I', |
|---|
| 623 | mathematica='I', matlab='i',maple='I', |
|---|
| 624 | octave='i', pari='I') |
|---|
| 625 | Constant.__init__(self, name, conversions=conversions, |
|---|
| 626 | latex='i', mathml="<mi>&i;</mi>") |
|---|
| 627 | |
|---|
| 628 | |
|---|
| 629 | def expression(self, constant=False): |
|---|
| 630 | """ |
|---|
| 631 | Returns an Expression for I. If *constnat* is True, then it |
|---|
| 632 | returns a wrapper around a Pynac constant. If *constant* is |
|---|
| 633 | False, then it returns a wrapper around a NumberFieldElement. |
|---|
| 634 | |
|---|
| 635 | EXAMPLES:: |
|---|
| 636 | |
|---|
| 637 | sage: from sage.symbolic.constants import I_class |
|---|
| 638 | sage: a = I_class() |
|---|
| 639 | sage: I_constant = a.expression(constant=True) |
|---|
| 640 | sage: type(I_constant.pyobject()) |
|---|
| 641 | <class 'sage.symbolic.constants.I_class'> |
|---|
| 642 | sage: I_nf = a.expression() |
|---|
| 643 | sage: type(I_nf.pyobject()) |
|---|
| 644 | <type 'sage.rings.number_field.number_field_element_quadratic.NumberFieldElement_quadratic'> |
|---|
| 645 | """ |
|---|
| 646 | if constant: |
|---|
| 647 | return Constant.expression(self) |
|---|
| 648 | else: |
|---|
| 649 | from sage.symbolic.pynac import I |
|---|
| 650 | return I |
|---|
| 651 | |
|---|
| 652 | a = I_class() |
|---|
| 653 | I_constant = a.expression(constant=True) |
|---|
| 654 | i = I = a.expression(constant=False) |
|---|
| 655 | |
|---|
| 656 | class E(Constant): |
|---|
| 657 | def __init__(self, name="e"): |
|---|
| 658 | """ |
|---|
| 659 | The base of the natural logarithm. |
|---|
| 660 | |
|---|
| 661 | EXAMPLES:: |
|---|
| 662 | |
|---|
| 663 | sage: RR(e) |
|---|
| 664 | 2.71828182845905 |
|---|
| 665 | sage: R = RealField(200); R |
|---|
| 666 | Real Field with 200 bits of precision |
|---|
| 667 | sage: R(e) |
|---|
| 668 | 2.7182818284590452353602874713526624977572470936999595749670 |
|---|
| 669 | sage: em = 1 + e^(1-e); em |
|---|
| 670 | e^(-e + 1) + 1 |
|---|
| 671 | sage: R(em) |
|---|
| 672 | 1.1793740787340171819619895873183164984596816017589156131574 |
|---|
| 673 | sage: maxima(e).float() |
|---|
| 674 | 2.718281828459045 |
|---|
| 675 | sage: t = mathematica(e) # optional |
|---|
| 676 | sage: t # optional |
|---|
| 677 | E |
|---|
| 678 | sage: float(t) # optional |
|---|
| 679 | 2.7182818284590451 |
|---|
| 680 | |
|---|
| 681 | sage: loads(dumps(e)) |
|---|
| 682 | e |
|---|
| 683 | """ |
|---|
| 684 | conversions = dict(axiom='%e', maxima='%e', gp='exp(1)', |
|---|
| 685 | kash='E', pari='exp(1)', mathematica='E', |
|---|
| 686 | maple='exp(1)', octave='e') |
|---|
| 687 | Constant.__init__(self, name, conversions=conversions, |
|---|
| 688 | latex='e', domain='real') |
|---|
| 689 | |
|---|
| 690 | def expression(self): |
|---|
| 691 | """ |
|---|
| 692 | .. note:: |
|---|
| 693 | |
|---|
| 694 | For e, we don't return a wrapper around a Pynac constant. |
|---|
| 695 | Instead, we return exp(1) so that Pynac can perform appropiate. |
|---|
| 696 | |
|---|
| 697 | EXAMPLES:: |
|---|
| 698 | |
|---|
| 699 | sage: e + 2 |
|---|
| 700 | e + 2 |
|---|
| 701 | sage: e.operator() |
|---|
| 702 | exp |
|---|
| 703 | sage: e.operands() |
|---|
| 704 | [1] |
|---|
| 705 | """ |
|---|
| 706 | from sage.symbolic.ring import SR |
|---|
| 707 | return SR(1).exp() |
|---|
| 708 | |
|---|
| 709 | def __float__(self): |
|---|
| 710 | """ |
|---|
| 711 | EXAMPLES:: |
|---|
| 712 | |
|---|
| 713 | sage: float(e) |
|---|
| 714 | 2.7182818284590451 |
|---|
| 715 | sage: e.__float__() |
|---|
| 716 | 2.7182818284590451 |
|---|
| 717 | """ |
|---|
| 718 | return math.e |
|---|
| 719 | |
|---|
| 720 | def _mpfr_(self, R): |
|---|
| 721 | """ |
|---|
| 722 | EXAMPLES:: |
|---|
| 723 | |
|---|
| 724 | sage: e._mpfr_(RealField(100)) |
|---|
| 725 | 2.7182818284590452353602874714 |
|---|
| 726 | """ |
|---|
| 727 | return R(1).exp() |
|---|
| 728 | |
|---|
| 729 | def _real_double_(self, R): |
|---|
| 730 | """ |
|---|
| 731 | EXAMPLES:: |
|---|
| 732 | |
|---|
| 733 | sage: e._real_double_(RDF) |
|---|
| 734 | 2.71828182846 |
|---|
| 735 | """ |
|---|
| 736 | return R(1).exp() |
|---|
| 737 | |
|---|
| 738 | def _sympy_(self): |
|---|
| 739 | """ |
|---|
| 740 | Converts e to sympy E. |
|---|
| 741 | |
|---|
| 742 | EXAMPLES:: |
|---|
| 743 | |
|---|
| 744 | sage: import sympy |
|---|
| 745 | sage: sympy.E == e # indirect doctest |
|---|
| 746 | True |
|---|
| 747 | """ |
|---|
| 748 | import sympy |
|---|
| 749 | return sympy.E |
|---|
| 750 | |
|---|
| 751 | e = E().expression() |
|---|
| 752 | |
|---|
| 753 | class NotANumber(Constant): |
|---|
| 754 | """ |
|---|
| 755 | Not a Number |
|---|
| 756 | """ |
|---|
| 757 | def __init__(self, name="NaN"): |
|---|
| 758 | """ |
|---|
| 759 | EXAMPLES:: |
|---|
| 760 | |
|---|
| 761 | sage: loads(dumps(NaN)) |
|---|
| 762 | NaN |
|---|
| 763 | """ |
|---|
| 764 | conversions=dict(matlab='NaN') |
|---|
| 765 | Constant.__init__(self, name, conversions=conversions) |
|---|
| 766 | |
|---|
| 767 | def _mpfr_(self,R): |
|---|
| 768 | """ |
|---|
| 769 | EXAMPLES:: |
|---|
| 770 | |
|---|
| 771 | sage: NaN._mpfr_(RealField(53)) |
|---|
| 772 | NaN |
|---|
| 773 | sage: type(_) |
|---|
| 774 | <type 'sage.rings.real_mpfr.RealNumber'> |
|---|
| 775 | """ |
|---|
| 776 | return R('NaN') #??? nan in mpfr: void mpfr_set_nan (mpfr_t x) |
|---|
| 777 | |
|---|
| 778 | def _real_double_(self, R): |
|---|
| 779 | """ |
|---|
| 780 | EXAMPLES:: |
|---|
| 781 | |
|---|
| 782 | sage: RDF(NaN) |
|---|
| 783 | NaN |
|---|
| 784 | """ |
|---|
| 785 | return R.NaN() |
|---|
| 786 | |
|---|
| 787 | def _sympy_(self): |
|---|
| 788 | """ |
|---|
| 789 | Converts NaN to SymPy NaN. |
|---|
| 790 | |
|---|
| 791 | EXAMPLES:: |
|---|
| 792 | |
|---|
| 793 | sage: import sympy |
|---|
| 794 | sage: sympy.nan == NaN # indirect doctest |
|---|
| 795 | True |
|---|
| 796 | """ |
|---|
| 797 | import sympy |
|---|
| 798 | return sympy.nan |
|---|
| 799 | |
|---|
| 800 | NaN = NotANumber().expression() |
|---|
| 801 | |
|---|
| 802 | class GoldenRatio(Constant): |
|---|
| 803 | """ |
|---|
| 804 | The number (1+sqrt(5))/2 |
|---|
| 805 | |
|---|
| 806 | EXAMPLES:: |
|---|
| 807 | |
|---|
| 808 | sage: gr = golden_ratio |
|---|
| 809 | sage: RR(gr) |
|---|
| 810 | 1.61803398874989 |
|---|
| 811 | sage: R = RealField(200) |
|---|
| 812 | sage: R(gr) |
|---|
| 813 | 1.6180339887498948482045868343656381177203091798057628621354 |
|---|
| 814 | sage: grm = maxima(golden_ratio);grm |
|---|
| 815 | (sqrt(5)+1)/2 |
|---|
| 816 | sage: grm + grm |
|---|
| 817 | sqrt(5)+1 |
|---|
| 818 | sage: float(grm + grm) |
|---|
| 819 | 3.2360679774997898 |
|---|
| 820 | """ |
|---|
| 821 | def __init__(self, name='golden_ratio'): |
|---|
| 822 | """ |
|---|
| 823 | EXAMPLES:: |
|---|
| 824 | |
|---|
| 825 | sage: loads(dumps(golden_ratio)) |
|---|
| 826 | golden_ratio |
|---|
| 827 | """ |
|---|
| 828 | conversions = dict(mathematica='N[(1+Sqrt[5])/2]', gp='(1+sqrt(5))/2', |
|---|
| 829 | maple='(1+sqrt(5))/2', maxima='(1+sqrt(5))/2', |
|---|
| 830 | pari='(1+sqrt(5))/2', octave='(1+sqrt(5))/2', |
|---|
| 831 | kash='(1+Sqrt(5))/2') |
|---|
| 832 | Constant.__init__(self, name, conversions=conversions, |
|---|
| 833 | latex=r'\phi', domain='positive') |
|---|
| 834 | |
|---|
| 835 | def minpoly(self, bits=None, degree=None, epsilon=0): |
|---|
| 836 | """ |
|---|
| 837 | EXAMPLES:: |
|---|
| 838 | |
|---|
| 839 | sage: golden_ratio.minpoly() |
|---|
| 840 | x^2 - x - 1 |
|---|
| 841 | """ |
|---|
| 842 | from sage.rings.all import QQ |
|---|
| 843 | x = QQ['x'].gen(0) |
|---|
| 844 | return x**2 - x - 1 |
|---|
| 845 | |
|---|
| 846 | def __float__(self): |
|---|
| 847 | """ |
|---|
| 848 | EXAMPLES:: |
|---|
| 849 | |
|---|
| 850 | sage: float(golden_ratio) |
|---|
| 851 | 1.6180339887498949 |
|---|
| 852 | sage: golden_ratio.__float__() |
|---|
| 853 | 1.6180339887498949 |
|---|
| 854 | """ |
|---|
| 855 | return float(0.5)*(float(1.0)+math.sqrt(float(5.0))) |
|---|
| 856 | |
|---|
| 857 | def _real_double_(self, R): |
|---|
| 858 | """ |
|---|
| 859 | EXAMPLES:: |
|---|
| 860 | |
|---|
| 861 | sage: RDF(golden_ratio) |
|---|
| 862 | 1.61803398875 |
|---|
| 863 | """ |
|---|
| 864 | return R('1.61803398874989484820458') |
|---|
| 865 | |
|---|
| 866 | |
|---|
| 867 | def _mpfr_(self,R): |
|---|
| 868 | """ |
|---|
| 869 | EXAMPLES:: |
|---|
| 870 | |
|---|
| 871 | sage: golden_ratio._mpfr_(RealField(100)) |
|---|
| 872 | 1.6180339887498948482045868344 |
|---|
| 873 | sage: RealField(100)(golden_ratio) |
|---|
| 874 | 1.6180339887498948482045868344 |
|---|
| 875 | """ |
|---|
| 876 | return (R(1)+R(5).sqrt())/R(2) |
|---|
| 877 | |
|---|
| 878 | def _algebraic_(self, field): |
|---|
| 879 | """ |
|---|
| 880 | EXAMPLES:: |
|---|
| 881 | |
|---|
| 882 | sage: golden_ratio._algebraic_(QQbar) |
|---|
| 883 | 1.618033988749895? |
|---|
| 884 | sage: QQbar(golden_ratio) |
|---|
| 885 | 1.618033988749895? |
|---|
| 886 | """ |
|---|
| 887 | import sage.rings.qqbar |
|---|
| 888 | return field(sage.rings.qqbar.get_AA_golden_ratio()) |
|---|
| 889 | |
|---|
| 890 | def _sympy_(self): |
|---|
| 891 | """ |
|---|
| 892 | Converts golden_ratio to SymPy GoldenRation. |
|---|
| 893 | |
|---|
| 894 | EXAMPLES:: |
|---|
| 895 | |
|---|
| 896 | sage: import sympy |
|---|
| 897 | sage: sympy.GoldenRatio == golden_ratio # indirect doctest |
|---|
| 898 | True |
|---|
| 899 | """ |
|---|
| 900 | import sympy |
|---|
| 901 | return sympy.GoldenRatio |
|---|
| 902 | |
|---|
| 903 | golden_ratio = GoldenRatio().expression() |
|---|
| 904 | |
|---|
| 905 | class Log2(Constant): |
|---|
| 906 | """ |
|---|
| 907 | The natural logarithm of the real number 2. |
|---|
| 908 | |
|---|
| 909 | EXAMPLES:: |
|---|
| 910 | |
|---|
| 911 | sage: log2 |
|---|
| 912 | log2 |
|---|
| 913 | sage: float(log2) |
|---|
| 914 | 0.69314718055994529 |
|---|
| 915 | sage: RR(log2) |
|---|
| 916 | 0.693147180559945 |
|---|
| 917 | sage: R = RealField(200); R |
|---|
| 918 | Real Field with 200 bits of precision |
|---|
| 919 | sage: R(log2) |
|---|
| 920 | 0.69314718055994530941723212145817656807550013436025525412068 |
|---|
| 921 | sage: l = (1-log2)/(1+log2); l |
|---|
| 922 | -(log2 - 1)/(log2 + 1) |
|---|
| 923 | sage: R(l) |
|---|
| 924 | 0.18123221829928249948761381864650311423330609774776013488056 |
|---|
| 925 | sage: maxima(log2) |
|---|
| 926 | log(2) |
|---|
| 927 | sage: maxima(log2).float() |
|---|
| 928 | .6931471805599453 |
|---|
| 929 | sage: gp(log2) |
|---|
| 930 | 0.6931471805599453094172321215 # 32-bit |
|---|
| 931 | 0.69314718055994530941723212145817656807 # 64-bit |
|---|
| 932 | """ |
|---|
| 933 | def __init__(self, name='log2'): |
|---|
| 934 | """ |
|---|
| 935 | EXAMPLES:: |
|---|
| 936 | |
|---|
| 937 | sage: loads(dumps(log2)) |
|---|
| 938 | log2 |
|---|
| 939 | """ |
|---|
| 940 | conversions = dict(mathematica='N[Log[2]]', kash='Log(2)', |
|---|
| 941 | maple='log(2)', maxima='log(2)', gp='log(2)', |
|---|
| 942 | pari='log(2)', octave='log(2)') |
|---|
| 943 | Constant.__init__(self, name, conversions=conversions, |
|---|
| 944 | latex=r'\log(2)', domain='positive') |
|---|
| 945 | |
|---|
| 946 | def __float__(self): |
|---|
| 947 | """ |
|---|
| 948 | EXAMPLES:: |
|---|
| 949 | |
|---|
| 950 | sage: float(log2) |
|---|
| 951 | 0.69314718055994529 |
|---|
| 952 | sage: log2.__float__() |
|---|
| 953 | 0.69314718055994529 |
|---|
| 954 | """ |
|---|
| 955 | return math.log(2) |
|---|
| 956 | |
|---|
| 957 | def _real_double_(self, R): |
|---|
| 958 | """ |
|---|
| 959 | EXAMPLES:: |
|---|
| 960 | |
|---|
| 961 | sage: RDF(log2) |
|---|
| 962 | 0.69314718056 |
|---|
| 963 | """ |
|---|
| 964 | return R.log2() |
|---|
| 965 | |
|---|
| 966 | |
|---|
| 967 | def _mpfr_(self,R): |
|---|
| 968 | """ |
|---|
| 969 | EXAMPLES:: |
|---|
| 970 | |
|---|
| 971 | sage: RealField(100)(log2) |
|---|
| 972 | 0.69314718055994530941723212146 |
|---|
| 973 | sage: log2._mpfr_(RealField(100)) |
|---|
| 974 | 0.69314718055994530941723212146 |
|---|
| 975 | """ |
|---|
| 976 | return R.log2() |
|---|
| 977 | |
|---|
| 978 | log2 = Log2().expression() |
|---|
| 979 | |
|---|
| 980 | class EulerGamma(Constant): |
|---|
| 981 | """ |
|---|
| 982 | The limiting difference between the harmonic series and the natural |
|---|
| 983 | logarithm. |
|---|
| 984 | |
|---|
| 985 | EXAMPLES:: |
|---|
| 986 | |
|---|
| 987 | sage: R = RealField() |
|---|
| 988 | sage: R(euler_gamma) |
|---|
| 989 | 0.577215664901533 |
|---|
| 990 | sage: R = RealField(200); R |
|---|
| 991 | Real Field with 200 bits of precision |
|---|
| 992 | sage: R(euler_gamma) |
|---|
| 993 | 0.57721566490153286060651209008240243104215933593992359880577 |
|---|
| 994 | sage: eg = euler_gamma + euler_gamma; eg |
|---|
| 995 | 2*euler_gamma |
|---|
| 996 | sage: R(eg) |
|---|
| 997 | 1.1544313298030657212130241801648048620843186718798471976115 |
|---|
| 998 | """ |
|---|
| 999 | def __init__(self, name='euler_gamma'): |
|---|
| 1000 | """ |
|---|
| 1001 | EXAMPLES:: |
|---|
| 1002 | |
|---|
| 1003 | sage: loads(dumps(euler_gamma)) |
|---|
| 1004 | euler_gamma |
|---|
| 1005 | """ |
|---|
| 1006 | conversions = dict(kash='EulerGamma(R)', maple='gamma', |
|---|
| 1007 | mathematica='EulerGamma', pari='Euler', |
|---|
| 1008 | maxima='%gamma', pynac='Euler') |
|---|
| 1009 | Constant.__init__(self, name, conversions=conversions, |
|---|
| 1010 | latex=r'\gamma', domain='positive') |
|---|
| 1011 | |
|---|
| 1012 | def _mpfr_(self,R): |
|---|
| 1013 | """ |
|---|
| 1014 | EXAMPLES:: |
|---|
| 1015 | |
|---|
| 1016 | sage: RealField(100)(euler_gamma) |
|---|
| 1017 | 0.57721566490153286060651209008 |
|---|
| 1018 | sage: euler_gamma._mpfr_(RealField(100)) |
|---|
| 1019 | 0.57721566490153286060651209008 |
|---|
| 1020 | """ |
|---|
| 1021 | return R.euler_constant() |
|---|
| 1022 | |
|---|
| 1023 | def _real_double_(self, R): |
|---|
| 1024 | """ |
|---|
| 1025 | EXAMPLES:: |
|---|
| 1026 | |
|---|
| 1027 | sage: RDF(euler_gamma) |
|---|
| 1028 | 0.577215664902 |
|---|
| 1029 | """ |
|---|
| 1030 | return R.euler_constant() |
|---|
| 1031 | |
|---|
| 1032 | def _sympy_(self): |
|---|
| 1033 | """ |
|---|
| 1034 | Converts euler_gamma to SymPy EulerGamma. |
|---|
| 1035 | |
|---|
| 1036 | EXAMPLES:: |
|---|
| 1037 | |
|---|
| 1038 | sage: import sympy |
|---|
| 1039 | sage: sympy.EulerGamma == euler_gamma # indirect doctest |
|---|
| 1040 | True |
|---|
| 1041 | """ |
|---|
| 1042 | import sympy |
|---|
| 1043 | return sympy.EulerGamma |
|---|
| 1044 | |
|---|
| 1045 | euler_gamma = EulerGamma().expression() |
|---|
| 1046 | |
|---|
| 1047 | class Catalan(Constant): |
|---|
| 1048 | """ |
|---|
| 1049 | A number appaering in combinatorics defined as the Dirichlet beta |
|---|
| 1050 | function evaluated at the number 2. |
|---|
| 1051 | |
|---|
| 1052 | EXAMPLES:: |
|---|
| 1053 | |
|---|
| 1054 | sage: catalan^2 + merten |
|---|
| 1055 | merten + catalan^2 |
|---|
| 1056 | """ |
|---|
| 1057 | def __init__(self, name='catalan'): |
|---|
| 1058 | """ |
|---|
| 1059 | EXAMPLES:: |
|---|
| 1060 | |
|---|
| 1061 | sage: loads(dumps(catalan)) |
|---|
| 1062 | catalan |
|---|
| 1063 | """ |
|---|
| 1064 | #kash: R is default prec |
|---|
| 1065 | conversions = dict(mathematica='Catalan', kash='Catalan(R)', |
|---|
| 1066 | maple='Catalan', maxima='catalan', |
|---|
| 1067 | pynac='Catalan') |
|---|
| 1068 | Constant.__init__(self, name, conversions=conversions, |
|---|
| 1069 | domain='positive') |
|---|
| 1070 | |
|---|
| 1071 | |
|---|
| 1072 | def _mpfr_(self, R): |
|---|
| 1073 | """ |
|---|
| 1074 | EXAMPLES:: |
|---|
| 1075 | |
|---|
| 1076 | sage: RealField(100)(catalan) |
|---|
| 1077 | 0.91596559417721901505460351493 |
|---|
| 1078 | sage: catalan._mpfr_(RealField(100)) |
|---|
| 1079 | 0.91596559417721901505460351493 |
|---|
| 1080 | """ |
|---|
| 1081 | return R.catalan_constant() |
|---|
| 1082 | |
|---|
| 1083 | def _real_double_(self, R): |
|---|
| 1084 | """ |
|---|
| 1085 | EXAMPLES: We coerce to the real double field:: |
|---|
| 1086 | |
|---|
| 1087 | sage: RDF(catalan) |
|---|
| 1088 | 0.915965594177 |
|---|
| 1089 | """ |
|---|
| 1090 | return R('0.91596559417721901505460351493252') |
|---|
| 1091 | |
|---|
| 1092 | |
|---|
| 1093 | def __float__(self): |
|---|
| 1094 | """ |
|---|
| 1095 | EXAMPLES:: |
|---|
| 1096 | |
|---|
| 1097 | sage: float(catalan) |
|---|
| 1098 | 0.91596559417721901 |
|---|
| 1099 | """ |
|---|
| 1100 | return 0.91596559417721901505460351493252 |
|---|
| 1101 | |
|---|
| 1102 | def _sympy_(self): |
|---|
| 1103 | """ |
|---|
| 1104 | Converts catalan to SymPy Catalan. |
|---|
| 1105 | |
|---|
| 1106 | EXAMPLES:: |
|---|
| 1107 | |
|---|
| 1108 | sage: import sympy |
|---|
| 1109 | sage: sympy.Catalan == catalan # indirect doctest |
|---|
| 1110 | True |
|---|
| 1111 | """ |
|---|
| 1112 | import sympy |
|---|
| 1113 | return sympy.Catalan |
|---|
| 1114 | |
|---|
| 1115 | catalan = Catalan().expression() |
|---|
| 1116 | |
|---|
| 1117 | ############################### |
|---|
| 1118 | # Limited precision constants # |
|---|
| 1119 | ############################### |
|---|
| 1120 | |
|---|
| 1121 | # NOTE: LimitedPrecisionConstant is an *abstract base class*. It is |
|---|
| 1122 | # not instantiated. It doesn't make sense to pickle and unpickle |
|---|
| 1123 | # this, so there is no loads(dumps(...)) doctest below. |
|---|
| 1124 | |
|---|
| 1125 | class LimitedPrecisionConstant(Constant): |
|---|
| 1126 | def __init__(self, name, value, **kwds): |
|---|
| 1127 | """ |
|---|
| 1128 | A class for constants that are only known to a limited |
|---|
| 1129 | precision. |
|---|
| 1130 | |
|---|
| 1131 | EXAMPLES:: |
|---|
| 1132 | |
|---|
| 1133 | sage: from sage.symbolic.constants import LimitedPrecisionConstant |
|---|
| 1134 | sage: a = LimitedPrecisionConstant('a', '1.234567891011121314').expression(); a |
|---|
| 1135 | a |
|---|
| 1136 | sage: RDF(a) |
|---|
| 1137 | 1.23456789101 |
|---|
| 1138 | sage: RealField(200)(a) |
|---|
| 1139 | Traceback (most recent call last): |
|---|
| 1140 | ... |
|---|
| 1141 | NotImplementedError: a is only available up to 59 bits |
|---|
| 1142 | """ |
|---|
| 1143 | self._value = value |
|---|
| 1144 | self._bits = len(self._value)*3-1 #FIXME |
|---|
| 1145 | Constant.__init__(self, name, **kwds) |
|---|
| 1146 | |
|---|
| 1147 | def _mpfr_(self, R): |
|---|
| 1148 | """ |
|---|
| 1149 | EXAMPLES:: |
|---|
| 1150 | |
|---|
| 1151 | sage: RealField(100)(khinchin) |
|---|
| 1152 | 2.6854520010653064453097148355 |
|---|
| 1153 | sage: RealField(20000)(khinchin) |
|---|
| 1154 | Traceback (most recent call last): |
|---|
| 1155 | ... |
|---|
| 1156 | NotImplementedError: khinchin is only available up to 3005 bits |
|---|
| 1157 | """ |
|---|
| 1158 | if R.precision() <= self._bits: |
|---|
| 1159 | return R(self._value) |
|---|
| 1160 | raise NotImplementedError, "%s is only available up to %s bits"%(self.name(), self._bits) |
|---|
| 1161 | |
|---|
| 1162 | def _real_double_(self, R): |
|---|
| 1163 | """ |
|---|
| 1164 | EXAMPLES:: |
|---|
| 1165 | |
|---|
| 1166 | sage: RDF(khinchin) |
|---|
| 1167 | 2.68545200107 |
|---|
| 1168 | """ |
|---|
| 1169 | return R(self._value) |
|---|
| 1170 | |
|---|
| 1171 | def __float__(self): |
|---|
| 1172 | """ |
|---|
| 1173 | EXAMPLES:: |
|---|
| 1174 | |
|---|
| 1175 | sage: float(khinchin) |
|---|
| 1176 | 2.6854520010653062 |
|---|
| 1177 | """ |
|---|
| 1178 | return float(self._value) |
|---|
| 1179 | |
|---|
| 1180 | class Khinchin(LimitedPrecisionConstant): |
|---|
| 1181 | """ |
|---|
| 1182 | The geometric mean of the continued fraction expansion of any |
|---|
| 1183 | (almost any) real number. |
|---|
| 1184 | |
|---|
| 1185 | EXAMPLES:: |
|---|
| 1186 | |
|---|
| 1187 | sage: float(khinchin) |
|---|
| 1188 | 2.6854520010653062 |
|---|
| 1189 | sage: m = mathematica(khinchin); m # optional |
|---|
| 1190 | Khinchin |
|---|
| 1191 | sage: m.N(200) # optional |
|---|
| 1192 | 2.68545200106530644530971483548179569382038229399446295305115234555721885953715200280114117493184769799515346590528809008289767771641096305179253348325966838185231542133211949962603932852204481940961807 # 32-bit |
|---|
| 1193 | 2.6854520010653064453097148354817956938203822939944629530511523455572188595371520028011411749318476979951534659052880900828976777164109630517925334832596683818523154213321194996260393285220448194096181 # 64-bit |
|---|
| 1194 | """ |
|---|
| 1195 | def __init__(self, name='khinchin'): |
|---|
| 1196 | """ |
|---|
| 1197 | EXAMPLES:: |
|---|
| 1198 | |
|---|
| 1199 | sage: loads(dumps(khinchin)) |
|---|
| 1200 | khinchin |
|---|
| 1201 | """ |
|---|
| 1202 | conversions = dict(maxima='khinchin', mathematica='Khinchin') |
|---|
| 1203 | # digits come from http://pi.lacim.uqam.ca/piDATA/khintchine.txt |
|---|
| 1204 | value = "2.6854520010653064453097148354817956938203822939944629530511523455572188595371520028011411749318476979951534659052880900828976777164109630517925334832596683818523154213321194996260393285220448194096180686641664289308477880620360737053501033672633577289049904270702723451702625237023545810686318501032374655803775026442524852869468234189949157306618987207994137235500057935736698933950879021244642075289741459147693018449050601793499385225470404203377985639831015709022233910000220772509651332460444439191691460859682348212832462282927101269069741823484776754573489862542033926623518620867781366509696583146995271837448054012195366666049648269890827548115254721177330319675947383719393578106059230401890711349624673706841221794681074060891827669566711716683740590473936880953450489997047176390451343232377151032196515038246988883248709353994696082647818120566349467125784366645797409778483662049777748682765697087163192938512899314199518611673792654620563505951385713761697126872299805327673278710513763" |
|---|
| 1205 | |
|---|
| 1206 | LimitedPrecisionConstant.__init__(self, name, value, |
|---|
| 1207 | conversions=conversions, |
|---|
| 1208 | domain='positive') |
|---|
| 1209 | |
|---|
| 1210 | khinchin = Khinchin().expression() |
|---|
| 1211 | |
|---|
| 1212 | class TwinPrime(LimitedPrecisionConstant): |
|---|
| 1213 | r""" |
|---|
| 1214 | The Twin Primes constant is defined as |
|---|
| 1215 | `\prod 1 - 1/(p-1)^2` for primes `p > 2`. |
|---|
| 1216 | |
|---|
| 1217 | EXAMPLES:: |
|---|
| 1218 | |
|---|
| 1219 | sage: float(twinprime) |
|---|
| 1220 | 0.66016181584686962 |
|---|
| 1221 | sage: R=RealField(200);R |
|---|
| 1222 | Real Field with 200 bits of precision |
|---|
| 1223 | sage: R(twinprime) |
|---|
| 1224 | 0.66016181584686957392781211001455577843262336028473341331945 |
|---|
| 1225 | """ |
|---|
| 1226 | def __init__(self, name='twinprime'): |
|---|
| 1227 | """ |
|---|
| 1228 | EXAMPLES:: |
|---|
| 1229 | |
|---|
| 1230 | sage: loads(dumps(twinprime)) |
|---|
| 1231 | twinprime |
|---|
| 1232 | """ |
|---|
| 1233 | conversions = dict(maxima='twinprime') |
|---|
| 1234 | #digits come from http://www.gn-50uma.de/alula/essays/Moree/Moree-details.en.shtml |
|---|
| 1235 | |
|---|
| 1236 | value = "0.660161815846869573927812110014555778432623360284733413319448423335405642304495277143760031413839867911779005226693304002965847755123366227747165713213986968741097620630214153735434853131596097803669932135255299767199302474590593101082978291553834469297505205916657133653611991532464281301172462306379341060056466676584434063501649322723528968010934966475600478812357962789459842433655749375581854814173628678098705969498703841243363386589311969079150040573717814371081810615401233104810577794415613125444598860988997585328984038108718035525261719887112136382808782349722374224097142697441764455225265548994829771790977784043757891956590649994567062907828608828395990394287082529070521554595671723599449769037800675978761690802426600295711092099633708272559284672129858001148697941855401824639887493941711828528382365997050328725708087980662201068630474305201992394282014311102297265141514194258422242375342296879836738796224286600285358098482833679152235700192585875285961205994728621007171131607980572" |
|---|
| 1237 | |
|---|
| 1238 | LimitedPrecisionConstant.__init__(self, name, value, |
|---|
| 1239 | conversions=conversions, |
|---|
| 1240 | domain='positive') |
|---|
| 1241 | |
|---|
| 1242 | twinprime = TwinPrime().expression() |
|---|
| 1243 | |
|---|
| 1244 | |
|---|
| 1245 | class Merten(LimitedPrecisionConstant): |
|---|
| 1246 | """ |
|---|
| 1247 | The Merten constant is related to the Twin Primes constant and |
|---|
| 1248 | appears in Merten's second theorem. |
|---|
| 1249 | |
|---|
| 1250 | EXAMPLES:: |
|---|
| 1251 | |
|---|
| 1252 | sage: float(merten) |
|---|
| 1253 | 0.26149721284764277 |
|---|
| 1254 | sage: R=RealField(200);R |
|---|
| 1255 | Real Field with 200 bits of precision |
|---|
| 1256 | sage: R(merten) |
|---|
| 1257 | 0.26149721284764278375542683860869585905156664826119920619206 |
|---|
| 1258 | """ |
|---|
| 1259 | def __init__(self, name='merten'): |
|---|
| 1260 | """ |
|---|
| 1261 | EXAMPLES:: |
|---|
| 1262 | |
|---|
| 1263 | sage: loads(dumps(merten)) |
|---|
| 1264 | merten |
|---|
| 1265 | """ |
|---|
| 1266 | conversions = dict(maxima='merten') |
|---|
| 1267 | |
|---|
| 1268 | # digits come from Sloane's tables at http://www.research.att.com/~njas/sequences/table?a=77761&fmt=0 |
|---|
| 1269 | value = "0.261497212847642783755426838608695859051566648261199206192064213924924510897368209714142631434246651051617" |
|---|
| 1270 | |
|---|
| 1271 | LimitedPrecisionConstant.__init__(self, name, value, |
|---|
| 1272 | conversions=conversions, |
|---|
| 1273 | domain='positive') |
|---|
| 1274 | |
|---|
| 1275 | merten = Merten().expression() |
|---|
| 1276 | |
|---|
| 1277 | class Brun(LimitedPrecisionConstant): |
|---|
| 1278 | """ |
|---|
| 1279 | Brun's constant is the sum of reciprocals of odd twin primes. |
|---|
| 1280 | |
|---|
| 1281 | It is not known to very high precision; calculating the number |
|---|
| 1282 | using twin primes up to `10^{16}` (Sebah 2002) gives the |
|---|
| 1283 | number `1.9021605831040`. |
|---|
| 1284 | |
|---|
| 1285 | EXAMPLES:: |
|---|
| 1286 | |
|---|
| 1287 | sage: float(brun) |
|---|
| 1288 | Traceback (most recent call last): |
|---|
| 1289 | ... |
|---|
| 1290 | NotImplementedError: brun is only available up to 41 bits |
|---|
| 1291 | sage: R = RealField(41); R |
|---|
| 1292 | Real Field with 41 bits of precision |
|---|
| 1293 | sage: R(brun) |
|---|
| 1294 | 1.90216058310 |
|---|
| 1295 | """ |
|---|
| 1296 | def __init__(self, name='brun'): |
|---|
| 1297 | """ |
|---|
| 1298 | EXAMPLES:: |
|---|
| 1299 | |
|---|
| 1300 | sage: loads(dumps(brun)) |
|---|
| 1301 | brun |
|---|
| 1302 | """ |
|---|
| 1303 | conversions = dict(maxima='brun') |
|---|
| 1304 | |
|---|
| 1305 | # digits come from Sloane's tables at http://www.research.att.com/~njas/sequences/table?a=65421&fmt=0 |
|---|
| 1306 | value = "1.902160583104" |
|---|
| 1307 | |
|---|
| 1308 | LimitedPrecisionConstant.__init__(self, name, value, |
|---|
| 1309 | conversions=conversions, |
|---|
| 1310 | domain='positive') |
|---|
| 1311 | |
|---|
| 1312 | brun = Brun().expression() |
|---|