| Hosted by CoCalc | Download
RealSet(pi,pi+1)
(pi + 1, pi)
RealSet._prep(pi, pi+1)
(4.141592653589794?, 3.141592653589794?)
RLF(pi+1) < RLF(pi)
True
bool(pi+1) < bool(pi)
False
RLF
Real Lazy Field
RLF?
File: /projects/sage/sage-6.9/src/sage/rings/real_lazy.pyx Signature : RLF(self, x=0, *args, **kwds) Docstring : This class represents the set of real numbers to unspecified precision. For the most part it simply wraps exact elements and defers evaluation until a specified precision is requested. Its primary use is to connect the exact rings (such as number fields) to fixed precision real numbers. For example, to specify an embedding of a number field K into RR one can map into this field and the coercion will then be able to carry the mapping to real fields of any precision. EXAMPLES: sage: a = RLF(1/3) sage: a 0.3333333333333334? sage: a + 1/5 0.5333333333333334? sage: a = RLF(1/3) sage: a 0.3333333333333334? sage: a + 5 5.333333333333334? sage: RealField(100)(a+5) 5.3333333333333333333333333333 TESTS: sage: TestSuite(RLF).run()
RIF(pi+1) < RIF(pi)
False
RealSet.open(pi,pi+1)
(pi + 1, pi)
v = [pi, pi+1] v.sort() v
[pi, pi + 1]
RealSet(3, 2)
(2, 3)
RealSet??
File: /projects/sage/sage-6.9/src/sage/misc/lazy_import.pyx Source: class RealSet(UniqueRepresentation, Parent): @staticmethod def __classcall__(cls, *args): """ Normalize the input. INPUT: See :class:`RealSet`. OUTPUT: A :class:`RealSet`. EXAMPLES:: sage: RealSet(RealSet.open_closed(0,1), RealSet.closed_open(2,3)) (0, 1] + [2, 3) """ if len(args) == 1 and isinstance(args[0], RealSet): return args[0] # common optimization intervals = [] if len(args) == 2: # allow RealSet(0,1) interval constructor try: lower, upper = args lower.n() upper.n() args = (RealSet._prep(lower, upper), ) except (AttributeError, ValueError, TypeError): pass for arg in args: if isinstance(arg, tuple): lower, upper = RealSet._prep(*arg) intervals.append(InternalRealInterval(lower, False, upper, False)) elif isinstance(arg, list): lower, upper = RealSet._prep(*arg) intervals.append(InternalRealInterval(lower, True, upper, True)) elif isinstance(arg, InternalRealInterval): intervals.append(arg) elif isinstance(arg, RealSet): intervals.extend(arg._intervals) else: raise ValueError(str(arg) + ' does not determine real interval') intervals = RealSet.normalize(intervals) return UniqueRepresentation.__classcall__(cls, intervals) def __init__(self, intervals): """ A subset of the real line INPUT: Arguments defining a real set. Possibilities are either two real numbers to construct an open set or a list/tuple/iterable of intervals. The individual intervals can be specified by either a :class:`RealInterval`, a tuple of two real numbers (constructing an open interval), or a list of two number (constructing a closed interval). EXAMPLES:: sage: RealSet(0,1) # open set from two numbers (0, 1) sage: i = RealSet(0,1)[0] sage: RealSet(i) # interval (0, 1) sage: RealSet(i, (3,4)) # tuple of two numbers = open set (0, 1) + (3, 4) sage: RealSet(i, [3,4]) # list of two numbers = closed set (0, 1) + [3, 4] """ self._intervals = intervals def __cmp__(self, other): """ Intervals are sorted by lower bound, then upper bound OUTPUT: `-1`, `0`, or `+1` depending on how the intervals compare. EXAMPLES:: sage: I1 = RealSet.open_closed(1, 3); I1 (1, 3] sage: I2 = RealSet.open_closed(0, 5); I2 (0, 5] sage: cmp(I1, I2) 1 sage: sorted([I1, I2]) [(0, 5], (1, 3]] sage: I1 == I1 True """ # note that the interval representation is normalized into a # unique form return cmp(self._intervals, other._intervals) def __iter__(self): """ Iterate over the component intervals is ascending order OUTPUT: An iterator over the intervals. EXAMPLES:: sage: s = RealSet(RealSet.open_closed(0,1), RealSet.closed_open(2,3)) sage: i = iter(s) sage: next(i) (0, 1] sage: next(i) [2, 3) """ return iter(self._intervals) def n_components(self): """ Return the number of connected components See also :meth:`get_interval` EXAMPLES:: sage: s = RealSet(RealSet.open_closed(0,1), RealSet.closed_open(2,3)) sage: s.n_components() 2 """ return len(self._intervals) def cardinality(self): """ Return the cardinality of the subset of the real line. OUTPUT: Integer or infinity. The size of a discrete set is the number of points; the size of a real interval is Infinity. EXAMPLES:: sage: RealSet([0, 0], [1, 1], [3, 3]).cardinality() 3 sage: RealSet(0,3).cardinality() +Infinity """ n = ZZ(0) for interval in self._intervals: if interval.is_point(): n += 1 else: return infinity return n def is_empty(self): """ Return whether the set is empty EXAMPLES:: sage: RealSet(0, 1).is_empty() False sage: RealSet(0, 0).is_empty() True """ return len(self._intervals) == 0 def get_interval(self, i): """ Return the ``i``-th connected component. Note that the intervals representing the real set are always normalized, see :meth:`normalize`. INPUT: - ``i`` -- integer. OUTPUT: The $i$-th connected component as a :class:`RealInterval`. EXAMPLES:: sage: s = RealSet(RealSet.open_closed(0,1), RealSet.closed_open(2,3)) sage: s.get_interval(0) (0, 1] sage: s[0] # shorthand (0, 1] sage: s.get_interval(1) [2, 3) sage: s[0] == s.get_interval(0) True """ return self._intervals[i] __getitem__ = get_interval @staticmethod def normalize(intervals): """ Bring a collection of intervals into canonical form INPUT: - ``intervals`` -- a list/tuple/iterable of intervals. OUTPUT: A tuple of intervals such that * they are sorted in ascending order (by lower bound) * there is a gap between each interval * all intervals are non-empty EXAMPLES:: sage: i1 = RealSet((0, 1))[0] sage: i2 = RealSet([1, 2])[0] sage: i3 = RealSet((2, 3))[0] sage: RealSet.normalize([i1, i2, i3]) ((0, 3),) sage: RealSet((0, 1), [1, 2], (2, 3)) (0, 3) sage: RealSet((0, 1), (1, 2), (2, 3)) (0, 1) + (1, 2) + (2, 3) sage: RealSet([0, 1], [2, 3]) [0, 1] + [2, 3] sage: RealSet((0, 2), (1, 3)) (0, 3) sage: RealSet(0,0) {} """ # sort by lower bound intervals = sorted(intervals) if len(intervals) == 0: return tuple() merged = [] curr = intervals.pop(0) while len(intervals) != 0: next = intervals.pop(0) cmp_ul = cmp(curr._upper, next._lower) if cmp_ul == +1 or ( cmp_ul == 0 and (curr._upper_closed or next._lower_closed)): curr = curr.convex_hull(next) else: if not curr.is_empty(): merged.append(curr) curr = next if not curr.is_empty(): merged.append(curr) return tuple(merged) def _repr_(self): """ Return a string representation OUTPUT: A string representation. EXAMPLES:: sage: RealSet(0, 1)._repr_() '(0, 1)' """ if self.n_components() == 0: return '{}' else: # Switch to u'\u222A' (cup sign) with Python 3 return ' + '.join(map(repr, self._intervals)) @staticmethod def _prep(lower, upper=None): """ Helper to prepare the lower and upper bound EXAMPLES:: sage: RealSet._prep(1, 0) (0, 1) sage: RealSet._prep(oo) +Infinity """ if lower == minus_infinity: lower = minus_infinity if lower == infinity: lower = infinity else: lower = RLF(lower) if upper is None: return lower if upper == minus_infinity: upper = minus_infinity if upper == infinity: upper = infinity else: upper = RLF(upper) if upper is infinity or lower is minus_infinity: return lower, upper elif lower is infinity or upper is minus_infinity: return upper, lower elif upper < lower: return upper, lower else: return lower, upper @staticmethod def open(lower, upper): """ Construct an open interval INPUT: - ``lower``, ``upper`` -- two real numbers or infinity. They will be sorted if necessary. OUTPUT: A new :class:`RealSet`. EXAMPLES:: sage: RealSet.open(1, 0) (0, 1) """ lower, upper = RealSet._prep(lower, upper) return RealSet(InternalRealInterval(lower, False, upper, False)) @staticmethod def closed(lower, upper): """ Construct a closed interval INPUT: - ``lower``, ``upper`` -- two real numbers or infinity. They will be sorted if necessary. OUTPUT: A new :class:`RealSet`. EXAMPLES:: sage: RealSet.closed(1, 0) [0, 1] """ lower, upper = RealSet._prep(lower, upper) return RealSet(InternalRealInterval(lower, True, upper, True)) @staticmethod def point(p): """ Construct an interval containing a single point INPUT: - ``p`` -- a real number. OUTPUT: A new :class:`RealSet`. EXAMPLES:: sage: RealSet.open(1, 0) (0, 1) """ p = RealSet._prep(p) return RealSet(InternalRealInterval(p, True, p, True)) @staticmethod def open_closed(lower, upper): """ Construct a half-open interval INPUT: - ``lower``, ``upper`` -- two real numbers or infinity. They will be sorted if necessary. OUTPUT: A new :class:`RealSet` that is open at the lower bound and closed at the upper bound. EXAMPLES:: sage: RealSet.open_closed(1, 0) (0, 1] """ lower, upper = RealSet._prep(lower, upper) return RealSet(InternalRealInterval(lower, False, upper, True)) @staticmethod def closed_open(lower, upper): """ Construct an half-open interval INPUT: - ``lower``, ``upper`` -- two real numbers or infinity. They will be sorted if necessary. OUTPUT: A new :class:`RealSet` that is closed at the lower bound and open an the upper bound. EXAMPLES:: sage: RealSet.closed_open(1, 0) [0, 1) """ lower, upper = RealSet._prep(lower, upper) return RealSet(InternalRealInterval(lower, True, upper, False)) @staticmethod def unbounded_below_closed(bound): """ Construct a semi-infinite interval INPUT: - ``bound`` -- a real number. OUTPUT: A new :class:`RealSet` from minus infinity to the bound (including). EXAMPLES:: sage: RealSet.unbounded_below_closed(1) (-oo, 1] """ bound = RealSet._prep(bound) return RealSet(InternalRealInterval(minus_infinity, False, bound, True)) @staticmethod def unbounded_below_open(bound): """ Construct a semi-infinite interval INPUT: - ``bound`` -- a real number. OUTPUT: A new :class:`RealSet` from minus infinity to the bound (excluding). EXAMPLES:: sage: RealSet.unbounded_below_open(1) (-oo, 1) """ bound = RealSet._prep(bound) return RealSet(InternalRealInterval(RLF(minus_infinity), False, RLF(bound), False)) @staticmethod def unbounded_above_closed(bound): """ Construct a semi-infinite interval INPUT: - ``bound`` -- a real number. OUTPUT: A new :class:`RealSet` from the bound (including) to plus infinity. EXAMPLES:: sage: RealSet.unbounded_above_closed(1) [1, +oo) """ bound = RealSet._prep(bound) return RealSet(InternalRealInterval(RLF(bound), True, RLF(infinity), False)) @staticmethod def unbounded_above_open(bound): """ Construct a semi-infinite interval INPUT: - ``bound`` -- a real number. OUTPUT: A new :class:`RealSet` from the bound (excluding) to plus infinity. EXAMPLES:: sage: RealSet.unbounded_above_open(1) (1, +oo) """ bound = RealSet._prep(bound) return RealSet(InternalRealInterval(RLF(bound), False, RLF(infinity), False)) def union(self, *other): """ Return the union of the two sets INPUT: - ``other`` -- a :class:`RealSet` or data that defines one. OUTPUT: The set-theoretic union as a new :class:`RealSet`. EXAMPLES:: sage: s1 = RealSet(0,2) sage: s2 = RealSet(1,3) sage: s1.union(s2) (0, 3) sage: s1.union(1,3) (0, 3) sage: s1 | s2 # syntactic sugar (0, 3) sage: s1 + s2 # syntactic sugar (0, 3) """ other = RealSet(*other) intervals = self._intervals + other._intervals return RealSet(*intervals) __or__ = union __add__ = union def intersection(self, *other): """ Return the intersection of the two sets INPUT: - ``other`` -- a :class:`RealSet` or data that defines one. OUTPUT: The set-theoretic intersection as a new :class:`RealSet`. EXAMPLES:: sage: s1 = RealSet(0,2) + RealSet.unbounded_above_closed(10); s1 (0, 2) + [10, +oo) sage: s2 = RealSet(1,3) + RealSet.unbounded_below_closed(-10); s2 (-oo, -10] + (1, 3) sage: s1.intersection(s2) (1, 2) sage: s1 & s2 # syntactic sugar (1, 2) sage: s1 = RealSet((0, 1), (2, 3)); s1 (0, 1) + (2, 3) sage: s2 = RealSet([0, 1], [2, 3]); s2 [0, 1] + [2, 3] sage: s3 = RealSet([1, 2]); s3 [1, 2] sage: s1.intersection(s2) (0, 1) + (2, 3) sage: s1.intersection(s3) {} sage: s2.intersection(s3) {1} + {2} """ other = RealSet(*other) # TODO: this can be done in linear time since the intervals are already sorted intervals = [] for i1 in self._intervals: for i2 in other._intervals: intervals.append(i1.intersection(i2)) return RealSet(*intervals) __and__ = intersection def inf(self): """ Return the infimum OUTPUT: A real number or infinity. EXAMPLES:: sage: s1 = RealSet(0,2) + RealSet.unbounded_above_closed(10); s1 (0, 2) + [10, +oo) sage: s1.inf() 0 sage: s2 = RealSet(1,3) + RealSet.unbounded_below_closed(-10); s2 (-oo, -10] + (1, 3) sage: s2.inf() -Infinity """ if self.n_components() == 0: return infinity return self._intervals[0].lower() def sup(self): """ Return the supremum OUTPUT: A real number or infinity. EXAMPLES:: sage: s1 = RealSet(0,2) + RealSet.unbounded_above_closed(10); s1 (0, 2) + [10, +oo) sage: s1.sup() +Infinity sage: s2 = RealSet(1,3) + RealSet.unbounded_below_closed(-10); s2 (-oo, -10] + (1, 3) sage: s2.sup() 3 """ if self.n_components() == 0: return minus_infinity return self._intervals[-1].upper() def complement(self): """ Return the complement OUTPUT: The set-theoretic complement as a new :class:`RealSet`. EXAMPLES:: sage: RealSet(0,1).complement() (-oo, 0] + [1, +oo) sage: s1 = RealSet(0,2) + RealSet.unbounded_above_closed(10); s1 (0, 2) + [10, +oo) sage: s1.complement() (-oo, 0] + [2, 10) sage: s2 = RealSet(1,3) + RealSet.unbounded_below_closed(-10); s2 (-oo, -10] + (1, 3) sage: s2.complement() (-10, 1] + [3, +oo) """ n = self.n_components() if n == 0: return RealSet(minus_infinity, infinity) intervals = [] if self.inf() != minus_infinity: first = self._intervals[0] intervals.append(InternalRealInterval(RLF(minus_infinity), False, first._lower, first.lower_open())) if self.sup() != infinity: last = self._intervals[-1] intervals.append(InternalRealInterval(last._upper, last.upper_open(), RLF(infinity), False)) for i in range(1,n): prev = self._intervals[i-1] next = self._intervals[i] i = InternalRealInterval(prev._upper, prev.upper_open(), next._lower, next.lower_open()) intervals.append(i) return RealSet(*intervals) def difference(self, *other): """ Return ``self`` with ``other`` subtracted INPUT: - ``other`` -- a :class:`RealSet` or data that defines one. OUTPUT: The set-theoretic difference of ``self`` with ``other`` removed as a new :class:`RealSet`. EXAMPLES:: sage: s1 = RealSet(0,2) + RealSet.unbounded_above_closed(10); s1 (0, 2) + [10, +oo) sage: s2 = RealSet(1,3) + RealSet.unbounded_below_closed(-10); s2 (-oo, -10] + (1, 3) sage: s1.difference(s2) (0, 1] + [10, +oo) sage: s1 - s2 # syntactic sugar (0, 1] + [10, +oo) sage: s2.difference(s1) (-oo, -10] + [2, 3) sage: s2 - s1 # syntactic sugar (-oo, -10] + [2, 3) sage: s1.difference(1,11) (0, 1] + [11, +oo) """ other = RealSet(*other) return self.intersection(other.complement()) __sub__ = difference def contains(self, x): """ Return whether `x` is contained in the set INPUT: - ``x`` -- a real number. OUTPUT: Boolean. EXAMPLES:: sage: s = RealSet(0,2) + RealSet.unbounded_above_closed(10); s (0, 2) + [10, +oo) sage: s.contains(1) True sage: s.contains(0) False sage: 10 in s # syntactic sugar True """ x = RLF(x) for interval in self._intervals: if interval.contains(x): return True return False __contains__ = contains def is_included_in(self, *other): r""" Tests interval inclusion INPUT: - ``*args`` -- a :class:`RealSet` or something that defines one. OUTPUT: Boolean. EXAMPLES:: sage: I = RealSet((1,2)) sage: J = RealSet((1,3)) sage: K = RealSet((2,3)) sage: I.is_included_in(J) True sage: J.is_included_in(K) False """ return RealSet(*other).intersection(self) == self def an_element(self): """ Return a point of the set OUTPUT: A real number. ``ValueError`` if the set is empty. EXAMPLES:: sage: RealSet.open_closed(0, 1).an_element() 1 sage: RealSet(0, 1).an_element() 1/2 """ if len(self._intervals) == 0: raise ValueError('set is empty') i = self._intervals[0] if i.lower_closed(): return i.lower() if i.upper_closed(): return i.upper() return (i.lower() + i.upper())/ZZ(2) def is_disjoint_from(self, *other): """ Test whether the two sets are disjoint INPUT: - ``other`` -- a :class:`RealSet` or data defining one. OUTPUT: Boolean. EXAMPLES:: sage: s1 = RealSet((0, 1), (2, 3)); s1 (0, 1) + (2, 3) sage: s2 = RealSet([1, 2]); s2 [1, 2] sage: s1.is_disjoint_from(s2) True sage: s1.is_disjoint_from([1, 2]) True """ other = RealSet(*other) return self.intersection(other).is_empty() @staticmethod def are_pairwise_disjoint(*real_set_collection): """ Test whether sets are pairwise disjoint INPUT: - ``*real_set_collection`` -- a list/tuple/iterable of :class:`RealSet`. OUTPUT: Boolean. EXAMPLES:: sage: s1 = RealSet((0, 1), (2, 3)) sage: s2 = RealSet((1, 2)) sage: s3 = RealSet.point(3) sage: RealSet.are_pairwise_disjoint(s1, s2, s3) True sage: RealSet.are_pairwise_disjoint(s1, s2, s3, [10,10]) True sage: RealSet.are_pairwise_disjoint(s1, s2, s3, [-1, 1/2]) False """ sets = [RealSet(_) for _ in real_set_collection] for i in range(len(sets)): for j in range(i): si = sets[i] sj = sets[j] if not si.is_disjoint_from(sj): return False return True