Thanks to visit codestin.com
Credit goes to github.com

Skip to content

BUG: N-D FFTs fail when both out and s are given #28890

Open
@vtavana

Description

@vtavana

Describe the issue:

The following example for calculating numpy.fft.fftn fails.
N-D FFTs are calculated in a loop over axes, and when s is passed the shape of the intermediate result in each step will vary depending on the value of s. In this scenario, out which has the shape of final result cannot be used in intermediate steps.

It is expected that ifftn, rfftn to to fail in a similar scenario but irfftn will works fin since out is not used (because of its dtype) in intermediate steps.

Reproduce the code example:

import numpy
a = numpy.random.random((3,4,5)) + 1j * numpy.random.random((3,4,5))
res_without_out = numpy.fft.fftn(a, axes=(0, 1, 2), s=(7, 8, 9))
out = numpy.empty(res_without_out.shape, dtype=numpy.complex128)

res_with_out = numpy.fft.fftn(a, axes=(0, 1, 2), s=(7, 8, 9), out=out)

Error message:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Cell In[15], line 1
----> 1 res_with_out = numpy.fft.fftn(a, axes=(0, 1, 2), s=(7, 8, 9), out=out)

File /miniforge/envs/fft/lib/python3.10/site-packages/numpy/fft/_pocketfft.py:878, in fftn(a, s, axes, norm, out)
    749 @array_function_dispatch(_fftn_dispatcher)
    750 def fftn(a, s=None, axes=None, norm=None, out=None):
    751     """
    752     Compute the N-dimensional discrete Fourier Transform.
    753 
   (...)
    876 
    877     """
--> 878     return _raw_fftnd(a, s, axes, fft, norm, out=out)

File /miniforge/envs/fft/lib/python3.10/site-packages/numpy/fft/_pocketfft.py:741, in _raw_fftnd(a, s, axes, function, norm, out)
    739 itl.reverse()
    740 for ii in itl:
--> 741     a = function(a, n=s[ii], axis=axes[ii], norm=norm, out=out)
    742 return a

File miniforge/envs/fft/lib/python3.10/site-packages/numpy/fft/_pocketfft.py:209, in fft(a, n, axis, norm, out)
    207 if n is None:
    208     n = a.shape[axis]
--> 209 output = _raw_fft(a, n, axis, False, True, norm, out)
    210 return output

File miniforge/envs/fft/lib/python3.10/site-packages/numpy/fft/_pocketfft.py:94, in _raw_fft(a, n, axis, is_real, is_forward, norm, out)
     90 elif ((shape := getattr(out, "shape", None)) is not None
     91       and (len(shape) != a.ndim or shape[axis] != n_out)):
     92     raise ValueError("output array has wrong shape.")
---> 94 return ufunc(a, fct, axes=[(axis,), (), (axis,)], out=out)

ValueError: operands could not be broadcast together with remapped shapes [original->remapped]: (3,4,5)->(3,4,newaxis) ()->() (7,8,9)->(7,8,9)  and requested shape (9)

Python and NumPy Versions:

2.2.3
3.10.16 | packaged by conda-forge | (main, Dec 5 2024, 14:16:10) [GCC 13.3.0]

Runtime Environment:

No response

Context for the issue:

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions