8000 ENH: Make it possible to NpyIter_RemoveAxis an empty dimension by seberg · Pull Request #3861 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

ENH: Make it possible to NpyIter_RemoveAxis an empty dimension #3861

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 3 commits into from
Apr 29, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

8000
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
BUG: Fix matrix multiply test gufunc for empty matrices
The case where the result is not empty was missing the zeroing out
now that it is legal to use that input.
  • Loading branch information
seberg committed Apr 29, 2017
commit 1c4e0d4d1b8e365111b70c559e6b66dd34c35d06
14 changes: 14 additions & 0 deletions numpy/core/src/umath/umath_tests.c.src
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,20 @@ static void
npy_intp ib2_n = is2_n*dn;
npy_intp ib2_p = is2_p*dp;
npy_intp ob_p = os_p *dp;
if (dn == 0) {
/* No operand, need to zero the output */
BEGIN_OUTER_LOOP_3
char *op=args[2];
for (m = 0; m < dm; m++) {
for (p = 0; p < dp; p++) {
*(@typ@ *)op = 0;
op += os_p;
}
op += os_m - ob_p;
}
END_OUTER_LOOP
return;
}
Copy link
Member
@eric-wieser eric-wieser Mar 26, 2017

Choose a reason for hiding this comment

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

Wouldn't a better fix be to just reassign the pointers, and run the below code?

Something like

if (dn == 0) {
     @typ@ ip_val = 0;
     @typ@ ip2_val = 0;
      ip1 = ip2 = &ip_val;
      // set strides to 0
      dn = 1
}

Copy link
Member Author

Choose a reason for hiding this comment

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

Nice hack, didn't think about it much.

Copy link
Member Author

Choose a reason for hiding this comment

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

Hmm, no, B935 the char *ip1=args[0], *ip2=args[1], *op=args[2]; line is important where it is, since the macro will manipulate args

BEGIN_OUTER_LOOP_3
char *ip1=args[0], *ip2=args[1], *op=args[2];
for (m = 0; m < dm; m++) {
Expand Down
6 changes: 6 additions & 0 deletions numpy/core/tests/test_ufunc.py
Original file line number Diff line number Diff line change
Expand Up @@ -542,6 +542,12 @@ def test_matrix_multiply(self):
self.compare_matrix_multiply_results(np.long)
self.compare_matrix_multiply_results(np.double)

def test_matrix_multiply_umath_empty(self):
res = umt.matrix_multiply(np.ones((0, 10)), np.ones((10, 0)))
assert_array_equal(res, np.zeros((0, 0)))
res = umt.matrix_multiply(np.ones((10, 0)), np.ones((0, 10)))
assert_array_equal(res, np.zeros((10, 10)))

def compare_matrix_multiply_results(self, tp):
d1 = np.array(np.random.rand(2, 3, 4), dtype=tp)
d2 = np.array(np.random.rand(2, 3, 4), dtype=tp)
Expand Down
0