Arbitrary Precision Real Intervals

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): trac ticket #3893: random_element() should pass on *args and **kwds.

  • Travis Scrimshaw (2012-10-20): Fixing scientific notation output to fix trac ticket #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))
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 == 5/4
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.

This convention for comparison operators has good and bad points. The good:

  • Expected transitivity properties hold (if a > b and b == c, then a > c; etc.)

  • a == 0 is true if the interval contains only the floating-point number 0; similarly for a == 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 of a < b, a == b, or a > b are true

  • There are values a and b such that a <= b but neither a < b nor a == b hold.

  • There are values a and b such that neither a != b nor a == 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

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

Comparison by equality shows what the semi-infinite intervals actually coerce to:

sage: RIF(1,oo) == oo
True
sage: RIF(-oo,-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

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

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
sage.rings.real_mpfi.RealInterval(s, upper=None, base=10, pad=0, min_prec=53)

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:

  • s – a 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 case s is the lower endpoint

  • base – an integer between 2 and 36

  • pad – (default: 0) an integer

  • 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]'
sage.rings.real_mpfi.RealIntervalField(prec=53, sci_not=False)

Construct a RealIntervalField_class, with caching.

INPUT:

  • prec – (integer) precision; default = 53: The number of bits used to represent the mantissa of a floating-point number. The precision can be any integer between mpfr_prec_min() and mpfr_prec_max(). In the current implementation, mpfr_prec_min() is equal to 2.

  • sci_not – (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

See the documentation for RealIntervalField_class for many more examples.

class sage.rings.real_mpfi.RealIntervalFieldElement

Bases: sage.structure.element.RingElement

A real number interval.

absolute_diameter()

The diameter of this interval (for \([a .. b]\), this is \(b-a\)), rounded upward, as a RealNumber.

EXAMPLES:

sage: RIF(1, pi).absolute_diameter()
2.14159265358979
alea()

Return a floating-point number picked at random from the interval.

EXAMPLES:

sage: RIF(1, 2).alea() # random
1.34696133696137
algdep(n)

Returns 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

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
arccos()

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
arccosh()

Return the hyperbolic inverse cosine of self.

EXAMPLES:

sage: q = RIF.pi()/2
sage: i = q.arccosh() ; i
1.023227478547551?
arccoth()

Return the inverse hyperbolic cotangent of self.

EXAMPLES:

sage: RealIntervalField(100)(2).arccoth()
0.549306144334054845697622618462?
sage: (2.0).arccoth()
0.549306144334055
arccsch()

Return the inverse hyperbolic cosecant of self.

EXAMPLES:

sage: RealIntervalField(100)(2).arccsch()
0.481211825059603447497758913425?
sage: (2.0).arccsch()
0.481211825059603
arcsech()

Return the inverse hyperbolic secant of self.

EXAMPLES:

sage: RealIntervalField(100)(0.5).arcsech()
1.316957896924816708625046347308?
sage: (0.5).arcsech()
1.31695789692482
arcsin()

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
arcsinh()

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
arctan()

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
arctanh()

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
argument()

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
bisection()

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) == RIF(I.center())
True
sage: a.union(b).endpoints() == I.endpoints()
True
ceil()

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 a ValueError otherwise

  • floor() – truncation towards minus infinity

  • trunc() – truncation towards zero

  • round() – 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]'
ceiling()

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 a ValueError otherwise

  • floor() – truncation towards minus infinity

  • trunc() – truncation towards zero

  • round() – 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]'
center()

Compute the center of the interval \([a .. b]\) which is \((a+b) / 2\).

EXAMPLES:

sage: RIF(1, 2).center()
1.50000000000000
contains_zero()

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
cos()

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?
cosh()

Return the hyperbolic cosine of self.

EXAMPLES:

sage: q = RIF.pi()/12
sage: q.cosh()
1.034465640095511?
cot()

Return the cotangent of self.

