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

Commit

Permalink
Sets.CartesianProducts.ParentMethods, FreeModule_ambient, IntegerRing…
Browse files Browse the repository at this point in the history
…_class, InternalRealInterval, RealSet, NonNegativeIntegers, IntegerRing_class, PositiveIntegers, RationalField: Add _sympy_ methods
  • Loading branch information
Matthias Koeppe committed Jun 9, 2021
1 parent 30ee8d6 commit 9fcf32e
Show file tree
Hide file tree
Showing 7 changed files with 141 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/sage/categories/sets_cat.py
Original file line number Diff line number Diff line change
Expand Up @@ -2438,6 +2438,21 @@ def _cartesian_product_of_elements(self, elements):
(42, 47, 42)
"""

def _sympy_(self):
"""
Return a SymPy ``ProductSet`` corresponding to ``self``.
EXAMPLES::
sage: ZZ3 = cartesian_product([ZZ, ZZ, ZZ])
sage: sZZ3 = ZZ3._sympy_(); sZZ3
ProductSet(Integers, Integers, Integers)
sage: (1, 2, 3) in sZZ3
True
"""
from sympy import ProductSet
return ProductSet(*self.cartesian_factors())

class ElementMethods:

def cartesian_projection(self, i):
Expand Down
14 changes: 14 additions & 0 deletions src/sage/modules/free_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -5306,6 +5306,20 @@ def gen(self, i=0):
v.set_immutable()
return v

def _sympy_(self):
"""
Return a SymPy ``ProductSet`` corresponding to ``self``.
EXAMPLES::
sage: sZZ3 = (ZZ^3)._sympy_(); sZZ3
ProductSet(Integers, Integers, Integers)
sage: (1, 2, 3) in sZZ3
True
"""
from sympy import ProductSet
return ProductSet(*([self.coordinate_ring()] * self.rank()))


###############################################################################
#
Expand Down
12 changes: 12 additions & 0 deletions src/sage/rings/integer_ring.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -1467,6 +1467,18 @@ cdef class IntegerRing_class(PrincipalIdealDomain):
"""
return '"Integer"'

def _sympy_(self):
r"""
Return the SymPy set ``Integers``.
EXAMPLES::
sage: ZZ._sympy_()
Integers
"""
from sympy import Integers
return Integers

def _sage_input_(self, sib, coerced):
r"""
Produce an expression which will reproduce this value when
Expand Down
12 changes: 12 additions & 0 deletions src/sage/rings/rational_field.py
Original file line number Diff line number Diff line change
Expand Up @@ -1576,6 +1576,18 @@ def _polymake_init_(self):
"""
return '"Rational"'

def _sympy_(self):
r"""
Return the SymPy set ``Rationals``.
EXAMPLES::
sage: QQ._sympy_()
Rationals
"""
from sympy import Rationals
return Rationals

def _sage_input_(self, sib, coerced):
r"""
Produce an expression which will reproduce this value when evaluated.
Expand Down
13 changes: 13 additions & 0 deletions src/sage/sets/non_negative_integers.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,3 +221,16 @@ def unrank(self, rnk):
100
"""
return self.from_integer(rnk)

def _sympy_(self):
r"""
Return the SymPy set ``Naturals0``.
EXAMPLES::
sage: NN = NonNegativeIntegers()
sage: NN._sympy_()
Naturals0
"""
from sympy import Naturals0
return Naturals0
12 changes: 12 additions & 0 deletions src/sage/sets/positive_integers.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,15 @@ def an_element(self):
42
"""
return Integer(42)

def _sympy_(self):
r"""
Return the SymPy set ``Naturals``.
EXAMPLES::
sage: PositiveIntegers()._sympy_()
Naturals
"""
from sympy import Naturals
return Naturals
63 changes: 63 additions & 0 deletions src/sage/sets/real_set.py
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,28 @@ def _sympy_condition_(self, variable):
upper_condition = true
return lower_condition & upper_condition

def _sympy_(self):
r"""
Return the SymPy set corresponding to ``self``.
EXAMPLES::
sage: RealSet.open_closed(0, 1)[0]._sympy_()
Interval.Lopen(0, 1)
sage: RealSet.point(0)[0]._sympy_()
FiniteSet(0)
sage: RealSet.open(0,1)[0]._sympy_()
Interval.open(0, 1)
sage: RealSet.open(-oo,1)[0]._sympy_()
Interval.open(-oo, 1)
sage: RealSet.open(0, oo)[0]._sympy_()
Interval.open(0, oo)
"""
from sympy import Interval
return Interval(self.lower(), self.upper(),
left_open=not self._lower_closed,
right_open=not self._upper_closed)

def closure(self):
"""
Return the closure
Expand Down Expand Up @@ -986,6 +1008,17 @@ def is_empty(self):
"""
return len(self._intervals) == 0

def is_universe(self):
"""
Return whether the set is the ambient space (the real line).
EXAMPLES::
sage: RealSet().ambient().is_universe()
True
"""
return self == self.ambient()

def get_interval(self, i):
"""
Return the ``i``-th connected component.
Expand Down Expand Up @@ -1811,3 +1844,33 @@ def __rmul__(self, other):
[0, 1/2*pi] + [2*pi, +oo)
"""
return self * other

def _sympy_(self):
r"""
Return the SymPy set corresponding to ``self``.
EXAMPLES::
sage: RealSet()._sympy_()
EmptySet
sage: RealSet.point(5)._sympy_()
FiniteSet(5)
sage: (RealSet.point(1).union(RealSet.point(2)))._sympy_()
FiniteSet(1, 2)
sage: (RealSet(1, 2).union(RealSet.closed(3, 4)))._sympy_()
Union(Interval.open(1, 2), Interval(3, 4))
sage: RealSet(-oo, oo)._sympy_()
Reals
Infinities are not elements::
sage: import sympy
sage: RealSet(-oo, oo)._sympy_().contains(sympy.oo)
False
"""
from sympy import Reals, Union
if self.is_universe():
return Reals
else:
return Union(*[interval._sympy_()
for interval in self._intervals])

0 comments on commit 9fcf32e

Please sign in to comment.