Skip to content

Commit

Permalink
Trac #21277: sage_input for infinities and RealSet
Browse files Browse the repository at this point in the history
{{{
sage: sage_input(RealSet((0, 1)))
ValueError: Can't convert (0, 1) to sage_input form
}}}

This ticket implements `sage_input` for `RealSet`s (and, as a
prerequisite for that, for infinities).

URL: https://trac.sagemath.org/21277
Reported by: mkoeppe
Ticket author(s): Matthias Koeppe
Reviewer(s): Volker Braun
  • Loading branch information
Release Manager authored and vbraun committed Feb 21, 2017
2 parents 4a07781 + 67f8fa1 commit ffa2644
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 1 deletion.
19 changes: 19 additions & 0 deletions src/sage/rings/infinity.py
Original file line number Diff line number Diff line change
Expand Up @@ -544,6 +544,25 @@ def lcm(self, x):
else:
return abs(self)

def _sage_input_(self, sib, coerced):
"""
Produce an expression which will reproduce this value when evaluated.
TESTS::
sage: sage_input(-oo)
-oo
sage: sage_input(oo)
oo
sage: sage_input(unsigned_infinity)
unsigned_infinity
"""
if self._sign == 0:
return sib.name('unsigned_infinity')
elif self._sign > 0:
return sib.name('oo')
else:
return -sib.name('oo')

class UnsignedInfinityRing_class(Singleton, Ring):

Expand Down
52 changes: 51 additions & 1 deletion src/sage/sets/real_set.py
Original file line number Diff line number Diff line change
Expand Up @@ -1453,4 +1453,54 @@ def are_pairwise_disjoint(*real_set_collection):
return False
return True


def _sage_input_(self, sib, coerced):
"""
Produce an expression which will reproduce this value when evaluated.
TESTS::
sage: sage_input(RealSet())
RealSet()
sage: sage_input(RealSet.open(-oo, +oo))
RealSet(-oo, oo)
sage: sage_input(RealSet.point(77))
RealSet.point(77)
sage: sage_input(RealSet.closed_open(0, +oo))
RealSet.closed_open(0, oo)
sage: sage_input(RealSet.open_closed(-oo, 0))
RealSet.open_closed(-oo, 0)
sage: sage_input(RealSet.open_closed(-1, 0))
RealSet.open_closed(-1, 0)
sage: sage_input(RealSet.closed_open(-1, 0))
RealSet.closed_open(-1, 0)
sage: sage_input(RealSet.closed(0, 1))
RealSet.closed(0, 1)
sage: sage_input(RealSet.open(0, 1))
RealSet.open(0, 1)
sage: sage_input(RealSet.open(0, 1) + RealSet.open(1, 2))
RealSet.open(0, 1) + RealSet.open(1, 2)
"""

def interval_input(i):
lower, upper = i.lower(), i.upper()
if i.is_point():
return sib.name('RealSet.point')(lower)
elif lower == minus_infinity and upper == infinity:
return sib.name('RealSet')(sib(minus_infinity), sib(infinity))
else:
if i.lower_closed():
if i.upper_closed():
t = 'RealSet.closed'
else:
t = 'RealSet.closed_open'
else:
if i.upper_closed():
t = 'RealSet.open_closed'
else:
t = 'RealSet.open'
return sib.name(t)(sib(lower), sib(upper))

if self.is_empty():
return sib.name('RealSet')()
else:
return sib.sum(interval_input(i) for i in self)

0 comments on commit ffa2644

Please sign in to comment.