EXAMPLES:

sage: RealIntervalField(100)(2).cot()
-0.457657554360285763750277410432?
coth()

Return the hyperbolic cotangent of self.

EXAMPLES:

sage: RealIntervalField(100)(2).coth()
1.03731472072754809587780976477?
csc()

Return the cosecant of self.

EXAMPLES:

sage: RealIntervalField(100)(2).csc()
1.099750170294616466756697397026?
csch()

Return the hyperbolic cosecant of self.

EXAMPLES:

sage: RealIntervalField(100)(2).csch()
0.275720564771783207758351482163?
diameter()

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
edges()

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()
(3.1415926535897932?, 3.1415926535897936?)
endpoints(rnd=None)

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()
(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
exp()

Returns \(e^\mathtt{self}\)

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
exp2()

Returns \(2^\mathtt{self}\)

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
factorial()

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?

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
floor()

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 a ValueError otherwise.

  • ceil() – truncation towards plus infinity

  • round() – rounding

  • trunc() – 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]'
fp_rank_diameter()

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
frac()

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 part

EXAMPLES:

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)
gamma()

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()
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

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]
imag()

Return the imaginary part of this real interval.

(Since this is interval is real, this simply returns the zero interval.)

See also

real()

EXAMPLES:

sage: RIF(2,3).imag()
0
intersection(other)

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
is_NaN()

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
is_exact()

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
is_int()

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)
lexico_cmp(left, right)

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
log(base='e')

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
log10()

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?
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
log2()

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]
lower(rnd=None)

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()
'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
magnitude()

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
max(*_others)

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)

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

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

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)

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 ..]

See also

min()

mignitude()

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
min(*_others)

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()
(-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)

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

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

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)

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 ..]

See also

max()

multiplicative_order()

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
overlaps(other)

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
prec()

Returns the precision of self.

EXAMPLES:

sage: RIF(2.1).precision()
53
sage: RealIntervalField(200)(2.1).precision()
200
precision()

Returns the precision of self.

EXAMPLES:

sage: RIF(2.1).precision()
53
sage: RealIntervalField(200)(2.1).precision()
200
psi()

Return the digamma function evaluated on self.

INPUT:

None.

OUTPUT:

A RealIntervalFieldElement.

EXAMPLES:

sage: psi_1 = RIF(1).psi()
sage: psi_1
-0.577215664901533?
sage: psi_1.overlaps(-RIF.euler_constant())
True
real()

Return the real part of this real interval.

(Since this interval is real, this simply returns itself.)

See also

imag()

EXAMPLES:

sage: RIF(1.2465).real() == RIF(1.2465)
True
relative_diameter()

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()
1.03418797197910
round()

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 a ValueError otherwise

  • floor() – 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

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

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
sec()

Return the secant of this number.

EXAMPLES:

sage: RealIntervalField(100)(2).sec()
-2.40299796172238098975460040142?
sech()

Return the hyperbolic secant of self.

EXAMPLES:

sage: RealIntervalField(100)(2).sech()
0.265802228834079692120862739820?
simplest_rational(low_open=False, high_open=False)

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 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
sin()

Return the sine of self.

EXAMPLES:

sage: R = RealIntervalField(100)
sage: R(2).sin()
0.909297426825681695396019865912?
sinh()

Return the hyperbolic sine of self.

EXAMPLES:

sage: q = RIF.pi()/12
sage: q.sinh()
0.2648002276022707?
sqrt()

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
square()

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]'
square_root()

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
str(base=10, style=None, no_sci=None, e=None, error_digits=None)

Return a string representation of self.

INPUT:

  • base – base for output

  • style – The printing style; either 'brackets' or 'question' (or None, to use the current default).

  • no_sci – if True do not print using scientific notation; if False print with scientific notation; if None (the default), print how the parent prints.

  • e – symbol used in scientific notation

  • error_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()
