Skip to content

ENH: add optional argument dtype to numpy.fft.fftfreq (#9126) #16496

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

Closed
wants to merge 3 commits into from
Closed

ENH: add optional argument dtype to numpy.fft.fftfreq (#9126) #16496

wants to merge 3 commits into from

Conversation

cval26
Copy link
Contributor

@cval26 cval26 commented Jun 4, 2020

Deals with issue #9126 requesting the addition of dtype argument option for fftfreq and rfftfreq functions. To do so, I added an optional argument and used the reciprocal function to set the type of the array that will return the result.

A different way to do so would be to leave the calculation as is and instead modify the return line so that it reads return asarray(results*val, dtype=dtype); I think the two ways should be equivalent in terms of the end result, but I was hoping for some feedback.

I've submitted a PR draft since I'm not entirely sure how to do the check that the required dtype is one of the numpy float dtypes; I'm using the isinstance function to check, but I'm not sure what would be the proper types to check against and where to import those from. Any additional comments are of course welcome!

EDIT: I'm now allowing the output array type to be either float32 or float64, with float64 being the default behavior (as before). Let me know what you think; I'm changing the PR from draft to open.

cc @jakirkham @mhvk @eric-wieser @leofang @charris

Closes #9126

@cval26 cval26 marked this pull request as ready for review June 13, 2020 03:56
@jakirkham
Copy link
Contributor

Should we cast d to the same dtype?

@cval26
Copy link
Contributor Author

cval26 commented Jun 15, 2020

We could do that, before using it to compute val, but I'm not sure if that would add something to the functionality; what's your thinking here?

EDIT: @jakirkham thoughts?

@rossbar
Copy link
Contributor

rossbar commented Jun 30, 2020

It's worth noting that even after the switch to pocketfft, numpy.fft still internally promotes float32 -> float64 (and analogously for complex64) as mentioned in #9126 (comment). That's not necessarily a blocker, but it is worth considering whether a dtype= kwarg should be supported for "helper functions" like fftfreq when the main functions in the fft module only support float64 (complex128, etc).

I just wanted to comment here quickly, even if the mailing list might be the more appropriate forum for this discussion.

@jakirkham
Copy link
Contributor

We could do that, before using it to compute val, but I'm not sure if that would add something to the functionality; what's your thinking here?

EDIT: @jakirkham thoughts?

No strong thoughts. More of a question. Though you're right. This may not be needed.

@jakirkham
Copy link
Contributor

LGTM. Thoughts from others?

if dtype != float32:
dtype = float64

val = reciprocal(n*d, dtype=dtype)
N = n//2 + 1
results = arange(0, N, dtype=int)
Copy link
Contributor

Choose a reason for hiding this comment

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

Sorry one last thought. Would it make sense to cast this to dtype as well?

Suggested change
results = arange(0, N, dtype=int)
results = arange(0, N, dtype=dtype)

Copy link
Contributor Author

@cval26 cval26 Jun 30, 2020

Choose a reason for hiding this comment

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

we could but from what I understand this is always just an array of integers, which when multiplied with the dtype cast val will lead to the returned result being of type dtype anyway, right?

@cval26
Copy link
Contributor Author

cval26 commented Jun 30, 2020

It's worth noting that even after the switch to pocketfft, numpy.fft still internally promotes float32 -> float64 (and analogously for complex64) as mentioned in #9126 (comment). That's not necessarily a blocker, but it is worth considering whether a dtype= kwarg should be supported for "helper functions" like fftfreq when the main functions in the fft module only support float64 (complex128, etc).

I just wanted to comment here quickly, even if the mailing list might be the more appropriate forum for this discussion.

that's a very good point @rossbar, thanks for bringing it up. To be honest I haven't really considered it this way; I saw that this was an open issue for quite some time, so I thought I would attempt to work on it.

But considering the fft module as a whole, I agree that having helper functions offer more kwargs than the "main" ones is certainly odd. Since fft still always promotes types to the highest accuracy, I believe it's best if we also leave the helper functions in their current state; if at some point later on it's decided that a dtype kwarg should be offered throughout, then we can reopen an issue like that and apply such a change in all fft functions. Does that sound reasonable?

@cval26 cval26 closed this Aug 6, 2020
@cval26 cval26 deleted the fftfreq-dtype branch November 21, 2020 11:53
@hexane360
Copy link
Contributor

hexane360 commented Mar 10, 2025

It seems like we can revisit this now that numpy.fft supports single-precision (PR #25711). I would be interested in this mainly to have parity with other array API implementations that already support this (e.g. jax).

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.

ENH: Optional dtype for fftfreq/rfftfreq
5 participants