Arbitrary Precision Real Intervals
AUTHORS:
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
and
(where
) 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
.
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 (so if
,
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 .
sage: RIF(sqrt(2))
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
sage: RIF(123, 125)
124.?
sage: RIF(123, 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
0.?e1
sage: v.str(error_digits=1)
'1.?4'
sage: v.str(error_digits=5)
'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'
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 == 1.25
True
sage: a == 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. The cmp(a, b) function works differently; it compares two intervals lexicographically. (However, the behavior is not specified if given a non-interval and an interval.)
This convention for comparison operators has good and bad points. The good:
The bad:
Note
Intervals a and b overlap iff not(a != b).
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
sage: cmp(RIF(0), RIF(0, 1))
-1
sage: cmp(RIF(0, 1), RIF(0))
1
sage: cmp(RIF(0, 1), RIF(1))
-1
sage: cmp(RIF(0, 1), RIF(0, 1))
0
Return the real number defined by the string s as an element of RealIntervalField(prec=n), where n potentially has slightly more (controlled by pad) bits than given by s.
INPUT:
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]'
TESTS:
Make sure we’ve rounded up log(10,2) enough to guarantee sufficient precision (trac ticket #10164). This is a little tricky because at the time of writing, we don’t support intervals long enough to trip the error. However, at least we can make sure that we either do it correctly or fail noisily:
sage: ks = 5*10**5, 10**6
sage: for k in ks:
... try:
... z = RealInterval("1." + "1"*k)
... assert len(str(z))-4 >= k
... except TypeError:
... pass
Construct a RealIntervalField_class, with caching.
INPUT:
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
See the documentation for RealIntervalField_class for many more examples.
Bases: sage.structure.element.RingElement
A real number interval.
The diameter of this interval (for , this is
), rounded
upward, as a RealNumber.
EXAMPLES:
sage: RIF(1, pi).absolute_diameter()
2.14159265358979
Return a floating-point number picked at random from the interval.
EXAMPLES:
sage: RIF(1, 2).alea() # random
1.34696133696137
Returns a polynomial of degree at most 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 .
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
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
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
Even on this extremely imprecise interval we get an answer which is technically correct:
sage: RIF(-1, 1).algdep(5)
x
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
Return the hyperbolic inverse cosine of self.
EXAMPLES:
sage: q = RIF.pi()/2
sage: i = q.arccosh() ; i
1.023227478547551?
Return the inverse hyperbolic cotangent of self.
EXAMPLES:
sage: RealIntervalField(100)(2).arccoth()
0.549306144334054845697622618462?
sage: (2.0).arccoth()
0.549306144334055
Return the inverse hyperbolic cosecant of self.
EXAMPLES:
sage: RealIntervalField(100)(2).arccsch()
0.481211825059603447497758913425?
sage: (2.0).arccsch()
0.481211825059603
Return the inverse hyperbolic secant of self.
EXAMPLES:
sage: RealIntervalField(100)(0.5).arcsech()
1.316957896924816708625046347308?
sage: (0.5).arcsech()
1.31695789692482
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
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
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
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
Returns the bisection of self into two intervals of half the size whose union is self and intersection is center().
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: I = RIF(e, pi)
sage: a, b = I.bisection()
sage: a.intersection(b) == I.center()
True
sage: a.union(b).endpoints() == I.endpoints()
True
Return the ceiling of self.
OUTPUT:
integer
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]'
Return the ceiling of self.
OUTPUT:
integer
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]'
Compute the center of the interval which is
.
EXAMPLES:
sage: RIF(1, 2).center()
1.50000000000000
Return True if self 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
Return the cosine of self.
EXAMPLES:
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?
TESTS:
This looped forever with an earlier version of MPFI, but now it works:
sage: RIF(-1, 1).cos().str(style='brackets')
'[0.54030230586813965 .. 1.0000000000000000]'
Return the hyperbolic cosine of self.
EXAMPLES:
sage: q = RIF.pi()/12
sage: q.cosh()
1.034465640095511?
Return the cotangent of self.
EXAMPLES:
sage: RealIntervalField(100)(2).cot()
-0.457657554360285763750277410432?
Return the hyperbolic cotangent of self.
EXAMPLES:
sage: RealIntervalField(100)(2).coth()
1.03731472072754809587780976477?
Return the cosecant of self.
EXAMPLES:
sage: RealIntervalField(100)(2).csc()
1.099750170294616466756697397026?
Return the hyperbolic cosecant of self.
EXAMPLES:
sage: RealIntervalField(100)(2).csch()
0.275720564771783207758351482163?
If 0 is in self, then return absolute_diameter(), otherwise return relative_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: 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
Return the lower and upper endpoints of self.
EXAMPLES:
sage: RIF(1,2).endpoints()
(1.00000000000000, 2.00000000000000)
sage: RIF(pi).endpoints()
(3.14159265358979, 3.14159265358980)
sage: a = CIF(RIF(1,2), RIF(3,4))
sage: a.real().endpoints()
(1.00000000000000, 2.00000000000000)
As with lower() and upper(), a rounding mode is accepted:
sage: RIF(1,2).endpoints('RNDD')[0].parent()
Real Field with 53 bits of precision and rounding RNDD
Returns
EXAMPLES:
sage: r = RIF(0.0)
sage: r.exp()
1
sage: r = RIF(32.3)
sage: a = r.exp(); a
1.065888472748645?e14
sage: a.log()
32.30000000000000?
sage: r = RIF(-32.3)
sage: r.exp()
9.38184458849869?e-15
Returns
EXAMPLES:
sage: r = RIF(0.0)
sage: r.exp2()
1
sage: r = RIF(32.0)
sage: r.exp2()
4294967296
sage: r = RIF(-32.3)
sage: r.exp2()
1.891172482530207?e-10
Return the floor of self.
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]'
Computes 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; an fp_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(pi).fp_rank_diameter()
1
sage: RIF(12345).fp_rank_diameter()
0
sage: RIF(-sqrt(2)).fp_rank_diameter()
1
sage: RIF(5/8).fp_rank_diameter()
0
sage: RIF(5/7).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
Just because we have the best possible interval, doesn’t mean the interval is actually small:
sage: a = RIF(pi)^12345678901234567890; a
[2.0985787164673874e323228496 .. +infinity] # 32-bit
[5.8756537891115869e1388255822130839282 .. +infinity] # 64-bit
sage: a.fp_rank_diameter()
1
Return the gamma function evalutated 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()
True
sage: 0 in (RealField(2000)(-19/3).gamma() - RealIntervalField(1000)(-19/3).gamma())
True
sage: gamma(RIF(100))
9.33262154439442?e155
sage: gamma(RIF(-10000/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
Correctly detects poles:
sage: gamma(RIF(-3/2,-1/2))
[-infinity .. +infinity]
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
Check to see if self is Not-a-Number NaN.
EXAMPLES:
sage: a = RIF(0) / RIF(0.0,0.00); a
[.. NaN ..]
sage: a.is_NaN()
True
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()
False
Checks to see whether this interval includes exactly one integer.
OUTPUT:
If this contains exactly one integer, it returns the tuple (True, n), where n 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)
Return the logarithm of self to the given base.
EXAMPLES:
sage: R = RealIntervalField()
sage: r = R(2); r.log()
0.6931471805599453?
sage: r = R(-2); r.log()
0.6931471805599453? + 3.141592653589794?*I
Return log to the base 10 of self.
EXAMPLES:
sage: r = RIF(16.0); r.log10()
1.204119982655925?
sage: r.log() / log(10.0)
1.204119982655925?
sage: r = RIF(39.9); r.log10()
1.600972895686749?
sage: r = RIF(0.0)
sage: r.log10()
[-infinity .. -infinity]
sage: r = RIF(-1.0)
sage: r.log10()
1.364376353841841?*I
Return log to the base 2 of self.
EXAMPLES:
sage: r = RIF(16.0)
sage: r.log2()
4
sage: r = RIF(31.9); r.log2()
4.995484518877507?
sage: r = RIF(0.0, 2.0)
sage: r.log2()
[-infinity .. 1.0000000000000000]
Return the lower bound of this interval
INPUT:
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(truncate=False)
'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().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() == x.lower('RNDU')
True
The largest absolute value of the elements of the interval.
EXAMPLES:
sage: RIF(-2, 1).magnitude()
2.00000000000000
sage: RIF(-1, 2).magnitude()
2.00000000000000
Return an interval containing the maximum of self and other.
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)
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)
The smallest absolute value of the elements of the interval.
EXAMPLES:
sage: RIF(-2, 1).mignitude()
0.000000000000000
sage: RIF(-2, -1).mignitude()
1.00000000000000
sage: RIF(3, 4).mignitude()
3.00000000000000
Return an interval containing the minimum of self and other.
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()
(-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)
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)
Return such that self^n == 1.
Only have finite multiplicative order.
EXAMPLES:
sage: RIF(1).multiplicative_order()
1
sage: RIF(-1).multiplicative_order()
2
sage: RIF(3).multiplicative_order()
+Infinity
Return True if self and other are intervals with at least one value in common. For intervals a and b, we have a.overlaps(b) iff not(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
Return the parent of self.
EXAMPLES:
sage: R = RealIntervalField()
sage: a = R('1.2456')
sage: a.parent()
Real Interval Field with 53 bits of precision
Returns the precision of self.
EXAMPLES:
sage: RIF(2.1).precision()
53
sage: RealIntervalField(200)(2.1).precision()
200
Returns the precision of self.
EXAMPLES:
sage: RIF(2.1).precision()
53
sage: RealIntervalField(200)(2.1).precision()
200
Return the real part of self.
(Since self is a real number, this simply returns self.)
EXAMPLES:
sage: RIF(1.2465).real() == RIF(1.2465)
True
The relative diameter of this interval (for , this is
), rounded upward, as a RealNumber.
EXAMPLES:
sage: RIF(1, pi).relative_diameter()
1.03418797197910
Return the secant of this number.
EXAMPLES:
sage: RealIntervalField(100)(2).sec()
-2.40299796172238098975460040142?
Return the hyperbolic secant of self.
EXAMPLES:
sage: RealIntervalField(100)(2).sech()
0.265802228834079692120862739820?
Return the simplest rational in this interval. Given rationals
and
(both in lowest terms), the former is simpler if
or if
and
.
If optional parameters low_open or high_open are True, then treat this as an open interval on that end.
EXAMPLES:
sage: RealIntervalField(10)(pi).simplest_rational()
22/7
sage: RealIntervalField(20)(pi).simplest_rational()
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
Return the sine of self.
EXAMPLES:
sage: R = RealIntervalField(100)
sage: R(2).sin()
0.909297426825681695396019865912?
Return the hyperbolic sine of self.
EXAMPLES:
sage: q = RIF.pi()/12
sage: q.sinh()
0.2648002276022707?
Return a square root of self. Raises an error if self is nonpositive.
If you use square_root() then an interval will always be returned (though it will be NaN if self is nonpositive).
EXAMPLES:
sage: r = RIF(4.0)
sage: r.sqrt()
2
sage: r.sqrt()^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]'
sage: r = RIF(-2.0)
sage: 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
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.00000000000000000 .. 1.0000000000000000]'
sage: (RIF(-1, 1) * RIF(-1, 1)).str(style='brackets')
'[-1.0000000000000000 .. 1.0000000000000000]'
Return a square root of self. An interval will always be returned (though it will be NaN if self 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
Return a string representation of self.
INPUT:
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()
'3.141592653589794?'
sage: RIF(pi, 22/7).str()
'3.142?'
sage: RIF(pi, 22/7).str(style='question')
'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'
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'
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]'
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
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]'
We always use brackets style for NaN and infinities:
sage: RIF(pi, infinity)
[3.1415926535897931 .. +infinity]
sage: RIF(NaN)
[.. 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 , 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 ;
in 8.?, the precision is
; and in 9.35? the precision
is
. This precision will always be
for some
(or, for an arbitrary base
,
).
Then the interval is contained in the interval:
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, truncate=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 as 61.?62 or 62.?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)
[.. NaN ..]
sage: RIF(pi, infinity)
[3.1415926535897931 .. +infinity]
sage: RIF(-infinity, pi)
[-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)
1.?e-71
TESTS:
Check that trac ticket #13634 is fixed:
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?
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?
Return the hyperbolic tangent of self.
EXAMPLES:
sage: q = RIF.pi()/11
sage: q.tanh()
0.2780794292958503?
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.00000000000000000 .. 2.0000000000000000]'
sage: RIF(1).union(RIF(-1)).str(style='brackets')
'[-1.0000000000000000 .. 1.0000000000000000]'
Returns the unique ceiling of this interval, if it is well defined, otherwise raises a ValueError.
OUTPUT:
EXAMPLES:
sage: RIF(pi).unique_ceil()
4
sage: RIF(100*pi).unique_ceil()
315
sage: RIF(100, 200).unique_ceil()
Traceback (most recent call last):
...
ValueError: interval does not have a unique ceil
Returns the unique floor of this interval, if it is well defined, otherwise raises a ValueError.
OUTPUT:
EXAMPLES:
sage: RIF(pi).unique_floor()
3
sage: RIF(100*pi).unique_floor()
314
sage: RIF(100, 200).unique_floor()
Traceback (most recent call last):
...
ValueError: interval does not have a unique floor
Return the unique integer in this interval, if there is exactly one, otherwise raises a ValueError.
EXAMPLES:
sage: RIF(pi).unique_integer()
Traceback (most recent call last):
...
ValueError: interval contains no integer
sage: RIF(pi, pi+1).unique_integer()
4
sage: RIF(pi, pi+2).unique_integer()
Traceback (most recent call last):
...
ValueError: interval contains more than one integer
sage: RIF(100).unique_integer()
100
Returns the unique round (nearest integer) of this interval, if it is well defined, otherwise raises a ValueError.
OUTPUT:
- an integer.
EXAMPLES:
sage: RIF(pi).unique_round() 3 sage: RIF(1000*pi).unique_round() 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() -3 sage: (RIF(4.5).unique_round(), RIF(-4.5).unique_round()) (5, -5)
TESTS:
sage: RIF(-1/2, -1/3).unique_round()
Traceback (most recent call last):
...
ValueError: interval does not have a unique round (nearest integer)
sage: RIF(-1/2, 1/3).unique_round()
Traceback (most recent call last):
...
ValueError: interval does not have a unique round (nearest integer)
sage: RIF(-1/3, 1/3).unique_round()
0
sage: RIF(-1/2, 0).unique_round()
Traceback (most recent call last):
...
ValueError: interval does not have a unique round (nearest integer)
sage: RIF(1/2).unique_round()
1
sage: RIF(-1/2).unique_round()
-1
sage: RIF(0).unique_round()
0
Return the upper bound of self
INPUT:
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(truncate=False)
'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().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
Bases: sage.rings.ring.Field
Class of the real interval field.
INPUT:
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
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)
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?
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]'
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]'
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.7182818284590455 .. 3.1415926535897932]'
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))
(3.141592653589794?, False)
sage: check(RIF(RR(pi)))
(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)
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
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)
3.14159265358979323846264338328?
sage: R(-2/19)
-0.1052631578947368421052631578948?
sage: R(e,pi).str(style='brackets')
'[2.7182818284590452353602874713512 .. 3.1415926535897932384626433832825]'
TESTS:
sage: RIF._lower_field() is RealField(53, rnd='RNDD')
True
sage: RIF._upper_field() is RealField(53, rnd='RNDU')
True
sage: RIF._middle_field() is RR
True
sage: TestSuite(RIF).run()
Returns 0, since the field of real numbers has characteristic 0.
EXAMPLES:
sage: RealIntervalField(10).characteristic()
0
Return complex field of the same precision.
EXAMPLES:
sage: RIF.complex_field()
Complex Interval Field with 53 bits of precision
Returns the functorial construction of self, namely, completion of
the rational numbers with respect to the prime at ,
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
Returns Euler’s gamma constant to the precision of this field.
EXAMPLES:
sage: RealIntervalField(100).euler_constant()
0.577215664901532860606512090083?
Return the i-th generator of self.
EXAMPLES:
sage: RIF.gen(0)
1
sage: RIF.gen(1)
Traceback (most recent call last):
...
IndexError: self has only one generator
Return a list of generators.
EXAMPLE:
sage: RIF.gens()
[1]
Returns whether or not this field is exact, which is always False.
EXAMPLES:
sage: RIF.is_exact()
False
Return False, since the field of real numbers is not finite.
EXAMPLES:
sage: RealIntervalField(10).is_finite()
False
Returns to the precision of this field.
EXAMPLES:
sage: R=RealIntervalField(100)
sage: R.log2()
0.693147180559945309417232121458?
sage: R(2).log()
0.693147180559945309417232121458?
Return the name of self.
EXAMPLES:
sage: RIF.name()
'IntervalRealIntervalField53'
sage: RealIntervalField(200).name()
'IntervalRealIntervalField200'
Return the number of generators of self, which is 1.
EXAMPLES:
sage: RIF.ngens()
1
Returns 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?
Return the precision of this field (in bits).
EXAMPLES:
sage: RIF.precision()
53
sage: RealIntervalField(200).precision()
200
Return the precision of this field (in bits).
EXAMPLES:
sage: RIF.precision()
53
sage: RealIntervalField(200).precision()
200
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 .
EXAMPLES:
sage: RIF.random_element()
0.15363619378561300?
sage: RIF.random_element()
-0.50298737524751780?
sage: RIF.random_element(-100, 100)
60.958996432224126?
Passes extra positional or keyword arguments through:
sage: RIF.random_element(min=0, max=100)
2.5572702830891970?
sage: RIF.random_element(min=-100, max=0)
-1.5803457307118123?
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:
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?
Returns 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
Return an -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
Check if x is a RealIntervalField_class.
EXAMPLES:
sage: sage.rings.real_mpfi.is_RealIntervalField(RIF)
True
sage: sage.rings.real_mpfi.is_RealIntervalField(RealIntervalField(200))
True
Check if x is a RealIntervalFieldElement.
EXAMPLES:
sage: sage.rings.real_mpfi.is_RealIntervalFieldElement(RIF(2.2))
True
sage: sage.rings.real_mpfi.is_RealIntervalFieldElement(RealIntervalField(200)(2.2))
True