8000 ENH: Added libdivide for floor divide by ganesh-k13 · Pull Request #17727 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

ENH: Added libdivide for floor divide #17727

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 32 commits into from
Dec 2, 2020
Merged
Changes from 1 commit
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
179038f
ENH: Added libdiv
ganesh-k13 Nov 7, 2020
e89175b
ENH: Fixed typos in header | use in2 over ip2
ganesh-k13 Nov 7, 2020
565759b
ENH: Added optimal divisor
ganesh-k13 Nov 8, 2020
d0c934c
ENH: Added libdivide header
ganesh-k13 Nov 8, 2020
b02399a
ENH: Made libdivide default
ganesh-k13 Nov 8, 2020
f0ddb7c
ENH: Handled divide by 0 case
ganesh-k13 Nov 8, 2020
72dcc04
ENH: Added libdivide zlib license
ganesh-k13 Nov 9, 2020
19835d2
ENH: Removed empty structure
ganesh-k13 Nov 10, 2020
3975a28
ENH: Auto generate libdivide structs
ganesh-k13 Nov 11, 2020
90e6cf5
ENH: Logic to optimize %
ganesh-k13 Nov 11, 2020
969aa03
ENH: Fix breaking case
ganesh-k13 Nov 11, 2020
44a3a31
ENH: Change comments
ganesh-k13 Nov 11, 2020
b3d70ef
ENH: Improved floor division (#17727)
ganesh-k13 Nov 11, 2020
931134b
ENH: Added asv benchmarks
ganesh-k13 Nov 11, 2020
6e2e281
ENH: Change comments
ganesh-k13 Nov 12, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
TST: Minor changes to floor divide | Added cases for timedelta divide
  • Loading branch information
ganesh-k13 committed Nov 21, 2020
commit f93ca93e93a9a215d25751cee442665018e345e6
53 changes: 41 additions & 12 deletions numpy/core/tests/test_umath.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,25 +252,54 @@ def test_division_int(self):
@pytest.mark.parametrize("input_dtype",
[np.int8, np.int16, np.int32, np.int64])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add timedelta64? assuming it is not super annoying...

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have added some cases, not sure if those many cases are needed :). Please let me know if any changes are needed.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably more than necessary, but that is fine. The different unit cases are not super interesting for the division code, but frankly a bit many tests are fine :).

def test_division_int_boundary(self, input_dtype):
class ListWithDiv(list):
def __floordiv__(self, divisor):
return [i//divisor for i in self]

iinfo = np.iinfo(input_dtype)

# Create array with min, 25th percentile, 0, 75th percentile, max
arr 8000 = ListWithDiv([iinfo.min, iinfo.min//2, 0, iinfo.max//2, iinfo.max])
dividends = [iinfo.min, iinfo.min//2, iinfo.max//2, iinfo.max]
a = np.array(arr, dtype = input_dtype)
# Create list with min, 25th percentile, 0, 75th percentile, max
lst = [iinfo.min, iinfo.min//2, 0, iinfo.max//2, iinfo.max]
divisors = [iinfo.min, iinfo.min//2, iinfo.max//2, iinfo.max]
a = np.array(lst, dtype=input_dtype)

for dividend in dividends:
div_a = a // dividend
div_arr = arr // dividend
assert_(all(div_a == div_arr))
for divisor in divisors:
div_a = a // divisor
b = a.copy(); b //= divisor
div_lst = [i // divisor for i in lst]
assert_(all(div_a == div_lst))
assert_(all(div_a == b))

with np.errstate(divide='raise'):
with pytest.raises(FloatingPointError):
a // 0
with pytest.raises(FloatingPointError):
a //= 0

@pytest.mark.parametrize(
"dividend,divisor,quotient",
[(np.timedelta64(2,'Y'), np.timedelta64(2,'M'), 12),
(np.timedelta64(2,'Y'), np.timedelta64(-2,'M'), -12),
(np.timedelta64(-2,'Y'), np.timedelta64(2,'M'), -12),
(np.timedelta64(-2,'Y'), np.timedelta64(-2,'M'), 12),
(np.timedelta64(2,'M'), np.timedelta64(-2,'Y'), -1),
(np.timedelta64(2,'Y'), np.timedelta64(0,'M'), 0),
(np.timedelta64(2,'Y'), 2, np.timedelta64(1,'Y')),
(np.timedelta64(2,'Y'), -2, np.timedelta64(-1,'Y')),
(np.timedelta64(-2,'Y'), 2, np.timedelta64(-1,'Y')),
(np.timedelta64(-2,'Y'), -2, np.timedelta64(1,'Y')),
(np.timedelta64(-2,'Y'), -2, np.timedelta64(1,'Y')),
(np.timedelta64(-2,'Y'), -3, np.timedelta64(0,'Y')),
(np.timedelta64(-2,'Y'), 0, np.timedelta64('Nat','Y')),
])
def test_division_int_timedelta(self, dividend, divisor, quotient):
# If either divisor is 0 or quotient is Nat, check for division by 0
if divisor and (isinstance(quotient, int) or not np.isnat(quotient)):
assert_(dividend // divisor == quotient)
# Test for arrays as well
assert_(all(
np.array([divide 6571 nd]*5) // divisor \
== np.array([quotient]*5)))
else:
with np.errstate(divide='raise', invalid='raise'):
with pytest.raises(FloatingPointError):
dividend // divisor

def test_division_complex(self):
# check that implementation is correct
Expand Down
0