26.6 Field of Arbitrary Precision Real Intervals

Module: sage.rings.real_mpfi

Field of Arbitrary Precision Real Intervals

Author Log:

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 <= b) and printed as [a .. b] (for instance, [3.1415 .. 3.1416]). These floating-point numbers implemented using MPFR (the same as the RealNumber elements of RealField).

The interval represents the set x : a <= x <= 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:

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 I have chosen the latter for now.

sage: R = RealIntervalField(3)
sage: a = R(1.25)
sage: a
[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: * Expected transitivity properties hold (if a > b and b == c, then a > c; etc.) * if a>b, then cmp(a, b) == 1; if a==b, then cmp(a,b) == 0; if a<b, then cmp(a, b) == -1 * 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 * It is not the case that if cmp(a, b) == 0 then a==b, or that if cmp(a, b) == 1 then a>b, or that if cmp(a, b) == -1 then a<b * There are values a,b such that a<=b but neither a<b nor a==b hold

Note that intervals a and b overlap iff not(a != b).

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

Module-level Functions

RealInterval( )

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
- an integer >= 0.
min_prec
- number will have at least this many bits of precision, no matter what.

sage: RealInterval('2.3')
[2.2999999999999998 .. 2.3000000000000003]
sage: RealInterval(10)
[10.000000000000000 .. 10.000000000000000]
sage: RealInterval('1.0000000000000000000000000000000000')
[1.000000000000000000000000000000000000 ..
1.000000000000000000000000000000000000]
sage: RealInterval(29308290382930840239842390482, 3^20)
[3.48678440100000000000000000000e9 .. 2.93082903829308402398423904820e28]

is_RealIntervalField( )

is_RealIntervalFieldElement( )

Class: RealIntervalField

class RealIntervalField
RealIntervalField(prec, sci_not, rnd):

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

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

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.0000000000000000 .. 3.0000000000000000]
sage: RIF(RIF(3))
[3.0000000000000000 .. 3.0000000000000000]
sage: RIF(pi)
[3.1415926535897931 .. 3.1415926535897936]
sage: RIF(RealField(53)('1.5'))
[1.5000000000000000 .. 1.5000000000000000]
sage: RIF(-2/19)
[-0.10526315789473686 .. -0.10526315789473683]
sage: RIF(-3939)
[-3939.0000000000000 .. -3939.0000000000000]
sage: RIF(-3939r)
[-3939.0000000000000 .. -3939.0000000000000]
sage: RIF('1.5')
[1.5000000000000000 .. 1.5000000000000000]
sage: RIF(RQDF.pi())
[3.1415926535897926 .. 3.1415926535897941]
sage: qdpi = RQDF(RealField(500).pi())
sage: cmp(RealIntervalField(212)(qdpi), RealIntervalField(212).pi())
0

The base must be explicitly specified as a named parameter:

sage: RIF('101101', base=2)
[45.000000000000000 .. 45.000000000000000]
sage: RIF('+infinity')
[+infinity .. +infinity]
sage: RIF('[1..3]')
[1.0000000000000000 .. 3.0000000000000000]

Next we coerce some 2-tuples, which define intervals.

sage: RIF((-1.5, -1.3))
[-1.5000000000000000 .. -1.3000000000000000]
sage: RIF((RDF('-1.5'), RDF('-1.3')))
[-1.5000000000000000 .. -1.3000000000000000]
sage: RIF((1/3,2/3))
[0.33333333333333331 .. 0.66666666666666675]

The extra paranthesis aren't needed.

