Skip to content

Fixed meshgrid to return arrays with same dtype as arguments. #5302

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 2 commits into from
Nov 6, 2016
Merged

Fixed meshgrid to return arrays with same dtype as arguments. #5302

merged 2 commits into from
Nov 6, 2016

Conversation

idfah
Copy link
Contributor

@idfah idfah commented Nov 20, 2014

Fixes issue with meshgrid discussed in issue #5297.

A few things to note:

  • meshgrid takes multiple arrays as arguments and returns multiple arrays. With this proposed change the dtype of each returned array will have the same dtype as its corresponding argument. An alternative would have been to use the common dtype returned by np.return_type but this didn't seem to make as much sense to me.
  • I added a test case to verify that the dtypes of the returned arrays match the arguments.
  • I added a test case to test_no_input because this test case would have failed before my patch if copy=False had been set.

return [x * mult_fact for x in output]
else:
return np.broadcast_arrays(*output)
output = np.broadcast_arrays(*output)
Copy link
Member

Choose a reason for hiding this comment

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

Might want subok=True here to match the asanyarray above. Otherwise LGTM.

@charris
Copy link
Member

charris commented Nov 29, 2014

Should probably add a check that the input arrays are all 1-D.

@charris
Copy link
Member

charris commented Nov 29, 2014

NVM, looks like that inputs are all flattened, at least if they aren't matrices. That should be in the documentation somewhere.

@charris
Copy link
Member

charris commented Nov 29, 2014

LGTM in general, just need to decide on the subok option for the broadcast values. I'm thinking of arrays with units that some folks are implementing.

@seberg
Copy link
Member

seberg commented Jan 4, 2015

Makes sense, but I wonder if we should put it into the release notes. If you used to have an integer arange input and now use meshgrid, it seems like you used to get floats. That could create annoying casting issues.

@wjakob
Copy link

wjakob commented Oct 8, 2016

I just ran into this and was about to file a bug. What's keeping this from being merged?

@idfah
Copy link
Contributor Author

idfah commented Oct 10, 2016

I added subok=True in the call to broadcast_arrays as per charris' comments. Can someone please look at merging this? Please let me know if there is anything else I can do.

@rgommers
Copy link
Member

rgommers commented Nov 6, 2016

Makes sense, but I wonder if we should put it into the release notes. If you used to have an integer arange input and now use meshgrid, it seems like you used to get floats. That could create annoying casting issues.

That doesn't seem to be the case, with numpy 1.11.1:

>>> x = np.arange(2)
>>> y = np.arange(2.)
>>> np.meshgrid(x, y)[0].dtype
dtype('int32')
>>> np.meshgrid(x, y)[1].dtype
dtype('float64')

Changing output dtypes of meshgrid seems like an unhealthy idea.

@rgommers
Copy link
Member

rgommers commented Nov 6, 2016

Okay, int was treated fine but gh-5297 discussed an issue with float32. Copying here because most of gh-5297 is about something else:

>>> ax, ay = np.meshgrid(a,a)
>>> ax.dtype
dtype('float64')

>>> ax, ay = np.meshgrid(a,a, copy=False)
>>> ax.dtype
dtype('float32')

@rgommers
Copy link
Member

rgommers commented Nov 6, 2016

makes sense to change that.

@rgommers rgommers merged commit e287741 into numpy:master Nov 6, 2016
@rgommers
Copy link
Member

rgommers commented Nov 6, 2016

Merged, thanks @idfah

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

Successfully merging this pull request may close these issues.

5 participants