Skip to content
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

BUG: Incorrect shape of broadcast result with the exponentiation operator **. #4145

Closed
WarrenWeckesser opened this issue Dec 26, 2013 · 5 comments · Fixed by #4146
Closed

Comments

@WarrenWeckesser
Copy link
Member

I haven't explored the full range of inputs that produce the error, but when x has shape (n,) and y has shape (1,1), the result of x ** y does not have the correct (broadcast) shape. It should be (1, n), but in all the numpy versions I've tried (1.6.1, 1.7.1, 1.8.0, and 1.9.0.dev-e7fe68a), the result has shape (n,).

Also, in numpy 1.8.0 and 1.9.0.dev-e7fe68a, the operation generates an unexpected DeprecationWarning.

The following shows an example. Python was run with the command python -W always.

>>> np.__version__
'1.8.0'

>>> x
array([1, 2])

>>> y = np.array([[2]])

>>> x ** y     # This should not generate a warning.  Result should be `array([[1, 4]])`
__main__:1: DeprecationWarning: converting an array with ndim > 0 to an index will result in an error in the future
array([1, 4])

>>> x + y    # No warning, correct shape.
array([[3, 4]])

>>> y = np.array([[2], [3]])

>>> x ** y    # No warning, correct shape.
array([[1, 4],
       [1, 8]])
@seberg
Copy link
Member

seberg commented Dec 26, 2013

Are we going to fix this for numpy 1.8.x? Otherwise, just finishing up that deprecation will fix the issue. It is a rather corner case deprecation warning, so it may be possible to just finish the deprecation in 1.9 already.

@WarrenWeckesser
Copy link
Member Author

What do you mean by "just finishing up that deprecation will fix the issue"?

@seberg
Copy link
Member

seberg commented Dec 26, 2013

Well, if you set warnings.filterwarnings('error') to raise the deprecation warning, you get the correct result, because trying to interpret the second array as an integer fails. That said, trying to interpret it as an integer when it already is an array does not make sense, so the __pow__ code should be fixed.

@njsmith
Copy link
Member

njsmith commented Dec 26, 2013

Why does pow have a special case for integers anyway? We don't switch
to matrix exponentiation or anything like that, right?
On 26 Dec 2013 13:05, "seberg" notifications@github.com wrote:

Well, if you set warnings.filterwarnings('error') to raise the
deprecation warning, you get the correct result, because trying to
interpret the second array as an integer fails. That said, trying to
interpret it as an integer when it already is an array does not make sense,
so the pow code should be fixed.


Reply to this email directly or view it on GitHubhttps://github.com//issues/4145#issuecomment-31235793
.

@seberg
Copy link
Member

seberg commented Dec 26, 2013

Ah, will open a PR with a fix... Apparently because we optimize (x ** 2) and some other operations like that.

seberg added a commit to seberg/numpy that referenced this issue Feb 28, 2014
Operations such as `x**array([2])` would convert the
2 into an integer and loose the dimension information,
because the array (at this time, it is deprecated),
supports `__index__` even though it is not 0-d.

This fixes it, by not trying the index machinery
when it was an array, since it is unnecessary.

Closes numpygh-4145
juliantaylor added a commit to juliantaylor/numpy that referenced this issue Mar 2, 2014
Operations such as `x**array([2])` would convert the
2 into an integer and loose the dimension information,
because the array (at this time, it is deprecated),
supports `__index__` even though it is not 0-d.

This fixes it, by not trying the index machinery
when it was an array, since it is unnecessary.

Closes numpygh-4145
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants