Skip to content

Commit e46379f

Browse files
committed
Add Rationals
1 parent bca6842 commit e46379f

File tree

5 files changed

+45
-4
lines changed

5 files changed

+45
-4
lines changed

symengine/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
UniversalSet = wrapper.S.UniversalSet
3030
Reals = wrapper.S.Reals
3131
Integers = wrapper.S.Integers
32+
Rationals = wrapper.S.Rationals
3233

3334

3435
if have_mpfr:

symengine/lib/symengine.pxd

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,7 @@ cdef extern from "<symengine/basic.h>" namespace "SymEngine":
302302
bool is_a_Interval "SymEngine::is_a<SymEngine::Interval>"(const Basic &b) nogil
303303
bool is_a_EmptySet "SymEngine::is_a<SymEngine::EmptySet>"(const Basic &b) nogil
304304
bool is_a_Reals "SymEngine::is_a<SymEngine::Reals>"(const Basic &b) nogil
305+
bool is_a_Rationals "SymEngine::is_a<SymEngine::Rationals>"(const Basic &b) nogil
305306
bool is_a_Integers "SymEngine::is_a<SymEngine::Integers>"(const Basic &b) nogil
306307
bool is_a_UniversalSet "SymEngine::is_a<SymEngine::UniversalSet>"(const Basic &b) nogil
307308
bool is_a_FiniteSet "SymEngine::is_a<SymEngine::FiniteSet>"(const Basic &b) nogil
@@ -1056,6 +1057,8 @@ cdef extern from "<symengine/sets.h>" namespace "SymEngine":
10561057
pass
10571058
cdef cppclass Reals(Set):
10581059
pass
1060+
cdef cppclass Rationals(Set):
1061+
pass
10591062
cdef cppclass Integers(Set):
10601063
pass
10611064
cdef cppclass UniversalSet(Set):
@@ -1074,6 +1077,7 @@ cdef extern from "<symengine/sets.h>" namespace "SymEngine":
10741077
cdef rcp_const_basic interval(RCP[const Number] &start, RCP[const Number] &end, bool l, bool r) nogil except +
10751078
cdef RCP[const EmptySet] emptyset() nogil except +
10761079
cdef RCP[const Reals] reals() nogil except +
1080+
cdef RCP[const Rationals] rationals() nogil except +
10771081
cdef RCP[const Integers] integers() nogil except +
10781082
cdef RCP[const UniversalSet] universalset() nogil except +
10791083
cdef RCP[const Set] finiteset(set_basic &container) nogil except +

symengine/lib/symengine_wrapper.pyx

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,8 @@ cdef object c2py(rcp_const_basic o):
239239
r = Set.__new__(Reals)
240240
elif (symengine.is_a_Integers(deref(o))):
241241
r = Set.__new__(Integers)
242+
elif (symengine.is_a_Rationals(deref(o))):
243+
r = Set.__new__(Rationals)
242244
elif (symengine.is_a_UniversalSet(deref(o))):
243245
r = Set.__new__(UniversalSet)
244246
elif (symengine.is_a_FiniteSet(deref(o))):
@@ -451,6 +453,8 @@ def sympy2symengine(a, raise_error=False):
451453
return S.Reals
452454
elif a is sympy.S.Integers:
453455
return S.Integers
456+
elif a is sympy.S.Rationals:
457+
return S.Rationals
454458
elif isinstance(a, sympy.Interval):
455459
return interval(*(a.args))
456460
elif a is sympy.S.EmptySet:
@@ -669,6 +673,10 @@ class Singleton(object):
669673
def Integers(self):
670674
return integers_singleton
671675

676+
@property
677+
def Rationals(self):
678+
return rationals_singleton
679+
672680
@property
673681
def Reals(self):
674682
return reals_singleton
@@ -3020,6 +3028,20 @@ class Reals(Set):
30203028
return self.__class__
30213029

30223030

3031+
class Rationals(Set):
3032+
3033+
def __new__(self):
3034+
return rationals()
3035+
3036+
def _sympy_(self):
3037+
import sympy
3038+
return sympy.S.Rationals
3039+
3040+
@property
3041+
def func(self):
3042+
return self.__class__
3043+
3044+
30233045
class Integers(Set):
30243046

30253047
def __new__(self):
@@ -5185,6 +5207,10 @@ def reals():
51855207
return c2py(<rcp_const_basic>(symengine.reals()))
51865208

51875209

5210+
def rationals():
5211+
return c2py(<rcp_const_basic>(symengine.rationals()))
5212+
5213+
51885214
def integers():
51895215
return c2py(<rcp_const_basic>(symengine.integers()))
51905216

@@ -5299,6 +5325,7 @@ def imageset(sym, expr, base):
52995325

53005326
universal_set_singleton = UniversalSet()
53015327
integers_singleton = Integers()
5328+
rationals_singleton = Rationals()
53025329
reals_singleton = Reals()
53035330
empty_set_singleton = EmptySet()
53045331

symengine/tests/test_sets.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from symengine.utilities import raises
22
from symengine.lib.symengine_wrapper import (Interval, EmptySet, UniversalSet,
3-
FiniteSet, Union, Complement, ImageSet, ConditionSet, Reals, Integers,
4-
And, Or, oo, Symbol, true, Ge, Eq, Gt)
3+
FiniteSet, Union, Complement, ImageSet, ConditionSet, Reals, Rationals,
4+
Integers, And, Or, oo, Symbol, true, Ge, Eq, Gt)
55

66

77
def test_Interval():
@@ -38,7 +38,13 @@ def test_Reals():
3838
assert R.contains(0) == true
3939

4040

41-
def test_Reals():
41+
def test_Rationals():
42+
Q = Rationals()
43+
assert Q.union(FiniteSet(2, 3)) == Q
44+
assert Q.contains(0) == true
45+
46+
47+
def test_Integers():
4248
Z = Integers()
4349
assert Z.union(FiniteSet(2, 4)) == Z
4450
assert Z.contains(0) == true

symengine/tests/test_sympy_conv.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
KroneckerDelta, LeviCivita, erf, erfc, lowergamma, uppergamma,
1010
loggamma, beta, polygamma, sign, floor, ceiling, conjugate, And,
1111
Or, Not, Xor, Piecewise, Interval, EmptySet, FiniteSet, Contains,
12-
Union, Complement, UniversalSet, Reals, Integers)
12+
Union, Complement, UniversalSet, Reals, Rationals, Integers)
1313
import unittest
1414

1515
# Note: We test _sympy_() for SymEngine -> SymPy conversion, as those are
@@ -721,6 +721,9 @@ def test_sets():
721721
assert sympify(sympy.S.Reals) == Reals()
722722
assert sympy.S.Reals == Reals()._sympy_()
723723

724+
assert sympify(sympy.S.Rationals) == Rationals()
725+
assert sympy.S.Rationals == Rationals()._sympy_()
726+
724727
assert sympify(sympy.S.Integers) == Integers()
725728
assert sympy.S.Integers == Integers()._sympy_()
726729

0 commit comments

Comments
 (0)