Skip to content
This repository has been archived by the owner on Jan 30, 2023. It is now read-only.

Commit

Permalink
24171: Formal set membership function
Browse files Browse the repository at this point in the history
  • Loading branch information
rwst committed Nov 7, 2017
1 parent bc45dab commit 669ea22
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 0 deletions.
55 changes: 55 additions & 0 deletions src/sage/functions/other.py
Original file line number Diff line number Diff line change
Expand Up @@ -2677,3 +2677,58 @@ def _print_latex_(self, l, **kwargs):

cases = Function_cases()

class Function_elementof(BuiltinFunction):
"""
Formal set membership function that is only accessible internally.
This function is called to express a set membership statement,
usually as part of a solution set returned by ``solve()``.
See :class:`sage.sets.set.Set` and :class:`sage.sets.real_set.RealSet`
for possible set arguments.
EXAMPLES::
sage: from sage.functions.other import element_of
sage: element_of(x, SR(Set(ZZ)))
element_of(x, Set of elements of Integer Ring)
sage: element_of(sin(x), SR(Set(QQ)))
element_of(sin(x), Set of elements of Rational Field)
sage: element_of(x, SR(RealSet.open_closed(0,1)))
element_of(x, (0, 1])
sage: element_of(x, SR(Set([4,6,8]).union(Primes())))
element_of(x, Set-theoretic union of {8, 4, 6} and Set of all prime...
"""
def __init__(self):
"""
EXAMPLES::
sage: from sage.functions.other import element_of
sage: loads(dumps(element_of))
element_of
"""
BuiltinFunction.__init__(self, "element_of", nargs=2)

def _latex_(self):
r"""
EXAMPLES::
sage: from sage.functions.other import element_of
sage: latex(element_of)
\in
"""
return r'\in'

def _print_latex_(self, ex, s):
r"""
EXAMPLES::
sage: from sage.functions.other import element_of
sage: latex(element_of(x, SR(Set(ZZ))))
x \in \Bold{Z}
sage: latex(element_of(x, SR(Set([4,6,8]))))
x \in \left\{8, 4, 6\right\}
"""
return r"{} \in {}".format(latex(ex), latex(s))

element_of = Function_elementof()

4 changes: 4 additions & 0 deletions src/sage/symbolic/ring.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,8 @@ cdef class SymbolicRing(CommutativeRing):
from sage.rings.infinity import (infinity, minus_infinity,
unsigned_infinity)
from sage.structure.factorization import Factorization
from sage.sets.set import is_Set
from sage.sets.real_set import RealSet

if isinstance(x, RealNumber):
if x.is_NaN():
Expand All @@ -378,6 +380,8 @@ cdef class SymbolicRing(CommutativeRing):
elif isinstance(x, Factorization):
from sage.misc.all import prod
return prod([SR(p)**e for p,e in x], SR(x.unit()))
elif is_Set(x) or isinstance(x, RealSet):
exp = x
else:
raise TypeError(f"unable to convert {x!r} to a symbolic expression")

Expand Down

0 comments on commit 669ea22

Please sign in to comment.