# HG changeset patch
# User Jason Grout <jason-sage@creativetrax.com>
# Date 1264426711 21600
# Node ID 5a0fc65a610385eebb944e8440a21fbe339e3263
# Parent 21f187597161a097055baf78bff2b340f1b67b89
Added a more comprehensive print options feature to RealField.
Fixed lots and lots of docstrings
Fixed the bug: RR('inf').is_unit() returns True
diff -r 21f187597161 -r 5a0fc65a6103 sage/calculus/calculus.py
a
|
b
|
|
868 | 868 | elif epsilon and error < epsilon: |
869 | 869 | return g |
870 | 870 | elif algorithm is not None: |
871 | | raise NotImplementedError, "Could not prove minimal polynomial %s (epsilon %s)" % (g, RR(error).str(no_sci=False)) |
| 871 | raise NotImplementedError, "Could not prove minimal polynomial %s (epsilon %s)" % (g, RR(error).str(scientific_notation='always')) |
872 | 872 | |
873 | 873 | if algorithm is not None: |
874 | 874 | raise ValueError, "Could not find minimal polynomial (%s bits, degree %s)." % (bits, degree) |
… |
… |
|
1529 | 1529 | search = sci_not.search(s) |
1530 | 1530 | while not search is None: |
1531 | 1531 | (start, end) = search.span() |
1532 | | r = create_RealNumber(s[start:end]).str(no_sci=2, truncate=True) |
| 1532 | r = create_RealNumber(s[start:end]).str(scientific_notation='never', truncate=True) |
1533 | 1533 | s = s.replace(s[start:end], r) |
1534 | 1534 | search = sci_not.search(s) |
1535 | 1535 | |
diff -r 21f187597161 -r 5a0fc65a6103 sage/calculus/desolvers.py
a
|
b
|
|
740 | 740 | |
741 | 741 | :: |
742 | 742 | |
743 | | sage: RR = RealField(sci_not=0, prec=4, rnd='RNDU') |
| 743 | sage: RR = RealField(prec=4, rnd='RNDU', print_options={'scientific_notation': 0}) |
744 | 744 | sage: x,y = PolynomialRing(RR,2,"xy").gens() |
745 | 745 | sage: eulers_method(5*x+y-5,0,1,1/2,1,method="None") |
746 | 746 | [[0, 1], [1/2, -1.0], [1, -2.7], [3/2, -4.0]] |
747 | 747 | |
748 | 748 | :: |
749 | 749 | |
750 | | sage: RR = RealField(sci_not=0, prec=4, rnd='RNDU') |
| 750 | sage: RR = RealField(prec=4, rnd='RNDU', print_options={'scientific_notation': 0}) |
751 | 751 | sage: x,y=PolynomialRing(RR,2,"xy").gens() |
752 | 752 | sage: eulers_method(5*x+y-5,0,1,1/2,1) |
753 | 753 | x y h*f(x,y) |
… |
… |
|
832 | 832 | |
833 | 833 | :: |
834 | 834 | |
835 | | sage: RR = RealField(sci_not=0, prec=4, rnd='RNDU') |
| 835 | sage: RR = RealField(prec=4, rnd='RNDU', print_options={'scientific_notation': 0}) |
836 | 836 | sage: t,x,y=PolynomialRing(RR,3,"txy").gens() |
837 | 837 | sage: f = x+y+t; g = x-y |
838 | 838 | sage: eulers_method_2x2(f,g, 0, 0, 0, 1/3, 1) |
… |
… |
|
847 | 847 | convert to a system: `y_1' = y_2`, `y_1(0)=1`; `y_2' = |
848 | 848 | \\frac{y_1-y_2}{1+t^2}`, `y_2(0)=-1`.:: |
849 | 849 | |
850 | | sage: RR = RealField(sci_not=0, prec=4, rnd='RNDU') |
| 850 | sage: RR = RealField(prec=4, rnd='RNDU', print_options={'scientific_notation': 0}) |
851 | 851 | sage: t, x, y=PolynomialRing(RR,3,"txy").gens() |
852 | 852 | sage: f = y; g = (x-y)/(1+t^2) |
853 | 853 | sage: eulers_method_2x2(f,g, 0, 1, -1, 1/4, 1) |
diff -r 21f187597161 -r 5a0fc65a6103 sage/misc/explain_pickle.py
a
|
b
|
|
210 | 210 | {('a', 'b'):[1r, 2r]} |
211 | 211 | sage: explain_pickle(dumps(RR(pi)), in_current_sage=True) |
212 | 212 | from sage.rings.real_mpfr import __create__RealNumber_version0 |
213 | | from sage.rings.real_mpfr import __create__RealField_version0 |
214 | | __create__RealNumber_version0(__create__RealField_version0(53r, False, 'RNDN'), '3.4gvml245kc0@0', 32r) |
| 213 | from sage.rings.real_mpfr import __create__RealField_version1 |
| 214 | __create__RealNumber_version0(__create__RealField_version1(53r, 'RNDN', {'scientific_notation':'extreme', 'skip_zeroes':False, 'truncate':True}), '3.4gvml245kc0@0', 32r) |
215 | 215 | sage: s = 'hi' |
216 | 216 | sage: explain_pickle(dumps((s, s))) |
217 | 217 | ('hi', 'hi') |
diff -r 21f187597161 -r 5a0fc65a6103 sage/misc/latex.py
a
|
b
|
|
418 | 418 | |
419 | 419 | sage: from sage.misc.latex import latex_extra_preamble |
420 | 420 | sage: latex_extra_preamble() |
421 | | '\n\\newcommand{\\ZZ}{\\Bold{Z}}\n\\newcommand{\\RR}{\\Bold{R}}\n\\newcommand{\\CC}{\\Bold{C}}\n\\newcommand{\\QQ}{\\Bold{Q}}\n\\newcommand{\\QQbar}{\\overline{\\QQ}}\n\\newcommand{\\GF}[1]{\\Bold{F}_{#1}}\n\\newcommand{\\Zp}[1]{\\ZZ_{#1}}\n\\newcommand{\\Qp}[1]{\\QQ_{#1}}\n\\newcommand{\\Zmod}[1]{\\ZZ/#1\\ZZ}\n\\newcommand{\\CDF}{\\texttt{Complex Double Field}}\n\\newcommand{\\CIF}{\\Bold{C}}\n\\newcommand{\\CLF}{\\Bold{C}}\n\\newcommand{\\RDF}{\\Bold{R}}\n\\newcommand{\\RIF}{\\Bold{I} \\Bold{R}}\n\\newcommand{\\RLF}{\\Bold{R}}\n\\newcommand{\\CFF}{\\Bold{CFF}}\n\\newcommand{\\Bold}[1]{\\mathbf{#1}}\n' |
| 421 | '\n\\newcommand{\\ZZ}{\\Bold{Z}}\n\\newcommand{\\RR}{\\Bold{R}^{}_{53}}\n\\newcommand{\\CC}{\\Bold{C}}\n\\newcommand{\\QQ}{\\Bold{Q}}\n\\newcommand{\\QQbar}{\\overline{\\QQ}}\n\\newcommand{\\GF}[1]{\\Bold{F}_{#1}}\n\\newcommand{\\Zp}[1]{\\ZZ_{#1}}\n\\newcommand{\\Qp}[1]{\\QQ_{#1}}\n\\newcommand{\\Zmod}[1]{\\ZZ/#1\\ZZ}\n\\newcommand{\\CDF}{\\texttt{Complex Double Field}}\n\\newcommand{\\CIF}{\\Bold{C}}\n\\newcommand{\\CLF}{\\Bold{C}}\n\\newcommand{\\RDF}{\\Bold{R}}\n\\newcommand{\\RIF}{\\Bold{I} \\Bold{R}}\n\\newcommand{\\RLF}{\\Bold{R}}\n\\newcommand{\\CFF}{\\Bold{CFF}}\n\\newcommand{\\Bold}[1]{\\mathbf{#1}}\n' |
422 | 422 | """ |
423 | 423 | from sage.misc.latex_macros import sage_latex_macros |
424 | 424 | return (_Latex_prefs._option['preamble'] + "\n" |
diff -r 21f187597161 -r 5a0fc65a6103 sage/misc/misc.py
a
|
b
|
|
1943 | 1943 | should be replaced. |
1944 | 1944 | |
1945 | 1945 | - ``version`` - (optional) on which version and when the deprecation |
1946 | | occured. Please put there the version of sageq at the time of deprecation. |
| 1946 | occured. Please put there the version of sage at the time of deprecation. |
1947 | 1947 | |
1948 | 1948 | EXAMPLES:: |
1949 | 1949 | |
… |
… |
|
1969 | 1969 | from sage.misc.lazy_attribute import lazy_attribute |
1970 | 1970 | class DeprecatedFunctionAlias(object): |
1971 | 1971 | """ |
1972 | | A wrapper around methods or functions wich automatically print the correct |
| 1972 | A wrapper around methods or functions which automatically print the correct |
1973 | 1973 | deprecation message. See :func:`deprecated_function_alias`. |
1974 | 1974 | |
1975 | 1975 | AUTHORS: |
diff -r 21f187597161 -r 5a0fc65a6103 sage/rings/real_interval_field.py
a
|
b
|
|
12 | 12 | return isinstance(x, RealIntervalFieldElementClass) |
13 | 13 | |
14 | 14 | def __reduce__RealIntervalField(prec, sci_not): |
15 | | return RealIntervalField(prec, sci_not) |
| 15 | print_options={'scientific_notation': sci_not} |
| 16 | return RealIntervalField(prec=prec, print_options=print_options) |
16 | 17 | |
17 | 18 | def __reduce__RealIntervalFieldElement(parent, x, base=10): |
18 | 19 | return RealIntervalFieldElementClass(parent, x, base=base) |
diff -r 21f187597161 -r 5a0fc65a6103 sage/rings/real_mpfi.pxd
a
|
b
|
|
18 | 18 | |
19 | 19 | cdef class RealIntervalField_class(sage.rings.ring.Field): |
20 | 20 | cdef int __prec |
21 | | cdef bint sci_not |
| 21 | cdef object print_options |
22 | 22 | # Cache RealField instances for the lower, upper, and middle bounds. |
23 | 23 | # These have the same precision as the interval field; |
24 | 24 | # __lower_field rounds down, __upper_field rounds up. |
diff -r 21f187597161 -r 5a0fc65a6103 sage/rings/real_mpfi.pyx
a
|
b
|
|
198 | 198 | |
199 | 199 | cimport real_mpfr |
200 | 200 | from real_mpfr cimport RealField_class, RealNumber |
201 | | from real_mpfr import RealField |
| 201 | from real_mpfr import RealField, sci_not_deprecation |
202 | 202 | import real_mpfr |
203 | 203 | |
204 | 204 | import operator |
… |
… |
|
227 | 227 | printing_style = 'question' |
228 | 228 | printing_error_digits = 0 |
229 | 229 | |
| 230 | _PRINT_OPTIONS = {'skip_zeroes': False, 'truncate': True, 'scientific_notation': 'extreme', |
| 231 | 'style': 'question', 'error_digits': 0} |
| 232 | |
| 233 | |
230 | 234 | #***************************************************************************** |
231 | 235 | # |
232 | 236 | # Real Field |
… |
… |
|
237 | 241 | |
238 | 242 | RealIntervalField_cache = {} |
239 | 243 | |
240 | | def RealIntervalField(prec=53, sci_not=False): |
| 244 | def RealIntervalField(prec=53, scientific_notation=None, print_options={},sci_not=None): |
241 | 245 | r""" |
242 | 246 | Construct a RealIntervalField_class, with caching. |
243 | 247 | |
… |
… |
|
249 | 253 | mpfr_prec_min() and mpfr_prec_max(). In the current |
250 | 254 | implementation, mpfr_prec_min() is equal to 2. |
251 | 255 | |
252 | | - ``sci_not`` - (default: False) whether or not to display using |
253 | | scientific notation |
| 256 | - ``scientific_notation`` |
| 257 | |
| 258 | - 'never' - never print using scientific notation; |
| 259 | |
| 260 | - 'extreme' (default) - print using scientific notation only for very |
| 261 | large or very small numbers; |
| 262 | |
| 263 | - 'always' - always print with scientific notation; |
254 | 264 | |
255 | 265 | EXAMPLES:: |
256 | 266 | |
257 | 267 | sage: RealIntervalField() |
258 | 268 | Real Interval Field with 53 bits of precision |
259 | | sage: RealIntervalField(200, sci_not=True) |
| 269 | sage: RealIntervalField(200, scientific_notation=True) |
260 | 270 | Real Interval Field with 200 bits of precision |
261 | 271 | sage: RealIntervalField(53) is RIF |
262 | 272 | True |
… |
… |
|
268 | 278 | See the documentation for :class:`RealIntervalField_class` for many more |
269 | 279 | examples. |
270 | 280 | """ |
| 281 | printing=_PRINT_OPTIONS.copy() |
| 282 | printing.update(print_options) |
| 283 | if sci_not is not None: |
| 284 | from sage.misc.misc import deprecation |
| 285 | deprecation("The sci_not keyword is deprecated. Use print_options={'scientific_notation': %r} instead."%sci_not_deprecation[sci_not]) |
| 286 | scientific_notation=sci_not_deprecation[sci_not] |
| 287 | if scientific_notation is not None: |
| 288 | printing['scientific_notation']=scientific_notation |
| 289 | print_cached=tuple(sorted(printing.items())) |
271 | 290 | try: |
272 | | return RealIntervalField_cache[prec, sci_not] |
| 291 | return RealIntervalField_cache[prec, print_cached] |
273 | 292 | except KeyError: |
274 | | RealIntervalField_cache[prec, sci_not] = R = RealIntervalField_class(prec, sci_not) |
| 293 | RealIntervalField_cache[prec, print_cached] = R = RealIntervalField_class(prec=prec, print_options=printing, sci_not=sci_not) |
275 | 294 | return R |
276 | 295 | |
277 | 296 | cdef class RealIntervalField_class(sage.rings.ring.Field): |
278 | 297 | """ |
279 | | RealIntervalField(prec, sci_not): |
| 298 | RealIntervalField(prec, scientific_notation): |
280 | 299 | |
281 | 300 | INPUT: |
282 | 301 | |
… |
… |
|
288 | 307 | implementation, mpfr_prec_min() is equal to 2. |
289 | 308 | |
290 | 309 | |
291 | | - ``sci_not`` - (default: False) whether or not to display using |
| 310 | - ``scientific_notation`` - (default: False) whether or not to display using |
292 | 311 | scientific notation |
293 | 312 | |
294 | 313 | EXAMPLES:: |
… |
… |
|
433 | 452 | sage: RIF._middle_field() is RR |
434 | 453 | True |
435 | 454 | """ |
436 | | |
437 | | def __init__(self, int prec=53, int sci_not=0): |
| 455 | def __init__(self, int prec=53, scientific_notation=None, print_options=None, sci_not=None): |
| 456 | if sci_not is not None: |
| 457 | from sage.misc.misc import deprecation |
| 458 | deprecation("The sci_not keyword is deprecated. Use scientific_notation instead.") |
| 459 | scientific_notation=sci_not |
| 460 | |
438 | 461 | if prec < MPFR_PREC_MIN or prec > MPFR_PREC_MAX: |
439 | 462 | raise ValueError, "prec (=%s) must be >= %s and <= %s."%( |
440 | 463 | prec, MPFR_PREC_MIN, MPFR_PREC_MAX) |
441 | 464 | self.__prec = prec |
442 | | self.sci_not = sci_not |
443 | | self.__lower_field = RealField(prec, sci_not, "RNDD") |
444 | | self.__middle_field = RealField(prec, sci_not, "RNDN") |
445 | | self.__upper_field = RealField(prec, sci_not, "RNDU") |
| 465 | self.print_options=_PRINT_OPTIONS.copy() |
| 466 | if scientific_notation is not None: |
| 467 | self.print_options['scientific_notation']=scientific_notation |
| 468 | |
| 469 | realfield_print_options={} |
| 470 | for key in real_mpfr._PRINT_OPTIONS: |
| 471 | if key in self.print_options: |
| 472 | realfield_print_options[key]=self.print_options[key] |
| 473 | |
| 474 | self.__lower_field = RealField(prec=prec, rnd="RNDD", |
| 475 | print_options=realfield_print_options) |
| 476 | self.__middle_field = RealField(prec=prec, rnd="RNDN", |
| 477 | print_options=realfield_print_options) |
| 478 | self.__upper_field = RealField(prec=prec, rnd="RNDU", |
| 479 | print_options=realfield_print_options) |
446 | 480 | ParentWithGens.__init__(self, self, tuple([]), False) |
447 | 481 | |
448 | 482 | def _lower_field(self): |
… |
… |
|
462 | 496 | elif rnd == "RNDU": |
463 | 497 | return self._upper_field() |
464 | 498 | else: |
465 | | return RealField(self.__prec, self.sci_not, "RNDZ") |
| 499 | return RealField(prec=self.__prec, rnd="RNDZ", |
| 500 | print_options=self.print_options) |
466 | 501 | |
467 | 502 | cdef RealIntervalFieldElement _new(self): |
468 | 503 | """ |
… |
… |
|
583 | 618 | from sage.categories.pushout import CompletionFunctor |
584 | 619 | return (CompletionFunctor(sage.rings.infinity.Infinity, |
585 | 620 | self.prec(), |
586 | | {'sci_not': self.scientific_notation(), 'type': 'Interval'}), |
| 621 | {'print_options': self.print_options, 'type': 'Interval'}), |
587 | 622 | sage.rings.rational_field.QQ) |
588 | 623 | |
589 | 624 | cdef _coerce_c_impl(self, x): |
… |
… |
|
638 | 673 | False |
639 | 674 | sage: RealIntervalField(10) == RealIntervalField(10) |
640 | 675 | True |
641 | | sage: RealIntervalField(10,sci_not=True) == RealIntervalField(10,sci_not=False) |
| 676 | sage: RealIntervalField(10,scientific_notation=True) == RealIntervalField(10,scientific_notation=False) |
642 | 677 | True |
643 | 678 | sage: RealIntervalField(10) == IntegerRing() |
644 | 679 | False |
… |
… |
|
655 | 690 | """ |
656 | 691 | EXAMPLES:: |
657 | 692 | |
658 | | sage: R = RealIntervalField(sci_not=1, prec=200) |
| 693 | sage: R = RealIntervalField(scientific_notation=1, prec=200) |
659 | 694 | sage: loads(dumps(R)) == R |
660 | 695 | True |
661 | 696 | """ |
662 | | return __create__RealIntervalField_version0, (self.__prec, self.sci_not) |
| 697 | return __create__RealIntervalField_version1, (self.__prec, self.print_options) |
663 | 698 | |
664 | 699 | def random_element(self, *args): |
665 | 700 | """ |
… |
… |
|
881 | 916 | |
882 | 917 | - ``status`` - (bool -) optional flag |
883 | 918 | """ |
| 919 | from sage.misc.misc import deprecation |
884 | 920 | if status is None: |
885 | | return self.sci_not |
886 | | else: |
887 | | self.sci_not = status |
| 921 | deprecation("scientific_notation() is deprecated. Use self.print_options['scientific_notation'] instead") |
| 922 | return self.print_options['scientific_notation'] |
| 923 | else: |
| 924 | deprecation("scientific_notation() is deprecated. Use self.print_options['scientific_notation']=value instead") |
| 925 | self.print_options['scientific_notation'] = status |
888 | 926 | |
889 | 927 | def zeta(self, n=2): |
890 | 928 | """ |
… |
… |
|
1059 | 1097 | sage: a = RIF(5,5.5) |
1060 | 1098 | sage: cmp(loads(dumps(a)), a) |
1061 | 1099 | 0 |
1062 | | sage: R = RealIntervalField(sci_not=1, prec=200) |
| 1100 | sage: R = RealIntervalField(scientific_notation=1, prec=200) |
1063 | 1101 | sage: b = R('393.39203845902384098234098230948209384028340') |
1064 | 1102 | sage: cmp(loads(dumps(b)), b) |
1065 | 1103 | 0 |
… |
… |
|
1201 | 1239 | """ |
1202 | 1240 | return self._parent |
1203 | 1241 | |
1204 | | # MPFR had an elaborate "truncation" scheme to avoid printing |
1205 | | # inaccurate-looking results; this has been removed for MPFI, |
| 1242 | # RealField had an elaborate "truncation" scheme to avoid printing |
| 1243 | # inaccurate-looking results; this has been removed for RealIntervalField, |
1206 | 1244 | # because I think it's less confusing to get such results than to |
1207 | 1245 | # see [0.333 .. 0.333] for an interval with unequal left and right |
1208 | 1246 | # sides. |
1209 | | def str(self, int base=10, style=None, no_sci=None, e=None, error_digits=None): |
| 1247 | def str(self, int base=10, style=None, no_sci=None, e=None, error_digits=None,scientific_notation=None): |
1210 | 1248 | r""" |
1211 | 1249 | INPUT: |
1212 | 1250 | |
… |
… |
|
1352 | 1390 | '[2.1851851851851851 .. 2.1851851851851856]' |
1353 | 1391 | sage: a.str(16) |
1354 | 1392 | '2.2f684bda12f69?' |
1355 | | sage: a.str(no_sci=False) |
| 1393 | sage: a.str(scientific_notation='always') |
1356 | 1394 | '2.185185185185186?e0' |
1357 | 1395 | sage: pi_appr = RIF(pi, 22/7) |
1358 | 1396 | sage: pi_appr.str(style='brackets') |
… |
… |
|
1390 | 1428 | else: |
1391 | 1429 | return "[.. NaN ..]" |
1392 | 1430 | |
| 1431 | |
| 1432 | if no_sci is not None: |
| 1433 | from sage.misc.misc import deprecation |
| 1434 | deprecated_option = {True: 'extreme', False: 'always'} |
| 1435 | deprecation("The no_sci keyword is deprecated. Use scientific_notation=%r instead."%deprecated_option[no_sci]) |
| 1436 | scientific_notation=deprecated_option[no_sci] |
| 1437 | if scientific_notation not in [None, 'always','extreme']: |
| 1438 | raise ValueError, "scientific_notation must be one of 'always', 'extreme', or 'never'." |
| 1439 | elif scientific_notation is None: |
| 1440 | scientific_notation = (<RealIntervalField_class>self._parent).print_options.get('scientific_notation',_PRINT_OPTIONS['scientific_notation']) |
| 1441 | |
| 1442 | |
| 1443 | |
1393 | 1444 | if e is None: |
1394 | 1445 | if base > 10: |
1395 | 1446 | e = '@' |
… |
… |
|
1405 | 1456 | style = 'brackets' |
1406 | 1457 | |
1407 | 1458 | if style == 'brackets': |
1408 | | t1 = self.lower().str(base=base, no_sci=no_sci, e=e, truncate=False) |
1409 | | t2 = self.upper().str(base=base, no_sci=no_sci, e=e, truncate=False) |
| 1459 | t1 = self.lower().str(base=base, scientific_notation=scientific_notation, e=e, truncate=False) |
| 1460 | t2 = self.upper().str(base=base, scientific_notation=scientific_notation, e=e, truncate=False) |
1410 | 1461 | |
1411 | 1462 | return "[%s .. %s]"%(t1, t2) |
1412 | 1463 | |
1413 | 1464 | elif style == 'question': |
1414 | | if no_sci == False: |
| 1465 | if scientific_notation=='always': |
1415 | 1466 | prefer_sci = True |
1416 | | else: |
| 1467 | elif scientific_notation=='extreme': |
1417 | 1468 | prefer_sci = False |
| 1469 | else: |
| 1470 | raise ValueError, "scientific_notation must be 'always' or 'extreme'" |
1418 | 1471 | return self._str_question_style(base, error_digits, e, prefer_sci) |
1419 | 1472 | |
1420 | 1473 | else: |
… |
… |
|
4167 | 4220 | |
4168 | 4221 | |
4169 | 4222 | #### pickle functions |
| 4223 | def __create__RealIntervalField_version1(prec, print_options): |
| 4224 | return RealIntervalField(prec=prec, print_options=print_options) |
| 4225 | |
4170 | 4226 | def __create__RealIntervalField_version0(prec, sci_not): |
4171 | | return RealIntervalField(prec, sci_not) |
| 4227 | print_options={'scientific_notation': sci_not} |
| 4228 | return RealIntervalField(prec=prec, print_options=print_options) |
4172 | 4229 | |
4173 | 4230 | ## Keep all old versions!!! |
4174 | 4231 | def __create__RealIntervalFieldElement_version0(parent, x, base=10): |
diff -r 21f187597161 -r 5a0fc65a6103 sage/rings/real_mpfr.pxd
a
|
b
|
|
16 | 16 | |
17 | 17 | cdef class RealField_class(sage.rings.ring.Field): |
18 | 18 | cdef int __prec |
19 | | cdef bint sci_not |
| 19 | cdef object print_options |
20 | 20 | cdef mpfr_rnd_t rnd |
21 | 21 | cdef object rnd_str |
22 | 22 | cdef RealNumber _new(self) |
diff -r 21f187597161 -r 5a0fc65a6103 sage/rings/real_mpfr.pyx
a
|
b
|
|
24 | 24 | |
25 | 25 | - Robert Bradshaw (2009-09): decimal literals, optimizations |
26 | 26 | |
| 27 | - Jason Grout (2010-01): unified printing option defaults |
| 28 | |
27 | 29 | This is a binding for the MPFR arbitrary-precision floating point |
28 | 30 | library. |
29 | 31 | |
… |
… |
|
194 | 196 | |
195 | 197 | _rounding_modes = ['RNDN', 'RNDZ', 'RNDU', 'RNDD'] |
196 | 198 | |
| 199 | _PRINT_OPTIONS = {'skip_zeroes': False, 'truncate': True, 'scientific_notation': 'extreme'} |
| 200 | |
| 201 | # Remove sci_not_deprecation when removing the sci_not deprecation warnings |
| 202 | sci_not_deprecation = {0: 'extreme', False: 'extreme', 1: 'always', True: 'always'} |
| 203 | |
197 | 204 | cdef object RealField_cache = weakref.WeakValueDictionary() |
198 | 205 | |
199 | | def RealField(int prec=53, int sci_not=0, rnd="RNDN"): |
200 | | """ |
201 | | RealField(prec, sci_not, rnd): |
| 206 | def RealField(int prec=53, scientific_notation=None, rnd="RNDN", truncate=None,skip_zeroes=None, print_options=None, sci_not=None): |
| 207 | """ |
| 208 | RealField(prec, scientific_notation, rnd): |
202 | 209 | |
203 | 210 | INPUT: |
204 | 211 | |
… |
… |
|
210 | 217 | implementation, mpfr_prec_min() is equal to 2. |
211 | 218 | |
212 | 219 | |
213 | | - ``sci_not`` - (default: False) if True, always display using |
214 | | scientific notation; if False, display using scientific notation |
215 | | only for very large or very small numbers |
| 220 | - ``scientific_notation`` |
| 221 | |
| 222 | - 'never' - never print using scientific notation; |
| 223 | |
| 224 | - 'extreme' (default) - print using scientific notation only for |
| 225 | very large or very small numbers; |
| 226 | |
| 227 | - 'always' - always print with scientific notation; |
216 | 228 | |
217 | 229 | - ``rnd`` - (string) the rounding mode |
218 | 230 | |
… |
… |
|
226 | 238 | |
227 | 239 | - RNDU - round towards plus infinity |
228 | 240 | |
| 241 | - ``sci_not`` - deprecated. See ``scientific_notation``. |
| 242 | |
229 | 243 | EXAMPLES:: |
230 | 244 | |
231 | 245 | sage: RealField(10) |
… |
… |
|
243 | 257 | numbers (double type in C), except the default exponent range |
244 | 258 | is much wider and subnormal numbers are not implemented.' |
245 | 259 | """ |
| 260 | printing=_PRINT_OPTIONS.copy() |
| 261 | if print_options is not None: |
| 262 | printing.update(print_options) |
| 263 | if sci_not is not None: |
| 264 | from sage.misc.misc import deprecation |
| 265 | deprecation("The sci_not keyword is deprecated. Use scientific_notation=%r instead."%sci_not_deprecation[sci_not], 'Sage 4.3.2') |
| 266 | scientific_notation=sci_not_deprecation[sci_not] |
| 267 | if scientific_notation is not None: |
| 268 | if scientific_notation not in ['always', 'extreme', 'never']: |
| 269 | raise ValueError, "scientific_notation must be one of 'always', 'extreme', or 'never' (was %r)"%scientific_notation |
| 270 | printing['scientific_notation']=scientific_notation |
| 271 | if truncate is not None: |
| 272 | printing['truncate']=truncate |
| 273 | if skip_zeroes is not None: |
| 274 | printing['skip_zeroes']=skip_zeroes |
| 275 | |
| 276 | print_cached=tuple(sorted(printing.items())) |
246 | 277 | try: |
247 | | return RealField_cache[prec, sci_not, rnd] |
| 278 | return RealField_cache[prec, rnd, print_cached] |
248 | 279 | except KeyError: |
249 | | RealField_cache[prec, sci_not, rnd] = R = RealField_class(prec=prec, sci_not=sci_not, rnd=rnd) |
| 280 | RealField_cache[prec, rnd, print_cached] = R = RealField_class(prec=prec, rnd=rnd, print_options=printing) |
250 | 281 | return R |
251 | 282 | |
252 | 283 | cdef class RealField_class(sage.rings.ring.Field): |
… |
… |
|
262 | 293 | details. |
263 | 294 | """ |
264 | 295 | |
265 | | def __init__(self, int prec=53, int sci_not=0, rnd="RNDN"): |
| 296 | def __init__(self, int prec=53, scientific_notation=None, rnd="RNDN", print_options=None, sci_not=None): |
| 297 | |
| 298 | if sci_not is not None: |
| 299 | from sage.misc.misc import deprecation |
| 300 | deprecation("The sci_not keyword is deprecated. Use print_options={'scientific_notation': %r} instead."%sci_not_deprecation[sci_not], 'Sage 4.3.1') |
| 301 | scientific_notation=sci_not_deprecation[sci_not] |
| 302 | |
266 | 303 | global MY_MPFR_PREC_MAX |
| 304 | if print_options is None: |
| 305 | print_options={} |
| 306 | if 'scientific_notation' in print_options: |
| 307 | if scientific_notation is None: |
| 308 | scientific_notation = print_options['scientific_notation'] |
| 309 | else: |
| 310 | print_options['scientific_notation']=scientific_notation |
| 311 | else: |
| 312 | if scientific_notation is not None: |
| 313 | print_options['scientific_notation']=scientific_notation |
| 314 | |
267 | 315 | cdef RealNumber rn |
268 | 316 | if prec < MPFR_PREC_MIN or prec > MY_MPFR_PREC_MAX: |
269 | 317 | raise ValueError, "prec (=%s) must be >= %s and <= %s."%( |
… |
… |
|
271 | 319 | self.__prec = prec |
272 | 320 | if not isinstance(rnd, str): |
273 | 321 | raise TypeError, "rnd must be a string" |
274 | | self.sci_not = sci_not |
| 322 | self.print_options=print_options |
275 | 323 | n = _rounding_modes.index(rnd) |
276 | 324 | if n == -1: |
277 | 325 | raise ValueError, "rnd (=%s) must be one of RNDN, RNDZ, RNDU, or RNDD"%rnd |
… |
… |
|
314 | 362 | return s |
315 | 363 | |
316 | 364 | def _latex_(self): |
317 | | return "\\Bold{R}" |
| 365 | """ |
| 366 | The latex representation of self. |
| 367 | |
| 368 | EXAMPLE:: |
| 369 | |
| 370 | sage: RealField(100)._latex_() |
| 371 | '\\Bold{R}^{}_{100}' |
| 372 | sage: RealField(100,rnd='RNDD')._latex_() |
| 373 | '\\Bold{R}^{-}_{100}' |
| 374 | sage: RealField(100,rnd='RNDU')._latex_() |
| 375 | '\\Bold{R}^{+}_{100}' |
| 376 | sage: RealField(100,rnd='RNDZ')._latex_() |
| 377 | '\\Bold{R}^{0}_{100}' |
| 378 | sage: RR._latex_() |
| 379 | '\\Bold{R}^{}_{53}' |
| 380 | """ |
| 381 | rnd_str={'RNDN':'', 'RNDZ':'0', 'RNDU':'+', 'RNDD':'-'} |
| 382 | |
| 383 | return "\\Bold{R}^{%s}_{%s}"%(rnd_str[self.rnd_str],self.__prec) |
318 | 384 | |
319 | 385 | def _sage_input_(self, sib, coerce): |
320 | 386 | r""" |
… |
… |
|
357 | 423 | return v |
358 | 424 | |
359 | 425 | cpdef bint is_exact(self) except -2: |
| 426 | """ |
| 427 | Return False, since a real field (representing using finite |
| 428 | precision) is not exact. |
| 429 | |
| 430 | EXAMPLE:: |
| 431 | |
| 432 | sage: RR.is_exact() |
| 433 | False |
| 434 | sage: RealField(100).is_exact() |
| 435 | False |
| 436 | """ |
360 | 437 | return False |
361 | 438 | |
362 | 439 | def _element_constructor_(self, x, base=10): |
… |
… |
|
388 | 465 | """ |
389 | 466 | Canonical coercion of x to this mpfr real field. |
390 | 467 | |
391 | | The rings that canonically coerce to this mpfr real field are: |
392 | | |
393 | | - Any mpfr real field with precision that is as large as this one |
| 468 | The rings that canonically coerce to this MPFR real field are: |
| 469 | |
| 470 | - Any MPFR real field with precision that is as large as this one |
394 | 471 | |
395 | 472 | - int, long, integer, and rational rings. |
396 | 473 | |
… |
… |
|
464 | 541 | |
465 | 542 | def __cmp__(self, other): |
466 | 543 | """ |
| 544 | Compare two real fields, returning True if they are equivalent |
| 545 | and False if they are not. |
| 546 | |
467 | 547 | EXAMPLES:: |
468 | 548 | |
469 | 549 | sage: RealField(10) == RealField(11) |
… |
… |
|
478 | 558 | |
479 | 559 | :: |
480 | 560 | |
481 | | sage: RealField(10,sci_not=True) == RealField(10,sci_not=False) |
| 561 | sage: RealField(10,scientific_notation='always') == RealField(10,scientific_notation='extreme') |
482 | 562 | True |
483 | 563 | sage: RealField(10) == IntegerRing() |
484 | 564 | False |
485 | 565 | |
486 | 566 | :: |
487 | 567 | |
488 | | sage: RS = RealField(sci_not=True) |
| 568 | sage: RS = RealField(scientific_notation='always') |
489 | 569 | sage: RR == RS |
490 | 570 | True |
491 | | sage: RS.scientific_notation(False) |
| 571 | sage: RS.print_options['scientific_notation']='extreme' |
492 | 572 | sage: RR == RS |
493 | 573 | True |
494 | 574 | """ |
… |
… |
|
497 | 577 | cdef RealField_class _other |
498 | 578 | _other = other # to access C structure |
499 | 579 | if self.__prec == _other.__prec and self.rnd == _other.rnd: \ |
500 | | #and self.sci_not == _other.sci_not: |
501 | 580 | return 0 |
502 | 581 | return 1 |
503 | 582 | |
504 | 583 | def __reduce__(self): |
505 | 584 | """ |
506 | | EXAMPLES:: |
507 | | |
508 | | sage: R = RealField(sci_not=1, prec=200, rnd='RNDU') |
| 585 | Return the arguments sufficient for pickling. |
| 586 | |
| 587 | EXAMPLES:: |
| 588 | |
| 589 | sage: R = RealField(scientific_notation='always', prec=200, rnd='RNDU') |
509 | 590 | sage: loads(dumps(R)) == R |
510 | 591 | True |
511 | 592 | """ |
512 | | return __create__RealField_version0, (self.__prec, self.sci_not, self.rnd_str) |
| 593 | return __create__RealField_version1, (self.__prec, self.rnd_str, self.print_options) |
513 | 594 | |
514 | 595 | def construction(self): |
515 | 596 | """ |
516 | | Returns the functorial construction of self, namely, completion of |
517 | | the rational numbers with respect to the prime at |
518 | | `\infty`. |
| 597 | Returns the functorial construction of self, namely, |
| 598 | completion of the rational numbers with respect to the prime |
| 599 | at `\infty`. |
519 | 600 | |
520 | 601 | Also preserves other information that makes this field unique (e.g. |
521 | 602 | precision, rounding, print mode). |
… |
… |
|
531 | 612 | from sage.categories.pushout import CompletionFunctor |
532 | 613 | return (CompletionFunctor(sage.rings.infinity.Infinity, |
533 | 614 | self.prec(), |
534 | | {'sci_not': self.scientific_notation(), 'rnd': self.rounding_mode()}), |
| 615 | {'rnd': self.rounding_mode(),'print_options': self.print_options}), |
535 | 616 | sage.rings.rational_field.QQ) |
536 | 617 | |
537 | 618 | def gen(self, i=0): |
| 619 | """ |
| 620 | Return the generators of self. |
| 621 | |
| 622 | EXAMPLES:: |
| 623 | |
| 624 | sage: R=RealField(100) |
| 625 | sage: R.gen(0) |
| 626 | 1.0000000000000000000000000000 |
| 627 | sage: R.gen(1) |
| 628 | Traceback (most recent call last): |
| 629 | ... |
| 630 | IndexError: self has only one generator |
| 631 | """ |
538 | 632 | if i == 0: |
539 | 633 | return self(1) |
540 | 634 | else: |
541 | | raise IndexError |
| 635 | raise IndexError, "self has only one generator" |
542 | 636 | |
543 | 637 | def complex_field(self): |
544 | 638 | """ |
545 | 639 | Return complex field of the same precision. |
| 640 | |
| 641 | EXAMPLES:: |
| 642 | |
| 643 | sage: RR.complex_field() |
| 644 | Complex Field with 53 bits of precision |
| 645 | sage: RR.complex_field() is CC |
| 646 | True |
| 647 | sage: RealField(100,rnd='RNDD').complex_field() |
| 648 | Complex Field with 100 bits of precision |
| 649 | sage: RealField(100).complex_field() |
| 650 | Complex Field with 100 bits of precision |
546 | 651 | """ |
547 | 652 | import sage.rings.complex_field |
548 | 653 | return sage.rings.complex_field.ComplexField(self.prec()) |
549 | 654 | |
550 | 655 | def algebraic_closure(self): |
551 | 656 | """ |
552 | | Returns the algebraic closure of self, ie the complex field with |
| 657 | Returns the algebraic closure of self, i.e., the complex field with |
553 | 658 | the same precision. |
| 659 | |
| 660 | EXAMPLES:: |
| 661 | |
| 662 | sage: RR.algebraic_closure() |
| 663 | Complex Field with 53 bits of precision |
| 664 | sage: RR.algebraic_closure() is CC |
| 665 | True |
| 666 | sage: RealField(100,rnd='RNDD').algebraic_closure() |
| 667 | Complex Field with 100 bits of precision |
| 668 | sage: RealField(100).algebraic_closure() |
| 669 | Complex Field with 100 bits of precision |
554 | 670 | """ |
555 | 671 | return self.complex_field() |
556 | 672 | |
557 | 673 | def ngens(self): |
| 674 | """ |
| 675 | Return the number of generators. |
| 676 | |
| 677 | EXAMPLES:: |
| 678 | |
| 679 | sage: RR.ngens() |
| 680 | 1 |
| 681 | """ |
558 | 682 | return 1 |
559 | 683 | |
560 | 684 | def gens(self): |
| 685 | """ |
| 686 | Return a list of generators. |
| 687 | |
| 688 | EXAMPLE:: |
| 689 | |
| 690 | sage: RR.gens() |
| 691 | [1.00000000000000] |
| 692 | """ |
561 | 693 | return [self.gen()] |
562 | 694 | |
563 | 695 | def _is_valid_homomorphism_(self, codomain, im_gens): |
| 696 | """ |
| 697 | Return True if the map from self to codomain sending |
| 698 | self(1) to the unique element of im_gens is a valid field |
| 699 | homomorphism. Otherwise, return False. |
| 700 | |
| 701 | EXAMPLES:: |
| 702 | |
| 703 | sage: RR._is_valid_homomorphism_(RDF,[RDF(1)]) |
| 704 | True |
| 705 | sage: RR._is_valid_homomorphism_(CDF,[CDF(1)]) |
| 706 | True |
| 707 | sage: RR._is_valid_homomorphism_(CDF,[CDF(-1)]) |
| 708 | False |
| 709 | sage: R=RealField(100) |
| 710 | sage: RR._is_valid_homomorphism_(R,[R(1)]) |
| 711 | False |
| 712 | sage: RR._is_valid_homomorphism_(CC,[CC(1)]) |
| 713 | True |
| 714 | sage: RR._is_valid_homomorphism_(GF(2),GF(2)(1)) |
| 715 | False |
| 716 | """ |
| 717 | |
564 | 718 | try: |
565 | 719 | s = codomain.coerce(self(1)) |
566 | 720 | except TypeError: |
… |
… |
|
603 | 757 | return 0 |
604 | 758 | |
605 | 759 | def name(self): |
| 760 | """ |
| 761 | Returns the name of self, which encodes the precision and |
| 762 | rounding convention. |
| 763 | |
| 764 | EXAMPLES:: |
| 765 | |
| 766 | sage: RR.name() |
| 767 | 'RealField53_0' |
| 768 | sage: RealField(100,rnd='RNDU').name() |
| 769 | 'RealField100_2' |
| 770 | """ |
606 | 771 | return "RealField%s_%s"%(self.__prec,self.rnd) |
607 | 772 | |
608 | 773 | def __hash__(self): |
| 774 | """ |
| 775 | Returns a hash function of the field, which takes into account |
| 776 | the precision and rounding convention. |
| 777 | |
| 778 | EXAMPLES:: |
| 779 | |
| 780 | sage: hash(RealField(100,rnd='RNDU')) |
| 781 | 5446556154562330496 # 64-bit |
| 782 | 703053696 # 32-bit |
| 783 | sage: hash(RR) |
| 784 | -688242835116843476 # 64-bit |
| 785 | -1247338964 # 32-bit |
| 786 | """ |
609 | 787 | return hash(self.name()) |
610 | 788 | |
611 | 789 | def precision(self): |
| 790 | """ |
| 791 | Returns the precision of self. |
| 792 | |
| 793 | EXAMPLES:: |
| 794 | |
| 795 | sage: RR.precision() |
| 796 | 53 |
| 797 | sage: RealField(20).precision() |
| 798 | 20 |
| 799 | """ |
612 | 800 | return self.__prec |
613 | 801 | |
614 | | def prec(self): |
615 | | return self.__prec |
| 802 | prec=precision # an alias |
616 | 803 | |
617 | 804 | def _magma_init_(self, magma): |
618 | 805 | r""" |
619 | 806 | Return a string representation of self in the Magma language. |
| 807 | |
| 808 | .. warning:: This ignores the rounding convention of self. |
620 | 809 | |
621 | 810 | EXAMPLES:: |
622 | 811 | |
… |
… |
|
631 | 820 | |
632 | 821 | def to_prec(self, prec): |
633 | 822 | """ |
634 | | Returns the real field that is identical to self, except at the |
635 | | specified precision. |
| 823 | Returns the real field that is identical to self, except that |
| 824 | it has the specified precision. |
636 | 825 | |
637 | 826 | EXAMPLES:: |
638 | 827 | |
… |
… |
|
645 | 834 | if prec == self.__prec: |
646 | 835 | return self |
647 | 836 | else: |
648 | | return RealField(prec, self.sci_not, _rounding_modes[self.rnd]) |
| 837 | return RealField(prec, rnd=_rounding_modes[self.rnd], |
| 838 | print_options=self.print_options) |
649 | 839 | |
650 | 840 | # int mpfr_const_pi (mpfr_t rop, mp_rnd_t rnd) |
651 | 841 | def pi(self): |
… |
… |
|
718 | 908 | # int mpfr_const_log2 (mpfr_t rop, mp_rnd_t rnd) |
719 | 909 | def log2(self): |
720 | 910 | """ |
721 | | Returns log(2) to the precision of this field. |
| 911 | Returns log(2) (i.e., the natural log of 2) to the precision |
| 912 | of this field. |
722 | 913 | |
723 | 914 | EXAMPLES:: |
724 | 915 | |
… |
… |
|
740 | 931 | Returns a uniformly distributed random number between min and max |
741 | 932 | (default -1 to 1). |
742 | 933 | |
| 934 | .. warning:: ``distribution`` is ignored---the random number |
| 935 | is from the uniform distribution. |
| 936 | |
743 | 937 | EXAMPLES:: |
744 | 938 | |
745 | 939 | sage: RealField(100).random_element(-5, 10) |
… |
… |
|
793 | 987 | |
794 | 988 | def factorial(self, int n): |
795 | 989 | """ |
796 | | Return the factorial of the integer n as a real number. |
| 990 | Return the factorial of the integer ``n`` as a real number. |
| 991 | |
| 992 | EXAMPLES:: |
| 993 | |
| 994 | sage: RR.factorial(0) |
| 995 | 1.00000000000000 |
| 996 | sage: RR.factorial(1000000) |
| 997 | 8.26393168833124e5565708 |
| 998 | sage: RR.factorial(-1) |
| 999 | Traceback (most recent call last): |
| 1000 | ... |
| 1001 | ArithmeticError: n must be nonnegative |
797 | 1002 | """ |
798 | 1003 | cdef RealNumber x |
799 | 1004 | if n < 0: |
… |
… |
|
805 | 1010 | return x |
806 | 1011 | |
807 | 1012 | def rounding_mode(self): |
| 1013 | """ |
| 1014 | Return the rounding mode. |
| 1015 | |
| 1016 | EXAMPLES:: |
| 1017 | |
| 1018 | sage: RR.rounding_mode() |
| 1019 | 'RNDN' |
| 1020 | sage: RealField(20,rnd='RNDZ').rounding_mode() |
| 1021 | 'RNDZ' |
| 1022 | sage: RealField(20,rnd='RNDU').rounding_mode() |
| 1023 | 'RNDU' |
| 1024 | sage: RealField(20,rnd='RNDD').rounding_mode() |
| 1025 | 'RNDD' |
| 1026 | """ |
808 | 1027 | return _rounding_modes[self.rnd] |
809 | 1028 | |
810 | 1029 | def scientific_notation(self, status=None): |
811 | 1030 | """ |
| 1031 | .. warning:: Deprecated. See :meth:`printing_options` for |
| 1032 | this functionality. |
| 1033 | |
812 | 1034 | Set or return the scientific notation printing flag. If this flag |
813 | 1035 | is True then real numbers with this space as parent print using |
814 | 1036 | scientific notation. |
815 | 1037 | |
816 | 1038 | INPUT: |
817 | | |
818 | | |
| 1039 | |
819 | 1040 | - ``status`` - (bool -) optional flag |
820 | | """ |
| 1041 | |
| 1042 | EXAMPLES:: |
| 1043 | |
| 1044 | sage: RR.scientific_notation() |
| 1045 | doctest:1172: DeprecationWarning: (Since Sage 4.3.1) scientific_notation() is deprecated. Use self.print_options['scientific_notation'] |
| 1046 | True |
| 1047 | |
| 1048 | """ |
| 1049 | from sage.misc.misc import deprecation |
821 | 1050 | if status is None: |
822 | | return self.sci_not |
823 | | else: |
824 | | self.sci_not = status |
825 | | |
| 1051 | deprecation("scientific_notation() is deprecated. Use self.print_options['scientific_notation']", 'Sage 4.3.1') |
| 1052 | return self.print_options.get('scientific_notation',_PRINT_OPTIONS['scientific_notation'])!='always' |
| 1053 | else: |
| 1054 | deprecation("scientific_notation() is deprecated. Use self.print_options['scientific_notation']=value instead", 'Sage 4.3.1') |
| 1055 | if status is True: |
| 1056 | self.print_options['scientific_notation'] = 'always' |
| 1057 | else: |
| 1058 | self.print_options['scientific_notation'] = 'extreme' |
| 1059 | |
| 1060 | |
| 1061 | |
| 1062 | property print_options: |
| 1063 | """ |
| 1064 | Options for printing a real number. |
| 1065 | """ |
| 1066 | def __get__(self): |
| 1067 | """ |
| 1068 | Get the printing options. |
| 1069 | """ |
| 1070 | return self.print_options |
| 1071 | def __set__(self,newval): |
| 1072 | """ |
| 1073 | Set the printing options. |
| 1074 | """ |
| 1075 | self.print_options=newval |
| 1076 | def __del__(self): |
| 1077 | """ |
| 1078 | Reset the printing options to be empty. |
| 1079 | """ |
| 1080 | self.print_options={} |
| 1081 | |
826 | 1082 | def zeta(self, n=2): |
827 | 1083 | """ |
828 | 1084 | Return an `n`-th root of unity in the real field, if one |
… |
… |
|
1062 | 1318 | """ |
1063 | 1319 | EXAMPLES:: |
1064 | 1320 | |
1065 | | sage: R = RealField(sci_not=1, prec=200, rnd='RNDU') |
| 1321 | sage: R = RealField(scientific_notation='always', prec=200, rnd='RNDU') |
1066 | 1322 | sage: b = R('393.39203845902384098234098230948209384028340') |
1067 | 1323 | sage: loads(dumps(b)) == b |
1068 | 1324 | True |
… |
… |
|
1079 | 1335 | sage: loads(dumps(b)) == b |
1080 | 1336 | True |
1081 | 1337 | """ |
1082 | | s = self.str(32, no_sci=False, e='@') |
| 1338 | s = self.str(32, scientific_notation='always', e='@') |
1083 | 1339 | return (__create__RealNumber_version0, (self._parent, s, 32)) |
1084 | 1340 | |
1085 | 1341 | def __dealloc__(self): |
… |
… |
|
1120 | 1376 | sage: s1 == RR(gp(s1)) |
1121 | 1377 | True |
1122 | 1378 | """ |
1123 | | return self.str(10, no_sci=True, truncate=False) |
| 1379 | return self.str(10, scientific_notation='extreme', truncate=False) |
1124 | 1380 | |
1125 | 1381 | def _sage_input_(self, sib, coerced): |
1126 | 1382 | r""" |
… |
… |
|
1321 | 1577 | """ |
1322 | 1578 | return self._parent |
1323 | 1579 | |
1324 | | def str(self, int base=10, no_sci=None, e=None, int truncate=1, bint skip_zeroes=0): |
| 1580 | def str(self, int base=10, no_sci=None, e=None, truncate=None, skip_zeroes=None,scientific_notation=None): |
1325 | 1581 | """ |
1326 | 1582 | INPUT: |
1327 | 1583 | |
1328 | 1584 | |
1329 | 1585 | - ``base`` - base for output |
1330 | 1586 | |
1331 | | - ``no_sci`` - if 2, never print using scientific |
1332 | | notation; if 1 or True, print using scientific notation only for |
1333 | | very large or very small numbers; if 0 or False always print with |
1334 | | scientific notation; if None (the default), print how the parent |
1335 | | prints. |
| 1587 | - ``scientific_notation`` - (default: None) |
| 1588 | |
| 1589 | - 'never' - never print using scientific notation; |
| 1590 | |
| 1591 | - 'extreme' - print using scientific notation only for very |
| 1592 | large or very small numbers; |
| 1593 | |
| 1594 | - 'always' - always print with scientific notation; |
| 1595 | |
| 1596 | - None (default) - print how the parent prints. |
1336 | 1597 | |
1337 | 1598 | - ``e`` - symbol used in scientific notation; defaults to 'e' for |
1338 | 1599 | base=10, and '@' otherwise |
… |
… |
|
1350 | 1611 | '20.333333333333332' |
1351 | 1612 | sage: a.str(2) |
1352 | 1613 | '10100.010101010101010101010101010101010101010101010101' |
1353 | | sage: a.str(no_sci=False) |
| 1614 | sage: a.str(scientific_notation='always') |
1354 | 1615 | '2.03333333333333e1' |
1355 | | sage: a.str(16, no_sci=False) |
| 1616 | sage: a.str(16, scientific_notation='always') |
1356 | 1617 | '1.4555555555555@1' |
1357 | 1618 | sage: b = 2.0^99 |
1358 | 1619 | sage: b.str() |
1359 | 1620 | '6.33825300114115e29' |
1360 | | sage: b.str(no_sci=False) |
| 1621 | sage: b.str(scientific_notation='always') |
1361 | 1622 | '6.33825300114115e29' |
1362 | | sage: b.str(no_sci=True) |
| 1623 | sage: b.str(scientific_notation='extreme') |
1363 | 1624 | '6.33825300114115e29' |
1364 | 1625 | sage: c = 2.0^100 |
1365 | 1626 | sage: c.str() |
1366 | 1627 | '1.26765060022823e30' |
1367 | | sage: c.str(no_sci=False) |
| 1628 | sage: c.str(scientific_notation='always') |
1368 | 1629 | '1.26765060022823e30' |
1369 | | sage: c.str(no_sci=True) |
| 1630 | sage: c.str(scientific_notation='extreme') |
1370 | 1631 | '1.26765060022823e30' |
1371 | | sage: c.str(no_sci=2) |
| 1632 | sage: c.str(scientific_notation='never') |
1372 | 1633 | '1267650600228230000000000000000.' |
1373 | 1634 | sage: 0.5^53 |
1374 | 1635 | 1.11022302462516e-16 |
… |
… |
|
1387 | 1648 | """ |
1388 | 1649 | if base < 2 or base > 36: |
1389 | 1650 | raise ValueError, "the base (=%s) must be between 2 and 36"%base |
| 1651 | |
| 1652 | if no_sci is not None: |
| 1653 | from sage.misc.misc import deprecation |
| 1654 | deprecated_option = {0: 'always', 1: 'extreme', 2: 'never'} |
| 1655 | deprecation("The no_sci keyword is deprecated. Use scientific_notation=%r instead."%deprecated_option[no_sci]) |
| 1656 | scientific_notation=deprecated_option[no_sci] |
| 1657 | if scientific_notation not in [None, 'always','extreme','never']: |
| 1658 | raise ValueError, "scientific_notation must be one of 'always', 'extreme', or 'never'." |
| 1659 | |
1390 | 1660 | if mpfr_nan_p(self.value): |
1391 | 1661 | if base >= 24: |
1392 | 1662 | return "@NaN@" |
… |
… |
|
1404 | 1674 | else: |
1405 | 1675 | e = 'e' |
1406 | 1676 | |
| 1677 | if skip_zeroes is None: |
| 1678 | skip_zeroes = (<RealField_class>self._parent).print_options.get('skip_zeroes',_PRINT_OPTIONS['skip_zeroes']) |
| 1679 | |
| 1680 | if truncate is None: |
| 1681 | truncate = (<RealField_class>self._parent).print_options.get('truncate',_PRINT_OPTIONS['truncate']) |
| 1682 | |
1407 | 1683 | cdef char *s |
1408 | 1684 | cdef mp_exp_t exponent |
1409 | 1685 | |
… |
… |
|
1450 | 1726 | if t[0] == "-": |
1451 | 1727 | digits = digits - 1 |
1452 | 1728 | |
1453 | | if no_sci is None: |
1454 | | no_sci = not (<RealField_class>self._parent).sci_not |
1455 | | |
1456 | | if no_sci is True and ( abs(exponent-1) >=6 ): |
1457 | | no_sci = False |
1458 | | |
1459 | | if no_sci==False: |
| 1729 | if scientific_notation is None: |
| 1730 | scientific_notation = (<RealField_class>self._parent).print_options.get('scientific_notation',_PRINT_OPTIONS['scientific_notation']) |
| 1731 | |
| 1732 | if (scientific_notation=='extreme' and ( abs(exponent-1) >=6 )) \ |
| 1733 | or scientific_notation=='always': |
1460 | 1734 | if t[0] == "-": |
1461 | 1735 | return "-%s.%s%s%s"%(t[1:2], t[2:], e, exponent-1) |
1462 | 1736 | return "%s.%s%s%s"%(t[0], t[1:], e, exponent-1) |
… |
… |
|
1770 | 2044 | return x |
1771 | 2045 | |
1772 | 2046 | cpdef ModuleElement _neg_(self): |
| 2047 | """ |
| 2048 | Return the negative of self. |
| 2049 | |
| 2050 | EXAMPLES:: |
| 2051 | |
| 2052 | sage: RR(1)._neg_() |
| 2053 | -1.00000000000000 |
| 2054 | sage: RR('inf')._neg_() |
| 2055 | -infinity |
| 2056 | sage: RR('nan')._neg_() |
| 2057 | NaN |
| 2058 | """ |
1773 | 2059 | cdef RealNumber x |
1774 | 2060 | x = self._new() |
1775 | 2061 | mpfr_neg(x.value, self.value, (<RealField_class>self._parent).rnd) |
1776 | 2062 | return x |
1777 | 2063 | |
1778 | 2064 | def __abs__(self): |
| 2065 | """ |
| 2066 | Return absolute value of self. |
| 2067 | |
| 2068 | EXAMPLES:: |
| 2069 | |
| 2070 | sage: RR(-1).__abs__() |
| 2071 | 1.00000000000000 |
| 2072 | sage: RR('-inf').__abs__() |
| 2073 | +infinity |
| 2074 | sage: RR('inf').__abs__() |
| 2075 | +infinity |
| 2076 | sage: RR('nan').__abs__() |
| 2077 | NaN |
| 2078 | """ |
1779 | 2079 | return self.abs() |
1780 | 2080 | |
1781 | 2081 | cdef RealNumber abs(RealNumber self): |
| 2082 | """ |
| 2083 | Return the absolute value of self. |
| 2084 | |
| 2085 | EXAMPLES:: |
| 2086 | |
| 2087 | sage: RR('-1').abs() |
| 2088 | 1.00000000000000 |
| 2089 | sage: RR('-inf').abs() |
| 2090 | +infinity |
| 2091 | sage: RR('inf').abs() |
| 2092 | +infinity |
| 2093 | sage: RR('nan').abs() |
| 2094 | NaN |
| 2095 | """ |
1782 | 2096 | cdef RealNumber x |
1783 | 2097 | x = self._new() |
1784 | 2098 | mpfr_abs(x.value, self.value, (<RealField_class>self._parent).rnd) |
… |
… |
|
1786 | 2100 | |
1787 | 2101 | # Bit shifting |
1788 | 2102 | def _lshift_(RealNumber self, n): |
| 2103 | """ |
| 2104 | Return `self*(2^n)` for an integer ``n``. |
| 2105 | |
| 2106 | EXAMPLES:: |
| 2107 | |
| 2108 | sage: RR(1.0)._lshift_(32) |
| 2109 | 4.29496729600000e9 |
| 2110 | sage: RR(1.5)._lshift_(2) |
| 2111 | 6.00000000000000 |
| 2112 | """ |
1789 | 2113 | cdef RealNumber x |
1790 | 2114 | if n > sys.maxint: |
1791 | 2115 | raise OverflowError, "n (=%s) must be <= %s"%(n, sys.maxint) |
1792 | 2116 | x = self._new() |
1793 | | mpfr_mul_2exp(x.value, self.value, n, (<RealField_class>self._parent).rnd) |
| 2117 | mpfr_mul_2ui(x.value, self.value, n, (<RealField_class>self._parent).rnd) |
1794 | 2118 | return x |
1795 | 2119 | |
1796 | 2120 | def __lshift__(x, y): |
1797 | 2121 | """ |
| 2122 | Return `self*(2^n)` for an integer ``n``. |
| 2123 | |
1798 | 2124 | EXAMPLES:: |
1799 | 2125 | |
1800 | 2126 | sage: 1.0 << 32 |
… |
… |
|
1812 | 2138 | raise TypeError, "unsupported operands for <<" |
1813 | 2139 | |
1814 | 2140 | def _rshift_(RealNumber self, n): |
| 2141 | """ |
| 2142 | Return `self/(2^n)` for an integer ``n``. |
| 2143 | |
| 2144 | EXAMPLES:: |
| 2145 | |
| 2146 | sage: RR(1.0)._rshift_(32) |
| 2147 | 2.32830643653870e-10 |
| 2148 | sage: RR(1.5)._rshift_(2) |
| 2149 | 0.375000000000000 |
| 2150 | """ |
1815 | 2151 | if n > sys.maxint: |
1816 | 2152 | raise OverflowError, "n (=%s) must be <= %s"%(n, sys.maxint) |
1817 | 2153 | cdef RealNumber x |
… |
… |
|
1821 | 2157 | |
1822 | 2158 | def __rshift__(x, y): |
1823 | 2159 | """ |
| 2160 | Return `self/(2^n)` for an integer ``n``. |
| 2161 | |
1824 | 2162 | EXAMPLES:: |
1825 | 2163 | |
1826 | 2164 | sage: 1024.0 >> 7 |
… |
… |
|
1875 | 2213 | """ |
1876 | 2214 | return mpfr_sgn(self.value) |
1877 | 2215 | |
1878 | | def prec(self): |
| 2216 | def precision(self): |
| 2217 | """ |
| 2218 | Return the precision of self. |
| 2219 | |
| 2220 | EXAMPLES:: |
| 2221 | |
| 2222 | sage: RR(1.0).precision() |
| 2223 | 53 |
| 2224 | sage: RealField(101)(-1).precision() |
| 2225 | 101 |
| 2226 | """ |
1879 | 2227 | return (<RealField_class>self._parent).__prec |
1880 | 2228 | |
| 2229 | prec = precision # alias |
| 2230 | |
1881 | 2231 | def conjugate(self): |
1882 | 2232 | """ |
1883 | 2233 | Return the complex conjugate of this real number, which is the |
… |
… |
|
2053 | 2403 | mpfr_ceil(x.value, self.value) |
2054 | 2404 | return x.integer_part() |
2055 | 2405 | |
2056 | | def ceiling(self): |
2057 | | return self.ceil() |
| 2406 | ceiling = ceil # alias |
2058 | 2407 | |
2059 | 2408 | def trunc(self): |
2060 | 2409 | """ |
… |
… |
|
2076 | 2425 | |
2077 | 2426 | def frac(self): |
2078 | 2427 | """ |
2079 | | frac returns a real number > -1 and < 1. it satisfies the relation: x = |
2080 | | x.trunc() + x.frac() |
| 2428 | Returns a real number such that `self = self.trunc() + |
| 2429 | self.frac()`. The return value will also satisfy `-1 < self.frac() < 1`. |
2081 | 2430 | |
2082 | 2431 | EXAMPLES:: |
2083 | 2432 | |
… |
… |
|
2087 | 2436 | 0.500000000000000 |
2088 | 2437 | sage: (-2.79).frac() |
2089 | 2438 | -0.790000000000000 |
| 2439 | sage: (-2.79).trunc() + (-2.79).frac() |
| 2440 | -2.79000000000000 |
2090 | 2441 | """ |
2091 | 2442 | cdef RealNumber x |
2092 | 2443 | x = self._new() |
… |
… |
|
2183 | 2534 | ########################################### |
2184 | 2535 | |
2185 | 2536 | def __float__(self): |
| 2537 | """ |
| 2538 | Returns a python float approximating self. |
| 2539 | |
| 2540 | EXAMPLES:: |
| 2541 | |
| 2542 | sage: RR(pi).__float__() |
| 2543 | 3.1415926535897931 |
| 2544 | sage: type(RR(pi).__float__()) |
| 2545 | <type 'float'> |
| 2546 | """ |
2186 | 2547 | return mpfr_get_d(self.value, (<RealField_class>self._parent).rnd) |
2187 | 2548 | |
2188 | 2549 | def _rpy_(self): |
… |
… |
|
2202 | 2563 | |
2203 | 2564 | def __int__(self): |
2204 | 2565 | """ |
2205 | | Returns integer truncation of this real number. |
| 2566 | Returns the python integer truncation of self. |
| 2567 | |
| 2568 | EXAMPLES:: |
| 2569 | |
| 2570 | sage: RR(pi).__int__() |
| 2571 | 3 |
| 2572 | sage: type(RR(pi).__int__()) |
| 2573 | <type 'int'> |
2206 | 2574 | """ |
2207 | 2575 | if not mpfr_number_p(self.value): |
2208 | 2576 | raise ValueError, 'Cannot convert infinity or NaN to Python int' |
… |
… |
|
2213 | 2581 | |
2214 | 2582 | def __long__(self): |
2215 | 2583 | """ |
2216 | | Returns long integer truncation of this real number. |
| 2584 | Returns python long integer truncation of this real number. |
| 2585 | |
| 2586 | EXAMPLES:: |
| 2587 | |
| 2588 | sage: RR(pi).__long__() |
| 2589 | 3L |
| 2590 | sage: type(RR(pi).__long__()) |
| 2591 | <type 'long'> |
2217 | 2592 | """ |
2218 | 2593 | if not mpfr_number_p(self.value): |
2219 | 2594 | raise ValueError, 'Cannot convert infinity or NaN to Python long' |
… |
… |
|
2223 | 2598 | return z.__long__() |
2224 | 2599 | |
2225 | 2600 | def __complex__(self): |
| 2601 | """ |
| 2602 | Returns a python complex number equal to self. |
| 2603 | |
| 2604 | EXAMPLES:: |
| 2605 | |
| 2606 | sage: RR(pi).__complex__() |
| 2607 | (3.1415926535897931+0j) |
| 2608 | sage: type(RR(pi).__complex__()) |
| 2609 | <type 'complex'> |
| 2610 | """ |
| 2611 | |
2226 | 2612 | return complex(float(self)) |
2227 | 2613 | |
2228 | 2614 | def _complex_number_(self): |
| 2615 | """ |
| 2616 | Returns a Sage complex number equal to self. |
| 2617 | |
| 2618 | EXAMPLES:: |
| 2619 | |
| 2620 | sage: RR(pi)._complex_number_() |
| 2621 | 3.14159265358979 |
| 2622 | sage: parent(RR(pi)._complex_number_()) |
| 2623 | Complex Field with 53 bits of precision |
| 2624 | """ |
2229 | 2625 | import sage.rings.complex_field |
2230 | 2626 | return sage.rings.complex_field.ComplexField(self.prec())(self) |
2231 | 2627 | |
… |
… |
|
2892 | 3288 | |
2893 | 3289 | def is_infinity(self): |
2894 | 3290 | """ |
| 3291 | Return True if self is infinite and False otherwise. |
| 3292 | |
2895 | 3293 | EXAMPLES:: |
2896 | 3294 | |
2897 | 3295 | sage: a = RR('1.494') / RR(0); a |
… |
… |
|
2904 | 3302 | True |
2905 | 3303 | sage: RR(1.5).is_infinity() |
2906 | 3304 | False |
| 3305 | sage: RR('nan').is_infinity() |
| 3306 | False |
2907 | 3307 | """ |
2908 | 3308 | return mpfr_inf_p(self.value) |
2909 | 3309 | |
2910 | 3310 | def is_unit(self): |
| 3311 | """ |
| 3312 | Return True if self is a unit (has a multiplicative inverse) |
| 3313 | and False otherwise. |
| 3314 | |
| 3315 | EXAMPLES:: |
| 3316 | |
| 3317 | sage: RR(1).is_unit() |
| 3318 | True |
| 3319 | sage: RR('0').is_unit() |
| 3320 | False |
| 3321 | sage: RR('-0').is_unit() |
| 3322 | False |
| 3323 | sage: RR('nan').is_unit() |
| 3324 | False |
| 3325 | sage: RR('inf').is_unit() |
| 3326 | False |
| 3327 | sage: RR('-inf').is_unit() |
| 3328 | False |
| 3329 | """ |
| 3330 | return mpfr_sgn(self.value) != 0 and not mpfr_inf_p(self.value) |
| 3331 | |
| 3332 | def is_real(self): |
| 3333 | """ |
| 3334 | Return True if self is real (of course, this always returns |
| 3335 | True for a finite element of a real field). |
| 3336 | |
| 3337 | EXAMPLES:: |
| 3338 | |
| 3339 | sage: RR(1).is_real() |
| 3340 | True |
| 3341 | sage: RR('-100').is_real() |
| 3342 | True |
| 3343 | """ |
| 3344 | return True |
| 3345 | |
| 3346 | def __nonzero__(self): |
| 3347 | """ |
| 3348 | Returns True if self is nonzero. |
| 3349 | |
| 3350 | EXAMPLES:: |
| 3351 | |
| 3352 | sage: RR(1).__nonzero__() |
| 3353 | True |
| 3354 | sage: RR(0).__nonzero__() |
| 3355 | False |
| 3356 | sage: RR('inf').__nonzero__() |
| 3357 | True |
| 3358 | """ |
2911 | 3359 | return mpfr_sgn(self.value) != 0 |
2912 | 3360 | |
2913 | | def is_real(self): |
2914 | | return True |
2915 | | |
2916 | | def __nonzero__(self): |
2917 | | return mpfr_sgn(self.value) != 0 |
2918 | | |
2919 | 3361 | def __richcmp__(left, right, int op): |
| 3362 | """ |
| 3363 | Return the cython rich comparison operator (see the cython |
| 3364 | documentation for details). |
| 3365 | |
| 3366 | EXAMPLES:: |
| 3367 | |
| 3368 | sage: RR('-inf')<RR('inf') |
| 3369 | True |
| 3370 | sage: RR('-inf')<RR('-10000') |
| 3371 | True |
| 3372 | sage: RR('100000000')<RR('inf') |
| 3373 | True |
| 3374 | sage: RR('100000000')>RR('inf') |
| 3375 | False |
| 3376 | sage: RR('100000000')<RR('inf') |
| 3377 | True |
| 3378 | sage: RR('nan')==RR('nan') |
| 3379 | True |
| 3380 | sage: RR('-1000')<RR('1000') |
| 3381 | True |
| 3382 | sage: RR('1')<RR('1').nextabove() |
| 3383 | True |
| 3384 | sage: RR('1')<=RR('1').nextabove() |
| 3385 | True |
| 3386 | sage: RR('1')<=RR('1') |
| 3387 | True |
| 3388 | sage: RR('1')<RR('1') |
| 3389 | False |
| 3390 | sage: RR('1')>RR('1') |
| 3391 | False |
| 3392 | sage: RR('1')>=RR('1') |
| 3393 | True |
| 3394 | """ |
2920 | 3395 | return (<RingElement>left)._richcmp(right, op) |
2921 | 3396 | |
2922 | 3397 | cdef int _cmp_c_impl(left, Element right) except -2: |
| 3398 | """ |
| 3399 | Returns -1 if exactly one of the numbers is NaN. Return -1 if |
| 3400 | left is less than right, 0 if left and right are equal, and 1 |
| 3401 | if left is greater than right. |
| 3402 | |
| 3403 | EXAMPLES:: |
| 3404 | |
| 3405 | sage: RR('1')<=RR('1') |
| 3406 | True |
| 3407 | sage: RR('1')<RR('1') |
| 3408 | False |
| 3409 | sage: RR('1')>RR('1') |
| 3410 | False |
| 3411 | sage: RR('1')>=RR('1') |
| 3412 | True |
| 3413 | sage: RR('nan')==R('nan') |
| 3414 | False |
| 3415 | sage: RR('inf')==RR('inf') |
| 3416 | True |
| 3417 | sage: RR('inf')==RR('-inf') |
| 3418 | False |
| 3419 | """ |
2923 | 3420 | cdef RealNumber self, x |
2924 | 3421 | self = left |
2925 | 3422 | x = right |
… |
… |
|
3048 | 3545 | return x |
3049 | 3546 | |
3050 | 3547 | def __pow(self, RealNumber exponent): |
| 3548 | """ |
| 3549 | Compute self raised to the power of exponent, rounded in the |
| 3550 | direction specified by the parent of self. |
| 3551 | |
| 3552 | EXAMPLES:: |
| 3553 | |
| 3554 | sage: R = RealField(30) |
| 3555 | sage: a = R('1.23456') |
| 3556 | sage: a.__pow(20.0) |
| 3557 | 67.646297 |
| 3558 | """ |
3051 | 3559 | cdef RealNumber x |
3052 | 3560 | x = self._new() |
3053 | 3561 | _sig_on |
… |
… |
|
3215 | 3723 | |
3216 | 3724 | def log1p(self): |
3217 | 3725 | """ |
3218 | | Returns log of 1 + self |
| 3726 | Returns log base e of 1 + self |
3219 | 3727 | |
3220 | 3728 | EXAMPLES:: |
3221 | 3729 | |
… |
… |
|
4534 | 5042 | |
4535 | 5043 | |
4536 | 5044 | # here because this imports the other two real fields |
4537 | | def create_RealField(prec=53, type="MPFR", rnd="RNDN", sci_not=0): |
| 5045 | def create_RealField(prec=53, type="MPFR", rnd="RNDN", scientific_notation=None, print_options={},sci_not=None): |
| 5046 | """ |
| 5047 | A generic constructor that returns the appropriate real field. |
| 5048 | |
| 5049 | INPUTS: |
| 5050 | |
| 5051 | - ``type`` |
| 5052 | |
| 5053 | - ``MPFR`` - A real field with precision ``prec`` and the |
| 5054 | specified printing options. |
| 5055 | - ``RDF`` - A real field with double precision |
| 5056 | - ``RQDF`` - A real field with quad double precision |
| 5057 | - ``RLF`` - A real lazy field |
| 5058 | - ``Interval`` - A real interval field with ``prec`` precision |
| 5059 | and the specified printing options |
| 5060 | """ |
4538 | 5061 | if type == "RDF": |
4539 | 5062 | return RDF |
4540 | 5063 | elif type == "RQDF": |
… |
… |
|
4542 | 5065 | return RQDF |
4543 | 5066 | elif type == "Interval": |
4544 | 5067 | from real_mpfi import RealIntervalField |
4545 | | return RealIntervalField(prec, sci_not) |
| 5068 | return RealIntervalField(prec=prec, scientific_notation=scientific_notation, print_options=print_options, sci_not=sci_not) |
4546 | 5069 | elif type == "RLF": |
4547 | 5070 | from real_lazy import RLF |
4548 | 5071 | return RLF |
4549 | 5072 | else: |
4550 | | return RealField(prec, sci_not, rnd) |
| 5073 | return RealField(prec=prec, scientific_notation=scientific_notation, rnd=rnd, print_options=print_options, sci_not=sci_not) |
4551 | 5074 | |
4552 | 5075 | |
4553 | 5076 | def is_RealField(x): |
| 5077 | """ |
| 5078 | Returns True if ``x`` is technically of a python real field type. |
| 5079 | |
| 5080 | EXAMPLES:: |
| 5081 | |
| 5082 | sage: sage.rings.real_mpfr.is_RealField(RR) |
| 5083 | True |
| 5084 | sage: sage.rings.real_mpfr.is_RealField(CC) |
| 5085 | False |
| 5086 | """ |
4554 | 5087 | return PY_TYPE_CHECK(x, RealField_class) |
4555 | 5088 | |
4556 | 5089 | def is_RealNumber(x): |
… |
… |
|
4572 | 5105 | """ |
4573 | 5106 | return PY_TYPE_CHECK(x, RealNumber) |
4574 | 5107 | |
| 5108 | def __create__RealField_version1(prec, rnd, print_options): |
| 5109 | """ |
| 5110 | Return a real field with precision ``prec``, rounding ``rnd``, and |
| 5111 | print options ``print_options``. |
| 5112 | |
| 5113 | EXAMPLES:: |
| 5114 | |
| 5115 | sage: sage.rings.real_mpfr.__create__RealField_version1(50, 'RNDD',{}) |
| 5116 | Real Field with 50 bits of precision and rounding RNDD |
| 5117 | """ |
| 5118 | return RealField(prec=prec, rnd=rnd, print_options=print_options) |
| 5119 | |
4575 | 5120 | def __create__RealField_version0(prec, sci_not, rnd): |
4576 | | return RealField(prec, sci_not, rnd) |
| 5121 | """ |
| 5122 | Return a real field with precision ``prec``, rounding ``rnd``, and |
| 5123 | the specified scientific notation setting. |
| 5124 | |
| 5125 | EXAMPLES:: |
| 5126 | |
| 5127 | sage: sage.rings.real_mpfr.__create__RealField_version1(50, 'RNDD',{}) |
| 5128 | Real Field with 50 bits of precision and rounding RNDD |
| 5129 | sage: R=sage.rings.real_mpfr.__create__RealField_version0(50, True, 'RNDD') |
| 5130 | sage: R.print_options |
| 5131 | {'scientific_notation': 'always', 'skip_zeroes': False, 'truncate': True} |
| 5132 | """ |
| 5133 | print_options={'scientific_notation':sci_not_deprecation[sci_not]} |
| 5134 | return RealField(prec=prec, rnd=rnd, print_options=print_options) |
4577 | 5135 | |
4578 | 5136 | def __create__RealNumber_version0(parent, x, base=10): |
| 5137 | """ |
| 5138 | Create a real number. |
| 5139 | |
| 5140 | EXAMPLES:: |
| 5141 | sage: sage.rings.real_mpfr.__create__RealNumber_version0(RR, 22,base=3) |
| 5142 | 22.0000000000000 |
| 5143 | """ |
4579 | 5144 | return RealNumber(parent, x, base=base) |
4580 | 5145 | |
4581 | 5146 | |
4582 | 5147 | cdef inline RealNumber empty_RealNumber(RealField_class parent): |
| 5148 | """ |
| 5149 | Create and return an empty initialized real number. |
| 5150 | |
| 5151 | EXAMPLES: |
| 5152 | |
| 5153 | These are indirect tests of this function. |
| 5154 | |
| 5155 | sage: from sage.rings.real_mpfr import RRtoRR |
| 5156 | sage: R10 = RealField(10) |
| 5157 | sage: R100 = RealField(100) |
| 5158 | sage: f = RRtoRR(R100, R10) |
| 5159 | sage: a = R100(1.2) |
| 5160 | sage: f(a) |
| 5161 | 1.2 |
| 5162 | sage: g = f.section() |
| 5163 | sage: g |
| 5164 | Generic map: |
| 5165 | From: Real Field with 10 bits of precision |
| 5166 | To: Real Field with 100 bits of precision |
| 5167 | sage: g(f(a)) |
| 5168 | 1.1992187500000000000000000000 |
| 5169 | sage: b = R10(2).sqrt() |
| 5170 | sage: f(g(b)) |
| 5171 | 1.4 |
| 5172 | sage: f(g(b)) == b |
| 5173 | True |
| 5174 | """ |
| 5175 | |
4583 | 5176 | cdef RealNumber y = <RealNumber>PY_NEW(RealNumber) |
4584 | 5177 | y._parent = parent |
4585 | 5178 | mpfr_init2(y.value, parent.__prec) |