'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 \(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\); in 8.?, the precision is \(10^0\); and in 9.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 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
tan()

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?
tanh()

Return the hyperbolic tangent of self.

EXAMPLES:

sage: q = RIF.pi()/11
sage: q.tanh()
0.2780794292958503?
trunc()

Return the truncation of this interval as an interval

The truncation of \(x\) is the floor of \(x\) if \(x\) is non-negative 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 a ValueError otherwise

  • floor() – 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

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
union(other)

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]'
unique_ceil()

Returns the unique ceiling of this interval, if it is well defined, otherwise raises a ValueError.

OUTPUT:

  • an integer.

See also

ceil() – return the ceil as an interval (and never raise error)

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
unique_floor()

Returns the unique floor of this interval, if it is well defined, otherwise raises a ValueError.

OUTPUT:

  • an integer.

See also

floor() – return the floor as an interval (and never raise error)

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
unique_integer()

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
unique_round()

Returns the unique round (nearest integer) of this interval, if it is well defined, otherwise raises a ValueError.

OUTPUT:

  • an integer.

See also

round() – return the round as an interval (and never raise error)

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)
unique_sign()

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
unique_trunc()

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)
upper(rnd=None)

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()
'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
zeta(a=None)

Return the image of this interval by the Hurwitz zeta function.

For a = 1 (or a = 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?
class sage.rings.real_mpfi.RealIntervalField_class

Bases: sage.rings.ring.Field

Class of the real interval field.

INPUT:

  • prec – (integer) precision; default = 53 prec is the number of bits used to represent the mantissa of a floating-point number. The precision can be any integer between mpfr_prec_min() and mpfr_prec_max(). In the current implementation, mpfr_prec_min() is equal to 2.

  • sci_not – (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

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?
sage: RIF(10^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]'

All string-like types are accepted:

sage: 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]'

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]'

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]'
Element

alias of RealIntervalFieldElement

algebraic_closure()

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
characteristic()

Returns 0, since the field of real numbers has characteristic 0.

EXAMPLES:

sage: RealIntervalField(10).characteristic()
0
complex_field()

Return complex field of the same precision.

EXAMPLES:

sage: RIF.complex_field()
Complex Interval Field with 53 bits of precision
construction()

Returns 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
euler_constant()

Returns Euler’s gamma constant to the precision of this field.

EXAMPLES:

sage: RealIntervalField(100).euler_constant()
0.577215664901532860606512090083?
gen(i=0)

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
gens()

Return a list of generators.

EXAMPLES:

sage: RIF.gens()
[1]
is_exact()

Returns whether or not this field is exact, which is always False.

EXAMPLES:

sage: RIF.is_exact()
False
log2()

Returns \(\log(2)\) to the precision of this field.

EXAMPLES:

sage: R=RealIntervalField(100)
sage: R.log2()
0.693147180559945309417232121458?
sage: R(2).log()
0.693147180559945309417232121458?
lower_field()

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
middle_field()

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
name()

Return the name of self.

EXAMPLES:

sage: RIF.name()
'IntervalRealIntervalField53'
sage: RealIntervalField(200).name()
'IntervalRealIntervalField200'
ngens()

Return the number of generators of self, which is 1.

EXAMPLES:

sage: RIF.ngens()
1
pi()

Returns \(\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?
prec()

Return the precision of this field (in bits).

EXAMPLES:

sage: RIF.precision()
53
sage: RealIntervalField(200).precision()
200
precision()

Return the precision of this field (in bits).

EXAMPLES:

sage: RIF.precision()
53
sage: RealIntervalField(200).precision()
200
random_element(*args, **kwds)

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()
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?
scientific_notation(status=None)

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?
to_prec(prec)

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
upper_field()

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
zeta(n=2)

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
sage.rings.real_mpfi.is_RealIntervalField(x)

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
sage.rings.real_mpfi.is_RealIntervalFieldElement(x)

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