sage: RIF(1/3,2/3)
[0.33333333333333331 .. 0.66666666666666675]
sage: RIF((1,2))
[1.0000000000000000 .. 2.0000000000000000]
sage: RIF((1r,2r))
[1.0000000000000000 .. 2.0000000000000000]
sage: RIF((pi, e))
[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.1415926535897931 .. 3.1415926535897936], False)
sage: check(RIF(RR(pi)))
([3.1415926535897931 .. 3.1415926535897932], True)
sage: check(RIF(1.5))
([1.5000000000000000 .. 1.5000000000000000], True)
sage: check(RIF('1.5'))
([1.5000000000000000 .. 1.5000000000000000], True)
sage: check(RIF(0.1))
([0.10000000000000000 .. 0.10000000000000001], True)
sage: check(RIF(1/10))
([0.099999999999999991 .. 0.10000000000000001], False)
sage: check(RIF('0.1'))
([0.099999999999999991 .. 0.10000000000000001], 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
[0.099999999999999991 .. 0.70000000000000007]
sage: nearest = RIF(RR(1/10), RR(7/10)); nearest
[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.0000000000000000000000000000000 .. 3.0000000000000000000000000000000]
sage: R(R(3))
[3.0000000000000000000000000000000 .. 3.0000000000000000000000000000000]
sage: R(pi)
[3.1415926535897932384626433832793 .. 3.1415926535897932384626433832825]
sage: R(-2/19)
[-0.10526315789473684210526315789481 ..
-0.10526315789473684210526315789470]
sage: R(e,pi)
[2.7182818284590452353602874713512 .. 3.1415926535897932384626433832825]

Functions: characteristic,$ \,$ complex_field,$ \,$ construction,$ \,$ euler_constant,$ \,$ gen,$ \,$ gens,$ \,$ is_atomic_repr,$ \,$ is_exact,$ \,$ is_finite,$ \,$ log2,$ \,$ name,$ \,$ ngens,$ \,$ pi,$ \,$ prec,$ \,$ precision,$ \,$ scientific_notation,$ \,$ zeta

characteristic( )

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

sage: RealIntervalField(10).characteristic()
0

complex_field( )

Return complex field of the same precision.

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 $ \infinity$ , and the note that this is an interval field.

Also preserves other information that makes this field unique (e.g. precision, print mode).

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.

sage: RealIntervalField(100).euler_constant()
[0.57721566490153286060651209008233 .. 0.57721566490153286060651209008313]

is_atomic_repr( )

Returns True, to signify that elements of this field print without sums, so parenthesis aren't required, e.g., in coefficients of polynomials.

sage: RealIntervalField(10).is_atomic_repr()
True

is_finite( )

Returns False, since the field of real numbers is not finite.

sage: RealIntervalField(10).is_finite()
False

log2( )

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

sage: R=RealIntervalField(100)
sage: R.log2() 
[0.69314718055994530941723212145798 .. 0.69314718055994530941723212145878]
sage: R(2).log()
[0.69314718055994530941723212145798 .. 0.69314718055994530941723212145878]

pi( )

Returns pi to the precision of this field.

sage: R = RealIntervalField(100)
sage: R.pi()
[3.1415926535897932384626433832793 .. 3.1415926535897932384626433832825]
sage: R.pi().sqrt()/2
[0.88622692545275801364908374166983 .. 0.88622692545275801364908374167142]
sage: R = RealIntervalField(150)
sage: R.pi().sqrt()/2
[0.88622692545275801364908374167057259139877472759 ..
0.88622692545275801364908374167057259139877472830]

scientific_notation( )

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
- (bool -) optional flag

zeta( )

Return an $ n$ -th root of unity in the real field, if one exists, or raise a ValueError otherwise.

sage: R = RealIntervalField()
sage: R.zeta()
[-1.0000000000000000 .. -1.0000000000000000]
sage: R.zeta(1)
[1.0000000000000000 .. 1.0000000000000000]
sage: R.zeta(5)
Traceback (most recent call last):
...
ValueError: No 5th root of unity in self

Special Functions: __call__,$ \,$ __cmp__,$ \,$ __init__,$ \,$ __reduce__,$ \,$ _is_valid_homomorphism_,$ \,$ _latex_,$ \,$ _lower_field,$ \,$ _middle_field,$ \,$ _real_field,$ \,$ _repr_,$ \,$ _upper_field

__call__( )

Create an element in this real interval field.

Input:

x
- a number, string, or 2-tuple.
y
- (default: None); if given x is set to (x,y); this is so you can write R(2,3) to make the interval from 2 to 3.
base
- integer (default: 10) - only used if x is a string.

Output: an element of this real interval field.

sage: R = RealIntervalField(20)
sage: R('1.234')
[1.2339992 .. 1.2340012]
sage: R('2', base=2)
Traceback (most recent call last):
...
TypeError: Unable to convert number to real interval.
sage: a = R('1.1001', base=2); a
[1.5625000 .. 1.5625000]
sage: a.str(2)
'[1.1001000000000000000 .. 1.1001000000000000000]'

Type: RealIntervalField? for more information.

__reduce__( )

sage: R = RealIntervalField(sci_not=1, prec=200)
sage: loads(dumps(R)) == R
True

_is_valid_homomorphism_( )

_latex_( )

_lower_field( )

_middle_field( )

_real_field( )

_repr_( )

_upper_field( )

Class: RealIntervalFieldElement

class RealIntervalFieldElement
A real number interval.

Functions: absolute_diameter,$ \,$ alea,$ \,$ algdep,$ \,$ arccos,$ \,$ arccosh,$ \,$ arcsin,$ \,$ arcsinh,$ \,$ arctan,$ \,$ arctanh,$ \,$ ceil,$ \,$ ceiling,$ \,$ center,$ \,$ contains_zero,$ \,$ cos,$ \,$ cosh,$ \,$ diameter,$ \,$ exp,$ \,$ exp2,$ \,$ floor,$ \,$ fp_rank_diameter,$ \,$ intersection,$ \,$ is_exact,$ \,$ is_int,$ \,$ is_NaN,$ \,$ log,$ \,$ log10,$ \,$ log2,$ \,$ lower,$ \,$ magnitude,$ \,$ mignitude,$ \,$ multiplicative_order,$ \,$ overlaps,$ \,$ parent,$ \,$ prec,$ \,$ real,$ \,$ relative_diameter,$ \,$ simplest_rational,$ \,$ sin,$ \,$ sinh,$ \,$ sqrt,$ \,$ square,$ \,$ square_root,$ \,$ str,$ \,$ tan,$ \,$ tanh,$ \,$ union,$ \,$ upper

absolute_diameter( )

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

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

alea( )

RIF(a, b).alea() gives a floating-point number picked at random from the interval.

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

algdep( )

Returns a polynomial of degree at most $ n$ which is approximately satisfied by this number. Note that the returned polynomial need not be irreducible, and indeed usually won't be if this number 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.

sage: r = sqrt(RIF(2)); r
[1.4142135623730949 .. 1.4142135623730952]
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.4142135623740045435034616524476131176321718753769480731766796 ..
1.4142135623740045435034616524476131176321718753769480731766809]
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( )

Returns the inverse cosine of this number

sage: q = RIF.pi()/3; q
[1.0471975511965976 .. 1.0471975511965979]
sage: i = q.cos(); i
[0.49999999999999988 .. 0.50000000000000012]
sage: q2 = i.arccos(); q2
[1.0471975511965974 .. 1.0471975511965981]
sage: q == q2
False
sage: q != q2
False
sage: q2.lower() == q.lower()
False
sage: q - q2
[-4.4408920985006262e-16 .. 4.4408920985006262e-16]
sage: q in q2
True

arccosh( )

Returns the hyperbolic inverse cosine of this number

sage: q = RIF.pi()/2 
sage: i = q.arccosh() ; i
[1.0232274785475503 .. 1.0232274785475509]

arcsin( )

Returns the inverse sine of this number

sage: q = RIF.pi()/5; q
[0.62831853071795862 .. 0.62831853071795874]
sage: i = q.sin(); i
[0.58778525229247302 .. 0.58778525229247325]
sage: q2 = i.arcsin(); q2
[0.62831853071795851 .. 0.62831853071795885]
sage: q == q2
False
sage: q != q2
False
sage: q2.lower() == q.lower()
False
sage: q - q2
[-2.2204460492503131e-16 .. 2.2204460492503131e-16]
sage: q in q2
True

arcsinh( )

Returns the hyperbolic inverse sine of this number

sage: q = RIF.pi()/7 
sage: i = q.sinh() ; i
[0.46401763049299088 .. 0.46401763049299106]
sage: i.arcsinh() - q
[-1.6653345369377349e-16 .. 1.6653345369377349e-16]

arctan( )

Returns the inverse tangent of this number

sage: q = RIF.pi()/5; q
[0.62831853071795862 .. 0.62831853071795874]
sage: i = q.tan(); i
[0.72654252800536078 .. 0.72654252800536113]
sage: q2 = i.arctan(); q2
[0.62831853071795851 .. 0.62831853071795885]
sage: q == q2
False
sage: q != q2
False
sage: q2.lower() == q.lower()
False
sage: q - q2
[-2.2204460492503131e-16 .. 2.2204460492503131e-16]
sage: q in q2
True

arctanh( )

Returns the hyperbolic inverse tangent of this number

sage: q = RIF.pi()/7 
sage: i = q.tanh() ; i
[0.42091124104853488 .. 0.42091124104853501]
sage: i.arctanh() - q
[-1.6653345369377349e-16 .. 1.6653345369377349e-16]

ceil( )

