8000 ENH: add `norm=forward,backward` to numpy.fft functions by cval26 · Pull Request #16476 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

ENH: add norm=forward,backward to numpy.fft functions #16476

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 16 commits into from
Jul 12, 2020
Merged
Show file tree
Hide file tree
Changes from 6 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
145 changes: 79 additions & 66 deletions numpy/fft/_pocketfft.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
"""
Discrete Fourier Transforms
Discrete Fourier Transforms.

Routines in this module:

fft(a, n=None, axis=-1)
ifft(a, n=None, axis=-1)
rfft(a, n=None, axis=-1)
irfft(a, n=None, axis=-1)
hfft(a, n=None, axis=-1)
ihfft(a, n=None, axis=-1)
fftn(a, s=None, axes=None)
ifftn(a, s=None, axes=None)
rfftn(a, s=None, axes=None)
irfftn(a, s=None, axes=None)
fft2(a, s=None, axes=(-2,-1))
ifft2(a, s=None, axes=(-2, -1))
rfft2(a, s=None, axes=(-2,-1))
irfft2(a, s=None, axes=(-2, -1))
fft(a, n=None, axis=-1, norm=None)
ifft(a, n=None, axis=-1, norm=None)
rfft(a, n=None, axis=-1, norm=None)
irfft(a, n=None, axis=-1, norm=None)
hfft(a, n=None, axis=-1, norm=None)
ihfft(a, n=None, axis=-1, norm=None)
fftn(a, s=None, axes=None, norm=None)
ifftn(a, s=None, axes=None, norm=None)
rfftn(a, s=None, axes=None, norm=None)
irfftn(a, s=None, axes=None, norm=None)
fft2(a, s=None, axes=(-2,-1), norm=None)
ifft2(a, s=None, axes=(-2, -1), norm=None)
rfft2(a, s=None, axes=(-2,-1), norm=None)
irfft2(a, s=None, axes=(-2, -1), norm=None)

i = inverse transform
r = transform of purely real data
Expand Down Expand Up @@ -80,12 +80,12 @@ def _raw_fft(a, n, axis, is_real, is_forward, inv_norm):


def _unitary(norm):
if norm is None:
return False
if norm=="ortho":
if norm == "ortho":
return True
raise ValueError("Invalid norm value %s, should be None or \"ortho\"."
% norm)
if norm is None or norm == "inverse":
return False
raise ValueError("Invalid norm value %s; should be None, \"ortho\" or\
\"inverse\"." % norm)


def _fft_dispatcher(a, n=None, axis=None, norm=None):
Expand Down Expand Up @@ -113,9 +113,9 @@ def fft(a, n=None, axis=-1, norm=None):
axis : int, optional
Axis over which to compute the FFT. If not given, the last axis is
used.
norm : {None, "ortho"}, optional
norm : {None, "ortho", "inverse"}, optional
.. versionadded:: 1.10.0

.. versionadded:: 1.20.0
Normalization mode (see `numpy.fft`). Default is None.

Returns
Expand Down Expand Up @@ -180,8 +180,10 @@ def fft(a, n=None, axis=-1, norm=None):
if n is None:
n = a.shape[axis]
inv_norm = 1
if norm is not None and _unitary(norm):
if _unitary(norm):
inv_norm = sqrt(n)
elif norm == "inverse":
inv_norm = n
output = _raw_fft(a, n, axis, False, True, inv_norm)
return output

Expand Down Expand Up @@ -222,9 +224,9 @@ def ifft(a, n=None, axis=-1, norm=None):
axis : int, optional
Axis over which to compute the inverse DFT. If not given, the last
axis is used.
norm : {None, "ortho"}, optional
norm : {None, "ortho", "inverse"}, optional
.. versionadded:: 1.10.0

.. versionadded:: 1.20.0
Normalization mode (see `numpy.fft`). Default is None.

Returns
Expand Down Expand Up @@ -274,15 +276,15 @@ def ifft(a, n=None, axis=-1, norm=None):
a = asarray(a)
if n is None:
n = a.shape[axis]
if norm is not None and _unitary(norm):
inv_norm = sqrt(max(n, 1))
else:
inv_norm = n
inv_norm = n
if _unitary(norm):
inv_norm = sqrt(n)
elif norm == "inverse":
inv_norm = 1
output = _raw_fft(a, n, axis, False, False, inv_norm)
return output



@array_function_dispatch(_fft_dispatcher)
def rfft(a, n=None, axis=-1, norm=None):
"""
Expand All @@ -304,9 +306,9 @@ def rfft(a, n=None, axis=-1, norm=None):
axis : int, optional
Axis over which to compute the FFT. If not given, the last axis is
used.
norm : {None, "ortho"}, optional
norm : {None, "ortho", "inverse"}, optional
.. versionadded:: 1.10.0

