-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Min max on iterables #3820
Merged
Merged
Min max on iterables #3820
Changes from 7 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
a29c786
initial commit
rjenc29 1e8dfd7
some basic tests
rjenc29 cabf2b3
more tests and extend support to max
rjenc29 72241de
small cleanup
rjenc29 2801775
fix typing vs implementation mismatch
rjenc29 ca9bf30
fix logic and add tests
rjenc29 41717b0
remove unused import
rjenc29 7621239
remove duplicate test
rjenc29 75e5581
tweaks per PR feedback
rjenc29 d583ea0
Merge branch 'master' into min-max
rjenc29 4b7b47f
use min and max in heapq
rjenc29 deac891
fix flake8 issue
rjenc29 File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,7 +8,7 @@ | |
|
||
import numba.unittest_support as unittest | ||
from numba.compiler import compile_isolated, Flags | ||
from numba import jit, typeof, errors, types, utils, config | ||
from numba import jit, typeof, errors, types, utils, config, njit | ||
from .support import TestCase, tag | ||
|
||
|
||
|
@@ -982,6 +982,60 @@ def test_pow_usecase(self): | |
r = cres.entry_point(x, y) | ||
self.assertPreciseEqual(r, pow_usecase(x, y)) | ||
|
||
def _check_min_max(self, pyfunc): | ||
cfunc = njit()(pyfunc) | ||
expected = pyfunc() | ||
got = cfunc() | ||
self.assertPreciseEqual(expected, got) | ||
|
||
def test_min_max_iterable_input(self): | ||
|
||
@njit | ||
def frange(start, stop, step): | ||
i = start | ||
while i < stop: | ||
yield i | ||
i += step | ||
|
||
def sample_functions(op): | ||
yield lambda: op(range(10)) | ||
yield lambda: op(range(4, 12)) | ||
yield lambda: op(range(-4, -15, -1)) | ||
yield lambda: op([6.6, 5.5, 7.7]) | ||
yield lambda: op([(3, 4), (1, 2)]) | ||
yield lambda: op(frange(1.1, 3.3, 0.1)) | ||
yield lambda: op([np.nan, -np.inf, np.inf, np.nan]) | ||
yield lambda: op([(1,), (1,), (1,)]) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Perhaps vary the tuple value? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. |
||
|
||
for fn in sample_functions(op=min): | ||
self._check_min_max(fn) | ||
|
||
for fn in sample_functions(op=max): | ||
self._check_min_max(fn) | ||
|
||
def test_min_max_supplemental(self): | ||
stuartarchibald marked this conversation as resolved.
Show resolved
Hide resolved
|
||
# check the parallel implementations of | ||
# argmin and argmax, which depend on | ||
# min and max builtins, respectively | ||
|
||
def argmin(arr): | ||
return np.argmin(arr) | ||
|
||
def argmax(arr): | ||
return np.argmax(arr) | ||
|
||
def arrays(): | ||
yield np.arange(10, 2, -1) | ||
yield np.ones(365) | ||
yield self.random.randn(10) | ||
|
||
for pyfunc in argmin, argmax: | ||
cfunc = njit(parallel=True)(pyfunc) | ||
for arr in arrays(): | ||
expected = pyfunc(arr) | ||
got = cfunc(arr) | ||
self.assertPreciseEqual(expected, got) | ||
|
||
|
||
if __name__ == '__main__': | ||
unittest.main() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wondering if it's possible to pull these out of this scope which has a non-intuitive signature? Would defining another overload like:
work? Could also close over the
<
,>
operator to save duplication too?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.