Skip to content

BUG: behavioral change of numpy.fft.rfft with numpy 2.x #26349

Closed

Description

@serge-sans-paille

Describe the issue:

With numpy 2.x:

% python -c 'import numpy as np; print(np.version.version); x = np.arange(8.) ; print(np.fft.rfft(x, 4))' 
2.1.0.dev0+git20240423.ef5f10d
[ 6.+0.j -2.+2.j -2.+4.j]

with numpy 1.26.4:

% python -c 'import numpy as np; print(np.version.version); x = np.arange(8.) ; print(np.fft.rfft(x, 4))'
1.26.4
[ 6.+0.j -2.+2.j -2.+0.j]

that's a notable output difference, maybe introduced by the vectorized form of pocketfft?

Reproduce the code example:

import numpy as np
x = np.arange(8.)
print(np.fft.rfft(x, 4))

Error message:

No response

Python and NumPy Versions:

2.1.0.dev0+git20240423.ef5f10d
3.12.2 (main, Feb 21 2024, 00:00:00) [GCC 13.2.1 20231205 (Red Hat 13.2.1-6)]

Runtime Environment:

No response

Context for the issue:

Difference found while porting pythran to numpy 2.x, as pythran mimics the 'legacy' behavior.
I'm unsure whether the new behavior is intended or not, at least it's not documented in the git log as far as I can tell (?)

Activity

  1. seberg commented on Apr 26, 2024

    @seberg
    Member

    @mhvk is maybe another one of those "FFT seems simple but is not?" off-by-one errors. I think the SciPy fft returns the same as NumPy used to here, so it seems plausible (didn't try to understand yet what is going on, maybe it makes sense).

  2. added this to the 2.0.0 release milestone on Apr 26, 2024
  3. whym1here commented on Apr 26, 2024

    @whym1here

    Is this intended behavior?

  4. WarrenWeckesser commented on Apr 26, 2024

    @WarrenWeckesser
    Member

    @whym1here, no, it is a bug. When n is even, the imaginary part of the last coefficient returned by rfft must be zero.

    The equivalent calculation using fft is

    In [39]: x = np.arange(8)
    
    In [40]: n = 4
    
    In [41]: np.fft.fft(x[:n])[:n//2 + 1]
    Out[41]: array([ 6.+0.j, -2.+2.j, -2.+0.j])
    
  5. WarrenWeckesser commented on Apr 26, 2024

    @WarrenWeckesser
    Member

    I have a possible fix in #26354.

  6. pijyoi commented on Apr 27, 2024

    @pijyoi
    Contributor

    There's an over-copy of the 5th input element when only a shorter length fft was required.

    >>> np.fft.rfft(np.array([0,1,2,3]), 4)
    array([ 6.+0.j, -2.+2.j, -2.+0.j])
    
    >>> np.fft.rfft(np.array([0,1,2,3,123]), 4)
    array([ 6.  +0.j, -2.  +2.j, -2.+123.j])
    
    >>> np.fft.rfft(np.array([0,1,2,3,123])[:4], 4)
    array([ 6.+0.j, -2.+2.j, -2.+0.j])
  7. added a commit that references this issue on Apr 27, 2024
    051f734
  8. added a commit that references this issue on Apr 27, 2024
    63a4f61
  9. serge-sans-paille commented on Apr 27, 2024

    @serge-sans-paille
    ContributorAuthor
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions