Skip to content

Commit add40b1

Browse files
committed
Added Tests
1 parent a3fa1f1 commit add40b1

File tree

3 files changed

+95
-19
lines changed

3 files changed

+95
-19
lines changed

symengine/lib/symengine.pxd

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,7 @@ cdef extern from "<symengine/basic.h>" namespace "SymEngine":
311311
bool is_a_Or "SymEngine::is_a<SymEngine::Or>"(const Basic &b) nogil
312312
bool is_a_Xor "SymEngine::is_a<SymEngine::Xor>"(const Basic &b) nogil
313313
RCP[const Basic] expand(RCP[const Basic] &o) nogil except +
314+
void as_numer_denom(RCP[const Basic] &x, const Ptr[RCP[Basic]] &numer, const Ptr[RCP[Basic]] &denom) nogil
314315

315316
cdef extern from "<symengine/subs.h>" namespace "SymEngine":
316317
RCP[const Basic] msubs (RCP[const Basic] &x, const map_basic_basic &x) nogil

symengine/lib/symengine_wrapper.pyx

Lines changed: 34 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -773,12 +773,17 @@ cdef class Basic(object):
773773
cdef _DictBasic D = get_dict(*args)
774774
return c2py(symengine.ssubs(self.thisptr, D.c))
775775

776-
xreplace = subs
776+
replace = xreplace = subs
777777

778778
def msubs(Basic self not None, *args):
779779
cdef _DictBasic D = get_dict(*args)
780780
return c2py(symengine.msubs(self.thisptr, D.c))
781781

782+
def as_numer_denom(Basic self not None):
783+
cdef RCP[const symengine.Basic] _num, _den
784+
symengine.as_numer_denom(self.thisptr, symengine.outArg(_num), symengine.outArg(_den))
785+
return c2py(<RCP[const symengine.Basic]>_num), c2py(<RCP[const symengine.Basic]>_den)
786+
782787
def n(self, prec = 53, real = False):
783788
if real:
784789
return eval_real(self, prec)
@@ -886,7 +891,7 @@ cdef class Basic(object):
886891
return False
887892

888893
def copy(self):
889-
return self.func(*self.args)
894+
return self
890895

891896
def _symbolic_(self, ring):
892897
return ring(self._sage_())
@@ -1363,10 +1368,6 @@ class Rational(Number):
13631368
symengine.outArg_Integer(_num), symengine.outArg_Integer(_den))
13641369
return [c2py(<RCP[const symengine.Basic]>_num), c2py(<RCP[const symengine.Basic]>_den)]
13651370

1366-
def as_numer_denom(self):
1367-
r = self.get_num_den()
1368-
return r[0], r[1]
1369-
13701371
def _sympy_(self):
13711372
rat = self.get_num_den()
13721373
return rat[0]._sympy_() / rat[1]._sympy_()
@@ -1486,8 +1487,8 @@ class Integer(Rational):
14861487
def q(self):
14871488
return 1
14881489

1489-
def as_numer_denom(Basic self):
1490-
return self, Integer(1)
1490+
def get_num_den(Basic self):
1491+
return self, 1
14911492

14921493
@property
14931494
def func(self):
@@ -1753,16 +1754,6 @@ class NaN(Number):
17531754

17541755

17551756
class AssocOp(Basic):
1756-
1757-
@classmethod
1758-
def _from_args(cls, args):
1759-
if len(args) == 0:
1760-
return cls.identity
1761-
elif len(args) == 1:
1762-
return args[0]
1763-
1764-
obj = super(AssocOp, cls).__new__(cls, *args)
1765-
return obj
17661757

17671758
@classmethod
17681759
def make_args(cls, expr):
@@ -1774,6 +1765,8 @@ class AssocOp(Basic):
17741765

17751766
class Add(AssocOp):
17761767

1768+
identity = 0
1769+
17771770
def __new__(cls, *args, **kwargs):
17781771
cdef symengine.vec_basic v_
17791772
cdef Basic e
@@ -1782,6 +1775,15 @@ class Add(AssocOp):
17821775
v_.push_back(e.thisptr)
17831776
return c2py(symengine.add(v_))
17841777