.. versionadded:: 1.20.0
Normalization mode (see `numpy.fft`). Default is None.

Returns
Expand Down Expand Up @@ -363,11 +365,13 @@ def rfft(a, n=None, axis=-1, norm=None):

"""
a = asarray(a)
if n is None:
n = a.shape[axis]
inv_norm = 1
if norm is not None and _unitary(norm):
if n is None:
n = a.shape[axis]
if _unitary(norm):
inv_norm = sqrt(n)
elif norm == "inverse":
inv_norm = n
output = _raw_fft(a, n, axis, True, True, inv_norm)
return output

Expand Down Expand Up @@ -402,9 +406,9 @@ def irfft(a, n=None, axis=-1, norm=None):
axis : int, optional
Axis over which to compute the inverse FFT. If not given, the last
axis is used.
norm : {None, "ortho"}, optional
norm : {None, "ortho", "inverse"}, optional
.. versionadded:: 1.10.0

.. versionadded:: 1.20.0
Normalization mode (see `numpy.fft`). Default is None.

Returns
Expand Down Expand Up @@ -466,8 +470,10 @@ def irfft(a, n=None, axis=-1, norm=None):
if n is None:
n = (a.shape[axis] - 1) * 2
inv_norm = n
if norm is not None and _unitary(norm):
if _unitary(norm):
inv_norm = sqrt(n)
elif norm == "inverse":
inv_norm = 1
output = _raw_fft(a, n, axis, True, False, inv_norm)
return output

Expand All @@ -492,10 +498,10 @@ def hfft(a, n=None, axis=-1, norm=None):
axis : int, optional
Axis over which to compute the FFT. If not given, the last
axis is used.
norm : {None, "ortho"}, optional
Normalization mode (see `numpy.fft`). Default is None.

norm : {None, "ortho", "inverse"}, optional
.. versionadded:: 1.10.0
.. versionadded:: 1.20.0
Normalization mode (see `numpy.fft`). Default is None.

Returns
-------
Expand Down Expand Up @@ -559,8 +565,12 @@ def hfft(a, n=None, axis=-1, norm=None):
a = asarray(a)
if n is None:
n = (a.shape[axis] - 1) * 2
unitary = _unitary(norm)
return irfft(conjugate(a), n, axis) * (sqrt(n) if unitary else n)
output = irfft(conjugate(a), n, axis)
if _unitary(norm):
output *= sqrt(n)
elif norm is None:
output *= n
return output


@array_function_dispatch(_fft_dispatcher)
Expand All @@ -581,10 +591,10 @@ def ihfft(a, n=None, axis=-1, norm=None):
axis : int, optional
Axis over which to compute the inverse FFT. If not given, the last
axis is used.
norm : {None, "ortho"}, optional
Normalization mode (see `numpy.fft`). Default is None.

norm : {None, "ortho", "inverse"}, optional
.. versionadded:: 1.10.0
.. versionadded:: 1.20.0
Normalization mode (see `numpy.fft`). Default is None.

Returns
-------
Expand Down Expand Up @@ -619,9 +629,12 @@ def ihfft(a, n=None, axis=-1, norm=None):
a = asarray(a)
if n is None:
n = a.shape[axis]
unitary = _unitary(norm)
output = conjugate(rfft(a, n, axis))
return output * (1 / (sqrt(n) if unitary else n))
if _unitary(norm):
output *= 1./sqrt(n)
elif norm is None:
output *= 1./n
return output


def _cook_nd_args(a, s=None, axes=None, invreal=0):
Expand Down Expand Up @@ -683,9 +696,9 @@ def fftn(a, s=None, axes=None, norm=None):
axes are used, or all axes if `s` is also not specified.
Repeated indices in `axes` means that the transform over that axis is
performed multiple times.
norm : {None, "ortho"}, optional
norm : {None, "ortho", "inverse"}, optional
.. versionadded:: 1.10.0

.. versionadded:: 1.20.0
Normalization mode (see `numpy.fft`). Default is None.

Returns
Expand Down Expand Up @@ -790,9 +803,9 @@ def ifftn(a, s=None, axes=None, norm=None):
axes are used, or all axes if `s` is also not specified.
Repeated indices in `axes` means that the inverse transform over that
axis is performed multiple times.
norm : {None, "ortho"}, optional
norm : {None, "ortho", "inverse"}, optional
.. versionadded:: 1.10.0

.. versionadded:: 1.20.0
Normalization mode (see `numpy.fft`). Default is None.

Returns
Expand Down Expand Up @@ -880,9 +893,9 @@ def fft2(a, s=None, axes=(-2, -1), norm=None):
axes are used. A repeated index in `axes` means the transform over
that axis is performed multiple times. A one-element sequence means
that a one-dimensional FFT is performed.
norm : {None, "ortho"}, optional
norm : {None, "ortho", "inverse"}, optional
.. versionadded:: 1.10.0

.. versionadded:: 1.20.0
Normalization mode (see `numpy.fft`). Default is None.

Returns
Expand Down Expand Up @@ -978,9 +991,9 @@ def ifft2(a, s=None, axes=(-2, -1), norm=None):
axes are used. A repeated index in `axes` means the transform over
that axis is performed multiple times. A one-element sequence means
that a one-dimensional FFT is performed.
norm : {None, "ortho"}, optional
norm : {None, "ortho", "inverse"}, optional
.. versionadded:: 1.10.0

.. versionadded:: 1.20.0
Normalization mode (see `numpy.fft`). Default is None.

Returns
Expand Down FC38 Expand Up @@ -1059,9 +1072,9 @@ def rfftn(a, s=None, axes=None, norm=None):
axes : sequence of ints, optional
Axes over which to compute the FFT. If not given, the last ``len(s)``
axes are used, or all axes if `s` is also not specified.
norm : {None, "ortho"}, optional
norm : {None, "ortho", "inverse"}, optional
.. versionadded:: 1.10.0

.. versionadded:: 1.20.0
Normalization mode (see `numpy.fft`). Default is None.

Returns
Expand Down Expand Up @@ -1137,9 +1150,9 @@ def rfft2(a, s=None, axes=(-2, -1), norm=None):
Shape of the FFT.
axes : sequence of ints, optional
Axes over which to compute the FFT.
norm : {None, "ortho"}, optional
norm : {None, "ortho", "inverse"}, optional
.. versionadded:: 1.10.0

.. versionadded:: 1.20.0
Normalization mode (see `numpy.fft`). Default is None.

Returns
Expand Down Expand Up @@ -1190,16 +1203,16 @@ def irfftn(a, s=None, axes=None, norm=None):
Along any axis, if the shape indicated by `s` is smaller than that of
the input, the input is cropped. If it is larger, the input is padded
with zeros. If `s` is not given, the shape of the input along the axes
specified by axes is used. Except for the last axis which is taken to be
``2*(m-1)`` where ``m`` is the length of the input along that axis.
specified by axes is used. Except for the last axis which is taken to
be ``2*(m-1)`` where ``m`` is the length of the input along that axis.
axes : sequence of ints, optional
Axes over which to compute the inverse FFT. If not given, the last
`len(s)` axes are used, or all axes if `s` is also not specified.
Repeated indices in `axes` means that the inverse transform over that
axis is performed multiple times.
norm : {None, "ortho"}, optional
norm : {None, "ortho", "inverse"}, optional
.. versionadded:: 1.10.0

.. versionadded:: 1.20.0
Normalization mode (see `numpy.fft`). Default is None.

Returns
Expand Down Expand Up @@ -1280,9 +1293,9 @@ def irfft2(a, s=None, axes=(-2, -1), norm=None):
axes : sequence of ints, optional
The axes over which to compute the inverse fft.
Default is the last two axes.
norm : {None, "ortho"}, optional
norm : {None, "ortho", "inverse"}, optional
.. versionadded:: 1.10.0

.. versionadded:: 1.20.0
Normalization mode (see `numpy.fft`). Default is None.

Returns
Expand Down
Loading
0