Returns the ceiling of this number

Output: integer

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
[-11.300000012 .. -9.5000000000]
sage: a.floor()
[-12.000000000 .. -10.000000000]
sage: a.ceil()
[-11.000000000 .. -9.0000000000]
sage: ceil(a)
[-11.000000000 .. -9.0000000000]

center( )

RIF(a, b).center() == (a+b)/2

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

contains_zero( )

Returns True if self is an interval containing zero.

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

Returns the cosine of this number.

sage: t=RIF(pi)/2
sage: t.cos()
[-1.6081226496766367e-16 .. 6.1232339957367661e-17]
sage: t.cos().cos()
[0.99999999999999988 .. 1.0000000000000000]

TESTS: This looped forever with an earlier version of MPFI, but now it works.

sage: RIF(-1, 1).cos()
[0.54030230586813965 .. 1.0000000000000000]

cosh( )

Returns the hyperbolic cosine of this number

sage: q = RIF.pi()/12
sage: q.cosh()
[1.0344656400955103 .. 1.0344656400955108]

diameter( )

If (0 in self), returns self.absolute_diameter(), otherwise self.relative_diameter().

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

exp( )

Returns $ e^\code{self}$

sage: r = RIF(0.0)
sage: r.exp()
[1.0000000000000000 .. 1.0000000000000000]

sage: r = RIF(32.3)
sage: a = r.exp(); a
[1.0658884727486446e14 .. 1.0658884727486449e14]
sage: a.log()
[32.299999999999990 .. 32.300000000000005]

sage: r = RIF(-32.3)
sage: r.exp()
[9.3818445884986834e-15 .. 9.3818445884986851e-15]

exp2( )

Returns $ 2^\code{self}$

sage: r = RIF(0.0)
sage: r.exp2()
[1.0000000000000000 .. 1.0000000000000000]

sage: r = RIF(32.0)
sage: r.exp2()
[4.2949672960000000e9 .. 4.2949672960000000e9]

sage: r = RIF(-32.3)
sage: r.exp2()
[1.8911724825302069e-10 .. 1.8911724825302073e-10]

floor( )

Returns the floor of this number

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
[9.5000000000000000000000000000000 .. 11.300000000000000710542735760101]
sage: floor(a)
[9.0000000000000000000000000000000 .. 11.000000000000000000000000000000]
sage: a.floor()
[9.0000000000000000000000000000000 .. 11.000000000000000000000000000000]
sage: ceil(a)
[10.000000000000000000000000000000 .. 12.000000000000000000000000000000]
sage: a.ceil()
[10.000000000000000000000000000000 .. 12.000000000000000000000000000000]

fp_rank_diameter( )

Computes the diameter of this interval in terms of the ``floating-point rank''. That is, it gives 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.

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.0662287925976398e6137 .. 2.0662287926061436e6137]
sage: a.fp_rank_diameter()
30524
sage: (RIF(sqrt(2)) - RIF(sqrt(2))).fp_rank_diameter()
9671406088542672151117826

Just because we have the best possible interval, doesn't mean the interval is actually small:

sage: a = RIF(pi)^1234567890; a
[2.0985787164673874e323228496 .. +infinity]
sage: a.fp_rank_diameter()
1

intersection( )

Return the intersection of two intervals. If the intervals do not overlap, raises a ValueError.

sage: RIF(1, 2).intersection(RIF(1.5, 3))
[1.5000000000000000 .. 2.0000000000000000]
sage: RIF(1, 2).intersection(RIF(4/3, 5/3))
[1.3333333333333332 .. 1.6666666666666668]
sage: RIF(1, 2).intersection(RIF(3, 4))
Traceback (most recent call last):
...
ValueError: intersection of non-overlapping intervals

is_int( )

Output:

bool
- True or False
n
- an integer

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)

log( )

sage: R = RealIntervalField()
sage: R(2).log()
[0.69314718055994528 .. 0.69314718055994540]

log10( )

Returns log to the base 10 of self

sage: r = RIF(16.0); r.log10()
[1.2041199826559245 ... 1.2041199826559248]
sage: r.log() / log(10.0)
[1.2041199826559245 ... 1.2041199826559251]

sage: r = RIF(39.9); r.log10()
[1.6009728956867481 .. 1.6009728956867484]

sage: r = RIF(0.0)
sage: r.log10()
[-infinity .. -infinity]

sage: r = RIF(-1.0)
sage: r.log10()
[.. NaN ..]

