Criteria
I assume that you want the div function to pass the following tests:
from sympy import sympify, simplify, Symbol def test_div(div): # check that div behaves as intended for integers for i in range(-5,5): for j in range(-5,5): if j==0: continue assert i
Modular
You can implement integer division using the modulo operator as follows:
div = lambda x,y: (xx%y)/y
Since SymPy supports modular arithmetic and can simplify it, this function passes the above tests. However, if complete simplification is not possible, you will get modulo-expressed expressions that may be undesirable.
Floor
As mentioned in the comments, SymPy provides a floor function that can be used to obtain integer division (just like the // operator for expressions):
div = lambda x,y: sympy.floor(x/y)
However, floor does not support simplifications and therefore does not perform the second test.
Wrzlprmft
source share