Skip to content

Commit b7e2cc2

Browse files
authored
Merge pull request symengine#441 from isuruf/floor
Define __floor__, __mod__, __divmod__ for basic
2 parents 03671e2 + 625004b commit b7e2cc2

File tree

2 files changed

+22
-10
lines changed

2 files changed

+22
-10
lines changed

symengine/lib/symengine_wrapper.pyx

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -890,6 +890,16 @@ cdef class Basic(object):
890890
if A is None or B is None: return NotImplemented
891891
return c2py(symengine.div(A.thisptr, B.thisptr))
892892

893+
def __floordiv__(x, y):
894+
return floor(x/y)
895+
896+
def __mod__(x, y):
897+
return x - y * floor(x/y)
898+
899+
def __divmod__(x, y):
900+
f = floor(x/y)
901+
return f, x - y * f
902+
893903
def __pow__(a, b, c):
894904
if c is not None:
895905
return powermod(a, b, c)
@@ -1830,15 +1840,6 @@ class Integer(Rational):
18301840
else:
18311841
return NotImplemented
18321842

1833-
def __floordiv__(x, y):
1834-
return quotient(x, y)
1835-
1836-
def __mod__(x, y):
1837-
return mod(x, y)
1838-
1839-
def __divmod__(x, y):
1840-
return quotient_mod(x, y)
1841-
18421843
def _sympy_(Basic self):
18431844
import sympy
18441845
return sympy.Integer(int(self))

symengine/tests/test_arit.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from symengine.test_utilities import raises
22

33
from symengine import (Symbol, Integer, Add, Mul, Pow, Rational, sqrt,
4-
symbols, S, I, count_ops)
4+
symbols, S, I, count_ops, floor)
55

66

77
def test_arit1():
@@ -165,12 +165,23 @@ def test_as_numer_denom():
165165
assert x == Integer(-5)
166166
assert y == Integer(1)
167167

168+
169+
def test_floor():
170+
exprs = [Symbol("x"), Symbol("y"), Integer(2), Rational(-3, 5), Integer(-3)]
171+
172+
for x in exprs:
173+
for y in exprs:
174+
assert x // y == floor(x / y)
175+
assert x == y * (x // y) + x % y
176+
177+
168178
def test_as_real_imag():
169179
x, y = (5 + 6 * I).as_real_imag()
170180

171181
assert x == 5
172182
assert y == 6
173183

184+
174185
def test_from_args():
175186
x = Symbol("x")
176187
y = Symbol("y")

0 commit comments

Comments
 (0)