log2( )

Returns log to the base 2 of self

sage: r = RIF(16.0)
sage: r.log2()
[4.0000000000000000 .. 4.0000000000000000]

sage: r = RIF(31.9); r.log2()
[4.9954845188775065 .. 4.9954845188775075]

sage: r = RIF(0.0, 2.0)
sage: r.log2()
[-infinity .. 1.0000000000000000]

lower( )

Returns the lower bound of this interval

rnd - (string) the rounding mode RNDN - round to nearest RNDD - (default) round towards minus infinity RNDZ - round towards zero RNDU - round towards plus infinity

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.

sage: R = RealIntervalField(13)
sage: R.pi().lower().str(truncate=False)
'3.1411'

sage: x = R(1.2,1.3); x
[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

magnitude( )

The largest absolute value of the elements of the interval.

sage: RIF(-2, 1).magnitude()
2.00000000000000
sage: RIF(-1, 2).magnitude()
2.00000000000000

mignitude( )

The smallest absolute value of the elements of the interval.

sage: RIF(-2, 1).mignitude()
0.000000000000000
sage: RIF(-2, -1).mignitude()
1.00000000000000
sage: RIF(3, 4).mignitude()
3.00000000000000

overlaps( )

Returns 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).

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

parent( )

sage: R = RealIntervalField()
sage: a = R('1.2456')
sage: a.parent()
Real Interval Field with 53 bits of precision

real( )

Return the real part of self.

(Since self is a real number, this simply returns self.)

relative_diameter( )

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

       sage: RIF(1, pi).relative_diameter()
1.03418797197910

simplest_rational( )

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

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

Returns the sine of this number

sage: R = RealIntervalField(100)
sage: R(2).sin()
[0.90929742682568169539601986591150 .. 0.90929742682568169539601986591230]

sinh( )

Returns the hyperbolic sine of this number

sage: q = RIF.pi()/12
sage: q.sinh()
[0.26480022760227067 .. 0.26480022760227079]

sqrt( )

Return a square root of self. Raises an error if self is nonpositive.

If you use self.square_root() then an interval will always be returned (though it will be NaN if self is nonpositive).

sage: r = RIF(4.0)
sage: r.sqrt()
[2.0000000000000000 .. 2.0000000000000000]
sage: r.sqrt()^2 == r
True

sage: r = RIF(4344)
sage: r.sqrt()
[65.909028213136323 .. 65.909028213136339]
sage: r.sqrt()^2 == r
False
sage: r in r.sqrt()^2
True
sage: r.sqrt()^2 - r            
[-9.0949470177292824e-13 .. 1.8189894035458565e-12]

sage: r = RIF(-2.0)
sage: r.sqrt()
Traceback (most recent call last):
...
ValueError: self (=[-2.0000000000000000 .. -2.0000000000000000]) is not >=
0

sage: r = RIF(-2, 2)
sage: r.sqrt()
Traceback (most recent call last):
...
ValueError: self (=[-2.0000000000000000 .. 2.0000000000000000]) is not >= 0

square( )

Return the square of the interval. Note that squaring an interval is different than multiplying it by itself, because the square can never be negative.

sage: RIF(1, 2).square()
[1.0000000000000000 .. 4.0000000000000000]
sage: RIF(-1, 1).square()
[0.00000000000000000 .. 1.0000000000000000]
sage: RIF(-1, 1) * RIF(-1, 1)
[-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).

sage: r = RIF(-2.0)
sage: r.square_root()
[.. NaN ..]
sage: r.sqrt()
Traceback (most recent call last):
...
ValueError: self (=[-2.0000000000000000 .. -2.0000000000000000]) is not >=
0

str( )

Input:

base
- base for output
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

sage: a = RIF(59/27); a
[2.1851851851851851 .. 2.1851851851851856]
sage: a.str(16)
'[2.2f684bda12f68 .. 2.2f684bda12f6a]'
sage: a.str(no_sci=False)
'[2.1851851851851851e0 .. 2.1851851851851856e0]'

tan( )

Returns the tangent of this number

sage: q = RIF.pi()/3
sage: q.tan()
[1.7320508075688767 .. 1.7320508075688779]
sage: q = RIF.pi()/6
sage: q.tan()
[0.57735026918962562 .. 0.57735026918962585]

tanh( )

Returns the hyperbolic tangent of this number

sage: q = RIF.pi()/11
sage: q.tanh()
[0.27807942929585022 .. 0.27807942929585039]

union( )

Return the union of two intervals, or of an interval and a real number (more precisely, the convex hull).

sage: RIF(1, 2).union(RIF(pi, 22/7))
[1.0000000000000000 .. 3.1428571428571433]
sage: RIF(1, 2).union(pi)
[1.0000000000000000 .. 3.1415926535897936]
sage: RIF(1).union(RIF(0, 2))
[0.00000000000000000 .. 2.0000000000000000]
sage: RIF(1).union(RIF(-1))
[-1.0000000000000000 .. 1.0000000000000000]

upper( )

Returns the upper bound of this interval

rnd - (string) the rounding mode RNDN - round to nearest RNDD - round towards minus infinity RNDZ - round towards zero RNDU - (default) round towards plus infinity

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.

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
[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

Special Functions: __abs__,$ \,$ __cmp__,$ \,$ __contains__,$ \,$ __copy__,$ \,$ __eq__,$ \,$ __ge__,$ \,$ __gt__,$ \,$ __init__,$ \,$ __invert__,$ \,$ __le__,$ \,$ __lshift__,$ \,$ __lt__,$ \,$ __ne__,$ \,$ __pow__,$ \,$ __reduce__,$ \,$ __repr__,$ \,$ __rlshift__,$ \,$ __rpow__,$ \,$ __rrshift__,$ \,$ __rshift__,$ \,$ _im_gens_,$ \,$ _interface_init_,$ \,$ _latex_,$ \,$ _lshift_,$ \,$ _rshift_

__abs__( )

__copy__( )

Return copy of self - since self is immutable, we just return self again.

sage: a = RIF(3.5)
sage: copy(a) is  a
True

__invert__( )

Return the multiplicative "inverse" of this interval. (Technically, non-precise intervals don't have multiplicative inverses.)

sage: v = RIF(2); v
[2.0000000000000000 .. 2.0000000000000000]
sage: ~v
[0.50000000000000000 .. 0.50000000000000000]
sage: v * ~v
[1.0000000000000000 .. 1.0000000000000000]
sage: v = RIF(1.5, 2.5); v
[1.5000000000000000 .. 2.5000000000000000]
sage: ~v
[0.39999999999999996 .. 0.66666666666666675]
sage: v * ~v
[0.59999999999999986 .. 1.6666666666666670]
sage: ~RIF(-1, 1)
[-infinity .. +infinity]

__lshift__( )

Returns $ x * 2^y$ , for $ y$ an integer. Much faster than an ordinary multiplication.

sage: RIF(1.0) << 32
[4.2949672960000000e9 .. 4.2949672960000000e9]

__reduce__( )

Pickling support.

sage: a = RIF(5,5.5)
sage: cmp(loads(dumps(a)), a)
0        
sage: R = RealIntervalField(sci_not=1, prec=200)
sage: b = R('393.39203845902384098234098230948209384028340')
sage: cmp(loads(dumps(b)), b)
0
sage: b = R(1)/R(0); b
[+infinity .. +infinity]
sage: loads(dumps(b)) == b
True
sage: b = R(-1)/R(0); b
[-infinity .. -infinity]
sage: loads(dumps(b)) == b
True
sage: b = R('[2 .. 3]'); b
[2.0000000000000000000000000000000000000000000000000000000000000e0 ..
3.0000000000000000000000000000000000000000000000000000000000000e0]
sage: cmp(loads(dumps(b)), b)
0

__repr__( )

__rshift__( )

Returns $ x / 2^y$ , for $ y$ an integer. Much faster than an ordinary division.

sage: RIF(1024.0) >> 14
[0.062500000000000000 .. 0.062500000000000000]

_im_gens_( )

_interface_init_( )

Raise a TypeError.

This function would return the string representation of self that makes sense as a default representation of a real interval in other computer algebra systems. But, most other computer algebra systems do not support interval arithmetic, so instead we just raise a TypeError.

Define the appropriate _cas_init_ function if there is a computer algebra system you would like to support.

sage: n = RIF(1.3939494594)
sage: n._interface_init_()
Traceback (most recent call last):
...
TypeError

Here a conversion to Maxima happens implicitly, which results in a type error:

sage: a = RealInterval('2.3')
sage: erf(a)
Traceback (most recent call last):
...
TypeError

_latex_( )

_lshift_( )

_rshift_( )

See About this document... for information on suggesting changes.