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

ENH: add dtype argument to fft.{fftfreq,rfftfreq} #249

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 27 additions & 5 deletions array_api_compat/common/_fft.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from typing import TYPE_CHECKING, Union, Optional, Literal

if TYPE_CHECKING:
from ._typing import Device, ndarray
from ._typing import Device, ndarray, DType
from collections.abc import Sequence

# Note: NumPy fft functions improperly upcast float32 and complex64 to
Expand Down Expand Up @@ -149,15 +149,37 @@ def ihfft(
return res.astype(xp.complex64)
return res

def fftfreq(n: int, /, xp, *, d: float = 1.0, device: Optional[Device] = None) -> ndarray:
def fftfreq(
n: int,
/,
xp,
*,
d: float = 1.0,
dtype: Optional[DType] = None,
device: Optional[Device] = None
) -> ndarray:
if device not in ["cpu", None]:
raise ValueError(f"Unsupported device {device!r}")
return xp.fft.fftfreq(n, d=d)
res = xp.fft.fftfreq(n, d=d)
if dtype is not None:
return res.astype(dtype)
return res

def rfftfreq(n: int, /, xp, *, d: float = 1.0, device: Optional[Device] = None) -> ndarray:
def rfftfreq(
n: int,
/,
xp,
*,
d: float = 1.0,
dtype: Optional[DType] = None,
device: Optional[Device] = None
) -> ndarray:
if device not in ["cpu", None]:
raise ValueError(f"Unsupported device {device!r}")
return xp.fft.rfftfreq(n, d=d)
res = xp.fft.rfftfreq(n, d=d)
if dtype is not None:
return res.astype(dtype)
return res

def fftshift(x: ndarray, /, xp, *, axes: Union[int, Sequence[int]] = None) -> ndarray:
return xp.fft.fftshift(x, axes=axes)
Expand Down
1 change: 1 addition & 0 deletions array_api_compat/common/_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,4 @@ def __len__(self, /) -> int: ...

Array = Any
Device = Any
DType = Any
Loading