1778+
@classmethod
1779+
def _from_args(self, args):
1780+
if len(args) == 0:
1781+
return self.identity
1782+
elif len(args) == 1:
1783+
return args[0]
1784+
1785+
return Add(*args)
1786+
17851787
@property
17861788
def is_Add(self):
17871789
return True
@@ -1817,6 +1819,8 @@ class Add(AssocOp):
18171819

18181820
class Mul(AssocOp):
18191821

1822+
identity = 1
1823+
18201824
def __new__(cls, *args, **kwargs):
18211825
cdef symengine.vec_basic v_
18221826
cdef Basic e
@@ -1825,6 +1829,15 @@ class Mul(AssocOp):
18251829
v_.push_back(e.thisptr)
18261830
return c2py(symengine.mul(v_))
18271831

1832+
@classmethod
1833+
def _from_args(self, args):
1834+
if len(args) == 0:
1835+
return self.identity
1836+
elif len(args) == 1:
1837+
return args[0]
1838+
1839+
return Mul(*args)
1840+
18281841
@property
18291842
def is_Mul(self):
18301843
return True
@@ -1870,6 +1883,9 @@ class Pow(Basic):
18701883
cdef RCP[const symengine.Pow] X = symengine.rcp_static_cast_Pow(self.thisptr)
18711884
return c2py(deref(X).get_exp())
18721885

1886+
def as_base_exp(self):
1887+
return self.base, self.exp
1888+
18731889
@property
18741890
def is_Pow(self):
18751891
return True

symengine/tests/test_arit.py

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from symengine.utilities import raises
22

3-
from symengine import Symbol, Integer, Add, Pow
3+
from symengine import Symbol, Integer, Add, Mul, Pow, Rational, sqrt
44

55

66
def test_arit1():
@@ -149,3 +149,62 @@ def test_free_symbols():
149149
z = Symbol("z")
150150
assert (x**2).free_symbols == set([x])
151151
assert (x**y + z).free_symbols == set([x, y, z])
152+
153+
154+
def test_as_numer_denom():
155+
x, y = Rational(17, 26).as_numer_denom()
156+
assert x == Integer(17)
157+
assert y == Integer(26)
158+
159+
x, y = Integer(-5).as_numer_denom()
160+
assert x == Integer(-5)
161+
assert y == Integer(1)
162+
163+
164+
def test_from_args():
165+
x = Symbol("x")
166+
y = Symbol("y")
167+
168+
assert Add._from_args([]) == 0
169+
assert Add._from_args([x]) == x
170+
assert Add._from_args([x, y]) == x + y
171+
172+
assert Mul._from_args([]) == 1
173+
assert Mul._from_args([x]) == x
174+
assert Mul._from_args([x, y]) == x * y
175+
176+
177+
def test_make_args():
178+
x = Symbol("x")
179+
y = Symbol("y")
180+
z = Symbol("z")
181+
182+
assert Add.make_args(x) == (x,)
183+
assert Mul.make_args(x) == (x,)
184+
185+
assert Add.make_args(x*y*z) == (x*y*z,)
186+
assert Mul.make_args(x*y*z) == (x*y*z).args
187+
188+
assert Add.make_args(x + y + z) == (x + y + z).args
189+
assert Mul.make_args(x + y + z) == (x + y + z,)
190+
191+
assert Add.make_args((x + y)**z) == ((x + y)**z,)
192+
assert Mul.make_args((x + y)**z) == ((x + y)**z,)
193+
194+
195+
def test_Pow_base_exp():
196+
x = Symbol("x")
197+
y = Symbol("y")
198+
e = Pow(x + y, 2)
199+
assert isinstance(e, Pow)
200+
assert e.exp == 2
201+
assert e.base == x + y
202+
203+
assert sqrt(x - 1).as_base_exp() == (x - 1, Rational(1, 2))
204+
205+
206+
def test_copy():
207+
b = Symbol("b")
208+
a = b.copy()
209+
assert a is b
210+
assert type(a) == type(b)

0 commit comments

Comments
 (0)