Arbitrary precision real intervals using MPFI¶
AUTHORS:
Carl Witty (2007-01-21): based on
real_mpfr.pyx
; changed it to use mpfi rather than mpfr.William Stein (2007-01-24): modifications and clean up and docs, etc.
Niles Johnson (2010-08): Issue #3893:
random_element()
should pass on*args
and**kwds
.Travis Scrimshaw (2012-10-20): Fixing scientific notation output to fix Issue #13634.
Travis Scrimshaw (2012-11-02): Added doctests for full coverage
This is a straightforward binding to the MPFI library; it may be useful to refer to its documentation for more details.
An interval is represented as a pair of floating-point numbers \(a\)
and \(b\) (where \(a \leq b\)) and is printed as a standard floating-point
number with a question mark (for instance, 3.1416?
). The question
mark indicates that the preceding digit may have an error of \(\pm 1\).
These floating-point numbers are implemented using MPFR (the same
as the RealNumber
elements of
RealField_class
).
There is also an alternate method of printing, where the interval
prints as [a .. b]
(for instance, [3.1415 .. 3.1416]
).
The interval represents the set \(\{ x : a \leq x \leq b \}\) (so if \(a = b\),
then the interval represents that particular floating-point number). The
endpoints can include positive and negative infinity, with the
obvious meaning. It is also possible to have a NaN
(Not-a-Number)
interval, which is represented by having either endpoint be NaN
.
PRINTING:
There are two styles for printing intervals: ‘brackets’ style and ‘question’ style (the default).
In question style, we print the “known correct” part of the number, followed by a question mark. The question mark indicates that the preceding digit is possibly wrong by \(\pm 1\).
sage: RIF(sqrt(2)) # needs sage.symbolic
1.414213562373095?
>>> from sage.all import *
>>> RIF(sqrt(Integer(2))) # needs sage.symbolic
1.414213562373095?
However, if the interval is precise (its lower bound is equal to its upper bound) and equal to a not-too-large integer, then we just print that integer.
sage: RIF(0)
0
sage: RIF(654321)
654321
>>> from sage.all import *
>>> RIF(Integer(0))
0
>>> RIF(Integer(654321))
654321
sage: RIF(123, 125)
124.?
sage: RIF(123, 126)
1.3?e2
>>> from sage.all import *
>>> RIF(Integer(123), Integer(125))
124.?
>>> RIF(Integer(123), Integer(126))
1.3?e2
As we see in the last example, question style can discard almost a whole digit’s worth of precision. We can reduce this by allowing “error digits”: an error following the question mark, that gives the maximum error of the digit(s) before the question mark. If the error is absent (which it always is in the default printing), then it is taken to be 1.
sage: RIF(123, 126).str(error_digits=1)
'125.?2'
sage: RIF(123, 127).str(error_digits=1)
'125.?2'
sage: v = RIF(-e, pi); v # needs sage.symbolic
0.?e1
sage: v.str(error_digits=1) # needs sage.symbolic
'1.?4'
sage: v.str(error_digits=5) # needs sage.symbolic
'0.2117?29300'
>>> from sage.all import *
>>> RIF(Integer(123), Integer(126)).str(error_digits=Integer(1))
'125.?2'
>>> RIF(Integer(123), Integer(127)).str(error_digits=Integer(1))
'125.?2'
>>> v = RIF(-e, pi); v # needs sage.symbolic
0.?e1
>>> v.str(error_digits=Integer(1)) # needs sage.symbolic
'1.?4'
>>> v.str(error_digits=Integer(5)) # needs sage.symbolic
'0.2117?29300'
Error digits also sometimes let us indicate that the interval is actually equal to a single floating-point number:
sage: RIF(54321/256)
212.19140625000000?
sage: RIF(54321/256).str(error_digits=1)
'212.19140625000000?0'
>>> from sage.all import *
>>> RIF(Integer(54321)/Integer(256))
212.19140625000000?
>>> RIF(Integer(54321)/Integer(256)).str(error_digits=Integer(1))
'212.19140625000000?0'
In brackets style, intervals are printed with the left value rounded down and the right rounded up, which is conservative, but in some ways unsatisfying.
Consider a 3-bit interval containing exactly the floating-point
number 1.25. In round-to-nearest or round-down, this prints as 1.2;
in round-up, this prints as 1.3. The straightforward options, then,
are to print this interval as [1.2 .. 1.2]
(which does not even
contain the true value, 1.25), or to print it as [1.2 .. 1.3]
(which gives the impression that the upper and lower bounds are not
equal, even though they really are). Neither of these is very
satisfying, but we have chosen the latter.
sage: R = RealIntervalField(3)
sage: a = R(1.25)
sage: a.str(style='brackets')
'[1.2 .. 1.3]'
sage: a == 5/4
True
sage: a == 2
False
>>> from sage.all import *
>>> R = RealIntervalField(Integer(3))
>>> a = R(RealNumber('1.25'))
>>> a.str(style='brackets')
'[1.2 .. 1.3]'
>>> a == Integer(5)/Integer(4)
True
>>> a == Integer(2)
False
COMPARISONS:
Comparison operations (==
, !=
, <
, <=
, >
, >=
)
return True
if every value in the first interval has the given relation
to every value in the second interval.
This convention for comparison operators has good and bad points. The good:
Expected transitivity properties hold (if
a > b
andb == c
, thena > c
; etc.)a == 0
is true if the interval contains only the floating-point number 0; similarly fora == 1
a > 0
means something useful (that every value in the interval is greater than 0)
The bad:
Trichotomy fails to hold: there are values
(a,b)
such that none ofa < b
,a == b
, ora > b
are trueThere are values
a
andb
such thata <= b
but neithera < b
nora == b
hold.There are values
a
andb
such that neithera != b
nora == b
hold.
Note
Intervals a
and b
overlap iff not(a != b)
.
Warning
The cmp(a, b)
function should not be used to compare real
intervals. Note that cmp
will disappear in Python3.
EXAMPLES:
sage: 0 < RIF(1, 2)
True
sage: 0 == RIF(0)
True
sage: not(0 == RIF(0, 1))
True
sage: not(0 != RIF(0, 1))
True
sage: 0 <= RIF(0, 1)
True
sage: not(0 < RIF(0, 1))
True
>>> from sage.all import *
>>> Integer(0) < RIF(Integer(1), Integer(2))
True
>>> Integer(0) == RIF(Integer(0))
True
>>> not(Integer(0) == RIF(Integer(0), Integer(1)))
True
>>> not(Integer(0) != RIF(Integer(0), Integer(1)))
True
>>> Integer(0) <= RIF(Integer(0), Integer(1))
True
>>> not(Integer(0) < RIF(Integer(0), Integer(1)))
True
Comparison with infinity is defined through coercion to the infinity ring where semi-infinite intervals are sent to their central value (plus or minus infinity); this implements the above convention for inequalities:
sage: InfinityRing.has_coerce_map_from(RIF)
True
sage: -oo < RIF(-1,1) < oo
True
sage: -oo < RIF(0,oo) <= oo
True
sage: -oo <= RIF(-oo,-1) < oo
True
>>> from sage.all import *
>>> InfinityRing.has_coerce_map_from(RIF)
True
>>> -oo < RIF(-Integer(1),Integer(1)) < oo
True
>>> -oo < RIF(Integer(0),oo) <= oo
True
>>> -oo <= RIF(-oo,-Integer(1)) < oo
True
Comparison by equality shows what the semi-infinite intervals actually coerce to:
sage: RIF(1,oo) == oo
True
sage: RIF(-oo,-1) == -oo
True
>>> from sage.all import *
>>> RIF(Integer(1),oo) == oo
True
>>> RIF(-oo,-Integer(1)) == -oo
True
For lack of a better value in the infinity ring, the doubly infinite interval coerces to plus infinity:
sage: RIF(-oo,oo) == oo
True
>>> from sage.all import *
>>> RIF(-oo,oo) == oo
True
If you want to compare two intervals lexicographically, you can use the
method lexico_cmp
. However, the behavior of this method is not
specified if given a non-interval and an interval:
sage: RIF(0).lexico_cmp(RIF(0, 1))
-1
sage: RIF(0, 1).lexico_cmp(RIF(0))
1
sage: RIF(0, 1).lexico_cmp(RIF(1))
-1
sage: RIF(0, 1).lexico_cmp(RIF(0, 1))
0
>>> from sage.all import *
>>> RIF(Integer(0)).lexico_cmp(RIF(Integer(0), Integer(1)))
-1
>>> RIF(Integer(0), Integer(1)).lexico_cmp(RIF(Integer(0)))
1
>>> RIF(Integer(0), Integer(1)).lexico_cmp(RIF(Integer(1)))
-1
>>> RIF(Integer(0), Integer(1)).lexico_cmp(RIF(Integer(0), Integer(1)))
0
Warning
Mixing symbolic expressions with intervals (in particular, converting constant symbolic expressions to intervals), can lead to incorrect results:
sage: ref = RealIntervalField(100)(ComplexBallField(100).one().airy_ai().real())
sage: ref
0.135292416312881415524147423515?
sage: val = RIF(airy_ai(1)); val # known bug
0.13529241631288142?
sage: val.overlaps(ref) # known bug
False
>>> from sage.all import *
>>> ref = RealIntervalField(Integer(100))(ComplexBallField(Integer(100)).one().airy_ai().real())
>>> ref
0.135292416312881415524147423515?
>>> val = RIF(airy_ai(Integer(1))); val # known bug
0.13529241631288142?
>>> val.overlaps(ref) # known bug
False
- sage.rings.real_mpfi.RealInterval(s, upper=None, base=10, pad=0, min_prec=53)[source]¶
Return the real number defined by the string s as an element of
RealIntervalField(prec=n)
, wheren
potentially has slightly more (controlled by pad) bits than given bys
.INPUT:
s
– string that defines a real number (or something whose string representation defines a number)upper
– (default:None
) upper endpoint of interval if given, in which cases
is the lower endpointbase
– integer between 2 and 36pad
– integer (default: 0)min_prec
– number will have at least this many bits of precision, no matter what
EXAMPLES:
sage: RealInterval('2.3') 2.300000000000000? sage: RealInterval(10) 10 sage: RealInterval('1.0000000000000000000000000000000000') 1 sage: RealInterval('1.2345678901234567890123456789012345') 1.23456789012345678901234567890123450? sage: RealInterval(29308290382930840239842390482, 3^20).str(style='brackets') '[3.48678440100000000000000000000e9 .. 2.93082903829308402398423904820e28]'
>>> from sage.all import * >>> RealInterval('2.3') 2.300000000000000? >>> RealInterval(Integer(10)) 10 >>> RealInterval('1.0000000000000000000000000000000000') 1 >>> RealInterval('1.2345678901234567890123456789012345') 1.23456789012345678901234567890123450? >>> RealInterval(Integer(29308290382930840239842390482), Integer(3)**Integer(20)).str(style='brackets') '[3.48678440100000000000000000000e9 .. 2.93082903829308402398423904820e28]'
- sage.rings.real_mpfi.RealIntervalField(prec=53, sci_not=False)[source]¶
Construct a
RealIntervalField_class
, with caching.INPUT:
prec
– integer (default: 53); precision. The number of bits used to represent the mantissa of a floating-point number. The precision can be any integer betweenmpfr_prec_min()
andmpfr_prec_max()
. In the current implementation,mpfr_prec_min()
is equal to 2.sci_not
– boolean (default:False
); whether or not to display using scientific notation
EXAMPLES:
sage: RealIntervalField() Real Interval Field with 53 bits of precision sage: RealIntervalField(200, sci_not=True) Real Interval Field with 200 bits of precision sage: RealIntervalField(53) is RIF True sage: RealIntervalField(200) is RIF False sage: RealIntervalField(200) is RealIntervalField(200) True
>>> from sage.all import * >>> RealIntervalField() Real Interval Field with 53 bits of precision >>> RealIntervalField(Integer(200), sci_not=True) Real Interval Field with 200 bits of precision >>> RealIntervalField(Integer(53)) is RIF True >>> RealIntervalField(Integer(200)) is RIF False >>> RealIntervalField(Integer(200)) is RealIntervalField(Integer(200)) True
See the documentation for
RealIntervalField_class
for many more examples.
- class sage.rings.real_mpfi.RealIntervalFieldElement[source]¶
Bases:
RingElement
A real number interval.
- absolute_diameter()[source]¶
The diameter of this interval (for \([a .. b]\), this is \(b-a\)), rounded upward, as a
RealNumber
.EXAMPLES:
sage: RIF(1, pi).absolute_diameter() # needs sage.symbolic 2.14159265358979
>>> from sage.all import * >>> RIF(Integer(1), pi).absolute_diameter() # needs sage.symbolic 2.14159265358979
- alea()[source]¶
Return a floating-point number picked at random from the interval.
EXAMPLES:
sage: RIF(1, 2).alea() # random 1.34696133696137
>>> from sage.all import * >>> RIF(Integer(1), Integer(2)).alea() # random 1.34696133696137
- algdep(n)[source]¶
Return a polynomial of degree at most \(n\) which is approximately satisfied by
self
.Note
The returned polynomial need not be irreducible, and indeed usually won’t be if
self
is a good approximation to an algebraic number of degree less than \(n\).Pari needs to know the number of “known good bits” in the number; we automatically get that from the interval width.
ALGORITHM:
Uses the PARI C-library
algdep
command.EXAMPLES:
sage: r = sqrt(RIF(2)); r 1.414213562373095? sage: r.algdep(5) x^2 - 2
>>> from sage.all import * >>> r = sqrt(RIF(Integer(2))); r 1.414213562373095? >>> r.algdep(Integer(5)) x^2 - 2
If we compute a wrong, but precise, interval, we get a wrong answer:
sage: r = sqrt(RealIntervalField(200)(2)) + (1/2)^40; r 1.414213562374004543503461652447613117632171875376948073176680? sage: r.algdep(5) 7266488*x^5 + 22441629*x^4 - 90470501*x^3 + 23297703*x^2 + 45778664*x + 13681026
>>> from sage.all import * >>> r = sqrt(RealIntervalField(Integer(200))(Integer(2))) + (Integer(1)/Integer(2))**Integer(40); r 1.414213562374004543503461652447613117632171875376948073176680? >>> r.algdep(Integer(5)) 7266488*x^5 + 22441629*x^4 - 90470501*x^3 + 23297703*x^2 + 45778664*x + 13681026
But if we compute an interval that includes the number we mean, we’re much more likely to get the right answer, even if the interval is very imprecise:
sage: r = r.union(sqrt(2.0)) sage: r.algdep(5) x^2 - 2
>>> from sage.all import * >>> r = r.union(sqrt(RealNumber('2.0'))) >>> r.algdep(Integer(5)) x^2 - 2
Even on this extremely imprecise interval we get an answer which is technically correct:
sage: RIF(-1, 1).algdep(5) x
>>> from sage.all import * >>> RIF(-Integer(1), Integer(1)).algdep(Integer(5)) x
- arccos()[source]¶
Return the inverse cosine of
self
.EXAMPLES:
sage: q = RIF.pi()/3; q 1.047197551196598? sage: i = q.cos(); i 0.500000000000000? sage: q2 = i.arccos(); q2 1.047197551196598? sage: q == q2 False sage: q != q2 False sage: q2.lower() == q.lower() False sage: q - q2 0.?e-15 sage: q in q2 True
>>> from sage.all import * >>> q = RIF.pi()/Integer(3); q 1.047197551196598? >>> i = q.cos(); i 0.500000000000000? >>> q2 = i.arccos(); q2 1.047197551196598? >>> q == q2 False >>> q != q2 False >>> q2.lower() == q.lower() False >>> q - q2 0.?e-15 >>> q in q2 True
- arccosh()[source]¶
Return the hyperbolic inverse cosine of
self
.EXAMPLES:
sage: q = RIF.pi()/2 sage: i = q.arccosh() ; i 1.023227478547551?
>>> from sage.all import * >>> q = RIF.pi()/Integer(2) >>> i = q.arccosh() ; i 1.023227478547551?
- arccoth()[source]¶
Return the inverse hyperbolic cotangent of
self
.EXAMPLES:
sage: RealIntervalField(100)(2).arccoth() 0.549306144334054845697622618462? sage: (2.0).arccoth() 0.549306144334055
>>> from sage.all import * >>> RealIntervalField(Integer(100))(Integer(2)).arccoth() 0.549306144334054845697622618462? >>> (RealNumber('2.0')).arccoth() 0.549306144334055
- arccsch()[source]¶
Return the inverse hyperbolic cosecant of
self
.EXAMPLES:
sage: RealIntervalField(100)(2).arccsch() 0.481211825059603447497758913425? sage: (2.0).arccsch() 0.481211825059603
>>> from sage.all import * >>> RealIntervalField(Integer(100))(Integer(2)).arccsch() 0.481211825059603447497758913425? >>> (RealNumber('2.0')).arccsch() 0.481211825059603
- arcsech()[source]¶
Return the inverse hyperbolic secant of
self
.EXAMPLES:
sage: RealIntervalField(100)(0.5).arcsech() 1.316957896924816708625046347308? sage: (0.5).arcsech() 1.31695789692482
>>> from sage.all import * >>> RealIntervalField(Integer(100))(RealNumber('0.5')).arcsech() 1.316957896924816708625046347308? >>> (RealNumber('0.5')).arcsech() 1.31695789692482
- arcsin()[source]¶
Return the inverse sine of
self
.EXAMPLES:
sage: q = RIF.pi()/5; q 0.6283185307179587? sage: i = q.sin(); i 0.587785252292474? sage: q2 = i.arcsin(); q2 0.628318530717959? sage: q == q2 False sage: q != q2 False sage: q2.lower() == q.lower() False sage: q - q2 0.?e-15 sage: q in q2 True
>>> from sage.all import * >>> q = RIF.pi()/Integer(5); q 0.6283185307179587? >>> i = q.sin(); i 0.587785252292474? >>> q2 = i.arcsin(); q2 0.628318530717959? >>> q == q2 False >>> q != q2 False >>> q2.lower() == q.lower() False >>> q - q2 0.?e-15 >>> q in q2 True
- arcsinh()[source]¶
Return the hyperbolic inverse sine of
self
.EXAMPLES:
sage: q = RIF.pi()/7 sage: i = q.sinh() ; i 0.464017630492991? sage: i.arcsinh() - q 0.?e-15
>>> from sage.all import * >>> q = RIF.pi()/Integer(7) >>> i = q.sinh() ; i 0.464017630492991? >>> i.arcsinh() - q 0.?e-15
- arctan()[source]¶
Return the inverse tangent of
self
.EXAMPLES:
sage: q = RIF.pi()/5; q 0.6283185307179587? sage: i = q.tan(); i 0.726542528005361? sage: q2 = i.arctan(); q2 0.628318530717959? sage: q == q2 False sage: q != q2 False sage: q2.lower() == q.lower() False sage: q - q2 0.?e-15 sage: q in q2 True
>>> from sage.all import * >>> q = RIF.pi()/Integer(5); q 0.6283185307179587? >>> i = q.tan(); i 0.726542528005361? >>> q2 = i.arctan(); q2 0.628318530717959? >>> q == q2 False >>> q != q2 False >>> q2.lower() == q.lower() False >>> q - q2 0.?e-15 >>> q in q2 True
- arctanh()[source]¶
Return the hyperbolic inverse tangent of
self
.EXAMPLES:
sage: q = RIF.pi()/7 sage: i = q.tanh() ; i 0.420911241048535? sage: i.arctanh() - q 0.?e-15
>>> from sage.all import * >>> q = RIF.pi()/Integer(7) >>> i = q.tanh() ; i 0.420911241048535? >>> i.arctanh() - q 0.?e-15
- argument()[source]¶
The argument of this interval, if it is well-defined, in the complex sense. Otherwise raises a
ValueError
.OUTPUT:
an element of the parent of this interval (0 or pi)
EXAMPLES:
sage: RIF(1).argument() 0 sage: RIF(-1).argument() 3.141592653589794? sage: RIF(0,1).argument() 0 sage: RIF(-1,0).argument() 3.141592653589794? sage: RIF(0).argument() Traceback (most recent call last): ... ValueError: Can't take the argument of an exact zero sage: RIF(-1,1).argument() Traceback (most recent call last): ... ValueError: Can't take the argument of interval strictly containing zero
>>> from sage.all import * >>> RIF(Integer(1)).argument() 0 >>> RIF(-Integer(1)).argument() 3.141592653589794? >>> RIF(Integer(0),Integer(1)).argument() 0 >>> RIF(-Integer(1),Integer(0)).argument() 3.141592653589794? >>> RIF(Integer(0)).argument() Traceback (most recent call last): ... ValueError: Can't take the argument of an exact zero >>> RIF(-Integer(1),Integer(1)).argument() Traceback (most recent call last): ... ValueError: Can't take the argument of interval strictly containing zero
- bisection()[source]¶
Return the bisection of
self
into two intervals of half the size whose union isself
and intersection iscenter()
.EXAMPLES:
sage: a, b = RIF(1,2).bisection() sage: a.lower(), a.upper() (1.00000000000000, 1.50000000000000) sage: b.lower(), b.upper() (1.50000000000000, 2.00000000000000) sage: # needs sage.symbolic sage: I = RIF(e, pi) sage: a, b = I.bisection() sage: a.intersection(b) == RIF(I.center()) True sage: a.union(b).endpoints() == I.endpoints() True
>>> from sage.all import * >>> a, b = RIF(Integer(1),Integer(2)).bisection() >>> a.lower(), a.upper() (1.00000000000000, 1.50000000000000) >>> b.lower(), b.upper() (1.50000000000000, 2.00000000000000) >>> # needs sage.symbolic >>> I = RIF(e, pi) >>> a, b = I.bisection() >>> a.intersection(b) == RIF(I.center()) True >>> a.union(b).endpoints() == I.endpoints() True
- ceil()[source]¶
Return the ceiling of this interval as an interval.
The ceiling of a real number \(x\) is the smallest integer larger than or equal to \(x\).
See also
unique_ceil()
– return the ceil as an integer if it is unique and raises aValueError
otherwisefloor()
– truncation towards minus infinitytrunc()
– truncation towards zeroround()
– rounding
EXAMPLES:
sage: (2.99).ceil() 3 sage: (2.00).ceil() 2 sage: (2.01).ceil() 3 sage: R = RealIntervalField(30) sage: a = R(-9.5, -11.3); a.str(style='brackets') '[-11.300000012 .. -9.5000000000]' sage: a.floor().str(style='brackets') '[-12.000000000 .. -10.000000000]' sage: a.ceil() -10.? sage: ceil(a).str(style='brackets') '[-11.000000000 .. -9.0000000000]'
>>> from sage.all import * >>> (RealNumber('2.99')).ceil() 3 >>> (RealNumber('2.00')).ceil() 2 >>> (RealNumber('2.01')).ceil() 3 >>> R = RealIntervalField(Integer(30)) >>> a = R(-RealNumber('9.5'), -RealNumber('11.3')); a.str(style='brackets') '[-11.300000012 .. -9.5000000000]' >>> a.floor().str(style='brackets') '[-12.000000000 .. -10.000000000]' >>> a.ceil() -10.? >>> ceil(a).str(style='brackets') '[-11.000000000 .. -9.0000000000]'
- ceiling()[source]¶
Return the ceiling of this interval as an interval.
The ceiling of a real number \(x\) is the smallest integer larger than or equal to \(x\).
See also
unique_ceil()
– return the ceil as an integer if it is unique and raises aValueError
otherwisefloor()
– truncation towards minus infinitytrunc()
– truncation towards zeroround()
– rounding
EXAMPLES:
sage: (2.99).ceil() 3 sage: (2.00).ceil() 2 sage: (2.01).ceil() 3 sage: R = RealIntervalField(30) sage: a = R(-9.5, -11.3); a.str(style='brackets') '[-11.300000012 .. -9.5000000000]' sage: a.floor().str(style='brackets') '[-12.000000000 .. -10.000000000]' sage: a.ceil() -10.? sage: ceil(a).str(style='brackets') '[-11.000000000 .. -9.0000000000]'
>>> from sage.all import * >>> (RealNumber('2.99')).ceil() 3 >>> (RealNumber('2.00')).ceil() 2 >>> (RealNumber('2.01')).ceil() 3 >>> R = RealIntervalField(Integer(30)) >>> a = R(-RealNumber('9.5'), -RealNumber('11.3')); a.str(style='brackets') '[-11.300000012 .. -9.5000000000]' >>> a.floor().str(style='brackets') '[-12.000000000 .. -10.000000000]' >>> a.ceil() -10.? >>> ceil(a).str(style='brackets') '[-11.000000000 .. -9.0000000000]'
- center()[source]¶
Compute the center of the interval \([a .. b]\) which is \((a+b) / 2\).
EXAMPLES:
sage: RIF(1, 2).center() 1.50000000000000
>>> from sage.all import * >>> RIF(Integer(1), Integer(2)).center() 1.50000000000000
- contains_zero()[source]¶
Return
True
ifself
is an interval containing zero.EXAMPLES:
sage: RIF(0).contains_zero() True sage: RIF(1, 2).contains_zero() False sage: RIF(-1, 1).contains_zero() True sage: RIF(-1, 0).contains_zero() True
>>> from sage.all import * >>> RIF(Integer(0)).contains_zero() True >>> RIF(Integer(1), Integer(2)).contains_zero() False >>> RIF(-Integer(1), Integer(1)).contains_zero() True >>> RIF(-Integer(1), Integer(0)).contains_zero() True
- cos()[source]¶
Return the cosine of
self
.EXAMPLES:
sage: # needs sage.symbolic sage: t = RIF(pi)/2 sage: t.cos() 0.?e-15 sage: t.cos().str(style='brackets') '[-1.6081226496766367e-16 .. 6.1232339957367661e-17]' sage: t.cos().cos() 0.9999999999999999?
>>> from sage.all import * >>> # needs sage.symbolic >>> t = RIF(pi)/Integer(2) >>> t.cos() 0.?e-15 >>> t.cos().str(style='brackets') '[-1.6081226496766367e-16 .. 6.1232339957367661e-17]' >>> t.cos().cos() 0.9999999999999999?
- cosh()[source]¶
Return the hyperbolic cosine of
self
.EXAMPLES:
sage: q = RIF.pi()/12 sage: q.cosh() 1.034465640095511?
>>> from sage.all import * >>> q = RIF.pi()/Integer(12) >>> q.cosh() 1.034465640095511?
- cot()[source]¶
Return the cotangent of
self
.EXAMPLES:
sage: RealIntervalField(100)(2).cot() -0.457657554360285763750277410432?
>>> from sage.all import * >>> RealIntervalField(Integer(100))(Integer(2)).cot() -0.457657554360285763750277410432?
- coth()[source]¶
Return the hyperbolic cotangent of
self
.EXAMPLES:
sage: RealIntervalField(100)(2).coth() 1.03731472072754809587780976477?
>>> from sage.all import * >>> RealIntervalField(Integer(100))(Integer(2)).coth() 1.03731472072754809587780976477?
- csc()[source]¶
Return the cosecant of
self
.EXAMPLES:
sage: RealIntervalField(100)(2).csc() 1.099750170294616466756697397026?
>>> from sage.all import * >>> RealIntervalField(Integer(100))(Integer(2)).csc() 1.099750170294616466756697397026?
- csch()[source]¶
Return the hyperbolic cosecant of
self
.EXAMPLES:
sage: RealIntervalField(100)(2).csch() 0.275720564771783207758351482163?
>>> from sage.all import * >>> RealIntervalField(Integer(100))(Integer(2)).csch() 0.275720564771783207758351482163?
- diameter()[source]¶
If 0 is in
self
, then returnabsolute_diameter()
, otherwise returnrelative_diameter()
.EXAMPLES:
sage: RIF(1, 2).diameter() 0.666666666666667 sage: RIF(1, 2).absolute_diameter() 1.00000000000000 sage: RIF(1, 2).relative_diameter() 0.666666666666667 sage: # needs sage.symbolic sage: RIF(pi).diameter() 1.41357985842823e-16 sage: RIF(pi).absolute_diameter() 4.44089209850063e-16 sage: RIF(pi).relative_diameter() 1.41357985842823e-16 sage: (RIF(pi) - RIF(3, 22/7)).diameter() 0.142857142857144 sage: (RIF(pi) - RIF(3, 22/7)).absolute_diameter() 0.142857142857144 sage: (RIF(pi) - RIF(3, 22/7)).relative_diameter() 2.03604377705518
>>> from sage.all import * >>> RIF(Integer(1), Integer(2)).diameter() 0.666666666666667 >>> RIF(Integer(1), Integer(2)).absolute_diameter() 1.00000000000000 >>> RIF(Integer(1), Integer(2)).relative_diameter() 0.666666666666667 >>> # needs sage.symbolic >>> RIF(pi).diameter() 1.41357985842823e-16 >>> RIF(pi).absolute_diameter() 4.44089209850063e-16 >>> RIF(pi).relative_diameter() 1.41357985842823e-16 >>> (RIF(pi) - RIF(Integer(3), Integer(22)/Integer(7))).diameter() 0.142857142857144 >>> (RIF(pi) - RIF(Integer(3), Integer(22)/Integer(7))).absolute_diameter() 0.142857142857144 >>> (RIF(pi) - RIF(Integer(3), Integer(22)/Integer(7))).relative_diameter() 2.03604377705518
- edges()[source]¶
Return the lower and upper endpoints of this interval as intervals.
OUTPUT: a 2-tuple of real intervals (lower endpoint, upper endpoint) each containing just one point.
See also
endpoints()
which returns the endpoints as real numbers instead of intervals.EXAMPLES:
sage: RIF(1,2).edges() (1, 2) sage: RIF(pi).edges() # needs sage.symbolic (3.1415926535897932?, 3.1415926535897936?)
>>> from sage.all import * >>> RIF(Integer(1),Integer(2)).edges() (1, 2) >>> RIF(pi).edges() # needs sage.symbolic (3.1415926535897932?, 3.1415926535897936?)
- endpoints(rnd=None)[source]¶
Return the lower and upper endpoints of this interval.
OUTPUT: a 2-tuple of real numbers (lower endpoint, upper endpoint)
See also
edges()
which returns the endpoints as exact intervals instead of real numbers.EXAMPLES:
sage: RIF(1,2).endpoints() (1.00000000000000, 2.00000000000000) sage: RIF(pi).endpoints() # needs sage.symbolic (3.14159265358979, 3.14159265358980) sage: a = CIF(RIF(1,2), RIF(3,4)) sage: a.real().endpoints() (1.00000000000000, 2.00000000000000)
>>> from sage.all import * >>> RIF(Integer(1),Integer(2)).endpoints() (1.00000000000000, 2.00000000000000) >>> RIF(pi).endpoints() # needs sage.symbolic (3.14159265358979, 3.14159265358980) >>> a = CIF(RIF(Integer(1),Integer(2)), RIF(Integer(3),Integer(4))) >>> a.real().endpoints() (1.00000000000000, 2.00000000000000)
As with
lower()
andupper()
, a rounding mode is accepted:sage: RIF(1,2).endpoints('RNDD')[0].parent() Real Field with 53 bits of precision and rounding RNDD
>>> from sage.all import * >>> RIF(Integer(1),Integer(2)).endpoints('RNDD')[Integer(0)].parent() Real Field with 53 bits of precision and rounding RNDD
- exp()[source]¶
Return \(e^\mathtt{self}\).
EXAMPLES:
sage: r = RIF(0.0) sage: r.exp() 1
>>> from sage.all import * >>> r = RIF(RealNumber('0.0')) >>> r.exp() 1
sage: r = RIF(32.3) sage: a = r.exp(); a 1.065888472748645?e14 sage: a.log() 32.30000000000000?
>>> from sage.all import * >>> r = RIF(RealNumber('32.3')) >>> a = r.exp(); a 1.065888472748645?e14 >>> a.log() 32.30000000000000?
sage: r = RIF(-32.3) sage: r.exp() 9.38184458849869?e-15
>>> from sage.all import * >>> r = RIF(-RealNumber('32.3')) >>> r.exp() 9.38184458849869?e-15
- exp2()[source]¶
Return \(2^\mathtt{self}\).
EXAMPLES:
sage: r = RIF(0.0) sage: r.exp2() 1
>>> from sage.all import * >>> r = RIF(RealNumber('0.0')) >>> r.exp2() 1
sage: r = RIF(32.0) sage: r.exp2() 4294967296
>>> from sage.all import * >>> r = RIF(RealNumber('32.0')) >>> r.exp2() 4294967296
sage: r = RIF(-32.3) sage: r.exp2() 1.891172482530207?e-10
>>> from sage.all import * >>> r = RIF(-RealNumber('32.3')) >>> r.exp2() 1.891172482530207?e-10
- factorial()[source]¶
Return the factorial evaluated on
self
.EXAMPLES:
sage: RIF(5).factorial() 120 sage: RIF(2.3,5.7).factorial() 1.?e3 sage: RIF(2.3).factorial() 2.683437381955768?
>>> from sage.all import * >>> RIF(Integer(5)).factorial() 120 >>> RIF(RealNumber('2.3'),RealNumber('5.7')).factorial() 1.?e3 >>> RIF(RealNumber('2.3')).factorial() 2.683437381955768?
Recover the factorial as integer:
sage: f = RealIntervalField(200)(50).factorial() sage: f 3.0414093201713378043612608166064768844377641568960512000000000?e64 sage: f.unique_integer() 30414093201713378043612608166064768844377641568960512000000000000 sage: 50.factorial() 30414093201713378043612608166064768844377641568960512000000000000
>>> from sage.all import * >>> f = RealIntervalField(Integer(200))(Integer(50)).factorial() >>> f 3.0414093201713378043612608166064768844377641568960512000000000?e64 >>> f.unique_integer() 30414093201713378043612608166064768844377641568960512000000000000 >>> Integer(50).factorial() 30414093201713378043612608166064768844377641568960512000000000000
- floor()[source]¶
Return the floor of this interval as an interval.
The floor of a real number \(x\) is the largest integer smaller than or equal to \(x\).
See also
unique_floor()
– method which returns the floor as an integer if it is unique or raises aValueError
otherwiseceil()
– truncation towards plus infinityround()
– roundingtrunc()
– truncation towards zero
EXAMPLES:
sage: R = RealIntervalField() sage: (2.99).floor() 2 sage: (2.00).floor() 2 sage: floor(RR(-5/2)) -3 sage: R = RealIntervalField(100) sage: a = R(9.5, 11.3); a.str(style='brackets') '[9.5000000000000000000000000000000 .. 11.300000000000000710542735760101]' sage: floor(a).str(style='brackets') '[9.0000000000000000000000000000000 .. 11.000000000000000000000000000000]' sage: a.floor() 10.? sage: ceil(a) 11.? sage: a.ceil().str(style='brackets') '[10.000000000000000000000000000000 .. 12.000000000000000000000000000000]'
>>> from sage.all import * >>> R = RealIntervalField() >>> (RealNumber('2.99')).floor() 2 >>> (RealNumber('2.00')).floor() 2 >>> floor(RR(-Integer(5)/Integer(2))) -3 >>> R = RealIntervalField(Integer(100)) >>> a = R(RealNumber('9.5'), RealNumber('11.3')); a.str(style='brackets') '[9.5000000000000000000000000000000 .. 11.300000000000000710542735760101]' >>> floor(a).str(style='brackets') '[9.0000000000000000000000000000000 .. 11.000000000000000000000000000000]' >>> a.floor() 10.? >>> ceil(a) 11.? >>> a.ceil().str(style='brackets') '[10.000000000000000000000000000000 .. 12.000000000000000000000000000000]'
- fp_rank_diameter()[source]¶
Compute the diameter of this interval in terms of the “floating-point rank”.
The floating-point rank is the number of floating-point numbers (of the current precision) contained in the given interval, minus one. An
fp_rank_diameter
of 0 means that the interval is exact; anfp_rank_diameter
of 1 means that the interval is as tight as possible, unless the number you’re trying to represent is actually exactly representable as a floating-point number.EXAMPLES:
sage: RIF(12345).fp_rank_diameter() 0 sage: RIF(5/8).fp_rank_diameter() 0 sage: RIF(5/7).fp_rank_diameter() 1 sage: # needs sage.symbolic sage: RIF(pi).fp_rank_diameter() 1 sage: RIF(-sqrt(2)).fp_rank_diameter() 1 sage: a = RIF(pi)^12345; a 2.06622879260?e6137 sage: a.fp_rank_diameter() 30524 sage: (RIF(sqrt(2)) - RIF(sqrt(2))).fp_rank_diameter() 9671406088542672151117826 # 32-bit 41538374868278620559869609387229186 # 64-bit
>>> from sage.all import * >>> RIF(Integer(12345)).fp_rank_diameter() 0 >>> RIF(Integer(5)/Integer(8)).fp_rank_diameter() 0 >>> RIF(Integer(5)/Integer(7)).fp_rank_diameter() 1 >>> # needs sage.symbolic >>> RIF(pi).fp_rank_diameter() 1 >>> RIF(-sqrt(Integer(2))).fp_rank_diameter() 1 >>> a = RIF(pi)**Integer(12345); a 2.06622879260?e6137 >>> a.fp_rank_diameter() 30524 >>> (RIF(sqrt(Integer(2))) - RIF(sqrt(Integer(2)))).fp_rank_diameter() 9671406088542672151117826 # 32-bit 41538374868278620559869609387229186 # 64-bit
Just because we have the best possible interval, doesn’t mean the interval is actually small:
sage: a = RIF(pi)^12345678901234567890; a # needs sage.symbolic [2.0985787164673874e323228496 .. +infinity] # 32-bit [5.8756537891115869e1388255822130839282 .. +infinity] # 64-bit sage: a.fp_rank_diameter() # needs sage.symbolic 1
>>> from sage.all import * >>> a = RIF(pi)**Integer(12345678901234567890); a # needs sage.symbolic [2.0985787164673874e323228496 .. +infinity] # 32-bit [5.8756537891115869e1388255822130839282 .. +infinity] # 64-bit >>> a.fp_rank_diameter() # needs sage.symbolic 1
- frac()[source]¶
Return the fractional part of this interval as an interval.
The fractional part \(y\) of a real number \(x\) is the unique element in the interval \((-1,1)\) that has the same sign as \(x\) and such that \(x-y\) is an integer. The integer \(x-y\) can be obtained through the method
trunc()
.The output of this function is the smallest interval that contains all possible values of \(frac(x)\) for \(x\) in this interval. Note that if it contains an integer then the answer might not be very meaningful. More precisely, if the endpoints are \(a\) and \(b\) then:
if \(floor(b) > \max(a,0)\) then the interval obtained contains \([0,1]\),
if \(ceil(a) < \min(b,0)\) then the interval obtained contains \([-1,0]\).
See also
trunc()
– return the integer part complement to this fractional partEXAMPLES:
sage: RIF(2.37123, 2.372).frac() 0.372? sage: RIF(-23.12, -23.13).frac() -0.13? sage: RIF(.5, 1).frac().endpoints() (0.000000000000000, 1.00000000000000) sage: RIF(1, 1.5).frac().endpoints() (0.000000000000000, 0.500000000000000) sage: r = RIF(-22.47, -22.468) sage: r in (r.frac() + r.trunc()) True sage: r = RIF(18.222, 18.223) sage: r in (r.frac() + r.trunc()) True sage: RIF(1.99, 2.025).frac().endpoints() (0.000000000000000, 1.00000000000000) sage: RIF(1.99, 2.00).frac().endpoints() (0.000000000000000, 1.00000000000000) sage: RIF(2.00, 2.025).frac().endpoints() (0.000000000000000, 0.0250000000000000) sage: RIF(-2.1,-0.9).frac().endpoints() (-1.00000000000000, -0.000000000000000) sage: RIF(-0.5,0.5).frac().endpoints() (-0.500000000000000, 0.500000000000000)
>>> from sage.all import * >>> RIF(RealNumber('2.37123'), RealNumber('2.372')).frac() 0.372? >>> RIF(-RealNumber('23.12'), -RealNumber('23.13')).frac() -0.13? >>> RIF(RealNumber('.5'), Integer(1)).frac().endpoints() (0.000000000000000, 1.00000000000000) >>> RIF(Integer(1), RealNumber('1.5')).frac().endpoints() (0.000000000000000, 0.500000000000000) >>> r = RIF(-RealNumber('22.47'), -RealNumber('22.468')) >>> r in (r.frac() + r.trunc()) True >>> r = RIF(RealNumber('18.222'), RealNumber('18.223')) >>> r in (r.frac() + r.trunc()) True >>> RIF(RealNumber('1.99'), RealNumber('2.025')).frac().endpoints() (0.000000000000000, 1.00000000000000) >>> RIF(RealNumber('1.99'), RealNumber('2.00')).frac().endpoints() (0.000000000000000, 1.00000000000000) >>> RIF(RealNumber('2.00'), RealNumber('2.025')).frac().endpoints() (0.000000000000000, 0.0250000000000000) >>> RIF(-RealNumber('2.1'),-RealNumber('0.9')).frac().endpoints() (-1.00000000000000, -0.000000000000000) >>> RIF(-RealNumber('0.5'),RealNumber('0.5')).frac().endpoints() (-0.500000000000000, 0.500000000000000)
- gamma()[source]¶
Return the gamma function evaluated on
self
.EXAMPLES:
sage: RIF(1).gamma() 1 sage: RIF(5).gamma() 24 sage: a = RIF(3,4).gamma(); a 1.?e1 sage: a.lower(), a.upper() (2.00000000000000, 6.00000000000000) sage: RIF(-1/2).gamma() -3.54490770181104? sage: gamma(-1/2).n(100) in RIF(-1/2).gamma() # needs sage.symbolic True sage: RIF1000 = RealIntervalField(1000) sage: 0 in (RIF1000(RealField(2000)(-19/3).gamma()) - RIF1000(-19/3).gamma()) True sage: gamma(RIF(100)) 9.33262154439442?e155 sage: gamma(RIF(-10000/3)) 1.31280781451?e-10297
>>> from sage.all import * >>> RIF(Integer(1)).gamma() 1 >>> RIF(Integer(5)).gamma() 24 >>> a = RIF(Integer(3),Integer(4)).gamma(); a 1.?e1 >>> a.lower(), a.upper() (2.00000000000000, 6.00000000000000) >>> RIF(-Integer(1)/Integer(2)).gamma() -3.54490770181104? >>> gamma(-Integer(1)/Integer(2)).n(Integer(100)) in RIF(-Integer(1)/Integer(2)).gamma() # needs sage.symbolic True >>> RIF1000 = RealIntervalField(Integer(1000)) >>> Integer(0) in (RIF1000(RealField(Integer(2000))(-Integer(19)/Integer(3)).gamma()) - RIF1000(-Integer(19)/Integer(3)).gamma()) True >>> gamma(RIF(Integer(100))) 9.33262154439442?e155 >>> gamma(RIF(-Integer(10000)/Integer(3))) 1.31280781451?e-10297
Verify the result contains the local minima:
sage: 0.88560319441088 in RIF(1, 2).gamma() True sage: 0.88560319441088 in RIF(0.25, 4).gamma() True sage: 0.88560319441088 in RIF(1.4616, 1.46164).gamma() True sage: (-0.99).gamma() -100.436954665809 sage: (-0.01).gamma() -100.587197964411 sage: RIF(-0.99, -0.01).gamma().upper() -1.60118039970055
>>> from sage.all import * >>> RealNumber('0.88560319441088') in RIF(Integer(1), Integer(2)).gamma() True >>> RealNumber('0.88560319441088') in RIF(RealNumber('0.25'), Integer(4)).gamma() True >>> RealNumber('0.88560319441088') in RIF(RealNumber('1.4616'), RealNumber('1.46164')).gamma() True >>> (-RealNumber('0.99')).gamma() -100.436954665809 >>> (-RealNumber('0.01')).gamma() -100.587197964411 >>> RIF(-RealNumber('0.99'), -RealNumber('0.01')).gamma().upper() -1.60118039970055
Correctly detects poles:
sage: gamma(RIF(-3/2,-1/2)) [-infinity .. +infinity]
>>> from sage.all import * >>> gamma(RIF(-Integer(3)/Integer(2),-Integer(1)/Integer(2))) [-infinity .. +infinity]
- imag()[source]¶
Return the imaginary part of this real interval.
(Since this is interval is real, this simply returns the zero interval.)
See also
EXAMPLES:
sage: RIF(2,3).imag() 0
>>> from sage.all import * >>> RIF(Integer(2),Integer(3)).imag() 0
- intersection(other)[source]¶
Return the intersection of two intervals. If the intervals do not overlap, raises a
ValueError
.EXAMPLES:
sage: RIF(1, 2).intersection(RIF(1.5, 3)).str(style='brackets') '[1.5000000000000000 .. 2.0000000000000000]' sage: RIF(1, 2).intersection(RIF(4/3, 5/3)).str(style='brackets') '[1.3333333333333332 .. 1.6666666666666668]' sage: RIF(1, 2).intersection(RIF(3, 4)) Traceback (most recent call last): ... ValueError: intersection of non-overlapping intervals
>>> from sage.all import * >>> RIF(Integer(1), Integer(2)).intersection(RIF(RealNumber('1.5'), Integer(3))).str(style='brackets') '[1.5000000000000000 .. 2.0000000000000000]' >>> RIF(Integer(1), Integer(2)).intersection(RIF(Integer(4)/Integer(3), Integer(5)/Integer(3))).str(style='brackets') '[1.3333333333333332 .. 1.6666666666666668]' >>> RIF(Integer(1), Integer(2)).intersection(RIF(Integer(3), Integer(4))) Traceback (most recent call last): ... ValueError: intersection of non-overlapping intervals
- is_NaN()[source]¶
Check to see if
self
is Not-a-NumberNaN
.EXAMPLES:
sage: a = RIF(0) / RIF(0.0,0.00); a [.. NaN ..] sage: a.is_NaN() True
>>> from sage.all import * >>> a = RIF(Integer(0)) / RIF(RealNumber('0.0'),RealNumber('0.00')); a [.. NaN ..] >>> a.is_NaN() True
- is_exact()[source]¶
Return whether this real interval is exact (i.e. contains exactly one real value).
EXAMPLES:
sage: RIF(3).is_exact() True sage: RIF(2*pi).is_exact() # needs sage.symbolic False
>>> from sage.all import * >>> RIF(Integer(3)).is_exact() True >>> RIF(Integer(2)*pi).is_exact() # needs sage.symbolic False
- is_int()[source]¶
Check to see whether this interval includes exactly one integer.
OUTPUT:
If this contains exactly one integer, it returns the tuple
(True, n)
, wheren
is that integer; otherwise, this returns(False, None)
.EXAMPLES:
sage: a = RIF(0.8,1.5) sage: a.is_int() (True, 1) sage: a = RIF(1.1,1.5) sage: a.is_int() (False, None) sage: a = RIF(1,2) sage: a.is_int() (False, None) sage: a = RIF(-1.1, -0.9) sage: a.is_int() (True, -1) sage: a = RIF(0.1, 1.9) sage: a.is_int() (True, 1) sage: RIF(+infinity,+infinity).is_int() (False, None)
>>> from sage.all import * >>> a = RIF(RealNumber('0.8'),RealNumber('1.5')) >>> a.is_int() (True, 1) >>> a = RIF(RealNumber('1.1'),RealNumber('1.5')) >>> a.is_int() (False, None) >>> a = RIF(Integer(1),Integer(2)) >>> a.is_int() (False, None) >>> a = RIF(-RealNumber('1.1'), -RealNumber('0.9')) >>> a.is_int() (True, -1) >>> a = RIF(RealNumber('0.1'), RealNumber('1.9')) >>> a.is_int() (True, 1) >>> RIF(+infinity,+infinity).is_int() (False, None)
- lexico_cmp(left, right)[source]¶
Compare two intervals lexicographically.
This means that the left bounds are compared first and then the right bounds are compared if the left bounds coincide.
Return 0 if they are the same interval, -1 if the second is larger, or 1 if the first is larger.
EXAMPLES:
sage: RIF(0).lexico_cmp(RIF(1)) -1 sage: RIF(0, 1).lexico_cmp(RIF(1)) -1 sage: RIF(0, 1).lexico_cmp(RIF(1, 2)) -1 sage: RIF(0, 0.99999).lexico_cmp(RIF(1, 2)) -1 sage: RIF(1, 2).lexico_cmp(RIF(0, 1)) 1 sage: RIF(1, 2).lexico_cmp(RIF(0)) 1 sage: RIF(0, 1).lexico_cmp(RIF(0, 2)) -1 sage: RIF(0, 1).lexico_cmp(RIF(0, 1)) 0 sage: RIF(0, 1).lexico_cmp(RIF(0, 1/2)) 1
>>> from sage.all import * >>> RIF(Integer(0)).lexico_cmp(RIF(Integer(1))) -1 >>> RIF(Integer(0), Integer(1)).lexico_cmp(RIF(Integer(1))) -1 >>> RIF(Integer(0), Integer(1)).lexico_cmp(RIF(Integer(1), Integer(2))) -1 >>> RIF(Integer(0), RealNumber('0.99999')).lexico_cmp(RIF(Integer(1), Integer(2))) -1 >>> RIF(Integer(1), Integer(2)).lexico_cmp(RIF(Integer(0), Integer(1))) 1 >>> RIF(Integer(1), Integer(2)).lexico_cmp(RIF(Integer(0))) 1 >>> RIF(Integer(0), Integer(1)).lexico_cmp(RIF(Integer(0), Integer(2))) -1 >>> RIF(Integer(0), Integer(1)).lexico_cmp(RIF(Integer(0), Integer(1))) 0 >>> RIF(Integer(0), Integer(1)).lexico_cmp(RIF(Integer(0), Integer(1)/Integer(2))) 1
- log(base='e')[source]¶
Return the logarithm of
self
to the givenbase
.EXAMPLES:
sage: R = RealIntervalField() sage: r = R(2); r.log() 0.6931471805599453? sage: r = R(-2); r.log() 0.6931471805599453? + 3.141592653589794?*I
>>> from sage.all import * >>> R = RealIntervalField() >>> r = R(Integer(2)); r.log() 0.6931471805599453? >>> r = R(-Integer(2)); r.log() 0.6931471805599453? + 3.141592653589794?*I
- log10()[source]¶
Return log to the base 10 of
self
.EXAMPLES:
sage: r = RIF(16.0); r.log10() 1.204119982655925? sage: r.log() / RIF(10).log() 1.204119982655925?
>>> from sage.all import * >>> r = RIF(RealNumber('16.0')); r.log10() 1.204119982655925? >>> r.log() / RIF(Integer(10)).log() 1.204119982655925?
sage: r = RIF(39.9); r.log10() 1.600972895686749?
>>> from sage.all import * >>> r = RIF(RealNumber('39.9')); r.log10() 1.600972895686749?
sage: r = RIF(0.0) sage: r.log10() [-infinity .. -infinity]
>>> from sage.all import * >>> r = RIF(RealNumber('0.0')) >>> r.log10() [-infinity .. -infinity]
sage: r = RIF(-1.0) sage: r.log10() 1.364376353841841?*I
>>> from sage.all import * >>> r = RIF(-RealNumber('1.0')) >>> r.log10() 1.364376353841841?*I
- log2()[source]¶
Return
log
to the base 2 ofself
.EXAMPLES:
sage: r = RIF(16.0) sage: r.log2() 4
>>> from sage.all import * >>> r = RIF(RealNumber('16.0')) >>> r.log2() 4
sage: r = RIF(31.9); r.log2() 4.995484518877507?
>>> from sage.all import * >>> r = RIF(RealNumber('31.9')); r.log2() 4.995484518877507?
sage: r = RIF(0.0, 2.0) sage: r.log2() [-infinity .. 1.0000000000000000]
>>> from sage.all import * >>> r = RIF(RealNumber('0.0'), RealNumber('2.0')) >>> r.log2() [-infinity .. 1.0000000000000000]
- lower(rnd=None)[source]¶
Return the lower bound of this interval.
INPUT:
rnd
– the rounding mode (default: towards minus infinity, seesage.rings.real_mpfr.RealField
for possible values)
The rounding mode does not affect the value returned as a floating-point number, but it does control which variety of
RealField
the returned number is in, which affects printing and subsequent operations.EXAMPLES:
sage: R = RealIntervalField(13) sage: R.pi().lower().str() '3.1411'
>>> from sage.all import * >>> R = RealIntervalField(Integer(13)) >>> R.pi().lower().str() '3.1411'
sage: x = R(1.2,1.3); x.str(style='brackets') '[1.1999 .. 1.3001]' sage: x.lower() 1.19 sage: x.lower('RNDU') 1.20 sage: x.lower('RNDN') 1.20 sage: x.lower('RNDZ') 1.19 sage: x.lower('RNDA') 1.20 sage: x.lower().parent() Real Field with 13 bits of precision and rounding RNDD sage: x.lower('RNDU').parent() Real Field with 13 bits of precision and rounding RNDU sage: x.lower('RNDA').parent() Real Field with 13 bits of precision and rounding RNDA sage: x.lower() == x.lower('RNDU') True
>>> from sage.all import * >>> x = R(RealNumber('1.2'),RealNumber('1.3')); x.str(style='brackets') '[1.1999 .. 1.3001]' >>> x.lower() 1.19 >>> x.lower('RNDU') 1.20 >>> x.lower('RNDN') 1.20 >>> x.lower('RNDZ') 1.19 >>> x.lower('RNDA') 1.20 >>> x.lower().parent() Real Field with 13 bits of precision and rounding RNDD >>> x.lower('RNDU').parent() Real Field with 13 bits of precision and rounding RNDU >>> x.lower('RNDA').parent() Real Field with 13 bits of precision and rounding RNDA >>> x.lower() == x.lower('RNDU') True
- magnitude()[source]¶
The largest absolute value of the elements of the interval.
OUTPUT: a real number with rounding mode
RNDU
EXAMPLES:
sage: RIF(-2, 1).magnitude() 2.00000000000000 sage: RIF(-1, 2).magnitude() 2.00000000000000 sage: parent(RIF(1).magnitude()) Real Field with 53 bits of precision and rounding RNDU
>>> from sage.all import * >>> RIF(-Integer(2), Integer(1)).magnitude() 2.00000000000000 >>> RIF(-Integer(1), Integer(2)).magnitude() 2.00000000000000 >>> parent(RIF(Integer(1)).magnitude()) Real Field with 53 bits of precision and rounding RNDU
- max(*_others)[source]¶
Return an interval containing the maximum of
self
and the arguments.EXAMPLES:
sage: RIF(-1, 1).max(0).endpoints() (0.000000000000000, 1.00000000000000) sage: RIF(-1, 1).max(RIF(2, 3)).endpoints() (2.00000000000000, 3.00000000000000) sage: RIF(-1, 1).max(RIF(-100, 100)).endpoints() (-1.00000000000000, 100.000000000000) sage: RIF(-1, 1).max(RIF(-100, 100), RIF(5, 10)).endpoints() (5.00000000000000, 100.000000000000)
>>> from sage.all import * >>> RIF(-Integer(1), Integer(1)).max(Integer(0)).endpoints() (0.000000000000000, 1.00000000000000) >>> RIF(-Integer(1), Integer(1)).max(RIF(Integer(2), Integer(3))).endpoints() (2.00000000000000, 3.00000000000000) >>> RIF(-Integer(1), Integer(1)).max(RIF(-Integer(100), Integer(100))).endpoints() (-1.00000000000000, 100.000000000000) >>> RIF(-Integer(1), Integer(1)).max(RIF(-Integer(100), Integer(100)), RIF(Integer(5), Integer(10))).endpoints() (5.00000000000000, 100.000000000000)
Note that if the maximum is one of the given elements, that element will be returned.
sage: a = RIF(-1, 1) sage: b = RIF(2, 3) sage: c = RIF(3, 4) sage: c.max(a, b) is c True sage: b.max(a, c) is c True sage: a.max(b, c) is c True
>>> from sage.all import * >>> a = RIF(-Integer(1), Integer(1)) >>> b = RIF(Integer(2), Integer(3)) >>> c = RIF(Integer(3), Integer(4)) >>> c.max(a, b) is c True >>> b.max(a, c) is c True >>> a.max(b, c) is c True
It might also be convenient to call the method as a function:
sage: from sage.rings.real_mpfi import RealIntervalFieldElement sage: RealIntervalFieldElement.max(a, b, c) is c True sage: elements = [a, b, c] sage: RealIntervalFieldElement.max(*elements) is c True
>>> from sage.all import * >>> from sage.rings.real_mpfi import RealIntervalFieldElement >>> RealIntervalFieldElement.max(a, b, c) is c True >>> elements = [a, b, c] >>> RealIntervalFieldElement.max(*elements) is c True
The generic max does not always do the right thing:
sage: max(0, RIF(-1, 1)) 0 sage: max(RIF(-1, 1), RIF(-100, 100)).endpoints() (-1.00000000000000, 1.00000000000000)
>>> from sage.all import * >>> max(Integer(0), RIF(-Integer(1), Integer(1))) 0 >>> max(RIF(-Integer(1), Integer(1)), RIF(-Integer(100), Integer(100))).endpoints() (-1.00000000000000, 1.00000000000000)
Note that calls involving NaNs try to return a number when possible. This is consistent with IEEE-754-2008 but may be surprising.
sage: RIF('nan').max(1, 2) 2 sage: RIF(-1/3).max(RIF('nan')) -0.3333333333333334? sage: RIF('nan').max(RIF('nan')) [.. NaN ..]
>>> from sage.all import * >>> RIF('nan').max(Integer(1), Integer(2)) 2 >>> RIF(-Integer(1)/Integer(3)).max(RIF('nan')) -0.3333333333333334? >>> RIF('nan').max(RIF('nan')) [.. NaN ..]
See also
- mignitude()[source]¶
The smallest absolute value of the elements of the interval.
OUTPUT: a real number with rounding mode
RNDD
EXAMPLES:
sage: RIF(-2, 1).mignitude() 0.000000000000000 sage: RIF(-2, -1).mignitude() 1.00000000000000 sage: RIF(3, 4).mignitude() 3.00000000000000 sage: parent(RIF(1).mignitude()) Real Field with 53 bits of precision and rounding RNDD
>>> from sage.all import * >>> RIF(-Integer(2), Integer(1)).mignitude() 0.000000000000000 >>> RIF(-Integer(2), -Integer(1)).mignitude() 1.00000000000000 >>> RIF(Integer(3), Integer(4)).mignitude() 3.00000000000000 >>> parent(RIF(Integer(1)).mignitude()) Real Field with 53 bits of precision and rounding RNDD
- min(*_others)[source]¶
Return an interval containing the minimum of
self
and the arguments.EXAMPLES:
sage: a = RIF(-1, 1).min(0).endpoints() sage: a[0] == -1.0 and a[1].abs() == 0.0 # in MPFI, the sign of 0.0 is not specified True sage: RIF(-1, 1).min(pi).endpoints() # needs sage.symbolic (-1.00000000000000, 1.00000000000000) sage: RIF(-1, 1).min(RIF(-100, 100)).endpoints() (-100.000000000000, 1.00000000000000) sage: RIF(-1, 1).min(RIF(-100, 0)).endpoints() (-100.000000000000, 0.000000000000000) sage: RIF(-1, 1).min(RIF(-100, 2), RIF(-200, -3)).endpoints() (-200.000000000000, -3.00000000000000)
>>> from sage.all import * >>> a = RIF(-Integer(1), Integer(1)).min(Integer(0)).endpoints() >>> a[Integer(0)] == -RealNumber('1.0') and a[Integer(1)].abs() == RealNumber('0.0') # in MPFI, the sign of 0.0 is not specified True >>> RIF(-Integer(1), Integer(1)).min(pi).endpoints() # needs sage.symbolic (-1.00000000000000, 1.00000000000000) >>> RIF(-Integer(1), Integer(1)).min(RIF(-Integer(100), Integer(100))).endpoints() (-100.000000000000, 1.00000000000000) >>> RIF(-Integer(1), Integer(1)).min(RIF(-Integer(100), Integer(0))).endpoints() (-100.000000000000, 0.000000000000000) >>> RIF(-Integer(1), Integer(1)).min(RIF(-Integer(100), Integer(2)), RIF(-Integer(200), -Integer(3))).endpoints() (-200.000000000000, -3.00000000000000)
Note that if the minimum is one of the given elements, that element will be returned.
sage: a = RIF(-1, 1) sage: b = RIF(2, 3) sage: c = RIF(3, 4) sage: c.min(a, b) is a True sage: b.min(a, c) is a True sage: a.min(b, c) is a True
>>> from sage.all import * >>> a = RIF(-Integer(1), Integer(1)) >>> b = RIF(Integer(2), Integer(3)) >>> c = RIF(Integer(3), Integer(4)) >>> c.min(a, b) is a True >>> b.min(a, c) is a True >>> a.min(b, c) is a True
It might also be convenient to call the method as a function:
sage: from sage.rings.real_mpfi import RealIntervalFieldElement sage: RealIntervalFieldElement.min(a, b, c) is a True sage: elements = [a, b, c] sage: RealIntervalFieldElement.min(*elements) is a True
>>> from sage.all import * >>> from sage.rings.real_mpfi import RealIntervalFieldElement >>> RealIntervalFieldElement.min(a, b, c) is a True >>> elements = [a, b, c] >>> RealIntervalFieldElement.min(*elements) is a True
The generic min does not always do the right thing:
sage: min(0, RIF(-1, 1)) 0 sage: min(RIF(-1, 1), RIF(-100, 100)).endpoints() (-1.00000000000000, 1.00000000000000)
>>> from sage.all import * >>> min(Integer(0), RIF(-Integer(1), Integer(1))) 0 >>> min(RIF(-Integer(1), Integer(1)), RIF(-Integer(100), Integer(100))).endpoints() (-1.00000000000000, 1.00000000000000)
Note that calls involving NaNs try to return a number when possible. This is consistent with IEEE-754-2008 but may be surprising.
sage: RIF('nan').min(2, 1) 1 sage: RIF(-1/3).min(RIF('nan')) -0.3333333333333334? sage: RIF('nan').min(RIF('nan')) [.. NaN ..]
>>> from sage.all import * >>> RIF('nan').min(Integer(2), Integer(1)) 1 >>> RIF(-Integer(1)/Integer(3)).min(RIF('nan')) -0.3333333333333334? >>> RIF('nan').min(RIF('nan')) [.. NaN ..]
See also
- multiplicative_order()[source]¶
Return \(n\) such that
self^n == 1
.Only \(\pm 1\) have finite multiplicative order.
EXAMPLES:
sage: RIF(1).multiplicative_order() 1 sage: RIF(-1).multiplicative_order() 2 sage: RIF(3).multiplicative_order() +Infinity
>>> from sage.all import * >>> RIF(Integer(1)).multiplicative_order() 1 >>> RIF(-Integer(1)).multiplicative_order() 2 >>> RIF(Integer(3)).multiplicative_order() +Infinity
- overlaps(other)[source]¶
Return
True
ifself
andother
are intervals with at least one value in common. For intervalsa
andb
, we havea.overlaps(b)
iffnot(a!=b)
.EXAMPLES:
sage: RIF(0, 1).overlaps(RIF(1, 2)) True sage: RIF(1, 2).overlaps(RIF(0, 1)) True sage: RIF(0, 1).overlaps(RIF(2, 3)) False sage: RIF(2, 3).overlaps(RIF(0, 1)) False sage: RIF(0, 3).overlaps(RIF(1, 2)) True sage: RIF(0, 2).overlaps(RIF(1, 3)) True
>>> from sage.all import * >>> RIF(Integer(0), Integer(1)).overlaps(RIF(Integer(1), Integer(2))) True >>> RIF(Integer(1), Integer(2)).overlaps(RIF(Integer(0), Integer(1))) True >>> RIF(Integer(0), Integer(1)).overlaps(RIF(Integer(2), Integer(3))) False >>> RIF(Integer(2), Integer(3)).overlaps(RIF(Integer(0), Integer(1))) False >>> RIF(Integer(0), Integer(3)).overlaps(RIF(Integer(1), Integer(2))) True >>> RIF(Integer(0), Integer(2)).overlaps(RIF(Integer(1), Integer(3))) True
- prec()[source]¶
Return the precision of
self
.EXAMPLES:
sage: RIF(2.1).precision() 53 sage: RealIntervalField(200)(2.1).precision() 200
>>> from sage.all import * >>> RIF(RealNumber('2.1')).precision() 53 >>> RealIntervalField(Integer(200))(RealNumber('2.1')).precision() 200
- precision()[source]¶
Return the precision of
self
.EXAMPLES:
sage: RIF(2.1).precision() 53 sage: RealIntervalField(200)(2.1).precision() 200
>>> from sage.all import * >>> RIF(RealNumber('2.1')).precision() 53 >>> RealIntervalField(Integer(200))(RealNumber('2.1')).precision() 200
- psi()[source]¶
Return the digamma function evaluated on
self
.OUTPUT: a
RealIntervalFieldElement
EXAMPLES:
sage: psi_1 = RIF(1).psi() sage: psi_1 -0.577215664901533? sage: psi_1.overlaps(-RIF.euler_constant()) True
>>> from sage.all import * >>> psi_1 = RIF(Integer(1)).psi() >>> psi_1 -0.577215664901533? >>> psi_1.overlaps(-RIF.euler_constant()) True
- real()[source]¶
Return the real part of this real interval.
(Since this interval is real, this simply returns itself.)
See also
EXAMPLES:
sage: RIF(1.2465).real() == RIF(1.2465) True
>>> from sage.all import * >>> RIF(RealNumber('1.2465')).real() == RIF(RealNumber('1.2465')) True
- relative_diameter()[source]¶
The relative diameter of this interval (for \([a .. b]\), this is \((b-a)/((a+b)/2)\)), rounded upward, as a
RealNumber
.EXAMPLES:
sage: RIF(1, pi).relative_diameter() # needs sage.symbolic 1.03418797197910
>>> from sage.all import * >>> RIF(Integer(1), pi).relative_diameter() # needs sage.symbolic 1.03418797197910
- round()[source]¶
Return the nearest integer of this interval as an interval.
See also
unique_round()
– return the round as an integer if it is unique and raises aValueError
otherwisefloor()
– truncation towards \(-\infty\)ceil()
– truncation towards \(+\infty\)trunc()
– truncation towards \(0\)
EXAMPLES:
sage: RIF(7.2, 7.3).round() 7 sage: RIF(-3.2, -3.1).round() -3
>>> from sage.all import * >>> RIF(RealNumber('7.2'), RealNumber('7.3')).round() 7 >>> RIF(-RealNumber('3.2'), -RealNumber('3.1')).round() -3
Be careful that the answer is not an integer but an interval:
sage: RIF(2.2, 2.3).round().parent() Real Interval Field with 53 bits of precision
>>> from sage.all import * >>> RIF(RealNumber('2.2'), RealNumber('2.3')).round().parent() Real Interval Field with 53 bits of precision
And in some cases, the lower and upper bounds of this interval do not agree:
sage: r = RIF(2.5, 3.5).round() sage: r 4.? sage: r.lower() 3.00000000000000 sage: r.upper() 4.00000000000000
>>> from sage.all import * >>> r = RIF(RealNumber('2.5'), RealNumber('3.5')).round() >>> r 4.? >>> r.lower() 3.00000000000000 >>> r.upper() 4.00000000000000
- sec()[source]¶
Return the secant of this number.
EXAMPLES:
sage: RealIntervalField(100)(2).sec() -2.40299796172238098975460040142?
>>> from sage.all import * >>> RealIntervalField(Integer(100))(Integer(2)).sec() -2.40299796172238098975460040142?
- sech()[source]¶
Return the hyperbolic secant of
self
.EXAMPLES:
sage: RealIntervalField(100)(2).sech() 0.265802228834079692120862739820?
>>> from sage.all import * >>> RealIntervalField(Integer(100))(Integer(2)).sech() 0.265802228834079692120862739820?
- simplest_rational(low_open=False, high_open=False)[source]¶
Return the simplest rational in this interval. Given rationals \(a / b\) and \(c / d\) (both in lowest terms), the former is simpler if \(b<d\) or if \(b = d\) and \(|a| < |c|\).
If optional parameters
low_open
orhigh_open
areTrue
, then treat this as an open interval on that end.EXAMPLES:
sage: RealIntervalField(10)(pi).simplest_rational() # needs sage.symbolic 22/7 sage: RealIntervalField(20)(pi).simplest_rational() # needs sage.symbolic 355/113 sage: RIF(0.123, 0.567).simplest_rational() 1/2 sage: RIF(RR(1/3).nextabove(), RR(3/7)).simplest_rational() 2/5 sage: RIF(1234/567).simplest_rational() 1234/567 sage: RIF(-8765/432).simplest_rational() -8765/432 sage: RIF(-1.234, 0.003).simplest_rational() 0 sage: RIF(RR(1/3)).simplest_rational() 6004799503160661/18014398509481984 sage: RIF(RR(1/3)).simplest_rational(high_open=True) Traceback (most recent call last): ... ValueError: simplest_rational() on open, empty interval sage: RIF(1/3, 1/2).simplest_rational() 1/2 sage: RIF(1/3, 1/2).simplest_rational(high_open=True) 1/3 sage: phi = ((RealIntervalField(500)(5).sqrt() + 1)/2) sage: phi.simplest_rational() == fibonacci(362)/fibonacci(361) True
>>> from sage.all import * >>> RealIntervalField(Integer(10))(pi).simplest_rational() # needs sage.symbolic 22/7 >>> RealIntervalField(Integer(20))(pi).simplest_rational() # needs sage.symbolic 355/113 >>> RIF(RealNumber('0.123'), RealNumber('0.567')).simplest_rational() 1/2 >>> RIF(RR(Integer(1)/Integer(3)).nextabove(), RR(Integer(3)/Integer(7))).simplest_rational() 2/5 >>> RIF(Integer(1234)/Integer(567)).simplest_rational() 1234/567 >>> RIF(-Integer(8765)/Integer(432)).simplest_rational() -8765/432 >>> RIF(-RealNumber('1.234'), RealNumber('0.003')).simplest_rational() 0 >>> RIF(RR(Integer(1)/Integer(3))).simplest_rational() 6004799503160661/18014398509481984 >>> RIF(RR(Integer(1)/Integer(3))).simplest_rational(high_open=True) Traceback (most recent call last): ... ValueError: simplest_rational() on open, empty interval >>> RIF(Integer(1)/Integer(3), Integer(1)/Integer(2)).simplest_rational() 1/2 >>> RIF(Integer(1)/Integer(3), Integer(1)/Integer(2)).simplest_rational(high_open=True) 1/3 >>> phi = ((RealIntervalField(Integer(500))(Integer(5)).sqrt() + Integer(1))/Integer(2)) >>> phi.simplest_rational() == fibonacci(Integer(362))/fibonacci(Integer(361)) True
- sin()[source]¶
Return the sine of
self
.EXAMPLES:
sage: R = RealIntervalField(100) sage: R(2).sin() 0.909297426825681695396019865912?
>>> from sage.all import * >>> R = RealIntervalField(Integer(100)) >>> R(Integer(2)).sin() 0.909297426825681695396019865912?
- sinh()[source]¶
Return the hyperbolic sine of
self
.EXAMPLES:
sage: q = RIF.pi()/12 sage: q.sinh() 0.2648002276022707?
>>> from sage.all import * >>> q = RIF.pi()/Integer(12) >>> q.sinh() 0.2648002276022707?
- sqrt()[source]¶
Return a square root of
self
. Raises an error ifself
is nonpositive.If you use
square_root()
then an interval will always be returned (though it will beNaN
ifself
is nonpositive).EXAMPLES:
sage: r = RIF(4.0) sage: r.sqrt() 2 sage: r.sqrt()^2 == r True
>>> from sage.all import * >>> r = RIF(RealNumber('4.0')) >>> r.sqrt() 2 >>> r.sqrt()**Integer(2) == r True
sage: r = RIF(4344) sage: r.sqrt() 65.90902821313633? sage: r.sqrt()^2 == r False sage: r in r.sqrt()^2 True sage: r.sqrt()^2 - r 0.?e-11 sage: (r.sqrt()^2 - r).str(style='brackets') '[-9.0949470177292824e-13 .. 1.8189894035458565e-12]'
>>> from sage.all import * >>> r = RIF(Integer(4344)) >>> r.sqrt() 65.90902821313633? >>> r.sqrt()**Integer(2) == r False >>> r in r.sqrt()**Integer(2) True >>> r.sqrt()**Integer(2) - r 0.?e-11 >>> (r.sqrt()**Integer(2) - r).str(style='brackets') '[-9.0949470177292824e-13 .. 1.8189894035458565e-12]'
sage: r = RIF(-2.0) sage: r.sqrt() Traceback (most recent call last): ... ValueError: self (=-2) is not >= 0
>>> from sage.all import * >>> r = RIF(-RealNumber('2.0')) >>> r.sqrt() Traceback (most recent call last): ... ValueError: self (=-2) is not >= 0
sage: r = RIF(-2, 2) sage: r.sqrt() Traceback (most recent call last): ... ValueError: self (=0.?e1) is not >= 0
>>> from sage.all import * >>> r = RIF(-Integer(2), Integer(2)) >>> r.sqrt() Traceback (most recent call last): ... ValueError: self (=0.?e1) is not >= 0
- square()[source]¶
Return the square of
self
.Note
Squaring an interval is different than multiplying it by itself, because the square can never be negative.
EXAMPLES:
sage: RIF(1, 2).square().str(style='brackets') '[1.0000000000000000 .. 4.0000000000000000]' sage: RIF(-1, 1).square().str(style='brackets') '[0.0000000000000000 .. 1.0000000000000000]' sage: (RIF(-1, 1) * RIF(-1, 1)).str(style='brackets') '[-1.0000000000000000 .. 1.0000000000000000]'
>>> from sage.all import * >>> RIF(Integer(1), Integer(2)).square().str(style='brackets') '[1.0000000000000000 .. 4.0000000000000000]' >>> RIF(-Integer(1), Integer(1)).square().str(style='brackets') '[0.0000000000000000 .. 1.0000000000000000]' >>> (RIF(-Integer(1), Integer(1)) * RIF(-Integer(1), Integer(1))).str(style='brackets') '[-1.0000000000000000 .. 1.0000000000000000]'
- square_root()[source]¶
Return a square root of
self
. An interval will always be returned (though it will beNaN
ifself
is nonpositive).EXAMPLES:
sage: r = RIF(-2.0) sage: r.square_root() [.. NaN ..] sage: r.sqrt() Traceback (most recent call last): ... ValueError: self (=-2) is not >= 0
>>> from sage.all import * >>> r = RIF(-RealNumber('2.0')) >>> r.square_root() [.. NaN ..] >>> r.sqrt() Traceback (most recent call last): ... ValueError: self (=-2) is not >= 0
- str(base=10, style=None, no_sci=None, e=None, error_digits=None)[source]¶
Return a string representation of
self
.INPUT:
base
– base for outputstyle
– the printing style; either'brackets'
or'question'
(orNone
, to use the current default)no_sci
– ifTrue
do not print using scientific notation; ifFalse
print with scientific notation; ifNone
(the default), print how the parent prints.e
– symbol used in scientific notationerror_digits
– the number of digits of error to print, in'question'
style
We support two different styles of printing;
'question'
style and'brackets'
style. In question style (the default), we print the “known correct” part of the number, followed by a question mark:sage: RIF(pi).str() # needs sage.symbolic '3.141592653589794?' sage: RIF(pi, 22/7).str() # needs sage.symbolic '3.142?' sage: RIF(pi, 22/7).str(style='question') # needs sage.symbolic '3.142?'
>>> from sage.all import * >>> RIF(pi).str() # needs sage.symbolic '3.141592653589794?' >>> RIF(pi, Integer(22)/Integer(7)).str() # needs sage.symbolic '3.142?' >>> RIF(pi, Integer(22)/Integer(7)).str(style='question') # needs sage.symbolic '3.142?'
However, if the interval is precisely equal to some integer that’s not too large, we just return that integer:
sage: RIF(-42).str() '-42' sage: RIF(0).str() '0' sage: RIF(12^5).str(base=3) '110122100000'
>>> from sage.all import * >>> RIF(-Integer(42)).str() '-42' >>> RIF(Integer(0)).str() '0' >>> RIF(Integer(12)**Integer(5)).str(base=Integer(3)) '110122100000'
Very large integers, however, revert to the normal question-style printing:
sage: RIF(3^7).str() '2187' sage: RIF(3^7 * 2^256).str() '2.5323729916201052?e80'
>>> from sage.all import * >>> RIF(Integer(3)**Integer(7)).str() '2187' >>> RIF(Integer(3)**Integer(7) * Integer(2)**Integer(256)).str() '2.5323729916201052?e80'
In brackets style, we print the lower and upper bounds of the interval within brackets:
sage: RIF(237/16).str(style='brackets') '[14.812500000000000 .. 14.812500000000000]'
>>> from sage.all import * >>> RIF(Integer(237)/Integer(16)).str(style='brackets') '[14.812500000000000 .. 14.812500000000000]'
Note that the lower bound is rounded down, and the upper bound is rounded up. So even if the lower and upper bounds are equal, they may print differently. (This is done so that the printed representation of the interval contains all the numbers in the internal binary interval.)
For instance, we find the best 10-bit floating point representation of
1/3
:sage: RR10 = RealField(10) sage: RR(RR10(1/3)) 0.333496093750000
>>> from sage.all import * >>> RR10 = RealField(Integer(10)) >>> RR(RR10(Integer(1)/Integer(3))) 0.333496093750000
And we see that the point interval containing only this floating-point number prints as a wider decimal interval, that does contain the number:
sage: RIF10 = RealIntervalField(10) sage: RIF10(RR10(1/3)).str(style='brackets') '[0.33349 .. 0.33350]'
>>> from sage.all import * >>> RIF10 = RealIntervalField(Integer(10)) >>> RIF10(RR10(Integer(1)/Integer(3))).str(style='brackets') '[0.33349 .. 0.33350]'
We always use brackets style for
NaN
and infinities:sage: RIF(pi, infinity) # needs sage.symbolic [3.1415926535897931 .. +infinity] sage: RIF(NaN) # needs sage.symbolic [.. NaN ..]
>>> from sage.all import * >>> RIF(pi, infinity) # needs sage.symbolic [3.1415926535897931 .. +infinity] >>> RIF(NaN) # needs sage.symbolic [.. NaN ..]
Let’s take a closer, formal look at the question style. In its full generality, a number printed in the question style looks like:
MANTISSA ?ERROR eEXPONENT
(without the spaces). The “eEXPONENT” part is optional; if it is missing, then the exponent is 0. (If the base is greater than 10, then the exponent separator is “@” instead of “e”.)
The “ERROR” is optional; if it is missing, then the error is 1.
The mantissa is printed in base \(b\), and always contains a decimal point (also known as a radix point, in bases other than 10). (The error and exponent are always printed in base 10.)
We define the “precision” of a floating-point printed representation to be the positional value of the last digit of the mantissa. For instance, in
2.7?e5
, the precision is \(10^4\); in8.?
, the precision is \(10^0\); and in9.35?
the precision is \(10^{-2}\). This precision will always be \(10^k\) for some \(k\) (or, for an arbitrary base \(b\), \(b^k\)).Then the interval is contained in the interval:
\[\text{mantissa} \cdot b^{\text{exponent}} - \text{error} \cdot b^k .. \text{mantissa} \cdot b^{\text{exponent}} + \text{error} \cdot b^k\]To control the printing, we can specify a maximum number of error digits. The default is 0, which means that we do not print an error at all (so that the error is always the default, 1).
Now, consider the precisions needed to represent the endpoints (this is the precision that would be produced by
v.lower().str(no_sci=False)
). Our result is no more precise than the less precise endpoint, and is sufficiently imprecise that the error can be represented with the given number of decimal digits. Our result is the most precise possible result, given these restrictions. When there are two possible results of equal precision and with the same error width, then we pick the one which is farther from zero. (For instance,RIF(0, 123)
with two error digits could print as61.?62
or62.?62
. We prefer the latter because it makes it clear that the interval is known not to be negative.)EXAMPLES:
sage: a = RIF(59/27); a 2.185185185185186? sage: a.str() '2.185185185185186?' sage: a.str(style='brackets') '[2.1851851851851851 .. 2.1851851851851856]' sage: a.str(16) '2.2f684bda12f69?' sage: a.str(no_sci=False) '2.185185185185186?e0' sage: pi_appr = RIF(pi, 22/7) sage: pi_appr.str(style='brackets') '[3.1415926535897931 .. 3.1428571428571433]' sage: pi_appr.str() '3.142?' sage: pi_appr.str(error_digits=1) '3.1422?7' sage: pi_appr.str(error_digits=2) '3.14223?64' sage: pi_appr.str(base=36) '3.6?' sage: RIF(NaN) # needs sage.symbolic [.. NaN ..] sage: RIF(pi, infinity) # needs sage.symbolic [3.1415926535897931 .. +infinity] sage: RIF(-infinity, pi) # needs sage.symbolic [-infinity .. 3.1415926535897936] sage: RealIntervalField(210)(3).sqrt() 1.732050807568877293527446341505872366942805253810380628055806980? sage: RealIntervalField(210)(RIF(3).sqrt()) 1.732050807568878? sage: RIF(3).sqrt() 1.732050807568878? sage: RIF(0, 3^-150) # needs sage.symbolic 1.?e-71
>>> from sage.all import * >>> a = RIF(Integer(59)/Integer(27)); a 2.185185185185186? >>> a.str() '2.185185185185186?' >>> a.str(style='brackets') '[2.1851851851851851 .. 2.1851851851851856]' >>> a.str(Integer(16)) '2.2f684bda12f69?' >>> a.str(no_sci=False) '2.185185185185186?e0' >>> pi_appr = RIF(pi, Integer(22)/Integer(7)) >>> pi_appr.str(style='brackets') '[3.1415926535897931 .. 3.1428571428571433]' >>> pi_appr.str() '3.142?' >>> pi_appr.str(error_digits=Integer(1)) '3.1422?7' >>> pi_appr.str(error_digits=Integer(2)) '3.14223?64' >>> pi_appr.str(base=Integer(36)) '3.6?' >>> RIF(NaN) # needs sage.symbolic [.. NaN ..] >>> RIF(pi, infinity) # needs sage.symbolic [3.1415926535897931 .. +infinity] >>> RIF(-infinity, pi) # needs sage.symbolic [-infinity .. 3.1415926535897936] >>> RealIntervalField(Integer(210))(Integer(3)).sqrt() 1.732050807568877293527446341505872366942805253810380628055806980? >>> RealIntervalField(Integer(210))(RIF(Integer(3)).sqrt()) 1.732050807568878? >>> RIF(Integer(3)).sqrt() 1.732050807568878? >>> RIF(Integer(0), Integer(3)**-Integer(150)) # needs sage.symbolic 1.?e-71
- tan()[source]¶
Return the tangent of
self
.EXAMPLES:
sage: q = RIF.pi()/3 sage: q.tan() 1.732050807568877? sage: q = RIF.pi()/6 sage: q.tan() 0.577350269189626?
>>> from sage.all import * >>> q = RIF.pi()/Integer(3) >>> q.tan() 1.732050807568877? >>> q = RIF.pi()/Integer(6) >>> q.tan() 0.577350269189626?
- tanh()[source]¶
Return the hyperbolic tangent of
self
.EXAMPLES:
sage: q = RIF.pi()/11 sage: q.tanh() 0.2780794292958503?
>>> from sage.all import * >>> q = RIF.pi()/Integer(11) >>> q.tanh() 0.2780794292958503?
- trunc()[source]¶
Return the truncation of this interval as an interval.
The truncation of \(x\) is the floor of \(x\) if \(x\) is nonnegative or the ceil of \(x\) if \(x\) is negative.
See also
unique_trunc()
– return the trunc as an integer if it is unique and raises aValueError
otherwisefloor()
– truncation towards \(-\infty\)ceil()
– truncation towards \(+\infty\)round()
– rounding
EXAMPLES:
sage: RIF(2.3, 2.7).trunc() 2 sage: parent(_) Real Interval Field with 53 bits of precision sage: RIF(-0.9, 0.9).trunc() 0 sage: RIF(-7.5, -7.3).trunc() -7
>>> from sage.all import * >>> RIF(RealNumber('2.3'), RealNumber('2.7')).trunc() 2 >>> parent(_) Real Interval Field with 53 bits of precision >>> RIF(-RealNumber('0.9'), RealNumber('0.9')).trunc() 0 >>> RIF(-RealNumber('7.5'), -RealNumber('7.3')).trunc() -7
In the above example, the obtained interval contains only one element. But on the following it is not the case anymore:
sage: r = RIF(2.99, 3.01).trunc() sage: r.upper() 3.00000000000000 sage: r.lower() 2.00000000000000
>>> from sage.all import * >>> r = RIF(RealNumber('2.99'), RealNumber('3.01')).trunc() >>> r.upper() 3.00000000000000 >>> r.lower() 2.00000000000000
- union(other)[source]¶
Return the union of two intervals, or of an interval and a real number (more precisely, the convex hull).
EXAMPLES:
sage: RIF(1, 2).union(RIF(pi, 22/7)).str(style='brackets') '[1.0000000000000000 .. 3.1428571428571433]' sage: RIF(1, 2).union(pi).str(style='brackets') '[1.0000000000000000 .. 3.1415926535897936]' sage: RIF(1).union(RIF(0, 2)).str(style='brackets') '[0.0000000000000000 .. 2.0000000000000000]' sage: RIF(1).union(RIF(-1)).str(style='brackets') '[-1.0000000000000000 .. 1.0000000000000000]'
>>> from sage.all import * >>> RIF(Integer(1), Integer(2)).union(RIF(pi, Integer(22)/Integer(7))).str(style='brackets') '[1.0000000000000000 .. 3.1428571428571433]' >>> RIF(Integer(1), Integer(2)).union(pi).str(style='brackets') '[1.0000000000000000 .. 3.1415926535897936]' >>> RIF(Integer(1)).union(RIF(Integer(0), Integer(2))).str(style='brackets') '[0.0000000000000000 .. 2.0000000000000000]' >>> RIF(Integer(1)).union(RIF(-Integer(1))).str(style='brackets') '[-1.0000000000000000 .. 1.0000000000000000]'
- unique_ceil()[source]¶
Return the unique ceiling of this interval, if it is well defined, otherwise raise a
ValueError
.OUTPUT: integer
See also
ceil()
– return the ceil as an interval (and never raise error)EXAMPLES:
sage: RIF(pi).unique_ceil() # needs sage.symbolic 4 sage: RIF(100*pi).unique_ceil() # needs sage.symbolic 315 sage: RIF(100, 200).unique_ceil() Traceback (most recent call last): ... ValueError: interval does not have a unique ceil
>>> from sage.all import * >>> RIF(pi).unique_ceil() # needs sage.symbolic 4 >>> RIF(Integer(100)*pi).unique_ceil() # needs sage.symbolic 315 >>> RIF(Integer(100), Integer(200)).unique_ceil() Traceback (most recent call last): ... ValueError: interval does not have a unique ceil
- unique_floor()[source]¶
Return the unique floor of this interval, if it is well defined, otherwise raise a
ValueError
.OUTPUT: integer
See also
floor()
– return the floor as an interval (and never raise error)EXAMPLES:
sage: RIF(pi).unique_floor() # needs sage.symbolic 3 sage: RIF(100*pi).unique_floor() # needs sage.symbolic 314 sage: RIF(100, 200).unique_floor() Traceback (most recent call last): ... ValueError: interval does not have a unique floor
>>> from sage.all import * >>> RIF(pi).unique_floor() # needs sage.symbolic 3 >>> RIF(Integer(100)*pi).unique_floor() # needs sage.symbolic 314 >>> RIF(Integer(100), Integer(200)).unique_floor() Traceback (most recent call last): ... ValueError: interval does not have a unique floor
- unique_integer()[source]¶
Return the unique integer in this interval, if there is exactly one, otherwise raise a
ValueError
.EXAMPLES:
sage: RIF(pi).unique_integer() # needs sage.symbolic Traceback (most recent call last): ... ValueError: interval contains no integer sage: RIF(pi, pi+1).unique_integer() # needs sage.symbolic 4 sage: RIF(pi, pi+2).unique_integer() # needs sage.symbolic Traceback (most recent call last): ... ValueError: interval contains more than one integer sage: RIF(100).unique_integer() 100
>>> from sage.all import * >>> RIF(pi).unique_integer() # needs sage.symbolic Traceback (most recent call last): ... ValueError: interval contains no integer >>> RIF(pi, pi+Integer(1)).unique_integer() # needs sage.symbolic 4 >>> RIF(pi, pi+Integer(2)).unique_integer() # needs sage.symbolic Traceback (most recent call last): ... ValueError: interval contains more than one integer >>> RIF(Integer(100)).unique_integer() 100
- unique_round()[source]¶
Return the unique round (nearest integer) of this interval, if it is well defined, otherwise raise a
ValueError
.OUTPUT: integer
See also
round()
– return the round as an interval (and never raise error)EXAMPLES:
sage: RIF(pi).unique_round() # needs sage.symbolic 3 sage: RIF(1000*pi).unique_round() # needs sage.symbolic 3142 sage: RIF(100, 200).unique_round() Traceback (most recent call last): ... ValueError: interval does not have a unique round (nearest integer) sage: RIF(1.2, 1.7).unique_round() Traceback (most recent call last): ... ValueError: interval does not have a unique round (nearest integer) sage: RIF(0.7, 1.2).unique_round() 1 sage: RIF(-pi).unique_round() # needs sage.symbolic -3 sage: (RIF(4.5).unique_round(), RIF(-4.5).unique_round()) (5, -5)
>>> from sage.all import * >>> RIF(pi).unique_round() # needs sage.symbolic 3 >>> RIF(Integer(1000)*pi).unique_round() # needs sage.symbolic 3142 >>> RIF(Integer(100), Integer(200)).unique_round() Traceback (most recent call last): ... ValueError: interval does not have a unique round (nearest integer) >>> RIF(RealNumber('1.2'), RealNumber('1.7')).unique_round() Traceback (most recent call last): ... ValueError: interval does not have a unique round (nearest integer) >>> RIF(RealNumber('0.7'), RealNumber('1.2')).unique_round() 1 >>> RIF(-pi).unique_round() # needs sage.symbolic -3 >>> (RIF(RealNumber('4.5')).unique_round(), RIF(-RealNumber('4.5')).unique_round()) (5, -5)
- unique_sign()[source]¶
Return the sign of this element if it is well defined.
This method returns \(+1\) if all elements in this interval are positive, \(-1\) if all of them are negative and \(0\) if it contains only zero. Otherwise it raises a
ValueError
.EXAMPLES:
sage: RIF(1.2,5.7).unique_sign() 1 sage: RIF(-3,-2).unique_sign() -1 sage: RIF(0).unique_sign() 0 sage: RIF(0,1).unique_sign() Traceback (most recent call last): ... ValueError: interval does not have a unique sign sage: RIF(-1,0).unique_sign() Traceback (most recent call last): ... ValueError: interval does not have a unique sign sage: RIF(-0.1, 0.1).unique_sign() Traceback (most recent call last): ... ValueError: interval does not have a unique sign
>>> from sage.all import * >>> RIF(RealNumber('1.2'),RealNumber('5.7')).unique_sign() 1 >>> RIF(-Integer(3),-Integer(2)).unique_sign() -1 >>> RIF(Integer(0)).unique_sign() 0 >>> RIF(Integer(0),Integer(1)).unique_sign() Traceback (most recent call last): ... ValueError: interval does not have a unique sign >>> RIF(-Integer(1),Integer(0)).unique_sign() Traceback (most recent call last): ... ValueError: interval does not have a unique sign >>> RIF(-RealNumber('0.1'), RealNumber('0.1')).unique_sign() Traceback (most recent call last): ... ValueError: interval does not have a unique sign
- unique_trunc()[source]¶
Return the nearest integer toward zero if it is unique, otherwise raise a
ValueError
.See also
trunc()
– return the truncation as an interval (and never raise error)EXAMPLES:
sage: RIF(1.3,1.4).unique_trunc() 1 sage: RIF(-3.3, -3.2).unique_trunc() -3 sage: RIF(2.9,3.2).unique_trunc() Traceback (most recent call last): ... ValueError: interval does not have a unique trunc (nearest integer toward zero) sage: RIF(-3.1,-2.9).unique_trunc() Traceback (most recent call last): ... ValueError: interval does not have a unique trunc (nearest integer toward zero)
>>> from sage.all import * >>> RIF(RealNumber('1.3'),RealNumber('1.4')).unique_trunc() 1 >>> RIF(-RealNumber('3.3'), -RealNumber('3.2')).unique_trunc() -3 >>> RIF(RealNumber('2.9'),RealNumber('3.2')).unique_trunc() Traceback (most recent call last): ... ValueError: interval does not have a unique trunc (nearest integer toward zero) >>> RIF(-RealNumber('3.1'),-RealNumber('2.9')).unique_trunc() Traceback (most recent call last): ... ValueError: interval does not have a unique trunc (nearest integer toward zero)
- upper(rnd=None)[source]¶
Return the upper bound of
self
.INPUT:
rnd
– the rounding mode (default: towards plus infinity, seesage.rings.real_mpfr.RealField
for possible values)
The rounding mode does not affect the value returned as a floating-point number, but it does control which variety of
RealField
the returned number is in, which affects printing and subsequent operations.EXAMPLES:
sage: R = RealIntervalField(13) sage: R.pi().upper().str() '3.1417'
>>> from sage.all import * >>> R = RealIntervalField(Integer(13)) >>> R.pi().upper().str() '3.1417'
sage: R = RealIntervalField(13) sage: x = R(1.2,1.3); x.str(style='brackets') '[1.1999 .. 1.3001]' sage: x.upper() 1.31 sage: x.upper('RNDU') 1.31 sage: x.upper('RNDN') 1.30 sage: x.upper('RNDD') 1.30 sage: x.upper('RNDZ') 1.30 sage: x.upper('RNDA') 1.31 sage: x.upper().parent() Real Field with 13 bits of precision and rounding RNDU sage: x.upper('RNDD').parent() Real Field with 13 bits of precision and rounding RNDD sage: x.upper() == x.upper('RNDD') True
>>> from sage.all import * >>> R = RealIntervalField(Integer(13)) >>> x = R(RealNumber('1.2'),RealNumber('1.3')); x.str(style='brackets') '[1.1999 .. 1.3001]' >>> x.upper() 1.31 >>> x.upper('RNDU') 1.31 >>> x.upper('RNDN') 1.30 >>> x.upper('RNDD') 1.30 >>> x.upper('RNDZ') 1.30 >>> x.upper('RNDA') 1.31 >>> x.upper().parent() Real Field with 13 bits of precision and rounding RNDU >>> x.upper('RNDD').parent() Real Field with 13 bits of precision and rounding RNDD >>> x.upper() == x.upper('RNDD') True
- zeta(a=None)[source]¶
Return the image of this interval by the Hurwitz zeta function.
For
a = 1
(ora = None
), this computes the Riemann zeta function.EXAMPLES:
sage: zeta(RIF(3)) 1.202056903159594? sage: _.parent() Real Interval Field with 53 bits of precision sage: RIF(3).zeta(1/2) 8.41439832211716?
>>> from sage.all import * >>> zeta(RIF(Integer(3))) 1.202056903159594? >>> _.parent() Real Interval Field with 53 bits of precision >>> RIF(Integer(3)).zeta(Integer(1)/Integer(2)) 8.41439832211716?
- class sage.rings.real_mpfi.RealIntervalField_class[source]¶
Bases:
RealIntervalField
Class of the real interval field.
INPUT:
prec
– integer (default: 53); precisionprec
is the number of bits used to represent the mantissa of a floating-point number. The precision can be any integer betweenmpfr_prec_min()
andmpfr_prec_max()
. In the current implementation,mpfr_prec_min()
is equal to 2.sci_not
– boolean (default:False
); whether or not to display using scientific notation
EXAMPLES:
sage: RealIntervalField(10) Real Interval Field with 10 bits of precision sage: RealIntervalField() Real Interval Field with 53 bits of precision sage: RealIntervalField(100000) Real Interval Field with 100000 bits of precision
>>> from sage.all import * >>> RealIntervalField(Integer(10)) Real Interval Field with 10 bits of precision >>> RealIntervalField() Real Interval Field with 53 bits of precision >>> RealIntervalField(Integer(100000)) Real Interval Field with 100000 bits of precision
Note
The default precision is 53, since according to the GMP manual: ‘mpfr should be able to exactly reproduce all computations with double-precision machine floating-point numbers (double type in C), except the default exponent range is much wider and subnormal numbers are not implemented.’
EXAMPLES:
Creation of elements.
First with default precision. First we coerce elements of various types, then we coerce intervals:
sage: RIF = RealIntervalField(); RIF Real Interval Field with 53 bits of precision sage: RIF(3) 3 sage: RIF(RIF(3)) 3 sage: RIF(pi) # needs sage.symbolic 3.141592653589794? sage: RIF(RealField(53)('1.5')) 1.5000000000000000? sage: RIF(-2/19) -0.1052631578947369? sage: RIF(-3939) -3939 sage: RIF(-3939r) -3939 sage: RIF('1.5') 1.5000000000000000? sage: R200 = RealField(200) sage: RIF(R200.pi()) 3.141592653589794? sage: RIF(10^100) 1.000000000000000?e100
>>> from sage.all import * >>> RIF = RealIntervalField(); RIF Real Interval Field with 53 bits of precision >>> RIF(Integer(3)) 3 >>> RIF(RIF(Integer(3))) 3 >>> RIF(pi) # needs sage.symbolic 3.141592653589794? >>> RIF(RealField(Integer(53))('1.5')) 1.5000000000000000? >>> RIF(-Integer(2)/Integer(19)) -0.1052631578947369? >>> RIF(-Integer(3939)) -3939 >>> RIF(-3939) -3939 >>> RIF('1.5') 1.5000000000000000? >>> R200 = RealField(Integer(200)) >>> RIF(R200.pi()) 3.141592653589794? >>> RIF(Integer(10)**Integer(100)) 1.000000000000000?e100
The base must be explicitly specified as a named parameter:
sage: RIF('101101', base=2) 45 sage: RIF('+infinity') [+infinity .. +infinity] sage: RIF('[1..3]').str(style='brackets') '[1.0000000000000000 .. 3.0000000000000000]'
>>> from sage.all import * >>> RIF('101101', base=Integer(2)) 45 >>> RIF('+infinity') [+infinity .. +infinity] >>> RIF('[1..3]').str(style='brackets') '[1.0000000000000000 .. 3.0000000000000000]'
All string-like types are accepted:
sage: RIF(b"100", u"100") 100
>>> from sage.all import * >>> RIF(b"100", u"100") 100
Next we coerce some 2-tuples, which define intervals:
sage: RIF((-1.5, -1.3)) -1.4? sage: RIF((RDF('-1.5'), RDF('-1.3'))) -1.4? sage: RIF((1/3,2/3)).str(style='brackets') '[0.33333333333333331 .. 0.66666666666666675]'
>>> from sage.all import * >>> RIF((-RealNumber('1.5'), -RealNumber('1.3'))) -1.4? >>> RIF((RDF('-1.5'), RDF('-1.3'))) -1.4? >>> RIF((Integer(1)/Integer(3),Integer(2)/Integer(3))).str(style='brackets') '[0.33333333333333331 .. 0.66666666666666675]'
The extra parentheses aren’t needed:
sage: RIF(1/3,2/3).str(style='brackets') '[0.33333333333333331 .. 0.66666666666666675]' sage: RIF((1,2)).str(style='brackets') '[1.0000000000000000 .. 2.0000000000000000]' sage: RIF((1r,2r)).str(style='brackets') '[1.0000000000000000 .. 2.0000000000000000]' sage: RIF((pi, e)).str(style='brackets') '[2.7182818284590450 .. 3.1415926535897936]'
>>> from sage.all import * >>> RIF(Integer(1)/Integer(3),Integer(2)/Integer(3)).str(style='brackets') '[0.33333333333333331 .. 0.66666666666666675]' >>> RIF((Integer(1),Integer(2))).str(style='brackets') '[1.0000000000000000 .. 2.0000000000000000]' >>> RIF((1,2)).str(style='brackets') '[1.0000000000000000 .. 2.0000000000000000]' >>> RIF((pi, e)).str(style='brackets') '[2.7182818284590450 .. 3.1415926535897936]'
Values which can be represented as an exact floating-point number (of the precision of this
RealIntervalField
) result in a precise interval, where the lower bound is equal to the upper bound (even if they print differently). Other values typically result in an interval where the lower and upper bounds are adjacent floating-point numbers.sage: def check(x): ....: return (x, x.lower() == x.upper()) sage: check(RIF(pi)) # needs sage.symbolic (3.141592653589794?, False) sage: check(RIF(RR(pi))) # needs sage.symbolic (3.1415926535897932?, True) sage: check(RIF(1.5)) (1.5000000000000000?, True) sage: check(RIF('1.5')) (1.5000000000000000?, True) sage: check(RIF(0.1)) (0.10000000000000001?, True) sage: check(RIF(1/10)) (0.10000000000000000?, False) sage: check(RIF('0.1')) (0.10000000000000000?, False)
>>> from sage.all import * >>> def check(x): ... return (x, x.lower() == x.upper()) >>> check(RIF(pi)) # needs sage.symbolic (3.141592653589794?, False) >>> check(RIF(RR(pi))) # needs sage.symbolic (3.1415926535897932?, True) >>> check(RIF(RealNumber('1.5'))) (1.5000000000000000?, True) >>> check(RIF('1.5')) (1.5000000000000000?, True) >>> check(RIF(RealNumber('0.1'))) (0.10000000000000001?, True) >>> check(RIF(Integer(1)/Integer(10))) (0.10000000000000000?, False) >>> check(RIF('0.1')) (0.10000000000000000?, False)
Similarly, when specifying both ends of an interval, the lower end is rounded down and the upper end is rounded up:
sage: outward = RIF(1/10, 7/10); outward.str(style='brackets') '[0.099999999999999991 .. 0.70000000000000007]' sage: nearest = RIF(RR(1/10), RR(7/10)); nearest.str(style='brackets') '[0.10000000000000000 .. 0.69999999999999996]' sage: nearest.lower() - outward.lower() 1.38777878078144e-17 sage: outward.upper() - nearest.upper() 1.11022302462516e-16
>>> from sage.all import * >>> outward = RIF(Integer(1)/Integer(10), Integer(7)/Integer(10)); outward.str(style='brackets') '[0.099999999999999991 .. 0.70000000000000007]' >>> nearest = RIF(RR(Integer(1)/Integer(10)), RR(Integer(7)/Integer(10))); nearest.str(style='brackets') '[0.10000000000000000 .. 0.69999999999999996]' >>> nearest.lower() - outward.lower() 1.38777878078144e-17 >>> outward.upper() - nearest.upper() 1.11022302462516e-16
Some examples with a real interval field of higher precision:
sage: R = RealIntervalField(100) sage: R(3) 3 sage: R(R(3)) 3 sage: R(pi) # needs sage.symbolic 3.14159265358979323846264338328? sage: R(-2/19) -0.1052631578947368421052631578948? sage: R(e,pi).str(style='brackets') # needs sage.symbolic '[2.7182818284590452353602874713512 .. 3.1415926535897932384626433832825]'
>>> from sage.all import * >>> R = RealIntervalField(Integer(100)) >>> R(Integer(3)) 3 >>> R(R(Integer(3))) 3 >>> R(pi) # needs sage.symbolic 3.14159265358979323846264338328? >>> R(-Integer(2)/Integer(19)) -0.1052631578947368421052631578948? >>> R(e,pi).str(style='brackets') # needs sage.symbolic '[2.7182818284590452353602874713512 .. 3.1415926535897932384626433832825]'
See also
sage.rings.real_arb.RealBallField
(alternative implementation of real intervals, with more features)
- Element[source]¶
alias of
RealIntervalFieldElement
- algebraic_closure()[source]¶
Return the algebraic closure of this interval field, i.e., the complex interval field with the same precision.
EXAMPLES:
sage: RIF.algebraic_closure() Complex Interval Field with 53 bits of precision sage: RIF.algebraic_closure() is CIF True sage: RealIntervalField(100).algebraic_closure() Complex Interval Field with 100 bits of precision
>>> from sage.all import * >>> RIF.algebraic_closure() Complex Interval Field with 53 bits of precision >>> RIF.algebraic_closure() is CIF True >>> RealIntervalField(Integer(100)).algebraic_closure() Complex Interval Field with 100 bits of precision
- characteristic()[source]¶
Return 0, since the field of real numbers has characteristic 0.
EXAMPLES:
sage: RealIntervalField(10).characteristic() 0
>>> from sage.all import * >>> RealIntervalField(Integer(10)).characteristic() 0
- complex_field()[source]¶
Return complex field of the same precision.
EXAMPLES:
sage: RIF.complex_field() Complex Interval Field with 53 bits of precision
>>> from sage.all import * >>> RIF.complex_field() Complex Interval Field with 53 bits of precision
- construction()[source]¶
Return the functorial construction of
self
, namely, completion of the rational numbers with respect to the prime at \(\infty\), and the note that this is an interval field.Also preserves other information that makes this field unique (e.g. precision, print mode).
EXAMPLES:
sage: R = RealIntervalField(123) sage: c, S = R.construction(); S Rational Field sage: R == c(S) True
>>> from sage.all import * >>> R = RealIntervalField(Integer(123)) >>> c, S = R.construction(); S Rational Field >>> R == c(S) True
- euler_constant()[source]¶
Return Euler’s gamma constant to the precision of this field.
EXAMPLES:
sage: RealIntervalField(100).euler_constant() 0.577215664901532860606512090083?
>>> from sage.all import * >>> RealIntervalField(Integer(100)).euler_constant() 0.577215664901532860606512090083?
- gen(i=0)[source]¶
Return the
i
-th generator ofself
.EXAMPLES:
sage: RIF.gen(0) 1 sage: RIF.gen(1) Traceback (most recent call last): ... IndexError: self has only one generator
>>> from sage.all import * >>> RIF.gen(Integer(0)) 1 >>> RIF.gen(Integer(1)) Traceback (most recent call last): ... IndexError: self has only one generator
- gens()[source]¶
Return a list of generators.
EXAMPLES:
sage: RIF.gens() [1]
>>> from sage.all import * >>> RIF.gens() [1]
- is_exact()[source]¶
Return whether or not this field is exact, which is always
False
.EXAMPLES:
sage: RIF.is_exact() False
>>> from sage.all import * >>> RIF.is_exact() False
- log2()[source]¶
Return \(\log(2)\) to the precision of this field.
EXAMPLES:
sage: R=RealIntervalField(100) sage: R.log2() 0.693147180559945309417232121458? sage: R(2).log() 0.693147180559945309417232121458?
>>> from sage.all import * >>> R=RealIntervalField(Integer(100)) >>> R.log2() 0.693147180559945309417232121458? >>> R(Integer(2)).log() 0.693147180559945309417232121458?
- lower_field()[source]¶
Return the
RealField_class
with rounding mode'RNDD'
(rounding towards minus infinity).EXAMPLES:
sage: RIF.lower_field() Real Field with 53 bits of precision and rounding RNDD sage: RealIntervalField(200).lower_field() Real Field with 200 bits of precision and rounding RNDD
>>> from sage.all import * >>> RIF.lower_field() Real Field with 53 bits of precision and rounding RNDD >>> RealIntervalField(Integer(200)).lower_field() Real Field with 200 bits of precision and rounding RNDD
- middle_field()[source]¶
Return the
RealField_class
with rounding mode'RNDN'
(rounding towards nearest).EXAMPLES:
sage: RIF.middle_field() Real Field with 53 bits of precision sage: RealIntervalField(200).middle_field() Real Field with 200 bits of precision
>>> from sage.all import * >>> RIF.middle_field() Real Field with 53 bits of precision >>> RealIntervalField(Integer(200)).middle_field() Real Field with 200 bits of precision
- name()[source]¶
Return the name of
self
.EXAMPLES:
sage: RIF.name() 'IntervalRealIntervalField53' sage: RealIntervalField(200).name() 'IntervalRealIntervalField200'
>>> from sage.all import * >>> RIF.name() 'IntervalRealIntervalField53' >>> RealIntervalField(Integer(200)).name() 'IntervalRealIntervalField200'
- ngens()[source]¶
Return the number of generators of
self
, which is 1.EXAMPLES:
sage: RIF.ngens() 1
>>> from sage.all import * >>> RIF.ngens() 1
- pi()[source]¶
Return \(\pi\) to the precision of this field.
EXAMPLES:
sage: R = RealIntervalField(100) sage: R.pi() 3.14159265358979323846264338328? sage: R.pi().sqrt()/2 0.88622692545275801364908374167? sage: R = RealIntervalField(150) sage: R.pi().sqrt()/2 0.886226925452758013649083741670572591398774728?
>>> from sage.all import * >>> R = RealIntervalField(Integer(100)) >>> R.pi() 3.14159265358979323846264338328? >>> R.pi().sqrt()/Integer(2) 0.88622692545275801364908374167? >>> R = RealIntervalField(Integer(150)) >>> R.pi().sqrt()/Integer(2) 0.886226925452758013649083741670572591398774728?
- prec()[source]¶
Return the precision of this field (in bits).
EXAMPLES:
sage: RIF.precision() 53 sage: RealIntervalField(200).precision() 200
>>> from sage.all import * >>> RIF.precision() 53 >>> RealIntervalField(Integer(200)).precision() 200
- precision()[source]¶
Return the precision of this field (in bits).
EXAMPLES:
sage: RIF.precision() 53 sage: RealIntervalField(200).precision() 200
>>> from sage.all import * >>> RIF.precision() 53 >>> RealIntervalField(Integer(200)).precision() 200
- random_element(*args, **kwds)[source]¶
Return a random element of
self
. Any arguments or keywords are passed onto the random element function in real field.By default, this is uniformly distributed in \([-1, 1]\).
EXAMPLES:
sage: RIF.random_element().parent() is RIF True sage: -100 <= RIF.random_element(-100, 100) <= 100 True
>>> from sage.all import * >>> RIF.random_element().parent() is RIF True >>> -Integer(100) <= RIF.random_element(-Integer(100), Integer(100)) <= Integer(100) True
Passes extra positional or keyword arguments through:
sage: 0 <= RIF.random_element(min=0, max=100) <= 100 True sage: -100 <= RIF.random_element(min=-100, max=0) <= 0 True
>>> from sage.all import * >>> Integer(0) <= RIF.random_element(min=Integer(0), max=Integer(100)) <= Integer(100) True >>> -Integer(100) <= RIF.random_element(min=-Integer(100), max=Integer(0)) <= Integer(0) True
- scientific_notation(status=None)[source]¶
Set or return the scientific notation printing flag.
If this flag is
True
then real numbers with this space as parent print using scientific notation.INPUT:
status
– boolean optional flag
EXAMPLES:
sage: RIF(0.025) 0.025000000000000002? sage: RIF.scientific_notation(True) sage: RIF(0.025) 2.5000000000000002?e-2 sage: RIF.scientific_notation(False) sage: RIF(0.025) 0.025000000000000002?
>>> from sage.all import * >>> RIF(RealNumber('0.025')) 0.025000000000000002? >>> RIF.scientific_notation(True) >>> RIF(RealNumber('0.025')) 2.5000000000000002?e-2 >>> RIF.scientific_notation(False) >>> RIF(RealNumber('0.025')) 0.025000000000000002?
- to_prec(prec)[source]¶
Return a real interval field to the given precision.
EXAMPLES:
sage: RIF.to_prec(200) Real Interval Field with 200 bits of precision sage: RIF.to_prec(20) Real Interval Field with 20 bits of precision sage: RIF.to_prec(53) is RIF True
>>> from sage.all import * >>> RIF.to_prec(Integer(200)) Real Interval Field with 200 bits of precision >>> RIF.to_prec(Integer(20)) Real Interval Field with 20 bits of precision >>> RIF.to_prec(Integer(53)) is RIF True
- upper_field()[source]¶
Return the
RealField_class
with rounding mode'RNDU'
(rounding towards plus infinity).EXAMPLES:
sage: RIF.upper_field() Real Field with 53 bits of precision and rounding RNDU sage: RealIntervalField(200).upper_field() Real Field with 200 bits of precision and rounding RNDU
>>> from sage.all import * >>> RIF.upper_field() Real Field with 53 bits of precision and rounding RNDU >>> RealIntervalField(Integer(200)).upper_field() Real Field with 200 bits of precision and rounding RNDU
- zeta(n=2)[source]¶
Return an \(n\)-th root of unity in the real field, if one exists, or raise a
ValueError
otherwise.EXAMPLES:
sage: R = RealIntervalField() sage: R.zeta() -1 sage: R.zeta(1) 1 sage: R.zeta(5) Traceback (most recent call last): ... ValueError: No 5th root of unity in self
>>> from sage.all import * >>> R = RealIntervalField() >>> R.zeta() -1 >>> R.zeta(Integer(1)) 1 >>> R.zeta(Integer(5)) Traceback (most recent call last): ... ValueError: No 5th root of unity in self
- sage.rings.real_mpfi.is_RealIntervalField(x)[source]¶
Check if
x
is aRealIntervalField_class
.EXAMPLES:
sage: sage.rings.real_mpfi.is_RealIntervalField(RIF) doctest:warning... DeprecationWarning: The function is_RealIntervalField is deprecated; use 'isinstance(..., RealIntervalField_class)' instead. See https://github.com/sagemath/sage/issues/38128 for details. True sage: sage.rings.real_mpfi.is_RealIntervalField(RealIntervalField(200)) True
>>> from sage.all import * >>> sage.rings.real_mpfi.is_RealIntervalField(RIF) doctest:warning... DeprecationWarning: The function is_RealIntervalField is deprecated; use 'isinstance(..., RealIntervalField_class)' instead. See https://github.com/sagemath/sage/issues/38128 for details. True >>> sage.rings.real_mpfi.is_RealIntervalField(RealIntervalField(Integer(200))) True
- sage.rings.real_mpfi.is_RealIntervalFieldElement(x)[source]¶
Check if
x
is aRealIntervalFieldElement
.EXAMPLES:
sage: sage.rings.real_mpfi.is_RealIntervalFieldElement(RIF(2.2)) doctest:warning... DeprecationWarning: The function is_RealIntervalFieldElement is deprecated; use 'isinstance(..., RealIntervalFieldElement)' instead. See https://github.com/sagemath/sage/issues/38128 for details. True sage: sage.rings.real_mpfi.is_RealIntervalFieldElement(RealIntervalField(200)(2.2)) True
>>> from sage.all import * >>> sage.rings.real_mpfi.is_RealIntervalFieldElement(RIF(RealNumber('2.2'))) doctest:warning... DeprecationWarning: The function is_RealIntervalFieldElement is deprecated; use 'isinstance(..., RealIntervalFieldElement)' instead. See https://github.com/sagemath/sage/issues/38128 for details. True >>> sage.rings.real_mpfi.is_RealIntervalFieldElement(RealIntervalField(Integer(200))(RealNumber('2.2'))) True