Skip to content

Commit

Permalink
Trac #31931: _sympy_ methods for some parent classes
Browse files Browse the repository at this point in the history
We add `_sympy_` methods to various parent classes, returning sympy
`Integers`, `Reals`, `Complexes`, `ProductSet`.

These methods override the generic `_sympy_` method provided by #31938.

Part of #31926 Meta-ticket: Connect Sage sets to sympy sets

URL: https://trac.sagemath.org/31931
Reported by: mkoeppe
Ticket author(s): Matthias Koeppe
Reviewer(s): Karl-Dieter Crisman
  • Loading branch information
Release Manager committed Jun 29, 2021
2 parents 7cbc668 + 32cdd5c commit f64fd90
Show file tree
Hide file tree
Showing 7 changed files with 157 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/sage/categories/sets_cat.py
Original file line number Diff line number Diff line change
Expand Up @@ -2479,6 +2479,23 @@ 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
from sage.interfaces.sympy import sympy_init
sympy_init()
return ProductSet(*self.cartesian_factors())

class ElementMethods:

def cartesian_projection(self, i):
Expand Down
16 changes: 16 additions & 0 deletions src/sage/modules/free_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -5341,6 +5341,22 @@ 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
from sage.interfaces.sympy import sympy_init
sympy_init()
return ProductSet(*([self.coordinate_ring()] * self.rank()))


###############################################################################
#
Expand Down
14 changes: 14 additions & 0 deletions src/sage/rings/integer_ring.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -1467,6 +1467,20 @@ 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
from sage.interfaces.sympy import sympy_init
sympy_init()
return Integers

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

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

def _sage_input_(self, sib, coerced):
r"""
Produce an expression which will reproduce this value when evaluated.
Expand Down
15 changes: 15 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,18 @@ 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
from sage.interfaces.sympy import sympy_init
sympy_init()
return Naturals0
14 changes: 14 additions & 0 deletions src/sage/sets/positive_integers.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,17 @@ 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
from sage.interfaces.sympy import sympy_init
sympy_init()
return Naturals
67 changes: 67 additions & 0 deletions src/sage/sets/real_set.py
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,30 @@ 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
from sage.interfaces.sympy import sympy_init
sympy_init()
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 @@ -1058,6 +1082,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 @@ -2038,3 +2073,35 @@ 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
from sage.interfaces.sympy import sympy_init
sympy_init()
if self.is_universe():
return Reals
else:
return Union(*[interval._sympy_()
for interval in self._intervals])

0 comments on commit f64fd90

Please sign in to comment.