Skip to content

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

Closed

Description

@WarrenWeckesser

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]])

Activity

  1. seberg commented on Dec 26, 2013

    @seberg
    Member

    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.

  2. WarrenWeckesser commented on Dec 26, 2013

    @WarrenWeckesser
    MemberAuthor

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

  3. seberg commented on Dec 26, 2013

    @seberg
    Member

    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.

  4. njsmith commented on Dec 26, 2013

    @njsmith
    Member

    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" [email protected] 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
    .

  5. seberg commented on Dec 26, 2013

    @seberg
    Member

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

  6. added a commit that references this issue on Mar 2, 2014
    27f9354
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions