Skip to content
Snippets Groups Projects

Clarify semantics of fancy integer division functions.

Merged Daniel Bauer requested to merge hyteg/pystencils:bauerd/fancy-int-div into v2.0-dev
1 unresolved thread
Compare and Show latest version
2 files
+ 39
23
Preferences
Compare changes
Files
2
import sympy as sp
import warnings
from pystencils.sympyextensions import is_integer_sequence
class IntegerFunctionTwoArgsMixIn(sp.Function):
@@ -87,17 +88,22 @@ class round_to_multiple_towards_zero(IntegerFunctionTwoArgsMixIn):
-8
"""
def _eval_op(self, arg1, arg2):
@classmethod
def eval(cls, arg1, arg2):
from ..utils import c_intdiv
return c_intdiv(arg1, arg2) * arg2
if is_integer_sequence((arg1, arg2)):
return c_intdiv(arg1, arg2) * arg2
def _eval_op(self, arg1, arg2):
return self.eval(arg1, arg2)
# noinspection PyPep8Naming
class ceil_to_multiple(IntegerFunctionTwoArgsMixIn):
"""For positive input, returns the next greater/equal integer divisible by
given divisor. The return value is implementation defined if either argument
is negative.
"""For positive input, returns the next greater/equal integer divisible
by given divisor. The return value is unspecified if either argument is
negative.
Examples:
>>> ceil_to_multiple(9, 4)
@@ -108,17 +114,22 @@ class ceil_to_multiple(IntegerFunctionTwoArgsMixIn):
12
"""
def _eval_op(self, arg1, arg2):
@classmethod
def eval(cls, arg1, arg2):
from ..utils import c_intdiv
return c_intdiv(arg1 + arg2 - 1, arg2) * arg2
if is_integer_sequence((arg1, arg2)):
return c_intdiv(arg1 + arg2 - 1, arg2) * arg2
def _eval_op(self, arg1, arg2):
return self.eval(arg1, arg2)
# noinspection PyPep8Naming
class div_ceil(IntegerFunctionTwoArgsMixIn):
"""For positive input, integer division that is always rounded up, i.e.
`div_ceil(a, b) = ceil(div(a, b))`. The return value is implementation
defined if either argument is negative.
`div_ceil(a, b) = ceil(div(a, b))`. The return value is unspecified if
either argument is negative.
Examples:
>>> div_ceil(9, 4)
@@ -127,10 +138,15 @@ class div_ceil(IntegerFunctionTwoArgsMixIn):
2
"""
def _eval_op(self, arg1, arg2):
@classmethod
def eval(cls, arg1, arg2):
from ..utils import c_intdiv
return sp.sympify(c_intdiv(arg1 + arg2 - 1, arg2))
if is_integer_sequence((arg1, arg2)):
return c_intdiv(arg1 + arg2 - 1, arg2)
def _eval_op(self, arg1, arg2):
return self.eval(arg1, arg2)
# Deprecated functions.